qec.c revision 1.9 1 1.9 pk /* $NetBSD: qec.c,v 1.9 1999/06/24 19:56:51 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.7 pk void qec_init __P((struct qec_softc *));
58 1.1 pk
59 1.1 pk static int qec_bus_map __P((
60 1.1 pk bus_space_tag_t,
61 1.1 pk bus_type_t, /*slot*/
62 1.1 pk bus_addr_t, /*offset*/
63 1.1 pk bus_size_t, /*size*/
64 1.1 pk int, /*flags*/
65 1.4 pk vaddr_t, /*preferred virtual address */
66 1.1 pk bus_space_handle_t *));
67 1.8 pk static void *qec_intr_establish __P((
68 1.8 pk bus_space_tag_t,
69 1.8 pk int, /*level*/
70 1.8 pk int, /*flags*/
71 1.8 pk int (*) __P((void *)), /*handler*/
72 1.8 pk void *)); /*arg*/
73 1.1 pk
74 1.1 pk struct cfattach qec_ca = {
75 1.1 pk sizeof(struct qec_softc), qecmatch, qecattach
76 1.1 pk };
77 1.1 pk
78 1.1 pk int
79 1.1 pk qecprint(aux, busname)
80 1.1 pk void *aux;
81 1.1 pk const char *busname;
82 1.1 pk {
83 1.1 pk struct sbus_attach_args *sa = aux;
84 1.1 pk bus_space_tag_t t = sa->sa_bustag;
85 1.1 pk struct qec_softc *sc = t->cookie;
86 1.1 pk
87 1.1 pk sa->sa_bustag = sc->sc_bustag; /* XXX */
88 1.1 pk sbus_print(aux, busname); /* XXX */
89 1.1 pk sa->sa_bustag = t; /* XXX */
90 1.1 pk return (UNCONF);
91 1.1 pk }
92 1.1 pk
93 1.1 pk int
94 1.1 pk qecmatch(parent, cf, aux)
95 1.1 pk struct device *parent;
96 1.1 pk struct cfdata *cf;
97 1.1 pk void *aux;
98 1.1 pk {
99 1.1 pk struct sbus_attach_args *sa = aux;
100 1.1 pk
101 1.1 pk return (strcmp(cf->cf_driver->cd_name, sa->sa_name) == 0);
102 1.1 pk }
103 1.1 pk
104 1.1 pk /*
105 1.1 pk * Attach all the sub-devices we can find
106 1.1 pk */
107 1.1 pk void
108 1.1 pk qecattach(parent, self, aux)
109 1.1 pk struct device *parent, *self;
110 1.1 pk void *aux;
111 1.1 pk {
112 1.1 pk struct sbus_attach_args *sa = aux;
113 1.1 pk struct qec_softc *sc = (void *)self;
114 1.1 pk int node;
115 1.1 pk int sbusburst;
116 1.1 pk bus_space_tag_t sbt;
117 1.1 pk bus_space_handle_t bh;
118 1.1 pk struct bootpath *bp;
119 1.1 pk int error;
120 1.1 pk
121 1.1 pk sc->sc_bustag = sa->sa_bustag;
122 1.1 pk sc->sc_dmatag = sa->sa_dmatag;
123 1.1 pk node = sa->sa_node;
124 1.1 pk
125 1.6 pk if (sa->sa_nreg < 2) {
126 1.6 pk printf("%s: only %d register sets\n",
127 1.6 pk self->dv_xname, sa->sa_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.6 pk sa->sa_reg[0].sbr_slot,
133 1.6 pk sa->sa_reg[0].sbr_offset,
134 1.6 pk sa->sa_reg[0].sbr_size,
135 1.7 pk BUS_SPACE_MAP_LINEAR, 0, &sc->sc_regs) != 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
140 1.1 pk /*
141 1.1 pk * This device's "register space 1" is just a buffer where the
142 1.1 pk * Lance ring-buffers can be stored. Note the buffer's location
143 1.1 pk * and size, so the child driver can pick them up.
144 1.1 pk */
145 1.1 pk if (sbus_bus_map(sa->sa_bustag,
146 1.6 pk sa->sa_reg[1].sbr_slot,
147 1.6 pk sa->sa_reg[1].sbr_offset,
148 1.6 pk sa->sa_reg[1].sbr_size,
149 1.1 pk BUS_SPACE_MAP_LINEAR, 0, &bh) != 0) {
150 1.1 pk printf("%s: attach: cannot map registers\n", self->dv_xname);
151 1.1 pk return;
152 1.1 pk }
153 1.1 pk sc->sc_buffer = (caddr_t)bh;
154 1.6 pk sc->sc_bufsiz = (bus_size_t)sa->sa_reg[1].sbr_size;
155 1.1 pk
156 1.7 pk /* Get number of on-board channels */
157 1.7 pk sc->sc_nchannels = getpropint(node, "#channels", -1);
158 1.7 pk if (sc->sc_nchannels == -1) {
159 1.7 pk printf(": no channels\n");
160 1.7 pk return;
161 1.7 pk }
162 1.7 pk
163 1.1 pk /*
164 1.1 pk * Get transfer burst size from PROM
165 1.1 pk */
166 1.1 pk sbusburst = ((struct sbus_softc *)parent)->sc_burst;
167 1.1 pk if (sbusburst == 0)
168 1.1 pk sbusburst = SBUS_BURST_32 - 1; /* 1->16 */
169 1.1 pk
170 1.1 pk sc->sc_burst = getpropint(node, "burst-sizes", -1);
171 1.1 pk if (sc->sc_burst == -1)
172 1.1 pk /* take SBus burst sizes */
173 1.1 pk sc->sc_burst = sbusburst;
174 1.1 pk
175 1.1 pk /* Clamp at parent's burst sizes */
176 1.1 pk sc->sc_burst &= sbusburst;
177 1.1 pk
178 1.1 pk sbus_establish(&sc->sc_sd, &sc->sc_dev);
179 1.1 pk
180 1.1 pk /*
181 1.1 pk * Collect address translations from the OBP.
182 1.1 pk */
183 1.6 pk error = getprop(node, "ranges", sizeof(struct sbus_range),
184 1.1 pk &sc->sc_nrange, (void **)&sc->sc_range);
185 1.1 pk switch (error) {
186 1.1 pk case 0:
187 1.1 pk break;
188 1.1 pk case ENOENT:
189 1.1 pk default:
190 1.1 pk panic("%s: error getting ranges property", self->dv_xname);
191 1.1 pk }
192 1.1 pk
193 1.1 pk /* Propagate bootpath */
194 1.1 pk if (sa->sa_bp != NULL)
195 1.1 pk bp = sa->sa_bp + 1;
196 1.1 pk else
197 1.1 pk bp = NULL;
198 1.1 pk
199 1.1 pk /* Allocate a bus tag */
200 1.1 pk sbt = (bus_space_tag_t)
201 1.1 pk malloc(sizeof(struct sparc_bus_space_tag), M_DEVBUF, M_NOWAIT);
202 1.1 pk if (sbt == NULL) {
203 1.1 pk printf("%s: attach: out of memory\n", self->dv_xname);
204 1.1 pk return;
205 1.1 pk }
206 1.1 pk
207 1.1 pk bzero(sbt, sizeof *sbt);
208 1.1 pk sbt->cookie = sc;
209 1.1 pk sbt->parent = sc->sc_bustag;
210 1.1 pk sbt->sparc_bus_map = qec_bus_map;
211 1.8 pk sbt->sparc_intr_establish = qec_intr_establish;
212 1.8 pk
213 1.8 pk /*
214 1.8 pk * Save interrupt information for use in our qec_intr_establish()
215 1.8 pk * function below. Apparently, the intr level for the quad
216 1.8 pk * ethernet board (qe) is stored in the QEC node rather then
217 1.8 pk * separately in each of the QE nodes.
218 1.8 pk *
219 1.8 pk * XXX - qe.c should call bus_intr_establish() with `level = 0'..
220 1.8 pk * XXX - maybe we should have our own attach args for all that.
221 1.8 pk */
222 1.8 pk sc->sc_intr = sa->sa_intr;
223 1.1 pk
224 1.1 pk printf(": %dK memory\n", sc->sc_bufsiz / 1024);
225 1.1 pk
226 1.7 pk qec_init(sc);
227 1.7 pk
228 1.1 pk /* search through children */
229 1.1 pk for (node = firstchild(node); node; node = nextsibling(node)) {
230 1.1 pk struct sbus_attach_args sa;
231 1.1 pk sbus_setup_attach_args((struct sbus_softc *)parent,
232 1.1 pk sbt, sc->sc_dmatag, node, bp, &sa);
233 1.1 pk (void)config_found(&sc->sc_dev, (void *)&sa, qecprint);
234 1.3 pk sbus_destroy_attach_args(&sa);
235 1.1 pk }
236 1.1 pk }
237 1.1 pk
238 1.1 pk int
239 1.1 pk qec_bus_map(t, btype, offset, size, flags, vaddr, hp)
240 1.1 pk bus_space_tag_t t;
241 1.1 pk bus_type_t btype;
242 1.1 pk bus_addr_t offset;
243 1.1 pk bus_size_t size;
244 1.1 pk int flags;
245 1.4 pk vaddr_t vaddr;
246 1.1 pk bus_space_handle_t *hp;
247 1.1 pk {
248 1.1 pk struct qec_softc *sc = t->cookie;
249 1.1 pk int slot = btype;
250 1.1 pk int i;
251 1.1 pk
252 1.1 pk for (i = 0; i < sc->sc_nrange; i++) {
253 1.1 pk bus_addr_t paddr;
254 1.1 pk bus_type_t iospace;
255 1.1 pk
256 1.1 pk if (sc->sc_range[i].cspace != slot)
257 1.1 pk continue;
258 1.1 pk
259 1.1 pk /* We've found the connection to the parent bus */
260 1.1 pk paddr = sc->sc_range[i].poffset + offset;
261 1.1 pk iospace = sc->sc_range[i].pspace;
262 1.1 pk return (bus_space_map2(sc->sc_bustag, iospace, paddr,
263 1.1 pk size, flags, vaddr, hp));
264 1.1 pk }
265 1.1 pk
266 1.1 pk return (EINVAL);
267 1.7 pk }
268 1.7 pk
269 1.8 pk void *
270 1.8 pk qec_intr_establish(t, level, flags, handler, arg)
271 1.8 pk bus_space_tag_t t;
272 1.8 pk int level;
273 1.8 pk int flags;
274 1.8 pk int (*handler) __P((void *));
275 1.8 pk void *arg;
276 1.8 pk {
277 1.8 pk struct qec_softc *sc = t->cookie;
278 1.8 pk
279 1.8 pk if (level == 0) {
280 1.8 pk /*
281 1.8 pk * qe.c calls bus_intr_establish() with `level = 0'
282 1.8 pk * XXX - see also comment in qec_attach().
283 1.8 pk */
284 1.8 pk if (sc->sc_intr == NULL) {
285 1.8 pk printf("%s: warning: no interrupts\n",
286 1.8 pk sc->sc_dev.dv_xname);
287 1.8 pk return (NULL);
288 1.8 pk }
289 1.8 pk level = sc->sc_intr->sbi_pri;
290 1.8 pk }
291 1.8 pk
292 1.8 pk return (bus_intr_establish(t->parent, level, flags, handler, arg));
293 1.8 pk }
294 1.8 pk
295 1.7 pk void
296 1.7 pk qec_init(sc)
297 1.7 pk struct qec_softc *sc;
298 1.7 pk {
299 1.7 pk bus_space_tag_t t = sc->sc_bustag;
300 1.7 pk bus_space_handle_t qr = sc->sc_regs;
301 1.9 pk u_int32_t v, burst = 0, psize;
302 1.9 pk int i;
303 1.9 pk
304 1.9 pk /* First, reset the controller */
305 1.9 pk bus_space_write_4(t, qr, QEC_QRI_CTRL, QEC_CTRL_RESET);
306 1.9 pk for (i = 0; i < 1000; i++) {
307 1.9 pk DELAY(100);
308 1.9 pk v = bus_space_read_4(t, qr, QEC_QRI_CTRL);
309 1.9 pk if ((v & QEC_CTRL_RESET) == 0)
310 1.9 pk break;
311 1.9 pk }
312 1.7 pk
313 1.7 pk /*
314 1.7 pk * Cut available buffer size into receive and transmit buffers.
315 1.7 pk * XXX - should probably be done in be & qe driver...
316 1.7 pk */
317 1.7 pk v = sc->sc_msize = sc->sc_bufsiz / sc->sc_nchannels;
318 1.7 pk bus_space_write_4(t, qr, QEC_QRI_MSIZE, v);
319 1.7 pk
320 1.7 pk v = sc->sc_rsize = sc->sc_bufsiz / (sc->sc_nchannels * 2);
321 1.7 pk bus_space_write_4(t, qr, QEC_QRI_RSIZE, v);
322 1.7 pk bus_space_write_4(t, qr, QEC_QRI_TSIZE, v);
323 1.7 pk
324 1.9 pk psize = sc->sc_nchannels == 1 ? QEC_PSIZE_2048 : 0;
325 1.9 pk bus_space_write_4(t, qr, QEC_QRI_PSIZE, psize);
326 1.7 pk
327 1.7 pk if (sc->sc_burst & SBUS_BURST_64)
328 1.7 pk burst = QEC_CTRL_B64;
329 1.7 pk else if (sc->sc_burst & SBUS_BURST_32)
330 1.7 pk burst = QEC_CTRL_B32;
331 1.7 pk else
332 1.7 pk burst = QEC_CTRL_B16;
333 1.7 pk
334 1.7 pk v = bus_space_read_4(t, qr, QEC_QRI_CTRL);
335 1.7 pk v = (v & QEC_CTRL_MODEMASK) | burst;
336 1.7 pk bus_space_write_4(t, qr, QEC_QRI_CTRL, v);
337 1.8 pk }
338 1.8 pk
339 1.8 pk /*
340 1.8 pk * Common routine to initialize the QEC packet ring buffer.
341 1.8 pk * Called from be & qe drivers.
342 1.8 pk */
343 1.8 pk void
344 1.8 pk qec_meminit(qr, pktbufsz)
345 1.8 pk struct qec_ring *qr;
346 1.8 pk unsigned int pktbufsz;
347 1.8 pk {
348 1.8 pk bus_addr_t txbufdma, rxbufdma;
349 1.8 pk bus_addr_t dma;
350 1.8 pk caddr_t p;
351 1.8 pk unsigned int ntbuf, nrbuf, i;
352 1.8 pk
353 1.8 pk p = qr->rb_membase;
354 1.8 pk dma = qr->rb_dmabase;
355 1.8 pk
356 1.8 pk ntbuf = qr->rb_ntbuf;
357 1.8 pk nrbuf = qr->rb_nrbuf;
358 1.8 pk
359 1.8 pk /*
360 1.8 pk * Allocate transmit descriptors
361 1.8 pk */
362 1.8 pk qr->rb_txd = (struct qec_xd *)p;
363 1.8 pk qr->rb_txddma = dma;
364 1.8 pk p += QEC_XD_RING_MAXSIZE * sizeof(struct qec_xd);
365 1.8 pk dma += QEC_XD_RING_MAXSIZE * sizeof(struct qec_xd);
366 1.8 pk
367 1.8 pk /*
368 1.8 pk * Allocate receive descriptors
369 1.8 pk */
370 1.8 pk qr->rb_rxd = (struct qec_xd *)p;
371 1.8 pk qr->rb_rxddma = dma;
372 1.8 pk p += QEC_XD_RING_MAXSIZE * sizeof(struct qec_xd);
373 1.8 pk dma += QEC_XD_RING_MAXSIZE * sizeof(struct qec_xd);
374 1.8 pk
375 1.8 pk
376 1.8 pk /*
377 1.8 pk * Allocate transmit buffers
378 1.8 pk */
379 1.8 pk qr->rb_txbuf = p;
380 1.8 pk txbufdma = dma;
381 1.8 pk p += ntbuf * pktbufsz;
382 1.8 pk dma += ntbuf * pktbufsz;
383 1.8 pk
384 1.8 pk /*
385 1.8 pk * Allocate receive buffers
386 1.8 pk */
387 1.8 pk qr->rb_rxbuf = p;
388 1.8 pk rxbufdma = dma;
389 1.8 pk p += nrbuf * pktbufsz;
390 1.8 pk dma += nrbuf * pktbufsz;
391 1.8 pk
392 1.8 pk /*
393 1.8 pk * Initialize transmit buffer descriptors
394 1.8 pk */
395 1.8 pk for (i = 0; i < QEC_XD_RING_MAXSIZE; i++) {
396 1.8 pk qr->rb_txd[i].xd_addr = (u_int32_t)
397 1.8 pk (txbufdma + (i % ntbuf) * pktbufsz);
398 1.8 pk qr->rb_txd[i].xd_flags = 0;
399 1.8 pk }
400 1.8 pk
401 1.8 pk /*
402 1.8 pk * Initialize receive buffer descriptors
403 1.8 pk */
404 1.8 pk for (i = 0; i < QEC_XD_RING_MAXSIZE; i++) {
405 1.8 pk qr->rb_rxd[i].xd_addr = (u_int32_t)
406 1.8 pk (rxbufdma + (i % nrbuf) * pktbufsz);
407 1.8 pk qr->rb_rxd[i].xd_flags = (i < nrbuf)
408 1.8 pk ? QEC_XD_OWN | (pktbufsz & QEC_XD_LENGTH)
409 1.8 pk : 0;
410 1.8 pk }
411 1.8 pk
412 1.8 pk qr->rb_tdhead = qr->rb_tdtail = 0;
413 1.8 pk qr->rb_td_nbusy = 0;
414 1.8 pk qr->rb_rdtail = 0;
415 1.1 pk }
416