1 /* $NetBSD: if_le.c,v 1.11 2008/04/04 12:25:06 tsutsui Exp $ */
4 * Copyright (c) 1996, 2000 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Adam Glass, Gordon W. Ross and Wayne Knowles
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: if_le.c,v 1.11 2008/04/04 12:25:06 tsutsui Exp $");
38 #include <sys/param.h>
39 #include <sys/systm.h>
41 #include <sys/syslog.h>
42 #include <sys/socket.h>
43 #include <sys/device.h>
46 #include <net/if_ether.h>
47 #include <net/if_media.h>
50 #include <netinet/in.h>
51 #include <netinet/if_inarp.h>
54 #include <machine/cpu.h>
55 #include <machine/mainboard.h>
56 #include <machine/autoconf.h>
57 #include <machine/bus.h>
59 #include <dev/ic/lancereg.h>
60 #include <dev/ic/lancevar.h>
61 #include <dev/ic/am7990reg.h>
62 #include <dev/ic/am7990var.h>
68 #define LEREG1_RDP 2 /* Offset to LANCE data register */
69 #define LEREG1_RAP 6 /* Offset to LANCE address register */
72 * Ethernet software status per interface.
73 * The real stuff is in dev/ic/am7990var.h
76 struct am7990_softc sc_am7990
; /* glue to MI code */
77 bus_space_tag_t sc_bustag
;
78 bus_dma_tag_t sc_dmatag
;
79 bus_space_handle_t sc_reg
; /* LANCE registers */
80 bus_dmamap_t sc_dmamap
;
81 struct evcnt sc_intrcnt
; /* Interrupt event counter */
84 static int le_match(device_t
, cfdata_t
, void *);
85 static void le_attach(device_t
, device_t
, void *);
87 CFATTACH_DECL_NEW(le
, sizeof(struct le_softc
),
88 le_match
, le_attach
, NULL
, NULL
);
90 static int le_attached
;
92 #if defined(_KERNEL_OPT)
100 #define integrate static inline
106 hide
void lewrcsr(struct lance_softc
*, uint16_t, uint16_t);
107 hide
uint16_t lerdcsr(struct lance_softc
*, uint16_t);
110 lewrcsr(struct lance_softc
*sc
, uint16_t port
, uint16_t val
)
112 struct le_softc
*lesc
= (struct le_softc
*)sc
;
114 bus_space_write_2(lesc
->sc_bustag
, lesc
->sc_reg
, LEREG1_RAP
, port
);
115 bus_space_write_2(lesc
->sc_bustag
, lesc
->sc_reg
, LEREG1_RDP
, val
);
119 lerdcsr(struct lance_softc
*sc
, uint16_t port
)
121 struct le_softc
*lesc
= (struct le_softc
*)sc
;
123 bus_space_write_2(lesc
->sc_bustag
, lesc
->sc_reg
, LEREG1_RAP
, port
);
124 return (bus_space_read_2(lesc
->sc_bustag
, lesc
->sc_reg
, LEREG1_RDP
));
128 le_match(device_t parent
, cfdata_t cf
, void *aux
)
130 struct confargs
*ca
= aux
;
133 if (strcmp(ca
->ca_name
, "le"))
140 if (badaddr((void *)addr
, 1))
146 #define LE_MEMSIZE (32*1024)
149 le_attach(device_t parent
, device_t self
, void *aux
)
151 struct le_softc
*lesc
= device_private(self
);
152 struct lance_softc
*sc
= &lesc
->sc_am7990
.lsc
;
153 struct confargs
*ca
= aux
;
156 bus_dma_segment_t seg
;
163 id
= (uint8_t *)ETHER_ID
;
164 lesc
->sc_bustag
= ca
->ca_bustag
;
165 dmat
= lesc
->sc_dmatag
= ca
->ca_dmatag
;
167 if (bus_space_map(ca
->ca_bustag
, ca
->ca_addr
,
169 BUS_SPACE_MAP_LINEAR
,
170 &lesc
->sc_reg
) != 0) {
171 aprint_error(": cannot map registers\n");
176 * Allocate a physically contiguous DMA area for the chip.
178 if (bus_dmamem_alloc(dmat
, LE_MEMSIZE
, 0, 0, &seg
, 1,
179 &rseg
, BUS_DMA_NOWAIT
)) {
180 aprint_error(": can't allocate DMA area\n");
183 /* Map pages into kernel memory */
184 if (bus_dmamem_map(dmat
, &seg
, rseg
, LE_MEMSIZE
,
185 &kvaddr
, BUS_DMA_NOWAIT
|BUS_DMA_COHERENT
)) {
186 aprint_error(": can't map DMA area\n");
187 bus_dmamem_free(dmat
, &seg
, rseg
);
190 /* Build DMA map so we can get physical address */
191 if (bus_dmamap_create(dmat
, LE_MEMSIZE
, 1, LE_MEMSIZE
,
192 0, BUS_DMA_NOWAIT
, &lesc
->sc_dmamap
)) {
193 aprint_error(": can't create DMA map\n");
196 if (bus_dmamap_load(dmat
, lesc
->sc_dmamap
, kvaddr
, LE_MEMSIZE
,
197 NULL
, BUS_DMA_NOWAIT
)) {
198 aprint_error(": can't load DMA map\n");
202 sc
->sc_memsize
= LE_MEMSIZE
; /* 16K Buffer space*/
203 sc
->sc_mem
= (void *)MIPS_PHYS_TO_KSEG1(kvaddr
);
204 sc
->sc_addr
= lesc
->sc_dmamap
->dm_segs
[0].ds_addr
;
206 sc
->sc_conf3
= LE_C3_BSWP
;
208 /* Copy Ethernet hardware address from NVRAM */
209 for (i
= 0; i
< ETHER_ADDR_LEN
; i
++)
210 sc
->sc_enaddr
[i
] = id
[i
* 4 + 3]; /* XXX */
212 sc
->sc_copytodesc
= lance_copytobuf_contig
;
213 sc
->sc_copyfromdesc
= lance_copyfrombuf_contig
;
214 sc
->sc_copytobuf
= lance_copytobuf_contig
;
215 sc
->sc_copyfrombuf
= lance_copyfrombuf_contig
;
216 sc
->sc_zerobuf
= lance_zerobuf_contig
;
218 sc
->sc_rdcsr
= lerdcsr
;
219 sc
->sc_wrcsr
= lewrcsr
;
220 sc
->sc_hwinit
= NULL
;
222 evcnt_attach_dynamic(&lesc
->sc_intrcnt
, EVCNT_TYPE_INTR
, NULL
,
223 device_xname(self
), "intr");
224 bus_intr_establish(lesc
->sc_bustag
, SYS_INTR_ETHER
, 0, 0,
227 am7990_config(&lesc
->sc_am7990
);
231 bus_dmamem_unmap(dmat
, kvaddr
, LE_MEMSIZE
);
232 bus_dmamem_free(dmat
, &seg
, rseg
);
238 struct le_softc
*lesc
= arg
;
240 lesc
->sc_intrcnt
.ev_count
++;
241 return am7990_intr(&lesc
->sc_am7990
);