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