Home | History | Annotate | Line # | Download | only in dev
obio.c revision 1.3
      1 /*	$NetBSD: obio.c,v 1.3 1994/10/02 22:00:29 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/ctlreg.h>
     48 #include <sparc/sparc/asm.h>
     49 #include <sparc/sparc/vaddrs.h>
     50 
     51 struct obio_softc {
     52 	struct	device sc_dev;		/* base device */
     53 	int	nothing;
     54 };
     55 
     56 /* autoconfiguration driver */
     57 static int	obiomatch(struct device *, struct cfdata *, void *);
     58 static void	obioattach(struct device *, struct device *, void *);
     59 struct cfdriver obiocd = { NULL, "obio", obiomatch, obioattach,
     60 	DV_DULL, sizeof(struct obio_softc)
     61 };
     62 
     63 void *		obio_map __P((void *, int));
     64 void *		obio_tmp_map __P((void *));
     65 void		obio_tmp_unmap __P((void));
     66 
     67 int
     68 obiomatch(parent, cf, aux)
     69 	struct device *parent;
     70 	struct cfdata *cf;
     71 	void *aux;
     72 {
     73 	register struct confargs *ca = aux;
     74 	register struct romaux *ra = &ca->ca_ra;
     75 
     76 	if (cputyp != CPU_SUN4)
     77 		return (0);
     78 	return (strcmp(cf->cf_driver->cd_name, ra->ra_name) == 0);
     79 }
     80 
     81 int
     82 obio_print(args, obio)
     83 	void *args;
     84 	char *obio;
     85 {
     86 	register struct confargs *ca = args;
     87 
     88 	if (ca->ca_ra.ra_name == NULL)
     89 		ca->ca_ra.ra_name = "<unknown>";
     90 	if (obio)
     91 		printf("[%s at %s]", ca->ca_ra.ra_name, obio);
     92 	printf(" addr %x", ca->ca_ra.ra_paddr);
     93 	return (UNCONF);
     94 }
     95 
     96 void
     97 obioattach(parent, self, args)
     98 	struct device *parent, *self;
     99 	void *args;
    100 {
    101 	register struct obio_softc *sc = (struct obio_softc *)self;
    102 	extern struct cfdata cfdata[];
    103 	register struct confargs *ca = args;
    104 	struct confargs oca;
    105 	register short *p;
    106 	struct cfdata *cf;
    107 	caddr_t tmpmap;
    108 
    109 	printf("\n");
    110 
    111 	if (sc->sc_dev.dv_unit > 0) {
    112 		printf(" unsupported\n");
    113 		return;
    114 	}
    115 
    116 	for (cf = cfdata; cf->cf_driver; cf++) {
    117 		if (cf->cf_fstate == FSTATE_FOUND)
    118 			continue;
    119 		for (p = cf->cf_parents; *p >= 0; p++)
    120 			if (self->dv_cfdata == &cfdata[*p]) {
    121 				oca.ca_ra.ra_iospace = -1;
    122 				oca.ca_ra.ra_paddr = (void *)cf->cf_loc[0];
    123 				oca.ca_ra.ra_len = 0;
    124 				tmpmap = NULL;
    125 				if (oca.ca_ra.ra_paddr)
    126 					tmpmap = obio_tmp_map(oca.ca_ra.ra_paddr);
    127 				oca.ca_ra.ra_vaddr = tmpmap;
    128 				oca.ca_ra.ra_intr[0].int_pri = cf->cf_loc[1];
    129 				oca.ca_ra.ra_intr[0].int_vec = 0;
    130 				oca.ca_ra.ra_nintr = 1;
    131 				oca.ca_ra.ra_name = cf->cf_driver->cd_name;
    132 				oca.ca_ra.ra_bp = ca->ca_ra.ra_bp;
    133 				oca.ca_bustype = BUS_OBIO;
    134 
    135 				if ((*cf->cf_driver->cd_match)(self, cf, &oca) == 0)
    136 					continue;
    137 
    138 				/*
    139 				 * check if XXmatch routine replaced the
    140 				 * temporary mapping with a real mapping.
    141 				 */
    142 				if (tmpmap == oca.ca_ra.ra_vaddr)
    143 					oca.ca_ra.ra_vaddr = NULL;
    144 				/*
    145 				 * or if it has asked us to create a mapping..
    146 				 * (which won't be seen on future XXmatch calls,
    147 				 * so not as useful as it seems.)
    148 				 */
    149 				if (oca.ca_ra.ra_len)
    150 					oca.ca_ra.ra_vaddr =
    151 					    obio_map(oca.ca_ra.ra_paddr,
    152 					    oca.ca_ra.ra_len);
    153 
    154 				config_attach(self, cf, &oca, obio_print);
    155 			}
    156 	}
    157 	obio_tmp_unmap();
    158 }
    159 
    160 #define	getpte(va)		lda(va, ASI_PTE)
    161 
    162 /*
    163  * If we can find a mapping that was established by the rom, use it.
    164  * Else, create a new mapping.
    165  */
    166 void *
    167 obio_map(pa, len)
    168 	void *pa;
    169 	int len;
    170 {
    171 	u_long	pf = (u_long)pa >> PGSHIFT;
    172 	u_long	va, pte;
    173 
    174 	if (len <= NBPG) {
    175 		for (va = OLDMON_STARTVADDR; va < OLDMON_ENDVADDR; va += NBPG) {
    176 			pte = getpte(va);
    177 			if ((pte & PG_V) != 0 && (pte & PG_TYPE) == PG_OBIO &&
    178 			    (pte & PG_PFNUM) == pf)
    179 				return ((void *)va);
    180 		}
    181 	}
    182 	return mapiodev(pa, len);
    183 }
    184 
    185 void *
    186 obio_tmp_map(pa)
    187 	void *pa;
    188 {
    189 	vm_offset_t addr = (vm_offset_t)pa & ~PGOFSET;
    190 
    191 	pmap_enter(kernel_pmap, TMPMAP_VA,
    192 	    addr | PMAP_OBIO | PMAP_NC,
    193 	    VM_PROT_READ | VM_PROT_WRITE, 1);
    194 	return ((void *)TMPMAP_VA);
    195 }
    196 
    197 void
    198 obio_tmp_unmap()
    199 {
    200 	pmap_remove(kernel_pmap, TMPMAP_VA, TMPMAP_VA+NBPG);
    201 }
    202