xref: /netbsd-src/sys/dev/isa/isa.c (revision c7fb772b85b2b5d4cfb282f868f454b4701534fd)
1 /*	$NetBSD: isa.c,v 1.140 2021/08/07 16:19:12 thorpej 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.140 2021/08/07 16:19:12 thorpej 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	isadetach(device_t, int);
60 int	isarescan(device_t, const char *, const int *);
61 void	isachilddetached(device_t, device_t);
62 int	isaprint(void *, const char *);
63 
64 CFATTACH_DECL2_NEW(isa, sizeof(struct isa_softc),
65     isamatch, isaattach, isadetach, NULL, isarescan, isachilddetached);
66 
67 void	isa_attach_knowndevs(struct isa_softc *);
68 void	isa_free_knowndevs(struct isa_softc *);
69 
70 int	isasubmatch(device_t, cfdata_t, const int *, void *);
71 int	isasearch(device_t, cfdata_t, const int *, void *);
72 
73 static int	isa_slotcount = -1;	/* -1 == don't know how many */
74 
75 int
isamatch(device_t parent,cfdata_t cf,void * aux)76 isamatch(device_t parent, cfdata_t cf, void *aux)
77 {
78 	/* XXX check other indicators */
79 
80 	return (1);
81 }
82 
83 void
isaattach(device_t parent,device_t self,void * aux)84 isaattach(device_t parent, device_t self, void *aux)
85 {
86 	struct isa_softc *sc = device_private(self);
87 	struct isabus_attach_args *iba = aux;
88 	static const int wildcard[ISACF_NLOCS] = {
89 		[ISACF_PORT]  = ISACF_PORT_DEFAULT,
90 		[ISACF_SIZE]  = ISACF_SIZE_DEFAULT,
91 		[ISACF_IOMEM] = ISACF_IOMEM_DEFAULT,
92 		[ISACF_IOSIZ] = ISACF_IOSIZ_DEFAULT,
93 		[ISACF_IRQ]   = ISACF_IRQ_DEFAULT,
94 		[ISACF_DRQ]   = ISACF_DRQ_DEFAULT,
95 		[ISACF_DRQ2]  = ISACF_DRQ2_DEFAULT,
96 	};
97 
98 	TAILQ_INIT(&sc->sc_knowndevs);
99 	sc->sc_dynamicdevs = 0;
100 
101 	sc->sc_dev = self;
102 
103 	isa_attach_hook(parent, self, iba);
104 	aprint_naive("\n");
105 	aprint_normal("\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 indirect-config children. */
139 	isarescan(self, NULL, wildcard);
140 
141 	if (!pmf_device_register(self, NULL, NULL))
142 		aprint_error_dev(self, "couldn't establish power handler\n");
143 }
144 
145 int
isadetach(device_t self,int flags)146 isadetach(device_t self, int flags)
147 {
148 	struct isa_softc *sc = device_private(self);
149 	int rc;
150 
151 	if ((rc = config_detach_children(self, flags)) != 0)
152 		return rc;
153 
154 	pmf_device_deregister(self);
155 
156 	isa_free_knowndevs(sc);
157 
158 #if NISADMA > 0
159 	isa_dmadestroy(sc->sc_ic);
160 #endif
161 	isa_detach_hook(sc->sc_ic, self);
162 
163 	return 0;
164 }
165 
166 int
isarescan(device_t self,const char * ifattr,const int * locators)167 isarescan(device_t self, const char *ifattr, const int *locators)
168 {
169 	prop_dictionary_t dict;
170 	int locs[ISACF_NLOCS];
171 	bool no_legacy_devices = false;
172 
173 	dict = device_properties(self);
174 	if (prop_dictionary_get_bool(dict, "no-legacy-devices",
175 	    &no_legacy_devices) == true) {
176 		aprint_debug_dev(self, "platform reports no legacy devices\n");
177 		return 0;
178 	}
179 
180 	memcpy(locs, locators, sizeof(locs));
181 
182 	/*
183 	 * XXX Bus independent code calling this function does not
184 	 * know the locator default values. It assumes "-1" for now.
185 	 * (should be made available by "config" one day)
186 	 * So fixup where the "-1" is not correct.
187 	 */
188 	if (locs[ISACF_SIZE] == -1)
189 		locs[ISACF_SIZE] = ISACF_SIZE_DEFAULT;
190 	if (locs[ISACF_IOSIZ] == -1)
191 		locs[ISACF_IOSIZ] = ISACF_IOSIZ_DEFAULT;
192 
193 	config_search(self, NULL,
194 	    CFARGS(.search = isasearch,
195 		   .locators = locs));
196 	return (0);
197 }
198 
199 void
isachilddetached(device_t self,device_t child)200 isachilddetached(device_t self, device_t child)
201 {
202 	struct isa_knowndev *ik;
203 	struct isa_softc *sc = device_private(self);
204 
205 	TAILQ_FOREACH(ik, &sc->sc_knowndevs, ik_list) {
206 		if (ik->ik_claimed == child)
207 			ik->ik_claimed = NULL;
208 	}
209 }
210 
211 void
isa_attach_knowndevs(struct isa_softc * sc)212 isa_attach_knowndevs(struct isa_softc *sc)
213 {
214 	struct isa_attach_args ia;
215 	struct isa_knowndev *ik;
216 
217 	if (TAILQ_EMPTY(&sc->sc_knowndevs))
218 		return;
219 
220 	TAILQ_FOREACH(ik, &sc->sc_knowndevs, ik_list) {
221 		if (ik->ik_claimed != NULL)
222 			continue;
223 
224 		ia.ia_iot = sc->sc_iot;
225 		ia.ia_memt = sc->sc_memt;
226 		ia.ia_dmat = sc->sc_dmat;
227 		ia.ia_ic = sc->sc_ic;
228 
229 		ia.ia_pnpname = ik->ik_pnpname;
230 		ia.ia_pnpcompatnames = ik->ik_pnpcompatnames;
231 
232 		ia.ia_io = ik->ik_io;
233 		ia.ia_nio = ik->ik_nio;
234 
235 		ia.ia_iomem = ik->ik_iomem;
236 		ia.ia_niomem = ik->ik_niomem;
237 
238 		ia.ia_irq = ik->ik_irq;
239 		ia.ia_nirq = ik->ik_nirq;
240 
241 		ia.ia_drq = ik->ik_drq;
242 		ia.ia_ndrq = ik->ik_ndrq;
243 
244 		ia.ia_aux = NULL;
245 
246 		/* XXX should setup locator array */
247 
248 		ik->ik_claimed = config_found(sc->sc_dev, &ia, isaprint,
249 		    CFARGS(.submatch = isasubmatch));
250 	}
251 }
252 
253 void
isa_free_knowndevs(struct isa_softc * sc)254 isa_free_knowndevs(struct isa_softc *sc)
255 {
256 	struct isa_knowndev *ik;
257 	struct isa_pnpname *ipn;
258 
259 #define	FREEIT(x)	if (x != NULL) free(x, M_DEVBUF)
260 
261 	while ((ik = TAILQ_FIRST(&sc->sc_knowndevs)) != NULL) {
262 		TAILQ_REMOVE(&sc->sc_knowndevs, ik, ik_list);
263 		FREEIT(ik->ik_pnpname);
264 		while ((ipn = ik->ik_pnpcompatnames) != NULL) {
265 			ik->ik_pnpcompatnames = ipn->ipn_next;
266 			free(ipn->ipn_name, M_DEVBUF);
267 			free(ipn, M_DEVBUF);
268 		}
269 		FREEIT(ik->ik_io);
270 		FREEIT(ik->ik_iomem);
271 		FREEIT(ik->ik_irq);
272 		FREEIT(ik->ik_drq);
273 		free(ik, M_DEVBUF);
274 	}
275 
276 #undef FREEIT
277 }
278 
279 static int
checkattachargs(struct isa_attach_args * ia,const int * loc)280 checkattachargs(struct isa_attach_args *ia, const int *loc)
281 {
282 	int i;
283 
284 	if (ia->ia_nio == 0) {
285 		if (loc[ISACF_PORT] != ISACF_PORT_DEFAULT)
286 			return (0);
287 	} else {
288 		if (loc[ISACF_PORT] != ISACF_PORT_DEFAULT &&
289 		    loc[ISACF_PORT] != ia->ia_io[0].ir_addr)
290 			return (0);
291 	}
292 
293 	if (ia->ia_niomem == 0) {
294 		if (loc[ISACF_IOMEM] != ISACF_IOMEM_DEFAULT)
295 			return (0);
296 	} else {
297 		if (loc[ISACF_IOMEM] != ISACF_IOMEM_DEFAULT &&
298 		    loc[ISACF_IOMEM] != ia->ia_iomem[0].ir_addr)
299 			return (0);
300 	}
301 
302 	if (ia->ia_nirq == 0) {
303 		if (loc[ISACF_IRQ] != ISACF_IRQ_DEFAULT)
304 			return (0);
305 	} else {
306 		if (loc[ISACF_IRQ] != ISACF_IRQ_DEFAULT &&
307 		    loc[ISACF_IRQ] != ia->ia_irq[0].ir_irq)
308 			return (0);
309 	}
310 
311 	if (ia->ia_ndrq == 0) {
312 		if (loc[ISACF_DRQ] != ISACF_DRQ_DEFAULT)
313 			return (0);
314 		if (loc[ISACF_DRQ2] != ISACF_DRQ2_DEFAULT)
315 			return (0);
316 	} else {
317 		for (i = 0; i < 2; i++) {
318 			if (i == ia->ia_ndrq)
319 				break;
320 			if (loc[ISACF_DRQ + i] != ISACF_DRQ_DEFAULT &&
321 			    loc[ISACF_DRQ + i] != ia->ia_drq[i].ir_drq)
322 				return (0);
323 		}
324 		for (; i < 2; i++) {
325 			if (loc[ISACF_DRQ + i] != ISACF_DRQ_DEFAULT)
326 				return (0);
327 		}
328 	}
329 
330 	return (1);
331 }
332 
333 int
isasubmatch(device_t parent,cfdata_t cf,const int * ldesc,void * aux)334 isasubmatch(device_t parent, cfdata_t cf, const int *ldesc, void *aux)
335 {
336 	struct isa_attach_args *ia = aux;
337 
338 	if (!checkattachargs(ia, cf->cf_loc))
339 		return (0);
340 
341 	return (config_match(parent, cf, aux));
342 }
343 
344 int
isaprint(void * aux,const char * isa)345 isaprint(void *aux, const char *isa)
346 {
347 	struct isa_attach_args *ia = aux;
348 	const char *sep;
349 	int i;
350 
351 	/*
352 	 * This block of code only fires if we have a direct-config'd
353 	 * device for which there is no driver match.
354 	 */
355 	if (isa != NULL) {
356 		struct isa_pnpname *ipn;
357 
358 		if (ia->ia_pnpname != NULL)
359 			aprint_normal("%s", ia->ia_pnpname);
360 		if ((ipn = ia->ia_pnpcompatnames) != NULL) {
361 			aprint_normal(" (");	/* ) */
362 			for (sep = ""; ipn != NULL;
363 			     ipn = ipn->ipn_next, sep = " ") {
364 				aprint_normal("%s%s", sep, ipn->ipn_name);
365 			}
366 	/* ( */		aprint_normal(")");
367 		}
368 		aprint_normal(" at %s", isa);
369 	}
370 
371 	if (ia->ia_nio) {
372 		sep = "";
373 		aprint_normal(" port ");
374 		for (i = 0; i < ia->ia_nio; i++) {
375 			if (ia->ia_io[i].ir_size == 0)
376 				continue;
377 			aprint_normal("%s0x%x", sep, ia->ia_io[i].ir_addr);
378 			if (ia->ia_io[i].ir_size > 1)
379 				aprint_normal("-0x%x", ia->ia_io[i].ir_addr +
380 				    ia->ia_io[i].ir_size - 1);
381 			sep = ",";
382 		}
383 	}
384 
385 	if (ia->ia_niomem) {
386 		sep = "";
387 		aprint_normal(" iomem ");
388 		for (i = 0; i < ia->ia_niomem; i++) {
389 			if (ia->ia_iomem[i].ir_size == 0)
390 				continue;
391 			aprint_normal("%s0x%x", sep, ia->ia_iomem[i].ir_addr);
392 			if (ia->ia_iomem[i].ir_size > 1)
393 				aprint_normal("-0x%x", ia->ia_iomem[i].ir_addr +
394 				    ia->ia_iomem[i].ir_size - 1);
395 			sep = ",";
396 		}
397 	}
398 
399 	if (ia->ia_nirq) {
400 		sep = "";
401 		aprint_normal(" irq ");
402 		for (i = 0; i < ia->ia_nirq; i++) {
403 			if (ia->ia_irq[i].ir_irq == ISACF_IRQ_DEFAULT)
404 				continue;
405 			aprint_normal("%s%d", sep, ia->ia_irq[i].ir_irq);
406 			sep = ",";
407 		}
408 	}
409 
410 	if (ia->ia_ndrq) {
411 		sep = "";
412 		aprint_normal(" drq ");
413 		for (i = 0; i < ia->ia_ndrq; i++) {
414 			if (ia->ia_drq[i].ir_drq == ISACF_DRQ_DEFAULT)
415 				continue;
416 			aprint_normal("%s%d", sep, ia->ia_drq[i].ir_drq);
417 			sep = ",";
418 		}
419 	}
420 
421 	return (UNCONF);
422 }
423 
424 int
isasearch(device_t parent,cfdata_t cf,const int * slocs,void * aux)425 isasearch(device_t parent, cfdata_t cf, const int *slocs, void *aux)
426 {
427 	struct isa_io res_io[1];
428 	struct isa_iomem res_mem[1];
429 	struct isa_irq res_irq[1];
430 	struct isa_drq res_drq[2];
431 	struct isa_softc *sc = device_private(parent);
432 	struct isa_attach_args ia;
433 	int flocs[ISACF_NLOCS];
434 	int tryagain;
435 
436 	do {
437 		ia.ia_pnpname = NULL;
438 		ia.ia_pnpcompatnames = NULL;
439 
440 		res_io[0].ir_addr = cf->cf_loc[ISACF_PORT];
441 		res_io[0].ir_size = 0;
442 
443 		res_mem[0].ir_addr = cf->cf_loc[ISACF_IOMEM];
444 		res_mem[0].ir_size = cf->cf_loc[ISACF_IOSIZ];
445 
446 		res_irq[0].ir_irq =
447 		    cf->cf_loc[ISACF_IRQ] == 2 ? 9 : cf->cf_loc[ISACF_IRQ];
448 
449 		res_drq[0].ir_drq = cf->cf_loc[ISACF_DRQ];
450 		res_drq[1].ir_drq = cf->cf_loc[ISACF_DRQ2];
451 
452 		ia.ia_iot = sc->sc_iot;
453 		ia.ia_memt = sc->sc_memt;
454 		ia.ia_dmat = sc->sc_dmat;
455 		ia.ia_ic = sc->sc_ic;
456 
457 		ia.ia_io = res_io;
458 		ia.ia_nio = 1;
459 
460 		ia.ia_iomem = res_mem;
461 		ia.ia_niomem = 1;
462 
463 		ia.ia_irq = res_irq;
464 		ia.ia_nirq = 1;
465 
466 		ia.ia_drq = res_drq;
467 		ia.ia_ndrq = 2;
468 
469 		if (!checkattachargs(&ia, slocs))
470 			return (0);
471 
472 		tryagain = 0;
473 		if (config_probe(parent, cf, &ia)) {
474 			/*
475 			 * This is not necessary for detach, but might
476 			 * still be useful to collect device information.
477 			 */
478 			flocs[ISACF_PORT] = ia.ia_io[0].ir_addr;
479 			flocs[ISACF_SIZE] = ia.ia_io[0].ir_size;
480 			flocs[ISACF_IOMEM] = ia.ia_iomem[0].ir_addr;
481 			flocs[ISACF_IOSIZ] = ia.ia_iomem[0].ir_size;
482 			flocs[ISACF_IRQ] = ia.ia_irq[0].ir_irq;
483 			flocs[ISACF_DRQ] = ia.ia_drq[0].ir_drq;
484 			flocs[ISACF_DRQ2] = ia.ia_drq[1].ir_drq;
485 			config_attach(parent, cf, &ia, isaprint,
486 			    CFARGS(.locators = flocs));
487 			tryagain = (cf->cf_fstate == FSTATE_STAR);
488 		}
489 	} while (tryagain);
490 
491 	return (0);
492 }
493 
494 const char *
isa_intr_typename(int type)495 isa_intr_typename(int type)
496 {
497 
498 	switch (type) {
499 	case IST_NONE:
500 		return ("none");
501 	case IST_PULSE:
502 		return ("pulsed");
503 	case IST_EDGE:
504 		return ("edge-triggered");
505 	case IST_LEVEL:
506 		return ("level-triggered");
507 	default:
508 		panic("isa_intr_typename: invalid type %d", type);
509 	}
510 }
511 
512 int
isa_get_slotcount(void)513 isa_get_slotcount(void)
514 {
515 
516 	return isa_slotcount;
517 }
518 
519 void
isa_set_slotcount(int arg)520 isa_set_slotcount(int arg)
521 {
522 
523 	isa_slotcount = arg;
524 }
525