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 360fb98db1SHeiko Stübner struct rk_priv_data; 370fb98db1SHeiko Stübner struct rk_gmac_ops { 380fb98db1SHeiko Stübner void (*set_to_rgmii)(struct rk_priv_data *bsp_priv, 390fb98db1SHeiko Stübner int tx_delay, int rx_delay); 400fb98db1SHeiko Stübner void (*set_to_rmii)(struct rk_priv_data *bsp_priv); 410fb98db1SHeiko Stübner void (*set_rgmii_speed)(struct rk_priv_data *bsp_priv, int speed); 420fb98db1SHeiko Stübner void (*set_rmii_speed)(struct rk_priv_data *bsp_priv, int speed); 430fb98db1SHeiko Stübner }; 440fb98db1SHeiko Stübner 457ad269eaSRoger Chen struct rk_priv_data { 467ad269eaSRoger Chen struct platform_device *pdev; 477ad269eaSRoger Chen int phy_iface; 482e12f536SRomain Perier struct regulator *regulator; 4992c2588fSJoachim Eastwood const 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*/ 790fb98db1SHeiko Stübner #define RK3288_GMAC_PHY_INTF_SEL_RGMII (GRF_BIT(6) | GRF_CLR_BIT(7) | \ 800fb98db1SHeiko Stübner GRF_CLR_BIT(8)) 810fb98db1SHeiko Stübner #define RK3288_GMAC_PHY_INTF_SEL_RMII (GRF_CLR_BIT(6) | GRF_CLR_BIT(7) | \ 820fb98db1SHeiko Stübner GRF_BIT(8)) 830fb98db1SHeiko Stübner #define RK3288_GMAC_FLOW_CTRL GRF_BIT(9) 840fb98db1SHeiko Stübner #define RK3288_GMAC_FLOW_CTRL_CLR GRF_CLR_BIT(9) 850fb98db1SHeiko Stübner #define RK3288_GMAC_SPEED_10M GRF_CLR_BIT(10) 860fb98db1SHeiko Stübner #define RK3288_GMAC_SPEED_100M GRF_BIT(10) 870fb98db1SHeiko Stübner #define RK3288_GMAC_RMII_CLK_25M GRF_BIT(11) 880fb98db1SHeiko Stübner #define RK3288_GMAC_RMII_CLK_2_5M GRF_CLR_BIT(11) 890fb98db1SHeiko Stübner #define RK3288_GMAC_CLK_125M (GRF_CLR_BIT(12) | GRF_CLR_BIT(13)) 900fb98db1SHeiko Stübner #define RK3288_GMAC_CLK_25M (GRF_BIT(12) | GRF_BIT(13)) 910fb98db1SHeiko Stübner #define RK3288_GMAC_CLK_2_5M (GRF_CLR_BIT(12) | GRF_BIT(13)) 920fb98db1SHeiko Stübner #define RK3288_GMAC_RMII_MODE GRF_BIT(14) 930fb98db1SHeiko Stübner #define RK3288_GMAC_RMII_MODE_CLR GRF_CLR_BIT(14) 947ad269eaSRoger Chen 957ad269eaSRoger Chen /*RK3288_GRF_SOC_CON3*/ 960fb98db1SHeiko Stübner #define RK3288_GMAC_TXCLK_DLY_ENABLE GRF_BIT(14) 970fb98db1SHeiko Stübner #define RK3288_GMAC_TXCLK_DLY_DISABLE GRF_CLR_BIT(14) 980fb98db1SHeiko Stübner #define RK3288_GMAC_RXCLK_DLY_ENABLE GRF_BIT(15) 990fb98db1SHeiko Stübner #define RK3288_GMAC_RXCLK_DLY_DISABLE GRF_CLR_BIT(15) 1000fb98db1SHeiko Stübner #define RK3288_GMAC_CLK_RX_DL_CFG(val) HIWORD_UPDATE(val, 0x7F, 7) 1010fb98db1SHeiko Stübner #define RK3288_GMAC_CLK_TX_DL_CFG(val) HIWORD_UPDATE(val, 0x7F, 0) 1027ad269eaSRoger Chen 1030fb98db1SHeiko 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, 1140fb98db1SHeiko Stübner RK3288_GMAC_PHY_INTF_SEL_RGMII | 1150fb98db1SHeiko Stübner RK3288_GMAC_RMII_MODE_CLR); 1167ad269eaSRoger Chen regmap_write(bsp_priv->grf, RK3288_GRF_SOC_CON3, 1170fb98db1SHeiko Stübner RK3288_GMAC_RXCLK_DLY_ENABLE | 1180fb98db1SHeiko Stübner RK3288_GMAC_TXCLK_DLY_ENABLE | 1190fb98db1SHeiko Stübner RK3288_GMAC_CLK_RX_DL_CFG(rx_delay) | 1200fb98db1SHeiko Stübner RK3288_GMAC_CLK_TX_DL_CFG(tx_delay)); 1217ad269eaSRoger Chen } 1227ad269eaSRoger Chen 1230fb98db1SHeiko 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, 1330fb98db1SHeiko Stübner RK3288_GMAC_PHY_INTF_SEL_RMII | RK3288_GMAC_RMII_MODE); 1347ad269eaSRoger Chen } 1357ad269eaSRoger Chen 1360fb98db1SHeiko 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) 1460fb98db1SHeiko Stübner regmap_write(bsp_priv->grf, RK3288_GRF_SOC_CON1, 1470fb98db1SHeiko Stübner RK3288_GMAC_CLK_2_5M); 1487ad269eaSRoger Chen else if (speed == 100) 1490fb98db1SHeiko Stübner regmap_write(bsp_priv->grf, RK3288_GRF_SOC_CON1, 1500fb98db1SHeiko Stübner RK3288_GMAC_CLK_25M); 1517ad269eaSRoger Chen else if (speed == 1000) 1520fb98db1SHeiko Stübner regmap_write(bsp_priv->grf, RK3288_GRF_SOC_CON1, 1530fb98db1SHeiko 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 1580fb98db1SHeiko 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, 1690fb98db1SHeiko Stübner RK3288_GMAC_RMII_CLK_2_5M | 1700fb98db1SHeiko Stübner RK3288_GMAC_SPEED_10M); 1717ad269eaSRoger Chen } else if (speed == 100) { 1727ad269eaSRoger Chen regmap_write(bsp_priv->grf, RK3288_GRF_SOC_CON1, 1730fb98db1SHeiko Stübner RK3288_GMAC_RMII_CLK_25M | 1740fb98db1SHeiko 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 18092c2588fSJoachim Eastwood static const struct rk_gmac_ops rk3288_ops = { 1810fb98db1SHeiko Stübner .set_to_rgmii = rk3288_set_to_rgmii, 1820fb98db1SHeiko Stübner .set_to_rmii = rk3288_set_to_rmii, 1830fb98db1SHeiko Stübner .set_rgmii_speed = rk3288_set_rgmii_speed, 1840fb98db1SHeiko Stübner .set_rmii_speed = rk3288_set_rmii_speed, 1850fb98db1SHeiko Stübner }; 1860fb98db1SHeiko Stübner 187df558854SHeiko Stübner #define RK3368_GRF_SOC_CON15 0x043c 188df558854SHeiko Stübner #define RK3368_GRF_SOC_CON16 0x0440 189df558854SHeiko Stübner 190df558854SHeiko Stübner /* RK3368_GRF_SOC_CON15 */ 191df558854SHeiko Stübner #define RK3368_GMAC_PHY_INTF_SEL_RGMII (GRF_BIT(9) | GRF_CLR_BIT(10) | \ 192df558854SHeiko Stübner GRF_CLR_BIT(11)) 193df558854SHeiko Stübner #define RK3368_GMAC_PHY_INTF_SEL_RMII (GRF_CLR_BIT(9) | GRF_CLR_BIT(10) | \ 194df558854SHeiko Stübner GRF_BIT(11)) 195df558854SHeiko Stübner #define RK3368_GMAC_FLOW_CTRL GRF_BIT(8) 196df558854SHeiko Stübner #define RK3368_GMAC_FLOW_CTRL_CLR GRF_CLR_BIT(8) 197df558854SHeiko Stübner #define RK3368_GMAC_SPEED_10M GRF_CLR_BIT(7) 198df558854SHeiko Stübner #define RK3368_GMAC_SPEED_100M GRF_BIT(7) 199df558854SHeiko Stübner #define RK3368_GMAC_RMII_CLK_25M GRF_BIT(3) 200df558854SHeiko Stübner #define RK3368_GMAC_RMII_CLK_2_5M GRF_CLR_BIT(3) 201df558854SHeiko Stübner #define RK3368_GMAC_CLK_125M (GRF_CLR_BIT(4) | GRF_CLR_BIT(5)) 202df558854SHeiko Stübner #define RK3368_GMAC_CLK_25M (GRF_BIT(4) | GRF_BIT(5)) 203df558854SHeiko Stübner #define RK3368_GMAC_CLK_2_5M (GRF_CLR_BIT(4) | GRF_BIT(5)) 204df558854SHeiko Stübner #define RK3368_GMAC_RMII_MODE GRF_BIT(6) 205df558854SHeiko Stübner #define RK3368_GMAC_RMII_MODE_CLR GRF_CLR_BIT(6) 206df558854SHeiko Stübner 207df558854SHeiko Stübner /* RK3368_GRF_SOC_CON16 */ 208df558854SHeiko Stübner #define RK3368_GMAC_TXCLK_DLY_ENABLE GRF_BIT(7) 209df558854SHeiko Stübner #define RK3368_GMAC_TXCLK_DLY_DISABLE GRF_CLR_BIT(7) 210df558854SHeiko Stübner #define RK3368_GMAC_RXCLK_DLY_ENABLE GRF_BIT(15) 211df558854SHeiko Stübner #define RK3368_GMAC_RXCLK_DLY_DISABLE GRF_CLR_BIT(15) 212df558854SHeiko Stübner #define RK3368_GMAC_CLK_RX_DL_CFG(val) HIWORD_UPDATE(val, 0x7F, 8) 213df558854SHeiko Stübner #define RK3368_GMAC_CLK_TX_DL_CFG(val) HIWORD_UPDATE(val, 0x7F, 0) 214df558854SHeiko Stübner 215df558854SHeiko Stübner static void rk3368_set_to_rgmii(struct rk_priv_data *bsp_priv, 216df558854SHeiko Stübner int tx_delay, int rx_delay) 217df558854SHeiko Stübner { 218df558854SHeiko Stübner struct device *dev = &bsp_priv->pdev->dev; 219df558854SHeiko Stübner 220df558854SHeiko Stübner if (IS_ERR(bsp_priv->grf)) { 221df558854SHeiko Stübner dev_err(dev, "%s: Missing rockchip,grf property\n", __func__); 222df558854SHeiko Stübner return; 223df558854SHeiko Stübner } 224df558854SHeiko Stübner 225df558854SHeiko Stübner regmap_write(bsp_priv->grf, RK3368_GRF_SOC_CON15, 226df558854SHeiko Stübner RK3368_GMAC_PHY_INTF_SEL_RGMII | 227df558854SHeiko Stübner RK3368_GMAC_RMII_MODE_CLR); 228df558854SHeiko Stübner regmap_write(bsp_priv->grf, RK3368_GRF_SOC_CON16, 229df558854SHeiko Stübner RK3368_GMAC_RXCLK_DLY_ENABLE | 230df558854SHeiko Stübner RK3368_GMAC_TXCLK_DLY_ENABLE | 231df558854SHeiko Stübner RK3368_GMAC_CLK_RX_DL_CFG(rx_delay) | 232df558854SHeiko Stübner RK3368_GMAC_CLK_TX_DL_CFG(tx_delay)); 233df558854SHeiko Stübner } 234df558854SHeiko Stübner 235df558854SHeiko Stübner static void rk3368_set_to_rmii(struct rk_priv_data *bsp_priv) 236df558854SHeiko Stübner { 237df558854SHeiko Stübner struct device *dev = &bsp_priv->pdev->dev; 238df558854SHeiko Stübner 239df558854SHeiko Stübner if (IS_ERR(bsp_priv->grf)) { 240df558854SHeiko Stübner dev_err(dev, "%s: Missing rockchip,grf property\n", __func__); 241df558854SHeiko Stübner return; 242df558854SHeiko Stübner } 243df558854SHeiko Stübner 244df558854SHeiko Stübner regmap_write(bsp_priv->grf, RK3368_GRF_SOC_CON15, 245df558854SHeiko Stübner RK3368_GMAC_PHY_INTF_SEL_RMII | RK3368_GMAC_RMII_MODE); 246df558854SHeiko Stübner } 247df558854SHeiko Stübner 248df558854SHeiko Stübner static void rk3368_set_rgmii_speed(struct rk_priv_data *bsp_priv, int speed) 249df558854SHeiko Stübner { 250df558854SHeiko Stübner struct device *dev = &bsp_priv->pdev->dev; 251df558854SHeiko Stübner 252df558854SHeiko Stübner if (IS_ERR(bsp_priv->grf)) { 253df558854SHeiko Stübner dev_err(dev, "%s: Missing rockchip,grf property\n", __func__); 254df558854SHeiko Stübner return; 255df558854SHeiko Stübner } 256df558854SHeiko Stübner 257df558854SHeiko Stübner if (speed == 10) 258df558854SHeiko Stübner regmap_write(bsp_priv->grf, RK3368_GRF_SOC_CON15, 259df558854SHeiko Stübner RK3368_GMAC_CLK_2_5M); 260df558854SHeiko Stübner else if (speed == 100) 261df558854SHeiko Stübner regmap_write(bsp_priv->grf, RK3368_GRF_SOC_CON15, 262df558854SHeiko Stübner RK3368_GMAC_CLK_25M); 263df558854SHeiko Stübner else if (speed == 1000) 264df558854SHeiko Stübner regmap_write(bsp_priv->grf, RK3368_GRF_SOC_CON15, 265df558854SHeiko Stübner RK3368_GMAC_CLK_125M); 266df558854SHeiko Stübner else 267df558854SHeiko Stübner dev_err(dev, "unknown speed value for RGMII! speed=%d", speed); 268df558854SHeiko Stübner } 269df558854SHeiko Stübner 270df558854SHeiko Stübner static void rk3368_set_rmii_speed(struct rk_priv_data *bsp_priv, int speed) 271df558854SHeiko Stübner { 272df558854SHeiko Stübner struct device *dev = &bsp_priv->pdev->dev; 273df558854SHeiko Stübner 274df558854SHeiko Stübner if (IS_ERR(bsp_priv->grf)) { 275df558854SHeiko Stübner dev_err(dev, "%s: Missing rockchip,grf property\n", __func__); 276df558854SHeiko Stübner return; 277df558854SHeiko Stübner } 278df558854SHeiko Stübner 279df558854SHeiko Stübner if (speed == 10) { 280df558854SHeiko Stübner regmap_write(bsp_priv->grf, RK3368_GRF_SOC_CON15, 281df558854SHeiko Stübner RK3368_GMAC_RMII_CLK_2_5M | 282df558854SHeiko Stübner RK3368_GMAC_SPEED_10M); 283df558854SHeiko Stübner } else if (speed == 100) { 284df558854SHeiko Stübner regmap_write(bsp_priv->grf, RK3368_GRF_SOC_CON15, 285df558854SHeiko Stübner RK3368_GMAC_RMII_CLK_25M | 286df558854SHeiko Stübner RK3368_GMAC_SPEED_100M); 287df558854SHeiko Stübner } else { 288df558854SHeiko Stübner dev_err(dev, "unknown speed value for RMII! speed=%d", speed); 289df558854SHeiko Stübner } 290df558854SHeiko Stübner } 291df558854SHeiko Stübner 29292c2588fSJoachim Eastwood static const struct rk_gmac_ops rk3368_ops = { 293df558854SHeiko Stübner .set_to_rgmii = rk3368_set_to_rgmii, 294df558854SHeiko Stübner .set_to_rmii = rk3368_set_to_rmii, 295df558854SHeiko Stübner .set_rgmii_speed = rk3368_set_rgmii_speed, 296df558854SHeiko Stübner .set_rmii_speed = rk3368_set_rmii_speed, 297df558854SHeiko Stübner }; 298df558854SHeiko Stübner 2997ad269eaSRoger Chen static int gmac_clk_init(struct rk_priv_data *bsp_priv) 3007ad269eaSRoger Chen { 3017ad269eaSRoger Chen struct device *dev = &bsp_priv->pdev->dev; 3027ad269eaSRoger Chen 3037ad269eaSRoger Chen bsp_priv->clk_enabled = false; 3047ad269eaSRoger Chen 3057ad269eaSRoger Chen bsp_priv->mac_clk_rx = devm_clk_get(dev, "mac_clk_rx"); 3067ad269eaSRoger Chen if (IS_ERR(bsp_priv->mac_clk_rx)) 307d42202dcSRomain Perier dev_err(dev, "cannot get clock %s\n", 308d42202dcSRomain Perier "mac_clk_rx"); 3097ad269eaSRoger Chen 3107ad269eaSRoger Chen bsp_priv->mac_clk_tx = devm_clk_get(dev, "mac_clk_tx"); 3117ad269eaSRoger Chen if (IS_ERR(bsp_priv->mac_clk_tx)) 312d42202dcSRomain Perier dev_err(dev, "cannot get clock %s\n", 313d42202dcSRomain Perier "mac_clk_tx"); 3147ad269eaSRoger Chen 3157ad269eaSRoger Chen bsp_priv->aclk_mac = devm_clk_get(dev, "aclk_mac"); 3167ad269eaSRoger Chen if (IS_ERR(bsp_priv->aclk_mac)) 317d42202dcSRomain Perier dev_err(dev, "cannot get clock %s\n", 318d42202dcSRomain Perier "aclk_mac"); 3197ad269eaSRoger Chen 3207ad269eaSRoger Chen bsp_priv->pclk_mac = devm_clk_get(dev, "pclk_mac"); 3217ad269eaSRoger Chen if (IS_ERR(bsp_priv->pclk_mac)) 322d42202dcSRomain Perier dev_err(dev, "cannot get clock %s\n", 323d42202dcSRomain Perier "pclk_mac"); 3247ad269eaSRoger Chen 3257ad269eaSRoger Chen bsp_priv->clk_mac = devm_clk_get(dev, "stmmaceth"); 3267ad269eaSRoger Chen if (IS_ERR(bsp_priv->clk_mac)) 327d42202dcSRomain Perier dev_err(dev, "cannot get clock %s\n", 328d42202dcSRomain Perier "stmmaceth"); 3297ad269eaSRoger Chen 3307ad269eaSRoger Chen if (bsp_priv->phy_iface == PHY_INTERFACE_MODE_RMII) { 3317ad269eaSRoger Chen bsp_priv->clk_mac_ref = devm_clk_get(dev, "clk_mac_ref"); 3327ad269eaSRoger Chen if (IS_ERR(bsp_priv->clk_mac_ref)) 333d42202dcSRomain Perier dev_err(dev, "cannot get clock %s\n", 334d42202dcSRomain Perier "clk_mac_ref"); 3357ad269eaSRoger Chen 3367ad269eaSRoger Chen if (!bsp_priv->clock_input) { 3377ad269eaSRoger Chen bsp_priv->clk_mac_refout = 3387ad269eaSRoger Chen devm_clk_get(dev, "clk_mac_refout"); 3397ad269eaSRoger Chen if (IS_ERR(bsp_priv->clk_mac_refout)) 340d42202dcSRomain Perier dev_err(dev, "cannot get clock %s\n", 341d42202dcSRomain Perier "clk_mac_refout"); 3427ad269eaSRoger Chen } 3437ad269eaSRoger Chen } 3447ad269eaSRoger Chen 3457ad269eaSRoger Chen if (bsp_priv->clock_input) { 346d42202dcSRomain Perier dev_info(dev, "clock input from PHY\n"); 3477ad269eaSRoger Chen } else { 3487ad269eaSRoger Chen if (bsp_priv->phy_iface == PHY_INTERFACE_MODE_RMII) 349c48fa33cSHeiko Stübner clk_set_rate(bsp_priv->clk_mac, 50000000); 3507ad269eaSRoger Chen } 3517ad269eaSRoger Chen 3527ad269eaSRoger Chen return 0; 3537ad269eaSRoger Chen } 3547ad269eaSRoger Chen 3557ad269eaSRoger Chen static int gmac_clk_enable(struct rk_priv_data *bsp_priv, bool enable) 3567ad269eaSRoger Chen { 3577ad269eaSRoger Chen int phy_iface = phy_iface = bsp_priv->phy_iface; 3587ad269eaSRoger Chen 3597ad269eaSRoger Chen if (enable) { 3607ad269eaSRoger Chen if (!bsp_priv->clk_enabled) { 3617ad269eaSRoger Chen if (phy_iface == PHY_INTERFACE_MODE_RMII) { 3627ad269eaSRoger Chen if (!IS_ERR(bsp_priv->mac_clk_rx)) 3637ad269eaSRoger Chen clk_prepare_enable( 3647ad269eaSRoger Chen bsp_priv->mac_clk_rx); 3657ad269eaSRoger Chen 3667ad269eaSRoger Chen if (!IS_ERR(bsp_priv->clk_mac_ref)) 3677ad269eaSRoger Chen clk_prepare_enable( 3687ad269eaSRoger Chen bsp_priv->clk_mac_ref); 3697ad269eaSRoger Chen 3707ad269eaSRoger Chen if (!IS_ERR(bsp_priv->clk_mac_refout)) 3717ad269eaSRoger Chen clk_prepare_enable( 3727ad269eaSRoger Chen bsp_priv->clk_mac_refout); 3737ad269eaSRoger Chen } 3747ad269eaSRoger Chen 3757ad269eaSRoger Chen if (!IS_ERR(bsp_priv->aclk_mac)) 3767ad269eaSRoger Chen clk_prepare_enable(bsp_priv->aclk_mac); 3777ad269eaSRoger Chen 3787ad269eaSRoger Chen if (!IS_ERR(bsp_priv->pclk_mac)) 3797ad269eaSRoger Chen clk_prepare_enable(bsp_priv->pclk_mac); 3807ad269eaSRoger Chen 3817ad269eaSRoger Chen if (!IS_ERR(bsp_priv->mac_clk_tx)) 3827ad269eaSRoger Chen clk_prepare_enable(bsp_priv->mac_clk_tx); 3837ad269eaSRoger Chen 3847ad269eaSRoger Chen /** 3857ad269eaSRoger Chen * if (!IS_ERR(bsp_priv->clk_mac)) 3867ad269eaSRoger Chen * clk_prepare_enable(bsp_priv->clk_mac); 3877ad269eaSRoger Chen */ 3887ad269eaSRoger Chen mdelay(5); 3897ad269eaSRoger Chen bsp_priv->clk_enabled = true; 3907ad269eaSRoger Chen } 3917ad269eaSRoger Chen } else { 3927ad269eaSRoger Chen if (bsp_priv->clk_enabled) { 3937ad269eaSRoger Chen if (phy_iface == PHY_INTERFACE_MODE_RMII) { 3947ad269eaSRoger Chen if (!IS_ERR(bsp_priv->mac_clk_rx)) 3957ad269eaSRoger Chen clk_disable_unprepare( 3967ad269eaSRoger Chen bsp_priv->mac_clk_rx); 3977ad269eaSRoger Chen 3987ad269eaSRoger Chen if (!IS_ERR(bsp_priv->clk_mac_ref)) 3997ad269eaSRoger Chen clk_disable_unprepare( 4007ad269eaSRoger Chen bsp_priv->clk_mac_ref); 4017ad269eaSRoger Chen 4027ad269eaSRoger Chen if (!IS_ERR(bsp_priv->clk_mac_refout)) 4037ad269eaSRoger Chen clk_disable_unprepare( 4047ad269eaSRoger Chen bsp_priv->clk_mac_refout); 4057ad269eaSRoger Chen } 4067ad269eaSRoger Chen 4077ad269eaSRoger Chen if (!IS_ERR(bsp_priv->aclk_mac)) 4087ad269eaSRoger Chen clk_disable_unprepare(bsp_priv->aclk_mac); 4097ad269eaSRoger Chen 4107ad269eaSRoger Chen if (!IS_ERR(bsp_priv->pclk_mac)) 4117ad269eaSRoger Chen clk_disable_unprepare(bsp_priv->pclk_mac); 4127ad269eaSRoger Chen 4137ad269eaSRoger Chen if (!IS_ERR(bsp_priv->mac_clk_tx)) 4147ad269eaSRoger Chen clk_disable_unprepare(bsp_priv->mac_clk_tx); 4157ad269eaSRoger Chen /** 4167ad269eaSRoger Chen * if (!IS_ERR(bsp_priv->clk_mac)) 4177ad269eaSRoger Chen * clk_disable_unprepare(bsp_priv->clk_mac); 4187ad269eaSRoger Chen */ 4197ad269eaSRoger Chen bsp_priv->clk_enabled = false; 4207ad269eaSRoger Chen } 4217ad269eaSRoger Chen } 4227ad269eaSRoger Chen 4237ad269eaSRoger Chen return 0; 4247ad269eaSRoger Chen } 4257ad269eaSRoger Chen 4267ad269eaSRoger Chen static int phy_power_on(struct rk_priv_data *bsp_priv, bool enable) 4277ad269eaSRoger Chen { 4282e12f536SRomain Perier struct regulator *ldo = bsp_priv->regulator; 4297ad269eaSRoger Chen int ret; 4307ad269eaSRoger Chen struct device *dev = &bsp_priv->pdev->dev; 4317ad269eaSRoger Chen 4322e12f536SRomain Perier if (!ldo) { 433d42202dcSRomain Perier dev_err(dev, "no regulator found\n"); 4347ad269eaSRoger Chen return -1; 4357ad269eaSRoger Chen } 4367ad269eaSRoger Chen 4377ad269eaSRoger Chen if (enable) { 4387ad269eaSRoger Chen ret = regulator_enable(ldo); 4392e12f536SRomain Perier if (ret) 440d42202dcSRomain Perier dev_err(dev, "fail to enable phy-supply\n"); 4417ad269eaSRoger Chen } else { 4427ad269eaSRoger Chen ret = regulator_disable(ldo); 4432e12f536SRomain Perier if (ret) 444d42202dcSRomain Perier dev_err(dev, "fail to disable phy-supply\n"); 4457ad269eaSRoger Chen } 4467ad269eaSRoger Chen 4477ad269eaSRoger Chen return 0; 4487ad269eaSRoger Chen } 4497ad269eaSRoger Chen 4500fb98db1SHeiko Stübner static struct rk_priv_data *rk_gmac_setup(struct platform_device *pdev, 45192c2588fSJoachim Eastwood const struct rk_gmac_ops *ops) 4527ad269eaSRoger Chen { 4537ad269eaSRoger Chen struct rk_priv_data *bsp_priv; 4547ad269eaSRoger Chen struct device *dev = &pdev->dev; 4557ad269eaSRoger Chen int ret; 4567ad269eaSRoger Chen const char *strings = NULL; 4577ad269eaSRoger Chen int value; 4587ad269eaSRoger Chen 4597ad269eaSRoger Chen bsp_priv = devm_kzalloc(dev, sizeof(*bsp_priv), GFP_KERNEL); 4607ad269eaSRoger Chen if (!bsp_priv) 4617ad269eaSRoger Chen return ERR_PTR(-ENOMEM); 4627ad269eaSRoger Chen 4637ad269eaSRoger Chen bsp_priv->phy_iface = of_get_phy_mode(dev->of_node); 4640fb98db1SHeiko Stübner bsp_priv->ops = ops; 4657ad269eaSRoger Chen 4662e12f536SRomain Perier bsp_priv->regulator = devm_regulator_get_optional(dev, "phy"); 4672e12f536SRomain Perier if (IS_ERR(bsp_priv->regulator)) { 4682e12f536SRomain Perier if (PTR_ERR(bsp_priv->regulator) == -EPROBE_DEFER) { 4692e12f536SRomain Perier dev_err(dev, "phy regulator is not available yet, deferred probing\n"); 4702e12f536SRomain Perier return ERR_PTR(-EPROBE_DEFER); 4712e12f536SRomain Perier } 4722e12f536SRomain Perier dev_err(dev, "no regulator found\n"); 4732e12f536SRomain Perier bsp_priv->regulator = NULL; 4747ad269eaSRoger Chen } 4757ad269eaSRoger Chen 4767ad269eaSRoger Chen ret = of_property_read_string(dev->of_node, "clock_in_out", &strings); 4777ad269eaSRoger Chen if (ret) { 478d42202dcSRomain Perier dev_err(dev, "Can not read property: clock_in_out.\n"); 4797ad269eaSRoger Chen bsp_priv->clock_input = true; 4807ad269eaSRoger Chen } else { 481d42202dcSRomain Perier dev_info(dev, "clock input or output? (%s).\n", 482d42202dcSRomain Perier strings); 4837ad269eaSRoger Chen if (!strcmp(strings, "input")) 4847ad269eaSRoger Chen bsp_priv->clock_input = true; 4857ad269eaSRoger Chen else 4867ad269eaSRoger Chen bsp_priv->clock_input = false; 4877ad269eaSRoger Chen } 4887ad269eaSRoger Chen 4897ad269eaSRoger Chen ret = of_property_read_u32(dev->of_node, "tx_delay", &value); 4907ad269eaSRoger Chen if (ret) { 4917ad269eaSRoger Chen bsp_priv->tx_delay = 0x30; 492d42202dcSRomain Perier dev_err(dev, "Can not read property: tx_delay."); 493d42202dcSRomain Perier dev_err(dev, "set tx_delay to 0x%x\n", 494d42202dcSRomain Perier bsp_priv->tx_delay); 4957ad269eaSRoger Chen } else { 496d42202dcSRomain Perier dev_info(dev, "TX delay(0x%x).\n", value); 4977ad269eaSRoger Chen bsp_priv->tx_delay = value; 4987ad269eaSRoger Chen } 4997ad269eaSRoger Chen 5007ad269eaSRoger Chen ret = of_property_read_u32(dev->of_node, "rx_delay", &value); 5017ad269eaSRoger Chen if (ret) { 5027ad269eaSRoger Chen bsp_priv->rx_delay = 0x10; 503d42202dcSRomain Perier dev_err(dev, "Can not read property: rx_delay."); 504d42202dcSRomain Perier dev_err(dev, "set rx_delay to 0x%x\n", 505d42202dcSRomain Perier bsp_priv->rx_delay); 5067ad269eaSRoger Chen } else { 507d42202dcSRomain Perier dev_info(dev, "RX delay(0x%x).\n", value); 5087ad269eaSRoger Chen bsp_priv->rx_delay = value; 5097ad269eaSRoger Chen } 5107ad269eaSRoger Chen 5117ad269eaSRoger Chen bsp_priv->grf = syscon_regmap_lookup_by_phandle(dev->of_node, 5127ad269eaSRoger Chen "rockchip,grf"); 5137ad269eaSRoger Chen bsp_priv->pdev = pdev; 5147ad269eaSRoger Chen 5157ad269eaSRoger Chen /*rmii or rgmii*/ 5167ad269eaSRoger Chen if (bsp_priv->phy_iface == PHY_INTERFACE_MODE_RGMII) { 517d42202dcSRomain Perier dev_info(dev, "init for RGMII\n"); 5180fb98db1SHeiko Stübner bsp_priv->ops->set_to_rgmii(bsp_priv, bsp_priv->tx_delay, 5190fb98db1SHeiko Stübner bsp_priv->rx_delay); 5207ad269eaSRoger Chen } else if (bsp_priv->phy_iface == PHY_INTERFACE_MODE_RMII) { 521d42202dcSRomain Perier dev_info(dev, "init for RMII\n"); 5220fb98db1SHeiko Stübner bsp_priv->ops->set_to_rmii(bsp_priv); 5237ad269eaSRoger Chen } else { 524d42202dcSRomain Perier dev_err(dev, "NO interface defined!\n"); 5257ad269eaSRoger Chen } 5267ad269eaSRoger Chen 5277ad269eaSRoger Chen gmac_clk_init(bsp_priv); 5287ad269eaSRoger Chen 5297ad269eaSRoger Chen return bsp_priv; 5307ad269eaSRoger Chen } 5317ad269eaSRoger Chen 5327ad269eaSRoger Chen static int rk_gmac_init(struct platform_device *pdev, void *priv) 5337ad269eaSRoger Chen { 5347ad269eaSRoger Chen struct rk_priv_data *bsp_priv = priv; 5357ad269eaSRoger Chen int ret; 5367ad269eaSRoger Chen 5377ad269eaSRoger Chen ret = phy_power_on(bsp_priv, true); 5387ad269eaSRoger Chen if (ret) 5397ad269eaSRoger Chen return ret; 5407ad269eaSRoger Chen 5417ad269eaSRoger Chen ret = gmac_clk_enable(bsp_priv, true); 5427ad269eaSRoger Chen if (ret) 5437ad269eaSRoger Chen return ret; 5447ad269eaSRoger Chen 5457ad269eaSRoger Chen return 0; 5467ad269eaSRoger Chen } 5477ad269eaSRoger Chen 5487ad269eaSRoger Chen static void rk_gmac_exit(struct platform_device *pdev, void *priv) 5497ad269eaSRoger Chen { 5507ad269eaSRoger Chen struct rk_priv_data *gmac = priv; 5517ad269eaSRoger Chen 5527ad269eaSRoger Chen phy_power_on(gmac, false); 5537ad269eaSRoger Chen gmac_clk_enable(gmac, false); 5547ad269eaSRoger Chen } 5557ad269eaSRoger Chen 5567ad269eaSRoger Chen static void rk_fix_speed(void *priv, unsigned int speed) 5577ad269eaSRoger Chen { 5587ad269eaSRoger Chen struct rk_priv_data *bsp_priv = priv; 5597ad269eaSRoger Chen struct device *dev = &bsp_priv->pdev->dev; 5607ad269eaSRoger Chen 5617ad269eaSRoger Chen if (bsp_priv->phy_iface == PHY_INTERFACE_MODE_RGMII) 5620fb98db1SHeiko Stübner bsp_priv->ops->set_rgmii_speed(bsp_priv, speed); 5637ad269eaSRoger Chen else if (bsp_priv->phy_iface == PHY_INTERFACE_MODE_RMII) 5640fb98db1SHeiko Stübner bsp_priv->ops->set_rmii_speed(bsp_priv, speed); 5657ad269eaSRoger Chen else 5667ad269eaSRoger Chen dev_err(dev, "unsupported interface %d", bsp_priv->phy_iface); 5677ad269eaSRoger Chen } 5687ad269eaSRoger Chen 56927ffefd2SJoachim Eastwood static int rk_gmac_probe(struct platform_device *pdev) 57027ffefd2SJoachim Eastwood { 57127ffefd2SJoachim Eastwood struct plat_stmmacenet_data *plat_dat; 57227ffefd2SJoachim Eastwood struct stmmac_resources stmmac_res; 573*f529f182SJoachim Eastwood const struct rk_gmac_ops *data; 57427ffefd2SJoachim Eastwood int ret; 57527ffefd2SJoachim Eastwood 576149adeddSJoachim Eastwood data = of_device_get_match_data(&pdev->dev); 577149adeddSJoachim Eastwood if (!data) { 578149adeddSJoachim Eastwood dev_err(&pdev->dev, "no of match data provided\n"); 579149adeddSJoachim Eastwood return -EINVAL; 580149adeddSJoachim Eastwood } 581149adeddSJoachim Eastwood 58227ffefd2SJoachim Eastwood ret = stmmac_get_platform_resources(pdev, &stmmac_res); 58327ffefd2SJoachim Eastwood if (ret) 58427ffefd2SJoachim Eastwood return ret; 58527ffefd2SJoachim Eastwood 58627ffefd2SJoachim Eastwood plat_dat = stmmac_probe_config_dt(pdev, &stmmac_res.mac); 58727ffefd2SJoachim Eastwood if (IS_ERR(plat_dat)) 58827ffefd2SJoachim Eastwood return PTR_ERR(plat_dat); 58927ffefd2SJoachim Eastwood 59027ffefd2SJoachim Eastwood plat_dat->has_gmac = true; 59127ffefd2SJoachim Eastwood plat_dat->init = rk_gmac_init; 59227ffefd2SJoachim Eastwood plat_dat->exit = rk_gmac_exit; 59327ffefd2SJoachim Eastwood plat_dat->fix_mac_speed = rk_fix_speed; 59427ffefd2SJoachim Eastwood 595*f529f182SJoachim Eastwood plat_dat->bsp_priv = rk_gmac_setup(pdev, data); 59627ffefd2SJoachim Eastwood if (IS_ERR(plat_dat->bsp_priv)) 59727ffefd2SJoachim Eastwood return PTR_ERR(plat_dat->bsp_priv); 59827ffefd2SJoachim Eastwood 59927ffefd2SJoachim Eastwood ret = rk_gmac_init(pdev, plat_dat->bsp_priv); 60027ffefd2SJoachim Eastwood if (ret) 60127ffefd2SJoachim Eastwood return ret; 60227ffefd2SJoachim Eastwood 60327ffefd2SJoachim Eastwood return stmmac_dvr_probe(&pdev->dev, plat_dat, &stmmac_res); 60427ffefd2SJoachim Eastwood } 60527ffefd2SJoachim Eastwood 606e0fb4013SJoachim Eastwood static const struct of_device_id rk_gmac_dwmac_match[] = { 607*f529f182SJoachim Eastwood { .compatible = "rockchip,rk3288-gmac", .data = &rk3288_ops }, 608*f529f182SJoachim Eastwood { .compatible = "rockchip,rk3368-gmac", .data = &rk3368_ops }, 609e0fb4013SJoachim Eastwood { } 610e0fb4013SJoachim Eastwood }; 611e0fb4013SJoachim Eastwood MODULE_DEVICE_TABLE(of, rk_gmac_dwmac_match); 612e0fb4013SJoachim Eastwood 613e0fb4013SJoachim Eastwood static struct platform_driver rk_gmac_dwmac_driver = { 61427ffefd2SJoachim Eastwood .probe = rk_gmac_probe, 615e0fb4013SJoachim Eastwood .remove = stmmac_pltfr_remove, 616e0fb4013SJoachim Eastwood .driver = { 617e0fb4013SJoachim Eastwood .name = "rk_gmac-dwmac", 618e0fb4013SJoachim Eastwood .pm = &stmmac_pltfr_pm_ops, 619e0fb4013SJoachim Eastwood .of_match_table = rk_gmac_dwmac_match, 620e0fb4013SJoachim Eastwood }, 621e0fb4013SJoachim Eastwood }; 622e0fb4013SJoachim Eastwood module_platform_driver(rk_gmac_dwmac_driver); 623e0fb4013SJoachim Eastwood 624e0fb4013SJoachim Eastwood MODULE_AUTHOR("Chen-Zhi (Roger Chen) <roger.chen@rock-chips.com>"); 625e0fb4013SJoachim Eastwood MODULE_DESCRIPTION("Rockchip RK3288 DWMAC specific glue layer"); 626e0fb4013SJoachim Eastwood MODULE_LICENSE("GPL"); 627