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