obio.c revision 1.14 1 /* $NetBSD: obio.c,v 1.14 1995/04/25 19:59:49 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 oca.ca_ra.ra_bp = ca->ca_ra.ra_bp;
159 oca.ca_bustype = bustype;
160
161 if ((*cf->cf_driver->cd_match)(parent, cf, &oca) == 0)
162 return 0;
163
164 /*
165 * check if XXmatch routine replaced the
166 * temporary mapping with a real mapping.
167 */
168 if (tmp == oca.ca_ra.ra_vaddr)
169 oca.ca_ra.ra_vaddr = NULL;
170 /*
171 * or if it has asked us to create a mapping..
172 * (which won't be seen on future XXmatch calls,
173 * so not as useful as it seems.)
174 */
175 if (oca.ca_ra.ra_len)
176 oca.ca_ra.ra_vaddr =
177 bus_map(oca.ca_ra.ra_paddr,
178 oca.ca_ra.ra_len, oca.ca_bustype);
179
180 config_attach(parent, cf, &oca, busprint);
181 return 1;
182 }
183
184 int
185 obio_scan(parent, child, args)
186 struct device *parent;
187 void *child, *args;
188 {
189 return busattach(parent, child, args, BUS_OBIO);
190 }
191
192 void
193 obioattach(parent, self, args)
194 struct device *parent, *self;
195 void *args;
196 {
197 if (self->dv_unit > 0) {
198 printf(" unsupported\n");
199 return;
200 }
201 printf("\n");
202
203 (void)config_search(obio_scan, self, args);
204 bus_untmp();
205 }
206
207 struct intrhand **vmeints;
208
209 int
210 vmes_scan(parent, child, args)
211 struct device *parent;
212 void *child, *args;
213 {
214 return busattach(parent, child, args, BUS_VME16);
215 }
216
217 void
218 vmesattach(parent, self, args)
219 struct device *parent, *self;
220 void *args;
221 {
222 if (self->dv_unit > 0) {
223 printf(" unsupported\n");
224 return;
225 }
226 printf("\n");
227
228 if (vmeints == NULL) {
229 vmeints = (struct intrhand **)malloc(256 *
230 sizeof(struct intrhand *), M_TEMP, M_NOWAIT);
231 bzero(vmeints, 256 * sizeof(struct intrhand *));
232 }
233 (void)config_search(vmes_scan, self, args);
234 bus_untmp();
235 }
236
237 int
238 vmel_scan(parent, child, args)
239 struct device *parent;
240 void *child, *args;
241 {
242 return busattach(parent, child, args, BUS_VME32);
243 }
244
245 void
246 vmelattach(parent, self, args)
247 struct device *parent, *self;
248 void *args;
249 {
250 if (self->dv_unit > 0) {
251 printf(" unsupported\n");
252 return;
253 }
254 printf("\n");
255
256 if (vmeints == NULL) {
257 vmeints = (struct intrhand **)malloc(256 *
258 sizeof(struct intrhand *), M_TEMP, M_NOWAIT);
259 bzero(vmeints, 256 * sizeof(struct intrhand *));
260 }
261 (void)config_search(vmel_scan, self, args);
262 bus_untmp();
263 }
264
265 int pil_to_vme[] = {
266 -1, /* pil 0 */
267 -1, /* pil 1 */
268 1, /* pil 2 */
269 2, /* pil 3 */
270 -1, /* pil 4 */
271 3, /* pil 5 */
272 -1, /* pil 6 */
273 4, /* pil 7 */
274 -1, /* pil 8 */
275 5, /* pil 9 */
276 -1, /* pil 10 */
277 6, /* pil 11 */
278 -1, /* pil 12 */
279 7, /* pil 13 */
280 -1, /* pil 14 */
281 -1, /* pil 15 */
282 };
283
284 int
285 vmeintr(arg)
286 void *arg;
287 {
288 int level = (int)arg, vec;
289 struct intrhand *ih;
290 int i = 0;
291
292 vec = ldcontrolb(AC_VMEINTVEC | (pil_to_vme[level] << 1) | 1);
293 if (vec == -1) {
294 printf("vme: spurious interrupt\n");
295 return 0;
296 }
297
298 for (ih = vmeints[vec]; ih; ih = ih->ih_next)
299 if (ih->ih_fun)
300 i += (ih->ih_fun)(ih->ih_arg);
301 return (i);
302 }
303
304 void
305 vmeintr_establish(vec, level, ih)
306 int vec, level;
307 struct intrhand *ih;
308 {
309 struct intrhand *ihs;
310
311 if (vec == -1)
312 panic("vmeintr_establish: uninitialized vec\n");
313
314 if (vmeints[vec] == NULL)
315 vmeints[vec] = ih;
316 else {
317 for (ihs = vmeints[vec]; ihs->ih_next; ihs = ihs->ih_next)
318 ;
319 ihs->ih_next = ih;
320 }
321
322 /* ensure the interrupt subsystem will call us at this level */
323 for (ihs = intrhand[level]; ihs; ihs = ihs->ih_next)
324 if (ihs->ih_fun == vmeintr)
325 return;
326
327 ihs = (struct intrhand *)malloc(sizeof(struct intrhand),
328 M_TEMP, M_NOWAIT);
329 if (ihs == NULL)
330 panic("vme_addirq");
331 bzero(ihs, sizeof *ihs);
332 ihs->ih_fun = vmeintr;
333 ihs->ih_arg = (void *)level;
334 intr_establish(level, ihs);
335 }
336
337 #define getpte(va) lda(va, ASI_PTE)
338
339 /*
340 * If we can find a mapping that was established by the rom, use it.
341 * Else, create a new mapping.
342 */
343 void *
344 bus_map(pa, len, bustype)
345 void *pa;
346 int len;
347 int bustype;
348 {
349 u_long pf = (u_long)pa >> PGSHIFT;
350 u_long va, pte;
351 int pgtype;
352
353 switch (bt2pmt[bustype]) {
354 case PMAP_OBIO:
355 pgtype = PG_OBIO;
356 break;
357 case PMAP_VME32:
358 pgtype = PG_VME32;
359 break;
360 case PMAP_VME16:
361 pgtype = PG_VME16;
362 break;
363 }
364
365 if (len <= NBPG) {
366 for (va = OLDMON_STARTVADDR; va < OLDMON_ENDVADDR; va += NBPG) {
367 pte = getpte(va);
368 if ((pte & PG_V) != 0 && (pte & PG_TYPE) == pgtype &&
369 (pte & PG_PFNUM) == pf)
370 return ((void *)va);
371 }
372 }
373 return mapiodev(pa, len, bustype);
374 }
375
376 void *
377 bus_tmp(pa, bustype)
378 void *pa;
379 int bustype;
380 {
381 vm_offset_t addr = (vm_offset_t)pa & ~PGOFSET;
382 int pmtype = bt2pmt[bustype];
383
384 pmap_enter(pmap_kernel(), TMPMAP_VA,
385 addr | pmtype | PMAP_NC,
386 VM_PROT_READ | VM_PROT_WRITE, 1);
387 return ((void *)TMPMAP_VA);
388 }
389
390 void
391 bus_untmp()
392 {
393 pmap_remove(pmap_kernel(), TMPMAP_VA, TMPMAP_VA+NBPG);
394 }
395