Deleted Added
sdiff udiff text old ( 50477 ) new ( 64187 )
full compact
1/*
2 * Copyright (c) 1998, Michael Smith
3 * Copyright (c) 1996, Sujal M. Patel
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 *
27 * $FreeBSD: head/sys/boot/common/isapnp.c 50477 1999-08-28 01:08:13Z peter $
28 */
29
30/*
31 * Machine-independant ISA PnP enumerator implementing a subset of the
32 * ISA PnP specification.
33 */
34#include <stand.h>
35#include <string.h>
36#include <bootstrap.h>
37#include <isapnp.h>
38
39#define inb(x) (archsw.arch_isainb((x)))
40#define outb(x,y) (archsw.arch_isaoutb((x),(y)))
41
42static void isapnp_write(int d, u_char r);
43static u_char isapnp_read(int d);
44static void isapnp_send_Initiation_LFSR();
45static int isapnp_get_serial(u_int8_t *p);
46static int isapnp_isolation_protocol(void);
47static void isapnp_enumerate(void);
48
49/* PnP read data port */
50int isapnp_readport = 0;
51
52#define _PNP_ID_LEN 9
53
54struct pnphandler isapnphandler =
55{
56 "ISA bus",
57 isapnp_enumerate
58};
59
60static void
61isapnp_write(int d, u_char r)
62{
63 outb (_PNP_ADDRESS, d);
64 outb (_PNP_WRITE_DATA, r);
65}
66
67static u_char
68isapnp_read(int d)
69{
70 outb (_PNP_ADDRESS, d);
71 return (inb(isapnp_readport));
72}
73
74/*
75 * Send Initiation LFSR as described in "Plug and Play ISA Specification",
76 * Intel May 94.
77 */
78static void
79isapnp_send_Initiation_LFSR()
80{
81 int cur, i;
82
83 /* Reset the LSFR */
84 outb(_PNP_ADDRESS, 0);
85 outb(_PNP_ADDRESS, 0); /* yes, we do need it twice! */
86
87 cur = 0x6a;
88 outb(_PNP_ADDRESS, cur);
89
90 for (i = 1; i < 32; i++) {
91 cur = (cur >> 1) | (((cur ^ (cur >> 1)) << 7) & 0xff);
92 outb(_PNP_ADDRESS, cur);
93 }
94}
95
96/*
97 * Get the device's serial number. Returns 1 if the serial is valid.
98 */
99static int
100isapnp_get_serial(u_int8_t *data)
101{
102 int i, bit, valid = 0, sum = 0x6a;
103
104 bzero(data, _PNP_ID_LEN);
105 outb(_PNP_ADDRESS, SERIAL_ISOLATION);
106 for (i = 0; i < 72; i++) {
107 bit = inb(isapnp_readport) == 0x55;
108 delay(250); /* Delay 250 usec */
109
110 /* Can't Short Circuit the next evaluation, so 'and' is last */
111 bit = (inb(isapnp_readport) == 0xaa) && bit;
112 delay(250); /* Delay 250 usec */
113
114 valid = valid || bit;
115
116 if (i < 64)
117 sum = (sum >> 1) |
118 (((sum ^ (sum >> 1) ^ bit) << 7) & 0xff);
119
120 data[i / 8] = (data[i / 8] >> 1) | (bit ? 0x80 : 0);
121 }
122
123 valid = valid && (data[8] == sum);
124
125 return valid;
126}
127
128/*
129 * Fills the buffer with resource info from the device.
130 * Returns nonzero if the device fails to report
131 */
132static int
133isapnp_get_resource_info(u_int8_t *buffer, int len)
134{
135 int i, j;
136 u_char temp;
137
138 for (i = 0; i < len; i++) {
139 outb(_PNP_ADDRESS, STATUS);
140 for (j = 0; j < 100; j++) {
141 if ((inb(isapnp_readport)) & 0x1)
142 break;
143 delay(1);
144 }
145 if (j == 100) {
146 printf("PnP device failed to report resource data\n");
147 return(1);
148 }
149 outb(_PNP_ADDRESS, RESOURCE_DATA);
150 temp = inb(isapnp_readport);
151 if (buffer != NULL)
152 buffer[i] = temp;
153 }
154 return(0);
155}
156
157/*
158 * Scan Resource Data for useful information.
159 *
160 * We scan the resource data for compatible device IDs and
161 * identifier strings; we only take the first identifier string
162 * and assume it's for the card as a whole.
163 *
164 * Returns 0 if the scan completed OK, nonzero on error.
165 */
166static int
167isapnp_scan_resdata(struct pnpinfo *pi)
168{
169 u_char tag, resinfo[8];
170 int large_len, limit;
171 char *str;
172
173 limit = 1000;
174 while ((limit-- > 0) && !isapnp_get_resource_info(&tag, 1)) {
175 if (PNP_RES_TYPE(tag) == 0) {
176 /* Small resource */
177 switch (PNP_SRES_NUM(tag)) {
178
179 case COMP_DEVICE_ID:
180 /* Got a compatible device id resource */
181 if (isapnp_get_resource_info(resinfo, PNP_SRES_LEN(tag)))
182 return(1);
183 pnp_addident(pi, pnp_eisaformat(resinfo));
184
185 case END_TAG:
186 return(0);
187 break;
188
189 default:
190 /* Skip this resource */
191 if (isapnp_get_resource_info(NULL, PNP_SRES_LEN(tag)))
192 return(1);
193 break;
194 }
195 } else {
196 /* Large resource */
197 if (isapnp_get_resource_info(resinfo, 2))
198 return(1);
199
200 large_len = resinfo[1];
201 large_len = (large_len << 8) + resinfo[0];
202
203 switch(PNP_LRES_NUM(tag)) {
204
205 case ID_STRING_ANSI:
206 str = malloc(large_len + 1);
207 if (isapnp_get_resource_info(str, large_len)) {
208 free(str);
209 return(1);
210 }
211 str[large_len] = 0;
212 if (pi->pi_desc == NULL) {
213 pi->pi_desc = str;
214 } else {
215 free(str);
216 }
217 break;
218
219 default:
220 /* Large resource, skip it */
221 if (isapnp_get_resource_info(NULL, large_len))
222 return(1);
223 }
224 }
225 }
226 return(1);
227}
228
229/*
230 * Run the isolation protocol. Upon exiting, all cards are aware that
231 * they should use isapnp_readport as the READ_DATA port.
232 */
233static int
234isapnp_isolation_protocol(void)
235{
236 int csn;
237 struct pnpinfo *pi;
238 u_int8_t cardid[_PNP_ID_LEN];
239 int ndevs;
240
241 isapnp_send_Initiation_LFSR();
242 ndevs = 0;
243
244 isapnp_write(CONFIG_CONTROL, 0x04); /* Reset CSN for All Cards */
245
246 for (csn = 1; ; csn++) {
247 /* Wake up cards without a CSN (ie. all of them) */
248 isapnp_write(WAKE, 0);
249 isapnp_write(SET_RD_DATA, (isapnp_readport >> 2));
250 outb(_PNP_ADDRESS, SERIAL_ISOLATION);
251 delay(1000); /* Delay 1 msec */
252
253 if (isapnp_get_serial(cardid)) {
254 isapnp_write(SET_CSN, csn);
255 pi = pnp_allocinfo();
256 ndevs++;
257 pnp_addident(pi, pnp_eisaformat(cardid));
258 /* scan the card obtaining all the identifiers it holds */
259 if (isapnp_scan_resdata(pi)) {
260 pnp_freeinfo(pi); /* error getting data, ignore */
261 } else {
262 pnp_addinfo(pi);
263 }
264 } else {
265 break;
266 }
267 }
268 /* Move all cards to wait-for-key state */
269 while (--csn > 0) {
270 isapnp_send_Initiation_LFSR();
271 isapnp_write(WAKE, csn);
272 isapnp_write(CONFIG_CONTROL, 0x02);
273 delay(1000); /* XXX is it really necessary ? */
274 csn--;
275 }
276 return(ndevs);
277}
278
279/*
280 * Locate ISA-PnP devices and populate the supplied list.
281 */
282static void
283isapnp_enumerate(void)
284{
285 int pnp_rd_port;
286
287 /* Check for I/O port access */
288 if ((archsw.arch_isainb == NULL) || (archsw.arch_isaoutb == NULL))
289 return;
290
291 /*
292 * Validate a possibly-suggested read port value. If the autoscan failed
293 * last time, this will return us to autoscan mode again.
294 */
295 if ((isapnp_readport > 0) &&
296 (((isapnp_readport < 0x203) ||
297 (isapnp_readport > 0x3ff) ||
298 (isapnp_readport & 0x3) != 0x3)))
299 /* invalid, go look for ourselves */
300 isapnp_readport = 0;
301
302 if (isapnp_readport < 0) {
303 /* someone is telling us there is no ISA in the system */
304 return;
305
306 } else if (isapnp_readport > 0) {
307 /* Someone has told us where the port is/should be, or we found one last time */
308 isapnp_isolation_protocol();
309
310 } else {
311 /* No clues, look for it ourselves */
312 for (pnp_rd_port = 0x80; pnp_rd_port < 0xff; pnp_rd_port += 0x10) {
313 /* Look for something, quit when we find it */
314 isapnp_readport = (pnp_rd_port << 2) | 0x3;
315 if (isapnp_isolation_protocol() > 0)
316 break;
317 }
318 }
319}