Home | History | Annotate | Line # | Download | only in dev
bootbus.c revision 1.3.2.3
      1  1.3.2.3  nathanw /*	$NetBSD: bootbus.c,v 1.3.2.3 2002/10/18 02:39:51 nathanw Exp $	*/
      2  1.3.2.2  nathanw 
      3  1.3.2.2  nathanw /*-
      4  1.3.2.2  nathanw  * Copyright (c) 2002 The NetBSD Foundation, Inc.
      5  1.3.2.2  nathanw  * All rights reserved.
      6  1.3.2.2  nathanw  *
      7  1.3.2.2  nathanw  * This code is derived from software contributed to The NetBSD Foundation
      8  1.3.2.2  nathanw  * by Jason R. Thorpe.
      9  1.3.2.2  nathanw  *
     10  1.3.2.2  nathanw  * Redistribution and use in source and binary forms, with or without
     11  1.3.2.2  nathanw  * modification, are permitted provided that the following conditions
     12  1.3.2.2  nathanw  * are met:
     13  1.3.2.2  nathanw  * 1. Redistributions of source code must retain the above copyright
     14  1.3.2.2  nathanw  *    notice, this list of conditions and the following disclaimer.
     15  1.3.2.2  nathanw  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.3.2.2  nathanw  *    notice, this list of conditions and the following disclaimer in the
     17  1.3.2.2  nathanw  *    documentation and/or other materials provided with the distribution.
     18  1.3.2.2  nathanw  * 3. All advertising materials mentioning features or use of this software
     19  1.3.2.2  nathanw  *    must display the following acknowledgement:
     20  1.3.2.2  nathanw  *	This product includes software developed by the NetBSD
     21  1.3.2.2  nathanw  *	Foundation, Inc. and its contributors.
     22  1.3.2.2  nathanw  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  1.3.2.2  nathanw  *    contributors may be used to endorse or promote products derived
     24  1.3.2.2  nathanw  *    from this software without specific prior written permission.
     25  1.3.2.2  nathanw  *
     26  1.3.2.2  nathanw  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  1.3.2.2  nathanw  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  1.3.2.2  nathanw  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  1.3.2.2  nathanw  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  1.3.2.2  nathanw  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  1.3.2.2  nathanw  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  1.3.2.2  nathanw  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  1.3.2.2  nathanw  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  1.3.2.2  nathanw  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  1.3.2.2  nathanw  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  1.3.2.2  nathanw  * POSSIBILITY OF SUCH DAMAGE.
     37  1.3.2.2  nathanw  */
     38  1.3.2.2  nathanw 
     39  1.3.2.2  nathanw /*
     40  1.3.2.2  nathanw  * Autoconfiguration support for Sun4d "bootbus".
     41  1.3.2.2  nathanw  */
     42  1.3.2.2  nathanw 
     43  1.3.2.2  nathanw #include <sys/param.h>
     44  1.3.2.2  nathanw #include <sys/malloc.h>
     45  1.3.2.2  nathanw #include <sys/systm.h>
     46  1.3.2.2  nathanw #include <sys/device.h>
     47  1.3.2.2  nathanw 
     48  1.3.2.2  nathanw #include <machine/autoconf.h>
     49  1.3.2.2  nathanw #include <machine/bus.h>
     50  1.3.2.2  nathanw 
     51  1.3.2.2  nathanw #include <sparc/sparc/cpuunitvar.h>
     52  1.3.2.2  nathanw #include <sparc/dev/bootbusvar.h>
     53  1.3.2.2  nathanw 
     54  1.3.2.2  nathanw #include "locators.h"
     55  1.3.2.2  nathanw 
     56  1.3.2.2  nathanw struct bootbus_softc {
     57  1.3.2.2  nathanw 	struct device sc_dev;
     58  1.3.2.2  nathanw 	int sc_node;				/* our OBP node */
     59  1.3.2.2  nathanw 
     60  1.3.2.2  nathanw 	bus_space_tag_t sc_st;			/* ours */
     61  1.3.2.2  nathanw 	bus_space_tag_t sc_bustag;		/* passed on to children */
     62  1.3.2.2  nathanw };
     63  1.3.2.2  nathanw 
     64  1.3.2.2  nathanw static int bootbus_match(struct device *, struct cfdata *, void *);
     65  1.3.2.2  nathanw static void bootbus_attach(struct device *, struct device *, void *);
     66  1.3.2.2  nathanw 
     67  1.3.2.3  nathanw CFATTACH_DECL(bootbus, sizeof(struct bootbus_softc),
     68  1.3.2.3  nathanw     bootbus_match, bootbus_attach, NULL, NULL);
     69  1.3.2.2  nathanw 
     70  1.3.2.2  nathanw static int bootbus_submatch(struct device *, struct cfdata *, void *);
     71  1.3.2.2  nathanw static int bootbus_print(void *, const char *);
     72  1.3.2.2  nathanw 
     73  1.3.2.2  nathanw static int bootbus_setup_attach_args(struct bootbus_softc *, bus_space_tag_t,
     74  1.3.2.2  nathanw     int, struct bootbus_attach_args *);
     75  1.3.2.2  nathanw static void bootbus_destroy_attach_args(struct bootbus_attach_args *);
     76  1.3.2.2  nathanw 
     77  1.3.2.2  nathanw static int
     78  1.3.2.2  nathanw bootbus_match(struct device *parent, struct cfdata *cf, void *aux)
     79  1.3.2.2  nathanw {
     80  1.3.2.2  nathanw 	struct cpuunit_attach_args *cpua = aux;
     81  1.3.2.2  nathanw 
     82  1.3.2.3  nathanw 	if (strcmp(cpua->cpua_name, cf->cf_name) == 0)
     83  1.3.2.2  nathanw 		return (1);
     84  1.3.2.2  nathanw 
     85  1.3.2.2  nathanw 	return (0);
     86  1.3.2.2  nathanw }
     87  1.3.2.2  nathanw 
     88  1.3.2.2  nathanw static void
     89  1.3.2.2  nathanw bootbus_attach(struct device *parent, struct device *self, void *aux)
     90  1.3.2.2  nathanw {
     91  1.3.2.2  nathanw 	struct bootbus_softc *sc = (void *) self;
     92  1.3.2.2  nathanw 	struct cpuunit_attach_args *cpua = aux;
     93  1.3.2.2  nathanw 	int node, error;
     94  1.3.2.2  nathanw 
     95  1.3.2.2  nathanw 	sc->sc_node = cpua->cpua_node;
     96  1.3.2.2  nathanw 	sc->sc_st = cpua->cpua_bustag;
     97  1.3.2.2  nathanw 
     98  1.3.2.2  nathanw 	printf("\n");
     99  1.3.2.2  nathanw 
    100  1.3.2.2  nathanw 	/*
    101  1.3.2.2  nathanw 	 * Initialize the bus space tag we pass on to our children.
    102  1.3.2.2  nathanw 	 */
    103  1.3.2.2  nathanw 	sc->sc_bustag = malloc(sizeof(*sc->sc_bustag), M_DEVBUF,
    104  1.3.2.2  nathanw 	    M_WAITOK|M_ZERO);
    105  1.3.2.2  nathanw 	sc->sc_bustag->cookie = sc;
    106  1.3.2.2  nathanw 	sc->sc_bustag->parent = sc->sc_st;
    107  1.3.2.2  nathanw 	sc->sc_bustag->sparc_bus_map = sc->sc_st->sparc_bus_map;
    108  1.3.2.2  nathanw 	sc->sc_bustag->sparc_bus_mmap = sc->sc_st->sparc_bus_mmap;
    109  1.3.2.2  nathanw 
    110  1.3.2.2  nathanw 	/*
    111  1.3.2.2  nathanw 	 * Collect address translations from the OBP.
    112  1.3.2.2  nathanw 	 */
    113  1.3.2.2  nathanw 	error = PROM_getprop(sc->sc_node, "ranges",
    114  1.3.2.2  nathanw 	    sizeof(struct openprom_range), &sc->sc_bustag->nranges,
    115  1.3.2.2  nathanw 	    (void **) &sc->sc_bustag->ranges);
    116  1.3.2.2  nathanw 	if (error) {
    117  1.3.2.2  nathanw 		printf("%s: error %d getting \"ranges\" property\n",
    118  1.3.2.2  nathanw 		    sc->sc_dev.dv_xname, error);
    119  1.3.2.2  nathanw 		panic("bootbus_attach");
    120  1.3.2.2  nathanw 	}
    121  1.3.2.2  nathanw 
    122  1.3.2.2  nathanw 	/* Attach the CPU (and possibly bootbus) child nodes. */
    123  1.3.2.2  nathanw 	for (node = firstchild(sc->sc_node); node != 0;
    124  1.3.2.2  nathanw 	     node = nextsibling(node)) {
    125  1.3.2.2  nathanw 		struct bootbus_attach_args baa;
    126  1.3.2.2  nathanw 
    127  1.3.2.2  nathanw 		if (bootbus_setup_attach_args(sc, sc->sc_bustag, node, &baa))
    128  1.3.2.2  nathanw 			panic("bootbus_attach: failed to set up attach args");
    129  1.3.2.2  nathanw 
    130  1.3.2.2  nathanw 		(void) config_found_sm(&sc->sc_dev, &baa, bootbus_print,
    131  1.3.2.2  nathanw 		    bootbus_submatch);
    132  1.3.2.2  nathanw 
    133  1.3.2.2  nathanw 		bootbus_destroy_attach_args(&baa);
    134  1.3.2.2  nathanw 	}
    135  1.3.2.2  nathanw }
    136  1.3.2.2  nathanw 
    137  1.3.2.2  nathanw static int
    138  1.3.2.2  nathanw bootbus_submatch(struct device *parent, struct cfdata *cf, void *aux)
    139  1.3.2.2  nathanw {
    140  1.3.2.2  nathanw 	struct bootbus_attach_args *baa = aux;
    141  1.3.2.2  nathanw 
    142  1.3.2.2  nathanw 	if (cf->cf_loc[BOOTBUSCF_SLOT] != BOOTBUSCF_SLOT_DEFAULT &&
    143  1.3.2.2  nathanw 	    cf->cf_loc[BOOTBUSCF_SLOT] != baa->ba_slot)
    144  1.3.2.2  nathanw 		return (0);
    145  1.3.2.2  nathanw 
    146  1.3.2.2  nathanw 	if (cf->cf_loc[BOOTBUSCF_OFFSET] != BOOTBUSCF_OFFSET_DEFAULT &&
    147  1.3.2.2  nathanw 	    cf->cf_loc[BOOTBUSCF_OFFSET] != baa->ba_offset)
    148  1.3.2.2  nathanw 		return (0);
    149  1.3.2.2  nathanw 
    150  1.3.2.3  nathanw 	return (config_match(parent, cf, aux));
    151  1.3.2.2  nathanw }
    152  1.3.2.2  nathanw 
    153  1.3.2.2  nathanw static int
    154  1.3.2.2  nathanw bootbus_print(void *aux, const char *pnp)
    155  1.3.2.2  nathanw {
    156  1.3.2.2  nathanw 	struct bootbus_attach_args *baa = aux;
    157  1.3.2.2  nathanw 	int i;
    158  1.3.2.2  nathanw 
    159  1.3.2.2  nathanw 	if (pnp)
    160  1.3.2.2  nathanw 		printf("%s at %s", baa->ba_name, pnp);
    161  1.3.2.2  nathanw 	printf(" slot %d offset 0x%x", baa->ba_slot, baa->ba_offset);
    162  1.3.2.2  nathanw 	for (i = 0; i < baa->ba_nintr; i++)
    163  1.3.2.2  nathanw 		printf(" ipl %d", baa->ba_intr[i].oi_pri);
    164  1.3.2.2  nathanw 
    165  1.3.2.2  nathanw 	return (UNCONF);
    166  1.3.2.2  nathanw }
    167  1.3.2.2  nathanw 
    168  1.3.2.2  nathanw static int
    169  1.3.2.2  nathanw bootbus_setup_attach_args(struct bootbus_softc *sc, bus_space_tag_t bustag,
    170  1.3.2.2  nathanw     int node, struct bootbus_attach_args *baa)
    171  1.3.2.2  nathanw {
    172  1.3.2.2  nathanw 	int n, error;
    173  1.3.2.2  nathanw 
    174  1.3.2.2  nathanw 	memset(baa, 0, sizeof(*baa));
    175  1.3.2.2  nathanw 
    176  1.3.2.2  nathanw 	error = PROM_getprop(node, "name", 1, &n, (void **) &baa->ba_name);
    177  1.3.2.2  nathanw 	if (error)
    178  1.3.2.2  nathanw 		return (error);
    179  1.3.2.2  nathanw 	baa->ba_name[n] = '\0';
    180  1.3.2.2  nathanw 
    181  1.3.2.2  nathanw 	baa->ba_bustag = bustag;
    182  1.3.2.2  nathanw 	baa->ba_node = node;
    183  1.3.2.2  nathanw 
    184  1.3.2.2  nathanw 	error = PROM_getprop(node, "reg", sizeof(struct openprom_addr),
    185  1.3.2.2  nathanw 	    &baa->ba_nreg, (void **) &baa->ba_reg);
    186  1.3.2.2  nathanw 	if (error) {
    187  1.3.2.2  nathanw 		bootbus_destroy_attach_args(baa);
    188  1.3.2.2  nathanw 		return (error);
    189  1.3.2.2  nathanw 	}
    190  1.3.2.2  nathanw 
    191  1.3.2.2  nathanw 	error = PROM_getprop(node, "intr", sizeof(struct openprom_intr),
    192  1.3.2.2  nathanw 	    &baa->ba_nintr, (void **) &baa->ba_intr);
    193  1.3.2.2  nathanw 	if (error != 0 && error != ENOENT) {
    194  1.3.2.2  nathanw 		bootbus_destroy_attach_args(baa);
    195  1.3.2.2  nathanw 		return (error);
    196  1.3.2.2  nathanw 	}
    197  1.3.2.2  nathanw 
    198  1.3.2.2  nathanw 	error = PROM_getprop(node, "address", sizeof(uint32_t),
    199  1.3.2.2  nathanw 	    &baa->ba_npromvaddrs, (void **) &baa->ba_promvaddrs);
    200  1.3.2.2  nathanw 	if (error != 0 && error != ENOENT) {
    201  1.3.2.2  nathanw 		bootbus_destroy_attach_args(baa);
    202  1.3.2.2  nathanw 		return (error);
    203  1.3.2.2  nathanw 	}
    204  1.3.2.2  nathanw 
    205  1.3.2.2  nathanw 	return (0);
    206  1.3.2.2  nathanw }
    207  1.3.2.2  nathanw 
    208  1.3.2.2  nathanw static void
    209  1.3.2.2  nathanw bootbus_destroy_attach_args(struct bootbus_attach_args *baa)
    210  1.3.2.2  nathanw {
    211  1.3.2.2  nathanw 
    212  1.3.2.2  nathanw 	if (baa->ba_name != NULL)
    213  1.3.2.2  nathanw 		free(baa->ba_name, M_DEVBUF);
    214  1.3.2.2  nathanw 
    215  1.3.2.2  nathanw 	if (baa->ba_reg != NULL)
    216  1.3.2.2  nathanw 		free(baa->ba_reg, M_DEVBUF);
    217  1.3.2.2  nathanw 
    218  1.3.2.2  nathanw 	if (baa->ba_intr != NULL)
    219  1.3.2.2  nathanw 		free(baa->ba_intr, M_DEVBUF);
    220  1.3.2.2  nathanw 
    221  1.3.2.2  nathanw 	if (baa->ba_promvaddrs != NULL)
    222  1.3.2.2  nathanw 		free(baa->ba_promvaddrs, M_DEVBUF);
    223  1.3.2.2  nathanw }
    224