xref: /openbmc/linux/drivers/net/ethernet/stmicro/stmmac/dwmac-rk.c (revision 0fb98db19700a4e1d80de401ffab42871c388dd9)
17ad269eaSRoger Chen /**
27ad269eaSRoger Chen  * dwmac-rk.c - Rockchip RK3288 DWMAC specific glue layer
37ad269eaSRoger Chen  *
47ad269eaSRoger Chen  * Copyright (C) 2014 Chen-Zhi (Roger Chen)
57ad269eaSRoger Chen  *
67ad269eaSRoger Chen  * Chen-Zhi (Roger Chen)  <roger.chen@rock-chips.com>
77ad269eaSRoger Chen  *
87ad269eaSRoger Chen  * This program is free software; you can redistribute it and/or modify
97ad269eaSRoger Chen  * it under the terms of the GNU General Public License as published by
107ad269eaSRoger Chen  * the Free Software Foundation; either version 2 of the License, or
117ad269eaSRoger Chen  * (at your option) any later version.
127ad269eaSRoger Chen  *
137ad269eaSRoger Chen  * This program is distributed in the hope that it will be useful,
147ad269eaSRoger Chen  * but WITHOUT ANY WARRANTY; without even the implied warranty of
157ad269eaSRoger Chen  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
167ad269eaSRoger Chen  * GNU General Public License for more details.
177ad269eaSRoger Chen  */
187ad269eaSRoger Chen 
197ad269eaSRoger Chen #include <linux/stmmac.h>
207ad269eaSRoger Chen #include <linux/bitops.h>
217ad269eaSRoger Chen #include <linux/clk.h>
227ad269eaSRoger Chen #include <linux/phy.h>
237ad269eaSRoger Chen #include <linux/of_net.h>
247ad269eaSRoger Chen #include <linux/gpio.h>
25e0fb4013SJoachim Eastwood #include <linux/module.h>
267ad269eaSRoger Chen #include <linux/of_gpio.h>
277ad269eaSRoger Chen #include <linux/of_device.h>
28e0fb4013SJoachim Eastwood #include <linux/platform_device.h>
297ad269eaSRoger Chen #include <linux/regulator/consumer.h>
307ad269eaSRoger Chen #include <linux/delay.h>
317ad269eaSRoger Chen #include <linux/mfd/syscon.h>
327ad269eaSRoger Chen #include <linux/regmap.h>
337ad269eaSRoger Chen 
34e0fb4013SJoachim Eastwood #include "stmmac_platform.h"
35e0fb4013SJoachim Eastwood 
36*0fb98db1SHeiko Stübner struct rk_priv_data;
37*0fb98db1SHeiko Stübner struct rk_gmac_ops {
38*0fb98db1SHeiko Stübner 	void (*set_to_rgmii)(struct rk_priv_data *bsp_priv,
39*0fb98db1SHeiko Stübner 			     int tx_delay, int rx_delay);
40*0fb98db1SHeiko Stübner 	void (*set_to_rmii)(struct rk_priv_data *bsp_priv);
41*0fb98db1SHeiko Stübner 	void (*set_rgmii_speed)(struct rk_priv_data *bsp_priv, int speed);
42*0fb98db1SHeiko Stübner 	void (*set_rmii_speed)(struct rk_priv_data *bsp_priv, int speed);
43*0fb98db1SHeiko Stübner };
44*0fb98db1SHeiko Stübner 
457ad269eaSRoger Chen struct rk_priv_data {
467ad269eaSRoger Chen 	struct platform_device *pdev;
477ad269eaSRoger Chen 	int phy_iface;
482e12f536SRomain Perier 	struct regulator *regulator;
49*0fb98db1SHeiko Stübner 	struct rk_gmac_ops *ops;
507ad269eaSRoger Chen 
517ad269eaSRoger Chen 	bool clk_enabled;
527ad269eaSRoger Chen 	bool clock_input;
537ad269eaSRoger Chen 
547ad269eaSRoger Chen 	struct clk *clk_mac;
557ad269eaSRoger Chen 	struct clk *gmac_clkin;
567ad269eaSRoger Chen 	struct clk *mac_clk_rx;
577ad269eaSRoger Chen 	struct clk *mac_clk_tx;
587ad269eaSRoger Chen 	struct clk *clk_mac_ref;
597ad269eaSRoger Chen 	struct clk *clk_mac_refout;
607ad269eaSRoger Chen 	struct clk *aclk_mac;
617ad269eaSRoger Chen 	struct clk *pclk_mac;
627ad269eaSRoger Chen 
637ad269eaSRoger Chen 	int tx_delay;
647ad269eaSRoger Chen 	int rx_delay;
657ad269eaSRoger Chen 
667ad269eaSRoger Chen 	struct regmap *grf;
677ad269eaSRoger Chen };
687ad269eaSRoger Chen 
697ad269eaSRoger Chen #define HIWORD_UPDATE(val, mask, shift) \
707ad269eaSRoger Chen 		((val) << (shift) | (mask) << ((shift) + 16))
717ad269eaSRoger Chen 
727ad269eaSRoger Chen #define GRF_BIT(nr)	(BIT(nr) | BIT(nr+16))
737ad269eaSRoger Chen #define GRF_CLR_BIT(nr)	(BIT(nr+16))
747ad269eaSRoger Chen 
757ad269eaSRoger Chen #define RK3288_GRF_SOC_CON1	0x0248
767ad269eaSRoger Chen #define RK3288_GRF_SOC_CON3	0x0250
777ad269eaSRoger Chen 
787ad269eaSRoger Chen /*RK3288_GRF_SOC_CON1*/
79*0fb98db1SHeiko Stübner #define RK3288_GMAC_PHY_INTF_SEL_RGMII	(GRF_BIT(6) | GRF_CLR_BIT(7) | \
80*0fb98db1SHeiko Stübner 					 GRF_CLR_BIT(8))
81*0fb98db1SHeiko Stübner #define RK3288_GMAC_PHY_INTF_SEL_RMII	(GRF_CLR_BIT(6) | GRF_CLR_BIT(7) | \
82*0fb98db1SHeiko Stübner 					 GRF_BIT(8))
83*0fb98db1SHeiko Stübner #define RK3288_GMAC_FLOW_CTRL		GRF_BIT(9)
84*0fb98db1SHeiko Stübner #define RK3288_GMAC_FLOW_CTRL_CLR	GRF_CLR_BIT(9)
85*0fb98db1SHeiko Stübner #define RK3288_GMAC_SPEED_10M		GRF_CLR_BIT(10)
86*0fb98db1SHeiko Stübner #define RK3288_GMAC_SPEED_100M		GRF_BIT(10)
87*0fb98db1SHeiko Stübner #define RK3288_GMAC_RMII_CLK_25M	GRF_BIT(11)
88*0fb98db1SHeiko Stübner #define RK3288_GMAC_RMII_CLK_2_5M	GRF_CLR_BIT(11)
89*0fb98db1SHeiko Stübner #define RK3288_GMAC_CLK_125M		(GRF_CLR_BIT(12) | GRF_CLR_BIT(13))
90*0fb98db1SHeiko Stübner #define RK3288_GMAC_CLK_25M		(GRF_BIT(12) | GRF_BIT(13))
91*0fb98db1SHeiko Stübner #define RK3288_GMAC_CLK_2_5M		(GRF_CLR_BIT(12) | GRF_BIT(13))
92*0fb98db1SHeiko Stübner #define RK3288_GMAC_RMII_MODE		GRF_BIT(14)
93*0fb98db1SHeiko Stübner #define RK3288_GMAC_RMII_MODE_CLR	GRF_CLR_BIT(14)
947ad269eaSRoger Chen 
957ad269eaSRoger Chen /*RK3288_GRF_SOC_CON3*/
96*0fb98db1SHeiko Stübner #define RK3288_GMAC_TXCLK_DLY_ENABLE	GRF_BIT(14)
97*0fb98db1SHeiko Stübner #define RK3288_GMAC_TXCLK_DLY_DISABLE	GRF_CLR_BIT(14)
98*0fb98db1SHeiko Stübner #define RK3288_GMAC_RXCLK_DLY_ENABLE	GRF_BIT(15)
99*0fb98db1SHeiko Stübner #define RK3288_GMAC_RXCLK_DLY_DISABLE	GRF_CLR_BIT(15)
100*0fb98db1SHeiko Stübner #define RK3288_GMAC_CLK_RX_DL_CFG(val)	HIWORD_UPDATE(val, 0x7F, 7)
101*0fb98db1SHeiko Stübner #define RK3288_GMAC_CLK_TX_DL_CFG(val)	HIWORD_UPDATE(val, 0x7F, 0)
1027ad269eaSRoger Chen 
103*0fb98db1SHeiko Stübner static void rk3288_set_to_rgmii(struct rk_priv_data *bsp_priv,
1047ad269eaSRoger Chen 				int tx_delay, int rx_delay)
1057ad269eaSRoger Chen {
1067ad269eaSRoger Chen 	struct device *dev = &bsp_priv->pdev->dev;
1077ad269eaSRoger Chen 
1087ad269eaSRoger Chen 	if (IS_ERR(bsp_priv->grf)) {
109d42202dcSRomain Perier 		dev_err(dev, "Missing rockchip,grf property\n");
1107ad269eaSRoger Chen 		return;
1117ad269eaSRoger Chen 	}
1127ad269eaSRoger Chen 
1137ad269eaSRoger Chen 	regmap_write(bsp_priv->grf, RK3288_GRF_SOC_CON1,
114*0fb98db1SHeiko Stübner 		     RK3288_GMAC_PHY_INTF_SEL_RGMII |
115*0fb98db1SHeiko Stübner 		     RK3288_GMAC_RMII_MODE_CLR);
1167ad269eaSRoger Chen 	regmap_write(bsp_priv->grf, RK3288_GRF_SOC_CON3,
117*0fb98db1SHeiko Stübner 		     RK3288_GMAC_RXCLK_DLY_ENABLE |
118*0fb98db1SHeiko Stübner 		     RK3288_GMAC_TXCLK_DLY_ENABLE |
119*0fb98db1SHeiko Stübner 		     RK3288_GMAC_CLK_RX_DL_CFG(rx_delay) |
120*0fb98db1SHeiko Stübner 		     RK3288_GMAC_CLK_TX_DL_CFG(tx_delay));
1217ad269eaSRoger Chen }
1227ad269eaSRoger Chen 
123*0fb98db1SHeiko Stübner static void rk3288_set_to_rmii(struct rk_priv_data *bsp_priv)
1247ad269eaSRoger Chen {
1257ad269eaSRoger Chen 	struct device *dev = &bsp_priv->pdev->dev;
1267ad269eaSRoger Chen 
1277ad269eaSRoger Chen 	if (IS_ERR(bsp_priv->grf)) {
128d42202dcSRomain Perier 		dev_err(dev, "Missing rockchip,grf property\n");
1297ad269eaSRoger Chen 		return;
1307ad269eaSRoger Chen 	}
1317ad269eaSRoger Chen 
1327ad269eaSRoger Chen 	regmap_write(bsp_priv->grf, RK3288_GRF_SOC_CON1,
133*0fb98db1SHeiko Stübner 		     RK3288_GMAC_PHY_INTF_SEL_RMII | RK3288_GMAC_RMII_MODE);
1347ad269eaSRoger Chen }
1357ad269eaSRoger Chen 
136*0fb98db1SHeiko Stübner static void rk3288_set_rgmii_speed(struct rk_priv_data *bsp_priv, int speed)
1377ad269eaSRoger Chen {
1387ad269eaSRoger Chen 	struct device *dev = &bsp_priv->pdev->dev;
1397ad269eaSRoger Chen 
1407ad269eaSRoger Chen 	if (IS_ERR(bsp_priv->grf)) {
141d42202dcSRomain Perier 		dev_err(dev, "Missing rockchip,grf property\n");
1427ad269eaSRoger Chen 		return;
1437ad269eaSRoger Chen 	}
1447ad269eaSRoger Chen 
1457ad269eaSRoger Chen 	if (speed == 10)
146*0fb98db1SHeiko Stübner 		regmap_write(bsp_priv->grf, RK3288_GRF_SOC_CON1,
147*0fb98db1SHeiko Stübner 			     RK3288_GMAC_CLK_2_5M);
1487ad269eaSRoger Chen 	else if (speed == 100)
149*0fb98db1SHeiko Stübner 		regmap_write(bsp_priv->grf, RK3288_GRF_SOC_CON1,
150*0fb98db1SHeiko Stübner 			     RK3288_GMAC_CLK_25M);
1517ad269eaSRoger Chen 	else if (speed == 1000)
152*0fb98db1SHeiko Stübner 		regmap_write(bsp_priv->grf, RK3288_GRF_SOC_CON1,
153*0fb98db1SHeiko Stübner 			     RK3288_GMAC_CLK_125M);
1547ad269eaSRoger Chen 	else
1557ad269eaSRoger Chen 		dev_err(dev, "unknown speed value for RGMII! speed=%d", speed);
1567ad269eaSRoger Chen }
1577ad269eaSRoger Chen 
158*0fb98db1SHeiko Stübner static void rk3288_set_rmii_speed(struct rk_priv_data *bsp_priv, int speed)
1597ad269eaSRoger Chen {
1607ad269eaSRoger Chen 	struct device *dev = &bsp_priv->pdev->dev;
1617ad269eaSRoger Chen 
1627ad269eaSRoger Chen 	if (IS_ERR(bsp_priv->grf)) {
163d42202dcSRomain Perier 		dev_err(dev, "Missing rockchip,grf property\n");
1647ad269eaSRoger Chen 		return;
1657ad269eaSRoger Chen 	}
1667ad269eaSRoger Chen 
1677ad269eaSRoger Chen 	if (speed == 10) {
1687ad269eaSRoger Chen 		regmap_write(bsp_priv->grf, RK3288_GRF_SOC_CON1,
169*0fb98db1SHeiko Stübner 			     RK3288_GMAC_RMII_CLK_2_5M |
170*0fb98db1SHeiko Stübner 			     RK3288_GMAC_SPEED_10M);
1717ad269eaSRoger Chen 	} else if (speed == 100) {
1727ad269eaSRoger Chen 		regmap_write(bsp_priv->grf, RK3288_GRF_SOC_CON1,
173*0fb98db1SHeiko Stübner 			     RK3288_GMAC_RMII_CLK_25M |
174*0fb98db1SHeiko Stübner 			     RK3288_GMAC_SPEED_100M);
1757ad269eaSRoger Chen 	} else {
1767ad269eaSRoger Chen 		dev_err(dev, "unknown speed value for RMII! speed=%d", speed);
1777ad269eaSRoger Chen 	}
1787ad269eaSRoger Chen }
1797ad269eaSRoger Chen 
180*0fb98db1SHeiko Stübner struct rk_gmac_ops rk3288_ops = {
181*0fb98db1SHeiko Stübner 	.set_to_rgmii = rk3288_set_to_rgmii,
182*0fb98db1SHeiko Stübner 	.set_to_rmii = rk3288_set_to_rmii,
183*0fb98db1SHeiko Stübner 	.set_rgmii_speed = rk3288_set_rgmii_speed,
184*0fb98db1SHeiko Stübner 	.set_rmii_speed = rk3288_set_rmii_speed,
185*0fb98db1SHeiko Stübner };
186*0fb98db1SHeiko Stübner 
1877ad269eaSRoger Chen static int gmac_clk_init(struct rk_priv_data *bsp_priv)
1887ad269eaSRoger Chen {
1897ad269eaSRoger Chen 	struct device *dev = &bsp_priv->pdev->dev;
1907ad269eaSRoger Chen 
1917ad269eaSRoger Chen 	bsp_priv->clk_enabled = false;
1927ad269eaSRoger Chen 
1937ad269eaSRoger Chen 	bsp_priv->mac_clk_rx = devm_clk_get(dev, "mac_clk_rx");
1947ad269eaSRoger Chen 	if (IS_ERR(bsp_priv->mac_clk_rx))
195d42202dcSRomain Perier 		dev_err(dev, "cannot get clock %s\n",
196d42202dcSRomain Perier 			"mac_clk_rx");
1977ad269eaSRoger Chen 
1987ad269eaSRoger Chen 	bsp_priv->mac_clk_tx = devm_clk_get(dev, "mac_clk_tx");
1997ad269eaSRoger Chen 	if (IS_ERR(bsp_priv->mac_clk_tx))
200d42202dcSRomain Perier 		dev_err(dev, "cannot get clock %s\n",
201d42202dcSRomain Perier 			"mac_clk_tx");
2027ad269eaSRoger Chen 
2037ad269eaSRoger Chen 	bsp_priv->aclk_mac = devm_clk_get(dev, "aclk_mac");
2047ad269eaSRoger Chen 	if (IS_ERR(bsp_priv->aclk_mac))
205d42202dcSRomain Perier 		dev_err(dev, "cannot get clock %s\n",
206d42202dcSRomain Perier 			"aclk_mac");
2077ad269eaSRoger Chen 
2087ad269eaSRoger Chen 	bsp_priv->pclk_mac = devm_clk_get(dev, "pclk_mac");
2097ad269eaSRoger Chen 	if (IS_ERR(bsp_priv->pclk_mac))
210d42202dcSRomain Perier 		dev_err(dev, "cannot get clock %s\n",
211d42202dcSRomain Perier 			"pclk_mac");
2127ad269eaSRoger Chen 
2137ad269eaSRoger Chen 	bsp_priv->clk_mac = devm_clk_get(dev, "stmmaceth");
2147ad269eaSRoger Chen 	if (IS_ERR(bsp_priv->clk_mac))
215d42202dcSRomain Perier 		dev_err(dev, "cannot get clock %s\n",
216d42202dcSRomain Perier 			"stmmaceth");
2177ad269eaSRoger Chen 
2187ad269eaSRoger Chen 	if (bsp_priv->phy_iface == PHY_INTERFACE_MODE_RMII) {
2197ad269eaSRoger Chen 		bsp_priv->clk_mac_ref = devm_clk_get(dev, "clk_mac_ref");
2207ad269eaSRoger Chen 		if (IS_ERR(bsp_priv->clk_mac_ref))
221d42202dcSRomain Perier 			dev_err(dev, "cannot get clock %s\n",
222d42202dcSRomain Perier 				"clk_mac_ref");
2237ad269eaSRoger Chen 
2247ad269eaSRoger Chen 		if (!bsp_priv->clock_input) {
2257ad269eaSRoger Chen 			bsp_priv->clk_mac_refout =
2267ad269eaSRoger Chen 				devm_clk_get(dev, "clk_mac_refout");
2277ad269eaSRoger Chen 			if (IS_ERR(bsp_priv->clk_mac_refout))
228d42202dcSRomain Perier 				dev_err(dev, "cannot get clock %s\n",
229d42202dcSRomain Perier 					"clk_mac_refout");
2307ad269eaSRoger Chen 		}
2317ad269eaSRoger Chen 	}
2327ad269eaSRoger Chen 
2337ad269eaSRoger Chen 	if (bsp_priv->clock_input) {
234d42202dcSRomain Perier 		dev_info(dev, "clock input from PHY\n");
2357ad269eaSRoger Chen 	} else {
2367ad269eaSRoger Chen 		if (bsp_priv->phy_iface == PHY_INTERFACE_MODE_RMII)
237c48fa33cSHeiko Stübner 			clk_set_rate(bsp_priv->clk_mac, 50000000);
2387ad269eaSRoger Chen 	}
2397ad269eaSRoger Chen 
2407ad269eaSRoger Chen 	return 0;
2417ad269eaSRoger Chen }
2427ad269eaSRoger Chen 
2437ad269eaSRoger Chen static int gmac_clk_enable(struct rk_priv_data *bsp_priv, bool enable)
2447ad269eaSRoger Chen {
2457ad269eaSRoger Chen 	int phy_iface = phy_iface = bsp_priv->phy_iface;
2467ad269eaSRoger Chen 
2477ad269eaSRoger Chen 	if (enable) {
2487ad269eaSRoger Chen 		if (!bsp_priv->clk_enabled) {
2497ad269eaSRoger Chen 			if (phy_iface == PHY_INTERFACE_MODE_RMII) {
2507ad269eaSRoger Chen 				if (!IS_ERR(bsp_priv->mac_clk_rx))
2517ad269eaSRoger Chen 					clk_prepare_enable(
2527ad269eaSRoger Chen 						bsp_priv->mac_clk_rx);
2537ad269eaSRoger Chen 
2547ad269eaSRoger Chen 				if (!IS_ERR(bsp_priv->clk_mac_ref))
2557ad269eaSRoger Chen 					clk_prepare_enable(
2567ad269eaSRoger Chen 						bsp_priv->clk_mac_ref);
2577ad269eaSRoger Chen 
2587ad269eaSRoger Chen 				if (!IS_ERR(bsp_priv->clk_mac_refout))
2597ad269eaSRoger Chen 					clk_prepare_enable(
2607ad269eaSRoger Chen 						bsp_priv->clk_mac_refout);
2617ad269eaSRoger Chen 			}
2627ad269eaSRoger Chen 
2637ad269eaSRoger Chen 			if (!IS_ERR(bsp_priv->aclk_mac))
2647ad269eaSRoger Chen 				clk_prepare_enable(bsp_priv->aclk_mac);
2657ad269eaSRoger Chen 
2667ad269eaSRoger Chen 			if (!IS_ERR(bsp_priv->pclk_mac))
2677ad269eaSRoger Chen 				clk_prepare_enable(bsp_priv->pclk_mac);
2687ad269eaSRoger Chen 
2697ad269eaSRoger Chen 			if (!IS_ERR(bsp_priv->mac_clk_tx))
2707ad269eaSRoger Chen 				clk_prepare_enable(bsp_priv->mac_clk_tx);
2717ad269eaSRoger Chen 
2727ad269eaSRoger Chen 			/**
2737ad269eaSRoger Chen 			 * if (!IS_ERR(bsp_priv->clk_mac))
2747ad269eaSRoger Chen 			 *	clk_prepare_enable(bsp_priv->clk_mac);
2757ad269eaSRoger Chen 			 */
2767ad269eaSRoger Chen 			mdelay(5);
2777ad269eaSRoger Chen 			bsp_priv->clk_enabled = true;
2787ad269eaSRoger Chen 		}
2797ad269eaSRoger Chen 	} else {
2807ad269eaSRoger Chen 		if (bsp_priv->clk_enabled) {
2817ad269eaSRoger Chen 			if (phy_iface == PHY_INTERFACE_MODE_RMII) {
2827ad269eaSRoger Chen 				if (!IS_ERR(bsp_priv->mac_clk_rx))
2837ad269eaSRoger Chen 					clk_disable_unprepare(
2847ad269eaSRoger Chen 						bsp_priv->mac_clk_rx);
2857ad269eaSRoger Chen 
2867ad269eaSRoger Chen 				if (!IS_ERR(bsp_priv->clk_mac_ref))
2877ad269eaSRoger Chen 					clk_disable_unprepare(
2887ad269eaSRoger Chen 						bsp_priv->clk_mac_ref);
2897ad269eaSRoger Chen 
2907ad269eaSRoger Chen 				if (!IS_ERR(bsp_priv->clk_mac_refout))
2917ad269eaSRoger Chen 					clk_disable_unprepare(
2927ad269eaSRoger Chen 						bsp_priv->clk_mac_refout);
2937ad269eaSRoger Chen 			}
2947ad269eaSRoger Chen 
2957ad269eaSRoger Chen 			if (!IS_ERR(bsp_priv->aclk_mac))
2967ad269eaSRoger Chen 				clk_disable_unprepare(bsp_priv->aclk_mac);
2977ad269eaSRoger Chen 
2987ad269eaSRoger Chen 			if (!IS_ERR(bsp_priv->pclk_mac))
2997ad269eaSRoger Chen 				clk_disable_unprepare(bsp_priv->pclk_mac);
3007ad269eaSRoger Chen 
3017ad269eaSRoger Chen 			if (!IS_ERR(bsp_priv->mac_clk_tx))
3027ad269eaSRoger Chen 				clk_disable_unprepare(bsp_priv->mac_clk_tx);
3037ad269eaSRoger Chen 			/**
3047ad269eaSRoger Chen 			 * if (!IS_ERR(bsp_priv->clk_mac))
3057ad269eaSRoger Chen 			 *	clk_disable_unprepare(bsp_priv->clk_mac);
3067ad269eaSRoger Chen 			 */
3077ad269eaSRoger Chen 			bsp_priv->clk_enabled = false;
3087ad269eaSRoger Chen 		}
3097ad269eaSRoger Chen 	}
3107ad269eaSRoger Chen 
3117ad269eaSRoger Chen 	return 0;
3127ad269eaSRoger Chen }
3137ad269eaSRoger Chen 
3147ad269eaSRoger Chen static int phy_power_on(struct rk_priv_data *bsp_priv, bool enable)
3157ad269eaSRoger Chen {
3162e12f536SRomain Perier 	struct regulator *ldo = bsp_priv->regulator;
3177ad269eaSRoger Chen 	int ret;
3187ad269eaSRoger Chen 	struct device *dev = &bsp_priv->pdev->dev;
3197ad269eaSRoger Chen 
3202e12f536SRomain Perier 	if (!ldo) {
321d42202dcSRomain Perier 		dev_err(dev, "no regulator found\n");
3227ad269eaSRoger Chen 		return -1;
3237ad269eaSRoger Chen 	}
3247ad269eaSRoger Chen 
3257ad269eaSRoger Chen 	if (enable) {
3267ad269eaSRoger Chen 		ret = regulator_enable(ldo);
3272e12f536SRomain Perier 		if (ret)
328d42202dcSRomain Perier 			dev_err(dev, "fail to enable phy-supply\n");
3297ad269eaSRoger Chen 	} else {
3307ad269eaSRoger Chen 		ret = regulator_disable(ldo);
3312e12f536SRomain Perier 		if (ret)
332d42202dcSRomain Perier 			dev_err(dev, "fail to disable phy-supply\n");
3337ad269eaSRoger Chen 	}
3347ad269eaSRoger Chen 
3357ad269eaSRoger Chen 	return 0;
3367ad269eaSRoger Chen }
3377ad269eaSRoger Chen 
338*0fb98db1SHeiko Stübner static struct rk_priv_data *rk_gmac_setup(struct platform_device *pdev,
339*0fb98db1SHeiko Stübner 					  struct rk_gmac_ops *ops)
3407ad269eaSRoger Chen {
3417ad269eaSRoger Chen 	struct rk_priv_data *bsp_priv;
3427ad269eaSRoger Chen 	struct device *dev = &pdev->dev;
3437ad269eaSRoger Chen 	int ret;
3447ad269eaSRoger Chen 	const char *strings = NULL;
3457ad269eaSRoger Chen 	int value;
3467ad269eaSRoger Chen 
3477ad269eaSRoger Chen 	bsp_priv = devm_kzalloc(dev, sizeof(*bsp_priv), GFP_KERNEL);
3487ad269eaSRoger Chen 	if (!bsp_priv)
3497ad269eaSRoger Chen 		return ERR_PTR(-ENOMEM);
3507ad269eaSRoger Chen 
3517ad269eaSRoger Chen 	bsp_priv->phy_iface = of_get_phy_mode(dev->of_node);
352*0fb98db1SHeiko Stübner 	bsp_priv->ops = ops;
3537ad269eaSRoger Chen 
3542e12f536SRomain Perier 	bsp_priv->regulator = devm_regulator_get_optional(dev, "phy");
3552e12f536SRomain Perier 	if (IS_ERR(bsp_priv->regulator)) {
3562e12f536SRomain Perier 		if (PTR_ERR(bsp_priv->regulator) == -EPROBE_DEFER) {
3572e12f536SRomain Perier 			dev_err(dev, "phy regulator is not available yet, deferred probing\n");
3582e12f536SRomain Perier 			return ERR_PTR(-EPROBE_DEFER);
3592e12f536SRomain Perier 		}
3602e12f536SRomain Perier 		dev_err(dev, "no regulator found\n");
3612e12f536SRomain Perier 		bsp_priv->regulator = NULL;
3627ad269eaSRoger Chen 	}
3637ad269eaSRoger Chen 
3647ad269eaSRoger Chen 	ret = of_property_read_string(dev->of_node, "clock_in_out", &strings);
3657ad269eaSRoger Chen 	if (ret) {
366d42202dcSRomain Perier 		dev_err(dev, "Can not read property: clock_in_out.\n");
3677ad269eaSRoger Chen 		bsp_priv->clock_input = true;
3687ad269eaSRoger Chen 	} else {
369d42202dcSRomain Perier 		dev_info(dev, "clock input or output? (%s).\n",
370d42202dcSRomain Perier 			 strings);
3717ad269eaSRoger Chen 		if (!strcmp(strings, "input"))
3727ad269eaSRoger Chen 			bsp_priv->clock_input = true;
3737ad269eaSRoger Chen 		else
3747ad269eaSRoger Chen 			bsp_priv->clock_input = false;
3757ad269eaSRoger Chen 	}
3767ad269eaSRoger Chen 
3777ad269eaSRoger Chen 	ret = of_property_read_u32(dev->of_node, "tx_delay", &value);
3787ad269eaSRoger Chen 	if (ret) {
3797ad269eaSRoger Chen 		bsp_priv->tx_delay = 0x30;
380d42202dcSRomain Perier 		dev_err(dev, "Can not read property: tx_delay.");
381d42202dcSRomain Perier 		dev_err(dev, "set tx_delay to 0x%x\n",
382d42202dcSRomain Perier 			bsp_priv->tx_delay);
3837ad269eaSRoger Chen 	} else {
384d42202dcSRomain Perier 		dev_info(dev, "TX delay(0x%x).\n", value);
3857ad269eaSRoger Chen 		bsp_priv->tx_delay = value;
3867ad269eaSRoger Chen 	}
3877ad269eaSRoger Chen 
3887ad269eaSRoger Chen 	ret = of_property_read_u32(dev->of_node, "rx_delay", &value);
3897ad269eaSRoger Chen 	if (ret) {
3907ad269eaSRoger Chen 		bsp_priv->rx_delay = 0x10;
391d42202dcSRomain Perier 		dev_err(dev, "Can not read property: rx_delay.");
392d42202dcSRomain Perier 		dev_err(dev, "set rx_delay to 0x%x\n",
393d42202dcSRomain Perier 			bsp_priv->rx_delay);
3947ad269eaSRoger Chen 	} else {
395d42202dcSRomain Perier 		dev_info(dev, "RX delay(0x%x).\n", value);
3967ad269eaSRoger Chen 		bsp_priv->rx_delay = value;
3977ad269eaSRoger Chen 	}
3987ad269eaSRoger Chen 
3997ad269eaSRoger Chen 	bsp_priv->grf = syscon_regmap_lookup_by_phandle(dev->of_node,
4007ad269eaSRoger Chen 							"rockchip,grf");
4017ad269eaSRoger Chen 	bsp_priv->pdev = pdev;
4027ad269eaSRoger Chen 
4037ad269eaSRoger Chen 	/*rmii or rgmii*/
4047ad269eaSRoger Chen 	if (bsp_priv->phy_iface == PHY_INTERFACE_MODE_RGMII) {
405d42202dcSRomain Perier 		dev_info(dev, "init for RGMII\n");
406*0fb98db1SHeiko Stübner 		bsp_priv->ops->set_to_rgmii(bsp_priv, bsp_priv->tx_delay,
407*0fb98db1SHeiko Stübner 					    bsp_priv->rx_delay);
4087ad269eaSRoger Chen 	} else if (bsp_priv->phy_iface == PHY_INTERFACE_MODE_RMII) {
409d42202dcSRomain Perier 		dev_info(dev, "init for RMII\n");
410*0fb98db1SHeiko Stübner 		bsp_priv->ops->set_to_rmii(bsp_priv);
4117ad269eaSRoger Chen 	} else {
412d42202dcSRomain Perier 		dev_err(dev, "NO interface defined!\n");
4137ad269eaSRoger Chen 	}
4147ad269eaSRoger Chen 
4157ad269eaSRoger Chen 	gmac_clk_init(bsp_priv);
4167ad269eaSRoger Chen 
4177ad269eaSRoger Chen 	return bsp_priv;
4187ad269eaSRoger Chen }
4197ad269eaSRoger Chen 
420*0fb98db1SHeiko Stübner static void *rk3288_gmac_setup(struct platform_device *pdev)
421*0fb98db1SHeiko Stübner {
422*0fb98db1SHeiko Stübner 	return rk_gmac_setup(pdev, &rk3288_ops);
423*0fb98db1SHeiko Stübner }
424*0fb98db1SHeiko Stübner 
4257ad269eaSRoger Chen static int rk_gmac_init(struct platform_device *pdev, void *priv)
4267ad269eaSRoger Chen {
4277ad269eaSRoger Chen 	struct rk_priv_data *bsp_priv = priv;
4287ad269eaSRoger Chen 	int ret;
4297ad269eaSRoger Chen 
4307ad269eaSRoger Chen 	ret = phy_power_on(bsp_priv, true);
4317ad269eaSRoger Chen 	if (ret)
4327ad269eaSRoger Chen 		return ret;
4337ad269eaSRoger Chen 
4347ad269eaSRoger Chen 	ret = gmac_clk_enable(bsp_priv, true);
4357ad269eaSRoger Chen 	if (ret)
4367ad269eaSRoger Chen 		return ret;
4377ad269eaSRoger Chen 
4387ad269eaSRoger Chen 	return 0;
4397ad269eaSRoger Chen }
4407ad269eaSRoger Chen 
4417ad269eaSRoger Chen static void rk_gmac_exit(struct platform_device *pdev, void *priv)
4427ad269eaSRoger Chen {
4437ad269eaSRoger Chen 	struct rk_priv_data *gmac = priv;
4447ad269eaSRoger Chen 
4457ad269eaSRoger Chen 	phy_power_on(gmac, false);
4467ad269eaSRoger Chen 	gmac_clk_enable(gmac, false);
4477ad269eaSRoger Chen }
4487ad269eaSRoger Chen 
4497ad269eaSRoger Chen static void rk_fix_speed(void *priv, unsigned int speed)
4507ad269eaSRoger Chen {
4517ad269eaSRoger Chen 	struct rk_priv_data *bsp_priv = priv;
4527ad269eaSRoger Chen 	struct device *dev = &bsp_priv->pdev->dev;
4537ad269eaSRoger Chen 
4547ad269eaSRoger Chen 	if (bsp_priv->phy_iface == PHY_INTERFACE_MODE_RGMII)
455*0fb98db1SHeiko Stübner 		bsp_priv->ops->set_rgmii_speed(bsp_priv, speed);
4567ad269eaSRoger Chen 	else if (bsp_priv->phy_iface == PHY_INTERFACE_MODE_RMII)
457*0fb98db1SHeiko Stübner 		bsp_priv->ops->set_rmii_speed(bsp_priv, speed);
4587ad269eaSRoger Chen 	else
4597ad269eaSRoger Chen 		dev_err(dev, "unsupported interface %d", bsp_priv->phy_iface);
4607ad269eaSRoger Chen }
4617ad269eaSRoger Chen 
462e0fb4013SJoachim Eastwood static const struct stmmac_of_data rk3288_gmac_data = {
4637ad269eaSRoger Chen 	.has_gmac = 1,
4647ad269eaSRoger Chen 	.fix_mac_speed = rk_fix_speed,
465*0fb98db1SHeiko Stübner 	.setup = rk3288_gmac_setup,
4667ad269eaSRoger Chen 	.init = rk_gmac_init,
4677ad269eaSRoger Chen 	.exit = rk_gmac_exit,
4687ad269eaSRoger Chen };
469e0fb4013SJoachim Eastwood 
470e0fb4013SJoachim Eastwood static const struct of_device_id rk_gmac_dwmac_match[] = {
471e0fb4013SJoachim Eastwood 	{ .compatible = "rockchip,rk3288-gmac", .data = &rk3288_gmac_data},
472e0fb4013SJoachim Eastwood 	{ }
473e0fb4013SJoachim Eastwood };
474e0fb4013SJoachim Eastwood MODULE_DEVICE_TABLE(of, rk_gmac_dwmac_match);
475e0fb4013SJoachim Eastwood 
476e0fb4013SJoachim Eastwood static struct platform_driver rk_gmac_dwmac_driver = {
477e0fb4013SJoachim Eastwood 	.probe  = stmmac_pltfr_probe,
478e0fb4013SJoachim Eastwood 	.remove = stmmac_pltfr_remove,
479e0fb4013SJoachim Eastwood 	.driver = {
480e0fb4013SJoachim Eastwood 		.name           = "rk_gmac-dwmac",
481e0fb4013SJoachim Eastwood 		.pm		= &stmmac_pltfr_pm_ops,
482e0fb4013SJoachim Eastwood 		.of_match_table = rk_gmac_dwmac_match,
483e0fb4013SJoachim Eastwood 	},
484e0fb4013SJoachim Eastwood };
485e0fb4013SJoachim Eastwood module_platform_driver(rk_gmac_dwmac_driver);
486e0fb4013SJoachim Eastwood 
487e0fb4013SJoachim Eastwood MODULE_AUTHOR("Chen-Zhi (Roger Chen) <roger.chen@rock-chips.com>");
488e0fb4013SJoachim Eastwood MODULE_DESCRIPTION("Rockchip RK3288 DWMAC specific glue layer");
489e0fb4013SJoachim Eastwood MODULE_LICENSE("GPL");
490