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