2 * Copyright (c) 2000,2001 Jonathan Chen.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
31 * CIS Handling for the Cardbus Bus
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/kernel.h>
37 #include <sys/malloc.h>
40 #include <machine/bus.h>
41 #include <machine/resource.h>
43 #include <sys/endian.h>
45 #include <sys/pciio.h>
46 #include <dev/pci/pcivar.h>
47 #include <dev/pci/pcireg.h>
49 #include <dev/pccard/pccardvar.h>
50 #include <dev/pccard/pccard_cis.h>
52 #include <dev/cardbus/cardbusreg.h>
53 #include <dev/cardbus/cardbusvar.h>
54 #include <dev/cardbus/cardbus_cis.h>
56 extern int cardbus_cis_debug;
58 #define DPRINTF(a) if (cardbus_cis_debug) printf a
59 #define DEVPRINTF(x) if (cardbus_cis_debug) device_printf x
61 #define CIS_CONFIG_SPACE (struct resource *)~0UL
63 static int decode_tuple_generic(device_t cbdev, device_t child, int id,
64 int len, uint8_t *tupledata, uint32_t start, uint32_t *off,
65 struct tuple_callbacks *info, void *);
66 static int decode_tuple_linktarget(device_t cbdev, device_t child, int id,
67 int len, uint8_t *tupledata, uint32_t start, uint32_t *off,
68 struct tuple_callbacks *info, void *);
69 static int decode_tuple_vers_1(device_t cbdev, device_t child, int id,
70 int len, uint8_t *tupledata, uint32_t start, uint32_t *off,
71 struct tuple_callbacks *info, void *);
72 static int decode_tuple_funcid(device_t cbdev, device_t child, int id,
73 int len, uint8_t *tupledata, uint32_t start, uint32_t *off,
74 struct tuple_callbacks *info, void *);
75 static int decode_tuple_manfid(device_t cbdev, device_t child, int id,
76 int len, uint8_t *tupledata, uint32_t start, uint32_t *off,
77 struct tuple_callbacks *info, void *);
78 static int decode_tuple_funce(device_t cbdev, device_t child, int id,
79 int len, uint8_t *tupledata, uint32_t start, uint32_t *off,
80 struct tuple_callbacks *info, void *);
81 static int decode_tuple_bar(device_t cbdev, device_t child, int id,
82 int len, uint8_t *tupledata, uint32_t start, uint32_t *off,
83 struct tuple_callbacks *info, void *);
84 static int decode_tuple_unhandled(device_t cbdev, device_t child, int id,
85 int len, uint8_t *tupledata, uint32_t start, uint32_t *off,
86 struct tuple_callbacks *info, void *);
87 static int decode_tuple_end(device_t cbdev, device_t child, int id,
88 int len, uint8_t *tupledata, uint32_t start, uint32_t *off,
89 struct tuple_callbacks *info, void *);
91 static int cardbus_read_tuple_conf(device_t cbdev, device_t child,
92 uint32_t start, uint32_t *off, int *tupleid, int *len,
94 static int cardbus_read_tuple_mem(device_t cbdev, struct resource *res,
95 uint32_t start, uint32_t *off, int *tupleid, int *len,
97 static int cardbus_read_tuple(device_t cbdev, device_t child,
98 struct resource *res, uint32_t start, uint32_t *off,
99 int *tupleid, int *len, uint8_t *tupledata);
100 static void cardbus_read_tuple_finish(device_t cbdev, device_t child,
101 int rid, struct resource *res);
102 static struct resource *cardbus_read_tuple_init(device_t cbdev, device_t child,
103 uint32_t *start, int *rid);
104 static int decode_tuple(device_t cbdev, device_t child, int tupleid,
105 int len, uint8_t *tupledata, uint32_t start,
106 uint32_t *off, struct tuple_callbacks *callbacks,
109 #define MAKETUPLE(NAME,FUNC) { CISTPL_ ## NAME, #NAME, decode_tuple_ ## FUNC }
111 static char *funcnames[] = {
125 * Handler functions for various CIS tuples
129 decode_tuple_generic(device_t cbdev, device_t child, int id,
130 int len, uint8_t *tupledata, uint32_t start, uint32_t *off,
131 struct tuple_callbacks *info, void *argp)
135 if (cardbus_cis_debug) {
137 printf("TUPLE: %s [%d]:", info->name, len);
139 printf("TUPLE: Unknown(0x%02x) [%d]:", id, len);
141 for (i = 0; i < len; i++) {
142 if (i % 0x10 == 0 && len > 0x10)
143 printf("\n 0x%02x:", i);
144 printf(" %02x", tupledata[i]);
152 decode_tuple_linktarget(device_t cbdev, device_t child, int id,
153 int len, uint8_t *tupledata, uint32_t start, uint32_t *off,
154 struct tuple_callbacks *info, void *argp)
158 if (cardbus_cis_debug) {
159 printf("TUPLE: %s [%d]:", info->name, len);
161 for (i = 0; i < len; i++) {
162 if (i % 0x10 == 0 && len > 0x10)
163 printf("\n 0x%02x:", i);
164 printf(" %02x", tupledata[i]);
168 if (len != 3 || tupledata[0] != 'C' || tupledata[1] != 'I' ||
169 tupledata[2] != 'S') {
170 printf("Invalid data for CIS Link Target!\n");
171 decode_tuple_generic(cbdev, child, id, len, tupledata,
172 start, off, info, argp);
179 decode_tuple_vers_1(device_t cbdev, device_t child, int id,
180 int len, uint8_t *tupledata, uint32_t start, uint32_t *off,
181 struct tuple_callbacks *info, void *argp)
185 if (cardbus_cis_debug) {
186 printf("Product version: %d.%d\n", tupledata[0], tupledata[1]);
187 printf("Product name: ");
188 for (i = 2; i < len; i++) {
189 if (tupledata[i] == '\0')
191 else if (tupledata[i] == 0xff)
194 printf("%c", tupledata[i]);
202 decode_tuple_funcid(device_t cbdev, device_t child, int id,
203 int len, uint8_t *tupledata, uint32_t start, uint32_t *off,
204 struct tuple_callbacks *info, void *argp)
206 struct cardbus_devinfo *dinfo = device_get_ivars(child);
207 int numnames = sizeof(funcnames) / sizeof(funcnames[0]);
210 if (cardbus_cis_debug) {
211 printf("Functions: ");
212 for (i = 0; i < len; i++) {
213 if (tupledata[i] < numnames)
214 printf("%s", funcnames[tupledata[i]]);
216 printf("Unknown(%d)", tupledata[i]);
223 dinfo->funcid = tupledata[0]; /* use first in list */
228 decode_tuple_manfid(device_t cbdev, device_t child, int id,
229 int len, uint8_t *tupledata, uint32_t start, uint32_t *off,
230 struct tuple_callbacks *info, void *argp)
232 struct cardbus_devinfo *dinfo = device_get_ivars(child);
235 if (cardbus_cis_debug) {
236 printf("Manufacturer ID: ");
237 for (i = 0; i < len; i++)
238 printf("%02x", tupledata[i]);
243 dinfo->mfrid = tupledata[1] | (tupledata[2] << 8);
244 dinfo->prodid = tupledata[3] | (tupledata[4] << 8);
250 decode_tuple_funce(device_t cbdev, device_t child, int id,
251 int len, uint8_t *tupledata, uint32_t start, uint32_t *off,
252 struct tuple_callbacks *info, void *argp)
254 struct cardbus_devinfo *dinfo = device_get_ivars(child);
257 if (cardbus_cis_debug) {
258 printf("Function Extension: ");
259 for (i = 0; i < len; i++)
260 printf("%02x", tupledata[i]);
263 if (len < 2) /* too short */
265 type = tupledata[0]; /* XXX <32 always? */
266 switch (dinfo->funcid) {
267 case PCCARD_FUNCTION_NETWORK:
269 case PCCARD_TPLFE_TYPE_LAN_NID:
270 if (tupledata[1] > sizeof(dinfo->funce.lan.nid)) {
271 /* ignore, warning? */
274 bcopy(tupledata + 2, dinfo->funce.lan.nid,
278 dinfo->fepresent |= 1<<type;
285 decode_tuple_bar(device_t cbdev, device_t child, int id,
286 int len, uint8_t *tupledata, uint32_t start, uint32_t *off,
287 struct tuple_callbacks *info, void *argp)
289 struct cardbus_devinfo *dinfo = device_get_ivars(child);
295 device_printf(cbdev, "CIS BAR length not 6 (%d)\n", len);
300 len = le32toh(*(uint32_t*)(tupledata + 2));
301 if (reg & TPL_BAR_REG_AS)
302 type = SYS_RES_IOPORT;
304 type = SYS_RES_MEMORY;
306 bar = reg & TPL_BAR_REG_ASI_MASK;
308 device_printf(cbdev, "Invalid BAR type 0 in CIS\n");
309 return (EINVAL); /* XXX Return an error? */
310 } else if (bar == 7) {
311 /* XXX Should we try to map in Option ROMs? */
315 /* Convert from BAR type to BAR offset */
316 bar = PCIR_BAR(bar - 1);
318 if (type == SYS_RES_MEMORY) {
319 if (reg & TPL_BAR_REG_PREFETCHABLE)
320 dinfo->mprefetchable |= (1 << PCI_RID2BAR(bar));
323 * XXX: It appears from a careful reading of the spec
324 * that we're not supposed to honor this when the bridge
325 * is not on the main system bus. PCI spec doesn't appear
326 * to allow for memory ranges not listed in the bridge's
327 * decode range to be decoded. The PC Card spec seems to
328 * indicate that this should only be done on x86 based
329 * machines, which seems to imply that on non-x86 machines
330 * the adddresses can be anywhere. This further implies that
331 * since the hardware can do it on non-x86 machines, it should
332 * be able to do it on x86 machines. Therefore, we can and
333 * should ignore this hint. Furthermore, the PC Card spec
334 * recommends always allocating memory above 1MB, contradicting
335 * the other part of the PC Card spec.
337 * NetBSD ignores this bit, but it also ignores the
338 * prefetchable bit too, so that's not an indication of
341 if (reg & TPL_BAR_REG_BELOW1MB)
342 dinfo->mbelow1mb |= (1 << PCI_RID2BAR(bar));
350 decode_tuple_unhandled(device_t cbdev, device_t child, int id,
351 int len, uint8_t *tupledata, uint32_t start, uint32_t *off,
352 struct tuple_callbacks *info, void *argp)
354 /* Make this message suck less XXX */
355 printf("TUPLE: %s [%d] is unhandled! Bailing...", info->name, len);
360 decode_tuple_end(device_t cbdev, device_t child, int id,
361 int len, uint8_t *tupledata, uint32_t start, uint32_t *off,
362 struct tuple_callbacks *info, void *argp)
364 if (cardbus_cis_debug)
365 printf("CIS reading done\n");
370 * Functions to read the a tuple from the card
374 cardbus_read_tuple_conf(device_t cbdev, device_t child, uint32_t start,
375 uint32_t *off, int *tupleid, int *len, uint8_t *tupledata)
383 e = pci_read_config(child, loc - loc % 4, 4);
384 for (j = loc % 4; j > 0; j--)
387 for (i = loc, j = -2; j < *len; j++, i++) {
389 e = pci_read_config(child, i, 4);
395 tupledata[j] = 0xff & e;
403 cardbus_read_tuple_mem(device_t cbdev, struct resource *res, uint32_t start,
404 uint32_t *off, int *tupleid, int *len, uint8_t *tupledata)
407 bus_space_handle_t bh;
410 bt = rman_get_bustag(res);
411 bh = rman_get_bushandle(res);
413 *tupleid = bus_space_read_1(bt, bh, start + *off);
414 *len = bus_space_read_1(bt, bh, start + *off + 1);
415 bus_space_read_region_1(bt, bh, *off + start + 2, tupledata, *len);
422 cardbus_read_tuple(device_t cbdev, device_t child, struct resource *res,
423 uint32_t start, uint32_t *off, int *tupleid, int *len,
426 if (res == CIS_CONFIG_SPACE)
427 return (cardbus_read_tuple_conf(cbdev, child, start, off,
428 tupleid, len, tupledata));
429 return (cardbus_read_tuple_mem(cbdev, res, start, off, tupleid, len,
434 cardbus_read_tuple_finish(device_t cbdev, device_t child, int rid,
435 struct resource *res)
437 if (res != CIS_CONFIG_SPACE) {
438 bus_release_resource(child, SYS_RES_MEMORY, rid, res);
439 if (rid == PCIM_CIS_ASI_ROM)
440 pci_write_config(child, rid, pci_read_config(child,
441 rid, 4) & ~PCIR_BIOS, 4);
445 static struct resource *
446 cardbus_read_tuple_init(device_t cbdev, device_t child, uint32_t *start,
449 struct resource *res;
452 space = *start & PCIM_CIS_ASI_MASK;
454 case PCIM_CIS_ASI_TUPLE:
455 if (cardbus_cis_debug)
456 device_printf(cbdev, "CIS in PCI config space\n");
457 /* CIS in PCI config space need no initialization */
458 return (CIS_CONFIG_SPACE);
459 case PCIM_CIS_ASI_BAR0:
460 case PCIM_CIS_ASI_BAR1:
461 case PCIM_CIS_ASI_BAR2:
462 case PCIM_CIS_ASI_BAR3:
463 case PCIM_CIS_ASI_BAR4:
464 case PCIM_CIS_ASI_BAR5:
465 *rid = PCIR_BAR(space - PCIM_CIS_ASI_BAR0);
466 if (cardbus_cis_debug)
467 device_printf(cbdev, "CIS in BAR %#x\n", *rid);
469 case PCIM_CIS_ASI_ROM:
471 if (cardbus_cis_debug)
472 device_printf(cbdev, "CIS in option rom\n");
475 device_printf(cbdev, "Unable to read CIS: Unknown space: %d\n",
480 /* allocate the memory space to read CIS */
481 res = bus_alloc_resource(child, SYS_RES_MEMORY, rid, 0, ~0, 1,
482 rman_make_alignment_flags(4096) | RF_ACTIVE);
484 device_printf(cbdev, "Unable to allocate resource "
488 if (*rid == PCIR_BIOS)
489 pci_write_config(child, *rid,
490 rman_get_start(res) | PCIM_BIOS_ENABLE, 4);
492 /* Flip to the right ROM image if CIS is in ROM */
493 if (space == PCIM_CIS_ASI_ROM) {
495 bus_space_handle_t bh;
497 uint32_t imagebase = 0;
503 bt = rman_get_bustag(res);
504 bh = rman_get_bushandle(res);
506 imagenum = (*start & PCIM_CIS_ROM_MASK) >> 28;
507 for (romnum = 0;; romnum++) {
508 romsig = bus_space_read_2(bt, bh,
509 imagebase + CARDBUS_EXROM_SIGNATURE);
510 if (romsig != 0xaa55) {
511 device_printf(cbdev, "Bad header in rom %d: "
512 "[%x] %04x\n", romnum, imagebase +
513 CARDBUS_EXROM_SIGNATURE, romsig);
514 bus_release_resource(cbdev, SYS_RES_MEMORY,
521 * If this was the Option ROM image that we were
522 * looking for, then we are done.
524 if (romnum == imagenum)
527 /* Find out where the next Option ROM image is */
528 pcidata = imagebase + bus_space_read_2(bt, bh,
529 imagebase + CARDBUS_EXROM_DATA_PTR);
530 imagesize = bus_space_read_2(bt, bh,
531 pcidata + CARDBUS_EXROM_DATA_IMAGE_LENGTH);
533 if (imagesize == 0) {
535 * XXX some ROMs seem to have this as zero,
536 * can we assume this means 1 block?
538 device_printf(cbdev, "Warning, size of Option "
539 "ROM image %d is 0 bytes, assuming 512 "
544 /* Image size is in 512 byte units */
547 if ((bus_space_read_1(bt, bh, pcidata +
548 CARDBUS_EXROM_DATA_INDICATOR) & 0x80) != 0) {
549 device_printf(cbdev, "Cannot find CIS in "
551 bus_release_resource(cbdev, SYS_RES_MEMORY,
556 imagebase += imagesize;
558 *start = imagebase + (*start & PCIM_CIS_ADDR_MASK);
560 *start = *start & PCIM_CIS_ADDR_MASK;
567 * Dispatch the right handler function per tuple
571 decode_tuple(device_t cbdev, device_t child, int tupleid, int len,
572 uint8_t *tupledata, uint32_t start, uint32_t *off,
573 struct tuple_callbacks *callbacks, void *argp)
576 for (i = 0; callbacks[i].id != CISTPL_GENERIC; i++) {
577 if (tupleid == callbacks[i].id)
578 return (callbacks[i].func(cbdev, child, tupleid, len,
579 tupledata, start, off, &callbacks[i], argp));
581 return (callbacks[i].func(cbdev, child, tupleid, len,
582 tupledata, start, off, NULL, argp));
586 cardbus_parse_cis(device_t cbdev, device_t child,
587 struct tuple_callbacks *callbacks, void *argp)
589 uint8_t tupledata[MAXTUPLESIZE];
592 int expect_linktarget;
594 struct resource *res;
597 bzero(tupledata, MAXTUPLESIZE);
598 expect_linktarget = TRUE;
599 if ((start = pci_read_config(child, PCIR_CIS, 4)) == 0) {
600 device_printf(cbdev, "CIS pointer is 0!\n");
603 if (cardbus_cis_debug)
604 device_printf(cbdev, "CIS pointer is %#x\n", start);
606 res = cardbus_read_tuple_init(cbdev, child, &start, &rid);
608 device_printf(cbdev, "Unable to allocate resources for CIS\n");
613 if (cardbus_read_tuple(cbdev, child, res, start, &off,
614 &tupleid, &len, tupledata) != 0) {
615 device_printf(cbdev, "Failed to read CIS.\n");
616 cardbus_read_tuple_finish(cbdev, child, rid, res);
620 if (expect_linktarget && tupleid != CISTPL_LINKTARGET) {
621 device_printf(cbdev, "Expecting link target, got 0x%x\n",
623 cardbus_read_tuple_finish(cbdev, child, rid, res);
626 expect_linktarget = decode_tuple(cbdev, child, tupleid, len,
627 tupledata, start, &off, callbacks, argp);
628 if (expect_linktarget != 0) {
629 device_printf(cbdev, "Parsing failed with %d\n",
631 cardbus_read_tuple_finish(cbdev, child, rid, res);
632 return (expect_linktarget);
634 } while (tupleid != CISTPL_END);
635 cardbus_read_tuple_finish(cbdev, child, rid, res);
640 cardbus_do_cis(device_t cbdev, device_t child)
643 struct tuple_callbacks init_callbacks[] = {
644 MAKETUPLE(LONGLINK_CB, unhandled),
645 MAKETUPLE(INDIRECT, unhandled),
646 MAKETUPLE(LONGLINK_MFC, unhandled),
648 MAKETUPLE(LONGLINK_A, unhandled),
649 MAKETUPLE(LONGLINK_C, unhandled),
650 MAKETUPLE(LINKTARGET, linktarget),
651 MAKETUPLE(VERS_1, vers_1),
652 MAKETUPLE(MANFID, manfid),
653 MAKETUPLE(FUNCID, funcid),
654 MAKETUPLE(FUNCE, funce),
656 MAKETUPLE(GENERIC, generic),
659 ret = cardbus_parse_cis(cbdev, child, init_callbacks, NULL);