depca.c revision 1.4 1 1.4 lukem /* $NetBSD: depca.c,v 1.4 2001/11/13 13:14:36 lukem Exp $ */
2 1.1 thorpej
3 1.1 thorpej /*-
4 1.1 thorpej * Copyright (c) 1997, 1998, 2000 The NetBSD Foundation, Inc.
5 1.1 thorpej * All rights reserved.
6 1.1 thorpej *
7 1.1 thorpej * This code is derived from software contributed to The NetBSD Foundation
8 1.1 thorpej * by Charles M. Hannum and by Jason R. Thorpe of the Numerical Aerospace
9 1.1 thorpej * Simulation Facility, NASA Ames Research Center.
10 1.1 thorpej *
11 1.1 thorpej * Redistribution and use in source and binary forms, with or without
12 1.1 thorpej * modification, are permitted provided that the following conditions
13 1.1 thorpej * are met:
14 1.1 thorpej * 1. Redistributions of source code must retain the above copyright
15 1.1 thorpej * notice, this list of conditions and the following disclaimer.
16 1.1 thorpej * 2. Redistributions in binary form must reproduce the above copyright
17 1.1 thorpej * notice, this list of conditions and the following disclaimer in the
18 1.1 thorpej * documentation and/or other materials provided with the distribution.
19 1.1 thorpej * 3. All advertising materials mentioning features or use of this software
20 1.1 thorpej * must display the following acknowledgement:
21 1.1 thorpej * This product includes software developed by the NetBSD
22 1.1 thorpej * Foundation, Inc. and its contributors.
23 1.1 thorpej * 4. Neither the name of The NetBSD Foundation nor the names of its
24 1.1 thorpej * contributors may be used to endorse or promote products derived
25 1.1 thorpej * from this software without specific prior written permission.
26 1.1 thorpej *
27 1.1 thorpej * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28 1.1 thorpej * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29 1.1 thorpej * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 1.1 thorpej * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31 1.1 thorpej * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 1.1 thorpej * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 1.1 thorpej * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 1.1 thorpej * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 1.1 thorpej * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 1.1 thorpej * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 1.1 thorpej * POSSIBILITY OF SUCH DAMAGE.
38 1.1 thorpej */
39 1.1 thorpej
40 1.1 thorpej /*-
41 1.1 thorpej * Copyright (c) 1992, 1993
42 1.1 thorpej * The Regents of the University of California. All rights reserved.
43 1.1 thorpej *
44 1.1 thorpej * This code is derived from software contributed to Berkeley by
45 1.1 thorpej * Ralph Campbell and Rick Macklem.
46 1.1 thorpej *
47 1.1 thorpej * Redistribution and use in source and binary forms, with or without
48 1.1 thorpej * modification, are permitted provided that the following conditions
49 1.1 thorpej * are met:
50 1.1 thorpej * 1. Redistributions of source code must retain the above copyright
51 1.1 thorpej * notice, this list of conditions and the following disclaimer.
52 1.1 thorpej * 2. Redistributions in binary form must reproduce the above copyright
53 1.1 thorpej * notice, this list of conditions and the following disclaimer in the
54 1.1 thorpej * documentation and/or other materials provided with the distribution.
55 1.1 thorpej * 3. All advertising materials mentioning features or use of this software
56 1.1 thorpej * must display the following acknowledgement:
57 1.1 thorpej * This product includes software developed by the University of
58 1.1 thorpej * California, Berkeley and its contributors.
59 1.1 thorpej * 4. Neither the name of the University nor the names of its contributors
60 1.1 thorpej * may be used to endorse or promote products derived from this software
61 1.1 thorpej * without specific prior written permission.
62 1.1 thorpej *
63 1.1 thorpej * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
64 1.1 thorpej * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
65 1.1 thorpej * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
66 1.1 thorpej * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
67 1.1 thorpej * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
68 1.1 thorpej * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
69 1.1 thorpej * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
70 1.1 thorpej * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
71 1.1 thorpej * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
72 1.1 thorpej * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
73 1.1 thorpej * SUCH DAMAGE.
74 1.1 thorpej *
75 1.1 thorpej * @(#)if_le.c 8.2 (Berkeley) 11/16/93
76 1.1 thorpej */
77 1.4 lukem
78 1.4 lukem #include <sys/cdefs.h>
79 1.4 lukem __KERNEL_RCSID(0, "$NetBSD: depca.c,v 1.4 2001/11/13 13:14:36 lukem Exp $");
80 1.1 thorpej
81 1.1 thorpej #include <sys/param.h>
82 1.1 thorpej #include <sys/systm.h>
83 1.1 thorpej #include <sys/mbuf.h>
84 1.1 thorpej #include <sys/syslog.h>
85 1.1 thorpej #include <sys/socket.h>
86 1.1 thorpej #include <sys/device.h>
87 1.1 thorpej
88 1.1 thorpej #include <net/if.h>
89 1.1 thorpej #include <net/if_ether.h>
90 1.1 thorpej #include <net/if_media.h>
91 1.1 thorpej
92 1.1 thorpej #include <machine/cpu.h>
93 1.1 thorpej #include <machine/intr.h>
94 1.1 thorpej #include <machine/bus.h>
95 1.1 thorpej
96 1.1 thorpej #include <dev/isa/isareg.h>
97 1.1 thorpej #include <dev/isa/isavar.h>
98 1.1 thorpej
99 1.1 thorpej #include <dev/ic/lancereg.h>
100 1.1 thorpej #include <dev/ic/lancevar.h>
101 1.1 thorpej #include <dev/ic/am7990reg.h>
102 1.1 thorpej #include <dev/ic/am7990var.h>
103 1.1 thorpej #include <dev/ic/depcareg.h>
104 1.1 thorpej #include <dev/ic/depcavar.h>
105 1.1 thorpej
106 1.1 thorpej struct le_depca_softc {
107 1.1 thorpej struct am7990_softc sc_am7990; /* glue to MI code */
108 1.1 thorpej
109 1.1 thorpej void *sc_ih;
110 1.1 thorpej };
111 1.1 thorpej
112 1.1 thorpej int le_depca_match(struct device *, struct cfdata *, void *);
113 1.1 thorpej void le_depca_attach(struct device *, struct device *, void *);
114 1.1 thorpej
115 1.1 thorpej struct cfattach le_depca_ca = {
116 1.1 thorpej sizeof(struct le_depca_softc), le_depca_match, le_depca_attach
117 1.1 thorpej };
118 1.1 thorpej
119 1.1 thorpej void depca_copytobuf(struct lance_softc *, void *, int, int);
120 1.1 thorpej void depca_copyfrombuf(struct lance_softc *, void *, int, int);
121 1.1 thorpej void depca_zerobuf(struct lance_softc *, int, int);
122 1.1 thorpej
123 1.1 thorpej struct depca_attach_args {
124 1.1 thorpej const char *da_name;
125 1.1 thorpej };
126 1.1 thorpej
127 1.1 thorpej int depca_print(void *, const char *);
128 1.1 thorpej
129 1.1 thorpej void
130 1.1 thorpej depca_attach(struct depca_softc *sc)
131 1.1 thorpej {
132 1.1 thorpej struct depca_attach_args da;
133 1.1 thorpej
134 1.1 thorpej da.da_name = "le";
135 1.1 thorpej
136 1.1 thorpej (void) config_found(&sc->sc_dev, &da, depca_print);
137 1.1 thorpej }
138 1.1 thorpej
139 1.1 thorpej int
140 1.1 thorpej depca_print(void *aux, const char *pnp)
141 1.1 thorpej {
142 1.1 thorpej struct depca_attach_args *da = aux;
143 1.1 thorpej
144 1.1 thorpej if (pnp)
145 1.1 thorpej printf("%s at %s", da->da_name, pnp);
146 1.1 thorpej
147 1.1 thorpej return (UNCONF);
148 1.1 thorpej }
149 1.1 thorpej
150 1.1 thorpej void
151 1.1 thorpej depca_wrcsr(struct lance_softc *sc, u_int16_t port, u_int16_t val)
152 1.1 thorpej {
153 1.1 thorpej struct depca_softc *dsc = (void *) sc->sc_dev.dv_parent;
154 1.1 thorpej
155 1.1 thorpej bus_space_write_2(dsc->sc_iot, dsc->sc_ioh, DEPCA_RAP, port);
156 1.1 thorpej bus_space_write_2(dsc->sc_iot, dsc->sc_ioh, DEPCA_RDP, val);
157 1.1 thorpej }
158 1.1 thorpej
159 1.1 thorpej u_int16_t
160 1.1 thorpej depca_rdcsr(struct lance_softc *sc, u_int16_t port)
161 1.1 thorpej {
162 1.1 thorpej struct depca_softc *dsc = (void *) sc->sc_dev.dv_parent;
163 1.1 thorpej
164 1.1 thorpej bus_space_write_2(dsc->sc_iot, dsc->sc_ioh, DEPCA_RAP, port);
165 1.1 thorpej return (bus_space_read_2(dsc->sc_iot, dsc->sc_ioh, DEPCA_RDP));
166 1.1 thorpej }
167 1.1 thorpej
168 1.1 thorpej int
169 1.1 thorpej depca_readprom(bus_space_tag_t iot, bus_space_handle_t ioh, u_int8_t *laddr)
170 1.1 thorpej {
171 1.1 thorpej int port, i;
172 1.1 thorpej
173 1.1 thorpej /*
174 1.1 thorpej * Extract the physical MAC address from the ROM.
175 1.1 thorpej *
176 1.1 thorpej * The address PROM is 32 bytes wide, and we access it through
177 1.1 thorpej * a single I/O port. On each read, it rotates to the next
178 1.1 thorpej * position. We find the ethernet address by looking for a
179 1.1 thorpej * particular sequence of bytes (0xff, 0x00, 0x55, 0xaa, 0xff,
180 1.1 thorpej * 0x00, 0x55, 0xaa), and then reading the next 8 bytes (the
181 1.1 thorpej * ethernet address and a checksum).
182 1.1 thorpej *
183 1.1 thorpej * It appears that the PROM can be at one of two locations, so
184 1.1 thorpej * we just try both.
185 1.1 thorpej */
186 1.1 thorpej port = DEPCA_ADP;
187 1.1 thorpej for (i = 0; i < 32; i++)
188 1.1 thorpej if (bus_space_read_1(iot, ioh, port) == 0xff &&
189 1.1 thorpej bus_space_read_1(iot, ioh, port) == 0x00 &&
190 1.1 thorpej bus_space_read_1(iot, ioh, port) == 0x55 &&
191 1.1 thorpej bus_space_read_1(iot, ioh, port) == 0xaa &&
192 1.1 thorpej bus_space_read_1(iot, ioh, port) == 0xff &&
193 1.1 thorpej bus_space_read_1(iot, ioh, port) == 0x00 &&
194 1.1 thorpej bus_space_read_1(iot, ioh, port) == 0x55 &&
195 1.1 thorpej bus_space_read_1(iot, ioh, port) == 0xaa)
196 1.1 thorpej goto found;
197 1.1 thorpej port = DEPCA_ADP + 1;
198 1.1 thorpej for (i = 0; i < 32; i++)
199 1.1 thorpej if (bus_space_read_1(iot, ioh, port) == 0xff &&
200 1.1 thorpej bus_space_read_1(iot, ioh, port) == 0x00 &&
201 1.1 thorpej bus_space_read_1(iot, ioh, port) == 0x55 &&
202 1.1 thorpej bus_space_read_1(iot, ioh, port) == 0xaa &&
203 1.1 thorpej bus_space_read_1(iot, ioh, port) == 0xff &&
204 1.1 thorpej bus_space_read_1(iot, ioh, port) == 0x00 &&
205 1.1 thorpej bus_space_read_1(iot, ioh, port) == 0x55 &&
206 1.1 thorpej bus_space_read_1(iot, ioh, port) == 0xaa)
207 1.1 thorpej goto found;
208 1.1 thorpej printf("depca: address not found\n");
209 1.1 thorpej return (-1);
210 1.1 thorpej
211 1.1 thorpej found:
212 1.1 thorpej
213 1.1 thorpej if (laddr) {
214 1.1 thorpej for (i = 0; i < 6; i++)
215 1.1 thorpej laddr[i] = bus_space_read_1(iot, ioh, port);
216 1.1 thorpej }
217 1.1 thorpej
218 1.1 thorpej #if 0
219 1.1 thorpej sum =
220 1.1 thorpej (laddr[0] << 2) +
221 1.1 thorpej (laddr[1] << 10) +
222 1.1 thorpej (laddr[2] << 1) +
223 1.1 thorpej (laddr[3] << 9) +
224 1.1 thorpej (laddr[4] << 0) +
225 1.1 thorpej (laddr[5] << 8);
226 1.1 thorpej sum = (sum & 0xffff) + (sum >> 16);
227 1.1 thorpej sum = (sum & 0xffff) + (sum >> 16);
228 1.1 thorpej
229 1.1 thorpej rom_sum = bus_space_read_1(iot, ioh, port);
230 1.1 thorpej rom_sum |= bus_space_read_1(iot, ioh, port) << 8;
231 1.1 thorpej
232 1.1 thorpej if (sum != rom_sum) {
233 1.1 thorpej printf("depca: checksum mismatch; calculated %04x != read %04x",
234 1.1 thorpej sum, rom_sum);
235 1.1 thorpej return (-1);
236 1.1 thorpej }
237 1.1 thorpej #endif
238 1.1 thorpej
239 1.1 thorpej return (0);
240 1.1 thorpej }
241 1.1 thorpej
242 1.1 thorpej int
243 1.1 thorpej le_depca_match(struct device *parent, struct cfdata *match, void *aux)
244 1.1 thorpej {
245 1.1 thorpej struct depca_attach_args *da = aux;
246 1.1 thorpej
247 1.1 thorpej return (strcmp(da->da_name, match->cf_driver->cd_name) == 0);
248 1.1 thorpej }
249 1.1 thorpej
250 1.1 thorpej void
251 1.1 thorpej le_depca_attach(struct device *parent, struct device *self, void *aux)
252 1.1 thorpej {
253 1.1 thorpej struct depca_softc *dsc = (void *) parent;
254 1.1 thorpej struct le_depca_softc *lesc = (void *) self;
255 1.1 thorpej struct lance_softc *sc = &lesc->sc_am7990.lsc;
256 1.1 thorpej u_int8_t val;
257 1.1 thorpej int i;
258 1.1 thorpej
259 1.1 thorpej printf("\n");
260 1.1 thorpej
261 1.1 thorpej /* I/O and memory spaces already mapped. */
262 1.1 thorpej
263 1.1 thorpej if (depca_readprom(dsc->sc_iot, dsc->sc_ioh, sc->sc_enaddr)) {
264 1.1 thorpej printf("%s: can't read PROM\n", self->dv_xname);
265 1.1 thorpej return;
266 1.1 thorpej }
267 1.1 thorpej
268 1.1 thorpej bus_space_write_2(dsc->sc_iot, dsc->sc_ioh, DEPCA_CSR,
269 1.1 thorpej DEPCA_CSR_DUM | DEPCA_CSR_IEN | DEPCA_CSR_SHE |
270 1.1 thorpej (dsc->sc_memsize == 32*1024 ? DEPCA_CSR_LOW32K : 0));
271 1.1 thorpej
272 1.1 thorpej val = 0xff;
273 1.1 thorpej for (;;) {
274 1.1 thorpej u_int8_t cv;
275 1.1 thorpej
276 1.1 thorpej bus_space_set_region_1(dsc->sc_memt, dsc->sc_memh, 0, val,
277 1.1 thorpej dsc->sc_memsize);
278 1.1 thorpej for (i = 0; i < dsc->sc_memsize; i++) {
279 1.1 thorpej cv = bus_space_read_1(dsc->sc_memt, dsc->sc_memh, i);
280 1.1 thorpej if (cv != val) {
281 1.1 thorpej printf("%s: failed to clear memory at %d "
282 1.1 thorpej "(0x%02x != 0x%02x)\n",
283 1.1 thorpej sc->sc_dev.dv_xname, i, cv, val);
284 1.1 thorpej return;
285 1.1 thorpej }
286 1.1 thorpej }
287 1.1 thorpej if (val == 0x00)
288 1.1 thorpej break;
289 1.1 thorpej val -= 0x55;
290 1.1 thorpej }
291 1.1 thorpej
292 1.1 thorpej sc->sc_conf3 = LE_C3_ACON;
293 1.1 thorpej sc->sc_mem = 0; /* Not used. */
294 1.1 thorpej sc->sc_addr = 0;
295 1.1 thorpej sc->sc_memsize = dsc->sc_memsize;
296 1.1 thorpej
297 1.1 thorpej sc->sc_copytodesc = depca_copytobuf;
298 1.1 thorpej sc->sc_copyfromdesc = depca_copyfrombuf;
299 1.1 thorpej sc->sc_copytobuf = depca_copytobuf;
300 1.1 thorpej sc->sc_copyfrombuf = depca_copyfrombuf;
301 1.1 thorpej sc->sc_zerobuf = depca_zerobuf;
302 1.1 thorpej
303 1.1 thorpej sc->sc_rdcsr = depca_rdcsr;
304 1.1 thorpej sc->sc_wrcsr = depca_wrcsr;
305 1.1 thorpej sc->sc_hwinit = NULL;
306 1.1 thorpej
307 1.1 thorpej printf("%s", sc->sc_dev.dv_xname);
308 1.1 thorpej am7990_config(&lesc->sc_am7990);
309 1.1 thorpej
310 1.1 thorpej lesc->sc_ih = (*dsc->sc_intr_establish)(dsc, sc);
311 1.1 thorpej }
312 1.1 thorpej
313 1.1 thorpej /*
314 1.1 thorpej * Controller interrupt.
315 1.1 thorpej */
316 1.1 thorpej int
317 1.1 thorpej depca_intredge(void *arg)
318 1.1 thorpej {
319 1.1 thorpej
320 1.1 thorpej if (am7990_intr(arg) == 0)
321 1.1 thorpej return (0);
322 1.1 thorpej for (;;)
323 1.1 thorpej if (am7990_intr(arg) == 0)
324 1.1 thorpej return (1);
325 1.1 thorpej }
326 1.1 thorpej
327 1.1 thorpej /*
328 1.1 thorpej * DEPCA shared memory access functions.
329 1.1 thorpej */
330 1.1 thorpej
331 1.1 thorpej void
332 1.1 thorpej depca_copytobuf(struct lance_softc *sc, void *from, int boff, int len)
333 1.1 thorpej {
334 1.1 thorpej struct depca_softc *dsc = (void *) sc->sc_dev.dv_parent;
335 1.1 thorpej
336 1.1 thorpej bus_space_write_region_1(dsc->sc_memt, dsc->sc_memh, boff,
337 1.1 thorpej from, len);
338 1.1 thorpej }
339 1.1 thorpej
340 1.1 thorpej void
341 1.1 thorpej depca_copyfrombuf(struct lance_softc *sc, void *to, int boff, int len)
342 1.1 thorpej {
343 1.1 thorpej struct depca_softc *dsc = (void *) sc->sc_dev.dv_parent;
344 1.1 thorpej
345 1.1 thorpej bus_space_read_region_1(dsc->sc_memt, dsc->sc_memh, boff,
346 1.1 thorpej to, len);
347 1.1 thorpej }
348 1.1 thorpej
349 1.1 thorpej void
350 1.1 thorpej depca_zerobuf(struct lance_softc *sc, int boff, int len)
351 1.1 thorpej {
352 1.1 thorpej struct depca_softc *dsc = (void *) sc->sc_dev.dv_parent;
353 1.1 thorpej
354 1.1 thorpej bus_space_set_region_1(dsc->sc_memt, dsc->sc_memh, boff,
355 1.1 thorpej 0x00, len);
356 1.1 thorpej }
357