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