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