diff mbox series

[v2,net-next,7/9] net: korina: Add support for device tree

Message ID 20210414152946.12517-8-tsbogend@alpha.franken.de (mailing list archive)
State Superseded
Delegated to: Netdev Maintainers
Headers show
Series net: Korina improvements | expand

Checks

Context Check Description
netdev/cover_letter success Link
netdev/fixes_present success Link
netdev/patch_count success Link
netdev/tree_selection success Clearly marked for net-next
netdev/subject_prefix success Link
netdev/cc_maintainers warning 4 maintainers not CCed: vincent.stehle@laposte.net rppt@kernel.org vvidic@valentin-vidic.from.hr akpm@linux-foundation.org
netdev/source_inline success Was 0 now: 0
netdev/verify_signedoff success Link
netdev/module_param success Was 0 now: 0
netdev/build_32bit success Errors and warnings before: 0 this patch: 0
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/verify_fixes success Link
netdev/checkpatch warning WARNING: DT compatible string "korina" appears un-documented -- check ./Documentation/devicetree/bindings/
netdev/build_allmodconfig_warn success Errors and warnings before: 0 this patch: 0
netdev/header_inline success Link

Commit Message

Thomas Bogendoerfer April 14, 2021, 3:29 p.m. UTC
If there is no mac address passed via platform data try to get it via
device tree and fall back to a random mac address, if all fail.

Signed-off-by: Thomas Bogendoerfer <tsbogend@alpha.franken.de>
---
 drivers/net/ethernet/korina.c | 29 ++++++++++++++++++++++++++---
 1 file changed, 26 insertions(+), 3 deletions(-)

Comments

Florian Fainelli April 14, 2021, 6:33 p.m. UTC | #1
On 4/14/2021 8:29 AM, Thomas Bogendoerfer wrote:
> If there is no mac address passed via platform data try to get it via
> device tree and fall back to a random mac address, if all fail.
> 
> Signed-off-by: Thomas Bogendoerfer <tsbogend@alpha.franken.de>
> ---
>  drivers/net/ethernet/korina.c | 29 ++++++++++++++++++++++++++---
>  1 file changed, 26 insertions(+), 3 deletions(-)
> 
> diff --git a/drivers/net/ethernet/korina.c b/drivers/net/ethernet/korina.c
> index 69c8baa87a6e..c4590b2c65aa 100644
> --- a/drivers/net/ethernet/korina.c
> +++ b/drivers/net/ethernet/korina.c
> @@ -42,6 +42,8 @@
>  #include <linux/interrupt.h>
>  #include <linux/ioport.h>
>  #include <linux/in.h>
> +#include <linux/of_device.h>
> +#include <linux/of_net.h>
>  #include <linux/slab.h>
>  #include <linux/string.h>
>  #include <linux/delay.h>
> @@ -1056,7 +1058,7 @@ static const struct net_device_ops korina_netdev_ops = {
>  
>  static int korina_probe(struct platform_device *pdev)
>  {
> -	u8 *mac_addr = dev_get_platdata(&pdev->dev);
> +	const u8 *mac_addr = dev_get_platdata(&pdev->dev);
>  	struct korina_private *lp;
>  	struct net_device *dev;
>  	void __iomem *p;
> @@ -1069,7 +1071,15 @@ static int korina_probe(struct platform_device *pdev)
>  	SET_NETDEV_DEV(dev, &pdev->dev);
>  	lp = netdev_priv(dev);
>  
> -	memcpy(dev->dev_addr, mac_addr, ETH_ALEN);
> +	if (mac_addr) {
> +		ether_addr_copy(dev->dev_addr, mac_addr);
> +	} else {
> +		mac_addr = of_get_mac_address(pdev->dev.of_node);
> +		if (!IS_ERR(mac_addr))
> +			ether_addr_copy(dev->dev_addr, mac_addr);
> +		else
> +			eth_hw_addr_random(dev);
> +	}
>  
>  	lp->rx_irq = platform_get_irq_byname(pdev, "korina_rx");
>  	lp->tx_irq = platform_get_irq_byname(pdev, "korina_tx");
> @@ -1149,8 +1159,21 @@ static int korina_remove(struct platform_device *pdev)
>  	return 0;
>  }
>  
> +#ifdef CONFIG_OF
> +static const struct of_device_id korina_match[] = {
> +	{
> +		.compatible = "korina",
> +	},

If you add Device Tree, you might as well provide a binding document for
this controller and possibly pick up a better name than what this driver
had to begin with (in hindsight rb532 is also a bad name since it was
just one board instance), how about using idt,rc32434-emac or something
along these lines?
diff mbox series

Patch

diff --git a/drivers/net/ethernet/korina.c b/drivers/net/ethernet/korina.c
index 69c8baa87a6e..c4590b2c65aa 100644
--- a/drivers/net/ethernet/korina.c
+++ b/drivers/net/ethernet/korina.c
@@ -42,6 +42,8 @@ 
 #include <linux/interrupt.h>
 #include <linux/ioport.h>
 #include <linux/in.h>
+#include <linux/of_device.h>
+#include <linux/of_net.h>
 #include <linux/slab.h>
 #include <linux/string.h>
 #include <linux/delay.h>
@@ -1056,7 +1058,7 @@  static const struct net_device_ops korina_netdev_ops = {
 
 static int korina_probe(struct platform_device *pdev)
 {
-	u8 *mac_addr = dev_get_platdata(&pdev->dev);
+	const u8 *mac_addr = dev_get_platdata(&pdev->dev);
 	struct korina_private *lp;
 	struct net_device *dev;
 	void __iomem *p;
@@ -1069,7 +1071,15 @@  static int korina_probe(struct platform_device *pdev)
 	SET_NETDEV_DEV(dev, &pdev->dev);
 	lp = netdev_priv(dev);
 
-	memcpy(dev->dev_addr, mac_addr, ETH_ALEN);
+	if (mac_addr) {
+		ether_addr_copy(dev->dev_addr, mac_addr);
+	} else {
+		mac_addr = of_get_mac_address(pdev->dev.of_node);
+		if (!IS_ERR(mac_addr))
+			ether_addr_copy(dev->dev_addr, mac_addr);
+		else
+			eth_hw_addr_random(dev);
+	}
 
 	lp->rx_irq = platform_get_irq_byname(pdev, "korina_rx");
 	lp->tx_irq = platform_get_irq_byname(pdev, "korina_tx");
@@ -1149,8 +1159,21 @@  static int korina_remove(struct platform_device *pdev)
 	return 0;
 }
 
+#ifdef CONFIG_OF
+static const struct of_device_id korina_match[] = {
+	{
+		.compatible = "korina",
+	},
+	{ }
+};
+MODULE_DEVICE_TABLE(of, korina_match);
+#endif
+
 static struct platform_driver korina_driver = {
-	.driver.name = "korina",
+	.driver = {
+		.name = "korina",
+		.of_match_table = of_match_ptr(korina_match),
+	},
 	.probe = korina_probe,
 	.remove = korina_remove,
 };