xref: /dragonfly/stand/boot/common/isapnp.c (revision f9993810)
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: src/sys/boot/common/isapnp.c,v 1.7 2003/08/25 23:30:41 obrien Exp $
28  */
29 
30 /*
31  * Machine-independent 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 
42 static void	isapnp_write(int d, int r);
43 static void	isapnp_send_Initiation_LFSR(void);
44 static int	isapnp_get_serial(u_int8_t *p);
45 static int	isapnp_isolation_protocol(void);
46 static void	isapnp_enumerate(void);
47 
48 /* PnP read data port */
49 int		isapnp_readport = 0;
50 
51 #define _PNP_ID_LEN	9
52 
53 struct pnphandler isapnphandler =
54 {
55     "ISA bus",
56     isapnp_enumerate
57 };
58 
59 static void
60 isapnp_write(int d, int r)
61 {
62     outb (_PNP_ADDRESS, d);
63     outb (_PNP_WRITE_DATA, r);
64 }
65 
66 /*
67  * Send Initiation LFSR as described in "Plug and Play ISA Specification",
68  * Intel May 94.
69  */
70 static void
71 isapnp_send_Initiation_LFSR(void)
72 {
73     int cur, i;
74 
75     /* Reset the LSFR */
76     outb(_PNP_ADDRESS, 0);
77     outb(_PNP_ADDRESS, 0); /* yes, we do need it twice! */
78 
79     cur = 0x6a;
80     outb(_PNP_ADDRESS, cur);
81 
82     for (i = 1; i < 32; i++) {
83 	cur = (cur >> 1) | (((cur ^ (cur >> 1)) << 7) & 0xff);
84 	outb(_PNP_ADDRESS, cur);
85     }
86 }
87 
88 /*
89  * Get the device's serial number.  Returns 1 if the serial is valid.
90  */
91 static int
92 isapnp_get_serial(u_int8_t *data)
93 {
94     int		i, bit, valid = 0, sum = 0x6a;
95 
96     bzero(data, _PNP_ID_LEN);
97     outb(_PNP_ADDRESS, SERIAL_ISOLATION);
98     for (i = 0; i < 72; i++) {
99 	bit = inb(isapnp_readport) == 0x55;
100 	delay(250);	/* Delay 250 usec */
101 
102 	/* Can't Short Circuit the next evaluation, so 'and' is last */
103 	bit = (inb(isapnp_readport) == 0xaa) && bit;
104 	delay(250);	/* Delay 250 usec */
105 
106 	valid = valid || bit;
107 
108 	if (i < 64)
109 	    sum = (sum >> 1) |
110 		(((sum ^ (sum >> 1) ^ bit) << 7) & 0xff);
111 
112 	data[i / 8] = (data[i / 8] >> 1) | (bit ? 0x80 : 0);
113     }
114 
115     valid = valid && (data[8] == sum);
116 
117     return valid;
118 }
119 
120 /*
121  * Fills the buffer with resource info from the device.
122  * Returns nonzero if the device fails to report
123  */
124 static int
125 isapnp_get_resource_info(u_int8_t *buffer, int len)
126 {
127     int		i, j;
128     u_char	temp;
129 
130     for (i = 0; i < len; i++) {
131         outb(_PNP_ADDRESS, STATUS);
132         for (j = 0; j < 100; j++) {
133             if ((inb(isapnp_readport)) & 0x1)
134                 break;
135             delay(1);
136         }
137         if (j == 100) {
138             printf("PnP device failed to report resource data\n");
139             return(1);
140         }
141         outb(_PNP_ADDRESS, RESOURCE_DATA);
142         temp = inb(isapnp_readport);
143         if (buffer != NULL)
144             buffer[i] = temp;
145     }
146     return(0);
147 }
148 
149 /*
150  * Scan Resource Data for useful information.
151  *
152  * We scan the resource data for compatible device IDs and
153  * identifier strings; we only take the first identifier string
154  * and assume it's for the card as a whole.
155  *
156  * Returns 0 if the scan completed OK, nonzero on error.
157  */
158 static int
159 isapnp_scan_resdata(struct pnpinfo *pi)
160 {
161     u_char	tag, resinfo[8];
162     u_int	limit;
163     size_t	large_len;
164     u_char	*str;
165 
166     limit = 1000;
167     while ((limit-- > 0) && !isapnp_get_resource_info(&tag, 1)) {
168         if (PNP_RES_TYPE(tag) == 0) {
169             /* Small resource */
170             switch (PNP_SRES_NUM(tag)) {
171 
172                 case COMP_DEVICE_ID:
173                     /* Got a compatible device id resource */
174                     if (isapnp_get_resource_info(resinfo, PNP_SRES_LEN(tag)))
175 			return(1);
176 		    pnp_addident(pi, pnp_eisaformat(resinfo));
177 
178                 case END_TAG:
179 		    return(0);
180                     break;
181 
182                 default:
183                     /* Skip this resource */
184                     if (isapnp_get_resource_info(NULL, PNP_SRES_LEN(tag)))
185 			return(1);
186                     break;
187             }
188         } else {
189 	    /* Large resource */
190 	    if (isapnp_get_resource_info(resinfo, 2))
191 		return(1);
192 
193 	    large_len = resinfo[1];
194 	    large_len = (large_len << 8) + resinfo[0];
195 
196 	    switch(PNP_LRES_NUM(tag)) {
197 
198 	    case ID_STRING_ANSI:
199 		str = malloc(large_len + 1);
200 		if (isapnp_get_resource_info(str, (ssize_t)large_len)) {
201 		    free(str);
202 		    return(1);
203 		}
204 		str[large_len] = 0;
205 		if (pi->pi_desc == NULL) {
206 		    pi->pi_desc = (char *)str;
207 		} else {
208 		    free(str);
209 		}
210 		break;
211 
212 	    default:
213 		/* Large resource, skip it */
214 		if (isapnp_get_resource_info(NULL, (ssize_t)large_len))
215 		    return(1);
216 	    }
217 	}
218     }
219     return(1);
220 }
221 
222 /*
223  * Run the isolation protocol. Upon exiting, all cards are aware that
224  * they should use isapnp_readport as the READ_DATA port.
225  */
226 static int
227 isapnp_isolation_protocol(void)
228 {
229     int			csn;
230     struct pnpinfo	*pi;
231     u_int8_t		cardid[_PNP_ID_LEN];
232     int			ndevs;
233 
234     isapnp_send_Initiation_LFSR();
235     ndevs = 0;
236 
237     isapnp_write(CONFIG_CONTROL, 0x04);	/* Reset CSN for All Cards */
238 
239     for (csn = 1; ; csn++) {
240 	/* Wake up cards without a CSN (ie. all of them) */
241 	isapnp_write(WAKE, 0);
242 	isapnp_write(SET_RD_DATA, (isapnp_readport >> 2));
243 	outb(_PNP_ADDRESS, SERIAL_ISOLATION);
244 	delay(1000);	/* Delay 1 msec */
245 
246 	if (isapnp_get_serial(cardid)) {
247 	    isapnp_write(SET_CSN, csn);
248 	    pi = pnp_allocinfo();
249 	    ndevs++;
250 	    pnp_addident(pi, pnp_eisaformat(cardid));
251 	    /* scan the card obtaining all the identifiers it holds */
252 	    if (isapnp_scan_resdata(pi)) {
253 		pnp_freeinfo(pi);	/* error getting data, ignore */
254 	    } else {
255 		pnp_addinfo(pi);
256 	    }
257 	} else {
258 	    break;
259 	}
260     }
261     /* Move all cards to wait-for-key state */
262     while (--csn > 0) {
263 	isapnp_send_Initiation_LFSR();
264 	isapnp_write(WAKE, csn);
265 	isapnp_write(CONFIG_CONTROL, 0x02);
266 	delay(1000); /* XXX is it really necessary ? */
267 	csn--;
268     }
269     return(ndevs);
270 }
271 
272 /*
273  * Locate ISA-PnP devices and populate the supplied list.
274  */
275 static void
276 isapnp_enumerate(void)
277 {
278     int		pnp_rd_port;
279 
280     /* Check for I/O port access */
281     if ((archsw.arch_isainb == NULL) || (archsw.arch_isaoutb == NULL))
282 	return;
283 
284     /*
285      * Validate a possibly-suggested read port value.  If the autoscan failed
286      * last time, this will return us to autoscan mode again.
287      */
288     if ((isapnp_readport > 0) &&
289 	(((isapnp_readport < 0x203) ||
290 	  (isapnp_readport > 0x3ff) ||
291 	  (isapnp_readport & 0x3) != 0x3)))
292 	 /* invalid, go look for ourselves */
293 	isapnp_readport = 0;
294 
295     if (isapnp_readport < 0) {
296 	/* someone is telling us there is no ISA in the system */
297 	return;
298 
299     } else if (isapnp_readport > 0) {
300 	/* Someone has told us where the port is/should be, or we found one last time */
301 	isapnp_isolation_protocol();
302 
303     } else {
304 	/* No clues, look for it ourselves */
305 	for (pnp_rd_port = 0x80; pnp_rd_port < 0xff; pnp_rd_port += 0x10) {
306 	    /* Look for something, quit when we find it */
307 	    isapnp_readport = (pnp_rd_port << 2) | 0x3;
308 	    if (isapnp_isolation_protocol() > 0)
309 		break;
310 	}
311     }
312 }
313