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