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