1 /*******************************************************************************
2   This contains the functions to handle the platform driver.
3 
4   Copyright (C) 2007-2011  STMicroelectronics Ltd
5 
6   This program is free software; you can redistribute it and/or modify it
7   under the terms and conditions of the GNU General Public License,
8   version 2, as published by the Free Software Foundation.
9 
10   This program is distributed in the hope it will be useful, but WITHOUT
11   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12   FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
13   more details.
14 
15   You should have received a copy of the GNU General Public License along with
16   this program; if not, write to the Free Software Foundation, Inc.,
17   51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
18 
19   The full GNU General Public License is included in this distribution in
20   the file called "COPYING".
21 
22   Author: Giuseppe Cavallaro <peppe.cavallaro@st.com>
23 *******************************************************************************/
24 
25 #include <linux/platform_device.h>
26 #include <linux/io.h>
27 #include <linux/of.h>
28 #include <linux/of_net.h>
29 #include "stmmac.h"
30 
31 #ifdef CONFIG_OF
32 static int __devinit stmmac_probe_config_dt(struct platform_device *pdev,
33 					    struct plat_stmmacenet_data *plat,
34 					    const char **mac)
35 {
36 	struct device_node *np = pdev->dev.of_node;
37 
38 	if (!np)
39 		return -ENODEV;
40 
41 	*mac = of_get_mac_address(np);
42 	plat->interface = of_get_phy_mode(np);
43 	plat->mdio_bus_data = devm_kzalloc(&pdev->dev,
44 					   sizeof(struct stmmac_mdio_bus_data),
45 					   GFP_KERNEL);
46 
47 	/*
48 	 * Currently only the properties needed on SPEAr600
49 	 * are provided. All other properties should be added
50 	 * once needed on other platforms.
51 	 */
52 	if (of_device_is_compatible(np, "st,spear600-gmac")) {
53 		plat->pbl = 8;
54 		plat->has_gmac = 1;
55 		plat->pmt = 1;
56 	}
57 
58 	return 0;
59 }
60 #else
61 static int __devinit stmmac_probe_config_dt(struct platform_device *pdev,
62 					    struct plat_stmmacenet_data *plat,
63 					    const char **mac)
64 {
65 	return -ENOSYS;
66 }
67 #endif /* CONFIG_OF */
68 
69 /**
70  * stmmac_pltfr_probe
71  * @pdev: platform device pointer
72  * Description: platform_device probe function. It allocates
73  * the necessary resources and invokes the main to init
74  * the net device, register the mdio bus etc.
75  */
76 static int stmmac_pltfr_probe(struct platform_device *pdev)
77 {
78 	int ret = 0;
79 	struct resource *res;
80 	void __iomem *addr = NULL;
81 	struct stmmac_priv *priv = NULL;
82 	struct plat_stmmacenet_data *plat_dat = NULL;
83 	const char *mac = NULL;
84 
85 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
86 	if (!res)
87 		return -ENODEV;
88 
89 	if (!request_mem_region(res->start, resource_size(res), pdev->name)) {
90 		pr_err("%s: ERROR: memory allocation failed"
91 		       "cannot get the I/O addr 0x%x\n",
92 		       __func__, (unsigned int)res->start);
93 		return -EBUSY;
94 	}
95 
96 	addr = ioremap(res->start, resource_size(res));
97 	if (!addr) {
98 		pr_err("%s: ERROR: memory mapping failed", __func__);
99 		ret = -ENOMEM;
100 		goto out_release_region;
101 	}
102 
103 	if (pdev->dev.of_node) {
104 		plat_dat = devm_kzalloc(&pdev->dev,
105 					sizeof(struct plat_stmmacenet_data),
106 					GFP_KERNEL);
107 		if (!plat_dat) {
108 			pr_err("%s: ERROR: no memory", __func__);
109 			ret = -ENOMEM;
110 			goto out_unmap;
111 		}
112 
113 		ret = stmmac_probe_config_dt(pdev, plat_dat, &mac);
114 		if (ret) {
115 			pr_err("%s: main dt probe failed", __func__);
116 			goto out_unmap;
117 		}
118 	} else {
119 		plat_dat = pdev->dev.platform_data;
120 	}
121 
122 	/* Custom initialisation (if needed)*/
123 	if (plat_dat->init) {
124 		ret = plat_dat->init(pdev);
125 		if (unlikely(ret))
126 			goto out_unmap;
127 	}
128 
129 	priv = stmmac_dvr_probe(&(pdev->dev), plat_dat, addr);
130 	if (!priv) {
131 		pr_err("%s: main driver probe failed", __func__);
132 		goto out_unmap;
133 	}
134 
135 	/* Get MAC address if available (DT) */
136 	if (mac)
137 		memcpy(priv->dev->dev_addr, mac, ETH_ALEN);
138 
139 	/* Get the MAC information */
140 	priv->dev->irq = platform_get_irq_byname(pdev, "macirq");
141 	if (priv->dev->irq == -ENXIO) {
142 		pr_err("%s: ERROR: MAC IRQ configuration "
143 		       "information not found\n", __func__);
144 		ret = -ENXIO;
145 		goto out_unmap;
146 	}
147 
148 	/*
149 	 * On some platforms e.g. SPEAr the wake up irq differs from the mac irq
150 	 * The external wake up irq can be passed through the platform code
151 	 * named as "eth_wake_irq"
152 	 *
153 	 * In case the wake up interrupt is not passed from the platform
154 	 * so the driver will continue to use the mac irq (ndev->irq)
155 	 */
156 	priv->wol_irq = platform_get_irq_byname(pdev, "eth_wake_irq");
157 	if (priv->wol_irq == -ENXIO)
158 		priv->wol_irq = priv->dev->irq;
159 
160 	platform_set_drvdata(pdev, priv->dev);
161 
162 	pr_debug("STMMAC platform driver registration completed");
163 
164 	return 0;
165 
166 out_unmap:
167 	iounmap(addr);
168 	platform_set_drvdata(pdev, NULL);
169 
170 out_release_region:
171 	release_mem_region(res->start, resource_size(res));
172 
173 	return ret;
174 }
175 
176 /**
177  * stmmac_pltfr_remove
178  * @pdev: platform device pointer
179  * Description: this function calls the main to free the net resources
180  * and calls the platforms hook and release the resources (e.g. mem).
181  */
182 static int stmmac_pltfr_remove(struct platform_device *pdev)
183 {
184 	struct net_device *ndev = platform_get_drvdata(pdev);
185 	struct stmmac_priv *priv = netdev_priv(ndev);
186 	struct resource *res;
187 	int ret = stmmac_dvr_remove(ndev);
188 
189 	if (priv->plat->exit)
190 		priv->plat->exit(pdev);
191 
192 	if (priv->plat->exit)
193 		priv->plat->exit(pdev);
194 
195 	platform_set_drvdata(pdev, NULL);
196 
197 	iounmap((void *)priv->ioaddr);
198 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
199 	release_mem_region(res->start, resource_size(res));
200 
201 	return ret;
202 }
203 
204 #ifdef CONFIG_PM
205 static int stmmac_pltfr_suspend(struct device *dev)
206 {
207 	struct net_device *ndev = dev_get_drvdata(dev);
208 
209 	return stmmac_suspend(ndev);
210 }
211 
212 static int stmmac_pltfr_resume(struct device *dev)
213 {
214 	struct net_device *ndev = dev_get_drvdata(dev);
215 
216 	return stmmac_resume(ndev);
217 }
218 
219 int stmmac_pltfr_freeze(struct device *dev)
220 {
221 	struct net_device *ndev = dev_get_drvdata(dev);
222 
223 	return stmmac_freeze(ndev);
224 }
225 
226 int stmmac_pltfr_restore(struct device *dev)
227 {
228 	struct net_device *ndev = dev_get_drvdata(dev);
229 
230 	return stmmac_restore(ndev);
231 }
232 
233 static const struct dev_pm_ops stmmac_pltfr_pm_ops = {
234 	.suspend = stmmac_pltfr_suspend,
235 	.resume = stmmac_pltfr_resume,
236 	.freeze = stmmac_pltfr_freeze,
237 	.thaw = stmmac_pltfr_restore,
238 	.restore = stmmac_pltfr_restore,
239 };
240 #else
241 static const struct dev_pm_ops stmmac_pltfr_pm_ops;
242 #endif /* CONFIG_PM */
243 
244 static const struct of_device_id stmmac_dt_ids[] = {
245 	{ .compatible = "st,spear600-gmac", },
246 	{ /* sentinel */ }
247 };
248 MODULE_DEVICE_TABLE(of, stmmac_dt_ids);
249 
250 static struct platform_driver stmmac_driver = {
251 	.probe = stmmac_pltfr_probe,
252 	.remove = stmmac_pltfr_remove,
253 	.driver = {
254 		   .name = STMMAC_RESOURCE_NAME,
255 		   .owner = THIS_MODULE,
256 		   .pm = &stmmac_pltfr_pm_ops,
257 		   .of_match_table = of_match_ptr(stmmac_dt_ids),
258 		   },
259 };
260 
261 module_platform_driver(stmmac_driver);
262 
263 MODULE_DESCRIPTION("STMMAC 10/100/1000 Ethernet PLATFORM driver");
264 MODULE_AUTHOR("Giuseppe Cavallaro <peppe.cavallaro@st.com>");
265 MODULE_LICENSE("GPL");
266