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