Home | History | Annotate | Line # | Download | only in dev
obio.c revision 1.7
      1 /*	$NetBSD: obio.c,v 1.7 1994/11/02 23:16:28 deraadt Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1993, 1994 Theo de Raadt
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *	This product includes software developed by Theo de Raadt.
     18  * 4. The name of the author may not be used to endorse or promote products
     19  *    derived from this software without specific prior written permission.
     20  *
     21  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     22  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     23  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     24  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     26  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     30  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     31  */
     32 
     33 #include <sys/param.h>
     34 #include <sys/device.h>
     35 #include <sys/malloc.h>
     36 
     37 #ifdef DEBUG
     38 #include <sys/proc.h>
     39 #include <sys/syslog.h>
     40 #endif
     41 
     42 #include <vm/vm.h>
     43 
     44 #include <machine/autoconf.h>
     45 #include <machine/pmap.h>
     46 #include <machine/oldmon.h>
     47 #include <machine/cpu.h>
     48 #include <machine/ctlreg.h>
     49 #include <sparc/sparc/asm.h>
     50 #include <sparc/sparc/vaddrs.h>
     51 
     52 struct bus_softc {
     53 	struct	device sc_dev;		/* base device */
     54 	int	nothing;
     55 };
     56 
     57 /* autoconfiguration driver */
     58 static int	busmatch(struct device *, struct cfdata *, void *);
     59 static void	obioattach(struct device *, struct device *, void *);
     60 static void	vmesattach(struct device *, struct device *, void *);
     61 static void	vmelattach(struct device *, struct device *, void *);
     62 
     63 struct cfdriver obiocd = { NULL, "obio", busmatch, obioattach,
     64 	DV_DULL, sizeof(struct bus_softc)
     65 };
     66 struct cfdriver vmelcd = { NULL, "vmel", busmatch, vmelattach,
     67 	DV_DULL, sizeof(struct bus_softc)
     68 };
     69 struct cfdriver vmescd = { NULL, "vmes", busmatch, vmesattach,
     70 	DV_DULL, sizeof(struct bus_softc)
     71 };
     72 
     73 static void	busattach(struct device *, struct device *, void *, int);
     74 
     75 void *		bus_map __P((void *, int, int));
     76 void *		bus_tmp __P((void *, int));
     77 void		bus_untmp __P((void));
     78 
     79 int
     80 busmatch(parent, cf, aux)
     81 	struct device *parent;
     82 	struct cfdata *cf;
     83 	void *aux;
     84 {
     85 	register struct confargs *ca = aux;
     86 	register struct romaux *ra = &ca->ca_ra;
     87 
     88 	if (cputyp != CPU_SUN4)
     89 		return (0);
     90 	return (strcmp(cf->cf_driver->cd_name, ra->ra_name) == 0);
     91 }
     92 
     93 int
     94 busprint(args, obio)
     95 	void *args;
     96 	char *obio;
     97 {
     98 	register struct confargs *ca = args;
     99 
    100 	if (ca->ca_ra.ra_name == NULL)
    101 		ca->ca_ra.ra_name = "<unknown>";
    102 	if (obio)
    103 		printf("[%s at %s]", ca->ca_ra.ra_name, obio);
    104 	printf(" addr %x", ca->ca_ra.ra_paddr);
    105 	if (ca->ca_ra.ra_intr[0].int_pri != -1)
    106 		printf(" pri %d", ca->ca_ra.ra_intr[0].int_pri);
    107 	if (ca->ca_ra.ra_intr[0].int_vec != -1)
    108 		printf(" vec 0x%x", ca->ca_ra.ra_intr[0].int_vec);
    109 	return (UNCONF);
    110 }
    111 
    112 void
    113 busattach(parent, self, args, bustype)
    114 	struct device *parent, *self;
    115 	void *args;
    116 	int bustype;
    117 {
    118 	register struct bus_softc *sc = (struct bus_softc *)self;
    119 	extern struct cfdata cfdata[];
    120 	register struct confargs *ca = args;
    121 	struct confargs oca;
    122 	register short *p;
    123 	struct cfdata *cf;
    124 	caddr_t tmp;
    125 
    126 	if (sc->sc_dev.dv_unit > 0) {
    127 		printf(" unsupported\n");
    128 		return;
    129 	}
    130 
    131 	printf("\n");
    132 
    133 	for (cf = cfdata; cf->cf_driver; cf++) {
    134 		if (cf->cf_fstate == FSTATE_FOUND)
    135 			continue;
    136 		for (p = cf->cf_parents; *p >= 0; p++)
    137 			if (self->dv_cfdata == &cfdata[*p]) {
    138 				oca.ca_ra.ra_iospace = -1;
    139 				oca.ca_ra.ra_paddr = (void *)cf->cf_loc[0];
    140 				oca.ca_ra.ra_len = 0;
    141 				tmp = NULL;
    142 				if (oca.ca_ra.ra_paddr)
    143 					tmp = bus_tmp(oca.ca_ra.ra_paddr,
    144 					    bustype);
    145 				oca.ca_ra.ra_vaddr = tmp;
    146 				oca.ca_ra.ra_intr[0].int_pri = cf->cf_loc[1];
    147 				if (bustype == BUS_VME16 || bustype == BUS_VME32)
    148 					oca.ca_ra.ra_intr[0].int_vec = cf->cf_loc[2];
    149 				else
    150 					oca.ca_ra.ra_intr[0].int_vec = 0;
    151 				oca.ca_ra.ra_nintr = 1;
    152 				oca.ca_ra.ra_name = cf->cf_driver->cd_name;
    153 				oca.ca_ra.ra_bp = ca->ca_ra.ra_bp;
    154 				oca.ca_bustype = bustype;
    155 
    156 				if ((*cf->cf_driver->cd_match)(self, cf, &oca) == 0)
    157 					continue;
    158 
    159 				/*
    160 				 * check if XXmatch routine replaced the
    161 				 * temporary mapping with a real mapping.
    162 				 */
    163 				if (tmp == oca.ca_ra.ra_vaddr)
    164 					oca.ca_ra.ra_vaddr = NULL;
    165 				/*
    166 				 * or if it has asked us to create a mapping..
    167 				 * (which won't be seen on future XXmatch calls,
    168 				 * so not as useful as it seems.)
    169 				 */
    170 				if (oca.ca_ra.ra_len)
    171 					oca.ca_ra.ra_vaddr =
    172 					    bus_map(oca.ca_ra.ra_paddr,
    173 					    oca.ca_ra.ra_len, oca.ca_bustype);
    174 
    175 				config_attach(self, cf, &oca, busprint);
    176 			}
    177 	}
    178 	bus_untmp();
    179 }
    180 
    181 void
    182 obioattach(parent, self, args)
    183 	struct device *parent, *self;
    184 	void *args;
    185 {
    186 	busattach(parent, self, args, BUS_OBIO);
    187 }
    188 
    189 struct intrhand **vmeints;
    190 
    191 void
    192 vmesattach(parent, self, args)
    193 	struct device *parent, *self;
    194 	void *args;
    195 {
    196 	if (vmeints == NULL) {
    197 		vmeints = (struct intrhand **)malloc(256 *
    198 		    sizeof(struct intrhand *), M_TEMP, M_NOWAIT);
    199 		bzero(vmeints, 256 * sizeof(struct intrhand *));
    200 	}
    201 	busattach(parent, self, args, BUS_VME16);
    202 }
    203 
    204 void
    205 vmelattach(parent, self, args)
    206 	struct device *parent, *self;
    207 	void *args;
    208 {
    209 	if (vmeints == NULL) {
    210 		vmeints = (struct intrhand **)malloc(256 *
    211 		    sizeof(struct intrhand *), M_TEMP, M_NOWAIT);
    212 		bzero(vmeints, 256 * sizeof(struct intrhand *));
    213 	}
    214 	busattach(parent, self, args, BUS_VME32);
    215 }
    216 
    217 int pil_to_vme[] = {
    218 	-1,	/* pil 0 */
    219 	-1,	/* pil 1 */
    220 	1,	/* pil 2 */
    221 	2,	/* pil 3 */
    222 	-1,	/* pil 4 */
    223 	3,	/* pil 5 */
    224 	-1,	/* pil 6 */
    225 	4,	/* pil 7 */
    226 	-1,	/* pil 8 */
    227 	5,	/* pil 9 */
    228 	-1,	/* pil 10 */
    229 	6,	/* pil 11 */
    230 	-1,	/* pil 12 */
    231 	7,	/* pil 13 */
    232 	-1,	/* pil 14 */
    233 	-1,	/* pil 15 */
    234 };
    235 
    236 int
    237 vmeintr(arg)
    238 	void *arg;
    239 {
    240 	int level = (int)arg, vec;
    241 	struct intrhand *ih;
    242 	int i = 0;
    243 
    244 	vec = ldcontrolb(AC_VMEINTVEC | (pil_to_vme[level] << 1) | 1);
    245 	if (vec == -1) {
    246 		printf("vme: spurious interrupt\n");
    247 		return 0;
    248 	}
    249 
    250 	for (ih = vmeints[vec]; ih; ih = ih->ih_next)
    251 		if (ih->ih_fun)
    252 			i += (ih->ih_fun)(ih->ih_arg);
    253 	return (i);
    254 }
    255 
    256 void
    257 vmeintr_establish(vec, level, ih)
    258 	int vec, level;
    259 	struct intrhand *ih;
    260 {
    261 	struct intrhand *ihs;
    262 
    263 	if (vmeints[vec] == NULL)
    264 		vmeints[vec] = ih;
    265 	else {
    266 		for (ihs = vmeints[vec]; ihs->ih_next; ihs = ihs->ih_next)
    267 			;
    268 		ihs->ih_next = ih;
    269 	}
    270 
    271 	/* ensure the interrupt subsystem will call us at this level */
    272 	for (ihs = intrhand[level]; ihs; ihs = ihs->ih_next)
    273 		if (ihs->ih_fun == vmeintr)
    274 			return;
    275 
    276 	ihs = (struct intrhand *)malloc(sizeof(struct intrhand),
    277 	    M_TEMP, M_NOWAIT);
    278 	if (ihs == NULL)
    279 		panic("vme_addirq");
    280 	bzero(ihs, sizeof *ihs);
    281 	ihs->ih_fun = vmeintr;
    282 	ihs->ih_arg = (void *)level;
    283 	intr_establish(level, ihs);
    284 }
    285 
    286 #define	getpte(va)		lda(va, ASI_PTE)
    287 
    288 /*
    289  * If we can find a mapping that was established by the rom, use it.
    290  * Else, create a new mapping.
    291  */
    292 void *
    293 bus_map(pa, len, bustype)
    294 	void *pa;
    295 	int len;
    296 	int bustype;
    297 {
    298 	u_long	pf = (u_long)pa >> PGSHIFT;
    299 	u_long	va, pte;
    300 	int pgtype;
    301 
    302 	switch (bt2pmt[bustype]) {
    303 	case PMAP_OBIO:
    304 		pgtype = PG_OBIO;
    305 		break;
    306 	case PMAP_VME32:
    307 		pgtype = PG_VME32;
    308 		break;
    309 	case PMAP_VME16:
    310 		pgtype = PG_VME16;
    311 		break;
    312 	}
    313 
    314 	if (len <= NBPG) {
    315 		for (va = OLDMON_STARTVADDR; va < OLDMON_ENDVADDR; va += NBPG) {
    316 			pte = getpte(va);
    317 			if ((pte & PG_V) != 0 && (pte & PG_TYPE) == pgtype &&
    318 			    (pte & PG_PFNUM) == pf)
    319 				return ((void *)va);
    320 		}
    321 	}
    322 	return mapiodev(pa, len, bustype);
    323 }
    324 
    325 void *
    326 bus_tmp(pa, bustype)
    327 	void *pa;
    328 	int bustype;
    329 {
    330 	vm_offset_t addr = (vm_offset_t)pa & ~PGOFSET;
    331 	int pmtype = bt2pmt[bustype];
    332 
    333 	pmap_enter(kernel_pmap, TMPMAP_VA,
    334 	    addr | pmtype | PMAP_NC,
    335 	    VM_PROT_READ | VM_PROT_WRITE, 1);
    336 	return ((void *)TMPMAP_VA);
    337 }
    338 
    339 void
    340 bus_untmp()
    341 {
    342 	pmap_remove(kernel_pmap, TMPMAP_VA, TMPMAP_VA+NBPG);
    343 }
    344 
    345 void
    346 wzero(b, l)
    347 	char *b;
    348 	int l;
    349 {
    350 	register char *be = b + l;
    351 	register short *sp;
    352 
    353 	if (l <= 0)
    354 		return;
    355 
    356 	/* front, */
    357 	if ((u_long)b & 1)
    358 		*b++ = 0;
    359 
    360 	/* back, */
    361 	if (b != be && ((u_long)be & 1) != 0) {
    362 		be--;
    363 		*be = 0;
    364 	}
    365 
    366 	/* and middle. */
    367 	sp = (u_short *)b;
    368 	while (sp != (u_short *)be)
    369 		*sp++ = 0;
    370 }
    371 
    372 void
    373 wcopy(b1, b2, l)
    374 	u_char *b1, *b2;
    375 	u_long l;
    376 {
    377 	register u_short *sp;
    378 	register int bstore = 0;
    379 	register u_char *b1e;
    380 
    381 	if (l <= 0)
    382 		return;
    383 
    384 	/* front, */
    385 	if ((u_long)b1 & 1) {
    386 		*b2++ = *b1++;
    387 		l--;
    388 	}
    389 
    390 	/* middle, */
    391 	sp = (u_short *)b1;
    392 	b1e = b1 + l;
    393 	if (l & 1)
    394 		b1e--;
    395 	bstore = (u_long)b2 & 1;
    396 
    397 	while (sp < (u_short *)b1e) {
    398 		if (bstore) {
    399 			b2[1] = *sp & 0xff;
    400 			b2[0] = *sp >> 8;
    401 		} else
    402 			*((short *)b2) = *sp;
    403 		sp++;
    404 		b2 += 2;
    405 	}
    406 
    407 	/* and back. */
    408 	if (l & 1)
    409 		*b2 = *b1e;
    410 }
    411