dt:net:stmmac: Allocate platform data only if its NULL.
[deliverable/linux.git] / drivers / net / ethernet / stmicro / stmmac / stmmac_platform.c
CommitLineData
bfab27a1
GC
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>
6a228452
SR
27#include <linux/of.h>
28#include <linux/of_net.h>
bfab27a1
GC
29#include "stmmac.h"
30
6a228452 31#ifdef CONFIG_OF
979857bb 32static int stmmac_probe_config_dt(struct platform_device *pdev,
1dd06ae8
GKH
33 struct plat_stmmacenet_data *plat,
34 const char **mac)
6a228452
SR
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 */
84c9f8c4
DN
52 if (of_device_is_compatible(np, "st,spear600-gmac") ||
53 of_device_is_compatible(np, "snps,dwmac-3.70a") ||
54 of_device_is_compatible(np, "snps,dwmac")) {
6a228452
SR
55 plat->has_gmac = 1;
56 plat->pmt = 1;
57 }
58
59 return 0;
60}
61#else
979857bb 62static int stmmac_probe_config_dt(struct platform_device *pdev,
1dd06ae8
GKH
63 struct plat_stmmacenet_data *plat,
64 const char **mac)
6a228452
SR
65{
66 return -ENOSYS;
67}
68#endif /* CONFIG_OF */
69
bfab27a1
GC
70/**
71 * stmmac_pltfr_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 */
979857bb 77static int stmmac_pltfr_probe(struct platform_device *pdev)
bfab27a1
GC
78{
79 int ret = 0;
80 struct resource *res;
3f8bdecd 81 struct device *dev = &pdev->dev;
bfab27a1
GC
82 void __iomem *addr = NULL;
83 struct stmmac_priv *priv = NULL;
6a228452
SR
84 struct plat_stmmacenet_data *plat_dat = NULL;
85 const char *mac = NULL;
bfab27a1
GC
86
87 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
88 if (!res)
89 return -ENODEV;
90
5760f427
SMP
91 addr = devm_ioremap_resource(dev, res);
92 if (IS_ERR(addr))
93 return PTR_ERR(addr);
6a228452 94
d741434c 95 plat_dat = pdev->dev.platform_data;
6a228452 96 if (pdev->dev.of_node) {
d741434c
SK
97 if (!plat_dat)
98 plat_dat = devm_kzalloc(&pdev->dev,
6a228452
SR
99 sizeof(struct plat_stmmacenet_data),
100 GFP_KERNEL);
101 if (!plat_dat) {
102 pr_err("%s: ERROR: no memory", __func__);
3f8bdecd 103 return -ENOMEM;
6a228452
SR
104 }
105
106 ret = stmmac_probe_config_dt(pdev, plat_dat, &mac);
107 if (ret) {
108 pr_err("%s: main dt probe failed", __func__);
3f8bdecd 109 return ret;
6a228452 110 }
6a228452 111 }
cf3f047b
GC
112
113 /* Custom initialisation (if needed)*/
114 if (plat_dat->init) {
115 ret = plat_dat->init(pdev);
116 if (unlikely(ret))
3f8bdecd 117 return ret;
cf3f047b
GC
118 }
119
120 priv = stmmac_dvr_probe(&(pdev->dev), plat_dat, addr);
bfab27a1 121 if (!priv) {
cf3f047b 122 pr_err("%s: main driver probe failed", __func__);
3f8bdecd 123 return -ENODEV;
bfab27a1
GC
124 }
125
6a228452
SR
126 /* Get MAC address if available (DT) */
127 if (mac)
128 memcpy(priv->dev->dev_addr, mac, ETH_ALEN);
129
bfab27a1
GC
130 /* Get the MAC information */
131 priv->dev->irq = platform_get_irq_byname(pdev, "macirq");
132 if (priv->dev->irq == -ENXIO) {
133 pr_err("%s: ERROR: MAC IRQ configuration "
134 "information not found\n", __func__);
3f8bdecd 135 return -ENXIO;
bfab27a1
GC
136 }
137
138 /*
139 * On some platforms e.g. SPEAr the wake up irq differs from the mac irq
140 * The external wake up irq can be passed through the platform code
141 * named as "eth_wake_irq"
142 *
143 * In case the wake up interrupt is not passed from the platform
144 * so the driver will continue to use the mac irq (ndev->irq)
145 */
146 priv->wol_irq = platform_get_irq_byname(pdev, "eth_wake_irq");
147 if (priv->wol_irq == -ENXIO)
148 priv->wol_irq = priv->dev->irq;
149
d765955d
GC
150 priv->lpi_irq = platform_get_irq_byname(pdev, "eth_lpi");
151
bfab27a1
GC
152 platform_set_drvdata(pdev, priv->dev);
153
bfab27a1
GC
154 pr_debug("STMMAC platform driver registration completed");
155
156 return 0;
bfab27a1
GC
157}
158
159/**
160 * stmmac_pltfr_remove
161 * @pdev: platform device pointer
162 * Description: this function calls the main to free the net resources
163 * and calls the platforms hook and release the resources (e.g. mem).
164 */
165static int stmmac_pltfr_remove(struct platform_device *pdev)
166{
167 struct net_device *ndev = platform_get_drvdata(pdev);
168 struct stmmac_priv *priv = netdev_priv(ndev);
bfab27a1
GC
169 int ret = stmmac_dvr_remove(ndev);
170
bfab27a1
GC
171 if (priv->plat->exit)
172 priv->plat->exit(pdev);
173
bfab27a1
GC
174 return ret;
175}
176
177#ifdef CONFIG_PM
178static int stmmac_pltfr_suspend(struct device *dev)
179{
180 struct net_device *ndev = dev_get_drvdata(dev);
181
182 return stmmac_suspend(ndev);
183}
184
185static int stmmac_pltfr_resume(struct device *dev)
186{
187 struct net_device *ndev = dev_get_drvdata(dev);
188
189 return stmmac_resume(ndev);
190}
191
192int stmmac_pltfr_freeze(struct device *dev)
193{
3256251f
FV
194 int ret;
195 struct plat_stmmacenet_data *plat_dat = dev_get_platdata(dev);
bfab27a1 196 struct net_device *ndev = dev_get_drvdata(dev);
3256251f 197 struct platform_device *pdev = to_platform_device(dev);
bfab27a1 198
3256251f
FV
199 ret = stmmac_freeze(ndev);
200 if (plat_dat->exit)
201 plat_dat->exit(pdev);
202
203 return ret;
bfab27a1
GC
204}
205
206int stmmac_pltfr_restore(struct device *dev)
207{
3256251f 208 struct plat_stmmacenet_data *plat_dat = dev_get_platdata(dev);
bfab27a1 209 struct net_device *ndev = dev_get_drvdata(dev);
3256251f
FV
210 struct platform_device *pdev = to_platform_device(dev);
211
212 if (plat_dat->init)
213 plat_dat->init(pdev);
bfab27a1
GC
214
215 return stmmac_restore(ndev);
216}
217
218static const struct dev_pm_ops stmmac_pltfr_pm_ops = {
219 .suspend = stmmac_pltfr_suspend,
220 .resume = stmmac_pltfr_resume,
221 .freeze = stmmac_pltfr_freeze,
222 .thaw = stmmac_pltfr_restore,
223 .restore = stmmac_pltfr_restore,
224};
225#else
226static const struct dev_pm_ops stmmac_pltfr_pm_ops;
227#endif /* CONFIG_PM */
228
6a228452 229static const struct of_device_id stmmac_dt_ids[] = {
84c9f8c4
DN
230 { .compatible = "st,spear600-gmac"},
231 { .compatible = "snps,dwmac-3.70a"},
232 { .compatible = "snps,dwmac"},
6a228452
SR
233 { /* sentinel */ }
234};
235MODULE_DEVICE_TABLE(of, stmmac_dt_ids);
236
ba27ec66 237struct platform_driver stmmac_pltfr_driver = {
bfab27a1
GC
238 .probe = stmmac_pltfr_probe,
239 .remove = stmmac_pltfr_remove,
240 .driver = {
241 .name = STMMAC_RESOURCE_NAME,
242 .owner = THIS_MODULE,
243 .pm = &stmmac_pltfr_pm_ops,
6a228452 244 .of_match_table = of_match_ptr(stmmac_dt_ids),
bfab27a1
GC
245 },
246};
247
bfab27a1
GC
248MODULE_DESCRIPTION("STMMAC 10/100/1000 Ethernet PLATFORM driver");
249MODULE_AUTHOR("Giuseppe Cavallaro <peppe.cavallaro@st.com>");
250MODULE_LICENSE("GPL");
This page took 0.177388 seconds and 5 git commands to generate.