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