xref: /netbsd/sys/arch/vax/vsa/vsbus.c (revision bf9ec67e)
1 /*	$NetBSD: vsbus.c,v 1.32 2001/05/16 05:36:56 matt 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/map.h>
44 #include <sys/device.h>
45 #include <sys/dkstat.h>
46 #include <sys/disklabel.h>
47 #include <sys/syslog.h>
48 #include <sys/stat.h>
49 
50 #include <uvm/uvm_extern.h>
51 
52 #define _VAX_BUS_DMA_PRIVATE
53 #include <machine/bus.h>
54 #include <machine/pte.h>
55 #include <machine/sid.h>
56 #include <machine/scb.h>
57 #include <machine/cpu.h>
58 #include <machine/trap.h>
59 #include <machine/nexus.h>
60 
61 #include <machine/uvax.h>
62 #include <machine/ka410.h>
63 #include <machine/ka420.h>
64 #include <machine/ka43.h>
65 
66 #include <machine/vsbus.h>
67 
68 #include "ioconf.h"
69 #include "opt_cputype.h"
70 
71 int	vsbus_match(struct device *, struct cfdata *, void *);
72 void	vsbus_attach(struct device *, struct device *, void *);
73 int	vsbus_print(void *, const char *);
74 int	vsbus_search(struct device *, struct cfdata *, void *);
75 
76 static struct vax_bus_dma_tag vsbus_bus_dma_tag = {
77 	0,
78 	0,
79 	0,
80 	0,
81 	0,
82 	0,
83 	_bus_dmamap_create,
84 	_bus_dmamap_destroy,
85 	_bus_dmamap_load,
86 	_bus_dmamap_load_mbuf,
87 	_bus_dmamap_load_uio,
88 	_bus_dmamap_load_raw,
89 	_bus_dmamap_unload,
90 	_bus_dmamap_sync,
91 	_bus_dmamem_alloc,
92 	_bus_dmamem_free,
93 	_bus_dmamem_map,
94 	_bus_dmamem_unmap,
95 	_bus_dmamem_mmap,
96 };
97 
98 extern struct vax_bus_space vax_mem_bus_space;
99 static SIMPLEQ_HEAD(, vsbus_dma) vsbus_dma;
100 
101 struct	cfattach vsbus_ca = {
102 	sizeof(struct vsbus_softc), vsbus_match, vsbus_attach
103 };
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 	printf(" 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 = (*cf->cf_attach->ca_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_driver->cd_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 	pte = uvtopte(trunc_page((vaddr_t)to), &p->p_addr->u_pcb);
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) | (NBPG - 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(NBPG, len));
328 		from += NBPG;
329 		to += NBPG;
330 		len -= NBPG;
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 	pte = uvtopte(trunc_page((vaddr_t)from), &p->p_addr->u_pcb);
346 	if ((vaddr_t)from & PGOFSET) {
347 		int cz = round_page((vaddr_t)from) - (vaddr_t)from;
348 
349 		pa = (pte->pg_pfn << VAX_PGSHIFT) | (NBPG - cz) | KERNBASE;
350 		bcopy((caddr_t)pa, to, min(cz, len));
351 		from += cz;
352 		to += cz;
353 		len -= cz;
354 		pte += 8; /* XXX */
355 	}
356 	while (len > 0) {
357 		pa = (pte->pg_pfn << VAX_PGSHIFT) | KERNBASE;
358 		bcopy((caddr_t)pa, to, min(NBPG, len));
359 		from += NBPG;
360 		to += NBPG;
361 		len -= NBPG;
362 		pte += 8; /* XXX */
363 	}
364 }
365 
366 /*
367  * There can only be one user of the DMA area on VS2k/VS3100 at one
368  * time, so keep track of it here.
369  */
370 static int vsbus_active = 0;
371 
372 void
373 vsbus_dma_start(struct vsbus_dma *vd)
374 {
375 
376 	SIMPLEQ_INSERT_TAIL(&vsbus_dma, vd, vd_q);
377 
378 	if (vsbus_active == 0)
379 		vsbus_dma_intr();
380 }
381 
382 void
383 vsbus_dma_intr(void)
384 {
385 	struct vsbus_dma *vd;
386 
387 	vd = SIMPLEQ_FIRST(&vsbus_dma);
388 	if (vd == NULL) {
389 		vsbus_active = 0;
390 		return;
391 	}
392 	vsbus_active = 1;
393 	SIMPLEQ_REMOVE_HEAD(&vsbus_dma, vd, vd_q);
394 	(*vd->vd_go)(vd->vd_arg);
395 }
396 
397