1a7c30e62SVinod Koul // SPDX-License-Identifier: GPL-2.0 2a7c30e62SVinod Koul // Copyright (c) 2018-19, Linaro Limited 3a7c30e62SVinod Koul 4a7c30e62SVinod Koul #include <linux/module.h> 5a7c30e62SVinod Koul #include <linux/of.h> 6a7c30e62SVinod Koul #include <linux/of_device.h> 7a7c30e62SVinod Koul #include <linux/platform_device.h> 8a7c30e62SVinod Koul #include <linux/phy.h> 9a7c30e62SVinod Koul #include "stmmac.h" 10a7c30e62SVinod Koul #include "stmmac_platform.h" 11a7c30e62SVinod Koul 12a7c30e62SVinod Koul #define RGMII_IO_MACRO_CONFIG 0x0 13a7c30e62SVinod Koul #define SDCC_HC_REG_DLL_CONFIG 0x4 14a7c30e62SVinod Koul #define SDCC_HC_REG_DDR_CONFIG 0xC 15a7c30e62SVinod Koul #define SDCC_HC_REG_DLL_CONFIG2 0x10 16a7c30e62SVinod Koul #define SDC4_STATUS 0x14 17a7c30e62SVinod Koul #define SDCC_USR_CTL 0x18 18a7c30e62SVinod Koul #define RGMII_IO_MACRO_CONFIG2 0x1C 19a7c30e62SVinod Koul #define RGMII_IO_MACRO_DEBUG1 0x20 20a7c30e62SVinod Koul #define EMAC_SYSTEM_LOW_POWER_DEBUG 0x28 21a7c30e62SVinod Koul 22a7c30e62SVinod Koul /* RGMII_IO_MACRO_CONFIG fields */ 23a7c30e62SVinod Koul #define RGMII_CONFIG_FUNC_CLK_EN BIT(30) 24a7c30e62SVinod Koul #define RGMII_CONFIG_POS_NEG_DATA_SEL BIT(23) 25a7c30e62SVinod Koul #define RGMII_CONFIG_GPIO_CFG_RX_INT GENMASK(21, 20) 26a7c30e62SVinod Koul #define RGMII_CONFIG_GPIO_CFG_TX_INT GENMASK(19, 17) 27a7c30e62SVinod Koul #define RGMII_CONFIG_MAX_SPD_PRG_9 GENMASK(16, 8) 28a7c30e62SVinod Koul #define RGMII_CONFIG_MAX_SPD_PRG_2 GENMASK(7, 6) 29a7c30e62SVinod Koul #define RGMII_CONFIG_INTF_SEL GENMASK(5, 4) 30a7c30e62SVinod Koul #define RGMII_CONFIG_BYPASS_TX_ID_EN BIT(3) 31a7c30e62SVinod Koul #define RGMII_CONFIG_LOOPBACK_EN BIT(2) 32a7c30e62SVinod Koul #define RGMII_CONFIG_PROG_SWAP BIT(1) 33a7c30e62SVinod Koul #define RGMII_CONFIG_DDR_MODE BIT(0) 34a7c30e62SVinod Koul 35a7c30e62SVinod Koul /* SDCC_HC_REG_DLL_CONFIG fields */ 36a7c30e62SVinod Koul #define SDCC_DLL_CONFIG_DLL_RST BIT(30) 37a7c30e62SVinod Koul #define SDCC_DLL_CONFIG_PDN BIT(29) 38a7c30e62SVinod Koul #define SDCC_DLL_CONFIG_MCLK_FREQ GENMASK(26, 24) 39a7c30e62SVinod Koul #define SDCC_DLL_CONFIG_CDR_SELEXT GENMASK(23, 20) 40a7c30e62SVinod Koul #define SDCC_DLL_CONFIG_CDR_EXT_EN BIT(19) 41a7c30e62SVinod Koul #define SDCC_DLL_CONFIG_CK_OUT_EN BIT(18) 42a7c30e62SVinod Koul #define SDCC_DLL_CONFIG_CDR_EN BIT(17) 43a7c30e62SVinod Koul #define SDCC_DLL_CONFIG_DLL_EN BIT(16) 44a7c30e62SVinod Koul #define SDCC_DLL_MCLK_GATING_EN BIT(5) 45a7c30e62SVinod Koul #define SDCC_DLL_CDR_FINE_PHASE GENMASK(3, 2) 46a7c30e62SVinod Koul 47a7c30e62SVinod Koul /* SDCC_HC_REG_DDR_CONFIG fields */ 48a7c30e62SVinod Koul #define SDCC_DDR_CONFIG_PRG_DLY_EN BIT(31) 49a7c30e62SVinod Koul #define SDCC_DDR_CONFIG_EXT_PRG_RCLK_DLY GENMASK(26, 21) 50a7c30e62SVinod Koul #define SDCC_DDR_CONFIG_EXT_PRG_RCLK_DLY_CODE GENMASK(29, 27) 51a7c30e62SVinod Koul #define SDCC_DDR_CONFIG_EXT_PRG_RCLK_DLY_EN BIT(30) 52a7c30e62SVinod Koul #define SDCC_DDR_CONFIG_PRG_RCLK_DLY GENMASK(8, 0) 53a7c30e62SVinod Koul 54a7c30e62SVinod Koul /* SDCC_HC_REG_DLL_CONFIG2 fields */ 55a7c30e62SVinod Koul #define SDCC_DLL_CONFIG2_DLL_CLOCK_DIS BIT(21) 56a7c30e62SVinod Koul #define SDCC_DLL_CONFIG2_MCLK_FREQ_CALC GENMASK(17, 10) 57a7c30e62SVinod Koul #define SDCC_DLL_CONFIG2_DDR_TRAFFIC_INIT_SEL GENMASK(3, 2) 58a7c30e62SVinod Koul #define SDCC_DLL_CONFIG2_DDR_TRAFFIC_INIT_SW BIT(1) 59a7c30e62SVinod Koul #define SDCC_DLL_CONFIG2_DDR_CAL_EN BIT(0) 60a7c30e62SVinod Koul 61a7c30e62SVinod Koul /* SDC4_STATUS bits */ 62a7c30e62SVinod Koul #define SDC4_STATUS_DLL_LOCK BIT(7) 63a7c30e62SVinod Koul 64a7c30e62SVinod Koul /* RGMII_IO_MACRO_CONFIG2 fields */ 65a7c30e62SVinod Koul #define RGMII_CONFIG2_RSVD_CONFIG15 GENMASK(31, 17) 66a7c30e62SVinod Koul #define RGMII_CONFIG2_RGMII_CLK_SEL_CFG BIT(16) 67a7c30e62SVinod Koul #define RGMII_CONFIG2_TX_TO_RX_LOOPBACK_EN BIT(13) 68a7c30e62SVinod Koul #define RGMII_CONFIG2_CLK_DIVIDE_SEL BIT(12) 69a7c30e62SVinod Koul #define RGMII_CONFIG2_RX_PROG_SWAP BIT(7) 70a7c30e62SVinod Koul #define RGMII_CONFIG2_DATA_DIVIDE_CLK_SEL BIT(6) 71a7c30e62SVinod Koul #define RGMII_CONFIG2_TX_CLK_PHASE_SHIFT_EN BIT(5) 72a7c30e62SVinod Koul 73a7c30e62SVinod Koul struct ethqos_emac_por { 74a7c30e62SVinod Koul unsigned int offset; 75a7c30e62SVinod Koul unsigned int value; 76a7c30e62SVinod Koul }; 77a7c30e62SVinod Koul 78a7c30e62SVinod Koul struct qcom_ethqos { 79a7c30e62SVinod Koul struct platform_device *pdev; 80a7c30e62SVinod Koul void __iomem *rgmii_base; 81a7c30e62SVinod Koul 82a7c30e62SVinod Koul unsigned int rgmii_clk_rate; 83a7c30e62SVinod Koul struct clk *rgmii_clk; 84a7c30e62SVinod Koul unsigned int speed; 85a7c30e62SVinod Koul 86a7c30e62SVinod Koul const struct ethqos_emac_por *por; 87a7c30e62SVinod Koul unsigned int num_por; 88a7c30e62SVinod Koul }; 89a7c30e62SVinod Koul 90a7c30e62SVinod Koul static int rgmii_readl(struct qcom_ethqos *ethqos, unsigned int offset) 91a7c30e62SVinod Koul { 92a7c30e62SVinod Koul return readl(ethqos->rgmii_base + offset); 93a7c30e62SVinod Koul } 94a7c30e62SVinod Koul 95a7c30e62SVinod Koul static void rgmii_writel(struct qcom_ethqos *ethqos, 96a7c30e62SVinod Koul int value, unsigned int offset) 97a7c30e62SVinod Koul { 98a7c30e62SVinod Koul writel(value, ethqos->rgmii_base + offset); 99a7c30e62SVinod Koul } 100a7c30e62SVinod Koul 101a7c30e62SVinod Koul static void rgmii_updatel(struct qcom_ethqos *ethqos, 102a7c30e62SVinod Koul int mask, int val, unsigned int offset) 103a7c30e62SVinod Koul { 104a7c30e62SVinod Koul unsigned int temp; 105a7c30e62SVinod Koul 106a7c30e62SVinod Koul temp = rgmii_readl(ethqos, offset); 107a7c30e62SVinod Koul temp = (temp & ~(mask)) | val; 108a7c30e62SVinod Koul rgmii_writel(ethqos, temp, offset); 109a7c30e62SVinod Koul } 110a7c30e62SVinod Koul 111a7c30e62SVinod Koul static void rgmii_dump(struct qcom_ethqos *ethqos) 112a7c30e62SVinod Koul { 113a7c30e62SVinod Koul dev_dbg(ðqos->pdev->dev, "Rgmii register dump\n"); 114a7c30e62SVinod Koul dev_dbg(ðqos->pdev->dev, "RGMII_IO_MACRO_CONFIG: %x\n", 115a7c30e62SVinod Koul rgmii_readl(ethqos, RGMII_IO_MACRO_CONFIG)); 116a7c30e62SVinod Koul dev_dbg(ðqos->pdev->dev, "SDCC_HC_REG_DLL_CONFIG: %x\n", 117a7c30e62SVinod Koul rgmii_readl(ethqos, SDCC_HC_REG_DLL_CONFIG)); 118a7c30e62SVinod Koul dev_dbg(ðqos->pdev->dev, "SDCC_HC_REG_DDR_CONFIG: %x\n", 119a7c30e62SVinod Koul rgmii_readl(ethqos, SDCC_HC_REG_DDR_CONFIG)); 120a7c30e62SVinod Koul dev_dbg(ðqos->pdev->dev, "SDCC_HC_REG_DLL_CONFIG2: %x\n", 121a7c30e62SVinod Koul rgmii_readl(ethqos, SDCC_HC_REG_DLL_CONFIG2)); 122a7c30e62SVinod Koul dev_dbg(ðqos->pdev->dev, "SDC4_STATUS: %x\n", 123a7c30e62SVinod Koul rgmii_readl(ethqos, SDC4_STATUS)); 124a7c30e62SVinod Koul dev_dbg(ðqos->pdev->dev, "SDCC_USR_CTL: %x\n", 125a7c30e62SVinod Koul rgmii_readl(ethqos, SDCC_USR_CTL)); 126a7c30e62SVinod Koul dev_dbg(ðqos->pdev->dev, "RGMII_IO_MACRO_CONFIG2: %x\n", 127a7c30e62SVinod Koul rgmii_readl(ethqos, RGMII_IO_MACRO_CONFIG2)); 128a7c30e62SVinod Koul dev_dbg(ðqos->pdev->dev, "RGMII_IO_MACRO_DEBUG1: %x\n", 129a7c30e62SVinod Koul rgmii_readl(ethqos, RGMII_IO_MACRO_DEBUG1)); 130a7c30e62SVinod Koul dev_dbg(ðqos->pdev->dev, "EMAC_SYSTEM_LOW_POWER_DEBUG: %x\n", 131a7c30e62SVinod Koul rgmii_readl(ethqos, EMAC_SYSTEM_LOW_POWER_DEBUG)); 132a7c30e62SVinod Koul } 133a7c30e62SVinod Koul 134a7c30e62SVinod Koul /* Clock rates */ 135a7c30e62SVinod Koul #define RGMII_1000_NOM_CLK_FREQ (250 * 1000 * 1000UL) 136a7c30e62SVinod Koul #define RGMII_ID_MODE_100_LOW_SVS_CLK_FREQ (50 * 1000 * 1000UL) 137a7c30e62SVinod Koul #define RGMII_ID_MODE_10_LOW_SVS_CLK_FREQ (5 * 1000 * 1000UL) 138a7c30e62SVinod Koul 139a7c30e62SVinod Koul static void 140a7c30e62SVinod Koul ethqos_update_rgmii_clk(struct qcom_ethqos *ethqos, unsigned int speed) 141a7c30e62SVinod Koul { 142a7c30e62SVinod Koul switch (speed) { 143a7c30e62SVinod Koul case SPEED_1000: 144a7c30e62SVinod Koul ethqos->rgmii_clk_rate = RGMII_1000_NOM_CLK_FREQ; 145a7c30e62SVinod Koul break; 146a7c30e62SVinod Koul 147a7c30e62SVinod Koul case SPEED_100: 148a7c30e62SVinod Koul ethqos->rgmii_clk_rate = RGMII_ID_MODE_100_LOW_SVS_CLK_FREQ; 149a7c30e62SVinod Koul break; 150a7c30e62SVinod Koul 151a7c30e62SVinod Koul case SPEED_10: 152a7c30e62SVinod Koul ethqos->rgmii_clk_rate = RGMII_ID_MODE_10_LOW_SVS_CLK_FREQ; 153a7c30e62SVinod Koul break; 154a7c30e62SVinod Koul } 155a7c30e62SVinod Koul 156a7c30e62SVinod Koul clk_set_rate(ethqos->rgmii_clk, ethqos->rgmii_clk_rate); 157a7c30e62SVinod Koul } 158a7c30e62SVinod Koul 159a7c30e62SVinod Koul static void ethqos_set_func_clk_en(struct qcom_ethqos *ethqos) 160a7c30e62SVinod Koul { 161a7c30e62SVinod Koul rgmii_updatel(ethqos, RGMII_CONFIG_FUNC_CLK_EN, 162a7c30e62SVinod Koul RGMII_CONFIG_FUNC_CLK_EN, RGMII_IO_MACRO_CONFIG); 163a7c30e62SVinod Koul } 164a7c30e62SVinod Koul 165a7c30e62SVinod Koul static const struct ethqos_emac_por emac_v2_3_0_por[] = { 166a7c30e62SVinod Koul { .offset = RGMII_IO_MACRO_CONFIG, .value = 0x00C01343 }, 167a7c30e62SVinod Koul { .offset = SDCC_HC_REG_DLL_CONFIG, .value = 0x2004642C }, 168a7c30e62SVinod Koul { .offset = SDCC_HC_REG_DDR_CONFIG, .value = 0x00000000 }, 169a7c30e62SVinod Koul { .offset = SDCC_HC_REG_DLL_CONFIG2, .value = 0x00200000 }, 170a7c30e62SVinod Koul { .offset = SDCC_USR_CTL, .value = 0x00010800 }, 171a7c30e62SVinod Koul { .offset = RGMII_IO_MACRO_CONFIG2, .value = 0x00002060 }, 172a7c30e62SVinod Koul }; 173a7c30e62SVinod Koul 174a7c30e62SVinod Koul static int ethqos_dll_configure(struct qcom_ethqos *ethqos) 175a7c30e62SVinod Koul { 176a7c30e62SVinod Koul unsigned int val; 177a7c30e62SVinod Koul int retry = 1000; 178a7c30e62SVinod Koul 179a7c30e62SVinod Koul /* Set CDR_EN */ 180a7c30e62SVinod Koul rgmii_updatel(ethqos, SDCC_DLL_CONFIG_CDR_EN, 181a7c30e62SVinod Koul SDCC_DLL_CONFIG_CDR_EN, SDCC_HC_REG_DLL_CONFIG); 182a7c30e62SVinod Koul 183a7c30e62SVinod Koul /* Set CDR_EXT_EN */ 184a7c30e62SVinod Koul rgmii_updatel(ethqos, SDCC_DLL_CONFIG_CDR_EXT_EN, 185a7c30e62SVinod Koul SDCC_DLL_CONFIG_CDR_EXT_EN, SDCC_HC_REG_DLL_CONFIG); 186a7c30e62SVinod Koul 187a7c30e62SVinod Koul /* Clear CK_OUT_EN */ 188a7c30e62SVinod Koul rgmii_updatel(ethqos, SDCC_DLL_CONFIG_CK_OUT_EN, 189a7c30e62SVinod Koul 0, SDCC_HC_REG_DLL_CONFIG); 190a7c30e62SVinod Koul 191a7c30e62SVinod Koul /* Set DLL_EN */ 192a7c30e62SVinod Koul rgmii_updatel(ethqos, SDCC_DLL_CONFIG_DLL_EN, 193a7c30e62SVinod Koul SDCC_DLL_CONFIG_DLL_EN, SDCC_HC_REG_DLL_CONFIG); 194a7c30e62SVinod Koul 195a7c30e62SVinod Koul rgmii_updatel(ethqos, SDCC_DLL_MCLK_GATING_EN, 196a7c30e62SVinod Koul 0, SDCC_HC_REG_DLL_CONFIG); 197a7c30e62SVinod Koul 198a7c30e62SVinod Koul rgmii_updatel(ethqos, SDCC_DLL_CDR_FINE_PHASE, 199a7c30e62SVinod Koul 0, SDCC_HC_REG_DLL_CONFIG); 200a7c30e62SVinod Koul 201a7c30e62SVinod Koul /* Wait for CK_OUT_EN clear */ 202a7c30e62SVinod Koul do { 203a7c30e62SVinod Koul val = rgmii_readl(ethqos, SDCC_HC_REG_DLL_CONFIG); 204a7c30e62SVinod Koul val &= SDCC_DLL_CONFIG_CK_OUT_EN; 205a7c30e62SVinod Koul if (!val) 206a7c30e62SVinod Koul break; 207a7c30e62SVinod Koul mdelay(1); 208a7c30e62SVinod Koul retry--; 209a7c30e62SVinod Koul } while (retry > 0); 210a7c30e62SVinod Koul if (!retry) 211a7c30e62SVinod Koul dev_err(ðqos->pdev->dev, "Clear CK_OUT_EN timedout\n"); 212a7c30e62SVinod Koul 213a7c30e62SVinod Koul /* Set CK_OUT_EN */ 214a7c30e62SVinod Koul rgmii_updatel(ethqos, SDCC_DLL_CONFIG_CK_OUT_EN, 215a7c30e62SVinod Koul SDCC_DLL_CONFIG_CK_OUT_EN, SDCC_HC_REG_DLL_CONFIG); 216a7c30e62SVinod Koul 217a7c30e62SVinod Koul /* Wait for CK_OUT_EN set */ 218a7c30e62SVinod Koul retry = 1000; 219a7c30e62SVinod Koul do { 220a7c30e62SVinod Koul val = rgmii_readl(ethqos, SDCC_HC_REG_DLL_CONFIG); 221a7c30e62SVinod Koul val &= SDCC_DLL_CONFIG_CK_OUT_EN; 222a7c30e62SVinod Koul if (val) 223a7c30e62SVinod Koul break; 224a7c30e62SVinod Koul mdelay(1); 225a7c30e62SVinod Koul retry--; 226a7c30e62SVinod Koul } while (retry > 0); 227a7c30e62SVinod Koul if (!retry) 228a7c30e62SVinod Koul dev_err(ðqos->pdev->dev, "Set CK_OUT_EN timedout\n"); 229a7c30e62SVinod Koul 230a7c30e62SVinod Koul /* Set DDR_CAL_EN */ 231a7c30e62SVinod Koul rgmii_updatel(ethqos, SDCC_DLL_CONFIG2_DDR_CAL_EN, 232a7c30e62SVinod Koul SDCC_DLL_CONFIG2_DDR_CAL_EN, SDCC_HC_REG_DLL_CONFIG2); 233a7c30e62SVinod Koul 234a7c30e62SVinod Koul rgmii_updatel(ethqos, SDCC_DLL_CONFIG2_DLL_CLOCK_DIS, 235a7c30e62SVinod Koul 0, SDCC_HC_REG_DLL_CONFIG2); 236a7c30e62SVinod Koul 237a7c30e62SVinod Koul rgmii_updatel(ethqos, SDCC_DLL_CONFIG2_MCLK_FREQ_CALC, 238a7c30e62SVinod Koul 0x1A << 10, SDCC_HC_REG_DLL_CONFIG2); 239a7c30e62SVinod Koul 240a7c30e62SVinod Koul rgmii_updatel(ethqos, SDCC_DLL_CONFIG2_DDR_TRAFFIC_INIT_SEL, 241a7c30e62SVinod Koul BIT(2), SDCC_HC_REG_DLL_CONFIG2); 242a7c30e62SVinod Koul 243a7c30e62SVinod Koul rgmii_updatel(ethqos, SDCC_DLL_CONFIG2_DDR_TRAFFIC_INIT_SW, 244a7c30e62SVinod Koul SDCC_DLL_CONFIG2_DDR_TRAFFIC_INIT_SW, 245a7c30e62SVinod Koul SDCC_HC_REG_DLL_CONFIG2); 246a7c30e62SVinod Koul 247a7c30e62SVinod Koul return 0; 248a7c30e62SVinod Koul } 249a7c30e62SVinod Koul 250a7c30e62SVinod Koul static int ethqos_rgmii_macro_init(struct qcom_ethqos *ethqos) 251a7c30e62SVinod Koul { 252a7c30e62SVinod Koul /* Disable loopback mode */ 253a7c30e62SVinod Koul rgmii_updatel(ethqos, RGMII_CONFIG2_TX_TO_RX_LOOPBACK_EN, 254a7c30e62SVinod Koul 0, RGMII_IO_MACRO_CONFIG2); 255a7c30e62SVinod Koul 256a7c30e62SVinod Koul /* Select RGMII, write 0 to interface select */ 257a7c30e62SVinod Koul rgmii_updatel(ethqos, RGMII_CONFIG_INTF_SEL, 258a7c30e62SVinod Koul 0, RGMII_IO_MACRO_CONFIG); 259a7c30e62SVinod Koul 260a7c30e62SVinod Koul switch (ethqos->speed) { 261a7c30e62SVinod Koul case SPEED_1000: 262a7c30e62SVinod Koul rgmii_updatel(ethqos, RGMII_CONFIG_DDR_MODE, 263a7c30e62SVinod Koul RGMII_CONFIG_DDR_MODE, RGMII_IO_MACRO_CONFIG); 264a7c30e62SVinod Koul rgmii_updatel(ethqos, RGMII_CONFIG_BYPASS_TX_ID_EN, 265a7c30e62SVinod Koul 0, RGMII_IO_MACRO_CONFIG); 266a7c30e62SVinod Koul rgmii_updatel(ethqos, RGMII_CONFIG_POS_NEG_DATA_SEL, 267a7c30e62SVinod Koul RGMII_CONFIG_POS_NEG_DATA_SEL, 268a7c30e62SVinod Koul RGMII_IO_MACRO_CONFIG); 269a7c30e62SVinod Koul rgmii_updatel(ethqos, RGMII_CONFIG_PROG_SWAP, 270a7c30e62SVinod Koul RGMII_CONFIG_PROG_SWAP, RGMII_IO_MACRO_CONFIG); 271a7c30e62SVinod Koul rgmii_updatel(ethqos, RGMII_CONFIG2_DATA_DIVIDE_CLK_SEL, 272a7c30e62SVinod Koul 0, RGMII_IO_MACRO_CONFIG2); 273a7c30e62SVinod Koul rgmii_updatel(ethqos, RGMII_CONFIG2_TX_CLK_PHASE_SHIFT_EN, 274a7c30e62SVinod Koul RGMII_CONFIG2_TX_CLK_PHASE_SHIFT_EN, 275a7c30e62SVinod Koul RGMII_IO_MACRO_CONFIG2); 276a7c30e62SVinod Koul rgmii_updatel(ethqos, RGMII_CONFIG2_RSVD_CONFIG15, 277a7c30e62SVinod Koul 0, RGMII_IO_MACRO_CONFIG2); 278a7c30e62SVinod Koul rgmii_updatel(ethqos, RGMII_CONFIG2_RX_PROG_SWAP, 279a7c30e62SVinod Koul RGMII_CONFIG2_RX_PROG_SWAP, 280a7c30e62SVinod Koul RGMII_IO_MACRO_CONFIG2); 281a7c30e62SVinod Koul 282a7c30e62SVinod Koul /* Set PRG_RCLK_DLY to 57 for 1.8 ns delay */ 283a7c30e62SVinod Koul rgmii_updatel(ethqos, SDCC_DDR_CONFIG_PRG_RCLK_DLY, 284a7c30e62SVinod Koul 57, SDCC_HC_REG_DDR_CONFIG); 285a7c30e62SVinod Koul rgmii_updatel(ethqos, SDCC_DDR_CONFIG_PRG_DLY_EN, 286a7c30e62SVinod Koul SDCC_DDR_CONFIG_PRG_DLY_EN, 287a7c30e62SVinod Koul SDCC_HC_REG_DDR_CONFIG); 288a7c30e62SVinod Koul rgmii_updatel(ethqos, RGMII_CONFIG_LOOPBACK_EN, 289a7c30e62SVinod Koul RGMII_CONFIG_LOOPBACK_EN, RGMII_IO_MACRO_CONFIG); 290a7c30e62SVinod Koul break; 291a7c30e62SVinod Koul 292a7c30e62SVinod Koul case SPEED_100: 293a7c30e62SVinod Koul rgmii_updatel(ethqos, RGMII_CONFIG_DDR_MODE, 294a7c30e62SVinod Koul RGMII_CONFIG_DDR_MODE, RGMII_IO_MACRO_CONFIG); 295a7c30e62SVinod Koul rgmii_updatel(ethqos, RGMII_CONFIG_BYPASS_TX_ID_EN, 296a7c30e62SVinod Koul RGMII_CONFIG_BYPASS_TX_ID_EN, 297a7c30e62SVinod Koul RGMII_IO_MACRO_CONFIG); 298a7c30e62SVinod Koul rgmii_updatel(ethqos, RGMII_CONFIG_POS_NEG_DATA_SEL, 299a7c30e62SVinod Koul 0, RGMII_IO_MACRO_CONFIG); 300a7c30e62SVinod Koul rgmii_updatel(ethqos, RGMII_CONFIG_PROG_SWAP, 301a7c30e62SVinod Koul 0, RGMII_IO_MACRO_CONFIG); 302a7c30e62SVinod Koul rgmii_updatel(ethqos, RGMII_CONFIG2_DATA_DIVIDE_CLK_SEL, 303a7c30e62SVinod Koul 0, RGMII_IO_MACRO_CONFIG2); 304a7c30e62SVinod Koul rgmii_updatel(ethqos, RGMII_CONFIG2_TX_CLK_PHASE_SHIFT_EN, 305a7c30e62SVinod Koul RGMII_CONFIG2_TX_CLK_PHASE_SHIFT_EN, 306a7c30e62SVinod Koul RGMII_IO_MACRO_CONFIG2); 307a7c30e62SVinod Koul rgmii_updatel(ethqos, RGMII_CONFIG_MAX_SPD_PRG_2, 308a7c30e62SVinod Koul BIT(6), RGMII_IO_MACRO_CONFIG); 309a7c30e62SVinod Koul rgmii_updatel(ethqos, RGMII_CONFIG2_RSVD_CONFIG15, 310a7c30e62SVinod Koul 0, RGMII_IO_MACRO_CONFIG2); 311a7c30e62SVinod Koul rgmii_updatel(ethqos, RGMII_CONFIG2_RX_PROG_SWAP, 312a7c30e62SVinod Koul 0, RGMII_IO_MACRO_CONFIG2); 313a7c30e62SVinod Koul /* Write 0x5 to PRG_RCLK_DLY_CODE */ 314a7c30e62SVinod Koul rgmii_updatel(ethqos, SDCC_DDR_CONFIG_EXT_PRG_RCLK_DLY_CODE, 315a7c30e62SVinod Koul (BIT(29) | BIT(27)), SDCC_HC_REG_DDR_CONFIG); 316a7c30e62SVinod Koul rgmii_updatel(ethqos, SDCC_DDR_CONFIG_EXT_PRG_RCLK_DLY, 317a7c30e62SVinod Koul SDCC_DDR_CONFIG_EXT_PRG_RCLK_DLY, 318a7c30e62SVinod Koul SDCC_HC_REG_DDR_CONFIG); 319a7c30e62SVinod Koul rgmii_updatel(ethqos, SDCC_DDR_CONFIG_EXT_PRG_RCLK_DLY_EN, 320a7c30e62SVinod Koul SDCC_DDR_CONFIG_EXT_PRG_RCLK_DLY_EN, 321a7c30e62SVinod Koul SDCC_HC_REG_DDR_CONFIG); 322a7c30e62SVinod Koul rgmii_updatel(ethqos, RGMII_CONFIG_LOOPBACK_EN, 323a7c30e62SVinod Koul RGMII_CONFIG_LOOPBACK_EN, RGMII_IO_MACRO_CONFIG); 324a7c30e62SVinod Koul break; 325a7c30e62SVinod Koul 326a7c30e62SVinod Koul case SPEED_10: 327a7c30e62SVinod Koul rgmii_updatel(ethqos, RGMII_CONFIG_DDR_MODE, 328a7c30e62SVinod Koul RGMII_CONFIG_DDR_MODE, RGMII_IO_MACRO_CONFIG); 329a7c30e62SVinod Koul rgmii_updatel(ethqos, RGMII_CONFIG_BYPASS_TX_ID_EN, 330a7c30e62SVinod Koul RGMII_CONFIG_BYPASS_TX_ID_EN, 331a7c30e62SVinod Koul RGMII_IO_MACRO_CONFIG); 332a7c30e62SVinod Koul rgmii_updatel(ethqos, RGMII_CONFIG_POS_NEG_DATA_SEL, 333a7c30e62SVinod Koul 0, RGMII_IO_MACRO_CONFIG); 334a7c30e62SVinod Koul rgmii_updatel(ethqos, RGMII_CONFIG_PROG_SWAP, 335a7c30e62SVinod Koul 0, RGMII_IO_MACRO_CONFIG); 336a7c30e62SVinod Koul rgmii_updatel(ethqos, RGMII_CONFIG2_DATA_DIVIDE_CLK_SEL, 337a7c30e62SVinod Koul 0, RGMII_IO_MACRO_CONFIG2); 338a7c30e62SVinod Koul rgmii_updatel(ethqos, RGMII_CONFIG2_TX_CLK_PHASE_SHIFT_EN, 339a7c30e62SVinod Koul 0, RGMII_IO_MACRO_CONFIG2); 340a7c30e62SVinod Koul rgmii_updatel(ethqos, RGMII_CONFIG_MAX_SPD_PRG_9, 341a7c30e62SVinod Koul BIT(12) | GENMASK(9, 8), 342a7c30e62SVinod Koul RGMII_IO_MACRO_CONFIG); 343a7c30e62SVinod Koul rgmii_updatel(ethqos, RGMII_CONFIG2_RSVD_CONFIG15, 344a7c30e62SVinod Koul 0, RGMII_IO_MACRO_CONFIG2); 345a7c30e62SVinod Koul rgmii_updatel(ethqos, RGMII_CONFIG2_RX_PROG_SWAP, 346a7c30e62SVinod Koul 0, RGMII_IO_MACRO_CONFIG2); 347a7c30e62SVinod Koul /* Write 0x5 to PRG_RCLK_DLY_CODE */ 348a7c30e62SVinod Koul rgmii_updatel(ethqos, SDCC_DDR_CONFIG_EXT_PRG_RCLK_DLY_CODE, 349a7c30e62SVinod Koul (BIT(29) | BIT(27)), SDCC_HC_REG_DDR_CONFIG); 350a7c30e62SVinod Koul rgmii_updatel(ethqos, SDCC_DDR_CONFIG_EXT_PRG_RCLK_DLY, 351a7c30e62SVinod Koul SDCC_DDR_CONFIG_EXT_PRG_RCLK_DLY, 352a7c30e62SVinod Koul SDCC_HC_REG_DDR_CONFIG); 353a7c30e62SVinod Koul rgmii_updatel(ethqos, SDCC_DDR_CONFIG_EXT_PRG_RCLK_DLY_EN, 354a7c30e62SVinod Koul SDCC_DDR_CONFIG_EXT_PRG_RCLK_DLY_EN, 355a7c30e62SVinod Koul SDCC_HC_REG_DDR_CONFIG); 356a7c30e62SVinod Koul rgmii_updatel(ethqos, RGMII_CONFIG_LOOPBACK_EN, 357a7c30e62SVinod Koul RGMII_CONFIG_LOOPBACK_EN, RGMII_IO_MACRO_CONFIG); 358a7c30e62SVinod Koul break; 359a7c30e62SVinod Koul default: 360a7c30e62SVinod Koul dev_err(ðqos->pdev->dev, 361a7c30e62SVinod Koul "Invalid speed %d\n", ethqos->speed); 362a7c30e62SVinod Koul return -EINVAL; 363a7c30e62SVinod Koul } 364a7c30e62SVinod Koul 365a7c30e62SVinod Koul return 0; 366a7c30e62SVinod Koul } 367a7c30e62SVinod Koul 368a7c30e62SVinod Koul static int ethqos_configure(struct qcom_ethqos *ethqos) 369a7c30e62SVinod Koul { 370a7c30e62SVinod Koul volatile unsigned int dll_lock; 371a7c30e62SVinod Koul unsigned int i, retry = 1000; 372a7c30e62SVinod Koul 373a7c30e62SVinod Koul /* Reset to POR values and enable clk */ 374a7c30e62SVinod Koul for (i = 0; i < ethqos->num_por; i++) 375a7c30e62SVinod Koul rgmii_writel(ethqos, ethqos->por[i].value, 376a7c30e62SVinod Koul ethqos->por[i].offset); 377a7c30e62SVinod Koul ethqos_set_func_clk_en(ethqos); 378a7c30e62SVinod Koul 379a7c30e62SVinod Koul /* Initialize the DLL first */ 380a7c30e62SVinod Koul 381a7c30e62SVinod Koul /* Set DLL_RST */ 382a7c30e62SVinod Koul rgmii_updatel(ethqos, SDCC_DLL_CONFIG_DLL_RST, 383a7c30e62SVinod Koul SDCC_DLL_CONFIG_DLL_RST, SDCC_HC_REG_DLL_CONFIG); 384a7c30e62SVinod Koul 385a7c30e62SVinod Koul /* Set PDN */ 386a7c30e62SVinod Koul rgmii_updatel(ethqos, SDCC_DLL_CONFIG_PDN, 387a7c30e62SVinod Koul SDCC_DLL_CONFIG_PDN, SDCC_HC_REG_DLL_CONFIG); 388a7c30e62SVinod Koul 389a7c30e62SVinod Koul /* Clear DLL_RST */ 390a7c30e62SVinod Koul rgmii_updatel(ethqos, SDCC_DLL_CONFIG_DLL_RST, 0, 391a7c30e62SVinod Koul SDCC_HC_REG_DLL_CONFIG); 392a7c30e62SVinod Koul 393a7c30e62SVinod Koul /* Clear PDN */ 394a7c30e62SVinod Koul rgmii_updatel(ethqos, SDCC_DLL_CONFIG_PDN, 0, 395a7c30e62SVinod Koul SDCC_HC_REG_DLL_CONFIG); 396a7c30e62SVinod Koul 397a7c30e62SVinod Koul if (ethqos->speed != SPEED_100 && ethqos->speed != SPEED_10) { 398a7c30e62SVinod Koul /* Set DLL_EN */ 399a7c30e62SVinod Koul rgmii_updatel(ethqos, SDCC_DLL_CONFIG_DLL_EN, 400a7c30e62SVinod Koul SDCC_DLL_CONFIG_DLL_EN, SDCC_HC_REG_DLL_CONFIG); 401a7c30e62SVinod Koul 402a7c30e62SVinod Koul /* Set CK_OUT_EN */ 403a7c30e62SVinod Koul rgmii_updatel(ethqos, SDCC_DLL_CONFIG_CK_OUT_EN, 404a7c30e62SVinod Koul SDCC_DLL_CONFIG_CK_OUT_EN, 405a7c30e62SVinod Koul SDCC_HC_REG_DLL_CONFIG); 406a7c30e62SVinod Koul 407a7c30e62SVinod Koul /* Set USR_CTL bit 26 with mask of 3 bits */ 408a7c30e62SVinod Koul rgmii_updatel(ethqos, GENMASK(26, 24), BIT(26), SDCC_USR_CTL); 409a7c30e62SVinod Koul 410a7c30e62SVinod Koul /* wait for DLL LOCK */ 411a7c30e62SVinod Koul do { 412a7c30e62SVinod Koul mdelay(1); 413a7c30e62SVinod Koul dll_lock = rgmii_readl(ethqos, SDC4_STATUS); 414a7c30e62SVinod Koul if (dll_lock & SDC4_STATUS_DLL_LOCK) 415a7c30e62SVinod Koul break; 416a7c30e62SVinod Koul } while (retry > 0); 417a7c30e62SVinod Koul if (!retry) 418a7c30e62SVinod Koul dev_err(ðqos->pdev->dev, 419a7c30e62SVinod Koul "Timeout while waiting for DLL lock\n"); 420a7c30e62SVinod Koul } 421a7c30e62SVinod Koul 422a7c30e62SVinod Koul if (ethqos->speed == SPEED_1000) 423a7c30e62SVinod Koul ethqos_dll_configure(ethqos); 424a7c30e62SVinod Koul 425a7c30e62SVinod Koul ethqos_rgmii_macro_init(ethqos); 426a7c30e62SVinod Koul 427a7c30e62SVinod Koul return 0; 428a7c30e62SVinod Koul } 429a7c30e62SVinod Koul 430a7c30e62SVinod Koul static void ethqos_fix_mac_speed(void *priv, unsigned int speed) 431a7c30e62SVinod Koul { 432a7c30e62SVinod Koul struct qcom_ethqos *ethqos = priv; 433a7c30e62SVinod Koul 434a7c30e62SVinod Koul ethqos->speed = speed; 435a7c30e62SVinod Koul ethqos_update_rgmii_clk(ethqos, speed); 436a7c30e62SVinod Koul ethqos_configure(ethqos); 437a7c30e62SVinod Koul } 438a7c30e62SVinod Koul 439a7c30e62SVinod Koul static int qcom_ethqos_probe(struct platform_device *pdev) 440a7c30e62SVinod Koul { 441a7c30e62SVinod Koul struct device_node *np = pdev->dev.of_node; 442a7c30e62SVinod Koul struct plat_stmmacenet_data *plat_dat; 443a7c30e62SVinod Koul struct stmmac_resources stmmac_res; 444a7c30e62SVinod Koul struct qcom_ethqos *ethqos; 445a7c30e62SVinod Koul struct resource *res; 446a7c30e62SVinod Koul int ret; 447a7c30e62SVinod Koul 448a7c30e62SVinod Koul ret = stmmac_get_platform_resources(pdev, &stmmac_res); 449a7c30e62SVinod Koul if (ret) 450a7c30e62SVinod Koul return ret; 451a7c30e62SVinod Koul 452a7c30e62SVinod Koul plat_dat = stmmac_probe_config_dt(pdev, &stmmac_res.mac); 453a7c30e62SVinod Koul if (IS_ERR(plat_dat)) { 454a7c30e62SVinod Koul dev_err(&pdev->dev, "dt configuration failed\n"); 455a7c30e62SVinod Koul return PTR_ERR(plat_dat); 456a7c30e62SVinod Koul } 457a7c30e62SVinod Koul 458a7c30e62SVinod Koul ethqos = devm_kzalloc(&pdev->dev, sizeof(*ethqos), GFP_KERNEL); 459a7c30e62SVinod Koul if (!ethqos) { 460a7c30e62SVinod Koul ret = -ENOMEM; 461a7c30e62SVinod Koul goto err_mem; 462a7c30e62SVinod Koul } 463a7c30e62SVinod Koul 464a7c30e62SVinod Koul ethqos->pdev = pdev; 465a7c30e62SVinod Koul res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "rgmii"); 466a7c30e62SVinod Koul ethqos->rgmii_base = devm_ioremap_resource(&pdev->dev, res); 467a7c30e62SVinod Koul if (IS_ERR(ethqos->rgmii_base)) { 468a7c30e62SVinod Koul dev_err(&pdev->dev, "Can't get rgmii base\n"); 469a7c30e62SVinod Koul ret = PTR_ERR(ethqos->rgmii_base); 470a7c30e62SVinod Koul goto err_mem; 471a7c30e62SVinod Koul } 472a7c30e62SVinod Koul 473a7c30e62SVinod Koul ethqos->por = of_device_get_match_data(&pdev->dev); 474a7c30e62SVinod Koul 475a7c30e62SVinod Koul ethqos->rgmii_clk = devm_clk_get(&pdev->dev, "rgmii"); 476*8f4ebaaaSWei Yongjun if (IS_ERR(ethqos->rgmii_clk)) { 477*8f4ebaaaSWei Yongjun ret = PTR_ERR(ethqos->rgmii_clk); 478a7c30e62SVinod Koul goto err_mem; 479a7c30e62SVinod Koul } 480a7c30e62SVinod Koul 481a7c30e62SVinod Koul ret = clk_prepare_enable(ethqos->rgmii_clk); 482a7c30e62SVinod Koul if (ret) 483a7c30e62SVinod Koul goto err_mem; 484a7c30e62SVinod Koul 485a7c30e62SVinod Koul ethqos->speed = SPEED_1000; 486a7c30e62SVinod Koul ethqos_update_rgmii_clk(ethqos, SPEED_1000); 487a7c30e62SVinod Koul ethqos_set_func_clk_en(ethqos); 488a7c30e62SVinod Koul 489a7c30e62SVinod Koul plat_dat->bsp_priv = ethqos; 490a7c30e62SVinod Koul plat_dat->fix_mac_speed = ethqos_fix_mac_speed; 491a7c30e62SVinod Koul plat_dat->has_gmac4 = 1; 492a7c30e62SVinod Koul plat_dat->pmt = 1; 493a7c30e62SVinod Koul plat_dat->tso_en = of_property_read_bool(np, "snps,tso"); 494a7c30e62SVinod Koul 495a7c30e62SVinod Koul ret = stmmac_dvr_probe(&pdev->dev, plat_dat, &stmmac_res); 496a7c30e62SVinod Koul if (ret) 497a7c30e62SVinod Koul goto err_clk; 498a7c30e62SVinod Koul 499a7c30e62SVinod Koul rgmii_dump(ethqos); 500a7c30e62SVinod Koul 501a7c30e62SVinod Koul return ret; 502a7c30e62SVinod Koul 503a7c30e62SVinod Koul err_clk: 504a7c30e62SVinod Koul clk_disable_unprepare(ethqos->rgmii_clk); 505a7c30e62SVinod Koul 506a7c30e62SVinod Koul err_mem: 507a7c30e62SVinod Koul stmmac_remove_config_dt(pdev, plat_dat); 508a7c30e62SVinod Koul 509a7c30e62SVinod Koul return ret; 510a7c30e62SVinod Koul } 511a7c30e62SVinod Koul 512a7c30e62SVinod Koul static int qcom_ethqos_remove(struct platform_device *pdev) 513a7c30e62SVinod Koul { 514a7c30e62SVinod Koul struct qcom_ethqos *ethqos; 515a7c30e62SVinod Koul int ret; 516a7c30e62SVinod Koul 517a7c30e62SVinod Koul ethqos = get_stmmac_bsp_priv(&pdev->dev); 518a7c30e62SVinod Koul if (!ethqos) 519a7c30e62SVinod Koul return -ENODEV; 520a7c30e62SVinod Koul 521a7c30e62SVinod Koul ret = stmmac_pltfr_remove(pdev); 522a7c30e62SVinod Koul clk_disable_unprepare(ethqos->rgmii_clk); 523a7c30e62SVinod Koul 524a7c30e62SVinod Koul return ret; 525a7c30e62SVinod Koul } 526a7c30e62SVinod Koul 527a7c30e62SVinod Koul static const struct of_device_id qcom_ethqos_match[] = { 528a7c30e62SVinod Koul { .compatible = "qcom,qcs404-ethqos", .data = &emac_v2_3_0_por}, 529a7c30e62SVinod Koul { } 530a7c30e62SVinod Koul }; 531a7c30e62SVinod Koul MODULE_DEVICE_TABLE(of, qcom_ethqos_match); 532a7c30e62SVinod Koul 533a7c30e62SVinod Koul static struct platform_driver qcom_ethqos_driver = { 534a7c30e62SVinod Koul .probe = qcom_ethqos_probe, 535a7c30e62SVinod Koul .remove = qcom_ethqos_remove, 536a7c30e62SVinod Koul .driver = { 537a7c30e62SVinod Koul .name = "qcom-ethqos", 538a7c30e62SVinod Koul .pm = &stmmac_pltfr_pm_ops, 539a7c30e62SVinod Koul .of_match_table = of_match_ptr(qcom_ethqos_match), 540a7c30e62SVinod Koul }, 541a7c30e62SVinod Koul }; 542a7c30e62SVinod Koul module_platform_driver(qcom_ethqos_driver); 543a7c30e62SVinod Koul 544a7c30e62SVinod Koul MODULE_DESCRIPTION("Qualcomm ETHQOS driver"); 545a7c30e62SVinod Koul MODULE_LICENSE("GPL v2"); 546