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