xref: /netbsd-src/sys/arch/vax/vsa/vsbus.c (revision da5f4674a3fc214be3572d358b66af40ab9401e7)
1 /*	$NetBSD: vsbus.c,v 1.44 2003/07/15 02:15:07 lukem 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/cdefs.h>
36 __KERNEL_RCSID(0, "$NetBSD: vsbus.c,v 1.44 2003/07/15 02:15:07 lukem Exp $");
37 
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/buf.h>
41 #include <sys/conf.h>
42 #include <sys/file.h>
43 #include <sys/ioctl.h>
44 #include <sys/proc.h>
45 #include <sys/user.h>
46 #include <sys/device.h>
47 #include <sys/disklabel.h>
48 #include <sys/syslog.h>
49 #include <sys/stat.h>
50 
51 #include <uvm/uvm_extern.h>
52 
53 #define _VAX_BUS_DMA_PRIVATE
54 #include <machine/bus.h>
55 #include <machine/pte.h>
56 #include <machine/sid.h>
57 #include <machine/scb.h>
58 #include <machine/cpu.h>
59 #include <machine/trap.h>
60 #include <machine/nexus.h>
61 
62 #include <machine/uvax.h>
63 #include <machine/ka410.h>
64 #include <machine/ka420.h>
65 #include <machine/ka43.h>
66 
67 #include <machine/vsbus.h>
68 
69 #include "ioconf.h"
70 #include "opt_cputype.h"
71 
72 int	vsbus_match(struct device *, struct cfdata *, void *);
73 void	vsbus_attach(struct device *, struct device *, void *);
74 int	vsbus_print(void *, const char *);
75 int	vsbus_search(struct device *, struct cfdata *, void *);
76 
77 static struct vax_bus_dma_tag vsbus_bus_dma_tag = {
78 	0,
79 	0,
80 	0,
81 	0,
82 	0,
83 	0,
84 	_bus_dmamap_create,
85 	_bus_dmamap_destroy,
86 	_bus_dmamap_load,
87 	_bus_dmamap_load_mbuf,
88 	_bus_dmamap_load_uio,
89 	_bus_dmamap_load_raw,
90 	_bus_dmamap_unload,
91 	_bus_dmamap_sync,
92 	_bus_dmamem_alloc,
93 	_bus_dmamem_free,
94 	_bus_dmamem_map,
95 	_bus_dmamem_unmap,
96 	_bus_dmamem_mmap,
97 };
98 
99 extern struct vax_bus_space vax_mem_bus_space;
100 static SIMPLEQ_HEAD(, vsbus_dma) vsbus_dma;
101 
102 CFATTACH_DECL(vsbus, sizeof(struct vsbus_softc),
103     vsbus_match, vsbus_attach, NULL, NULL);
104 
105 int
106 vsbus_print(aux, name)
107 	void *aux;
108 	const char *name;
109 {
110 	struct vsbus_attach_args *va = aux;
111 
112 	aprint_normal(" csr 0x%lx vec %o ipl %x maskbit %d", va->va_paddr,
113 	    va->va_cvec & 511, va->va_br, va->va_maskno - 1);
114 	return(UNCONF);
115 }
116 
117 int
118 vsbus_match(parent, cf, aux)
119 	struct	device	*parent;
120 	struct cfdata	*cf;
121 	void	*aux;
122 {
123 #if VAX53 || VAXANY
124 	/* Kludge: VAX53 is... special */
125 	if (vax_boardtype == VAX_BTYP_53 && (int)aux == 1)
126 		return 1; /* Hack */
127 #endif
128 	if (vax_bustype == VAX_VSBUS)
129 		return 1;
130 	return 0;
131 }
132 
133 void
134 vsbus_attach(parent, self, aux)
135 	struct	device	*parent, *self;
136 	void	*aux;
137 {
138 	struct	vsbus_softc *sc = (void *)self;
139 	int dbase, dsize;
140 
141 	printf("\n");
142 
143 	sc->sc_dmatag = vsbus_bus_dma_tag;
144 
145 	switch (vax_boardtype) {
146 #if VAX49 || VAX53
147 	case VAX_BTYP_53:
148 	case VAX_BTYP_49:
149 		sc->sc_vsregs = vax_map_physmem(0x25c00000, 1);
150 		sc->sc_intreq = (char *)sc->sc_vsregs + 12;
151 		sc->sc_intclr = (char *)sc->sc_vsregs + 12;
152 		sc->sc_intmsk = (char *)sc->sc_vsregs + 8;
153 		vsbus_dma_init(sc, 8192);
154 		break;
155 #endif
156 
157 #if VAX46 || VAX48
158 	case VAX_BTYP_48:
159 	case VAX_BTYP_46:
160 		sc->sc_vsregs = vax_map_physmem(VS_REGS, 1);
161 		sc->sc_intreq = (char *)sc->sc_vsregs + 15;
162 		sc->sc_intclr = (char *)sc->sc_vsregs + 15;
163 		sc->sc_intmsk = (char *)sc->sc_vsregs + 12;
164 		vsbus_dma_init(sc, 32768);
165 #endif
166 
167 	default:
168 		sc->sc_vsregs = vax_map_physmem(VS_REGS, 1);
169 		sc->sc_intreq = (char *)sc->sc_vsregs + 15;
170 		sc->sc_intclr = (char *)sc->sc_vsregs + 15;
171 		sc->sc_intmsk = (char *)sc->sc_vsregs + 12;
172 		if (vax_boardtype == VAX_BTYP_410) {
173 			dbase = KA410_DMA_BASE;
174 			dsize = KA410_DMA_SIZE;
175 		} else {
176 			dbase = KA420_DMA_BASE;
177 			dsize = KA420_DMA_SIZE;
178 			*(char *)(sc->sc_vsregs + 0xe0) = 1; /* Big DMA */
179 		}
180 		sc->sc_dmasize = dsize;
181 		sc->sc_dmaaddr = uvm_km_valloc(kernel_map, dsize);
182 		ioaccess(sc->sc_dmaaddr, dbase, dsize/VAX_NBPG);
183 		break;
184 	}
185 
186 	SIMPLEQ_INIT(&vsbus_dma);
187 	/*
188 	 * First: find which interrupts we won't care about.
189 	 * There are interrupts that interrupt on a periodic basic
190 	 * that we don't want to interfere with the rest of the
191 	 * interrupt probing.
192 	 */
193 	*sc->sc_intmsk = 0;
194 	*sc->sc_intclr = 0xff;
195 	DELAY(1000000); /* Wait a second */
196 	sc->sc_mask = *sc->sc_intreq;
197 	printf("%s: interrupt mask %x\n", self->dv_xname, sc->sc_mask);
198 	/*
199 	 * now check for all possible devices on this "bus"
200 	 */
201 	config_search(vsbus_search, self, NULL);
202 
203 	/* Autoconfig finished, enable interrupts */
204 	*sc->sc_intmsk = ~sc->sc_mask;
205 }
206 
207 int
208 vsbus_search(parent, cf, aux)
209 	struct device *parent;
210 	struct cfdata *cf;
211 	void *aux;
212 {
213 	struct	vsbus_softc *sc = (void *)parent;
214 	struct	vsbus_attach_args va;
215 	int i, vec, br;
216 	u_char c;
217 
218 	va.va_paddr = cf->cf_loc[0];
219 	va.va_addr = vax_map_physmem(va.va_paddr, 1);
220 	va.va_dmat = &sc->sc_dmatag;
221 	va.va_iot = &vax_mem_bus_space;
222 
223 	*sc->sc_intmsk = 0;
224 	*sc->sc_intclr = 0xff;
225 	scb_vecref(0, 0); /* Clear vector ref */
226 
227 	i = config_match(parent, cf, &va);
228 	vax_unmap_physmem(va.va_addr, 1);
229 	c = *sc->sc_intreq & ~sc->sc_mask;
230 	if (i == 0)
231 		goto forgetit;
232 	if (i > 10)
233 		c = sc->sc_mask; /* Fooling interrupt */
234 	else if (c == 0)
235 		goto forgetit;
236 
237 	*sc->sc_intmsk = c;
238 	DELAY(100);
239 	*sc->sc_intmsk = 0;
240 	va.va_maskno = ffs((u_int)c);
241 	i = scb_vecref(&vec, &br);
242 	if (i == 0)
243 		goto fail;
244 	if (vec == 0)
245 		goto fail;
246 
247 	va.va_br = br;
248 	va.va_cvec = vec;
249 	va.va_dmaaddr = sc->sc_dmaaddr;
250 	va.va_dmasize = sc->sc_dmasize;
251 	*sc->sc_intmsk = c; /* Allow interrupts during attach */
252 	config_attach(parent, cf, &va, vsbus_print);
253 	*sc->sc_intmsk = 0;
254 	return 0;
255 
256 fail:
257 	printf("%s%d at %s csr 0x%x %s\n",
258 	    cf->cf_name, cf->cf_unit, parent->dv_xname,
259 	    cf->cf_loc[0], (i ? "zero vector" : "didn't interrupt"));
260 forgetit:
261 	return 0;
262 }
263 
264 /*
265  * Sets a new interrupt mask. Returns the old one.
266  * Works like spl functions.
267  */
268 unsigned char
269 vsbus_setmask(mask)
270 	unsigned char mask;
271 {
272 	struct vsbus_softc *sc;
273 	unsigned char ch;
274 
275 	if (vsbus_cd.cd_ndevs == 0)
276 		return 0;
277 	sc = vsbus_cd.cd_devs[0];
278 
279 	ch = *sc->sc_intmsk;
280 	*sc->sc_intmsk = mask;
281 	return ch;
282 }
283 
284 /*
285  * Clears the interrupts in mask.
286  */
287 void
288 vsbus_clrintr(mask)
289 	unsigned char mask;
290 {
291 	struct vsbus_softc *sc;
292 
293 	if (vsbus_cd.cd_ndevs == 0)
294 		return;
295 	sc = vsbus_cd.cd_devs[0];
296 
297 	*sc->sc_intclr = mask;
298 }
299 
300 /*
301  * Copy data from/to a user process' space from the DMA area.
302  * Use the physical memory directly.
303  */
304 void
305 vsbus_copytoproc(struct proc *p, caddr_t from, caddr_t to, int len)
306 {
307 	struct pte *pte;
308 	paddr_t pa;
309 
310 	if ((vaddr_t)to & KERNBASE) { /* In kernel space */
311 		bcopy(from, to, len);
312 		return;
313 	}
314 	if ((long)to & 0x40000000)
315 		pte = &p->p_vmspace->vm_map.pmap->pm_p1br[((long)to & ~0x40000000)];
316 	else
317 		pte = &p->p_vmspace->vm_map.pmap->pm_p0br[(long)to];
318 	if ((vaddr_t)to & PGOFSET) {
319 		int cz = round_page((vaddr_t)to) - (vaddr_t)to;
320 
321 		pa = (pte->pg_pfn << VAX_PGSHIFT) | (PAGE_SIZE - cz) | KERNBASE;
322 		bcopy(from, (caddr_t)pa, min(cz, len));
323 		from += cz;
324 		to += cz;
325 		len -= cz;
326 		pte += 8; /* XXX */
327 	}
328 	while (len > 0) {
329 		pa = (pte->pg_pfn << VAX_PGSHIFT) | KERNBASE;
330 		bcopy(from, (caddr_t)pa, min(PAGE_SIZE, len));
331 		from += PAGE_SIZE;
332 		to += PAGE_SIZE;
333 		len -= PAGE_SIZE;
334 		pte += 8; /* XXX */
335 	}
336 }
337 
338 void
339 vsbus_copyfromproc(struct proc *p, caddr_t from, caddr_t to, int len)
340 {
341 	struct pte *pte;
342 	paddr_t pa;
343 
344 	if ((vaddr_t)from & KERNBASE) { /* In kernel space */
345 		bcopy(from, to, len);
346 		return;
347 	}
348 	if ((long)to & 0x40000000)
349 		pte = &p->p_vmspace->vm_map.pmap->pm_p1br[((long)to & ~0x40000000)];
350 	else
351 		pte = &p->p_vmspace->vm_map.pmap->pm_p0br[(long)to];
352 	if ((vaddr_t)from & PGOFSET) {
353 		int cz = round_page((vaddr_t)from) - (vaddr_t)from;
354 
355 		pa = (pte->pg_pfn << VAX_PGSHIFT) | (PAGE_SIZE - cz) | KERNBASE;
356 		bcopy((caddr_t)pa, to, min(cz, len));
357 		from += cz;
358 		to += cz;
359 		len -= cz;
360 		pte += 8; /* XXX */
361 	}
362 	while (len > 0) {
363 		pa = (pte->pg_pfn << VAX_PGSHIFT) | KERNBASE;
364 		bcopy((caddr_t)pa, to, min(PAGE_SIZE, len));
365 		from += PAGE_SIZE;
366 		to += PAGE_SIZE;
367 		len -= PAGE_SIZE;
368 		pte += 8; /* XXX */
369 	}
370 }
371 
372 /*
373  * There can only be one user of the DMA area on VS2k/VS3100 at one
374  * time, so keep track of it here.
375  */
376 static int vsbus_active = 0;
377 
378 void
379 vsbus_dma_start(struct vsbus_dma *vd)
380 {
381 
382 	SIMPLEQ_INSERT_TAIL(&vsbus_dma, vd, vd_q);
383 
384 	if (vsbus_active == 0)
385 		vsbus_dma_intr();
386 }
387 
388 void
389 vsbus_dma_intr(void)
390 {
391 	struct vsbus_dma *vd;
392 
393 	vd = SIMPLEQ_FIRST(&vsbus_dma);
394 	if (vd == NULL) {
395 		vsbus_active = 0;
396 		return;
397 	}
398 	vsbus_active = 1;
399 	SIMPLEQ_REMOVE_HEAD(&vsbus_dma, vd_q);
400 	(*vd->vd_go)(vd->vd_arg);
401 }
402 
403