1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /**
3  * emac_arc.c - ARC EMAC specific glue layer
4  *
5  * Copyright (C) 2014 Romain Perier
6  *
7  * Romain Perier  <romain.perier@gmail.com>
8  */
9 
10 #include <linux/etherdevice.h>
11 #include <linux/module.h>
12 #include <linux/of_net.h>
13 #include <linux/platform_device.h>
14 
15 #include "emac.h"
16 
17 #define DRV_NAME    "emac_arc"
18 #define DRV_VERSION "1.0"
19 
20 static int emac_arc_probe(struct platform_device *pdev)
21 {
22 	struct device *dev = &pdev->dev;
23 	struct net_device *ndev;
24 	struct arc_emac_priv *priv;
25 	int interface, err;
26 
27 	if (!dev->of_node)
28 		return -ENODEV;
29 
30 	ndev = alloc_etherdev(sizeof(struct arc_emac_priv));
31 	if (!ndev)
32 		return -ENOMEM;
33 	platform_set_drvdata(pdev, ndev);
34 	SET_NETDEV_DEV(ndev, dev);
35 
36 	priv = netdev_priv(ndev);
37 	priv->drv_name = DRV_NAME;
38 	priv->drv_version = DRV_VERSION;
39 
40 	interface = of_get_phy_mode(dev->of_node);
41 	if (interface < 0)
42 		interface = PHY_INTERFACE_MODE_MII;
43 
44 	priv->clk = devm_clk_get(dev, "hclk");
45 	if (IS_ERR(priv->clk)) {
46 		dev_err(dev, "failed to retrieve host clock from device tree\n");
47 		err = -EINVAL;
48 		goto out_netdev;
49 	}
50 
51 	err = arc_emac_probe(ndev, interface);
52 out_netdev:
53 	if (err)
54 		free_netdev(ndev);
55 	return err;
56 }
57 
58 static int emac_arc_remove(struct platform_device *pdev)
59 {
60 	struct net_device *ndev = platform_get_drvdata(pdev);
61 	int err;
62 
63 	err = arc_emac_remove(ndev);
64 	free_netdev(ndev);
65 	return err;
66 }
67 
68 static const struct of_device_id emac_arc_dt_ids[] = {
69 	{ .compatible = "snps,arc-emac" },
70 	{ /* Sentinel */ }
71 };
72 MODULE_DEVICE_TABLE(of, emac_arc_dt_ids);
73 
74 static struct platform_driver emac_arc_driver = {
75 	.probe = emac_arc_probe,
76 	.remove = emac_arc_remove,
77 	.driver = {
78 		.name = DRV_NAME,
79 		.of_match_table  = emac_arc_dt_ids,
80 	},
81 };
82 
83 module_platform_driver(emac_arc_driver);
84 
85 MODULE_AUTHOR("Romain Perier <romain.perier@gmail.com>");
86 MODULE_DESCRIPTION("ARC EMAC platform driver");
87 MODULE_LICENSE("GPL");
88