xref: /linux/drivers/net/ethernet/samsung/sxgbe/sxgbe_platform.c (revision 91ec2035134982b98fab0609a9fd8480e8217dc1)
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
sxgbe_probe_config_dt(struct platform_device * pdev,struct sxgbe_plat_data * plat)27 static int sxgbe_probe_config_dt(struct platform_device *pdev,
28 				 struct sxgbe_plat_data *plat)
29 {
30 	struct device_node *np = pdev->dev.of_node;
31 	struct sxgbe_dma_cfg *dma_cfg;
32 	int err;
33 
34 	if (!np)
35 		return -ENODEV;
36 
37 	err = of_get_phy_mode(np, &plat->interface);
38 	if (err && err != -ENODEV)
39 		return err;
40 
41 	plat->bus_id = of_alias_get_id(np, "ethernet");
42 	if (plat->bus_id < 0)
43 		plat->bus_id = 0;
44 
45 	plat->mdio_bus_data = devm_kzalloc(&pdev->dev,
46 					   sizeof(*plat->mdio_bus_data),
47 					   GFP_KERNEL);
48 	if (!plat->mdio_bus_data)
49 		return -ENOMEM;
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
sxgbe_probe_config_dt(struct platform_device * pdev,struct sxgbe_plat_data * plat)63 static int sxgbe_probe_config_dt(struct platform_device *pdev,
64 				 struct sxgbe_plat_data *plat)
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  */
sxgbe_platform_probe(struct platform_device * pdev)77 static int sxgbe_platform_probe(struct platform_device *pdev)
78 {
79 	int ret;
80 	int i, chan;
81 	struct device *dev = &pdev->dev;
82 	void __iomem *addr;
83 	struct sxgbe_priv_data *priv = NULL;
84 	struct sxgbe_plat_data *plat_dat = NULL;
85 	struct device_node *node = dev->of_node;
86 
87 	/* Get memory resource */
88 	addr = devm_platform_ioremap_resource(pdev, 0);
89 	if (IS_ERR(addr))
90 		return PTR_ERR(addr);
91 
92 	if (pdev->dev.of_node) {
93 		plat_dat = devm_kzalloc(&pdev->dev,
94 					sizeof(struct sxgbe_plat_data),
95 					GFP_KERNEL);
96 		if (!plat_dat)
97 			return  -ENOMEM;
98 
99 		ret = sxgbe_probe_config_dt(pdev, plat_dat);
100 		if (ret) {
101 			pr_err("%s: main dt probe failed\n", __func__);
102 			return ret;
103 		}
104 	}
105 
106 	priv = sxgbe_drv_probe(&(pdev->dev), plat_dat, addr);
107 	if (!priv) {
108 		pr_err("%s: main driver probe failed\n", __func__);
109 		goto err_out;
110 	}
111 
112 	/* Get the SXGBE common INT information */
113 	priv->irq  = irq_of_parse_and_map(node, 0);
114 	if (priv->irq <= 0) {
115 		dev_err(dev, "sxgbe common irq parsing failed\n");
116 		goto err_drv_remove;
117 	}
118 
119 	/* Get MAC address if available (DT) */
120 	of_get_ethdev_address(node, priv->dev);
121 
122 	/* Get the TX/RX IRQ numbers */
123 	for (i = 0, chan = 1; i < SXGBE_TX_QUEUES; i++) {
124 		priv->txq[i]->irq_no = irq_of_parse_and_map(node, chan++);
125 		if (priv->txq[i]->irq_no <= 0) {
126 			dev_err(dev, "sxgbe tx irq parsing failed\n");
127 			goto err_tx_irq_unmap;
128 		}
129 	}
130 
131 	for (i = 0; i < SXGBE_RX_QUEUES; i++) {
132 		priv->rxq[i]->irq_no = irq_of_parse_and_map(node, chan++);
133 		if (priv->rxq[i]->irq_no <= 0) {
134 			dev_err(dev, "sxgbe rx irq parsing failed\n");
135 			goto err_rx_irq_unmap;
136 		}
137 	}
138 
139 	priv->lpi_irq = irq_of_parse_and_map(node, chan);
140 	if (priv->lpi_irq <= 0) {
141 		dev_err(dev, "sxgbe lpi irq parsing failed\n");
142 		goto err_rx_irq_unmap;
143 	}
144 
145 	platform_set_drvdata(pdev, priv->dev);
146 
147 	pr_debug("platform driver registration completed\n");
148 
149 	return 0;
150 
151 err_rx_irq_unmap:
152 	while (i--)
153 		irq_dispose_mapping(priv->rxq[i]->irq_no);
154 	i = SXGBE_TX_QUEUES;
155 err_tx_irq_unmap:
156 	while (i--)
157 		irq_dispose_mapping(priv->txq[i]->irq_no);
158 	irq_dispose_mapping(priv->irq);
159 err_drv_remove:
160 	sxgbe_drv_remove(priv->dev);
161 err_out:
162 	return -ENODEV;
163 }
164 
165 /**
166  * sxgbe_platform_remove
167  * @pdev: platform device pointer
168  * Description: this function calls the main to free the net resources
169  * and calls the platforms hook and release the resources (e.g. mem).
170  */
sxgbe_platform_remove(struct platform_device * pdev)171 static void sxgbe_platform_remove(struct platform_device *pdev)
172 {
173 	struct net_device *ndev = platform_get_drvdata(pdev);
174 
175 	sxgbe_drv_remove(ndev);
176 }
177 
178 #ifdef CONFIG_PM
sxgbe_platform_suspend(struct device * dev)179 static int sxgbe_platform_suspend(struct device *dev)
180 {
181 	struct net_device *ndev = dev_get_drvdata(dev);
182 
183 	return sxgbe_suspend(ndev);
184 }
185 
sxgbe_platform_resume(struct device * dev)186 static int sxgbe_platform_resume(struct device *dev)
187 {
188 	struct net_device *ndev = dev_get_drvdata(dev);
189 
190 	return sxgbe_resume(ndev);
191 }
192 
sxgbe_platform_freeze(struct device * dev)193 static int sxgbe_platform_freeze(struct device *dev)
194 {
195 	struct net_device *ndev = dev_get_drvdata(dev);
196 
197 	return sxgbe_freeze(ndev);
198 }
199 
sxgbe_platform_restore(struct device * dev)200 static int sxgbe_platform_restore(struct device *dev)
201 {
202 	struct net_device *ndev = dev_get_drvdata(dev);
203 
204 	return sxgbe_restore(ndev);
205 }
206 
207 static const struct dev_pm_ops sxgbe_platform_pm_ops = {
208 	.suspend	= sxgbe_platform_suspend,
209 	.resume		= sxgbe_platform_resume,
210 	.freeze		= sxgbe_platform_freeze,
211 	.thaw		= sxgbe_platform_restore,
212 	.restore	= sxgbe_platform_restore,
213 };
214 #else
215 static const struct dev_pm_ops sxgbe_platform_pm_ops;
216 #endif /* CONFIG_PM */
217 
218 static const struct of_device_id sxgbe_dt_ids[] = {
219 	{ .compatible = "samsung,sxgbe-v2.0a"},
220 	{ /* sentinel */ }
221 };
222 MODULE_DEVICE_TABLE(of, sxgbe_dt_ids);
223 
224 static struct platform_driver sxgbe_platform_driver = {
225 	.probe	= sxgbe_platform_probe,
226 	.remove = sxgbe_platform_remove,
227 	.driver	= {
228 		.name		= SXGBE_RESOURCE_NAME,
229 		.pm		= &sxgbe_platform_pm_ops,
230 		.of_match_table	= sxgbe_dt_ids,
231 	},
232 };
233 
sxgbe_register_platform(void)234 int sxgbe_register_platform(void)
235 {
236 	int err;
237 
238 	err = platform_driver_register(&sxgbe_platform_driver);
239 	if (err)
240 		pr_err("failed to register the platform driver\n");
241 
242 	return err;
243 }
244 
sxgbe_unregister_platform(void)245 void sxgbe_unregister_platform(void)
246 {
247 	platform_driver_unregister(&sxgbe_platform_driver);
248 }
249