Home | History | Annotate | Line # | Download | only in dev
obio.c revision 1.16
      1 /*	$NetBSD: obio.c,v 1.16 1995/08/18 08:20:26 pk 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 __P((struct device *, void *, void *));
     59 static void	obioattach __P((struct device *, struct device *, void *));
     60 static void	vmesattach __P((struct device *, struct device *, void *));
     61 static void	vmelattach __P((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 int	busattach __P((struct device *, void *, 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, vcf, aux)
     81 	struct device *parent;
     82 	void *vcf, *aux;
     83 {
     84 	struct cfdata *cf = vcf;
     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_vec != -1)
    106 		printf(" vec 0x%x", ca->ca_ra.ra_intr[0].int_vec);
    107 	return (UNCONF);
    108 }
    109 
    110 
    111 int
    112 busattach(parent, child, args, bustype)
    113 	struct device *parent;
    114 	void *args, *child;
    115 	int bustype;
    116 {
    117 	struct cfdata *cf = child;
    118 	register struct bus_softc *sc = (struct bus_softc *)parent;
    119 	register struct confargs *ca = args;
    120 	struct confargs oca;
    121 	caddr_t tmp;
    122 
    123 	if (bustype == BUS_OBIO && cputyp == CPU_SUN4) {
    124 		/*
    125 		 * On the 4/100 obio addresses must be mapped at
    126 		 * 0x0YYYYYYY, but alias higher up (we avoid the
    127 		 * alias condition because it causes pmap difficulties)
    128 		 * XXX: We also assume that 4/[23]00 obio addresses
    129 		 * must be 0xZYYYYYYY, where (Z != 0)
    130 		 */
    131 		if (cpumod==SUN4_100 && (cf->cf_loc[0] & 0xf0000000))
    132 			return 0;
    133 		if (cpumod!=SUN4_100 && !(cf->cf_loc[0] & 0xf0000000))
    134 			return 0;
    135 	}
    136 
    137 	if (parent->dv_cfdata->cf_driver->cd_indirect) {
    138 		printf(" indirect devices not supported\n");
    139 		return 0;
    140 	}
    141 
    142 	oca.ca_ra.ra_iospace = -1;
    143 	oca.ca_ra.ra_paddr = (void *)cf->cf_loc[0];
    144 	oca.ca_ra.ra_len = 0;
    145 	oca.ca_ra.ra_nreg = 1;
    146 	tmp = NULL;
    147 	if (oca.ca_ra.ra_paddr)
    148 		tmp = bus_tmp(oca.ca_ra.ra_paddr,
    149 		    bustype);
    150 	oca.ca_ra.ra_vaddr = tmp;
    151 	oca.ca_ra.ra_intr[0].int_pri = cf->cf_loc[1];
    152 	if (bustype == BUS_VME16 || bustype == BUS_VME32)
    153 		oca.ca_ra.ra_intr[0].int_vec = cf->cf_loc[2];
    154 	else
    155 		oca.ca_ra.ra_intr[0].int_vec = -1;
    156 	oca.ca_ra.ra_nintr = 1;
    157 	oca.ca_ra.ra_name = cf->cf_driver->cd_name;
    158 	if (ca->ca_ra.ra_bp != NULL &&
    159 	  ((bustype == BUS_VME16 && strcmp(ca->ca_ra.ra_bp->name,"vmes") ==0) ||
    160 	   (bustype == BUS_VME32 && strcmp(ca->ca_ra.ra_bp->name,"vmel") ==0) ||
    161 	   (bustype == BUS_OBIO && strcmp(ca->ca_ra.ra_bp->name,"obio") == 0)))
    162 		oca.ca_ra.ra_bp = ca->ca_ra.ra_bp + 1;
    163 	else
    164 		oca.ca_ra.ra_bp = NULL;
    165 	oca.ca_bustype = bustype;
    166 
    167 	if ((*cf->cf_driver->cd_match)(parent, cf, &oca) == 0)
    168 		return 0;
    169 
    170 	/*
    171 	 * check if XXmatch routine replaced the
    172 	 * temporary mapping with a real mapping.
    173 	 */
    174 	if (tmp == oca.ca_ra.ra_vaddr)
    175 		oca.ca_ra.ra_vaddr = NULL;
    176 	/*
    177 	 * or if it has asked us to create a mapping..
    178 	 * (which won't be seen on future XXmatch calls,
    179 	 * so not as useful as it seems.)
    180 	 */
    181 	if (oca.ca_ra.ra_len)
    182 		oca.ca_ra.ra_vaddr =
    183 		    bus_map(oca.ca_ra.ra_paddr,
    184 		    oca.ca_ra.ra_len, oca.ca_bustype);
    185 
    186 	config_attach(parent, cf, &oca, busprint);
    187 	return 1;
    188 }
    189 
    190 int
    191 obio_scan(parent, child, args)
    192 	struct device *parent;
    193 	void *child, *args;
    194 {
    195 	return busattach(parent, child, args, BUS_OBIO);
    196 }
    197 
    198 void
    199 obioattach(parent, self, args)
    200 	struct device *parent, *self;
    201 	void *args;
    202 {
    203 	if (self->dv_unit > 0) {
    204 		printf(" unsupported\n");
    205 		return;
    206 	}
    207 	printf("\n");
    208 
    209 	(void)config_search(obio_scan, self, args);
    210 	bus_untmp();
    211 }
    212 
    213 struct intrhand **vmeints;
    214 
    215 int
    216 vmes_scan(parent, child, args)
    217 	struct device *parent;
    218 	void *child, *args;
    219 {
    220 	return busattach(parent, child, args, BUS_VME16);
    221 }
    222 
    223 void
    224 vmesattach(parent, self, args)
    225 	struct device *parent, *self;
    226 	void *args;
    227 {
    228 	if (self->dv_unit > 0) {
    229 		printf(" unsupported\n");
    230 		return;
    231 	}
    232 	printf("\n");
    233 
    234 	if (vmeints == NULL) {
    235 		vmeints = (struct intrhand **)malloc(256 *
    236 		    sizeof(struct intrhand *), M_TEMP, M_NOWAIT);
    237 		bzero(vmeints, 256 * sizeof(struct intrhand *));
    238 	}
    239 	(void)config_search(vmes_scan, self, args);
    240 	bus_untmp();
    241 }
    242 
    243 int
    244 vmel_scan(parent, child, args)
    245 	struct device *parent;
    246 	void *child, *args;
    247 {
    248 	return busattach(parent, child, args, BUS_VME32);
    249 }
    250 
    251 void
    252 vmelattach(parent, self, args)
    253 	struct device *parent, *self;
    254 	void *args;
    255 {
    256 	if (self->dv_unit > 0) {
    257 		printf(" unsupported\n");
    258 		return;
    259 	}
    260 	printf("\n");
    261 
    262 	if (vmeints == NULL) {
    263 		vmeints = (struct intrhand **)malloc(256 *
    264 		    sizeof(struct intrhand *), M_TEMP, M_NOWAIT);
    265 		bzero(vmeints, 256 * sizeof(struct intrhand *));
    266 	}
    267 	(void)config_search(vmel_scan, self, args);
    268 	bus_untmp();
    269 }
    270 
    271 int pil_to_vme[] = {
    272 	-1,	/* pil 0 */
    273 	-1,	/* pil 1 */
    274 	1,	/* pil 2 */
    275 	2,	/* pil 3 */
    276 	-1,	/* pil 4 */
    277 	3,	/* pil 5 */
    278 	-1,	/* pil 6 */
    279 	4,	/* pil 7 */
    280 	-1,	/* pil 8 */
    281 	5,	/* pil 9 */
    282 	-1,	/* pil 10 */
    283 	6,	/* pil 11 */
    284 	-1,	/* pil 12 */
    285 	7,	/* pil 13 */
    286 	-1,	/* pil 14 */
    287 	-1,	/* pil 15 */
    288 };
    289 
    290 int
    291 vmeintr(arg)
    292 	void *arg;
    293 {
    294 	int level = (int)arg, vec;
    295 	struct intrhand *ih;
    296 	int i = 0;
    297 
    298 	vec = ldcontrolb(AC_VMEINTVEC | (pil_to_vme[level] << 1) | 1);
    299 	if (vec == -1) {
    300 		printf("vme: spurious interrupt\n");
    301 		return 0;
    302 	}
    303 
    304 	for (ih = vmeints[vec]; ih; ih = ih->ih_next)
    305 		if (ih->ih_fun)
    306 			i += (ih->ih_fun)(ih->ih_arg);
    307 	return (i);
    308 }
    309 
    310 void
    311 vmeintr_establish(vec, level, ih)
    312 	int vec, level;
    313 	struct intrhand *ih;
    314 {
    315 	struct intrhand *ihs;
    316 
    317 	if (vec == -1)
    318 		panic("vmeintr_establish: uninitialized vec\n");
    319 
    320 	if (vmeints[vec] == NULL)
    321 		vmeints[vec] = ih;
    322 	else {
    323 		for (ihs = vmeints[vec]; ihs->ih_next; ihs = ihs->ih_next)
    324 			;
    325 		ihs->ih_next = ih;
    326 	}
    327 
    328 	/* ensure the interrupt subsystem will call us at this level */
    329 	for (ihs = intrhand[level]; ihs; ihs = ihs->ih_next)
    330 		if (ihs->ih_fun == vmeintr)
    331 			return;
    332 
    333 	ihs = (struct intrhand *)malloc(sizeof(struct intrhand),
    334 	    M_TEMP, M_NOWAIT);
    335 	if (ihs == NULL)
    336 		panic("vme_addirq");
    337 	bzero(ihs, sizeof *ihs);
    338 	ihs->ih_fun = vmeintr;
    339 	ihs->ih_arg = (void *)level;
    340 	intr_establish(level, ihs);
    341 }
    342 
    343 #define	getpte(va)		lda(va, ASI_PTE)
    344 
    345 /*
    346  * If we can find a mapping that was established by the rom, use it.
    347  * Else, create a new mapping.
    348  */
    349 void *
    350 bus_map(pa, len, bustype)
    351 	void *pa;
    352 	int len;
    353 	int bustype;
    354 {
    355 	u_long	pf = (u_long)pa >> PGSHIFT;
    356 	u_long	va, pte;
    357 	int pgtype;
    358 
    359 	switch (bt2pmt[bustype]) {
    360 	case PMAP_OBIO:
    361 		pgtype = PG_OBIO;
    362 		break;
    363 	case PMAP_VME32:
    364 		pgtype = PG_VME32;
    365 		break;
    366 	case PMAP_VME16:
    367 		pgtype = PG_VME16;
    368 		break;
    369 	}
    370 
    371 	if (len <= NBPG) {
    372 		for (va = OLDMON_STARTVADDR; va < OLDMON_ENDVADDR; va += NBPG) {
    373 			pte = getpte(va);
    374 			if ((pte & PG_V) != 0 && (pte & PG_TYPE) == pgtype &&
    375 			    (pte & PG_PFNUM) == pf)
    376 				return ((void *)va);
    377 		}
    378 	}
    379 	return mapiodev(pa, len, bustype);
    380 }
    381 
    382 void *
    383 bus_tmp(pa, bustype)
    384 	void *pa;
    385 	int bustype;
    386 {
    387 	vm_offset_t addr = (vm_offset_t)pa & ~PGOFSET;
    388 	int pmtype = bt2pmt[bustype];
    389 
    390 	pmap_enter(pmap_kernel(), TMPMAP_VA,
    391 	    addr | pmtype | PMAP_NC,
    392 	    VM_PROT_READ | VM_PROT_WRITE, 1);
    393 	return ((void *)TMPMAP_VA);
    394 }
    395 
    396 void
    397 bus_untmp()
    398 {
    399 	pmap_remove(pmap_kernel(), TMPMAP_VA, TMPMAP_VA+NBPG);
    400 }
    401