epe.c revision 1.52 1 1.52 thorpej /* $NetBSD: epe.c,v 1.52 2025/10/04 04:44:19 thorpej Exp $ */
2 1.1 joff
3 1.1 joff /*
4 1.1 joff * Copyright (c) 2004 Jesse Off
5 1.1 joff * All rights reserved.
6 1.1 joff *
7 1.1 joff * Redistribution and use in source and binary forms, with or without
8 1.1 joff * modification, are permitted provided that the following conditions
9 1.1 joff * are met:
10 1.1 joff * 1. Redistributions of source code must retain the above copyright
11 1.1 joff * notice, this list of conditions and the following disclaimer.
12 1.1 joff * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 joff * notice, this list of conditions and the following disclaimer in the
14 1.1 joff * documentation and/or other materials provided with the distribution.
15 1.1 joff *
16 1.1 joff * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 1.1 joff * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 1.1 joff * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 1.1 joff * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 1.1 joff * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 1.1 joff * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 1.1 joff * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 1.1 joff * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 1.1 joff * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 1.1 joff * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 1.1 joff * POSSIBILITY OF SUCH DAMAGE.
27 1.1 joff */
28 1.1 joff
29 1.1 joff #include <sys/cdefs.h>
30 1.52 thorpej __KERNEL_RCSID(0, "$NetBSD: epe.c,v 1.52 2025/10/04 04:44:19 thorpej Exp $");
31 1.1 joff
32 1.1 joff #include <sys/types.h>
33 1.1 joff #include <sys/param.h>
34 1.1 joff #include <sys/systm.h>
35 1.1 joff #include <sys/ioctl.h>
36 1.1 joff #include <sys/kernel.h>
37 1.1 joff #include <sys/proc.h>
38 1.1 joff #include <sys/time.h>
39 1.1 joff #include <sys/device.h>
40 1.1 joff #include <uvm/uvm_extern.h>
41 1.1 joff
42 1.26 dyoung #include <sys/bus.h>
43 1.1 joff #include <machine/intr.h>
44 1.1 joff
45 1.1 joff #include <arm/cpufunc.h>
46 1.1 joff
47 1.41 msaitoh #include <arm/ep93xx/epsocvar.h>
48 1.1 joff #include <arm/ep93xx/ep93xxvar.h>
49 1.1 joff
50 1.1 joff #include <net/if.h>
51 1.1 joff #include <net/if_dl.h>
52 1.1 joff #include <net/if_types.h>
53 1.1 joff #include <net/if_media.h>
54 1.1 joff #include <net/if_ether.h>
55 1.37 msaitoh #include <net/bpf.h>
56 1.1 joff
57 1.1 joff #include <dev/mii/mii.h>
58 1.1 joff #include <dev/mii/miivar.h>
59 1.1 joff
60 1.1 joff #ifdef INET
61 1.1 joff #include <netinet/in.h>
62 1.1 joff #include <netinet/in_systm.h>
63 1.1 joff #include <netinet/in_var.h>
64 1.1 joff #include <netinet/ip.h>
65 1.1 joff #include <netinet/if_inarp.h>
66 1.1 joff #endif
67 1.1 joff
68 1.2 joff #include <arm/ep93xx/ep93xxreg.h>
69 1.41 msaitoh #include <arm/ep93xx/epereg.h>
70 1.41 msaitoh #include <arm/ep93xx/epevar.h>
71 1.1 joff
72 1.4 hamajima #define DEFAULT_MDCDIV 32
73 1.4 hamajima
74 1.2 joff #ifndef EPE_FAST
75 1.2 joff #define EPE_FAST
76 1.2 joff #endif
77 1.1 joff
78 1.2 joff #ifndef EPE_FAST
79 1.1 joff #define EPE_READ(x) \
80 1.1 joff bus_space_read_4(sc->sc_iot, sc->sc_ioh, (EPE_ ## x))
81 1.1 joff #define EPE_WRITE(x, y) \
82 1.1 joff bus_space_write_4(sc->sc_iot, sc->sc_ioh, (EPE_ ## x), (y))
83 1.2 joff #define CTRLPAGE_DMASYNC(x, y, z) \
84 1.2 joff bus_dmamap_sync(sc->sc_dmat, sc->ctrlpage_dmamap, (x), (y), (z))
85 1.2 joff #else
86 1.29 skrll #define EPE_READ(x) *(volatile uint32_t *) \
87 1.2 joff (EP93XX_AHB_VBASE + EP93XX_AHB_EPE + (EPE_ ## x))
88 1.29 skrll #define EPE_WRITE(x, y) *(volatile uint32_t *) \
89 1.2 joff (EP93XX_AHB_VBASE + EP93XX_AHB_EPE + (EPE_ ## x)) = y
90 1.2 joff #define CTRLPAGE_DMASYNC(x, y, z)
91 1.2 joff #endif /* ! EPE_FAST */
92 1.1 joff
93 1.27 matt static int epe_match(device_t , cfdata_t, void *);
94 1.27 matt static void epe_attach(device_t, device_t, void *);
95 1.1 joff static void epe_init(struct epe_softc *);
96 1.42 msaitoh static int epe_intr(void* arg);
97 1.2 joff static int epe_gctx(struct epe_softc *);
98 1.39 msaitoh int epe_mii_readreg (device_t, int, int, uint16_t *);
99 1.39 msaitoh int epe_mii_writereg (device_t, int, int, uint16_t);
100 1.27 matt void epe_statchg (struct ifnet *);
101 1.1 joff void epe_tick (void *);
102 1.10 christos static int epe_ifioctl (struct ifnet *, u_long, void *);
103 1.1 joff static void epe_ifstart (struct ifnet *);
104 1.1 joff static void epe_ifwatchdog (struct ifnet *);
105 1.1 joff static int epe_ifinit (struct ifnet *);
106 1.1 joff static void epe_ifstop (struct ifnet *, int);
107 1.1 joff static void epe_setaddr (struct ifnet *);
108 1.1 joff
109 1.28 chs CFATTACH_DECL_NEW(epe, sizeof(struct epe_softc),
110 1.1 joff epe_match, epe_attach, NULL, NULL);
111 1.1 joff
112 1.1 joff static int
113 1.27 matt epe_match(device_t parent, cfdata_t match, void *aux)
114 1.1 joff {
115 1.1 joff return 2;
116 1.1 joff }
117 1.1 joff
118 1.1 joff static void
119 1.27 matt epe_attach(device_t parent, device_t self, void *aux)
120 1.1 joff {
121 1.27 matt struct epe_softc *sc = device_private(self);
122 1.1 joff struct epsoc_attach_args *sa;
123 1.1 joff
124 1.27 matt aprint_normal("\n");
125 1.1 joff sa = aux;
126 1.27 matt sc->sc_dev = self;
127 1.1 joff sc->sc_iot = sa->sa_iot;
128 1.1 joff sc->sc_intr = sa->sa_intr;
129 1.1 joff sc->sc_dmat = sa->sa_dmat;
130 1.1 joff
131 1.41 msaitoh if (bus_space_map(sa->sa_iot, sa->sa_addr, sa->sa_size,
132 1.1 joff 0, &sc->sc_ioh))
133 1.28 chs panic("%s: Cannot map registers", device_xname(self));
134 1.1 joff
135 1.4 hamajima /* Fetch the Ethernet address from property if set. */
136 1.52 thorpej if (ether_getaddr(self, sc->sc_enaddr)) {
137 1.4 hamajima bus_space_write_4(sc->sc_iot, sc->sc_ioh, EPE_AFP, 0);
138 1.4 hamajima bus_space_write_region_1(sc->sc_iot, sc->sc_ioh, EPE_IndAd,
139 1.4 hamajima sc->sc_enaddr, ETHER_ADDR_LEN);
140 1.4 hamajima }
141 1.4 hamajima
142 1.42 msaitoh ep93xx_intr_establish(sc->sc_intr, IPL_NET, epe_intr, sc);
143 1.1 joff epe_init(sc);
144 1.1 joff }
145 1.1 joff
146 1.1 joff static int
147 1.2 joff epe_gctx(struct epe_softc *sc)
148 1.2 joff {
149 1.2 joff struct ifnet * ifp = &sc->sc_ec.ec_if;
150 1.29 skrll uint32_t *cur, ndq = 0;
151 1.2 joff
152 1.2 joff /* Handle transmit completions */
153 1.29 skrll cur = (uint32_t *)(EPE_READ(TXStsQCurAdd) -
154 1.11 he sc->ctrlpage_dsaddr + (char*)sc->ctrlpage);
155 1.2 joff
156 1.41 msaitoh if (sc->TXStsQ_cur != cur) {
157 1.41 msaitoh CTRLPAGE_DMASYNC(TX_QLEN * 2 * sizeof(uint32_t),
158 1.29 skrll TX_QLEN * sizeof(uint32_t), BUS_DMASYNC_PREREAD);
159 1.41 msaitoh } else
160 1.2 joff return 0;
161 1.2 joff
162 1.2 joff do {
163 1.29 skrll uint32_t tbi = *sc->TXStsQ_cur & 0x7fff;
164 1.2 joff struct mbuf *m = sc->txq[tbi].m;
165 1.2 joff
166 1.41 msaitoh if ((*sc->TXStsQ_cur & TXStsQ_TxWE) == 0)
167 1.46 skrll if_statinc(ifp, if_oerrors);
168 1.41 msaitoh
169 1.2 joff bus_dmamap_unload(sc->sc_dmat, sc->txq[tbi].m_dmamap);
170 1.2 joff m_freem(m);
171 1.2 joff do {
172 1.2 joff sc->txq[tbi].m = NULL;
173 1.2 joff ndq++;
174 1.2 joff tbi = (tbi + 1) % TX_QLEN;
175 1.2 joff } while (sc->txq[tbi].m == m);
176 1.2 joff
177 1.46 skrll if_statinc(ifp, if_opackets);
178 1.2 joff sc->TXStsQ_cur++;
179 1.2 joff if (sc->TXStsQ_cur >= sc->TXStsQ + TX_QLEN) {
180 1.2 joff sc->TXStsQ_cur = sc->TXStsQ;
181 1.2 joff }
182 1.41 msaitoh } while (sc->TXStsQ_cur != cur);
183 1.2 joff
184 1.2 joff sc->TXDQ_avail += ndq;
185 1.49 thorpej if (sc->tx_busy) {
186 1.49 thorpej sc->tx_busy = false;
187 1.2 joff /* Disable end-of-tx-chain interrupt */
188 1.2 joff EPE_WRITE(IntEn, IntEn_REOFIE);
189 1.2 joff }
190 1.2 joff return ndq;
191 1.2 joff }
192 1.2 joff
193 1.2 joff static int
194 1.1 joff epe_intr(void *arg)
195 1.1 joff {
196 1.1 joff struct epe_softc *sc = (struct epe_softc *)arg;
197 1.1 joff struct ifnet * ifp = &sc->sc_ec.ec_if;
198 1.29 skrll uint32_t ndq = 0, irq, *cur;
199 1.1 joff
200 1.1 joff irq = EPE_READ(IntStsC);
201 1.1 joff begin:
202 1.29 skrll cur = (uint32_t *)(EPE_READ(RXStsQCurAdd) -
203 1.11 he sc->ctrlpage_dsaddr + (char*)sc->ctrlpage);
204 1.29 skrll CTRLPAGE_DMASYNC(TX_QLEN * 3 * sizeof(uint32_t),
205 1.41 msaitoh RX_QLEN * 4 * sizeof(uint32_t),
206 1.1 joff BUS_DMASYNC_PREREAD);
207 1.1 joff while (sc->RXStsQ_cur != cur) {
208 1.41 msaitoh if ((sc->RXStsQ_cur[0] & (RXStsQ_RWE | RXStsQ_RFP |RXStsQ_EOB))
209 1.41 msaitoh == (RXStsQ_RWE | RXStsQ_RFP | RXStsQ_EOB)) {
210 1.29 skrll uint32_t bi = (sc->RXStsQ_cur[1] >> 16) & 0x7fff;
211 1.29 skrll uint32_t fl = sc->RXStsQ_cur[1] & 0xffff;
212 1.1 joff struct mbuf *m;
213 1.1 joff
214 1.1 joff MGETHDR(m, M_DONTWAIT, MT_DATA);
215 1.1 joff if (m != NULL) MCLGET(m, M_DONTWAIT);
216 1.1 joff if (m != NULL && (m->m_flags & M_EXT)) {
217 1.41 msaitoh bus_dmamap_unload(sc->sc_dmat,
218 1.1 joff sc->rxq[bi].m_dmamap);
219 1.34 ozaki m_set_rcvif(sc->rxq[bi].m, ifp);
220 1.41 msaitoh sc->rxq[bi].m->m_pkthdr.len =
221 1.1 joff sc->rxq[bi].m->m_len = fl;
222 1.33 ozaki if_percpuq_enqueue(ifp->if_percpuq,
223 1.33 ozaki sc->rxq[bi].m);
224 1.1 joff sc->rxq[bi].m = m;
225 1.41 msaitoh bus_dmamap_load(sc->sc_dmat,
226 1.41 msaitoh sc->rxq[bi].m_dmamap,
227 1.1 joff m->m_ext.ext_buf, MCLBYTES,
228 1.1 joff NULL, BUS_DMA_NOWAIT);
229 1.41 msaitoh sc->RXDQ[bi * 2] =
230 1.1 joff sc->rxq[bi].m_dmamap->dm_segs[0].ds_addr;
231 1.1 joff } else {
232 1.1 joff /* Drop packets until we can get replacement
233 1.1 joff * empty mbufs for the RXDQ.
234 1.1 joff */
235 1.51 rin m_freem(m);
236 1.41 msaitoh
237 1.46 skrll if_statinc(ifp, if_ierrors);
238 1.41 msaitoh }
239 1.41 msaitoh } else
240 1.46 skrll if_statinc(ifp, if_ierrors);
241 1.1 joff
242 1.1 joff ndq++;
243 1.1 joff
244 1.1 joff sc->RXStsQ_cur += 2;
245 1.41 msaitoh if (sc->RXStsQ_cur >= sc->RXStsQ + (RX_QLEN * 2))
246 1.1 joff sc->RXStsQ_cur = sc->RXStsQ;
247 1.1 joff }
248 1.1 joff
249 1.1 joff if (ndq > 0) {
250 1.29 skrll CTRLPAGE_DMASYNC(TX_QLEN * 3 * sizeof(uint32_t),
251 1.42 msaitoh RX_QLEN * 4 * sizeof(uint32_t),
252 1.41 msaitoh BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
253 1.1 joff EPE_WRITE(RXStsEnq, ndq);
254 1.1 joff EPE_WRITE(RXDEnq, ndq);
255 1.1 joff ndq = 0;
256 1.1 joff }
257 1.1 joff
258 1.2 joff if (epe_gctx(sc) > 0 && IFQ_IS_EMPTY(&ifp->if_snd) == 0) {
259 1.36 nonaka if_schedule_deferred_start(ifp);
260 1.41 msaitoh }
261 1.1 joff
262 1.1 joff irq = EPE_READ(IntStsC);
263 1.41 msaitoh if ((irq & (IntSts_RxSQ | IntSts_ECI)) != 0)
264 1.1 joff goto begin;
265 1.2 joff
266 1.41 msaitoh return 1;
267 1.1 joff }
268 1.1 joff
269 1.1 joff
270 1.1 joff static void
271 1.1 joff epe_init(struct epe_softc *sc)
272 1.1 joff {
273 1.1 joff bus_dma_segment_t segs;
274 1.11 he char *addr;
275 1.1 joff int rsegs, err, i;
276 1.1 joff struct ifnet * ifp = &sc->sc_ec.ec_if;
277 1.44 msaitoh struct mii_data *mii = &sc->sc_mii;
278 1.4 hamajima int mdcdiv = DEFAULT_MDCDIV;
279 1.1 joff
280 1.12 ad callout_init(&sc->epe_tick_ch, 0);
281 1.1 joff
282 1.1 joff /* Select primary Individual Address in Address Filter Pointer */
283 1.1 joff EPE_WRITE(AFP, 0);
284 1.1 joff /* Read ethernet MAC, should already be set by bootrom */
285 1.1 joff bus_space_read_region_1(sc->sc_iot, sc->sc_ioh, EPE_IndAd,
286 1.1 joff sc->sc_enaddr, ETHER_ADDR_LEN);
287 1.41 msaitoh aprint_normal_dev(sc->sc_dev, "MAC address %s\n",
288 1.1 joff ether_sprintf(sc->sc_enaddr));
289 1.1 joff
290 1.1 joff /* Soft Reset the MAC */
291 1.1 joff EPE_WRITE(SelfCtl, SelfCtl_RESET);
292 1.41 msaitoh while (EPE_READ(SelfCtl) & SelfCtl_RESET)
293 1.41 msaitoh ;
294 1.1 joff
295 1.1 joff /* suggested magic initialization values from datasheet */
296 1.1 joff EPE_WRITE(RXBufThrshld, 0x800040);
297 1.1 joff EPE_WRITE(TXBufThrshld, 0x200010);
298 1.1 joff EPE_WRITE(RXStsThrshld, 0x40002);
299 1.1 joff EPE_WRITE(TXStsThrshld, 0x40002);
300 1.1 joff EPE_WRITE(RXDThrshld, 0x40002);
301 1.1 joff EPE_WRITE(TXDThrshld, 0x40002);
302 1.1 joff
303 1.1 joff /* Allocate a page of memory for descriptor and status queues */
304 1.41 msaitoh err = bus_dmamem_alloc(sc->sc_dmat, PAGE_SIZE, 0, PAGE_SIZE,
305 1.1 joff &segs, 1, &rsegs, BUS_DMA_WAITOK);
306 1.1 joff if (err == 0) {
307 1.41 msaitoh err = bus_dmamem_map(sc->sc_dmat, &segs, 1, PAGE_SIZE,
308 1.41 msaitoh &sc->ctrlpage, (BUS_DMA_WAITOK | BUS_DMA_COHERENT));
309 1.1 joff }
310 1.1 joff if (err == 0) {
311 1.1 joff err = bus_dmamap_create(sc->sc_dmat, PAGE_SIZE, 1, PAGE_SIZE,
312 1.1 joff 0, BUS_DMA_WAITOK, &sc->ctrlpage_dmamap);
313 1.1 joff }
314 1.1 joff if (err == 0) {
315 1.1 joff err = bus_dmamap_load(sc->sc_dmat, sc->ctrlpage_dmamap,
316 1.1 joff sc->ctrlpage, PAGE_SIZE, NULL, BUS_DMA_WAITOK);
317 1.1 joff }
318 1.1 joff if (err != 0) {
319 1.27 matt panic("%s: Cannot get DMA memory", device_xname(sc->sc_dev));
320 1.1 joff }
321 1.2 joff sc->ctrlpage_dsaddr = sc->ctrlpage_dmamap->dm_segs[0].ds_addr;
322 1.21 cegger memset(sc->ctrlpage, 0, PAGE_SIZE);
323 1.41 msaitoh
324 1.1 joff /* Set up pointers to start of each queue in kernel addr space.
325 1.1 joff * Each descriptor queue or status queue entry uses 2 words
326 1.1 joff */
327 1.29 skrll sc->TXDQ = (uint32_t *)sc->ctrlpage;
328 1.1 joff sc->TXDQ_cur = sc->TXDQ;
329 1.1 joff sc->TXDQ_avail = TX_QLEN - 1;
330 1.1 joff sc->TXStsQ = &sc->TXDQ[TX_QLEN * 2];
331 1.1 joff sc->TXStsQ_cur = sc->TXStsQ;
332 1.1 joff sc->RXDQ = &sc->TXStsQ[TX_QLEN];
333 1.1 joff sc->RXStsQ = &sc->RXDQ[RX_QLEN * 2];
334 1.1 joff sc->RXStsQ_cur = sc->RXStsQ;
335 1.1 joff
336 1.1 joff /* Program each queue's start addr, cur addr, and len registers
337 1.41 msaitoh * with the physical addresses.
338 1.1 joff */
339 1.11 he addr = (char *)sc->ctrlpage_dmamap->dm_segs[0].ds_addr;
340 1.29 skrll EPE_WRITE(TXDQBAdd, (uint32_t)addr);
341 1.29 skrll EPE_WRITE(TXDQCurAdd, (uint32_t)addr);
342 1.41 msaitoh EPE_WRITE(TXDQBLen, TX_QLEN * 2 * sizeof(uint32_t));
343 1.29 skrll
344 1.29 skrll addr += (sc->TXStsQ - sc->TXDQ) * sizeof(uint32_t);
345 1.29 skrll EPE_WRITE(TXStsQBAdd, (uint32_t)addr);
346 1.29 skrll EPE_WRITE(TXStsQCurAdd, (uint32_t)addr);
347 1.29 skrll EPE_WRITE(TXStsQBLen, TX_QLEN * sizeof(uint32_t));
348 1.29 skrll
349 1.29 skrll addr += (sc->RXDQ - sc->TXStsQ) * sizeof(uint32_t);
350 1.29 skrll EPE_WRITE(RXDQBAdd, (uint32_t)addr);
351 1.29 skrll EPE_WRITE(RXDCurAdd, (uint32_t)addr);
352 1.29 skrll EPE_WRITE(RXDQBLen, RX_QLEN * 2 * sizeof(uint32_t));
353 1.41 msaitoh
354 1.29 skrll addr += (sc->RXStsQ - sc->RXDQ) * sizeof(uint32_t);
355 1.29 skrll EPE_WRITE(RXStsQBAdd, (uint32_t)addr);
356 1.29 skrll EPE_WRITE(RXStsQCurAdd, (uint32_t)addr);
357 1.29 skrll EPE_WRITE(RXStsQBLen, RX_QLEN * 2 * sizeof(uint32_t));
358 1.1 joff
359 1.1 joff /* Populate the RXDQ with mbufs */
360 1.41 msaitoh for (i = 0; i < RX_QLEN; i++) {
361 1.1 joff struct mbuf *m;
362 1.1 joff
363 1.41 msaitoh bus_dmamap_create(sc->sc_dmat, MCLBYTES, TX_QLEN/4, MCLBYTES,
364 1.41 msaitoh 0, BUS_DMA_WAITOK, &sc->rxq[i].m_dmamap);
365 1.1 joff MGETHDR(m, M_WAIT, MT_DATA);
366 1.1 joff MCLGET(m, M_WAIT);
367 1.1 joff sc->rxq[i].m = m;
368 1.41 msaitoh bus_dmamap_load(sc->sc_dmat, sc->rxq[i].m_dmamap,
369 1.41 msaitoh m->m_ext.ext_buf, MCLBYTES, NULL, BUS_DMA_WAITOK);
370 1.1 joff
371 1.1 joff sc->RXDQ[i * 2] = sc->rxq[i].m_dmamap->dm_segs[0].ds_addr;
372 1.1 joff sc->RXDQ[i * 2 + 1] = (i << 16) | MCLBYTES;
373 1.1 joff bus_dmamap_sync(sc->sc_dmat, sc->rxq[i].m_dmamap, 0,
374 1.1 joff MCLBYTES, BUS_DMASYNC_PREREAD);
375 1.1 joff }
376 1.1 joff
377 1.41 msaitoh for (i = 0; i < TX_QLEN; i++) {
378 1.1 joff bus_dmamap_create(sc->sc_dmat, MCLBYTES, 1, MCLBYTES, 0,
379 1.41 msaitoh (BUS_DMA_WAITOK | BUS_DMA_ALLOCNOW),
380 1.1 joff &sc->txq[i].m_dmamap);
381 1.1 joff sc->txq[i].m = NULL;
382 1.1 joff sc->TXDQ[i * 2 + 1] = (i << 16);
383 1.1 joff }
384 1.1 joff
385 1.1 joff /* Divide HCLK by 32 for MDC clock */
386 1.27 matt if (device_cfdata(sc->sc_dev)->cf_flags)
387 1.27 matt mdcdiv = device_cfdata(sc->sc_dev)->cf_flags;
388 1.41 msaitoh EPE_WRITE(SelfCtl, (SelfCtl_MDCDIV(mdcdiv) | SelfCtl_PSPRS));
389 1.1 joff
390 1.44 msaitoh mii->mii_ifp = ifp;
391 1.44 msaitoh mii->mii_readreg = epe_mii_readreg;
392 1.44 msaitoh mii->mii_writereg = epe_mii_writereg;
393 1.44 msaitoh mii->mii_statchg = epe_statchg;
394 1.44 msaitoh sc->sc_ec.ec_mii = mii;
395 1.48 thorpej ifmedia_init(&mii->mii_media, IFM_IMASK, ether_mediachange,
396 1.15 dyoung ether_mediastatus);
397 1.44 msaitoh mii_attach(sc->sc_dev, mii, 0xffffffff, MII_PHY_ANY,
398 1.44 msaitoh MII_OFFSET_ANY, 0);
399 1.44 msaitoh ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_AUTO);
400 1.1 joff
401 1.41 msaitoh EPE_WRITE(BMCtl, BMCtl_RxEn | BMCtl_TxEn);
402 1.2 joff EPE_WRITE(IntEn, IntEn_REOFIE);
403 1.1 joff /* maximum valid max frame length */
404 1.41 msaitoh EPE_WRITE(MaxFrmLen, (0x7ff << 16) | MHLEN);
405 1.1 joff /* wait for receiver ready */
406 1.41 msaitoh while ((EPE_READ(BMSts) & BMSts_RxAct) == 0)
407 1.30 joerg continue;
408 1.1 joff /* enqueue the entries in RXStsQ and RXDQ */
409 1.41 msaitoh CTRLPAGE_DMASYNC(0, sc->ctrlpage_dmamap->dm_mapsize,
410 1.41 msaitoh BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
411 1.1 joff EPE_WRITE(RXDEnq, RX_QLEN - 1);
412 1.1 joff EPE_WRITE(RXStsEnq, RX_QLEN - 1);
413 1.1 joff
414 1.1 joff /*
415 1.1 joff * We can support 802.1Q VLAN-sized frames.
416 1.1 joff */
417 1.1 joff sc->sc_ec.ec_capabilities |= ETHERCAP_VLAN_MTU;
418 1.1 joff
419 1.42 msaitoh strcpy(ifp->if_xname, device_xname(sc->sc_dev));
420 1.42 msaitoh ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
421 1.42 msaitoh ifp->if_ioctl = epe_ifioctl;
422 1.42 msaitoh ifp->if_start = epe_ifstart;
423 1.42 msaitoh ifp->if_watchdog = epe_ifwatchdog;
424 1.42 msaitoh ifp->if_init = epe_ifinit;
425 1.42 msaitoh ifp->if_stop = epe_ifstop;
426 1.42 msaitoh ifp->if_timer = 0;
427 1.1 joff ifp->if_softc = sc;
428 1.42 msaitoh IFQ_SET_READY(&ifp->if_snd);
429 1.42 msaitoh if_attach(ifp);
430 1.36 nonaka if_deferred_start_init(ifp, NULL);
431 1.42 msaitoh ether_ifattach(ifp, (sc)->sc_enaddr);
432 1.1 joff }
433 1.1 joff
434 1.1 joff int
435 1.39 msaitoh epe_mii_readreg(device_t self, int phy, int reg, uint16_t *val)
436 1.1 joff {
437 1.39 msaitoh uint32_t d;
438 1.1 joff
439 1.1 joff d = EPE_READ(SelfCtl);
440 1.1 joff EPE_WRITE(SelfCtl, d & ~SelfCtl_PSPRS); /* no preamble suppress */
441 1.1 joff EPE_WRITE(MIICmd, (MIICmd_READ | (phy << 5) | reg));
442 1.41 msaitoh while (EPE_READ(MIISts) & MIISts_BUSY)
443 1.41 msaitoh ;
444 1.39 msaitoh *val = EPE_READ(MIIData) & 0xffff;
445 1.1 joff EPE_WRITE(SelfCtl, d); /* restore old value */
446 1.39 msaitoh return 0;
447 1.1 joff }
448 1.1 joff
449 1.39 msaitoh int
450 1.39 msaitoh epe_mii_writereg(device_t self, int phy, int reg, uint16_t val)
451 1.1 joff {
452 1.29 skrll uint32_t d;
453 1.1 joff
454 1.1 joff d = EPE_READ(SelfCtl);
455 1.1 joff EPE_WRITE(SelfCtl, d & ~SelfCtl_PSPRS); /* no preamble suppress */
456 1.3 hamajima EPE_WRITE(MIIData, val);
457 1.1 joff EPE_WRITE(MIICmd, (MIICmd_WRITE | (phy << 5) | reg));
458 1.41 msaitoh while (EPE_READ(MIISts) & MIISts_BUSY)
459 1.41 msaitoh ;
460 1.1 joff EPE_WRITE(SelfCtl, d); /* restore old value */
461 1.39 msaitoh
462 1.39 msaitoh return 0;
463 1.1 joff }
464 1.1 joff
465 1.1 joff void
466 1.27 matt epe_statchg(struct ifnet *ifp)
467 1.1 joff {
468 1.42 msaitoh struct epe_softc *sc = ifp->if_softc;
469 1.42 msaitoh uint32_t reg;
470 1.1 joff
471 1.42 msaitoh /*
472 1.42 msaitoh * We must keep the MAC and the PHY in sync as
473 1.42 msaitoh * to the status of full-duplex!
474 1.42 msaitoh */
475 1.42 msaitoh reg = EPE_READ(TestCtl);
476 1.42 msaitoh if (sc->sc_mii.mii_media_active & IFM_FDX)
477 1.42 msaitoh reg |= TestCtl_MFDX;
478 1.42 msaitoh else
479 1.42 msaitoh reg &= ~TestCtl_MFDX;
480 1.1 joff EPE_WRITE(TestCtl, reg);
481 1.1 joff }
482 1.1 joff
483 1.1 joff void
484 1.19 dsl epe_tick(void *arg)
485 1.1 joff {
486 1.1 joff struct epe_softc* sc = (struct epe_softc *)arg;
487 1.1 joff struct ifnet * ifp = &sc->sc_ec.ec_if;
488 1.2 joff int s;
489 1.29 skrll uint32_t misses;
490 1.1 joff
491 1.47 skrll if_statadd(ifp, if_collisions, EPE_READ(TXCollCnt));
492 1.1 joff /* These misses are ok, they will happen if the RAM/CPU can't keep up */
493 1.1 joff misses = EPE_READ(RXMissCnt);
494 1.41 msaitoh if (misses > 0)
495 1.27 matt printf("%s: %d rx misses\n", device_xname(sc->sc_dev), misses);
496 1.41 msaitoh
497 1.2 joff s = splnet();
498 1.2 joff if (epe_gctx(sc) > 0 && IFQ_IS_EMPTY(&ifp->if_snd) == 0) {
499 1.2 joff epe_ifstart(ifp);
500 1.2 joff }
501 1.2 joff splx(s);
502 1.2 joff
503 1.1 joff mii_tick(&sc->sc_mii);
504 1.1 joff callout_reset(&sc->epe_tick_ch, hz, epe_tick, sc);
505 1.1 joff }
506 1.1 joff
507 1.1 joff
508 1.1 joff static int
509 1.19 dsl epe_ifioctl(struct ifnet *ifp, u_long cmd, void *data)
510 1.1 joff {
511 1.1 joff int s, error;
512 1.1 joff
513 1.1 joff s = splnet();
514 1.15 dyoung error = ether_ioctl(ifp, cmd, data);
515 1.15 dyoung if (error == ENETRESET) {
516 1.15 dyoung if (ifp->if_flags & IFF_RUNNING)
517 1.15 dyoung epe_setaddr(ifp);
518 1.15 dyoung error = 0;
519 1.1 joff }
520 1.1 joff splx(s);
521 1.1 joff return error;
522 1.1 joff }
523 1.1 joff
524 1.1 joff static void
525 1.19 dsl epe_ifstart(struct ifnet *ifp)
526 1.1 joff {
527 1.1 joff struct epe_softc *sc = (struct epe_softc *)ifp->if_softc;
528 1.1 joff struct mbuf *m;
529 1.1 joff bus_dma_segment_t *segs;
530 1.2 joff int s, bi, err, nsegs, ndq;
531 1.2 joff
532 1.41 msaitoh s = splnet();
533 1.2 joff start:
534 1.2 joff ndq = 0;
535 1.1 joff if (sc->TXDQ_avail == 0) {
536 1.2 joff if (epe_gctx(sc) == 0) {
537 1.2 joff /* Enable End-Of-TX-Chain interrupt */
538 1.41 msaitoh EPE_WRITE(IntEn, IntEn_REOFIE | IntEn_ECIE);
539 1.49 thorpej sc->tx_busy = true;
540 1.2 joff ifp->if_timer = 10;
541 1.2 joff splx(s);
542 1.2 joff return;
543 1.2 joff }
544 1.41 msaitoh }
545 1.2 joff
546 1.41 msaitoh bi = sc->TXDQ_cur - sc->TXDQ;
547 1.1 joff
548 1.1 joff IFQ_POLL(&ifp->if_snd, m);
549 1.1 joff if (m == NULL) {
550 1.1 joff splx(s);
551 1.1 joff return;
552 1.1 joff }
553 1.2 joff more:
554 1.1 joff if ((err = bus_dmamap_load_mbuf(sc->sc_dmat, sc->txq[bi].m_dmamap, m,
555 1.41 msaitoh BUS_DMA_NOWAIT)) ||
556 1.1 joff sc->txq[bi].m_dmamap->dm_segs[0].ds_addr & 0x3 ||
557 1.1 joff sc->txq[bi].m_dmamap->dm_nsegs > (sc->TXDQ_avail - ndq)) {
558 1.1 joff /* Copy entire mbuf chain to new and 32-bit aligned storage */
559 1.1 joff struct mbuf *mn;
560 1.1 joff
561 1.41 msaitoh if (err == 0)
562 1.1 joff bus_dmamap_unload(sc->sc_dmat, sc->txq[bi].m_dmamap);
563 1.1 joff
564 1.1 joff MGETHDR(mn, M_DONTWAIT, MT_DATA);
565 1.1 joff if (mn == NULL) goto stop;
566 1.1 joff if (m->m_pkthdr.len > (MHLEN & (~0x3))) {
567 1.1 joff MCLGET(mn, M_DONTWAIT);
568 1.1 joff if ((mn->m_flags & M_EXT) == 0) {
569 1.1 joff m_freem(mn);
570 1.1 joff goto stop;
571 1.1 joff }
572 1.1 joff }
573 1.41 msaitoh mn->m_data = (void *)(((uint32_t)mn->m_data + 0x3) & (~0x3));
574 1.10 christos m_copydata(m, 0, m->m_pkthdr.len, mtod(mn, void *));
575 1.1 joff mn->m_pkthdr.len = mn->m_len = m->m_pkthdr.len;
576 1.1 joff IFQ_DEQUEUE(&ifp->if_snd, m);
577 1.1 joff m_freem(m);
578 1.1 joff m = mn;
579 1.1 joff bus_dmamap_load_mbuf(sc->sc_dmat, sc->txq[bi].m_dmamap, m,
580 1.1 joff BUS_DMA_NOWAIT);
581 1.1 joff } else {
582 1.1 joff IFQ_DEQUEUE(&ifp->if_snd, m);
583 1.1 joff }
584 1.1 joff
585 1.38 msaitoh bpf_mtap(ifp, m, BPF_D_OUT);
586 1.1 joff
587 1.1 joff nsegs = sc->txq[bi].m_dmamap->dm_nsegs;
588 1.1 joff segs = sc->txq[bi].m_dmamap->dm_segs;
589 1.41 msaitoh bus_dmamap_sync(sc->sc_dmat, sc->txq[bi].m_dmamap, 0,
590 1.41 msaitoh sc->txq[bi].m_dmamap->dm_mapsize,
591 1.41 msaitoh BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
592 1.1 joff
593 1.1 joff /* XXX: This driver hasn't been tested w/nsegs > 1 */
594 1.1 joff while (nsegs > 0) {
595 1.1 joff nsegs--;
596 1.1 joff sc->txq[bi].m = m;
597 1.1 joff sc->TXDQ[bi * 2] = segs->ds_addr;
598 1.1 joff if (nsegs == 0)
599 1.1 joff sc->TXDQ[bi * 2 + 1] = segs->ds_len | (bi << 16) |
600 1.1 joff (1 << 31);
601 1.1 joff else
602 1.1 joff sc->TXDQ[bi * 2 + 1] = segs->ds_len | (bi << 16);
603 1.1 joff segs++;
604 1.1 joff bi = (bi + 1) % TX_QLEN;
605 1.1 joff ndq++;
606 1.1 joff }
607 1.1 joff
608 1.1 joff
609 1.2 joff /*
610 1.2 joff * Enqueue another. Don't do more than half the available
611 1.2 joff * descriptors before telling the MAC about them
612 1.2 joff */
613 1.2 joff if ((sc->TXDQ_avail - ndq) > 0 && ndq < TX_QLEN / 2) {
614 1.1 joff IFQ_POLL(&ifp->if_snd, m);
615 1.41 msaitoh if (m != NULL)
616 1.2 joff goto more;
617 1.41 msaitoh }
618 1.1 joff stop:
619 1.1 joff if (ndq > 0) {
620 1.1 joff sc->TXDQ_avail -= ndq;
621 1.1 joff sc->TXDQ_cur = &sc->TXDQ[bi];
622 1.29 skrll CTRLPAGE_DMASYNC(0, TX_QLEN * 2 * sizeof(uint32_t),
623 1.41 msaitoh BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
624 1.1 joff EPE_WRITE(TXDEnq, ndq);
625 1.1 joff }
626 1.2 joff
627 1.2 joff if (IFQ_IS_EMPTY(&ifp->if_snd) == 0)
628 1.2 joff goto start;
629 1.2 joff
630 1.1 joff splx(s);
631 1.1 joff return;
632 1.1 joff }
633 1.1 joff
634 1.1 joff static void
635 1.19 dsl epe_ifwatchdog(struct ifnet *ifp)
636 1.1 joff {
637 1.1 joff struct epe_softc *sc = (struct epe_softc *)ifp->if_softc;
638 1.1 joff
639 1.1 joff if ((ifp->if_flags & IFF_RUNNING) == 0)
640 1.1 joff return;
641 1.42 msaitoh printf("%s: device timeout, BMCtl = 0x%08x, BMSts = 0x%08x\n",
642 1.27 matt device_xname(sc->sc_dev), EPE_READ(BMCtl), EPE_READ(BMSts));
643 1.1 joff }
644 1.1 joff
645 1.1 joff static int
646 1.19 dsl epe_ifinit(struct ifnet *ifp)
647 1.1 joff {
648 1.1 joff struct epe_softc *sc = ifp->if_softc;
649 1.15 dyoung int rc, s = splnet();
650 1.1 joff
651 1.1 joff callout_stop(&sc->epe_tick_ch);
652 1.41 msaitoh EPE_WRITE(RXCtl, RXCtl_IA0 | RXCtl_BA | RXCtl_RCRCA | RXCtl_SRxON);
653 1.1 joff EPE_WRITE(TXCtl, TXCtl_STxON);
654 1.1 joff EPE_WRITE(GIIntMsk, GIIntMsk_INT); /* start interrupting */
655 1.15 dyoung
656 1.15 dyoung if ((rc = mii_mediachg(&sc->sc_mii)) == ENXIO)
657 1.15 dyoung rc = 0;
658 1.15 dyoung else if (rc != 0)
659 1.15 dyoung goto out;
660 1.15 dyoung
661 1.1 joff callout_reset(&sc->epe_tick_ch, hz, epe_tick, sc);
662 1.42 msaitoh ifp->if_flags |= IFF_RUNNING;
663 1.15 dyoung out:
664 1.1 joff splx(s);
665 1.1 joff return 0;
666 1.1 joff }
667 1.1 joff
668 1.1 joff static void
669 1.19 dsl epe_ifstop(struct ifnet *ifp, int disable)
670 1.1 joff {
671 1.1 joff struct epe_softc *sc = ifp->if_softc;
672 1.1 joff
673 1.1 joff
674 1.1 joff EPE_WRITE(RXCtl, 0);
675 1.1 joff EPE_WRITE(TXCtl, 0);
676 1.1 joff EPE_WRITE(GIIntMsk, 0);
677 1.1 joff callout_stop(&sc->epe_tick_ch);
678 1.1 joff
679 1.1 joff /* Down the MII. */
680 1.1 joff mii_down(&sc->sc_mii);
681 1.1 joff
682 1.49 thorpej ifp->if_flags &= ~IFF_RUNNING;
683 1.1 joff ifp->if_timer = 0;
684 1.1 joff sc->sc_mii.mii_media_status &= ~IFM_ACTIVE;
685 1.1 joff }
686 1.1 joff
687 1.1 joff static void
688 1.19 dsl epe_setaddr(struct ifnet *ifp)
689 1.1 joff {
690 1.1 joff struct epe_softc *sc = ifp->if_softc;
691 1.41 msaitoh struct ethercom *ec = &sc->sc_ec;
692 1.1 joff struct ether_multi *enm;
693 1.1 joff struct ether_multistep step;
694 1.29 skrll uint8_t ias[2][ETHER_ADDR_LEN];
695 1.29 skrll uint32_t h, nma = 0, hashes[2] = { 0, 0 };
696 1.29 skrll uint32_t rxctl = EPE_READ(RXCtl);
697 1.1 joff
698 1.1 joff /* disable receiver temporarily */
699 1.1 joff EPE_WRITE(RXCtl, rxctl & ~RXCtl_SRxON);
700 1.1 joff
701 1.41 msaitoh rxctl &= ~(RXCtl_MA | RXCtl_PA | RXCtl_IA2 | RXCtl_IA3);
702 1.41 msaitoh
703 1.41 msaitoh if (ifp->if_flags & IFF_PROMISC)
704 1.1 joff rxctl |= RXCtl_PA;
705 1.1 joff
706 1.1 joff ifp->if_flags &= ~IFF_ALLMULTI;
707 1.1 joff
708 1.43 msaitoh ETHER_LOCK(ec);
709 1.41 msaitoh ETHER_FIRST_MULTI(step, ec, enm);
710 1.1 joff while (enm != NULL) {
711 1.1 joff if (memcmp(enm->enm_addrlo, enm->enm_addrhi, ETHER_ADDR_LEN)) {
712 1.1 joff /*
713 1.1 joff * We must listen to a range of multicast addresses.
714 1.1 joff * For now, just accept all multicasts, rather than
715 1.1 joff * trying to set only those filter bits needed to match
716 1.1 joff * the range. (At this time, the only use of address
717 1.1 joff * ranges is for IP multicast routing, for which the
718 1.1 joff * range is big enough to require all bits set.)
719 1.1 joff */
720 1.41 msaitoh rxctl &= ~(RXCtl_IA2 | RXCtl_IA3);
721 1.1 joff rxctl |= RXCtl_MA;
722 1.1 joff hashes[0] = 0xffffffffUL;
723 1.1 joff hashes[1] = 0xffffffffUL;
724 1.1 joff ifp->if_flags |= IFF_ALLMULTI;
725 1.1 joff break;
726 1.1 joff }
727 1.1 joff
728 1.1 joff if (nma < 2) {
729 1.1 joff /* We can program 2 perfect address filters for mcast */
730 1.41 msaitoh memcpy(ias[nma], enm->enm_addrlo, ETHER_ADDR_LEN);
731 1.1 joff rxctl |= (1 << (nma + 2));
732 1.1 joff } else {
733 1.1 joff /*
734 1.1 joff * XXX: Datasheet is not very clear here, I'm not sure
735 1.1 joff * if I'm doing this right. --joff
736 1.1 joff */
737 1.1 joff h = ether_crc32_le(enm->enm_addrlo, ETHER_ADDR_LEN);
738 1.1 joff
739 1.1 joff /* Just want the 6 most-significant bits. */
740 1.1 joff h = h >> 26;
741 1.1 joff
742 1.1 joff hashes[ h / 32 ] |= (1 << (h % 32));
743 1.1 joff rxctl |= RXCtl_MA;
744 1.1 joff }
745 1.1 joff ETHER_NEXT_MULTI(step, enm);
746 1.1 joff nma++;
747 1.1 joff }
748 1.43 msaitoh ETHER_UNLOCK(ec);
749 1.41 msaitoh
750 1.1 joff EPE_WRITE(AFP, 0);
751 1.41 msaitoh bus_space_write_region_1(sc->sc_iot, sc->sc_ioh, EPE_IndAd,
752 1.1 joff sc->sc_enaddr, ETHER_ADDR_LEN);
753 1.1 joff if (rxctl & RXCtl_IA2) {
754 1.1 joff EPE_WRITE(AFP, 2);
755 1.1 joff bus_space_write_region_1(sc->sc_iot, sc->sc_ioh, EPE_IndAd,
756 1.1 joff ias[0], ETHER_ADDR_LEN);
757 1.1 joff }
758 1.1 joff if (rxctl & RXCtl_IA3) {
759 1.1 joff EPE_WRITE(AFP, 3);
760 1.1 joff bus_space_write_region_1(sc->sc_iot, sc->sc_ioh, EPE_IndAd,
761 1.1 joff ias[1], ETHER_ADDR_LEN);
762 1.1 joff }
763 1.1 joff if (hashes[0] != 0 && hashes[1] != 0) {
764 1.1 joff EPE_WRITE(AFP, 7);
765 1.1 joff EPE_WRITE(HashTbl, hashes[0]);
766 1.1 joff EPE_WRITE(HashTbl + 4, hashes[1]);
767 1.1 joff }
768 1.1 joff EPE_WRITE(RXCtl, rxctl);
769 1.1 joff }
770