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