Home | History | Annotate | Line # | Download | only in dev
obio.c revision 1.74.50.1
      1 /*	$NetBSD: obio.c,v 1.74.50.1 2021/03/20 19:33:38 thorpej 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.74.50.1 2021/03/20 19:33:38 thorpej 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(self, &oa,
    168 			    CFARG_SUBMATCH, obiosearch,
    169 			    CFARG_IATTR, "obio",
    170 			    CFARG_EOL);
    171 		}
    172 
    173 		/* Find all other obio devices */
    174 		oa.name = NULL;
    175 		config_search(self, &oa,
    176 		    CFARG_SUBMATCH, obiosearch,
    177 		    CFARG_IATTR, "obio",
    178 		    CFARG_EOL);
    179 #endif
    180 		return;
    181 	} else if (CPU_ISSUN4M) {
    182 #if NSBUS > 0
    183 		/*
    184 		 * Attach the on-board I/O bus at on a sun4m.
    185 		 * In this case we treat the obio bus as another sbus slot.
    186 		 */
    187 		union obio_softc *usc = device_private(self);
    188 		struct sbus_softc *sc = &usc->sc_sbus;
    189 
    190 		static const char *const special4m[] = {
    191 			/* find these first */
    192 			"eeprom",
    193 			"counter",
    194 #if 0 /* Not all sun4m's have an `auxio' */
    195 			"auxio",
    196 #endif
    197 			"",
    198 			/* place device to ignore here */
    199 			"interrupt",
    200 			NULL
    201 		};
    202 
    203 		sc->sc_dev = self;
    204 		sc->sc_bustag = ma->ma_bustag;
    205 		sc->sc_dmatag = ma->ma_dmatag;
    206 		sc->sc_intr2ipl = intr_obio2ipl;
    207 
    208 		sbus_attach_common(sc, "obio", ma->ma_node, special4m);
    209 #endif
    210 	} else {
    211 		printf("obio on this machine?\n");
    212 	}
    213 }
    214 
    215 #if defined(SUN4)
    216 static int
    217 obioprint(void *args, const char *busname)
    218 {
    219 	union obio_attach_args *uoba = args;
    220 	struct obio4_attach_args *oba = &uoba->uoba_oba4;
    221 
    222 	aprint_normal(" addr 0x%lx", (u_long)BUS_ADDR_PADDR(oba->oba_paddr));
    223 	if (oba->oba_pri != -1)
    224 		aprint_normal(" level %d", oba->oba_pri);
    225 
    226 	return (UNCONF);
    227 }
    228 
    229 static int
    230 _obio_bus_map(bus_space_tag_t t, bus_addr_t ba, bus_size_t size, int flags,
    231 	      vaddr_t va, bus_space_handle_t *hp)
    232 {
    233 
    234 	if ((flags & OBIO_BUS_MAP_USE_ROM) != 0 &&
    235 	     obio_find_rom_map(ba, size, hp) == 0)
    236 		return (0);
    237 
    238 	return (bus_space_map2(t->parent, ba, size, flags, va, hp));
    239 }
    240 
    241 static paddr_t
    242 obio_bus_mmap(bus_space_tag_t t, bus_addr_t ba, off_t off, int prot, int flags)
    243 {
    244 
    245 	return (bus_space_mmap(t->parent, ba, off, prot, flags));
    246 }
    247 
    248 static int
    249 obiosearch(device_t parent, struct cfdata *cf, const int *ldesc,
    250 	   void *aux)
    251 {
    252 	struct obio4_busattachargs *oap = aux;
    253 	union obio_attach_args uoba;
    254 	struct obio4_attach_args *oba = &uoba.uoba_oba4;
    255 	int addr;
    256 
    257 	/* Check whether we're looking for a specifically named device */
    258 	if (oap->name != NULL && strcmp(oap->name, cf->cf_name) != 0)
    259 		return (0);
    260 
    261 	/*
    262 	 * Avoid sun4m entries which don't have valid PAs.
    263 	 * no point in even probing them.
    264 	 */
    265 	addr = cf->cf_loc[OBIOCF_ADDR];
    266 	if (addr == -1)
    267 		return (0);
    268 
    269 	/*
    270 	 * On the 4/100 obio addresses must be mapped at
    271 	 * 0x0YYYYYYY, but alias higher up (we avoid the
    272 	 * alias condition because it causes pmap difficulties)
    273 	 * XXX: We also assume that 4/[23]00 obio addresses
    274 	 * must be 0xZYYYYYYY, where (Z != 0)
    275 	 */
    276 	if (cpuinfo.cpu_type == CPUTYP_4_100 && (addr & 0xf0000000))
    277 		return (0);
    278 	if (cpuinfo.cpu_type != CPUTYP_4_100 && !(addr & 0xf0000000))
    279 		return (0);
    280 
    281 	uoba.uoba_isobio4 = 1;
    282 	oba->oba_bustag = &obio_space_tag;
    283 	oba->oba_dmatag = oap->ma->ma_dmatag;
    284 	oba->oba_paddr = BUS_ADDR(PMAP_OBIO, addr);
    285 	oba->oba_pri = cf->cf_loc[OBIOCF_LEVEL];
    286 
    287 	if (config_match(parent, cf, &uoba) == 0)
    288 		return (0);
    289 
    290 	config_attach(parent, cf, &uoba, obioprint);
    291 	return (1);
    292 }
    293 
    294 
    295 /*
    296  * If we can find a mapping that was established by the rom, use it.
    297  * Else, create a new mapping.
    298  */
    299 int
    300 obio_find_rom_map(bus_addr_t ba, int len, bus_space_handle_t *hp)
    301 {
    302 #define	getpte(va)		lda(va, ASI_PTE)
    303 
    304 	u_long	pa, pf;
    305 	int	pgtype;
    306 	u_long	va, pte;
    307 
    308 	if (len > PAGE_SIZE)
    309 		return (EINVAL);
    310 
    311 	pa = BUS_ADDR_PADDR(ba);
    312 	pf = pa >> PGSHIFT;
    313 	pgtype = PMAP_T2PTE_4(PMAP_OBIO);
    314 
    315 	for (va = OLDMON_STARTVADDR; va < OLDMON_ENDVADDR; va += PAGE_SIZE) {
    316 		pte = getpte(va);
    317 		if ((pte & PG_V) == 0 || (pte & PG_TYPE) != pgtype ||
    318 		    (pte & PG_PFNUM) != pf)
    319 			continue;
    320 
    321 		/*
    322 		 * Found entry in PROM's pagetable
    323 		 * note: preserve page offset
    324 		 */
    325 		*hp = (bus_space_handle_t)(va | (pa & PGOFSET));
    326 		return (0);
    327 	}
    328 
    329 	return (ENOENT);
    330 }
    331 #endif /* SUN4 */
    332