qec.c revision 1.26 1 1.26 pk /* $NetBSD: qec.c,v 1.26 2002/12/10 13:44:48 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.14 lukem
39 1.14 lukem #include <sys/cdefs.h>
40 1.26 pk __KERNEL_RCSID(0, "$NetBSD: qec.c,v 1.26 2002/12/10 13:44:48 pk Exp $");
41 1.1 pk
42 1.1 pk #include <sys/param.h>
43 1.1 pk #include <sys/systm.h>
44 1.1 pk #include <sys/kernel.h>
45 1.1 pk #include <sys/errno.h>
46 1.1 pk #include <sys/device.h>
47 1.1 pk #include <sys/malloc.h>
48 1.1 pk
49 1.2 pk #include <machine/bus.h>
50 1.11 pk #include <machine/intr.h>
51 1.2 pk #include <machine/autoconf.h>
52 1.1 pk
53 1.1 pk #include <dev/sbus/sbusvar.h>
54 1.1 pk #include <dev/sbus/qecreg.h>
55 1.1 pk #include <dev/sbus/qecvar.h>
56 1.1 pk
57 1.1 pk static int qecprint __P((void *, const char *));
58 1.1 pk static int qecmatch __P((struct device *, struct cfdata *, void *));
59 1.1 pk static void qecattach __P((struct device *, struct device *, void *));
60 1.7 pk void qec_init __P((struct qec_softc *));
61 1.1 pk
62 1.1 pk static int qec_bus_map __P((
63 1.1 pk bus_space_tag_t,
64 1.18 pk bus_addr_t, /*coded slot+offset*/
65 1.1 pk bus_size_t, /*size*/
66 1.1 pk int, /*flags*/
67 1.4 pk vaddr_t, /*preferred virtual address */
68 1.1 pk bus_space_handle_t *));
69 1.8 pk static void *qec_intr_establish __P((
70 1.8 pk bus_space_tag_t,
71 1.11 pk int, /*bus interrupt priority*/
72 1.11 pk int, /*`device class' interrupt level*/
73 1.8 pk int (*) __P((void *)), /*handler*/
74 1.25 pk void *, /*arg*/
75 1.25 pk void (*) __P((void)))); /*optional fast trap handler*/
76 1.1 pk
77 1.23 thorpej CFATTACH_DECL(qec, sizeof(struct qec_softc),
78 1.24 thorpej qecmatch, qecattach, NULL, NULL);
79 1.1 pk
80 1.1 pk int
81 1.1 pk qecprint(aux, busname)
82 1.1 pk void *aux;
83 1.1 pk const char *busname;
84 1.1 pk {
85 1.1 pk struct sbus_attach_args *sa = aux;
86 1.1 pk bus_space_tag_t t = sa->sa_bustag;
87 1.1 pk struct qec_softc *sc = t->cookie;
88 1.1 pk
89 1.1 pk sa->sa_bustag = sc->sc_bustag; /* XXX */
90 1.1 pk sbus_print(aux, busname); /* XXX */
91 1.1 pk sa->sa_bustag = t; /* XXX */
92 1.1 pk return (UNCONF);
93 1.1 pk }
94 1.1 pk
95 1.1 pk int
96 1.1 pk qecmatch(parent, cf, aux)
97 1.1 pk struct device *parent;
98 1.1 pk struct cfdata *cf;
99 1.1 pk void *aux;
100 1.1 pk {
101 1.1 pk struct sbus_attach_args *sa = aux;
102 1.1 pk
103 1.21 thorpej return (strcmp(cf->cf_name, sa->sa_name) == 0);
104 1.1 pk }
105 1.1 pk
106 1.1 pk /*
107 1.1 pk * Attach all the sub-devices we can find
108 1.1 pk */
109 1.1 pk void
110 1.1 pk qecattach(parent, self, aux)
111 1.1 pk struct device *parent, *self;
112 1.1 pk void *aux;
113 1.1 pk {
114 1.1 pk struct sbus_attach_args *sa = aux;
115 1.1 pk struct qec_softc *sc = (void *)self;
116 1.1 pk int node;
117 1.1 pk int sbusburst;
118 1.1 pk bus_space_tag_t sbt;
119 1.1 pk bus_space_handle_t bh;
120 1.1 pk int error;
121 1.1 pk
122 1.1 pk sc->sc_bustag = sa->sa_bustag;
123 1.1 pk sc->sc_dmatag = sa->sa_dmatag;
124 1.1 pk node = sa->sa_node;
125 1.1 pk
126 1.6 pk if (sa->sa_nreg < 2) {
127 1.6 pk printf("%s: only %d register sets\n",
128 1.6 pk self->dv_xname, sa->sa_nreg);
129 1.1 pk return;
130 1.1 pk }
131 1.1 pk
132 1.1 pk if (sbus_bus_map(sa->sa_bustag,
133 1.20 thorpej sa->sa_reg[0].oa_space,
134 1.20 thorpej sa->sa_reg[0].oa_base,
135 1.20 thorpej sa->sa_reg[0].oa_size,
136 1.19 eeh 0, &sc->sc_regs) != 0) {
137 1.1 pk printf("%s: attach: cannot map registers\n", self->dv_xname);
138 1.1 pk return;
139 1.1 pk }
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.20 thorpej sa->sa_reg[1].oa_space,
148 1.20 thorpej sa->sa_reg[1].oa_base,
149 1.20 thorpej sa->sa_reg[1].oa_size,
150 1.18 pk BUS_SPACE_MAP_LINEAR, &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.19 eeh sc->sc_buffer = (caddr_t)bus_space_vaddr(sa->sa_bustag, bh);
155 1.20 thorpej sc->sc_bufsiz = (bus_size_t)sa->sa_reg[1].oa_size;
156 1.1 pk
157 1.7 pk /* Get number of on-board channels */
158 1.13 eeh sc->sc_nchannels = PROM_getpropint(node, "#channels", -1);
159 1.7 pk if (sc->sc_nchannels == -1) {
160 1.7 pk printf(": no channels\n");
161 1.7 pk return;
162 1.7 pk }
163 1.7 pk
164 1.1 pk /*
165 1.1 pk * Get transfer burst size from PROM
166 1.1 pk */
167 1.1 pk sbusburst = ((struct sbus_softc *)parent)->sc_burst;
168 1.1 pk if (sbusburst == 0)
169 1.1 pk sbusburst = SBUS_BURST_32 - 1; /* 1->16 */
170 1.1 pk
171 1.13 eeh sc->sc_burst = PROM_getpropint(node, "burst-sizes", -1);
172 1.1 pk if (sc->sc_burst == -1)
173 1.1 pk /* take SBus burst sizes */
174 1.1 pk sc->sc_burst = sbusburst;
175 1.1 pk
176 1.1 pk /* Clamp at parent's burst sizes */
177 1.1 pk sc->sc_burst &= sbusburst;
178 1.1 pk
179 1.1 pk sbus_establish(&sc->sc_sd, &sc->sc_dev);
180 1.1 pk
181 1.1 pk /*
182 1.1 pk * Collect address translations from the OBP.
183 1.1 pk */
184 1.20 thorpej error = PROM_getprop(node, "ranges", sizeof(struct openprom_range),
185 1.1 pk &sc->sc_nrange, (void **)&sc->sc_range);
186 1.1 pk switch (error) {
187 1.1 pk case 0:
188 1.1 pk break;
189 1.1 pk case ENOENT:
190 1.1 pk default:
191 1.1 pk panic("%s: error getting ranges property", self->dv_xname);
192 1.1 pk }
193 1.1 pk
194 1.1 pk /* Allocate a bus tag */
195 1.17 tsutsui sbt = (bus_space_tag_t) malloc(sizeof(struct sparc_bus_space_tag),
196 1.17 tsutsui M_DEVBUF, M_NOWAIT|M_ZERO);
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 sbt->cookie = sc;
203 1.1 pk sbt->parent = sc->sc_bustag;
204 1.1 pk sbt->sparc_bus_map = qec_bus_map;
205 1.8 pk sbt->sparc_intr_establish = qec_intr_establish;
206 1.8 pk
207 1.8 pk /*
208 1.8 pk * Save interrupt information for use in our qec_intr_establish()
209 1.8 pk * function below. Apparently, the intr level for the quad
210 1.16 wiz * ethernet board (qe) is stored in the QEC node rather than
211 1.8 pk * separately in each of the QE nodes.
212 1.8 pk *
213 1.8 pk * XXX - qe.c should call bus_intr_establish() with `level = 0'..
214 1.8 pk * XXX - maybe we should have our own attach args for all that.
215 1.8 pk */
216 1.8 pk sc->sc_intr = sa->sa_intr;
217 1.1 pk
218 1.1 pk printf(": %dK memory\n", sc->sc_bufsiz / 1024);
219 1.1 pk
220 1.7 pk qec_init(sc);
221 1.7 pk
222 1.1 pk /* search through children */
223 1.1 pk for (node = firstchild(node); node; node = nextsibling(node)) {
224 1.1 pk struct sbus_attach_args sa;
225 1.1 pk sbus_setup_attach_args((struct sbus_softc *)parent,
226 1.10 pk sbt, sc->sc_dmatag, node, &sa);
227 1.1 pk (void)config_found(&sc->sc_dev, (void *)&sa, qecprint);
228 1.3 pk sbus_destroy_attach_args(&sa);
229 1.1 pk }
230 1.1 pk }
231 1.1 pk
232 1.1 pk int
233 1.18 pk qec_bus_map(t, baddr, size, flags, va, hp)
234 1.1 pk bus_space_tag_t t;
235 1.18 pk bus_addr_t baddr;
236 1.1 pk bus_size_t size;
237 1.1 pk int flags;
238 1.18 pk vaddr_t va; /* Ignored */
239 1.1 pk bus_space_handle_t *hp;
240 1.1 pk {
241 1.1 pk struct qec_softc *sc = t->cookie;
242 1.18 pk int slot = BUS_ADDR_IOSPACE(baddr);
243 1.1 pk int i;
244 1.1 pk
245 1.1 pk for (i = 0; i < sc->sc_nrange; i++) {
246 1.20 thorpej struct openprom_range *rp = &sc->sc_range[i];
247 1.1 pk
248 1.20 thorpej if (sc->sc_range[i].or_child_space != slot)
249 1.1 pk continue;
250 1.1 pk
251 1.1 pk /* We've found the connection to the parent bus */
252 1.18 pk return (bus_space_map(sc->sc_bustag,
253 1.20 thorpej BUS_ADDR(rp->or_parent_space,
254 1.20 thorpej rp->or_parent_base + BUS_ADDR_PADDR(baddr)),
255 1.20 thorpej size, flags, hp));
256 1.1 pk }
257 1.1 pk
258 1.1 pk return (EINVAL);
259 1.7 pk }
260 1.7 pk
261 1.8 pk void *
262 1.26 pk qec_intr_establish(t, pri, level, handler, arg, fastvec)
263 1.8 pk bus_space_tag_t t;
264 1.11 pk int pri;
265 1.11 pk int level;
266 1.11 pk int (*handler) __P((void *));
267 1.11 pk void *arg;
268 1.25 pk void (*fastvec) __P((void)); /* ignored */
269 1.8 pk {
270 1.8 pk struct qec_softc *sc = t->cookie;
271 1.8 pk
272 1.11 pk if (pri == 0) {
273 1.8 pk /*
274 1.11 pk * qe.c calls bus_intr_establish() with `pri == 0'
275 1.8 pk * XXX - see also comment in qec_attach().
276 1.8 pk */
277 1.8 pk if (sc->sc_intr == NULL) {
278 1.8 pk printf("%s: warning: no interrupts\n",
279 1.8 pk sc->sc_dev.dv_xname);
280 1.8 pk return (NULL);
281 1.8 pk }
282 1.20 thorpej pri = sc->sc_intr->oi_pri;
283 1.8 pk }
284 1.8 pk
285 1.26 pk return (bus_intr_establish(t->parent, pri, level, handler, arg));
286 1.8 pk }
287 1.8 pk
288 1.7 pk void
289 1.7 pk qec_init(sc)
290 1.7 pk struct qec_softc *sc;
291 1.7 pk {
292 1.7 pk bus_space_tag_t t = sc->sc_bustag;
293 1.7 pk bus_space_handle_t qr = sc->sc_regs;
294 1.9 pk u_int32_t v, burst = 0, psize;
295 1.9 pk int i;
296 1.9 pk
297 1.9 pk /* First, reset the controller */
298 1.9 pk bus_space_write_4(t, qr, QEC_QRI_CTRL, QEC_CTRL_RESET);
299 1.9 pk for (i = 0; i < 1000; i++) {
300 1.9 pk DELAY(100);
301 1.9 pk v = bus_space_read_4(t, qr, QEC_QRI_CTRL);
302 1.9 pk if ((v & QEC_CTRL_RESET) == 0)
303 1.9 pk break;
304 1.9 pk }
305 1.7 pk
306 1.7 pk /*
307 1.7 pk * Cut available buffer size into receive and transmit buffers.
308 1.7 pk * XXX - should probably be done in be & qe driver...
309 1.7 pk */
310 1.7 pk v = sc->sc_msize = sc->sc_bufsiz / sc->sc_nchannels;
311 1.7 pk bus_space_write_4(t, qr, QEC_QRI_MSIZE, v);
312 1.7 pk
313 1.7 pk v = sc->sc_rsize = sc->sc_bufsiz / (sc->sc_nchannels * 2);
314 1.7 pk bus_space_write_4(t, qr, QEC_QRI_RSIZE, v);
315 1.7 pk bus_space_write_4(t, qr, QEC_QRI_TSIZE, v);
316 1.7 pk
317 1.9 pk psize = sc->sc_nchannels == 1 ? QEC_PSIZE_2048 : 0;
318 1.9 pk bus_space_write_4(t, qr, QEC_QRI_PSIZE, psize);
319 1.7 pk
320 1.7 pk if (sc->sc_burst & SBUS_BURST_64)
321 1.7 pk burst = QEC_CTRL_B64;
322 1.7 pk else if (sc->sc_burst & SBUS_BURST_32)
323 1.7 pk burst = QEC_CTRL_B32;
324 1.7 pk else
325 1.7 pk burst = QEC_CTRL_B16;
326 1.7 pk
327 1.7 pk v = bus_space_read_4(t, qr, QEC_QRI_CTRL);
328 1.7 pk v = (v & QEC_CTRL_MODEMASK) | burst;
329 1.7 pk bus_space_write_4(t, qr, QEC_QRI_CTRL, v);
330 1.8 pk }
331 1.8 pk
332 1.8 pk /*
333 1.8 pk * Common routine to initialize the QEC packet ring buffer.
334 1.8 pk * Called from be & qe drivers.
335 1.8 pk */
336 1.8 pk void
337 1.8 pk qec_meminit(qr, pktbufsz)
338 1.8 pk struct qec_ring *qr;
339 1.8 pk unsigned int pktbufsz;
340 1.8 pk {
341 1.8 pk bus_addr_t txbufdma, rxbufdma;
342 1.8 pk bus_addr_t dma;
343 1.8 pk caddr_t p;
344 1.8 pk unsigned int ntbuf, nrbuf, i;
345 1.8 pk
346 1.8 pk p = qr->rb_membase;
347 1.8 pk dma = qr->rb_dmabase;
348 1.8 pk
349 1.8 pk ntbuf = qr->rb_ntbuf;
350 1.8 pk nrbuf = qr->rb_nrbuf;
351 1.8 pk
352 1.8 pk /*
353 1.8 pk * Allocate transmit descriptors
354 1.8 pk */
355 1.8 pk qr->rb_txd = (struct qec_xd *)p;
356 1.8 pk qr->rb_txddma = dma;
357 1.8 pk p += QEC_XD_RING_MAXSIZE * sizeof(struct qec_xd);
358 1.8 pk dma += QEC_XD_RING_MAXSIZE * sizeof(struct qec_xd);
359 1.8 pk
360 1.8 pk /*
361 1.8 pk * Allocate receive descriptors
362 1.8 pk */
363 1.8 pk qr->rb_rxd = (struct qec_xd *)p;
364 1.8 pk qr->rb_rxddma = dma;
365 1.8 pk p += QEC_XD_RING_MAXSIZE * sizeof(struct qec_xd);
366 1.8 pk dma += QEC_XD_RING_MAXSIZE * sizeof(struct qec_xd);
367 1.8 pk
368 1.8 pk
369 1.8 pk /*
370 1.8 pk * Allocate transmit buffers
371 1.8 pk */
372 1.8 pk qr->rb_txbuf = p;
373 1.8 pk txbufdma = dma;
374 1.8 pk p += ntbuf * pktbufsz;
375 1.8 pk dma += ntbuf * pktbufsz;
376 1.8 pk
377 1.8 pk /*
378 1.8 pk * Allocate receive buffers
379 1.8 pk */
380 1.8 pk qr->rb_rxbuf = p;
381 1.8 pk rxbufdma = dma;
382 1.8 pk p += nrbuf * pktbufsz;
383 1.8 pk dma += nrbuf * pktbufsz;
384 1.8 pk
385 1.8 pk /*
386 1.8 pk * Initialize transmit buffer descriptors
387 1.8 pk */
388 1.8 pk for (i = 0; i < QEC_XD_RING_MAXSIZE; i++) {
389 1.8 pk qr->rb_txd[i].xd_addr = (u_int32_t)
390 1.8 pk (txbufdma + (i % ntbuf) * pktbufsz);
391 1.8 pk qr->rb_txd[i].xd_flags = 0;
392 1.8 pk }
393 1.8 pk
394 1.8 pk /*
395 1.8 pk * Initialize receive buffer descriptors
396 1.8 pk */
397 1.8 pk for (i = 0; i < QEC_XD_RING_MAXSIZE; i++) {
398 1.8 pk qr->rb_rxd[i].xd_addr = (u_int32_t)
399 1.8 pk (rxbufdma + (i % nrbuf) * pktbufsz);
400 1.8 pk qr->rb_rxd[i].xd_flags = (i < nrbuf)
401 1.8 pk ? QEC_XD_OWN | (pktbufsz & QEC_XD_LENGTH)
402 1.8 pk : 0;
403 1.8 pk }
404 1.8 pk
405 1.8 pk qr->rb_tdhead = qr->rb_tdtail = 0;
406 1.8 pk qr->rb_td_nbusy = 0;
407 1.8 pk qr->rb_rdtail = 0;
408 1.1 pk }
409