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>
257ad269eaSRoger Chen #include <linux/of_gpio.h>
267ad269eaSRoger Chen #include <linux/of_device.h>
277ad269eaSRoger Chen #include <linux/regulator/consumer.h>
287ad269eaSRoger Chen #include <linux/delay.h>
297ad269eaSRoger Chen #include <linux/mfd/syscon.h>
307ad269eaSRoger Chen #include <linux/regmap.h>
317ad269eaSRoger Chen 
327ad269eaSRoger Chen struct rk_priv_data {
337ad269eaSRoger Chen 	struct platform_device *pdev;
347ad269eaSRoger Chen 	int phy_iface;
352e12f536SRomain Perier 	struct regulator *regulator;
367ad269eaSRoger Chen 
377ad269eaSRoger Chen 	bool clk_enabled;
387ad269eaSRoger Chen 	bool clock_input;
397ad269eaSRoger Chen 
407ad269eaSRoger Chen 	struct clk *clk_mac;
417ad269eaSRoger Chen 	struct clk *clk_mac_pll;
427ad269eaSRoger Chen 	struct clk *gmac_clkin;
437ad269eaSRoger Chen 	struct clk *mac_clk_rx;
447ad269eaSRoger Chen 	struct clk *mac_clk_tx;
457ad269eaSRoger Chen 	struct clk *clk_mac_ref;
467ad269eaSRoger Chen 	struct clk *clk_mac_refout;
477ad269eaSRoger Chen 	struct clk *aclk_mac;
487ad269eaSRoger Chen 	struct clk *pclk_mac;
497ad269eaSRoger Chen 
507ad269eaSRoger Chen 	int tx_delay;
517ad269eaSRoger Chen 	int rx_delay;
527ad269eaSRoger Chen 
537ad269eaSRoger Chen 	struct regmap *grf;
547ad269eaSRoger Chen };
557ad269eaSRoger Chen 
567ad269eaSRoger Chen #define HIWORD_UPDATE(val, mask, shift) \
577ad269eaSRoger Chen 		((val) << (shift) | (mask) << ((shift) + 16))
587ad269eaSRoger Chen 
597ad269eaSRoger Chen #define GRF_BIT(nr)	(BIT(nr) | BIT(nr+16))
607ad269eaSRoger Chen #define GRF_CLR_BIT(nr)	(BIT(nr+16))
617ad269eaSRoger Chen 
627ad269eaSRoger Chen #define RK3288_GRF_SOC_CON1	0x0248
637ad269eaSRoger Chen #define RK3288_GRF_SOC_CON3	0x0250
647ad269eaSRoger Chen #define RK3288_GRF_GPIO3D_E	0x01ec
657ad269eaSRoger Chen #define RK3288_GRF_GPIO4A_E	0x01f0
667ad269eaSRoger Chen #define RK3288_GRF_GPIO4B_E	0x01f4
677ad269eaSRoger Chen 
687ad269eaSRoger Chen /*RK3288_GRF_SOC_CON1*/
697ad269eaSRoger Chen #define GMAC_PHY_INTF_SEL_RGMII	(GRF_BIT(6) | GRF_CLR_BIT(7) | GRF_CLR_BIT(8))
707ad269eaSRoger Chen #define GMAC_PHY_INTF_SEL_RMII	(GRF_CLR_BIT(6) | GRF_CLR_BIT(7) | GRF_BIT(8))
717ad269eaSRoger Chen #define GMAC_FLOW_CTRL		GRF_BIT(9)
727ad269eaSRoger Chen #define GMAC_FLOW_CTRL_CLR	GRF_CLR_BIT(9)
737ad269eaSRoger Chen #define GMAC_SPEED_10M		GRF_CLR_BIT(10)
747ad269eaSRoger Chen #define GMAC_SPEED_100M		GRF_BIT(10)
757ad269eaSRoger Chen #define GMAC_RMII_CLK_25M	GRF_BIT(11)
767ad269eaSRoger Chen #define GMAC_RMII_CLK_2_5M	GRF_CLR_BIT(11)
777ad269eaSRoger Chen #define GMAC_CLK_125M		(GRF_CLR_BIT(12) | GRF_CLR_BIT(13))
787ad269eaSRoger Chen #define GMAC_CLK_25M		(GRF_BIT(12) | GRF_BIT(13))
797ad269eaSRoger Chen #define GMAC_CLK_2_5M		(GRF_CLR_BIT(12) | GRF_BIT(13))
807ad269eaSRoger Chen #define GMAC_RMII_MODE		GRF_BIT(14)
817ad269eaSRoger Chen #define GMAC_RMII_MODE_CLR	GRF_CLR_BIT(14)
827ad269eaSRoger Chen 
837ad269eaSRoger Chen /*RK3288_GRF_SOC_CON3*/
847ad269eaSRoger Chen #define GMAC_TXCLK_DLY_ENABLE	GRF_BIT(14)
857ad269eaSRoger Chen #define GMAC_TXCLK_DLY_DISABLE	GRF_CLR_BIT(14)
867ad269eaSRoger Chen #define GMAC_RXCLK_DLY_ENABLE	GRF_BIT(15)
877ad269eaSRoger Chen #define GMAC_RXCLK_DLY_DISABLE	GRF_CLR_BIT(15)
887ad269eaSRoger Chen #define GMAC_CLK_RX_DL_CFG(val)	HIWORD_UPDATE(val, 0x7F, 7)
897ad269eaSRoger Chen #define GMAC_CLK_TX_DL_CFG(val)	HIWORD_UPDATE(val, 0x7F, 0)
907ad269eaSRoger Chen 
917ad269eaSRoger Chen static void set_to_rgmii(struct rk_priv_data *bsp_priv,
927ad269eaSRoger Chen 			 int tx_delay, int rx_delay)
937ad269eaSRoger Chen {
947ad269eaSRoger Chen 	struct device *dev = &bsp_priv->pdev->dev;
957ad269eaSRoger Chen 
967ad269eaSRoger Chen 	if (IS_ERR(bsp_priv->grf)) {
977ad269eaSRoger Chen 		dev_err(dev, "%s: Missing rockchip,grf property\n", __func__);
987ad269eaSRoger Chen 		return;
997ad269eaSRoger Chen 	}
1007ad269eaSRoger Chen 
1017ad269eaSRoger Chen 	regmap_write(bsp_priv->grf, RK3288_GRF_SOC_CON1,
1027ad269eaSRoger Chen 		     GMAC_PHY_INTF_SEL_RGMII | GMAC_RMII_MODE_CLR);
1037ad269eaSRoger Chen 	regmap_write(bsp_priv->grf, RK3288_GRF_SOC_CON3,
1047ad269eaSRoger Chen 		     GMAC_RXCLK_DLY_ENABLE | GMAC_TXCLK_DLY_ENABLE |
1057ad269eaSRoger Chen 		     GMAC_CLK_RX_DL_CFG(rx_delay) |
1067ad269eaSRoger Chen 		     GMAC_CLK_TX_DL_CFG(tx_delay));
1077ad269eaSRoger Chen }
1087ad269eaSRoger Chen 
1097ad269eaSRoger Chen static void set_to_rmii(struct rk_priv_data *bsp_priv)
1107ad269eaSRoger Chen {
1117ad269eaSRoger Chen 	struct device *dev = &bsp_priv->pdev->dev;
1127ad269eaSRoger Chen 
1137ad269eaSRoger Chen 	if (IS_ERR(bsp_priv->grf)) {
1147ad269eaSRoger Chen 		dev_err(dev, "%s: Missing rockchip,grf property\n", __func__);
1157ad269eaSRoger Chen 		return;
1167ad269eaSRoger Chen 	}
1177ad269eaSRoger Chen 
1187ad269eaSRoger Chen 	regmap_write(bsp_priv->grf, RK3288_GRF_SOC_CON1,
1197ad269eaSRoger Chen 		     GMAC_PHY_INTF_SEL_RMII | GMAC_RMII_MODE);
1207ad269eaSRoger Chen }
1217ad269eaSRoger Chen 
1227ad269eaSRoger Chen static void set_rgmii_speed(struct rk_priv_data *bsp_priv, int speed)
1237ad269eaSRoger Chen {
1247ad269eaSRoger Chen 	struct device *dev = &bsp_priv->pdev->dev;
1257ad269eaSRoger Chen 
1267ad269eaSRoger Chen 	if (IS_ERR(bsp_priv->grf)) {
1277ad269eaSRoger Chen 		dev_err(dev, "%s: Missing rockchip,grf property\n", __func__);
1287ad269eaSRoger Chen 		return;
1297ad269eaSRoger Chen 	}
1307ad269eaSRoger Chen 
1317ad269eaSRoger Chen 	if (speed == 10)
1327ad269eaSRoger Chen 		regmap_write(bsp_priv->grf, RK3288_GRF_SOC_CON1, GMAC_CLK_2_5M);
1337ad269eaSRoger Chen 	else if (speed == 100)
1347ad269eaSRoger Chen 		regmap_write(bsp_priv->grf, RK3288_GRF_SOC_CON1, GMAC_CLK_25M);
1357ad269eaSRoger Chen 	else if (speed == 1000)
1367ad269eaSRoger Chen 		regmap_write(bsp_priv->grf, RK3288_GRF_SOC_CON1, GMAC_CLK_125M);
1377ad269eaSRoger Chen 	else
1387ad269eaSRoger Chen 		dev_err(dev, "unknown speed value for RGMII! speed=%d", speed);
1397ad269eaSRoger Chen }
1407ad269eaSRoger Chen 
1417ad269eaSRoger Chen static void set_rmii_speed(struct rk_priv_data *bsp_priv, int speed)
1427ad269eaSRoger Chen {
1437ad269eaSRoger Chen 	struct device *dev = &bsp_priv->pdev->dev;
1447ad269eaSRoger Chen 
1457ad269eaSRoger Chen 	if (IS_ERR(bsp_priv->grf)) {
1467ad269eaSRoger Chen 		dev_err(dev, "%s: Missing rockchip,grf property\n", __func__);
1477ad269eaSRoger Chen 		return;
1487ad269eaSRoger Chen 	}
1497ad269eaSRoger Chen 
1507ad269eaSRoger Chen 	if (speed == 10) {
1517ad269eaSRoger Chen 		regmap_write(bsp_priv->grf, RK3288_GRF_SOC_CON1,
1527ad269eaSRoger Chen 			     GMAC_RMII_CLK_2_5M | GMAC_SPEED_10M);
1537ad269eaSRoger Chen 	} else if (speed == 100) {
1547ad269eaSRoger Chen 		regmap_write(bsp_priv->grf, RK3288_GRF_SOC_CON1,
1557ad269eaSRoger Chen 			     GMAC_RMII_CLK_25M | GMAC_SPEED_100M);
1567ad269eaSRoger Chen 	} else {
1577ad269eaSRoger Chen 		dev_err(dev, "unknown speed value for RMII! speed=%d", speed);
1587ad269eaSRoger Chen 	}
1597ad269eaSRoger Chen }
1607ad269eaSRoger Chen 
1617ad269eaSRoger Chen static int gmac_clk_init(struct rk_priv_data *bsp_priv)
1627ad269eaSRoger Chen {
1637ad269eaSRoger Chen 	struct device *dev = &bsp_priv->pdev->dev;
1647ad269eaSRoger Chen 
1657ad269eaSRoger Chen 	bsp_priv->clk_enabled = false;
1667ad269eaSRoger Chen 
1677ad269eaSRoger Chen 	bsp_priv->mac_clk_rx = devm_clk_get(dev, "mac_clk_rx");
1687ad269eaSRoger Chen 	if (IS_ERR(bsp_priv->mac_clk_rx))
1697ad269eaSRoger Chen 		dev_err(dev, "%s: cannot get clock %s\n",
1707ad269eaSRoger Chen 			__func__, "mac_clk_rx");
1717ad269eaSRoger Chen 
1727ad269eaSRoger Chen 	bsp_priv->mac_clk_tx = devm_clk_get(dev, "mac_clk_tx");
1737ad269eaSRoger Chen 	if (IS_ERR(bsp_priv->mac_clk_tx))
1747ad269eaSRoger Chen 		dev_err(dev, "%s: cannot get clock %s\n",
1757ad269eaSRoger Chen 			__func__, "mac_clk_tx");
1767ad269eaSRoger Chen 
1777ad269eaSRoger Chen 	bsp_priv->aclk_mac = devm_clk_get(dev, "aclk_mac");
1787ad269eaSRoger Chen 	if (IS_ERR(bsp_priv->aclk_mac))
1797ad269eaSRoger Chen 		dev_err(dev, "%s: cannot get clock %s\n",
1807ad269eaSRoger Chen 			__func__, "aclk_mac");
1817ad269eaSRoger Chen 
1827ad269eaSRoger Chen 	bsp_priv->pclk_mac = devm_clk_get(dev, "pclk_mac");
1837ad269eaSRoger Chen 	if (IS_ERR(bsp_priv->pclk_mac))
1847ad269eaSRoger Chen 		dev_err(dev, "%s: cannot get clock %s\n",
1857ad269eaSRoger Chen 			__func__, "pclk_mac");
1867ad269eaSRoger Chen 
1877ad269eaSRoger Chen 	bsp_priv->clk_mac = devm_clk_get(dev, "stmmaceth");
1887ad269eaSRoger Chen 	if (IS_ERR(bsp_priv->clk_mac))
1897ad269eaSRoger Chen 		dev_err(dev, "%s: cannot get clock %s\n",
1907ad269eaSRoger Chen 			__func__, "stmmaceth");
1917ad269eaSRoger Chen 
1927ad269eaSRoger Chen 	if (bsp_priv->phy_iface == PHY_INTERFACE_MODE_RMII) {
1937ad269eaSRoger Chen 		bsp_priv->clk_mac_ref = devm_clk_get(dev, "clk_mac_ref");
1947ad269eaSRoger Chen 		if (IS_ERR(bsp_priv->clk_mac_ref))
1957ad269eaSRoger Chen 			dev_err(dev, "%s: cannot get clock %s\n",
1967ad269eaSRoger Chen 				__func__, "clk_mac_ref");
1977ad269eaSRoger Chen 
1987ad269eaSRoger Chen 		if (!bsp_priv->clock_input) {
1997ad269eaSRoger Chen 			bsp_priv->clk_mac_refout =
2007ad269eaSRoger Chen 				devm_clk_get(dev, "clk_mac_refout");
2017ad269eaSRoger Chen 			if (IS_ERR(bsp_priv->clk_mac_refout))
2027ad269eaSRoger Chen 				dev_err(dev, "%s: cannot get clock %s\n",
2037ad269eaSRoger Chen 					__func__, "clk_mac_refout");
2047ad269eaSRoger Chen 		}
2057ad269eaSRoger Chen 	}
2067ad269eaSRoger Chen 
2077ad269eaSRoger Chen 	if (bsp_priv->clock_input) {
2087ad269eaSRoger Chen 		dev_info(dev, "%s: clock input from PHY\n", __func__);
2097ad269eaSRoger Chen 	} else {
2107ad269eaSRoger Chen 		if (bsp_priv->phy_iface == PHY_INTERFACE_MODE_RMII)
2117ad269eaSRoger Chen 			clk_set_rate(bsp_priv->clk_mac_pll, 50000000);
2127ad269eaSRoger Chen 	}
2137ad269eaSRoger Chen 
2147ad269eaSRoger Chen 	return 0;
2157ad269eaSRoger Chen }
2167ad269eaSRoger Chen 
2177ad269eaSRoger Chen static int gmac_clk_enable(struct rk_priv_data *bsp_priv, bool enable)
2187ad269eaSRoger Chen {
2197ad269eaSRoger Chen 	int phy_iface = phy_iface = bsp_priv->phy_iface;
2207ad269eaSRoger Chen 
2217ad269eaSRoger Chen 	if (enable) {
2227ad269eaSRoger Chen 		if (!bsp_priv->clk_enabled) {
2237ad269eaSRoger Chen 			if (phy_iface == PHY_INTERFACE_MODE_RMII) {
2247ad269eaSRoger Chen 				if (!IS_ERR(bsp_priv->mac_clk_rx))
2257ad269eaSRoger Chen 					clk_prepare_enable(
2267ad269eaSRoger Chen 						bsp_priv->mac_clk_rx);
2277ad269eaSRoger Chen 
2287ad269eaSRoger Chen 				if (!IS_ERR(bsp_priv->clk_mac_ref))
2297ad269eaSRoger Chen 					clk_prepare_enable(
2307ad269eaSRoger Chen 						bsp_priv->clk_mac_ref);
2317ad269eaSRoger Chen 
2327ad269eaSRoger Chen 				if (!IS_ERR(bsp_priv->clk_mac_refout))
2337ad269eaSRoger Chen 					clk_prepare_enable(
2347ad269eaSRoger Chen 						bsp_priv->clk_mac_refout);
2357ad269eaSRoger Chen 			}
2367ad269eaSRoger Chen 
2377ad269eaSRoger Chen 			if (!IS_ERR(bsp_priv->aclk_mac))
2387ad269eaSRoger Chen 				clk_prepare_enable(bsp_priv->aclk_mac);
2397ad269eaSRoger Chen 
2407ad269eaSRoger Chen 			if (!IS_ERR(bsp_priv->pclk_mac))
2417ad269eaSRoger Chen 				clk_prepare_enable(bsp_priv->pclk_mac);
2427ad269eaSRoger Chen 
2437ad269eaSRoger Chen 			if (!IS_ERR(bsp_priv->mac_clk_tx))
2447ad269eaSRoger Chen 				clk_prepare_enable(bsp_priv->mac_clk_tx);
2457ad269eaSRoger Chen 
2467ad269eaSRoger Chen 			/**
2477ad269eaSRoger Chen 			 * if (!IS_ERR(bsp_priv->clk_mac))
2487ad269eaSRoger Chen 			 *	clk_prepare_enable(bsp_priv->clk_mac);
2497ad269eaSRoger Chen 			 */
2507ad269eaSRoger Chen 			mdelay(5);
2517ad269eaSRoger Chen 			bsp_priv->clk_enabled = true;
2527ad269eaSRoger Chen 		}
2537ad269eaSRoger Chen 	} else {
2547ad269eaSRoger Chen 		if (bsp_priv->clk_enabled) {
2557ad269eaSRoger Chen 			if (phy_iface == PHY_INTERFACE_MODE_RMII) {
2567ad269eaSRoger Chen 				if (!IS_ERR(bsp_priv->mac_clk_rx))
2577ad269eaSRoger Chen 					clk_disable_unprepare(
2587ad269eaSRoger Chen 						bsp_priv->mac_clk_rx);
2597ad269eaSRoger Chen 
2607ad269eaSRoger Chen 				if (!IS_ERR(bsp_priv->clk_mac_ref))
2617ad269eaSRoger Chen 					clk_disable_unprepare(
2627ad269eaSRoger Chen 						bsp_priv->clk_mac_ref);
2637ad269eaSRoger Chen 
2647ad269eaSRoger Chen 				if (!IS_ERR(bsp_priv->clk_mac_refout))
2657ad269eaSRoger Chen 					clk_disable_unprepare(
2667ad269eaSRoger Chen 						bsp_priv->clk_mac_refout);
2677ad269eaSRoger Chen 			}
2687ad269eaSRoger Chen 
2697ad269eaSRoger Chen 			if (!IS_ERR(bsp_priv->aclk_mac))
2707ad269eaSRoger Chen 				clk_disable_unprepare(bsp_priv->aclk_mac);
2717ad269eaSRoger Chen 
2727ad269eaSRoger Chen 			if (!IS_ERR(bsp_priv->pclk_mac))
2737ad269eaSRoger Chen 				clk_disable_unprepare(bsp_priv->pclk_mac);
2747ad269eaSRoger Chen 
2757ad269eaSRoger Chen 			if (!IS_ERR(bsp_priv->mac_clk_tx))
2767ad269eaSRoger Chen 				clk_disable_unprepare(bsp_priv->mac_clk_tx);
2777ad269eaSRoger Chen 			/**
2787ad269eaSRoger Chen 			 * if (!IS_ERR(bsp_priv->clk_mac))
2797ad269eaSRoger Chen 			 *	clk_disable_unprepare(bsp_priv->clk_mac);
2807ad269eaSRoger Chen 			 */
2817ad269eaSRoger Chen 			bsp_priv->clk_enabled = false;
2827ad269eaSRoger Chen 		}
2837ad269eaSRoger Chen 	}
2847ad269eaSRoger Chen 
2857ad269eaSRoger Chen 	return 0;
2867ad269eaSRoger Chen }
2877ad269eaSRoger Chen 
2887ad269eaSRoger Chen static int phy_power_on(struct rk_priv_data *bsp_priv, bool enable)
2897ad269eaSRoger Chen {
2902e12f536SRomain Perier 	struct regulator *ldo = bsp_priv->regulator;
2917ad269eaSRoger Chen 	int ret;
2927ad269eaSRoger Chen 	struct device *dev = &bsp_priv->pdev->dev;
2937ad269eaSRoger Chen 
2942e12f536SRomain Perier 	if (!ldo) {
2952e12f536SRomain Perier 		dev_err(dev, "%s: no regulator found\n", __func__);
2967ad269eaSRoger Chen 		return -1;
2977ad269eaSRoger Chen 	}
2987ad269eaSRoger Chen 
2997ad269eaSRoger Chen 	if (enable) {
3007ad269eaSRoger Chen 		ret = regulator_enable(ldo);
3012e12f536SRomain Perier 		if (ret)
3022e12f536SRomain Perier 			dev_err(dev, "%s: fail to enable phy-supply\n",
3032e12f536SRomain Perier 				__func__);
3047ad269eaSRoger Chen 	} else {
3057ad269eaSRoger Chen 		ret = regulator_disable(ldo);
3062e12f536SRomain Perier 		if (ret)
3072e12f536SRomain Perier 			dev_err(dev, "%s: fail to disable phy-supply\n",
3082e12f536SRomain Perier 				__func__);
3097ad269eaSRoger Chen 	}
3107ad269eaSRoger Chen 
3117ad269eaSRoger Chen 	return 0;
3127ad269eaSRoger Chen }
3137ad269eaSRoger Chen 
3147ad269eaSRoger Chen static void *rk_gmac_setup(struct platform_device *pdev)
3157ad269eaSRoger Chen {
3167ad269eaSRoger Chen 	struct rk_priv_data *bsp_priv;
3177ad269eaSRoger Chen 	struct device *dev = &pdev->dev;
3187ad269eaSRoger Chen 	int ret;
3197ad269eaSRoger Chen 	const char *strings = NULL;
3207ad269eaSRoger Chen 	int value;
3217ad269eaSRoger Chen 
3227ad269eaSRoger Chen 	bsp_priv = devm_kzalloc(dev, sizeof(*bsp_priv), GFP_KERNEL);
3237ad269eaSRoger Chen 	if (!bsp_priv)
3247ad269eaSRoger Chen 		return ERR_PTR(-ENOMEM);
3257ad269eaSRoger Chen 
3267ad269eaSRoger Chen 	bsp_priv->phy_iface = of_get_phy_mode(dev->of_node);
3277ad269eaSRoger Chen 
3282e12f536SRomain Perier 	bsp_priv->regulator = devm_regulator_get_optional(dev, "phy");
3292e12f536SRomain Perier 	if (IS_ERR(bsp_priv->regulator)) {
3302e12f536SRomain Perier 		if (PTR_ERR(bsp_priv->regulator) == -EPROBE_DEFER) {
3312e12f536SRomain Perier 			dev_err(dev, "phy regulator is not available yet, deferred probing\n");
3322e12f536SRomain Perier 			return ERR_PTR(-EPROBE_DEFER);
3332e12f536SRomain Perier 		}
3342e12f536SRomain Perier 		dev_err(dev, "no regulator found\n");
3352e12f536SRomain Perier 		bsp_priv->regulator = NULL;
3367ad269eaSRoger Chen 	}
3377ad269eaSRoger Chen 
3387ad269eaSRoger Chen 	ret = of_property_read_string(dev->of_node, "clock_in_out", &strings);
3397ad269eaSRoger Chen 	if (ret) {
3407ad269eaSRoger Chen 		dev_err(dev, "%s: Can not read property: clock_in_out.\n",
3417ad269eaSRoger Chen 			__func__);
3427ad269eaSRoger Chen 		bsp_priv->clock_input = true;
3437ad269eaSRoger Chen 	} else {
3447ad269eaSRoger Chen 		dev_info(dev, "%s: clock input or output? (%s).\n",
3457ad269eaSRoger Chen 			 __func__, strings);
3467ad269eaSRoger Chen 		if (!strcmp(strings, "input"))
3477ad269eaSRoger Chen 			bsp_priv->clock_input = true;
3487ad269eaSRoger Chen 		else
3497ad269eaSRoger Chen 			bsp_priv->clock_input = false;
3507ad269eaSRoger Chen 	}
3517ad269eaSRoger Chen 
3527ad269eaSRoger Chen 	ret = of_property_read_u32(dev->of_node, "tx_delay", &value);
3537ad269eaSRoger Chen 	if (ret) {
3547ad269eaSRoger Chen 		bsp_priv->tx_delay = 0x30;
3557ad269eaSRoger Chen 		dev_err(dev, "%s: Can not read property: tx_delay.", __func__);
3567ad269eaSRoger Chen 		dev_err(dev, "%s: set tx_delay to 0x%x\n",
3577ad269eaSRoger Chen 			__func__, bsp_priv->tx_delay);
3587ad269eaSRoger Chen 	} else {
3597ad269eaSRoger Chen 		dev_info(dev, "%s: TX delay(0x%x).\n", __func__, value);
3607ad269eaSRoger Chen 		bsp_priv->tx_delay = value;
3617ad269eaSRoger Chen 	}
3627ad269eaSRoger Chen 
3637ad269eaSRoger Chen 	ret = of_property_read_u32(dev->of_node, "rx_delay", &value);
3647ad269eaSRoger Chen 	if (ret) {
3657ad269eaSRoger Chen 		bsp_priv->rx_delay = 0x10;
3667ad269eaSRoger Chen 		dev_err(dev, "%s: Can not read property: rx_delay.", __func__);
3677ad269eaSRoger Chen 		dev_err(dev, "%s: set rx_delay to 0x%x\n",
3687ad269eaSRoger Chen 			__func__, bsp_priv->rx_delay);
3697ad269eaSRoger Chen 	} else {
3707ad269eaSRoger Chen 		dev_info(dev, "%s: RX delay(0x%x).\n", __func__, value);
3717ad269eaSRoger Chen 		bsp_priv->rx_delay = value;
3727ad269eaSRoger Chen 	}
3737ad269eaSRoger Chen 
3747ad269eaSRoger Chen 	bsp_priv->grf = syscon_regmap_lookup_by_phandle(dev->of_node,
3757ad269eaSRoger Chen 							"rockchip,grf");
3767ad269eaSRoger Chen 	bsp_priv->pdev = pdev;
3777ad269eaSRoger Chen 
3787ad269eaSRoger Chen 	/*rmii or rgmii*/
3797ad269eaSRoger Chen 	if (bsp_priv->phy_iface == PHY_INTERFACE_MODE_RGMII) {
3807ad269eaSRoger Chen 		dev_info(dev, "%s: init for RGMII\n", __func__);
3817ad269eaSRoger Chen 		set_to_rgmii(bsp_priv, bsp_priv->tx_delay, bsp_priv->rx_delay);
3827ad269eaSRoger Chen 	} else if (bsp_priv->phy_iface == PHY_INTERFACE_MODE_RMII) {
3837ad269eaSRoger Chen 		dev_info(dev, "%s: init for RMII\n", __func__);
3847ad269eaSRoger Chen 		set_to_rmii(bsp_priv);
3857ad269eaSRoger Chen 	} else {
3867ad269eaSRoger Chen 		dev_err(dev, "%s: NO interface defined!\n", __func__);
3877ad269eaSRoger Chen 	}
3887ad269eaSRoger Chen 
3897ad269eaSRoger Chen 	gmac_clk_init(bsp_priv);
3907ad269eaSRoger Chen 
3917ad269eaSRoger Chen 	return bsp_priv;
3927ad269eaSRoger Chen }
3937ad269eaSRoger Chen 
3947ad269eaSRoger Chen static int rk_gmac_init(struct platform_device *pdev, void *priv)
3957ad269eaSRoger Chen {
3967ad269eaSRoger Chen 	struct rk_priv_data *bsp_priv = priv;
3977ad269eaSRoger Chen 	int ret;
3987ad269eaSRoger Chen 
3997ad269eaSRoger Chen 	ret = phy_power_on(bsp_priv, true);
4007ad269eaSRoger Chen 	if (ret)
4017ad269eaSRoger Chen 		return ret;
4027ad269eaSRoger Chen 
4037ad269eaSRoger Chen 	ret = gmac_clk_enable(bsp_priv, true);
4047ad269eaSRoger Chen 	if (ret)
4057ad269eaSRoger Chen 		return ret;
4067ad269eaSRoger Chen 
4077ad269eaSRoger Chen 	return 0;
4087ad269eaSRoger Chen }
4097ad269eaSRoger Chen 
4107ad269eaSRoger Chen static void rk_gmac_exit(struct platform_device *pdev, void *priv)
4117ad269eaSRoger Chen {
4127ad269eaSRoger Chen 	struct rk_priv_data *gmac = priv;
4137ad269eaSRoger Chen 
4147ad269eaSRoger Chen 	phy_power_on(gmac, false);
4157ad269eaSRoger Chen 	gmac_clk_enable(gmac, false);
4167ad269eaSRoger Chen }
4177ad269eaSRoger Chen 
4187ad269eaSRoger Chen static void rk_fix_speed(void *priv, unsigned int speed)
4197ad269eaSRoger Chen {
4207ad269eaSRoger Chen 	struct rk_priv_data *bsp_priv = priv;
4217ad269eaSRoger Chen 	struct device *dev = &bsp_priv->pdev->dev;
4227ad269eaSRoger Chen 
4237ad269eaSRoger Chen 	if (bsp_priv->phy_iface == PHY_INTERFACE_MODE_RGMII)
4247ad269eaSRoger Chen 		set_rgmii_speed(bsp_priv, speed);
4257ad269eaSRoger Chen 	else if (bsp_priv->phy_iface == PHY_INTERFACE_MODE_RMII)
4267ad269eaSRoger Chen 		set_rmii_speed(bsp_priv, speed);
4277ad269eaSRoger Chen 	else
4287ad269eaSRoger Chen 		dev_err(dev, "unsupported interface %d", bsp_priv->phy_iface);
4297ad269eaSRoger Chen }
4307ad269eaSRoger Chen 
4317ad269eaSRoger Chen const struct stmmac_of_data rk3288_gmac_data = {
4327ad269eaSRoger Chen 	.has_gmac = 1,
4337ad269eaSRoger Chen 	.fix_mac_speed = rk_fix_speed,
4347ad269eaSRoger Chen 	.setup = rk_gmac_setup,
4357ad269eaSRoger Chen 	.init = rk_gmac_init,
4367ad269eaSRoger Chen 	.exit = rk_gmac_exit,
4377ad269eaSRoger Chen };
438