1 // SPDX-License-Identifier: GPL-2.0-only
4 * Copyright (C) 2011 John Crispin <blogic@openwrt.org>
7 #include <linux/kernel.h>
8 #include <linux/slab.h>
9 #include <linux/errno.h>
10 #include <linux/types.h>
11 #include <linux/interrupt.h>
12 #include <linux/uaccess.h>
14 #include <linux/netdevice.h>
15 #include <linux/etherdevice.h>
16 #include <linux/phy.h>
18 #include <linux/tcp.h>
19 #include <linux/skbuff.h>
21 #include <linux/platform_device.h>
22 #include <linux/ethtool.h>
23 #include <linux/init.h>
24 #include <linux/delay.h>
26 #include <linux/dma-mapping.h>
27 #include <linux/module.h>
29 #include <asm/checksum.h>
31 #include <lantiq_soc.h>
33 #include <lantiq_platform.h>
35 #define LTQ_ETOP_MDIO 0x11804
36 #define MDIO_REQUEST 0x80000000
37 #define MDIO_READ 0x40000000
38 #define MDIO_ADDR_MASK 0x1f
39 #define MDIO_ADDR_OFFSET 0x15
40 #define MDIO_REG_MASK 0x1f
41 #define MDIO_REG_OFFSET 0x10
42 #define MDIO_VAL_MASK 0xffff
44 #define PPE32_CGEN 0x800
45 #define LQ_PPE32_ENET_MAC_CFG 0x1840
47 #define LTQ_ETOP_ENETS0 0x11850
48 #define LTQ_ETOP_MAC_DA0 0x1186C
49 #define LTQ_ETOP_MAC_DA1 0x11870
50 #define LTQ_ETOP_CFG 0x16020
51 #define LTQ_ETOP_IGPLEN 0x16080
53 #define MAX_DMA_CHAN 0x8
54 #define MAX_DMA_CRC_LEN 0x4
55 #define MAX_DMA_DATA_LEN 0x600
57 #define ETOP_FTCU BIT(28)
58 #define ETOP_MII_MASK 0xf
59 #define ETOP_MII_NORMAL 0xd
60 #define ETOP_MII_REVERSE 0xe
61 #define ETOP_PLEN_UNDER 0x40
62 #define ETOP_CGEN 0x800
64 /* use 2 static channels for TX/RX */
65 #define LTQ_ETOP_TX_CHANNEL 1
66 #define LTQ_ETOP_RX_CHANNEL 6
67 #define IS_TX(x) (x == LTQ_ETOP_TX_CHANNEL)
68 #define IS_RX(x) (x == LTQ_ETOP_RX_CHANNEL)
70 #define ltq_etop_r32(x) ltq_r32(ltq_etop_membase + (x))
71 #define ltq_etop_w32(x, y) ltq_w32(x, ltq_etop_membase + (y))
72 #define ltq_etop_w32_mask(x, y, z) \
73 ltq_w32_mask(x, y, ltq_etop_membase + (z))
75 #define DRV_VERSION "1.0"
77 static void __iomem *ltq_etop_membase;
79 struct ltq_etop_chan {
82 struct net_device *netdev;
83 struct napi_struct napi;
84 struct ltq_dma_channel dma;
85 struct sk_buff *skb[LTQ_DESC_NUM];
88 struct ltq_etop_priv {
89 struct net_device *netdev;
90 struct platform_device *pdev;
91 struct ltq_eth_data *pldata;
94 struct mii_bus *mii_bus;
96 struct ltq_etop_chan ch[MAX_DMA_CHAN];
97 int tx_free[MAX_DMA_CHAN >> 1];
103 ltq_etop_alloc_skb(struct ltq_etop_chan *ch)
105 struct ltq_etop_priv *priv = netdev_priv(ch->netdev);
107 ch->skb[ch->dma.desc] = netdev_alloc_skb(ch->netdev, MAX_DMA_DATA_LEN);
108 if (!ch->skb[ch->dma.desc])
110 ch->dma.desc_base[ch->dma.desc].addr = dma_map_single(&priv->pdev->dev,
111 ch->skb[ch->dma.desc]->data, MAX_DMA_DATA_LEN,
113 ch->dma.desc_base[ch->dma.desc].addr =
114 CPHYSADDR(ch->skb[ch->dma.desc]->data);
115 ch->dma.desc_base[ch->dma.desc].ctl =
116 LTQ_DMA_OWN | LTQ_DMA_RX_OFFSET(NET_IP_ALIGN) |
118 skb_reserve(ch->skb[ch->dma.desc], NET_IP_ALIGN);
123 ltq_etop_hw_receive(struct ltq_etop_chan *ch)
125 struct ltq_etop_priv *priv = netdev_priv(ch->netdev);
126 struct ltq_dma_desc *desc = &ch->dma.desc_base[ch->dma.desc];
127 struct sk_buff *skb = ch->skb[ch->dma.desc];
128 int len = (desc->ctl & LTQ_DMA_SIZE_MASK) - MAX_DMA_CRC_LEN;
131 spin_lock_irqsave(&priv->lock, flags);
132 if (ltq_etop_alloc_skb(ch)) {
133 netdev_err(ch->netdev,
134 "failed to allocate new rx buffer, stopping DMA\n");
135 ltq_dma_close(&ch->dma);
138 ch->dma.desc %= LTQ_DESC_NUM;
139 spin_unlock_irqrestore(&priv->lock, flags);
142 skb->protocol = eth_type_trans(skb, ch->netdev);
143 netif_receive_skb(skb);
147 ltq_etop_poll_rx(struct napi_struct *napi, int budget)
149 struct ltq_etop_chan *ch = container_of(napi,
150 struct ltq_etop_chan, napi);
153 while (work_done < budget) {
154 struct ltq_dma_desc *desc = &ch->dma.desc_base[ch->dma.desc];
156 if ((desc->ctl & (LTQ_DMA_OWN | LTQ_DMA_C)) != LTQ_DMA_C)
158 ltq_etop_hw_receive(ch);
161 if (work_done < budget) {
162 napi_complete_done(&ch->napi, work_done);
163 ltq_dma_ack_irq(&ch->dma);
169 ltq_etop_poll_tx(struct napi_struct *napi, int budget)
171 struct ltq_etop_chan *ch =
172 container_of(napi, struct ltq_etop_chan, napi);
173 struct ltq_etop_priv *priv = netdev_priv(ch->netdev);
174 struct netdev_queue *txq =
175 netdev_get_tx_queue(ch->netdev, ch->idx >> 1);
178 spin_lock_irqsave(&priv->lock, flags);
179 while ((ch->dma.desc_base[ch->tx_free].ctl &
180 (LTQ_DMA_OWN | LTQ_DMA_C)) == LTQ_DMA_C) {
181 dev_kfree_skb_any(ch->skb[ch->tx_free]);
182 ch->skb[ch->tx_free] = NULL;
183 memset(&ch->dma.desc_base[ch->tx_free], 0,
184 sizeof(struct ltq_dma_desc));
186 ch->tx_free %= LTQ_DESC_NUM;
188 spin_unlock_irqrestore(&priv->lock, flags);
190 if (netif_tx_queue_stopped(txq))
191 netif_tx_start_queue(txq);
192 napi_complete(&ch->napi);
193 ltq_dma_ack_irq(&ch->dma);
198 ltq_etop_dma_irq(int irq, void *_priv)
200 struct ltq_etop_priv *priv = _priv;
201 int ch = irq - LTQ_DMA_CH0_INT;
203 napi_schedule(&priv->ch[ch].napi);
208 ltq_etop_free_channel(struct net_device *dev, struct ltq_etop_chan *ch)
210 struct ltq_etop_priv *priv = netdev_priv(dev);
212 ltq_dma_free(&ch->dma);
214 free_irq(ch->dma.irq, priv);
215 if (IS_RX(ch->idx)) {
217 for (desc = 0; desc < LTQ_DESC_NUM; desc++)
218 dev_kfree_skb_any(ch->skb[ch->dma.desc]);
223 ltq_etop_hw_exit(struct net_device *dev)
225 struct ltq_etop_priv *priv = netdev_priv(dev);
228 ltq_pmu_disable(PMU_PPE);
229 for (i = 0; i < MAX_DMA_CHAN; i++)
230 if (IS_TX(i) || IS_RX(i))
231 ltq_etop_free_channel(dev, &priv->ch[i]);
235 ltq_etop_hw_init(struct net_device *dev)
237 struct ltq_etop_priv *priv = netdev_priv(dev);
240 ltq_pmu_enable(PMU_PPE);
242 switch (priv->pldata->mii_mode) {
243 case PHY_INTERFACE_MODE_RMII:
244 ltq_etop_w32_mask(ETOP_MII_MASK,
245 ETOP_MII_REVERSE, LTQ_ETOP_CFG);
248 case PHY_INTERFACE_MODE_MII:
249 ltq_etop_w32_mask(ETOP_MII_MASK,
250 ETOP_MII_NORMAL, LTQ_ETOP_CFG);
254 netdev_err(dev, "unknown mii mode %d\n",
255 priv->pldata->mii_mode);
259 /* enable crc generation */
260 ltq_etop_w32(PPE32_CGEN, LQ_PPE32_ENET_MAC_CFG);
262 ltq_dma_init_port(DMA_PORT_ETOP);
264 for (i = 0; i < MAX_DMA_CHAN; i++) {
265 int irq = LTQ_DMA_CH0_INT + i;
266 struct ltq_etop_chan *ch = &priv->ch[i];
268 ch->idx = ch->dma.nr = i;
269 ch->dma.dev = &priv->pdev->dev;
272 ltq_dma_alloc_tx(&ch->dma);
273 request_irq(irq, ltq_etop_dma_irq, 0, "etop_tx", priv);
274 } else if (IS_RX(i)) {
275 ltq_dma_alloc_rx(&ch->dma);
276 for (ch->dma.desc = 0; ch->dma.desc < LTQ_DESC_NUM;
278 if (ltq_etop_alloc_skb(ch))
281 request_irq(irq, ltq_etop_dma_irq, 0, "etop_rx", priv);
289 ltq_etop_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
291 strlcpy(info->driver, "Lantiq ETOP", sizeof(info->driver));
292 strlcpy(info->bus_info, "internal", sizeof(info->bus_info));
293 strlcpy(info->version, DRV_VERSION, sizeof(info->version));
296 static const struct ethtool_ops ltq_etop_ethtool_ops = {
297 .get_drvinfo = ltq_etop_get_drvinfo,
298 .nway_reset = phy_ethtool_nway_reset,
299 .get_link_ksettings = phy_ethtool_get_link_ksettings,
300 .set_link_ksettings = phy_ethtool_set_link_ksettings,
304 ltq_etop_mdio_wr(struct mii_bus *bus, int phy_addr, int phy_reg, u16 phy_data)
306 u32 val = MDIO_REQUEST |
307 ((phy_addr & MDIO_ADDR_MASK) << MDIO_ADDR_OFFSET) |
308 ((phy_reg & MDIO_REG_MASK) << MDIO_REG_OFFSET) |
311 while (ltq_etop_r32(LTQ_ETOP_MDIO) & MDIO_REQUEST)
313 ltq_etop_w32(val, LTQ_ETOP_MDIO);
318 ltq_etop_mdio_rd(struct mii_bus *bus, int phy_addr, int phy_reg)
320 u32 val = MDIO_REQUEST | MDIO_READ |
321 ((phy_addr & MDIO_ADDR_MASK) << MDIO_ADDR_OFFSET) |
322 ((phy_reg & MDIO_REG_MASK) << MDIO_REG_OFFSET);
324 while (ltq_etop_r32(LTQ_ETOP_MDIO) & MDIO_REQUEST)
326 ltq_etop_w32(val, LTQ_ETOP_MDIO);
327 while (ltq_etop_r32(LTQ_ETOP_MDIO) & MDIO_REQUEST)
329 val = ltq_etop_r32(LTQ_ETOP_MDIO) & MDIO_VAL_MASK;
334 ltq_etop_mdio_link(struct net_device *dev)
340 ltq_etop_mdio_probe(struct net_device *dev)
342 struct ltq_etop_priv *priv = netdev_priv(dev);
343 struct phy_device *phydev;
345 phydev = phy_find_first(priv->mii_bus);
348 netdev_err(dev, "no PHY found\n");
352 phydev = phy_connect(dev, phydev_name(phydev),
353 <q_etop_mdio_link, priv->pldata->mii_mode);
355 if (IS_ERR(phydev)) {
356 netdev_err(dev, "Could not attach to PHY\n");
357 return PTR_ERR(phydev);
360 phy_set_max_speed(phydev, SPEED_100);
362 phy_attached_info(phydev);
368 ltq_etop_mdio_init(struct net_device *dev)
370 struct ltq_etop_priv *priv = netdev_priv(dev);
373 priv->mii_bus = mdiobus_alloc();
374 if (!priv->mii_bus) {
375 netdev_err(dev, "failed to allocate mii bus\n");
380 priv->mii_bus->priv = dev;
381 priv->mii_bus->read = ltq_etop_mdio_rd;
382 priv->mii_bus->write = ltq_etop_mdio_wr;
383 priv->mii_bus->name = "ltq_mii";
384 snprintf(priv->mii_bus->id, MII_BUS_ID_SIZE, "%s-%x",
385 priv->pdev->name, priv->pdev->id);
386 if (mdiobus_register(priv->mii_bus)) {
388 goto err_out_free_mdiobus;
391 if (ltq_etop_mdio_probe(dev)) {
393 goto err_out_unregister_bus;
397 err_out_unregister_bus:
398 mdiobus_unregister(priv->mii_bus);
399 err_out_free_mdiobus:
400 mdiobus_free(priv->mii_bus);
406 ltq_etop_mdio_cleanup(struct net_device *dev)
408 struct ltq_etop_priv *priv = netdev_priv(dev);
410 phy_disconnect(dev->phydev);
411 mdiobus_unregister(priv->mii_bus);
412 mdiobus_free(priv->mii_bus);
416 ltq_etop_open(struct net_device *dev)
418 struct ltq_etop_priv *priv = netdev_priv(dev);
421 for (i = 0; i < MAX_DMA_CHAN; i++) {
422 struct ltq_etop_chan *ch = &priv->ch[i];
424 if (!IS_TX(i) && (!IS_RX(i)))
426 ltq_dma_open(&ch->dma);
427 ltq_dma_enable_irq(&ch->dma);
428 napi_enable(&ch->napi);
430 phy_start(dev->phydev);
431 netif_tx_start_all_queues(dev);
436 ltq_etop_stop(struct net_device *dev)
438 struct ltq_etop_priv *priv = netdev_priv(dev);
441 netif_tx_stop_all_queues(dev);
442 phy_stop(dev->phydev);
443 for (i = 0; i < MAX_DMA_CHAN; i++) {
444 struct ltq_etop_chan *ch = &priv->ch[i];
446 if (!IS_RX(i) && !IS_TX(i))
448 napi_disable(&ch->napi);
449 ltq_dma_close(&ch->dma);
455 ltq_etop_tx(struct sk_buff *skb, struct net_device *dev)
457 int queue = skb_get_queue_mapping(skb);
458 struct netdev_queue *txq = netdev_get_tx_queue(dev, queue);
459 struct ltq_etop_priv *priv = netdev_priv(dev);
460 struct ltq_etop_chan *ch = &priv->ch[(queue << 1) | 1];
461 struct ltq_dma_desc *desc = &ch->dma.desc_base[ch->dma.desc];
466 len = skb->len < ETH_ZLEN ? ETH_ZLEN : skb->len;
468 if ((desc->ctl & (LTQ_DMA_OWN | LTQ_DMA_C)) || ch->skb[ch->dma.desc]) {
469 dev_kfree_skb_any(skb);
470 netdev_err(dev, "tx ring full\n");
471 netif_tx_stop_queue(txq);
472 return NETDEV_TX_BUSY;
475 /* dma needs to start on a 16 byte aligned address */
476 byte_offset = CPHYSADDR(skb->data) % 16;
477 ch->skb[ch->dma.desc] = skb;
479 netif_trans_update(dev);
481 spin_lock_irqsave(&priv->lock, flags);
482 desc->addr = ((unsigned int) dma_map_single(&priv->pdev->dev, skb->data, len,
483 DMA_TO_DEVICE)) - byte_offset;
485 desc->ctl = LTQ_DMA_OWN | LTQ_DMA_SOP | LTQ_DMA_EOP |
486 LTQ_DMA_TX_OFFSET(byte_offset) | (len & LTQ_DMA_SIZE_MASK);
488 ch->dma.desc %= LTQ_DESC_NUM;
489 spin_unlock_irqrestore(&priv->lock, flags);
491 if (ch->dma.desc_base[ch->dma.desc].ctl & LTQ_DMA_OWN)
492 netif_tx_stop_queue(txq);
498 ltq_etop_change_mtu(struct net_device *dev, int new_mtu)
500 struct ltq_etop_priv *priv = netdev_priv(dev);
505 spin_lock_irqsave(&priv->lock, flags);
506 ltq_etop_w32((ETOP_PLEN_UNDER << 16) | new_mtu, LTQ_ETOP_IGPLEN);
507 spin_unlock_irqrestore(&priv->lock, flags);
513 ltq_etop_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
515 /* TODO: mii-toll reports "No MII transceiver present!." ?!*/
516 return phy_mii_ioctl(dev->phydev, rq, cmd);
520 ltq_etop_set_mac_address(struct net_device *dev, void *p)
522 int ret = eth_mac_addr(dev, p);
525 struct ltq_etop_priv *priv = netdev_priv(dev);
528 /* store the mac for the unicast filter */
529 spin_lock_irqsave(&priv->lock, flags);
530 ltq_etop_w32(*((u32 *)dev->dev_addr), LTQ_ETOP_MAC_DA0);
531 ltq_etop_w32(*((u16 *)&dev->dev_addr[4]) << 16,
533 spin_unlock_irqrestore(&priv->lock, flags);
539 ltq_etop_set_multicast_list(struct net_device *dev)
541 struct ltq_etop_priv *priv = netdev_priv(dev);
544 /* ensure that the unicast filter is not enabled in promiscious mode */
545 spin_lock_irqsave(&priv->lock, flags);
546 if ((dev->flags & IFF_PROMISC) || (dev->flags & IFF_ALLMULTI))
547 ltq_etop_w32_mask(ETOP_FTCU, 0, LTQ_ETOP_ENETS0);
549 ltq_etop_w32_mask(0, ETOP_FTCU, LTQ_ETOP_ENETS0);
550 spin_unlock_irqrestore(&priv->lock, flags);
554 ltq_etop_init(struct net_device *dev)
556 struct ltq_etop_priv *priv = netdev_priv(dev);
559 bool random_mac = false;
561 dev->watchdog_timeo = 10 * HZ;
562 err = ltq_etop_hw_init(dev);
565 ltq_etop_change_mtu(dev, 1500);
567 memcpy(&mac, &priv->pldata->mac, sizeof(struct sockaddr));
568 if (!is_valid_ether_addr(mac.sa_data)) {
569 pr_warn("etop: invalid MAC, using random\n");
570 eth_random_addr(mac.sa_data);
574 err = ltq_etop_set_mac_address(dev, &mac);
578 /* Set addr_assign_type here, ltq_etop_set_mac_address would reset it. */
580 dev->addr_assign_type = NET_ADDR_RANDOM;
582 ltq_etop_set_multicast_list(dev);
583 err = ltq_etop_mdio_init(dev);
589 unregister_netdev(dev);
592 ltq_etop_hw_exit(dev);
597 ltq_etop_tx_timeout(struct net_device *dev)
601 ltq_etop_hw_exit(dev);
602 err = ltq_etop_hw_init(dev);
605 netif_trans_update(dev);
606 netif_wake_queue(dev);
610 ltq_etop_hw_exit(dev);
611 netdev_err(dev, "failed to restart etop after TX timeout\n");
614 static const struct net_device_ops ltq_eth_netdev_ops = {
615 .ndo_open = ltq_etop_open,
616 .ndo_stop = ltq_etop_stop,
617 .ndo_start_xmit = ltq_etop_tx,
618 .ndo_change_mtu = ltq_etop_change_mtu,
619 .ndo_do_ioctl = ltq_etop_ioctl,
620 .ndo_set_mac_address = ltq_etop_set_mac_address,
621 .ndo_validate_addr = eth_validate_addr,
622 .ndo_set_rx_mode = ltq_etop_set_multicast_list,
623 .ndo_select_queue = dev_pick_tx_zero,
624 .ndo_init = ltq_etop_init,
625 .ndo_tx_timeout = ltq_etop_tx_timeout,
629 ltq_etop_probe(struct platform_device *pdev)
631 struct net_device *dev;
632 struct ltq_etop_priv *priv;
633 struct resource *res;
637 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
639 dev_err(&pdev->dev, "failed to get etop resource\n");
644 res = devm_request_mem_region(&pdev->dev, res->start,
645 resource_size(res), dev_name(&pdev->dev));
647 dev_err(&pdev->dev, "failed to request etop resource\n");
652 ltq_etop_membase = devm_ioremap_nocache(&pdev->dev,
653 res->start, resource_size(res));
654 if (!ltq_etop_membase) {
655 dev_err(&pdev->dev, "failed to remap etop engine %d\n",
661 dev = alloc_etherdev_mq(sizeof(struct ltq_etop_priv), 4);
666 strcpy(dev->name, "eth%d");
667 dev->netdev_ops = <q_eth_netdev_ops;
668 dev->ethtool_ops = <q_etop_ethtool_ops;
669 priv = netdev_priv(dev);
672 priv->pldata = dev_get_platdata(&pdev->dev);
674 spin_lock_init(&priv->lock);
675 SET_NETDEV_DEV(dev, &pdev->dev);
677 for (i = 0; i < MAX_DMA_CHAN; i++) {
679 netif_napi_add(dev, &priv->ch[i].napi,
680 ltq_etop_poll_tx, 8);
682 netif_napi_add(dev, &priv->ch[i].napi,
683 ltq_etop_poll_rx, 32);
684 priv->ch[i].netdev = dev;
687 err = register_netdev(dev);
691 platform_set_drvdata(pdev, dev);
701 ltq_etop_remove(struct platform_device *pdev)
703 struct net_device *dev = platform_get_drvdata(pdev);
706 netif_tx_stop_all_queues(dev);
707 ltq_etop_hw_exit(dev);
708 ltq_etop_mdio_cleanup(dev);
709 unregister_netdev(dev);
714 static struct platform_driver ltq_mii_driver = {
715 .remove = ltq_etop_remove,
724 int ret = platform_driver_probe(<q_mii_driver, ltq_etop_probe);
727 pr_err("ltq_etop: Error registering platform driver!");
734 platform_driver_unregister(<q_mii_driver);
737 module_init(init_ltq_etop);
738 module_exit(exit_ltq_etop);
740 MODULE_AUTHOR("John Crispin <blogic@openwrt.org>");
741 MODULE_DESCRIPTION("Lantiq SoC ETOP");
742 MODULE_LICENSE("GPL");