Home | History | Annotate | Line # | Download | only in dev
obio.c revision 1.4
      1  1.3  deraadt /*	$NetBSD: obio.c,v 1.4 1994/10/15 05:49:06 deraadt 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.2  deraadt #include <machine/ctlreg.h>
     48  1.2  deraadt #include <sparc/sparc/asm.h>
     49  1.2  deraadt #include <sparc/sparc/vaddrs.h>
     50  1.1  deraadt 
     51  1.4  deraadt struct bus_softc {
     52  1.1  deraadt 	struct	device sc_dev;		/* base device */
     53  1.1  deraadt 	int	nothing;
     54  1.1  deraadt };
     55  1.1  deraadt 
     56  1.1  deraadt /* autoconfiguration driver */
     57  1.4  deraadt static int	busmatch(struct device *, struct cfdata *, void *);
     58  1.1  deraadt static void	obioattach(struct device *, struct device *, void *);
     59  1.4  deraadt static void	vmesattach(struct device *, struct device *, void *);
     60  1.4  deraadt static void	vmelattach(struct device *, struct device *, void *);
     61  1.4  deraadt 
     62  1.4  deraadt struct cfdriver obiocd = { NULL, "obio", busmatch, obioattach,
     63  1.4  deraadt 	DV_DULL, sizeof(struct bus_softc)
     64  1.4  deraadt };
     65  1.4  deraadt struct cfdriver vmelcd = { NULL, "vmel", busmatch, vmelattach,
     66  1.4  deraadt 	DV_DULL, sizeof(struct bus_softc)
     67  1.1  deraadt };
     68  1.4  deraadt struct cfdriver vmescd = { NULL, "vmes", busmatch, vmesattach,
     69  1.4  deraadt 	DV_DULL, sizeof(struct bus_softc)
     70  1.4  deraadt };
     71  1.4  deraadt 
     72  1.4  deraadt static void	busattach(struct device *, struct device *, void *, int);
     73  1.1  deraadt 
     74  1.4  deraadt void *		bus_map __P((void *, int, int));
     75  1.4  deraadt void *		bus_tmp __P((void *, int));
     76  1.4  deraadt void		bus_untmp __P((void));
     77  1.2  deraadt 
     78  1.1  deraadt int
     79  1.4  deraadt busmatch(parent, cf, aux)
     80  1.1  deraadt 	struct device *parent;
     81  1.1  deraadt 	struct cfdata *cf;
     82  1.3  deraadt 	void *aux;
     83  1.1  deraadt {
     84  1.3  deraadt 	register struct confargs *ca = aux;
     85  1.3  deraadt 	register struct romaux *ra = &ca->ca_ra;
     86  1.3  deraadt 
     87  1.1  deraadt 	if (cputyp != CPU_SUN4)
     88  1.3  deraadt 		return (0);
     89  1.3  deraadt 	return (strcmp(cf->cf_driver->cd_name, ra->ra_name) == 0);
     90  1.1  deraadt }
     91  1.1  deraadt 
     92  1.1  deraadt int
     93  1.4  deraadt busprint(args, obio)
     94  1.1  deraadt 	void *args;
     95  1.1  deraadt 	char *obio;
     96  1.1  deraadt {
     97  1.2  deraadt 	register struct confargs *ca = args;
     98  1.2  deraadt 
     99  1.2  deraadt 	if (ca->ca_ra.ra_name == NULL)
    100  1.2  deraadt 		ca->ca_ra.ra_name = "<unknown>";
    101  1.2  deraadt 	if (obio)
    102  1.3  deraadt 		printf("[%s at %s]", ca->ca_ra.ra_name, obio);
    103  1.3  deraadt 	printf(" addr %x", ca->ca_ra.ra_paddr);
    104  1.1  deraadt 	return (UNCONF);
    105  1.1  deraadt }
    106  1.1  deraadt 
    107  1.1  deraadt void
    108  1.4  deraadt busattach(parent, self, args, bustype)
    109  1.1  deraadt 	struct device *parent, *self;
    110  1.3  deraadt 	void *args;
    111  1.4  deraadt 	int bustype;
    112  1.1  deraadt {
    113  1.4  deraadt 	register struct bus_softc *sc = (struct bus_softc *)self;
    114  1.1  deraadt 	extern struct cfdata cfdata[];
    115  1.3  deraadt 	register struct confargs *ca = args;
    116  1.3  deraadt 	struct confargs oca;
    117  1.1  deraadt 	register short *p;
    118  1.1  deraadt 	struct cfdata *cf;
    119  1.4  deraadt 	caddr_t tmp;
    120  1.1  deraadt 
    121  1.1  deraadt 	if (sc->sc_dev.dv_unit > 0) {
    122  1.1  deraadt 		printf(" unsupported\n");
    123  1.1  deraadt 		return;
    124  1.1  deraadt 	}
    125  1.1  deraadt 
    126  1.4  deraadt 	printf("\n");
    127  1.4  deraadt 
    128  1.1  deraadt 	for (cf = cfdata; cf->cf_driver; cf++) {
    129  1.1  deraadt 		if (cf->cf_fstate == FSTATE_FOUND)
    130  1.1  deraadt 			continue;
    131  1.1  deraadt 		for (p = cf->cf_parents; *p >= 0; p++)
    132  1.3  deraadt 			if (self->dv_cfdata == &cfdata[*p]) {
    133  1.3  deraadt 				oca.ca_ra.ra_iospace = -1;
    134  1.3  deraadt 				oca.ca_ra.ra_paddr = (void *)cf->cf_loc[0];
    135  1.3  deraadt 				oca.ca_ra.ra_len = 0;
    136  1.4  deraadt 				tmp = NULL;
    137  1.3  deraadt 				if (oca.ca_ra.ra_paddr)
    138  1.4  deraadt 					tmp = bus_tmp(oca.ca_ra.ra_paddr,
    139  1.4  deraadt 					    bustype);
    140  1.4  deraadt 				oca.ca_ra.ra_vaddr = tmp;
    141  1.3  deraadt 				oca.ca_ra.ra_intr[0].int_pri = cf->cf_loc[1];
    142  1.3  deraadt 				oca.ca_ra.ra_intr[0].int_vec = 0;
    143  1.3  deraadt 				oca.ca_ra.ra_nintr = 1;
    144  1.3  deraadt 				oca.ca_ra.ra_name = cf->cf_driver->cd_name;
    145  1.3  deraadt 				oca.ca_ra.ra_bp = ca->ca_ra.ra_bp;
    146  1.4  deraadt 				oca.ca_bustype = bustype;
    147  1.3  deraadt 
    148  1.3  deraadt 				if ((*cf->cf_driver->cd_match)(self, cf, &oca) == 0)
    149  1.2  deraadt 					continue;
    150  1.2  deraadt 
    151  1.3  deraadt 				/*
    152  1.3  deraadt 				 * check if XXmatch routine replaced the
    153  1.3  deraadt 				 * temporary mapping with a real mapping.
    154  1.3  deraadt 				 */
    155  1.4  deraadt 				if (tmp == oca.ca_ra.ra_vaddr)
    156  1.3  deraadt 					oca.ca_ra.ra_vaddr = NULL;
    157  1.3  deraadt 				/*
    158  1.3  deraadt 				 * or if it has asked us to create a mapping..
    159  1.3  deraadt 				 * (which won't be seen on future XXmatch calls,
    160  1.3  deraadt 				 * so not as useful as it seems.)
    161  1.3  deraadt 				 */
    162  1.3  deraadt 				if (oca.ca_ra.ra_len)
    163  1.3  deraadt 					oca.ca_ra.ra_vaddr =
    164  1.4  deraadt 					    bus_map(oca.ca_ra.ra_paddr,
    165  1.4  deraadt 					    oca.ca_ra.ra_len, oca.ca_bustype);
    166  1.3  deraadt 
    167  1.4  deraadt 				config_attach(self, cf, &oca, busprint);
    168  1.1  deraadt 			}
    169  1.1  deraadt 	}
    170  1.4  deraadt 	bus_untmp();
    171  1.4  deraadt }
    172  1.4  deraadt 
    173  1.4  deraadt void
    174  1.4  deraadt obioattach(parent, self, args)
    175  1.4  deraadt 	struct device *parent, *self;
    176  1.4  deraadt 	void *args;
    177  1.4  deraadt {
    178  1.4  deraadt 	busattach(parent, self, args, BUS_OBIO);
    179  1.2  deraadt }
    180  1.2  deraadt 
    181  1.4  deraadt void
    182  1.4  deraadt vmesattach(parent, self, args)
    183  1.4  deraadt 	struct device *parent, *self;
    184  1.4  deraadt 	void *args;
    185  1.4  deraadt {
    186  1.4  deraadt 	busattach(parent, self, args, BUS_VME16);
    187  1.4  deraadt }
    188  1.4  deraadt 
    189  1.4  deraadt void
    190  1.4  deraadt vmelattach(parent, self, args)
    191  1.4  deraadt 	struct device *parent, *self;
    192  1.4  deraadt 	void *args;
    193  1.4  deraadt {
    194  1.4  deraadt 	busattach(parent, self, args, BUS_VME32);
    195  1.4  deraadt }
    196  1.4  deraadt 
    197  1.4  deraadt 
    198  1.2  deraadt #define	getpte(va)		lda(va, ASI_PTE)
    199  1.2  deraadt 
    200  1.2  deraadt /*
    201  1.2  deraadt  * If we can find a mapping that was established by the rom, use it.
    202  1.2  deraadt  * Else, create a new mapping.
    203  1.2  deraadt  */
    204  1.2  deraadt void *
    205  1.4  deraadt bus_map(pa, len, bustype)
    206  1.2  deraadt 	void *pa;
    207  1.2  deraadt 	int len;
    208  1.4  deraadt 	int bustype;
    209  1.2  deraadt {
    210  1.3  deraadt 	u_long	pf = (u_long)pa >> PGSHIFT;
    211  1.2  deraadt 	u_long	va, pte;
    212  1.4  deraadt 	int pgtype;
    213  1.4  deraadt 
    214  1.4  deraadt 	switch (bt2pmt[bustype]) {
    215  1.4  deraadt 	case PMAP_OBIO:
    216  1.4  deraadt 		pgtype = PG_OBIO;
    217  1.4  deraadt 		break;
    218  1.4  deraadt 	case PMAP_VME32:
    219  1.4  deraadt 		pgtype = PG_VME32;
    220  1.4  deraadt 		break;
    221  1.4  deraadt 	case PMAP_VME16:
    222  1.4  deraadt 		pgtype = PG_VME16;
    223  1.4  deraadt 		break;
    224  1.4  deraadt 	}
    225  1.2  deraadt 
    226  1.3  deraadt 	if (len <= NBPG) {
    227  1.3  deraadt 		for (va = OLDMON_STARTVADDR; va < OLDMON_ENDVADDR; va += NBPG) {
    228  1.3  deraadt 			pte = getpte(va);
    229  1.4  deraadt 			if ((pte & PG_V) != 0 && (pte & PG_TYPE) == pgtype &&
    230  1.3  deraadt 			    (pte & PG_PFNUM) == pf)
    231  1.3  deraadt 				return ((void *)va);
    232  1.3  deraadt 		}
    233  1.2  deraadt 	}
    234  1.4  deraadt 	return mapiodev(pa, len, bustype);
    235  1.2  deraadt }
    236  1.2  deraadt 
    237  1.2  deraadt void *
    238  1.4  deraadt bus_tmp(pa, bustype)
    239  1.2  deraadt 	void *pa;
    240  1.4  deraadt 	int bustype;
    241  1.2  deraadt {
    242  1.3  deraadt 	vm_offset_t addr = (vm_offset_t)pa & ~PGOFSET;
    243  1.4  deraadt 	int pmtype = bt2pmt[bustype];
    244  1.3  deraadt 
    245  1.2  deraadt 	pmap_enter(kernel_pmap, TMPMAP_VA,
    246  1.4  deraadt 	    addr | pmtype | PMAP_NC,
    247  1.2  deraadt 	    VM_PROT_READ | VM_PROT_WRITE, 1);
    248  1.2  deraadt 	return ((void *)TMPMAP_VA);
    249  1.2  deraadt }
    250  1.2  deraadt 
    251  1.2  deraadt void
    252  1.4  deraadt bus_untmp()
    253  1.2  deraadt {
    254  1.2  deraadt 	pmap_remove(kernel_pmap, TMPMAP_VA, TMPMAP_VA+NBPG);
    255  1.1  deraadt }
    256