Home | History | Annotate | Line # | Download | only in sbus
qec.c revision 1.6
      1  1.6  pk /*	$NetBSD: qec.c,v 1.6 1998/08/30 21:25:30 pk Exp $ */
      2  1.1  pk 
      3  1.1  pk /*-
      4  1.1  pk  * Copyright (c) 1998 The NetBSD Foundation, Inc.
      5  1.1  pk  * All rights reserved.
      6  1.1  pk  *
      7  1.1  pk  * This code is derived from software contributed to The NetBSD Foundation
      8  1.1  pk  * by Paul Kranenburg.
      9  1.1  pk  *
     10  1.1  pk  * Redistribution and use in source and binary forms, with or without
     11  1.1  pk  * modification, are permitted provided that the following conditions
     12  1.1  pk  * are met:
     13  1.1  pk  * 1. Redistributions of source code must retain the above copyright
     14  1.1  pk  *    notice, this list of conditions and the following disclaimer.
     15  1.1  pk  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.1  pk  *    notice, this list of conditions and the following disclaimer in the
     17  1.1  pk  *    documentation and/or other materials provided with the distribution.
     18  1.1  pk  * 3. All advertising materials mentioning features or use of this software
     19  1.1  pk  *    must display the following acknowledgement:
     20  1.1  pk  *        This product includes software developed by the NetBSD
     21  1.1  pk  *        Foundation, Inc. and its contributors.
     22  1.1  pk  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  1.1  pk  *    contributors may be used to endorse or promote products derived
     24  1.1  pk  *    from this software without specific prior written permission.
     25  1.1  pk  *
     26  1.1  pk  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  1.1  pk  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  1.1  pk  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  1.1  pk  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  1.1  pk  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  1.1  pk  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  1.1  pk  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  1.1  pk  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  1.1  pk  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  1.1  pk  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  1.1  pk  * POSSIBILITY OF SUCH DAMAGE.
     37  1.1  pk  */
     38  1.1  pk 
     39  1.1  pk #include <sys/types.h>
     40  1.1  pk #include <sys/param.h>
     41  1.1  pk #include <sys/systm.h>
     42  1.1  pk #include <sys/kernel.h>
     43  1.1  pk #include <sys/errno.h>
     44  1.1  pk #include <sys/device.h>
     45  1.1  pk #include <sys/malloc.h>
     46  1.1  pk 
     47  1.2  pk #include <machine/bus.h>
     48  1.2  pk #include <machine/autoconf.h>
     49  1.1  pk 
     50  1.1  pk #include <dev/sbus/sbusvar.h>
     51  1.1  pk #include <dev/sbus/qecreg.h>
     52  1.1  pk #include <dev/sbus/qecvar.h>
     53  1.1  pk 
     54  1.1  pk static int	qecprint	__P((void *, const char *));
     55  1.1  pk static int	qecmatch	__P((struct device *, struct cfdata *, void *));
     56  1.1  pk static void	qecattach	__P((struct device *, struct device *, void *));
     57  1.1  pk 
     58  1.1  pk static int qec_bus_map __P((
     59  1.1  pk 		bus_space_tag_t,
     60  1.1  pk 		bus_type_t,		/*slot*/
     61  1.1  pk 		bus_addr_t,		/*offset*/
     62  1.1  pk 		bus_size_t,		/*size*/
     63  1.1  pk 		int,			/*flags*/
     64  1.4  pk 		vaddr_t,		/*preferred virtual address */
     65  1.1  pk 		bus_space_handle_t *));
     66  1.1  pk 
     67  1.1  pk struct cfattach qec_ca = {
     68  1.1  pk 	sizeof(struct qec_softc), qecmatch, qecattach
     69  1.1  pk };
     70  1.1  pk 
     71  1.1  pk int
     72  1.1  pk qecprint(aux, busname)
     73  1.1  pk 	void *aux;
     74  1.1  pk 	const char *busname;
     75  1.1  pk {
     76  1.1  pk 	struct sbus_attach_args *sa = aux;
     77  1.1  pk 	bus_space_tag_t t = sa->sa_bustag;
     78  1.1  pk 	struct qec_softc *sc = t->cookie;
     79  1.1  pk 
     80  1.1  pk 	sa->sa_bustag = sc->sc_bustag;	/* XXX */
     81  1.1  pk 	sbus_print(aux, busname);	/* XXX */
     82  1.1  pk 	sa->sa_bustag = t;		/* XXX */
     83  1.1  pk 	return (UNCONF);
     84  1.1  pk }
     85  1.1  pk 
     86  1.1  pk int
     87  1.1  pk qecmatch(parent, cf, aux)
     88  1.1  pk 	struct device *parent;
     89  1.1  pk 	struct cfdata *cf;
     90  1.1  pk 	void *aux;
     91  1.1  pk {
     92  1.1  pk 	struct sbus_attach_args *sa = aux;
     93  1.1  pk 
     94  1.1  pk 	return (strcmp(cf->cf_driver->cd_name, sa->sa_name) == 0);
     95  1.1  pk }
     96  1.1  pk 
     97  1.1  pk /*
     98  1.1  pk  * Attach all the sub-devices we can find
     99  1.1  pk  */
    100  1.1  pk void
    101  1.1  pk qecattach(parent, self, aux)
    102  1.1  pk 	struct device *parent, *self;
    103  1.1  pk 	void *aux;
    104  1.1  pk {
    105  1.1  pk 	struct sbus_attach_args *sa = aux;
    106  1.1  pk 	struct qec_softc *sc = (void *)self;
    107  1.1  pk 	int node;
    108  1.1  pk 	int sbusburst;
    109  1.1  pk 	bus_space_tag_t sbt;
    110  1.1  pk 	bus_space_handle_t bh;
    111  1.1  pk 	struct bootpath *bp;
    112  1.1  pk 	int error;
    113  1.1  pk 
    114  1.1  pk 	sc->sc_bustag = sa->sa_bustag;
    115  1.1  pk 	sc->sc_dmatag = sa->sa_dmatag;
    116  1.1  pk 	node = sa->sa_node;
    117  1.1  pk 
    118  1.6  pk 	if (sa->sa_nreg < 2) {
    119  1.6  pk 		printf("%s: only %d register sets\n",
    120  1.6  pk 			self->dv_xname, sa->sa_nreg);
    121  1.1  pk 		return;
    122  1.1  pk 	}
    123  1.1  pk 
    124  1.1  pk 	if (sbus_bus_map(sa->sa_bustag,
    125  1.6  pk 			 sa->sa_reg[0].sbr_slot,
    126  1.6  pk 			 sa->sa_reg[0].sbr_offset,
    127  1.6  pk 			 sa->sa_reg[0].sbr_size,
    128  1.1  pk 			 BUS_SPACE_MAP_LINEAR, 0, &bh) != 0) {
    129  1.1  pk 		printf("%s: attach: cannot map registers\n", self->dv_xname);
    130  1.1  pk 		return;
    131  1.1  pk 	}
    132  1.1  pk 	sc->sc_regs = (void *)bh;
    133  1.1  pk 
    134  1.1  pk 	/*
    135  1.1  pk 	 * This device's "register space 1" is just a buffer where the
    136  1.1  pk 	 * Lance ring-buffers can be stored. Note the buffer's location
    137  1.1  pk 	 * and size, so the child driver can pick them up.
    138  1.1  pk 	 */
    139  1.1  pk 	if (sbus_bus_map(sa->sa_bustag,
    140  1.6  pk 			 sa->sa_reg[1].sbr_slot,
    141  1.6  pk 			 sa->sa_reg[1].sbr_offset,
    142  1.6  pk 			 sa->sa_reg[1].sbr_size,
    143  1.1  pk 			 BUS_SPACE_MAP_LINEAR, 0, &bh) != 0) {
    144  1.1  pk 		printf("%s: attach: cannot map registers\n", self->dv_xname);
    145  1.1  pk 		return;
    146  1.1  pk 	}
    147  1.1  pk 	sc->sc_buffer = (caddr_t)bh;
    148  1.6  pk 	sc->sc_bufsiz = (bus_size_t)sa->sa_reg[1].sbr_size;
    149  1.1  pk 
    150  1.1  pk 	/*
    151  1.1  pk 	 * Get transfer burst size from PROM
    152  1.1  pk 	 */
    153  1.1  pk 	sbusburst = ((struct sbus_softc *)parent)->sc_burst;
    154  1.1  pk 	if (sbusburst == 0)
    155  1.1  pk 		sbusburst = SBUS_BURST_32 - 1; /* 1->16 */
    156  1.1  pk 
    157  1.1  pk 	sc->sc_burst = getpropint(node, "burst-sizes", -1);
    158  1.1  pk 	if (sc->sc_burst == -1)
    159  1.1  pk 		/* take SBus burst sizes */
    160  1.1  pk 		sc->sc_burst = sbusburst;
    161  1.1  pk 
    162  1.1  pk 	/* Clamp at parent's burst sizes */
    163  1.1  pk 	sc->sc_burst &= sbusburst;
    164  1.1  pk 
    165  1.1  pk 	sbus_establish(&sc->sc_sd, &sc->sc_dev);
    166  1.1  pk 
    167  1.1  pk 
    168  1.1  pk 	/*
    169  1.1  pk 	 * Collect address translations from the OBP.
    170  1.1  pk 	 */
    171  1.6  pk 	error = getprop(node, "ranges", sizeof(struct sbus_range),
    172  1.1  pk 			 &sc->sc_nrange, (void **)&sc->sc_range);
    173  1.1  pk 	switch (error) {
    174  1.1  pk 	case 0:
    175  1.1  pk 		break;
    176  1.1  pk 	case ENOENT:
    177  1.1  pk 	default:
    178  1.1  pk 		panic("%s: error getting ranges property", self->dv_xname);
    179  1.1  pk 	}
    180  1.1  pk 
    181  1.1  pk 	/* Propagate bootpath */
    182  1.1  pk 	if (sa->sa_bp != NULL)
    183  1.1  pk 		bp = sa->sa_bp + 1;
    184  1.1  pk 	else
    185  1.1  pk 		bp = NULL;
    186  1.1  pk 
    187  1.1  pk 	/* Allocate a bus tag */
    188  1.1  pk 	sbt = (bus_space_tag_t)
    189  1.1  pk 		malloc(sizeof(struct sparc_bus_space_tag), M_DEVBUF, M_NOWAIT);
    190  1.1  pk 	if (sbt == NULL) {
    191  1.1  pk 		printf("%s: attach: out of memory\n", self->dv_xname);
    192  1.1  pk 		return;
    193  1.1  pk 	}
    194  1.1  pk 
    195  1.1  pk 	bzero(sbt, sizeof *sbt);
    196  1.1  pk 	sbt->cookie = sc;
    197  1.1  pk 	sbt->parent = sc->sc_bustag;
    198  1.1  pk 	sbt->sparc_bus_map = qec_bus_map;
    199  1.1  pk 
    200  1.1  pk 	printf(": %dK memory\n", sc->sc_bufsiz / 1024);
    201  1.1  pk 
    202  1.1  pk 	/* search through children */
    203  1.1  pk 	for (node = firstchild(node); node; node = nextsibling(node)) {
    204  1.1  pk 		struct sbus_attach_args sa;
    205  1.1  pk 		sbus_setup_attach_args((struct sbus_softc *)parent,
    206  1.1  pk 				       sbt, sc->sc_dmatag, node, bp, &sa);
    207  1.1  pk 		(void)config_found(&sc->sc_dev, (void *)&sa, qecprint);
    208  1.3  pk 		sbus_destroy_attach_args(&sa);
    209  1.1  pk 	}
    210  1.1  pk }
    211  1.1  pk 
    212  1.1  pk int
    213  1.1  pk qec_bus_map(t, btype, offset, size, flags, vaddr, hp)
    214  1.1  pk 	bus_space_tag_t t;
    215  1.1  pk 	bus_type_t btype;
    216  1.1  pk 	bus_addr_t offset;
    217  1.1  pk 	bus_size_t size;
    218  1.1  pk 	int	flags;
    219  1.4  pk 	vaddr_t vaddr;
    220  1.1  pk 	bus_space_handle_t *hp;
    221  1.1  pk {
    222  1.1  pk 	struct qec_softc *sc = t->cookie;
    223  1.1  pk 	int slot = btype;
    224  1.1  pk 	int i;
    225  1.1  pk 
    226  1.1  pk 	for (i = 0; i < sc->sc_nrange; i++) {
    227  1.1  pk 		bus_addr_t paddr;
    228  1.1  pk 		bus_type_t iospace;
    229  1.1  pk 
    230  1.1  pk 		if (sc->sc_range[i].cspace != slot)
    231  1.1  pk 			continue;
    232  1.1  pk 
    233  1.1  pk 		/* We've found the connection to the parent bus */
    234  1.1  pk 		paddr = sc->sc_range[i].poffset + offset;
    235  1.1  pk 		iospace = sc->sc_range[i].pspace;
    236  1.1  pk 		return (bus_space_map2(sc->sc_bustag, iospace, paddr,
    237  1.1  pk 					size, flags, vaddr, hp));
    238  1.1  pk 	}
    239  1.1  pk 
    240  1.1  pk 	return (EINVAL);
    241  1.1  pk }
    242