uba.c revision 1.73.8.1 1 1.73.8.1 yamt /* $NetBSD: uba.c,v 1.73.8.1 2006/04/01 12:07:22 yamt Exp $ */
2 1.67 agc /*
3 1.67 agc * Copyright (c) 1982, 1986 The Regents of the University of California.
4 1.67 agc * All rights reserved.
5 1.67 agc *
6 1.67 agc * Redistribution and use in source and binary forms, with or without
7 1.67 agc * modification, are permitted provided that the following conditions
8 1.67 agc * are met:
9 1.67 agc * 1. Redistributions of source code must retain the above copyright
10 1.67 agc * notice, this list of conditions and the following disclaimer.
11 1.67 agc * 2. Redistributions in binary form must reproduce the above copyright
12 1.67 agc * notice, this list of conditions and the following disclaimer in the
13 1.67 agc * documentation and/or other materials provided with the distribution.
14 1.67 agc * 3. Neither the name of the University nor the names of its contributors
15 1.67 agc * may be used to endorse or promote products derived from this software
16 1.67 agc * without specific prior written permission.
17 1.67 agc *
18 1.67 agc * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19 1.67 agc * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 1.67 agc * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 1.67 agc * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22 1.67 agc * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 1.67 agc * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 1.67 agc * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 1.67 agc * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 1.67 agc * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 1.67 agc * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 1.67 agc * SUCH DAMAGE.
29 1.67 agc *
30 1.67 agc * @(#)uba.c 7.10 (Berkeley) 12/16/90
31 1.67 agc * @(#)autoconf.c 7.20 (Berkeley) 5/9/91
32 1.67 agc */
33 1.67 agc
34 1.1 ragge /*
35 1.26 ragge * Copyright (c) 1996 Jonathan Stone.
36 1.26 ragge * Copyright (c) 1994, 1996 Ludd, University of Lule}, Sweden.
37 1.1 ragge * All rights reserved.
38 1.1 ragge *
39 1.1 ragge * Redistribution and use in source and binary forms, with or without
40 1.1 ragge * modification, are permitted provided that the following conditions
41 1.1 ragge * are met:
42 1.1 ragge * 1. Redistributions of source code must retain the above copyright
43 1.1 ragge * notice, this list of conditions and the following disclaimer.
44 1.1 ragge * 2. Redistributions in binary form must reproduce the above copyright
45 1.1 ragge * notice, this list of conditions and the following disclaimer in the
46 1.1 ragge * documentation and/or other materials provided with the distribution.
47 1.1 ragge * 3. All advertising materials mentioning features or use of this software
48 1.1 ragge * must display the following acknowledgement:
49 1.1 ragge * This product includes software developed by the University of
50 1.1 ragge * California, Berkeley and its contributors.
51 1.1 ragge * 4. Neither the name of the University nor the names of its contributors
52 1.1 ragge * may be used to endorse or promote products derived from this software
53 1.1 ragge * without specific prior written permission.
54 1.1 ragge *
55 1.1 ragge * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
56 1.1 ragge * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
57 1.1 ragge * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
58 1.1 ragge * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
59 1.1 ragge * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
60 1.1 ragge * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
61 1.1 ragge * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
62 1.1 ragge * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
63 1.1 ragge * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
64 1.1 ragge * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
65 1.1 ragge * SUCH DAMAGE.
66 1.1 ragge *
67 1.3 cgd * @(#)uba.c 7.10 (Berkeley) 12/16/90
68 1.25 ragge * @(#)autoconf.c 7.20 (Berkeley) 5/9/91
69 1.1 ragge */
70 1.58 lukem
71 1.58 lukem #include <sys/cdefs.h>
72 1.73.8.1 yamt __KERNEL_RCSID(0, "$NetBSD: uba.c,v 1.73.8.1 2006/04/01 12:07:22 yamt Exp $");
73 1.1 ragge
74 1.13 mycroft #include <sys/param.h>
75 1.13 mycroft #include <sys/time.h>
76 1.13 mycroft #include <sys/systm.h>
77 1.13 mycroft #include <sys/buf.h>
78 1.13 mycroft #include <sys/proc.h>
79 1.13 mycroft #include <sys/user.h>
80 1.13 mycroft #include <sys/conf.h>
81 1.13 mycroft #include <sys/kernel.h>
82 1.13 mycroft #include <sys/malloc.h>
83 1.13 mycroft #include <sys/device.h>
84 1.1 ragge
85 1.55 mrg #include <uvm/uvm_extern.h>
86 1.9 ragge
87 1.43 ragge #include <machine/bus.h>
88 1.43 ragge #include <machine/scb.h>
89 1.13 mycroft #include <machine/cpu.h>
90 1.9 ragge
91 1.51 ragge #include <dev/qbus/ubareg.h>
92 1.47 ragge #include <dev/qbus/ubavar.h>
93 1.1 ragge
94 1.47 ragge #include "ioconf.h"
95 1.70 chs #include "locators.h"
96 1.26 ragge
97 1.69 drochner static int ubasearch (struct device *, struct cfdata *,
98 1.72 drochner const int *, void *);
99 1.52 matt static int ubaprint (void *, const char *);
100 1.26 ragge
101 1.1 ragge /*
102 1.47 ragge * If we failed to allocate uba resources, put us on a queue to wait
103 1.47 ragge * until there is available resources. Resources to compete about
104 1.47 ragge * are map registers and BDPs. This is normally only a problem on
105 1.47 ragge * Unibus systems, Qbus systems have more map registers than usable.
106 1.1 ragge */
107 1.47 ragge void
108 1.52 matt uba_enqueue(struct uba_unit *uu)
109 1.1 ragge {
110 1.47 ragge struct uba_softc *uh;
111 1.47 ragge int s;
112 1.1 ragge
113 1.73.8.1 yamt uh = (void *)device_parent((struct device *)(uu->uu_softc));
114 1.26 ragge
115 1.56 thorpej s = spluba();
116 1.26 ragge SIMPLEQ_INSERT_TAIL(&uh->uh_resq, uu, uu_resq);
117 1.1 ragge splx(s);
118 1.1 ragge }
119 1.1 ragge
120 1.1 ragge /*
121 1.47 ragge * When a routine that uses resources is finished, the next device
122 1.47 ragge * in queue for map registers etc is called. If it succeeds to get
123 1.47 ragge * resources, call next, and next, and next...
124 1.56 thorpej * This routine must be called at spluba.
125 1.1 ragge */
126 1.16 ragge void
127 1.52 matt uba_done(struct uba_softc *uh)
128 1.1 ragge {
129 1.26 ragge struct uba_unit *uu;
130 1.71 simonb
131 1.47 ragge while ((uu = SIMPLEQ_FIRST(&uh->uh_resq))) {
132 1.60 lukem SIMPLEQ_REMOVE_HEAD(&uh->uh_resq, uu_resq);
133 1.47 ragge if ((*uu->uu_ready)(uu) == 0) {
134 1.47 ragge SIMPLEQ_INSERT_HEAD(&uh->uh_resq, uu, uu_resq);
135 1.26 ragge break;
136 1.47 ragge }
137 1.1 ragge }
138 1.1 ragge }
139 1.1 ragge
140 1.1 ragge /*
141 1.51 ragge * Each device that needs some handling if an ubareset occurs must
142 1.51 ragge * register for reset first through this routine.
143 1.51 ragge */
144 1.51 ragge void
145 1.51 ragge uba_reset_establish(void (*reset)(struct device *), struct device *dev)
146 1.51 ragge {
147 1.73.8.1 yamt struct uba_softc *uh = (void *)device_parent(dev);
148 1.51 ragge struct uba_reset *ur;
149 1.51 ragge
150 1.51 ragge ur = malloc(sizeof(struct uba_reset), M_DEVBUF, M_NOWAIT);
151 1.57 ragge if (ur == NULL)
152 1.57 ragge panic("uba_reset_establish");
153 1.51 ragge ur->ur_dev = dev;
154 1.51 ragge ur->ur_reset = reset;
155 1.51 ragge
156 1.51 ragge SIMPLEQ_INSERT_TAIL(&uh->uh_resetq, ur, ur_resetq);
157 1.57 ragge }
158 1.57 ragge
159 1.57 ragge /*
160 1.57 ragge * Allocate a bunch of map registers and map them to the given address.
161 1.57 ragge */
162 1.57 ragge int
163 1.57 ragge uballoc(struct uba_softc *uh, struct ubinfo *ui, int flags)
164 1.57 ragge {
165 1.57 ragge int waitok = (flags & UBA_CANTWAIT) == 0;
166 1.57 ragge int error;
167 1.57 ragge
168 1.57 ragge if ((error = bus_dmamap_create(uh->uh_dmat, ui->ui_size, 1,
169 1.57 ragge ui->ui_size, 0, (waitok ? BUS_DMA_WAITOK : BUS_DMA_NOWAIT),
170 1.57 ragge &ui->ui_dmam)))
171 1.57 ragge return error;
172 1.57 ragge
173 1.57 ragge if ((error = bus_dmamap_load(uh->uh_dmat, ui->ui_dmam, ui->ui_vaddr,
174 1.57 ragge ui->ui_size, NULL, (waitok ? BUS_DMA_WAITOK : BUS_DMA_NOWAIT)))) {
175 1.57 ragge bus_dmamap_destroy(uh->uh_dmat, ui->ui_dmam);
176 1.57 ragge return error;
177 1.57 ragge }
178 1.57 ragge ui->ui_baddr = ui->ui_dmam->dm_segs[0].ds_addr;
179 1.57 ragge return 0;
180 1.57 ragge }
181 1.57 ragge
182 1.57 ragge /*
183 1.57 ragge * Allocate DMA-able memory and map it on the unibus.
184 1.57 ragge */
185 1.57 ragge int
186 1.57 ragge ubmemalloc(struct uba_softc *uh, struct ubinfo *ui, int flags)
187 1.57 ragge {
188 1.57 ragge int waitok = (flags & UBA_CANTWAIT ? BUS_DMA_NOWAIT : BUS_DMA_WAITOK);
189 1.57 ragge int error;
190 1.57 ragge
191 1.65 thorpej if ((error = bus_dmamem_alloc(uh->uh_dmat, ui->ui_size, PAGE_SIZE, 0,
192 1.57 ragge &ui->ui_seg, 1, &ui->ui_rseg, waitok)))
193 1.57 ragge return error;
194 1.57 ragge if ((error = bus_dmamem_map(uh->uh_dmat, &ui->ui_seg, ui->ui_rseg,
195 1.57 ragge ui->ui_size, &ui->ui_vaddr, waitok|BUS_DMA_COHERENT))) {
196 1.57 ragge bus_dmamem_free(uh->uh_dmat, &ui->ui_seg, ui->ui_rseg);
197 1.57 ragge return error;
198 1.57 ragge }
199 1.57 ragge if ((error = uballoc(uh, ui, flags))) {
200 1.57 ragge bus_dmamem_unmap(uh->uh_dmat, ui->ui_vaddr, ui->ui_size);
201 1.57 ragge bus_dmamem_free(uh->uh_dmat, &ui->ui_seg, ui->ui_rseg);
202 1.57 ragge }
203 1.57 ragge return error;
204 1.57 ragge }
205 1.57 ragge
206 1.57 ragge void
207 1.57 ragge ubfree(struct uba_softc *uh, struct ubinfo *ui)
208 1.57 ragge {
209 1.57 ragge bus_dmamap_unload(uh->uh_dmat, ui->ui_dmam);
210 1.57 ragge bus_dmamap_destroy(uh->uh_dmat, ui->ui_dmam);
211 1.57 ragge }
212 1.57 ragge
213 1.57 ragge void
214 1.57 ragge ubmemfree(struct uba_softc *uh, struct ubinfo *ui)
215 1.57 ragge {
216 1.57 ragge bus_dmamem_unmap(uh->uh_dmat, ui->ui_vaddr, ui->ui_size);
217 1.57 ragge bus_dmamem_free(uh->uh_dmat, &ui->ui_seg, ui->ui_rseg);
218 1.57 ragge ubfree(uh, ui);
219 1.51 ragge }
220 1.51 ragge
221 1.51 ragge /*
222 1.25 ragge * Generate a reset on uba number uban. Then
223 1.21 ragge * call each device that asked to be called during attach,
224 1.1 ragge * giving it a chance to clean up so as to be able to continue.
225 1.1 ragge */
226 1.21 ragge void
227 1.51 ragge ubareset(struct uba_softc *uh)
228 1.1 ragge {
229 1.51 ragge struct uba_reset *ur;
230 1.51 ragge int s;
231 1.1 ragge
232 1.56 thorpej s = spluba();
233 1.26 ragge SIMPLEQ_INIT(&uh->uh_resq);
234 1.29 christos printf("%s: reset", uh->uh_dev.dv_xname);
235 1.26 ragge (*uh->uh_ubainit)(uh);
236 1.26 ragge
237 1.51 ragge ur = SIMPLEQ_FIRST(&uh->uh_resetq);
238 1.51 ragge if (ur) do {
239 1.51 ragge printf(" %s", ur->ur_dev->dv_xname);
240 1.51 ragge (*ur->ur_reset)(ur->ur_dev);
241 1.51 ragge } while ((ur = SIMPLEQ_NEXT(ur, ur_resetq)));
242 1.51 ragge
243 1.29 christos printf("\n");
244 1.1 ragge splx(s);
245 1.1 ragge }
246 1.1 ragge
247 1.16 ragge /*
248 1.47 ragge * The common attach routine:
249 1.16 ragge * Calls the scan routine to search for uba devices.
250 1.16 ragge */
251 1.5 ragge void
252 1.52 matt uba_attach(struct uba_softc *sc, paddr_t iopagephys)
253 1.5 ragge {
254 1.5 ragge
255 1.9 ragge /*
256 1.26 ragge * Set last free interrupt vector for devices with
257 1.26 ragge * programmable interrupt vectors. Use is to decrement
258 1.26 ragge * this number and use result as interrupt vector.
259 1.9 ragge */
260 1.26 ragge sc->uh_lastiv = 0x200;
261 1.26 ragge SIMPLEQ_INIT(&sc->uh_resq);
262 1.51 ragge SIMPLEQ_INIT(&sc->uh_resetq);
263 1.26 ragge
264 1.9 ragge /*
265 1.43 ragge * Allocate place for unibus I/O space in virtual space.
266 1.9 ragge */
267 1.44 ragge if (bus_space_map(sc->uh_iot, iopagephys, UBAIOSIZE, 0, &sc->uh_ioh))
268 1.43 ragge return;
269 1.9 ragge
270 1.68 ragge /*
271 1.68 ragge * Keep track of which addressed devices are found.
272 1.68 ragge */
273 1.68 ragge sc->uh_used = malloc(UBAIOSIZE, M_TEMP, M_WAITOK);
274 1.68 ragge memset(sc->uh_used, 0, UBAIOSIZE);
275 1.68 ragge
276 1.26 ragge if (sc->uh_beforescan)
277 1.26 ragge (*sc->uh_beforescan)(sc);
278 1.10 ragge /*
279 1.10 ragge * Now start searching for devices.
280 1.10 ragge */
281 1.69 drochner config_search_ia(ubasearch,(struct device *)sc, "uba", NULL);
282 1.10 ragge
283 1.26 ragge if (sc->uh_afterscan)
284 1.26 ragge (*sc->uh_afterscan)(sc);
285 1.68 ragge
286 1.68 ragge free(sc->uh_used, M_TEMP);
287 1.5 ragge }
288 1.5 ragge
289 1.32 ragge int
290 1.69 drochner ubasearch(struct device *parent, struct cfdata *cf,
291 1.72 drochner const int *ldesc, void *aux)
292 1.10 ragge {
293 1.10 ragge struct uba_softc *sc = (struct uba_softc *)parent;
294 1.10 ragge struct uba_attach_args ua;
295 1.70 chs int i, csr, vec, br;
296 1.10 ragge
297 1.70 chs csr = cf->cf_loc[UBACF_CSR];
298 1.70 chs if (sc->uh_used[ubdevreg(csr)])
299 1.68 ragge return 0; /* something are already at this address */
300 1.68 ragge
301 1.70 chs ua.ua_ioh = ubdevreg(csr) + sc->uh_ioh;
302 1.44 ragge ua.ua_iot = sc->uh_iot;
303 1.47 ragge ua.ua_dmat = sc->uh_dmat;
304 1.10 ragge
305 1.44 ragge if (badaddr((caddr_t)ua.ua_ioh, 2) ||
306 1.43 ragge (sc->uh_errchk ? (*sc->uh_errchk)(sc):0))
307 1.10 ragge goto forgetit;
308 1.10 ragge
309 1.40 ragge scb_vecref(0, 0); /* Clear vector ref */
310 1.63 thorpej i = config_match(parent, cf, &ua);
311 1.10 ragge
312 1.26 ragge if (sc->uh_errchk)
313 1.26 ragge if ((*sc->uh_errchk)(sc))
314 1.26 ragge goto forgetit;
315 1.10 ragge if (i == 0)
316 1.10 ragge goto forgetit;
317 1.10 ragge
318 1.40 ragge i = scb_vecref(&vec, &br);
319 1.40 ragge if (i == 0)
320 1.40 ragge goto fail;
321 1.40 ragge if (vec == 0)
322 1.10 ragge goto fail;
323 1.49 matt
324 1.40 ragge ua.ua_br = br;
325 1.40 ragge ua.ua_cvec = vec;
326 1.70 chs ua.ua_iaddr = csr;
327 1.53 matt ua.ua_evcnt = NULL;
328 1.68 ragge
329 1.70 chs sc->uh_used[ubdevreg(csr)] = 1;
330 1.10 ragge
331 1.32 ragge config_attach(parent, cf, &ua, ubaprint);
332 1.32 ragge return 0;
333 1.10 ragge
334 1.10 ragge fail:
335 1.40 ragge printf("%s%d at %s csr %o %s\n",
336 1.62 thorpej cf->cf_name, cf->cf_unit, parent->dv_xname,
337 1.70 chs csr, (i ? "zero vector" : "didn't interrupt"));
338 1.10 ragge
339 1.10 ragge forgetit:
340 1.32 ragge return 0;
341 1.10 ragge }
342 1.10 ragge
343 1.16 ragge /*
344 1.16 ragge * Print out some interesting info common to all unibus devices.
345 1.16 ragge */
346 1.10 ragge int
347 1.52 matt ubaprint(void *aux, const char *uba)
348 1.10 ragge {
349 1.10 ragge struct uba_attach_args *ua = aux;
350 1.10 ragge
351 1.64 thorpej aprint_normal(" csr %o vec %o ipl %x", ua->ua_iaddr,
352 1.40 ragge ua->ua_cvec & 511, ua->ua_br);
353 1.10 ragge return UNCONF;
354 1.49 matt }
355 1.49 matt
356 1.49 matt /*
357 1.49 matt * Move to machdep eventually
358 1.49 matt */
359 1.49 matt void
360 1.52 matt uba_intr_establish(void *icookie, int vec, void (*ifunc)(void *iarg),
361 1.52 matt void *iarg, struct evcnt *ev)
362 1.49 matt {
363 1.52 matt scb_vecalloc(vec, ifunc, iarg, SCB_ISTACK, ev);
364 1.10 ragge }
365