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