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