]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/net/ethernet/stmicro/stmmac/stmmac_platform.c
Merge git://git.kernel.org/pub/scm/linux/kernel/git/davem/net
[karo-tx-linux.git] / drivers / net / ethernet / stmicro / stmmac / stmmac_platform.c
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 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         struct stmmac_dma_cfg *dma_cfg;
38
39         if (!np)
40                 return -ENODEV;
41
42         *mac = of_get_mac_address(np);
43         plat->interface = of_get_phy_mode(np);
44
45         plat->bus_id = of_alias_get_id(np, "ethernet");
46         if (plat->bus_id < 0)
47                 plat->bus_id = 0;
48
49         of_property_read_u32(np, "snps,phy-addr", &plat->phy_addr);
50
51         plat->mdio_bus_data = devm_kzalloc(&pdev->dev,
52                                            sizeof(struct stmmac_mdio_bus_data),
53                                            GFP_KERNEL);
54
55         /*
56          * Currently only the properties needed on SPEAr600
57          * are provided. All other properties should be added
58          * once needed on other platforms.
59          */
60         if (of_device_is_compatible(np, "st,spear600-gmac") ||
61                 of_device_is_compatible(np, "snps,dwmac-3.70a") ||
62                 of_device_is_compatible(np, "snps,dwmac")) {
63                 plat->has_gmac = 1;
64                 plat->pmt = 1;
65         }
66
67         if (of_device_is_compatible(np, "snps,dwmac-3.610") ||
68                 of_device_is_compatible(np, "snps,dwmac-3.710")) {
69                 plat->enh_desc = 1;
70                 plat->bugged_jumbo = 1;
71                 plat->force_sf_dma_mode = 1;
72         }
73
74         if (of_find_property(np, "snps,pbl", NULL)) {
75                 dma_cfg = devm_kzalloc(&pdev->dev, sizeof(*dma_cfg),
76                                        GFP_KERNEL);
77                 if (!dma_cfg)
78                         return -ENOMEM;
79                 plat->dma_cfg = dma_cfg;
80                 of_property_read_u32(np, "snps,pbl", &dma_cfg->pbl);
81                 dma_cfg->fixed_burst =
82                         of_property_read_bool(np, "snps,fixed-burst");
83                 dma_cfg->mixed_burst =
84                         of_property_read_bool(np, "snps,mixed-burst");
85         }
86         plat->force_thresh_dma_mode = of_property_read_bool(np, "snps,force_thresh_dma_mode");
87         if (plat->force_thresh_dma_mode) {
88                 plat->force_sf_dma_mode = 0;
89                 pr_warn("force_sf_dma_mode is ignored if force_thresh_dma_mode is set.");
90
91         return 0;
92 }
93 #else
94 static int stmmac_probe_config_dt(struct platform_device *pdev,
95                                   struct plat_stmmacenet_data *plat,
96                                   const char **mac)
97 {
98         return -ENOSYS;
99 }
100 #endif /* CONFIG_OF */
101
102 /**
103  * stmmac_pltfr_probe
104  * @pdev: platform device pointer
105  * Description: platform_device probe function. It allocates
106  * the necessary resources and invokes the main to init
107  * the net device, register the mdio bus etc.
108  */
109 static int stmmac_pltfr_probe(struct platform_device *pdev)
110 {
111         int ret = 0;
112         struct resource *res;
113         struct device *dev = &pdev->dev;
114         void __iomem *addr = NULL;
115         struct stmmac_priv *priv = NULL;
116         struct plat_stmmacenet_data *plat_dat = NULL;
117         const char *mac = NULL;
118
119         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
120         addr = devm_ioremap_resource(dev, res);
121         if (IS_ERR(addr))
122                 return PTR_ERR(addr);
123
124         plat_dat = dev_get_platdata(&pdev->dev);
125         if (pdev->dev.of_node) {
126                 if (!plat_dat)
127                         plat_dat = devm_kzalloc(&pdev->dev,
128                                         sizeof(struct plat_stmmacenet_data),
129                                         GFP_KERNEL);
130                 if (!plat_dat) {
131                         pr_err("%s: ERROR: no memory", __func__);
132                         return  -ENOMEM;
133                 }
134
135                 ret = stmmac_probe_config_dt(pdev, plat_dat, &mac);
136                 if (ret) {
137                         pr_err("%s: main dt probe failed", __func__);
138                         return ret;
139                 }
140         }
141
142         /* Custom initialisation (if needed)*/
143         if (plat_dat->init) {
144                 ret = plat_dat->init(pdev);
145                 if (unlikely(ret))
146                         return ret;
147         }
148
149         priv = stmmac_dvr_probe(&(pdev->dev), plat_dat, addr);
150         if (!priv) {
151                 pr_err("%s: main driver probe failed", __func__);
152                 return -ENODEV;
153         }
154
155         /* Get MAC address if available (DT) */
156         if (mac)
157                 memcpy(priv->dev->dev_addr, mac, ETH_ALEN);
158
159         /* Get the MAC information */
160         priv->dev->irq = platform_get_irq_byname(pdev, "macirq");
161         if (priv->dev->irq == -ENXIO) {
162                 pr_err("%s: ERROR: MAC IRQ configuration "
163                        "information not found\n", __func__);
164                 return -ENXIO;
165         }
166
167         /*
168          * On some platforms e.g. SPEAr the wake up irq differs from the mac irq
169          * The external wake up irq can be passed through the platform code
170          * named as "eth_wake_irq"
171          *
172          * In case the wake up interrupt is not passed from the platform
173          * so the driver will continue to use the mac irq (ndev->irq)
174          */
175         priv->wol_irq = platform_get_irq_byname(pdev, "eth_wake_irq");
176         if (priv->wol_irq == -ENXIO)
177                 priv->wol_irq = priv->dev->irq;
178
179         priv->lpi_irq = platform_get_irq_byname(pdev, "eth_lpi");
180
181         platform_set_drvdata(pdev, priv->dev);
182
183         pr_debug("STMMAC platform driver registration completed");
184
185         return 0;
186 }
187
188 /**
189  * stmmac_pltfr_remove
190  * @pdev: platform device pointer
191  * Description: this function calls the main to free the net resources
192  * and calls the platforms hook and release the resources (e.g. mem).
193  */
194 static int stmmac_pltfr_remove(struct platform_device *pdev)
195 {
196         struct net_device *ndev = platform_get_drvdata(pdev);
197         struct stmmac_priv *priv = netdev_priv(ndev);
198         int ret = stmmac_dvr_remove(ndev);
199
200         if (priv->plat->exit)
201                 priv->plat->exit(pdev);
202
203         return ret;
204 }
205
206 #ifdef CONFIG_PM
207 static int stmmac_pltfr_suspend(struct device *dev)
208 {
209         struct net_device *ndev = dev_get_drvdata(dev);
210
211         return stmmac_suspend(ndev);
212 }
213
214 static int stmmac_pltfr_resume(struct device *dev)
215 {
216         struct net_device *ndev = dev_get_drvdata(dev);
217
218         return stmmac_resume(ndev);
219 }
220
221 int stmmac_pltfr_freeze(struct device *dev)
222 {
223         int ret;
224         struct plat_stmmacenet_data *plat_dat = dev_get_platdata(dev);
225         struct net_device *ndev = dev_get_drvdata(dev);
226         struct platform_device *pdev = to_platform_device(dev);
227
228         ret = stmmac_freeze(ndev);
229         if (plat_dat->exit)
230                 plat_dat->exit(pdev);
231
232         return ret;
233 }
234
235 int stmmac_pltfr_restore(struct device *dev)
236 {
237         struct plat_stmmacenet_data *plat_dat = dev_get_platdata(dev);
238         struct net_device *ndev = dev_get_drvdata(dev);
239         struct platform_device *pdev = to_platform_device(dev);
240
241         if (plat_dat->init)
242                 plat_dat->init(pdev);
243
244         return stmmac_restore(ndev);
245 }
246
247 static const struct dev_pm_ops stmmac_pltfr_pm_ops = {
248         .suspend = stmmac_pltfr_suspend,
249         .resume = stmmac_pltfr_resume,
250         .freeze = stmmac_pltfr_freeze,
251         .thaw = stmmac_pltfr_restore,
252         .restore = stmmac_pltfr_restore,
253 };
254 #else
255 static const struct dev_pm_ops stmmac_pltfr_pm_ops;
256 #endif /* CONFIG_PM */
257
258 static const struct of_device_id stmmac_dt_ids[] = {
259         { .compatible = "st,spear600-gmac"},
260         { .compatible = "snps,dwmac-3.610"},
261         { .compatible = "snps,dwmac-3.70a"},
262         { .compatible = "snps,dwmac-3.710"},
263         { .compatible = "snps,dwmac"},
264         { /* sentinel */ }
265 };
266 MODULE_DEVICE_TABLE(of, stmmac_dt_ids);
267
268 struct platform_driver stmmac_pltfr_driver = {
269         .probe = stmmac_pltfr_probe,
270         .remove = stmmac_pltfr_remove,
271         .driver = {
272                    .name = STMMAC_RESOURCE_NAME,
273                    .owner = THIS_MODULE,
274                    .pm = &stmmac_pltfr_pm_ops,
275                    .of_match_table = of_match_ptr(stmmac_dt_ids),
276                    },
277 };
278
279 MODULE_DESCRIPTION("STMMAC 10/100/1000 Ethernet PLATFORM driver");
280 MODULE_AUTHOR("Giuseppe Cavallaro <peppe.cavallaro@st.com>");
281 MODULE_LICENSE("GPL");