isa.c revision 1.58 1 /* $NetBSD: isa.c,v 1.58 1994/10/27 04:17:45 cgd Exp $ */
2
3 /*-
4 * Copyright (c) 1993, 1994 Charles Hannum.
5 * Copyright (c) 1991 The Regents of the University of California.
6 * All rights reserved.
7 *
8 * This code is derived from software contributed to Berkeley by
9 * William Jolitz.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by the University of
22 * California, Berkeley and its contributors.
23 * 4. Neither the name of the University nor the names of its contributors
24 * may be used to endorse or promote products derived from this software
25 * without specific prior written permission.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 * SUCH DAMAGE.
38 *
39 * @(#)isa.c 7.2 (Berkeley) 5/13/91
40 */
41
42 /*
43 * Code to manage AT bus
44 */
45
46 #include <sys/param.h>
47 #include <sys/systm.h>
48 #include <sys/kernel.h>
49 #include <sys/conf.h>
50 #include <sys/malloc.h>
51 #include <sys/device.h>
52
53 #include <machine/pio.h>
54
55 #include <i386/isa/isareg.h>
56 #include <i386/isa/isa_device.h>
57 #include <i386/isa/isavar.h>
58 #include <i386/isa/ic/i8042.h>
59
60 /* sorry, has to be here, no place else really suitable */
61 #include <machine/pc/display.h>
62 u_short *Crtat = (u_short *)MONO_BUF;
63
64 /*
65 * Configure all ISA devices
66 *
67 * XXX This code is a hack. It wants to be new config, but can't be until the
68 * interrupt system is redone. For now, we do some gross hacks to make it look
69 * 99% like new config.
70 */
71 static char *msgs[3] = { "", " not configured\n", " unsupported\n" };
72
73 struct cfdata *
74 config_search(fn, parent, aux)
75 cfmatch_t fn;
76 struct device *parent;
77 void *aux;
78 {
79 struct cfdata *cf = 0;
80 struct device *dv = 0;
81 size_t devsize;
82 struct cfdriver *cd;
83 struct isa_device *id,
84 *idp = parent ? (void *)parent->dv_cfdata->cf_loc : 0;
85
86 for (id = isa_devtab; id->id_driver; id++) {
87 if (id->id_state == FSTATE_FOUND)
88 continue;
89 if (id->id_parent != idp)
90 continue;
91 cd = id->id_driver;
92 if (id->id_unit < cd->cd_ndevs) {
93 if (cd->cd_devs[id->id_unit] != 0)
94 continue;
95 } else {
96 int old = cd->cd_ndevs, new;
97 void **nsp;
98
99 if (old == 0)
100 new = MINALLOCSIZE / sizeof(void *);
101 else
102 new = old * 2;
103 while (new <= id->id_unit)
104 new *= 2;
105 cd->cd_ndevs = new;
106 nsp = malloc(new * sizeof(void *), M_DEVBUF, M_NOWAIT);
107 if (nsp == 0)
108 panic("config_search: %sing dev array",
109 old != 0 ? "expand" : "creat");
110 bzero(nsp + old, (new - old) * sizeof(void *));
111 if (old != 0) {
112 bcopy(cd->cd_devs, nsp, old * sizeof(void *));
113 free(cd->cd_devs, M_DEVBUF);
114 }
115 cd->cd_devs = nsp;
116 }
117 if (!cf) {
118 cf = malloc(sizeof(struct cfdata), M_DEVBUF, M_NOWAIT);
119 if (!cf)
120 panic("config_search: creating cfdata");
121 }
122 cf->cf_driver = cd;
123 cf->cf_unit = id->id_unit;
124 cf->cf_fstate = 0;
125 cf->cf_loc = (void *)id;
126 cf->cf_flags = id->id_flags;
127 cf->cf_parents = 0;
128 cf->cf_ivstubs = 0;
129 if (dv && devsize != cd->cd_devsize) {
130 free(dv, M_DEVBUF);
131 dv = 0;
132 }
133 if (!dv) {
134 devsize = cd->cd_devsize;
135 dv = malloc(devsize, M_DEVBUF, M_NOWAIT);
136 if (!dv)
137 panic("config_search: creating softc");
138 }
139 bzero(dv, cd->cd_devsize);
140 dv->dv_class = cd->cd_class;
141 dv->dv_cfdata = cf;
142 dv->dv_unit = id->id_unit;
143 sprintf(dv->dv_xname, "%s%d", cd->cd_name, id->id_unit);
144 dv->dv_parent = parent;
145 cd->cd_devs[id->id_unit] = dv;
146 if (fn) {
147 if ((*fn)(parent, dv, aux))
148 return cf;
149 } else {
150 if ((*cd->cd_match)(parent, dv, aux))
151 return cf;
152 }
153 cd->cd_devs[id->id_unit] = 0;
154 }
155 if (cf)
156 free(cf, M_DEVBUF);
157 if (dv)
158 free(dv, M_DEVBUF);
159 return 0;
160 }
161
162 void
163 config_attach(parent, cf, aux, print)
164 struct device *parent;
165 struct cfdata *cf;
166 void *aux;
167 cfprint_t print;
168 {
169 struct isa_device *id = (void *)cf->cf_loc;
170 struct cfdriver *cd = cf->cf_driver;
171 struct device *dv = cd->cd_devs[id->id_unit];
172
173 cf->cf_fstate = id->id_state = FSTATE_FOUND;
174 printf("%s at %s", dv->dv_xname, parent ? parent->dv_xname : "isa0");
175 if (print)
176 (void) (*print)(aux, (char *)0);
177 (*cd->cd_attach)(parent, dv, aux);
178 }
179
180 int
181 config_found(parent, aux, print)
182 struct device *parent;
183 void *aux;
184 cfprint_t print;
185 {
186 struct cfdata *cf;
187
188 if ((cf = config_search((cfmatch_t)NULL, parent, aux)) != NULL) {
189 config_attach(parent, cf, aux, print);
190 return 1;
191 }
192 if (print)
193 printf(msgs[(*print)(aux, parent->dv_xname)]);
194 return 0;
195 }
196
197 int
198 isaprint(aux, isa)
199 void *aux;
200 char *isa;
201 {
202 struct isa_attach_args *ia = aux;
203
204 if (ia->ia_iosize)
205 printf(" port 0x%x", ia->ia_iobase);
206 if (ia->ia_iosize > 1)
207 printf("-0x%x", ia->ia_iobase + ia->ia_iosize - 1);
208 if (ia->ia_msize)
209 printf(" iomem 0x%x", ia->ia_maddr - atdevbase + 0xa0000);
210 if (ia->ia_msize > 1)
211 printf("-0x%x",
212 ia->ia_maddr - atdevbase + 0xa0000 + ia->ia_msize - 1);
213 if (ia->ia_irq)
214 printf(" irq %d", ffs(ia->ia_irq) - 1);
215 if (ia->ia_drq != (u_short)-1)
216 printf(" drq %d", ia->ia_drq);
217 /* XXXX print flags */
218 return QUIET;
219 }
220
221 int
222 isasubmatch(parent, self, aux)
223 struct device *parent, *self;
224 void *aux;
225 {
226 struct isa_device *id = (void *)self->dv_cfdata->cf_loc;
227 struct isa_attach_args ia;
228
229 ia.ia_iobase = id->id_iobase;
230 ia.ia_iosize = 0x666;
231 ia.ia_irq = id->id_irq;
232 ia.ia_drq = id->id_drq;
233 ia.ia_maddr = id->id_maddr - 0xa0000 + atdevbase;
234 ia.ia_msize = id->id_msize;
235
236 if (!(*id->id_driver->cd_match)(parent, self, &ia)) {
237 /*
238 * If we don't do this, isa_configure() will repeatedly try to
239 * probe devices that weren't found. But we need to be careful
240 * to do it only for the ISA bus, or we would cause things like
241 * `com0 at ast? slave ?' to not probe on the second ast.
242 */
243 if (!parent)
244 id->id_state = FSTATE_FOUND;
245
246 return 0;
247 }
248
249 config_attach(parent, self->dv_cfdata, &ia, isaprint);
250
251 return 1;
252 }
253
254 void
255 isa_configure()
256 {
257
258 startrtclock();
259
260 while (config_search(isasubmatch, NULL, NULL));
261
262 printf("biomask %x netmask %x ttymask %x\n",
263 (u_short)imask[IPL_BIO], (u_short)imask[IPL_NET],
264 (u_short)imask[IPL_TTY]);
265
266 spl0();
267 }
268