obio.c revision 1.19 1 1.19 chuck /* $NetBSD: obio.c,v 1.19 1996/01/12 21:44:16 chuck Exp $ */
2 1.1 deraadt
3 1.1 deraadt /*
4 1.1 deraadt * Copyright (c) 1993, 1994 Theo de Raadt
5 1.1 deraadt * All rights reserved.
6 1.1 deraadt *
7 1.1 deraadt * Redistribution and use in source and binary forms, with or without
8 1.1 deraadt * modification, are permitted provided that the following conditions
9 1.1 deraadt * are met:
10 1.1 deraadt * 1. Redistributions of source code must retain the above copyright
11 1.1 deraadt * notice, this list of conditions and the following disclaimer.
12 1.1 deraadt * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 deraadt * notice, this list of conditions and the following disclaimer in the
14 1.1 deraadt * documentation and/or other materials provided with the distribution.
15 1.1 deraadt * 3. All advertising materials mentioning features or use of this software
16 1.1 deraadt * must display the following acknowledgement:
17 1.1 deraadt * This product includes software developed by Theo de Raadt.
18 1.1 deraadt * 4. The name of the author may not be used to endorse or promote products
19 1.1 deraadt * derived from this software without specific prior written permission.
20 1.1 deraadt *
21 1.1 deraadt * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 1.1 deraadt * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 1.1 deraadt * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 1.1 deraadt * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25 1.1 deraadt * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26 1.1 deraadt * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 1.1 deraadt * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 1.1 deraadt * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 1.1 deraadt * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30 1.1 deraadt * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 1.1 deraadt */
32 1.1 deraadt
33 1.1 deraadt #include <sys/param.h>
34 1.1 deraadt #include <sys/device.h>
35 1.1 deraadt #include <sys/malloc.h>
36 1.1 deraadt
37 1.1 deraadt #ifdef DEBUG
38 1.1 deraadt #include <sys/proc.h>
39 1.1 deraadt #include <sys/syslog.h>
40 1.1 deraadt #endif
41 1.1 deraadt
42 1.1 deraadt #include <vm/vm.h>
43 1.1 deraadt
44 1.1 deraadt #include <machine/autoconf.h>
45 1.1 deraadt #include <machine/pmap.h>
46 1.2 deraadt #include <machine/oldmon.h>
47 1.5 deraadt #include <machine/cpu.h>
48 1.2 deraadt #include <machine/ctlreg.h>
49 1.2 deraadt #include <sparc/sparc/asm.h>
50 1.2 deraadt #include <sparc/sparc/vaddrs.h>
51 1.1 deraadt
52 1.4 deraadt struct bus_softc {
53 1.1 deraadt struct device sc_dev; /* base device */
54 1.1 deraadt int nothing;
55 1.1 deraadt };
56 1.1 deraadt
57 1.1 deraadt /* autoconfiguration driver */
58 1.9 deraadt static int busmatch __P((struct device *, void *, void *));
59 1.9 deraadt static void obioattach __P((struct device *, struct device *, void *));
60 1.17 pk #if defined(SUN4)
61 1.9 deraadt static void vmesattach __P((struct device *, struct device *, void *));
62 1.9 deraadt static void vmelattach __P((struct device *, struct device *, void *));
63 1.17 pk static int busattach __P((struct device *, void *, void *, int));
64 1.17 pk void * bus_map __P((struct rom_reg *, int, int));
65 1.17 pk void * bus_tmp __P((void *, int));
66 1.17 pk void bus_untmp __P((void));
67 1.17 pk #endif
68 1.4 deraadt
69 1.4 deraadt struct cfdriver obiocd = { NULL, "obio", busmatch, obioattach,
70 1.4 deraadt DV_DULL, sizeof(struct bus_softc)
71 1.4 deraadt };
72 1.17 pk #if defined(SUN4)
73 1.4 deraadt struct cfdriver vmelcd = { NULL, "vmel", busmatch, vmelattach,
74 1.4 deraadt DV_DULL, sizeof(struct bus_softc)
75 1.1 deraadt };
76 1.4 deraadt struct cfdriver vmescd = { NULL, "vmes", busmatch, vmesattach,
77 1.4 deraadt DV_DULL, sizeof(struct bus_softc)
78 1.4 deraadt };
79 1.17 pk #endif
80 1.4 deraadt
81 1.2 deraadt
82 1.1 deraadt int
83 1.9 deraadt busmatch(parent, vcf, aux)
84 1.1 deraadt struct device *parent;
85 1.9 deraadt void *vcf, *aux;
86 1.1 deraadt {
87 1.9 deraadt struct cfdata *cf = vcf;
88 1.3 deraadt register struct confargs *ca = aux;
89 1.3 deraadt register struct romaux *ra = &ca->ca_ra;
90 1.3 deraadt
91 1.1 deraadt if (cputyp != CPU_SUN4)
92 1.3 deraadt return (0);
93 1.3 deraadt return (strcmp(cf->cf_driver->cd_name, ra->ra_name) == 0);
94 1.1 deraadt }
95 1.1 deraadt
96 1.1 deraadt int
97 1.4 deraadt busprint(args, obio)
98 1.1 deraadt void *args;
99 1.1 deraadt char *obio;
100 1.1 deraadt {
101 1.2 deraadt register struct confargs *ca = args;
102 1.2 deraadt
103 1.2 deraadt if (ca->ca_ra.ra_name == NULL)
104 1.2 deraadt ca->ca_ra.ra_name = "<unknown>";
105 1.2 deraadt if (obio)
106 1.3 deraadt printf("[%s at %s]", ca->ca_ra.ra_name, obio);
107 1.19 chuck printf(" addr 0x%x", ca->ca_ra.ra_paddr);
108 1.7 deraadt if (ca->ca_ra.ra_intr[0].int_vec != -1)
109 1.7 deraadt printf(" vec 0x%x", ca->ca_ra.ra_intr[0].int_vec);
110 1.1 deraadt return (UNCONF);
111 1.1 deraadt }
112 1.1 deraadt
113 1.14 pk
114 1.14 pk int
115 1.14 pk busattach(parent, child, args, bustype)
116 1.14 pk struct device *parent;
117 1.14 pk void *args, *child;
118 1.4 deraadt int bustype;
119 1.1 deraadt {
120 1.14 pk struct cfdata *cf = child;
121 1.14 pk register struct bus_softc *sc = (struct bus_softc *)parent;
122 1.3 deraadt register struct confargs *ca = args;
123 1.3 deraadt struct confargs oca;
124 1.4 deraadt caddr_t tmp;
125 1.1 deraadt
126 1.14 pk if (bustype == BUS_OBIO && cputyp == CPU_SUN4) {
127 1.14 pk /*
128 1.14 pk * On the 4/100 obio addresses must be mapped at
129 1.14 pk * 0x0YYYYYYY, but alias higher up (we avoid the
130 1.14 pk * alias condition because it causes pmap difficulties)
131 1.14 pk * XXX: We also assume that 4/[23]00 obio addresses
132 1.14 pk * must be 0xZYYYYYYY, where (Z != 0)
133 1.14 pk */
134 1.14 pk if (cpumod==SUN4_100 && (cf->cf_loc[0] & 0xf0000000))
135 1.14 pk return 0;
136 1.14 pk if (cpumod!=SUN4_100 && !(cf->cf_loc[0] & 0xf0000000))
137 1.14 pk return 0;
138 1.14 pk }
139 1.14 pk
140 1.14 pk if (parent->dv_cfdata->cf_driver->cd_indirect) {
141 1.14 pk printf(" indirect devices not supported\n");
142 1.14 pk return 0;
143 1.1 deraadt }
144 1.1 deraadt
145 1.14 pk oca.ca_ra.ra_iospace = -1;
146 1.14 pk oca.ca_ra.ra_paddr = (void *)cf->cf_loc[0];
147 1.14 pk oca.ca_ra.ra_len = 0;
148 1.14 pk oca.ca_ra.ra_nreg = 1;
149 1.14 pk tmp = NULL;
150 1.14 pk if (oca.ca_ra.ra_paddr)
151 1.14 pk tmp = bus_tmp(oca.ca_ra.ra_paddr,
152 1.14 pk bustype);
153 1.14 pk oca.ca_ra.ra_vaddr = tmp;
154 1.14 pk oca.ca_ra.ra_intr[0].int_pri = cf->cf_loc[1];
155 1.14 pk if (bustype == BUS_VME16 || bustype == BUS_VME32)
156 1.14 pk oca.ca_ra.ra_intr[0].int_vec = cf->cf_loc[2];
157 1.14 pk else
158 1.14 pk oca.ca_ra.ra_intr[0].int_vec = -1;
159 1.14 pk oca.ca_ra.ra_nintr = 1;
160 1.14 pk oca.ca_ra.ra_name = cf->cf_driver->cd_name;
161 1.15 pk if (ca->ca_ra.ra_bp != NULL &&
162 1.16 pk ((bustype == BUS_VME16 && strcmp(ca->ca_ra.ra_bp->name,"vmes") ==0) ||
163 1.16 pk (bustype == BUS_VME32 && strcmp(ca->ca_ra.ra_bp->name,"vmel") ==0) ||
164 1.16 pk (bustype == BUS_OBIO && strcmp(ca->ca_ra.ra_bp->name,"obio") == 0)))
165 1.15 pk oca.ca_ra.ra_bp = ca->ca_ra.ra_bp + 1;
166 1.15 pk else
167 1.15 pk oca.ca_ra.ra_bp = NULL;
168 1.14 pk oca.ca_bustype = bustype;
169 1.14 pk
170 1.14 pk if ((*cf->cf_driver->cd_match)(parent, cf, &oca) == 0)
171 1.14 pk return 0;
172 1.14 pk
173 1.14 pk /*
174 1.19 chuck * check if XXmatch routine replaced the temporary mapping with
175 1.19 chuck * a real mapping. If not, then make sure we don't pass the
176 1.19 chuck * tmp mapping to the attach routine.
177 1.14 pk */
178 1.19 chuck if (oca.ca_ra.ra_vaddr == tmp)
179 1.19 chuck oca.ca_ra.ra_vaddr = NULL; /* wipe out tmp address */
180 1.14 pk /*
181 1.19 chuck * the match routine will set "ra_len" if it wants us to
182 1.19 chuck * establish a mapping for it.
183 1.14 pk * (which won't be seen on future XXmatch calls,
184 1.14 pk * so not as useful as it seems.)
185 1.14 pk */
186 1.19 chuck if (oca.ca_ra.ra_len)
187 1.14 pk oca.ca_ra.ra_vaddr =
188 1.17 pk bus_map(oca.ca_ra.ra_reg,
189 1.14 pk oca.ca_ra.ra_len, oca.ca_bustype);
190 1.14 pk
191 1.14 pk config_attach(parent, cf, &oca, busprint);
192 1.14 pk return 1;
193 1.14 pk }
194 1.4 deraadt
195 1.17 pk #if defined(SUN4)
196 1.14 pk int
197 1.14 pk obio_scan(parent, child, args)
198 1.14 pk struct device *parent;
199 1.14 pk void *child, *args;
200 1.14 pk {
201 1.14 pk return busattach(parent, child, args, BUS_OBIO);
202 1.4 deraadt }
203 1.17 pk #endif
204 1.4 deraadt
205 1.4 deraadt void
206 1.4 deraadt obioattach(parent, self, args)
207 1.4 deraadt struct device *parent, *self;
208 1.4 deraadt void *args;
209 1.4 deraadt {
210 1.17 pk #if defined(SUN4)
211 1.17 pk if (cputyp == CPU_SUN4) {
212 1.17 pk if (self->dv_unit > 0) {
213 1.17 pk printf(" unsupported\n");
214 1.17 pk return;
215 1.17 pk }
216 1.17 pk printf("\n");
217 1.17 pk
218 1.17 pk (void)config_search(obio_scan, self, args);
219 1.17 pk bus_untmp();
220 1.14 pk }
221 1.17 pk #endif
222 1.2 deraadt }
223 1.2 deraadt
224 1.17 pk #if defined(SUN4)
225 1.5 deraadt struct intrhand **vmeints;
226 1.5 deraadt
227 1.14 pk int
228 1.14 pk vmes_scan(parent, child, args)
229 1.14 pk struct device *parent;
230 1.14 pk void *child, *args;
231 1.14 pk {
232 1.14 pk return busattach(parent, child, args, BUS_VME16);
233 1.14 pk }
234 1.14 pk
235 1.4 deraadt void
236 1.4 deraadt vmesattach(parent, self, args)
237 1.4 deraadt struct device *parent, *self;
238 1.4 deraadt void *args;
239 1.4 deraadt {
240 1.14 pk if (self->dv_unit > 0) {
241 1.14 pk printf(" unsupported\n");
242 1.14 pk return;
243 1.14 pk }
244 1.14 pk printf("\n");
245 1.14 pk
246 1.5 deraadt if (vmeints == NULL) {
247 1.5 deraadt vmeints = (struct intrhand **)malloc(256 *
248 1.5 deraadt sizeof(struct intrhand *), M_TEMP, M_NOWAIT);
249 1.5 deraadt bzero(vmeints, 256 * sizeof(struct intrhand *));
250 1.5 deraadt }
251 1.14 pk (void)config_search(vmes_scan, self, args);
252 1.14 pk bus_untmp();
253 1.14 pk }
254 1.14 pk
255 1.14 pk int
256 1.14 pk vmel_scan(parent, child, args)
257 1.14 pk struct device *parent;
258 1.14 pk void *child, *args;
259 1.14 pk {
260 1.14 pk return busattach(parent, child, args, BUS_VME32);
261 1.4 deraadt }
262 1.4 deraadt
263 1.4 deraadt void
264 1.4 deraadt vmelattach(parent, self, args)
265 1.4 deraadt struct device *parent, *self;
266 1.4 deraadt void *args;
267 1.4 deraadt {
268 1.14 pk if (self->dv_unit > 0) {
269 1.14 pk printf(" unsupported\n");
270 1.14 pk return;
271 1.14 pk }
272 1.14 pk printf("\n");
273 1.14 pk
274 1.5 deraadt if (vmeints == NULL) {
275 1.5 deraadt vmeints = (struct intrhand **)malloc(256 *
276 1.5 deraadt sizeof(struct intrhand *), M_TEMP, M_NOWAIT);
277 1.5 deraadt bzero(vmeints, 256 * sizeof(struct intrhand *));
278 1.5 deraadt }
279 1.14 pk (void)config_search(vmel_scan, self, args);
280 1.14 pk bus_untmp();
281 1.4 deraadt }
282 1.4 deraadt
283 1.5 deraadt int pil_to_vme[] = {
284 1.5 deraadt -1, /* pil 0 */
285 1.5 deraadt -1, /* pil 1 */
286 1.5 deraadt 1, /* pil 2 */
287 1.5 deraadt 2, /* pil 3 */
288 1.5 deraadt -1, /* pil 4 */
289 1.5 deraadt 3, /* pil 5 */
290 1.5 deraadt -1, /* pil 6 */
291 1.5 deraadt 4, /* pil 7 */
292 1.5 deraadt -1, /* pil 8 */
293 1.5 deraadt 5, /* pil 9 */
294 1.5 deraadt -1, /* pil 10 */
295 1.5 deraadt 6, /* pil 11 */
296 1.5 deraadt -1, /* pil 12 */
297 1.5 deraadt 7, /* pil 13 */
298 1.5 deraadt -1, /* pil 14 */
299 1.5 deraadt -1, /* pil 15 */
300 1.5 deraadt };
301 1.5 deraadt
302 1.5 deraadt int
303 1.5 deraadt vmeintr(arg)
304 1.5 deraadt void *arg;
305 1.5 deraadt {
306 1.5 deraadt int level = (int)arg, vec;
307 1.5 deraadt struct intrhand *ih;
308 1.5 deraadt int i = 0;
309 1.5 deraadt
310 1.6 deraadt vec = ldcontrolb(AC_VMEINTVEC | (pil_to_vme[level] << 1) | 1);
311 1.6 deraadt if (vec == -1) {
312 1.6 deraadt printf("vme: spurious interrupt\n");
313 1.6 deraadt return 0;
314 1.6 deraadt }
315 1.5 deraadt
316 1.5 deraadt for (ih = vmeints[vec]; ih; ih = ih->ih_next)
317 1.5 deraadt if (ih->ih_fun)
318 1.5 deraadt i += (ih->ih_fun)(ih->ih_arg);
319 1.5 deraadt return (i);
320 1.5 deraadt }
321 1.5 deraadt
322 1.5 deraadt void
323 1.5 deraadt vmeintr_establish(vec, level, ih)
324 1.5 deraadt int vec, level;
325 1.5 deraadt struct intrhand *ih;
326 1.5 deraadt {
327 1.5 deraadt struct intrhand *ihs;
328 1.5 deraadt
329 1.8 deraadt if (vec == -1)
330 1.8 deraadt panic("vmeintr_establish: uninitialized vec\n");
331 1.8 deraadt
332 1.5 deraadt if (vmeints[vec] == NULL)
333 1.5 deraadt vmeints[vec] = ih;
334 1.5 deraadt else {
335 1.5 deraadt for (ihs = vmeints[vec]; ihs->ih_next; ihs = ihs->ih_next)
336 1.5 deraadt ;
337 1.5 deraadt ihs->ih_next = ih;
338 1.5 deraadt }
339 1.5 deraadt
340 1.5 deraadt /* ensure the interrupt subsystem will call us at this level */
341 1.5 deraadt for (ihs = intrhand[level]; ihs; ihs = ihs->ih_next)
342 1.5 deraadt if (ihs->ih_fun == vmeintr)
343 1.5 deraadt return;
344 1.5 deraadt
345 1.5 deraadt ihs = (struct intrhand *)malloc(sizeof(struct intrhand),
346 1.5 deraadt M_TEMP, M_NOWAIT);
347 1.5 deraadt if (ihs == NULL)
348 1.5 deraadt panic("vme_addirq");
349 1.5 deraadt bzero(ihs, sizeof *ihs);
350 1.5 deraadt ihs->ih_fun = vmeintr;
351 1.5 deraadt ihs->ih_arg = (void *)level;
352 1.5 deraadt intr_establish(level, ihs);
353 1.5 deraadt }
354 1.4 deraadt
355 1.2 deraadt #define getpte(va) lda(va, ASI_PTE)
356 1.2 deraadt
357 1.2 deraadt /*
358 1.2 deraadt * If we can find a mapping that was established by the rom, use it.
359 1.2 deraadt * Else, create a new mapping.
360 1.2 deraadt */
361 1.2 deraadt void *
362 1.4 deraadt bus_map(pa, len, bustype)
363 1.17 pk struct rom_reg *pa;
364 1.2 deraadt int len;
365 1.4 deraadt int bustype;
366 1.2 deraadt {
367 1.18 pk u_long pf = (u_long)(pa->rr_paddr) >> PGSHIFT;
368 1.2 deraadt u_long va, pte;
369 1.4 deraadt int pgtype;
370 1.4 deraadt
371 1.4 deraadt switch (bt2pmt[bustype]) {
372 1.4 deraadt case PMAP_OBIO:
373 1.4 deraadt pgtype = PG_OBIO;
374 1.4 deraadt break;
375 1.4 deraadt case PMAP_VME32:
376 1.4 deraadt pgtype = PG_VME32;
377 1.4 deraadt break;
378 1.4 deraadt case PMAP_VME16:
379 1.4 deraadt pgtype = PG_VME16;
380 1.4 deraadt break;
381 1.4 deraadt }
382 1.2 deraadt
383 1.3 deraadt if (len <= NBPG) {
384 1.3 deraadt for (va = OLDMON_STARTVADDR; va < OLDMON_ENDVADDR; va += NBPG) {
385 1.3 deraadt pte = getpte(va);
386 1.4 deraadt if ((pte & PG_V) != 0 && (pte & PG_TYPE) == pgtype &&
387 1.3 deraadt (pte & PG_PFNUM) == pf)
388 1.19 chuck return ((void *)
389 1.19 chuck (va | ((u_long)pa->rr_paddr & PGOFSET)) );
390 1.19 chuck /* note: preserve page offset */
391 1.3 deraadt }
392 1.2 deraadt }
393 1.17 pk return mapiodev(pa, 0, len, bustype);
394 1.2 deraadt }
395 1.2 deraadt
396 1.2 deraadt void *
397 1.4 deraadt bus_tmp(pa, bustype)
398 1.2 deraadt void *pa;
399 1.4 deraadt int bustype;
400 1.2 deraadt {
401 1.3 deraadt vm_offset_t addr = (vm_offset_t)pa & ~PGOFSET;
402 1.4 deraadt int pmtype = bt2pmt[bustype];
403 1.3 deraadt
404 1.13 mycroft pmap_enter(pmap_kernel(), TMPMAP_VA,
405 1.4 deraadt addr | pmtype | PMAP_NC,
406 1.2 deraadt VM_PROT_READ | VM_PROT_WRITE, 1);
407 1.19 chuck return ((void *)(TMPMAP_VA | ((u_long) pa & PGOFSET)) );
408 1.2 deraadt }
409 1.2 deraadt
410 1.2 deraadt void
411 1.4 deraadt bus_untmp()
412 1.2 deraadt {
413 1.13 mycroft pmap_remove(pmap_kernel(), TMPMAP_VA, TMPMAP_VA+NBPG);
414 1.1 deraadt }
415 1.17 pk #endif
416