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