Home | History | Annotate | Line # | Download | only in vsa
vsbus.c revision 1.26
      1 /*	$NetBSD: vsbus.c,v 1.26 2000/06/18 22:47:19 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 <vm/vm.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	__P((struct device *, struct cfdata *, void *));
     72 void	vsbus_attach	__P((struct device *, struct device *, void *));
     73 int	vsbus_print	__P((void *, const char *));
     74 int	vsbus_search	__P((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 
    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 (vax_bustype == VAX_VSBUS)
    123 		return 1;
    124 	return 0;
    125 }
    126 
    127 void
    128 vsbus_attach(parent, self, aux)
    129 	struct	device	*parent, *self;
    130 	void	*aux;
    131 {
    132 	struct	vsbus_softc *sc = (void *)self;
    133 
    134 	printf("\n");
    135 
    136 	sc->sc_dmatag = vsbus_bus_dma_tag;
    137 
    138 	switch (vax_boardtype) {
    139 #if VAX49
    140 	case VAX_BTYP_49:
    141 		sc->sc_vsregs = vax_map_physmem(0x25c00000, 1);
    142 		sc->sc_intreq = (char *)sc->sc_vsregs + 12;
    143 		sc->sc_intclr = (char *)sc->sc_vsregs + 12;
    144 		sc->sc_intmsk = (char *)sc->sc_vsregs + 8;
    145 		vsbus_dma_init(sc, 8192);
    146 		break;
    147 #endif
    148 
    149 #if VAX46 || VAX48
    150 	case VAX_BTYP_48:
    151 	case VAX_BTYP_46:
    152 		sc->sc_vsregs = vax_map_physmem(VS_REGS, 1);
    153 		sc->sc_intreq = (char *)sc->sc_vsregs + 15;
    154 		sc->sc_intclr = (char *)sc->sc_vsregs + 15;
    155 		sc->sc_intmsk = (char *)sc->sc_vsregs + 12;
    156 		vsbus_dma_init(sc, 32768);
    157 #endif
    158 
    159 	default:
    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 		break;
    165 	}
    166 
    167 	/*
    168 	 * First: find which interrupts we won't care about.
    169 	 * There are interrupts that interrupt on a periodic basic
    170 	 * that we don't want to interfere with the rest of the
    171 	 * interrupt probing.
    172 	 */
    173 	*sc->sc_intmsk = 0;
    174 	*sc->sc_intclr = 0xff;
    175 	DELAY(1000000); /* Wait a second */
    176 	sc->sc_mask = *sc->sc_intreq;
    177 	printf("%s: interrupt mask %x\n", self->dv_xname, sc->sc_mask);
    178 	/*
    179 	 * now check for all possible devices on this "bus"
    180 	 */
    181 	config_search(vsbus_search, self, NULL);
    182 
    183 	/* Autoconfig finished, enable interrupts */
    184 	*sc->sc_intmsk = ~sc->sc_mask;
    185 }
    186 
    187 int
    188 vsbus_search(parent, cf, aux)
    189 	struct device *parent;
    190 	struct cfdata *cf;
    191 	void *aux;
    192 {
    193 	struct	vsbus_softc *sc = (void *)parent;
    194 	struct	vsbus_attach_args va;
    195 	int i, vec, br;
    196 	u_char c;
    197 
    198 	va.va_paddr = cf->cf_loc[0];
    199 	va.va_addr = vax_map_physmem(va.va_paddr, 1);
    200 	va.va_dmat = &sc->sc_dmatag;
    201 	va.va_iot = &vax_mem_bus_space;
    202 
    203 	*sc->sc_intmsk = 0;
    204 	*sc->sc_intclr = 0xff;
    205 	scb_vecref(0, 0); /* Clear vector ref */
    206 
    207 	i = (*cf->cf_attach->ca_match) (parent, cf, &va);
    208 	vax_unmap_physmem(va.va_addr, 1);
    209 	c = *sc->sc_intreq & ~sc->sc_mask;
    210 	if (i == 0)
    211 		goto forgetit;
    212 	if (i > 10)
    213 		c = sc->sc_mask; /* Fooling interrupt */
    214 	else if (c == 0)
    215 		goto forgetit;
    216 
    217 	*sc->sc_intmsk = c;
    218 	DELAY(100);
    219 	*sc->sc_intmsk = 0;
    220 	va.va_maskno = ffs((u_int)c);
    221 	i = scb_vecref(&vec, &br);
    222 	if (i == 0)
    223 		goto fail;
    224 	if (vec == 0)
    225 		goto fail;
    226 
    227 	va.va_br = br;
    228 	va.va_cvec = vec;
    229 
    230 	config_attach(parent, cf, &va, vsbus_print);
    231 	return 0;
    232 
    233 fail:
    234 	printf("%s%d at %s csr %x %s\n",
    235 	    cf->cf_driver->cd_name, cf->cf_unit, parent->dv_xname,
    236 	    cf->cf_loc[0], (i ? "zero vector" : "didn't interrupt"));
    237 forgetit:
    238 	return 0;
    239 }
    240 
    241 /*
    242  * Sets a new interrupt mask. Returns the old one.
    243  * Works like spl functions.
    244  */
    245 unsigned char
    246 vsbus_setmask(mask)
    247 	unsigned char mask;
    248 {
    249 	struct vsbus_softc *sc;
    250 	unsigned char ch;
    251 
    252 	if (vsbus_cd.cd_ndevs == 0)
    253 		return 0;
    254 	sc = vsbus_cd.cd_devs[0];
    255 
    256 	ch = *sc->sc_intmsk;
    257 	*sc->sc_intmsk = mask;
    258 	return ch;
    259 }
    260 
    261 /*
    262  * Clears the interrupts in mask.
    263  */
    264 void
    265 vsbus_clrintr(mask)
    266 	unsigned char mask;
    267 {
    268 	struct vsbus_softc *sc;
    269 
    270 	if (vsbus_cd.cd_ndevs == 0)
    271 		return;
    272 	sc = vsbus_cd.cd_devs[0];
    273 
    274 	*sc->sc_intclr = mask;
    275 }
    276 
    277 /*
    278  * Copy data from/to a user process' space from the DMA area.
    279  * Use the physical memory directly.
    280  */
    281 void
    282 vsbus_copytoproc(p, from, to, len)
    283 	struct proc *p;
    284 	caddr_t from, to;
    285 	int len;
    286 {
    287 	struct pte *pte;
    288 	paddr_t pa;
    289 
    290 	pte = uvtopte(TRUNC_PAGE(to), (&p->p_addr->u_pcb));
    291 	if ((vaddr_t)to & PGOFSET) {
    292 		int cz = ROUND_PAGE(to) - (vaddr_t)to;
    293 
    294 		pa = (pte->pg_pfn << VAX_PGSHIFT) | (NBPG - cz) | KERNBASE;
    295 		bcopy(from, (caddr_t)pa, min(cz, len));
    296 		from += cz;
    297 		to += cz;
    298 		len -= cz;
    299 		pte += 8; /* XXX */
    300 	}
    301 	while (len > 0) {
    302 		pa = (pte->pg_pfn << VAX_PGSHIFT) | KERNBASE;
    303 		bcopy(from, (caddr_t)pa, min(NBPG, len));
    304 		from += NBPG;
    305 		to += NBPG;
    306 		len -= NBPG;
    307 		pte += 8; /* XXX */
    308 	}
    309 }
    310 
    311 void
    312 vsbus_copyfromproc(p, from, to, len)
    313 	struct proc *p;
    314 	caddr_t from, to;
    315 	int len;
    316 {
    317 	struct pte *pte;
    318 	paddr_t pa;
    319 
    320 	pte = uvtopte(TRUNC_PAGE(from), (&p->p_addr->u_pcb));
    321 	if ((vaddr_t)from & PGOFSET) {
    322 		int cz = ROUND_PAGE(from) - (vaddr_t)from;
    323 
    324 		pa = (pte->pg_pfn << VAX_PGSHIFT) | (NBPG - cz) | KERNBASE;
    325 		bcopy((caddr_t)pa, to, min(cz, len));
    326 		from += cz;
    327 		to += cz;
    328 		len -= cz;
    329 		pte += 8; /* XXX */
    330 	}
    331 	while (len > 0) {
    332 		pa = (pte->pg_pfn << VAX_PGSHIFT) | KERNBASE;
    333 		bcopy((caddr_t)pa, to, min(NBPG, len));
    334 		from += NBPG;
    335 		to += NBPG;
    336 		len -= NBPG;
    337 		pte += 8; /* XXX */
    338 	}
    339 }
    340