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