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