isa.c revision 1.135 1 /* $NetBSD: isa.c,v 1.135 2009/04/07 21:48:46 dyoung Exp $ */
2
3 /*-
4 * Copyright (c) 1998, 2001, 2008 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Charles M. Hannum; by Jason R. Thorpe of Wasabi Systems, Inc.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: isa.c,v 1.135 2009/04/07 21:48:46 dyoung Exp $");
34
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/kernel.h>
38 #include <sys/malloc.h>
39 #include <sys/device.h>
40
41 #include <sys/intr.h>
42
43 #include <dev/isa/isareg.h>
44 #include <dev/isa/isavar.h>
45 #include <dev/isa/isadmareg.h>
46
47 #include "isadma.h"
48
49 #include "isapnp.h"
50 #if NISAPNP > 0
51 #include <dev/isapnp/isapnpreg.h>
52 #include <dev/isapnp/isapnpvar.h>
53 #endif
54
55 #include "locators.h"
56
57 int isamatch(device_t, cfdata_t, void *);
58 void isaattach(device_t, device_t, void *);
59 int isarescan(device_t, const char *, const int *);
60 void isachilddetached(device_t, device_t);
61 int isaprint(void *, const char *);
62
63 CFATTACH_DECL2_NEW(isa, sizeof(struct isa_softc),
64 isamatch, isaattach, NULL, NULL, isarescan, isachilddetached);
65
66 void isa_attach_knowndevs(struct isa_softc *);
67 void isa_free_knowndevs(struct isa_softc *);
68
69 int isasubmatch(device_t, cfdata_t, const int *, void *);
70 int isasearch(device_t, cfdata_t, const int *, void *);
71
72 static int isa_slotcount = -1; /* -1 == don't know how many */
73
74 int
75 isamatch(device_t parent, cfdata_t cf, void *aux)
76 {
77 /* XXX check other indicators */
78
79 return (1);
80 }
81
82 void
83 isaattach(device_t parent, device_t self, void *aux)
84 {
85 struct isa_softc *sc = device_private(self);
86 struct isabus_attach_args *iba = aux;
87 static const int wildcard[ISACF_NLOCS] = {
88 ISACF_PORT_DEFAULT, ISACF_SIZE_DEFAULT,
89 ISACF_IOMEM_DEFAULT, ISACF_IOSIZ_DEFAULT,
90 ISACF_IRQ_DEFAULT, ISACF_DRQ_DEFAULT, ISACF_DRQ2_DEFAULT
91 };
92
93 TAILQ_INIT(&sc->sc_knowndevs);
94 sc->sc_dynamicdevs = 0;
95
96 sc->sc_dev = self;
97
98 isa_attach_hook(parent, self, iba);
99 aprint_naive("\n");
100 aprint_normal("\n");
101
102 /* XXX Add code to fetch known-devices. */
103
104 sc->sc_iot = iba->iba_iot;
105 sc->sc_memt = iba->iba_memt;
106 sc->sc_dmat = iba->iba_dmat;
107 sc->sc_ic = iba->iba_ic;
108
109 #if NISAPNP > 0
110 /*
111 * Reset isapnp cards that the bios configured for us
112 */
113 isapnp_isa_attach_hook(sc);
114 #endif
115
116 #if NISADMA > 0
117 /*
118 * Initialize our DMA state.
119 */
120 isa_dmainit(sc->sc_ic, sc->sc_iot, sc->sc_dmat, self);
121 #endif
122
123 /* Attach all direct-config children. */
124 isa_attach_knowndevs(sc);
125
126 /*
127 * If we don't support dynamic hello/goodbye of devices,
128 * then free the knowndevs info now.
129 */
130 if (sc->sc_dynamicdevs == 0)
131 isa_free_knowndevs(sc);
132
133 /* Attach all indirect-config children. */
134 isarescan(self, "isa", wildcard);
135
136 if (!pmf_device_register(self, NULL, NULL))
137 aprint_error_dev(self, "couldn't establish power handler\n");
138 }
139
140 int
141 isarescan(device_t self, const char *ifattr, const int *locators)
142 {
143 int locs[ISACF_NLOCS];
144
145 memcpy(locs, locators, sizeof(locs));
146
147 /*
148 * XXX Bus independent code calling this function does not
149 * know the locator default values. It assumes "-1" for now.
150 * (should be made available by "config" one day)
151 * So fixup where the "-1" is not correct.
152 */
153 if (locs[ISACF_SIZE] == -1)
154 locs[ISACF_SIZE] = ISACF_SIZE_DEFAULT;
155 if (locs[ISACF_IOSIZ] == -1)
156 locs[ISACF_IOSIZ] = ISACF_IOSIZ_DEFAULT;
157
158 config_search_loc(isasearch, self, ifattr, locs, NULL);
159 return (0);
160 }
161
162 void
163 isachilddetached(device_t self, device_t child)
164 {
165 /* nothing to do */
166 }
167
168 void
169 isa_attach_knowndevs(struct isa_softc *sc)
170 {
171 struct isa_attach_args ia;
172 struct isa_knowndev *ik;
173
174 if (TAILQ_EMPTY(&sc->sc_knowndevs))
175 return;
176
177 TAILQ_FOREACH(ik, &sc->sc_knowndevs, ik_list) {
178 if (ik->ik_claimed != NULL)
179 continue;
180
181 ia.ia_iot = sc->sc_iot;
182 ia.ia_memt = sc->sc_memt;
183 ia.ia_dmat = sc->sc_dmat;
184 ia.ia_ic = sc->sc_ic;
185
186 ia.ia_pnpname = ik->ik_pnpname;
187 ia.ia_pnpcompatnames = ik->ik_pnpcompatnames;
188
189 ia.ia_io = ik->ik_io;
190 ia.ia_nio = ik->ik_nio;
191
192 ia.ia_iomem = ik->ik_iomem;
193 ia.ia_niomem = ik->ik_niomem;
194
195 ia.ia_irq = ik->ik_irq;
196 ia.ia_nirq = ik->ik_nirq;
197
198 ia.ia_drq = ik->ik_drq;
199 ia.ia_ndrq = ik->ik_ndrq;
200
201 ia.ia_aux = NULL;
202
203 /* XXX should setup locator array */
204
205 ik->ik_claimed = config_found_sm_loc(sc->sc_dev,
206 "isa", 0, &ia, isaprint, isasubmatch);
207 }
208 }
209
210 void
211 isa_free_knowndevs(struct isa_softc *sc)
212 {
213 struct isa_knowndev *ik;
214 struct isa_pnpname *ipn;
215
216 #define FREEIT(x) if (x != NULL) free(x, M_DEVBUF)
217
218 while ((ik = TAILQ_FIRST(&sc->sc_knowndevs)) != NULL) {
219 TAILQ_REMOVE(&sc->sc_knowndevs, ik, ik_list);
220 FREEIT(ik->ik_pnpname);
221 while ((ipn = ik->ik_pnpcompatnames) != NULL) {
222 ik->ik_pnpcompatnames = ipn->ipn_next;
223 free(ipn->ipn_name, M_DEVBUF);
224 free(ipn, M_DEVBUF);
225 }
226 FREEIT(ik->ik_io);
227 FREEIT(ik->ik_iomem);
228 FREEIT(ik->ik_irq);
229 FREEIT(ik->ik_drq);
230 free(ik, M_DEVBUF);
231 }
232
233 #undef FREEIT
234 }
235
236 static int
237 checkattachargs(struct isa_attach_args *ia, const int *loc)
238 {
239 int i;
240
241 if (ia->ia_nio == 0) {
242 if (loc[ISACF_PORT] != ISACF_PORT_DEFAULT)
243 return (0);
244 } else {
245 if (loc[ISACF_PORT] != ISACF_PORT_DEFAULT &&
246 loc[ISACF_PORT] != ia->ia_io[0].ir_addr)
247 return (0);
248 }
249
250 if (ia->ia_niomem == 0) {
251 if (loc[ISACF_IOMEM] != ISACF_IOMEM_DEFAULT)
252 return (0);
253 } else {
254 if (loc[ISACF_IOMEM] != ISACF_IOMEM_DEFAULT &&
255 loc[ISACF_IOMEM] != ia->ia_iomem[0].ir_addr)
256 return (0);
257 }
258
259 if (ia->ia_nirq == 0) {
260 if (loc[ISACF_IRQ] != ISACF_IRQ_DEFAULT)
261 return (0);
262 } else {
263 if (loc[ISACF_IRQ] != ISACF_IRQ_DEFAULT &&
264 loc[ISACF_IRQ] != ia->ia_irq[0].ir_irq)
265 return (0);
266 }
267
268 if (ia->ia_ndrq == 0) {
269 if (loc[ISACF_DRQ] != ISACF_DRQ_DEFAULT)
270 return (0);
271 if (loc[ISACF_DRQ2] != ISACF_DRQ2_DEFAULT)
272 return (0);
273 } else {
274 for (i = 0; i < 2; i++) {
275 if (i == ia->ia_ndrq)
276 break;
277 if (loc[ISACF_DRQ + i] != ISACF_DRQ_DEFAULT &&
278 loc[ISACF_DRQ + i] != ia->ia_drq[i].ir_drq)
279 return (0);
280 }
281 for (; i < 2; i++) {
282 if (loc[ISACF_DRQ + i] != ISACF_DRQ_DEFAULT)
283 return (0);
284 }
285 }
286
287 return (1);
288 }
289
290 int
291 isasubmatch(device_t parent, cfdata_t cf, const int *ldesc, void *aux)
292 {
293 struct isa_attach_args *ia = aux;
294
295 if (!checkattachargs(ia, cf->cf_loc))
296 return (0);
297
298 return (config_match(parent, cf, aux));
299 }
300
301 int
302 isaprint(void *aux, const char *isa)
303 {
304 struct isa_attach_args *ia = aux;
305 const char *sep;
306 int i;
307
308 /*
309 * This block of code only fires if we have a direct-config'd
310 * device for which there is no driver match.
311 */
312 if (isa != NULL) {
313 struct isa_pnpname *ipn;
314
315 if (ia->ia_pnpname != NULL)
316 aprint_normal("%s", ia->ia_pnpname);
317 if ((ipn = ia->ia_pnpcompatnames) != NULL) {
318 aprint_normal(" ("); /* ) */
319 for (sep = ""; ipn != NULL;
320 ipn = ipn->ipn_next, sep = " ") {
321 aprint_normal("%s%s", sep, ipn->ipn_name);
322 }
323 /* ( */ aprint_normal(")");
324 }
325 aprint_normal(" at %s", isa);
326 }
327
328 if (ia->ia_nio) {
329 sep = "";
330 aprint_normal(" port ");
331 for (i = 0; i < ia->ia_nio; i++) {
332 if (ia->ia_io[i].ir_size == 0)
333 continue;
334 aprint_normal("%s0x%x", sep, ia->ia_io[i].ir_addr);
335 if (ia->ia_io[i].ir_size > 1)
336 aprint_normal("-0x%x", ia->ia_io[i].ir_addr +
337 ia->ia_io[i].ir_size - 1);
338 sep = ",";
339 }
340 }
341
342 if (ia->ia_niomem) {
343 sep = "";
344 aprint_normal(" iomem ");
345 for (i = 0; i < ia->ia_niomem; i++) {
346 if (ia->ia_iomem[i].ir_size == 0)
347 continue;
348 aprint_normal("%s0x%x", sep, ia->ia_iomem[i].ir_addr);
349 if (ia->ia_iomem[i].ir_size > 1)
350 aprint_normal("-0x%x", ia->ia_iomem[i].ir_addr +
351 ia->ia_iomem[i].ir_size - 1);
352 sep = ",";
353 }
354 }
355
356 if (ia->ia_nirq) {
357 sep = "";
358 aprint_normal(" irq ");
359 for (i = 0; i < ia->ia_nirq; i++) {
360 if (ia->ia_irq[i].ir_irq == ISACF_IRQ_DEFAULT)
361 continue;
362 aprint_normal("%s%d", sep, ia->ia_irq[i].ir_irq);
363 sep = ",";
364 }
365 }
366
367 if (ia->ia_ndrq) {
368 sep = "";
369 aprint_normal(" drq ");
370 for (i = 0; i < ia->ia_ndrq; i++) {
371 if (ia->ia_drq[i].ir_drq == ISACF_DRQ_DEFAULT)
372 continue;
373 aprint_normal("%s%d", sep, ia->ia_drq[i].ir_drq);
374 sep = ",";
375 }
376 }
377
378 return (UNCONF);
379 }
380
381 int
382 isasearch(device_t parent, cfdata_t cf, const int *slocs, void *aux)
383 {
384 struct isa_io res_io[1];
385 struct isa_iomem res_mem[1];
386 struct isa_irq res_irq[1];
387 struct isa_drq res_drq[2];
388 struct isa_softc *sc = device_private(parent);
389 struct isa_attach_args ia;
390 int flocs[ISACF_NLOCS];
391 int tryagain;
392
393 do {
394 ia.ia_pnpname = NULL;
395 ia.ia_pnpcompatnames = NULL;
396
397 res_io[0].ir_addr = cf->cf_loc[ISACF_PORT];
398 res_io[0].ir_size = 0;
399
400 res_mem[0].ir_addr = cf->cf_loc[ISACF_IOMEM];
401 res_mem[0].ir_size = cf->cf_loc[ISACF_IOSIZ];
402
403 res_irq[0].ir_irq =
404 cf->cf_loc[ISACF_IRQ] == 2 ? 9 : cf->cf_loc[ISACF_IRQ];
405
406 res_drq[0].ir_drq = cf->cf_loc[ISACF_DRQ];
407 res_drq[1].ir_drq = cf->cf_loc[ISACF_DRQ2];
408
409 ia.ia_iot = sc->sc_iot;
410 ia.ia_memt = sc->sc_memt;
411 ia.ia_dmat = sc->sc_dmat;
412 ia.ia_ic = sc->sc_ic;
413
414 ia.ia_io = res_io;
415 ia.ia_nio = 1;
416
417 ia.ia_iomem = res_mem;
418 ia.ia_niomem = 1;
419
420 ia.ia_irq = res_irq;
421 ia.ia_nirq = 1;
422
423 ia.ia_drq = res_drq;
424 ia.ia_ndrq = 2;
425
426 if (!checkattachargs(&ia, slocs))
427 return (0);
428
429 tryagain = 0;
430 if (config_match(parent, cf, &ia) > 0) {
431 /*
432 * This is not necessary for detach, but might
433 * still be useful to collect device information.
434 */
435 flocs[ISACF_PORT] = ia.ia_io[0].ir_addr;
436 flocs[ISACF_SIZE] = ia.ia_io[0].ir_size;
437 flocs[ISACF_IOMEM] = ia.ia_iomem[0].ir_addr;
438 flocs[ISACF_IOSIZ] = ia.ia_iomem[0].ir_size;
439 flocs[ISACF_IRQ] = ia.ia_irq[0].ir_irq;
440 flocs[ISACF_DRQ] = ia.ia_drq[0].ir_drq;
441 flocs[ISACF_DRQ2] = ia.ia_drq[1].ir_drq;
442 config_attach_loc(parent, cf, flocs, &ia, isaprint);
443 tryagain = (cf->cf_fstate == FSTATE_STAR);
444 }
445 } while (tryagain);
446
447 return (0);
448 }
449
450 const char *
451 isa_intr_typename(int type)
452 {
453
454 switch (type) {
455 case IST_NONE:
456 return ("none");
457 case IST_PULSE:
458 return ("pulsed");
459 case IST_EDGE:
460 return ("edge-triggered");
461 case IST_LEVEL:
462 return ("level-triggered");
463 default:
464 panic("isa_intr_typename: invalid type %d", type);
465 }
466 }
467
468 int
469 isa_get_slotcount(void)
470 {
471
472 return isa_slotcount;
473 }
474
475 void
476 isa_set_slotcount(int arg)
477 {
478
479 isa_slotcount = arg;
480 }
481