obio.c revision 1.6 1 /* $NetBSD: obio.c,v 1.6 1994/11/02 04:55:20 deraadt 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(struct device *, struct cfdata *, void *);
59 static void obioattach(struct device *, struct device *, void *);
60 static void vmesattach(struct device *, struct device *, void *);
61 static void vmelattach(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 void busattach(struct device *, struct device *, 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, cf, aux)
81 struct device *parent;
82 struct cfdata *cf;
83 void *aux;
84 {
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 return (UNCONF);
106 }
107
108 void
109 busattach(parent, self, args, bustype)
110 struct device *parent, *self;
111 void *args;
112 int bustype;
113 {
114 register struct bus_softc *sc = (struct bus_softc *)self;
115 extern struct cfdata cfdata[];
116 register struct confargs *ca = args;
117 struct confargs oca;
118 register short *p;
119 struct cfdata *cf;
120 caddr_t tmp;
121
122 if (sc->sc_dev.dv_unit > 0) {
123 printf(" unsupported\n");
124 return;
125 }
126
127 printf("\n");
128
129 for (cf = cfdata; cf->cf_driver; cf++) {
130 if (cf->cf_fstate == FSTATE_FOUND)
131 continue;
132 for (p = cf->cf_parents; *p >= 0; p++)
133 if (self->dv_cfdata == &cfdata[*p]) {
134 oca.ca_ra.ra_iospace = -1;
135 oca.ca_ra.ra_paddr = (void *)cf->cf_loc[0];
136 oca.ca_ra.ra_len = 0;
137 tmp = NULL;
138 if (oca.ca_ra.ra_paddr)
139 tmp = bus_tmp(oca.ca_ra.ra_paddr,
140 bustype);
141 oca.ca_ra.ra_vaddr = tmp;
142 oca.ca_ra.ra_intr[0].int_pri = cf->cf_loc[1];
143 if (bustype == BUS_VME16 || bustype == BUS_VME32)
144 oca.ca_ra.ra_intr[0].int_vec = cf->cf_loc[2];
145 else
146 oca.ca_ra.ra_intr[0].int_vec = 0;
147 oca.ca_ra.ra_nintr = 1;
148 oca.ca_ra.ra_name = cf->cf_driver->cd_name;
149 oca.ca_ra.ra_bp = ca->ca_ra.ra_bp;
150 oca.ca_bustype = bustype;
151
152 if ((*cf->cf_driver->cd_match)(self, cf, &oca) == 0)
153 continue;
154
155 /*
156 * check if XXmatch routine replaced the
157 * temporary mapping with a real mapping.
158 */
159 if (tmp == oca.ca_ra.ra_vaddr)
160 oca.ca_ra.ra_vaddr = NULL;
161 /*
162 * or if it has asked us to create a mapping..
163 * (which won't be seen on future XXmatch calls,
164 * so not as useful as it seems.)
165 */
166 if (oca.ca_ra.ra_len)
167 oca.ca_ra.ra_vaddr =
168 bus_map(oca.ca_ra.ra_paddr,
169 oca.ca_ra.ra_len, oca.ca_bustype);
170
171 config_attach(self, cf, &oca, busprint);
172 }
173 }
174 bus_untmp();
175 }
176
177 void
178 obioattach(parent, self, args)
179 struct device *parent, *self;
180 void *args;
181 {
182 busattach(parent, self, args, BUS_OBIO);
183 }
184
185 struct intrhand **vmeints;
186
187 void
188 vmesattach(parent, self, args)
189 struct device *parent, *self;
190 void *args;
191 {
192 if (vmeints == NULL) {
193 vmeints = (struct intrhand **)malloc(256 *
194 sizeof(struct intrhand *), M_TEMP, M_NOWAIT);
195 bzero(vmeints, 256 * sizeof(struct intrhand *));
196 }
197 busattach(parent, self, args, BUS_VME16);
198 }
199
200 void
201 vmelattach(parent, self, args)
202 struct device *parent, *self;
203 void *args;
204 {
205 if (vmeints == NULL) {
206 vmeints = (struct intrhand **)malloc(256 *
207 sizeof(struct intrhand *), M_TEMP, M_NOWAIT);
208 bzero(vmeints, 256 * sizeof(struct intrhand *));
209 }
210 busattach(parent, self, args, BUS_VME32);
211 }
212
213 int pil_to_vme[] = {
214 -1, /* pil 0 */
215 -1, /* pil 1 */
216 1, /* pil 2 */
217 2, /* pil 3 */
218 -1, /* pil 4 */
219 3, /* pil 5 */
220 -1, /* pil 6 */
221 4, /* pil 7 */
222 -1, /* pil 8 */
223 5, /* pil 9 */
224 -1, /* pil 10 */
225 6, /* pil 11 */
226 -1, /* pil 12 */
227 7, /* pil 13 */
228 -1, /* pil 14 */
229 -1, /* pil 15 */
230 };
231
232 int
233 vmeintr(arg)
234 void *arg;
235 {
236 int level = (int)arg, vec;
237 struct intrhand *ih;
238 int i = 0;
239
240 vec = ldcontrolb(AC_VMEINTVEC | (pil_to_vme[level] << 1) | 1);
241 if (vec == -1) {
242 printf("vme: spurious interrupt\n");
243 return 0;
244 }
245
246 for (ih = vmeints[vec]; ih; ih = ih->ih_next)
247 if (ih->ih_fun)
248 i += (ih->ih_fun)(ih->ih_arg);
249 return (i);
250 }
251
252 void
253 vmeintr_establish(vec, level, ih)
254 int vec, level;
255 struct intrhand *ih;
256 {
257 struct intrhand *ihs;
258
259 if (vmeints[vec] == NULL)
260 vmeints[vec] = ih;
261 else {
262 for (ihs = vmeints[vec]; ihs->ih_next; ihs = ihs->ih_next)
263 ;
264 ihs->ih_next = ih;
265 }
266
267 /* ensure the interrupt subsystem will call us at this level */
268 for (ihs = intrhand[level]; ihs; ihs = ihs->ih_next)
269 if (ihs->ih_fun == vmeintr)
270 return;
271
272 ihs = (struct intrhand *)malloc(sizeof(struct intrhand),
273 M_TEMP, M_NOWAIT);
274 if (ihs == NULL)
275 panic("vme_addirq");
276 bzero(ihs, sizeof *ihs);
277 ihs->ih_fun = vmeintr;
278 ihs->ih_arg = (void *)level;
279 intr_establish(level, ihs);
280 }
281
282 #define getpte(va) lda(va, ASI_PTE)
283
284 /*
285 * If we can find a mapping that was established by the rom, use it.
286 * Else, create a new mapping.
287 */
288 void *
289 bus_map(pa, len, bustype)
290 void *pa;
291 int len;
292 int bustype;
293 {
294 u_long pf = (u_long)pa >> PGSHIFT;
295 u_long va, pte;
296 int pgtype;
297
298 switch (bt2pmt[bustype]) {
299 case PMAP_OBIO:
300 pgtype = PG_OBIO;
301 break;
302 case PMAP_VME32:
303 pgtype = PG_VME32;
304 break;
305 case PMAP_VME16:
306 pgtype = PG_VME16;
307 break;
308 }
309
310 if (len <= NBPG) {
311 for (va = OLDMON_STARTVADDR; va < OLDMON_ENDVADDR; va += NBPG) {
312 pte = getpte(va);
313 if ((pte & PG_V) != 0 && (pte & PG_TYPE) == pgtype &&
314 (pte & PG_PFNUM) == pf)
315 return ((void *)va);
316 }
317 }
318 return mapiodev(pa, len, bustype);
319 }
320
321 void *
322 bus_tmp(pa, bustype)
323 void *pa;
324 int bustype;
325 {
326 vm_offset_t addr = (vm_offset_t)pa & ~PGOFSET;
327 int pmtype = bt2pmt[bustype];
328
329 pmap_enter(kernel_pmap, TMPMAP_VA,
330 addr | pmtype | PMAP_NC,
331 VM_PROT_READ | VM_PROT_WRITE, 1);
332 return ((void *)TMPMAP_VA);
333 }
334
335 void
336 bus_untmp()
337 {
338 pmap_remove(kernel_pmap, TMPMAP_VA, TMPMAP_VA+NBPG);
339 }
340
341 void
342 wzero(b, l)
343 char *b;
344 int l;
345 {
346 register char *be = b + l;
347 register short *sp;
348
349 if (l <= 0)
350 return;
351
352 /* front, */
353 if ((u_long)b & 1)
354 *b++ = 0;
355
356 /* back, */
357 if (b != be && ((u_long)be & 1) != 0) {
358 be--;
359 *be = 0;
360 }
361
362 /* and middle. */
363 sp = (u_short *)b;
364 while (sp != (u_short *)be)
365 *sp++ = 0;
366 }
367
368 void
369 wcopy(b1, b2, l)
370 u_char *b1, *b2;
371 u_long l;
372 {
373 register u_short *sp;
374 register int bstore = 0;
375 register u_char *b1e;
376
377 if (l <= 0)
378 return;
379
380 /* front, */
381 if ((u_long)b1 & 1) {
382 *b2++ = *b1++;
383 l--;
384 }
385
386 /* middle, */
387 sp = (u_short *)b1;
388 b1e = b1 + l;
389 if (l & 1)
390 b1e--;
391 bstore = (u_long)b2 & 1;
392
393 while (sp < (u_short *)b1e) {
394 if (bstore) {
395 b2[1] = *sp & 0xff;
396 b2[0] = *sp >> 8;
397 } else
398 *((short *)b2) = *sp;
399 sp++;
400 b2 += 2;
401 }
402
403 /* and back. */
404 if (l & 1)
405 *b2 = *b1e;
406 }
407