i82365.c revision 1.67 1 /* $NetBSD: i82365.c,v 1.67 2002/09/27 03:18:11 thorpej Exp $ */
2
3 /*
4 * Copyright (c) 2000 Christian E. Hopps. All rights reserved.
5 * Copyright (c) 1997 Marc Horowitz. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by Marc Horowitz.
18 * 4. The name of the author may not be used to endorse or promote products
19 * derived from this software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 */
32
33 #include <sys/cdefs.h>
34 __KERNEL_RCSID(0, "$NetBSD: i82365.c,v 1.67 2002/09/27 03:18:11 thorpej Exp $");
35
36 #define PCICDEBUG
37
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/device.h>
41 #include <sys/extent.h>
42 #include <sys/kernel.h>
43 #include <sys/malloc.h>
44 #include <sys/kthread.h>
45
46 #include <machine/bus.h>
47 #include <machine/intr.h>
48
49 #include <dev/pcmcia/pcmciareg.h>
50 #include <dev/pcmcia/pcmciavar.h>
51
52 #include <dev/ic/i82365reg.h>
53 #include <dev/ic/i82365var.h>
54
55 #include "locators.h"
56
57 #ifdef PCICDEBUG
58 int pcic_debug = 0;
59 #define DPRINTF(arg) if (pcic_debug) printf arg;
60 #else
61 #define DPRINTF(arg)
62 #endif
63
64 /*
65 * Individual drivers will allocate their own memory and io regions. Memory
66 * regions must be a multiple of 4k, aligned on a 4k boundary.
67 */
68
69 #define PCIC_MEM_ALIGN PCIC_MEM_PAGESIZE
70
71 void pcic_attach_socket __P((struct pcic_handle *));
72 void pcic_attach_socket_finish __P((struct pcic_handle *));
73
74 int pcic_submatch __P((struct device *, struct cfdata *, void *));
75 int pcic_print __P((void *arg, const char *pnp));
76 int pcic_intr_socket __P((struct pcic_handle *));
77 void pcic_poll_intr __P((void *));
78
79 void pcic_attach_card __P((struct pcic_handle *));
80 void pcic_detach_card __P((struct pcic_handle *, int));
81 void pcic_deactivate_card __P((struct pcic_handle *));
82
83 void pcic_chip_do_mem_map __P((struct pcic_handle *, int));
84 void pcic_chip_do_io_map __P((struct pcic_handle *, int));
85
86 void pcic_create_event_thread __P((void *));
87 void pcic_event_thread __P((void *));
88
89 void pcic_queue_event __P((struct pcic_handle *, int));
90 void pcic_power __P((int, void *));
91
92 static void pcic_wait_ready __P((struct pcic_handle *));
93 static void pcic_delay __P((struct pcic_handle *, int, const char *));
94
95 static u_int8_t st_pcic_read __P((struct pcic_handle *, int));
96 static void st_pcic_write __P((struct pcic_handle *, int, u_int8_t));
97
98 int
99 pcic_ident_ok(ident)
100 int ident;
101 {
102 /* this is very empirical and heuristic */
103
104 if ((ident == 0) || (ident == 0xff) || (ident & PCIC_IDENT_ZERO))
105 return (0);
106
107 if ((ident & PCIC_IDENT_IFTYPE_MASK) != PCIC_IDENT_IFTYPE_MEM_AND_IO) {
108 #ifdef DIAGNOSTIC
109 printf("pcic: does not support memory and I/O cards, "
110 "ignored (ident=%0x)\n", ident);
111 #endif
112 return (0);
113 }
114 return (1);
115 }
116
117 int
118 pcic_vendor(h)
119 struct pcic_handle *h;
120 {
121 int reg;
122
123 /*
124 * the chip_id of the cirrus toggles between 11 and 00 after a write.
125 * weird.
126 */
127
128 pcic_write(h, PCIC_CIRRUS_CHIP_INFO, 0);
129 reg = pcic_read(h, -1);
130
131 if ((reg & PCIC_CIRRUS_CHIP_INFO_CHIP_ID) ==
132 PCIC_CIRRUS_CHIP_INFO_CHIP_ID) {
133 reg = pcic_read(h, -1);
134 if ((reg & PCIC_CIRRUS_CHIP_INFO_CHIP_ID) == 0) {
135 if (reg & PCIC_CIRRUS_CHIP_INFO_SLOTS)
136 return (PCIC_VENDOR_CIRRUS_PD672X);
137 else
138 return (PCIC_VENDOR_CIRRUS_PD6710);
139 }
140 }
141
142 reg = pcic_read(h, PCIC_IDENT);
143
144 if ((reg & PCIC_IDENT_REV_MASK) == PCIC_IDENT_REV_I82365SLR0)
145 return (PCIC_VENDOR_I82365SLR0);
146 else
147 return (PCIC_VENDOR_I82365SLR1);
148
149 return (PCIC_VENDOR_UNKNOWN);
150 }
151
152 char *
153 pcic_vendor_to_string(vendor)
154 int vendor;
155 {
156 switch (vendor) {
157 case PCIC_VENDOR_I82365SLR0:
158 return ("Intel 82365SL Revision 0");
159 case PCIC_VENDOR_I82365SLR1:
160 return ("Intel 82365SL Revision 1");
161 case PCIC_VENDOR_CIRRUS_PD6710:
162 return ("Cirrus PD6710");
163 case PCIC_VENDOR_CIRRUS_PD672X:
164 return ("Cirrus PD672X");
165 }
166
167 return ("Unknown controller");
168 }
169
170 void
171 pcic_attach(sc)
172 struct pcic_softc *sc;
173 {
174 int i, reg, chip, socket, intr;
175 struct pcic_handle *h;
176
177 DPRINTF(("pcic ident regs:"));
178
179 lockinit(&sc->sc_pcic_lock, PWAIT, "pciclk", 0, 0);
180
181 /* find and configure for the available sockets */
182 for (i = 0; i < PCIC_NSLOTS; i++) {
183 h = &sc->handle[i];
184 chip = i / 2;
185 socket = i % 2;
186
187 h->ph_parent = (struct device *)sc;
188 h->chip = chip;
189 h->sock = chip * PCIC_CHIP_OFFSET + socket * PCIC_SOCKET_OFFSET;
190 h->laststate = PCIC_LASTSTATE_EMPTY;
191 /* initialize pcic_read and pcic_write functions */
192 h->ph_read = st_pcic_read;
193 h->ph_write = st_pcic_write;
194 h->ph_bus_t = sc->iot;
195 h->ph_bus_h = sc->ioh;
196
197 /* need to read vendor -- for cirrus to report no xtra chip */
198 if (socket == 0)
199 h->vendor = (h+1)->vendor = pcic_vendor(h);
200
201 /*
202 * During the socket probe, read the ident register twice.
203 * I don't understand why, but sometimes the clone chips
204 * in hpcmips boxes read all-0s the first time. -- mycroft
205 */
206 reg = pcic_read(h, PCIC_IDENT);
207 reg = pcic_read(h, PCIC_IDENT);
208 DPRINTF(("ident reg 0x%02x\n", reg));
209 if (pcic_ident_ok(reg))
210 h->flags = PCIC_FLAG_SOCKETP;
211 else
212 h->flags = 0;
213 }
214
215 for (i = 0; i < PCIC_NSLOTS; i++) {
216 h = &sc->handle[i];
217
218 if (h->flags & PCIC_FLAG_SOCKETP) {
219 SIMPLEQ_INIT(&h->events);
220
221 /* disable interrupts -- for now */
222 pcic_write(h, PCIC_CSC_INTR, 0);
223 intr = pcic_read(h, PCIC_INTR);
224 DPRINTF(("intr was 0x%02x\n", intr));
225 intr &= ~(PCIC_INTR_RI_ENABLE | PCIC_INTR_ENABLE |
226 PCIC_INTR_IRQ_MASK);
227 pcic_write(h, PCIC_INTR, intr);
228 (void) pcic_read(h, PCIC_CSC);
229 }
230 }
231
232 /* print detected info */
233 for (i = 0; i < PCIC_NSLOTS; i += 2) {
234 h = &sc->handle[i];
235 chip = i / 2;
236
237 printf("%s: controller %d (%s) has ", sc->dev.dv_xname, chip,
238 pcic_vendor_to_string(sc->handle[i].vendor));
239
240 if ((h->flags & PCIC_FLAG_SOCKETP) &&
241 ((h+1)->flags & PCIC_FLAG_SOCKETP))
242 printf("sockets A and B\n");
243 else if (h->flags & PCIC_FLAG_SOCKETP)
244 printf("socket A only\n");
245 else if ((h+1)->flags & PCIC_FLAG_SOCKETP)
246 printf("socket B only\n");
247 else
248 printf("no sockets\n");
249 }
250 }
251
252 /*
253 * attach the sockets before we know what interrupts we have
254 */
255 void
256 pcic_attach_sockets(sc)
257 struct pcic_softc *sc;
258 {
259 int i;
260
261 for (i = 0; i < PCIC_NSLOTS; i++)
262 if (sc->handle[i].flags & PCIC_FLAG_SOCKETP)
263 pcic_attach_socket(&sc->handle[i]);
264 }
265
266 void
267 pcic_power(why, arg)
268 int why;
269 void *arg;
270 {
271 struct pcic_handle *h = (struct pcic_handle *)arg;
272 struct pcic_softc *sc = (struct pcic_softc *)h->ph_parent;
273 int reg;
274
275 DPRINTF(("%s: power: why %d\n", h->ph_parent->dv_xname, why));
276
277 if (h->flags & PCIC_FLAG_SOCKETP) {
278 if ((why == PWR_RESUME) &&
279 (pcic_read(h, PCIC_CSC_INTR) == 0)) {
280 #ifdef PCICDEBUG
281 char bitbuf[64];
282 #endif
283 reg = PCIC_CSC_INTR_CD_ENABLE;
284 if (sc->irq != -1)
285 reg |= sc->irq << PCIC_CSC_INTR_IRQ_SHIFT;
286 pcic_write(h, PCIC_CSC_INTR, reg);
287 DPRINTF(("%s: CSC_INTR was zero; reset to %s\n",
288 sc->dev.dv_xname,
289 bitmask_snprintf(pcic_read(h, PCIC_CSC_INTR),
290 PCIC_CSC_INTR_FORMAT,
291 bitbuf, sizeof(bitbuf))));
292 }
293
294 /*
295 * check for card insertion or removal during suspend period.
296 * XXX: the code can't cope with card swap (remove then insert).
297 * how can we detect such situation?
298 */
299 if (why == PWR_RESUME)
300 (void)pcic_intr_socket(h);
301 }
302 }
303
304
305 /*
306 * attach a socket -- we don't know about irqs yet
307 */
308 void
309 pcic_attach_socket(h)
310 struct pcic_handle *h;
311 {
312 struct pcmciabus_attach_args paa;
313 struct pcic_softc *sc = (struct pcic_softc *)h->ph_parent;
314
315 /* initialize the rest of the handle */
316
317 h->shutdown = 0;
318 h->memalloc = 0;
319 h->ioalloc = 0;
320 h->ih_irq = 0;
321
322 /* now, config one pcmcia device per socket */
323
324 paa.paa_busname = "pcmcia";
325 paa.pct = (pcmcia_chipset_tag_t) sc->pct;
326 paa.pch = (pcmcia_chipset_handle_t) h;
327 paa.iobase = sc->iobase;
328 paa.iosize = sc->iosize;
329
330 h->pcmcia = config_found_sm(&sc->dev, &paa, pcic_print, pcic_submatch);
331 if (h->pcmcia == NULL) {
332 h->flags &= ~PCIC_FLAG_SOCKETP;
333 return;
334 }
335
336 /*
337 * queue creation of a kernel thread to handle insert/removal events.
338 */
339 #ifdef DIAGNOSTIC
340 if (h->event_thread != NULL)
341 panic("pcic_attach_socket: event thread");
342 #endif
343 config_pending_incr();
344 kthread_create(pcic_create_event_thread, h);
345 }
346
347 /*
348 * now finish attaching the sockets, we are ready to allocate
349 * interrupts
350 */
351 void
352 pcic_attach_sockets_finish(sc)
353 struct pcic_softc *sc;
354 {
355 int i;
356
357 for (i = 0; i < PCIC_NSLOTS; i++)
358 if (sc->handle[i].flags & PCIC_FLAG_SOCKETP)
359 pcic_attach_socket_finish(&sc->handle[i]);
360 }
361
362 /*
363 * finishing attaching the socket. Interrupts may now be on
364 * if so expects the pcic interrupt to be blocked
365 */
366 void
367 pcic_attach_socket_finish(h)
368 struct pcic_handle *h;
369 {
370 struct pcic_softc *sc = (struct pcic_softc *)h->ph_parent;
371 int reg, intr;
372
373 DPRINTF(("%s: attach finish socket %ld\n", h->ph_parent->dv_xname,
374 (long) (h - &sc->handle[0])));
375
376 /*
377 * Set up a powerhook to ensure it continues to interrupt on
378 * card detect even after suspend.
379 * (this works around a bug seen in suspend-to-disk on the
380 * Sony VAIO Z505; on resume, the CSC_INTR state is not preserved).
381 */
382 powerhook_establish(pcic_power, h);
383
384 /* enable interrupts on card detect, poll for them if no irq avail */
385 reg = PCIC_CSC_INTR_CD_ENABLE;
386 if (sc->irq == -1) {
387 if (sc->poll_established == 0) {
388 callout_init(&sc->poll_ch);
389 callout_reset(&sc->poll_ch, hz / 2, pcic_poll_intr, sc);
390 sc->poll_established = 1;
391 }
392 } else
393 reg |= sc->irq << PCIC_CSC_INTR_IRQ_SHIFT;
394 pcic_write(h, PCIC_CSC_INTR, reg);
395
396 /* steer above mgmt interrupt to configured place */
397 intr = pcic_read(h, PCIC_INTR);
398 intr &= ~(PCIC_INTR_IRQ_MASK | PCIC_INTR_ENABLE);
399 pcic_write(h, PCIC_INTR, intr);
400
401 /* power down the socket */
402 pcic_write(h, PCIC_PWRCTL, 0);
403
404 /* zero out the address windows */
405 pcic_write(h, PCIC_ADDRWIN_ENABLE, 0);
406
407 /* clear possible card detect interrupt */
408 pcic_read(h, PCIC_CSC);
409
410 DPRINTF(("%s: attach finish vendor 0x%02x\n", h->ph_parent->dv_xname,
411 h->vendor));
412
413 /* unsleep the cirrus controller */
414 if ((h->vendor == PCIC_VENDOR_CIRRUS_PD6710) ||
415 (h->vendor == PCIC_VENDOR_CIRRUS_PD672X)) {
416 reg = pcic_read(h, PCIC_CIRRUS_MISC_CTL_2);
417 if (reg & PCIC_CIRRUS_MISC_CTL_2_SUSPEND) {
418 DPRINTF(("%s: socket %02x was suspended\n",
419 h->ph_parent->dv_xname, h->sock));
420 reg &= ~PCIC_CIRRUS_MISC_CTL_2_SUSPEND;
421 pcic_write(h, PCIC_CIRRUS_MISC_CTL_2, reg);
422 }
423 }
424
425 /* if there's a card there, then attach it. */
426 reg = pcic_read(h, PCIC_IF_STATUS);
427 if ((reg & PCIC_IF_STATUS_CARDDETECT_MASK) ==
428 PCIC_IF_STATUS_CARDDETECT_PRESENT) {
429 pcic_queue_event(h, PCIC_EVENT_INSERTION);
430 h->laststate = PCIC_LASTSTATE_PRESENT;
431 } else {
432 h->laststate = PCIC_LASTSTATE_EMPTY;
433 }
434 }
435
436 void
437 pcic_create_event_thread(arg)
438 void *arg;
439 {
440 struct pcic_handle *h = arg;
441 const char *cs;
442
443 switch (h->sock) {
444 case C0SA:
445 cs = "0,0";
446 break;
447 case C0SB:
448 cs = "0,1";
449 break;
450 case C1SA:
451 cs = "1,0";
452 break;
453 case C1SB:
454 cs = "1,1";
455 break;
456 default:
457 panic("pcic_create_event_thread: unknown pcic socket");
458 }
459
460 if (kthread_create1(pcic_event_thread, h, &h->event_thread,
461 "%s,%s", h->ph_parent->dv_xname, cs)) {
462 printf("%s: unable to create event thread for sock 0x%02x\n",
463 h->ph_parent->dv_xname, h->sock);
464 panic("pcic_create_event_thread");
465 }
466 }
467
468 void
469 pcic_event_thread(arg)
470 void *arg;
471 {
472 struct pcic_handle *h = arg;
473 struct pcic_event *pe;
474 int s, first = 1;
475 struct pcic_softc *sc = (struct pcic_softc *)h->ph_parent;
476
477 while (h->shutdown == 0) {
478 /*
479 * Serialize event processing on the PCIC. We may
480 * sleep while we hold this lock.
481 */
482 (void) lockmgr(&sc->sc_pcic_lock, LK_EXCLUSIVE, NULL);
483
484 s = splhigh();
485 if ((pe = SIMPLEQ_FIRST(&h->events)) == NULL) {
486 splx(s);
487 if (first) {
488 first = 0;
489 config_pending_decr();
490 }
491 /*
492 * No events to process; release the PCIC lock.
493 */
494 (void) lockmgr(&sc->sc_pcic_lock, LK_RELEASE, NULL);
495 (void) tsleep(&h->events, PWAIT, "pcicev", 0);
496 continue;
497 } else {
498 splx(s);
499 /* sleep .25s to be enqueued chatterling interrupts */
500 (void) tsleep((caddr_t)pcic_event_thread, PWAIT,
501 "pcicss", hz/4);
502 }
503 s = splhigh();
504 SIMPLEQ_REMOVE_HEAD(&h->events, pe_q);
505 splx(s);
506
507 switch (pe->pe_type) {
508 case PCIC_EVENT_INSERTION:
509 s = splhigh();
510 while (1) {
511 struct pcic_event *pe1, *pe2;
512
513 if ((pe1 = SIMPLEQ_FIRST(&h->events)) == NULL)
514 break;
515 if (pe1->pe_type != PCIC_EVENT_REMOVAL)
516 break;
517 if ((pe2 = SIMPLEQ_NEXT(pe1, pe_q)) == NULL)
518 break;
519 if (pe2->pe_type == PCIC_EVENT_INSERTION) {
520 SIMPLEQ_REMOVE_HEAD(&h->events, pe_q);
521 free(pe1, M_TEMP);
522 SIMPLEQ_REMOVE_HEAD(&h->events, pe_q);
523 free(pe2, M_TEMP);
524 }
525 }
526 splx(s);
527
528 DPRINTF(("%s: insertion event\n",
529 h->ph_parent->dv_xname));
530 pcic_attach_card(h);
531 break;
532
533 case PCIC_EVENT_REMOVAL:
534 s = splhigh();
535 while (1) {
536 struct pcic_event *pe1, *pe2;
537
538 if ((pe1 = SIMPLEQ_FIRST(&h->events)) == NULL)
539 break;
540 if (pe1->pe_type != PCIC_EVENT_INSERTION)
541 break;
542 if ((pe2 = SIMPLEQ_NEXT(pe1, pe_q)) == NULL)
543 break;
544 if (pe2->pe_type == PCIC_EVENT_REMOVAL) {
545 SIMPLEQ_REMOVE_HEAD(&h->events, pe_q);
546 free(pe1, M_TEMP);
547 SIMPLEQ_REMOVE_HEAD(&h->events, pe_q);
548 free(pe2, M_TEMP);
549 }
550 }
551 splx(s);
552
553 DPRINTF(("%s: removal event\n",
554 h->ph_parent->dv_xname));
555 pcic_detach_card(h, DETACH_FORCE);
556 break;
557
558 default:
559 panic("pcic_event_thread: unknown event %d",
560 pe->pe_type);
561 }
562 free(pe, M_TEMP);
563
564 (void) lockmgr(&sc->sc_pcic_lock, LK_RELEASE, NULL);
565 }
566
567 h->event_thread = NULL;
568
569 /* In case parent is waiting for us to exit. */
570 wakeup(sc);
571
572 kthread_exit(0);
573 }
574
575 int
576 pcic_submatch(parent, cf, aux)
577 struct device *parent;
578 struct cfdata *cf;
579 void *aux;
580 {
581
582 struct pcmciabus_attach_args *paa = aux;
583 struct pcic_handle *h = (struct pcic_handle *) paa->pch;
584
585 switch (h->sock) {
586 case C0SA:
587 if (cf->cf_loc[PCMCIABUSCF_CONTROLLER] !=
588 PCMCIABUSCF_CONTROLLER_DEFAULT &&
589 cf->cf_loc[PCMCIABUSCF_CONTROLLER] != 0)
590 return 0;
591 if (cf->cf_loc[PCMCIABUSCF_SOCKET] !=
592 PCMCIABUSCF_SOCKET_DEFAULT &&
593 cf->cf_loc[PCMCIABUSCF_SOCKET] != 0)
594 return 0;
595
596 break;
597 case C0SB:
598 if (cf->cf_loc[PCMCIABUSCF_CONTROLLER] !=
599 PCMCIABUSCF_CONTROLLER_DEFAULT &&
600 cf->cf_loc[PCMCIABUSCF_CONTROLLER] != 0)
601 return 0;
602 if (cf->cf_loc[PCMCIABUSCF_SOCKET] !=
603 PCMCIABUSCF_SOCKET_DEFAULT &&
604 cf->cf_loc[PCMCIABUSCF_SOCKET] != 1)
605 return 0;
606
607 break;
608 case C1SA:
609 if (cf->cf_loc[PCMCIABUSCF_CONTROLLER] !=
610 PCMCIABUSCF_CONTROLLER_DEFAULT &&
611 cf->cf_loc[PCMCIABUSCF_CONTROLLER] != 1)
612 return 0;
613 if (cf->cf_loc[PCMCIABUSCF_SOCKET] !=
614 PCMCIABUSCF_SOCKET_DEFAULT &&
615 cf->cf_loc[PCMCIABUSCF_SOCKET] != 0)
616 return 0;
617
618 break;
619 case C1SB:
620 if (cf->cf_loc[PCMCIABUSCF_CONTROLLER] !=
621 PCMCIABUSCF_CONTROLLER_DEFAULT &&
622 cf->cf_loc[PCMCIABUSCF_CONTROLLER] != 1)
623 return 0;
624 if (cf->cf_loc[PCMCIABUSCF_SOCKET] !=
625 PCMCIABUSCF_SOCKET_DEFAULT &&
626 cf->cf_loc[PCMCIABUSCF_SOCKET] != 1)
627 return 0;
628
629 break;
630 default:
631 panic("unknown pcic socket");
632 }
633
634 return (config_match(parent, cf, aux));
635 }
636
637 int
638 pcic_print(arg, pnp)
639 void *arg;
640 const char *pnp;
641 {
642 struct pcmciabus_attach_args *paa = arg;
643 struct pcic_handle *h = (struct pcic_handle *) paa->pch;
644
645 /* Only "pcmcia"s can attach to "pcic"s... easy. */
646 if (pnp)
647 printf("pcmcia at %s", pnp);
648
649 switch (h->sock) {
650 case C0SA:
651 printf(" controller 0 socket 0");
652 break;
653 case C0SB:
654 printf(" controller 0 socket 1");
655 break;
656 case C1SA:
657 printf(" controller 1 socket 0");
658 break;
659 case C1SB:
660 printf(" controller 1 socket 1");
661 break;
662 default:
663 panic("unknown pcic socket");
664 }
665
666 return (UNCONF);
667 }
668
669 void
670 pcic_poll_intr(arg)
671 void *arg;
672 {
673 struct pcic_softc *sc;
674 int i, s;
675
676 s = spltty();
677 sc = arg;
678 for (i = 0; i < PCIC_NSLOTS; i++)
679 if (sc->handle[i].flags & PCIC_FLAG_SOCKETP)
680 (void)pcic_intr_socket(&sc->handle[i]);
681 callout_reset(&sc->poll_ch, hz / 2, pcic_poll_intr, sc);
682 splx(s);
683 }
684
685 int
686 pcic_intr(arg)
687 void *arg;
688 {
689 struct pcic_softc *sc = arg;
690 int i, ret = 0;
691
692 DPRINTF(("%s: intr\n", sc->dev.dv_xname));
693
694 for (i = 0; i < PCIC_NSLOTS; i++)
695 if (sc->handle[i].flags & PCIC_FLAG_SOCKETP)
696 ret += pcic_intr_socket(&sc->handle[i]);
697
698 return (ret ? 1 : 0);
699 }
700
701 int
702 pcic_intr_socket(h)
703 struct pcic_handle *h;
704 {
705 int cscreg;
706
707 cscreg = pcic_read(h, PCIC_CSC);
708
709 cscreg &= (PCIC_CSC_GPI |
710 PCIC_CSC_CD |
711 PCIC_CSC_READY |
712 PCIC_CSC_BATTWARN |
713 PCIC_CSC_BATTDEAD);
714
715 if (cscreg & PCIC_CSC_GPI) {
716 DPRINTF(("%s: %02x GPI\n", h->ph_parent->dv_xname, h->sock));
717 }
718 if (cscreg & PCIC_CSC_CD) {
719 int statreg;
720
721 statreg = pcic_read(h, PCIC_IF_STATUS);
722
723 DPRINTF(("%s: %02x CD %x\n", h->ph_parent->dv_xname, h->sock,
724 statreg));
725
726 if ((statreg & PCIC_IF_STATUS_CARDDETECT_MASK) ==
727 PCIC_IF_STATUS_CARDDETECT_PRESENT) {
728 if (h->laststate != PCIC_LASTSTATE_PRESENT) {
729 DPRINTF(("%s: enqueing INSERTION event\n",
730 h->ph_parent->dv_xname));
731 pcic_queue_event(h, PCIC_EVENT_INSERTION);
732 }
733 h->laststate = PCIC_LASTSTATE_PRESENT;
734 } else {
735 if (h->laststate == PCIC_LASTSTATE_PRESENT) {
736 /* Deactivate the card now. */
737 DPRINTF(("%s: deactivating card\n",
738 h->ph_parent->dv_xname));
739 pcic_deactivate_card(h);
740
741 DPRINTF(("%s: enqueing REMOVAL event\n",
742 h->ph_parent->dv_xname));
743 pcic_queue_event(h, PCIC_EVENT_REMOVAL);
744 }
745 h->laststate =
746 ((statreg & PCIC_IF_STATUS_CARDDETECT_MASK) == 0) ?
747 PCIC_LASTSTATE_EMPTY : PCIC_LASTSTATE_HALF;
748 }
749 }
750 if (cscreg & PCIC_CSC_READY) {
751 DPRINTF(("%s: %02x READY\n", h->ph_parent->dv_xname, h->sock));
752 /* shouldn't happen */
753 }
754 if (cscreg & PCIC_CSC_BATTWARN) {
755 DPRINTF(("%s: %02x BATTWARN\n", h->ph_parent->dv_xname,
756 h->sock));
757 }
758 if (cscreg & PCIC_CSC_BATTDEAD) {
759 DPRINTF(("%s: %02x BATTDEAD\n", h->ph_parent->dv_xname,
760 h->sock));
761 }
762 return (cscreg ? 1 : 0);
763 }
764
765 void
766 pcic_queue_event(h, event)
767 struct pcic_handle *h;
768 int event;
769 {
770 struct pcic_event *pe;
771 int s;
772
773 pe = malloc(sizeof(*pe), M_TEMP, M_NOWAIT);
774 if (pe == NULL)
775 panic("pcic_queue_event: can't allocate event");
776
777 pe->pe_type = event;
778 s = splhigh();
779 SIMPLEQ_INSERT_TAIL(&h->events, pe, pe_q);
780 splx(s);
781 wakeup(&h->events);
782 }
783
784 void
785 pcic_attach_card(h)
786 struct pcic_handle *h;
787 {
788
789 if (!(h->flags & PCIC_FLAG_CARDP)) {
790 /* call the MI attach function */
791 pcmcia_card_attach(h->pcmcia);
792
793 h->flags |= PCIC_FLAG_CARDP;
794 } else {
795 DPRINTF(("pcic_attach_card: already attached"));
796 }
797 }
798
799 void
800 pcic_detach_card(h, flags)
801 struct pcic_handle *h;
802 int flags; /* DETACH_* */
803 {
804
805 if (h->flags & PCIC_FLAG_CARDP) {
806 h->flags &= ~PCIC_FLAG_CARDP;
807
808 /* call the MI detach function */
809 pcmcia_card_detach(h->pcmcia, flags);
810 } else {
811 DPRINTF(("pcic_detach_card: already detached"));
812 }
813 }
814
815 void
816 pcic_deactivate_card(h)
817 struct pcic_handle *h;
818 {
819
820 /* call the MI deactivate function */
821 pcmcia_card_deactivate(h->pcmcia);
822
823 /* power down the socket */
824 pcic_write(h, PCIC_PWRCTL, 0);
825
826 /* reset the socket */
827 pcic_write(h, PCIC_INTR, 0);
828 }
829
830 int
831 pcic_chip_mem_alloc(pch, size, pcmhp)
832 pcmcia_chipset_handle_t pch;
833 bus_size_t size;
834 struct pcmcia_mem_handle *pcmhp;
835 {
836 struct pcic_handle *h = (struct pcic_handle *) pch;
837 bus_space_handle_t memh;
838 bus_addr_t addr;
839 bus_size_t sizepg;
840 int i, mask, mhandle;
841 struct pcic_softc *sc = (struct pcic_softc *)h->ph_parent;
842
843 /* out of sc->memh, allocate as many pages as necessary */
844
845 /* convert size to PCIC pages */
846 sizepg = (size + (PCIC_MEM_ALIGN - 1)) / PCIC_MEM_ALIGN;
847 if (sizepg > PCIC_MAX_MEM_PAGES)
848 return (1);
849
850 mask = (1 << sizepg) - 1;
851
852 addr = 0; /* XXX gcc -Wuninitialized */
853 mhandle = 0; /* XXX gcc -Wuninitialized */
854
855 for (i = 0; i <= PCIC_MAX_MEM_PAGES - sizepg; i++) {
856 if ((sc->subregionmask & (mask << i)) == (mask << i)) {
857 if (bus_space_subregion(sc->memt, sc->memh,
858 i * PCIC_MEM_PAGESIZE,
859 sizepg * PCIC_MEM_PAGESIZE, &memh))
860 return (1);
861 mhandle = mask << i;
862 addr = sc->membase + (i * PCIC_MEM_PAGESIZE);
863 sc->subregionmask &= ~(mhandle);
864 pcmhp->memt = sc->memt;
865 pcmhp->memh = memh;
866 pcmhp->addr = addr;
867 pcmhp->size = size;
868 pcmhp->mhandle = mhandle;
869 pcmhp->realsize = sizepg * PCIC_MEM_PAGESIZE;
870 return (0);
871 }
872 }
873
874 return (1);
875 }
876
877 void
878 pcic_chip_mem_free(pch, pcmhp)
879 pcmcia_chipset_handle_t pch;
880 struct pcmcia_mem_handle *pcmhp;
881 {
882 struct pcic_handle *h = (struct pcic_handle *) pch;
883 struct pcic_softc *sc = (struct pcic_softc *)h->ph_parent;
884
885 sc->subregionmask |= pcmhp->mhandle;
886 }
887
888 static const struct mem_map_index_st {
889 int sysmem_start_lsb;
890 int sysmem_start_msb;
891 int sysmem_stop_lsb;
892 int sysmem_stop_msb;
893 int cardmem_lsb;
894 int cardmem_msb;
895 int memenable;
896 } mem_map_index[] = {
897 {
898 PCIC_SYSMEM_ADDR0_START_LSB,
899 PCIC_SYSMEM_ADDR0_START_MSB,
900 PCIC_SYSMEM_ADDR0_STOP_LSB,
901 PCIC_SYSMEM_ADDR0_STOP_MSB,
902 PCIC_CARDMEM_ADDR0_LSB,
903 PCIC_CARDMEM_ADDR0_MSB,
904 PCIC_ADDRWIN_ENABLE_MEM0,
905 },
906 {
907 PCIC_SYSMEM_ADDR1_START_LSB,
908 PCIC_SYSMEM_ADDR1_START_MSB,
909 PCIC_SYSMEM_ADDR1_STOP_LSB,
910 PCIC_SYSMEM_ADDR1_STOP_MSB,
911 PCIC_CARDMEM_ADDR1_LSB,
912 PCIC_CARDMEM_ADDR1_MSB,
913 PCIC_ADDRWIN_ENABLE_MEM1,
914 },
915 {
916 PCIC_SYSMEM_ADDR2_START_LSB,
917 PCIC_SYSMEM_ADDR2_START_MSB,
918 PCIC_SYSMEM_ADDR2_STOP_LSB,
919 PCIC_SYSMEM_ADDR2_STOP_MSB,
920 PCIC_CARDMEM_ADDR2_LSB,
921 PCIC_CARDMEM_ADDR2_MSB,
922 PCIC_ADDRWIN_ENABLE_MEM2,
923 },
924 {
925 PCIC_SYSMEM_ADDR3_START_LSB,
926 PCIC_SYSMEM_ADDR3_START_MSB,
927 PCIC_SYSMEM_ADDR3_STOP_LSB,
928 PCIC_SYSMEM_ADDR3_STOP_MSB,
929 PCIC_CARDMEM_ADDR3_LSB,
930 PCIC_CARDMEM_ADDR3_MSB,
931 PCIC_ADDRWIN_ENABLE_MEM3,
932 },
933 {
934 PCIC_SYSMEM_ADDR4_START_LSB,
935 PCIC_SYSMEM_ADDR4_START_MSB,
936 PCIC_SYSMEM_ADDR4_STOP_LSB,
937 PCIC_SYSMEM_ADDR4_STOP_MSB,
938 PCIC_CARDMEM_ADDR4_LSB,
939 PCIC_CARDMEM_ADDR4_MSB,
940 PCIC_ADDRWIN_ENABLE_MEM4,
941 },
942 };
943
944 void
945 pcic_chip_do_mem_map(h, win)
946 struct pcic_handle *h;
947 int win;
948 {
949 int reg;
950 int kind = h->mem[win].kind & ~PCMCIA_WIDTH_MEM_MASK;
951 int mem8 =
952 (h->mem[win].kind & PCMCIA_WIDTH_MEM_MASK) == PCMCIA_WIDTH_MEM8
953 || (kind == PCMCIA_MEM_ATTR);
954
955 DPRINTF(("mem8 %d\n", mem8));
956 /* mem8 = 1; */
957
958 pcic_write(h, mem_map_index[win].sysmem_start_lsb,
959 (h->mem[win].addr >> PCIC_SYSMEM_ADDRX_SHIFT) & 0xff);
960 pcic_write(h, mem_map_index[win].sysmem_start_msb,
961 ((h->mem[win].addr >> (PCIC_SYSMEM_ADDRX_SHIFT + 8)) &
962 PCIC_SYSMEM_ADDRX_START_MSB_ADDR_MASK) |
963 (mem8 ? 0 : PCIC_SYSMEM_ADDRX_START_MSB_DATASIZE_16BIT));
964
965 pcic_write(h, mem_map_index[win].sysmem_stop_lsb,
966 ((h->mem[win].addr + h->mem[win].size) >>
967 PCIC_SYSMEM_ADDRX_SHIFT) & 0xff);
968 pcic_write(h, mem_map_index[win].sysmem_stop_msb,
969 (((h->mem[win].addr + h->mem[win].size) >>
970 (PCIC_SYSMEM_ADDRX_SHIFT + 8)) &
971 PCIC_SYSMEM_ADDRX_STOP_MSB_ADDR_MASK) |
972 PCIC_SYSMEM_ADDRX_STOP_MSB_WAIT2);
973
974 pcic_write(h, mem_map_index[win].cardmem_lsb,
975 (h->mem[win].offset >> PCIC_CARDMEM_ADDRX_SHIFT) & 0xff);
976 pcic_write(h, mem_map_index[win].cardmem_msb,
977 ((h->mem[win].offset >> (PCIC_CARDMEM_ADDRX_SHIFT + 8)) &
978 PCIC_CARDMEM_ADDRX_MSB_ADDR_MASK) |
979 ((kind == PCMCIA_MEM_ATTR) ?
980 PCIC_CARDMEM_ADDRX_MSB_REGACTIVE_ATTR : 0));
981
982 reg = pcic_read(h, PCIC_ADDRWIN_ENABLE);
983 reg |= (mem_map_index[win].memenable | PCIC_ADDRWIN_ENABLE_MEMCS16);
984 pcic_write(h, PCIC_ADDRWIN_ENABLE, reg);
985
986 delay(100);
987
988 #ifdef PCICDEBUG
989 {
990 int r1, r2, r3, r4, r5, r6;
991
992 r1 = pcic_read(h, mem_map_index[win].sysmem_start_msb);
993 r2 = pcic_read(h, mem_map_index[win].sysmem_start_lsb);
994 r3 = pcic_read(h, mem_map_index[win].sysmem_stop_msb);
995 r4 = pcic_read(h, mem_map_index[win].sysmem_stop_lsb);
996 r5 = pcic_read(h, mem_map_index[win].cardmem_msb);
997 r6 = pcic_read(h, mem_map_index[win].cardmem_lsb);
998
999 DPRINTF(("pcic_chip_do_mem_map window %d: %02x%02x %02x%02x "
1000 "%02x%02x\n", win, r1, r2, r3, r4, r5, r6));
1001 }
1002 #endif
1003 }
1004
1005 int
1006 pcic_chip_mem_map(pch, kind, card_addr, size, pcmhp, offsetp, windowp)
1007 pcmcia_chipset_handle_t pch;
1008 int kind;
1009 bus_addr_t card_addr;
1010 bus_size_t size;
1011 struct pcmcia_mem_handle *pcmhp;
1012 bus_size_t *offsetp;
1013 int *windowp;
1014 {
1015 struct pcic_handle *h = (struct pcic_handle *) pch;
1016 bus_addr_t busaddr;
1017 long card_offset;
1018 int i, win;
1019 struct pcic_softc *sc = (struct pcic_softc *)h->ph_parent;
1020
1021 win = -1;
1022 for (i = 0; i < (sizeof(mem_map_index) / sizeof(mem_map_index[0]));
1023 i++) {
1024 if ((h->memalloc & (1 << i)) == 0) {
1025 win = i;
1026 h->memalloc |= (1 << i);
1027 break;
1028 }
1029 }
1030
1031 if (win == -1)
1032 return (1);
1033
1034 *windowp = win;
1035
1036 /* XXX this is pretty gross */
1037
1038 if (sc->memt != pcmhp->memt)
1039 panic("pcic_chip_mem_map memt is bogus");
1040
1041 busaddr = pcmhp->addr;
1042
1043 /*
1044 * compute the address offset to the pcmcia address space for the
1045 * pcic. this is intentionally signed. The masks and shifts below
1046 * will cause TRT to happen in the pcic registers. Deal with making
1047 * sure the address is aligned, and return the alignment offset.
1048 */
1049
1050 *offsetp = card_addr % PCIC_MEM_ALIGN;
1051 card_addr -= *offsetp;
1052
1053 DPRINTF(("pcic_chip_mem_map window %d bus %lx+%lx+%lx at card addr "
1054 "%lx\n", win, (u_long) busaddr, (u_long) * offsetp, (u_long) size,
1055 (u_long) card_addr));
1056
1057 /*
1058 * include the offset in the size, and decrement size by one, since
1059 * the hw wants start/stop
1060 */
1061 size += *offsetp - 1;
1062
1063 card_offset = (((long) card_addr) - ((long) busaddr));
1064
1065 h->mem[win].addr = busaddr;
1066 h->mem[win].size = size;
1067 h->mem[win].offset = card_offset;
1068 h->mem[win].kind = kind;
1069
1070 pcic_chip_do_mem_map(h, win);
1071
1072 return (0);
1073 }
1074
1075 void
1076 pcic_chip_mem_unmap(pch, window)
1077 pcmcia_chipset_handle_t pch;
1078 int window;
1079 {
1080 struct pcic_handle *h = (struct pcic_handle *) pch;
1081 int reg;
1082
1083 if (window >= (sizeof(mem_map_index) / sizeof(mem_map_index[0])))
1084 panic("pcic_chip_mem_unmap: window out of range");
1085
1086 reg = pcic_read(h, PCIC_ADDRWIN_ENABLE);
1087 reg &= ~mem_map_index[window].memenable;
1088 pcic_write(h, PCIC_ADDRWIN_ENABLE, reg);
1089
1090 h->memalloc &= ~(1 << window);
1091 }
1092
1093 int
1094 pcic_chip_io_alloc(pch, start, size, align, pcihp)
1095 pcmcia_chipset_handle_t pch;
1096 bus_addr_t start;
1097 bus_size_t size;
1098 bus_size_t align;
1099 struct pcmcia_io_handle *pcihp;
1100 {
1101 struct pcic_handle *h = (struct pcic_handle *) pch;
1102 bus_space_tag_t iot;
1103 bus_space_handle_t ioh;
1104 bus_addr_t ioaddr;
1105 int flags = 0;
1106 struct pcic_softc *sc = (struct pcic_softc *)h->ph_parent;
1107
1108 /*
1109 * Allocate some arbitrary I/O space.
1110 */
1111
1112 iot = sc->iot;
1113
1114 if (start) {
1115 ioaddr = start;
1116 if (bus_space_map(iot, start, size, 0, &ioh))
1117 return (1);
1118 DPRINTF(("pcic_chip_io_alloc map port %lx+%lx\n",
1119 (u_long) ioaddr, (u_long) size));
1120 } else {
1121 flags |= PCMCIA_IO_ALLOCATED;
1122 if (bus_space_alloc(iot, sc->iobase,
1123 sc->iobase + sc->iosize, size, align, 0, 0,
1124 &ioaddr, &ioh))
1125 return (1);
1126 DPRINTF(("pcic_chip_io_alloc alloc port %lx+%lx\n",
1127 (u_long) ioaddr, (u_long) size));
1128 }
1129
1130 pcihp->iot = iot;
1131 pcihp->ioh = ioh;
1132 pcihp->addr = ioaddr;
1133 pcihp->size = size;
1134 pcihp->flags = flags;
1135
1136 return (0);
1137 }
1138
1139 void
1140 pcic_chip_io_free(pch, pcihp)
1141 pcmcia_chipset_handle_t pch;
1142 struct pcmcia_io_handle *pcihp;
1143 {
1144 bus_space_tag_t iot = pcihp->iot;
1145 bus_space_handle_t ioh = pcihp->ioh;
1146 bus_size_t size = pcihp->size;
1147
1148 if (pcihp->flags & PCMCIA_IO_ALLOCATED)
1149 bus_space_free(iot, ioh, size);
1150 else
1151 bus_space_unmap(iot, ioh, size);
1152 }
1153
1154
1155 static const struct io_map_index_st {
1156 int start_lsb;
1157 int start_msb;
1158 int stop_lsb;
1159 int stop_msb;
1160 int ioenable;
1161 int ioctlmask;
1162 int ioctlbits[3]; /* indexed by PCMCIA_WIDTH_* */
1163 } io_map_index[] = {
1164 {
1165 PCIC_IOADDR0_START_LSB,
1166 PCIC_IOADDR0_START_MSB,
1167 PCIC_IOADDR0_STOP_LSB,
1168 PCIC_IOADDR0_STOP_MSB,
1169 PCIC_ADDRWIN_ENABLE_IO0,
1170 PCIC_IOCTL_IO0_WAITSTATE | PCIC_IOCTL_IO0_ZEROWAIT |
1171 PCIC_IOCTL_IO0_IOCS16SRC_MASK | PCIC_IOCTL_IO0_DATASIZE_MASK,
1172 {
1173 PCIC_IOCTL_IO0_IOCS16SRC_CARD,
1174 PCIC_IOCTL_IO0_IOCS16SRC_DATASIZE |
1175 PCIC_IOCTL_IO0_DATASIZE_8BIT,
1176 PCIC_IOCTL_IO0_IOCS16SRC_DATASIZE |
1177 PCIC_IOCTL_IO0_DATASIZE_16BIT,
1178 },
1179 },
1180 {
1181 PCIC_IOADDR1_START_LSB,
1182 PCIC_IOADDR1_START_MSB,
1183 PCIC_IOADDR1_STOP_LSB,
1184 PCIC_IOADDR1_STOP_MSB,
1185 PCIC_ADDRWIN_ENABLE_IO1,
1186 PCIC_IOCTL_IO1_WAITSTATE | PCIC_IOCTL_IO1_ZEROWAIT |
1187 PCIC_IOCTL_IO1_IOCS16SRC_MASK | PCIC_IOCTL_IO1_DATASIZE_MASK,
1188 {
1189 PCIC_IOCTL_IO1_IOCS16SRC_CARD,
1190 PCIC_IOCTL_IO1_IOCS16SRC_DATASIZE |
1191 PCIC_IOCTL_IO1_DATASIZE_8BIT,
1192 PCIC_IOCTL_IO1_IOCS16SRC_DATASIZE |
1193 PCIC_IOCTL_IO1_DATASIZE_16BIT,
1194 },
1195 },
1196 };
1197
1198 void
1199 pcic_chip_do_io_map(h, win)
1200 struct pcic_handle *h;
1201 int win;
1202 {
1203 int reg;
1204
1205 DPRINTF(("pcic_chip_do_io_map win %d addr %lx size %lx width %d\n",
1206 win, (long) h->io[win].addr, (long) h->io[win].size,
1207 h->io[win].width * 8));
1208
1209 pcic_write(h, io_map_index[win].start_lsb, h->io[win].addr & 0xff);
1210 pcic_write(h, io_map_index[win].start_msb,
1211 (h->io[win].addr >> 8) & 0xff);
1212
1213 pcic_write(h, io_map_index[win].stop_lsb,
1214 (h->io[win].addr + h->io[win].size - 1) & 0xff);
1215 pcic_write(h, io_map_index[win].stop_msb,
1216 ((h->io[win].addr + h->io[win].size - 1) >> 8) & 0xff);
1217
1218 reg = pcic_read(h, PCIC_IOCTL);
1219 reg &= ~io_map_index[win].ioctlmask;
1220 reg |= io_map_index[win].ioctlbits[h->io[win].width];
1221 pcic_write(h, PCIC_IOCTL, reg);
1222
1223 reg = pcic_read(h, PCIC_ADDRWIN_ENABLE);
1224 reg |= io_map_index[win].ioenable;
1225 pcic_write(h, PCIC_ADDRWIN_ENABLE, reg);
1226 }
1227
1228 int
1229 pcic_chip_io_map(pch, width, offset, size, pcihp, windowp)
1230 pcmcia_chipset_handle_t pch;
1231 int width;
1232 bus_addr_t offset;
1233 bus_size_t size;
1234 struct pcmcia_io_handle *pcihp;
1235 int *windowp;
1236 {
1237 struct pcic_handle *h = (struct pcic_handle *) pch;
1238 bus_addr_t ioaddr = pcihp->addr + offset;
1239 int i, win;
1240 #ifdef PCICDEBUG
1241 static char *width_names[] = { "auto", "io8", "io16" };
1242 #endif
1243 struct pcic_softc *sc = (struct pcic_softc *)h->ph_parent;
1244
1245 /* XXX Sanity check offset/size. */
1246
1247 win = -1;
1248 for (i = 0; i < (sizeof(io_map_index) / sizeof(io_map_index[0])); i++) {
1249 if ((h->ioalloc & (1 << i)) == 0) {
1250 win = i;
1251 h->ioalloc |= (1 << i);
1252 break;
1253 }
1254 }
1255
1256 if (win == -1)
1257 return (1);
1258
1259 *windowp = win;
1260
1261 /* XXX this is pretty gross */
1262
1263 if (sc->iot != pcihp->iot)
1264 panic("pcic_chip_io_map iot is bogus");
1265
1266 DPRINTF(("pcic_chip_io_map window %d %s port %lx+%lx\n",
1267 win, width_names[width], (u_long) ioaddr, (u_long) size));
1268
1269 /* XXX wtf is this doing here? */
1270
1271 printf(" port 0x%lx", (u_long) ioaddr);
1272 if (size > 1)
1273 printf("-0x%lx", (u_long) ioaddr + (u_long) size - 1);
1274
1275 h->io[win].addr = ioaddr;
1276 h->io[win].size = size;
1277 h->io[win].width = width;
1278
1279 pcic_chip_do_io_map(h, win);
1280
1281 return (0);
1282 }
1283
1284 void
1285 pcic_chip_io_unmap(pch, window)
1286 pcmcia_chipset_handle_t pch;
1287 int window;
1288 {
1289 struct pcic_handle *h = (struct pcic_handle *) pch;
1290 int reg;
1291
1292 if (window >= (sizeof(io_map_index) / sizeof(io_map_index[0])))
1293 panic("pcic_chip_io_unmap: window out of range");
1294
1295 reg = pcic_read(h, PCIC_ADDRWIN_ENABLE);
1296 reg &= ~io_map_index[window].ioenable;
1297 pcic_write(h, PCIC_ADDRWIN_ENABLE, reg);
1298
1299 h->ioalloc &= ~(1 << window);
1300 }
1301
1302 static void
1303 pcic_wait_ready(h)
1304 struct pcic_handle *h;
1305 {
1306 int i;
1307
1308 /* wait an initial 10ms for quick cards */
1309 if (pcic_read(h, PCIC_IF_STATUS) & PCIC_IF_STATUS_READY)
1310 return;
1311 pcic_delay(h, 10, "pccwr0");
1312 for (i = 0; i < 50; i++) {
1313 if (pcic_read(h, PCIC_IF_STATUS) & PCIC_IF_STATUS_READY)
1314 return;
1315 /* wait .1s (100ms) each iteration now */
1316 pcic_delay(h, 100, "pccwr1");
1317 #ifdef PCICDEBUG
1318 if (pcic_debug) {
1319 if ((i > 20) && (i % 100 == 99))
1320 printf(".");
1321 }
1322 #endif
1323 }
1324
1325 #ifdef DIAGNOSTIC
1326 printf("pcic_wait_ready: ready never happened, status = %02x\n",
1327 pcic_read(h, PCIC_IF_STATUS));
1328 #endif
1329 }
1330
1331 /*
1332 * Perform long (msec order) delay.
1333 */
1334 static void
1335 pcic_delay(h, timo, wmesg)
1336 struct pcic_handle *h;
1337 int timo; /* in ms. must not be zero */
1338 const char *wmesg;
1339 {
1340
1341 #ifdef DIAGNOSTIC
1342 if (timo <= 0) {
1343 printf("called with timeout %d\n", timo);
1344 panic("pcic_delay");
1345 }
1346 if (curproc == NULL) {
1347 printf("called in interrupt context\n");
1348 panic("pcic_delay");
1349 }
1350 if (h->event_thread == NULL) {
1351 printf("no event thread\n");
1352 panic("pcic_delay");
1353 }
1354 #endif
1355 DPRINTF(("pcic_delay: \"%s\" %p, sleep %d ms\n",
1356 wmesg, h->event_thread, timo));
1357 tsleep(pcic_delay, PWAIT, wmesg, roundup(timo * hz, 1000) / 1000);
1358 }
1359
1360 void
1361 pcic_chip_socket_enable(pch)
1362 pcmcia_chipset_handle_t pch;
1363 {
1364 struct pcic_handle *h = (struct pcic_handle *) pch;
1365 int cardtype, win, intr, pwr;
1366 #if defined(DIAGNOSTIC) || defined(PCICDEBUG)
1367 int reg;
1368 #endif
1369
1370 #ifdef DIAGNOSTIC
1371 if (h->flags & PCIC_FLAG_ENABLED)
1372 printf("pcic_chip_socket_enable: enabling twice\n");
1373 #endif
1374
1375 /* disable interrupts */
1376 intr = pcic_read(h, PCIC_INTR);
1377 intr &= ~(PCIC_INTR_IRQ_MASK | PCIC_INTR_ENABLE);
1378 pcic_write(h, PCIC_INTR, intr);
1379
1380 /* power down the socket to reset it, clear the card reset pin */
1381 pwr = 0;
1382 pcic_write(h, PCIC_PWRCTL, pwr);
1383
1384 /*
1385 * wait 300ms until power fails (Tpf). Then, wait 100ms since
1386 * we are changing Vcc (Toff).
1387 */
1388 pcic_delay(h, 300 + 100, "pccen0");
1389
1390 #ifdef VADEM_POWER_HACK
1391 bus_space_write_1(sc->iot, sc->ioh, PCIC_REG_INDEX, 0x0e);
1392 bus_space_write_1(sc->iot, sc->ioh, PCIC_REG_INDEX, 0x37);
1393 printf("prcr = %02x\n", pcic_read(h, 0x02));
1394 printf("cvsr = %02x\n", pcic_read(h, 0x2f));
1395 printf("DANGER WILL ROBINSON! Changing voltage select!\n");
1396 pcic_write(h, 0x2f, pcic_read(h, 0x2f) & ~0x03);
1397 printf("cvsr = %02x\n", pcic_read(h, 0x2f));
1398 #endif
1399 /* power up the socket */
1400 pwr |= PCIC_PWRCTL_DISABLE_RESETDRV | PCIC_PWRCTL_PWR_ENABLE | PCIC_PWRCTL_VPP1_VCC;
1401 pcic_write(h, PCIC_PWRCTL, pwr);
1402
1403 /*
1404 * wait 100ms until power raise (Tpr) and 20ms to become
1405 * stable (Tsu(Vcc)).
1406 *
1407 * some machines require some more time to be settled
1408 * (300ms is added here).
1409 */
1410 pcic_delay(h, 100 + 20 + 300, "pccen1");
1411 pwr |= PCIC_PWRCTL_OE;
1412 pcic_write(h, PCIC_PWRCTL, pwr);
1413
1414 /* now make sure we have reset# active */
1415 intr &= ~PCIC_INTR_RESET;
1416 pcic_write(h, PCIC_INTR, intr);
1417
1418 pcic_write(h, PCIC_PWRCTL, PCIC_PWRCTL_DISABLE_RESETDRV |
1419 PCIC_PWRCTL_OE | PCIC_PWRCTL_PWR_ENABLE | PCIC_PWRCTL_VPP1_VCC);
1420 /*
1421 * hold RESET at least 10us, this is a min allow for slop in
1422 * delay routine.
1423 */
1424 delay(20);
1425
1426 /* clear the reset flag */
1427 intr |= PCIC_INTR_RESET;
1428 pcic_write(h, PCIC_INTR, intr);
1429
1430 /* wait 20ms as per pc card standard (r2.01) section 4.3.6 */
1431 pcic_delay(h, 20, "pccen2");
1432
1433 #ifdef DIAGNOSTIC
1434 reg = pcic_read(h, PCIC_IF_STATUS);
1435 if (!(reg & PCIC_IF_STATUS_POWERACTIVE)) {
1436 printf("pcic_chip_socket_enable: status %x\n", reg);
1437 }
1438 #endif
1439 /* wait for the chip to finish initializing */
1440 pcic_wait_ready(h);
1441
1442 /* zero out the address windows */
1443 pcic_write(h, PCIC_ADDRWIN_ENABLE, 0);
1444
1445 /* set the card type and enable the interrupt */
1446 cardtype = pcmcia_card_gettype(h->pcmcia);
1447 intr |= ((cardtype == PCMCIA_IFTYPE_IO) ?
1448 PCIC_INTR_CARDTYPE_IO : PCIC_INTR_CARDTYPE_MEM);
1449 pcic_write(h, PCIC_INTR, intr);
1450
1451 DPRINTF(("%s: pcic_chip_socket_enable %02x cardtype %s %02x\n",
1452 h->ph_parent->dv_xname, h->sock,
1453 ((cardtype == PCMCIA_IFTYPE_IO) ? "io" : "mem"), reg));
1454
1455 /* reinstall all the memory and io mappings */
1456 for (win = 0; win < PCIC_MEM_WINS; win++)
1457 if (h->memalloc & (1 << win))
1458 pcic_chip_do_mem_map(h, win);
1459 for (win = 0; win < PCIC_IO_WINS; win++)
1460 if (h->ioalloc & (1 << win))
1461 pcic_chip_do_io_map(h, win);
1462
1463 h->flags |= PCIC_FLAG_ENABLED;
1464
1465 /* finally enable the interrupt */
1466 intr |= h->ih_irq;
1467 pcic_write(h, PCIC_INTR, intr);
1468 }
1469
1470 void
1471 pcic_chip_socket_disable(pch)
1472 pcmcia_chipset_handle_t pch;
1473 {
1474 struct pcic_handle *h = (struct pcic_handle *) pch;
1475 int intr;
1476
1477 DPRINTF(("pcic_chip_socket_disable\n"));
1478
1479 /* disable interrupts */
1480 intr = pcic_read(h, PCIC_INTR);
1481 intr &= ~(PCIC_INTR_IRQ_MASK | PCIC_INTR_ENABLE);
1482 pcic_write(h, PCIC_INTR, intr);
1483
1484 /* power down the socket */
1485 pcic_write(h, PCIC_PWRCTL, 0);
1486
1487 /* zero out the address windows */
1488 pcic_write(h, PCIC_ADDRWIN_ENABLE, 0);
1489
1490 h->flags &= ~PCIC_FLAG_ENABLED;
1491 }
1492
1493 static u_int8_t
1494 st_pcic_read(h, idx)
1495 struct pcic_handle *h;
1496 int idx;
1497 {
1498
1499 if (idx != -1)
1500 bus_space_write_1(h->ph_bus_t, h->ph_bus_h, PCIC_REG_INDEX,
1501 h->sock + idx);
1502 return (bus_space_read_1(h->ph_bus_t, h->ph_bus_h, PCIC_REG_DATA));
1503 }
1504
1505 static void
1506 st_pcic_write(h, idx, data)
1507 struct pcic_handle *h;
1508 int idx;
1509 u_int8_t data;
1510 {
1511
1512 if (idx != -1)
1513 bus_space_write_1(h->ph_bus_t, h->ph_bus_h, PCIC_REG_INDEX,
1514 h->sock + idx);
1515 bus_space_write_1(h->ph_bus_t, h->ph_bus_h, PCIC_REG_DATA, data);
1516 }
1517