1 /* $NetBSD: cardbus_exrom.c,v 1.10 2007/10/19 11:59:38 ad Exp $ */
4 * Copyright (c) 1999 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to
8 * The NetBSD Foundation by Johan Danielsson.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
22 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
23 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
24 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
25 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
26 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGE.
35 #include <sys/cdefs.h>
36 __KERNEL_RCSID(0, "$NetBSD: cardbus_exrom.c,v 1.10 2007/10/19 11:59:38 ad Exp $");
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/queue.h>
41 #include <sys/malloc.h>
45 #include <dev/cardbus/cardbus_exrom.h>
47 #define READ_INT16(T, H, O) \
48 (bus_space_read_1((T), (H), (O)) | \
49 (bus_space_read_1((T), (H), (O) + 1) << 8))
51 /* A PCI ROM is divided into a number of images. Each image has two
52 * data structures, a header located at the start of the image, and a
53 * `data structure' at some offset into it.
55 * The header is a 26 byte structure:
57 * Offset Length Description
58 * 0x00 1 signature byte 1 (0x55)
59 * 0x01 1 signature byte 2 (0xAA)
60 * 0x02 22 processor architecture data
61 * 0x18 2 pointer to the data structure
63 * The data structure is a 24 byte structure:
65 * Offset Length Description
66 * 0x00 4 signature (PCIR)
70 * 0x0A 2 data structure length
71 * 0x0C 1 data structure revision (0)
73 * 0x10 2 image length (in 512 byte blocks)
74 * 0x12 2 code revision level
76 * 0x15 1 indicator (bit 7 indicates final image)
82 * Scan through a PCI expansion ROM, and create subregions for each
83 * ROM image. This function assumes that the ROM is mapped at
84 * (tag,handle), and that the expansion ROM address decoder is
85 * enabled. The PCI specification requires that no other BAR should
86 * be accessed while the ROM is enabled, so interrupts should be
91 cardbus_read_exrom(bus_space_tag_t romt
, bus_space_handle_t romh
,
92 struct cardbus_rom_image_head
*head
)
94 static const char thisfunc
[] = "cardbus_read_exrom";
95 size_t addr
= 0; /* offset of current rom image */
97 unsigned int rom_image
= 0;
102 struct cardbus_rom_image
*image
;
105 val
= READ_INT16(romt
, romh
, addr
+ CARDBUS_EXROM_SIGNATURE
);
108 "bad header signature in ROM image %u: 0x%04x\n",
109 thisfunc
, rom_image
, val
);
113 READ_INT16(romt
, romh
, addr
+ CARDBUS_EXROM_DATA_PTR
);
114 /* get the ROM image size, in blocks */
115 image_size
= READ_INT16(romt
, romh
,
116 dataptr
+ CARDBUS_EXROM_DATA_IMAGE_LENGTH
);
119 * XXX some ROMs seem to have this as zero,
120 * can we assume this means 1 block?
124 image
= malloc(sizeof(*image
), M_DEVBUF
, M_NOWAIT
);
126 printf("%s: out of memory\n", thisfunc
);
129 image
->rom_image
= rom_image
;
130 image
->image_size
= image_size
;
132 if (bus_space_subregion(romt
, romh
, addr
,
133 image_size
, &image
->romh
)) {
134 printf("%s: bus_space_subregion failed", thisfunc
);
135 free(image
, M_DEVBUF
);
138 SIMPLEQ_INSERT_TAIL(head
, image
, next
);
141 } while ((bus_space_read_1(romt
, romh
,
142 dataptr
+ CARDBUS_EXROM_DATA_INDICATOR
) & 0x80) == 0);
148 struct cardbus_exrom_data_structure
{
150 cardbusreg_t id
; /* vendor & device id */
151 u_int16_t structure_length
;
152 u_int8_t structure_revision
;
153 cardbusreg_t
class; /* class code in upper 24 bits */
154 u_int16_t image_length
;
155 u_int16_t data_revision
;
161 pci_exrom_parse_data_structure(bus_space_tag_t tag
,
162 bus_space_handle_t handle
,
163 struct pci_exrom_data_structure
*ds
)
165 unsigned char hdr
[16];
167 bus_space_read_region_1(tag
, handle
, dataptr
, hdr
, sizeof(hdr
));
168 memcpy(header
->signature
, hdr
+ PCI_EXROM_DATA_SIGNATURE
, 4);
169 #define LEINT16(B, O) ((B)[(O)] | ((B)[(O) + 1] << 8))
170 header
->id
= LEINT16(hdr
, PCI_EXROM_DATA_VENDOR_ID
) |
171 (LEINT16(hdr
, PCI_EXROM_DATA_DEVICE_ID
) << 16);
172 header
->structure_length
= LEINT16(hdr
, PCI_EXROM_DATA_LENGTH
);
173 header
->structure_rev
= hdr
[PCI_EXROM_DATA_REV
];
174 header
->class = (hdr
[PCI_EXROM_DATA_CLASS_CODE
] << 8) |
175 (hdr
[PCI_EXROM_DATA_CLASS_CODE
+ 1] << 16) |
176 (hdr
[PCI_EXROM_DATA_CLASS_CODE
+ 2] << 24);
177 header
->image_length
= LEINT16(hdr
, PCI_EXROM_DATA_IMAGE_LENGTH
) << 16;
178 header
->data_revision
= LEINT16(hdr
, PCI_EXROM_DATA_DATA_REV
);
179 header
->code_type
= hdr
[PCI_EXROM_DATA_CODE_TYPE
];
180 header
->indicator
= hdr
[PCI_EXROM_DATA_INDICATOR
];
181 length
= min(length
, header
->image_length
- 0x18 - offset
);
182 bus_space_read_region_1(tag
, handle
, dataptr
+ 0x18 + offset
,