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