obio.c revision 1.17 1 /* $NetBSD: obio.c,v 1.17 1995/12/11 12:43:30 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 #if defined(SUN4)
61 static void vmesattach __P((struct device *, struct device *, void *));
62 static void vmelattach __P((struct device *, struct device *, void *));
63 static int busattach __P((struct device *, void *, void *, int));
64 void * bus_map __P((struct rom_reg *, int, int));
65 void * bus_tmp __P((void *, int));
66 void bus_untmp __P((void));
67 #endif
68
69 struct cfdriver obiocd = { NULL, "obio", busmatch, obioattach,
70 DV_DULL, sizeof(struct bus_softc)
71 };
72 #if defined(SUN4)
73 struct cfdriver vmelcd = { NULL, "vmel", busmatch, vmelattach,
74 DV_DULL, sizeof(struct bus_softc)
75 };
76 struct cfdriver vmescd = { NULL, "vmes", busmatch, vmesattach,
77 DV_DULL, sizeof(struct bus_softc)
78 };
79 #endif
80
81
82 int
83 busmatch(parent, vcf, aux)
84 struct device *parent;
85 void *vcf, *aux;
86 {
87 struct cfdata *cf = vcf;
88 register struct confargs *ca = aux;
89 register struct romaux *ra = &ca->ca_ra;
90
91 if (cputyp != CPU_SUN4)
92 return (0);
93 return (strcmp(cf->cf_driver->cd_name, ra->ra_name) == 0);
94 }
95
96 int
97 busprint(args, obio)
98 void *args;
99 char *obio;
100 {
101 register struct confargs *ca = args;
102
103 if (ca->ca_ra.ra_name == NULL)
104 ca->ca_ra.ra_name = "<unknown>";
105 if (obio)
106 printf("[%s at %s]", ca->ca_ra.ra_name, obio);
107 printf(" addr %x", ca->ca_ra.ra_paddr);
108 if (ca->ca_ra.ra_intr[0].int_vec != -1)
109 printf(" vec 0x%x", ca->ca_ra.ra_intr[0].int_vec);
110 return (UNCONF);
111 }
112
113
114 int
115 busattach(parent, child, args, bustype)
116 struct device *parent;
117 void *args, *child;
118 int bustype;
119 {
120 struct cfdata *cf = child;
121 register struct bus_softc *sc = (struct bus_softc *)parent;
122 register struct confargs *ca = args;
123 struct confargs oca;
124 caddr_t tmp;
125
126 if (bustype == BUS_OBIO && cputyp == CPU_SUN4) {
127 /*
128 * On the 4/100 obio addresses must be mapped at
129 * 0x0YYYYYYY, but alias higher up (we avoid the
130 * alias condition because it causes pmap difficulties)
131 * XXX: We also assume that 4/[23]00 obio addresses
132 * must be 0xZYYYYYYY, where (Z != 0)
133 */
134 if (cpumod==SUN4_100 && (cf->cf_loc[0] & 0xf0000000))
135 return 0;
136 if (cpumod!=SUN4_100 && !(cf->cf_loc[0] & 0xf0000000))
137 return 0;
138 }
139
140 if (parent->dv_cfdata->cf_driver->cd_indirect) {
141 printf(" indirect devices not supported\n");
142 return 0;
143 }
144
145 oca.ca_ra.ra_iospace = -1;
146 oca.ca_ra.ra_paddr = (void *)cf->cf_loc[0];
147 oca.ca_ra.ra_len = 0;
148 oca.ca_ra.ra_nreg = 1;
149 tmp = NULL;
150 if (oca.ca_ra.ra_paddr)
151 tmp = bus_tmp(oca.ca_ra.ra_paddr,
152 bustype);
153 oca.ca_ra.ra_vaddr = tmp;
154 oca.ca_ra.ra_intr[0].int_pri = cf->cf_loc[1];
155 if (bustype == BUS_VME16 || bustype == BUS_VME32)
156 oca.ca_ra.ra_intr[0].int_vec = cf->cf_loc[2];
157 else
158 oca.ca_ra.ra_intr[0].int_vec = -1;
159 oca.ca_ra.ra_nintr = 1;
160 oca.ca_ra.ra_name = cf->cf_driver->cd_name;
161 if (ca->ca_ra.ra_bp != NULL &&
162 ((bustype == BUS_VME16 && strcmp(ca->ca_ra.ra_bp->name,"vmes") ==0) ||
163 (bustype == BUS_VME32 && strcmp(ca->ca_ra.ra_bp->name,"vmel") ==0) ||
164 (bustype == BUS_OBIO && strcmp(ca->ca_ra.ra_bp->name,"obio") == 0)))
165 oca.ca_ra.ra_bp = ca->ca_ra.ra_bp + 1;
166 else
167 oca.ca_ra.ra_bp = NULL;
168 oca.ca_bustype = bustype;
169
170 if ((*cf->cf_driver->cd_match)(parent, cf, &oca) == 0)
171 return 0;
172
173 /*
174 * check if XXmatch routine replaced the
175 * temporary mapping with a real mapping.
176 */
177 if (tmp == oca.ca_ra.ra_vaddr)
178 oca.ca_ra.ra_vaddr = NULL;
179 /*
180 * or if it has asked us to create a mapping..
181 * (which won't be seen on future XXmatch calls,
182 * so not as useful as it seems.)
183 */
184 if (oca.ca_ra.ra_len)
185 oca.ca_ra.ra_vaddr =
186 bus_map(oca.ca_ra.ra_reg,
187 oca.ca_ra.ra_len, oca.ca_bustype);
188
189 config_attach(parent, cf, &oca, busprint);
190 return 1;
191 }
192
193 #if defined(SUN4)
194 int
195 obio_scan(parent, child, args)
196 struct device *parent;
197 void *child, *args;
198 {
199 return busattach(parent, child, args, BUS_OBIO);
200 }
201 #endif
202
203 void
204 obioattach(parent, self, args)
205 struct device *parent, *self;
206 void *args;
207 {
208 #if defined(SUN4)
209 if (cputyp == CPU_SUN4) {
210 if (self->dv_unit > 0) {
211 printf(" unsupported\n");
212 return;
213 }
214 printf("\n");
215
216 (void)config_search(obio_scan, self, args);
217 bus_untmp();
218 }
219 #endif
220 }
221
222 #if defined(SUN4)
223 struct intrhand **vmeints;
224
225 int
226 vmes_scan(parent, child, args)
227 struct device *parent;
228 void *child, *args;
229 {
230 return busattach(parent, child, args, BUS_VME16);
231 }
232
233 void
234 vmesattach(parent, self, args)
235 struct device *parent, *self;
236 void *args;
237 {
238 if (self->dv_unit > 0) {
239 printf(" unsupported\n");
240 return;
241 }
242 printf("\n");
243
244 if (vmeints == NULL) {
245 vmeints = (struct intrhand **)malloc(256 *
246 sizeof(struct intrhand *), M_TEMP, M_NOWAIT);
247 bzero(vmeints, 256 * sizeof(struct intrhand *));
248 }
249 (void)config_search(vmes_scan, self, args);
250 bus_untmp();
251 }
252
253 int
254 vmel_scan(parent, child, args)
255 struct device *parent;
256 void *child, *args;
257 {
258 return busattach(parent, child, args, BUS_VME32);
259 }
260
261 void
262 vmelattach(parent, self, args)
263 struct device *parent, *self;
264 void *args;
265 {
266 if (self->dv_unit > 0) {
267 printf(" unsupported\n");
268 return;
269 }
270 printf("\n");
271
272 if (vmeints == NULL) {
273 vmeints = (struct intrhand **)malloc(256 *
274 sizeof(struct intrhand *), M_TEMP, M_NOWAIT);
275 bzero(vmeints, 256 * sizeof(struct intrhand *));
276 }
277 (void)config_search(vmel_scan, self, args);
278 bus_untmp();
279 }
280
281 int pil_to_vme[] = {
282 -1, /* pil 0 */
283 -1, /* pil 1 */
284 1, /* pil 2 */
285 2, /* pil 3 */
286 -1, /* pil 4 */
287 3, /* pil 5 */
288 -1, /* pil 6 */
289 4, /* pil 7 */
290 -1, /* pil 8 */
291 5, /* pil 9 */
292 -1, /* pil 10 */
293 6, /* pil 11 */
294 -1, /* pil 12 */
295 7, /* pil 13 */
296 -1, /* pil 14 */
297 -1, /* pil 15 */
298 };
299
300 int
301 vmeintr(arg)
302 void *arg;
303 {
304 int level = (int)arg, vec;
305 struct intrhand *ih;
306 int i = 0;
307
308 vec = ldcontrolb(AC_VMEINTVEC | (pil_to_vme[level] << 1) | 1);
309 if (vec == -1) {
310 printf("vme: spurious interrupt\n");
311 return 0;
312 }
313
314 for (ih = vmeints[vec]; ih; ih = ih->ih_next)
315 if (ih->ih_fun)
316 i += (ih->ih_fun)(ih->ih_arg);
317 return (i);
318 }
319
320 void
321 vmeintr_establish(vec, level, ih)
322 int vec, level;
323 struct intrhand *ih;
324 {
325 struct intrhand *ihs;
326
327 if (vec == -1)
328 panic("vmeintr_establish: uninitialized vec\n");
329
330 if (vmeints[vec] == NULL)
331 vmeints[vec] = ih;
332 else {
333 for (ihs = vmeints[vec]; ihs->ih_next; ihs = ihs->ih_next)
334 ;
335 ihs->ih_next = ih;
336 }
337
338 /* ensure the interrupt subsystem will call us at this level */
339 for (ihs = intrhand[level]; ihs; ihs = ihs->ih_next)
340 if (ihs->ih_fun == vmeintr)
341 return;
342
343 ihs = (struct intrhand *)malloc(sizeof(struct intrhand),
344 M_TEMP, M_NOWAIT);
345 if (ihs == NULL)
346 panic("vme_addirq");
347 bzero(ihs, sizeof *ihs);
348 ihs->ih_fun = vmeintr;
349 ihs->ih_arg = (void *)level;
350 intr_establish(level, ihs);
351 }
352
353 #define getpte(va) lda(va, ASI_PTE)
354
355 /*
356 * If we can find a mapping that was established by the rom, use it.
357 * Else, create a new mapping.
358 */
359 void *
360 bus_map(pa, len, bustype)
361 struct rom_reg *pa;
362 int len;
363 int bustype;
364 {
365 u_long pf = (u_long)pa >> PGSHIFT;
366 u_long va, pte;
367 int pgtype;
368
369 switch (bt2pmt[bustype]) {
370 case PMAP_OBIO:
371 pgtype = PG_OBIO;
372 break;
373 case PMAP_VME32:
374 pgtype = PG_VME32;
375 break;
376 case PMAP_VME16:
377 pgtype = PG_VME16;
378 break;
379 }
380
381 if (len <= NBPG) {
382 for (va = OLDMON_STARTVADDR; va < OLDMON_ENDVADDR; va += NBPG) {
383 pte = getpte(va);
384 if ((pte & PG_V) != 0 && (pte & PG_TYPE) == pgtype &&
385 (pte & PG_PFNUM) == pf)
386 return ((void *)va);
387 }
388 }
389 return mapiodev(pa, 0, len, bustype);
390 }
391
392 void *
393 bus_tmp(pa, bustype)
394 void *pa;
395 int bustype;
396 {
397 vm_offset_t addr = (vm_offset_t)pa & ~PGOFSET;
398 int pmtype = bt2pmt[bustype];
399
400 pmap_enter(pmap_kernel(), TMPMAP_VA,
401 addr | pmtype | PMAP_NC,
402 VM_PROT_READ | VM_PROT_WRITE, 1);
403 return ((void *)TMPMAP_VA);
404 }
405
406 void
407 bus_untmp()
408 {
409 pmap_remove(pmap_kernel(), TMPMAP_VA, TMPMAP_VA+NBPG);
410 }
411 #endif
412