Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /* Copyright Sunplus Technology Co., Ltd.
0003  *       All rights reserved.
0004  */
0005 
0006 #include <linux/platform_device.h>
0007 #include <linux/nvmem-consumer.h>
0008 #include <linux/etherdevice.h>
0009 #include <linux/netdevice.h>
0010 #include <linux/spinlock.h>
0011 #include <linux/of_net.h>
0012 #include <linux/reset.h>
0013 #include <linux/clk.h>
0014 #include <linux/of.h>
0015 
0016 #include "spl2sw_register.h"
0017 #include "spl2sw_define.h"
0018 #include "spl2sw_desc.h"
0019 #include "spl2sw_mdio.h"
0020 #include "spl2sw_phy.h"
0021 #include "spl2sw_int.h"
0022 #include "spl2sw_mac.h"
0023 
0024 /* net device operations */
0025 static int spl2sw_ethernet_open(struct net_device *ndev)
0026 {
0027     struct spl2sw_mac *mac = netdev_priv(ndev);
0028     struct spl2sw_common *comm = mac->comm;
0029     u32 mask;
0030 
0031     netdev_dbg(ndev, "Open port = %x\n", mac->lan_port);
0032 
0033     comm->enable |= mac->lan_port;
0034 
0035     spl2sw_mac_hw_start(comm);
0036 
0037     /* Enable TX and RX interrupts */
0038     mask = readl(comm->l2sw_reg_base + L2SW_SW_INT_MASK_0);
0039     mask &= ~(MAC_INT_TX | MAC_INT_RX);
0040     writel(mask, comm->l2sw_reg_base + L2SW_SW_INT_MASK_0);
0041 
0042     phy_start(ndev->phydev);
0043 
0044     netif_start_queue(ndev);
0045 
0046     return 0;
0047 }
0048 
0049 static int spl2sw_ethernet_stop(struct net_device *ndev)
0050 {
0051     struct spl2sw_mac *mac = netdev_priv(ndev);
0052     struct spl2sw_common *comm = mac->comm;
0053 
0054     netif_stop_queue(ndev);
0055 
0056     comm->enable &= ~mac->lan_port;
0057 
0058     phy_stop(ndev->phydev);
0059 
0060     spl2sw_mac_hw_stop(comm);
0061 
0062     return 0;
0063 }
0064 
0065 static int spl2sw_ethernet_start_xmit(struct sk_buff *skb, struct net_device *ndev)
0066 {
0067     struct spl2sw_mac *mac = netdev_priv(ndev);
0068     struct spl2sw_common *comm = mac->comm;
0069     struct spl2sw_skb_info *skbinfo;
0070     struct spl2sw_mac_desc *txdesc;
0071     unsigned long flags;
0072     u32 mapping;
0073     u32 tx_pos;
0074     u32 cmd1;
0075     u32 cmd2;
0076 
0077     if (unlikely(comm->tx_desc_full == 1)) {
0078         /* No TX descriptors left. Wait for tx interrupt. */
0079         netdev_dbg(ndev, "TX descriptor queue full when xmit!\n");
0080         return NETDEV_TX_BUSY;
0081     }
0082 
0083     /* If skb size is shorter than ETH_ZLEN (60), pad it with 0. */
0084     if (unlikely(skb->len < ETH_ZLEN)) {
0085         if (skb_padto(skb, ETH_ZLEN))
0086             return NETDEV_TX_OK;
0087 
0088         skb_put(skb, ETH_ZLEN - skb->len);
0089     }
0090 
0091     mapping = dma_map_single(&comm->pdev->dev, skb->data,
0092                  skb->len, DMA_TO_DEVICE);
0093     if (dma_mapping_error(&comm->pdev->dev, mapping)) {
0094         ndev->stats.tx_errors++;
0095         dev_kfree_skb(skb);
0096         return NETDEV_TX_OK;
0097     }
0098 
0099     spin_lock_irqsave(&comm->tx_lock, flags);
0100 
0101     tx_pos = comm->tx_pos;
0102     txdesc = &comm->tx_desc[tx_pos];
0103     skbinfo = &comm->tx_temp_skb_info[tx_pos];
0104     skbinfo->mapping = mapping;
0105     skbinfo->len = skb->len;
0106     skbinfo->skb = skb;
0107 
0108     /* Set up a TX descriptor */
0109     cmd1 = TXD_OWN | TXD_SOP | TXD_EOP | (mac->to_vlan << 12) |
0110            (skb->len & TXD_PKT_LEN);
0111     cmd2 = skb->len & TXD_BUF_LEN1;
0112 
0113     if (tx_pos == (TX_DESC_NUM - 1))
0114         cmd2 |= TXD_EOR;
0115 
0116     txdesc->addr1 = skbinfo->mapping;
0117     txdesc->cmd2 = cmd2;
0118     wmb();  /* Set TXD_OWN after other fields are effective. */
0119     txdesc->cmd1 = cmd1;
0120 
0121     /* Move tx_pos to next position */
0122     tx_pos = ((tx_pos + 1) == TX_DESC_NUM) ? 0 : tx_pos + 1;
0123 
0124     if (unlikely(tx_pos == comm->tx_done_pos)) {
0125         netif_stop_queue(ndev);
0126         comm->tx_desc_full = 1;
0127     }
0128     comm->tx_pos = tx_pos;
0129     wmb();      /* make sure settings are effective. */
0130 
0131     /* Trigger mac to transmit */
0132     writel(MAC_TRIG_L_SOC0, comm->l2sw_reg_base + L2SW_CPU_TX_TRIG);
0133 
0134     spin_unlock_irqrestore(&comm->tx_lock, flags);
0135     return NETDEV_TX_OK;
0136 }
0137 
0138 static void spl2sw_ethernet_set_rx_mode(struct net_device *ndev)
0139 {
0140     struct spl2sw_mac *mac = netdev_priv(ndev);
0141 
0142     spl2sw_mac_rx_mode_set(mac);
0143 }
0144 
0145 static int spl2sw_ethernet_set_mac_address(struct net_device *ndev, void *addr)
0146 {
0147     struct spl2sw_mac *mac = netdev_priv(ndev);
0148     int err;
0149 
0150     err = eth_mac_addr(ndev, addr);
0151     if (err)
0152         return err;
0153 
0154     /* Delete the old MAC address */
0155     netdev_dbg(ndev, "Old Ethernet (MAC) address = %pM\n", mac->mac_addr);
0156     if (is_valid_ether_addr(mac->mac_addr)) {
0157         err = spl2sw_mac_addr_del(mac);
0158         if (err)
0159             return err;
0160     }
0161 
0162     /* Set the MAC address */
0163     ether_addr_copy(mac->mac_addr, ndev->dev_addr);
0164     return spl2sw_mac_addr_add(mac);
0165 }
0166 
0167 static void spl2sw_ethernet_tx_timeout(struct net_device *ndev, unsigned int txqueue)
0168 {
0169     struct spl2sw_mac *mac = netdev_priv(ndev);
0170     struct spl2sw_common *comm = mac->comm;
0171     unsigned long flags;
0172     int i;
0173 
0174     netdev_err(ndev, "TX timed out!\n");
0175     ndev->stats.tx_errors++;
0176 
0177     spin_lock_irqsave(&comm->tx_lock, flags);
0178 
0179     for (i = 0; i < MAX_NETDEV_NUM; i++)
0180         if (comm->ndev[i])
0181             netif_stop_queue(comm->ndev[i]);
0182 
0183     spl2sw_mac_soft_reset(comm);
0184 
0185     /* Accept TX packets again. */
0186     for (i = 0; i < MAX_NETDEV_NUM; i++)
0187         if (comm->ndev[i]) {
0188             netif_trans_update(comm->ndev[i]);
0189             netif_wake_queue(comm->ndev[i]);
0190         }
0191 
0192     spin_unlock_irqrestore(&comm->tx_lock, flags);
0193 }
0194 
0195 static const struct net_device_ops netdev_ops = {
0196     .ndo_open = spl2sw_ethernet_open,
0197     .ndo_stop = spl2sw_ethernet_stop,
0198     .ndo_start_xmit = spl2sw_ethernet_start_xmit,
0199     .ndo_set_rx_mode = spl2sw_ethernet_set_rx_mode,
0200     .ndo_set_mac_address = spl2sw_ethernet_set_mac_address,
0201     .ndo_do_ioctl = phy_do_ioctl,
0202     .ndo_tx_timeout = spl2sw_ethernet_tx_timeout,
0203 };
0204 
0205 static void spl2sw_check_mac_vendor_id_and_convert(u8 *mac_addr)
0206 {
0207     /* Byte order of MAC address of some samples are reversed.
0208      * Check vendor id and convert byte order if it is wrong.
0209      * OUI of Sunplus: fc:4b:bc
0210      */
0211     if (mac_addr[5] == 0xfc && mac_addr[4] == 0x4b && mac_addr[3] == 0xbc &&
0212         (mac_addr[0] != 0xfc || mac_addr[1] != 0x4b || mac_addr[2] != 0xbc)) {
0213 
0214         swap(mac_addr[0], mac_addr[5]);
0215         swap(mac_addr[1], mac_addr[4]);
0216         swap(mac_addr[2], mac_addr[3]);
0217     }
0218 }
0219 
0220 static int spl2sw_nvmem_get_mac_address(struct device *dev, struct device_node *np,
0221                     void *addrbuf)
0222 {
0223     struct nvmem_cell *cell;
0224     ssize_t len;
0225     u8 *mac;
0226 
0227     /* Get nvmem cell of mac-address from dts. */
0228     cell = of_nvmem_cell_get(np, "mac-address");
0229     if (IS_ERR(cell))
0230         return PTR_ERR(cell);
0231 
0232     /* Read mac address from nvmem cell. */
0233     mac = nvmem_cell_read(cell, &len);
0234     nvmem_cell_put(cell);
0235     if (IS_ERR(mac))
0236         return PTR_ERR(mac);
0237 
0238     if (len != ETH_ALEN) {
0239         kfree(mac);
0240         dev_info(dev, "Invalid length of mac address in nvmem!\n");
0241         return -EINVAL;
0242     }
0243 
0244     /* Byte order of some samples are reversed.
0245      * Convert byte order here.
0246      */
0247     spl2sw_check_mac_vendor_id_and_convert(mac);
0248 
0249     /* Check if mac address is valid */
0250     if (!is_valid_ether_addr(mac)) {
0251         kfree(mac);
0252         dev_info(dev, "Invalid mac address in nvmem (%pM)!\n", mac);
0253         return -EINVAL;
0254     }
0255 
0256     ether_addr_copy(addrbuf, mac);
0257     kfree(mac);
0258     return 0;
0259 }
0260 
0261 static u32 spl2sw_init_netdev(struct platform_device *pdev, u8 *mac_addr,
0262                   struct net_device **r_ndev)
0263 {
0264     struct net_device *ndev;
0265     struct spl2sw_mac *mac;
0266     int ret;
0267 
0268     /* Allocate the devices, and also allocate spl2sw_mac,
0269      * we can get it by netdev_priv().
0270      */
0271     ndev = devm_alloc_etherdev(&pdev->dev, sizeof(*mac));
0272     if (!ndev) {
0273         *r_ndev = NULL;
0274         return -ENOMEM;
0275     }
0276     SET_NETDEV_DEV(ndev, &pdev->dev);
0277     ndev->netdev_ops = &netdev_ops;
0278     mac = netdev_priv(ndev);
0279     mac->ndev = ndev;
0280     ether_addr_copy(mac->mac_addr, mac_addr);
0281 
0282     eth_hw_addr_set(ndev, mac_addr);
0283     dev_info(&pdev->dev, "Ethernet (MAC) address = %pM\n", mac_addr);
0284 
0285     ret = register_netdev(ndev);
0286     if (ret) {
0287         dev_err(&pdev->dev, "Failed to register net device \"%s\"!\n",
0288             ndev->name);
0289         free_netdev(ndev);
0290         *r_ndev = NULL;
0291         return ret;
0292     }
0293     netdev_dbg(ndev, "Registered net device \"%s\" successfully.\n", ndev->name);
0294 
0295     *r_ndev = ndev;
0296     return 0;
0297 }
0298 
0299 static struct device_node *spl2sw_get_eth_child_node(struct device_node *ether_np, int id)
0300 {
0301     struct device_node *port_np;
0302     int port_id;
0303 
0304     for_each_child_of_node(ether_np, port_np) {
0305         /* It is not a 'port' node, continue. */
0306         if (strcmp(port_np->name, "port"))
0307             continue;
0308 
0309         if (of_property_read_u32(port_np, "reg", &port_id) < 0)
0310             continue;
0311 
0312         if (port_id == id)
0313             return port_np;
0314     }
0315 
0316     /* Not found! */
0317     return NULL;
0318 }
0319 
0320 static int spl2sw_probe(struct platform_device *pdev)
0321 {
0322     struct device_node *eth_ports_np;
0323     struct device_node *port_np;
0324     struct spl2sw_common *comm;
0325     struct device_node *phy_np;
0326     phy_interface_t phy_mode;
0327     struct net_device *ndev;
0328     struct spl2sw_mac *mac;
0329     u8 mac_addr[ETH_ALEN];
0330     int irq, i, ret;
0331 
0332     if (platform_get_drvdata(pdev))
0333         return -ENODEV;
0334 
0335     /* Allocate memory for 'spl2sw_common' area. */
0336     comm = devm_kzalloc(&pdev->dev, sizeof(*comm), GFP_KERNEL);
0337     if (!comm)
0338         return -ENOMEM;
0339 
0340     comm->pdev = pdev;
0341     platform_set_drvdata(pdev, comm);
0342 
0343     spin_lock_init(&comm->tx_lock);
0344     spin_lock_init(&comm->mdio_lock);
0345     spin_lock_init(&comm->int_mask_lock);
0346 
0347     /* Get memory resource 0 from dts. */
0348     comm->l2sw_reg_base = devm_platform_ioremap_resource(pdev, 0);
0349     if (IS_ERR(comm->l2sw_reg_base))
0350         return PTR_ERR(comm->l2sw_reg_base);
0351 
0352     /* Get irq resource from dts. */
0353     ret = platform_get_irq(pdev, 0);
0354     if (ret < 0)
0355         return ret;
0356     irq = ret;
0357 
0358     /* Get clock controller. */
0359     comm->clk = devm_clk_get(&pdev->dev, NULL);
0360     if (IS_ERR(comm->clk)) {
0361         dev_err_probe(&pdev->dev, PTR_ERR(comm->clk),
0362                   "Failed to retrieve clock controller!\n");
0363         return PTR_ERR(comm->clk);
0364     }
0365 
0366     /* Get reset controller. */
0367     comm->rstc = devm_reset_control_get_exclusive(&pdev->dev, NULL);
0368     if (IS_ERR(comm->rstc)) {
0369         dev_err_probe(&pdev->dev, PTR_ERR(comm->rstc),
0370                   "Failed to retrieve reset controller!\n");
0371         return PTR_ERR(comm->rstc);
0372     }
0373 
0374     /* Enable clock. */
0375     ret = clk_prepare_enable(comm->clk);
0376     if (ret)
0377         return ret;
0378     udelay(1);
0379 
0380     /* Reset MAC */
0381     reset_control_assert(comm->rstc);
0382     udelay(1);
0383     reset_control_deassert(comm->rstc);
0384     usleep_range(1000, 2000);
0385 
0386     /* Request irq. */
0387     ret = devm_request_irq(&pdev->dev, irq, spl2sw_ethernet_interrupt, 0,
0388                    dev_name(&pdev->dev), comm);
0389     if (ret) {
0390         dev_err(&pdev->dev, "Failed to request irq #%d!\n", irq);
0391         goto out_clk_disable;
0392     }
0393 
0394     /* Initialize TX and RX descriptors. */
0395     ret = spl2sw_descs_init(comm);
0396     if (ret) {
0397         dev_err(&pdev->dev, "Fail to initialize mac descriptors!\n");
0398         spl2sw_descs_free(comm);
0399         goto out_clk_disable;
0400     }
0401 
0402     /* Initialize MAC. */
0403     spl2sw_mac_init(comm);
0404 
0405     /* Initialize mdio bus */
0406     ret = spl2sw_mdio_init(comm);
0407     if (ret) {
0408         dev_err(&pdev->dev, "Failed to initialize mdio bus!\n");
0409         goto out_clk_disable;
0410     }
0411 
0412     /* Get child node ethernet-ports. */
0413     eth_ports_np = of_get_child_by_name(pdev->dev.of_node, "ethernet-ports");
0414     if (!eth_ports_np) {
0415         dev_err(&pdev->dev, "No ethernet-ports child node found!\n");
0416         ret = -ENODEV;
0417         goto out_free_mdio;
0418     }
0419 
0420     for (i = 0; i < MAX_NETDEV_NUM; i++) {
0421         /* Get port@i of node ethernet-ports. */
0422         port_np = spl2sw_get_eth_child_node(eth_ports_np, i);
0423         if (!port_np)
0424             continue;
0425 
0426         /* Get phy-mode. */
0427         if (of_get_phy_mode(port_np, &phy_mode)) {
0428             dev_err(&pdev->dev, "Failed to get phy-mode property of port@%d!\n",
0429                 i);
0430             continue;
0431         }
0432 
0433         /* Get phy-handle. */
0434         phy_np = of_parse_phandle(port_np, "phy-handle", 0);
0435         if (!phy_np) {
0436             dev_err(&pdev->dev, "Failed to get phy-handle property of port@%d!\n",
0437                 i);
0438             continue;
0439         }
0440 
0441         /* Get mac-address from nvmem. */
0442         ret = spl2sw_nvmem_get_mac_address(&pdev->dev, port_np, mac_addr);
0443         if (ret == -EPROBE_DEFER) {
0444             goto out_unregister_dev;
0445         } else if (ret) {
0446             dev_info(&pdev->dev, "Generate a random mac address!\n");
0447             eth_random_addr(mac_addr);
0448         }
0449 
0450         /* Initialize the net device. */
0451         ret = spl2sw_init_netdev(pdev, mac_addr, &ndev);
0452         if (ret)
0453             goto out_unregister_dev;
0454 
0455         ndev->irq = irq;
0456         comm->ndev[i] = ndev;
0457         mac = netdev_priv(ndev);
0458         mac->phy_node = phy_np;
0459         mac->phy_mode = phy_mode;
0460         mac->comm = comm;
0461 
0462         mac->lan_port = 0x1 << i;   /* forward to port i */
0463         mac->to_vlan = 0x1 << i;    /* vlan group: i     */
0464         mac->vlan_id = i;       /* vlan group: i     */
0465 
0466         /* Set MAC address */
0467         ret = spl2sw_mac_addr_add(mac);
0468         if (ret)
0469             goto out_unregister_dev;
0470 
0471         spl2sw_mac_rx_mode_set(mac);
0472     }
0473 
0474     /* Find first valid net device. */
0475     for (i = 0; i < MAX_NETDEV_NUM; i++) {
0476         if (comm->ndev[i])
0477             break;
0478     }
0479     if (i >= MAX_NETDEV_NUM) {
0480         dev_err(&pdev->dev, "No valid ethernet port!\n");
0481         ret = -ENODEV;
0482         goto out_free_mdio;
0483     }
0484 
0485     /* Save first valid net device */
0486     ndev = comm->ndev[i];
0487 
0488     ret = spl2sw_phy_connect(comm);
0489     if (ret) {
0490         netdev_err(ndev, "Failed to connect phy!\n");
0491         goto out_unregister_dev;
0492     }
0493 
0494     /* Add and enable napi. */
0495     netif_napi_add(ndev, &comm->rx_napi, spl2sw_rx_poll, NAPI_POLL_WEIGHT);
0496     napi_enable(&comm->rx_napi);
0497     netif_napi_add_tx(ndev, &comm->tx_napi, spl2sw_tx_poll);
0498     napi_enable(&comm->tx_napi);
0499     return 0;
0500 
0501 out_unregister_dev:
0502     for (i = 0; i < MAX_NETDEV_NUM; i++)
0503         if (comm->ndev[i])
0504             unregister_netdev(comm->ndev[i]);
0505 
0506 out_free_mdio:
0507     spl2sw_mdio_remove(comm);
0508 
0509 out_clk_disable:
0510     clk_disable_unprepare(comm->clk);
0511     return ret;
0512 }
0513 
0514 static int spl2sw_remove(struct platform_device *pdev)
0515 {
0516     struct spl2sw_common *comm;
0517     int i;
0518 
0519     comm = platform_get_drvdata(pdev);
0520 
0521     spl2sw_phy_remove(comm);
0522 
0523     /* Unregister and free net device. */
0524     for (i = 0; i < MAX_NETDEV_NUM; i++)
0525         if (comm->ndev[i])
0526             unregister_netdev(comm->ndev[i]);
0527 
0528     comm->enable = 0;
0529     spl2sw_mac_hw_stop(comm);
0530     spl2sw_descs_free(comm);
0531 
0532     /* Disable and delete napi. */
0533     napi_disable(&comm->rx_napi);
0534     netif_napi_del(&comm->rx_napi);
0535     napi_disable(&comm->tx_napi);
0536     netif_napi_del(&comm->tx_napi);
0537 
0538     spl2sw_mdio_remove(comm);
0539 
0540     clk_disable_unprepare(comm->clk);
0541 
0542     return 0;
0543 }
0544 
0545 static const struct of_device_id spl2sw_of_match[] = {
0546     {.compatible = "sunplus,sp7021-emac"},
0547     { /* sentinel */ }
0548 };
0549 
0550 MODULE_DEVICE_TABLE(of, spl2sw_of_match);
0551 
0552 static struct platform_driver spl2sw_driver = {
0553     .probe = spl2sw_probe,
0554     .remove = spl2sw_remove,
0555     .driver = {
0556         .name = "sp7021_emac",
0557         .of_match_table = spl2sw_of_match,
0558     },
0559 };
0560 
0561 module_platform_driver(spl2sw_driver);
0562 
0563 MODULE_AUTHOR("Wells Lu <wellslutw@gmail.com>");
0564 MODULE_DESCRIPTION("Sunplus Dual 10M/100M Ethernet driver");
0565 MODULE_LICENSE("GPL");