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