Home | History | Annotate | Line # | Download | only in sbus
qec.c revision 1.5
      1 /*	$NetBSD: qec.c,v 1.5 1998/08/29 20:38:38 pk Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 1998 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Paul Kranenburg.
      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 #include <sys/types.h>
     40 #include <sys/param.h>
     41 #include <sys/systm.h>
     42 #include <sys/kernel.h>
     43 #include <sys/errno.h>
     44 #include <sys/device.h>
     45 #include <sys/malloc.h>
     46 
     47 #include <machine/bus.h>
     48 #include <machine/autoconf.h>
     49 
     50 #include <dev/sbus/sbusvar.h>
     51 #include <dev/sbus/qecreg.h>
     52 #include <dev/sbus/qecvar.h>
     53 
     54 static int	qecprint	__P((void *, const char *));
     55 static int	qecmatch	__P((struct device *, struct cfdata *, void *));
     56 static void	qecattach	__P((struct device *, struct device *, void *));
     57 
     58 static int qec_bus_map __P((
     59 		bus_space_tag_t,
     60 		bus_type_t,		/*slot*/
     61 		bus_addr_t,		/*offset*/
     62 		bus_size_t,		/*size*/
     63 		int,			/*flags*/
     64 		vaddr_t,		/*preferred virtual address */
     65 		bus_space_handle_t *));
     66 
     67 struct cfattach qec_ca = {
     68 	sizeof(struct qec_softc), qecmatch, qecattach
     69 };
     70 
     71 int
     72 qecprint(aux, busname)
     73 	void *aux;
     74 	const char *busname;
     75 {
     76 	struct sbus_attach_args *sa = aux;
     77 	bus_space_tag_t t = sa->sa_bustag;
     78 	struct qec_softc *sc = t->cookie;
     79 
     80 	sa->sa_bustag = sc->sc_bustag;	/* XXX */
     81 	sbus_print(aux, busname);	/* XXX */
     82 	sa->sa_bustag = t;		/* XXX */
     83 	return (UNCONF);
     84 }
     85 
     86 int
     87 qecmatch(parent, cf, aux)
     88 	struct device *parent;
     89 	struct cfdata *cf;
     90 	void *aux;
     91 {
     92 	struct sbus_attach_args *sa = aux;
     93 
     94 	return (strcmp(cf->cf_driver->cd_name, sa->sa_name) == 0);
     95 }
     96 
     97 /*
     98  * Attach all the sub-devices we can find
     99  */
    100 void
    101 qecattach(parent, self, aux)
    102 	struct device *parent, *self;
    103 	void *aux;
    104 {
    105 	struct sbus_attach_args *sa = aux;
    106 	struct qec_softc *sc = (void *)self;
    107 	int node;
    108 	int sbusburst;
    109 	bus_space_tag_t sbt;
    110 	bus_space_handle_t bh;
    111 	struct bootpath *bp;
    112 	int error;
    113 	int nreg;
    114 	struct rom_reg *rr;
    115 
    116 	sc->sc_bustag = sa->sa_bustag;
    117 	sc->sc_dmatag = sa->sa_dmatag;
    118 	node = sa->sa_node;
    119 
    120 	rr = NULL;
    121 	if (getpropA(node, "reg", sizeof(struct rom_reg),
    122 		     &nreg, (void **)&rr) != 0) {
    123 		printf("%s: cannot get register property\n", self->dv_xname);
    124 		return;
    125 	}
    126 	if (nreg < 2) {
    127 		printf("%s: only %d register sets\n", self->dv_xname, nreg);
    128 		return;
    129 	}
    130 
    131 	if (sbus_bus_map(sa->sa_bustag,
    132 			 (bus_type_t)rr[0].rr_iospace,
    133 			 (bus_addr_t)rr[0].rr_paddr,
    134 			 (bus_size_t)rr[0].rr_len,
    135 			 BUS_SPACE_MAP_LINEAR, 0, &bh) != 0) {
    136 		printf("%s: attach: cannot map registers\n", self->dv_xname);
    137 		return;
    138 	}
    139 	sc->sc_regs = (void *)bh;
    140 
    141 	/*
    142 	 * This device's "register space 1" is just a buffer where the
    143 	 * Lance ring-buffers can be stored. Note the buffer's location
    144 	 * and size, so the child driver can pick them up.
    145 	 */
    146 	if (sbus_bus_map(sa->sa_bustag,
    147 			 (bus_type_t)rr[1].rr_iospace,
    148 			 (bus_addr_t)rr[1].rr_paddr,
    149 			 (bus_size_t)rr[1].rr_len,
    150 			 BUS_SPACE_MAP_LINEAR, 0, &bh) != 0) {
    151 		printf("%s: attach: cannot map registers\n", self->dv_xname);
    152 		return;
    153 	}
    154 	sc->sc_buffer = (caddr_t)bh;
    155 	sc->sc_bufsiz = (bus_size_t)rr[1].rr_len;
    156 
    157 	/*
    158 	 * Get transfer burst size from PROM
    159 	 */
    160 	sbusburst = ((struct sbus_softc *)parent)->sc_burst;
    161 	if (sbusburst == 0)
    162 		sbusburst = SBUS_BURST_32 - 1; /* 1->16 */
    163 
    164 	sc->sc_burst = getpropint(node, "burst-sizes", -1);
    165 	if (sc->sc_burst == -1)
    166 		/* take SBus burst sizes */
    167 		sc->sc_burst = sbusburst;
    168 
    169 	/* Clamp at parent's burst sizes */
    170 	sc->sc_burst &= sbusburst;
    171 
    172 	sbus_establish(&sc->sc_sd, &sc->sc_dev);
    173 
    174 
    175 	/*
    176 	 * Collect address translations from the OBP.
    177 	 */
    178 	error = getpropA(node, "ranges", sizeof(struct sbus_range),
    179 			 &sc->sc_nrange, (void **)&sc->sc_range);
    180 	switch (error) {
    181 	case 0:
    182 		break;
    183 	case ENOENT:
    184 	default:
    185 		panic("%s: error getting ranges property", self->dv_xname);
    186 	}
    187 
    188 	/* Propagate bootpath */
    189 	if (sa->sa_bp != NULL)
    190 		bp = sa->sa_bp + 1;
    191 	else
    192 		bp = NULL;
    193 
    194 	/* Allocate a bus tag */
    195 	sbt = (bus_space_tag_t)
    196 		malloc(sizeof(struct sparc_bus_space_tag), M_DEVBUF, M_NOWAIT);
    197 	if (sbt == NULL) {
    198 		printf("%s: attach: out of memory\n", self->dv_xname);
    199 		return;
    200 	}
    201 
    202 	bzero(sbt, sizeof *sbt);
    203 	sbt->cookie = sc;
    204 	sbt->parent = sc->sc_bustag;
    205 	sbt->sparc_bus_map = qec_bus_map;
    206 
    207 	printf(": %dK memory\n", sc->sc_bufsiz / 1024);
    208 
    209 	/* search through children */
    210 	for (node = firstchild(node); node; node = nextsibling(node)) {
    211 		struct sbus_attach_args sa;
    212 		sbus_setup_attach_args((struct sbus_softc *)parent,
    213 				       sbt, sc->sc_dmatag, node, bp, &sa);
    214 		(void)config_found(&sc->sc_dev, (void *)&sa, qecprint);
    215 		sbus_destroy_attach_args(&sa);
    216 	}
    217 	free(rr, M_DEVBUF);
    218 }
    219 
    220 int
    221 qec_bus_map(t, btype, offset, size, flags, vaddr, hp)
    222 	bus_space_tag_t t;
    223 	bus_type_t btype;
    224 	bus_addr_t offset;
    225 	bus_size_t size;
    226 	int	flags;
    227 	vaddr_t vaddr;
    228 	bus_space_handle_t *hp;
    229 {
    230 	struct qec_softc *sc = t->cookie;
    231 	int slot = btype;
    232 	int i;
    233 
    234 	for (i = 0; i < sc->sc_nrange; i++) {
    235 		bus_addr_t paddr;
    236 		bus_type_t iospace;
    237 
    238 		if (sc->sc_range[i].cspace != slot)
    239 			continue;
    240 
    241 		/* We've found the connection to the parent bus */
    242 		paddr = sc->sc_range[i].poffset + offset;
    243 		iospace = sc->sc_range[i].pspace;
    244 		return (bus_space_map2(sc->sc_bustag, iospace, paddr,
    245 					size, flags, vaddr, hp));
    246 	}
    247 
    248 	return (EINVAL);
    249 }
    250