Home | History | Annotate | Line # | Download | only in dev
bootbus.c revision 1.1
      1  1.1  thorpej /*	$NetBSD: bootbus.c,v 1.1 2002/08/24 05:26:57 thorpej Exp $	*/
      2  1.1  thorpej 
      3  1.1  thorpej /*-
      4  1.1  thorpej  * Copyright (c) 2002 The NetBSD Foundation, Inc.
      5  1.1  thorpej  * All rights reserved.
      6  1.1  thorpej  *
      7  1.1  thorpej  * This code is derived from software contributed to The NetBSD Foundation
      8  1.1  thorpej  * by Jason R. Thorpe.
      9  1.1  thorpej  *
     10  1.1  thorpej  * Redistribution and use in source and binary forms, with or without
     11  1.1  thorpej  * modification, are permitted provided that the following conditions
     12  1.1  thorpej  * are met:
     13  1.1  thorpej  * 1. Redistributions of source code must retain the above copyright
     14  1.1  thorpej  *    notice, this list of conditions and the following disclaimer.
     15  1.1  thorpej  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.1  thorpej  *    notice, this list of conditions and the following disclaimer in the
     17  1.1  thorpej  *    documentation and/or other materials provided with the distribution.
     18  1.1  thorpej  * 3. All advertising materials mentioning features or use of this software
     19  1.1  thorpej  *    must display the following acknowledgement:
     20  1.1  thorpej  *	This product includes software developed by the NetBSD
     21  1.1  thorpej  *	Foundation, Inc. and its contributors.
     22  1.1  thorpej  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  1.1  thorpej  *    contributors may be used to endorse or promote products derived
     24  1.1  thorpej  *    from this software without specific prior written permission.
     25  1.1  thorpej  *
     26  1.1  thorpej  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  1.1  thorpej  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  1.1  thorpej  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  1.1  thorpej  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  1.1  thorpej  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  1.1  thorpej  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  1.1  thorpej  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  1.1  thorpej  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  1.1  thorpej  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  1.1  thorpej  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  1.1  thorpej  * POSSIBILITY OF SUCH DAMAGE.
     37  1.1  thorpej  */
     38  1.1  thorpej 
     39  1.1  thorpej /*
     40  1.1  thorpej  * Autoconfiguration support for Sun4d "bootbus".
     41  1.1  thorpej  */
     42  1.1  thorpej 
     43  1.1  thorpej #include <sys/param.h>
     44  1.1  thorpej #include <sys/malloc.h>
     45  1.1  thorpej #include <sys/systm.h>
     46  1.1  thorpej #include <sys/device.h>
     47  1.1  thorpej 
     48  1.1  thorpej #include <machine/bus.h>
     49  1.1  thorpej 
     50  1.1  thorpej #include <sparc/sparc/cpuunitvar.h>
     51  1.1  thorpej #include <sparc/dev/bootbusvar.h>
     52  1.1  thorpej #include <machine/autoconf.h>
     53  1.1  thorpej 
     54  1.1  thorpej #include "locators.h"
     55  1.1  thorpej 
     56  1.1  thorpej struct bootbus_softc {
     57  1.1  thorpej 	struct device sc_dev;
     58  1.1  thorpej 	int sc_node;				/* our OBP node */
     59  1.1  thorpej 
     60  1.1  thorpej 	bus_space_tag_t sc_st;			/* ours */
     61  1.1  thorpej 	bus_space_tag_t sc_bustag;		/* passed on to children */
     62  1.1  thorpej 
     63  1.1  thorpej 	struct openprom_range *sc_range;	/* our address ranges */
     64  1.1  thorpej 	int sc_nrange;
     65  1.1  thorpej };
     66  1.1  thorpej 
     67  1.1  thorpej static int bootbus_match(struct device *, struct cfdata *, void *);
     68  1.1  thorpej static void bootbus_attach(struct device *, struct device *, void *);
     69  1.1  thorpej 
     70  1.1  thorpej struct cfattach bootbus_ca = {
     71  1.1  thorpej 	sizeof(struct bootbus_softc), bootbus_match, bootbus_attach,
     72  1.1  thorpej };
     73  1.1  thorpej 
     74  1.1  thorpej static int bootbus_submatch(struct device *, struct cfdata *, void *);
     75  1.1  thorpej static int bootbus_print(void *, const char *);
     76  1.1  thorpej 
     77  1.1  thorpej static int bootbus_bus_map(bus_space_tag_t, bus_addr_t, size_t, int,
     78  1.1  thorpej     vaddr_t, bus_space_handle_t *);
     79  1.1  thorpej static paddr_t bootbus_bus_mmap(bus_space_tag_t, bus_addr_t, off_t,
     80  1.1  thorpej     int, int);
     81  1.1  thorpej static void *bootbus_intr_establish(bus_space_tag_t, int, int, int,
     82  1.1  thorpej     int (*)(void *), void *);
     83  1.1  thorpej 
     84  1.1  thorpej static int bootbus_setup_attach_args(struct bootbus_softc *, bus_space_tag_t,
     85  1.1  thorpej     int, struct bootbus_attach_args *);
     86  1.1  thorpej static void bootbus_destroy_attach_args(struct bootbus_attach_args *);
     87  1.1  thorpej 
     88  1.1  thorpej static int
     89  1.1  thorpej bootbus_match(struct device *parent, struct cfdata *cf, void *aux)
     90  1.1  thorpej {
     91  1.1  thorpej 	struct cpuunit_attach_args *cpua = aux;
     92  1.1  thorpej 
     93  1.1  thorpej 	if (strcmp(cpua->cpua_name, cf->cf_driver->cd_name) == 0)
     94  1.1  thorpej 		return (1);
     95  1.1  thorpej 
     96  1.1  thorpej 	return (0);
     97  1.1  thorpej }
     98  1.1  thorpej 
     99  1.1  thorpej static void
    100  1.1  thorpej bootbus_attach(struct device *parent, struct device *self, void *aux)
    101  1.1  thorpej {
    102  1.1  thorpej 	struct bootbus_softc *sc = (void *) self;
    103  1.1  thorpej 	struct cpuunit_attach_args *cpua = aux;
    104  1.1  thorpej 	int node, error;
    105  1.1  thorpej 
    106  1.1  thorpej 	sc->sc_node = cpua->cpua_node;
    107  1.1  thorpej 	sc->sc_st = cpua->cpua_bustag;
    108  1.1  thorpej 
    109  1.1  thorpej 	printf("\n");
    110  1.1  thorpej 
    111  1.1  thorpej 	/*
    112  1.1  thorpej 	 * Initialize the bus space tag we pass on to our children.
    113  1.1  thorpej 	 */
    114  1.1  thorpej 	sc->sc_bustag = malloc(sizeof(*sc->sc_bustag), M_DEVBUF,
    115  1.1  thorpej 	    M_WAITOK|M_ZERO);
    116  1.1  thorpej 	sc->sc_bustag->cookie = sc;
    117  1.1  thorpej 	sc->sc_bustag->parent = sc->sc_st;
    118  1.1  thorpej 	sc->sc_bustag->sparc_bus_map = bootbus_bus_map;
    119  1.1  thorpej 	sc->sc_bustag->sparc_bus_mmap = bootbus_bus_mmap;
    120  1.1  thorpej 	sc->sc_bustag->sparc_intr_establish = bootbus_intr_establish;
    121  1.1  thorpej 
    122  1.1  thorpej 	/*
    123  1.1  thorpej 	 * Collect address translations from the OBP.
    124  1.1  thorpej 	 */
    125  1.1  thorpej 	error = PROM_getprop(sc->sc_node, "ranges",
    126  1.1  thorpej 	    sizeof(struct openprom_range), &sc->sc_nrange,
    127  1.1  thorpej 	    (void **) &sc->sc_range);
    128  1.1  thorpej 	if (error) {
    129  1.1  thorpej 		printf("%s: error %d getting \"ranges\" property\n",
    130  1.1  thorpej 		    sc->sc_dev.dv_xname, error);
    131  1.1  thorpej 		panic("bootbus_attach");
    132  1.1  thorpej 	}
    133  1.1  thorpej 
    134  1.1  thorpej 	/* Attach the CPU (and possibly bootbus) child nodes. */
    135  1.1  thorpej 	for (node = firstchild(sc->sc_node); node != 0;
    136  1.1  thorpej 	     node = nextsibling(node)) {
    137  1.1  thorpej 		struct bootbus_attach_args baa;
    138  1.1  thorpej 
    139  1.1  thorpej 		if (bootbus_setup_attach_args(sc, sc->sc_bustag, node, &baa))
    140  1.1  thorpej 			panic("bootbus_attach: failed to set up attach args");
    141  1.1  thorpej 
    142  1.1  thorpej 		(void) config_found_sm(&sc->sc_dev, &baa, bootbus_print,
    143  1.1  thorpej 		    bootbus_submatch);
    144  1.1  thorpej 
    145  1.1  thorpej 		bootbus_destroy_attach_args(&baa);
    146  1.1  thorpej 	}
    147  1.1  thorpej }
    148  1.1  thorpej 
    149  1.1  thorpej static int
    150  1.1  thorpej bootbus_submatch(struct device *parent, struct cfdata *cf, void *aux)
    151  1.1  thorpej {
    152  1.1  thorpej 	struct bootbus_attach_args *baa = aux;
    153  1.1  thorpej 
    154  1.1  thorpej 	if (cf->cf_loc[BOOTBUSCF_SLOT] != BOOTBUSCF_SLOT_DEFAULT &&
    155  1.1  thorpej 	    cf->cf_loc[BOOTBUSCF_SLOT] != baa->ba_slot)
    156  1.1  thorpej 		return (0);
    157  1.1  thorpej 
    158  1.1  thorpej 	if (cf->cf_loc[BOOTBUSCF_OFFSET] != BOOTBUSCF_OFFSET_DEFAULT &&
    159  1.1  thorpej 	    cf->cf_loc[BOOTBUSCF_OFFSET] != baa->ba_offset)
    160  1.1  thorpej 		return (0);
    161  1.1  thorpej 
    162  1.1  thorpej 	return ((*cf->cf_attach->ca_match)(parent, cf, aux));
    163  1.1  thorpej }
    164  1.1  thorpej 
    165  1.1  thorpej static int
    166  1.1  thorpej bootbus_print(void *aux, const char *pnp)
    167  1.1  thorpej {
    168  1.1  thorpej 	struct bootbus_attach_args *baa = aux;
    169  1.1  thorpej 	int i;
    170  1.1  thorpej 
    171  1.1  thorpej 	if (pnp)
    172  1.1  thorpej 		printf("%s at %s", baa->ba_name, pnp);
    173  1.1  thorpej 	printf(" slot %d offset 0x%x", baa->ba_slot, baa->ba_offset);
    174  1.1  thorpej 	for (i = 0; i < baa->ba_nintr; i++)
    175  1.1  thorpej 		printf(" ipl %d", baa->ba_intr[i].oi_pri);
    176  1.1  thorpej 
    177  1.1  thorpej 	return (UNCONF);
    178  1.1  thorpej }
    179  1.1  thorpej 
    180  1.1  thorpej static int
    181  1.1  thorpej bootbus_setup_attach_args(struct bootbus_softc *sc, bus_space_tag_t bustag,
    182  1.1  thorpej     int node, struct bootbus_attach_args *baa)
    183  1.1  thorpej {
    184  1.1  thorpej 	int n, error;
    185  1.1  thorpej 
    186  1.1  thorpej 	memset(baa, 0, sizeof(*baa));
    187  1.1  thorpej 
    188  1.1  thorpej 	error = PROM_getprop(node, "name", 1, &n, (void **) &baa->ba_name);
    189  1.1  thorpej 	if (error)
    190  1.1  thorpej 		return (error);
    191  1.1  thorpej 	baa->ba_name[n] = '\0';
    192  1.1  thorpej 
    193  1.1  thorpej 	baa->ba_bustag = bustag;
    194  1.1  thorpej 	baa->ba_node = node;
    195  1.1  thorpej 
    196  1.1  thorpej 	error = PROM_getprop(node, "reg", sizeof(struct openprom_addr),
    197  1.1  thorpej 	    &baa->ba_nreg, (void **) &baa->ba_reg);
    198  1.1  thorpej 	if (error) {
    199  1.1  thorpej 		bootbus_destroy_attach_args(baa);
    200  1.1  thorpej 		return (error);
    201  1.1  thorpej 	}
    202  1.1  thorpej 
    203  1.1  thorpej 	error = PROM_getprop(node, "intr", sizeof(struct openprom_intr),
    204  1.1  thorpej 	    &baa->ba_nintr, (void **) &baa->ba_intr);
    205  1.1  thorpej 	if (error != 0 && error != ENOENT) {
    206  1.1  thorpej 		bootbus_destroy_attach_args(baa);
    207  1.1  thorpej 		return (error);
    208  1.1  thorpej 	}
    209  1.1  thorpej 
    210  1.1  thorpej 	error = PROM_getprop(node, "address", sizeof(uint32_t),
    211  1.1  thorpej 	    &baa->ba_npromvaddrs, (void **) &baa->ba_promvaddrs);
    212  1.1  thorpej 	if (error != 0 && error != ENOENT) {
    213  1.1  thorpej 		bootbus_destroy_attach_args(baa);
    214  1.1  thorpej 		return (error);
    215  1.1  thorpej 	}
    216  1.1  thorpej 
    217  1.1  thorpej 	return (0);
    218  1.1  thorpej }
    219  1.1  thorpej 
    220  1.1  thorpej static void
    221  1.1  thorpej bootbus_destroy_attach_args(struct bootbus_attach_args *baa)
    222  1.1  thorpej {
    223  1.1  thorpej 
    224  1.1  thorpej 	if (baa->ba_name != NULL)
    225  1.1  thorpej 		free(baa->ba_name, M_DEVBUF);
    226  1.1  thorpej 
    227  1.1  thorpej 	if (baa->ba_reg != NULL)
    228  1.1  thorpej 		free(baa->ba_reg, M_DEVBUF);
    229  1.1  thorpej 
    230  1.1  thorpej 	if (baa->ba_intr != NULL)
    231  1.1  thorpej 		free(baa->ba_intr, M_DEVBUF);
    232  1.1  thorpej 
    233  1.1  thorpej 	if (baa->ba_promvaddrs != NULL)
    234  1.1  thorpej 		free(baa->ba_promvaddrs, M_DEVBUF);
    235  1.1  thorpej }
    236  1.1  thorpej 
    237  1.1  thorpej static int
    238  1.1  thorpej bootbus_translate_address(struct bootbus_softc *sc, bus_addr_t addr,
    239  1.1  thorpej     bus_addr_t *addrp)
    240  1.1  thorpej {
    241  1.1  thorpej 	int space = BUS_ADDR_IOSPACE(addr);
    242  1.1  thorpej 	int i;
    243  1.1  thorpej 
    244  1.1  thorpej 	for (i = 0; i < sc->sc_nrange; i++) {
    245  1.1  thorpej 		struct openprom_range *rp = &sc->sc_range[i];
    246  1.1  thorpej 
    247  1.1  thorpej 		if (rp->or_child_space != space)
    248  1.1  thorpej 			continue;
    249  1.1  thorpej 
    250  1.1  thorpej 		/* We've found the connection to the parent bus. */
    251  1.1  thorpej 		*addrp = BUS_ADDR(rp->or_parent_space,
    252  1.1  thorpej 		    rp->or_parent_base + BUS_ADDR_PADDR(addr));
    253  1.1  thorpej 		return (0);
    254  1.1  thorpej 	}
    255  1.1  thorpej 
    256  1.1  thorpej 	return (EINVAL);
    257  1.1  thorpej }
    258  1.1  thorpej 
    259  1.1  thorpej static int
    260  1.1  thorpej bootbus_bus_map(bus_space_tag_t t, bus_addr_t ba, bus_size_t size,
    261  1.1  thorpej     int flags, vaddr_t va, bus_space_handle_t *hp)
    262  1.1  thorpej {
    263  1.1  thorpej 	struct bootbus_softc *sc = t->cookie;
    264  1.1  thorpej 	bus_addr_t addr;
    265  1.1  thorpej 	int error;
    266  1.1  thorpej 
    267  1.1  thorpej 	error = bootbus_translate_address(sc, ba, &addr);
    268  1.1  thorpej 	if (error)
    269  1.1  thorpej 		return (error);
    270  1.1  thorpej 	return (bus_space_map2(sc->sc_st, addr, size, flags, va, hp));
    271  1.1  thorpej }
    272  1.1  thorpej 
    273  1.1  thorpej static paddr_t
    274  1.1  thorpej bootbus_bus_mmap(bus_space_tag_t t, bus_addr_t ba, off_t off, int prot,
    275  1.1  thorpej     int flags)
    276  1.1  thorpej {
    277  1.1  thorpej 	struct bootbus_softc *sc = t->cookie;
    278  1.1  thorpej 	bus_addr_t addr;
    279  1.1  thorpej 	int error;
    280  1.1  thorpej 
    281  1.1  thorpej 	error = bootbus_translate_address(sc, ba, &addr);
    282  1.1  thorpej 	if (error)
    283  1.1  thorpej 		return (-1);
    284  1.1  thorpej 	return (bus_space_mmap(sc->sc_st, addr, off, prot, flags));
    285  1.1  thorpej }
    286  1.1  thorpej 
    287  1.1  thorpej static void *
    288  1.1  thorpej bootbus_intr_establish(bus_space_tag_t t, int pil, int level, int flags,
    289  1.1  thorpej     int (*handler)(void *), void *arg)
    290  1.1  thorpej {
    291  1.1  thorpej 	struct intrhand *ih;
    292  1.1  thorpej 
    293  1.1  thorpej 	ih = malloc(sizeof(*ih), M_DEVBUF, M_NOWAIT);
    294  1.1  thorpej 	if (ih == NULL)
    295  1.1  thorpej 		return (NULL);
    296  1.1  thorpej 
    297  1.1  thorpej 	ih->ih_fun = handler;
    298  1.1  thorpej 	ih->ih_arg = arg;
    299  1.1  thorpej 	if ((flags & BUS_INTR_ESTABLISH_FASTTRAP) != 0)
    300  1.1  thorpej 		intr_fasttrap(pil, (void (*)__P((void)))handler);
    301  1.1  thorpej 	else
    302  1.1  thorpej 		intr_establish(pil, ih);
    303  1.1  thorpej 	return (ih);
    304  1.1  thorpej }
    305