summaryrefslogtreecommitdiff
path: root/sys/dev/cardbus/cardbus_exrom.c
blob: 08ec66f992c1b8c517056ff609ff24758dc1f3af (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/*	$OpenBSD: cardbus_exrom.c,v 1.2 2005/09/12 18:52:49 fgsch Exp $	*/
/*	$NetBSD: cardbus_exrom.c,v 1.4 2000/02/03 06:47:31 thorpej Exp $	*/

/*
 * Copyright (c) 1999 The NetBSD Foundation, Inc.
 * All rights reserved.
 *
 * This code is derived from software contributed to
 * The NetBSD Foundation by Johan Danielsson.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * 3. Neither the name of The NetBSD Foundation nor the names of its
 *    contributors may be used to endorse or promote products derived
 *    from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/queue.h>
#include <sys/malloc.h>

#include <machine/bus.h>

#include <dev/cardbus/cardbus_exrom.h>

#define READ_INT16(T, H, O)  \
(bus_space_read_1((T), (H), (O)) | (bus_space_read_1((T), (H), (O) + 1) << 8))

/*  A PCI ROM is divided into a number of images. Each image has two
 *  data structures, a header located at the start of the image, and a
 *  `data structure' at some offset into it.
 *
 *  The header is a 26 byte structure:
 *
 *  Offset	Length	Description
 *  0x00	   1	signature byte 1 (0x55)
 *  0x01	   1	signature byte 2 (0xAA)
 *  0x02	  22	processor architecture data
 *  0x18	   2	pointer to the data structure
 *
 *  The data structure is a 24 byte structure:
 *
 *  Offset	Length	Description
 *  0x00	   4	signature (PCIR)
 *  0x04	   2	vendor id
 *  0x06	   2	device id
 *  0x08	   2	reserved
 *  0x0A	   2	data structure length
 *  0x0C	   1	data structure revision (0)
 *  0x0D	   3	class code
 *  0x10	   2	image length (in 512 byte blocks)
 *  0x12	   2	code revision level
 *  0x14	   1	code type
 *  0x15	   1	indicator (bit 7 indicates final image)
 *  0x16	   2	reserved
 *
 */

/*
 *  Scan through a PCI expansion ROM, and create subregions for each
 *  ROM image. This function assumes that the ROM is mapped at
 *  (tag,handle), and that the expansion ROM address decoder is
 *  enabled. The PCI specification requires that no other BAR should
 *  be accessed while the ROM is enabled, so interrupts should be
 *  disabled.
 */

int
cardbus_read_exrom(bus_space_tag_t romt, bus_space_handle_t romh,
    struct cardbus_rom_image_head *head)
{
	size_t addr = 0; /* offset of current rom image */
	size_t dataptr;
	unsigned int rom_image = 0;

	SIMPLEQ_INIT(head);
	do {
		size_t image_size;
		struct cardbus_rom_image *image;
		u_int16_t val;

		val = READ_INT16(romt, romh, addr + CARDBUS_EXROM_SIGNATURE);
		if (val != 0xaa55) {
			printf("%s: bad header signature in ROM image %u: "
			    "0x%04x\n", __func__, rom_image, val);
			return (1);
		}
		dataptr = addr + READ_INT16(romt, romh,
		    addr + CARDBUS_EXROM_DATA_PTR);
		/* Get the ROM image size, in blocks */
		image_size = READ_INT16(romt, romh,
		    dataptr + CARDBUS_EXROM_DATA_IMAGE_LENGTH);
		if (image_size == 0)
			/* XXX some ROMs seem to have this as zero, can we
			   assume this means 1 block? */
			image_size = 1;
		image_size <<= 9;
		image = malloc(sizeof(*image), M_DEVBUF, M_NOWAIT);
		if (image == NULL) {
			printf("%s: out of memory\n", __func__);
			return (1);
		}
		image->rom_image = rom_image;
		image->image_size = image_size;
		image->romt = romt;
		if (bus_space_subregion(romt, romh, addr,
		    image_size, &image->romh)) {
			printf("%s: bus_space_subregion failed", __func__);
			free(image, M_DEVBUF);
			return (1);
		}
		SIMPLEQ_INSERT_TAIL(head, image, next);
		addr += image_size;
		rom_image++;
	} while ((bus_space_read_1(romt, romh,
	    dataptr + CARDBUS_EXROM_DATA_INDICATOR) & 0x80) == 0);
	return (0);
}

#if 0
struct cardbus_exrom_data_structure {
	char		signature[4];
	cardbusreg_t	id;	/* vendor & device id */
	u_int16_t	structure_length;
	u_int8_t	structure_revision;
	cardbusreg_t	class; /* class code in upper 24 bits */
	u_int16_t	image_length;
	u_int16_t	data_revision;
	u_int8_t	code_type;
	u_int8_t	indicator;
};

pci_exrom_parse_data_structure(bus_space_tag_t tag,
    bus_space_handle_t handle, struct pci_exrom_data_structure *ds)
{
	unsigned char hdr[16];
	bus_space_read_region_1(tag, handle, dataptr, hdr, sizeof(hdr));
	memcpy(header->signature, hdr + PCI_EXROM_DATA_SIGNATURE, 4);
#define LEINT16(B, O) ((B)[(O)] | ((B)[(O) + 1] << 8))
	header->id = LEINT16(hdr, PCI_EXROM_DATA_VENDOR_ID) |
	    (LEINT16(hdr, PCI_EXROM_DATA_DEVICE_ID) << 16);
	header->structure_length = LEINT16(hdr, PCI_EXROM_DATA_LENGTH);
	header->structure_rev = hdr[PCI_EXROM_DATA_REV];
	header->class = (hdr[PCI_EXROM_DATA_CLASS_CODE] << 8) |
	    (hdr[PCI_EXROM_DATA_CLASS_CODE + 1] << 16) |
	    (hdr[PCI_EXROM_DATA_CLASS_CODE + 2] << 24);
	header->image_length = LEINT16(hdr, PCI_EXROM_DATA_IMAGE_LENGTH) << 16;
	header->data_revision = LEINT16(hdr, PCI_EXROM_DATA_DATA_REV);
	header->code_type = hdr[PCI_EXROM_DATA_CODE_TYPE];
	header->indicator = hdr[PCI_EXROM_DATA_INDICATOR];
	length = min(length, header->image_length - 0x18 - offset);
	bus_space_read_region_1(tag, handle, dataptr + 0x18 + offset,
	    buf, length);
	ret = length;
}
#endif