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