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