i82365.c revision 1.73 1 /* $NetBSD: i82365.c,v 1.73 2003/09/02 22:44:08 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.73 2003/09/02 22:44:08 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
868 /* call the MI deactivate function */
869 pcmcia_card_deactivate(h->pcmcia);
870
871 /* power down the socket */
872 pcic_write(h, PCIC_PWRCTL, 0);
873
874 /* reset the socket */
875 pcic_write(h, PCIC_INTR, 0);
876 }
877
878 int
879 pcic_chip_mem_alloc(pch, size, pcmhp)
880 pcmcia_chipset_handle_t pch;
881 bus_size_t size;
882 struct pcmcia_mem_handle *pcmhp;
883 {
884 struct pcic_handle *h = (struct pcic_handle *) pch;
885 bus_space_handle_t memh;
886 bus_addr_t addr;
887 bus_size_t sizepg;
888 int i, mask, mhandle;
889 struct pcic_softc *sc = (struct pcic_softc *)h->ph_parent;
890
891 /* out of sc->memh, allocate as many pages as necessary */
892
893 /* convert size to PCIC pages */
894 sizepg = (size + (PCIC_MEM_ALIGN - 1)) / PCIC_MEM_ALIGN;
895 if (sizepg > PCIC_MAX_MEM_PAGES)
896 return (1);
897
898 mask = (1 << sizepg) - 1;
899
900 addr = 0; /* XXX gcc -Wuninitialized */
901 mhandle = 0; /* XXX gcc -Wuninitialized */
902
903 for (i = 0; i <= PCIC_MAX_MEM_PAGES - sizepg; i++) {
904 if ((sc->subregionmask & (mask << i)) == (mask << i)) {
905 if (bus_space_subregion(sc->memt, sc->memh,
906 i * PCIC_MEM_PAGESIZE,
907 sizepg * PCIC_MEM_PAGESIZE, &memh))
908 return (1);
909 mhandle = mask << i;
910 addr = sc->membase + (i * PCIC_MEM_PAGESIZE);
911 sc->subregionmask &= ~(mhandle);
912 pcmhp->memt = sc->memt;
913 pcmhp->memh = memh;
914 pcmhp->addr = addr;
915 pcmhp->size = size;
916 pcmhp->mhandle = mhandle;
917 pcmhp->realsize = sizepg * PCIC_MEM_PAGESIZE;
918 return (0);
919 }
920 }
921
922 return (1);
923 }
924
925 void
926 pcic_chip_mem_free(pch, pcmhp)
927 pcmcia_chipset_handle_t pch;
928 struct pcmcia_mem_handle *pcmhp;
929 {
930 struct pcic_handle *h = (struct pcic_handle *) pch;
931 struct pcic_softc *sc = (struct pcic_softc *)h->ph_parent;
932
933 sc->subregionmask |= pcmhp->mhandle;
934 }
935
936 static const struct mem_map_index_st {
937 int sysmem_start_lsb;
938 int sysmem_start_msb;
939 int sysmem_stop_lsb;
940 int sysmem_stop_msb;
941 int cardmem_lsb;
942 int cardmem_msb;
943 int memenable;
944 } mem_map_index[] = {
945 {
946 PCIC_SYSMEM_ADDR0_START_LSB,
947 PCIC_SYSMEM_ADDR0_START_MSB,
948 PCIC_SYSMEM_ADDR0_STOP_LSB,
949 PCIC_SYSMEM_ADDR0_STOP_MSB,
950 PCIC_CARDMEM_ADDR0_LSB,
951 PCIC_CARDMEM_ADDR0_MSB,
952 PCIC_ADDRWIN_ENABLE_MEM0,
953 },
954 {
955 PCIC_SYSMEM_ADDR1_START_LSB,
956 PCIC_SYSMEM_ADDR1_START_MSB,
957 PCIC_SYSMEM_ADDR1_STOP_LSB,
958 PCIC_SYSMEM_ADDR1_STOP_MSB,
959 PCIC_CARDMEM_ADDR1_LSB,
960 PCIC_CARDMEM_ADDR1_MSB,
961 PCIC_ADDRWIN_ENABLE_MEM1,
962 },
963 {
964 PCIC_SYSMEM_ADDR2_START_LSB,
965 PCIC_SYSMEM_ADDR2_START_MSB,
966 PCIC_SYSMEM_ADDR2_STOP_LSB,
967 PCIC_SYSMEM_ADDR2_STOP_MSB,
968 PCIC_CARDMEM_ADDR2_LSB,
969 PCIC_CARDMEM_ADDR2_MSB,
970 PCIC_ADDRWIN_ENABLE_MEM2,
971 },
972 {
973 PCIC_SYSMEM_ADDR3_START_LSB,
974 PCIC_SYSMEM_ADDR3_START_MSB,
975 PCIC_SYSMEM_ADDR3_STOP_LSB,
976 PCIC_SYSMEM_ADDR3_STOP_MSB,
977 PCIC_CARDMEM_ADDR3_LSB,
978 PCIC_CARDMEM_ADDR3_MSB,
979 PCIC_ADDRWIN_ENABLE_MEM3,
980 },
981 {
982 PCIC_SYSMEM_ADDR4_START_LSB,
983 PCIC_SYSMEM_ADDR4_START_MSB,
984 PCIC_SYSMEM_ADDR4_STOP_LSB,
985 PCIC_SYSMEM_ADDR4_STOP_MSB,
986 PCIC_CARDMEM_ADDR4_LSB,
987 PCIC_CARDMEM_ADDR4_MSB,
988 PCIC_ADDRWIN_ENABLE_MEM4,
989 },
990 };
991
992 void
993 pcic_chip_do_mem_map(h, win)
994 struct pcic_handle *h;
995 int win;
996 {
997 int reg;
998 int kind = h->mem[win].kind & ~PCMCIA_WIDTH_MEM_MASK;
999 int mem8 =
1000 (h->mem[win].kind & PCMCIA_WIDTH_MEM_MASK) == PCMCIA_WIDTH_MEM8
1001 || (kind == PCMCIA_MEM_ATTR);
1002
1003 DPRINTF(("mem8 %d\n", mem8));
1004 /* mem8 = 1; */
1005
1006 pcic_write(h, mem_map_index[win].sysmem_start_lsb,
1007 (h->mem[win].addr >> PCIC_SYSMEM_ADDRX_SHIFT) & 0xff);
1008 pcic_write(h, mem_map_index[win].sysmem_start_msb,
1009 ((h->mem[win].addr >> (PCIC_SYSMEM_ADDRX_SHIFT + 8)) &
1010 PCIC_SYSMEM_ADDRX_START_MSB_ADDR_MASK) |
1011 (mem8 ? 0 : PCIC_SYSMEM_ADDRX_START_MSB_DATASIZE_16BIT));
1012
1013 pcic_write(h, mem_map_index[win].sysmem_stop_lsb,
1014 ((h->mem[win].addr + h->mem[win].size) >>
1015 PCIC_SYSMEM_ADDRX_SHIFT) & 0xff);
1016 pcic_write(h, mem_map_index[win].sysmem_stop_msb,
1017 (((h->mem[win].addr + h->mem[win].size) >>
1018 (PCIC_SYSMEM_ADDRX_SHIFT + 8)) &
1019 PCIC_SYSMEM_ADDRX_STOP_MSB_ADDR_MASK) |
1020 PCIC_SYSMEM_ADDRX_STOP_MSB_WAIT2);
1021
1022 pcic_write(h, mem_map_index[win].cardmem_lsb,
1023 (h->mem[win].offset >> PCIC_CARDMEM_ADDRX_SHIFT) & 0xff);
1024 pcic_write(h, mem_map_index[win].cardmem_msb,
1025 ((h->mem[win].offset >> (PCIC_CARDMEM_ADDRX_SHIFT + 8)) &
1026 PCIC_CARDMEM_ADDRX_MSB_ADDR_MASK) |
1027 ((kind == PCMCIA_MEM_ATTR) ?
1028 PCIC_CARDMEM_ADDRX_MSB_REGACTIVE_ATTR : 0));
1029
1030 reg = pcic_read(h, PCIC_ADDRWIN_ENABLE);
1031 reg |= (mem_map_index[win].memenable | PCIC_ADDRWIN_ENABLE_MEMCS16);
1032 pcic_write(h, PCIC_ADDRWIN_ENABLE, reg);
1033
1034 delay(100);
1035
1036 #ifdef PCICDEBUG
1037 {
1038 int r1, r2, r3, r4, r5, r6;
1039
1040 r1 = pcic_read(h, mem_map_index[win].sysmem_start_msb);
1041 r2 = pcic_read(h, mem_map_index[win].sysmem_start_lsb);
1042 r3 = pcic_read(h, mem_map_index[win].sysmem_stop_msb);
1043 r4 = pcic_read(h, mem_map_index[win].sysmem_stop_lsb);
1044 r5 = pcic_read(h, mem_map_index[win].cardmem_msb);
1045 r6 = pcic_read(h, mem_map_index[win].cardmem_lsb);
1046
1047 DPRINTF(("pcic_chip_do_mem_map window %d: %02x%02x %02x%02x "
1048 "%02x%02x\n", win, r1, r2, r3, r4, r5, r6));
1049 }
1050 #endif
1051 }
1052
1053 int
1054 pcic_chip_mem_map(pch, kind, card_addr, size, pcmhp, offsetp, windowp)
1055 pcmcia_chipset_handle_t pch;
1056 int kind;
1057 bus_addr_t card_addr;
1058 bus_size_t size;
1059 struct pcmcia_mem_handle *pcmhp;
1060 bus_size_t *offsetp;
1061 int *windowp;
1062 {
1063 struct pcic_handle *h = (struct pcic_handle *) pch;
1064 bus_addr_t busaddr;
1065 long card_offset;
1066 int i, win;
1067 struct pcic_softc *sc = (struct pcic_softc *)h->ph_parent;
1068
1069 win = -1;
1070 for (i = 0; i < (sizeof(mem_map_index) / sizeof(mem_map_index[0]));
1071 i++) {
1072 if ((h->memalloc & (1 << i)) == 0) {
1073 win = i;
1074 h->memalloc |= (1 << i);
1075 break;
1076 }
1077 }
1078
1079 if (win == -1)
1080 return (1);
1081
1082 *windowp = win;
1083
1084 /* XXX this is pretty gross */
1085
1086 if (sc->memt != pcmhp->memt)
1087 panic("pcic_chip_mem_map memt is bogus");
1088
1089 busaddr = pcmhp->addr;
1090
1091 /*
1092 * compute the address offset to the pcmcia address space for the
1093 * pcic. this is intentionally signed. The masks and shifts below
1094 * will cause TRT to happen in the pcic registers. Deal with making
1095 * sure the address is aligned, and return the alignment offset.
1096 */
1097
1098 *offsetp = card_addr % PCIC_MEM_ALIGN;
1099 card_addr -= *offsetp;
1100
1101 DPRINTF(("pcic_chip_mem_map window %d bus %lx+%lx+%lx at card addr "
1102 "%lx\n", win, (u_long) busaddr, (u_long) * offsetp, (u_long) size,
1103 (u_long) card_addr));
1104
1105 /*
1106 * include the offset in the size, and decrement size by one, since
1107 * the hw wants start/stop
1108 */
1109 size += *offsetp - 1;
1110
1111 card_offset = (((long) card_addr) - ((long) busaddr));
1112
1113 h->mem[win].addr = busaddr;
1114 h->mem[win].size = size;
1115 h->mem[win].offset = card_offset;
1116 h->mem[win].kind = kind;
1117
1118 pcic_chip_do_mem_map(h, win);
1119
1120 return (0);
1121 }
1122
1123 void
1124 pcic_chip_mem_unmap(pch, window)
1125 pcmcia_chipset_handle_t pch;
1126 int window;
1127 {
1128 struct pcic_handle *h = (struct pcic_handle *) pch;
1129 int reg;
1130
1131 if (window >= (sizeof(mem_map_index) / sizeof(mem_map_index[0])))
1132 panic("pcic_chip_mem_unmap: window out of range");
1133
1134 reg = pcic_read(h, PCIC_ADDRWIN_ENABLE);
1135 reg &= ~mem_map_index[window].memenable;
1136 pcic_write(h, PCIC_ADDRWIN_ENABLE, reg);
1137
1138 h->memalloc &= ~(1 << window);
1139 }
1140
1141 int
1142 pcic_chip_io_alloc(pch, start, size, align, pcihp)
1143 pcmcia_chipset_handle_t pch;
1144 bus_addr_t start;
1145 bus_size_t size;
1146 bus_size_t align;
1147 struct pcmcia_io_handle *pcihp;
1148 {
1149 struct pcic_handle *h = (struct pcic_handle *) pch;
1150 bus_space_tag_t iot;
1151 bus_space_handle_t ioh;
1152 bus_addr_t ioaddr;
1153 int flags = 0;
1154 struct pcic_softc *sc = (struct pcic_softc *)h->ph_parent;
1155
1156 /*
1157 * Allocate some arbitrary I/O space.
1158 */
1159
1160 iot = sc->iot;
1161
1162 if (start) {
1163 ioaddr = start;
1164 if (bus_space_map(iot, start, size, 0, &ioh))
1165 return (1);
1166 DPRINTF(("pcic_chip_io_alloc map port %lx+%lx\n",
1167 (u_long) ioaddr, (u_long) size));
1168 } else {
1169 flags |= PCMCIA_IO_ALLOCATED;
1170 if (bus_space_alloc(iot, sc->iobase,
1171 sc->iobase + sc->iosize, size, align, 0, 0,
1172 &ioaddr, &ioh))
1173 return (1);
1174 DPRINTF(("pcic_chip_io_alloc alloc port %lx+%lx\n",
1175 (u_long) ioaddr, (u_long) size));
1176 }
1177
1178 pcihp->iot = iot;
1179 pcihp->ioh = ioh;
1180 pcihp->addr = ioaddr;
1181 pcihp->size = size;
1182 pcihp->flags = flags;
1183
1184 return (0);
1185 }
1186
1187 void
1188 pcic_chip_io_free(pch, pcihp)
1189 pcmcia_chipset_handle_t pch;
1190 struct pcmcia_io_handle *pcihp;
1191 {
1192 bus_space_tag_t iot = pcihp->iot;
1193 bus_space_handle_t ioh = pcihp->ioh;
1194 bus_size_t size = pcihp->size;
1195
1196 if (pcihp->flags & PCMCIA_IO_ALLOCATED)
1197 bus_space_free(iot, ioh, size);
1198 else
1199 bus_space_unmap(iot, ioh, size);
1200 }
1201
1202
1203 static const struct io_map_index_st {
1204 int start_lsb;
1205 int start_msb;
1206 int stop_lsb;
1207 int stop_msb;
1208 int ioenable;
1209 int ioctlmask;
1210 int ioctlbits[3]; /* indexed by PCMCIA_WIDTH_* */
1211 } io_map_index[] = {
1212 {
1213 PCIC_IOADDR0_START_LSB,
1214 PCIC_IOADDR0_START_MSB,
1215 PCIC_IOADDR0_STOP_LSB,
1216 PCIC_IOADDR0_STOP_MSB,
1217 PCIC_ADDRWIN_ENABLE_IO0,
1218 PCIC_IOCTL_IO0_WAITSTATE | PCIC_IOCTL_IO0_ZEROWAIT |
1219 PCIC_IOCTL_IO0_IOCS16SRC_MASK | PCIC_IOCTL_IO0_DATASIZE_MASK,
1220 {
1221 PCIC_IOCTL_IO0_IOCS16SRC_CARD,
1222 PCIC_IOCTL_IO0_IOCS16SRC_DATASIZE |
1223 PCIC_IOCTL_IO0_DATASIZE_8BIT,
1224 PCIC_IOCTL_IO0_IOCS16SRC_DATASIZE |
1225 PCIC_IOCTL_IO0_DATASIZE_16BIT,
1226 },
1227 },
1228 {
1229 PCIC_IOADDR1_START_LSB,
1230 PCIC_IOADDR1_START_MSB,
1231 PCIC_IOADDR1_STOP_LSB,
1232 PCIC_IOADDR1_STOP_MSB,
1233 PCIC_ADDRWIN_ENABLE_IO1,
1234 PCIC_IOCTL_IO1_WAITSTATE | PCIC_IOCTL_IO1_ZEROWAIT |
1235 PCIC_IOCTL_IO1_IOCS16SRC_MASK | PCIC_IOCTL_IO1_DATASIZE_MASK,
1236 {
1237 PCIC_IOCTL_IO1_IOCS16SRC_CARD,
1238 PCIC_IOCTL_IO1_IOCS16SRC_DATASIZE |
1239 PCIC_IOCTL_IO1_DATASIZE_8BIT,
1240 PCIC_IOCTL_IO1_IOCS16SRC_DATASIZE |
1241 PCIC_IOCTL_IO1_DATASIZE_16BIT,
1242 },
1243 },
1244 };
1245
1246 void
1247 pcic_chip_do_io_map(h, win)
1248 struct pcic_handle *h;
1249 int win;
1250 {
1251 int reg;
1252
1253 DPRINTF(("pcic_chip_do_io_map win %d addr %lx size %lx width %d\n",
1254 win, (long) h->io[win].addr, (long) h->io[win].size,
1255 h->io[win].width * 8));
1256
1257 pcic_write(h, io_map_index[win].start_lsb, h->io[win].addr & 0xff);
1258 pcic_write(h, io_map_index[win].start_msb,
1259 (h->io[win].addr >> 8) & 0xff);
1260
1261 pcic_write(h, io_map_index[win].stop_lsb,
1262 (h->io[win].addr + h->io[win].size - 1) & 0xff);
1263 pcic_write(h, io_map_index[win].stop_msb,
1264 ((h->io[win].addr + h->io[win].size - 1) >> 8) & 0xff);
1265
1266 reg = pcic_read(h, PCIC_IOCTL);
1267 reg &= ~io_map_index[win].ioctlmask;
1268 reg |= io_map_index[win].ioctlbits[h->io[win].width];
1269 pcic_write(h, PCIC_IOCTL, reg);
1270
1271 reg = pcic_read(h, PCIC_ADDRWIN_ENABLE);
1272 reg |= io_map_index[win].ioenable;
1273 pcic_write(h, PCIC_ADDRWIN_ENABLE, reg);
1274 }
1275
1276 int
1277 pcic_chip_io_map(pch, width, offset, size, pcihp, windowp)
1278 pcmcia_chipset_handle_t pch;
1279 int width;
1280 bus_addr_t offset;
1281 bus_size_t size;
1282 struct pcmcia_io_handle *pcihp;
1283 int *windowp;
1284 {
1285 struct pcic_handle *h = (struct pcic_handle *) pch;
1286 bus_addr_t ioaddr = pcihp->addr + offset;
1287 int i, win;
1288 #ifdef PCICDEBUG
1289 static char *width_names[] = { "auto", "io8", "io16" };
1290 #endif
1291 struct pcic_softc *sc = (struct pcic_softc *)h->ph_parent;
1292
1293 /* XXX Sanity check offset/size. */
1294
1295 win = -1;
1296 for (i = 0; i < (sizeof(io_map_index) / sizeof(io_map_index[0])); i++) {
1297 if ((h->ioalloc & (1 << i)) == 0) {
1298 win = i;
1299 h->ioalloc |= (1 << i);
1300 break;
1301 }
1302 }
1303
1304 if (win == -1)
1305 return (1);
1306
1307 *windowp = win;
1308
1309 /* XXX this is pretty gross */
1310
1311 if (sc->iot != pcihp->iot)
1312 panic("pcic_chip_io_map iot is bogus");
1313
1314 DPRINTF(("pcic_chip_io_map window %d %s port %lx+%lx\n",
1315 win, width_names[width], (u_long) ioaddr, (u_long) size));
1316
1317 /* XXX wtf is this doing here? */
1318
1319 printf(" port 0x%lx", (u_long) ioaddr);
1320 if (size > 1)
1321 printf("-0x%lx", (u_long) ioaddr + (u_long) size - 1);
1322
1323 h->io[win].addr = ioaddr;
1324 h->io[win].size = size;
1325 h->io[win].width = width;
1326
1327 pcic_chip_do_io_map(h, win);
1328
1329 return (0);
1330 }
1331
1332 void
1333 pcic_chip_io_unmap(pch, window)
1334 pcmcia_chipset_handle_t pch;
1335 int window;
1336 {
1337 struct pcic_handle *h = (struct pcic_handle *) pch;
1338 int reg;
1339
1340 if (window >= (sizeof(io_map_index) / sizeof(io_map_index[0])))
1341 panic("pcic_chip_io_unmap: window out of range");
1342
1343 reg = pcic_read(h, PCIC_ADDRWIN_ENABLE);
1344 reg &= ~io_map_index[window].ioenable;
1345 pcic_write(h, PCIC_ADDRWIN_ENABLE, reg);
1346
1347 h->ioalloc &= ~(1 << window);
1348 }
1349
1350 static void
1351 pcic_wait_ready(h)
1352 struct pcic_handle *h;
1353 {
1354 int i;
1355
1356 /* wait an initial 10ms for quick cards */
1357 if (pcic_read(h, PCIC_IF_STATUS) & PCIC_IF_STATUS_READY)
1358 return;
1359 pcic_delay(h, 10, "pccwr0");
1360 for (i = 0; i < 50; i++) {
1361 if (pcic_read(h, PCIC_IF_STATUS) & PCIC_IF_STATUS_READY)
1362 return;
1363 /* wait .1s (100ms) each iteration now */
1364 pcic_delay(h, 100, "pccwr1");
1365 #ifdef PCICDEBUG
1366 if (pcic_debug) {
1367 if ((i > 20) && (i % 100 == 99))
1368 printf(".");
1369 }
1370 #endif
1371 }
1372
1373 #ifdef DIAGNOSTIC
1374 printf("pcic_wait_ready: ready never happened, status = %02x\n",
1375 pcic_read(h, PCIC_IF_STATUS));
1376 #endif
1377 }
1378
1379 /*
1380 * Perform long (msec order) delay.
1381 */
1382 static void
1383 pcic_delay(h, timo, wmesg)
1384 struct pcic_handle *h;
1385 int timo; /* in ms. must not be zero */
1386 const char *wmesg;
1387 {
1388
1389 #ifdef DIAGNOSTIC
1390 if (timo <= 0) {
1391 printf("called with timeout %d\n", timo);
1392 panic("pcic_delay");
1393 }
1394 if (curlwp == NULL) {
1395 printf("called in interrupt context\n");
1396 panic("pcic_delay");
1397 }
1398 if (h->event_thread == NULL) {
1399 printf("no event thread\n");
1400 panic("pcic_delay");
1401 }
1402 #endif
1403 DPRINTF(("pcic_delay: \"%s\" %p, sleep %d ms\n",
1404 wmesg, h->event_thread, timo));
1405 tsleep(pcic_delay, PWAIT, wmesg, roundup(timo * hz, 1000) / 1000);
1406 }
1407
1408 void
1409 pcic_chip_socket_enable(pch)
1410 pcmcia_chipset_handle_t pch;
1411 {
1412 struct pcic_handle *h = (struct pcic_handle *) pch;
1413 int cardtype, win, intr, pwr;
1414 int vcc_3v, regtmp;
1415 #if defined(DIAGNOSTIC) || defined(PCICDEBUG)
1416 int reg;
1417 #endif
1418
1419 #ifdef DIAGNOSTIC
1420 if (h->flags & PCIC_FLAG_ENABLED)
1421 printf("pcic_chip_socket_enable: enabling twice\n");
1422 #endif
1423
1424 /* disable interrupts */
1425 intr = pcic_read(h, PCIC_INTR);
1426 intr &= ~PCIC_INTR_IRQ_MASK;
1427 pcic_write(h, PCIC_INTR, intr);
1428
1429 /* power down the socket to reset it, clear the card reset pin */
1430 pwr = 0;
1431 pcic_write(h, PCIC_PWRCTL, pwr);
1432
1433 /*
1434 * wait 300ms until power fails (Tpf). Then, wait 100ms since
1435 * we are changing Vcc (Toff).
1436 */
1437 pcic_delay(h, 300 + 100, "pccen0");
1438
1439 /*
1440 * power hack for RICOH RF5C[23]96
1441 */
1442 switch( h->vendor ) {
1443 case PCIC_VENDOR_RICOH_5C296:
1444 case PCIC_VENDOR_RICOH_5C396:
1445 vcc_3v = 0;
1446 regtmp = pcic_read(h, PCIC_CARD_DETECT);
1447 if(regtmp & PCIC_CARD_DETECT_GPI_ENABLE) {
1448 DPRINTF(("\nGPI is enabled. Can't sense VS1\n"));
1449 } else {
1450 regtmp = pcic_read(h, PCIC_IF_STATUS) ;
1451 vcc_3v = (regtmp & PCIC_IF_STATUS_GPI) ? 1 : 0;
1452 DPRINTF(("\n5VDET = %s\n",
1453 vcc_3v ? "1 (3.3V)" : "0 (5V)"));
1454 }
1455
1456 regtmp = pcic_read(h, PCIC_RICOH_REG_MCR2);
1457 regtmp &= ~PCIC_RICOH_MCR2_VCC_SEL_MASK;
1458 if(vcc_3v) {
1459 regtmp |= PCIC_RICOH_MCR2_VCC_SEL_3V;
1460 } else {
1461 regtmp |= PCIC_RICOH_MCR2_VCC_SEL_5V;
1462 }
1463 pcic_write(h, PCIC_RICOH_REG_MCR2, regtmp);
1464 break;
1465 default:
1466 break;
1467 }
1468
1469 #ifdef VADEM_POWER_HACK
1470 bus_space_write_1(sc->iot, sc->ioh, PCIC_REG_INDEX, 0x0e);
1471 bus_space_write_1(sc->iot, sc->ioh, PCIC_REG_INDEX, 0x37);
1472 printf("prcr = %02x\n", pcic_read(h, 0x02));
1473 printf("cvsr = %02x\n", pcic_read(h, 0x2f));
1474 printf("DANGER WILL ROBINSON! Changing voltage select!\n");
1475 pcic_write(h, 0x2f, pcic_read(h, 0x2f) & ~0x03);
1476 printf("cvsr = %02x\n", pcic_read(h, 0x2f));
1477 #endif
1478 /* power up the socket */
1479 pwr |= PCIC_PWRCTL_DISABLE_RESETDRV | PCIC_PWRCTL_PWR_ENABLE | PCIC_PWRCTL_VPP1_VCC;
1480 pcic_write(h, PCIC_PWRCTL, pwr);
1481
1482 /*
1483 * wait 100ms until power raise (Tpr) and 20ms to become
1484 * stable (Tsu(Vcc)).
1485 *
1486 * some machines require some more time to be settled
1487 * (300ms is added here).
1488 */
1489 pcic_delay(h, 100 + 20 + 300, "pccen1");
1490 pwr |= PCIC_PWRCTL_OE;
1491 pcic_write(h, PCIC_PWRCTL, pwr);
1492
1493 /* now make sure we have reset# active */
1494 intr &= ~PCIC_INTR_RESET;
1495 pcic_write(h, PCIC_INTR, intr);
1496
1497 pcic_write(h, PCIC_PWRCTL, PCIC_PWRCTL_DISABLE_RESETDRV |
1498 PCIC_PWRCTL_OE | PCIC_PWRCTL_PWR_ENABLE | PCIC_PWRCTL_VPP1_VCC);
1499 /*
1500 * hold RESET at least 10us, this is a min allow for slop in
1501 * delay routine.
1502 */
1503 delay(20);
1504
1505 /* clear the reset flag */
1506 intr |= PCIC_INTR_RESET;
1507 pcic_write(h, PCIC_INTR, intr);
1508
1509 /* wait 20ms as per pc card standard (r2.01) section 4.3.6 */
1510 pcic_delay(h, 20, "pccen2");
1511
1512 #if defined(DIAGNOSTIC) || defined(PCICDEBUG)
1513 reg = pcic_read(h, PCIC_IF_STATUS);
1514 #endif
1515 #ifdef DIAGNOSTIC
1516 if (!(reg & PCIC_IF_STATUS_POWERACTIVE)) {
1517 printf("pcic_chip_socket_enable: status %x\n", reg);
1518 }
1519 #endif
1520 /* wait for the chip to finish initializing */
1521 pcic_wait_ready(h);
1522
1523 /* zero out the address windows */
1524 pcic_write(h, PCIC_ADDRWIN_ENABLE, 0);
1525
1526 /* set the card type and enable the interrupt */
1527 cardtype = pcmcia_card_gettype(h->pcmcia);
1528 intr |= ((cardtype == PCMCIA_IFTYPE_IO) ?
1529 PCIC_INTR_CARDTYPE_IO : PCIC_INTR_CARDTYPE_MEM);
1530 pcic_write(h, PCIC_INTR, intr);
1531
1532 DPRINTF(("%s: pcic_chip_socket_enable %02x cardtype %s %02x\n",
1533 h->ph_parent->dv_xname, h->sock,
1534 ((cardtype == PCMCIA_IFTYPE_IO) ? "io" : "mem"), reg));
1535
1536 /* reinstall all the memory and io mappings */
1537 for (win = 0; win < PCIC_MEM_WINS; win++)
1538 if (h->memalloc & (1 << win))
1539 pcic_chip_do_mem_map(h, win);
1540 for (win = 0; win < PCIC_IO_WINS; win++)
1541 if (h->ioalloc & (1 << win))
1542 pcic_chip_do_io_map(h, win);
1543
1544 h->flags |= PCIC_FLAG_ENABLED;
1545
1546 /* finally enable the interrupt */
1547 intr |= h->ih_irq;
1548 pcic_write(h, PCIC_INTR, intr);
1549 }
1550
1551 void
1552 pcic_chip_socket_disable(pch)
1553 pcmcia_chipset_handle_t pch;
1554 {
1555 struct pcic_handle *h = (struct pcic_handle *) pch;
1556 int intr;
1557
1558 DPRINTF(("pcic_chip_socket_disable\n"));
1559
1560 /* disable interrupts */
1561 intr = pcic_read(h, PCIC_INTR);
1562 intr &= ~PCIC_INTR_IRQ_MASK;
1563 pcic_write(h, PCIC_INTR, intr);
1564
1565 /* power down the socket */
1566 pcic_write(h, PCIC_PWRCTL, 0);
1567
1568 /* zero out the address windows */
1569 pcic_write(h, PCIC_ADDRWIN_ENABLE, 0);
1570
1571 h->flags &= ~PCIC_FLAG_ENABLED;
1572 }
1573
1574 static u_int8_t
1575 st_pcic_read(h, idx)
1576 struct pcic_handle *h;
1577 int idx;
1578 {
1579
1580 if (idx != -1)
1581 bus_space_write_1(h->ph_bus_t, h->ph_bus_h, PCIC_REG_INDEX,
1582 h->sock + idx);
1583 return (bus_space_read_1(h->ph_bus_t, h->ph_bus_h, PCIC_REG_DATA));
1584 }
1585
1586 static void
1587 st_pcic_write(h, idx, data)
1588 struct pcic_handle *h;
1589 int idx;
1590 u_int8_t data;
1591 {
1592
1593 if (idx != -1)
1594 bus_space_write_1(h->ph_bus_t, h->ph_bus_h, PCIC_REG_INDEX,
1595 h->sock + idx);
1596 bus_space_write_1(h->ph_bus_t, h->ph_bus_h, PCIC_REG_DATA, data);
1597 }
1598