xref: /netbsd-src/sys/dev/ic/tcic2.c (revision 404fbe5fb94ca1e054339640cabb2801ce52dd30)
1 /*	$NetBSD: tcic2.c,v 1.30 2008/04/08 12:07:27 cegger Exp $	*/
2 
3 /*
4  * Copyright (c) 1998, 1999 Christoph Badura.  All rights reserved.
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/cdefs.h>
34 __KERNEL_RCSID(0, "$NetBSD: tcic2.c,v 1.30 2008/04/08 12:07:27 cegger Exp $");
35 
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/device.h>
39 #include <sys/extent.h>
40 #include <sys/malloc.h>
41 #include <sys/kthread.h>
42 
43 #include <sys/bus.h>
44 #include <sys/intr.h>
45 
46 #include <dev/pcmcia/pcmciareg.h>
47 #include <dev/pcmcia/pcmciavar.h>
48 
49 #include <dev/ic/tcic2reg.h>
50 #include <dev/ic/tcic2var.h>
51 
52 #include "locators.h"
53 
54 #ifdef TCICDEBUG
55 int	tcic_debug = 1;
56 #define	DPRINTF(arg) if (tcic_debug) printf arg;
57 #else
58 #define	DPRINTF(arg)
59 #endif
60 
61 /*
62  * Individual drivers will allocate their own memory and io regions. Memory
63  * regions must be a multiple of 4k, aligned on a 4k boundary.
64  */
65 
66 #define	TCIC_MEM_ALIGN	TCIC_MEM_PAGESIZE
67 
68 void	tcic_attach_socket(struct tcic_handle *);
69 void	tcic_init_socket(struct tcic_handle *);
70 
71 int	tcic_print(void *arg, const char *pnp);
72 int	tcic_intr_socket(struct tcic_handle *);
73 
74 void	tcic_attach_card(struct tcic_handle *);
75 void	tcic_detach_card(struct tcic_handle *, int);
76 void	tcic_deactivate_card(struct tcic_handle *);
77 
78 void	tcic_chip_do_mem_map(struct tcic_handle *, int);
79 void	tcic_chip_do_io_map(struct tcic_handle *, int);
80 
81 void	tcic_create_event_thread(void *);
82 void	tcic_event_thread(void *);
83 
84 void	tcic_queue_event(struct tcic_handle *, int);
85 
86 /* Map between irq numbers and internal representation */
87 #if 1
88 int tcic_irqmap[] =
89     { 0, 0, 0, 3, 4, 5, 6, 7, 0, 0, 10, 1, 0, 0, 14, 0 };
90 int tcic_valid_irqs = 0x4cf8;
91 #else
92 int tcic_irqmap[] =	/* irqs 9 and 6 switched, some ISA cards */
93     { 0, 0, 0, 3, 4, 5, 0, 7, 0, 6, 10, 1, 0, 0, 14, 0 };
94 int tcic_valid_irqs = 0x4eb8;
95 #endif
96 
97 int tcic_mem_speed = 250;	/* memory access time in nanoseconds */
98 int tcic_io_speed = 165;	/* io access time in nanoseconds */
99 
100 /*
101  * Check various reserved and otherwise in their value restricted bits.
102  */
103 int
104 tcic_check_reserved_bits(iot, ioh)
105 	bus_space_tag_t iot;
106 	bus_space_handle_t ioh;
107 {
108 	int val, auxreg;
109 
110 	DPRINTF(("tcic: chkrsvd 1\n"));
111 	/* R_ADDR bit 30:28 have a restricted range. */
112 	val = (bus_space_read_2(iot, ioh, TCIC_R_ADDR2) & TCIC_SS_MASK)
113 	    >> TCIC_SS_SHIFT;
114 	if (val > 1)
115 		return 0;
116 
117 	DPRINTF(("tcic: chkrsvd 2\n"));
118 	/* R_SCTRL bits 6,2,1 are reserved. */
119 	val = bus_space_read_1(iot, ioh, TCIC_R_SCTRL);
120 	if (val & TCIC_SCTRL_RSVD)
121 		return 0;
122 
123 	DPRINTF(("tcic: chkrsvd 3\n"));
124 	/* R_ICSR bit 2 must be same as bit 3. */
125 	val = bus_space_read_1(iot, ioh, TCIC_R_ICSR);
126 	if (((val >> 1) & 1) != ((val >> 2) & 1))
127 		return 0;
128 
129 	DPRINTF(("tcic: chkrsvd 4\n"));
130 	/* R_IENA bits 7,2 are reserverd. */
131 	val = bus_space_read_1(iot, ioh, TCIC_R_IENA);
132 	if (val & TCIC_IENA_RSVD)
133 		return 0;
134 
135 	DPRINTF(("tcic: chkrsvd 5\n"));
136 	/* Some aux registers have reserved bits. */
137 	/* Which are we looking at? */
138 	auxreg = bus_space_read_1(iot, ioh, TCIC_R_MODE)
139 	    & TCIC_AR_MASK;
140 	val = bus_space_read_2(iot, ioh, TCIC_R_AUX);
141 	DPRINTF(("tcic: auxreg 0x%02x val 0x%04x\n", auxreg, val));
142 	switch (auxreg) {
143 	case TCIC_AR_SYSCFG:
144 		if (INVALID_AR_SYSCFG(val))
145 			return 0;
146 		break;
147 	case TCIC_AR_ILOCK:
148 		if (INVALID_AR_ILOCK(val))
149 			return 0;
150 		break;
151 	case TCIC_AR_TEST:
152 		if (INVALID_AR_TEST(val))
153 			return 0;
154 		break;
155 	}
156 
157 	DPRINTF(("tcic: chkrsvd 6\n"));
158 	/* XXX fails if pcmcia bios is enabled. */
159 	/* Various bits set or not depending if in RESET mode. */
160 	val = bus_space_read_1(iot, ioh, TCIC_R_SCTRL);
161 	if (val & TCIC_SCTRL_RESET) {
162 		DPRINTF(("tcic: chkrsvd 7\n"));
163 		/* Address bits must be 0 */
164 		val = bus_space_read_2(iot, ioh, TCIC_R_ADDR);
165 		if (val != 0)
166 			return 0;
167 		val = bus_space_read_2(iot, ioh, TCIC_R_ADDR2);
168 		if (val != 0)
169 			return 0;
170 		DPRINTF(("tcic: chkrsvd 8\n"));
171 		/* EDC bits must be 0 */
172 		val = bus_space_read_2(iot, ioh, TCIC_R_EDC);
173 		if (val != 0)
174 			return 0;
175 		/* We're OK, so take it out of reset. XXX -chb */
176 		bus_space_write_1(iot, ioh, TCIC_R_SCTRL, 0);
177 	}
178 	else {	/* not in RESET mode */
179 		int omode;
180 		int val1, val2;
181 		DPRINTF(("tcic: chkrsvd 9\n"));
182 		/* Programming timers must have expired. */
183 		val = bus_space_read_1(iot, ioh, TCIC_R_SSTAT);
184 		if ((val & (TCIC_SSTAT_6US|TCIC_SSTAT_10US|TCIC_SSTAT_PROGTIME))
185 		    != (TCIC_SSTAT_6US|TCIC_SSTAT_10US|TCIC_SSTAT_PROGTIME))
186 			return 0;
187 		DPRINTF(("tcic: chkrsvd 10\n"));
188 		/*
189 		 * EDC bits should change on read from data space
190 		 * as long as either EDC or the data are nonzero.
191 		 */
192 		 if ((bus_space_read_2(iot, ioh, TCIC_R_ADDR2)
193 		     & TCIC_ADDR2_INDREG) != 0) {
194 			val1 = bus_space_read_2(iot, ioh, TCIC_R_EDC);
195 			val2 = bus_space_read_2(iot, ioh, TCIC_R_DATA);
196 			if (val1 | val2) {
197 				val1 = bus_space_read_2(iot, ioh, TCIC_R_EDC);
198 				if (val1 == val2)
199 					return 0;
200 			}
201 		}
202 		DPRINTF(("tcic: chkrsvd 11\n"));
203 		/* XXX what does this check? -chb */
204 		omode = bus_space_read_1(iot, ioh, TCIC_R_MODE);
205 		val1 = omode ^ TCIC_AR_MASK;
206 		bus_space_write_1(iot, ioh, TCIC_R_MODE, val1);
207 		val2 = bus_space_read_1(iot, ioh, TCIC_R_MODE);
208 		bus_space_write_1(iot, ioh, TCIC_R_MODE, omode);
209 		if ( val1 != val2)
210 			return 0;
211 	}
212 	/* All tests passed */
213 	return 1;
214 }
215 
216 /*
217  * Read chip ID from AR_ILOCK in test mode.
218  */
219 int
220 tcic_chipid(iot, ioh)
221 	bus_space_tag_t iot;
222 	bus_space_handle_t ioh;
223 {
224 	unsigned id, otest;
225 
226 	otest = tcic_read_aux_2(iot, ioh, TCIC_AR_TEST);
227 	tcic_write_aux_2(iot, ioh, TCIC_AR_TEST, TCIC_TEST_DIAG);
228 	id = tcic_read_aux_2(iot, ioh, TCIC_AR_ILOCK);
229 	tcic_write_aux_2(iot, ioh, TCIC_AR_TEST, otest);
230 	id &= TCIC_ILOCKTEST_ID_MASK;
231 	id >>= TCIC_ILOCKTEST_ID_SHFT;
232 
233 	/* clear up IRQs inside tcic. XXX -chb */
234 	while (bus_space_read_1(iot, ioh, TCIC_R_ICSR))
235 		bus_space_write_1(iot, ioh, TCIC_R_ICSR, TCIC_ICSR_JAM);
236 
237 	return id;
238 }
239 /*
240  * Indicate whether the driver can handle the chip.
241  */
242 int
243 tcic_chipid_known(id)
244 	int id;
245 {
246 	/* XXX only know how to handle DB86082 -chb */
247 	switch (id) {
248 	case TCIC_CHIPID_DB86082_1:
249 	case TCIC_CHIPID_DB86082A:
250 	case TCIC_CHIPID_DB86082B_ES:
251 	case TCIC_CHIPID_DB86082B:
252 	case TCIC_CHIPID_DB86084_1:
253 	case TCIC_CHIPID_DB86084A:
254 	case TCIC_CHIPID_DB86184_1:
255 	case TCIC_CHIPID_DB86072_1_ES:
256 	case TCIC_CHIPID_DB86072_1:
257 		return 1;
258 	}
259 
260 	return 0;
261 }
262 
263 const char *
264 tcic_chipid_to_string(id)
265 	int id;
266 {
267 	switch (id) {
268 	case TCIC_CHIPID_DB86082_1:
269 		return ("Databook DB86082");
270 	case TCIC_CHIPID_DB86082A:
271 		return ("Databook DB86082A");
272 	case TCIC_CHIPID_DB86082B_ES:
273 		return ("Databook DB86082B-es");
274 	case TCIC_CHIPID_DB86082B:
275 		return ("Databook DB86082B");
276 	case TCIC_CHIPID_DB86084_1:
277 		return ("Databook DB86084");
278 	case TCIC_CHIPID_DB86084A:
279 		return ("Databook DB86084A");
280 	case TCIC_CHIPID_DB86184_1:
281 		return ("Databook DB86184");
282 	case TCIC_CHIPID_DB86072_1_ES:
283 		return ("Databook DB86072-es");
284 	case TCIC_CHIPID_DB86072_1:
285 		return ("Databook DB86072");
286 	}
287 
288 	return ("Unknown controller");
289 }
290 /*
291  * Return bitmask of IRQs that the chip can handle.
292  * XXX should be table driven.
293  */
294 int
295 tcic_validirqs(chipid)
296 	int chipid;
297 {
298 	switch (chipid) {
299 	case TCIC_CHIPID_DB86082_1:
300 	case TCIC_CHIPID_DB86082A:
301 	case TCIC_CHIPID_DB86082B_ES:
302 	case TCIC_CHIPID_DB86082B:
303 	case TCIC_CHIPID_DB86084_1:
304 	case TCIC_CHIPID_DB86084A:
305 	case TCIC_CHIPID_DB86184_1:
306 	case TCIC_CHIPID_DB86072_1_ES:
307 	case TCIC_CHIPID_DB86072_1:
308 		return tcic_valid_irqs;
309 	}
310 	return 0;
311 }
312 
313 void
314 tcic_attach(sc)
315 	struct tcic_softc *sc;
316 {
317 	int i, reg;
318 
319 	/* set more chipset dependent parameters in the softc. */
320 	switch (sc->chipid) {
321 	case TCIC_CHIPID_DB86084_1:
322 	case TCIC_CHIPID_DB86084A:
323 	case TCIC_CHIPID_DB86184_1:
324 		sc->pwrena = TCIC_PWR_ENA;
325 		break;
326 	default:
327 		sc->pwrena = 0;
328 		break;
329 	}
330 
331 	/* set up global config registers */
332 	reg = TCIC_WAIT_SYNC | TCIC_WAIT_CCLK | TCIC_WAIT_RISING;
333 	reg |= (tcic_ns2wscnt(250) & TCIC_WAIT_COUNT_MASK);
334 	tcic_write_aux_1(sc->iot, sc->ioh, TCIC_AR_WCTL, TCIC_R_WCTL_WAIT, reg);
335 	reg = TCIC_SYSCFG_MPSEL_RI | TCIC_SYSCFG_MCSFULL;
336 	tcic_write_aux_2(sc->iot, sc->ioh, TCIC_AR_SYSCFG, reg);
337 	reg = tcic_read_aux_2(sc->iot, sc->ioh, TCIC_AR_ILOCK);
338 	reg |= TCIC_ILOCK_HOLD_CCLK;
339 	tcic_write_aux_2(sc->iot, sc->ioh, TCIC_AR_ILOCK, reg);
340 
341 	/* the TCIC has two sockets */
342 	/* XXX should i check for actual presence of sockets? -chb */
343 	for (i = 0; i < TCIC_NSLOTS; i++) {
344 		sc->handle[i].sc = sc;
345 		sc->handle[i].sock = i;
346 		sc->handle[i].flags = TCIC_FLAG_SOCKETP;
347 		sc->handle[i].memwins
348 		    = sc->chipid == TCIC_CHIPID_DB86082_1 ?  4 : 5;
349 	}
350 
351 	/* establish the interrupt */
352 	reg = tcic_read_1(&sc->handle[0], TCIC_R_IENA);
353 	tcic_write_1(&sc->handle[0], TCIC_R_IENA,
354 	    (reg & ~TCIC_IENA_CFG_MASK) | TCIC_IENA_CFG_HIGH);
355 	reg = tcic_read_aux_2(sc->iot, sc->ioh, TCIC_AR_SYSCFG);
356 	tcic_write_aux_2(sc->iot, sc->ioh, TCIC_AR_SYSCFG,
357 	    (reg & ~TCIC_SYSCFG_IRQ_MASK) | tcic_irqmap[sc->irq]);
358 
359 	/* XXX block interrupts? */
360 
361 	for (i = 0; i < TCIC_NSLOTS; i++) {
362 		/* XXX make more clear what happens here -chb */
363 		tcic_sel_sock(&sc->handle[i]);
364 		tcic_write_ind_2(&sc->handle[i], TCIC_IR_SCF1_N(i), 0);
365 		tcic_write_ind_2(&sc->handle[i], TCIC_IR_SCF2_N(i),
366 		    (TCIC_SCF2_MCD|TCIC_SCF2_MWP|TCIC_SCF2_MRDY
367 #if 1		/* XXX explain byte routing issue */
368 		    |TCIC_SCF2_MLBAT2|TCIC_SCF2_MLBAT1|TCIC_SCF2_IDBR));
369 #else
370 		    |TCIC_SCF2_MLBAT2|TCIC_SCF2_MLBAT1));
371 #endif
372 		tcic_write_1(&sc->handle[i], TCIC_R_MODE, 0);
373 		reg = tcic_read_aux_2(sc->iot, sc->ioh, TCIC_AR_SYSCFG);
374 		reg &= ~TCIC_SYSCFG_AUTOBUSY;
375 		tcic_write_aux_2(sc->iot, sc->ioh, TCIC_AR_SYSCFG, reg);
376 		SIMPLEQ_INIT(&sc->handle[i].events);
377 	}
378 
379 	if ((sc->handle[0].flags & TCIC_FLAG_SOCKETP) ||
380 	    (sc->handle[1].flags & TCIC_FLAG_SOCKETP)) {
381 		printf("%s: %s has ", device_xname(&sc->dev),
382 		       tcic_chipid_to_string(sc->chipid));
383 
384 		if ((sc->handle[0].flags & TCIC_FLAG_SOCKETP) &&
385 		    (sc->handle[1].flags & TCIC_FLAG_SOCKETP))
386 			printf("sockets A and B\n");
387 		else if (sc->handle[0].flags & TCIC_FLAG_SOCKETP)
388 			printf("socket A only\n");
389 		else
390 			printf("socket B only\n");
391 
392 	}
393 }
394 
395 void
396 tcic_attach_sockets(sc)
397 	struct tcic_softc *sc;
398 {
399 	int i;
400 
401 	for (i = 0; i < TCIC_NSLOTS; i++)
402 		if (sc->handle[i].flags & TCIC_FLAG_SOCKETP)
403 			tcic_attach_socket(&sc->handle[i]);
404 }
405 
406 void
407 tcic_attach_socket(h)
408 	struct tcic_handle *h;
409 {
410 	struct pcmciabus_attach_args paa;
411 	int locs[PCMCIABUSCF_NLOCS];
412 
413 	/* initialize the rest of the handle */
414 
415 	h->shutdown = 0;
416 	h->memalloc = 0;
417 	h->ioalloc = 0;
418 	h->ih_irq = 0;
419 
420 	/* now, config one pcmcia device per socket */
421 
422 	paa.paa_busname = "pcmcia";
423 	paa.pct = (pcmcia_chipset_tag_t) h->sc->pct;
424 	paa.pch = (pcmcia_chipset_handle_t) h;
425 	paa.iobase = h->sc->iobase;
426 	paa.iosize = h->sc->iosize;
427 
428 	locs[PCMCIABUSCF_CONTROLLER] = 0;
429 	locs[PCMCIABUSCF_SOCKET] = h->sock;
430 
431 	h->pcmcia = config_found_sm_loc(&h->sc->dev, "pcmciabus", locs, &paa,
432 					tcic_print, config_stdsubmatch);
433 
434 	/* if there's actually a pcmcia device attached, initialize the slot */
435 
436 	if (h->pcmcia)
437 		tcic_init_socket(h);
438 }
439 
440 void
441 tcic_create_event_thread(arg)
442 	void *arg;
443 {
444 	struct tcic_handle *h = arg;
445 	const char *cs;
446 
447 	switch (h->sock) {
448 	case 0:
449 		cs = "0";
450 		break;
451 	case 1:
452 		cs = "1";
453 		break;
454 	default:
455 		panic("tcic_create_event_thread: unknown tcic socket");
456 	}
457 
458 	if (kthread_create(PRI_NONE, 0, NULL, tcic_event_thread, h,
459 	    &h->event_thread, "%s,%s", device_xname(&h->sc->dev), cs)) {
460 		aprint_error_dev(&h->sc->dev, "unable to create event thread for sock 0x%02x\n", h->sock);
461 		panic("tcic_create_event_thread");
462 	}
463 }
464 
465 void
466 tcic_event_thread(arg)
467 	void *arg;
468 {
469 	struct tcic_handle *h = arg;
470 	struct tcic_event *pe;
471 	int s;
472 
473 	while (h->shutdown == 0) {
474 		s = splhigh();
475 		if ((pe = SIMPLEQ_FIRST(&h->events)) == NULL) {
476 			splx(s);
477 			(void) tsleep(&h->events, PWAIT, "tcicev", 0);
478 			continue;
479 		}
480 		SIMPLEQ_REMOVE_HEAD(&h->events, pe_q);
481 		splx(s);
482 
483 		switch (pe->pe_type) {
484 		case TCIC_EVENT_INSERTION:
485 			DPRINTF(("%s: insertion event\n", device_xname(&h->sc->dev)));
486 			tcic_attach_card(h);
487 			break;
488 
489 		case TCIC_EVENT_REMOVAL:
490 			DPRINTF(("%s: removal event\n", device_xname(&h->sc->dev)));
491 			tcic_detach_card(h, DETACH_FORCE);
492 			break;
493 
494 		default:
495 			panic("tcic_event_thread: unknown event %d",
496 			    pe->pe_type);
497 		}
498 		free(pe, M_TEMP);
499 	}
500 
501 	h->event_thread = NULL;
502 
503 	/* In case parent is waiting for us to exit. */
504 	wakeup(h->sc);
505 
506 	kthread_exit(0);
507 }
508 
509 
510 void
511 tcic_init_socket(h)
512 	struct tcic_handle *h;
513 {
514 	int reg;
515 
516 	/* select this socket's config registers */
517 	tcic_sel_sock(h);
518 
519 	/* set up the socket to interrupt on card detect */
520 	reg = tcic_read_ind_2(h, TCIC_IR_SCF2_N(h->sock));
521 	tcic_write_ind_2(h, TCIC_IR_SCF2_N(h->sock), reg & ~TCIC_SCF2_MCD);
522 
523 	/* enable CD irq in R_IENA */
524 	reg = tcic_read_2(h, TCIC_R_IENA);
525 	tcic_write_2(h, TCIC_R_IENA, reg |= TCIC_IENA_CDCHG);
526 
527 	/* if there's a card there, then attach it. also save sstat */
528 	h->sstat = reg = tcic_read_1(h, TCIC_R_SSTAT) & TCIC_SSTAT_STAT_MASK;
529 	if (reg & TCIC_SSTAT_CD)
530 		tcic_attach_card(h);
531 }
532 
533 int
534 tcic_print(arg, pnp)
535 	void *arg;
536 	const char *pnp;
537 {
538 	struct pcmciabus_attach_args *paa = arg;
539 	struct tcic_handle *h = (struct tcic_handle *) paa->pch;
540 
541 	/* Only "pcmcia"s can attach to "tcic"s... easy. */
542 	if (pnp)
543 		aprint_normal("pcmcia at %s", pnp);
544 
545 	aprint_normal(" socket %d", h->sock);
546 
547 	return (UNCONF);
548 }
549 
550 int
551 tcic_intr(arg)
552 	void *arg;
553 {
554 	struct tcic_softc *sc = arg;
555 	int i, ret = 0;
556 
557 	DPRINTF(("%s: intr\n", device_xname(&sc->dev)));
558 
559 	for (i = 0; i < TCIC_NSLOTS; i++)
560 		if (sc->handle[i].flags & TCIC_FLAG_SOCKETP)
561 			ret += tcic_intr_socket(&sc->handle[i]);
562 
563 	return (ret ? 1 : 0);
564 }
565 
566 int
567 tcic_intr_socket(h)
568 	struct tcic_handle *h;
569 {
570 	int icsr, rv;
571 
572 	rv = 0;
573 	tcic_sel_sock(h);
574 	icsr = tcic_read_1(h, TCIC_R_ICSR);
575 
576 	DPRINTF(("%s: %d icsr: 0x%02x \n", device_xname(&h->sc->dev), h->sock, icsr));
577 
578 	/* XXX or should the next three be handled in tcic_intr? -chb */
579 	if (icsr & TCIC_ICSR_PROGTIME) {
580 		DPRINTF(("%s: %02x PROGTIME\n", device_xname(&h->sc->dev), h->sock));
581 		rv = 1;
582 	}
583 	if (icsr & TCIC_ICSR_ILOCK) {
584 		DPRINTF(("%s: %02x ILOCK\n", device_xname(&h->sc->dev), h->sock));
585 		rv = 1;
586 	}
587 	if (icsr & TCIC_ICSR_ERR) {
588 		DPRINTF(("%s: %02x ERR\n", device_xname(&h->sc->dev), h->sock));
589 		rv = 1;
590 	}
591 	if (icsr & TCIC_ICSR_CDCHG) {
592 		int sstat, delta;
593 
594 		/* compute what changed since last interrupt */
595 		sstat = tcic_read_aux_1(h->sc->iot, h->sc->ioh,
596 		    TCIC_AR_WCTL, TCIC_R_WCTL_XCSR) & TCIC_XCSR_STAT_MASK;
597 		delta = h->sstat ^ sstat;
598 		h->sstat = sstat;
599 
600 		if (delta)
601 			rv = 1;
602 
603 		DPRINTF(("%s: %02x CDCHG %x\n", device_xname(&h->sc->dev), h->sock,
604 		    delta));
605 
606 		/*
607 		 * XXX This should probably schedule something to happen
608 		 * after the interrupt handler completes
609 		 */
610 
611 		if (delta & TCIC_SSTAT_CD) {
612 			if (sstat & TCIC_SSTAT_CD) {
613 				if (!(h->flags & TCIC_FLAG_CARDP)) {
614 					DPRINTF(("%s: enqueing INSERTION event\n",
615 					    device_xname(&h->sc->dev)));
616 					tcic_queue_event(h, TCIC_EVENT_INSERTION);
617 				}
618 			} else {
619 				if (h->flags & TCIC_FLAG_CARDP) {
620 					/* Deactivate the card now. */
621 					DPRINTF(("%s: deactivating card\n",
622 					    device_xname(&h->sc->dev)));
623 					tcic_deactivate_card(h);
624 
625 					DPRINTF(("%s: enqueing REMOVAL event\n",
626 					    device_xname(&h->sc->dev)));
627 					tcic_queue_event(h, TCIC_EVENT_REMOVAL);
628 				}
629 			}
630 		}
631 		if (delta & TCIC_SSTAT_RDY) {
632 			DPRINTF(("%s: %02x READY\n", device_xname(&h->sc->dev), h->sock));
633 			/* shouldn't happen */
634 		}
635 		if (delta & TCIC_SSTAT_LBAT1) {
636 			DPRINTF(("%s: %02x LBAT1\n", device_xname(&h->sc->dev), h->sock));
637 		}
638 		if (delta & TCIC_SSTAT_LBAT2) {
639 			DPRINTF(("%s: %02x LBAT2\n", device_xname(&h->sc->dev), h->sock));
640 		}
641 		if (delta & TCIC_SSTAT_WP) {
642 			DPRINTF(("%s: %02x WP\n", device_xname(&h->sc->dev), h->sock));
643 		}
644 	}
645 	return rv;
646 }
647 
648 void
649 tcic_queue_event(h, event)
650 	struct tcic_handle *h;
651 	int event;
652 {
653 	struct tcic_event *pe;
654 	int s;
655 
656 	pe = malloc(sizeof(*pe), M_TEMP, M_NOWAIT);
657 	if (pe == NULL)
658 		panic("tcic_queue_event: can't allocate event");
659 
660 	pe->pe_type = event;
661 	s = splhigh();
662 	SIMPLEQ_INSERT_TAIL(&h->events, pe, pe_q);
663 	splx(s);
664 	wakeup(&h->events);
665 }
666 void
667 tcic_attach_card(h)
668 	struct tcic_handle *h;
669 {
670 	DPRINTF(("tcic_attach_card\n"));
671 
672 	if (h->flags & TCIC_FLAG_CARDP)
673 		panic("tcic_attach_card: already attached");
674 
675 	/* call the MI attach function */
676 
677 	pcmcia_card_attach(h->pcmcia);
678 
679 	h->flags |= TCIC_FLAG_CARDP;
680 }
681 
682 void
683 tcic_detach_card(h, flags)
684 	struct tcic_handle *h;
685 	int flags;		/* DETACH_* */
686 {
687 	DPRINTF(("tcic_detach_card\n"));
688 
689 	if (!(h->flags & TCIC_FLAG_CARDP))
690 		panic("tcic_detach_card: already detached");
691 
692 	h->flags &= ~TCIC_FLAG_CARDP;
693 
694 	/* call the MI detach function */
695 
696 	pcmcia_card_detach(h->pcmcia, flags);
697 
698 }
699 
700 void
701 tcic_deactivate_card(h)
702 	struct tcic_handle *h;
703 {
704 	int val, reg;
705 
706 	if (!(h->flags & TCIC_FLAG_CARDP))
707 		 panic("tcic_deactivate_card: already detached");
708 
709 	/* call the MI deactivate function */
710 	pcmcia_card_deactivate(h->pcmcia);
711 
712 	tcic_sel_sock(h);
713 
714 	/* XXX disable card detect resume and configuration reset??? */
715 
716 	/* power down the socket */
717 	tcic_write_1(h, TCIC_R_PWR, 0);
718 
719 	/* reset the card XXX ? -chb */
720 
721 	/* turn off irq's for this socket */
722 	reg = TCIC_IR_SCF1_N(h->sock);
723 	val = tcic_read_ind_2(h, reg);
724 	tcic_write_ind_2(h, reg, (val & ~TCIC_SCF1_IRQ_MASK)|TCIC_SCF1_IRQOFF);
725 	reg = TCIC_IR_SCF2_N(h->sock);
726 	val = tcic_read_ind_2(h, reg);
727 	tcic_write_ind_2(h, reg,
728 	    (val | (TCIC_SCF2_MLBAT1|TCIC_SCF2_MLBAT2|TCIC_SCF2_MRDY
729 		|TCIC_SCF2_MWP|TCIC_SCF2_MCD)));
730 }
731 
732 /* XXX the following routine may need to be rewritten. -chb */
733 int
734 tcic_chip_mem_alloc(pch, size, pcmhp)
735 	pcmcia_chipset_handle_t pch;
736 	bus_size_t size;
737 	struct pcmcia_mem_handle *pcmhp;
738 {
739 	struct tcic_handle *h = (struct tcic_handle *) pch;
740 	bus_space_handle_t memh;
741 	bus_addr_t addr;
742 	bus_size_t sizepg;
743 	int i, mask, mhandle, got = 0;
744 
745 	/* out of sc->memh, allocate as many pages as necessary */
746 
747 	/*
748 	 * The TCIC can map memory only in sizes that are
749 	 * powers of two, aligned at the natural boundary for the size.
750 	 */
751 	i = tcic_log2((u_int)size);
752 	if ((1<<i) < size)
753 		i++;
754 	sizepg = max(i, TCIC_MEM_SHIFT) - (TCIC_MEM_SHIFT-1);
755 
756 	DPRINTF(("tcic_chip_mem_alloc: size %ld sizepg %ld\n", size, sizepg));
757 
758 	/* can't allocate that much anyway */
759 	if (sizepg > TCIC_MEM_PAGES)	/* XXX -chb */
760 		return 1;
761 
762 	mask = (1 << sizepg) - 1;
763 
764 	addr = 0;		/* XXX gcc -Wuninitialized */
765 	mhandle = 0;		/* XXX gcc -Wuninitialized */
766 
767 	/* XXX i should be initialised to always lay on boundary. -chb */
768 	for (i = 0; i < (TCIC_MEM_PAGES + 1 - sizepg); i += sizepg) {
769 		if ((h->sc->subregionmask & (mask << i)) == (mask << i)) {
770 			if (bus_space_subregion(h->sc->memt, h->sc->memh,
771 			    i * TCIC_MEM_PAGESIZE,
772 			    sizepg * TCIC_MEM_PAGESIZE, &memh))
773 				return (1);
774 			mhandle = mask << i;
775 			addr = h->sc->membase + (i * TCIC_MEM_PAGESIZE);
776 			h->sc->subregionmask &= ~(mhandle);
777 			got = 1;
778 			break;
779 		}
780 	}
781 
782 	if (got == 0)
783 		return (1);
784 
785 	DPRINTF(("tcic_chip_mem_alloc bus addr 0x%lx+0x%lx\n", (u_long) addr,
786 		 (u_long) size));
787 
788 	pcmhp->memt = h->sc->memt;
789 	pcmhp->memh = memh;
790 	pcmhp->addr = addr;
791 	pcmhp->size = size;
792 	pcmhp->mhandle = mhandle;
793 	pcmhp->realsize = sizepg * TCIC_MEM_PAGESIZE;
794 
795 	return (0);
796 }
797 
798 /* XXX the following routine may need to be rewritten. -chb */
799 void
800 tcic_chip_mem_free(pch, pcmhp)
801 	pcmcia_chipset_handle_t pch;
802 	struct pcmcia_mem_handle *pcmhp;
803 {
804 	struct tcic_handle *h = (struct tcic_handle *) pch;
805 
806 	h->sc->subregionmask |= pcmhp->mhandle;
807 }
808 
809 void
810 tcic_chip_do_mem_map(h, win)
811 	struct tcic_handle *h;
812 	int win;
813 {
814 	int reg, hwwin, wscnt;
815 
816 	int kind = h->mem[win].kind & ~PCMCIA_WIDTH_MEM_MASK;
817 	int mem8 = (h->mem[win].kind & PCMCIA_WIDTH_MEM_MASK) == PCMCIA_WIDTH_MEM8;
818 	DPRINTF(("tcic_chip_do_mem_map window %d: 0x%lx+0x%lx 0x%lx\n",
819 		win, (u_long)h->mem[win].addr, (u_long)h->mem[win].size,
820 		(u_long)h->mem[win].offset));
821 	/*
822 	 * the even windows are used for socket 0,
823 	 * the odd ones for socket 1.
824 	 */
825 	hwwin = (win << 1) + h->sock;
826 
827 	/* the WR_MEXT register is MBZ */
828 	tcic_write_ind_2(h, TCIC_WR_MEXT_N(hwwin), 0);
829 
830 	/* set the host base address and window size */
831 	if (h->mem[win].size2 <= 1) {
832 		reg = ((h->mem[win].addr >> TCIC_MEM_SHIFT) &
833 		    TCIC_MBASE_ADDR_MASK) | TCIC_MBASE_4K;
834 	} else {
835 		reg = ((h->mem[win].addr >> TCIC_MEM_SHIFT) &
836 		    TCIC_MBASE_ADDR_MASK) | (h->mem[win].size2 >> 1);
837 	}
838 	tcic_write_ind_2(h, TCIC_WR_MBASE_N(hwwin), reg);
839 
840 	/* set the card address and address space */
841 	reg = 0;
842 	reg = ((h->mem[win].offset >> TCIC_MEM_SHIFT) & TCIC_MMAP_ADDR_MASK);
843 	reg |= (kind == PCMCIA_MEM_ATTR) ? TCIC_MMAP_ATTR : 0;
844 	DPRINTF(("tcic_chip_do_map_mem window %d(%d) mmap 0x%04x\n",
845 	    win, hwwin, reg));
846 	tcic_write_ind_2(h, TCIC_WR_MMAP_N(hwwin), reg);
847 
848 	/* set the MCTL register */
849 	/* must save WSCNT field in case this is a DB86082 rev 0 */
850 	/* XXX why can't I do the following two in one statement? */
851 	reg = tcic_read_ind_2(h, TCIC_WR_MCTL_N(hwwin)) & TCIC_MCTL_WSCNT_MASK;
852 	reg |= TCIC_MCTL_ENA|TCIC_MCTL_QUIET;
853 	reg |= mem8 ? TCIC_MCTL_B8 : 0;
854 	reg |= (h->sock << TCIC_MCTL_SS_SHIFT) & TCIC_MCTL_SS_MASK;
855 #ifdef notyet	/* XXX must get speed from CIS somehow. -chb */
856 	wscnt = tcic_ns2wscnt(h->mem[win].speed);
857 #else
858 	wscnt = tcic_ns2wscnt(tcic_mem_speed);	/*  300 is "save" default for CIS memory */
859 #endif
860 	if (h->sc->chipid == TCIC_CHIPID_DB86082_1) {
861 		/*
862 		 * this chip has the wait state count in window
863 		 * register 7 - hwwin.
864 		 */
865 		int reg2;
866 		reg2 = tcic_read_ind_2(h, TCIC_WR_MCTL_N(7-hwwin));
867 		reg2 &= ~TCIC_MCTL_WSCNT_MASK;
868 		reg2 |= wscnt & TCIC_MCTL_WSCNT_MASK;
869 		tcic_write_ind_2(h, TCIC_WR_MCTL_N(7-hwwin), reg2);
870 	} else {
871 		reg |= wscnt & TCIC_MCTL_WSCNT_MASK;
872 	}
873 	tcic_write_ind_2(h, TCIC_WR_MCTL_N(hwwin), reg);
874 
875 #ifdef TCICDEBUG
876 	{
877 		int r1, r2, r3;
878 
879 		r1 = tcic_read_ind_2(h, TCIC_WR_MBASE_N(hwwin));
880 		r2 = tcic_read_ind_2(h, TCIC_WR_MMAP_N(hwwin));
881 		r3 = tcic_read_ind_2(h, TCIC_WR_MCTL_N(hwwin));
882 
883 		DPRINTF(("tcic_chip_do_mem_map window %d(%d): %04x %04x %04x\n",
884 		    win, hwwin, r1, r2, r3));
885 	}
886 #endif
887 }
888 
889 /* XXX needs work */
890 int
891 tcic_chip_mem_map(pch, kind, card_addr, size, pcmhp, offsetp, windowp)
892 	pcmcia_chipset_handle_t pch;
893 	int kind;
894 	bus_addr_t card_addr;
895 	bus_size_t size;
896 	struct pcmcia_mem_handle *pcmhp;
897 	bus_size_t *offsetp;
898 	int *windowp;
899 {
900 	struct tcic_handle *h = (struct tcic_handle *) pch;
901 	bus_addr_t busaddr;
902 	long card_offset;
903 	int i, win;
904 
905 	win = -1;
906 	for (i = 0; i < h->memwins; i++) {
907 		if ((h->memalloc & (1 << i)) == 0) {
908 			win = i;
909 			h->memalloc |= (1 << i);
910 			break;
911 		}
912 	}
913 
914 	if (win == -1)
915 		return (1);
916 
917 	*windowp = win;
918 
919 	/* XXX this is pretty gross */
920 
921 	if (h->sc->memt != pcmhp->memt)
922 		panic("tcic_chip_mem_map memt is bogus");
923 
924 	busaddr = pcmhp->addr;
925 
926 	/*
927 	 * compute the address offset to the pcmcia address space for the
928 	 * tcic.  this is intentionally signed.  The masks and shifts below
929 	 * will cause TRT to happen in the tcic registers.  Deal with making
930 	 * sure the address is aligned, and return the alignment offset.
931 	 */
932 
933 	*offsetp = card_addr % TCIC_MEM_ALIGN;
934 	card_addr -= *offsetp;
935 
936 	DPRINTF(("tcic_chip_mem_map window %d bus %lx+%lx+%lx at card addr "
937 	    "%lx\n", win, (u_long) busaddr, (u_long) * offsetp, (u_long) size,
938 	    (u_long) card_addr));
939 
940 	/* XXX we can't use size. -chb */
941 	/*
942 	 * include the offset in the size, and decrement size by one, since
943 	 * the hw wants start/stop
944 	 */
945 	size += *offsetp - 1;
946 
947 	card_offset = (((long) card_addr) - ((long) busaddr));
948 
949 	DPRINTF(("tcic_chip_mem_map window %d card_offset 0x%lx\n",
950 	    win, (u_long)card_offset));
951 
952 	h->mem[win].addr = busaddr;
953 	h->mem[win].size = size;
954 	h->mem[win].size2 = tcic_log2((u_int)pcmhp->realsize) - TCIC_MEM_SHIFT;
955 	h->mem[win].offset = card_offset;
956 	h->mem[win].kind = kind;
957 
958 	tcic_chip_do_mem_map(h, win);
959 
960 	return (0);
961 }
962 
963 void
964 tcic_chip_mem_unmap(pch, window)
965 	pcmcia_chipset_handle_t pch;
966 	int window;
967 {
968 	struct tcic_handle *h = (struct tcic_handle *) pch;
969 	int hwwin;
970 
971 	if (window >= h->memwins)
972 		panic("tcic_chip_mem_unmap: window out of range");
973 
974 	hwwin = (window << 1) + h->sock;
975 	tcic_write_ind_2(h, TCIC_WR_MCTL_N(hwwin), 0);
976 
977 	h->memalloc &= ~(1 << window);
978 }
979 
980 int
981 tcic_chip_io_alloc(pch, start, size, align, pcihp)
982 	pcmcia_chipset_handle_t pch;
983 	bus_addr_t start;
984 	bus_size_t size;
985 	bus_size_t align;
986 	struct pcmcia_io_handle *pcihp;
987 {
988 	struct tcic_handle *h = (struct tcic_handle *) pch;
989 	bus_space_tag_t iot;
990 	bus_space_handle_t ioh;
991 	bus_addr_t ioaddr;
992 	int size2, flags = 0;
993 
994 	/*
995 	 * Allocate some arbitrary I/O space.
996 	 */
997 
998 	DPRINTF(("tcic_chip_io_alloc req 0x%lx %ld %ld\n",
999 	    (u_long) start, (u_long) size, (u_long) align));
1000 	/*
1001 	 * The TCIC can map I/O space only in sizes that are
1002 	 * powers of two, aligned at the natural boundary for the size.
1003 	 */
1004 	size2 = tcic_log2((u_int)size);
1005 	if ((1 << size2) < size)
1006 		size2++;
1007 	/* can't allocate that much anyway */
1008 	if (size2 > 16)	/* XXX 64K -chb */
1009 		return 1;
1010 	if (align) {
1011 		if ((1 << size2) != align)
1012 			return 1;	/* not suitably  aligned */
1013 	} else {
1014 		align = 1 << size2;	/* no alignment given, make it natural */
1015 	}
1016 	if (start & (align - 1))
1017 		return 1;	/* not suitably aligned */
1018 
1019 	iot = h->sc->iot;
1020 
1021 	if (start) {
1022 		ioaddr = start;
1023 		if (bus_space_map(iot, start, size, 0, &ioh))
1024 			return (1);
1025 		DPRINTF(("tcic_chip_io_alloc map port %lx+%lx\n",
1026 		    (u_long) ioaddr, (u_long) size));
1027 	} else {
1028 		flags |= PCMCIA_IO_ALLOCATED;
1029 		if (bus_space_alloc(iot, h->sc->iobase,
1030 		    h->sc->iobase + h->sc->iosize, size, align, 0, 0,
1031 		    &ioaddr, &ioh))
1032 			return (1);
1033 		DPRINTF(("tcic_chip_io_alloc alloc port %lx+%lx\n",
1034 		    (u_long) ioaddr, (u_long) size));
1035 	}
1036 
1037 	pcihp->iot = iot;
1038 	pcihp->ioh = ioh;
1039 	pcihp->addr = ioaddr;
1040 	pcihp->size = size;
1041 	pcihp->flags = flags;
1042 
1043 	return (0);
1044 }
1045 
1046 void
1047 tcic_chip_io_free(pcmcia_chipset_handle_t pch,
1048     struct pcmcia_io_handle *pcihp)
1049 {
1050 	bus_space_tag_t iot = pcihp->iot;
1051 	bus_space_handle_t ioh = pcihp->ioh;
1052 	bus_size_t size = pcihp->size;
1053 
1054 	if (pcihp->flags & PCMCIA_IO_ALLOCATED)
1055 		bus_space_free(iot, ioh, size);
1056 	else
1057 		bus_space_unmap(iot, ioh, size);
1058 }
1059 
1060 static int tcic_iowidth_map[] =
1061     { TCIC_ICTL_AUTOSZ, TCIC_ICTL_B8, TCIC_ICTL_B16 };
1062 
1063 void
1064 tcic_chip_do_io_map(h, win)
1065 	struct tcic_handle *h;
1066 	int win;
1067 {
1068 	int reg, size2, iotiny, wbase, hwwin, wscnt;
1069 
1070 	DPRINTF(("tcic_chip_do_io_map win %d addr %lx size %lx width %d\n",
1071 	    win, (long) h->io[win].addr, (long) h->io[win].size,
1072 	    h->io[win].width * 8));
1073 
1074 	/*
1075 	 * the even windows are used for socket 0,
1076 	 * the odd ones for socket 1.
1077 	 */
1078 	hwwin = (win << 1) + h->sock;
1079 
1080 	/* set the WR_BASE register */
1081 	/* XXX what if size isn't power of 2? -chb */
1082 	size2 = tcic_log2((u_int)h->io[win].size);
1083 	DPRINTF(("tcic_chip_do_io_map win %d size2 %d\n", win, size2));
1084 	if (size2 < 1) {
1085 		iotiny = TCIC_ICTL_TINY;
1086 		wbase = h->io[win].addr;
1087 	} else {
1088 		iotiny = 0;
1089 		/* XXX we should do better -chb */
1090 		wbase = h->io[win].addr | (1 << (size2 - 1));
1091 	}
1092 	tcic_write_ind_2(h, TCIC_WR_IBASE_N(hwwin), wbase);
1093 
1094 	/* set the WR_ICTL register */
1095 	reg = TCIC_ICTL_ENA | TCIC_ICTL_QUIET;
1096 	reg |= (h->sock << TCIC_ICTL_SS_SHIFT) & TCIC_ICTL_SS_MASK;
1097 	reg |= iotiny | tcic_iowidth_map[h->io[win].width];
1098 	if (h->sc->chipid != TCIC_CHIPID_DB86082_1)
1099 		reg |= TCIC_ICTL_PASS16;
1100 #ifdef notyet	/* XXX must get speed from CIS somehow. -chb */
1101 	wscnt = tcic_ns2wscnt(h->io[win].speed);
1102 #else
1103 	wscnt = tcic_ns2wscnt(tcic_io_speed);	/* linux uses 0 as default */
1104 #endif
1105 	reg |= wscnt & TCIC_ICTL_WSCNT_MASK;
1106 	tcic_write_ind_2(h, TCIC_WR_ICTL_N(hwwin), reg);
1107 
1108 #ifdef TCICDEBUG
1109 	{
1110 		int r1, r2;
1111 
1112 		r1 = tcic_read_ind_2(h, TCIC_WR_IBASE_N(hwwin));
1113 		r2 = tcic_read_ind_2(h, TCIC_WR_ICTL_N(hwwin));
1114 
1115 		DPRINTF(("tcic_chip_do_io_map window %d(%d): %04x %04x\n",
1116 		    win, hwwin, r1, r2));
1117 	}
1118 #endif
1119 }
1120 
1121 int
1122 tcic_chip_io_map(pch, width, offset, size, pcihp, windowp)
1123 	pcmcia_chipset_handle_t pch;
1124 	int width;
1125 	bus_addr_t offset;
1126 	bus_size_t size;
1127 	struct pcmcia_io_handle *pcihp;
1128 	int *windowp;
1129 {
1130 	struct tcic_handle *h = (struct tcic_handle *) pch;
1131 	bus_addr_t ioaddr = pcihp->addr + offset;
1132 	int i, win;
1133 #ifdef TCICDEBUG
1134 	static const char *width_names[] = { "auto", "io8", "io16" };
1135 #endif
1136 
1137 	/* XXX Sanity check offset/size. */
1138 
1139 	win = -1;
1140 	for (i = 0; i < TCIC_IO_WINS; i++) {
1141 		if ((h->ioalloc & (1 << i)) == 0) {
1142 			win = i;
1143 			h->ioalloc |= (1 << i);
1144 			break;
1145 		}
1146 	}
1147 
1148 	if (win == -1)
1149 		return (1);
1150 
1151 	*windowp = win;
1152 
1153 	/* XXX this is pretty gross */
1154 
1155 	if (h->sc->iot != pcihp->iot)
1156 		panic("tcic_chip_io_map iot is bogus");
1157 
1158 	DPRINTF(("tcic_chip_io_map window %d %s port %lx+%lx\n",
1159 		 win, width_names[width], (u_long) ioaddr, (u_long) size));
1160 
1161 	/* XXX wtf is this doing here? */
1162 
1163 	printf("%s: port 0x%lx", device_xname(&h->sc->dev), (u_long) ioaddr);
1164 	if (size > 1)
1165 		printf("-0x%lx", (u_long) ioaddr + (u_long) size - 1);
1166 	printf("\n");
1167 
1168 	h->io[win].addr = ioaddr;
1169 	h->io[win].size = size;
1170 	h->io[win].width = width;
1171 
1172 	tcic_chip_do_io_map(h, win);
1173 
1174 	return (0);
1175 }
1176 
1177 void
1178 tcic_chip_io_unmap(pch, window)
1179 	pcmcia_chipset_handle_t pch;
1180 	int window;
1181 {
1182 	struct tcic_handle *h = (struct tcic_handle *) pch;
1183 	int hwwin;
1184 
1185 	if (window >= TCIC_IO_WINS)
1186 		panic("tcic_chip_io_unmap: window out of range");
1187 
1188 	hwwin = (window << 1) + h->sock;
1189 	tcic_write_ind_2(h, TCIC_WR_ICTL_N(hwwin), 0);
1190 
1191 	h->ioalloc &= ~(1 << window);
1192 }
1193 
1194 void
1195 tcic_chip_socket_enable(pch)
1196 	pcmcia_chipset_handle_t pch;
1197 {
1198 	struct tcic_handle *h = (struct tcic_handle *) pch;
1199 	int reg, win;
1200 
1201 	tcic_sel_sock(h);
1202 
1203 	/*
1204 	 * power down the socket to reset it.
1205 	 * put card reset into high-z, put chip outputs to card into high-z
1206 	 */
1207 
1208 	tcic_write_1(h, TCIC_R_PWR, 0);
1209 	reg = tcic_read_aux_2(h->sc->iot, h->sc->ioh, TCIC_AR_ILOCK);
1210 	reg |= TCIC_ILOCK_CWAIT;
1211 	reg &= ~(TCIC_ILOCK_CRESET|TCIC_ILOCK_CRESENA);
1212 	tcic_write_aux_2(h->sc->iot, h->sc->ioh, TCIC_AR_ILOCK, reg);
1213 	tcic_write_1(h, TCIC_R_SCTRL, 0);	/* clear TCIC_SCTRL_ENA */
1214 
1215 	/* zero out the address windows */
1216 
1217 	tcic_write_ind_2(h, TCIC_IR_SCF1_N(h->sock), 0);
1218 	/* writing to WR_MBASE_N disables the window */
1219 	for (win = 0; win < h->memwins; win++) {
1220 		tcic_write_ind_2(h, TCIC_WR_MBASE_N((win << 1) + h->sock), 0);
1221 	}
1222 	/* writing to WR_IBASE_N disables the window */
1223 	for (win = 0; win < TCIC_IO_WINS; win++) {
1224 		tcic_write_ind_2(h, TCIC_WR_IBASE_N((win << 1) + h->sock), 0);
1225 	}
1226 
1227 	/* power up the socket */
1228 
1229 	/* turn on VCC, turn of VPP */
1230 	reg = TCIC_PWR_VCC_N(h->sock) | TCIC_PWR_VPP_N(h->sock) | h->sc->pwrena;
1231 	if (h->sc->pwrena)		/* this is a '84 type chip */
1232 		reg |= TCIC_PWR_VCC5V;
1233 	tcic_write_1(h, TCIC_R_PWR, reg);
1234 	delay(10000);
1235 
1236 	/* enable reset and wiggle it to reset the card */
1237 	reg = tcic_read_aux_2(h->sc->iot, h->sc->ioh, TCIC_AR_ILOCK);
1238 	reg |= TCIC_ILOCK_CRESENA;
1239 	tcic_write_aux_2(h->sc->iot, h->sc->ioh, TCIC_AR_ILOCK, reg);
1240 	/* XXX need bus_space_barrier here */
1241 	reg |= TCIC_ILOCK_CRESET;
1242 	tcic_write_aux_2(h->sc->iot, h->sc->ioh, TCIC_AR_ILOCK, reg);
1243 	/* enable card signals */
1244 	tcic_write_1(h, TCIC_R_SCTRL, TCIC_SCTRL_ENA);
1245 	delay(10);	/* wait 10 us */
1246 
1247 	/* clear the reset flag */
1248 	reg = tcic_read_aux_2(h->sc->iot, h->sc->ioh, TCIC_AR_ILOCK);
1249 	reg &= ~(TCIC_ILOCK_CRESET);
1250 	tcic_write_aux_2(h->sc->iot, h->sc->ioh, TCIC_AR_ILOCK, reg);
1251 
1252 	/* wait 20ms as per pc card standard (r2.01) section 4.3.6 */
1253 	delay(20000);
1254 
1255 	/* wait for the chip to finish initializing */
1256 	tcic_wait_ready(h);
1257 
1258 	/* WWW */
1259 
1260 	/* reinstall all the memory and io mappings */
1261 
1262 	for (win = 0; win < h->memwins; win++)
1263 		if (h->memalloc & (1 << win))
1264 			tcic_chip_do_mem_map(h, win);
1265 
1266 	for (win = 0; win < TCIC_IO_WINS; win++)
1267 		if (h->ioalloc & (1 << win))
1268 			tcic_chip_do_io_map(h, win);
1269 }
1270 
1271 void
1272 tcic_chip_socket_settype(pch, type)
1273 	pcmcia_chipset_handle_t pch;
1274 	int type;
1275 {
1276 	struct tcic_handle *h = (struct tcic_handle *) pch;
1277 	int reg;
1278 
1279 	tcic_sel_sock(h);
1280 
1281 	/* set the card type */
1282 
1283 	reg = 0;
1284 	if (type == PCMCIA_IFTYPE_IO) {
1285 		reg |= TCIC_SCF1_IOSTS;
1286 		reg |= tcic_irqmap[h->ih_irq];		/* enable interrupts */
1287 	}
1288 	tcic_write_ind_2(h, TCIC_IR_SCF1_N(h->sock), reg);
1289 
1290 	DPRINTF(("%s: tcic_chip_socket_enable %d cardtype %s 0x%02x\n",
1291 	    device_xname(&h->sc->dev), h->sock,
1292 	    ((type == PCMCIA_IFTYPE_IO) ? "io" : "mem"), reg));
1293 }
1294 
1295 void
1296 tcic_chip_socket_disable(pch)
1297 	pcmcia_chipset_handle_t pch;
1298 {
1299 	struct tcic_handle *h = (struct tcic_handle *) pch;
1300 	int val;
1301 
1302 	DPRINTF(("tcic_chip_socket_disable\n"));
1303 
1304 	tcic_sel_sock(h);
1305 
1306 	/* disable interrupts */
1307 	val = tcic_read_ind_2(h, TCIC_IR_SCF1_N(h->sock));
1308 	val &= TCIC_SCF1_IRQ_MASK;
1309 	tcic_write_ind_2(h, TCIC_IR_SCF1_N(h->sock), val);
1310 
1311 	/* disable the output signals */
1312 	tcic_write_1(h, TCIC_R_SCTRL, 0);
1313 	val = tcic_read_aux_2(h->sc->iot, h->sc->ioh, TCIC_AR_ILOCK);
1314 	val &= ~TCIC_ILOCK_CRESENA;
1315 	tcic_write_aux_2(h->sc->iot, h->sc->ioh, TCIC_AR_ILOCK, val);
1316 
1317 	/* power down the socket */
1318 	tcic_write_1(h, TCIC_R_PWR, 0);
1319 }
1320 
1321 /*
1322  * XXX The following is Linux driver but doesn't match the table
1323  * in the manual.
1324  */
1325 int
1326 tcic_ns2wscnt(ns)
1327 	int ns;
1328 {
1329 	if (ns < 14) {
1330 		return 0;
1331 	} else {
1332 		return (2*(ns-14))/70;	/* XXX assumes 14.31818 MHz clock. */
1333 	}
1334 }
1335 
1336 int
1337 tcic_log2(val)
1338 	u_int val;
1339 {
1340 	int i, l2;
1341 
1342 	l2 = i = 0;
1343 	while (val) {
1344 		if (val & 1)
1345 			l2 = i;
1346 		i++;
1347 		val >>= 1;
1348 	}
1349 	return l2;
1350 }
1351