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