isapnp.c revision 1.27 1 1.27 christos /* $NetBSD: isapnp.c,v 1.27 1998/09/05 14:15:25 christos Exp $ */
2 1.1 christos
3 1.27 christos /*-
4 1.27 christos * Copyright (c) 1996 The NetBSD Foundation, Inc.
5 1.27 christos * All rights reserved.
6 1.27 christos *
7 1.27 christos * This code is derived from software contributed to The NetBSD Foundation
8 1.27 christos * by Christos Zoulas.
9 1.1 christos *
10 1.1 christos * Redistribution and use in source and binary forms, with or without
11 1.1 christos * modification, are permitted provided that the following conditions
12 1.1 christos * are met:
13 1.1 christos * 1. Redistributions of source code must retain the above copyright
14 1.1 christos * notice, this list of conditions and the following disclaimer.
15 1.1 christos * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 christos * notice, this list of conditions and the following disclaimer in the
17 1.1 christos * documentation and/or other materials provided with the distribution.
18 1.1 christos * 3. All advertising materials mentioning features or use of this software
19 1.1 christos * must display the following acknowledgement:
20 1.27 christos * This product includes software developed by the NetBSD
21 1.27 christos * Foundation, Inc. and its contributors.
22 1.27 christos * 4. Neither the name of The NetBSD Foundation nor the names of its
23 1.27 christos * contributors may be used to endorse or promote products derived
24 1.27 christos * from this software without specific prior written permission.
25 1.1 christos *
26 1.27 christos * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 1.27 christos * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 1.27 christos * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 1.27 christos * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 1.27 christos * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 1.27 christos * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 1.27 christos * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 1.27 christos * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 1.27 christos * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 1.27 christos * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 1.27 christos * POSSIBILITY OF SUCH DAMAGE.
37 1.1 christos */
38 1.1 christos
39 1.1 christos /*
40 1.1 christos * ISA PnP bus autoconfiguration.
41 1.1 christos */
42 1.1 christos
43 1.1 christos #include <sys/param.h>
44 1.1 christos #include <sys/systm.h>
45 1.1 christos #include <sys/device.h>
46 1.1 christos #include <sys/malloc.h>
47 1.1 christos
48 1.1 christos #include <machine/bus.h>
49 1.1 christos
50 1.1 christos #include <dev/isa/isavar.h>
51 1.10 thorpej #include <dev/isa/isadmavar.h>
52 1.1 christos
53 1.1 christos #include <dev/isapnp/isapnpreg.h>
54 1.1 christos #include <dev/isapnp/isapnpvar.h>
55 1.20 christos #include <dev/isapnp/isapnpdevs.h>
56 1.1 christos
57 1.1 christos static void isapnp_init __P((struct isapnp_softc *));
58 1.1 christos static __inline u_char isapnp_shift_bit __P((struct isapnp_softc *));
59 1.1 christos static int isapnp_findcard __P((struct isapnp_softc *));
60 1.1 christos static void isapnp_free_region __P((bus_space_tag_t, struct isapnp_region *));
61 1.1 christos static int isapnp_alloc_region __P((bus_space_tag_t, struct isapnp_region *));
62 1.11 thorpej static int isapnp_alloc_irq __P((isa_chipset_tag_t, struct isapnp_pin *));
63 1.18 thorpej static int isapnp_alloc_drq __P((isa_chipset_tag_t, struct isapnp_pin *));
64 1.1 christos static int isapnp_testconfig __P((bus_space_tag_t, bus_space_tag_t,
65 1.1 christos struct isapnp_attach_args *, int));
66 1.18 thorpej static struct isapnp_attach_args *isapnp_bestconfig __P((struct isapnp_softc *,
67 1.18 thorpej struct isapnp_attach_args **));
68 1.1 christos static void isapnp_print_region __P((const char *, struct isapnp_region *,
69 1.1 christos size_t));
70 1.1 christos static void isapnp_configure __P((struct isapnp_softc *,
71 1.1 christos const struct isapnp_attach_args *));
72 1.1 christos static void isapnp_print_pin __P((const char *, struct isapnp_pin *, size_t));
73 1.1 christos static int isapnp_print __P((void *, const char *));
74 1.7 mikel #ifdef _KERNEL
75 1.13 drochner static int isapnp_submatch __P((struct device *, struct cfdata *, void *));
76 1.13 drochner #endif
77 1.9 mikel static int isapnp_find __P((struct isapnp_softc *, int));
78 1.9 mikel static int isapnp_match __P((struct device *, struct cfdata *, void *));
79 1.1 christos static void isapnp_attach __P((struct device *, struct device *, void *));
80 1.22 thorpej static void isapnp_callback __P((struct device *));
81 1.1 christos
82 1.1 christos struct cfattach isapnp_ca = {
83 1.1 christos sizeof(struct isapnp_softc), isapnp_match, isapnp_attach
84 1.1 christos };
85 1.1 christos
86 1.23 thorpej /*
87 1.23 thorpej * This keeps track if which ISA's we have been probed on.
88 1.23 thorpej */
89 1.23 thorpej struct isapnp_probe_cookie {
90 1.23 thorpej LIST_ENTRY(isapnp_probe_cookie) ipc_link;
91 1.23 thorpej struct device *ipc_parent;
92 1.23 thorpej };
93 1.23 thorpej LIST_HEAD(, isapnp_probe_cookie) isapnp_probes =
94 1.23 thorpej LIST_HEAD_INITIALIZER(isapnp_probes);
95 1.23 thorpej
96 1.1 christos /* isapnp_init():
97 1.1 christos * Write the PNP initiation key to wake up the cards...
98 1.1 christos */
99 1.1 christos static void
100 1.1 christos isapnp_init(sc)
101 1.1 christos struct isapnp_softc *sc;
102 1.1 christos {
103 1.1 christos int i;
104 1.1 christos u_char v = ISAPNP_LFSR_INIT;
105 1.1 christos
106 1.1 christos /* First write 0's twice to enter the Wait for Key state */
107 1.1 christos ISAPNP_WRITE_ADDR(sc, 0);
108 1.1 christos ISAPNP_WRITE_ADDR(sc, 0);
109 1.1 christos
110 1.1 christos /* Send the 32 byte sequence to awake the logic */
111 1.9 mikel for (i = 0; i < ISAPNP_LFSR_LENGTH; i++) {
112 1.1 christos ISAPNP_WRITE_ADDR(sc, v);
113 1.1 christos v = ISAPNP_LFSR_NEXT(v);
114 1.1 christos }
115 1.1 christos }
116 1.1 christos
117 1.1 christos
118 1.1 christos /* isapnp_shift_bit():
119 1.1 christos * Read a bit at a time from the config card.
120 1.1 christos */
121 1.1 christos static __inline u_char
122 1.1 christos isapnp_shift_bit(sc)
123 1.1 christos struct isapnp_softc *sc;
124 1.1 christos {
125 1.1 christos u_char c1, c2;
126 1.1 christos
127 1.1 christos DELAY(250);
128 1.1 christos c1 = ISAPNP_READ_DATA(sc);
129 1.1 christos DELAY(250);
130 1.1 christos c2 = ISAPNP_READ_DATA(sc);
131 1.1 christos
132 1.1 christos if (c1 == 0x55 && c2 == 0xAA)
133 1.1 christos return 0x80;
134 1.1 christos else
135 1.1 christos return 0;
136 1.1 christos }
137 1.1 christos
138 1.1 christos
139 1.1 christos /* isapnp_findcard():
140 1.1 christos * Attempt to read the vendor/serial/checksum for a card
141 1.1 christos * If a card is found [the checksum matches], assign the
142 1.1 christos * next card number to it and return 1
143 1.1 christos */
144 1.1 christos static int
145 1.1 christos isapnp_findcard(sc)
146 1.1 christos struct isapnp_softc *sc;
147 1.1 christos {
148 1.4 christos u_char v = ISAPNP_LFSR_INIT, csum, w;
149 1.1 christos int i, b;
150 1.1 christos
151 1.1 christos if (sc->sc_ncards == ISAPNP_MAX_CARDS) {
152 1.1 christos printf("%s: Too many pnp cards\n", sc->sc_dev.dv_xname);
153 1.1 christos return 0;
154 1.1 christos }
155 1.1 christos
156 1.1 christos /* Set the read port */
157 1.1 christos isapnp_write_reg(sc, ISAPNP_WAKE, 0);
158 1.1 christos isapnp_write_reg(sc, ISAPNP_SET_RD_PORT, sc->sc_read_port >> 2);
159 1.1 christos sc->sc_read_port |= 3;
160 1.1 christos DELAY(1000);
161 1.1 christos
162 1.1 christos ISAPNP_WRITE_ADDR(sc, ISAPNP_SERIAL_ISOLATION);
163 1.1 christos DELAY(1000);
164 1.1 christos
165 1.1 christos /* Read the 8 bytes of the Vendor ID and Serial Number */
166 1.1 christos for(i = 0; i < 8; i++) {
167 1.1 christos /* Read each bit separately */
168 1.1 christos for (w = 0, b = 0; b < 8; b++) {
169 1.1 christos u_char neg = isapnp_shift_bit(sc);
170 1.1 christos
171 1.1 christos w >>= 1;
172 1.1 christos w |= neg;
173 1.1 christos v = ISAPNP_LFSR_NEXT(v) ^ neg;
174 1.1 christos }
175 1.1 christos sc->sc_id[sc->sc_ncards][i] = w;
176 1.1 christos }
177 1.1 christos
178 1.1 christos /* Read the remaining checksum byte */
179 1.4 christos for (csum = 0, b = 0; b < 8; b++) {
180 1.1 christos u_char neg = isapnp_shift_bit(sc);
181 1.1 christos
182 1.1 christos csum >>= 1;
183 1.1 christos csum |= neg;
184 1.1 christos }
185 1.4 christos sc->sc_id[sc->sc_ncards][8] = csum;
186 1.1 christos
187 1.1 christos if (csum == v) {
188 1.1 christos sc->sc_ncards++;
189 1.1 christos isapnp_write_reg(sc, ISAPNP_CARD_SELECT_NUM, sc->sc_ncards);
190 1.1 christos return 1;
191 1.1 christos }
192 1.1 christos return 0;
193 1.1 christos }
194 1.1 christos
195 1.1 christos
196 1.1 christos /* isapnp_free_region():
197 1.1 christos * Free a region
198 1.1 christos */
199 1.1 christos static void
200 1.1 christos isapnp_free_region(t, r)
201 1.1 christos bus_space_tag_t t;
202 1.1 christos struct isapnp_region *r;
203 1.1 christos {
204 1.8 mikel #ifdef _KERNEL
205 1.1 christos bus_space_unmap(t, r->h, r->length);
206 1.8 mikel #endif
207 1.1 christos }
208 1.1 christos
209 1.1 christos
210 1.1 christos /* isapnp_alloc_region():
211 1.1 christos * Allocate a single region if possible
212 1.1 christos */
213 1.1 christos static int
214 1.1 christos isapnp_alloc_region(t, r)
215 1.1 christos bus_space_tag_t t;
216 1.1 christos struct isapnp_region *r;
217 1.1 christos {
218 1.1 christos int error = 0;
219 1.1 christos
220 1.8 mikel for (r->base = r->minbase; r->base <= r->maxbase;
221 1.8 mikel r->base += r->align) {
222 1.8 mikel #ifdef _KERNEL
223 1.1 christos error = bus_space_map(t, r->base, r->length, 0, &r->h);
224 1.8 mikel #endif
225 1.1 christos if (error == 0)
226 1.1 christos return 0;
227 1.1 christos }
228 1.1 christos return error;
229 1.1 christos }
230 1.1 christos
231 1.1 christos
232 1.10 thorpej /* isapnp_alloc_irq():
233 1.10 thorpej * Allocate an irq
234 1.1 christos */
235 1.1 christos static int
236 1.11 thorpej isapnp_alloc_irq(ic, i)
237 1.11 thorpej isa_chipset_tag_t ic;
238 1.1 christos struct isapnp_pin *i;
239 1.1 christos {
240 1.11 thorpej int irq;
241 1.12 christos #define LEVEL_IRQ (ISAPNP_IRQTYPE_LEVEL_PLUS|ISAPNP_IRQTYPE_LEVEL_MINUS)
242 1.12 christos i->type = (i->flags & LEVEL_IRQ) ? IST_LEVEL : IST_EDGE;
243 1.1 christos
244 1.10 thorpej if (i->bits == 0) {
245 1.1 christos i->num = 0;
246 1.10 thorpej return 0;
247 1.10 thorpej }
248 1.10 thorpej
249 1.12 christos if (isa_intr_alloc(ic, i->bits, i->type, &irq) == 0) {
250 1.11 thorpej i->num = irq;
251 1.11 thorpej return 0;
252 1.11 thorpej }
253 1.10 thorpej
254 1.10 thorpej return EINVAL;
255 1.1 christos }
256 1.1 christos
257 1.10 thorpej /* isapnp_alloc_drq():
258 1.10 thorpej * Allocate a drq
259 1.10 thorpej */
260 1.10 thorpej static int
261 1.18 thorpej isapnp_alloc_drq(ic, i)
262 1.18 thorpej isa_chipset_tag_t ic;
263 1.10 thorpej struct isapnp_pin *i;
264 1.10 thorpej {
265 1.10 thorpej int b;
266 1.10 thorpej
267 1.10 thorpej if (i->bits == 0) {
268 1.10 thorpej i->num = 0;
269 1.10 thorpej return 0;
270 1.10 thorpej }
271 1.10 thorpej
272 1.17 thorpej for (b = 0; b < 8; b++)
273 1.18 thorpej if ((i->bits & (1 << b)) && isa_drq_isfree(ic, b)) {
274 1.10 thorpej i->num = b;
275 1.10 thorpej return 0;
276 1.10 thorpej }
277 1.10 thorpej
278 1.10 thorpej return EINVAL;
279 1.10 thorpej }
280 1.1 christos
281 1.1 christos /* isapnp_testconfig():
282 1.1 christos * Test/Allocate the regions used
283 1.1 christos */
284 1.1 christos static int
285 1.1 christos isapnp_testconfig(iot, memt, ipa, alloc)
286 1.1 christos bus_space_tag_t iot, memt;
287 1.1 christos struct isapnp_attach_args *ipa;
288 1.1 christos int alloc;
289 1.1 christos {
290 1.1 christos int nio = 0, nmem = 0, nmem32 = 0, nirq = 0, ndrq = 0;
291 1.1 christos int error = 0;
292 1.1 christos
293 1.1 christos #ifdef DEBUG_ISAPNP
294 1.1 christos isapnp_print_attach(ipa);
295 1.1 christos #endif
296 1.1 christos
297 1.1 christos for (; nio < ipa->ipa_nio; nio++) {
298 1.1 christos error = isapnp_alloc_region(iot, &ipa->ipa_io[nio]);
299 1.1 christos if (error)
300 1.1 christos goto bad;
301 1.1 christos }
302 1.1 christos
303 1.1 christos for (; nmem < ipa->ipa_nmem; nmem++) {
304 1.1 christos error = isapnp_alloc_region(memt, &ipa->ipa_mem[nmem]);
305 1.1 christos if (error)
306 1.1 christos goto bad;
307 1.1 christos }
308 1.1 christos
309 1.1 christos for (; nmem32 < ipa->ipa_nmem32; nmem32++) {
310 1.1 christos error = isapnp_alloc_region(memt, &ipa->ipa_mem32[nmem32]);
311 1.1 christos if (error)
312 1.1 christos goto bad;
313 1.1 christos }
314 1.1 christos
315 1.1 christos for (; nirq < ipa->ipa_nirq; nirq++) {
316 1.11 thorpej error = isapnp_alloc_irq(ipa->ipa_ic, &ipa->ipa_irq[nirq]);
317 1.1 christos if (error)
318 1.1 christos goto bad;
319 1.1 christos }
320 1.1 christos
321 1.1 christos for (; ndrq < ipa->ipa_ndrq; ndrq++) {
322 1.18 thorpej error = isapnp_alloc_drq(ipa->ipa_ic, &ipa->ipa_drq[ndrq]);
323 1.1 christos if (error)
324 1.1 christos goto bad;
325 1.1 christos }
326 1.1 christos
327 1.1 christos if (alloc)
328 1.1 christos return error;
329 1.1 christos
330 1.1 christos bad:
331 1.1 christos #ifdef notyet
332 1.1 christos for (ndrq--; ndrq >= 0; ndrq--)
333 1.1 christos isapnp_free_pin(&ipa->ipa_drq[ndrq]);
334 1.1 christos
335 1.1 christos for (nirq--; nirq >= 0; nirq--)
336 1.1 christos isapnp_free_pin(&ipa->ipa_irq[nirq]);
337 1.1 christos #endif
338 1.1 christos
339 1.1 christos for (nmem32--; nmem32 >= 0; nmem32--)
340 1.1 christos isapnp_free_region(memt, &ipa->ipa_mem32[nmem32]);
341 1.1 christos
342 1.1 christos for (nmem--; nmem >= 0; nmem--)
343 1.1 christos isapnp_free_region(memt, &ipa->ipa_mem[nmem]);
344 1.1 christos
345 1.1 christos for (nio--; nio >= 0; nio--)
346 1.1 christos isapnp_free_region(iot, &ipa->ipa_io[nio]);
347 1.1 christos
348 1.1 christos return error;
349 1.1 christos }
350 1.1 christos
351 1.1 christos
352 1.1 christos /* isapnp_config():
353 1.1 christos * Test/Allocate the regions used
354 1.1 christos */
355 1.1 christos int
356 1.1 christos isapnp_config(iot, memt, ipa)
357 1.1 christos bus_space_tag_t iot, memt;
358 1.1 christos struct isapnp_attach_args *ipa;
359 1.1 christos {
360 1.1 christos return isapnp_testconfig(iot, memt, ipa, 1);
361 1.1 christos }
362 1.1 christos
363 1.1 christos
364 1.1 christos /* isapnp_unconfig():
365 1.1 christos * Free the regions used
366 1.1 christos */
367 1.1 christos void
368 1.1 christos isapnp_unconfig(iot, memt, ipa)
369 1.1 christos bus_space_tag_t iot, memt;
370 1.1 christos struct isapnp_attach_args *ipa;
371 1.1 christos {
372 1.1 christos int i;
373 1.1 christos
374 1.1 christos #ifdef notyet
375 1.1 christos for (i = 0; i < ipa->ipa_ndrq; i++)
376 1.1 christos isapnp_free_pin(&ipa->ipa_drq[i]);
377 1.1 christos
378 1.1 christos for (i = 0; i < ipa->ipa_nirq; i++)
379 1.1 christos isapnp_free_pin(&ipa->ipa_irq[i]);
380 1.1 christos #endif
381 1.1 christos
382 1.1 christos for (i = 0; i < ipa->ipa_nmem32; i++)
383 1.1 christos isapnp_free_region(memt, &ipa->ipa_mem32[i]);
384 1.1 christos
385 1.1 christos for (i = 0; i < ipa->ipa_nmem; i++)
386 1.1 christos isapnp_free_region(memt, &ipa->ipa_mem[i]);
387 1.1 christos
388 1.1 christos for (i = 0; i < ipa->ipa_nio; i++)
389 1.1 christos isapnp_free_region(iot, &ipa->ipa_io[i]);
390 1.1 christos }
391 1.1 christos
392 1.1 christos
393 1.1 christos /* isapnp_bestconfig():
394 1.1 christos * Return the best configuration for each logical device, remove and
395 1.1 christos * free all other configurations.
396 1.1 christos */
397 1.1 christos static struct isapnp_attach_args *
398 1.18 thorpej isapnp_bestconfig(sc, ipa)
399 1.1 christos struct isapnp_softc *sc;
400 1.1 christos struct isapnp_attach_args **ipa;
401 1.1 christos {
402 1.1 christos struct isapnp_attach_args *c, *best, *f = *ipa;
403 1.1 christos int error;
404 1.1 christos
405 1.1 christos for (;;) {
406 1.1 christos if (f == NULL)
407 1.1 christos return NULL;
408 1.1 christos
409 1.1 christos #define SAMEDEV(a, b) (strcmp((a)->ipa_devlogic, (b)->ipa_devlogic) == 0)
410 1.1 christos
411 1.1 christos /* Find the best config */
412 1.2 christos for (best = c = f; c != NULL; c = c->ipa_sibling) {
413 1.1 christos if (!SAMEDEV(c, f))
414 1.1 christos continue;
415 1.1 christos if (c->ipa_pref < best->ipa_pref)
416 1.1 christos best = c;
417 1.1 christos }
418 1.1 christos
419 1.18 thorpej /*
420 1.18 thorpej * Make sure the ISA chipset is initialized! We need
421 1.18 thorpej * it to test the best config!
422 1.18 thorpej */
423 1.18 thorpej best->ipa_ic = sc->sc_ic;
424 1.18 thorpej
425 1.1 christos /* Test the best config */
426 1.1 christos error = isapnp_testconfig(sc->sc_iot, sc->sc_memt, best, 0);
427 1.1 christos
428 1.1 christos /* Remove this config from the list */
429 1.1 christos if (best == f)
430 1.2 christos f = f->ipa_sibling;
431 1.1 christos else {
432 1.2 christos for (c = f; c->ipa_sibling != best; c = c->ipa_sibling)
433 1.1 christos continue;
434 1.2 christos c->ipa_sibling = best->ipa_sibling;
435 1.1 christos }
436 1.1 christos
437 1.1 christos if (error) {
438 1.1 christos best->ipa_pref = ISAPNP_DEP_CONFLICTING;
439 1.1 christos
440 1.2 christos for (c = f; c != NULL; c = c->ipa_sibling)
441 1.1 christos if (c != best && SAMEDEV(c, best))
442 1.1 christos break;
443 1.1 christos /* Last config for this logical device is conflicting */
444 1.1 christos if (c == NULL) {
445 1.1 christos *ipa = f;
446 1.1 christos return best;
447 1.1 christos }
448 1.1 christos
449 1.1 christos ISAPNP_FREE(best);
450 1.1 christos continue;
451 1.1 christos }
452 1.1 christos else {
453 1.1 christos /* Remove all other configs for this device */
454 1.1 christos struct isapnp_attach_args *l = NULL, *n = NULL, *d;
455 1.1 christos
456 1.1 christos for (c = f; c; ) {
457 1.1 christos if (c == best)
458 1.1 christos continue;
459 1.2 christos d = c->ipa_sibling;
460 1.1 christos if (SAMEDEV(c, best))
461 1.1 christos ISAPNP_FREE(c);
462 1.1 christos else {
463 1.1 christos if (n)
464 1.2 christos n->ipa_sibling = c;
465 1.1 christos
466 1.1 christos else
467 1.1 christos l = c;
468 1.1 christos n = c;
469 1.2 christos c->ipa_sibling = NULL;
470 1.1 christos }
471 1.1 christos c = d;
472 1.1 christos }
473 1.1 christos f = l;
474 1.1 christos }
475 1.1 christos *ipa = f;
476 1.1 christos return best;
477 1.1 christos }
478 1.1 christos }
479 1.1 christos
480 1.1 christos
481 1.1 christos /* isapnp_id_to_vendor():
482 1.1 christos * Convert a pnp ``compressed ascii'' vendor id to a string
483 1.1 christos */
484 1.1 christos char *
485 1.1 christos isapnp_id_to_vendor(v, id)
486 1.1 christos char *v;
487 1.1 christos const u_char *id;
488 1.1 christos {
489 1.1 christos static const char hex[] = "0123456789ABCDEF";
490 1.1 christos char *p = v;
491 1.1 christos
492 1.1 christos *p++ = 'A' + (id[0] >> 2) - 1;
493 1.1 christos *p++ = 'A' + ((id[0] & 3) << 3) + (id[1] >> 5) - 1;
494 1.1 christos *p++ = 'A' + (id[1] & 0x1f) - 1;
495 1.1 christos *p++ = hex[id[2] >> 4];
496 1.1 christos *p++ = hex[id[2] & 0x0f];
497 1.1 christos *p++ = hex[id[3] >> 4];
498 1.1 christos *p++ = hex[id[3] & 0x0f];
499 1.1 christos *p = '\0';
500 1.1 christos
501 1.1 christos return v;
502 1.1 christos }
503 1.1 christos
504 1.1 christos
505 1.1 christos /* isapnp_print_region():
506 1.1 christos * Print a region allocation
507 1.1 christos */
508 1.1 christos static void
509 1.1 christos isapnp_print_region(str, r, n)
510 1.1 christos const char *str;
511 1.1 christos struct isapnp_region *r;
512 1.1 christos size_t n;
513 1.1 christos {
514 1.1 christos size_t i;
515 1.1 christos
516 1.1 christos if (n == 0)
517 1.1 christos return;
518 1.1 christos
519 1.1 christos printf(" %s ", str);
520 1.1 christos for (i = 0; i < n; i++, r++) {
521 1.1 christos printf("0x%x", r->base);
522 1.1 christos if (r->length)
523 1.1 christos printf("/%d", r->length);
524 1.1 christos if (i != n - 1)
525 1.1 christos printf(",");
526 1.1 christos }
527 1.1 christos }
528 1.1 christos
529 1.1 christos
530 1.1 christos /* isapnp_print_pin():
531 1.1 christos * Print an irq/drq assignment
532 1.1 christos */
533 1.1 christos static void
534 1.1 christos isapnp_print_pin(str, p, n)
535 1.1 christos const char *str;
536 1.1 christos struct isapnp_pin *p;
537 1.1 christos size_t n;
538 1.1 christos {
539 1.1 christos size_t i;
540 1.1 christos
541 1.1 christos if (n == 0)
542 1.1 christos return;
543 1.1 christos
544 1.1 christos printf(" %s ", str);
545 1.1 christos for (i = 0; i < n; i++, p++) {
546 1.1 christos printf("%d", p->num);
547 1.1 christos if (i != n - 1)
548 1.1 christos printf(",");
549 1.1 christos }
550 1.1 christos }
551 1.1 christos
552 1.1 christos
553 1.1 christos /* isapnp_print():
554 1.1 christos * Print the configuration line for an ISA PnP card.
555 1.1 christos */
556 1.1 christos static int
557 1.1 christos isapnp_print(aux, str)
558 1.1 christos void *aux;
559 1.1 christos const char *str;
560 1.1 christos {
561 1.1 christos struct isapnp_attach_args *ipa = aux;
562 1.1 christos
563 1.1 christos if (str != NULL)
564 1.7 mikel printf("%s: <%s, %s, %s, %s>",
565 1.1 christos str, ipa->ipa_devident, ipa->ipa_devlogic,
566 1.7 mikel ipa->ipa_devcompat, ipa->ipa_devclass);
567 1.1 christos
568 1.1 christos isapnp_print_region("port", ipa->ipa_io, ipa->ipa_nio);
569 1.1 christos isapnp_print_region("mem", ipa->ipa_mem, ipa->ipa_nmem);
570 1.1 christos isapnp_print_region("mem32", ipa->ipa_mem32, ipa->ipa_nmem32);
571 1.1 christos isapnp_print_pin("irq", ipa->ipa_irq, ipa->ipa_nirq);
572 1.1 christos isapnp_print_pin("drq", ipa->ipa_drq, ipa->ipa_ndrq);
573 1.1 christos
574 1.1 christos return UNCONF;
575 1.1 christos }
576 1.1 christos
577 1.1 christos
578 1.7 mikel #ifdef _KERNEL
579 1.1 christos /* isapnp_submatch():
580 1.9 mikel * Probe the logical device...
581 1.1 christos */
582 1.1 christos static int
583 1.1 christos isapnp_submatch(parent, match, aux)
584 1.1 christos struct device *parent;
585 1.13 drochner struct cfdata *match;
586 1.13 drochner void *aux;
587 1.1 christos {
588 1.14 mjacob struct cfdata *cf = (struct cfdata *) match;
589 1.1 christos return ((*cf->cf_attach->ca_match)(parent, match, aux));
590 1.1 christos }
591 1.20 christos
592 1.21 christos
593 1.20 christos /* isapnp_devmatch():
594 1.20 christos * Match a probed device with the information from the driver
595 1.20 christos */
596 1.20 christos int
597 1.20 christos isapnp_devmatch(ipa, dinfo)
598 1.20 christos const struct isapnp_attach_args *ipa;
599 1.20 christos const struct isapnp_devinfo *dinfo;
600 1.20 christos {
601 1.20 christos const char *const *name;
602 1.20 christos
603 1.20 christos for (name = dinfo->devlogic; *name; name++)
604 1.20 christos if (strcmp(*name, ipa->ipa_devlogic) == 0)
605 1.20 christos return 1;
606 1.20 christos
607 1.20 christos for (name = dinfo->devcompat; *name; name++)
608 1.20 christos if (strcmp(*name, ipa->ipa_devcompat) == 0)
609 1.20 christos return 1;
610 1.20 christos
611 1.20 christos return 0;
612 1.21 christos }
613 1.21 christos
614 1.21 christos
615 1.21 christos /* isapnp_isa_attach_hook():
616 1.21 christos * This routine is called from the isa attach code and
617 1.21 christos * is a kludge; we are resetting all the cards here in order
618 1.21 christos * to undo any card configuration that the bios did for us, in order
619 1.21 christos * to avoid having the PnP devices match an isa probe. The correct
620 1.21 christos * way of doing this is to read the PnP BIOS and find the card settings
621 1.21 christos * from there. Unfortunately it is not as easy as it sounds.
622 1.21 christos */
623 1.21 christos void
624 1.21 christos isapnp_isa_attach_hook(isa_sc)
625 1.21 christos struct isa_softc *isa_sc;
626 1.21 christos {
627 1.21 christos struct isapnp_softc sc;
628 1.21 christos
629 1.21 christos sc.sc_iot = isa_sc->sc_iot;
630 1.21 christos sc.sc_ncards = 0;
631 1.21 christos
632 1.21 christos if (isapnp_map(&sc))
633 1.21 christos return;
634 1.21 christos
635 1.21 christos isapnp_init(&sc);
636 1.21 christos
637 1.21 christos isapnp_write_reg(&sc, ISAPNP_CONFIG_CONTROL, ISAPNP_CC_RESET_DRV);
638 1.21 christos DELAY(2000);
639 1.21 christos
640 1.21 christos isapnp_unmap(&sc);
641 1.20 christos }
642 1.7 mikel #endif
643 1.1 christos
644 1.1 christos
645 1.1 christos /* isapnp_find():
646 1.1 christos * Probe and add cards
647 1.1 christos */
648 1.1 christos static int
649 1.9 mikel isapnp_find(sc, all)
650 1.1 christos struct isapnp_softc *sc;
651 1.9 mikel int all;
652 1.1 christos {
653 1.1 christos int p;
654 1.1 christos
655 1.1 christos isapnp_init(sc);
656 1.1 christos
657 1.1 christos isapnp_write_reg(sc, ISAPNP_CONFIG_CONTROL, ISAPNP_CC_RESET_DRV);
658 1.1 christos DELAY(2000);
659 1.1 christos
660 1.1 christos isapnp_init(sc);
661 1.1 christos DELAY(2000);
662 1.1 christos
663 1.1 christos for (p = ISAPNP_RDDATA_MIN; p <= ISAPNP_RDDATA_MAX; p += 4) {
664 1.1 christos sc->sc_read_port = p;
665 1.1 christos if (isapnp_map_readport(sc))
666 1.1 christos continue;
667 1.20 christos DPRINTF(("%s: Trying port %x\r", sc->sc_dev.dv_xname, p));
668 1.1 christos if (isapnp_findcard(sc))
669 1.1 christos break;
670 1.1 christos isapnp_unmap_readport(sc);
671 1.1 christos }
672 1.1 christos
673 1.1 christos if (p > ISAPNP_RDDATA_MAX) {
674 1.1 christos sc->sc_read_port = 0;
675 1.1 christos return 0;
676 1.1 christos }
677 1.1 christos
678 1.9 mikel if (all)
679 1.9 mikel while (isapnp_findcard(sc))
680 1.9 mikel continue;
681 1.1 christos
682 1.1 christos return 1;
683 1.1 christos }
684 1.1 christos
685 1.1 christos
686 1.1 christos /* isapnp_configure():
687 1.1 christos * Configure a PnP card
688 1.1 christos * XXX: The memory configuration code is wrong. We need to check the
689 1.1 christos * range/length bit an do appropriate sets.
690 1.1 christos */
691 1.1 christos static void
692 1.1 christos isapnp_configure(sc, ipa)
693 1.1 christos struct isapnp_softc *sc;
694 1.1 christos const struct isapnp_attach_args *ipa;
695 1.1 christos {
696 1.1 christos int i;
697 1.1 christos static u_char isapnp_mem_range[] = ISAPNP_MEM_DESC;
698 1.1 christos static u_char isapnp_io_range[] = ISAPNP_IO_DESC;
699 1.1 christos static u_char isapnp_irq_range[] = ISAPNP_IRQ_DESC;
700 1.1 christos static u_char isapnp_drq_range[] = ISAPNP_DRQ_DESC;
701 1.1 christos static u_char isapnp_mem32_range[] = ISAPNP_MEM32_DESC;
702 1.1 christos const struct isapnp_region *r;
703 1.1 christos const struct isapnp_pin *p;
704 1.1 christos struct isapnp_region rz;
705 1.1 christos struct isapnp_pin pz;
706 1.1 christos
707 1.1 christos memset(&pz, 0, sizeof(pz));
708 1.1 christos memset(&rz, 0, sizeof(rz));
709 1.1 christos
710 1.1 christos #define B0(a) ((a) & 0xff)
711 1.1 christos #define B1(a) (((a) >> 8) & 0xff)
712 1.1 christos #define B2(a) (((a) >> 16) & 0xff)
713 1.1 christos #define B3(a) (((a) >> 24) & 0xff)
714 1.1 christos
715 1.1 christos for (i = 0; i < sizeof(isapnp_io_range); i++) {
716 1.1 christos if (i < ipa->ipa_nio)
717 1.1 christos r = &ipa->ipa_io[i];
718 1.1 christos else
719 1.1 christos r = &rz;
720 1.1 christos
721 1.1 christos isapnp_write_reg(sc,
722 1.1 christos isapnp_io_range[i] + ISAPNP_IO_BASE_15_8, B1(r->base));
723 1.1 christos isapnp_write_reg(sc,
724 1.1 christos isapnp_io_range[i] + ISAPNP_IO_BASE_7_0, B0(r->base));
725 1.1 christos }
726 1.1 christos
727 1.1 christos for (i = 0; i < sizeof(isapnp_mem_range); i++) {
728 1.1 christos if (i < ipa->ipa_nmem)
729 1.1 christos r = &ipa->ipa_mem[i];
730 1.1 christos else
731 1.1 christos r = &rz;
732 1.1 christos
733 1.1 christos isapnp_write_reg(sc,
734 1.1 christos isapnp_mem_range[i] + ISAPNP_MEM_BASE_23_16, B2(r->base));
735 1.1 christos isapnp_write_reg(sc,
736 1.1 christos isapnp_mem_range[i] + ISAPNP_MEM_BASE_15_8, B1(r->base));
737 1.1 christos
738 1.1 christos isapnp_write_reg(sc,
739 1.1 christos isapnp_mem_range[i] + ISAPNP_MEM_LRANGE_23_16,
740 1.1 christos B2(r->length));
741 1.1 christos isapnp_write_reg(sc,
742 1.1 christos isapnp_mem_range[i] + ISAPNP_MEM_LRANGE_15_8,
743 1.1 christos B1(r->length));
744 1.1 christos }
745 1.1 christos
746 1.1 christos for (i = 0; i < sizeof(isapnp_irq_range); i++) {
747 1.1 christos u_char v;
748 1.1 christos
749 1.1 christos if (i < ipa->ipa_nirq)
750 1.1 christos p = &ipa->ipa_irq[i];
751 1.1 christos else
752 1.1 christos p = &pz;
753 1.1 christos
754 1.1 christos isapnp_write_reg(sc,
755 1.1 christos isapnp_irq_range[i] + ISAPNP_IRQ_NUMBER, p->num);
756 1.1 christos
757 1.1 christos switch (p->flags) {
758 1.1 christos case ISAPNP_IRQTYPE_LEVEL_PLUS:
759 1.1 christos v = ISAPNP_IRQ_LEVEL|ISAPNP_IRQ_HIGH;
760 1.1 christos break;
761 1.1 christos
762 1.1 christos case ISAPNP_IRQTYPE_EDGE_PLUS:
763 1.1 christos v = ISAPNP_IRQ_HIGH;
764 1.1 christos break;
765 1.1 christos
766 1.1 christos case ISAPNP_IRQTYPE_LEVEL_MINUS:
767 1.1 christos v = ISAPNP_IRQ_LEVEL;
768 1.1 christos break;
769 1.1 christos
770 1.1 christos default:
771 1.1 christos case ISAPNP_IRQTYPE_EDGE_MINUS:
772 1.1 christos v = 0;
773 1.1 christos break;
774 1.1 christos }
775 1.1 christos isapnp_write_reg(sc,
776 1.1 christos isapnp_irq_range[i] + ISAPNP_IRQ_CONTROL, v);
777 1.1 christos }
778 1.1 christos
779 1.1 christos for (i = 0; i < sizeof(isapnp_drq_range); i++) {
780 1.1 christos u_char v;
781 1.1 christos
782 1.1 christos if (i < ipa->ipa_ndrq)
783 1.1 christos v = ipa->ipa_drq[i].num;
784 1.1 christos else
785 1.1 christos v = 4;
786 1.1 christos
787 1.1 christos isapnp_write_reg(sc, isapnp_drq_range[i], v);
788 1.1 christos }
789 1.1 christos
790 1.1 christos for (i = 0; i < sizeof(isapnp_mem32_range); i++) {
791 1.1 christos if (i < ipa->ipa_nmem32)
792 1.1 christos r = &ipa->ipa_mem32[i];
793 1.1 christos else
794 1.1 christos r = &rz;
795 1.1 christos
796 1.1 christos isapnp_write_reg(sc,
797 1.1 christos isapnp_mem32_range[i] + ISAPNP_MEM32_BASE_31_24,
798 1.1 christos B3(r->base));
799 1.1 christos isapnp_write_reg(sc,
800 1.1 christos isapnp_mem32_range[i] + ISAPNP_MEM32_BASE_23_16,
801 1.1 christos B2(r->base));
802 1.1 christos isapnp_write_reg(sc,
803 1.1 christos isapnp_mem32_range[i] + ISAPNP_MEM32_BASE_15_8,
804 1.1 christos B1(r->base));
805 1.1 christos isapnp_write_reg(sc,
806 1.1 christos isapnp_mem32_range[i] + ISAPNP_MEM32_BASE_7_0,
807 1.1 christos B0(r->base));
808 1.1 christos
809 1.1 christos isapnp_write_reg(sc,
810 1.1 christos isapnp_mem32_range[i] + ISAPNP_MEM32_LRANGE_31_24,
811 1.1 christos B3(r->length));
812 1.1 christos isapnp_write_reg(sc,
813 1.1 christos isapnp_mem32_range[i] + ISAPNP_MEM32_LRANGE_23_16,
814 1.1 christos B2(r->length));
815 1.1 christos isapnp_write_reg(sc,
816 1.1 christos isapnp_mem32_range[i] + ISAPNP_MEM32_LRANGE_15_8,
817 1.1 christos B1(r->length));
818 1.1 christos isapnp_write_reg(sc,
819 1.1 christos isapnp_mem32_range[i] + ISAPNP_MEM32_LRANGE_7_0,
820 1.1 christos B0(r->length));
821 1.1 christos }
822 1.1 christos }
823 1.1 christos
824 1.1 christos
825 1.1 christos /* isapnp_match():
826 1.1 christos * Probe routine
827 1.1 christos */
828 1.1 christos static int
829 1.1 christos isapnp_match(parent, match, aux)
830 1.1 christos struct device *parent;
831 1.9 mikel struct cfdata *match;
832 1.9 mikel void *aux;
833 1.1 christos {
834 1.1 christos struct isapnp_softc sc;
835 1.1 christos struct isa_attach_args *ia = aux;
836 1.23 thorpej struct isapnp_probe_cookie *ipc;
837 1.23 thorpej
838 1.23 thorpej /*
839 1.23 thorpej * Ensure we only probe ISA PnP once; we don't actually consume
840 1.23 thorpej * bus resources, so we have to prevent being cloned forever.
841 1.23 thorpej */
842 1.23 thorpej for (ipc = LIST_FIRST(&isapnp_probes); ipc != NULL;
843 1.23 thorpej ipc = LIST_NEXT(ipc, ipc_link))
844 1.23 thorpej if (ipc->ipc_parent == parent)
845 1.23 thorpej return (0);
846 1.23 thorpej
847 1.23 thorpej ipc = malloc(sizeof(*ipc), M_DEVBUF, M_NOWAIT);
848 1.23 thorpej if (ipc == NULL)
849 1.23 thorpej panic("isapnp_match: can't allocate probe cookie");
850 1.23 thorpej
851 1.23 thorpej ipc->ipc_parent = parent;
852 1.23 thorpej LIST_INSERT_HEAD(&isapnp_probes, ipc, ipc_link);
853 1.1 christos
854 1.1 christos sc.sc_iot = ia->ia_iot;
855 1.1 christos (void) strcpy(sc.sc_dev.dv_xname, "(isapnp probe)");
856 1.1 christos
857 1.1 christos if (isapnp_map(&sc))
858 1.1 christos return 0;
859 1.1 christos
860 1.22 thorpej isapnp_unmap(&sc);
861 1.22 thorpej
862 1.22 thorpej /*
863 1.22 thorpej * We always match. We must let all legacy ISA devices map
864 1.22 thorpej * their address spaces before we look for a read port.
865 1.22 thorpej */
866 1.9 mikel ia->ia_iobase = ISAPNP_ADDR;
867 1.9 mikel ia->ia_iosize = 1;
868 1.1 christos
869 1.22 thorpej return (1);
870 1.1 christos }
871 1.1 christos
872 1.1 christos
873 1.1 christos /* isapnp_attach
874 1.22 thorpej * Attach the PnP `bus'.
875 1.1 christos */
876 1.1 christos static void
877 1.1 christos isapnp_attach(parent, self, aux)
878 1.1 christos struct device *parent, *self;
879 1.1 christos void *aux;
880 1.1 christos {
881 1.1 christos struct isapnp_softc *sc = (struct isapnp_softc *) self;
882 1.1 christos struct isa_attach_args *ia = aux;
883 1.1 christos
884 1.1 christos sc->sc_iot = ia->ia_iot;
885 1.1 christos sc->sc_memt = ia->ia_memt;
886 1.18 thorpej sc->sc_ic = ia->ia_ic;
887 1.22 thorpej sc->sc_dmat = ia->ia_dmat;
888 1.1 christos sc->sc_ncards = 0;
889 1.1 christos
890 1.22 thorpej printf(": ISA Plug 'n Play device support\n");
891 1.1 christos
892 1.22 thorpej if (isapnp_map(sc)) {
893 1.22 thorpej printf("%s: unable to map PnP register\n",
894 1.22 thorpej sc->sc_dev.dv_xname);
895 1.22 thorpej return;
896 1.22 thorpej }
897 1.1 christos
898 1.22 thorpej #ifdef _KERNEL
899 1.22 thorpej /*
900 1.22 thorpej * Defer configuration until the rest of the ISA devices have
901 1.22 thorpej * attached themselves.
902 1.22 thorpej */
903 1.22 thorpej config_defer(self, isapnp_callback);
904 1.22 thorpej #else
905 1.22 thorpej isapnp_callback(self);
906 1.22 thorpej #endif
907 1.22 thorpej }
908 1.1 christos
909 1.22 thorpej /* isapnp_callback
910 1.22 thorpej * Find and attach PnP cards.
911 1.22 thorpej */
912 1.22 thorpej void
913 1.22 thorpej isapnp_callback(self)
914 1.22 thorpej struct device *self;
915 1.22 thorpej {
916 1.22 thorpej struct isapnp_softc *sc = (struct isapnp_softc *)self;
917 1.22 thorpej struct isapnp_attach_args *ipa, *lpa;
918 1.25 thorpej int c, d;
919 1.22 thorpej
920 1.22 thorpej /*
921 1.22 thorpej * Look for cards. If none are found, we say so and just return.
922 1.22 thorpej */
923 1.24 thorpej if (isapnp_find(sc, 1) == 0) {
924 1.26 thorpej printf("%s: no ISA Plug 'n Play devices found\n",
925 1.24 thorpej sc->sc_dev.dv_xname);
926 1.24 thorpej return;
927 1.24 thorpej }
928 1.22 thorpej
929 1.22 thorpej printf("%s: read port 0x%x\n", sc->sc_dev.dv_xname, sc->sc_read_port);
930 1.22 thorpej
931 1.22 thorpej /*
932 1.22 thorpej * Now configure all of the cards.
933 1.22 thorpej */
934 1.1 christos for (c = 0; c < sc->sc_ncards; c++) {
935 1.1 christos /* Good morning card c */
936 1.1 christos isapnp_write_reg(sc, ISAPNP_WAKE, c + 1);
937 1.1 christos
938 1.1 christos if ((ipa = isapnp_get_resource(sc, c)) == NULL)
939 1.1 christos continue;
940 1.1 christos
941 1.1 christos DPRINTF(("Selecting attachments\n"));
942 1.10 thorpej for (d = 0;
943 1.18 thorpej (lpa = isapnp_bestconfig(sc, &ipa)) != NULL; d++) {
944 1.1 christos isapnp_write_reg(sc, ISAPNP_LOGICAL_DEV_NUM, d);
945 1.1 christos isapnp_configure(sc, lpa);
946 1.1 christos #ifdef DEBUG_ISAPNP
947 1.1 christos {
948 1.1 christos struct isapnp_attach_args pa;
949 1.7 mikel
950 1.1 christos isapnp_get_config(sc, &pa);
951 1.1 christos isapnp_print_config(&pa);
952 1.1 christos }
953 1.1 christos #endif
954 1.1 christos
955 1.12 christos DPRINTF(("%s: configuring <%s, %s, %s, %s>\n",
956 1.1 christos sc->sc_dev.dv_xname,
957 1.1 christos lpa->ipa_devident, lpa->ipa_devlogic,
958 1.12 christos lpa->ipa_devcompat, lpa->ipa_devclass));
959 1.1 christos if (lpa->ipa_pref == ISAPNP_DEP_CONFLICTING) {
960 1.7 mikel printf("%s: <%s, %s, %s, %s> ignored; %s\n",
961 1.1 christos sc->sc_dev.dv_xname,
962 1.1 christos lpa->ipa_devident, lpa->ipa_devlogic,
963 1.7 mikel lpa->ipa_devcompat, lpa->ipa_devclass,
964 1.7 mikel "resource conflict");
965 1.1 christos ISAPNP_FREE(lpa);
966 1.1 christos continue;
967 1.1 christos }
968 1.1 christos
969 1.22 thorpej lpa->ipa_ic = sc->sc_ic;
970 1.22 thorpej lpa->ipa_iot = sc->sc_iot;
971 1.22 thorpej lpa->ipa_memt = sc->sc_memt;
972 1.22 thorpej lpa->ipa_dmat = sc->sc_dmat;
973 1.1 christos
974 1.1 christos isapnp_write_reg(sc, ISAPNP_ACTIVATE, 1);
975 1.1 christos #ifdef _KERNEL
976 1.7 mikel if (config_found_sm(self, lpa, isapnp_print,
977 1.1 christos isapnp_submatch) == NULL)
978 1.1 christos isapnp_write_reg(sc, ISAPNP_ACTIVATE, 0);
979 1.1 christos #else
980 1.1 christos isapnp_print(lpa, NULL);
981 1.1 christos printf("\n");
982 1.1 christos #endif
983 1.1 christos ISAPNP_FREE(lpa);
984 1.1 christos }
985 1.1 christos isapnp_write_reg(sc, ISAPNP_WAKE, 0); /* Good night cards */
986 1.1 christos }
987 1.1 christos }
988