18 #include <linux/kernel.h>
19 #include <linux/slab.h>
20 #include <linux/errno.h>
21 #include <linux/types.h>
25 #include <linux/netdevice.h>
29 #include <linux/tcp.h>
33 #include <linux/ethtool.h>
38 #include <linux/module.h>
40 #include <asm/checksum.h>
42 #include <lantiq_soc.h>
46 #define LTQ_ETOP_MDIO 0x11804
47 #define MDIO_REQUEST 0x80000000
48 #define MDIO_READ 0x40000000
49 #define MDIO_ADDR_MASK 0x1f
50 #define MDIO_ADDR_OFFSET 0x15
51 #define MDIO_REG_MASK 0x1f
52 #define MDIO_REG_OFFSET 0x10
53 #define MDIO_VAL_MASK 0xffff
55 #define PPE32_CGEN 0x800
56 #define LQ_PPE32_ENET_MAC_CFG 0x1840
58 #define LTQ_ETOP_ENETS0 0x11850
59 #define LTQ_ETOP_MAC_DA0 0x1186C
60 #define LTQ_ETOP_MAC_DA1 0x11870
61 #define LTQ_ETOP_CFG 0x16020
62 #define LTQ_ETOP_IGPLEN 0x16080
64 #define MAX_DMA_CHAN 0x8
65 #define MAX_DMA_CRC_LEN 0x4
66 #define MAX_DMA_DATA_LEN 0x600
68 #define ETOP_FTCU BIT(28)
69 #define ETOP_MII_MASK 0xf
70 #define ETOP_MII_NORMAL 0xd
71 #define ETOP_MII_REVERSE 0xe
72 #define ETOP_PLEN_UNDER 0x40
73 #define ETOP_CGEN 0x800
76 #define LTQ_ETOP_TX_CHANNEL 1
77 #define LTQ_ETOP_RX_CHANNEL 6
78 #define IS_TX(x) (x == LTQ_ETOP_TX_CHANNEL)
79 #define IS_RX(x) (x == LTQ_ETOP_RX_CHANNEL)
81 #define ltq_etop_r32(x) ltq_r32(ltq_etop_membase + (x))
82 #define ltq_etop_w32(x, y) ltq_w32(x, ltq_etop_membase + (y))
83 #define ltq_etop_w32_mask(x, y, z) \
84 ltq_w32_mask(x, y, ltq_etop_membase + (z))
86 #define DRV_VERSION "1.0"
88 static void __iomem *ltq_etop_membase;
118 if (!ch->
skb[ch->
dma.desc])
123 ch->
dma.desc_base[ch->
dma.desc].addr =
125 ch->
dma.desc_base[ch->
dma.desc].ctl =
142 if (ltq_etop_alloc_skb(ch)) {
144 "failed to allocate new rx buffer, stopping DMA\n");
149 spin_unlock_irqrestore(&priv->
lock, flags);
164 while ((rx < budget) && !complete) {
168 ltq_etop_hw_receive(ch);
174 if (complete || !rx) {
182 ltq_etop_poll_tx(
struct napi_struct *napi,
int budget)
188 netdev_get_tx_queue(ch->
netdev, ch->
idx >> 1);
201 spin_unlock_irqrestore(&priv->
lock, flags);
203 if (netif_tx_queue_stopped(txq))
204 netif_tx_start_queue(txq);
211 ltq_etop_dma_irq(
int irq,
void *_priv)
216 napi_schedule(&priv->
ch[ch].napi);
244 ltq_etop_free_channel(dev, &priv->
ch[i]);
255 switch (priv->
pldata->mii_mode) {
267 netdev_err(dev,
"unknown mii mode %d\n",
287 }
else if (
IS_RX(i)) {
291 if (ltq_etop_alloc_skb(ch))
334 static const struct ethtool_ops ltq_etop_ethtool_ops = {
335 .get_drvinfo = ltq_etop_get_drvinfo,
336 .get_settings = ltq_etop_get_settings,
337 .set_settings = ltq_etop_set_settings,
338 .nway_reset = ltq_etop_nway_reset,
356 ltq_etop_mdio_rd(
struct mii_bus *bus,
int phy_addr,
int phy_reg)
384 for (phy_addr = 0; phy_addr <
PHY_MAX_ADDR; phy_addr++) {
385 if (priv->
mii_bus->phy_map[phy_addr]) {
386 phydev = priv->
mii_bus->phy_map[phy_addr];
392 netdev_err(dev,
"no PHY found\n");
396 phydev =
phy_connect(dev, dev_name(&phydev->
dev), <q_etop_mdio_link,
397 0, priv->
pldata->mii_mode);
399 if (IS_ERR(phydev)) {
400 netdev_err(dev,
"Could not attach to PHY\n");
401 return PTR_ERR(phydev);
414 pr_info(
"%s: attached PHY [%s] (phy_addr=%s, irq=%d)\n",
416 dev_name(&phydev->
dev), phydev->
irq);
428 priv->
mii_bus = mdiobus_alloc();
430 netdev_err(dev,
"failed to allocate mii bus\n");
436 priv->
mii_bus->read = ltq_etop_mdio_rd;
437 priv->
mii_bus->write = ltq_etop_mdio_wr;
438 priv->
mii_bus->name =
"ltq_mii";
444 goto err_out_free_mdiobus;
452 goto err_out_free_mdio_irq;
455 if (ltq_etop_mdio_probe(dev)) {
457 goto err_out_unregister_bus;
461 err_out_unregister_bus:
463 err_out_free_mdio_irq:
465 err_out_free_mdiobus:
494 napi_enable(&ch->
napi);
497 netif_tx_start_all_queues(dev);
507 netif_tx_stop_all_queues(dev);
514 napi_disable(&ch->
napi);
523 int queue = skb_get_queue_mapping(skb);
524 struct netdev_queue *txq = netdev_get_tx_queue(dev, queue);
536 netdev_err(dev,
"tx ring full\n");
537 netif_tx_stop_queue(txq);
555 spin_unlock_irqrestore(&priv->
lock, flags);
558 netif_tx_stop_queue(txq);
564 ltq_etop_change_mtu(
struct net_device *dev,
int new_mtu)
575 spin_unlock_irqrestore(&priv->
lock, flags);
590 ltq_etop_set_mac_address(
struct net_device *dev,
void *
p)
603 spin_unlock_irqrestore(&priv->
lock, flags);
609 ltq_etop_set_multicast_list(
struct net_device *dev)
620 spin_unlock_irqrestore(&priv->
lock, flags);
636 bool random_mac =
false;
640 err = ltq_etop_hw_init(dev);
643 ltq_etop_change_mtu(dev, 1500);
646 if (!is_valid_ether_addr(
mac.sa_data)) {
647 pr_warn(
"etop: invalid MAC, using random\n");
648 eth_random_addr(
mac.sa_data);
652 err = ltq_etop_set_mac_address(dev, &
mac);
660 ltq_etop_set_multicast_list(dev);
661 err = ltq_etop_mdio_init(dev);
670 ltq_etop_hw_exit(dev);
679 ltq_etop_hw_exit(dev);
680 err = ltq_etop_hw_init(dev);
684 netif_wake_queue(dev);
688 ltq_etop_hw_exit(dev);
689 netdev_err(dev,
"failed to restart etop after TX timeout\n");
693 .ndo_open = ltq_etop_open,
694 .ndo_stop = ltq_etop_stop,
695 .ndo_start_xmit = ltq_etop_tx,
696 .ndo_change_mtu = ltq_etop_change_mtu,
697 .ndo_do_ioctl = ltq_etop_ioctl,
698 .ndo_set_mac_address = ltq_etop_set_mac_address,
700 .ndo_set_rx_mode = ltq_etop_set_multicast_list,
701 .ndo_select_queue = ltq_etop_select_queue,
702 .ndo_init = ltq_etop_init,
703 .ndo_tx_timeout = ltq_etop_tx_timeout,
717 dev_err(&pdev->
dev,
"failed to get etop resource\n");
723 resource_size(res), dev_name(&pdev->
dev));
725 dev_err(&pdev->
dev,
"failed to request etop resource\n");
731 res->
start, resource_size(res));
732 if (!ltq_etop_membase) {
733 dev_err(&pdev->
dev,
"failed to remap etop engine %d\n",
747 priv = netdev_priv(dev);
750 priv->
pldata = dev_get_platdata(&pdev->
dev);
757 ltq_etop_poll_tx, 8);
760 ltq_etop_poll_rx, 32);
768 platform_set_drvdata(pdev, dev);
780 struct net_device *dev = platform_get_drvdata(pdev);
783 netif_tx_stop_all_queues(dev);
784 ltq_etop_hw_exit(dev);
785 ltq_etop_mdio_cleanup(dev);
805 pr_err(
"ltq_etop: Error registering platform driver!");