xref: /netbsd-src/sys/arch/vax/vsa/vsbus.c (revision c34236556bea94afcaca1782d7d228301edc3ea0)
1 /*	$NetBSD: vsbus.c,v 1.62 2016/07/07 06:55:39 msaitoh 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.62 2016/07/07 06:55:39 msaitoh 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 		break;
142 #endif
143 
144 	default:
145 		sc->sc_vsregs = vax_map_physmem(VS_REGS, 1);
146 		sc->sc_intreq = (char *)sc->sc_vsregs + 15;
147 		sc->sc_intclr = (char *)sc->sc_vsregs + 15;
148 		sc->sc_intmsk = (char *)sc->sc_vsregs + 12;
149 		if (vax_boardtype == VAX_BTYP_410) {
150 			dbase = KA410_DMA_BASE;
151 			dsize = KA410_DMA_SIZE;
152 		} else {
153 			dbase = KA420_DMA_BASE;
154 			dsize = KA420_DMA_SIZE;
155 			*(char *)(sc->sc_vsregs + 0xe0) = 1; /* Big DMA */
156 		}
157 		sc->sc_dmasize = dsize;
158 		sc->sc_dmaaddr = uvm_km_alloc(kernel_map, dsize, 0,
159 		    UVM_KMF_VAONLY);
160 		ioaccess(sc->sc_dmaaddr, dbase, dsize/VAX_NBPG);
161 		break;
162 	}
163 
164 	SIMPLEQ_INIT(&vsbus_dma);
165 	/*
166 	 * First: find which interrupts we won't care about.
167 	 * There are interrupts that interrupt on a periodic basic
168 	 * that we don't want to interfere with the rest of the
169 	 * interrupt probing.
170 	 */
171 	*sc->sc_intmsk = 0;
172 	*sc->sc_intclr = 0xff;
173 	DELAY(1000000); /* Wait a second */
174 	sc->sc_mask = *sc->sc_intreq;
175 	aprint_normal_dev(self, "interrupt mask %x\n", sc->sc_mask);
176 	/*
177 	 * now check for all possible devices on this "bus"
178 	 */
179 	config_search_ia(vsbus_search, self, "vsbus", NULL);
180 
181 	/* Autoconfig finished, enable interrupts */
182 	*sc->sc_intmsk = ~sc->sc_mask;
183 }
184 
185 int
186 vsbus_search(device_t parent, cfdata_t cf, const int *ldesc, void *aux)
187 {
188 	struct vsbus_softc *sc = device_private(parent);
189 	struct vsbus_attach_args va;
190 	int i, vec, br;
191 	u_char c;
192 
193 	va.va_paddr = cf->cf_loc[VSBUSCF_CSR];
194 	va.va_addr = vax_map_physmem(va.va_paddr, 1);
195 	va.va_dmat = &sc->sc_dmatag;
196 	va.va_memt = sc->sc_iot;
197 
198 	*sc->sc_intmsk = 0;
199 	*sc->sc_intclr = 0xff;
200 	scb_vecref(0, 0); /* Clear vector ref */
201 
202 	i = config_match(parent, cf, &va);
203 	vax_unmap_physmem(va.va_addr, 1);
204 	c = *sc->sc_intreq & ~sc->sc_mask;
205 	if (i == 0)
206 		goto forgetit;
207 	if (i > 10)
208 		c = sc->sc_mask; /* Fooling interrupt */
209 	else if (c == 0)
210 		goto forgetit;
211 
212 	*sc->sc_intmsk = c;
213 	DELAY(100);
214 	*sc->sc_intmsk = 0;
215 	va.va_maskno = ffs((u_int)c);
216 	i = scb_vecref(&vec, &br);
217 	if (i == 0)
218 		goto fail;
219 	if (vec == 0)
220 		goto fail;
221 
222 	va.va_br = br;
223 	va.va_cvec = vec;
224 	va.va_dmaaddr = sc->sc_dmaaddr;
225 	va.va_dmasize = sc->sc_dmasize;
226 	*sc->sc_intmsk = c; /* Allow interrupts during attach */
227 	config_attach(parent, cf, &va, vsbus_print);
228 	*sc->sc_intmsk = 0;
229 	return 0;
230 
231 fail:
232 	printf("%s%d at %s csr 0x%x %s\n",
233 	    cf->cf_name, cf->cf_unit, device_xname(parent),
234 	    cf->cf_loc[VSBUSCF_CSR], (i ? "zero vector" : "didn't interrupt"));
235 forgetit:
236 	return 0;
237 }
238 
239 /*
240  * Sets a new interrupt mask. Returns the old one.
241  * Works like spl functions.
242  */
243 unsigned char
244 vsbus_setmask(int mask)
245 {
246 	struct vsbus_softc * const sc = device_lookup_private(&vsbus_cd, 0);
247 	unsigned char ch;
248 
249 	if (sc == NULL)
250 		return 0;
251 
252 	ch = *sc->sc_intmsk;
253 	*sc->sc_intmsk = mask;
254 	return ch;
255 }
256 
257 /*
258  * Clears the interrupts in mask.
259  */
260 void
261 vsbus_clrintr(int mask)
262 {
263 	struct vsbus_softc * const sc = device_lookup_private(&vsbus_cd, 0);
264 	if (sc == NULL)
265 		return;
266 
267 	*sc->sc_intclr = mask;
268 }
269 
270 /*
271  * Copy data from/to a user process' space from the DMA area.
272  * Use the physical memory directly.
273  */
274 void
275 vsbus_copytoproc(struct proc *p, void *fromv, void *tov, int len)
276 {
277 	char *from = fromv, *to = tov;
278 	struct pte *pte;
279 	paddr_t pa;
280 
281 	if ((vaddr_t)to & KERNBASE) { /* In kernel space */
282 		memcpy(to, from, len);
283 		return;
284 	}
285 
286 #ifdef DIAGNOSTIC
287 	if (p == NULL)
288 		panic("vsbus_copytoproc: no proc");
289 #endif
290 
291 	if ((vaddr_t)to & 0x40000000)
292 		pte = &p->p_vmspace->vm_map.pmap->pm_p1br[vax_btop((vaddr_t)to & ~0x40000000)];
293 	else
294 		pte = &p->p_vmspace->vm_map.pmap->pm_p0br[vax_btop((vaddr_t)to)];
295 	if ((vaddr_t)to & PGOFSET) {
296 		int cz = round_page((vaddr_t)to) - (vaddr_t)to;
297 
298 		pa = (pte->pg_pfn << VAX_PGSHIFT) | (PAGE_SIZE - cz) | KERNBASE;
299 		memcpy((void *)pa, from, min(cz, len));
300 		from += cz;
301 		to += cz;
302 		len -= cz;
303 		pte += 8; /* XXX */
304 	}
305 	while (len > 0) {
306 		pa = (pte->pg_pfn << VAX_PGSHIFT) | KERNBASE;
307 		memcpy((void *)pa, from, min(PAGE_SIZE, len));
308 		from += PAGE_SIZE;
309 		to += PAGE_SIZE;
310 		len -= PAGE_SIZE;
311 		pte += 8; /* XXX */
312 	}
313 }
314 
315 void
316 vsbus_copyfromproc(struct proc *p, void *fromv, void *tov, int len)
317 {
318 	char *from = fromv, *to = tov;
319 	struct pte *pte;
320 	paddr_t pa;
321 
322 	if ((vaddr_t)from & KERNBASE) { /* In kernel space */
323 		memcpy(to, from, len);
324 		return;
325 	}
326 
327 #ifdef DIAGNOSTIC
328 	if (p == NULL)
329 		panic("vsbus_copyfromproc: no proc");
330 #endif
331 
332 	if ((vaddr_t)from & 0x40000000)
333 		pte = &p->p_vmspace->vm_map.pmap->pm_p1br[vax_btop((vaddr_t)from & ~0x40000000)];
334 	else
335 		pte = &p->p_vmspace->vm_map.pmap->pm_p0br[vax_btop((vaddr_t)from)];
336 	if ((vaddr_t)from & PGOFSET) {
337 		int cz = round_page((vaddr_t)from) - (vaddr_t)from;
338 
339 		pa = (pte->pg_pfn << VAX_PGSHIFT) | (PAGE_SIZE - cz) | KERNBASE;
340 		memcpy(to, (void *)pa, min(cz, len));
341 		from += cz;
342 		to += cz;
343 		len -= cz;
344 		pte += 8; /* XXX */
345 	}
346 	while (len > 0) {
347 		pa = (pte->pg_pfn << VAX_PGSHIFT) | KERNBASE;
348 		memcpy(to,  (void *)pa, min(PAGE_SIZE, len));
349 		from += PAGE_SIZE;
350 		to += PAGE_SIZE;
351 		len -= PAGE_SIZE;
352 		pte += 8; /* XXX */
353 	}
354 }
355 
356 /*
357  * There can only be one user of the DMA area on VS2k/VS3100 at one
358  * time, so keep track of it here.
359  */
360 static int vsbus_active = 0;
361 
362 void
363 vsbus_dma_start(struct vsbus_dma *vd)
364 {
365 
366 	SIMPLEQ_INSERT_TAIL(&vsbus_dma, vd, vd_q);
367 
368 	if (vsbus_active == 0)
369 		vsbus_dma_intr();
370 }
371 
372 void
373 vsbus_dma_intr(void)
374 {
375 	struct vsbus_dma *vd;
376 
377 	vd = SIMPLEQ_FIRST(&vsbus_dma);
378 	if (vd == NULL) {
379 		vsbus_active = 0;
380 		return;
381 	}
382 	vsbus_active = 1;
383 	SIMPLEQ_REMOVE_HEAD(&vsbus_dma, vd_q);
384 	(*vd->vd_go)(vd->vd_arg);
385 }
386 
387