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