vsbus.c revision 1.43 1 /* $NetBSD: vsbus.c,v 1.43 2003/06/18 08:58:42 drochner Exp $ */
2 /*
3 * Copyright (c) 1996, 1999 Ludd, University of Lule}, Sweden.
4 * All rights reserved.
5 *
6 * This code is derived from software contributed to Ludd by Bertram Barth.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed at Ludd, University of
19 * Lule}, Sweden and its contributors.
20 * 4. The name of the author may not be used to endorse or promote products
21 * derived from this software without specific prior written permission
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
24 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
27 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
28 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
32 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 */
34
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/buf.h>
38 #include <sys/conf.h>
39 #include <sys/file.h>
40 #include <sys/ioctl.h>
41 #include <sys/proc.h>
42 #include <sys/user.h>
43 #include <sys/device.h>
44 #include <sys/disklabel.h>
45 #include <sys/syslog.h>
46 #include <sys/stat.h>
47
48 #include <uvm/uvm_extern.h>
49
50 #define _VAX_BUS_DMA_PRIVATE
51 #include <machine/bus.h>
52 #include <machine/pte.h>
53 #include <machine/sid.h>
54 #include <machine/scb.h>
55 #include <machine/cpu.h>
56 #include <machine/trap.h>
57 #include <machine/nexus.h>
58
59 #include <machine/uvax.h>
60 #include <machine/ka410.h>
61 #include <machine/ka420.h>
62 #include <machine/ka43.h>
63
64 #include <machine/vsbus.h>
65
66 #include "ioconf.h"
67 #include "opt_cputype.h"
68
69 int vsbus_match(struct device *, struct cfdata *, void *);
70 void vsbus_attach(struct device *, struct device *, void *);
71 int vsbus_print(void *, const char *);
72 int vsbus_search(struct device *, struct cfdata *, void *);
73
74 static struct vax_bus_dma_tag vsbus_bus_dma_tag = {
75 0,
76 0,
77 0,
78 0,
79 0,
80 0,
81 _bus_dmamap_create,
82 _bus_dmamap_destroy,
83 _bus_dmamap_load,
84 _bus_dmamap_load_mbuf,
85 _bus_dmamap_load_uio,
86 _bus_dmamap_load_raw,
87 _bus_dmamap_unload,
88 _bus_dmamap_sync,
89 _bus_dmamem_alloc,
90 _bus_dmamem_free,
91 _bus_dmamem_map,
92 _bus_dmamem_unmap,
93 _bus_dmamem_mmap,
94 };
95
96 extern struct vax_bus_space vax_mem_bus_space;
97 static SIMPLEQ_HEAD(, vsbus_dma) vsbus_dma;
98
99 CFATTACH_DECL(vsbus, sizeof(struct vsbus_softc),
100 vsbus_match, vsbus_attach, NULL, NULL);
101
102 int
103 vsbus_print(aux, name)
104 void *aux;
105 const char *name;
106 {
107 struct vsbus_attach_args *va = aux;
108
109 aprint_normal(" csr 0x%lx vec %o ipl %x maskbit %d", va->va_paddr,
110 va->va_cvec & 511, va->va_br, va->va_maskno - 1);
111 return(UNCONF);
112 }
113
114 int
115 vsbus_match(parent, cf, aux)
116 struct device *parent;
117 struct cfdata *cf;
118 void *aux;
119 {
120 #if VAX53 || VAXANY
121 /* Kludge: VAX53 is... special */
122 if (vax_boardtype == VAX_BTYP_53 && (int)aux == 1)
123 return 1; /* Hack */
124 #endif
125 if (vax_bustype == VAX_VSBUS)
126 return 1;
127 return 0;
128 }
129
130 void
131 vsbus_attach(parent, self, aux)
132 struct device *parent, *self;
133 void *aux;
134 {
135 struct vsbus_softc *sc = (void *)self;
136 int dbase, dsize;
137
138 printf("\n");
139
140 sc->sc_dmatag = vsbus_bus_dma_tag;
141
142 switch (vax_boardtype) {
143 #if VAX49 || VAX53
144 case VAX_BTYP_53:
145 case VAX_BTYP_49:
146 sc->sc_vsregs = vax_map_physmem(0x25c00000, 1);
147 sc->sc_intreq = (char *)sc->sc_vsregs + 12;
148 sc->sc_intclr = (char *)sc->sc_vsregs + 12;
149 sc->sc_intmsk = (char *)sc->sc_vsregs + 8;
150 vsbus_dma_init(sc, 8192);
151 break;
152 #endif
153
154 #if VAX46 || VAX48
155 case VAX_BTYP_48:
156 case VAX_BTYP_46:
157 sc->sc_vsregs = vax_map_physmem(VS_REGS, 1);
158 sc->sc_intreq = (char *)sc->sc_vsregs + 15;
159 sc->sc_intclr = (char *)sc->sc_vsregs + 15;
160 sc->sc_intmsk = (char *)sc->sc_vsregs + 12;
161 vsbus_dma_init(sc, 32768);
162 #endif
163
164 default:
165 sc->sc_vsregs = vax_map_physmem(VS_REGS, 1);
166 sc->sc_intreq = (char *)sc->sc_vsregs + 15;
167 sc->sc_intclr = (char *)sc->sc_vsregs + 15;
168 sc->sc_intmsk = (char *)sc->sc_vsregs + 12;
169 if (vax_boardtype == VAX_BTYP_410) {
170 dbase = KA410_DMA_BASE;
171 dsize = KA410_DMA_SIZE;
172 } else {
173 dbase = KA420_DMA_BASE;
174 dsize = KA420_DMA_SIZE;
175 *(char *)(sc->sc_vsregs + 0xe0) = 1; /* Big DMA */
176 }
177 sc->sc_dmasize = dsize;
178 sc->sc_dmaaddr = uvm_km_valloc(kernel_map, dsize);
179 ioaccess(sc->sc_dmaaddr, dbase, dsize/VAX_NBPG);
180 break;
181 }
182
183 SIMPLEQ_INIT(&vsbus_dma);
184 /*
185 * First: find which interrupts we won't care about.
186 * There are interrupts that interrupt on a periodic basic
187 * that we don't want to interfere with the rest of the
188 * interrupt probing.
189 */
190 *sc->sc_intmsk = 0;
191 *sc->sc_intclr = 0xff;
192 DELAY(1000000); /* Wait a second */
193 sc->sc_mask = *sc->sc_intreq;
194 printf("%s: interrupt mask %x\n", self->dv_xname, sc->sc_mask);
195 /*
196 * now check for all possible devices on this "bus"
197 */
198 config_search(vsbus_search, self, NULL);
199
200 /* Autoconfig finished, enable interrupts */
201 *sc->sc_intmsk = ~sc->sc_mask;
202 }
203
204 int
205 vsbus_search(parent, cf, aux)
206 struct device *parent;
207 struct cfdata *cf;
208 void *aux;
209 {
210 struct vsbus_softc *sc = (void *)parent;
211 struct vsbus_attach_args va;
212 int i, vec, br;
213 u_char c;
214
215 va.va_paddr = cf->cf_loc[0];
216 va.va_addr = vax_map_physmem(va.va_paddr, 1);
217 va.va_dmat = &sc->sc_dmatag;
218 va.va_iot = &vax_mem_bus_space;
219
220 *sc->sc_intmsk = 0;
221 *sc->sc_intclr = 0xff;
222 scb_vecref(0, 0); /* Clear vector ref */
223
224 i = config_match(parent, cf, &va);
225 vax_unmap_physmem(va.va_addr, 1);
226 c = *sc->sc_intreq & ~sc->sc_mask;
227 if (i == 0)
228 goto forgetit;
229 if (i > 10)
230 c = sc->sc_mask; /* Fooling interrupt */
231 else if (c == 0)
232 goto forgetit;
233
234 *sc->sc_intmsk = c;
235 DELAY(100);
236 *sc->sc_intmsk = 0;
237 va.va_maskno = ffs((u_int)c);
238 i = scb_vecref(&vec, &br);
239 if (i == 0)
240 goto fail;
241 if (vec == 0)
242 goto fail;
243
244 va.va_br = br;
245 va.va_cvec = vec;
246 va.va_dmaaddr = sc->sc_dmaaddr;
247 va.va_dmasize = sc->sc_dmasize;
248 *sc->sc_intmsk = c; /* Allow interrupts during attach */
249 config_attach(parent, cf, &va, vsbus_print);
250 *sc->sc_intmsk = 0;
251 return 0;
252
253 fail:
254 printf("%s%d at %s csr 0x%x %s\n",
255 cf->cf_name, cf->cf_unit, parent->dv_xname,
256 cf->cf_loc[0], (i ? "zero vector" : "didn't interrupt"));
257 forgetit:
258 return 0;
259 }
260
261 /*
262 * Sets a new interrupt mask. Returns the old one.
263 * Works like spl functions.
264 */
265 unsigned char
266 vsbus_setmask(mask)
267 unsigned char mask;
268 {
269 struct vsbus_softc *sc;
270 unsigned char ch;
271
272 if (vsbus_cd.cd_ndevs == 0)
273 return 0;
274 sc = vsbus_cd.cd_devs[0];
275
276 ch = *sc->sc_intmsk;
277 *sc->sc_intmsk = mask;
278 return ch;
279 }
280
281 /*
282 * Clears the interrupts in mask.
283 */
284 void
285 vsbus_clrintr(mask)
286 unsigned char mask;
287 {
288 struct vsbus_softc *sc;
289
290 if (vsbus_cd.cd_ndevs == 0)
291 return;
292 sc = vsbus_cd.cd_devs[0];
293
294 *sc->sc_intclr = mask;
295 }
296
297 /*
298 * Copy data from/to a user process' space from the DMA area.
299 * Use the physical memory directly.
300 */
301 void
302 vsbus_copytoproc(struct proc *p, caddr_t from, caddr_t to, int len)
303 {
304 struct pte *pte;
305 paddr_t pa;
306
307 if ((vaddr_t)to & KERNBASE) { /* In kernel space */
308 bcopy(from, to, len);
309 return;
310 }
311 if ((long)to & 0x40000000)
312 pte = &p->p_vmspace->vm_map.pmap->pm_p1br[((long)to & ~0x40000000)];
313 else
314 pte = &p->p_vmspace->vm_map.pmap->pm_p0br[(long)to];
315 if ((vaddr_t)to & PGOFSET) {
316 int cz = round_page((vaddr_t)to) - (vaddr_t)to;
317
318 pa = (pte->pg_pfn << VAX_PGSHIFT) | (PAGE_SIZE - cz) | KERNBASE;
319 bcopy(from, (caddr_t)pa, min(cz, len));
320 from += cz;
321 to += cz;
322 len -= cz;
323 pte += 8; /* XXX */
324 }
325 while (len > 0) {
326 pa = (pte->pg_pfn << VAX_PGSHIFT) | KERNBASE;
327 bcopy(from, (caddr_t)pa, min(PAGE_SIZE, len));
328 from += PAGE_SIZE;
329 to += PAGE_SIZE;
330 len -= PAGE_SIZE;
331 pte += 8; /* XXX */
332 }
333 }
334
335 void
336 vsbus_copyfromproc(struct proc *p, caddr_t from, caddr_t to, int len)
337 {
338 struct pte *pte;
339 paddr_t pa;
340
341 if ((vaddr_t)from & KERNBASE) { /* In kernel space */
342 bcopy(from, to, len);
343 return;
344 }
345 if ((long)to & 0x40000000)
346 pte = &p->p_vmspace->vm_map.pmap->pm_p1br[((long)to & ~0x40000000)];
347 else
348 pte = &p->p_vmspace->vm_map.pmap->pm_p0br[(long)to];
349 if ((vaddr_t)from & PGOFSET) {
350 int cz = round_page((vaddr_t)from) - (vaddr_t)from;
351
352 pa = (pte->pg_pfn << VAX_PGSHIFT) | (PAGE_SIZE - cz) | KERNBASE;
353 bcopy((caddr_t)pa, to, min(cz, len));
354 from += cz;
355 to += cz;
356 len -= cz;
357 pte += 8; /* XXX */
358 }
359 while (len > 0) {
360 pa = (pte->pg_pfn << VAX_PGSHIFT) | KERNBASE;
361 bcopy((caddr_t)pa, to, min(PAGE_SIZE, len));
362 from += PAGE_SIZE;
363 to += PAGE_SIZE;
364 len -= PAGE_SIZE;
365 pte += 8; /* XXX */
366 }
367 }
368
369 /*
370 * There can only be one user of the DMA area on VS2k/VS3100 at one
371 * time, so keep track of it here.
372 */
373 static int vsbus_active = 0;
374
375 void
376 vsbus_dma_start(struct vsbus_dma *vd)
377 {
378
379 SIMPLEQ_INSERT_TAIL(&vsbus_dma, vd, vd_q);
380
381 if (vsbus_active == 0)
382 vsbus_dma_intr();
383 }
384
385 void
386 vsbus_dma_intr(void)
387 {
388 struct vsbus_dma *vd;
389
390 vd = SIMPLEQ_FIRST(&vsbus_dma);
391 if (vd == NULL) {
392 vsbus_active = 0;
393 return;
394 }
395 vsbus_active = 1;
396 SIMPLEQ_REMOVE_HEAD(&vsbus_dma, vd_q);
397 (*vd->vd_go)(vd->vd_arg);
398 }
399
400