ethernet: gmac with rgmii interface works
[firefly-linux-kernel-4.4.55.git] / drivers / net / ethernet / rockchip / gmac / 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 <linux/gpio.h>
30 #include <linux/of_gpio.h>
31 #include "stmmac.h"
32 #include <linux/rockchip/iomap.h>
33 #include <linux/rockchip/grf.h>
34
35 #define grf_readl(offset)       readl_relaxed(RK_GRF_VIRT + offset)
36 #define grf_writel(v, offset)   do { writel_relaxed(v, RK_GRF_VIRT + offset); dsb(); } while (0)
37
38 // RK3288_GRF_SOC_CON1
39 #define GMAC_PHY_INTF_SEL_RGMII ((0x01C0 << 16) | (0x0040))
40 #define GMAC_PHY_INTF_SEL_RMII  ((0x01C0 << 16) | (0x0100))
41 #define GMAC_FLOW_CTRL          ((0x0200 << 16) | (0x0200))
42 #define GMAC_FLOW_CTRL_CLR      ((0x0200 << 16) | (0x0000))
43 #define GMAC_SPEED_10M          ((0x0400 << 16) | (0x0400))
44 #define GMAC_SPEED_100M         ((0x0400 << 16) | (0x0000))
45 #define GMAC_RMII_CLK_25M       ((0x0800 << 16) | (0x0800))
46 #define GMAC_RMII_CLK_2_5M      ((0x0800 << 16) | (0x0000))
47 #define GMAC_CLK_125M           ((0x3000 << 16) | (0x0000))
48 #define GMAC_CLK_25M            ((0x3000 << 16) | (0x3000))
49 #define GMAC_CLK_2_5M           ((0x3000 << 16) | (0x2000))
50 #define GMAC_RMII_MODE          ((0x4000 << 16) | (0x4000))
51 #define GMAC_RMII_MODE_CLR      ((0x4000 << 16) | (0x0000))
52
53 // RK3288_GRF_SOC_CON3
54 #define GMAC_TXCLK_DLY_ENABLE   ((0x4000 << 16) | (0x4000))
55 #define GMAC_TXCLK_DLY_DISABLE  ((0x4000 << 16) | (0x0000))
56 #define GMAC_RXCLK_DLY_ENABLE   ((0x8000 << 16) | (0x8000))
57 #define GMAC_RXCLK_DLY_DISABLE  ((0x8000 << 16) | (0x0000))
58 #if 0
59 #define GMAC_CLK_RX_DL_CFG      ((0x3F80 << 16) | (0x0800))
60 #define GMAC_CLK_TX_DL_CFG      ((0x007F << 16) | (0x0040))
61 #else
62 #define GMAC_CLK_RX_DL_CFG(val) ((0x3F80 << 16) | (val<<7))        // 7bit
63 #define GMAC_CLK_TX_DL_CFG(val) ((0x007F << 16) | (val))           // 7bit
64 #endif
65 struct bsp_priv g_bsp_priv;
66
67 static int phy_power_on(struct plat_stmmacenet_data *plat, int enable)
68 {
69         struct bsp_priv * bsp_priv;
70
71         printk("enter %s ,enable = %d \n",__func__,enable);
72
73         if ((plat) && (plat->bsp_priv)) {
74                 bsp_priv = plat->bsp_priv;
75         } else {
76                 printk("ERROR: platform data or private data is NULL. %s\n", __FUNCTION__);
77                 return -1;
78         }
79
80         if (enable) {
81                 //power on
82                 if (gpio_is_valid(bsp_priv->power_io)) {
83                         gpio_direction_output(bsp_priv->power_io, 0);
84                         gpio_set_value(bsp_priv->power_io, 1);
85                 }
86
87                 //reset
88                 if (gpio_is_valid(bsp_priv->reset_io)) {
89                         gpio_direction_output(bsp_priv->reset_io, 0);
90                         gpio_set_value(bsp_priv->reset_io, 0);
91                         msleep(10);
92                         gpio_set_value(bsp_priv->reset_io, 1);
93                 }
94         } else {
95                 //power off
96                 if (gpio_is_valid(bsp_priv->power_io)) {
97                         gpio_direction_output(bsp_priv->power_io, 0);
98                         gpio_set_value(bsp_priv->power_io, 0);
99                 }
100         }
101
102         return 0;
103 }
104
105 int stmmc_pltfr_init(struct platform_device *pdev) {
106         struct pinctrl_state *gmac_state;
107         int phy_iface;
108         int err;
109
110         printk("enter func %s...\n", __func__);
111
112 //iomux
113 #if 0
114         if ((pdev->dev.pins) && (pdev->dev.pins->p)) {
115                 gmac_state = pinctrl_lookup_state(pdev->dev.pins->p, "default");
116                 if (IS_ERR(gmac_state)) {
117                                 dev_err(&pdev->dev, "no gmc pinctrl state\n");
118                                 return -1;
119                 }
120
121                 pinctrl_select_state(pdev->dev.pins->p, gmac_state);
122         }
123 #endif
124
125         struct bsp_priv * bsp_priv = &g_bsp_priv;
126         phy_iface = bsp_priv->phy_iface;
127 //power
128         if (!gpio_is_valid(bsp_priv->power_io)) {
129                 printk("%s: ERROR: Get power-gpio failed.\n", __func__);
130                 //return -EINVAL;
131         }
132
133         err = gpio_request(bsp_priv->power_io, "gmac_phy_power");
134         if (err) {
135                 printk("%s: ERROR: Request gmac phy power pin failed.\n", __func__);
136                 //return -EINVAL;
137         }
138
139         if (!gpio_is_valid(bsp_priv->reset_io)) {
140                 printk("%s: ERROR: Get reset-gpio failed.\n", __func__);
141                 //return -EINVAL;
142         }
143
144         err = gpio_request(bsp_priv->reset_io, "gmac_phy_reset");
145         if (err) {
146                 printk("%s: ERROR: Request gmac phy reset pin failed.\n", __func__);
147                 //return -EINVAL;
148         }
149
150 //rmii or rgmii
151         if (phy_iface & PHY_INTERFACE_MODE_RGMII) {
152                 printk("init for RGMII\n");
153                 grf_writel(GMAC_PHY_INTF_SEL_RGMII, RK3288_GRF_SOC_CON1);
154                 grf_writel(GMAC_RMII_MODE_CLR, RK3288_GRF_SOC_CON1);
155                 grf_writel(GMAC_RXCLK_DLY_ENABLE, RK3288_GRF_SOC_CON3);
156                 grf_writel(GMAC_TXCLK_DLY_ENABLE, RK3288_GRF_SOC_CON3);
157                 grf_writel(GMAC_CLK_RX_DL_CFG(0x10), RK3288_GRF_SOC_CON3);
158                 grf_writel(GMAC_CLK_TX_DL_CFG(0x40), RK3288_GRF_SOC_CON3);
159         } else if (phy_iface & PHY_INTERFACE_MODE_RMII) {
160                 printk("init for RMII\n");
161                 grf_writel(GMAC_PHY_INTF_SEL_RMII, RK3288_GRF_SOC_CON1);
162                 grf_writel(GMAC_RMII_MODE, RK3288_GRF_SOC_CON1);
163         } else {
164                 printk("ERROR: NO interface defined!\n");
165         }
166
167         return 0;
168 }
169
170 void * stmmc_pltfr_fix_mac_speed(void *priv, unsigned int speed){
171         printk("enter func %s...\n", __func__);
172         struct bsp_priv * bsp_priv = priv;
173         int interface;
174
175         printk("fix speed to %d\n", speed);
176
177         if (bsp_priv) {
178                 interface = bsp_priv->phy_iface;
179         }
180
181         if (interface & PHY_INTERFACE_MODE_RGMII) {
182                 printk("fix speed for RGMII\n");
183
184                 switch (speed) {
185                         case 10: {
186                                 grf_writel(GMAC_CLK_2_5M, RK3288_GRF_SOC_CON1);
187                                 break;
188                         }
189                         case 100: {
190                                 grf_writel(GMAC_CLK_25M, RK3288_GRF_SOC_CON1);
191                                 break;
192                         }
193                         case 1000: {
194                                 grf_writel(GMAC_CLK_125M, RK3288_GRF_SOC_CON1);
195                                 break;
196                         }
197                         default: {
198                                 printk("ERROR: speed %d is not defined!\n");
199                         }
200                 }
201
202         } else if (interface & PHY_INTERFACE_MODE_RMII) {
203                 printk("fix speed for RMII\n");
204                 switch (speed) {
205                         case 10: {
206                                 grf_writel(GMAC_RMII_CLK_2_5M, RK3288_GRF_SOC_CON1);
207                                 break;
208                         }
209                         case 100: {
210                                 grf_writel(GMAC_RMII_CLK_25M, RK3288_GRF_SOC_CON1);
211                                 break;
212                         }
213                         default: {
214                                 printk("ERROR: speed %d is not defined!\n");
215                         }
216                 }
217         } else {
218                 printk("ERROR: NO interface defined!\n");
219         }
220
221         return NULL;
222 }
223
224
225 #ifdef CONFIG_OF
226 static int stmmac_probe_config_dt(struct platform_device *pdev,
227                                   struct plat_stmmacenet_data *plat,
228                                   const char **mac)
229 {
230         struct device_node *np = pdev->dev.of_node;
231         enum of_gpio_flags flags;
232
233         if (!np)
234                 return -ENODEV;
235
236         *mac = of_get_mac_address(np);
237         plat->interface = of_get_phy_mode(np);
238         plat->mdio_bus_data = devm_kzalloc(&pdev->dev,
239                                            sizeof(struct stmmac_mdio_bus_data),
240                                            GFP_KERNEL);
241
242         plat->init = stmmc_pltfr_init;
243         plat->fix_mac_speed = stmmc_pltfr_fix_mac_speed;
244
245         g_bsp_priv.reset_io = 
246                         of_get_named_gpio_flags(np, "reset-gpio", 0, &flags);
247         g_bsp_priv.power_io = 
248                         of_get_named_gpio_flags(np, "power-gpio", 0, &flags);
249
250         g_bsp_priv.phy_iface = plat->interface;
251         g_bsp_priv.phy_power_on = phy_power_on;
252
253         plat->bsp_priv = &g_bsp_priv;
254
255         /*
256          * Currently only the properties needed on SPEAr600
257          * are provided. All other properties should be added
258          * once needed on other platforms.
259          */
260         if (of_device_is_compatible(np, "rockchip,gmac")) {
261                 plat->has_gmac = 1;
262                 plat->pmt = 1;
263         }
264
265         return 0;
266 }
267 #else
268 static int stmmac_probe_config_dt(struct platform_device *pdev,
269                                   struct plat_stmmacenet_data *plat,
270                                   const char **mac)
271 {
272         return -ENOSYS;
273 }
274 #endif /* CONFIG_OF */
275
276 /**
277  * stmmac_pltfr_probe
278  * @pdev: platform device pointer
279  * Description: platform_device probe function. It allocates
280  * the necessary resources and invokes the main to init
281  * the net device, register the mdio bus etc.
282  */
283 static int stmmac_pltfr_probe(struct platform_device *pdev)
284 {
285         int ret = 0;
286         struct resource *res;
287         struct device *dev = &pdev->dev;
288         void __iomem *addr = NULL;
289         struct stmmac_priv *priv = NULL;
290         struct plat_stmmacenet_data *plat_dat = NULL;
291         const char *mac = NULL;
292
293         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
294         if (!res)
295                 return -ENODEV;
296
297         addr = devm_ioremap_resource(dev, res);
298         if (IS_ERR(addr))
299                 return PTR_ERR(addr);
300
301         if (pdev->dev.of_node) {
302                 plat_dat = devm_kzalloc(&pdev->dev,
303                                         sizeof(struct plat_stmmacenet_data),
304                                         GFP_KERNEL);
305                 if (!plat_dat) {
306                         pr_err("%s: ERROR: no memory", __func__);
307                         return  -ENOMEM;
308                 }
309
310                 ret = stmmac_probe_config_dt(pdev, plat_dat, &mac);
311                 if (ret) {
312                         pr_err("%s: main dt probe failed", __func__);
313                         return ret;
314                 }
315         } else {
316                 plat_dat = pdev->dev.platform_data;
317         }
318
319         /* Custom initialisation (if needed)*/
320         if (plat_dat->init) {
321                 ret = plat_dat->init(pdev);
322                 if (unlikely(ret))
323                         return ret;
324         }
325
326         priv = stmmac_dvr_probe(&(pdev->dev), plat_dat, addr);
327         if (!priv) {
328                 pr_err("%s: main driver probe failed", __func__);
329                 return -ENODEV;
330         }
331
332         /* Get MAC address if available (DT) */
333         if (mac)
334                 memcpy(priv->dev->dev_addr, mac, ETH_ALEN);
335
336         /* Get the MAC information */
337         priv->dev->irq = platform_get_irq_byname(pdev, "macirq");
338         if (priv->dev->irq == -ENXIO) {
339                 pr_err("%s: ERROR: MAC IRQ configuration "
340                        "information not found\n", __func__);
341                 return -ENXIO;
342         }
343
344         /*
345          * On some platforms e.g. SPEAr the wake up irq differs from the mac irq
346          * The external wake up irq can be passed through the platform code
347          * named as "eth_wake_irq"
348          *
349          * In case the wake up interrupt is not passed from the platform
350          * so the driver will continue to use the mac irq (ndev->irq)
351          */
352         priv->wol_irq = platform_get_irq_byname(pdev, "eth_wake_irq");
353         if (priv->wol_irq == -ENXIO)
354                 priv->wol_irq = priv->dev->irq;
355
356         priv->lpi_irq = platform_get_irq_byname(pdev, "eth_lpi");
357
358         platform_set_drvdata(pdev, priv->dev);
359
360         pr_debug("STMMAC platform driver registration completed");
361
362         return 0;
363 }
364
365 /**
366  * stmmac_pltfr_remove
367  * @pdev: platform device pointer
368  * Description: this function calls the main to free the net resources
369  * and calls the platforms hook and release the resources (e.g. mem).
370  */
371 static int stmmac_pltfr_remove(struct platform_device *pdev)
372 {
373         struct net_device *ndev = platform_get_drvdata(pdev);
374         struct stmmac_priv *priv = netdev_priv(ndev);
375         int ret = stmmac_dvr_remove(ndev);
376
377         if (priv->plat->exit)
378                 priv->plat->exit(pdev);
379
380         platform_set_drvdata(pdev, NULL);
381
382         return ret;
383 }
384
385 #ifdef CONFIG_PM
386 static int stmmac_pltfr_suspend(struct device *dev)
387 {
388         struct net_device *ndev = dev_get_drvdata(dev);
389
390         return stmmac_suspend(ndev);
391 }
392
393 static int stmmac_pltfr_resume(struct device *dev)
394 {
395         struct net_device *ndev = dev_get_drvdata(dev);
396
397         return stmmac_resume(ndev);
398 }
399
400 int stmmac_pltfr_freeze(struct device *dev)
401 {
402         int ret;
403         struct plat_stmmacenet_data *plat_dat = dev_get_platdata(dev);
404         struct net_device *ndev = dev_get_drvdata(dev);
405         struct platform_device *pdev = to_platform_device(dev);
406
407         ret = stmmac_freeze(ndev);
408         if (plat_dat->exit)
409                 plat_dat->exit(pdev);
410
411         return ret;
412 }
413
414 int stmmac_pltfr_restore(struct device *dev)
415 {
416         struct plat_stmmacenet_data *plat_dat = dev_get_platdata(dev);
417         struct net_device *ndev = dev_get_drvdata(dev);
418         struct platform_device *pdev = to_platform_device(dev);
419
420         if (plat_dat->init)
421                 plat_dat->init(pdev);
422
423         return stmmac_restore(ndev);
424 }
425
426 static const struct dev_pm_ops stmmac_pltfr_pm_ops = {
427         .suspend = stmmac_pltfr_suspend,
428         .resume = stmmac_pltfr_resume,
429         .freeze = stmmac_pltfr_freeze,
430         .thaw = stmmac_pltfr_restore,
431         .restore = stmmac_pltfr_restore,
432 };
433 #else
434 static const struct dev_pm_ops stmmac_pltfr_pm_ops;
435 #endif /* CONFIG_PM */
436
437 static const struct of_device_id stmmac_dt_ids[] = {
438         { .compatible = "rockchip,gmac"},
439         { /* sentinel */ }
440 };
441 MODULE_DEVICE_TABLE(of, stmmac_dt_ids);
442
443 struct platform_driver stmmac_pltfr_driver = {
444         .probe = stmmac_pltfr_probe,
445         .remove = stmmac_pltfr_remove,
446         .driver = {
447                    .name = STMMAC_RESOURCE_NAME,
448                    .owner = THIS_MODULE,
449                    .pm = &stmmac_pltfr_pm_ops,
450                    .of_match_table = of_match_ptr(stmmac_dt_ids),
451                    },
452 };
453
454 MODULE_DESCRIPTION("STMMAC 10/100/1000 Ethernet PLATFORM driver");
455 MODULE_AUTHOR("Giuseppe Cavallaro <peppe.cavallaro@st.com>");
456 MODULE_LICENSE("GPL");