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