qec.c revision 1.5 1 1.5 pk /* $NetBSD: qec.c,v 1.5 1998/08/29 20:38:38 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 int nreg;
114 1.1 pk struct rom_reg *rr;
115 1.1 pk
116 1.1 pk sc->sc_bustag = sa->sa_bustag;
117 1.1 pk sc->sc_dmatag = sa->sa_dmatag;
118 1.1 pk node = sa->sa_node;
119 1.1 pk
120 1.1 pk rr = NULL;
121 1.1 pk if (getpropA(node, "reg", sizeof(struct rom_reg),
122 1.1 pk &nreg, (void **)&rr) != 0) {
123 1.1 pk printf("%s: cannot get register property\n", self->dv_xname);
124 1.1 pk return;
125 1.1 pk }
126 1.1 pk if (nreg < 2) {
127 1.1 pk printf("%s: only %d register sets\n", self->dv_xname, nreg);
128 1.1 pk return;
129 1.1 pk }
130 1.1 pk
131 1.1 pk if (sbus_bus_map(sa->sa_bustag,
132 1.1 pk (bus_type_t)rr[0].rr_iospace,
133 1.1 pk (bus_addr_t)rr[0].rr_paddr,
134 1.1 pk (bus_size_t)rr[0].rr_len,
135 1.1 pk BUS_SPACE_MAP_LINEAR, 0, &bh) != 0) {
136 1.1 pk printf("%s: attach: cannot map registers\n", self->dv_xname);
137 1.1 pk return;
138 1.1 pk }
139 1.1 pk sc->sc_regs = (void *)bh;
140 1.1 pk
141 1.1 pk /*
142 1.1 pk * This device's "register space 1" is just a buffer where the
143 1.1 pk * Lance ring-buffers can be stored. Note the buffer's location
144 1.1 pk * and size, so the child driver can pick them up.
145 1.1 pk */
146 1.1 pk if (sbus_bus_map(sa->sa_bustag,
147 1.1 pk (bus_type_t)rr[1].rr_iospace,
148 1.1 pk (bus_addr_t)rr[1].rr_paddr,
149 1.1 pk (bus_size_t)rr[1].rr_len,
150 1.1 pk BUS_SPACE_MAP_LINEAR, 0, &bh) != 0) {
151 1.1 pk printf("%s: attach: cannot map registers\n", self->dv_xname);
152 1.1 pk return;
153 1.1 pk }
154 1.1 pk sc->sc_buffer = (caddr_t)bh;
155 1.1 pk sc->sc_bufsiz = (bus_size_t)rr[1].rr_len;
156 1.1 pk
157 1.1 pk /*
158 1.1 pk * Get transfer burst size from PROM
159 1.1 pk */
160 1.1 pk sbusburst = ((struct sbus_softc *)parent)->sc_burst;
161 1.1 pk if (sbusburst == 0)
162 1.1 pk sbusburst = SBUS_BURST_32 - 1; /* 1->16 */
163 1.1 pk
164 1.1 pk sc->sc_burst = getpropint(node, "burst-sizes", -1);
165 1.1 pk if (sc->sc_burst == -1)
166 1.1 pk /* take SBus burst sizes */
167 1.1 pk sc->sc_burst = sbusburst;
168 1.1 pk
169 1.1 pk /* Clamp at parent's burst sizes */
170 1.1 pk sc->sc_burst &= sbusburst;
171 1.1 pk
172 1.1 pk sbus_establish(&sc->sc_sd, &sc->sc_dev);
173 1.1 pk
174 1.1 pk
175 1.1 pk /*
176 1.1 pk * Collect address translations from the OBP.
177 1.1 pk */
178 1.3 pk error = getpropA(node, "ranges", sizeof(struct sbus_range),
179 1.1 pk &sc->sc_nrange, (void **)&sc->sc_range);
180 1.1 pk switch (error) {
181 1.1 pk case 0:
182 1.1 pk break;
183 1.1 pk case ENOENT:
184 1.1 pk default:
185 1.1 pk panic("%s: error getting ranges property", self->dv_xname);
186 1.1 pk }
187 1.1 pk
188 1.1 pk /* Propagate bootpath */
189 1.1 pk if (sa->sa_bp != NULL)
190 1.1 pk bp = sa->sa_bp + 1;
191 1.1 pk else
192 1.1 pk bp = NULL;
193 1.1 pk
194 1.1 pk /* Allocate a bus tag */
195 1.1 pk sbt = (bus_space_tag_t)
196 1.1 pk malloc(sizeof(struct sparc_bus_space_tag), M_DEVBUF, M_NOWAIT);
197 1.1 pk if (sbt == NULL) {
198 1.1 pk printf("%s: attach: out of memory\n", self->dv_xname);
199 1.1 pk return;
200 1.1 pk }
201 1.1 pk
202 1.1 pk bzero(sbt, sizeof *sbt);
203 1.1 pk sbt->cookie = sc;
204 1.1 pk sbt->parent = sc->sc_bustag;
205 1.1 pk sbt->sparc_bus_map = qec_bus_map;
206 1.1 pk
207 1.1 pk printf(": %dK memory\n", sc->sc_bufsiz / 1024);
208 1.1 pk
209 1.1 pk /* search through children */
210 1.1 pk for (node = firstchild(node); node; node = nextsibling(node)) {
211 1.1 pk struct sbus_attach_args sa;
212 1.1 pk sbus_setup_attach_args((struct sbus_softc *)parent,
213 1.1 pk sbt, sc->sc_dmatag, node, bp, &sa);
214 1.1 pk (void)config_found(&sc->sc_dev, (void *)&sa, qecprint);
215 1.3 pk sbus_destroy_attach_args(&sa);
216 1.1 pk }
217 1.1 pk free(rr, M_DEVBUF);
218 1.1 pk }
219 1.1 pk
220 1.1 pk int
221 1.1 pk qec_bus_map(t, btype, offset, size, flags, vaddr, hp)
222 1.1 pk bus_space_tag_t t;
223 1.1 pk bus_type_t btype;
224 1.1 pk bus_addr_t offset;
225 1.1 pk bus_size_t size;
226 1.1 pk int flags;
227 1.4 pk vaddr_t vaddr;
228 1.1 pk bus_space_handle_t *hp;
229 1.1 pk {
230 1.1 pk struct qec_softc *sc = t->cookie;
231 1.1 pk int slot = btype;
232 1.1 pk int i;
233 1.1 pk
234 1.1 pk for (i = 0; i < sc->sc_nrange; i++) {
235 1.1 pk bus_addr_t paddr;
236 1.1 pk bus_type_t iospace;
237 1.1 pk
238 1.1 pk if (sc->sc_range[i].cspace != slot)
239 1.1 pk continue;
240 1.1 pk
241 1.1 pk /* We've found the connection to the parent bus */
242 1.1 pk paddr = sc->sc_range[i].poffset + offset;
243 1.1 pk iospace = sc->sc_range[i].pspace;
244 1.1 pk return (bus_space_map2(sc->sc_bustag, iospace, paddr,
245 1.1 pk size, flags, vaddr, hp));
246 1.1 pk }
247 1.1 pk
248 1.1 pk return (EINVAL);
249 1.1 pk }
250