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