xref: /netbsd/sys/dev/ic/depca.c (revision bf9ec67e)
1 /*	$NetBSD: depca.c,v 1.4 2001/11/13 13:14:36 lukem 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 <sys/cdefs.h>
79 __KERNEL_RCSID(0, "$NetBSD: depca.c,v 1.4 2001/11/13 13:14:36 lukem Exp $");
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 #include <machine/cpu.h>
93 #include <machine/intr.h>
94 #include <machine/bus.h>
95 
96 #include <dev/isa/isareg.h>
97 #include <dev/isa/isavar.h>
98 
99 #include <dev/ic/lancereg.h>
100 #include <dev/ic/lancevar.h>
101 #include <dev/ic/am7990reg.h>
102 #include <dev/ic/am7990var.h>
103 #include <dev/ic/depcareg.h>
104 #include <dev/ic/depcavar.h>
105 
106 struct le_depca_softc {
107 	struct am7990_softc sc_am7990;	/* glue to MI code */
108 
109 	void *sc_ih;
110 };
111 
112 int	le_depca_match(struct device *, struct cfdata *, void *);
113 void	le_depca_attach(struct device *, struct device *, void *);
114 
115 struct cfattach le_depca_ca = {
116 	sizeof(struct le_depca_softc), le_depca_match, le_depca_attach
117 };
118 
119 void	depca_copytobuf(struct lance_softc *, void *, int, int);
120 void	depca_copyfrombuf(struct lance_softc *, void *, int, int);
121 void	depca_zerobuf(struct lance_softc *, int, int);
122 
123 struct depca_attach_args {
124 	const char *da_name;
125 };
126 
127 int	depca_print(void *, const char *);
128 
129 void
130 depca_attach(struct depca_softc *sc)
131 {
132 	struct depca_attach_args da;
133 
134 	da.da_name = "le";
135 
136 	(void) config_found(&sc->sc_dev, &da, depca_print);
137 }
138 
139 int
140 depca_print(void *aux, const char *pnp)
141 {
142 	struct depca_attach_args *da = aux;
143 
144 	if (pnp)
145 		printf("%s at %s", da->da_name, pnp);
146 
147 	return (UNCONF);
148 }
149 
150 void
151 depca_wrcsr(struct lance_softc *sc, u_int16_t port, u_int16_t val)
152 {
153 	struct depca_softc *dsc = (void *) sc->sc_dev.dv_parent;
154 
155 	bus_space_write_2(dsc->sc_iot, dsc->sc_ioh, DEPCA_RAP, port);
156 	bus_space_write_2(dsc->sc_iot, dsc->sc_ioh, DEPCA_RDP, val);
157 }
158 
159 u_int16_t
160 depca_rdcsr(struct lance_softc *sc, u_int16_t port)
161 {
162 	struct depca_softc *dsc = (void *) sc->sc_dev.dv_parent;
163 
164 	bus_space_write_2(dsc->sc_iot, dsc->sc_ioh, DEPCA_RAP, port);
165 	return (bus_space_read_2(dsc->sc_iot, dsc->sc_ioh, DEPCA_RDP));
166 }
167 
168 int
169 depca_readprom(bus_space_tag_t iot, bus_space_handle_t ioh, u_int8_t *laddr)
170 {
171 	int port, i;
172 
173 	/*
174 	 * Extract the physical MAC address from the ROM.
175 	 *
176 	 * The address PROM is 32 bytes wide, and we access it through
177 	 * a single I/O port.  On each read, it rotates to the next
178 	 * position.  We find the ethernet address by looking for a
179 	 * particular sequence of bytes (0xff, 0x00, 0x55, 0xaa, 0xff,
180 	 * 0x00, 0x55, 0xaa), and then reading the next 8 bytes (the
181 	 * ethernet address and a checksum).
182 	 *
183 	 * It appears that the PROM can be at one of two locations, so
184 	 * we just try both.
185 	 */
186 	port = DEPCA_ADP;
187 	for (i = 0; i < 32; i++)
188 		if (bus_space_read_1(iot, ioh, port) == 0xff &&
189 		    bus_space_read_1(iot, ioh, port) == 0x00 &&
190 		    bus_space_read_1(iot, ioh, port) == 0x55 &&
191 		    bus_space_read_1(iot, ioh, port) == 0xaa &&
192 		    bus_space_read_1(iot, ioh, port) == 0xff &&
193 		    bus_space_read_1(iot, ioh, port) == 0x00 &&
194 		    bus_space_read_1(iot, ioh, port) == 0x55 &&
195 		    bus_space_read_1(iot, ioh, port) == 0xaa)
196 			goto found;
197 	port = DEPCA_ADP + 1;
198 	for (i = 0; i < 32; i++)
199 		if (bus_space_read_1(iot, ioh, port) == 0xff &&
200 		    bus_space_read_1(iot, ioh, port) == 0x00 &&
201 		    bus_space_read_1(iot, ioh, port) == 0x55 &&
202 		    bus_space_read_1(iot, ioh, port) == 0xaa &&
203 		    bus_space_read_1(iot, ioh, port) == 0xff &&
204 		    bus_space_read_1(iot, ioh, port) == 0x00 &&
205 		    bus_space_read_1(iot, ioh, port) == 0x55 &&
206 		    bus_space_read_1(iot, ioh, port) == 0xaa)
207 			goto found;
208 	printf("depca: address not found\n");
209 	return (-1);
210 
211 found:
212 
213 	if (laddr) {
214 		for (i = 0; i < 6; i++)
215 			laddr[i] = bus_space_read_1(iot, ioh, port);
216 	}
217 
218 #if 0
219 	sum =
220 	    (laddr[0] <<  2) +
221 	    (laddr[1] << 10) +
222 	    (laddr[2] <<  1) +
223 	    (laddr[3] <<  9) +
224 	    (laddr[4] <<  0) +
225 	    (laddr[5] <<  8);
226 	sum = (sum & 0xffff) + (sum >> 16);
227 	sum = (sum & 0xffff) + (sum >> 16);
228 
229 	rom_sum = bus_space_read_1(iot, ioh, port);
230 	rom_sum |= bus_space_read_1(iot, ioh, port) << 8;
231 
232 	if (sum != rom_sum) {
233 		printf("depca: checksum mismatch; calculated %04x != read %04x",
234 		       sum, rom_sum);
235 		return (-1);
236 	}
237 #endif
238 
239 	return (0);
240 }
241 
242 int
243 le_depca_match(struct device *parent, struct cfdata *match, void *aux)
244 {
245 	struct depca_attach_args *da = aux;
246 
247 	return (strcmp(da->da_name, match->cf_driver->cd_name) == 0);
248 }
249 
250 void
251 le_depca_attach(struct device *parent, struct device *self, void *aux)
252 {
253 	struct depca_softc *dsc = (void *) parent;
254 	struct le_depca_softc *lesc = (void *) self;
255 	struct lance_softc *sc = &lesc->sc_am7990.lsc;
256 	u_int8_t val;
257 	int i;
258 
259 	printf("\n");
260 
261 	/* I/O and memory spaces already mapped. */
262 
263 	if (depca_readprom(dsc->sc_iot, dsc->sc_ioh, sc->sc_enaddr)) {
264 		printf("%s: can't read PROM\n", self->dv_xname);
265 		return;
266 	}
267 
268 	bus_space_write_2(dsc->sc_iot, dsc->sc_ioh, DEPCA_CSR,
269 	    DEPCA_CSR_DUM | DEPCA_CSR_IEN | DEPCA_CSR_SHE |
270 	    (dsc->sc_memsize == 32*1024 ? DEPCA_CSR_LOW32K : 0));
271 
272 	val = 0xff;
273 	for (;;) {
274 		u_int8_t cv;
275 
276 		bus_space_set_region_1(dsc->sc_memt, dsc->sc_memh, 0, val,
277 		    dsc->sc_memsize);
278 		for (i = 0; i < dsc->sc_memsize; i++) {
279 			cv = bus_space_read_1(dsc->sc_memt, dsc->sc_memh, i);
280 			if (cv != val) {
281 				printf("%s: failed to clear memory at %d "
282 				    "(0x%02x != 0x%02x)\n",
283 				    sc->sc_dev.dv_xname, i, cv, val);
284 				return;
285 			}
286 		}
287 		if (val == 0x00)
288 			break;
289 		val -= 0x55;
290 	}
291 
292 	sc->sc_conf3 = LE_C3_ACON;
293 	sc->sc_mem = 0;			/* Not used. */
294 	sc->sc_addr = 0;
295 	sc->sc_memsize = dsc->sc_memsize;
296 
297 	sc->sc_copytodesc = depca_copytobuf;
298 	sc->sc_copyfromdesc = depca_copyfrombuf;
299 	sc->sc_copytobuf = depca_copytobuf;
300 	sc->sc_copyfrombuf = depca_copyfrombuf;
301 	sc->sc_zerobuf = depca_zerobuf;
302 
303 	sc->sc_rdcsr = depca_rdcsr;
304 	sc->sc_wrcsr = depca_wrcsr;
305 	sc->sc_hwinit = NULL;
306 
307 	printf("%s", sc->sc_dev.dv_xname);
308 	am7990_config(&lesc->sc_am7990);
309 
310 	lesc->sc_ih = (*dsc->sc_intr_establish)(dsc, sc);
311 }
312 
313 /*
314  * Controller interrupt.
315  */
316 int
317 depca_intredge(void *arg)
318 {
319 
320 	if (am7990_intr(arg) == 0)
321 		return (0);
322 	for (;;)
323 		if (am7990_intr(arg) == 0)
324 			return (1);
325 }
326 
327 /*
328  * DEPCA shared memory access functions.
329  */
330 
331 void
332 depca_copytobuf(struct lance_softc *sc, void *from, int boff, int len)
333 {
334 	struct depca_softc *dsc = (void *) sc->sc_dev.dv_parent;
335 
336 	bus_space_write_region_1(dsc->sc_memt, dsc->sc_memh, boff,
337 	    from, len);
338 }
339 
340 void
341 depca_copyfrombuf(struct lance_softc *sc, void *to, int boff, int len)
342 {
343 	struct depca_softc *dsc = (void *) sc->sc_dev.dv_parent;
344 
345 	bus_space_read_region_1(dsc->sc_memt, dsc->sc_memh, boff,
346 	    to, len);
347 }
348 
349 void
350 depca_zerobuf(struct lance_softc *sc, int boff, int len)
351 {
352 	struct depca_softc *dsc = (void *) sc->sc_dev.dv_parent;
353 
354 	bus_space_set_region_1(dsc->sc_memt, dsc->sc_memh, boff,
355 	    0x00, len);
356 }
357