2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; version 2 of the License
6 * This program is distributed in the hope that it will be useful,
7 * but WITHOUT ANY WARRANTY; without even the implied warranty of
8 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9 * GNU General Public License for more details.
11 * You should have received a copy of the GNU General Public License
12 * along with this program; if not, write to the Free Software
13 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
15 * Copyright (C) 2009-2013 John Crispin <blogic@openwrt.org>
18 #include <linux/module.h>
19 #include <linux/kernel.h>
20 #include <linux/types.h>
21 #include <linux/dma-mapping.h>
22 #include <linux/init.h>
23 #include <linux/skbuff.h>
24 #include <linux/etherdevice.h>
25 #include <linux/ethtool.h>
26 #include <linux/platform_device.h>
27 #include <linux/of_device.h>
28 #include <linux/clk.h>
29 #include <linux/of_net.h>
30 #include <linux/of_mdio.h>
31 #include <linux/if_vlan.h>
32 #include <linux/reset.h>
33 #include <linux/tcp.h>
36 #include <asm/mach-ralink/ralink_regs.h>
38 #include "ralink_soc_eth.h"
39 #include "esw_rt3052.h"
41 #include "ralink_ethtool.h"
43 #define MAX_RX_LENGTH 1536
44 #define FE_RX_HLEN (NET_SKB_PAD + VLAN_ETH_HLEN + VLAN_HLEN + \
45 + NET_IP_ALIGN + ETH_FCS_LEN)
46 #define DMA_DUMMY_DESC 0xffffffff
47 #define FE_DEFAULT_MSG_ENABLE \
57 #define TX_DMA_DESP2_DEF (TX_DMA_LS0 | TX_DMA_DONE)
58 #define TX_DMA_DESP4_DEF (TX_DMA_QN(3) | TX_DMA_PN(1))
59 #define NEXT_TX_DESP_IDX(X) (((X) + 1) & (NUM_DMA_DESC - 1))
60 #define NEXT_RX_DESP_IDX(X) (((X) + 1) & (NUM_DMA_DESC - 1))
62 #define SYSC_REG_RSTCTRL 0x34
64 static int fe_msg_level = -1;
65 module_param_named(msg_level, fe_msg_level, int, 0);
66 MODULE_PARM_DESC(msg_level, "Message level (-1=defaults,0=none,...,16=all)");
68 static const u32 fe_reg_table_default[FE_REG_COUNT] = {
69 [FE_REG_PDMA_GLO_CFG] = FE_PDMA_GLO_CFG,
70 [FE_REG_PDMA_RST_CFG] = FE_PDMA_RST_CFG,
71 [FE_REG_DLY_INT_CFG] = FE_DLY_INT_CFG,
72 [FE_REG_TX_BASE_PTR0] = FE_TX_BASE_PTR0,
73 [FE_REG_TX_MAX_CNT0] = FE_TX_MAX_CNT0,
74 [FE_REG_TX_CTX_IDX0] = FE_TX_CTX_IDX0,
75 [FE_REG_TX_DTX_IDX0] = FE_TX_DTX_IDX0,
76 [FE_REG_RX_BASE_PTR0] = FE_RX_BASE_PTR0,
77 [FE_REG_RX_MAX_CNT0] = FE_RX_MAX_CNT0,
78 [FE_REG_RX_CALC_IDX0] = FE_RX_CALC_IDX0,
79 [FE_REG_RX_DRX_IDX0] = FE_RX_DRX_IDX0,
80 [FE_REG_FE_INT_ENABLE] = FE_FE_INT_ENABLE,
81 [FE_REG_FE_INT_STATUS] = FE_FE_INT_STATUS,
82 [FE_REG_FE_DMA_VID_BASE] = FE_DMA_VID0,
83 [FE_REG_FE_COUNTER_BASE] = FE_GDMA1_TX_GBCNT,
84 [FE_REG_FE_RST_GL] = FE_FE_RST_GL,
87 static const u32 *fe_reg_table = fe_reg_table_default;
91 void (*action)(struct fe_priv *);
94 static void __iomem *fe_base = 0;
96 void fe_w32(u32 val, unsigned reg)
98 __raw_writel(val, fe_base + reg);
101 u32 fe_r32(unsigned reg)
103 return __raw_readl(fe_base + reg);
106 void fe_reg_w32(u32 val, enum fe_reg reg)
108 fe_w32(val, fe_reg_table[reg]);
111 u32 fe_reg_r32(enum fe_reg reg)
113 return fe_r32(fe_reg_table[reg]);
116 void fe_reset(u32 reset_bits)
120 t = rt_sysc_r32(SYSC_REG_RSTCTRL);
122 rt_sysc_w32(t , SYSC_REG_RSTCTRL);
126 rt_sysc_w32(t, SYSC_REG_RSTCTRL);
130 static inline void fe_int_disable(u32 mask)
132 fe_reg_w32(fe_reg_r32(FE_REG_FE_INT_ENABLE) & ~mask,
133 FE_REG_FE_INT_ENABLE);
135 fe_reg_r32(FE_REG_FE_INT_ENABLE);
138 static inline void fe_int_enable(u32 mask)
140 fe_reg_w32(fe_reg_r32(FE_REG_FE_INT_ENABLE) | mask,
141 FE_REG_FE_INT_ENABLE);
143 fe_reg_r32(FE_REG_FE_INT_ENABLE);
146 static inline void fe_hw_set_macaddr(struct fe_priv *priv, unsigned char *mac)
150 spin_lock_irqsave(&priv->page_lock, flags);
151 fe_w32((mac[0] << 8) | mac[1], FE_GDMA1_MAC_ADRH);
152 fe_w32((mac[2] << 24) | (mac[3] << 16) | (mac[4] << 8) | mac[5],
154 spin_unlock_irqrestore(&priv->page_lock, flags);
157 static int fe_set_mac_address(struct net_device *dev, void *p)
159 int ret = eth_mac_addr(dev, p);
162 struct fe_priv *priv = netdev_priv(dev);
164 if (priv->soc->set_mac)
165 priv->soc->set_mac(priv, dev->dev_addr);
167 fe_hw_set_macaddr(priv, p);
173 static inline int fe_max_frag_size(int mtu)
175 return SKB_DATA_ALIGN(FE_RX_HLEN + mtu) +
176 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
179 static inline int fe_max_buf_size(int frag_size)
181 return frag_size - NET_SKB_PAD - NET_IP_ALIGN -
182 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
185 static inline void fe_get_rxd(struct fe_rx_dma *rxd, struct fe_rx_dma *dma_rxd)
187 rxd->rxd1 = dma_rxd->rxd1;
188 rxd->rxd2 = dma_rxd->rxd2;
189 rxd->rxd3 = dma_rxd->rxd3;
190 rxd->rxd4 = dma_rxd->rxd4;
193 static inline void fe_get_txd(struct fe_tx_dma *txd, struct fe_tx_dma *dma_txd)
195 txd->txd1 = dma_txd->txd1;
196 txd->txd2 = dma_txd->txd2;
197 txd->txd3 = dma_txd->txd3;
198 txd->txd4 = dma_txd->txd4;
201 static inline void fe_set_txd(struct fe_tx_dma *txd, struct fe_tx_dma *dma_txd)
203 dma_txd->txd1 = txd->txd1;
204 dma_txd->txd3 = txd->txd3;
205 dma_txd->txd4 = txd->txd4;
206 /* clean dma done flag last */
207 dma_txd->txd2 = txd->txd2;
210 static void fe_clean_rx(struct fe_priv *priv)
215 for (i = 0; i < NUM_DMA_DESC; i++)
216 if (priv->rx_data[i]) {
217 if (priv->rx_dma && priv->rx_dma[i].rxd1)
218 dma_unmap_single(&priv->netdev->dev,
219 priv->rx_dma[i].rxd1,
222 put_page(virt_to_head_page(priv->rx_data[i]));
225 kfree(priv->rx_data);
226 priv->rx_data = NULL;
230 dma_free_coherent(&priv->netdev->dev,
231 NUM_DMA_DESC * sizeof(*priv->rx_dma),
238 static int fe_alloc_rx(struct fe_priv *priv)
240 struct net_device *netdev = priv->netdev;
243 priv->rx_data = kcalloc(NUM_DMA_DESC, sizeof(*priv->rx_data),
248 for (i = 0; i < NUM_DMA_DESC; i++) {
249 priv->rx_data[i] = netdev_alloc_frag(priv->frag_size);
250 if (!priv->rx_data[i])
254 priv->rx_dma = dma_alloc_coherent(&netdev->dev,
255 NUM_DMA_DESC * sizeof(*priv->rx_dma),
257 GFP_ATOMIC | __GFP_ZERO);
261 if (priv->flags & FE_FLAG_RX_2B_OFFSET)
265 for (i = 0; i < NUM_DMA_DESC; i++) {
266 dma_addr_t dma_addr = dma_map_single(&netdev->dev,
267 priv->rx_data[i] + NET_SKB_PAD + pad,
270 if (unlikely(dma_mapping_error(&netdev->dev, dma_addr)))
272 priv->rx_dma[i].rxd1 = (unsigned int) dma_addr;
274 if (priv->flags & FE_FLAG_RX_SG_DMA)
275 priv->rx_dma[i].rxd2 = RX_DMA_PLEN0(priv->rx_buf_size);
277 priv->rx_dma[i].rxd2 = RX_DMA_LSO;
281 fe_reg_w32(priv->rx_phys, FE_REG_RX_BASE_PTR0);
282 fe_reg_w32(NUM_DMA_DESC, FE_REG_RX_MAX_CNT0);
283 fe_reg_w32((NUM_DMA_DESC - 1), FE_REG_RX_CALC_IDX0);
284 fe_reg_w32(FE_PST_DRX_IDX0, FE_REG_PDMA_RST_CFG);
292 static void fe_clean_tx(struct fe_priv *priv)
297 for (i = 0; i < NUM_DMA_DESC; i++) {
299 dev_kfree_skb_any(priv->tx_skb[i]);
306 dma_free_coherent(&priv->netdev->dev,
307 NUM_DMA_DESC * sizeof(*priv->tx_dma),
314 static int fe_alloc_tx(struct fe_priv *priv)
318 priv->tx_free_idx = 0;
320 priv->tx_skb = kcalloc(NUM_DMA_DESC, sizeof(*priv->tx_skb),
325 priv->tx_dma = dma_alloc_coherent(&priv->netdev->dev,
326 NUM_DMA_DESC * sizeof(*priv->tx_dma),
328 GFP_ATOMIC | __GFP_ZERO);
332 for (i = 0; i < NUM_DMA_DESC; i++) {
333 if (priv->soc->tx_dma) {
334 priv->soc->tx_dma(&priv->tx_dma[i]);
337 priv->tx_dma[i].txd2 = TX_DMA_DESP2_DEF;
341 fe_reg_w32(priv->tx_phys, FE_REG_TX_BASE_PTR0);
342 fe_reg_w32(NUM_DMA_DESC, FE_REG_TX_MAX_CNT0);
343 fe_reg_w32(0, FE_REG_TX_CTX_IDX0);
344 fe_reg_w32(FE_PST_DTX_IDX0, FE_REG_PDMA_RST_CFG);
352 static int fe_init_dma(struct fe_priv *priv)
356 err = fe_alloc_tx(priv);
360 err = fe_alloc_rx(priv);
367 static void fe_free_dma(struct fe_priv *priv)
372 netdev_reset_queue(priv->netdev);
375 static inline void txd_unmap_single(struct device *dev, struct fe_tx_dma *txd)
377 if (txd->txd1 && TX_DMA_GET_PLEN0(txd->txd2))
378 dma_unmap_single(dev, txd->txd1,
379 TX_DMA_GET_PLEN0(txd->txd2),
383 static inline void txd_unmap_page0(struct device *dev, struct fe_tx_dma *txd)
385 if (txd->txd1 && TX_DMA_GET_PLEN0(txd->txd2))
386 dma_unmap_page(dev, txd->txd1,
387 TX_DMA_GET_PLEN0(txd->txd2),
391 static inline void txd_unmap_page1(struct device *dev, struct fe_tx_dma *txd)
393 if (txd->txd3 && TX_DMA_GET_PLEN1(txd->txd2))
394 dma_unmap_page(dev, txd->txd3,
395 TX_DMA_GET_PLEN1(txd->txd2),
399 void fe_stats_update(struct fe_priv *priv)
401 struct fe_hw_stats *hwstats = priv->hw_stats;
402 unsigned int base = fe_reg_table[FE_REG_FE_COUNTER_BASE];
404 u64_stats_update_begin(&hwstats->syncp);
406 if (IS_ENABLED(CONFIG_SOC_MT7621)) {
407 hwstats->rx_bytes += fe_r32(base);
408 hwstats->rx_packets += fe_r32(base + 0x08);
409 hwstats->rx_overflow += fe_r32(base + 0x10);
410 hwstats->rx_fcs_errors += fe_r32(base + 0x14);
411 hwstats->rx_short_errors += fe_r32(base + 0x18);
412 hwstats->rx_long_errors += fe_r32(base + 0x1c);
413 hwstats->rx_checksum_errors += fe_r32(base + 0x20);
414 hwstats->rx_flow_control_packets += fe_r32(base + 0x24);
415 hwstats->tx_skip += fe_r32(base + 0x28);
416 hwstats->tx_collisions += fe_r32(base + 0x2c);
417 hwstats->tx_bytes += fe_r32(base + 0x30);
418 hwstats->tx_packets += fe_r32(base + 0x38);
420 hwstats->tx_bytes += fe_r32(base);
421 hwstats->tx_packets += fe_r32(base + 0x04);
422 hwstats->tx_skip += fe_r32(base + 0x08);
423 hwstats->tx_collisions += fe_r32(base + 0x0c);
424 hwstats->rx_bytes += fe_r32(base + 0x20);
425 hwstats->rx_packets += fe_r32(base + 0x24);
426 hwstats->rx_overflow += fe_r32(base + 0x28);
427 hwstats->rx_fcs_errors += fe_r32(base + 0x2c);
428 hwstats->rx_short_errors += fe_r32(base + 0x30);
429 hwstats->rx_long_errors += fe_r32(base + 0x34);
430 hwstats->rx_checksum_errors += fe_r32(base + 0x38);
431 hwstats->rx_flow_control_packets += fe_r32(base + 0x3c);
434 u64_stats_update_end(&hwstats->syncp);
437 static struct rtnl_link_stats64 *fe_get_stats64(struct net_device *dev,
438 struct rtnl_link_stats64 *storage)
440 struct fe_priv *priv = netdev_priv(dev);
441 struct fe_hw_stats *hwstats = priv->hw_stats;
442 unsigned int base = fe_reg_table[FE_REG_FE_COUNTER_BASE];
446 netdev_stats_to_stats64(storage, &dev->stats);
450 if (netif_running(dev) && netif_device_present(dev)) {
451 if (spin_trylock(&hwstats->stats_lock)) {
452 fe_stats_update(priv);
453 spin_unlock(&hwstats->stats_lock);
458 start = u64_stats_fetch_begin_bh(&hwstats->syncp);
459 storage->rx_packets = hwstats->rx_packets;
460 storage->tx_packets = hwstats->tx_packets;
461 storage->rx_bytes = hwstats->rx_bytes;
462 storage->tx_bytes = hwstats->tx_bytes;
463 storage->collisions = hwstats->tx_collisions;
464 storage->rx_length_errors = hwstats->rx_short_errors +
465 hwstats->rx_long_errors;
466 storage->rx_over_errors = hwstats->rx_overflow;
467 storage->rx_crc_errors = hwstats->rx_fcs_errors;
468 storage->rx_errors = hwstats->rx_checksum_errors;
469 storage->tx_aborted_errors = hwstats->tx_skip;
470 } while (u64_stats_fetch_retry_bh(&hwstats->syncp, start));
472 storage->tx_errors = priv->netdev->stats.tx_errors;
473 storage->rx_dropped = priv->netdev->stats.rx_dropped;
474 storage->tx_dropped = priv->netdev->stats.tx_dropped;
479 static int fe_vlan_rx_add_vid(struct net_device *dev,
480 __be16 proto, u16 vid)
482 struct fe_priv *priv = netdev_priv(dev);
483 u32 idx = (vid & 0xf);
486 if (!((fe_reg_table[FE_REG_FE_DMA_VID_BASE]) &&
487 (dev->features & NETIF_F_HW_VLAN_CTAG_TX)))
490 if (test_bit(idx, &priv->vlan_map)) {
491 netdev_warn(dev, "disable tx vlan offload\n");
492 dev->wanted_features &= ~NETIF_F_HW_VLAN_CTAG_TX;
493 netdev_update_features(dev);
495 vlan_cfg = fe_r32(fe_reg_table[FE_REG_FE_DMA_VID_BASE] +
499 vlan_cfg |= (vid << 16);
501 vlan_cfg &= 0xffff0000;
504 fe_w32(vlan_cfg, fe_reg_table[FE_REG_FE_DMA_VID_BASE] +
506 set_bit(idx, &priv->vlan_map);
512 static int fe_vlan_rx_kill_vid(struct net_device *dev,
513 __be16 proto, u16 vid)
515 struct fe_priv *priv = netdev_priv(dev);
516 u32 idx = (vid & 0xf);
518 if (!((fe_reg_table[FE_REG_FE_DMA_VID_BASE]) &&
519 (dev->features | NETIF_F_HW_VLAN_CTAG_TX)))
522 clear_bit(idx, &priv->vlan_map);
527 static int fe_tx_map_dma(struct sk_buff *skb, struct net_device *dev,
530 struct fe_priv *priv = netdev_priv(dev);
531 struct skb_frag_struct *frag;
532 struct fe_tx_dma txd, *ptxd;
533 dma_addr_t mapped_addr;
534 unsigned int nr_frags;
536 int i, j, unmap_idx, tx_num;
538 memset(&txd, 0, sizeof(txd));
539 nr_frags = skb_shinfo(skb)->nr_frags;
540 tx_num = 1 + (nr_frags >> 1);
542 /* init tx descriptor */
543 if (priv->soc->tx_dma)
544 priv->soc->tx_dma(&txd);
546 txd.txd4 = TX_DMA_DESP4_DEF;
549 /* use dma_unmap_single to free it */
550 txd.txd4 |= priv->soc->tx_udf_bit;
552 /* TX Checksum offload */
553 if (skb->ip_summed == CHECKSUM_PARTIAL)
554 txd.txd4 |= TX_DMA_CHKSUM;
556 /* VLAN header offload */
557 if (vlan_tx_tag_present(skb)) {
558 if (IS_ENABLED(CONFIG_SOC_MT7621))
559 txd.txd4 |= TX_DMA_INS_VLAN_MT7621 | vlan_tx_tag_get(skb);
561 txd.txd4 |= TX_DMA_INS_VLAN |
562 ((vlan_tx_tag_get(skb) >> VLAN_PRIO_SHIFT) << 4) |
563 (vlan_tx_tag_get(skb) & 0xF);
566 /* TSO: fill MSS info in tcp checksum field */
567 if (skb_is_gso(skb)) {
568 if (skb_cow_head(skb, 0)) {
569 netif_warn(priv, tx_err, dev,
570 "GSO expand head fail.\n");
573 if (skb_shinfo(skb)->gso_type &
574 (SKB_GSO_TCPV4 | SKB_GSO_TCPV6)) {
575 txd.txd4 |= TX_DMA_TSO;
576 tcp_hdr(skb)->check = htons(skb_shinfo(skb)->gso_size);
580 mapped_addr = dma_map_single(&dev->dev, skb->data,
581 skb_headlen(skb), DMA_TO_DEVICE);
582 if (unlikely(dma_mapping_error(&dev->dev, mapped_addr)))
584 txd.txd1 = mapped_addr;
585 txd.txd2 = TX_DMA_PLEN0(skb_headlen(skb));
589 for (i = 0; i < nr_frags; i++) {
591 frag = &skb_shinfo(skb)->frags[i];
592 mapped_addr = skb_frag_dma_map(&dev->dev, frag, 0,
593 skb_frag_size(frag), DMA_TO_DEVICE);
594 if (unlikely(dma_mapping_error(&dev->dev, mapped_addr)))
598 j = NEXT_TX_DESP_IDX(j);
599 txd.txd1 = mapped_addr;
600 txd.txd2 = TX_DMA_PLEN0(frag->size);
603 txd.txd3 = mapped_addr;
604 txd.txd2 |= TX_DMA_PLEN1(frag->size);
605 if (i != (nr_frags -1)) {
606 fe_set_txd(&txd, &priv->tx_dma[j]);
607 memset(&txd, 0, sizeof(txd));
609 priv->tx_skb[j] = (struct sk_buff *) DMA_DUMMY_DESC;
613 /* set last segment */
615 txd.txd2 |= TX_DMA_LS1;
617 txd.txd2 |= TX_DMA_LS0;
618 fe_set_txd(&txd, &priv->tx_dma[j]);
620 /* store skb to cleanup */
621 priv->tx_skb[j] = skb;
623 netdev_sent_queue(dev, skb->len);
624 skb_tx_timestamp(skb);
627 j = NEXT_TX_DESP_IDX(j);
628 fe_reg_w32(j, FE_REG_TX_CTX_IDX0);
634 ptxd = &priv->tx_dma[idx];
635 txd_unmap_single(&dev->dev, ptxd);
639 for (i = 0; i < unmap_idx; i++) {
641 j = NEXT_TX_DESP_IDX(j);
642 ptxd = &priv->tx_dma[j];
643 txd_unmap_page0(&dev->dev, ptxd);
645 txd_unmap_page1(&dev->dev, ptxd);
650 /* reinit descriptors and skb */
652 for (i = 0; i < tx_num; i++) {
653 priv->tx_dma[j].txd2 = TX_DMA_DESP2_DEF;
654 priv->tx_skb[j] = NULL;
655 j = NEXT_TX_DESP_IDX(j);
662 static inline int fe_skb_padto(struct sk_buff *skb, struct fe_priv *priv) {
667 if (unlikely(skb->len < VLAN_ETH_ZLEN)) {
668 if ((priv->flags & FE_FLAG_PADDING_64B) &&
669 !(priv->flags & FE_FLAG_PADDING_BUG))
672 if (vlan_tx_tag_present(skb))
674 else if (skb->protocol == cpu_to_be16(ETH_P_8021Q))
676 else if(!(priv->flags & FE_FLAG_PADDING_64B))
681 if (skb->len < len) {
682 if ((ret = skb_pad(skb, len - skb->len)) < 0)
685 skb_set_tail_pointer(skb, len);
692 static inline u32 fe_empty_txd(struct fe_priv *priv, u32 tx_fill_idx)
694 return (u32)(NUM_DMA_DESC - ((tx_fill_idx - priv->tx_free_idx) &
695 (NUM_DMA_DESC - 1)));
698 static int fe_start_xmit(struct sk_buff *skb, struct net_device *dev)
700 struct fe_priv *priv = netdev_priv(dev);
701 struct net_device_stats *stats = &dev->stats;
706 if (fe_skb_padto(skb, priv)) {
707 netif_warn(priv, tx_err, dev, "tx padding failed!\n");
711 tx_num = 1 + (skb_shinfo(skb)->nr_frags >> 1);
712 tx = fe_reg_r32(FE_REG_TX_CTX_IDX0);
713 if (unlikely(fe_empty_txd(priv, tx) <= tx_num))
715 netif_stop_queue(dev);
716 netif_err(priv, tx_queued,dev,
717 "Tx Ring full when queue awake!\n");
718 return NETDEV_TX_BUSY;
721 if (fe_tx_map_dma(skb, dev, tx) < 0) {
727 stats->tx_bytes += len;
733 static inline void fe_rx_vlan(struct sk_buff *skb)
738 if (!__vlan_get_tag(skb, &vlanid)) {
739 /* pop the vlan tag */
740 ehdr = (struct ethhdr *)skb->data;
741 memmove(skb->data + VLAN_HLEN, ehdr, ETH_ALEN * 2);
742 skb_pull(skb, VLAN_HLEN);
743 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), vlanid);
747 static int fe_poll_rx(struct napi_struct *napi, int budget,
748 struct fe_priv *priv)
750 struct net_device *netdev = priv->netdev;
751 struct net_device_stats *stats = &netdev->stats;
752 struct fe_soc_data *soc = priv->soc;
754 int idx = fe_reg_r32(FE_REG_RX_CALC_IDX0);
757 struct fe_rx_dma *rxd, trxd;
759 bool rx_vlan = netdev->features & NETIF_F_HW_VLAN_CTAG_RX;
761 if (netdev->features & NETIF_F_RXCSUM)
762 checksum_bit = soc->checksum_bit;
766 if (priv->flags & FE_FLAG_RX_2B_OFFSET)
770 while (done < budget) {
773 idx = NEXT_RX_DESP_IDX(idx);
774 rxd = &priv->rx_dma[idx];
775 data = priv->rx_data[idx];
777 fe_get_rxd(&trxd, rxd);
778 if (!(trxd.rxd2 & RX_DMA_DONE))
781 /* alloc new buffer */
782 new_data = netdev_alloc_frag(priv->frag_size);
783 if (unlikely(!new_data)) {
787 dma_addr = dma_map_single(&netdev->dev,
788 new_data + NET_SKB_PAD + pad,
791 if (unlikely(dma_mapping_error(&netdev->dev, dma_addr))) {
792 put_page(virt_to_head_page(new_data));
797 skb = build_skb(data, priv->frag_size);
798 if (unlikely(!skb)) {
799 put_page(virt_to_head_page(new_data));
802 skb_reserve(skb, NET_SKB_PAD + NET_IP_ALIGN);
804 dma_unmap_single(&netdev->dev, trxd.rxd1,
805 priv->rx_buf_size, DMA_FROM_DEVICE);
806 pktlen = RX_DMA_PLEN0(trxd.rxd2);
808 skb_put(skb, pktlen);
809 if (trxd.rxd4 & checksum_bit) {
810 skb->ip_summed = CHECKSUM_UNNECESSARY;
812 skb_checksum_none_assert(skb);
816 skb->protocol = eth_type_trans(skb, netdev);
819 stats->rx_bytes += pktlen;
821 if (skb->ip_summed == CHECKSUM_NONE)
822 netif_receive_skb(skb);
824 napi_gro_receive(napi, skb);
826 priv->rx_data[idx] = new_data;
827 rxd->rxd1 = (unsigned int) dma_addr;
830 if (priv->flags & FE_FLAG_RX_SG_DMA)
831 rxd->rxd2 = RX_DMA_PLEN0(priv->rx_buf_size);
833 rxd->rxd2 = RX_DMA_LSO;
836 fe_reg_w32(idx, FE_REG_RX_CALC_IDX0);
843 static int fe_poll_tx(struct fe_priv *priv, int budget)
845 struct net_device *netdev = priv->netdev;
846 struct device *dev = &netdev->dev;
847 unsigned int bytes_compl = 0;
849 struct fe_tx_dma txd;
851 u32 udf_bit = priv->soc->tx_udf_bit;
853 idx = priv->tx_free_idx;
854 while (done < budget) {
855 fe_get_txd(&txd, &priv->tx_dma[idx]);
856 skb = priv->tx_skb[idx];
858 if (!(txd.txd2 & TX_DMA_DONE) || !skb)
861 txd_unmap_page1(dev, &txd);
863 if (txd.txd4 & udf_bit)
864 txd_unmap_single(dev, &txd);
866 txd_unmap_page0(dev, &txd);
868 if (skb != (struct sk_buff *) DMA_DUMMY_DESC) {
869 bytes_compl += skb->len;
870 dev_kfree_skb_any(skb);
873 priv->tx_skb[idx] = NULL;
874 idx = NEXT_TX_DESP_IDX(idx);
876 priv->tx_free_idx = idx;
881 netdev_completed_queue(netdev, done, bytes_compl);
882 if (unlikely(netif_queue_stopped(netdev) &&
883 netif_carrier_ok(netdev))) {
884 netif_wake_queue(netdev);
890 static int fe_poll(struct napi_struct *napi, int budget)
892 struct fe_priv *priv = container_of(napi, struct fe_priv, rx_napi);
893 struct fe_hw_stats *hwstat = priv->hw_stats;
894 int tx_done, rx_done;
896 u32 tx_intr, rx_intr;
898 status = fe_reg_r32(FE_REG_FE_INT_STATUS);
899 tx_intr = priv->soc->tx_int;
900 rx_intr = priv->soc->rx_int;
901 tx_done = rx_done = 0;
904 if (status & tx_intr) {
905 tx_done += fe_poll_tx(priv, budget - tx_done);
906 if (tx_done < budget) {
907 fe_reg_w32(tx_intr, FE_REG_FE_INT_STATUS);
909 status = fe_reg_r32(FE_REG_FE_INT_STATUS);
912 if (status & rx_intr) {
913 rx_done += fe_poll_rx(napi, budget - rx_done, priv);
914 if (rx_done < budget) {
915 fe_reg_w32(rx_intr, FE_REG_FE_INT_STATUS);
919 if (unlikely(hwstat && (status & FE_CNT_GDM_AF))) {
920 if (spin_trylock(&hwstat->stats_lock)) {
921 fe_stats_update(priv);
922 spin_unlock(&hwstat->stats_lock);
924 fe_reg_w32(FE_CNT_GDM_AF, FE_REG_FE_INT_STATUS);
927 if (unlikely(netif_msg_intr(priv))) {
928 mask = fe_reg_r32(FE_REG_FE_INT_ENABLE);
929 netdev_info(priv->netdev,
930 "done tx %d, rx %d, intr 0x%08x/0x%x\n",
931 tx_done, rx_done, status, mask);
934 if ((tx_done < budget) && (rx_done < budget)) {
935 status = fe_reg_r32(FE_REG_FE_INT_STATUS);
936 if (status & (tx_intr | rx_intr )) {
940 fe_int_enable(tx_intr | rx_intr);
946 static void fe_tx_timeout(struct net_device *dev)
948 struct fe_priv *priv = netdev_priv(dev);
950 priv->netdev->stats.tx_errors++;
951 netif_err(priv, tx_err, dev,
952 "transmit timed out\n");
953 netif_info(priv, drv, dev, "dma_cfg:%08x\n",
954 fe_reg_r32(FE_REG_PDMA_GLO_CFG));
955 netif_info(priv, drv, dev, "tx_ring=%d, " \
956 "base=%08x, max=%u, ctx=%u, dtx=%u, fdx=%d\n", 0,
957 fe_reg_r32(FE_REG_TX_BASE_PTR0),
958 fe_reg_r32(FE_REG_TX_MAX_CNT0),
959 fe_reg_r32(FE_REG_TX_CTX_IDX0),
960 fe_reg_r32(FE_REG_TX_DTX_IDX0),
963 netif_info(priv, drv, dev, "rx_ring=%d, " \
964 "base=%08x, max=%u, calc=%u, drx=%u\n", 0,
965 fe_reg_r32(FE_REG_RX_BASE_PTR0),
966 fe_reg_r32(FE_REG_RX_MAX_CNT0),
967 fe_reg_r32(FE_REG_RX_CALC_IDX0),
968 fe_reg_r32(FE_REG_RX_DRX_IDX0)
971 if (!test_and_set_bit(FE_FLAG_RESET_PENDING, priv->pending_flags))
972 schedule_work(&priv->pending_work);
975 static irqreturn_t fe_handle_irq(int irq, void *dev)
977 struct fe_priv *priv = netdev_priv(dev);
978 u32 status, int_mask;
980 status = fe_reg_r32(FE_REG_FE_INT_STATUS);
982 if (unlikely(!status))
985 int_mask = (priv->soc->rx_int | priv->soc->tx_int);
986 if (likely(status & int_mask)) {
987 fe_int_disable(int_mask);
988 napi_schedule(&priv->rx_napi);
990 fe_reg_w32(status, FE_REG_FE_INT_STATUS);
996 #ifdef CONFIG_NET_POLL_CONTROLLER
997 static void fe_poll_controller(struct net_device *dev)
999 struct fe_priv *priv = netdev_priv(dev);
1000 u32 int_mask = priv->soc->tx_int | priv->soc->rx_int;
1002 fe_int_disable(int_mask);
1003 fe_handle_irq(dev->irq, dev);
1004 fe_int_enable(int_mask);
1008 int fe_set_clock_cycle(struct fe_priv *priv)
1010 unsigned long sysclk = priv->sysclk;
1016 sysclk /= FE_US_CYC_CNT_DIVISOR;
1017 sysclk <<= FE_US_CYC_CNT_SHIFT;
1019 fe_w32((fe_r32(FE_FE_GLO_CFG) &
1020 ~(FE_US_CYC_CNT_MASK << FE_US_CYC_CNT_SHIFT)) |
1026 void fe_fwd_config(struct fe_priv *priv)
1030 fwd_cfg = fe_r32(FE_GDMA1_FWD_CFG);
1032 /* disable jumbo frame */
1033 if (priv->flags & FE_FLAG_JUMBO_FRAME)
1034 fwd_cfg &= ~FE_GDM1_JMB_EN;
1036 /* set unicast/multicast/broadcast frame to cpu */
1039 fe_w32(fwd_cfg, FE_GDMA1_FWD_CFG);
1042 static void fe_rxcsum_config(bool enable)
1045 fe_w32(fe_r32(FE_GDMA1_FWD_CFG) | (FE_GDM1_ICS_EN |
1046 FE_GDM1_TCS_EN | FE_GDM1_UCS_EN),
1049 fe_w32(fe_r32(FE_GDMA1_FWD_CFG) & ~(FE_GDM1_ICS_EN |
1050 FE_GDM1_TCS_EN | FE_GDM1_UCS_EN),
1054 static void fe_txcsum_config(bool enable)
1057 fe_w32(fe_r32(FE_CDMA_CSG_CFG) | (FE_ICS_GEN_EN |
1058 FE_TCS_GEN_EN | FE_UCS_GEN_EN),
1061 fe_w32(fe_r32(FE_CDMA_CSG_CFG) & ~(FE_ICS_GEN_EN |
1062 FE_TCS_GEN_EN | FE_UCS_GEN_EN),
1066 void fe_csum_config(struct fe_priv *priv)
1068 struct net_device *dev = priv_netdev(priv);
1070 fe_txcsum_config((dev->features & NETIF_F_IP_CSUM));
1071 fe_rxcsum_config((dev->features & NETIF_F_RXCSUM));
1074 static int fe_hw_init(struct net_device *dev)
1076 struct fe_priv *priv = netdev_priv(dev);
1079 err = devm_request_irq(priv->device, dev->irq, fe_handle_irq, 0,
1080 dev_name(priv->device), dev);
1084 if (priv->soc->set_mac)
1085 priv->soc->set_mac(priv, dev->dev_addr);
1087 fe_hw_set_macaddr(priv, dev->dev_addr);
1089 fe_int_disable(priv->soc->tx_int | priv->soc->rx_int);
1091 /* frame engine will push VLAN tag regarding to VIDX feild in Tx desc. */
1092 if (fe_reg_table[FE_REG_FE_DMA_VID_BASE])
1093 for (i = 0; i < 16; i += 2)
1094 fe_w32(((i + 1) << 16) + i,
1095 fe_reg_table[FE_REG_FE_DMA_VID_BASE] +
1098 BUG_ON(!priv->soc->fwd_config);
1099 if (priv->soc->fwd_config(priv))
1100 netdev_err(dev, "unable to get clock\n");
1102 if (fe_reg_table[FE_REG_FE_RST_GL]) {
1103 fe_reg_w32(1, FE_REG_FE_RST_GL);
1104 fe_reg_w32(0, FE_REG_FE_RST_GL);
1110 static int fe_open(struct net_device *dev)
1112 struct fe_priv *priv = netdev_priv(dev);
1113 unsigned long flags;
1117 err = fe_init_dma(priv);
1121 spin_lock_irqsave(&priv->page_lock, flags);
1122 napi_enable(&priv->rx_napi);
1124 val = FE_TX_WB_DDONE | FE_RX_DMA_EN | FE_TX_DMA_EN;
1125 if (priv->flags & FE_FLAG_RX_2B_OFFSET)
1126 val |= FE_RX_2B_OFFSET;
1127 val |= priv->soc->pdma_glo_cfg;
1128 fe_reg_w32(val, FE_REG_PDMA_GLO_CFG);
1130 spin_unlock_irqrestore(&priv->page_lock, flags);
1133 priv->phy->start(priv);
1135 if (priv->soc->has_carrier && priv->soc->has_carrier(priv))
1136 netif_carrier_on(dev);
1138 netif_start_queue(dev);
1139 fe_int_enable(priv->soc->tx_int | priv->soc->rx_int);
1148 static int fe_stop(struct net_device *dev)
1150 struct fe_priv *priv = netdev_priv(dev);
1151 unsigned long flags;
1154 fe_int_disable(priv->soc->tx_int | priv->soc->rx_int);
1156 netif_tx_disable(dev);
1159 priv->phy->stop(priv);
1161 spin_lock_irqsave(&priv->page_lock, flags);
1162 napi_disable(&priv->rx_napi);
1164 fe_reg_w32(fe_reg_r32(FE_REG_PDMA_GLO_CFG) &
1165 ~(FE_TX_WB_DDONE | FE_RX_DMA_EN | FE_TX_DMA_EN),
1166 FE_REG_PDMA_GLO_CFG);
1167 spin_unlock_irqrestore(&priv->page_lock, flags);
1170 for (i = 0; i < 10; i++) {
1171 if (fe_reg_r32(FE_REG_PDMA_GLO_CFG) &
1172 (FE_TX_DMA_BUSY | FE_RX_DMA_BUSY)) {
1184 static int __init fe_init(struct net_device *dev)
1186 struct fe_priv *priv = netdev_priv(dev);
1187 struct device_node *port;
1190 BUG_ON(!priv->soc->reset_fe);
1191 priv->soc->reset_fe();
1193 if (priv->soc->switch_init)
1194 priv->soc->switch_init(priv);
1196 memcpy(dev->dev_addr, priv->soc->mac, ETH_ALEN);
1197 of_get_mac_address_mtd(priv->device->of_node, dev->dev_addr);
1199 err = fe_mdio_init(priv);
1203 if (priv->soc->port_init)
1204 for_each_child_of_node(priv->device->of_node, port)
1205 if (of_device_is_compatible(port, "ralink,eth-port") && of_device_is_available(port))
1206 priv->soc->port_init(priv, port);
1209 err = priv->phy->connect(priv);
1211 goto err_phy_disconnect;
1214 err = fe_hw_init(dev);
1216 goto err_phy_disconnect;
1218 if (priv->soc->switch_config)
1219 priv->soc->switch_config(priv);
1225 priv->phy->disconnect(priv);
1226 fe_mdio_cleanup(priv);
1231 static void fe_uninit(struct net_device *dev)
1233 struct fe_priv *priv = netdev_priv(dev);
1236 priv->phy->disconnect(priv);
1237 fe_mdio_cleanup(priv);
1239 fe_reg_w32(0, FE_REG_FE_INT_ENABLE);
1240 free_irq(dev->irq, dev);
1243 static int fe_do_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
1245 struct fe_priv *priv = netdev_priv(dev);
1252 return phy_ethtool_ioctl(priv->phy_dev,
1253 (void *) ifr->ifr_data);
1257 return phy_mii_ioctl(priv->phy_dev, ifr, cmd);
1265 static int fe_change_mtu(struct net_device *dev, int new_mtu)
1267 struct fe_priv *priv = netdev_priv(dev);
1268 int frag_size, old_mtu;
1271 if (!(priv->flags & FE_FLAG_JUMBO_FRAME))
1272 return eth_change_mtu(dev, new_mtu);
1274 frag_size = fe_max_frag_size(new_mtu);
1275 if (new_mtu < 68 || frag_size > PAGE_SIZE)
1281 /* return early if the buffer sizes will not change */
1282 if (old_mtu <= ETH_DATA_LEN && new_mtu <= ETH_DATA_LEN)
1284 if (old_mtu > ETH_DATA_LEN && new_mtu > ETH_DATA_LEN)
1287 if (new_mtu <= ETH_DATA_LEN)
1288 priv->frag_size = fe_max_frag_size(ETH_DATA_LEN);
1290 priv->frag_size = PAGE_SIZE;
1291 priv->rx_buf_size = fe_max_buf_size(priv->frag_size);
1293 if (!netif_running(dev))
1297 fwd_cfg = fe_r32(FE_GDMA1_FWD_CFG);
1298 if (new_mtu <= ETH_DATA_LEN)
1299 fwd_cfg &= ~FE_GDM1_JMB_EN;
1301 fwd_cfg &= ~(FE_GDM1_JMB_LEN_MASK << FE_GDM1_JMB_LEN_SHIFT);
1302 fwd_cfg |= (DIV_ROUND_UP(frag_size, 1024) <<
1303 FE_GDM1_JMB_LEN_SHIFT) | FE_GDM1_JMB_EN;
1305 fe_w32(fwd_cfg, FE_GDMA1_FWD_CFG);
1307 return fe_open(dev);
1310 static const struct net_device_ops fe_netdev_ops = {
1311 .ndo_init = fe_init,
1312 .ndo_uninit = fe_uninit,
1313 .ndo_open = fe_open,
1314 .ndo_stop = fe_stop,
1315 .ndo_start_xmit = fe_start_xmit,
1316 .ndo_set_mac_address = fe_set_mac_address,
1317 .ndo_validate_addr = eth_validate_addr,
1318 .ndo_do_ioctl = fe_do_ioctl,
1319 .ndo_change_mtu = fe_change_mtu,
1320 .ndo_tx_timeout = fe_tx_timeout,
1321 .ndo_get_stats64 = fe_get_stats64,
1322 .ndo_vlan_rx_add_vid = fe_vlan_rx_add_vid,
1323 .ndo_vlan_rx_kill_vid = fe_vlan_rx_kill_vid,
1324 #ifdef CONFIG_NET_POLL_CONTROLLER
1325 .ndo_poll_controller = fe_poll_controller,
1329 static void fe_reset_pending(struct fe_priv *priv)
1331 struct net_device *dev = priv->netdev;
1344 netif_alert(priv, ifup, dev,
1345 "Driver up/down cycle failed, closing device.\n");
1350 static const struct fe_work_t fe_work[] = {
1351 {FE_FLAG_RESET_PENDING, fe_reset_pending},
1354 static void fe_pending_work(struct work_struct *work)
1356 struct fe_priv *priv = container_of(work, struct fe_priv, pending_work);
1360 for (i = 0; i < ARRAY_SIZE(fe_work); i++) {
1361 pending = test_and_clear_bit(fe_work[i].bitnr,
1362 priv->pending_flags);
1364 fe_work[i].action(priv);
1368 static int fe_probe(struct platform_device *pdev)
1370 struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1371 const struct of_device_id *match;
1372 struct fe_soc_data *soc;
1373 struct net_device *netdev;
1374 struct fe_priv *priv;
1378 device_reset(&pdev->dev);
1380 match = of_match_device(of_fe_match, &pdev->dev);
1381 soc = (struct fe_soc_data *) match->data;
1384 fe_reg_table = soc->reg_table;
1386 soc->reg_table = fe_reg_table;
1388 fe_base = devm_request_and_ioremap(&pdev->dev, res);
1390 err = -EADDRNOTAVAIL;
1394 netdev = alloc_etherdev(sizeof(*priv));
1396 dev_err(&pdev->dev, "alloc_etherdev failed\n");
1401 SET_NETDEV_DEV(netdev, &pdev->dev);
1402 netdev->netdev_ops = &fe_netdev_ops;
1403 netdev->base_addr = (unsigned long) fe_base;
1405 netdev->irq = platform_get_irq(pdev, 0);
1406 if (netdev->irq < 0) {
1407 dev_err(&pdev->dev, "no IRQ resource found\n");
1413 soc->init_data(soc, netdev);
1414 /* fake NETIF_F_HW_VLAN_CTAG_RX for good GRO performance */
1415 netdev->hw_features |= NETIF_F_HW_VLAN_CTAG_RX;
1416 netdev->vlan_features = netdev->hw_features &
1417 ~(NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_CTAG_RX);
1418 netdev->features |= netdev->hw_features;
1420 /* fake rx vlan filter func. to support tx vlan offload func */
1421 if (fe_reg_table[FE_REG_FE_DMA_VID_BASE])
1422 netdev->features |= NETIF_F_HW_VLAN_CTAG_FILTER;
1424 priv = netdev_priv(netdev);
1425 spin_lock_init(&priv->page_lock);
1426 if (fe_reg_table[FE_REG_FE_COUNTER_BASE]) {
1427 priv->hw_stats = kzalloc(sizeof(*priv->hw_stats), GFP_KERNEL);
1428 if (!priv->hw_stats) {
1432 spin_lock_init(&priv->hw_stats->stats_lock);
1435 sysclk = devm_clk_get(&pdev->dev, NULL);
1436 if (!IS_ERR(sysclk))
1437 priv->sysclk = clk_get_rate(sysclk);
1439 priv->netdev = netdev;
1440 priv->device = &pdev->dev;
1442 priv->msg_enable = netif_msg_init(fe_msg_level, FE_DEFAULT_MSG_ENABLE);
1443 priv->frag_size = fe_max_frag_size(ETH_DATA_LEN);
1444 priv->rx_buf_size = fe_max_buf_size(priv->frag_size);
1445 if (priv->frag_size > PAGE_SIZE) {
1446 dev_err(&pdev->dev, "error frag size.\n");
1450 INIT_WORK(&priv->pending_work, fe_pending_work);
1452 netif_napi_add(netdev, &priv->rx_napi, fe_poll, 32);
1453 fe_set_ethtool_ops(netdev);
1455 err = register_netdev(netdev);
1457 dev_err(&pdev->dev, "error bringing up device\n");
1461 platform_set_drvdata(pdev, netdev);
1463 netif_info(priv, probe, netdev, "ralink at 0x%08lx, irq %d\n",
1464 netdev->base_addr, netdev->irq);
1469 free_netdev(netdev);
1471 devm_iounmap(&pdev->dev, fe_base);
1476 static int fe_remove(struct platform_device *pdev)
1478 struct net_device *dev = platform_get_drvdata(pdev);
1479 struct fe_priv *priv = netdev_priv(dev);
1481 netif_napi_del(&priv->rx_napi);
1483 kfree(priv->hw_stats);
1485 cancel_work_sync(&priv->pending_work);
1487 unregister_netdev(dev);
1489 platform_set_drvdata(pdev, NULL);
1494 static struct platform_driver fe_driver = {
1496 .remove = fe_remove,
1498 .name = "ralink_soc_eth",
1499 .owner = THIS_MODULE,
1500 .of_match_table = of_fe_match,
1504 static int __init init_rtfe(void)
1512 ret = platform_driver_register(&fe_driver);
1519 static void __exit exit_rtfe(void)
1521 platform_driver_unregister(&fe_driver);
1525 module_init(init_rtfe);
1526 module_exit(exit_rtfe);
1528 MODULE_LICENSE("GPL");
1529 MODULE_AUTHOR("John Crispin <blogic@openwrt.org>");
1530 MODULE_DESCRIPTION("Ethernet driver for Ralink SoC");
1531 MODULE_VERSION(FE_DRV_VERSION);