hc
2024-02-19 1c055e55a242a33e574e48be530e06770a210dcd
kernel/drivers/net/ethernet/nxp/lpc_eth.c
....@@ -1,3 +1,4 @@
1
+// SPDX-License-Identifier: GPL-2.0-or-later
12 /*
23 * drivers/net/ethernet/nxp/lpc_eth.c
34 *
....@@ -5,48 +6,21 @@
56 *
67 * Copyright (C) 2010 NXP Semiconductors
78 * Copyright (C) 2012 Roland Stigge <stigge@antcom.de>
8
- *
9
- * This program is free software; you can redistribute it and/or modify
10
- * it under the terms of the GNU General Public License as published by
11
- * the Free Software Foundation; either version 2 of the License, or
12
- * (at your option) any later version.
13
- *
14
- * This program is distributed in the hope that it will be useful,
15
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17
- * GNU General Public License for more details.
189 */
1910
2011 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
2112
22
-#include <linux/module.h>
23
-#include <linux/kernel.h>
24
-#include <linux/sched.h>
25
-#include <linux/slab.h>
26
-#include <linux/delay.h>
27
-#include <linux/interrupt.h>
28
-#include <linux/errno.h>
29
-#include <linux/ioport.h>
13
+#include <linux/clk.h>
3014 #include <linux/crc32.h>
15
+#include <linux/etherdevice.h>
16
+#include <linux/module.h>
17
+#include <linux/of.h>
18
+#include <linux/of_mdio.h>
19
+#include <linux/of_net.h>
20
+#include <linux/phy.h>
3121 #include <linux/platform_device.h>
3222 #include <linux/spinlock.h>
33
-#include <linux/ethtool.h>
34
-#include <linux/mii.h>
35
-#include <linux/clk.h>
36
-#include <linux/workqueue.h>
37
-#include <linux/netdevice.h>
38
-#include <linux/etherdevice.h>
39
-#include <linux/skbuff.h>
40
-#include <linux/phy.h>
41
-#include <linux/dma-mapping.h>
42
-#include <linux/of.h>
43
-#include <linux/of_net.h>
44
-#include <linux/types.h>
45
-
46
-#include <linux/io.h>
47
-#include <mach/board.h>
48
-#include <mach/platform.h>
49
-#include <mach/hardware.h>
23
+#include <linux/soc/nxp/lpc32xx-misc.h>
5024
5125 #define MODNAME "lpc-eth"
5226 #define DRV_VERSION "1.00"
....@@ -296,7 +270,7 @@
296270 #define LPC_FCCR_MIRRORCOUNTERCURRENT(n) ((n) & 0xFFFF)
297271
298272 /*
299
- * rxfliterctrl, rxfilterwolstatus, and rxfilterwolclear shared
273
+ * rxfilterctrl, rxfilterwolstatus, and rxfilterwolclear shared
300274 * register definitions
301275 */
302276 #define LPC_RXFLTRW_ACCEPTUNICAST (1 << 0)
....@@ -307,7 +281,7 @@
307281 #define LPC_RXFLTRW_ACCEPTPERFECT (1 << 5)
308282
309283 /*
310
- * rxfliterctrl register definitions
284
+ * rxfilterctrl register definitions
311285 */
312286 #define LPC_RXFLTRWSTS_MAGICPACKETENWOL (1 << 12)
313287 #define LPC_RXFLTRWSTS_RXFILTERENWOL (1 << 13)
....@@ -418,6 +392,7 @@
418392 struct netdata_local {
419393 struct platform_device *pdev;
420394 struct net_device *ndev;
395
+ struct device_node *phy_node;
421396 spinlock_t lock;
422397 void __iomem *net_base;
423398 u32 msg_enable;
....@@ -776,31 +751,32 @@
776751 static int lpc_mii_probe(struct net_device *ndev)
777752 {
778753 struct netdata_local *pldat = netdev_priv(ndev);
779
- struct phy_device *phydev = phy_find_first(pldat->mii_bus);
780
-
781
- if (!phydev) {
782
- netdev_err(ndev, "no PHY found\n");
783
- return -ENODEV;
784
- }
754
+ struct phy_device *phydev;
785755
786756 /* Attach to the PHY */
787757 if (lpc_phy_interface_mode(&pldat->pdev->dev) == PHY_INTERFACE_MODE_MII)
788758 netdev_info(ndev, "using MII interface\n");
789759 else
790760 netdev_info(ndev, "using RMII interface\n");
761
+
762
+ if (pldat->phy_node)
763
+ phydev = of_phy_find_device(pldat->phy_node);
764
+ else
765
+ phydev = phy_find_first(pldat->mii_bus);
766
+ if (!phydev) {
767
+ netdev_err(ndev, "no PHY found\n");
768
+ return -ENODEV;
769
+ }
770
+
791771 phydev = phy_connect(ndev, phydev_name(phydev),
792772 &lpc_handle_link_change,
793773 lpc_phy_interface_mode(&pldat->pdev->dev));
794
-
795774 if (IS_ERR(phydev)) {
796775 netdev_err(ndev, "Could not attach to PHY\n");
797776 return PTR_ERR(phydev);
798777 }
799778
800
- /* mask with MAC supported features */
801
- phydev->supported &= PHY_BASIC_FEATURES;
802
-
803
- phydev->advertising = phydev->supported;
779
+ phy_set_max_speed(phydev, SPEED_100);
804780
805781 pldat->link = 0;
806782 pldat->speed = 0;
....@@ -813,6 +789,7 @@
813789
814790 static int lpc_mii_init(struct netdata_local *pldat)
815791 {
792
+ struct device_node *node;
816793 int err = -ENXIO;
817794
818795 pldat->mii_bus = mdiobus_alloc();
....@@ -840,9 +817,10 @@
840817 pldat->mii_bus->priv = pldat;
841818 pldat->mii_bus->parent = &pldat->pdev->dev;
842819
843
- platform_set_drvdata(pldat->pdev, pldat->mii_bus);
844
-
845
- if (mdiobus_register(pldat->mii_bus))
820
+ node = of_get_child_by_name(pldat->pdev->dev.of_node, "mdio");
821
+ err = of_mdiobus_register(pldat->mii_bus, node);
822
+ of_node_put(node);
823
+ if (err)
846824 goto err_out_unregister_bus;
847825
848826 err = lpc_mii_probe(pldat->ndev);
....@@ -1051,7 +1029,8 @@
10511029 return 0;
10521030 }
10531031
1054
-static int lpc_eth_hard_start_xmit(struct sk_buff *skb, struct net_device *ndev)
1032
+static netdev_tx_t lpc_eth_hard_start_xmit(struct sk_buff *skb,
1033
+ struct net_device *ndev)
10551034 {
10561035 struct netdata_local *pldat = netdev_priv(ndev);
10571036 u32 len, txidx;
....@@ -1171,19 +1150,6 @@
11711150 spin_unlock_irqrestore(&pldat->lock, flags);
11721151 }
11731152
1174
-static int lpc_eth_ioctl(struct net_device *ndev, struct ifreq *req, int cmd)
1175
-{
1176
- struct phy_device *phydev = ndev->phydev;
1177
-
1178
- if (!netif_running(ndev))
1179
- return -EINVAL;
1180
-
1181
- if (!phydev)
1182
- return -ENODEV;
1183
-
1184
- return phy_mii_ioctl(phydev, req, cmd);
1185
-}
1186
-
11871153 static int lpc_eth_open(struct net_device *ndev)
11881154 {
11891155 struct netdata_local *pldat = netdev_priv(ndev);
....@@ -1251,35 +1217,29 @@
12511217 .ndo_stop = lpc_eth_close,
12521218 .ndo_start_xmit = lpc_eth_hard_start_xmit,
12531219 .ndo_set_rx_mode = lpc_eth_set_multicast_list,
1254
- .ndo_do_ioctl = lpc_eth_ioctl,
1220
+ .ndo_do_ioctl = phy_do_ioctl_running,
12551221 .ndo_set_mac_address = lpc_set_mac_address,
12561222 .ndo_validate_addr = eth_validate_addr,
12571223 };
12581224
12591225 static int lpc_eth_drv_probe(struct platform_device *pdev)
12601226 {
1261
- struct resource *res;
1262
- struct net_device *ndev;
1227
+ struct device *dev = &pdev->dev;
1228
+ struct device_node *np = dev->of_node;
12631229 struct netdata_local *pldat;
1264
- struct phy_device *phydev;
1230
+ struct net_device *ndev;
12651231 dma_addr_t dma_handle;
1232
+ struct resource *res;
12661233 int irq, ret;
1267
- u32 tmp;
12681234
12691235 /* Setup network interface for RMII or MII mode */
1270
- tmp = __raw_readl(LPC32XX_CLKPWR_MACCLK_CTRL);
1271
- tmp &= ~LPC32XX_CLKPWR_MACCTRL_PINS_MSK;
1272
- if (lpc_phy_interface_mode(&pdev->dev) == PHY_INTERFACE_MODE_MII)
1273
- tmp |= LPC32XX_CLKPWR_MACCTRL_USE_MII_PINS;
1274
- else
1275
- tmp |= LPC32XX_CLKPWR_MACCTRL_USE_RMII_PINS;
1276
- __raw_writel(tmp, LPC32XX_CLKPWR_MACCLK_CTRL);
1236
+ lpc32xx_set_phy_interface_mode(lpc_phy_interface_mode(dev));
12771237
12781238 /* Get platform resources */
12791239 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
12801240 irq = platform_get_irq(pdev, 0);
12811241 if (!res || irq < 0) {
1282
- dev_err(&pdev->dev, "error getting resources.\n");
1242
+ dev_err(dev, "error getting resources.\n");
12831243 ret = -ENXIO;
12841244 goto err_exit;
12851245 }
....@@ -1287,12 +1247,12 @@
12871247 /* Allocate net driver data structure */
12881248 ndev = alloc_etherdev(sizeof(struct netdata_local));
12891249 if (!ndev) {
1290
- dev_err(&pdev->dev, "could not allocate device.\n");
1250
+ dev_err(dev, "could not allocate device.\n");
12911251 ret = -ENOMEM;
12921252 goto err_exit;
12931253 }
12941254
1295
- SET_NETDEV_DEV(ndev, &pdev->dev);
1255
+ SET_NETDEV_DEV(ndev, dev);
12961256
12971257 pldat = netdev_priv(ndev);
12981258 pldat->pdev = pdev;
....@@ -1304,9 +1264,9 @@
13041264 ndev->irq = irq;
13051265
13061266 /* Get clock for the device */
1307
- pldat->clk = clk_get(&pdev->dev, NULL);
1267
+ pldat->clk = clk_get(dev, NULL);
13081268 if (IS_ERR(pldat->clk)) {
1309
- dev_err(&pdev->dev, "error getting clock.\n");
1269
+ dev_err(dev, "error getting clock.\n");
13101270 ret = PTR_ERR(pldat->clk);
13111271 goto err_out_free_dev;
13121272 }
....@@ -1319,14 +1279,14 @@
13191279 /* Map IO space */
13201280 pldat->net_base = ioremap(res->start, resource_size(res));
13211281 if (!pldat->net_base) {
1322
- dev_err(&pdev->dev, "failed to map registers\n");
1282
+ dev_err(dev, "failed to map registers\n");
13231283 ret = -ENOMEM;
13241284 goto err_out_disable_clocks;
13251285 }
13261286 ret = request_irq(ndev->irq, __lpc_eth_interrupt, 0,
13271287 ndev->name, ndev);
13281288 if (ret) {
1329
- dev_err(&pdev->dev, "error requesting interrupt.\n");
1289
+ dev_err(dev, "error requesting interrupt.\n");
13301290 goto err_out_iounmap;
13311291 }
13321292
....@@ -1338,20 +1298,19 @@
13381298 /* Get size of DMA buffers/descriptors region */
13391299 pldat->dma_buff_size = (ENET_TX_DESC + ENET_RX_DESC) * (ENET_MAXF_SIZE +
13401300 sizeof(struct txrx_desc_t) + sizeof(struct rx_status_t));
1341
- pldat->dma_buff_base_v = 0;
13421301
1343
- if (use_iram_for_net(&pldat->pdev->dev)) {
1344
- dma_handle = LPC32XX_IRAM_BASE;
1345
- if (pldat->dma_buff_size <= lpc32xx_return_iram_size())
1346
- pldat->dma_buff_base_v =
1347
- io_p2v(LPC32XX_IRAM_BASE);
1348
- else
1302
+ if (use_iram_for_net(dev)) {
1303
+ if (pldat->dma_buff_size >
1304
+ lpc32xx_return_iram(&pldat->dma_buff_base_v, &dma_handle)) {
1305
+ pldat->dma_buff_base_v = NULL;
1306
+ pldat->dma_buff_size = 0;
13491307 netdev_err(ndev,
13501308 "IRAM not big enough for net buffers, using SDRAM instead.\n");
1309
+ }
13511310 }
13521311
1353
- if (pldat->dma_buff_base_v == 0) {
1354
- ret = dma_coerce_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
1312
+ if (pldat->dma_buff_base_v == NULL) {
1313
+ ret = dma_coerce_mask_and_coherent(dev, DMA_BIT_MASK(32));
13551314 if (ret)
13561315 goto err_out_free_irq;
13571316
....@@ -1360,7 +1319,7 @@
13601319 /* Allocate a chunk of memory for the DMA ethernet buffers
13611320 and descriptors */
13621321 pldat->dma_buff_base_v =
1363
- dma_alloc_coherent(&pldat->pdev->dev,
1322
+ dma_alloc_coherent(dev,
13641323 pldat->dma_buff_size, &dma_handle,
13651324 GFP_KERNEL);
13661325 if (pldat->dma_buff_base_v == NULL) {
....@@ -1382,19 +1341,18 @@
13821341 netdev_dbg(ndev, "DMA buffer V address :0x%p\n",
13831342 pldat->dma_buff_base_v);
13841343
1344
+ pldat->phy_node = of_parse_phandle(np, "phy-handle", 0);
1345
+
13851346 /* Get MAC address from current HW setting (POR state is all zeros) */
13861347 __lpc_get_mac(pldat, ndev->dev_addr);
13871348
13881349 if (!is_valid_ether_addr(ndev->dev_addr)) {
1389
- const char *macaddr = of_get_mac_address(pdev->dev.of_node);
1390
- if (macaddr)
1391
- memcpy(ndev->dev_addr, macaddr, ETH_ALEN);
1350
+ const char *macaddr = of_get_mac_address(np);
1351
+ if (!IS_ERR(macaddr))
1352
+ ether_addr_copy(ndev->dev_addr, macaddr);
13921353 }
13931354 if (!is_valid_ether_addr(ndev->dev_addr))
13941355 eth_hw_addr_random(ndev);
1395
-
1396
- /* Reset the ethernet controller */
1397
- __lpc_eth_reset(pldat);
13981356
13991357 /* then shut everything down to save power */
14001358 __lpc_eth_shutdown(pldat);
....@@ -1416,7 +1374,7 @@
14161374
14171375 ret = register_netdev(ndev);
14181376 if (ret) {
1419
- dev_err(&pdev->dev, "Cannot register net device, aborting.\n");
1377
+ dev_err(dev, "Cannot register net device, aborting.\n");
14201378 goto err_out_dma_unmap;
14211379 }
14221380 platform_set_drvdata(pdev, ndev);
....@@ -1428,19 +1386,17 @@
14281386 netdev_info(ndev, "LPC mac at 0x%08lx irq %d\n",
14291387 (unsigned long)res->start, ndev->irq);
14301388
1431
- phydev = ndev->phydev;
1432
-
1433
- device_init_wakeup(&pdev->dev, 1);
1434
- device_set_wakeup_enable(&pdev->dev, 0);
1389
+ device_init_wakeup(dev, 1);
1390
+ device_set_wakeup_enable(dev, 0);
14351391
14361392 return 0;
14371393
14381394 err_out_unregister_netdev:
14391395 unregister_netdev(ndev);
14401396 err_out_dma_unmap:
1441
- if (!use_iram_for_net(&pldat->pdev->dev) ||
1442
- pldat->dma_buff_size > lpc32xx_return_iram_size())
1443
- dma_free_coherent(&pldat->pdev->dev, pldat->dma_buff_size,
1397
+ if (!use_iram_for_net(dev) ||
1398
+ pldat->dma_buff_size > lpc32xx_return_iram(NULL, NULL))
1399
+ dma_free_coherent(dev, pldat->dma_buff_size,
14441400 pldat->dma_buff_base_v,
14451401 pldat->dma_buff_base_p);
14461402 err_out_free_irq:
....@@ -1466,7 +1422,7 @@
14661422 unregister_netdev(ndev);
14671423
14681424 if (!use_iram_for_net(&pldat->pdev->dev) ||
1469
- pldat->dma_buff_size > lpc32xx_return_iram_size())
1425
+ pldat->dma_buff_size > lpc32xx_return_iram(NULL, NULL))
14701426 dma_free_coherent(&pldat->pdev->dev, pldat->dma_buff_size,
14711427 pldat->dma_buff_base_v,
14721428 pldat->dma_buff_base_p);
....@@ -1512,6 +1468,7 @@
15121468 {
15131469 struct net_device *ndev = platform_get_drvdata(pdev);
15141470 struct netdata_local *pldat;
1471
+ int ret;
15151472
15161473 if (device_may_wakeup(&pdev->dev))
15171474 disable_irq_wake(ndev->irq);
....@@ -1521,7 +1478,9 @@
15211478 pldat = netdev_priv(ndev);
15221479
15231480 /* Enable interface clock */
1524
- clk_enable(pldat->clk);
1481
+ ret = clk_enable(pldat->clk);
1482
+ if (ret)
1483
+ return ret;
15251484
15261485 /* Reset and initialize */
15271486 __lpc_eth_reset(pldat);
....@@ -1535,13 +1494,11 @@
15351494 }
15361495 #endif
15371496
1538
-#ifdef CONFIG_OF
15391497 static const struct of_device_id lpc_eth_match[] = {
15401498 { .compatible = "nxp,lpc-eth" },
15411499 { }
15421500 };
15431501 MODULE_DEVICE_TABLE(of, lpc_eth_match);
1544
-#endif
15451502
15461503 static struct platform_driver lpc_eth_driver = {
15471504 .probe = lpc_eth_drv_probe,
....@@ -1552,7 +1509,7 @@
15521509 #endif
15531510 .driver = {
15541511 .name = MODNAME,
1555
- .of_match_table = of_match_ptr(lpc_eth_match),
1512
+ .of_match_table = lpc_eth_match,
15561513 },
15571514 };
15581515