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