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