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