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