Home | History | Annotate | Line # | Download | only in dev
obio.c revision 1.69
      1 /*	$NetBSD: obio.c,v 1.69 2005/11/16 00:49:03 uwe Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1997,1998 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Paul Kranenburg.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *        This product includes software developed by the NetBSD
     21  *        Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 
     39 #include <sys/cdefs.h>
     40 __KERNEL_RCSID(0, "$NetBSD: obio.c,v 1.69 2005/11/16 00:49:03 uwe Exp $");
     41 
     42 #include "locators.h"
     43 
     44 #include <sys/param.h>
     45 #include <sys/systm.h>
     46 #include <sys/device.h>
     47 #include <sys/malloc.h>
     48 
     49 #ifdef DEBUG
     50 #include <sys/proc.h>
     51 #include <sys/syslog.h>
     52 #endif
     53 
     54 #include <uvm/uvm_extern.h>
     55 
     56 #include <machine/bus.h>
     57 #include <sparc/dev/sbusvar.h>
     58 #include <machine/autoconf.h>
     59 #include <machine/oldmon.h>
     60 #include <machine/cpu.h>
     61 #include <machine/ctlreg.h>
     62 #include <sparc/sparc/asm.h>
     63 #include <sparc/sparc/vaddrs.h>
     64 #include <sparc/sparc/cpuvar.h>
     65 
     66 struct obio4_softc {
     67 	struct device	sc_dev;		/* base device */
     68 	bus_space_tag_t	sc_bustag;	/* parent bus tag */
     69 	bus_dma_tag_t	sc_dmatag;	/* parent bus DMA tag */
     70 };
     71 
     72 union obio_softc {
     73 	struct	device sc_dev;		/* base device */
     74 	struct	obio4_softc sc_obio;	/* sun4 obio */
     75 	struct	sbus_softc sc_sbus;	/* sun4m obio is another sbus slot */
     76 };
     77 
     78 
     79 /* autoconfiguration driver */
     80 static	int obiomatch(struct device *, struct cfdata *, void *);
     81 static	void obioattach(struct device *, struct device *, void *);
     82 
     83 CFATTACH_DECL(obio, sizeof(union obio_softc),
     84     obiomatch, obioattach, NULL, NULL);
     85 
     86 static int obio_attached;
     87 
     88 /*
     89  * This `obio4_busattachargs' data structure only exists to pass down
     90  * to obiosearch() the name of a device that must be configured early.
     91  */
     92 struct obio4_busattachargs {
     93 	struct mainbus_attach_args	*ma;
     94 	const char			*name;
     95 };
     96 
     97 #if defined(SUN4)
     98 static	int obioprint(void *, const char *);
     99 static	int obiosearch(struct device *, struct cfdata *, const int *, void *);
    100 static	paddr_t obio_bus_mmap(bus_space_tag_t, bus_addr_t, off_t, int, int);
    101 static	int _obio_bus_map(bus_space_tag_t, bus_addr_t, bus_size_t, int,
    102 			  vaddr_t, bus_space_handle_t *);
    103 
    104 /* There's at most one obio bus, so we can allocate the bus tag statically */
    105 static struct sparc_bus_space_tag obio_space_tag;
    106 #endif
    107 
    108 /*
    109  * Translate obio `interrupts' property value to processor IPL (see sbus.c)
    110  * Apparently, the `interrupts' property on obio devices is just
    111  * the processor IPL.
    112  */
    113 static int intr_obio2ipl[] = {
    114 	0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15
    115 };
    116 
    117 static int
    118 obiomatch(struct device *parent, struct cfdata *cf, void *aux)
    119 {
    120 	struct mainbus_attach_args *ma = aux;
    121 
    122 	if (obio_attached)
    123 		return 0;
    124 
    125 	return (strcmp(cf->cf_name, ma->ma_name) == 0);
    126 }
    127 
    128 static void
    129 obioattach(struct device *parent, struct device *self, void *aux)
    130 {
    131 	struct mainbus_attach_args *ma = aux;
    132 
    133 	obio_attached = 1;
    134 
    135 	printf("\n");
    136 
    137 	if (CPU_ISSUN4) {
    138 #if defined(SUN4)
    139 		struct obio4_softc *sc = &((union obio_softc *)self)->sc_obio;
    140 		struct obio4_busattachargs oa;
    141 		const char *const *cpp;
    142 		static const char *const special4[] = {
    143 			/* find these first */
    144 			"timer",
    145 			"dma",		/* need this before `esp', if any */
    146 			NULL
    147 		};
    148 
    149 		sc->sc_bustag = ma->ma_bustag;
    150 		sc->sc_dmatag = ma->ma_dmatag;
    151 
    152 		memcpy(&obio_space_tag, sc->sc_bustag, sizeof(obio_space_tag));
    153 		obio_space_tag.cookie = sc;
    154 		obio_space_tag.parent = sc->sc_bustag;
    155 		obio_space_tag.sparc_bus_map = _obio_bus_map;
    156 		obio_space_tag.sparc_bus_mmap = obio_bus_mmap;
    157 
    158 		oa.ma = ma;
    159 
    160 		/* Find all `early' obio devices */
    161 		for (cpp = special4; *cpp != NULL; cpp++) {
    162 			oa.name = *cpp;
    163 			config_search_ia(obiosearch, self, "obio", &oa);
    164 		}
    165 
    166 		/* Find all other obio devices */
    167 		oa.name = NULL;
    168 		config_search_ia(obiosearch, self, "obio", &oa);
    169 #endif
    170 		return;
    171 	} else if (CPU_ISSUN4M) {
    172 		/*
    173 		 * Attach the on-board I/O bus at on a sun4m.
    174 		 * In this case we treat the obio bus as another sbus slot.
    175 		 */
    176 		struct sbus_softc *sc = &((union obio_softc *)self)->sc_sbus;
    177 
    178 		static const char *const special4m[] = {
    179 			/* find these first */
    180 			"eeprom",
    181 			"counter",
    182 #if 0 /* Not all sun4m's have an `auxio' */
    183 			"auxio",
    184 #endif
    185 			"",
    186 			/* place device to ignore here */
    187 			"interrupt",
    188 			NULL
    189 		};
    190 
    191 		sc->sc_bustag = ma->ma_bustag;
    192 		sc->sc_dmatag = ma->ma_dmatag;
    193 		sc->sc_intr2ipl = intr_obio2ipl;
    194 
    195 		sbus_attach_common(sc, "obio", ma->ma_node, special4m);
    196 	} else {
    197 		printf("obio on this machine?\n");
    198 	}
    199 }
    200 
    201 #if defined(SUN4)
    202 static int
    203 obioprint(void *args, const char *busname)
    204 {
    205 	union obio_attach_args *uoba = args;
    206 	struct obio4_attach_args *oba = &uoba->uoba_oba4;
    207 
    208 	aprint_normal(" addr 0x%lx", (u_long)BUS_ADDR_PADDR(oba->oba_paddr));
    209 	if (oba->oba_pri != -1)
    210 		aprint_normal(" level %d", oba->oba_pri);
    211 
    212 	return (UNCONF);
    213 }
    214 
    215 static int
    216 _obio_bus_map(bus_space_tag_t t, bus_addr_t ba, bus_size_t size, int flags,
    217 	      vaddr_t va, bus_space_handle_t *hp)
    218 {
    219 
    220 	if ((flags & OBIO_BUS_MAP_USE_ROM) != 0 &&
    221 	     obio_find_rom_map(ba, size, hp) == 0)
    222 		return (0);
    223 
    224 	return (bus_space_map2(t->parent, ba, size, flags, va, hp));
    225 }
    226 
    227 static paddr_t
    228 obio_bus_mmap(bus_space_tag_t t, bus_addr_t ba, off_t off, int prot, int flags)
    229 {
    230 
    231 	return (bus_space_mmap(t->parent, ba, off, prot, flags));
    232 }
    233 
    234 static int
    235 obiosearch(struct device *parent, struct cfdata *cf, const int *ldesc,
    236 	   void *aux)
    237 {
    238 	struct obio4_busattachargs *oap = aux;
    239 	union obio_attach_args uoba;
    240 	struct obio4_attach_args *oba = &uoba.uoba_oba4;
    241 	int addr;
    242 
    243 	/* Check whether we're looking for a specifically named device */
    244 	if (oap->name != NULL && strcmp(oap->name, cf->cf_name) != 0)
    245 		return (0);
    246 
    247 	/*
    248 	 * Avoid sun4m entries which don't have valid PAs.
    249 	 * no point in even probing them.
    250 	 */
    251 	addr = cf->cf_loc[OBIOCF_ADDR];
    252 	if (addr == -1)
    253 		return (0);
    254 
    255 	/*
    256 	 * On the 4/100 obio addresses must be mapped at
    257 	 * 0x0YYYYYYY, but alias higher up (we avoid the
    258 	 * alias condition because it causes pmap difficulties)
    259 	 * XXX: We also assume that 4/[23]00 obio addresses
    260 	 * must be 0xZYYYYYYY, where (Z != 0)
    261 	 */
    262 	if (cpuinfo.cpu_type == CPUTYP_4_100 && (addr & 0xf0000000))
    263 		return (0);
    264 	if (cpuinfo.cpu_type != CPUTYP_4_100 && !(addr & 0xf0000000))
    265 		return (0);
    266 
    267 	uoba.uoba_isobio4 = 1;
    268 	oba->oba_bustag = &obio_space_tag;
    269 	oba->oba_dmatag = oap->ma->ma_dmatag;
    270 	oba->oba_paddr = BUS_ADDR(PMAP_OBIO, addr);
    271 	oba->oba_pri = cf->cf_loc[OBIOCF_LEVEL];
    272 
    273 	if (config_match(parent, cf, &uoba) == 0)
    274 		return (0);
    275 
    276 	config_attach(parent, cf, &uoba, obioprint);
    277 	return (1);
    278 }
    279 
    280 
    281 /*
    282  * If we can find a mapping that was established by the rom, use it.
    283  * Else, create a new mapping.
    284  */
    285 int
    286 obio_find_rom_map(bus_addr_t ba, int len, bus_space_handle_t *hp)
    287 {
    288 #define	getpte(va)		lda(va, ASI_PTE)
    289 
    290 	u_long	pa, pf;
    291 	int	pgtype;
    292 	u_long	va, pte;
    293 
    294 	if (len > PAGE_SIZE)
    295 		return (EINVAL);
    296 
    297 	pa = BUS_ADDR_PADDR(ba);
    298 	pf = pa >> PGSHIFT;
    299 	pgtype = PMAP_T2PTE_4(PMAP_OBIO);
    300 
    301 	for (va = OLDMON_STARTVADDR; va < OLDMON_ENDVADDR; va += PAGE_SIZE) {
    302 		pte = getpte(va);
    303 		if ((pte & PG_V) == 0 || (pte & PG_TYPE) != pgtype ||
    304 		    (pte & PG_PFNUM) != pf)
    305 			continue;
    306 
    307 		/*
    308 		 * Found entry in PROM's pagetable
    309 		 * note: preserve page offset
    310 		 */
    311 		*hp = (bus_space_handle_t)(va | (pa & PGOFSET));
    312 		return (0);
    313 	}
    314 
    315 	return (ENOENT);
    316 }
    317 #endif /* SUN4 */
    318