xref: /netbsd-src/sys/arch/luna68k/dev/if_le.c (revision d48f14661dda8638fee055ba15d35bdfb29b9fa8)
1 /* $NetBSD: if_le.c,v 1.4 2005/12/11 12:17:52 christos Exp $ */
2 
3 /*-
4  * Copyright (c) 1992, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * This code is derived from software contributed to Berkeley by
8  * Ralph Campbell and Rick Macklem.
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. Neither the name of the University nor the names of its contributors
19  *    may be used to endorse or promote products derived from this software
20  *    without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  *
34  *	@(#)if_le.c	8.2 (Berkeley) 11/16/93
35  */
36 
37 /*-
38  * Copyright (c) 1995 Charles M. Hannum.  All rights reserved.
39  *
40  * This code is derived from software contributed to Berkeley by
41  * Ralph Campbell and Rick Macklem.
42  *
43  * Redistribution and use in source and binary forms, with or without
44  * modification, are permitted provided that the following conditions
45  * are met:
46  * 1. Redistributions of source code must retain the above copyright
47  *    notice, this list of conditions and the following disclaimer.
48  * 2. Redistributions in binary form must reproduce the above copyright
49  *    notice, this list of conditions and the following disclaimer in the
50  *    documentation and/or other materials provided with the distribution.
51  * 3. All advertising materials mentioning features or use of this software
52  *    must display the following acknowledgement:
53  *	This product includes software developed by the University of
54  *	California, Berkeley and its contributors.
55  * 4. Neither the name of the University nor the names of its contributors
56  *    may be used to endorse or promote products derived from this software
57  *    without specific prior written permission.
58  *
59  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
60  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
61  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
62  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
63  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
64  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
65  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
66  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
67  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
68  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
69  * SUCH DAMAGE.
70  *
71  *	@(#)if_le.c	8.2 (Berkeley) 11/16/93
72  */
73 
74 #include <sys/cdefs.h>			/* RCS ID & Copyright macro defns */
75 
76 __KERNEL_RCSID(0, "$NetBSD: if_le.c,v 1.4 2005/12/11 12:17:52 christos Exp $");
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/socket.h>
85 #include <sys/device.h>
86 
87 #include <net/if.h>
88 #include <net/if_ether.h>
89 #include <net/if_media.h>
90 
91 #ifdef INET
92 #include <netinet/in.h>
93 #include <netinet/if_inarp.h>
94 #endif
95 
96 #include <machine/cpu.h>
97 #include <machine/autoconf.h>
98 
99 #include <luna68k/luna68k/isr.h>
100 
101 #include <dev/ic/lancereg.h>
102 #include <dev/ic/lancevar.h>
103 #include <dev/ic/am7990reg.h>
104 #include <dev/ic/am7990var.h>
105 
106 /*
107  * LANCE registers.
108  */
109 struct lereg1 {
110 	volatile u_int16_t	ler1_rdp;	/* data port */
111 	volatile u_int16_t	ler1_rap;	/* register select port */
112 };
113 
114 /*
115  * Ethernet software status per interface.
116  *
117  * Each interface is referenced by a network interface structure,
118  * arpcom.ac_if, which the routing code uses to locate the interface.
119  * This structure contains the output queue for the interface, its address, ...
120  */
121 struct	le_softc {
122 	struct	am7990_softc sc_am7990;	/* glue to MI code */
123 
124 	struct	lereg1 *sc_r1;		/* LANCE registers */
125 };
126 
127 static int  le_match __P((struct device *, struct cfdata  *, void *));
128 static void le_attach __P((struct device *, struct device *, void *));
129 
130 CFATTACH_DECL(le, sizeof(struct le_softc),
131     le_match, le_attach, NULL, NULL);
132 extern struct cfdriver le_cd;
133 
134 static void lesrcsr __P((struct lance_softc *, u_int16_t, u_int16_t));
135 static u_int16_t lerdcsr __P((struct lance_softc *, u_int16_t));
136 static void myetheraddr __P((u_int8_t *));
137 
138 static void
139 lesrcsr(sc, port, val)
140 	struct lance_softc *sc;
141 	u_int16_t port, val;
142 {
143 	struct lereg1 *ler1 = ((struct le_softc *)sc)->sc_r1;
144 
145 	ler1->ler1_rap = port;
146 	ler1->ler1_rdp = val;
147 }
148 
149 static u_int16_t
150 lerdcsr(sc, port)
151 	struct lance_softc *sc;
152 	u_int16_t port;
153 {
154 	struct lereg1 *ler1 = ((struct le_softc *)sc)->sc_r1;
155 	u_int16_t val;
156 
157 	ler1->ler1_rap = port;
158 	val = ler1->ler1_rdp;
159 	return (val);
160 }
161 
162 static int
163 le_match(parent, cf, aux)
164 	struct device *parent;
165 	struct cfdata *cf;
166 	void *aux;
167 {
168 	struct mainbus_attach_args *ma = aux;
169 
170 	if (strcmp(ma->ma_name, le_cd.cd_name))
171 		return (0);
172 
173 	return (1);
174 }
175 
176 void
177 le_attach(parent, self, aux)
178 	struct device *parent, *self;
179 	void *aux;
180 {
181 	struct le_softc *lesc = (void *)self;
182 	struct lance_softc *sc = &lesc->sc_am7990.lsc;
183 	struct mainbus_attach_args *ma = aux;
184 
185 	/* Map control registers. */
186 	lesc->sc_r1 = (struct lereg1 *)ma->ma_addr;	/* LANCE */
187 
188 	sc->sc_mem = (void *)0x71010000;		/* SRAM */
189 	sc->sc_conf3 = LE_C3_BSWP;
190 	sc->sc_addr = (u_long)sc->sc_mem & 0xffffff;
191 	sc->sc_memsize = 64 * 1024;			/* 64KB */
192 
193 	myetheraddr(sc->sc_enaddr);
194 
195 	sc->sc_copytodesc = lance_copytobuf_contig;
196 	sc->sc_copyfromdesc = lance_copyfrombuf_contig;
197 	sc->sc_copytobuf = lance_copytobuf_contig;
198 	sc->sc_copyfrombuf = lance_copyfrombuf_contig;
199 	sc->sc_zerobuf = lance_zerobuf_contig;
200 
201 	sc->sc_rdcsr = lerdcsr;
202 	sc->sc_wrcsr = lesrcsr;
203 	sc->sc_hwinit = NULL;
204 
205 	am7990_config(&lesc->sc_am7990);
206 
207 	isrlink_autovec(am7990_intr, (void *)sc, ma->ma_ilvl, ISRPRI_NET);
208 }
209 
210 /*
211  * LUNA-I has 1Mbit CPU ROM, which contains MAC address directly
212  * readable at 0x4100FFE0 as a sequence of 12 ASCII characters.
213  *
214  * LUNA-II has 16Kbit NVSRAM on its ethercard, whose contents are
215  * accessible 4bit-wise by ctl register operation.  The register is
216  * mapped at 0xF1000004.
217  */
218 void
219 myetheraddr(ether)
220 	u_int8_t *ether;
221 {
222 	unsigned i, loc;
223 	u_int8_t *ea;
224 	volatile struct { u_int32_t ctl; } *ds1220;
225 
226 	switch (machtype) {
227 	case LUNA_I:
228 		ea = (u_int8_t *)0x4101FFE0;
229 		for (i = 0; i < 6; i++) {
230 			int u, l;
231 
232 			u = ea[0];
233 			u = (u < 'A') ? u & 0xf : u - 'A' + 10;
234 			l = ea[1];
235 			l = (l < 'A') ? l & 0xf : l - 'A' + 10;
236 
237 			ether[i] = l | (u << 4);
238 			ea += 2;
239 		}
240 		break;
241 	case LUNA_II:
242 		ds1220 = (void *)0xF1000004;
243 		loc = 12;
244 		for (i = 0; i < 6; i++) {
245 			unsigned u, l, hex;
246 
247 			ds1220->ctl = (loc) << 16;
248 			u = 0xf0 & (ds1220->ctl >> 12);
249 			ds1220->ctl = (loc + 1) << 16;
250 			l = 0x0f & (ds1220->ctl >> 16);
251 			hex = (u < '9') ? l : l + 9;
252 
253 			ds1220->ctl = (loc + 2) << 16;
254 			u = 0xf0 & (ds1220->ctl >> 12);
255 			ds1220->ctl = (loc + 3) << 16;
256 			l = 0x0f & (ds1220->ctl >> 16);
257 
258 			ether[i] = ((u < '9') ? l : l + 9) | (hex << 4);
259 			loc += 4;
260 		}
261 		break;
262 	default:
263 		ether[0] = 0x00; ether[1] = 0x00; ether[2] = 0x0a;
264 		ether[3] = 0xDE; ether[4] = 0xAD; ether[5] = 0x00;
265 		break;
266 	}
267 }
268