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