sunxi_emac.c revision 1.4.4.5 1 /* $NetBSD: sunxi_emac.c,v 1.4.4.5 2019/05/12 09:19:07 martin Exp $ */
2
3 /*-
4 * Copyright (c) 2016-2017 Jared McNeill <jmcneill (at) invisible.ca>
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29 /*
30 * Allwinner Gigabit Ethernet MAC (EMAC) controller
31 */
32
33 #include "opt_net_mpsafe.h"
34
35 #include <sys/cdefs.h>
36 __KERNEL_RCSID(0, "$NetBSD: sunxi_emac.c,v 1.4.4.5 2019/05/12 09:19:07 martin Exp $");
37
38 #include <sys/param.h>
39 #include <sys/bus.h>
40 #include <sys/device.h>
41 #include <sys/intr.h>
42 #include <sys/systm.h>
43 #include <sys/kernel.h>
44 #include <sys/mutex.h>
45 #include <sys/callout.h>
46 #include <sys/gpio.h>
47 #include <sys/cprng.h>
48
49 #include <net/if.h>
50 #include <net/if_dl.h>
51 #include <net/if_ether.h>
52 #include <net/if_media.h>
53 #include <net/bpf.h>
54
55 #include <dev/mii/miivar.h>
56
57 #include <dev/fdt/fdtvar.h>
58
59 #include <arm/sunxi/sunxi_emac.h>
60
61 #ifdef NET_MPSAFE
62 #define EMAC_MPSAFE 1
63 #define CALLOUT_FLAGS CALLOUT_MPSAFE
64 #define FDT_INTR_FLAGS FDT_INTR_MPSAFE
65 #else
66 #define CALLOUT_FLAGS 0
67 #define FDT_INTR_FLAGS 0
68 #endif
69
70 #define EMAC_IFNAME "emac%d"
71
72 #define ETHER_ALIGN 2
73
74 #define EMAC_LOCK(sc) mutex_enter(&(sc)->mtx)
75 #define EMAC_UNLOCK(sc) mutex_exit(&(sc)->mtx)
76 #define EMAC_ASSERT_LOCKED(sc) KASSERT(mutex_owned(&(sc)->mtx))
77
78 #define DESC_ALIGN sizeof(struct sunxi_emac_desc)
79 #define TX_DESC_COUNT 1024
80 #define TX_DESC_SIZE (sizeof(struct sunxi_emac_desc) * TX_DESC_COUNT)
81 #define RX_DESC_COUNT 256
82 #define RX_DESC_SIZE (sizeof(struct sunxi_emac_desc) * RX_DESC_COUNT)
83
84 #define DESC_OFF(n) ((n) * sizeof(struct sunxi_emac_desc))
85 #define TX_NEXT(n) (((n) + 1) & (TX_DESC_COUNT - 1))
86 #define TX_SKIP(n, o) (((n) + (o)) & (TX_DESC_COUNT - 1))
87 #define RX_NEXT(n) (((n) + 1) & (RX_DESC_COUNT - 1))
88
89 #define TX_MAX_SEGS 128
90
91 #define SOFT_RST_RETRY 1000
92 #define MII_BUSY_RETRY 1000
93 #define MDIO_FREQ 2500000
94
95 #define BURST_LEN_DEFAULT 8
96 #define RX_TX_PRI_DEFAULT 0
97 #define PAUSE_TIME_DEFAULT 0x400
98 #define TX_INTERVAL_DEFAULT 64
99
100 /* syscon EMAC clock register */
101 #define EMAC_CLK_EPHY_ADDR (0x1f << 20) /* H3 */
102 #define EMAC_CLK_EPHY_ADDR_SHIFT 20
103 #define EMAC_CLK_EPHY_LED_POL (1 << 17) /* H3 */
104 #define EMAC_CLK_EPHY_SHUTDOWN (1 << 16) /* H3 */
105 #define EMAC_CLK_EPHY_SELECT (1 << 15) /* H3 */
106 #define EMAC_CLK_RMII_EN (1 << 13)
107 #define EMAC_CLK_ETXDC (0x7 << 10)
108 #define EMAC_CLK_ETXDC_SHIFT 10
109 #define EMAC_CLK_ERXDC (0x1f << 5)
110 #define EMAC_CLK_ERXDC_SHIFT 5
111 #define EMAC_CLK_PIT (0x1 << 2)
112 #define EMAC_CLK_PIT_MII (0 << 2)
113 #define EMAC_CLK_PIT_RGMII (1 << 2)
114 #define EMAC_CLK_SRC (0x3 << 0)
115 #define EMAC_CLK_SRC_MII (0 << 0)
116 #define EMAC_CLK_SRC_EXT_RGMII (1 << 0)
117 #define EMAC_CLK_SRC_RGMII (2 << 0)
118
119 /* Burst length of RX and TX DMA transfers */
120 static int sunxi_emac_burst_len = BURST_LEN_DEFAULT;
121
122 /* RX / TX DMA priority. If 1, RX DMA has priority over TX DMA. */
123 static int sunxi_emac_rx_tx_pri = RX_TX_PRI_DEFAULT;
124
125 /* Pause time field in the transmitted control frame */
126 static int sunxi_emac_pause_time = PAUSE_TIME_DEFAULT;
127
128 /* Request a TX interrupt every <n> descriptors */
129 static int sunxi_emac_tx_interval = TX_INTERVAL_DEFAULT;
130
131 enum sunxi_emac_type {
132 EMAC_A83T = 1,
133 EMAC_H3,
134 };
135
136 static const struct of_compat_data compat_data[] = {
137 { "allwinner,sun8i-a83t-emac", EMAC_A83T },
138 { "allwinner,sun8i-h3-emac", EMAC_H3 },
139 { NULL }
140 };
141
142 struct sunxi_emac_bufmap {
143 bus_dmamap_t map;
144 struct mbuf *mbuf;
145 };
146
147 struct sunxi_emac_txring {
148 bus_dma_tag_t desc_tag;
149 bus_dmamap_t desc_map;
150 bus_dma_segment_t desc_dmaseg;
151 struct sunxi_emac_desc *desc_ring;
152 bus_addr_t desc_ring_paddr;
153 bus_dma_tag_t buf_tag;
154 struct sunxi_emac_bufmap buf_map[TX_DESC_COUNT];
155 u_int cur, next, queued;
156 };
157
158 struct sunxi_emac_rxring {
159 bus_dma_tag_t desc_tag;
160 bus_dmamap_t desc_map;
161 bus_dma_segment_t desc_dmaseg;
162 struct sunxi_emac_desc *desc_ring;
163 bus_addr_t desc_ring_paddr;
164 bus_dma_tag_t buf_tag;
165 struct sunxi_emac_bufmap buf_map[RX_DESC_COUNT];
166 u_int cur;
167 };
168
169 enum {
170 _RES_EMAC,
171 _RES_SYSCON,
172 _RES_NITEMS
173 };
174
175 struct sunxi_emac_softc {
176 device_t dev;
177 int phandle;
178 enum sunxi_emac_type type;
179 bus_space_tag_t bst;
180 bus_dma_tag_t dmat;
181
182 bus_space_handle_t bsh[_RES_NITEMS];
183 struct clk *clk_ahb;
184 struct clk *clk_ephy;
185 struct fdtbus_reset *rst_ahb;
186 struct fdtbus_reset *rst_ephy;
187 struct fdtbus_regulator *reg_phy;
188 struct fdtbus_gpio_pin *pin_reset;
189
190 kmutex_t mtx;
191 struct ethercom ec;
192 struct mii_data mii;
193 callout_t stat_ch;
194 void *ih;
195 u_int mdc_div_ratio_m;
196
197 struct sunxi_emac_txring tx;
198 struct sunxi_emac_rxring rx;
199 };
200
201 #define RD4(sc, reg) \
202 bus_space_read_4((sc)->bst, (sc)->bsh[_RES_EMAC], (reg))
203 #define WR4(sc, reg, val) \
204 bus_space_write_4((sc)->bst, (sc)->bsh[_RES_EMAC], (reg), (val))
205
206 #define SYSCONRD4(sc, reg) \
207 bus_space_read_4((sc)->bst, (sc)->bsh[_RES_SYSCON], (reg))
208 #define SYSCONWR4(sc, reg, val) \
209 bus_space_write_4((sc)->bst, (sc)->bsh[_RES_SYSCON], (reg), (val))
210
211 static int
212 sunxi_emac_mii_readreg(device_t dev, int phy, int reg)
213 {
214 struct sunxi_emac_softc *sc = device_private(dev);
215 int retry, val;
216
217 val = 0;
218
219 WR4(sc, EMAC_MII_CMD,
220 (sc->mdc_div_ratio_m << MDC_DIV_RATIO_M_SHIFT) |
221 (phy << PHY_ADDR_SHIFT) |
222 (reg << PHY_REG_ADDR_SHIFT) |
223 MII_BUSY);
224 for (retry = MII_BUSY_RETRY; retry > 0; retry--) {
225 if ((RD4(sc, EMAC_MII_CMD) & MII_BUSY) == 0) {
226 val = RD4(sc, EMAC_MII_DATA);
227 break;
228 }
229 delay(10);
230 }
231
232 if (retry == 0)
233 device_printf(dev, "phy read timeout, phy=%d reg=%d\n",
234 phy, reg);
235
236 return val;
237 }
238
239 static void
240 sunxi_emac_mii_writereg(device_t dev, int phy, int reg, int val)
241 {
242 struct sunxi_emac_softc *sc = device_private(dev);
243 int retry;
244
245 WR4(sc, EMAC_MII_DATA, val);
246 WR4(sc, EMAC_MII_CMD,
247 (sc->mdc_div_ratio_m << MDC_DIV_RATIO_M_SHIFT) |
248 (phy << PHY_ADDR_SHIFT) |
249 (reg << PHY_REG_ADDR_SHIFT) |
250 MII_WR | MII_BUSY);
251 for (retry = MII_BUSY_RETRY; retry > 0; retry--) {
252 if ((RD4(sc, EMAC_MII_CMD) & MII_BUSY) == 0)
253 break;
254 delay(10);
255 }
256
257 if (retry == 0)
258 device_printf(dev, "phy write timeout, phy=%d reg=%d\n",
259 phy, reg);
260 }
261
262 static void
263 sunxi_emac_update_link(struct sunxi_emac_softc *sc)
264 {
265 struct mii_data *mii = &sc->mii;
266 uint32_t val;
267
268 val = RD4(sc, EMAC_BASIC_CTL_0);
269 val &= ~(BASIC_CTL_SPEED | BASIC_CTL_DUPLEX);
270
271 if (IFM_SUBTYPE(mii->mii_media_active) == IFM_1000_T ||
272 IFM_SUBTYPE(mii->mii_media_active) == IFM_1000_SX)
273 val |= BASIC_CTL_SPEED_1000 << BASIC_CTL_SPEED_SHIFT;
274 else if (IFM_SUBTYPE(mii->mii_media_active) == IFM_100_TX)
275 val |= BASIC_CTL_SPEED_100 << BASIC_CTL_SPEED_SHIFT;
276 else
277 val |= BASIC_CTL_SPEED_10 << BASIC_CTL_SPEED_SHIFT;
278
279 if ((IFM_OPTIONS(mii->mii_media_active) & IFM_FDX) != 0)
280 val |= BASIC_CTL_DUPLEX;
281
282 WR4(sc, EMAC_BASIC_CTL_0, val);
283
284 val = RD4(sc, EMAC_RX_CTL_0);
285 val &= ~RX_FLOW_CTL_EN;
286 if ((IFM_OPTIONS(mii->mii_media_active) & IFM_ETH_RXPAUSE) != 0)
287 val |= RX_FLOW_CTL_EN;
288 WR4(sc, EMAC_RX_CTL_0, val);
289
290 val = RD4(sc, EMAC_TX_FLOW_CTL);
291 val &= ~(PAUSE_TIME|TX_FLOW_CTL_EN);
292 if ((IFM_OPTIONS(mii->mii_media_active) & IFM_ETH_TXPAUSE) != 0)
293 val |= TX_FLOW_CTL_EN;
294 if ((IFM_OPTIONS(mii->mii_media_active) & IFM_FDX) != 0)
295 val |= sunxi_emac_pause_time << PAUSE_TIME_SHIFT;
296 WR4(sc, EMAC_TX_FLOW_CTL, val);
297 }
298
299 static void
300 sunxi_emac_mii_statchg(struct ifnet *ifp)
301 {
302 struct sunxi_emac_softc * const sc = ifp->if_softc;
303
304 sunxi_emac_update_link(sc);
305 }
306
307 static void
308 sunxi_emac_dma_sync(struct sunxi_emac_softc *sc, bus_dma_tag_t dmat,
309 bus_dmamap_t map, int start, int end, int total, int flags)
310 {
311 if (end > start) {
312 bus_dmamap_sync(dmat, map, DESC_OFF(start),
313 DESC_OFF(end) - DESC_OFF(start), flags);
314 } else {
315 bus_dmamap_sync(dmat, map, DESC_OFF(start),
316 DESC_OFF(total) - DESC_OFF(start), flags);
317 if (DESC_OFF(end) - DESC_OFF(0) > 0)
318 bus_dmamap_sync(dmat, map, DESC_OFF(0),
319 DESC_OFF(end) - DESC_OFF(0), flags);
320 }
321 }
322
323 static void
324 sunxi_emac_setup_txdesc(struct sunxi_emac_softc *sc, int index, int flags,
325 bus_addr_t paddr, u_int len)
326 {
327 uint32_t status, size;
328
329 if (paddr == 0 || len == 0) {
330 status = 0;
331 size = 0;
332 --sc->tx.queued;
333 } else {
334 status = TX_DESC_CTL;
335 size = flags | len;
336 if ((index & (sunxi_emac_tx_interval - 1)) == 0)
337 size |= TX_INT_CTL;
338 ++sc->tx.queued;
339 }
340
341 sc->tx.desc_ring[index].addr = htole32((uint32_t)paddr);
342 sc->tx.desc_ring[index].size = htole32(size);
343 sc->tx.desc_ring[index].status = htole32(status);
344 }
345
346 static int
347 sunxi_emac_setup_txbuf(struct sunxi_emac_softc *sc, int index, struct mbuf *m)
348 {
349 bus_dma_segment_t *segs;
350 int error, nsegs, cur, i, flags;
351 u_int csum_flags;
352
353 error = bus_dmamap_load_mbuf(sc->tx.buf_tag,
354 sc->tx.buf_map[index].map, m, BUS_DMA_WRITE|BUS_DMA_NOWAIT);
355 if (error == EFBIG) {
356 device_printf(sc->dev,
357 "TX packet needs too many DMA segments, dropping...\n");
358 m_freem(m);
359 return 0;
360 }
361 if (error != 0)
362 return 0;
363
364 segs = sc->tx.buf_map[index].map->dm_segs;
365 nsegs = sc->tx.buf_map[index].map->dm_nsegs;
366
367 flags = TX_FIR_DESC;
368 if ((m->m_pkthdr.csum_flags & M_CSUM_IPv4) != 0) {
369 if ((m->m_pkthdr.csum_flags & (M_CSUM_TCPv4|M_CSUM_UDPv4)) != 0)
370 csum_flags = TX_CHECKSUM_CTL_FULL;
371 else
372 csum_flags = TX_CHECKSUM_CTL_IP;
373 flags |= (csum_flags << TX_CHECKSUM_CTL_SHIFT);
374 }
375
376 for (cur = index, i = 0; i < nsegs; i++) {
377 sc->tx.buf_map[cur].mbuf = (i == 0 ? m : NULL);
378 if (i == nsegs - 1)
379 flags |= TX_LAST_DESC;
380
381 sunxi_emac_setup_txdesc(sc, cur, flags, segs[i].ds_addr,
382 segs[i].ds_len);
383 flags &= ~TX_FIR_DESC;
384 cur = TX_NEXT(cur);
385 }
386
387 bus_dmamap_sync(sc->tx.buf_tag, sc->tx.buf_map[index].map,
388 0, sc->tx.buf_map[index].map->dm_mapsize, BUS_DMASYNC_PREWRITE);
389
390 return nsegs;
391 }
392
393 static void
394 sunxi_emac_setup_rxdesc(struct sunxi_emac_softc *sc, int index,
395 bus_addr_t paddr)
396 {
397 uint32_t status, size;
398
399 status = RX_DESC_CTL;
400 size = MCLBYTES - 1;
401
402 sc->rx.desc_ring[index].addr = htole32((uint32_t)paddr);
403 sc->rx.desc_ring[index].size = htole32(size);
404 sc->rx.desc_ring[index].next =
405 htole32(sc->rx.desc_ring_paddr + DESC_OFF(RX_NEXT(index)));
406 sc->rx.desc_ring[index].status = htole32(status);
407 }
408
409 static int
410 sunxi_emac_setup_rxbuf(struct sunxi_emac_softc *sc, int index, struct mbuf *m)
411 {
412 int error;
413
414 m_adj(m, ETHER_ALIGN);
415
416 error = bus_dmamap_load_mbuf(sc->rx.buf_tag,
417 sc->rx.buf_map[index].map, m, BUS_DMA_READ|BUS_DMA_NOWAIT);
418 if (error != 0)
419 return error;
420
421 bus_dmamap_sync(sc->rx.buf_tag, sc->rx.buf_map[index].map,
422 0, sc->rx.buf_map[index].map->dm_mapsize,
423 BUS_DMASYNC_PREREAD);
424
425 sc->rx.buf_map[index].mbuf = m;
426 sunxi_emac_setup_rxdesc(sc, index,
427 sc->rx.buf_map[index].map->dm_segs[0].ds_addr);
428
429 return 0;
430 }
431
432 static struct mbuf *
433 sunxi_emac_alloc_mbufcl(struct sunxi_emac_softc *sc)
434 {
435 struct mbuf *m;
436
437 m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
438 if (m != NULL)
439 m->m_pkthdr.len = m->m_len = m->m_ext.ext_size;
440
441 return m;
442 }
443
444 static void
445 sunxi_emac_start_locked(struct sunxi_emac_softc *sc)
446 {
447 struct ifnet *ifp = &sc->ec.ec_if;
448 struct mbuf *m;
449 uint32_t val;
450 int cnt, nsegs, start;
451
452 EMAC_ASSERT_LOCKED(sc);
453
454 if ((ifp->if_flags & (IFF_RUNNING | IFF_OACTIVE)) != IFF_RUNNING)
455 return;
456
457 for (cnt = 0, start = sc->tx.cur; ; cnt++) {
458 if (sc->tx.queued >= TX_DESC_COUNT - TX_MAX_SEGS) {
459 ifp->if_flags |= IFF_OACTIVE;
460 break;
461 }
462
463 IFQ_POLL(&ifp->if_snd, m);
464 if (m == NULL)
465 break;
466
467 nsegs = sunxi_emac_setup_txbuf(sc, sc->tx.cur, m);
468 if (nsegs == 0) {
469 ifp->if_flags |= IFF_OACTIVE;
470 break;
471 }
472 IFQ_DEQUEUE(&ifp->if_snd, m);
473 bpf_mtap(ifp, m);
474
475 sc->tx.cur = TX_SKIP(sc->tx.cur, nsegs);
476 }
477
478 if (cnt != 0) {
479 sunxi_emac_dma_sync(sc, sc->tx.desc_tag, sc->tx.desc_map,
480 start, sc->tx.cur, TX_DESC_COUNT,
481 BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
482
483 /* Start and run TX DMA */
484 val = RD4(sc, EMAC_TX_CTL_1);
485 WR4(sc, EMAC_TX_CTL_1, val | TX_DMA_START);
486 }
487 }
488
489 static void
490 sunxi_emac_start(struct ifnet *ifp)
491 {
492 struct sunxi_emac_softc *sc = ifp->if_softc;
493
494 EMAC_LOCK(sc);
495 sunxi_emac_start_locked(sc);
496 EMAC_UNLOCK(sc);
497 }
498
499 static void
500 sunxi_emac_tick(void *softc)
501 {
502 struct sunxi_emac_softc *sc = softc;
503 struct mii_data *mii = &sc->mii;
504 #ifndef EMAC_MPSAFE
505 int s = splnet();
506 #endif
507
508 EMAC_LOCK(sc);
509 mii_tick(mii);
510 callout_schedule(&sc->stat_ch, hz);
511 EMAC_UNLOCK(sc);
512
513 #ifndef EMAC_MPSAFE
514 splx(s);
515 #endif
516 }
517
518 /* Bit Reversal - http://aggregate.org/MAGIC/#Bit%20Reversal */
519 static uint32_t
520 bitrev32(uint32_t x)
521 {
522 x = (((x & 0xaaaaaaaa) >> 1) | ((x & 0x55555555) << 1));
523 x = (((x & 0xcccccccc) >> 2) | ((x & 0x33333333) << 2));
524 x = (((x & 0xf0f0f0f0) >> 4) | ((x & 0x0f0f0f0f) << 4));
525 x = (((x & 0xff00ff00) >> 8) | ((x & 0x00ff00ff) << 8));
526
527 return (x >> 16) | (x << 16);
528 }
529
530 static void
531 sunxi_emac_setup_rxfilter(struct sunxi_emac_softc *sc)
532 {
533 struct ifnet *ifp = &sc->ec.ec_if;
534 uint32_t val, crc, hashreg, hashbit, hash[2], machi, maclo;
535 struct ether_multi *enm;
536 struct ether_multistep step;
537 const uint8_t *eaddr;
538
539 EMAC_ASSERT_LOCKED(sc);
540
541 val = 0;
542 hash[0] = hash[1] = 0;
543
544 if ((ifp->if_flags & IFF_PROMISC) != 0)
545 val |= DIS_ADDR_FILTER;
546 else if ((ifp->if_flags & IFF_ALLMULTI) != 0) {
547 val |= RX_ALL_MULTICAST;
548 hash[0] = hash[1] = ~0;
549 } else {
550 val |= HASH_MULTICAST;
551 ETHER_LOCK(&sc->ec);
552 ETHER_FIRST_MULTI(step, &sc->ec, enm);
553 while (enm != NULL) {
554 crc = ether_crc32_le(enm->enm_addrlo, ETHER_ADDR_LEN);
555 crc &= 0x7f;
556 crc = bitrev32(~crc) >> 26;
557 hashreg = (crc >> 5);
558 hashbit = (crc & 0x1f);
559 hash[hashreg] |= (1 << hashbit);
560 ETHER_NEXT_MULTI(step, enm);
561 }
562 ETHER_UNLOCK(&sc->ec);
563 }
564
565 /* Write our unicast address */
566 eaddr = CLLADDR(ifp->if_sadl);
567 machi = (eaddr[5] << 8) | eaddr[4];
568 maclo = (eaddr[3] << 24) | (eaddr[2] << 16) | (eaddr[1] << 8) |
569 (eaddr[0] << 0);
570 WR4(sc, EMAC_ADDR_HIGH(0), machi);
571 WR4(sc, EMAC_ADDR_LOW(0), maclo);
572
573 /* Multicast hash filters */
574 WR4(sc, EMAC_RX_HASH_0, hash[1]);
575 WR4(sc, EMAC_RX_HASH_1, hash[0]);
576
577 /* RX frame filter config */
578 WR4(sc, EMAC_RX_FRM_FLT, val);
579 }
580
581 static void
582 sunxi_emac_enable_intr(struct sunxi_emac_softc *sc)
583 {
584 /* Enable interrupts */
585 WR4(sc, EMAC_INT_EN, RX_INT_EN | TX_INT_EN | TX_BUF_UA_INT_EN);
586 }
587
588 static void
589 sunxi_emac_disable_intr(struct sunxi_emac_softc *sc)
590 {
591 /* Disable interrupts */
592 WR4(sc, EMAC_INT_EN, 0);
593 }
594
595 static int
596 sunxi_emac_init_locked(struct sunxi_emac_softc *sc)
597 {
598 struct ifnet *ifp = &sc->ec.ec_if;
599 struct mii_data *mii = &sc->mii;
600 uint32_t val;
601
602 EMAC_ASSERT_LOCKED(sc);
603
604 if ((ifp->if_flags & IFF_RUNNING) != 0)
605 return 0;
606
607 sunxi_emac_setup_rxfilter(sc);
608
609 /* Configure DMA burst length and priorities */
610 val = sunxi_emac_burst_len << BASIC_CTL_BURST_LEN_SHIFT;
611 if (sunxi_emac_rx_tx_pri)
612 val |= BASIC_CTL_RX_TX_PRI;
613 WR4(sc, EMAC_BASIC_CTL_1, val);
614
615 /* Enable interrupts */
616 sunxi_emac_enable_intr(sc);
617
618 /* Enable transmit DMA */
619 val = RD4(sc, EMAC_TX_CTL_1);
620 WR4(sc, EMAC_TX_CTL_1, val | TX_DMA_EN | TX_MD | TX_NEXT_FRAME);
621
622 /* Enable receive DMA */
623 val = RD4(sc, EMAC_RX_CTL_1);
624 WR4(sc, EMAC_RX_CTL_1, val | RX_DMA_EN | RX_MD);
625
626 /* Enable transmitter */
627 val = RD4(sc, EMAC_TX_CTL_0);
628 WR4(sc, EMAC_TX_CTL_0, val | TX_EN);
629
630 /* Enable receiver */
631 val = RD4(sc, EMAC_RX_CTL_0);
632 WR4(sc, EMAC_RX_CTL_0, val | RX_EN | CHECK_CRC);
633
634 ifp->if_flags |= IFF_RUNNING;
635 ifp->if_flags &= ~IFF_OACTIVE;
636
637 mii_mediachg(mii);
638 callout_schedule(&sc->stat_ch, hz);
639
640 return 0;
641 }
642
643 static int
644 sunxi_emac_init(struct ifnet *ifp)
645 {
646 struct sunxi_emac_softc *sc = ifp->if_softc;
647 int error;
648
649 EMAC_LOCK(sc);
650 error = sunxi_emac_init_locked(sc);
651 EMAC_UNLOCK(sc);
652
653 return error;
654 }
655
656 static void
657 sunxi_emac_stop_locked(struct sunxi_emac_softc *sc, int disable)
658 {
659 struct ifnet *ifp = &sc->ec.ec_if;
660 uint32_t val;
661
662 EMAC_ASSERT_LOCKED(sc);
663
664 callout_stop(&sc->stat_ch);
665
666 mii_down(&sc->mii);
667
668 /* Stop transmit DMA and flush data in the TX FIFO */
669 val = RD4(sc, EMAC_TX_CTL_1);
670 val &= ~TX_DMA_EN;
671 val |= FLUSH_TX_FIFO;
672 WR4(sc, EMAC_TX_CTL_1, val);
673
674 /* Disable transmitter */
675 val = RD4(sc, EMAC_TX_CTL_0);
676 WR4(sc, EMAC_TX_CTL_0, val & ~TX_EN);
677
678 /* Disable receiver */
679 val = RD4(sc, EMAC_RX_CTL_0);
680 WR4(sc, EMAC_RX_CTL_0, val & ~RX_EN);
681
682 /* Disable interrupts */
683 sunxi_emac_disable_intr(sc);
684
685 /* Disable transmit DMA */
686 val = RD4(sc, EMAC_TX_CTL_1);
687 WR4(sc, EMAC_TX_CTL_1, val & ~TX_DMA_EN);
688
689 /* Disable receive DMA */
690 val = RD4(sc, EMAC_RX_CTL_1);
691 WR4(sc, EMAC_RX_CTL_1, val & ~RX_DMA_EN);
692
693 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
694 }
695
696 static void
697 sunxi_emac_stop(struct ifnet *ifp, int disable)
698 {
699 struct sunxi_emac_softc * const sc = ifp->if_softc;
700
701 EMAC_LOCK(sc);
702 sunxi_emac_stop_locked(sc, disable);
703 EMAC_UNLOCK(sc);
704 }
705
706 static int
707 sunxi_emac_rxintr(struct sunxi_emac_softc *sc)
708 {
709 struct ifnet *ifp = &sc->ec.ec_if;
710 int error, index, len, npkt;
711 struct mbuf *m, *m0;
712 uint32_t status;
713
714 npkt = 0;
715
716 for (index = sc->rx.cur; ; index = RX_NEXT(index)) {
717 sunxi_emac_dma_sync(sc, sc->rx.desc_tag, sc->rx.desc_map,
718 index, index + 1,
719 RX_DESC_COUNT, BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
720
721 status = le32toh(sc->rx.desc_ring[index].status);
722 if ((status & RX_DESC_CTL) != 0)
723 break;
724
725 bus_dmamap_sync(sc->rx.buf_tag, sc->rx.buf_map[index].map,
726 0, sc->rx.buf_map[index].map->dm_mapsize,
727 BUS_DMASYNC_POSTREAD);
728 bus_dmamap_unload(sc->rx.buf_tag, sc->rx.buf_map[index].map);
729
730 len = (status & RX_FRM_LEN) >> RX_FRM_LEN_SHIFT;
731 if (len != 0) {
732 m = sc->rx.buf_map[index].mbuf;
733 m_set_rcvif(m, ifp);
734 m->m_flags |= M_HASFCS;
735 m->m_pkthdr.len = len;
736 m->m_len = len;
737 m->m_nextpkt = NULL;
738
739 if ((ifp->if_capenable & IFCAP_CSUM_IPv4_Rx) != 0 &&
740 (status & RX_FRM_TYPE) != 0) {
741 m->m_pkthdr.csum_flags = M_CSUM_IPv4;
742 if ((status & RX_HEADER_ERR) != 0)
743 m->m_pkthdr.csum_flags |=
744 M_CSUM_IPv4_BAD;
745 if ((status & RX_PAYLOAD_ERR) == 0) {
746 m->m_pkthdr.csum_flags |=
747 M_CSUM_DATA;
748 m->m_pkthdr.csum_data = 0xffff;
749 }
750 }
751
752 ++npkt;
753
754 if_percpuq_enqueue(ifp->if_percpuq, m);
755 }
756
757 if ((m0 = sunxi_emac_alloc_mbufcl(sc)) != NULL) {
758 error = sunxi_emac_setup_rxbuf(sc, index, m0);
759 if (error != 0) {
760 /* XXX hole in RX ring */
761 }
762 } else
763 ifp->if_ierrors++;
764
765 sunxi_emac_dma_sync(sc, sc->rx.desc_tag, sc->rx.desc_map,
766 index, index + 1,
767 RX_DESC_COUNT, BUS_DMASYNC_PREWRITE|BUS_DMASYNC_PREREAD);
768 }
769
770 sc->rx.cur = index;
771
772 return npkt;
773 }
774
775 static void
776 sunxi_emac_txintr(struct sunxi_emac_softc *sc)
777 {
778 struct ifnet *ifp = &sc->ec.ec_if;
779 struct sunxi_emac_bufmap *bmap;
780 struct sunxi_emac_desc *desc;
781 uint32_t status;
782 int i;
783
784 EMAC_ASSERT_LOCKED(sc);
785
786 for (i = sc->tx.next; sc->tx.queued > 0; i = TX_NEXT(i)) {
787 KASSERT(sc->tx.queued > 0 && sc->tx.queued <= TX_DESC_COUNT);
788 sunxi_emac_dma_sync(sc, sc->tx.desc_tag, sc->tx.desc_map,
789 i, i + 1, TX_DESC_COUNT,
790 BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
791 desc = &sc->tx.desc_ring[i];
792 status = le32toh(desc->status);
793 if ((status & TX_DESC_CTL) != 0)
794 break;
795 bmap = &sc->tx.buf_map[i];
796 if (bmap->mbuf != NULL) {
797 bus_dmamap_sync(sc->tx.buf_tag, bmap->map,
798 0, bmap->map->dm_mapsize,
799 BUS_DMASYNC_POSTWRITE);
800 bus_dmamap_unload(sc->tx.buf_tag, bmap->map);
801 m_freem(bmap->mbuf);
802 bmap->mbuf = NULL;
803 }
804
805 sunxi_emac_setup_txdesc(sc, i, 0, 0, 0);
806 sunxi_emac_dma_sync(sc, sc->tx.desc_tag, sc->tx.desc_map,
807 i, i + 1, TX_DESC_COUNT,
808 BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
809
810 ifp->if_flags &= ~IFF_OACTIVE;
811 ifp->if_opackets++;
812 }
813
814 sc->tx.next = i;
815 }
816
817 static int
818 sunxi_emac_intr(void *arg)
819 {
820 struct sunxi_emac_softc *sc = arg;
821 struct ifnet *ifp = &sc->ec.ec_if;
822 uint32_t val;
823
824 EMAC_LOCK(sc);
825
826 val = RD4(sc, EMAC_INT_STA);
827 WR4(sc, EMAC_INT_STA, val);
828
829 if (val & RX_INT)
830 sunxi_emac_rxintr(sc);
831
832 if (val & (TX_INT|TX_BUF_UA_INT)) {
833 sunxi_emac_txintr(sc);
834 if_schedule_deferred_start(ifp);
835 }
836
837 EMAC_UNLOCK(sc);
838
839 return 1;
840 }
841
842 static int
843 sunxi_emac_ioctl(struct ifnet *ifp, u_long cmd, void *data)
844 {
845 struct sunxi_emac_softc *sc = ifp->if_softc;
846 struct mii_data *mii = &sc->mii;
847 struct ifreq *ifr = data;
848 int error, s;
849
850 #ifndef EMAC_MPSAFE
851 s = splnet();
852 #endif
853
854 switch (cmd) {
855 case SIOCSIFMEDIA:
856 case SIOCGIFMEDIA:
857 #ifdef EMAC_MPSAFE
858 s = splnet();
859 #endif
860 error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, cmd);
861 #ifdef EMAC_MPSAFE
862 splx(s);
863 #endif
864 break;
865 default:
866 #ifdef EMAC_MPSAFE
867 s = splnet();
868 #endif
869 error = ether_ioctl(ifp, cmd, data);
870 #ifdef EMAC_MPSAFE
871 splx(s);
872 #endif
873 if (error != ENETRESET)
874 break;
875
876 error = 0;
877
878 if (cmd == SIOCSIFCAP)
879 error = (*ifp->if_init)(ifp);
880 else if (cmd != SIOCADDMULTI && cmd != SIOCDELMULTI)
881 ;
882 else if ((ifp->if_flags & IFF_RUNNING) != 0) {
883 EMAC_LOCK(sc);
884 sunxi_emac_setup_rxfilter(sc);
885 EMAC_UNLOCK(sc);
886 }
887 break;
888 }
889
890 #ifndef EMAC_MPSAFE
891 splx(s);
892 #endif
893
894 return error;
895 }
896
897 static int
898 sunxi_emac_setup_phy(struct sunxi_emac_softc *sc)
899 {
900 uint32_t reg, tx_delay, rx_delay;
901 const char *phy_type;
902
903 phy_type = fdtbus_get_string(sc->phandle, "phy-mode");
904 if (phy_type == NULL)
905 return 0;
906
907 aprint_debug_dev(sc->dev, "PHY type: %s\n", phy_type);
908
909 reg = SYSCONRD4(sc, 0);
910
911 reg &= ~(EMAC_CLK_PIT | EMAC_CLK_SRC | EMAC_CLK_RMII_EN);
912 if (strcmp(phy_type, "rgmii") == 0)
913 reg |= EMAC_CLK_PIT_RGMII | EMAC_CLK_SRC_RGMII;
914 else if (strcmp(phy_type, "rmii") == 0)
915 reg |= EMAC_CLK_RMII_EN;
916 else
917 reg |= EMAC_CLK_PIT_MII | EMAC_CLK_SRC_MII;
918
919 if (of_getprop_uint32(sc->phandle, "tx-delay", &tx_delay) == 0) {
920 reg &= ~EMAC_CLK_ETXDC;
921 reg |= (tx_delay << EMAC_CLK_ETXDC_SHIFT);
922 }
923 if (of_getprop_uint32(sc->phandle, "rx-delay", &rx_delay) == 0) {
924 reg &= ~EMAC_CLK_ERXDC;
925 reg |= (rx_delay << EMAC_CLK_ERXDC_SHIFT);
926 }
927
928 if (sc->type == EMAC_H3) {
929 if (of_hasprop(sc->phandle, "allwinner,use-internal-phy")) {
930 reg |= EMAC_CLK_EPHY_SELECT;
931 reg &= ~EMAC_CLK_EPHY_SHUTDOWN;
932 if (of_hasprop(sc->phandle,
933 "allwinner,leds-active-low"))
934 reg |= EMAC_CLK_EPHY_LED_POL;
935 else
936 reg &= ~EMAC_CLK_EPHY_LED_POL;
937
938 /* Set internal PHY addr to 1 */
939 reg &= ~EMAC_CLK_EPHY_ADDR;
940 reg |= (1 << EMAC_CLK_EPHY_ADDR_SHIFT);
941 } else {
942 reg &= ~EMAC_CLK_EPHY_SELECT;
943 }
944 }
945
946 aprint_debug_dev(sc->dev, "EMAC clock: 0x%08x\n", reg);
947
948 SYSCONWR4(sc, 0, reg);
949
950 return 0;
951 }
952
953 static int
954 sunxi_emac_setup_resources(struct sunxi_emac_softc *sc)
955 {
956 u_int freq;
957 int error, div;
958
959 /* Configure PHY for MII or RGMII mode */
960 if (sunxi_emac_setup_phy(sc) != 0)
961 return ENXIO;
962
963 /* Enable clocks */
964 error = clk_enable(sc->clk_ahb);
965 if (error != 0) {
966 aprint_error_dev(sc->dev, "cannot enable ahb clock\n");
967 return error;
968 }
969
970 if (sc->clk_ephy != NULL) {
971 error = clk_enable(sc->clk_ephy);
972 if (error != 0) {
973 aprint_error_dev(sc->dev, "cannot enable ephy clock\n");
974 return error;
975 }
976 }
977
978 /* De-assert reset */
979 error = fdtbus_reset_deassert(sc->rst_ahb);
980 if (error != 0) {
981 aprint_error_dev(sc->dev, "cannot de-assert ahb reset\n");
982 return error;
983 }
984 if (sc->rst_ephy != NULL) {
985 error = fdtbus_reset_deassert(sc->rst_ephy);
986 if (error != 0) {
987 aprint_error_dev(sc->dev,
988 "cannot de-assert ephy reset\n");
989 return error;
990 }
991 }
992
993 /* Enable PHY regulator if applicable */
994 if (sc->reg_phy != NULL) {
995 error = fdtbus_regulator_enable(sc->reg_phy);
996 if (error != 0) {
997 aprint_error_dev(sc->dev,
998 "cannot enable PHY regulator\n");
999 return error;
1000 }
1001 }
1002
1003 /* Determine MDC clock divide ratio based on AHB clock */
1004 freq = clk_get_rate(sc->clk_ahb);
1005 if (freq == 0) {
1006 aprint_error_dev(sc->dev, "cannot get AHB clock frequency\n");
1007 return ENXIO;
1008 }
1009 div = freq / MDIO_FREQ;
1010 if (div <= 16)
1011 sc->mdc_div_ratio_m = MDC_DIV_RATIO_M_16;
1012 else if (div <= 32)
1013 sc->mdc_div_ratio_m = MDC_DIV_RATIO_M_32;
1014 else if (div <= 64)
1015 sc->mdc_div_ratio_m = MDC_DIV_RATIO_M_64;
1016 else if (div <= 128)
1017 sc->mdc_div_ratio_m = MDC_DIV_RATIO_M_128;
1018 else {
1019 aprint_error_dev(sc->dev,
1020 "cannot determine MDC clock divide ratio\n");
1021 return ENXIO;
1022 }
1023
1024 aprint_debug_dev(sc->dev, "AHB frequency %u Hz, MDC div: 0x%x\n",
1025 freq, sc->mdc_div_ratio_m);
1026
1027 return 0;
1028 }
1029
1030 static void
1031 sunxi_emac_get_eaddr(struct sunxi_emac_softc *sc, uint8_t *eaddr)
1032 {
1033 uint32_t maclo, machi;
1034 #if notyet
1035 u_char rootkey[16];
1036 #endif
1037
1038 machi = RD4(sc, EMAC_ADDR_HIGH(0)) & 0xffff;
1039 maclo = RD4(sc, EMAC_ADDR_LOW(0));
1040
1041 if (maclo == 0xffffffff && machi == 0xffff) {
1042 #if notyet
1043 /* MAC address in hardware is invalid, create one */
1044 if (aw_sid_get_rootkey(rootkey) == 0 &&
1045 (rootkey[3] | rootkey[12] | rootkey[13] | rootkey[14] |
1046 rootkey[15]) != 0) {
1047 /* MAC address is derived from the root key in SID */
1048 maclo = (rootkey[13] << 24) | (rootkey[12] << 16) |
1049 (rootkey[3] << 8) | 0x02;
1050 machi = (rootkey[15] << 8) | rootkey[14];
1051 } else {
1052 #endif
1053 /* Create one */
1054 maclo = 0x00f2 | (cprng_strong32() & 0xffff0000);
1055 machi = cprng_strong32() & 0xffff;
1056 #if notyet
1057 }
1058 #endif
1059 }
1060
1061 eaddr[0] = maclo & 0xff;
1062 eaddr[1] = (maclo >> 8) & 0xff;
1063 eaddr[2] = (maclo >> 16) & 0xff;
1064 eaddr[3] = (maclo >> 24) & 0xff;
1065 eaddr[4] = machi & 0xff;
1066 eaddr[5] = (machi >> 8) & 0xff;
1067 }
1068
1069 #ifdef SUNXI_EMAC_DEBUG
1070 static void
1071 sunxi_emac_dump_regs(struct sunxi_emac_softc *sc)
1072 {
1073 static const struct {
1074 const char *name;
1075 u_int reg;
1076 } regs[] = {
1077 { "BASIC_CTL_0", EMAC_BASIC_CTL_0 },
1078 { "BASIC_CTL_1", EMAC_BASIC_CTL_1 },
1079 { "INT_STA", EMAC_INT_STA },
1080 { "INT_EN", EMAC_INT_EN },
1081 { "TX_CTL_0", EMAC_TX_CTL_0 },
1082 { "TX_CTL_1", EMAC_TX_CTL_1 },
1083 { "TX_FLOW_CTL", EMAC_TX_FLOW_CTL },
1084 { "TX_DMA_LIST", EMAC_TX_DMA_LIST },
1085 { "RX_CTL_0", EMAC_RX_CTL_0 },
1086 { "RX_CTL_1", EMAC_RX_CTL_1 },
1087 { "RX_DMA_LIST", EMAC_RX_DMA_LIST },
1088 { "RX_FRM_FLT", EMAC_RX_FRM_FLT },
1089 { "RX_HASH_0", EMAC_RX_HASH_0 },
1090 { "RX_HASH_1", EMAC_RX_HASH_1 },
1091 { "MII_CMD", EMAC_MII_CMD },
1092 { "ADDR_HIGH0", EMAC_ADDR_HIGH(0) },
1093 { "ADDR_LOW0", EMAC_ADDR_LOW(0) },
1094 { "TX_DMA_STA", EMAC_TX_DMA_STA },
1095 { "TX_DMA_CUR_DESC", EMAC_TX_DMA_CUR_DESC },
1096 { "TX_DMA_CUR_BUF", EMAC_TX_DMA_CUR_BUF },
1097 { "RX_DMA_STA", EMAC_RX_DMA_STA },
1098 { "RX_DMA_CUR_DESC", EMAC_RX_DMA_CUR_DESC },
1099 { "RX_DMA_CUR_BUF", EMAC_RX_DMA_CUR_BUF },
1100 { "RGMII_STA", EMAC_RGMII_STA },
1101 };
1102 u_int n;
1103
1104 for (n = 0; n < __arraycount(regs); n++)
1105 device_printf(dev, " %-20s %08x\n", regs[n].name,
1106 RD4(sc, regs[n].reg));
1107 }
1108 #endif
1109
1110 static int
1111 sunxi_emac_phy_reset(struct sunxi_emac_softc *sc)
1112 {
1113 uint32_t delay_prop[3];
1114 int pin_value;
1115
1116 if (sc->pin_reset == NULL)
1117 return 0;
1118
1119 if (OF_getprop(sc->phandle, "allwinner,reset-delays-us", delay_prop,
1120 sizeof(delay_prop)) <= 0)
1121 return ENXIO;
1122
1123 pin_value = of_hasprop(sc->phandle, "allwinner,reset-active-low");
1124
1125 fdtbus_gpio_write(sc->pin_reset, pin_value);
1126 delay(htole32(delay_prop[0]));
1127 fdtbus_gpio_write(sc->pin_reset, !pin_value);
1128 delay(htole32(delay_prop[1]));
1129 fdtbus_gpio_write(sc->pin_reset, pin_value);
1130 delay(htole32(delay_prop[2]));
1131
1132 return 0;
1133 }
1134
1135 static int
1136 sunxi_emac_reset(struct sunxi_emac_softc *sc)
1137 {
1138 int retry;
1139
1140 /* Reset PHY if necessary */
1141 if (sunxi_emac_phy_reset(sc) != 0) {
1142 aprint_error_dev(sc->dev, "failed to reset PHY\n");
1143 return ENXIO;
1144 }
1145
1146 /* Soft reset all registers and logic */
1147 WR4(sc, EMAC_BASIC_CTL_1, BASIC_CTL_SOFT_RST);
1148
1149 /* Wait for soft reset bit to self-clear */
1150 for (retry = SOFT_RST_RETRY; retry > 0; retry--) {
1151 if ((RD4(sc, EMAC_BASIC_CTL_1) & BASIC_CTL_SOFT_RST) == 0)
1152 break;
1153 delay(10);
1154 }
1155 if (retry == 0) {
1156 aprint_error_dev(sc->dev, "soft reset timed out\n");
1157 #ifdef SUNXI_EMAC_DEBUG
1158 sunxi_emac_dump_regs(sc);
1159 #endif
1160 return ETIMEDOUT;
1161 }
1162
1163 return 0;
1164 }
1165
1166 static int
1167 sunxi_emac_setup_dma(struct sunxi_emac_softc *sc)
1168 {
1169 struct mbuf *m;
1170 int error, nsegs, i;
1171
1172 /* Setup TX ring */
1173 sc->tx.buf_tag = sc->tx.desc_tag = sc->dmat;
1174 error = bus_dmamap_create(sc->dmat, TX_DESC_SIZE, 1, TX_DESC_SIZE, 0,
1175 BUS_DMA_WAITOK, &sc->tx.desc_map);
1176 if (error)
1177 return error;
1178 error = bus_dmamem_alloc(sc->dmat, TX_DESC_SIZE, DESC_ALIGN, 0,
1179 &sc->tx.desc_dmaseg, 1, &nsegs, BUS_DMA_WAITOK);
1180 if (error)
1181 return error;
1182 error = bus_dmamem_map(sc->dmat, &sc->tx.desc_dmaseg, nsegs,
1183 TX_DESC_SIZE, (void *)&sc->tx.desc_ring,
1184 BUS_DMA_WAITOK);
1185 if (error)
1186 return error;
1187 error = bus_dmamap_load(sc->dmat, sc->tx.desc_map, sc->tx.desc_ring,
1188 TX_DESC_SIZE, NULL, BUS_DMA_WAITOK);
1189 if (error)
1190 return error;
1191 sc->tx.desc_ring_paddr = sc->tx.desc_map->dm_segs[0].ds_addr;
1192
1193 memset(sc->tx.desc_ring, 0, TX_DESC_SIZE);
1194 bus_dmamap_sync(sc->dmat, sc->tx.desc_map, 0, TX_DESC_SIZE,
1195 BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
1196
1197 for (i = 0; i < TX_DESC_COUNT; i++)
1198 sc->tx.desc_ring[i].next =
1199 htole32(sc->tx.desc_ring_paddr + DESC_OFF(TX_NEXT(i)));
1200
1201 sc->tx.queued = TX_DESC_COUNT;
1202 for (i = 0; i < TX_DESC_COUNT; i++) {
1203 error = bus_dmamap_create(sc->tx.buf_tag, MCLBYTES,
1204 TX_MAX_SEGS, MCLBYTES, 0, BUS_DMA_WAITOK,
1205 &sc->tx.buf_map[i].map);
1206 if (error != 0) {
1207 device_printf(sc->dev, "cannot create TX buffer map\n");
1208 return error;
1209 }
1210 sunxi_emac_setup_txdesc(sc, i, 0, 0, 0);
1211 }
1212
1213 /* Setup RX ring */
1214 sc->rx.buf_tag = sc->rx.desc_tag = sc->dmat;
1215 error = bus_dmamap_create(sc->dmat, RX_DESC_SIZE, 1, RX_DESC_SIZE, 0,
1216 BUS_DMA_WAITOK, &sc->rx.desc_map);
1217 if (error)
1218 return error;
1219 error = bus_dmamem_alloc(sc->dmat, RX_DESC_SIZE, DESC_ALIGN, 0,
1220 &sc->rx.desc_dmaseg, 1, &nsegs, BUS_DMA_WAITOK);
1221 if (error)
1222 return error;
1223 error = bus_dmamem_map(sc->dmat, &sc->rx.desc_dmaseg, nsegs,
1224 RX_DESC_SIZE, (void *)&sc->rx.desc_ring,
1225 BUS_DMA_WAITOK);
1226 if (error)
1227 return error;
1228 error = bus_dmamap_load(sc->dmat, sc->rx.desc_map, sc->rx.desc_ring,
1229 RX_DESC_SIZE, NULL, BUS_DMA_WAITOK);
1230 if (error)
1231 return error;
1232 sc->rx.desc_ring_paddr = sc->rx.desc_map->dm_segs[0].ds_addr;
1233
1234 memset(sc->rx.desc_ring, 0, RX_DESC_SIZE);
1235
1236 for (i = 0; i < RX_DESC_COUNT; i++) {
1237 error = bus_dmamap_create(sc->rx.buf_tag, MCLBYTES,
1238 RX_DESC_COUNT, MCLBYTES, 0, BUS_DMA_WAITOK,
1239 &sc->rx.buf_map[i].map);
1240 if (error != 0) {
1241 device_printf(sc->dev, "cannot create RX buffer map\n");
1242 return error;
1243 }
1244 if ((m = sunxi_emac_alloc_mbufcl(sc)) == NULL) {
1245 device_printf(sc->dev, "cannot allocate RX mbuf\n");
1246 return ENOMEM;
1247 }
1248 error = sunxi_emac_setup_rxbuf(sc, i, m);
1249 if (error != 0) {
1250 device_printf(sc->dev, "cannot create RX buffer\n");
1251 return error;
1252 }
1253 }
1254 bus_dmamap_sync(sc->rx.desc_tag, sc->rx.desc_map,
1255 0, sc->rx.desc_map->dm_mapsize,
1256 BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
1257
1258 /* Write transmit and receive descriptor base address registers */
1259 WR4(sc, EMAC_TX_DMA_LIST, sc->tx.desc_ring_paddr);
1260 WR4(sc, EMAC_RX_DMA_LIST, sc->rx.desc_ring_paddr);
1261
1262 return 0;
1263 }
1264
1265 static int
1266 sunxi_emac_get_resources(struct sunxi_emac_softc *sc)
1267 {
1268 const int phandle = sc->phandle;
1269 bus_addr_t addr, size;
1270 u_int n;
1271
1272 /* Map registers */
1273 for (n = 0; n < _RES_NITEMS; n++) {
1274 if (fdtbus_get_reg(phandle, n, &addr, &size) != 0)
1275 return ENXIO;
1276 if (bus_space_map(sc->bst, addr, size, 0, &sc->bsh[n]) != 0)
1277 return ENXIO;
1278 }
1279
1280 /* Get clocks and resets. "ahb" is required, "ephy" is optional. */
1281
1282 if ((sc->clk_ahb = fdtbus_clock_get(phandle, "ahb")) == NULL)
1283 return ENXIO;
1284 sc->clk_ephy = fdtbus_clock_get(phandle, "ephy");
1285
1286 if ((sc->rst_ahb = fdtbus_reset_get(phandle, "ahb")) == NULL)
1287 return ENXIO;
1288 sc->rst_ephy = fdtbus_reset_get(phandle, "ephy");
1289
1290 /* Regulator is optional */
1291 sc->reg_phy = fdtbus_regulator_acquire(phandle, "phy-supply");
1292
1293 /* Reset GPIO is optional */
1294 sc->pin_reset = fdtbus_gpio_acquire(sc->phandle,
1295 "allwinner,reset-gpio", GPIO_PIN_OUTPUT);
1296
1297 return 0;
1298 }
1299
1300 static int
1301 sunxi_emac_match(device_t parent, cfdata_t cf, void *aux)
1302 {
1303 struct fdt_attach_args * const faa = aux;
1304
1305 return of_match_compat_data(faa->faa_phandle, compat_data);
1306 }
1307
1308 static void
1309 sunxi_emac_attach(device_t parent, device_t self, void *aux)
1310 {
1311 struct fdt_attach_args * const faa = aux;
1312 struct sunxi_emac_softc * const sc = device_private(self);
1313 const int phandle = faa->faa_phandle;
1314 struct mii_data *mii = &sc->mii;
1315 struct ifnet *ifp = &sc->ec.ec_if;
1316 uint8_t eaddr[ETHER_ADDR_LEN];
1317 char intrstr[128];
1318
1319 sc->dev = self;
1320 sc->phandle = phandle;
1321 sc->bst = faa->faa_bst;
1322 sc->dmat = faa->faa_dmat;
1323 sc->type = of_search_compatible(phandle, compat_data)->data;
1324
1325 if (sunxi_emac_get_resources(sc) != 0) {
1326 aprint_error(": cannot allocate resources for device\n");
1327 return;
1328 }
1329 if (!fdtbus_intr_str(phandle, 0, intrstr, sizeof(intrstr))) {
1330 aprint_error(": cannot decode interrupt\n");
1331 return;
1332 }
1333
1334 mutex_init(&sc->mtx, MUTEX_DEFAULT, IPL_NET);
1335 callout_init(&sc->stat_ch, CALLOUT_FLAGS);
1336 callout_setfunc(&sc->stat_ch, sunxi_emac_tick, sc);
1337
1338 aprint_naive("\n");
1339 aprint_normal(": EMAC\n");
1340
1341 /* Setup clocks and regulators */
1342 if (sunxi_emac_setup_resources(sc) != 0)
1343 return;
1344
1345 /* Read MAC address before resetting the chip */
1346 sunxi_emac_get_eaddr(sc, eaddr);
1347
1348 /* Soft reset EMAC core */
1349 if (sunxi_emac_reset(sc) != 0)
1350 return;
1351
1352 /* Setup DMA descriptors */
1353 if (sunxi_emac_setup_dma(sc) != 0) {
1354 aprint_error_dev(self, "failed to setup DMA descriptors\n");
1355 return;
1356 }
1357
1358 /* Install interrupt handler */
1359 sc->ih = fdtbus_intr_establish(phandle, 0, IPL_NET,
1360 FDT_INTR_FLAGS, sunxi_emac_intr, sc);
1361 if (sc->ih == NULL) {
1362 aprint_error_dev(self, "failed to establish interrupt on %s\n",
1363 intrstr);
1364 return;
1365 }
1366 aprint_normal_dev(self, "interrupting on %s\n", intrstr);
1367
1368 /* Setup ethernet interface */
1369 ifp->if_softc = sc;
1370 snprintf(ifp->if_xname, IFNAMSIZ, EMAC_IFNAME, device_unit(self));
1371 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
1372 #ifdef EMAC_MPSAFE
1373 ifp->if_extflags = IFEF_MPSAFE;
1374 #endif
1375 ifp->if_start = sunxi_emac_start;
1376 ifp->if_ioctl = sunxi_emac_ioctl;
1377 ifp->if_init = sunxi_emac_init;
1378 ifp->if_stop = sunxi_emac_stop;
1379 ifp->if_capabilities = IFCAP_CSUM_IPv4_Rx |
1380 IFCAP_CSUM_IPv4_Tx |
1381 IFCAP_CSUM_TCPv4_Rx |
1382 IFCAP_CSUM_TCPv4_Tx |
1383 IFCAP_CSUM_UDPv4_Rx |
1384 IFCAP_CSUM_UDPv4_Tx;
1385 IFQ_SET_MAXLEN(&ifp->if_snd, IFQ_MAXLEN);
1386 IFQ_SET_READY(&ifp->if_snd);
1387
1388 /* 802.1Q VLAN-sized frames are supported */
1389 sc->ec.ec_capabilities |= ETHERCAP_VLAN_MTU;
1390
1391 /* Attach MII driver */
1392 sc->ec.ec_mii = mii;
1393 ifmedia_init(&mii->mii_media, 0, ether_mediachange, ether_mediastatus);
1394 mii->mii_ifp = ifp;
1395 mii->mii_readreg = sunxi_emac_mii_readreg;
1396 mii->mii_writereg = sunxi_emac_mii_writereg;
1397 mii->mii_statchg = sunxi_emac_mii_statchg;
1398 mii_attach(self, mii, 0xffffffff, MII_PHY_ANY, MII_OFFSET_ANY,
1399 MIIF_DOPAUSE);
1400
1401 if (LIST_EMPTY(&mii->mii_phys)) {
1402 aprint_error_dev(self, "no PHY found!\n");
1403 return;
1404 }
1405 ifmedia_set(&mii->mii_media, IFM_ETHER|IFM_AUTO);
1406
1407 /* Attach interface */
1408 if_attach(ifp);
1409 if_deferred_start_init(ifp, NULL);
1410
1411 /* Attach ethernet interface */
1412 ether_ifattach(ifp, eaddr);
1413 }
1414
1415 CFATTACH_DECL_NEW(sunxi_emac, sizeof(struct sunxi_emac_softc),
1416 sunxi_emac_match, sunxi_emac_attach, NULL, NULL);
1417