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