if_le.c revision 1.6 1 /* $NetBSD: if_le.c,v 1.6 2003/07/15 02:43:43 lukem Exp $ */
2
3 /*-
4 * Copyright (c) 1996, 2000 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Adam Glass, Gordon W. Ross and Wayne Knowles
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. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 #include <sys/cdefs.h>
40 __KERNEL_RCSID(0, "$NetBSD: if_le.c,v 1.6 2003/07/15 02:43:43 lukem Exp $");
41
42 #include "opt_inet.h"
43 #include "bpfilter.h"
44
45 #include <sys/param.h>
46 #include <sys/systm.h>
47 #include <sys/mbuf.h>
48 #include <sys/syslog.h>
49 #include <sys/socket.h>
50 #include <sys/device.h>
51
52 #include <net/if.h>
53 #include <net/if_ether.h>
54 #include <net/if_media.h>
55
56 #ifdef INET
57 #include <netinet/in.h>
58 #include <netinet/if_inarp.h>
59 #endif
60
61 #include <machine/cpu.h>
62 #include <machine/mainboard.h>
63 #include <machine/autoconf.h>
64 #include <machine/bus.h>
65
66 #include <dev/ic/lancereg.h>
67 #include <dev/ic/lancevar.h>
68 #include <dev/ic/am7990reg.h>
69 #include <dev/ic/am7990var.h>
70
71 /*
72 * LANCE registers.
73 */
74
75 #define LEREG1_RDP 2 /* Offset to LANCE data register */
76 #define LEREG1_RAP 6 /* Offset to LANCE address register */
77
78 /*
79 * Ethernet software status per interface.
80 * The real stuff is in dev/ic/am7990var.h
81 */
82 struct le_softc {
83 struct am7990_softc sc_am7990; /* glue to MI code */
84 bus_space_tag_t sc_bustag;
85 bus_dma_tag_t sc_dmatag;
86 bus_space_handle_t sc_reg; /* LANCE registers */
87 bus_dmamap_t sc_dmamap;
88 struct evcnt sc_intrcnt; /* Interrupt event counter */
89 };
90
91 static int le_match __P((struct device *, struct cfdata *, void *));
92 static void le_attach __P((struct device *, struct device *, void *));
93
94 CFATTACH_DECL(le, sizeof(struct le_softc),
95 le_match, le_attach, NULL, NULL);
96
97 #if defined(_KERNEL_OPT)
98 #include "opt_ddb.h"
99 #endif
100
101 #ifdef DDB
102 #define integrate
103 #define hide
104 #else
105 #define integrate static __inline
106 #define hide static
107 #endif
108
109 int le_intr __P((void *));
110
111 hide void lewrcsr __P((struct lance_softc *, u_int16_t, u_int16_t));
112 hide u_int16_t lerdcsr __P((struct lance_softc *, u_int16_t));
113
114 hide void
115 lewrcsr(sc, port, val)
116 struct lance_softc *sc;
117 u_int16_t port, val;
118 {
119 struct le_softc *lesc = (struct le_softc *)sc;
120
121 bus_space_write_2(lesc->sc_bustag, lesc->sc_reg, LEREG1_RAP, port);
122 bus_space_write_2(lesc->sc_bustag, lesc->sc_reg, LEREG1_RDP, val);
123 }
124
125 hide u_int16_t
126 lerdcsr(sc, port)
127 struct lance_softc *sc;
128 u_int16_t port;
129 {
130 struct le_softc *lesc = (struct le_softc *)sc;
131
132 bus_space_write_2(lesc->sc_bustag, lesc->sc_reg, LEREG1_RAP, port);
133 return (bus_space_read_2(lesc->sc_bustag, lesc->sc_reg, LEREG1_RDP));
134 }
135
136 int
137 le_match(parent, cf, aux)
138 struct device *parent;
139 struct cfdata *cf;
140 void *aux;
141 {
142 struct confargs *ca = aux;
143 int addr;
144
145 if (strcmp(ca->ca_name, "le"))
146 return 0;
147
148 switch(cf->cf_unit) {
149
150 case 0:
151 addr = LANCE_PORT;
152 break;
153 default:
154 return 0;
155 }
156
157 if (badaddr((void *)addr, 1))
158 return 0;
159
160 return 1;
161 }
162
163 #define LE_MEMSIZE (32*1024)
164
165 void
166 le_attach(parent, self, aux)
167 struct device *parent, *self;
168 void *aux;
169 {
170 struct le_softc *lesc = (struct le_softc *)self;
171 struct lance_softc *sc = &lesc->sc_am7990.lsc;
172 struct confargs *ca = aux;
173
174 bus_dma_tag_t dmat;
175 bus_dma_segment_t seg;
176 int rseg;
177
178 /*struct confargs *ca = aux;*/
179 u_char *id;
180 int i;
181 caddr_t kvaddr;
182
183 switch (sc->sc_dev.dv_unit) {
184 case 0:
185 id = (u_char *)(ETHER_ID);
186 break;
187 default:
188 panic("le_attach");
189 }
190
191 lesc->sc_bustag = ca->ca_bustag;
192 dmat = lesc->sc_dmatag = ca->ca_dmatag;
193
194 if (bus_space_map(ca->ca_bustag, ca->ca_addr,
195 8, /* size */
196 BUS_SPACE_MAP_LINEAR,
197 &lesc->sc_reg) != 0) {
198 printf(": cannot map registers\n");
199 return;
200 }
201
202 /*
203 * Allocate a physically contiguous DMA area for the chip.
204 */
205 if (bus_dmamem_alloc(dmat, LE_MEMSIZE, 0, 0, &seg, 1,
206 &rseg, BUS_DMA_NOWAIT)) {
207 printf(": can't allocate DMA area\n");
208 return;
209 }
210 /* Map pages into kernel memory */
211 if (bus_dmamem_map(dmat, &seg, rseg, LE_MEMSIZE,
212 &kvaddr, BUS_DMA_NOWAIT|BUS_DMA_COHERENT)) {
213 printf(": can't map DMA area\n");
214 bus_dmamem_free(dmat, &seg, rseg);
215 return;
216 }
217 /* Build DMA map so we can get physical address */
218 if (bus_dmamap_create(dmat, LE_MEMSIZE, 1, LE_MEMSIZE,
219 0, BUS_DMA_NOWAIT, &lesc->sc_dmamap)) {
220 printf(": can't create DMA map\n");
221 goto bad;
222 }
223 if (bus_dmamap_load(dmat, lesc->sc_dmamap, kvaddr, LE_MEMSIZE,
224 NULL, BUS_DMA_NOWAIT)) {
225 printf(": can't load DMA map\n");
226 goto bad;
227 }
228
229 sc->sc_memsize = LE_MEMSIZE; /* 16K Buffer space*/
230 sc->sc_mem = (void *) MIPS_PHYS_TO_KSEG1(kvaddr);
231 sc->sc_addr = lesc->sc_dmamap->dm_segs[0].ds_addr;
232
233 sc->sc_conf3 = LE_C3_BSWP;
234
235 /* Copy Ethernet hardware address from NVRAM */
236 for (i=0; i < 6; i++)
237 sc->sc_enaddr[i] = id[i*4+3]; /* XXX */
238
239 sc->sc_copytodesc = lance_copytobuf_contig;
240 sc->sc_copyfromdesc = lance_copyfrombuf_contig;
241 sc->sc_copytobuf = lance_copytobuf_contig;
242 sc->sc_copyfrombuf = lance_copyfrombuf_contig;
243 sc->sc_zerobuf = lance_zerobuf_contig;
244
245 sc->sc_rdcsr = lerdcsr;
246 sc->sc_wrcsr = lewrcsr;
247 sc->sc_hwinit = NULL;
248
249 evcnt_attach_dynamic(&lesc->sc_intrcnt, EVCNT_TYPE_INTR, NULL,
250 self->dv_xname, "intr");
251 bus_intr_establish(lesc->sc_bustag, SYS_INTR_ETHER, 0, 0,
252 le_intr, lesc);
253
254 am7990_config(&lesc->sc_am7990);
255 return;
256
257 bad:
258 bus_dmamem_unmap(dmat, kvaddr, LE_MEMSIZE);
259 bus_dmamem_free(dmat, &seg, rseg);
260 }
261
262 int
263 le_intr(arg)
264 void *arg;
265 {
266 struct le_softc *lesc = arg;
267
268 lesc->sc_intrcnt.ev_count++;
269 return am7990_intr(&lesc->sc_am7990);
270 }
271