Merge branch develop-3.10
[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 <linux/of_device.h>
32 #include <dt-bindings/gpio/gpio.h>
33 #include "stmmac.h"
34 #include <linux/rockchip/iomap.h>
35 #include <linux/rockchip/grf.h>
36 #include <linux/regulator/consumer.h>
37
38 #define grf_readl(offset)       readl_relaxed(RK_GRF_VIRT + offset)
39 #define grf_writel(v, offset)   do { writel_relaxed(v, RK_GRF_VIRT + offset); dsb(sy); } while (0)
40
41 //RK3288_GRF_SOC_CON1
42 //RK3128_GRF_MAC_CON1
43 #define GMAC_PHY_INTF_SEL_RGMII ((0x01C0 << 16) | (0x0040))
44 #define GMAC_PHY_INTF_SEL_RMII  ((0x01C0 << 16) | (0x0100))
45 #define GMAC_FLOW_CTRL          ((0x0200 << 16) | (0x0200))
46 #define GMAC_FLOW_CTRL_CLR      ((0x0200 << 16) | (0x0000))
47 #define GMAC_SPEED_10M          ((0x0400 << 16) | (0x0000))
48 #define GMAC_SPEED_100M         ((0x0400 << 16) | (0x0400))
49 #define GMAC_RMII_CLK_25M       ((0x0800 << 16) | (0x0800))
50 #define GMAC_RMII_CLK_2_5M      ((0x0800 << 16) | (0x0000))
51 #define GMAC_CLK_125M           ((0x3000 << 16) | (0x0000))
52 #define GMAC_CLK_25M            ((0x3000 << 16) | (0x3000))
53 #define GMAC_CLK_2_5M           ((0x3000 << 16) | (0x2000))
54 #define GMAC_RMII_MODE          ((0x4000 << 16) | (0x4000))
55 #define GMAC_RMII_MODE_CLR      ((0x4000 << 16) | (0x0000))
56
57 //RK3288_GRF_SOC_CON3
58 //RK3128_GRF_MAC_CON0
59 #define GMAC_TXCLK_DLY_ENABLE   ((0x4000 << 16) | (0x4000))
60 #define GMAC_TXCLK_DLY_DISABLE  ((0x4000 << 16) | (0x0000))
61 #define GMAC_RXCLK_DLY_ENABLE   ((0x8000 << 16) | (0x8000))
62 #define GMAC_RXCLK_DLY_DISABLE  ((0x8000 << 16) | (0x0000))
63 #define GMAC_CLK_RX_DL_CFG(val) ((0x3F80 << 16) | (val<<7))        // 7bit
64 #define GMAC_CLK_TX_DL_CFG(val) ((0x007F << 16) | (val))           // 7bit
65
66 static void SET_RGMII(int type, int tx_delay, int rx_delay)
67 {
68     if (type == RK3288_GMAC) {
69         grf_writel(GMAC_PHY_INTF_SEL_RGMII, RK3288_GRF_SOC_CON1);
70         grf_writel(GMAC_RMII_MODE_CLR, RK3288_GRF_SOC_CON1);
71         grf_writel(GMAC_RXCLK_DLY_ENABLE, RK3288_GRF_SOC_CON3);
72         grf_writel(GMAC_TXCLK_DLY_ENABLE, RK3288_GRF_SOC_CON3);
73         grf_writel(GMAC_CLK_RX_DL_CFG(rx_delay), RK3288_GRF_SOC_CON3);
74         grf_writel(GMAC_CLK_TX_DL_CFG(tx_delay), RK3288_GRF_SOC_CON3);
75         pr_info("tx delay=0x%x\nrx delay=0x%x\n", tx_delay, rx_delay);
76         //grf_writel(0xffffffff,RK3288_GRF_GPIO3D_E);
77         //grf_writel(grf_readl(RK3288_GRF_GPIO4B_E) | 0x3<<2<<16 | 0x3<<2, RK3288_GRF_GPIO4B_E);
78         //grf_writel(0xffffffff,RK3288_GRF_GPIO4A_E);
79     } else if (type == RK312X_GMAC) {
80         grf_writel(GMAC_PHY_INTF_SEL_RGMII, RK312X_GRF_MAC_CON1);
81         grf_writel(GMAC_RMII_MODE_CLR, RK312X_GRF_MAC_CON1);
82         grf_writel(GMAC_RXCLK_DLY_ENABLE, RK312X_GRF_MAC_CON0);
83         grf_writel(GMAC_TXCLK_DLY_ENABLE, RK312X_GRF_MAC_CON0);
84         grf_writel(GMAC_CLK_RX_DL_CFG(rx_delay), RK312X_GRF_MAC_CON0);
85         grf_writel(GMAC_CLK_TX_DL_CFG(tx_delay), RK312X_GRF_MAC_CON0);
86         pr_info("tx delay=0x%x\nrx delay=0x%x\n", tx_delay, rx_delay);
87     }
88 }
89
90 static void SET_RMII(int type)
91 {
92     if (type == RK3288_GMAC) {
93         grf_writel(GMAC_PHY_INTF_SEL_RMII, RK3288_GRF_SOC_CON1);
94         grf_writel(GMAC_RMII_MODE, RK3288_GRF_SOC_CON1);
95     } else if (type == RK312X_GMAC) {
96         grf_writel(GMAC_PHY_INTF_SEL_RMII, RK312X_GRF_MAC_CON1);
97         grf_writel(GMAC_RMII_MODE, RK312X_GRF_MAC_CON1);
98     }
99 }
100
101 static void SET_RGMII_10M(int type)
102 {
103     if (type == RK3288_GMAC) {
104         grf_writel(GMAC_CLK_2_5M, RK3288_GRF_SOC_CON1);
105     } else if (type == RK312X_GMAC) {
106         grf_writel(GMAC_CLK_2_5M, RK312X_GRF_MAC_CON1);
107     }
108 }
109
110 static void SET_RGMII_100M(int type)
111 {
112     if (type == RK3288_GMAC) {
113         grf_writel(GMAC_CLK_25M, RK3288_GRF_SOC_CON1);
114     } else if (type == RK312X_GMAC) {
115         grf_writel(GMAC_CLK_25M, RK312X_GRF_MAC_CON1);
116     }
117 }
118
119 static void SET_RGMII_1000M(int type)
120 {
121     if (type == RK3288_GMAC) {
122         grf_writel(GMAC_CLK_125M, RK3288_GRF_SOC_CON1);
123     } else if (type == RK312X_GMAC) {
124         grf_writel(GMAC_CLK_125M, RK312X_GRF_MAC_CON1);
125     }
126 }
127
128 static void SET_RMII_10M(int type)
129 {
130     if (type == RK3288_GMAC) {
131         grf_writel(GMAC_RMII_CLK_2_5M, RK3288_GRF_SOC_CON1);
132         grf_writel(GMAC_SPEED_10M, RK3288_GRF_SOC_CON1);
133     } else if (type == RK312X_GMAC) {
134         grf_writel(GMAC_RMII_CLK_2_5M, RK312X_GRF_MAC_CON1);
135         grf_writel(GMAC_SPEED_10M, RK312X_GRF_MAC_CON1);
136     }
137 }
138
139 static void SET_RMII_100M(int type)
140 {
141     if (type == RK3288_GMAC) {
142         grf_writel(GMAC_RMII_CLK_25M, RK3288_GRF_SOC_CON1);
143         grf_writel(GMAC_SPEED_100M, RK3288_GRF_SOC_CON1);
144     } else if (type == RK312X_GMAC) {
145         grf_writel(GMAC_RMII_CLK_25M, RK312X_GRF_MAC_CON1);
146         grf_writel(GMAC_SPEED_100M, RK312X_GRF_MAC_CON1);
147     }
148 }
149
150 struct bsp_priv g_bsp_priv;
151
152 int gmac_clk_init(struct device *device)
153 {
154         struct bsp_priv * bsp_priv = &g_bsp_priv;
155
156         bsp_priv->clk_enable = false;
157
158         bsp_priv->mac_clk_rx = clk_get(device,"mac_clk_rx");
159         if (IS_ERR(bsp_priv->mac_clk_rx)) {
160                 pr_warn("%s: warning: cannot get mac_clk_rx clock\n", __func__);
161         }
162
163         bsp_priv->mac_clk_tx = clk_get(device,"mac_clk_tx");
164         if (IS_ERR(bsp_priv->mac_clk_tx)) {
165                 pr_warn("%s: warning: cannot get mac_clk_tx clock\n", __func__);
166         }
167
168         bsp_priv->clk_mac_ref = clk_get(device,"clk_mac_ref");
169         if (IS_ERR(bsp_priv->clk_mac_ref)) {
170                 pr_warn("%s: warning: cannot get clk_mac_ref clock\n", __func__);
171         }
172
173         bsp_priv->clk_mac_refout = clk_get(device,"clk_mac_refout");
174         if (IS_ERR(bsp_priv->clk_mac_refout)) {
175                 pr_warn("%s: warning: cannot get clk_mac_refout clock\n", __func__);
176         }
177
178         bsp_priv->aclk_mac = clk_get(device,"aclk_mac");
179         if (IS_ERR(bsp_priv->aclk_mac)) {
180                 pr_warn("%s: warning: cannot get aclk_mac clock\n", __func__);
181         }
182
183         bsp_priv->pclk_mac = clk_get(device,"pclk_mac");
184         if (IS_ERR(bsp_priv->pclk_mac)) {
185                 pr_warn("%s: warning: cannot get pclk_mac clock\n", __func__);
186         }
187
188         bsp_priv->clk_mac_pll = clk_get(device,"clk_mac_pll");
189         if (IS_ERR(bsp_priv->clk_mac_pll)) {
190                 pr_warn("%s: warning: cannot get clk_mac_pll clock\n", __func__);
191         }
192
193         bsp_priv->gmac_clkin = clk_get(device,"gmac_clkin");
194         if (IS_ERR(bsp_priv->gmac_clkin)) {
195                 pr_warn("%s: warning: cannot get gmac_clkin clock\n", __func__);
196         }
197
198         bsp_priv->clk_mac = clk_get(device, "clk_mac");
199         if (IS_ERR(bsp_priv->clk_mac)) {
200                 pr_warn("%s: warning: cannot get clk_mac clock\n", __func__);
201         }
202
203         if (bsp_priv->clock_input) {
204                 if (bsp_priv->phy_iface == PHY_INTERFACE_MODE_RMII) {
205                         clk_set_rate(bsp_priv->gmac_clkin, 50000000);
206                 }
207                 clk_set_parent(bsp_priv->clk_mac, bsp_priv->gmac_clkin);
208         } else {
209                 if (bsp_priv->phy_iface == PHY_INTERFACE_MODE_RMII) {
210                         clk_set_rate(bsp_priv->clk_mac_pll, 50000000);
211                 }
212                 clk_set_parent(bsp_priv->clk_mac, bsp_priv->clk_mac_pll);
213         }
214         return 0;
215 }
216
217 static int gmac_clk_enable(bool enable) {
218         int phy_iface = -1;
219         struct bsp_priv * bsp_priv = &g_bsp_priv;
220         phy_iface = bsp_priv->phy_iface;
221
222         if (enable) {
223                 if (!bsp_priv->clk_enable) {
224                         if (phy_iface == PHY_INTERFACE_MODE_RMII) {
225                                 if (!IS_ERR(bsp_priv->mac_clk_rx))
226                                         clk_prepare_enable(bsp_priv->mac_clk_rx);
227
228                                 if (!IS_ERR(bsp_priv->clk_mac_ref))
229                                         clk_prepare_enable(bsp_priv->clk_mac_ref);
230
231                                 if (!IS_ERR(bsp_priv->clk_mac_refout))
232                                         clk_prepare_enable(bsp_priv->clk_mac_refout);
233                         }
234
235                         if (!IS_ERR(bsp_priv->aclk_mac))
236                                 clk_prepare_enable(bsp_priv->aclk_mac);
237
238                         if (!IS_ERR(bsp_priv->pclk_mac))
239                                 clk_prepare_enable(bsp_priv->pclk_mac);
240
241                         if (!IS_ERR(bsp_priv->mac_clk_tx))
242                                 clk_prepare_enable(bsp_priv->mac_clk_tx);
243
244                         if (!IS_ERR(bsp_priv->clk_mac))
245                                 clk_prepare_enable(bsp_priv->clk_mac);
246
247                         mdelay(5);
248                         bsp_priv->clk_enable = true;
249                 }
250         } else {
251                 if (bsp_priv->clk_enable) {
252                         if (phy_iface == PHY_INTERFACE_MODE_RMII) {
253                                 if (!IS_ERR(bsp_priv->mac_clk_rx))
254                                         clk_disable_unprepare(bsp_priv->mac_clk_rx);
255
256                                 if (!IS_ERR(bsp_priv->clk_mac_ref))
257                                         clk_disable_unprepare(bsp_priv->clk_mac_ref);
258
259                                 if (!IS_ERR(bsp_priv->clk_mac_refout))
260                                         clk_disable_unprepare(bsp_priv->clk_mac_refout);
261                         }
262
263                         if (!IS_ERR(bsp_priv->aclk_mac))
264                                 clk_disable_unprepare(bsp_priv->aclk_mac);
265
266                         if (!IS_ERR(bsp_priv->pclk_mac))
267                                 clk_disable_unprepare(bsp_priv->pclk_mac);
268
269                         if (!IS_ERR(bsp_priv->mac_clk_tx))
270                                 clk_disable_unprepare(bsp_priv->mac_clk_tx);
271
272                         if (!IS_ERR(bsp_priv->clk_mac))
273                                 clk_disable_unprepare(bsp_priv->clk_mac);
274
275                         bsp_priv->clk_enable = false;
276                 }
277         }
278
279         return 0;
280 }
281
282 static int power_on_by_pmu(bool enable) {
283         struct bsp_priv * bsp_priv = &g_bsp_priv;
284         struct regulator * ldo;
285         char * ldostr = bsp_priv->pmu_regulator;
286         int ret;
287
288         if (ldostr == NULL) {
289                 pr_err("%s: no ldo found\n", __func__);
290                 return -1;
291         }
292
293         ldo = regulator_get(NULL, ldostr);
294         if (ldo == NULL) {
295                 pr_err("\n%s get ldo %s failed\n", __func__, ldostr);
296         } else {
297                 if (enable) {
298                         if(!regulator_is_enabled(ldo)) {
299                                 regulator_set_voltage(ldo, 3300000, 3300000);
300                                 ret = regulator_enable(ldo);
301                                 if(ret != 0){
302                                         pr_err("%s: faild to enable %s\n", __func__, ldostr);
303                                 } else {
304                                         pr_info("turn on ldo done.\n");
305                                 }
306                         } else {
307                                 pr_warn("%s is enabled before enable", ldostr);
308                         }
309                 } else {
310                         if(regulator_is_enabled(ldo)) {
311                                 ret = regulator_disable(ldo);
312                                 if(ret != 0){
313                                         pr_err("%s: faild to disable %s\n", __func__, ldostr);
314                                 } else {
315                                         pr_info("turn off ldo done.\n");
316                                 }
317                         } else {
318                                 pr_warn("%s is disabled before disable", ldostr);
319                         }
320                 }
321                 regulator_put(ldo);
322         }
323
324         return 0;
325 }
326
327 static int power_on_by_gpio(bool enable) {
328         struct bsp_priv * bsp_priv = &g_bsp_priv;
329         if (enable) {
330                 //power on
331                 if (gpio_is_valid(bsp_priv->power_io)) {
332                         gpio_direction_output(bsp_priv->power_io, bsp_priv->power_io_level);
333                 }
334         } else {
335                 //power off
336                 if (gpio_is_valid(bsp_priv->power_io)) {
337                         gpio_direction_output(bsp_priv->power_io, !bsp_priv->power_io_level);
338                 }
339         }
340
341         return 0;
342 }
343
344 static int phy_power_on(bool enable)
345 {
346         struct bsp_priv *bsp_priv = &g_bsp_priv;
347         int ret = -1;
348
349         printk("%s: enable = %d \n", __func__, enable);
350
351         if (bsp_priv->power_ctrl_by_pmu) {
352                 ret = power_on_by_pmu(enable);
353         } else {
354                 ret =  power_on_by_gpio(enable);
355         }
356
357         if (enable) {
358                 //reset
359                 if (gpio_is_valid(bsp_priv->reset_io)) {
360                         gpio_direction_output(bsp_priv->reset_io, bsp_priv->reset_io_level);
361                         mdelay(5);
362                         gpio_direction_output(bsp_priv->reset_io, !bsp_priv->reset_io_level);
363                 }
364                 mdelay(30);
365
366         } else {
367                 //pull down reset
368                 if (gpio_is_valid(bsp_priv->reset_io)) {
369                         gpio_direction_output(bsp_priv->reset_io, bsp_priv->reset_io_level);
370                 }
371         }
372
373         return ret;
374 }
375
376 int stmmc_pltfr_init(struct platform_device *pdev) {
377         int phy_iface;
378         int err;
379         struct bsp_priv *bsp_priv;
380         int irq;
381
382         pr_info("%s: \n", __func__);
383
384 //iomux
385 #if 0
386         if ((pdev->dev.pins) && (pdev->dev.pins->p)) {
387                 gmac_state = pinctrl_lookup_state(pdev->dev.pins->p, "default");
388                 if (IS_ERR(gmac_state)) {
389                                 dev_err(&pdev->dev, "no gmc pinctrl state\n");
390                                 return -1;
391                 }
392
393                 pinctrl_select_state(pdev->dev.pins->p, gmac_state);
394         }
395 #endif
396
397         bsp_priv = &g_bsp_priv;
398         phy_iface = bsp_priv->phy_iface;
399 //power
400         if (!gpio_is_valid(bsp_priv->power_io)) {
401                 pr_err("%s: ERROR: Get power-gpio failed.\n", __func__);
402         } else {
403                 err = gpio_request(bsp_priv->power_io, "gmac_phy_power");
404                 if (err) {
405                         pr_err("%s: ERROR: Request gmac phy power pin failed.\n", __func__);
406                 }
407         }
408
409         if (!gpio_is_valid(bsp_priv->reset_io)) {
410                 pr_err("%s: ERROR: Get reset-gpio failed.\n", __func__);
411         } else {
412                 err = gpio_request(bsp_priv->reset_io, "gmac_phy_reset");
413                 if (err) {
414                         pr_err("%s: ERROR: Request gmac phy reset pin failed.\n", __func__);
415                 }
416         }
417
418         if (bsp_priv->phyirq_io > 0) {
419                 err = gpio_request(bsp_priv->phyirq_io, "gmac_phyirq");
420                 if (err < 0) {
421                         printk("gmac_phyirq: failed to request GPIO %d,"
422                                 " error %d\n", bsp_priv->phyirq_io, err);
423                 } else {
424                         err = gpio_direction_input(bsp_priv->phyirq_io);
425                         if (err < 0) {
426                                 pr_err("gmac_phyirq: failed to configure input"
427                                         " direction for GPIO %d, error %d\n",
428                                 bsp_priv->phyirq_io, err);
429                                 gpio_free(bsp_priv->phyirq_io);
430                         } else {
431                                 irq = gpio_to_irq(bsp_priv->phyirq_io);
432                                 if (irq < 0) {
433                                         err = irq;
434                                         pr_err("gpio-keys: Unable to get irq number for GPIO %d, error %d\n", bsp_priv->phyirq_io, err);
435                                         gpio_free(bsp_priv->phyirq_io);
436                                 } else {
437                                         struct plat_stmmacenet_data *plat_dat = dev_get_platdata(&pdev->dev);
438                                         if (plat_dat)
439                                                 plat_dat->mdio_bus_data->probed_phy_irq = irq;
440                                         else
441                                                 pr_err("%s: plat_data is NULL\n", __func__);
442                                 }
443                         }
444                 }
445         }
446
447 //rmii or rgmii
448         if (phy_iface == PHY_INTERFACE_MODE_RGMII) {
449                 pr_info("%s: init for RGMII\n", __func__);
450                 SET_RGMII(bsp_priv->chip, bsp_priv->tx_delay, bsp_priv->rx_delay);
451         } else if (phy_iface == PHY_INTERFACE_MODE_RMII) {
452                 pr_info("%s: init for RMII\n", __func__);
453                 SET_RMII(bsp_priv->chip);
454         } else {
455                 pr_err("%s: ERROR: NO interface defined!\n", __func__);
456         }
457
458         return 0;
459 }
460
461 void stmmc_pltfr_fix_mac_speed(void *priv, unsigned int speed){
462         struct bsp_priv * bsp_priv = priv;
463         int interface;
464
465         pr_info("%s: fix speed to %d\n", __func__, speed);
466
467         if (bsp_priv) {
468                 interface = bsp_priv->phy_iface;
469         }
470
471         if (interface == PHY_INTERFACE_MODE_RGMII) {
472                 pr_info("%s: fix speed for RGMII\n", __func__);
473
474                 switch (speed) {
475                         case 10: {
476                                 SET_RGMII_10M(bsp_priv->chip);
477                                 break;
478                         }
479                         case 100: {
480                                 SET_RGMII_100M(bsp_priv->chip);
481                                 break;
482                         }
483                         case 1000: {
484                                 SET_RGMII_1000M(bsp_priv->chip);
485                                 break;
486                         }
487                         default: {
488                                 pr_err("%s: ERROR: speed %d is not defined!\n", __func__, speed);
489                         }
490                 }
491
492         } else if (interface == PHY_INTERFACE_MODE_RMII) {
493                 pr_info("%s: fix speed for RMII\n", __func__);
494                 switch (speed) {
495                         case 10: {
496                                 SET_RMII_10M(bsp_priv->chip);
497                                 break;
498                         }
499                         case 100: {
500                                 SET_RMII_100M(bsp_priv->chip);
501                                 break;
502                         }
503                         default: {
504                                 pr_err("%s: ERROR: speed %d is not defined!\n", __func__, speed);
505                         }
506                 }
507         } else {
508                 pr_err("%s: ERROR: NO interface defined!\n", __func__);
509         }
510 }
511
512
513 #ifdef CONFIG_OF
514 static int stmmac_probe_config_dt(struct platform_device *pdev,
515                                   struct plat_stmmacenet_data *plat,
516                                   const char **mac)
517 {
518         struct device_node *np = pdev->dev.of_node;
519         enum of_gpio_flags flags;
520         int ret;
521         const char * strings = NULL;
522         int value;
523
524         if (!np)
525                 return -ENODEV;
526
527         *mac = of_get_mac_address(np);
528         plat->interface = of_get_phy_mode(np);
529
530         plat->mdio_bus_data = devm_kzalloc(&pdev->dev,
531                                            sizeof(struct stmmac_mdio_bus_data),
532                                            GFP_KERNEL);
533
534         plat->init = stmmc_pltfr_init;
535         plat->fix_mac_speed = stmmc_pltfr_fix_mac_speed;
536
537         ret = of_property_read_string(np, "pmu_regulator", &strings);
538         if (ret) {
539                 pr_err("%s: Can not read property: pmu_regulator.\n", __func__);
540                 g_bsp_priv.power_ctrl_by_pmu = false;
541         } else {
542                 pr_info("%s: ethernet phy power controled by pmu(%s).\n", __func__, strings);
543                 g_bsp_priv.power_ctrl_by_pmu = true;
544                 strcpy(g_bsp_priv.pmu_regulator, strings);
545         }
546
547         ret = of_property_read_u32(np, "pmu_enable_level", &value);
548         if (ret) {
549                 pr_err("%s: Can not read property: pmu_enable_level.\n", __func__);
550                 g_bsp_priv.power_ctrl_by_pmu = false;
551         } else {
552                 pr_info("%s: ethernet phy power controled by pmu(level = %s).\n", __func__, (value == 1)?"HIGH":"LOW");
553                 g_bsp_priv.power_ctrl_by_pmu = true;
554                 g_bsp_priv.pmu_enable_level = value;
555         }
556
557         ret = of_property_read_string(np, "clock_in_out", &strings);
558         if (ret) {
559                 pr_err("%s: Can not read property: clock_in_out.\n", __func__);
560                 g_bsp_priv.clock_input = true;
561         } else {
562                 pr_info("%s: clock input or output? (%s).\n", __func__, strings);
563                 if (!strcmp(strings, "input")) {
564                         g_bsp_priv.clock_input = true;
565                 } else {
566                         g_bsp_priv.clock_input = false;
567                 }
568         }
569
570         ret = of_property_read_u32(np, "tx_delay", &value);
571         if (ret) {
572                 g_bsp_priv.tx_delay = 0x30;
573                 pr_err("%s: Can not read property: tx_delay. set tx_delay to 0x%x\n", __func__, g_bsp_priv.tx_delay);
574         } else {
575                 pr_info("%s: TX delay(0x%x).\n", __func__, value);
576                 g_bsp_priv.tx_delay = value;
577         }
578
579         ret = of_property_read_u32(np, "rx_delay", &value);
580         if (ret) {
581                 g_bsp_priv.rx_delay = 0x10;
582                 pr_err("%s: Can not read property: rx_delay. set rx_delay to 0x%x\n", __func__, g_bsp_priv.rx_delay);
583         } else {
584                 pr_info("%s: RX delay(0x%x).\n", __func__, value);
585                 g_bsp_priv.rx_delay = value;
586         }
587
588         g_bsp_priv.phyirq_io =
589                         of_get_named_gpio_flags(np, "phyirq-gpio", 0, &flags);
590         g_bsp_priv.phyirq_io_level = (flags == GPIO_ACTIVE_HIGH) ? 1 : 0;
591
592         g_bsp_priv.reset_io = 
593                         of_get_named_gpio_flags(np, "reset-gpio", 0, &flags);
594         g_bsp_priv.reset_io_level = (flags == GPIO_ACTIVE_HIGH) ? 1 : 0;
595         g_bsp_priv.power_io = 
596                         of_get_named_gpio_flags(np, "power-gpio", 0, &flags);
597         g_bsp_priv.power_io_level = (flags == GPIO_ACTIVE_HIGH) ? 1 : 0;
598
599         g_bsp_priv.phy_iface = plat->interface;
600         g_bsp_priv.phy_power_on = phy_power_on;
601         g_bsp_priv.gmac_clk_enable = gmac_clk_enable;
602
603         plat->bsp_priv = &g_bsp_priv;
604
605         /*
606          * Currently only the properties needed on SPEAr600
607          * are provided. All other properties should be added
608          * once needed on other platforms.
609          */
610         if (of_device_is_compatible(np, "rockchip,rk3288-gmac") ||
611             of_device_is_compatible(np, "rockchip,rk312x-gmac")) {
612                 plat->has_gmac = 1;
613                 plat->pmt = 1;
614         }
615
616         if (of_device_is_compatible(np, "rockchip,rk3288-gmac")) {
617                 g_bsp_priv.chip = RK3288_GMAC;
618                 printk("%s: is rockchip,rk3288-gmac", __func__);
619         } if (of_device_is_compatible(np, "rockchip,rk312x-gmac")) {
620                 g_bsp_priv.chip = RK312X_GMAC;
621                 printk("%s: is rockchip,rk312x-gmac", __func__);
622         }
623
624         return 0;
625 }
626 #else
627 static int stmmac_probe_config_dt(struct platform_device *pdev,
628                                   struct plat_stmmacenet_data *plat,
629                                   const char **mac)
630 {
631         return -ENOSYS;
632 }
633 #endif /* CONFIG_OF */
634
635 /**
636  * stmmac_pltfr_probe
637  * @pdev: platform device pointer
638  * Description: platform_device probe function. It allocates
639  * the necessary resources and invokes the main to init
640  * the net device, register the mdio bus etc.
641  */
642 static int stmmac_pltfr_probe(struct platform_device *pdev)
643 {
644         int ret = 0;
645         struct resource *res;
646         struct device *dev = &pdev->dev;
647         void __iomem *addr = NULL;
648         struct stmmac_priv *priv = NULL;
649         struct plat_stmmacenet_data *plat_dat = NULL;
650         const char *mac = NULL;
651
652         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
653         if (!res)
654                 return -ENODEV;
655
656         addr = devm_ioremap_resource(dev, res);
657         if (IS_ERR(addr))
658                 return PTR_ERR(addr);
659
660         if (pdev->dev.of_node) {
661                 plat_dat = devm_kzalloc(&pdev->dev,
662                                         sizeof(struct plat_stmmacenet_data),
663                                         GFP_KERNEL);
664                 if (!plat_dat) {
665                         pr_err("%s: ERROR: no memory", __func__);
666                         return  -ENOMEM;
667                 }
668
669                 ret = stmmac_probe_config_dt(pdev, plat_dat, &mac);
670                 if (ret) {
671                         pr_err("%s: main dt probe failed", __func__);
672                         return ret;
673                 }
674
675                 pdev->dev.platform_data = plat_dat;
676
677         } else {
678                 plat_dat = pdev->dev.platform_data;
679         }
680
681         /* Custom initialisation (if needed)*/
682         if (plat_dat->init) {
683                 ret = plat_dat->init(pdev);
684                 if (unlikely(ret))
685                         return ret;
686         }
687
688         gmac_clk_init(&(pdev->dev));
689
690         priv = stmmac_dvr_probe(&(pdev->dev), plat_dat, addr);
691         if (!priv) {
692                 pr_err("%s: main driver probe failed", __func__);
693                 return -ENODEV;
694         }
695
696
697         /* Get MAC address if available (DT) */
698         if (mac)
699                 memcpy(priv->dev->dev_addr, mac, ETH_ALEN);
700
701         /* Get the MAC information */
702         priv->dev->irq = platform_get_irq_byname(pdev, "macirq");
703         if (priv->dev->irq == -ENXIO) {
704                 pr_err("%s: ERROR: MAC IRQ configuration "
705                        "information not found\n", __func__);
706                 return -ENXIO;
707         }
708
709         /*
710          * On some platforms e.g. SPEAr the wake up irq differs from the mac irq
711          * The external wake up irq can be passed through the platform code
712          * named as "eth_wake_irq"
713          *
714          * In case the wake up interrupt is not passed from the platform
715          * so the driver will continue to use the mac irq (ndev->irq)
716          */
717         priv->wol_irq = platform_get_irq_byname(pdev, "eth_wake_irq");
718         if (priv->wol_irq == -ENXIO)
719                 priv->wol_irq = priv->dev->irq;
720
721         priv->lpi_irq = platform_get_irq_byname(pdev, "eth_lpi");
722
723         platform_set_drvdata(pdev, priv->dev);
724
725         pr_debug("STMMAC platform driver registration completed");
726
727         return 0;
728 }
729
730 /**
731  * stmmac_pltfr_remove
732  * @pdev: platform device pointer
733  * Description: this function calls the main to free the net resources
734  * and calls the platforms hook and release the resources (e.g. mem).
735  */
736 static int stmmac_pltfr_remove(struct platform_device *pdev)
737 {
738         struct net_device *ndev = platform_get_drvdata(pdev);
739         struct stmmac_priv *priv = netdev_priv(ndev);
740         int ret = stmmac_dvr_remove(ndev);
741
742         if (priv->plat->exit)
743                 priv->plat->exit(pdev);
744
745         platform_set_drvdata(pdev, NULL);
746
747         return ret;
748 }
749
750 #ifdef CONFIG_PM
751 static int stmmac_pltfr_suspend(struct device *dev)
752 {
753         struct net_device *ndev = dev_get_drvdata(dev);
754
755         return stmmac_suspend(ndev);
756 }
757
758 static int stmmac_pltfr_resume(struct device *dev)
759 {
760         struct net_device *ndev = dev_get_drvdata(dev);
761
762         return stmmac_resume(ndev);
763 }
764
765 int stmmac_pltfr_freeze(struct device *dev)
766 {
767         int ret;
768         struct plat_stmmacenet_data *plat_dat = dev_get_platdata(dev);
769         struct net_device *ndev = dev_get_drvdata(dev);
770         struct platform_device *pdev = to_platform_device(dev);
771
772         ret = stmmac_freeze(ndev);
773         if (plat_dat->exit)
774                 plat_dat->exit(pdev);
775
776         return ret;
777 }
778
779 int stmmac_pltfr_restore(struct device *dev)
780 {
781         struct plat_stmmacenet_data *plat_dat = dev_get_platdata(dev);
782         struct net_device *ndev = dev_get_drvdata(dev);
783         struct platform_device *pdev = to_platform_device(dev);
784
785         if (plat_dat->init)
786                 plat_dat->init(pdev);
787
788         return stmmac_restore(ndev);
789 }
790
791 static const struct dev_pm_ops stmmac_pltfr_pm_ops = {
792         .suspend = stmmac_pltfr_suspend,
793         .resume = stmmac_pltfr_resume,
794         .freeze = stmmac_pltfr_freeze,
795         .thaw = stmmac_pltfr_restore,
796         .restore = stmmac_pltfr_restore,
797 };
798 #else
799 static const struct dev_pm_ops stmmac_pltfr_pm_ops;
800 #endif /* CONFIG_PM */
801
802 static const struct of_device_id stmmac_dt_ids[] = {
803         { .compatible = "rockchip,rk3288-gmac"},
804         { .compatible = "rockchip,rk312x-gmac"},
805         { /* sentinel */ }
806 };
807 MODULE_DEVICE_TABLE(of, stmmac_dt_ids);
808
809 struct platform_driver stmmac_pltfr_driver = {
810         .probe = stmmac_pltfr_probe,
811         .remove = stmmac_pltfr_remove,
812         .driver = {
813                    .name = STMMAC_RESOURCE_NAME,
814                    .owner = THIS_MODULE,
815                    .pm = &stmmac_pltfr_pm_ops,
816                    .of_match_table = of_match_ptr(stmmac_dt_ids),
817                    },
818 };
819
820 MODULE_DESCRIPTION("STMMAC 10/100/1000 Ethernet PLATFORM driver");
821 MODULE_AUTHOR("Giuseppe Cavallaro <peppe.cavallaro@st.com>");
822 MODULE_LICENSE("GPL");