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