1 /* $NetBSD: if_le.c,v 1.14 2022/05/29 10:45:05 rin 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 *
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.
30 */
31
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: if_le.c,v 1.14 2022/05/29 10:45:05 rin Exp $");
34
35 #include "opt_inet.h"
36
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/mbuf.h>
40 #include <sys/syslog.h>
41 #include <sys/socket.h>
42 #include <sys/device.h>
43
44 #include <net/if.h>
45 #include <net/if_ether.h>
46 #include <net/if_media.h>
47
48 #ifdef INET
49 #include <netinet/in.h>
50 #include <netinet/if_inarp.h>
51 #endif
52
53 #include <machine/cpu.h>
54 #include <machine/mainboard.h>
55 #include <machine/autoconf.h>
56 #include <machine/bus.h>
57
58 #include <dev/ic/lancereg.h>
59 #include <dev/ic/lancevar.h>
60 #include <dev/ic/am7990reg.h>
61 #include <dev/ic/am7990var.h>
62
63 /*
64 * LANCE registers.
65 */
66
67 #define LEREG1_RDP 2 /* Offset to LANCE data register */
68 #define LEREG1_RAP 6 /* Offset to LANCE address register */
69
70 /*
71 * Ethernet software status per interface.
72 * The real stuff is in dev/ic/am7990var.h
73 */
74 struct le_softc {
75 struct am7990_softc sc_am7990; /* glue to MI code */
76 bus_space_tag_t sc_bustag;
77 bus_dma_tag_t sc_dmatag;
78 bus_space_handle_t sc_reg; /* LANCE registers */
79 bus_dmamap_t sc_dmamap;
80 struct evcnt sc_intrcnt; /* Interrupt event counter */
81 };
82
83 static int le_match(device_t, cfdata_t, void *);
84 static void le_attach(device_t, device_t, void *);
85
86 CFATTACH_DECL_NEW(le, sizeof(struct le_softc),
87 le_match, le_attach, NULL, NULL);
88
89 static int le_attached;
90
91 #if defined(_KERNEL_OPT)
92 #include "opt_ddb.h"
93 #endif
94
95 #ifdef DDB
96 #define integrate
97 #define hide
98 #else
99 #define integrate static inline
100 #define hide static
101 #endif
102
103 int le_intr(void *);
104
105 hide void lewrcsr(struct lance_softc *, uint16_t, uint16_t);
106 hide uint16_t lerdcsr(struct lance_softc *, uint16_t);
107
108 hide void
lewrcsr(struct lance_softc * sc,uint16_t port,uint16_t val)109 lewrcsr(struct lance_softc *sc, uint16_t port, uint16_t val)
110 {
111 struct le_softc *lesc = (struct le_softc *)sc;
112
113 bus_space_write_2(lesc->sc_bustag, lesc->sc_reg, LEREG1_RAP, port);
114 bus_space_write_2(lesc->sc_bustag, lesc->sc_reg, LEREG1_RDP, val);
115 }
116
117 hide uint16_t
lerdcsr(struct lance_softc * sc,uint16_t port)118 lerdcsr(struct lance_softc *sc, uint16_t port)
119 {
120 struct le_softc *lesc = (struct le_softc *)sc;
121
122 bus_space_write_2(lesc->sc_bustag, lesc->sc_reg, LEREG1_RAP, port);
123 return (bus_space_read_2(lesc->sc_bustag, lesc->sc_reg, LEREG1_RDP));
124 }
125
126 int
le_match(device_t parent,cfdata_t cf,void * aux)127 le_match(device_t parent, cfdata_t cf, void *aux)
128 {
129 struct confargs *ca = aux;
130 int addr;
131
132 if (strcmp(ca->ca_name, "le"))
133 return 0;
134
135 if (le_attached)
136 return 0;
137
138 addr = LANCE_PORT;
139 if (badaddr((void *)addr, 1))
140 return 0;
141
142 return 1;
143 }
144
145 #define LE_MEMSIZE (32*1024)
146
147 void
le_attach(device_t parent,device_t self,void * aux)148 le_attach(device_t parent, device_t self, void *aux)
149 {
150 struct le_softc *lesc = device_private(self);
151 struct lance_softc *sc = &lesc->sc_am7990.lsc;
152 struct confargs *ca = aux;
153
154 bus_dma_tag_t dmat;
155 bus_dma_segment_t seg;
156 int rseg;
157 uint8_t *id;
158 int i;
159 void *kvaddr;
160
161 sc->sc_dev = self;
162 id = (uint8_t *)ETHER_ID;
163 lesc->sc_bustag = ca->ca_bustag;
164 dmat = lesc->sc_dmatag = ca->ca_dmatag;
165
166 if (bus_space_map(ca->ca_bustag, ca->ca_addr,
167 8, /* size */
168 BUS_SPACE_MAP_LINEAR,
169 &lesc->sc_reg) != 0) {
170 aprint_error(": cannot map registers\n");
171 return;
172 }
173
174 /*
175 * Allocate a physically contiguous DMA area for the chip.
176 */
177 if (bus_dmamem_alloc(dmat, LE_MEMSIZE, 0, 0, &seg, 1,
178 &rseg, BUS_DMA_NOWAIT)) {
179 aprint_error(": can't allocate DMA area\n");
180 goto bad_bsunmap;
181 }
182 /* Map pages into kernel memory */
183 if (bus_dmamem_map(dmat, &seg, rseg, LE_MEMSIZE,
184 &kvaddr, BUS_DMA_NOWAIT|BUS_DMA_COHERENT)) {
185 aprint_error(": can't map DMA area\n");
186 goto bad_free;
187 }
188 /* Build DMA map so we can get physical address */
189 if (bus_dmamap_create(dmat, LE_MEMSIZE, 1, LE_MEMSIZE,
190 0, BUS_DMA_NOWAIT, &lesc->sc_dmamap)) {
191 aprint_error(": can't create DMA map\n");
192 goto bad_unmap;
193 }
194 if (bus_dmamap_load(dmat, lesc->sc_dmamap, kvaddr, LE_MEMSIZE,
195 NULL, BUS_DMA_NOWAIT)) {
196 aprint_error(": can't load DMA map\n");
197 goto bad_destroy;
198 }
199
200 sc->sc_memsize = LE_MEMSIZE; /* 16K Buffer space*/
201 sc->sc_mem = (void *)MIPS_PHYS_TO_KSEG1(kvaddr);
202 sc->sc_addr = lesc->sc_dmamap->dm_segs[0].ds_addr;
203
204 sc->sc_conf3 = LE_C3_BSWP;
205
206 /* Copy Ethernet hardware address from NVRAM */
207 for (i = 0; i < ETHER_ADDR_LEN; i++)
208 sc->sc_enaddr[i] = id[i * 4 + 3]; /* XXX */
209
210 sc->sc_copytodesc = lance_copytobuf_contig;
211 sc->sc_copyfromdesc = lance_copyfrombuf_contig;
212 sc->sc_copytobuf = lance_copytobuf_contig;
213 sc->sc_copyfrombuf = lance_copyfrombuf_contig;
214 sc->sc_zerobuf = lance_zerobuf_contig;
215
216 sc->sc_rdcsr = lerdcsr;
217 sc->sc_wrcsr = lewrcsr;
218 sc->sc_hwinit = NULL;
219
220 evcnt_attach_dynamic(&lesc->sc_intrcnt, EVCNT_TYPE_INTR, NULL,
221 device_xname(self), "intr");
222 bus_intr_establish(lesc->sc_bustag, SYS_INTR_ETHER, 0, 0,
223 le_intr, lesc);
224
225 am7990_config(&lesc->sc_am7990);
226 return;
227
228 bad_destroy:
229 bus_dmamap_destroy(dmat, lesc->sc_dmamap);
230 bad_unmap:
231 bus_dmamem_unmap(dmat, kvaddr, LE_MEMSIZE);
232 bad_free:
233 bus_dmamem_free(dmat, &seg, rseg);
234 bad_bsunmap:
235 bus_space_unmap(ca->ca_bustag, lesc->sc_reg, 8);
236 }
237
238 int
le_intr(void * arg)239 le_intr(void *arg)
240 {
241 struct le_softc *lesc = arg;
242
243 lesc->sc_intrcnt.ev_count++;
244 return am7990_intr(&lesc->sc_am7990);
245 }
246