i82365.c revision 1.105 1 /* $NetBSD: i82365.c,v 1.105 2009/03/14 15:36:17 dsl 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.105 2009/03/14 15:36:17 dsl 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 = (struct device *)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(h, flags)
761 struct pcic_handle *h;
762 int flags; /* DETACH_* */
763 {
764
765 if (h->flags & PCIC_FLAG_CARDP) {
766 h->flags &= ~PCIC_FLAG_CARDP;
767
768 /* call the MI detach function */
769 pcmcia_card_detach(h->pcmcia, flags);
770 } else {
771 DPRINTF(("pcic_detach_card: already detached"));
772 }
773 }
774
775 void
776 pcic_deactivate_card(struct pcic_handle *h)
777 {
778 int intr;
779
780 /* call the MI deactivate function */
781 pcmcia_card_deactivate(h->pcmcia);
782
783 /* reset the socket */
784 intr = pcic_read(h, PCIC_INTR);
785 intr &= PCIC_INTR_ENABLE;
786 pcic_write(h, PCIC_INTR, intr);
787
788 /* power down the socket */
789 pcic_write(h, PCIC_PWRCTL, 0);
790 }
791
792 int
793 pcic_chip_mem_alloc(pcmcia_chipset_handle_t pch, bus_size_t size, struct pcmcia_mem_handle *pcmhp)
794 {
795 struct pcic_handle *h = (struct pcic_handle *) pch;
796 bus_space_handle_t memh;
797 bus_addr_t addr;
798 bus_size_t sizepg;
799 int i, mask, mhandle;
800 struct pcic_softc *sc = (struct pcic_softc *)h->ph_parent;
801
802 /* out of sc->memh, allocate as many pages as necessary */
803
804 /* convert size to PCIC pages */
805 sizepg = (size + (PCIC_MEM_ALIGN - 1)) / PCIC_MEM_ALIGN;
806 if (sizepg > PCIC_MAX_MEM_PAGES)
807 return (1);
808
809 mask = (1 << sizepg) - 1;
810
811 addr = 0; /* XXX gcc -Wuninitialized */
812 mhandle = 0; /* XXX gcc -Wuninitialized */
813
814 for (i = 0; i <= PCIC_MAX_MEM_PAGES - sizepg; i++) {
815 if ((sc->subregionmask & (mask << i)) == (mask << i)) {
816 if (bus_space_subregion(sc->memt, sc->memh,
817 i * PCIC_MEM_PAGESIZE,
818 sizepg * PCIC_MEM_PAGESIZE, &memh))
819 return (1);
820 mhandle = mask << i;
821 addr = sc->membase + (i * PCIC_MEM_PAGESIZE);
822 sc->subregionmask &= ~(mhandle);
823 pcmhp->memt = sc->memt;
824 pcmhp->memh = memh;
825 pcmhp->addr = addr;
826 pcmhp->size = size;
827 pcmhp->mhandle = mhandle;
828 pcmhp->realsize = sizepg * PCIC_MEM_PAGESIZE;
829 return (0);
830 }
831 }
832
833 return (1);
834 }
835
836 void
837 pcic_chip_mem_free(pcmcia_chipset_handle_t pch, struct pcmcia_mem_handle *pcmhp)
838 {
839 struct pcic_handle *h = (struct pcic_handle *) pch;
840 struct pcic_softc *sc = (struct pcic_softc *)h->ph_parent;
841
842 sc->subregionmask |= pcmhp->mhandle;
843 }
844
845 static const struct mem_map_index_st {
846 int sysmem_start_lsb;
847 int sysmem_start_msb;
848 int sysmem_stop_lsb;
849 int sysmem_stop_msb;
850 int cardmem_lsb;
851 int cardmem_msb;
852 int memenable;
853 } mem_map_index[] = {
854 {
855 PCIC_SYSMEM_ADDR0_START_LSB,
856 PCIC_SYSMEM_ADDR0_START_MSB,
857 PCIC_SYSMEM_ADDR0_STOP_LSB,
858 PCIC_SYSMEM_ADDR0_STOP_MSB,
859 PCIC_CARDMEM_ADDR0_LSB,
860 PCIC_CARDMEM_ADDR0_MSB,
861 PCIC_ADDRWIN_ENABLE_MEM0,
862 },
863 {
864 PCIC_SYSMEM_ADDR1_START_LSB,
865 PCIC_SYSMEM_ADDR1_START_MSB,
866 PCIC_SYSMEM_ADDR1_STOP_LSB,
867 PCIC_SYSMEM_ADDR1_STOP_MSB,
868 PCIC_CARDMEM_ADDR1_LSB,
869 PCIC_CARDMEM_ADDR1_MSB,
870 PCIC_ADDRWIN_ENABLE_MEM1,
871 },
872 {
873 PCIC_SYSMEM_ADDR2_START_LSB,
874 PCIC_SYSMEM_ADDR2_START_MSB,
875 PCIC_SYSMEM_ADDR2_STOP_LSB,
876 PCIC_SYSMEM_ADDR2_STOP_MSB,
877 PCIC_CARDMEM_ADDR2_LSB,
878 PCIC_CARDMEM_ADDR2_MSB,
879 PCIC_ADDRWIN_ENABLE_MEM2,
880 },
881 {
882 PCIC_SYSMEM_ADDR3_START_LSB,
883 PCIC_SYSMEM_ADDR3_START_MSB,
884 PCIC_SYSMEM_ADDR3_STOP_LSB,
885 PCIC_SYSMEM_ADDR3_STOP_MSB,
886 PCIC_CARDMEM_ADDR3_LSB,
887 PCIC_CARDMEM_ADDR3_MSB,
888 PCIC_ADDRWIN_ENABLE_MEM3,
889 },
890 {
891 PCIC_SYSMEM_ADDR4_START_LSB,
892 PCIC_SYSMEM_ADDR4_START_MSB,
893 PCIC_SYSMEM_ADDR4_STOP_LSB,
894 PCIC_SYSMEM_ADDR4_STOP_MSB,
895 PCIC_CARDMEM_ADDR4_LSB,
896 PCIC_CARDMEM_ADDR4_MSB,
897 PCIC_ADDRWIN_ENABLE_MEM4,
898 },
899 };
900
901 void
902 pcic_chip_do_mem_map(struct pcic_handle *h, int win)
903 {
904 int reg;
905 int kind = h->mem[win].kind & ~PCMCIA_WIDTH_MEM_MASK;
906 int mem8 =
907 (h->mem[win].kind & PCMCIA_WIDTH_MEM_MASK) == PCMCIA_WIDTH_MEM8
908 || (kind == PCMCIA_MEM_ATTR);
909
910 DPRINTF(("mem8 %d\n", mem8));
911 /* mem8 = 1; */
912
913 pcic_write(h, mem_map_index[win].sysmem_start_lsb,
914 (h->mem[win].addr >> PCIC_SYSMEM_ADDRX_SHIFT) & 0xff);
915 pcic_write(h, mem_map_index[win].sysmem_start_msb,
916 ((h->mem[win].addr >> (PCIC_SYSMEM_ADDRX_SHIFT + 8)) &
917 PCIC_SYSMEM_ADDRX_START_MSB_ADDR_MASK) |
918 (mem8 ? 0 : PCIC_SYSMEM_ADDRX_START_MSB_DATASIZE_16BIT));
919
920 pcic_write(h, mem_map_index[win].sysmem_stop_lsb,
921 ((h->mem[win].addr + h->mem[win].size) >>
922 PCIC_SYSMEM_ADDRX_SHIFT) & 0xff);
923 pcic_write(h, mem_map_index[win].sysmem_stop_msb,
924 (((h->mem[win].addr + h->mem[win].size) >>
925 (PCIC_SYSMEM_ADDRX_SHIFT + 8)) &
926 PCIC_SYSMEM_ADDRX_STOP_MSB_ADDR_MASK) |
927 PCIC_SYSMEM_ADDRX_STOP_MSB_WAIT2);
928
929 pcic_write(h, mem_map_index[win].cardmem_lsb,
930 (h->mem[win].offset >> PCIC_CARDMEM_ADDRX_SHIFT) & 0xff);
931 pcic_write(h, mem_map_index[win].cardmem_msb,
932 ((h->mem[win].offset >> (PCIC_CARDMEM_ADDRX_SHIFT + 8)) &
933 PCIC_CARDMEM_ADDRX_MSB_ADDR_MASK) |
934 ((kind == PCMCIA_MEM_ATTR) ?
935 PCIC_CARDMEM_ADDRX_MSB_REGACTIVE_ATTR : 0));
936
937 reg = pcic_read(h, PCIC_ADDRWIN_ENABLE);
938 reg |= (mem_map_index[win].memenable | PCIC_ADDRWIN_ENABLE_MEMCS16);
939 pcic_write(h, PCIC_ADDRWIN_ENABLE, reg);
940
941 delay(100);
942
943 #ifdef PCICDEBUG
944 {
945 int r1, r2, r3, r4, r5, r6;
946
947 r1 = pcic_read(h, mem_map_index[win].sysmem_start_msb);
948 r2 = pcic_read(h, mem_map_index[win].sysmem_start_lsb);
949 r3 = pcic_read(h, mem_map_index[win].sysmem_stop_msb);
950 r4 = pcic_read(h, mem_map_index[win].sysmem_stop_lsb);
951 r5 = pcic_read(h, mem_map_index[win].cardmem_msb);
952 r6 = pcic_read(h, mem_map_index[win].cardmem_lsb);
953
954 DPRINTF(("pcic_chip_do_mem_map window %d: %02x%02x %02x%02x "
955 "%02x%02x\n", win, r1, r2, r3, r4, r5, r6));
956 }
957 #endif
958 }
959
960 int
961 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)
962 {
963 struct pcic_handle *h = (struct pcic_handle *) pch;
964 bus_addr_t busaddr;
965 long card_offset;
966 int i, win;
967 struct pcic_softc *sc = (struct pcic_softc *)h->ph_parent;
968
969 win = -1;
970 for (i = 0; i < (sizeof(mem_map_index) / sizeof(mem_map_index[0]));
971 i++) {
972 if ((h->memalloc & (1 << i)) == 0) {
973 win = i;
974 h->memalloc |= (1 << i);
975 break;
976 }
977 }
978
979 if (win == -1)
980 return (1);
981
982 *windowp = win;
983
984 /* XXX this is pretty gross */
985
986 if (sc->memt != pcmhp->memt)
987 panic("pcic_chip_mem_map memt is bogus");
988
989 busaddr = pcmhp->addr;
990
991 /*
992 * compute the address offset to the pcmcia address space for the
993 * pcic. this is intentionally signed. The masks and shifts below
994 * will cause TRT to happen in the pcic registers. Deal with making
995 * sure the address is aligned, and return the alignment offset.
996 */
997
998 *offsetp = card_addr % PCIC_MEM_ALIGN;
999 card_addr -= *offsetp;
1000
1001 DPRINTF(("pcic_chip_mem_map window %d bus %lx+%lx+%lx at card addr "
1002 "%lx\n", win, (u_long) busaddr, (u_long) * offsetp, (u_long) size,
1003 (u_long) card_addr));
1004
1005 /*
1006 * include the offset in the size, and decrement size by one, since
1007 * the hw wants start/stop
1008 */
1009 size += *offsetp - 1;
1010
1011 card_offset = (((long) card_addr) - ((long) busaddr));
1012
1013 h->mem[win].addr = busaddr;
1014 h->mem[win].size = size;
1015 h->mem[win].offset = card_offset;
1016 h->mem[win].kind = kind;
1017
1018 pcic_chip_do_mem_map(h, win);
1019
1020 return (0);
1021 }
1022
1023 void
1024 pcic_chip_mem_unmap(pcmcia_chipset_handle_t pch, int window)
1025 {
1026 struct pcic_handle *h = (struct pcic_handle *) pch;
1027 int reg;
1028
1029 if (window >= (sizeof(mem_map_index) / sizeof(mem_map_index[0])))
1030 panic("pcic_chip_mem_unmap: window out of range");
1031
1032 reg = pcic_read(h, PCIC_ADDRWIN_ENABLE);
1033 reg &= ~mem_map_index[window].memenable;
1034 pcic_write(h, PCIC_ADDRWIN_ENABLE, reg);
1035
1036 h->memalloc &= ~(1 << window);
1037 }
1038
1039 int
1040 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)
1041 {
1042 struct pcic_handle *h = (struct pcic_handle *) pch;
1043 bus_space_tag_t iot;
1044 bus_space_handle_t ioh;
1045 bus_addr_t ioaddr;
1046 int flags = 0;
1047 struct pcic_softc *sc = (struct pcic_softc *)h->ph_parent;
1048
1049 /*
1050 * Allocate some arbitrary I/O space.
1051 */
1052
1053 iot = sc->iot;
1054
1055 if (start) {
1056 ioaddr = start;
1057 if (bus_space_map(iot, start, size, 0, &ioh))
1058 return (1);
1059 DPRINTF(("pcic_chip_io_alloc map port %lx+%lx\n",
1060 (u_long) ioaddr, (u_long) size));
1061 } else {
1062 flags |= PCMCIA_IO_ALLOCATED;
1063 if (bus_space_alloc(iot, sc->iobase,
1064 sc->iobase + sc->iosize, size, align, 0, 0,
1065 &ioaddr, &ioh))
1066 return (1);
1067 DPRINTF(("pcic_chip_io_alloc alloc port %lx+%lx\n",
1068 (u_long) ioaddr, (u_long) size));
1069 }
1070
1071 pcihp->iot = iot;
1072 pcihp->ioh = ioh;
1073 pcihp->addr = ioaddr;
1074 pcihp->size = size;
1075 pcihp->flags = flags;
1076
1077 return (0);
1078 }
1079
1080 void
1081 pcic_chip_io_free(pcmcia_chipset_handle_t pch,
1082 struct pcmcia_io_handle *pcihp)
1083 {
1084 bus_space_tag_t iot = pcihp->iot;
1085 bus_space_handle_t ioh = pcihp->ioh;
1086 bus_size_t size = pcihp->size;
1087
1088 if (pcihp->flags & PCMCIA_IO_ALLOCATED)
1089 bus_space_free(iot, ioh, size);
1090 else
1091 bus_space_unmap(iot, ioh, size);
1092 }
1093
1094
1095 static const struct io_map_index_st {
1096 int start_lsb;
1097 int start_msb;
1098 int stop_lsb;
1099 int stop_msb;
1100 int ioenable;
1101 int ioctlmask;
1102 int ioctlbits[3]; /* indexed by PCMCIA_WIDTH_* */
1103 } io_map_index[] = {
1104 {
1105 PCIC_IOADDR0_START_LSB,
1106 PCIC_IOADDR0_START_MSB,
1107 PCIC_IOADDR0_STOP_LSB,
1108 PCIC_IOADDR0_STOP_MSB,
1109 PCIC_ADDRWIN_ENABLE_IO0,
1110 PCIC_IOCTL_IO0_WAITSTATE | PCIC_IOCTL_IO0_ZEROWAIT |
1111 PCIC_IOCTL_IO0_IOCS16SRC_MASK | PCIC_IOCTL_IO0_DATASIZE_MASK,
1112 {
1113 PCIC_IOCTL_IO0_IOCS16SRC_CARD,
1114 PCIC_IOCTL_IO0_IOCS16SRC_DATASIZE |
1115 PCIC_IOCTL_IO0_DATASIZE_8BIT,
1116 PCIC_IOCTL_IO0_IOCS16SRC_DATASIZE |
1117 PCIC_IOCTL_IO0_DATASIZE_16BIT,
1118 },
1119 },
1120 {
1121 PCIC_IOADDR1_START_LSB,
1122 PCIC_IOADDR1_START_MSB,
1123 PCIC_IOADDR1_STOP_LSB,
1124 PCIC_IOADDR1_STOP_MSB,
1125 PCIC_ADDRWIN_ENABLE_IO1,
1126 PCIC_IOCTL_IO1_WAITSTATE | PCIC_IOCTL_IO1_ZEROWAIT |
1127 PCIC_IOCTL_IO1_IOCS16SRC_MASK | PCIC_IOCTL_IO1_DATASIZE_MASK,
1128 {
1129 PCIC_IOCTL_IO1_IOCS16SRC_CARD,
1130 PCIC_IOCTL_IO1_IOCS16SRC_DATASIZE |
1131 PCIC_IOCTL_IO1_DATASIZE_8BIT,
1132 PCIC_IOCTL_IO1_IOCS16SRC_DATASIZE |
1133 PCIC_IOCTL_IO1_DATASIZE_16BIT,
1134 },
1135 },
1136 };
1137
1138 void
1139 pcic_chip_do_io_map(struct pcic_handle *h, int win)
1140 {
1141 int reg;
1142
1143 DPRINTF(("pcic_chip_do_io_map win %d addr %lx size %lx width %d\n",
1144 win, (long) h->io[win].addr, (long) h->io[win].size,
1145 h->io[win].width * 8));
1146
1147 pcic_write(h, io_map_index[win].start_lsb, h->io[win].addr & 0xff);
1148 pcic_write(h, io_map_index[win].start_msb,
1149 (h->io[win].addr >> 8) & 0xff);
1150
1151 pcic_write(h, io_map_index[win].stop_lsb,
1152 (h->io[win].addr + h->io[win].size - 1) & 0xff);
1153 pcic_write(h, io_map_index[win].stop_msb,
1154 ((h->io[win].addr + h->io[win].size - 1) >> 8) & 0xff);
1155
1156 reg = pcic_read(h, PCIC_IOCTL);
1157 reg &= ~io_map_index[win].ioctlmask;
1158 reg |= io_map_index[win].ioctlbits[h->io[win].width];
1159 pcic_write(h, PCIC_IOCTL, reg);
1160
1161 reg = pcic_read(h, PCIC_ADDRWIN_ENABLE);
1162 reg |= io_map_index[win].ioenable;
1163 pcic_write(h, PCIC_ADDRWIN_ENABLE, reg);
1164 }
1165
1166 int
1167 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)
1168 {
1169 struct pcic_handle *h = (struct pcic_handle *) pch;
1170 bus_addr_t ioaddr = pcihp->addr + offset;
1171 int i, win;
1172 #ifdef PCICDEBUG
1173 static const char *width_names[] = { "auto", "io8", "io16" };
1174 #endif
1175 struct pcic_softc *sc = (struct pcic_softc *)h->ph_parent;
1176
1177 /* XXX Sanity check offset/size. */
1178
1179 win = -1;
1180 for (i = 0; i < (sizeof(io_map_index) / sizeof(io_map_index[0])); i++) {
1181 if ((h->ioalloc & (1 << i)) == 0) {
1182 win = i;
1183 h->ioalloc |= (1 << i);
1184 break;
1185 }
1186 }
1187
1188 if (win == -1)
1189 return (1);
1190
1191 *windowp = win;
1192
1193 /* XXX this is pretty gross */
1194
1195 if (sc->iot != pcihp->iot)
1196 panic("pcic_chip_io_map iot is bogus");
1197
1198 DPRINTF(("pcic_chip_io_map window %d %s port %lx+%lx\n",
1199 win, width_names[width], (u_long) ioaddr, (u_long) size));
1200
1201 /* XXX wtf is this doing here? */
1202
1203 printf("%s: port 0x%lx", device_xname(&sc->dev), (u_long) ioaddr);
1204 if (size > 1)
1205 printf("-0x%lx", (u_long) ioaddr + (u_long) size - 1);
1206 printf("\n");
1207
1208 h->io[win].addr = ioaddr;
1209 h->io[win].size = size;
1210 h->io[win].width = width;
1211
1212 pcic_chip_do_io_map(h, win);
1213
1214 return (0);
1215 }
1216
1217 void
1218 pcic_chip_io_unmap(pcmcia_chipset_handle_t pch, int window)
1219 {
1220 struct pcic_handle *h = (struct pcic_handle *) pch;
1221 int reg;
1222
1223 if (window >= (sizeof(io_map_index) / sizeof(io_map_index[0])))
1224 panic("pcic_chip_io_unmap: window out of range");
1225
1226 reg = pcic_read(h, PCIC_ADDRWIN_ENABLE);
1227 reg &= ~io_map_index[window].ioenable;
1228 pcic_write(h, PCIC_ADDRWIN_ENABLE, reg);
1229
1230 h->ioalloc &= ~(1 << window);
1231 }
1232
1233 static int
1234 pcic_wait_ready(struct pcic_handle *h)
1235 {
1236 u_int8_t stat;
1237 int i;
1238
1239 /* wait an initial 10ms for quick cards */
1240 stat = pcic_read(h, PCIC_IF_STATUS);
1241 if (stat & PCIC_IF_STATUS_READY)
1242 return (0);
1243 pcic_delay(h, 10, "pccwr0");
1244 for (i = 0; i < 50; i++) {
1245 stat = pcic_read(h, PCIC_IF_STATUS);
1246 if (stat & PCIC_IF_STATUS_READY)
1247 return (0);
1248 if ((stat & PCIC_IF_STATUS_CARDDETECT_MASK) !=
1249 PCIC_IF_STATUS_CARDDETECT_PRESENT)
1250 return (ENXIO);
1251 /* wait .1s (100ms) each iteration now */
1252 pcic_delay(h, 100, "pccwr1");
1253 }
1254
1255 printf("pcic_wait_ready: ready never happened, status=%02x\n", stat);
1256 return (EWOULDBLOCK);
1257 }
1258
1259 /*
1260 * Perform long (msec order) delay.
1261 */
1262 static void
1263 pcic_delay(h, timo, wmesg)
1264 struct pcic_handle *h;
1265 int timo; /* in ms. must not be zero */
1266 const char *wmesg;
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 u_int8_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 u_int8_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 u_int8_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, u_int8_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