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