hc
2024-02-19 1c055e55a242a33e574e48be530e06770a210dcd
kernel/drivers/net/ethernet/xilinx/xilinx_emaclite.c
....@@ -1,3 +1,4 @@
1
+// SPDX-License-Identifier: GPL-2.0-or-later
12 /*
23 * Xilinx EmacLite Linux driver for the Xilinx Ethernet MAC Lite device.
34 *
....@@ -5,11 +6,6 @@
56 * driver from John Williams <john.williams@xilinx.com>.
67 *
78 * 2007 - 2013 (c) Xilinx, Inc.
8
- *
9
- * This program is free software; you can redistribute it and/or modify it
10
- * under the terms of the GNU General Public License as published by the
11
- * Free Software Foundation; either version 2 of the License, or (at your
12
- * option) any later version.
139 */
1410
1511 #include <linux/module.h>
....@@ -17,6 +13,7 @@
1713 #include <linux/netdevice.h>
1814 #include <linux/etherdevice.h>
1915 #include <linux/skbuff.h>
16
+#include <linux/ethtool.h>
2017 #include <linux/io.h>
2118 #include <linux/slab.h>
2219 #include <linux/of_address.h>
....@@ -26,6 +23,7 @@
2623 #include <linux/of_net.h>
2724 #include <linux/phy.h>
2825 #include <linux/interrupt.h>
26
+#include <linux/iopoll.h>
2927
3028 #define DRIVER_NAME "xilinx_emaclite"
3129
....@@ -523,7 +521,7 @@
523521 *
524522 * This function is called when Tx time out occurs for Emaclite device.
525523 */
526
-static void xemaclite_tx_timeout(struct net_device *dev)
524
+static void xemaclite_tx_timeout(struct net_device *dev, unsigned int txqueue)
527525 {
528526 struct net_local *lp = netdev_priv(dev);
529527 unsigned long flags;
....@@ -543,7 +541,7 @@
543541 xemaclite_enable_interrupts(lp);
544542
545543 if (lp->deferred_skb) {
546
- dev_kfree_skb(lp->deferred_skb);
544
+ dev_kfree_skb_irq(lp->deferred_skb);
547545 lp->deferred_skb = NULL;
548546 dev->stats.tx_errors++;
549547 }
....@@ -581,7 +579,7 @@
581579 return;
582580
583581 dev->stats.tx_bytes += lp->deferred_skb->len;
584
- dev_kfree_skb_irq(lp->deferred_skb);
582
+ dev_consume_skb_irq(lp->deferred_skb);
585583 lp->deferred_skb = NULL;
586584 netif_trans_update(dev); /* prevent tx timeout */
587585 netif_wake_queue(dev);
....@@ -713,20 +711,15 @@
713711
714712 static int xemaclite_mdio_wait(struct net_local *lp)
715713 {
716
- unsigned long end = jiffies + 2;
714
+ u32 val;
717715
718716 /* wait for the MDIO interface to not be busy or timeout
719717 * after some time.
720718 */
721
- while (xemaclite_readl(lp->base_addr + XEL_MDIOCTRL_OFFSET) &
722
- XEL_MDIOCTRL_MDIOSTS_MASK) {
723
- if (time_before_eq(end, jiffies)) {
724
- WARN_ON(1);
725
- return -ETIMEDOUT;
726
- }
727
- msleep(1);
728
- }
729
- return 0;
719
+ return readx_poll_timeout(xemaclite_readl,
720
+ lp->base_addr + XEL_MDIOCTRL_OFFSET,
721
+ val, !(val & XEL_MDIOCTRL_MDIOSTS_MASK),
722
+ 1000, 20000);
730723 }
731724
732725 /**
....@@ -827,10 +820,10 @@
827820 static int xemaclite_mdio_setup(struct net_local *lp, struct device *dev)
828821 {
829822 struct mii_bus *bus;
830
- int rc;
831823 struct resource res;
832824 struct device_node *np = of_get_parent(lp->phy_node);
833825 struct device_node *npp;
826
+ int rc, ret;
834827
835828 /* Don't register the MDIO bus if the phy_node or its parent node
836829 * can't be found.
....@@ -840,8 +833,14 @@
840833 return -ENODEV;
841834 }
842835 npp = of_get_parent(np);
843
-
844
- of_address_to_resource(npp, 0, &res);
836
+ ret = of_address_to_resource(npp, 0, &res);
837
+ of_node_put(npp);
838
+ if (ret) {
839
+ dev_err(dev, "%s resource error!\n",
840
+ dev->of_node->full_name);
841
+ of_node_put(np);
842
+ return ret;
843
+ }
845844 if (lp->ndev->mem_start != res.start) {
846845 struct phy_device *phydev;
847846 phydev = of_phy_find_device(lp->phy_node);
....@@ -850,6 +849,7 @@
850849 "MDIO of the phy is not registered yet\n");
851850 else
852851 put_device(&phydev->mdio.dev);
852
+ of_node_put(np);
853853 return 0;
854854 }
855855
....@@ -862,6 +862,7 @@
862862 bus = mdiobus_alloc();
863863 if (!bus) {
864864 dev_err(dev, "Failed to allocate mdiobus\n");
865
+ of_node_put(np);
865866 return -ENOMEM;
866867 }
867868
....@@ -874,6 +875,7 @@
874875 bus->parent = dev;
875876
876877 rc = of_mdiobus_register(bus, np);
878
+ of_node_put(np);
877879 if (rc) {
878880 dev_err(dev, "Failed to register mdio bus.\n");
879881 goto err_register;
....@@ -930,8 +932,6 @@
930932 xemaclite_disable_interrupts(lp);
931933
932934 if (lp->phy_node) {
933
- u32 bmcr;
934
-
935935 lp->phy_dev = of_phy_connect(lp->ndev, lp->phy_node,
936936 xemaclite_adjust_link, 0,
937937 PHY_INTERFACE_MODE_MII);
....@@ -941,21 +941,7 @@
941941 }
942942
943943 /* EmacLite doesn't support giga-bit speeds */
944
- lp->phy_dev->supported &= (PHY_BASIC_FEATURES);
945
- lp->phy_dev->advertising = lp->phy_dev->supported;
946
-
947
- /* Don't advertise 1000BASE-T Full/Half duplex speeds */
948
- phy_write(lp->phy_dev, MII_CTRL1000, 0);
949
-
950
- /* Advertise only 10 and 100mbps full/half duplex speeds */
951
- phy_write(lp->phy_dev, MII_ADVERTISE, ADVERTISE_ALL |
952
- ADVERTISE_CSMA);
953
-
954
- /* Restart auto negotiation */
955
- bmcr = phy_read(lp->phy_dev, MII_BMCR);
956
- bmcr |= (BMCR_ANENABLE | BMCR_ANRESTART);
957
- phy_write(lp->phy_dev, MII_BMCR, bmcr);
958
-
944
+ phy_set_max_speed(lp->phy_dev, SPEED_100);
959945 phy_start(lp->phy_dev);
960946 }
961947
....@@ -1079,6 +1065,27 @@
10791065 return (bool)*p;
10801066 }
10811067
1068
+/**
1069
+ * xemaclite_ethtools_get_drvinfo - Get various Axi Emac Lite driver info
1070
+ * @ndev: Pointer to net_device structure
1071
+ * @ed: Pointer to ethtool_drvinfo structure
1072
+ *
1073
+ * This implements ethtool command for getting the driver information.
1074
+ * Issue "ethtool -i ethX" under linux prompt to execute this function.
1075
+ */
1076
+static void xemaclite_ethtools_get_drvinfo(struct net_device *ndev,
1077
+ struct ethtool_drvinfo *ed)
1078
+{
1079
+ strlcpy(ed->driver, DRIVER_NAME, sizeof(ed->driver));
1080
+}
1081
+
1082
+static const struct ethtool_ops xemaclite_ethtool_ops = {
1083
+ .get_drvinfo = xemaclite_ethtools_get_drvinfo,
1084
+ .get_link = ethtool_op_get_link,
1085
+ .get_link_ksettings = phy_ethtool_get_link_ksettings,
1086
+ .set_link_ksettings = phy_ethtool_set_link_ksettings,
1087
+};
1088
+
10821089 static const struct net_device_ops xemaclite_netdev_ops;
10831090
10841091 /**
....@@ -1144,9 +1151,9 @@
11441151 lp->rx_ping_pong = get_bool(ofdev, "xlnx,rx-ping-pong");
11451152 mac_address = of_get_mac_address(ofdev->dev.of_node);
11461153
1147
- if (mac_address) {
1154
+ if (!IS_ERR(mac_address)) {
11481155 /* Set the MAC address. */
1149
- memcpy(ndev->dev_addr, mac_address, ETH_ALEN);
1156
+ ether_addr_copy(ndev->dev_addr, mac_address);
11501157 } else {
11511158 dev_warn(dev, "No MAC address found, using random\n");
11521159 eth_hw_addr_random(ndev);
....@@ -1165,6 +1172,7 @@
11651172 dev_info(dev, "MAC address is now %pM\n", ndev->dev_addr);
11661173
11671174 ndev->netdev_ops = &xemaclite_netdev_ops;
1175
+ ndev->ethtool_ops = &xemaclite_ethtool_ops;
11681176 ndev->flags &= ~IFF_MULTICAST;
11691177 ndev->watchdog_timeo = TX_TIMEOUT;
11701178
....@@ -1173,7 +1181,7 @@
11731181 if (rc) {
11741182 dev_err(dev,
11751183 "Cannot register network device, aborting\n");
1176
- goto error;
1184
+ goto put_node;
11771185 }
11781186
11791187 dev_info(dev,
....@@ -1181,6 +1189,8 @@
11811189 (unsigned int __force)ndev->mem_start, lp->base_addr, ndev->irq);
11821190 return 0;
11831191
1192
+put_node:
1193
+ of_node_put(lp->phy_node);
11841194 error:
11851195 free_netdev(ndev);
11861196 return rc;
....@@ -1229,12 +1239,29 @@
12291239 }
12301240 #endif
12311241
1242
+/* Ioctl MII Interface */
1243
+static int xemaclite_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
1244
+{
1245
+ if (!dev->phydev || !netif_running(dev))
1246
+ return -EINVAL;
1247
+
1248
+ switch (cmd) {
1249
+ case SIOCGMIIPHY:
1250
+ case SIOCGMIIREG:
1251
+ case SIOCSMIIREG:
1252
+ return phy_mii_ioctl(dev->phydev, rq, cmd);
1253
+ default:
1254
+ return -EOPNOTSUPP;
1255
+ }
1256
+}
1257
+
12321258 static const struct net_device_ops xemaclite_netdev_ops = {
12331259 .ndo_open = xemaclite_open,
12341260 .ndo_stop = xemaclite_close,
12351261 .ndo_start_xmit = xemaclite_send,
12361262 .ndo_set_mac_address = xemaclite_set_mac_address,
12371263 .ndo_tx_timeout = xemaclite_tx_timeout,
1264
+ .ndo_do_ioctl = xemaclite_ioctl,
12381265 #ifdef CONFIG_NET_POLL_CONTROLLER
12391266 .ndo_poll_controller = xemaclite_poll_controller,
12401267 #endif