cardbus.c revision 1.32 1 /* $NetBSD: cardbus.c,v 1.32 2001/04/25 09:29:36 haya Exp $ */
2
3 /*
4 * Copyright (c) 1997, 1998, 1999 and 2000
5 * HAYAKAWA Koichi. 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 HAYAKAWA Koichi.
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 *
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
24 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
25 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
26 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
27 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
28 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
30 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGE.
33 */
34
35 #include "opt_cardbus.h"
36
37 #include <sys/types.h>
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/device.h>
41 #include <sys/malloc.h>
42 #include <sys/kernel.h>
43 #include <sys/syslog.h>
44 #include <sys/proc.h>
45
46 #include <machine/bus.h>
47
48 #include <dev/cardbus/cardbusvar.h>
49 #include <dev/cardbus/cardbusdevs.h>
50
51 #include <dev/cardbus/cardbus_exrom.h>
52
53 #include <dev/pci/pcivar.h> /* XXX */
54 #include <dev/pci/pcireg.h> /* XXX */
55
56 #include <dev/pcmcia/pcmciareg.h>
57
58 #if defined CARDBUS_DEBUG
59 #define STATIC
60 #define DPRINTF(a) printf a
61 #else
62 #define STATIC static
63 #define DPRINTF(a)
64 #endif
65
66
67 STATIC void cardbusattach(struct device *, struct device *, void *);
68 int cardbus_attach_card(struct cardbus_softc *);
69
70 STATIC int cardbusmatch(struct device *, struct cfdata *, void *);
71 static int cardbussubmatch(struct device *, struct cfdata *, void *);
72 static int cardbusprint(void *, const char *);
73
74 typedef void (*tuple_decode_func)(u_int8_t*, int, void*);
75
76 static int decode_tuples(u_int8_t *, int, tuple_decode_func, void*);
77 #ifdef CARDBUS_DEBUG
78 static void print_tuple(u_int8_t*, int, void*);
79 #endif
80
81 static int cardbus_read_tuples(struct cardbus_attach_args *,
82 cardbusreg_t, u_int8_t *, size_t);
83
84 static void enable_function(struct cardbus_softc *, int, int);
85 static void disable_function(struct cardbus_softc *, int);
86
87 struct cfattach cardbus_ca = {
88 sizeof(struct cardbus_softc), cardbusmatch, cardbusattach
89 };
90
91 #ifndef __NetBSD_Version__
92 struct cfdriver cardbus_cd = {
93 NULL, "cardbus", DV_DULL
94 };
95 #endif
96
97
98 STATIC int
99 cardbusmatch(struct device *parent, struct cfdata *cf, void *aux)
100 {
101 struct cbslot_attach_args *cba = aux;
102
103 if (strcmp(cba->cba_busname, cf->cf_driver->cd_name)) {
104 DPRINTF(("cardbusmatch: busname differs %s <=> %s\n",
105 cba->cba_busname, cf->cf_driver->cd_name));
106 return (0);
107 }
108
109 return (1);
110 }
111
112 STATIC void
113 cardbusattach(struct device *parent, struct device *self, void *aux)
114 {
115 struct cardbus_softc *sc = (void *)self;
116 struct cbslot_attach_args *cba = aux;
117
118 sc->sc_bus = cba->cba_bus;
119 sc->sc_device = 0;
120 sc->sc_intrline = cba->cba_intrline;
121 sc->sc_cacheline = cba->cba_cacheline;
122 sc->sc_lattimer = cba->cba_lattimer;
123
124 printf(": bus %d device %d", sc->sc_bus, sc->sc_device);
125 printf(" cacheline 0x%x, lattimer 0x%x\n", sc->sc_cacheline,
126 sc->sc_lattimer);
127
128 sc->sc_iot = cba->cba_iot; /* CardBus I/O space tag */
129 sc->sc_memt = cba->cba_memt; /* CardBus MEM space tag */
130 sc->sc_dmat = cba->cba_dmat; /* DMA tag */
131 sc->sc_cc = cba->cba_cc;
132 sc->sc_cf = cba->cba_cf;
133
134 #if rbus
135 sc->sc_rbus_iot = cba->cba_rbus_iot;
136 sc->sc_rbus_memt = cba->cba_rbus_memt;
137 #endif
138
139 sc->sc_funcs = NULL;
140 }
141
142 static int
143 cardbus_read_tuples(struct cardbus_attach_args *ca, cardbusreg_t cis_ptr,
144 u_int8_t *tuples, size_t len)
145 {
146 struct cardbus_softc *sc = ca->ca_ct->ct_sc;
147 cardbus_chipset_tag_t cc = ca->ca_ct->ct_cc;
148 cardbus_function_tag_t cf = ca->ca_ct->ct_cf;
149 cardbustag_t tag = ca->ca_tag;
150 cardbusreg_t command;
151 bus_space_tag_t bar_tag;
152 bus_space_handle_t bar_memh;
153 bus_size_t bar_size;
154 bus_addr_t bar_addr;
155 cardbusreg_t reg;
156 int found = 0;
157 int cardbus_space = cis_ptr & CARDBUS_CIS_ASIMASK;
158 int i, j;
159
160 memset(tuples, 0, len);
161
162 cis_ptr = cis_ptr & CARDBUS_CIS_ADDRMASK;
163
164 switch (cardbus_space) {
165 case CARDBUS_CIS_ASI_TUPLE:
166 DPRINTF(("%s: reading CIS data from configuration space\n",
167 sc->sc_dev.dv_xname));
168 for (i = cis_ptr, j = 0; i < 0xff; i += 4) {
169 u_int32_t e = (*cf->cardbus_conf_read)(cc, tag, i);
170 tuples[j] = 0xff & e;
171 e >>= 8;
172 tuples[j + 1] = 0xff & e;
173 e >>= 8;
174 tuples[j + 2] = 0xff & e;
175 e >>= 8;
176 tuples[j + 3] = 0xff & e;
177 j += 4;
178 }
179 found++;
180 break;
181
182 case CARDBUS_CIS_ASI_BAR0:
183 case CARDBUS_CIS_ASI_BAR1:
184 case CARDBUS_CIS_ASI_BAR2:
185 case CARDBUS_CIS_ASI_BAR3:
186 case CARDBUS_CIS_ASI_BAR4:
187 case CARDBUS_CIS_ASI_BAR5:
188 case CARDBUS_CIS_ASI_ROM:
189 if (cardbus_space == CARDBUS_CIS_ASI_ROM) {
190 reg = CARDBUS_ROM_REG;
191 DPRINTF(("%s: reading CIS data from ROM\n",
192 sc->sc_dev.dv_xname));
193 } else {
194 reg = CARDBUS_BASE0_REG + (cardbus_space - 1) * 4;
195 DPRINTF(("%s: reading CIS data from BAR%d\n",
196 sc->sc_dev.dv_xname, cardbus_space - 1));
197 }
198
199 /*
200 * XXX zero register so mapreg_map doesn't get confused by old
201 * contents.
202 */
203 cardbus_conf_write(cc, cf, tag, reg, 0);
204 if (Cardbus_mapreg_map(ca->ca_ct, reg,
205 CARDBUS_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_32BIT,
206 0, &bar_tag, &bar_memh, &bar_addr, &bar_size)) {
207 printf("%s: failed to map memory\n",
208 sc->sc_dev.dv_xname);
209 return (1);
210 }
211
212 if (cardbus_space == CARDBUS_CIS_ASI_ROM) {
213 cardbusreg_t exrom;
214 int save;
215 struct cardbus_rom_image_head rom_image;
216 struct cardbus_rom_image *p;
217
218 save = splhigh();
219 /* enable rom address decoder */
220 exrom = cardbus_conf_read(cc, cf, tag, reg);
221 cardbus_conf_write(cc, cf, tag, reg, exrom | 1);
222
223 command = cardbus_conf_read(cc, cf, tag,
224 CARDBUS_COMMAND_STATUS_REG);
225 cardbus_conf_write(cc, cf, tag,
226 CARDBUS_COMMAND_STATUS_REG,
227 command | CARDBUS_COMMAND_MEM_ENABLE);
228
229 if (cardbus_read_exrom(ca->ca_memt, bar_memh,
230 &rom_image))
231 goto out;
232
233 for (p = SIMPLEQ_FIRST(&rom_image); p != NULL;
234 p = SIMPLEQ_NEXT(p, next)) {
235 if (p->rom_image ==
236 CARDBUS_CIS_ASI_ROM_IMAGE(cis_ptr)) {
237 bus_space_read_region_1(p->romt,
238 p->romh, CARDBUS_CIS_ADDR(cis_ptr),
239 tuples, 256);
240 found++;
241 }
242 break;
243 }
244 while ((p = SIMPLEQ_FIRST(&rom_image)) != NULL) {
245 SIMPLEQ_REMOVE_HEAD(&rom_image, p, next);
246 free(p, M_DEVBUF);
247 }
248 out:
249 exrom = cardbus_conf_read(cc, cf, tag, reg);
250 cardbus_conf_write(cc, cf, tag, reg, exrom & ~1);
251 splx(save);
252 } else {
253 command = cardbus_conf_read(cc, cf, tag,
254 CARDBUS_COMMAND_STATUS_REG);
255 cardbus_conf_write(cc, cf, tag,
256 CARDBUS_COMMAND_STATUS_REG,
257 command | CARDBUS_COMMAND_MEM_ENABLE);
258 /* XXX byte order? */
259 bus_space_read_region_1(ca->ca_memt, bar_memh,
260 cis_ptr, tuples, 256);
261 found++;
262 }
263 command = cardbus_conf_read(cc, cf, tag,
264 CARDBUS_COMMAND_STATUS_REG);
265 cardbus_conf_write(cc, cf, tag, CARDBUS_COMMAND_STATUS_REG,
266 command & ~CARDBUS_COMMAND_MEM_ENABLE);
267 cardbus_conf_write(cc, cf, tag, reg, 0);
268
269 Cardbus_mapreg_unmap(ca->ca_ct, reg, bar_tag, bar_memh,
270 bar_size);
271 break;
272
273 #ifdef DIAGNOSTIC
274 default:
275 panic("%s: bad CIS space (%d)", sc->sc_dev.dv_xname,
276 cardbus_space);
277 #endif
278 }
279 return (!found);
280 }
281
282 static void
283 parse_tuple(u_int8_t *tuple, int len, void *data)
284 {
285 #ifdef CARDBUS_DEBUG
286 static const char __func__[] = "parse_tuple";
287 #endif
288 struct cardbus_cis_info *cis = data;
289 char *p;
290 int i, bar_index;
291
292 switch (tuple[0]) {
293 case PCMCIA_CISTPL_MANFID:
294 if (tuple[1] != 5) {
295 DPRINTF(("%s: wrong length manufacturer id (%d)\n",
296 __func__, tuple[1]));
297 break;
298 }
299 cis->manufacturer = tuple[2] | (tuple[3] << 8);
300 cis->product = tuple[4] | (tuple[5] << 8);
301 break;
302
303 case PCMCIA_CISTPL_VERS_1:
304 memcpy(cis->cis1_info_buf, tuple + 2, tuple[1]);
305 i = 0;
306 p = cis->cis1_info_buf + 2;
307 while (i <
308 sizeof(cis->cis1_info) / sizeof(cis->cis1_info[0])) {
309 cis->cis1_info[i++] = p;
310 while (*p != '\0' && *p != '\xff')
311 p++;
312 if (*p == '\xff')
313 break;
314 p++;
315 }
316 break;
317
318 case PCMCIA_CISTPL_BAR:
319 if (tuple[1] != 6) {
320 DPRINTF(("%s: BAR with short length (%d)\n",
321 __func__, tuple[1]));
322 break;
323 }
324 bar_index = tuple[2] & 7;
325 if (bar_index == 0) {
326 DPRINTF(("%s: invalid ASI in BAR tuple\n", __func__));
327 break;
328 }
329 bar_index--;
330 cis->bar[bar_index].flags = tuple[2];
331 cis->bar[bar_index].size =
332 (tuple[4] << 0) |
333 (tuple[5] << 8) |
334 (tuple[6] << 16) |
335 (tuple[7] << 24);
336 break;
337
338 case PCMCIA_CISTPL_FUNCID:
339 cis->funcid = tuple[2];
340 break;
341
342 case PCMCIA_CISTPL_FUNCE:
343 switch (cis->funcid) {
344 case PCMCIA_FUNCTION_SERIAL:
345 if (tuple[1] >= 2 &&
346 /* XXX PCMCIA_TPLFE_TYPE_SERIAL_??? */
347 tuple[2] == 0) {
348 cis->funce.serial.uart_type = tuple[3] & 0x1f;
349 cis->funce.serial.uart_present = 1;
350 }
351 break;
352
353 case PCMCIA_FUNCTION_NETWORK:
354 if (tuple[1] >= 8 &&
355 tuple[2] == PCMCIA_TPLFE_TYPE_LAN_NID) {
356 if (tuple[3] >
357 sizeof(cis->funce.network.netid)) {
358 DPRINTF(("%s: unknown network id type "
359 "(len = %d)\n",
360 __func__, tuple[3]));
361 } else {
362 cis->funce.network.netid_present = 1;
363 memcpy(cis->funce.network.netid,
364 tuple + 4, tuple[3]);
365 }
366 }
367 break;
368 }
369 break;
370 }
371 }
372
373 /*
374 * int cardbus_attach_card(struct cardbus_softc *sc)
375 *
376 * This function attaches the card on the slot: turns on power,
377 * reads and analyses tuple, sets configuration index.
378 *
379 * This function returns the number of recognised device functions.
380 * If no functions are recognised, return 0.
381 */
382 int
383 cardbus_attach_card(struct cardbus_softc *sc)
384 {
385 cardbus_chipset_tag_t cc;
386 cardbus_function_tag_t cf;
387 cardbustag_t tag;
388 cardbusreg_t id, class, cis_ptr;
389 cardbusreg_t bhlc;
390 u_int8_t tuple[2048];
391 int cdstatus;
392 int function, nfunction;
393 struct cardbus_devfunc **previous_next = &(sc->sc_funcs);
394 struct device *csc;
395 int no_work_funcs = 0;
396 cardbus_devfunc_t ct;
397
398 cc = sc->sc_cc;
399 cf = sc->sc_cf;
400
401 DPRINTF(("cardbus_attach_card: cb%d start\n", sc->sc_dev.dv_unit));
402
403 /* inspect initial voltage */
404 if ((cdstatus = (*cf->cardbus_ctrl)(cc, CARDBUS_CD)) == 0) {
405 DPRINTF(("cardbusattach: no CardBus card on cb%d\n",
406 sc->sc_dev.dv_unit));
407 return (0);
408 }
409
410 /*
411 * XXX use fake function 8 to keep power on during whole
412 * configuration.
413 */
414 enable_function(sc, cdstatus, 8);
415 function = 0;
416
417 tag = cardbus_make_tag(cc, cf, sc->sc_bus, sc->sc_device, function);
418
419 /*
420 * Wait until power comes up. Maxmum 500 ms.
421 */
422 {
423 int i;
424
425 for (i = 0; i < 5; ++i) {
426 id = cardbus_conf_read(cc, cf, tag, CARDBUS_ID_REG);
427 if (id != 0xffffffff && id != 0) {
428 break;
429 }
430 if (cold) { /* before kernel thread invoked */
431 delay(100 * 1000);
432 } else { /* thread context */
433 if (tsleep((void *)sc, PCATCH, "cardbus",
434 hz / 10) != EWOULDBLOCK) {
435 break;
436 }
437 }
438 }
439 if (i == 5) {
440 return (0);
441 }
442 }
443
444 nfunction = CARDBUS_HDRTYPE_MULTIFN(bhlc) ? 8 : 1;
445
446 for (function = 0; function < nfunction; function++) {
447 struct cardbus_attach_args ca;
448
449 tag = cardbus_make_tag(cc, cf, sc->sc_bus, sc->sc_device,
450 function);
451
452 id = cardbus_conf_read(cc, cf, tag, CARDBUS_ID_REG);
453 class = cardbus_conf_read(cc, cf, tag, CARDBUS_CLASS_REG);
454 cis_ptr = cardbus_conf_read(cc, cf, tag, CARDBUS_CIS_REG);
455
456 /* Invalid vendor ID value? */
457 if (CARDBUS_VENDOR(id) == CARDBUS_VENDOR_INVALID) {
458 continue;
459 }
460
461 DPRINTF(("cardbus_attach_card: "
462 "Vendor 0x%x, Product 0x%x, CIS 0x%x\n",
463 CARDBUS_VENDOR(id), CARDBUS_PRODUCT(id), cis_ptr));
464
465 enable_function(sc, cdstatus, function);
466
467 /* clean up every BAR */
468 cardbus_conf_write(cc, cf, tag, CARDBUS_BASE0_REG, 0);
469 cardbus_conf_write(cc, cf, tag, CARDBUS_BASE1_REG, 0);
470 cardbus_conf_write(cc, cf, tag, CARDBUS_BASE2_REG, 0);
471 cardbus_conf_write(cc, cf, tag, CARDBUS_BASE3_REG, 0);
472 cardbus_conf_write(cc, cf, tag, CARDBUS_BASE4_REG, 0);
473 cardbus_conf_write(cc, cf, tag, CARDBUS_BASE5_REG, 0);
474 cardbus_conf_write(cc, cf, tag, CARDBUS_ROM_REG, 0);
475
476 /* set initial latency and cacheline size */
477 bhlc = cardbus_conf_read(cc, cf, tag, CARDBUS_BHLC_REG);
478 DPRINTF(("%s func%d bhlc 0x%08x -> ", sc->sc_dev.dv_xname,
479 function, bhlc));
480 bhlc &= ~((CARDBUS_LATTIMER_MASK << CARDBUS_LATTIMER_SHIFT) |
481 (CARDBUS_CACHELINE_MASK << CARDBUS_CACHELINE_SHIFT));
482 bhlc |= ((sc->sc_cacheline & CARDBUS_CACHELINE_MASK) << CARDBUS_CACHELINE_SHIFT);
483 bhlc |= ((sc->sc_lattimer & CARDBUS_LATTIMER_MASK) << CARDBUS_LATTIMER_SHIFT);
484
485 cardbus_conf_write(cc, cf, tag, CARDBUS_BHLC_REG, bhlc);
486 bhlc = cardbus_conf_read(cc, cf, tag, CARDBUS_BHLC_REG);
487 DPRINTF(("0x%08x\n", bhlc));
488
489 if (CARDBUS_LATTIMER(bhlc) < 0x10) {
490 bhlc &= ~(CARDBUS_LATTIMER_MASK << CARDBUS_LATTIMER_SHIFT);
491 bhlc |= (0x10 << CARDBUS_LATTIMER_SHIFT);
492 cardbus_conf_write(cc, cf, tag, CARDBUS_BHLC_REG, bhlc);
493 }
494
495 /*
496 * We need to allocate the ct here, since we might
497 * need it when reading the CIS
498 */
499 if ((ct = malloc(sizeof(struct cardbus_devfunc),
500 M_DEVBUF, M_NOWAIT)) == NULL) {
501 panic("no room for cardbus_tag");
502 }
503
504 ct->ct_cc = sc->sc_cc;
505 ct->ct_cf = sc->sc_cf;
506 ct->ct_bus = sc->sc_bus;
507 ct->ct_dev = sc->sc_device;
508 ct->ct_func = function;
509 ct->ct_sc = sc;
510 ct->ct_next = NULL;
511 *previous_next = ct;
512
513 memset(&ca, 0, sizeof(ca));
514
515 ca.ca_unit = sc->sc_dev.dv_unit;
516 ca.ca_ct = ct;
517
518 ca.ca_iot = sc->sc_iot;
519 ca.ca_memt = sc->sc_memt;
520 ca.ca_dmat = sc->sc_dmat;
521
522 ca.ca_tag = tag;
523 ca.ca_device = sc->sc_device;
524 ca.ca_function = function;
525 ca.ca_id = id;
526 ca.ca_class = class;
527
528 ca.ca_intrline = sc->sc_intrline;
529
530 if (cardbus_read_tuples(&ca, cis_ptr, tuple, sizeof(tuple))) {
531 printf("cardbus_attach_card: failed to read CIS\n");
532 } else {
533 #ifdef CARDBUS_DEBUG
534 decode_tuples(tuple, 2048, print_tuple, NULL);
535 #endif
536 decode_tuples(tuple, 2048, parse_tuple, &ca.ca_cis);
537 }
538
539 if ((csc = config_found_sm((void *)sc, &ca, cardbusprint,
540 cardbussubmatch)) == NULL) {
541 /* do not match */
542 disable_function(sc, function);
543 free(ct, M_DEVBUF);
544 *previous_next = NULL;
545 } else {
546 /* found */
547 previous_next = &(ct->ct_next);
548 ct->ct_device = csc;
549 ++no_work_funcs;
550 }
551 }
552 /*
553 * XXX power down pseudo function 8 (this will power down the card
554 * if no functions were attached).
555 */
556 disable_function(sc, 8);
557
558 return (no_work_funcs);
559 }
560
561 static int
562 cardbussubmatch(struct device *parent, struct cfdata *cf, void *aux)
563 {
564 struct cardbus_attach_args *ca = aux;
565
566 if (cf->cardbuscf_dev != CARDBUS_UNK_DEV &&
567 cf->cardbuscf_dev != ca->ca_unit) {
568 return (0);
569 }
570 if (cf->cardbuscf_function != CARDBUS_UNK_FUNCTION &&
571 cf->cardbuscf_function != ca->ca_function) {
572 return (0);
573 }
574
575 return ((*cf->cf_attach->ca_match)(parent, cf, aux));
576 }
577
578 static int
579 cardbusprint(void *aux, const char *pnp)
580 {
581 struct cardbus_attach_args *ca = aux;
582 char devinfo[256];
583 int i;
584
585 if (pnp) {
586 pci_devinfo(ca->ca_id, ca->ca_class, 1, devinfo);
587 for (i = 0; i < 4; i++) {
588 if (ca->ca_cis.cis1_info[i] == NULL)
589 break;
590 if (i)
591 printf(", ");
592 printf("%s", ca->ca_cis.cis1_info[i]);
593 }
594 if (i)
595 printf(" ");
596 printf("(manufacturer 0x%x, product 0x%x)",
597 ca->ca_cis.manufacturer, ca->ca_cis.product);
598 printf(" %s at %s", devinfo, pnp);
599 }
600 printf(" dev %d function %d", ca->ca_device, ca->ca_function);
601
602 return (UNCONF);
603 }
604
605 /*
606 * void cardbus_detach_card(struct cardbus_softc *sc)
607 *
608 * This function detaches the card on the slot: detach device data
609 * structure and turns off the power.
610 *
611 * This function must not be called under interrupt context.
612 */
613 void
614 cardbus_detach_card(struct cardbus_softc *sc)
615 {
616 struct cardbus_devfunc *ct, *ct_next, **prev_next;
617
618 prev_next = &(sc->sc_funcs->ct_next);
619
620 for (ct = sc->sc_funcs; ct != NULL; ct = ct_next) {
621 struct device *fndev = ct->ct_device;
622 ct_next = ct->ct_next;
623
624 DPRINTF(("%s: detaching %s\n", sc->sc_dev.dv_xname,
625 fndev->dv_xname));
626 /* call device detach function */
627
628 if (0 != config_detach(fndev, 0)) {
629 printf("%s: cannot detaching dev %s, function %d\n",
630 sc->sc_dev.dv_xname, fndev->dv_xname, ct->ct_func);
631 prev_next = &(ct->ct_next);
632 } else {
633 sc->sc_poweron_func &= ~(1 << ct->ct_func);
634 *prev_next = ct->ct_next;
635 free(ct, M_DEVBUF);
636 }
637 }
638
639 sc->sc_poweron_func = 0;
640 (*sc->sc_cf->cardbus_power)(sc->sc_cc,
641 CARDBUS_VCC_0V | CARDBUS_VPP_0V);
642 }
643
644 /*
645 * void *cardbus_intr_establish(cc, cf, irq, level, func, arg)
646 * Interrupt handler of pccard.
647 * args:
648 * cardbus_chipset_tag_t *cc
649 * int irq:
650 */
651 void *
652 cardbus_intr_establish(cardbus_chipset_tag_t cc, cardbus_function_tag_t cf,
653 cardbus_intr_handle_t irq, int level, int (*func)(void *), void *arg)
654 {
655
656 DPRINTF(("- cardbus_intr_establish: irq %d\n", irq));
657 return ((*cf->cardbus_intr_establish)(cc, irq, level, func, arg));
658 }
659
660 /*
661 * void cardbus_intr_disestablish(cc, cf, handler)
662 * Interrupt handler of pccard.
663 * args:
664 * cardbus_chipset_tag_t *cc
665 */
666 void
667 cardbus_intr_disestablish(cardbus_chipset_tag_t cc, cardbus_function_tag_t cf,
668 void *handler)
669 {
670
671 DPRINTF(("- pccard_intr_disestablish\n"));
672 (*cf->cardbus_intr_disestablish)(cc, handler);
673 }
674
675 /*
676 * XXX this should be merged with cardbus_function_{enable,disable},
677 * but we don't have a ct when these functions are called.
678 */
679 static void
680 enable_function(struct cardbus_softc *sc, int cdstatus, int function)
681 {
682
683 if (sc->sc_poweron_func == 0) {
684 /* switch to 3V and/or wait for power to stabilize */
685 if (cdstatus & CARDBUS_3V_CARD) {
686 /*
687 * sc_poweron_func must be substituted before
688 * entering sleep, in order to avoid turn on
689 * power twice.
690 */
691 sc->sc_poweron_func |= (1 << function);
692 (*sc->sc_cf->cardbus_power)(sc->sc_cc, CARDBUS_VCC_3V);
693 } else {
694 /* No cards other than 3.3V cards. */
695 return;
696 }
697 (*sc->sc_cf->cardbus_ctrl)(sc->sc_cc, CARDBUS_RESET);
698 }
699 sc->sc_poweron_func |= (1 << function);
700 }
701
702 static void
703 disable_function(struct cardbus_softc *sc, int function)
704 {
705
706 sc->sc_poweron_func &= ~(1 << function);
707 if (sc->sc_poweron_func == 0) {
708 /* power-off because no functions are enabled */
709 (*sc->sc_cf->cardbus_power)(sc->sc_cc, CARDBUS_VCC_0V);
710 }
711 }
712
713 /*
714 * int cardbus_function_enable(struct cardbus_softc *sc, int func)
715 *
716 * This function enables a function on a card. When no power is
717 * applied on the card, power will be applied on it.
718 */
719 int
720 cardbus_function_enable(struct cardbus_softc *sc, int func)
721 {
722 cardbus_chipset_tag_t cc = sc->sc_cc;
723 cardbus_function_tag_t cf = sc->sc_cf;
724 cardbusreg_t command;
725 cardbustag_t tag;
726
727 DPRINTF(("entering cardbus_function_enable... "));
728
729 /* entering critical area */
730
731 /* XXX: sc_vold should be used */
732 enable_function(sc, CARDBUS_3V_CARD, func);
733
734 /* exiting critical area */
735
736 tag = cardbus_make_tag(cc, cf, sc->sc_bus, sc->sc_device, func);
737
738 command = cardbus_conf_read(cc, cf, tag, CARDBUS_COMMAND_STATUS_REG);
739 command |= (CARDBUS_COMMAND_MEM_ENABLE | CARDBUS_COMMAND_IO_ENABLE |
740 CARDBUS_COMMAND_MASTER_ENABLE); /* XXX: good guess needed */
741
742 cardbus_conf_write(cc, cf, tag, CARDBUS_COMMAND_STATUS_REG, command);
743
744 cardbus_free_tag(cc, cf, tag);
745
746 DPRINTF(("%x\n", sc->sc_poweron_func));
747
748 return (0);
749 }
750
751 /*
752 * int cardbus_function_disable(struct cardbus_softc *, int func)
753 *
754 * This function disable a function on a card. When no functions are
755 * enabled, it turns off the power.
756 */
757 int
758 cardbus_function_disable(struct cardbus_softc *sc, int func)
759 {
760
761 DPRINTF(("entering cardbus_function_disable... "));
762
763 disable_function(sc, func);
764
765 return (0);
766 }
767
768 /*
769 * int cardbus_get_capability(cardbus_chipset_tag_t cc,
770 * cardbus_function_tag_t cf, cardbustag_t tag, int capid, int *offset,
771 * cardbusreg_t *value)
772 *
773 * Find the specified PCI capability.
774 */
775 int
776 cardbus_get_capability(cardbus_chipset_tag_t cc, cardbus_function_tag_t cf,
777 cardbustag_t tag, int capid, int *offset, cardbusreg_t *value)
778 {
779 cardbusreg_t reg;
780 unsigned int ofs;
781
782 reg = cardbus_conf_read(cc, cf, tag, PCI_COMMAND_STATUS_REG);
783 if (!(reg & PCI_STATUS_CAPLIST_SUPPORT))
784 return (0);
785
786 ofs = PCI_CAPLIST_PTR(cardbus_conf_read(cc, cf, tag,
787 PCI_CAPLISTPTR_REG));
788 while (ofs != 0) {
789 #ifdef DIAGNOSTIC
790 if ((ofs & 3) || (ofs < 0x40))
791 panic("cardbus_get_capability");
792 #endif
793 reg = cardbus_conf_read(cc, cf, tag, ofs);
794 if (PCI_CAPLIST_CAP(reg) == capid) {
795 if (offset)
796 *offset = ofs;
797 if (value)
798 *value = reg;
799 return (1);
800 }
801 ofs = PCI_CAPLIST_NEXT(reg);
802 }
803
804 return (0);
805 }
806
807 /*
808 * below this line, there are some functions for decoding tuples.
809 * They should go out from this file.
810 */
811
812 static u_int8_t *
813 decode_tuple(u_int8_t *tuple, tuple_decode_func func, void *data);
814
815 static int
816 decode_tuples(u_int8_t *tuple, int buflen, tuple_decode_func func, void *data)
817 {
818 u_int8_t *tp = tuple;
819
820 if (PCMCIA_CISTPL_LINKTARGET != *tuple) {
821 DPRINTF(("WRONG TUPLE: 0x%x\n", *tuple));
822 return (0);
823 }
824
825 while (NULL != (tp = decode_tuple(tp, func, data))) {
826 if (tuple + buflen < tp) {
827 break;
828 }
829 }
830
831 return (1);
832 }
833
834 static u_int8_t *
835 decode_tuple(u_int8_t *tuple, tuple_decode_func func, void *data)
836 {
837 u_int8_t type;
838 u_int8_t len;
839
840 type = tuple[0];
841 len = tuple[1] + 2;
842
843 (*func)(tuple, len, data);
844
845 if (type == PCMCIA_CISTPL_END) {
846 return (NULL);
847 }
848
849 return (tuple + len);
850 }
851
852 #ifdef CARDBUS_DEBUG
853 static const char *tuple_name(int);
854 static const char *tuple_names[] = {
855 "TPL_NULL", "TPL_DEVICE", "Reserved", "Reserved", /* 0-3 */
856 "CONFIG_CB", "CFTABLE_ENTRY_CB", "Reserved", "BAR", /* 4-7 */
857 "Reserved", "Reserved", "Reserved", "Reserved", /* 8-B */
858 "Reserved", "Reserved", "Reserved", "Reserved", /* C-F */
859 "CHECKSUM", "LONGLINK_A", "LONGLINK_C", "LINKTARGET", /* 10-13 */
860 "NO_LINK", "VERS_1", "ALTSTR", "DEVICE_A",
861 "JEDEC_C", "JEDEC_A", "CONFIG", "CFTABLE_ENTRY",
862 "DEVICE_OC", "DEVICE_OA", "DEVICE_GEO", "DEVICE_GEO_A",
863 "MANFID", "FUNCID", "FUNCE", "SWIL", /* 20-23 */
864 "Reserved", "Reserved", "Reserved", "Reserved", /* 24-27 */
865 "Reserved", "Reserved", "Reserved", "Reserved", /* 28-2B */
866 "Reserved", "Reserved", "Reserved", "Reserved", /* 2C-2F */
867 "Reserved", "Reserved", "Reserved", "Reserved", /* 30-33 */
868 "Reserved", "Reserved", "Reserved", "Reserved", /* 34-37 */
869 "Reserved", "Reserved", "Reserved", "Reserved", /* 38-3B */
870 "Reserved", "Reserved", "Reserved", "Reserved", /* 3C-3F */
871 "VERS_2", "FORMAT", "GEOMETRY", "BYTEORDER",
872 "DATE", "BATTERY", "ORG"
873 };
874 #define NAME_LEN(x) (sizeof x / sizeof(x[0]))
875
876 static const char *
877 tuple_name(int type)
878 {
879
880 if (0 <= type && type < NAME_LEN(tuple_names)) {
881 return (tuple_names[type]);
882 } else if (type == 0xff) {
883 return ("END");
884 } else {
885 return ("Reserved");
886 }
887 }
888
889 static void
890 print_tuple(u_int8_t *tuple, int len, void *data)
891 {
892 int i;
893
894 printf("tuple: %s len %d\n", tuple_name(tuple[0]), len);
895
896 for (i = 0; i < len; ++i) {
897 if (i % 16 == 0) {
898 printf(" 0x%2x:", i);
899 }
900 printf(" %x", tuple[i]);
901 if (i % 16 == 15) {
902 printf("\n");
903 }
904 }
905 if (i % 16 != 0) {
906 printf("\n");
907 }
908 }
909 #endif
910