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