1 // SPDX-License-Identifier: GPL-2.0-only
2 /* 10G controller driver for Samsung SoCs
3  *
4  * Copyright (C) 2013 Samsung Electronics Co., Ltd.
5  *		http://www.samsung.com
6  *
7  * Author: Siva Reddy Kallam <siva.kallam@samsung.com>
8  */
9 
10 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
11 
12 #include <linux/etherdevice.h>
13 #include <linux/io.h>
14 #include <linux/module.h>
15 #include <linux/netdevice.h>
16 #include <linux/of.h>
17 #include <linux/of_irq.h>
18 #include <linux/of_net.h>
19 #include <linux/phy.h>
20 #include <linux/platform_device.h>
21 #include <linux/sxgbe_platform.h>
22 
23 #include "sxgbe_common.h"
24 #include "sxgbe_reg.h"
25 
26 #ifdef CONFIG_OF
27 static int sxgbe_probe_config_dt(struct platform_device *pdev,
28 				 struct sxgbe_plat_data *plat,
29 				 const char **mac)
30 {
31 	struct device_node *np = pdev->dev.of_node;
32 	struct sxgbe_dma_cfg *dma_cfg;
33 
34 	if (!np)
35 		return -ENODEV;
36 
37 	*mac = of_get_mac_address(np);
38 	plat->interface = of_get_phy_mode(np);
39 
40 	plat->bus_id = of_alias_get_id(np, "ethernet");
41 	if (plat->bus_id < 0)
42 		plat->bus_id = 0;
43 
44 	plat->mdio_bus_data = devm_kzalloc(&pdev->dev,
45 					   sizeof(*plat->mdio_bus_data),
46 					   GFP_KERNEL);
47 	if (!plat->mdio_bus_data)
48 		return -ENOMEM;
49 
50 	dma_cfg = devm_kzalloc(&pdev->dev, sizeof(*dma_cfg), GFP_KERNEL);
51 	if (!dma_cfg)
52 		return -ENOMEM;
53 
54 	plat->dma_cfg = dma_cfg;
55 	of_property_read_u32(np, "samsung,pbl", &dma_cfg->pbl);
56 	if (of_property_read_u32(np, "samsung,burst-map", &dma_cfg->burst_map) == 0)
57 		dma_cfg->fixed_burst = true;
58 
59 	return 0;
60 }
61 #else
62 static int sxgbe_probe_config_dt(struct platform_device *pdev,
63 				 struct sxgbe_plat_data *plat,
64 				 const char **mac)
65 {
66 	return -ENOSYS;
67 }
68 #endif /* CONFIG_OF */
69 
70 /**
71  * sxgbe_platform_probe
72  * @pdev: platform device pointer
73  * Description: platform_device probe function. It allocates
74  * the necessary resources and invokes the main to init
75  * the net device, register the mdio bus etc.
76  */
77 static int sxgbe_platform_probe(struct platform_device *pdev)
78 {
79 	int ret;
80 	int i, chan;
81 	struct resource *res;
82 	struct device *dev = &pdev->dev;
83 	void __iomem *addr;
84 	struct sxgbe_priv_data *priv = NULL;
85 	struct sxgbe_plat_data *plat_dat = NULL;
86 	const char *mac = NULL;
87 	struct net_device *ndev = platform_get_drvdata(pdev);
88 	struct device_node *node = dev->of_node;
89 
90 	/* Get memory resource */
91 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
92 	addr = devm_ioremap_resource(dev, res);
93 	if (IS_ERR(addr))
94 		return PTR_ERR(addr);
95 
96 	if (pdev->dev.of_node) {
97 		plat_dat = devm_kzalloc(&pdev->dev,
98 					sizeof(struct sxgbe_plat_data),
99 					GFP_KERNEL);
100 		if (!plat_dat)
101 			return  -ENOMEM;
102 
103 		ret = sxgbe_probe_config_dt(pdev, plat_dat, &mac);
104 		if (ret) {
105 			pr_err("%s: main dt probe failed\n", __func__);
106 			return ret;
107 		}
108 	}
109 
110 	priv = sxgbe_drv_probe(&(pdev->dev), plat_dat, addr);
111 	if (!priv) {
112 		pr_err("%s: main driver probe failed\n", __func__);
113 		goto err_out;
114 	}
115 
116 	/* Get the SXGBE common INT information */
117 	priv->irq  = irq_of_parse_and_map(node, 0);
118 	if (priv->irq <= 0) {
119 		dev_err(dev, "sxgbe common irq parsing failed\n");
120 		goto err_drv_remove;
121 	}
122 
123 	/* Get MAC address if available (DT) */
124 	if (!IS_ERR_OR_NULL(mac))
125 		ether_addr_copy(priv->dev->dev_addr, mac);
126 
127 	/* Get the TX/RX IRQ numbers */
128 	for (i = 0, chan = 1; i < SXGBE_TX_QUEUES; i++) {
129 		priv->txq[i]->irq_no = irq_of_parse_and_map(node, chan++);
130 		if (priv->txq[i]->irq_no <= 0) {
131 			dev_err(dev, "sxgbe tx irq parsing failed\n");
132 			goto err_tx_irq_unmap;
133 		}
134 	}
135 
136 	for (i = 0; i < SXGBE_RX_QUEUES; i++) {
137 		priv->rxq[i]->irq_no = irq_of_parse_and_map(node, chan++);
138 		if (priv->rxq[i]->irq_no <= 0) {
139 			dev_err(dev, "sxgbe rx irq parsing failed\n");
140 			goto err_rx_irq_unmap;
141 		}
142 	}
143 
144 	priv->lpi_irq = irq_of_parse_and_map(node, chan);
145 	if (priv->lpi_irq <= 0) {
146 		dev_err(dev, "sxgbe lpi irq parsing failed\n");
147 		goto err_rx_irq_unmap;
148 	}
149 
150 	platform_set_drvdata(pdev, priv->dev);
151 
152 	pr_debug("platform driver registration completed\n");
153 
154 	return 0;
155 
156 err_rx_irq_unmap:
157 	while (i--)
158 		irq_dispose_mapping(priv->rxq[i]->irq_no);
159 	i = SXGBE_TX_QUEUES;
160 err_tx_irq_unmap:
161 	while (i--)
162 		irq_dispose_mapping(priv->txq[i]->irq_no);
163 	irq_dispose_mapping(priv->irq);
164 err_drv_remove:
165 	sxgbe_drv_remove(ndev);
166 err_out:
167 	return -ENODEV;
168 }
169 
170 /**
171  * sxgbe_platform_remove
172  * @pdev: platform device pointer
173  * Description: this function calls the main to free the net resources
174  * and calls the platforms hook and release the resources (e.g. mem).
175  */
176 static int sxgbe_platform_remove(struct platform_device *pdev)
177 {
178 	struct net_device *ndev = platform_get_drvdata(pdev);
179 	int ret = sxgbe_drv_remove(ndev);
180 
181 	return ret;
182 }
183 
184 #ifdef CONFIG_PM
185 static int sxgbe_platform_suspend(struct device *dev)
186 {
187 	struct net_device *ndev = dev_get_drvdata(dev);
188 
189 	return sxgbe_suspend(ndev);
190 }
191 
192 static int sxgbe_platform_resume(struct device *dev)
193 {
194 	struct net_device *ndev = dev_get_drvdata(dev);
195 
196 	return sxgbe_resume(ndev);
197 }
198 
199 static int sxgbe_platform_freeze(struct device *dev)
200 {
201 	struct net_device *ndev = dev_get_drvdata(dev);
202 
203 	return sxgbe_freeze(ndev);
204 }
205 
206 static int sxgbe_platform_restore(struct device *dev)
207 {
208 	struct net_device *ndev = dev_get_drvdata(dev);
209 
210 	return sxgbe_restore(ndev);
211 }
212 
213 static const struct dev_pm_ops sxgbe_platform_pm_ops = {
214 	.suspend	= sxgbe_platform_suspend,
215 	.resume		= sxgbe_platform_resume,
216 	.freeze		= sxgbe_platform_freeze,
217 	.thaw		= sxgbe_platform_restore,
218 	.restore	= sxgbe_platform_restore,
219 };
220 #else
221 static const struct dev_pm_ops sxgbe_platform_pm_ops;
222 #endif /* CONFIG_PM */
223 
224 static const struct of_device_id sxgbe_dt_ids[] = {
225 	{ .compatible = "samsung,sxgbe-v2.0a"},
226 	{ /* sentinel */ }
227 };
228 MODULE_DEVICE_TABLE(of, sxgbe_dt_ids);
229 
230 static struct platform_driver sxgbe_platform_driver = {
231 	.probe	= sxgbe_platform_probe,
232 	.remove	= sxgbe_platform_remove,
233 	.driver	= {
234 		.name		= SXGBE_RESOURCE_NAME,
235 		.pm		= &sxgbe_platform_pm_ops,
236 		.of_match_table	= of_match_ptr(sxgbe_dt_ids),
237 	},
238 };
239 
240 int sxgbe_register_platform(void)
241 {
242 	int err;
243 
244 	err = platform_driver_register(&sxgbe_platform_driver);
245 	if (err)
246 		pr_err("failed to register the platform driver\n");
247 
248 	return err;
249 }
250 
251 void sxgbe_unregister_platform(void)
252 {
253 	platform_driver_unregister(&sxgbe_platform_driver);
254 }
255