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