Adding support for MOXA ART SoC. Testing port of linux-2.6.32.60-moxart.
[linux-3.6.7-moxart.git] / drivers / net / ethernet / stmicro / stmmac / stmmac_platform.c
blobb93245c11995bc15329321e6879be78d4c1b44dc
1 /*******************************************************************************
2 This contains the functions to handle the platform driver.
4 Copyright (C) 2007-2011 STMicroelectronics Ltd
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.
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.
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.
19 The full GNU General Public License is included in this distribution in
20 the file called "COPYING".
22 Author: Giuseppe Cavallaro <peppe.cavallaro@st.com>
23 *******************************************************************************/
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"
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)
36 struct device_node *np = pdev->dev.of_node;
38 if (!np)
39 return -ENODEV;
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);
48 * Currently only the properties needed on SPEAr600
49 * are provided. All other properties should be added
50 * once needed on other platforms.
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")) {
55 plat->has_gmac = 1;
56 plat->pmt = 1;
59 return 0;
61 #else
62 static int __devinit stmmac_probe_config_dt(struct platform_device *pdev,
63 struct plat_stmmacenet_data *plat,
64 const char **mac)
66 return -ENOSYS;
68 #endif /* CONFIG_OF */
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.
77 static int __devinit stmmac_pltfr_probe(struct platform_device *pdev)
79 int ret = 0;
80 struct resource *res;
81 void __iomem *addr = NULL;
82 struct stmmac_priv *priv = NULL;
83 struct plat_stmmacenet_data *plat_dat = NULL;
84 const char *mac = NULL;
86 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
87 if (!res)
88 return -ENODEV;
90 if (!request_mem_region(res->start, resource_size(res), pdev->name)) {
91 pr_err("%s: ERROR: memory allocation failed"
92 "cannot get the I/O addr 0x%x\n",
93 __func__, (unsigned int)res->start);
94 return -EBUSY;
97 addr = ioremap(res->start, resource_size(res));
98 if (!addr) {
99 pr_err("%s: ERROR: memory mapping failed", __func__);
100 ret = -ENOMEM;
101 goto out_release_region;
104 if (pdev->dev.of_node) {
105 plat_dat = devm_kzalloc(&pdev->dev,
106 sizeof(struct plat_stmmacenet_data),
107 GFP_KERNEL);
108 if (!plat_dat) {
109 pr_err("%s: ERROR: no memory", __func__);
110 ret = -ENOMEM;
111 goto out_unmap;
114 ret = stmmac_probe_config_dt(pdev, plat_dat, &mac);
115 if (ret) {
116 pr_err("%s: main dt probe failed", __func__);
117 goto out_unmap;
119 } else {
120 plat_dat = pdev->dev.platform_data;
123 /* Custom initialisation (if needed)*/
124 if (plat_dat->init) {
125 ret = plat_dat->init(pdev);
126 if (unlikely(ret))
127 goto out_unmap;
130 priv = stmmac_dvr_probe(&(pdev->dev), plat_dat, addr);
131 if (!priv) {
132 pr_err("%s: main driver probe failed", __func__);
133 goto out_unmap;
136 /* Get MAC address if available (DT) */
137 if (mac)
138 memcpy(priv->dev->dev_addr, mac, ETH_ALEN);
140 /* Get the MAC information */
141 priv->dev->irq = platform_get_irq_byname(pdev, "macirq");
142 if (priv->dev->irq == -ENXIO) {
143 pr_err("%s: ERROR: MAC IRQ configuration "
144 "information not found\n", __func__);
145 ret = -ENXIO;
146 goto out_unmap;
150 * On some platforms e.g. SPEAr the wake up irq differs from the mac irq
151 * The external wake up irq can be passed through the platform code
152 * named as "eth_wake_irq"
154 * In case the wake up interrupt is not passed from the platform
155 * so the driver will continue to use the mac irq (ndev->irq)
157 priv->wol_irq = platform_get_irq_byname(pdev, "eth_wake_irq");
158 if (priv->wol_irq == -ENXIO)
159 priv->wol_irq = priv->dev->irq;
161 priv->lpi_irq = platform_get_irq_byname(pdev, "eth_lpi");
163 platform_set_drvdata(pdev, priv->dev);
165 pr_debug("STMMAC platform driver registration completed");
167 return 0;
169 out_unmap:
170 iounmap(addr);
171 platform_set_drvdata(pdev, NULL);
173 out_release_region:
174 release_mem_region(res->start, resource_size(res));
176 return ret;
180 * stmmac_pltfr_remove
181 * @pdev: platform device pointer
182 * Description: this function calls the main to free the net resources
183 * and calls the platforms hook and release the resources (e.g. mem).
185 static int stmmac_pltfr_remove(struct platform_device *pdev)
187 struct net_device *ndev = platform_get_drvdata(pdev);
188 struct stmmac_priv *priv = netdev_priv(ndev);
189 struct resource *res;
190 int ret = stmmac_dvr_remove(ndev);
192 if (priv->plat->exit)
193 priv->plat->exit(pdev);
195 platform_set_drvdata(pdev, NULL);
197 iounmap((void __force __iomem *)priv->ioaddr);
198 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
199 release_mem_region(res->start, resource_size(res));
201 return ret;
204 #ifdef CONFIG_PM
205 static int stmmac_pltfr_suspend(struct device *dev)
207 struct net_device *ndev = dev_get_drvdata(dev);
209 return stmmac_suspend(ndev);
212 static int stmmac_pltfr_resume(struct device *dev)
214 struct net_device *ndev = dev_get_drvdata(dev);
216 return stmmac_resume(ndev);
219 int stmmac_pltfr_freeze(struct device *dev)
221 int ret;
222 struct plat_stmmacenet_data *plat_dat = dev_get_platdata(dev);
223 struct net_device *ndev = dev_get_drvdata(dev);
224 struct platform_device *pdev = to_platform_device(dev);
226 ret = stmmac_freeze(ndev);
227 if (plat_dat->exit)
228 plat_dat->exit(pdev);
230 return ret;
233 int stmmac_pltfr_restore(struct device *dev)
235 struct plat_stmmacenet_data *plat_dat = dev_get_platdata(dev);
236 struct net_device *ndev = dev_get_drvdata(dev);
237 struct platform_device *pdev = to_platform_device(dev);
239 if (plat_dat->init)
240 plat_dat->init(pdev);
242 return stmmac_restore(ndev);
245 static const struct dev_pm_ops stmmac_pltfr_pm_ops = {
246 .suspend = stmmac_pltfr_suspend,
247 .resume = stmmac_pltfr_resume,
248 .freeze = stmmac_pltfr_freeze,
249 .thaw = stmmac_pltfr_restore,
250 .restore = stmmac_pltfr_restore,
252 #else
253 static const struct dev_pm_ops stmmac_pltfr_pm_ops;
254 #endif /* CONFIG_PM */
256 static const struct of_device_id stmmac_dt_ids[] = {
257 { .compatible = "st,spear600-gmac"},
258 { .compatible = "snps,dwmac-3.70a"},
259 { .compatible = "snps,dwmac"},
260 { /* sentinel */ }
262 MODULE_DEVICE_TABLE(of, stmmac_dt_ids);
264 struct platform_driver stmmac_pltfr_driver = {
265 .probe = stmmac_pltfr_probe,
266 .remove = stmmac_pltfr_remove,
267 .driver = {
268 .name = STMMAC_RESOURCE_NAME,
269 .owner = THIS_MODULE,
270 .pm = &stmmac_pltfr_pm_ops,
271 .of_match_table = of_match_ptr(stmmac_dt_ids),
275 MODULE_DESCRIPTION("STMMAC 10/100/1000 Ethernet PLATFORM driver");
276 MODULE_AUTHOR("Giuseppe Cavallaro <peppe.cavallaro@st.com>");
277 MODULE_LICENSE("GPL");