qec.c revision 1.7 1 /* $NetBSD: qec.c,v 1.7 1999/01/16 12:46:08 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 void qec_init __P((struct qec_softc *));
58
59 static int qec_bus_map __P((
60 bus_space_tag_t,
61 bus_type_t, /*slot*/
62 bus_addr_t, /*offset*/
63 bus_size_t, /*size*/
64 int, /*flags*/
65 vaddr_t, /*preferred virtual address */
66 bus_space_handle_t *));
67
68 struct cfattach qec_ca = {
69 sizeof(struct qec_softc), qecmatch, qecattach
70 };
71
72 int
73 qecprint(aux, busname)
74 void *aux;
75 const char *busname;
76 {
77 struct sbus_attach_args *sa = aux;
78 bus_space_tag_t t = sa->sa_bustag;
79 struct qec_softc *sc = t->cookie;
80
81 sa->sa_bustag = sc->sc_bustag; /* XXX */
82 sbus_print(aux, busname); /* XXX */
83 sa->sa_bustag = t; /* XXX */
84 return (UNCONF);
85 }
86
87 int
88 qecmatch(parent, cf, aux)
89 struct device *parent;
90 struct cfdata *cf;
91 void *aux;
92 {
93 struct sbus_attach_args *sa = aux;
94
95 return (strcmp(cf->cf_driver->cd_name, sa->sa_name) == 0);
96 }
97
98 /*
99 * Attach all the sub-devices we can find
100 */
101 void
102 qecattach(parent, self, aux)
103 struct device *parent, *self;
104 void *aux;
105 {
106 struct sbus_attach_args *sa = aux;
107 struct qec_softc *sc = (void *)self;
108 int node;
109 int sbusburst;
110 bus_space_tag_t sbt;
111 bus_space_handle_t bh;
112 struct bootpath *bp;
113 int error;
114
115 sc->sc_bustag = sa->sa_bustag;
116 sc->sc_dmatag = sa->sa_dmatag;
117 node = sa->sa_node;
118
119 if (sa->sa_nreg < 2) {
120 printf("%s: only %d register sets\n",
121 self->dv_xname, sa->sa_nreg);
122 return;
123 }
124
125 if (sbus_bus_map(sa->sa_bustag,
126 sa->sa_reg[0].sbr_slot,
127 sa->sa_reg[0].sbr_offset,
128 sa->sa_reg[0].sbr_size,
129 BUS_SPACE_MAP_LINEAR, 0, &sc->sc_regs) != 0) {
130 printf("%s: attach: cannot map registers\n", self->dv_xname);
131 return;
132 }
133
134 /*
135 * This device's "register space 1" is just a buffer where the
136 * Lance ring-buffers can be stored. Note the buffer's location
137 * and size, so the child driver can pick them up.
138 */
139 if (sbus_bus_map(sa->sa_bustag,
140 sa->sa_reg[1].sbr_slot,
141 sa->sa_reg[1].sbr_offset,
142 sa->sa_reg[1].sbr_size,
143 BUS_SPACE_MAP_LINEAR, 0, &bh) != 0) {
144 printf("%s: attach: cannot map registers\n", self->dv_xname);
145 return;
146 }
147 sc->sc_buffer = (caddr_t)bh;
148 sc->sc_bufsiz = (bus_size_t)sa->sa_reg[1].sbr_size;
149
150 /* Get number of on-board channels */
151 sc->sc_nchannels = getpropint(node, "#channels", -1);
152 if (sc->sc_nchannels == -1) {
153 printf(": no channels\n");
154 return;
155 }
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 * Collect address translations from the OBP.
176 */
177 error = getprop(node, "ranges", sizeof(struct sbus_range),
178 &sc->sc_nrange, (void **)&sc->sc_range);
179 switch (error) {
180 case 0:
181 break;
182 case ENOENT:
183 default:
184 panic("%s: error getting ranges property", self->dv_xname);
185 }
186
187 /* Propagate bootpath */
188 if (sa->sa_bp != NULL)
189 bp = sa->sa_bp + 1;
190 else
191 bp = NULL;
192
193 /* Allocate a bus tag */
194 sbt = (bus_space_tag_t)
195 malloc(sizeof(struct sparc_bus_space_tag), M_DEVBUF, M_NOWAIT);
196 if (sbt == NULL) {
197 printf("%s: attach: out of memory\n", self->dv_xname);
198 return;
199 }
200
201 bzero(sbt, sizeof *sbt);
202 sbt->cookie = sc;
203 sbt->parent = sc->sc_bustag;
204 sbt->sparc_bus_map = qec_bus_map;
205
206 printf(": %dK memory\n", sc->sc_bufsiz / 1024);
207
208 qec_init(sc);
209
210 /* search through children */
211 for (node = firstchild(node); node; node = nextsibling(node)) {
212 struct sbus_attach_args sa;
213 sbus_setup_attach_args((struct sbus_softc *)parent,
214 sbt, sc->sc_dmatag, node, bp, &sa);
215 (void)config_found(&sc->sc_dev, (void *)&sa, qecprint);
216 sbus_destroy_attach_args(&sa);
217 }
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
251 void
252 qec_init(sc)
253 struct qec_softc *sc;
254 {
255 bus_space_tag_t t = sc->sc_bustag;
256 bus_space_handle_t qr = sc->sc_regs;
257 u_int32_t v, burst = 0;
258
259 /*
260 * Cut available buffer size into receive and transmit buffers.
261 * XXX - should probably be done in be & qe driver...
262 */
263 v = sc->sc_msize = sc->sc_bufsiz / sc->sc_nchannels;
264 bus_space_write_4(t, qr, QEC_QRI_MSIZE, v);
265
266 v = sc->sc_rsize = sc->sc_bufsiz / (sc->sc_nchannels * 2);
267 bus_space_write_4(t, qr, QEC_QRI_RSIZE, v);
268 bus_space_write_4(t, qr, QEC_QRI_TSIZE, v);
269
270 bus_space_write_4(t, qr, QEC_QRI_PSIZE, QEC_PSIZE_2048);
271
272 if (sc->sc_burst & SBUS_BURST_64)
273 burst = QEC_CTRL_B64;
274 else if (sc->sc_burst & SBUS_BURST_32)
275 burst = QEC_CTRL_B32;
276 else
277 burst = QEC_CTRL_B16;
278
279 v = bus_space_read_4(t, qr, QEC_QRI_CTRL);
280 v = (v & QEC_CTRL_MODEMASK) | burst;
281 bus_space_write_4(t, qr, QEC_QRI_CTRL, v);
282 }
283