144d30d62SAlan Douglas // SPDX-License-Identifier: GPL-2.0 244d30d62SAlan Douglas /* 344d30d62SAlan Douglas * Cadence Sierra PHY Driver 444d30d62SAlan Douglas * 544d30d62SAlan Douglas * Copyright (c) 2018 Cadence Design Systems 644d30d62SAlan Douglas * Author: Alan Douglas <adouglas@cadence.com> 744d30d62SAlan Douglas * 844d30d62SAlan Douglas */ 944d30d62SAlan Douglas #include <linux/clk.h> 1044d30d62SAlan Douglas #include <linux/delay.h> 1144d30d62SAlan Douglas #include <linux/err.h> 1244d30d62SAlan Douglas #include <linux/io.h> 1344d30d62SAlan Douglas #include <linux/module.h> 1444d30d62SAlan Douglas #include <linux/phy/phy.h> 1544d30d62SAlan Douglas #include <linux/platform_device.h> 1644d30d62SAlan Douglas #include <linux/pm_runtime.h> 1744d30d62SAlan Douglas #include <linux/regmap.h> 1844d30d62SAlan Douglas #include <linux/reset.h> 1944d30d62SAlan Douglas #include <linux/slab.h> 2044d30d62SAlan Douglas #include <linux/of.h> 2144d30d62SAlan Douglas #include <linux/of_platform.h> 2244d30d62SAlan Douglas #include <dt-bindings/phy/phy.h> 2344d30d62SAlan Douglas 2444d30d62SAlan Douglas /* PHY register offsets */ 25380f5708SKishon Vijay Abraham I #define SIERRA_COMMON_CDB_OFFSET 0x0 26380f5708SKishon Vijay Abraham I #define SIERRA_MACRO_ID_REG 0x0 27871002d7SAnil Varughese #define SIERRA_CMN_PLLLC_MODE_PREG 0x48 28871002d7SAnil Varughese #define SIERRA_CMN_PLLLC_LF_COEFF_MODE1_PREG 0x49 29871002d7SAnil Varughese #define SIERRA_CMN_PLLLC_LF_COEFF_MODE0_PREG 0x4A 30871002d7SAnil Varughese #define SIERRA_CMN_PLLLC_LOCK_CNTSTART_PREG 0x4B 31871002d7SAnil Varughese #define SIERRA_CMN_PLLLC_BWCAL_MODE1_PREG 0x4F 32871002d7SAnil Varughese #define SIERRA_CMN_PLLLC_BWCAL_MODE0_PREG 0x50 33871002d7SAnil Varughese #define SIERRA_CMN_PLLLC_SS_TIME_STEPSIZE_MODE_PREG 0x62 34380f5708SKishon Vijay Abraham I 35380f5708SKishon Vijay Abraham I #define SIERRA_LANE_CDB_OFFSET(ln, block_offset, reg_offset) \ 36380f5708SKishon Vijay Abraham I ((0x4000 << (block_offset)) + \ 37380f5708SKishon Vijay Abraham I (((ln) << 9) << (reg_offset))) 38aead5fd6SKishon Vijay Abraham I 39aead5fd6SKishon Vijay Abraham I #define SIERRA_DET_STANDEC_A_PREG 0x000 40aead5fd6SKishon Vijay Abraham I #define SIERRA_DET_STANDEC_B_PREG 0x001 41aead5fd6SKishon Vijay Abraham I #define SIERRA_DET_STANDEC_C_PREG 0x002 42aead5fd6SKishon Vijay Abraham I #define SIERRA_DET_STANDEC_D_PREG 0x003 43aead5fd6SKishon Vijay Abraham I #define SIERRA_DET_STANDEC_E_PREG 0x004 44871002d7SAnil Varughese #define SIERRA_PSM_LANECAL_DLY_A1_RESETS_PREG 0x008 45871002d7SAnil Varughese #define SIERRA_PSM_A0IN_TMR_PREG 0x009 46aead5fd6SKishon Vijay Abraham I #define SIERRA_PSM_DIAG_PREG 0x015 47aead5fd6SKishon Vijay Abraham I #define SIERRA_PSC_TX_A0_PREG 0x028 48aead5fd6SKishon Vijay Abraham I #define SIERRA_PSC_TX_A1_PREG 0x029 49aead5fd6SKishon Vijay Abraham I #define SIERRA_PSC_TX_A2_PREG 0x02A 50aead5fd6SKishon Vijay Abraham I #define SIERRA_PSC_TX_A3_PREG 0x02B 51aead5fd6SKishon Vijay Abraham I #define SIERRA_PSC_RX_A0_PREG 0x030 52aead5fd6SKishon Vijay Abraham I #define SIERRA_PSC_RX_A1_PREG 0x031 53aead5fd6SKishon Vijay Abraham I #define SIERRA_PSC_RX_A2_PREG 0x032 54aead5fd6SKishon Vijay Abraham I #define SIERRA_PSC_RX_A3_PREG 0x033 55aead5fd6SKishon Vijay Abraham I #define SIERRA_PLLCTRL_SUBRATE_PREG 0x03A 56aead5fd6SKishon Vijay Abraham I #define SIERRA_PLLCTRL_GEN_D_PREG 0x03E 57871002d7SAnil Varughese #define SIERRA_PLLCTRL_CPGAIN_MODE_PREG 0x03F 58adc4bd6fSKishon Vijay Abraham I #define SIERRA_PLLCTRL_STATUS_PREG 0x044 59871002d7SAnil Varughese #define SIERRA_CLKPATH_BIASTRIM_PREG 0x04B 60871002d7SAnil Varughese #define SIERRA_DFE_BIASTRIM_PREG 0x04C 61aead5fd6SKishon Vijay Abraham I #define SIERRA_DRVCTRL_ATTEN_PREG 0x06A 62aead5fd6SKishon Vijay Abraham I #define SIERRA_CLKPATHCTRL_TMR_PREG 0x081 63871002d7SAnil Varughese #define SIERRA_RX_CREQ_FLTR_A_MODE3_PREG 0x085 64871002d7SAnil Varughese #define SIERRA_RX_CREQ_FLTR_A_MODE2_PREG 0x086 65aead5fd6SKishon Vijay Abraham I #define SIERRA_RX_CREQ_FLTR_A_MODE1_PREG 0x087 66aead5fd6SKishon Vijay Abraham I #define SIERRA_RX_CREQ_FLTR_A_MODE0_PREG 0x088 67aead5fd6SKishon Vijay Abraham I #define SIERRA_CREQ_CCLKDET_MODE01_PREG 0x08E 68aead5fd6SKishon Vijay Abraham I #define SIERRA_RX_CTLE_MAINTENANCE_PREG 0x091 69aead5fd6SKishon Vijay Abraham I #define SIERRA_CREQ_FSMCLK_SEL_PREG 0x092 70871002d7SAnil Varughese #define SIERRA_CREQ_EQ_CTRL_PREG 0x093 71871002d7SAnil Varughese #define SIERRA_CREQ_SPARE_PREG 0x096 72871002d7SAnil Varughese #define SIERRA_CREQ_EQ_OPEN_EYE_THRESH_PREG 0x097 73aead5fd6SKishon Vijay Abraham I #define SIERRA_CTLELUT_CTRL_PREG 0x098 74aead5fd6SKishon Vijay Abraham I #define SIERRA_DFE_ECMP_RATESEL_PREG 0x0C0 75aead5fd6SKishon Vijay Abraham I #define SIERRA_DFE_SMP_RATESEL_PREG 0x0C1 76871002d7SAnil Varughese #define SIERRA_DEQ_PHALIGN_CTRL 0x0C4 77871002d7SAnil Varughese #define SIERRA_DEQ_CONCUR_CTRL1_PREG 0x0C8 78871002d7SAnil Varughese #define SIERRA_DEQ_CONCUR_CTRL2_PREG 0x0C9 79871002d7SAnil Varughese #define SIERRA_DEQ_EPIPWR_CTRL2_PREG 0x0CD 80871002d7SAnil Varughese #define SIERRA_DEQ_FAST_MAINT_CYCLES_PREG 0x0CE 81871002d7SAnil Varughese #define SIERRA_DEQ_ERRCMP_CTRL_PREG 0x0D0 82871002d7SAnil Varughese #define SIERRA_DEQ_OFFSET_CTRL_PREG 0x0D8 83871002d7SAnil Varughese #define SIERRA_DEQ_GAIN_CTRL_PREG 0x0E0 84aead5fd6SKishon Vijay Abraham I #define SIERRA_DEQ_VGATUNE_CTRL_PREG 0x0E1 85871002d7SAnil Varughese #define SIERRA_DEQ_GLUT0 0x0E8 86871002d7SAnil Varughese #define SIERRA_DEQ_GLUT1 0x0E9 87871002d7SAnil Varughese #define SIERRA_DEQ_GLUT2 0x0EA 88871002d7SAnil Varughese #define SIERRA_DEQ_GLUT3 0x0EB 89871002d7SAnil Varughese #define SIERRA_DEQ_GLUT4 0x0EC 90871002d7SAnil Varughese #define SIERRA_DEQ_GLUT5 0x0ED 91871002d7SAnil Varughese #define SIERRA_DEQ_GLUT6 0x0EE 92871002d7SAnil Varughese #define SIERRA_DEQ_GLUT7 0x0EF 93871002d7SAnil Varughese #define SIERRA_DEQ_GLUT8 0x0F0 94871002d7SAnil Varughese #define SIERRA_DEQ_GLUT9 0x0F1 95871002d7SAnil Varughese #define SIERRA_DEQ_GLUT10 0x0F2 96871002d7SAnil Varughese #define SIERRA_DEQ_GLUT11 0x0F3 97871002d7SAnil Varughese #define SIERRA_DEQ_GLUT12 0x0F4 98871002d7SAnil Varughese #define SIERRA_DEQ_GLUT13 0x0F5 99871002d7SAnil Varughese #define SIERRA_DEQ_GLUT14 0x0F6 100871002d7SAnil Varughese #define SIERRA_DEQ_GLUT15 0x0F7 101871002d7SAnil Varughese #define SIERRA_DEQ_GLUT16 0x0F8 102871002d7SAnil Varughese #define SIERRA_DEQ_ALUT0 0x108 103871002d7SAnil Varughese #define SIERRA_DEQ_ALUT1 0x109 104871002d7SAnil Varughese #define SIERRA_DEQ_ALUT2 0x10A 105871002d7SAnil Varughese #define SIERRA_DEQ_ALUT3 0x10B 106871002d7SAnil Varughese #define SIERRA_DEQ_ALUT4 0x10C 107871002d7SAnil Varughese #define SIERRA_DEQ_ALUT5 0x10D 108871002d7SAnil Varughese #define SIERRA_DEQ_ALUT6 0x10E 109871002d7SAnil Varughese #define SIERRA_DEQ_ALUT7 0x10F 110871002d7SAnil Varughese #define SIERRA_DEQ_ALUT8 0x110 111871002d7SAnil Varughese #define SIERRA_DEQ_ALUT9 0x111 112871002d7SAnil Varughese #define SIERRA_DEQ_ALUT10 0x112 113871002d7SAnil Varughese #define SIERRA_DEQ_ALUT11 0x113 114871002d7SAnil Varughese #define SIERRA_DEQ_ALUT12 0x114 115871002d7SAnil Varughese #define SIERRA_DEQ_ALUT13 0x115 116871002d7SAnil Varughese #define SIERRA_DEQ_DFETAP_CTRL_PREG 0x128 117871002d7SAnil Varughese #define SIERRA_DFE_EN_1010_IGNORE_PREG 0x134 118871002d7SAnil Varughese #define SIERRA_DEQ_TAU_CTRL1_SLOW_MAINT_PREG 0x150 119871002d7SAnil Varughese #define SIERRA_DEQ_TAU_CTRL2_PREG 0x151 120871002d7SAnil Varughese #define SIERRA_DEQ_PICTRL_PREG 0x161 121871002d7SAnil Varughese #define SIERRA_CPICAL_TMRVAL_MODE1_PREG 0x170 122871002d7SAnil Varughese #define SIERRA_CPICAL_TMRVAL_MODE0_PREG 0x171 123871002d7SAnil Varughese #define SIERRA_CPICAL_PICNT_MODE1_PREG 0x174 124aead5fd6SKishon Vijay Abraham I #define SIERRA_CPI_OUTBUF_RATESEL_PREG 0x17C 125871002d7SAnil Varughese #define SIERRA_CPICAL_RES_STARTCODE_MODE23_PREG 0x183 126871002d7SAnil Varughese #define SIERRA_LFPSDET_SUPPORT_PREG 0x188 127aead5fd6SKishon Vijay Abraham I #define SIERRA_LFPSFILT_NS_PREG 0x18A 128aead5fd6SKishon Vijay Abraham I #define SIERRA_LFPSFILT_RD_PREG 0x18B 129aead5fd6SKishon Vijay Abraham I #define SIERRA_LFPSFILT_MP_PREG 0x18C 130871002d7SAnil Varughese #define SIERRA_SIGDET_SUPPORT_PREG 0x190 131aead5fd6SKishon Vijay Abraham I #define SIERRA_SDFILT_H2L_A_PREG 0x191 132871002d7SAnil Varughese #define SIERRA_SDFILT_L2H_PREG 0x193 133871002d7SAnil Varughese #define SIERRA_RXBUFFER_CTLECTRL_PREG 0x19E 134871002d7SAnil Varughese #define SIERRA_RXBUFFER_RCDFECTRL_PREG 0x19F 135871002d7SAnil Varughese #define SIERRA_RXBUFFER_DFECTRL_PREG 0x1A0 136871002d7SAnil Varughese #define SIERRA_DEQ_TAU_CTRL1_FAST_MAINT_PREG 0x14F 137871002d7SAnil Varughese #define SIERRA_DEQ_TAU_CTRL1_SLOW_MAINT_PREG 0x150 138380f5708SKishon Vijay Abraham I 139380f5708SKishon Vijay Abraham I #define SIERRA_PHY_CONFIG_CTRL_OFFSET(block_offset) \ 140380f5708SKishon Vijay Abraham I (0xc000 << (block_offset)) 141380f5708SKishon Vijay Abraham I #define SIERRA_PHY_PLL_CFG 0xe 14244d30d62SAlan Douglas 14344d30d62SAlan Douglas #define SIERRA_MACRO_ID 0x00007364 144a43f72aeSKishon Vijay Abraham I #define SIERRA_MAX_LANES 16 145adc4bd6fSKishon Vijay Abraham I #define PLL_LOCK_TIME 100000 14644d30d62SAlan Douglas 147a0c30cd7SKishon Vijay Abraham I #define CDNS_SIERRA_INPUT_CLOCKS 3 148a0c30cd7SKishon Vijay Abraham I enum cdns_sierra_clock_input { 149a0c30cd7SKishon Vijay Abraham I PHY_CLK, 150a0c30cd7SKishon Vijay Abraham I CMN_REFCLK_DIG_DIV, 151a0c30cd7SKishon Vijay Abraham I CMN_REFCLK1_DIG_DIV, 152a0c30cd7SKishon Vijay Abraham I }; 153a0c30cd7SKishon Vijay Abraham I 154380f5708SKishon Vijay Abraham I static const struct reg_field macro_id_type = 155380f5708SKishon Vijay Abraham I REG_FIELD(SIERRA_MACRO_ID_REG, 0, 15); 156380f5708SKishon Vijay Abraham I static const struct reg_field phy_pll_cfg_1 = 157380f5708SKishon Vijay Abraham I REG_FIELD(SIERRA_PHY_PLL_CFG, 1, 1); 158adc4bd6fSKishon Vijay Abraham I static const struct reg_field pllctrl_lock = 159adc4bd6fSKishon Vijay Abraham I REG_FIELD(SIERRA_PLLCTRL_STATUS_PREG, 0, 0); 160380f5708SKishon Vijay Abraham I 16144d30d62SAlan Douglas struct cdns_sierra_inst { 16244d30d62SAlan Douglas struct phy *phy; 16344d30d62SAlan Douglas u32 phy_type; 16444d30d62SAlan Douglas u32 num_lanes; 16544d30d62SAlan Douglas u32 mlane; 16644d30d62SAlan Douglas struct reset_control *lnk_rst; 16744d30d62SAlan Douglas }; 16844d30d62SAlan Douglas 16944d30d62SAlan Douglas struct cdns_reg_pairs { 17044d30d62SAlan Douglas u16 val; 17144d30d62SAlan Douglas u32 off; 17244d30d62SAlan Douglas }; 17344d30d62SAlan Douglas 17444d30d62SAlan Douglas struct cdns_sierra_data { 17544d30d62SAlan Douglas u32 id_value; 176380f5708SKishon Vijay Abraham I u8 block_offset_shift; 177380f5708SKishon Vijay Abraham I u8 reg_offset_shift; 178871002d7SAnil Varughese u32 pcie_cmn_regs; 179871002d7SAnil Varughese u32 pcie_ln_regs; 180871002d7SAnil Varughese u32 usb_cmn_regs; 181871002d7SAnil Varughese u32 usb_ln_regs; 1823cfb0e8eSRikard Falkeborn const struct cdns_reg_pairs *pcie_cmn_vals; 1833cfb0e8eSRikard Falkeborn const struct cdns_reg_pairs *pcie_ln_vals; 1843cfb0e8eSRikard Falkeborn const struct cdns_reg_pairs *usb_cmn_vals; 1853cfb0e8eSRikard Falkeborn const struct cdns_reg_pairs *usb_ln_vals; 18644d30d62SAlan Douglas }; 18744d30d62SAlan Douglas 188380f5708SKishon Vijay Abraham I struct cdns_regmap_cdb_context { 18944d30d62SAlan Douglas struct device *dev; 19044d30d62SAlan Douglas void __iomem *base; 191380f5708SKishon Vijay Abraham I u8 reg_offset_shift; 192380f5708SKishon Vijay Abraham I }; 193380f5708SKishon Vijay Abraham I 194380f5708SKishon Vijay Abraham I struct cdns_sierra_phy { 195380f5708SKishon Vijay Abraham I struct device *dev; 196380f5708SKishon Vijay Abraham I struct regmap *regmap; 19744d30d62SAlan Douglas struct cdns_sierra_data *init_data; 19844d30d62SAlan Douglas struct cdns_sierra_inst phys[SIERRA_MAX_LANES]; 19944d30d62SAlan Douglas struct reset_control *phy_rst; 20044d30d62SAlan Douglas struct reset_control *apb_rst; 201380f5708SKishon Vijay Abraham I struct regmap *regmap_lane_cdb[SIERRA_MAX_LANES]; 202380f5708SKishon Vijay Abraham I struct regmap *regmap_phy_config_ctrl; 203380f5708SKishon Vijay Abraham I struct regmap *regmap_common_cdb; 204380f5708SKishon Vijay Abraham I struct regmap_field *macro_id_type; 205380f5708SKishon Vijay Abraham I struct regmap_field *phy_pll_cfg_1; 206adc4bd6fSKishon Vijay Abraham I struct regmap_field *pllctrl_lock[SIERRA_MAX_LANES]; 207a0c30cd7SKishon Vijay Abraham I struct clk *input_clks[CDNS_SIERRA_INPUT_CLOCKS]; 20844d30d62SAlan Douglas int nsubnodes; 209a43f72aeSKishon Vijay Abraham I u32 num_lanes; 21044d30d62SAlan Douglas bool autoconf; 21144d30d62SAlan Douglas }; 21244d30d62SAlan Douglas 213380f5708SKishon Vijay Abraham I static int cdns_regmap_write(void *context, unsigned int reg, unsigned int val) 214380f5708SKishon Vijay Abraham I { 215380f5708SKishon Vijay Abraham I struct cdns_regmap_cdb_context *ctx = context; 216380f5708SKishon Vijay Abraham I u32 offset = reg << ctx->reg_offset_shift; 217380f5708SKishon Vijay Abraham I 218380f5708SKishon Vijay Abraham I writew(val, ctx->base + offset); 219380f5708SKishon Vijay Abraham I 220380f5708SKishon Vijay Abraham I return 0; 221380f5708SKishon Vijay Abraham I } 222380f5708SKishon Vijay Abraham I 223380f5708SKishon Vijay Abraham I static int cdns_regmap_read(void *context, unsigned int reg, unsigned int *val) 224380f5708SKishon Vijay Abraham I { 225380f5708SKishon Vijay Abraham I struct cdns_regmap_cdb_context *ctx = context; 226380f5708SKishon Vijay Abraham I u32 offset = reg << ctx->reg_offset_shift; 227380f5708SKishon Vijay Abraham I 228380f5708SKishon Vijay Abraham I *val = readw(ctx->base + offset); 229380f5708SKishon Vijay Abraham I return 0; 230380f5708SKishon Vijay Abraham I } 231380f5708SKishon Vijay Abraham I 232380f5708SKishon Vijay Abraham I #define SIERRA_LANE_CDB_REGMAP_CONF(n) \ 233380f5708SKishon Vijay Abraham I { \ 234380f5708SKishon Vijay Abraham I .name = "sierra_lane" n "_cdb", \ 235380f5708SKishon Vijay Abraham I .reg_stride = 1, \ 236380f5708SKishon Vijay Abraham I .fast_io = true, \ 237380f5708SKishon Vijay Abraham I .reg_write = cdns_regmap_write, \ 238380f5708SKishon Vijay Abraham I .reg_read = cdns_regmap_read, \ 239380f5708SKishon Vijay Abraham I } 240380f5708SKishon Vijay Abraham I 2413cfb0e8eSRikard Falkeborn static const struct regmap_config cdns_sierra_lane_cdb_config[] = { 242380f5708SKishon Vijay Abraham I SIERRA_LANE_CDB_REGMAP_CONF("0"), 243380f5708SKishon Vijay Abraham I SIERRA_LANE_CDB_REGMAP_CONF("1"), 244380f5708SKishon Vijay Abraham I SIERRA_LANE_CDB_REGMAP_CONF("2"), 245380f5708SKishon Vijay Abraham I SIERRA_LANE_CDB_REGMAP_CONF("3"), 246a43f72aeSKishon Vijay Abraham I SIERRA_LANE_CDB_REGMAP_CONF("4"), 247a43f72aeSKishon Vijay Abraham I SIERRA_LANE_CDB_REGMAP_CONF("5"), 248a43f72aeSKishon Vijay Abraham I SIERRA_LANE_CDB_REGMAP_CONF("6"), 249a43f72aeSKishon Vijay Abraham I SIERRA_LANE_CDB_REGMAP_CONF("7"), 250a43f72aeSKishon Vijay Abraham I SIERRA_LANE_CDB_REGMAP_CONF("8"), 251a43f72aeSKishon Vijay Abraham I SIERRA_LANE_CDB_REGMAP_CONF("9"), 252a43f72aeSKishon Vijay Abraham I SIERRA_LANE_CDB_REGMAP_CONF("10"), 253a43f72aeSKishon Vijay Abraham I SIERRA_LANE_CDB_REGMAP_CONF("11"), 254a43f72aeSKishon Vijay Abraham I SIERRA_LANE_CDB_REGMAP_CONF("12"), 255a43f72aeSKishon Vijay Abraham I SIERRA_LANE_CDB_REGMAP_CONF("13"), 256a43f72aeSKishon Vijay Abraham I SIERRA_LANE_CDB_REGMAP_CONF("14"), 257a43f72aeSKishon Vijay Abraham I SIERRA_LANE_CDB_REGMAP_CONF("15"), 258380f5708SKishon Vijay Abraham I }; 259380f5708SKishon Vijay Abraham I 2603cfb0e8eSRikard Falkeborn static const struct regmap_config cdns_sierra_common_cdb_config = { 261380f5708SKishon Vijay Abraham I .name = "sierra_common_cdb", 262380f5708SKishon Vijay Abraham I .reg_stride = 1, 263380f5708SKishon Vijay Abraham I .fast_io = true, 264380f5708SKishon Vijay Abraham I .reg_write = cdns_regmap_write, 265380f5708SKishon Vijay Abraham I .reg_read = cdns_regmap_read, 266380f5708SKishon Vijay Abraham I }; 267380f5708SKishon Vijay Abraham I 2683cfb0e8eSRikard Falkeborn static const struct regmap_config cdns_sierra_phy_config_ctrl_config = { 269380f5708SKishon Vijay Abraham I .name = "sierra_phy_config_ctrl", 270380f5708SKishon Vijay Abraham I .reg_stride = 1, 271380f5708SKishon Vijay Abraham I .fast_io = true, 272380f5708SKishon Vijay Abraham I .reg_write = cdns_regmap_write, 273380f5708SKishon Vijay Abraham I .reg_read = cdns_regmap_read, 274380f5708SKishon Vijay Abraham I }; 275380f5708SKishon Vijay Abraham I 276cedcc2e2SKishon Vijay Abraham I static int cdns_sierra_phy_init(struct phy *gphy) 27744d30d62SAlan Douglas { 27844d30d62SAlan Douglas struct cdns_sierra_inst *ins = phy_get_drvdata(gphy); 27944d30d62SAlan Douglas struct cdns_sierra_phy *phy = dev_get_drvdata(gphy->dev.parent); 28080f96fb1SColin Ian King struct regmap *regmap; 28144d30d62SAlan Douglas int i, j; 2823cfb0e8eSRikard Falkeborn const struct cdns_reg_pairs *cmn_vals, *ln_vals; 283871002d7SAnil Varughese u32 num_cmn_regs, num_ln_regs; 28444d30d62SAlan Douglas 285cedcc2e2SKishon Vijay Abraham I /* Initialise the PHY registers, unless auto configured */ 286cedcc2e2SKishon Vijay Abraham I if (phy->autoconf) 287cedcc2e2SKishon Vijay Abraham I return 0; 288cedcc2e2SKishon Vijay Abraham I 289a0c30cd7SKishon Vijay Abraham I clk_set_rate(phy->input_clks[CMN_REFCLK_DIG_DIV], 25000000); 290a0c30cd7SKishon Vijay Abraham I clk_set_rate(phy->input_clks[CMN_REFCLK1_DIG_DIV], 25000000); 29144d30d62SAlan Douglas if (ins->phy_type == PHY_TYPE_PCIE) { 292871002d7SAnil Varughese num_cmn_regs = phy->init_data->pcie_cmn_regs; 293871002d7SAnil Varughese num_ln_regs = phy->init_data->pcie_ln_regs; 294871002d7SAnil Varughese cmn_vals = phy->init_data->pcie_cmn_vals; 295871002d7SAnil Varughese ln_vals = phy->init_data->pcie_ln_vals; 29644d30d62SAlan Douglas } else if (ins->phy_type == PHY_TYPE_USB3) { 297871002d7SAnil Varughese num_cmn_regs = phy->init_data->usb_cmn_regs; 298871002d7SAnil Varughese num_ln_regs = phy->init_data->usb_ln_regs; 299871002d7SAnil Varughese cmn_vals = phy->init_data->usb_cmn_vals; 300871002d7SAnil Varughese ln_vals = phy->init_data->usb_ln_vals; 30144d30d62SAlan Douglas } else { 302cedcc2e2SKishon Vijay Abraham I return -EINVAL; 30344d30d62SAlan Douglas } 304871002d7SAnil Varughese 305871002d7SAnil Varughese regmap = phy->regmap_common_cdb; 306871002d7SAnil Varughese for (j = 0; j < num_cmn_regs ; j++) 307871002d7SAnil Varughese regmap_write(regmap, cmn_vals[j].off, cmn_vals[j].val); 308871002d7SAnil Varughese 309380f5708SKishon Vijay Abraham I for (i = 0; i < ins->num_lanes; i++) { 310871002d7SAnil Varughese for (j = 0; j < num_ln_regs ; j++) { 311380f5708SKishon Vijay Abraham I regmap = phy->regmap_lane_cdb[i + ins->mlane]; 312871002d7SAnil Varughese regmap_write(regmap, ln_vals[j].off, ln_vals[j].val); 313380f5708SKishon Vijay Abraham I } 314380f5708SKishon Vijay Abraham I } 315cedcc2e2SKishon Vijay Abraham I 316cedcc2e2SKishon Vijay Abraham I return 0; 31744d30d62SAlan Douglas } 31844d30d62SAlan Douglas 31944d30d62SAlan Douglas static int cdns_sierra_phy_on(struct phy *gphy) 32044d30d62SAlan Douglas { 321adc4bd6fSKishon Vijay Abraham I struct cdns_sierra_phy *sp = dev_get_drvdata(gphy->dev.parent); 32244d30d62SAlan Douglas struct cdns_sierra_inst *ins = phy_get_drvdata(gphy); 323adc4bd6fSKishon Vijay Abraham I struct device *dev = sp->dev; 324adc4bd6fSKishon Vijay Abraham I u32 val; 325adc4bd6fSKishon Vijay Abraham I int ret; 32644d30d62SAlan Douglas 3275b4f5757SKishon Vijay Abraham I ret = reset_control_deassert(sp->phy_rst); 3285b4f5757SKishon Vijay Abraham I if (ret) { 3295b4f5757SKishon Vijay Abraham I dev_err(dev, "Failed to take the PHY out of reset\n"); 3305b4f5757SKishon Vijay Abraham I return ret; 3315b4f5757SKishon Vijay Abraham I } 3325b4f5757SKishon Vijay Abraham I 33344d30d62SAlan Douglas /* Take the PHY lane group out of reset */ 334adc4bd6fSKishon Vijay Abraham I ret = reset_control_deassert(ins->lnk_rst); 335adc4bd6fSKishon Vijay Abraham I if (ret) { 336adc4bd6fSKishon Vijay Abraham I dev_err(dev, "Failed to take the PHY lane out of reset\n"); 337adc4bd6fSKishon Vijay Abraham I return ret; 338adc4bd6fSKishon Vijay Abraham I } 339adc4bd6fSKishon Vijay Abraham I 340adc4bd6fSKishon Vijay Abraham I ret = regmap_field_read_poll_timeout(sp->pllctrl_lock[ins->mlane], 341adc4bd6fSKishon Vijay Abraham I val, val, 1000, PLL_LOCK_TIME); 342adc4bd6fSKishon Vijay Abraham I if (ret < 0) 343adc4bd6fSKishon Vijay Abraham I dev_err(dev, "PLL lock of lane failed\n"); 344adc4bd6fSKishon Vijay Abraham I 345adc4bd6fSKishon Vijay Abraham I return ret; 34644d30d62SAlan Douglas } 34744d30d62SAlan Douglas 34844d30d62SAlan Douglas static int cdns_sierra_phy_off(struct phy *gphy) 34944d30d62SAlan Douglas { 35044d30d62SAlan Douglas struct cdns_sierra_inst *ins = phy_get_drvdata(gphy); 35144d30d62SAlan Douglas 35244d30d62SAlan Douglas return reset_control_assert(ins->lnk_rst); 35344d30d62SAlan Douglas } 35444d30d62SAlan Douglas 3557904e15bSRoger Quadros static int cdns_sierra_phy_reset(struct phy *gphy) 3567904e15bSRoger Quadros { 3577904e15bSRoger Quadros struct cdns_sierra_phy *sp = dev_get_drvdata(gphy->dev.parent); 3587904e15bSRoger Quadros 3597904e15bSRoger Quadros reset_control_assert(sp->phy_rst); 3607904e15bSRoger Quadros reset_control_deassert(sp->phy_rst); 3617904e15bSRoger Quadros return 0; 3627904e15bSRoger Quadros }; 3637904e15bSRoger Quadros 36444d30d62SAlan Douglas static const struct phy_ops ops = { 365cedcc2e2SKishon Vijay Abraham I .init = cdns_sierra_phy_init, 36644d30d62SAlan Douglas .power_on = cdns_sierra_phy_on, 36744d30d62SAlan Douglas .power_off = cdns_sierra_phy_off, 3687904e15bSRoger Quadros .reset = cdns_sierra_phy_reset, 36944d30d62SAlan Douglas .owner = THIS_MODULE, 37044d30d62SAlan Douglas }; 37144d30d62SAlan Douglas 37244d30d62SAlan Douglas static int cdns_sierra_get_optional(struct cdns_sierra_inst *inst, 37344d30d62SAlan Douglas struct device_node *child) 37444d30d62SAlan Douglas { 37544d30d62SAlan Douglas if (of_property_read_u32(child, "reg", &inst->mlane)) 37644d30d62SAlan Douglas return -EINVAL; 37744d30d62SAlan Douglas 37844d30d62SAlan Douglas if (of_property_read_u32(child, "cdns,num-lanes", &inst->num_lanes)) 37944d30d62SAlan Douglas return -EINVAL; 38044d30d62SAlan Douglas 38144d30d62SAlan Douglas if (of_property_read_u32(child, "cdns,phy-type", &inst->phy_type)) 38244d30d62SAlan Douglas return -EINVAL; 38344d30d62SAlan Douglas 38444d30d62SAlan Douglas return 0; 38544d30d62SAlan Douglas } 38644d30d62SAlan Douglas 38744d30d62SAlan Douglas static const struct of_device_id cdns_sierra_id_table[]; 38844d30d62SAlan Douglas 389380f5708SKishon Vijay Abraham I static struct regmap *cdns_regmap_init(struct device *dev, void __iomem *base, 390380f5708SKishon Vijay Abraham I u32 block_offset, u8 reg_offset_shift, 391380f5708SKishon Vijay Abraham I const struct regmap_config *config) 392380f5708SKishon Vijay Abraham I { 393380f5708SKishon Vijay Abraham I struct cdns_regmap_cdb_context *ctx; 394380f5708SKishon Vijay Abraham I 395380f5708SKishon Vijay Abraham I ctx = devm_kzalloc(dev, sizeof(*ctx), GFP_KERNEL); 396380f5708SKishon Vijay Abraham I if (!ctx) 397380f5708SKishon Vijay Abraham I return ERR_PTR(-ENOMEM); 398380f5708SKishon Vijay Abraham I 399380f5708SKishon Vijay Abraham I ctx->dev = dev; 400380f5708SKishon Vijay Abraham I ctx->base = base + block_offset; 401380f5708SKishon Vijay Abraham I ctx->reg_offset_shift = reg_offset_shift; 402380f5708SKishon Vijay Abraham I 403380f5708SKishon Vijay Abraham I return devm_regmap_init(dev, NULL, ctx, config); 404380f5708SKishon Vijay Abraham I } 405380f5708SKishon Vijay Abraham I 406380f5708SKishon Vijay Abraham I static int cdns_regfield_init(struct cdns_sierra_phy *sp) 407380f5708SKishon Vijay Abraham I { 408380f5708SKishon Vijay Abraham I struct device *dev = sp->dev; 409380f5708SKishon Vijay Abraham I struct regmap_field *field; 410380f5708SKishon Vijay Abraham I struct regmap *regmap; 411adc4bd6fSKishon Vijay Abraham I int i; 412380f5708SKishon Vijay Abraham I 413380f5708SKishon Vijay Abraham I regmap = sp->regmap_common_cdb; 414380f5708SKishon Vijay Abraham I field = devm_regmap_field_alloc(dev, regmap, macro_id_type); 415380f5708SKishon Vijay Abraham I if (IS_ERR(field)) { 416380f5708SKishon Vijay Abraham I dev_err(dev, "MACRO_ID_TYPE reg field init failed\n"); 417380f5708SKishon Vijay Abraham I return PTR_ERR(field); 418380f5708SKishon Vijay Abraham I } 419380f5708SKishon Vijay Abraham I sp->macro_id_type = field; 420380f5708SKishon Vijay Abraham I 421380f5708SKishon Vijay Abraham I regmap = sp->regmap_phy_config_ctrl; 422380f5708SKishon Vijay Abraham I field = devm_regmap_field_alloc(dev, regmap, phy_pll_cfg_1); 423380f5708SKishon Vijay Abraham I if (IS_ERR(field)) { 424380f5708SKishon Vijay Abraham I dev_err(dev, "PHY_PLL_CFG_1 reg field init failed\n"); 425380f5708SKishon Vijay Abraham I return PTR_ERR(field); 426380f5708SKishon Vijay Abraham I } 427380f5708SKishon Vijay Abraham I sp->phy_pll_cfg_1 = field; 428380f5708SKishon Vijay Abraham I 429adc4bd6fSKishon Vijay Abraham I for (i = 0; i < SIERRA_MAX_LANES; i++) { 430adc4bd6fSKishon Vijay Abraham I regmap = sp->regmap_lane_cdb[i]; 431adc4bd6fSKishon Vijay Abraham I field = devm_regmap_field_alloc(dev, regmap, pllctrl_lock); 432adc4bd6fSKishon Vijay Abraham I if (IS_ERR(field)) { 433adc4bd6fSKishon Vijay Abraham I dev_err(dev, "P%d_ENABLE reg field init failed\n", i); 434adc4bd6fSKishon Vijay Abraham I return PTR_ERR(field); 435adc4bd6fSKishon Vijay Abraham I } 436adc4bd6fSKishon Vijay Abraham I sp->pllctrl_lock[i] = field; 437adc4bd6fSKishon Vijay Abraham I } 438adc4bd6fSKishon Vijay Abraham I 439380f5708SKishon Vijay Abraham I return 0; 440380f5708SKishon Vijay Abraham I } 441380f5708SKishon Vijay Abraham I 442380f5708SKishon Vijay Abraham I static int cdns_regmap_init_blocks(struct cdns_sierra_phy *sp, 443380f5708SKishon Vijay Abraham I void __iomem *base, u8 block_offset_shift, 444380f5708SKishon Vijay Abraham I u8 reg_offset_shift) 445380f5708SKishon Vijay Abraham I { 446380f5708SKishon Vijay Abraham I struct device *dev = sp->dev; 447380f5708SKishon Vijay Abraham I struct regmap *regmap; 448380f5708SKishon Vijay Abraham I u32 block_offset; 449380f5708SKishon Vijay Abraham I int i; 450380f5708SKishon Vijay Abraham I 451380f5708SKishon Vijay Abraham I for (i = 0; i < SIERRA_MAX_LANES; i++) { 452380f5708SKishon Vijay Abraham I block_offset = SIERRA_LANE_CDB_OFFSET(i, block_offset_shift, 453380f5708SKishon Vijay Abraham I reg_offset_shift); 454380f5708SKishon Vijay Abraham I regmap = cdns_regmap_init(dev, base, block_offset, 455380f5708SKishon Vijay Abraham I reg_offset_shift, 456380f5708SKishon Vijay Abraham I &cdns_sierra_lane_cdb_config[i]); 457380f5708SKishon Vijay Abraham I if (IS_ERR(regmap)) { 458380f5708SKishon Vijay Abraham I dev_err(dev, "Failed to init lane CDB regmap\n"); 459380f5708SKishon Vijay Abraham I return PTR_ERR(regmap); 460380f5708SKishon Vijay Abraham I } 461380f5708SKishon Vijay Abraham I sp->regmap_lane_cdb[i] = regmap; 462380f5708SKishon Vijay Abraham I } 463380f5708SKishon Vijay Abraham I 464380f5708SKishon Vijay Abraham I regmap = cdns_regmap_init(dev, base, SIERRA_COMMON_CDB_OFFSET, 465380f5708SKishon Vijay Abraham I reg_offset_shift, 466380f5708SKishon Vijay Abraham I &cdns_sierra_common_cdb_config); 467380f5708SKishon Vijay Abraham I if (IS_ERR(regmap)) { 468380f5708SKishon Vijay Abraham I dev_err(dev, "Failed to init common CDB regmap\n"); 469380f5708SKishon Vijay Abraham I return PTR_ERR(regmap); 470380f5708SKishon Vijay Abraham I } 471380f5708SKishon Vijay Abraham I sp->regmap_common_cdb = regmap; 472380f5708SKishon Vijay Abraham I 473380f5708SKishon Vijay Abraham I block_offset = SIERRA_PHY_CONFIG_CTRL_OFFSET(block_offset_shift); 474380f5708SKishon Vijay Abraham I regmap = cdns_regmap_init(dev, base, block_offset, reg_offset_shift, 475380f5708SKishon Vijay Abraham I &cdns_sierra_phy_config_ctrl_config); 476380f5708SKishon Vijay Abraham I if (IS_ERR(regmap)) { 477380f5708SKishon Vijay Abraham I dev_err(dev, "Failed to init PHY config and control regmap\n"); 478380f5708SKishon Vijay Abraham I return PTR_ERR(regmap); 479380f5708SKishon Vijay Abraham I } 480380f5708SKishon Vijay Abraham I sp->regmap_phy_config_ctrl = regmap; 481380f5708SKishon Vijay Abraham I 482380f5708SKishon Vijay Abraham I return 0; 483380f5708SKishon Vijay Abraham I } 484380f5708SKishon Vijay Abraham I 4857e016cbcSKishon Vijay Abraham I static int cdns_sierra_phy_get_clocks(struct cdns_sierra_phy *sp, 4867e016cbcSKishon Vijay Abraham I struct device *dev) 4877e016cbcSKishon Vijay Abraham I { 4887e016cbcSKishon Vijay Abraham I struct clk *clk; 4897e016cbcSKishon Vijay Abraham I int ret; 4907e016cbcSKishon Vijay Abraham I 4917e016cbcSKishon Vijay Abraham I clk = devm_clk_get_optional(dev, "phy_clk"); 4927e016cbcSKishon Vijay Abraham I if (IS_ERR(clk)) { 4937e016cbcSKishon Vijay Abraham I dev_err(dev, "failed to get clock phy_clk\n"); 4947e016cbcSKishon Vijay Abraham I return PTR_ERR(clk); 4957e016cbcSKishon Vijay Abraham I } 496a0c30cd7SKishon Vijay Abraham I sp->input_clks[PHY_CLK] = clk; 4977e016cbcSKishon Vijay Abraham I 4987e016cbcSKishon Vijay Abraham I clk = devm_clk_get_optional(dev, "cmn_refclk_dig_div"); 4997e016cbcSKishon Vijay Abraham I if (IS_ERR(clk)) { 5007e016cbcSKishon Vijay Abraham I dev_err(dev, "cmn_refclk_dig_div clock not found\n"); 5017e016cbcSKishon Vijay Abraham I ret = PTR_ERR(clk); 5027e016cbcSKishon Vijay Abraham I return ret; 5037e016cbcSKishon Vijay Abraham I } 504a0c30cd7SKishon Vijay Abraham I sp->input_clks[CMN_REFCLK_DIG_DIV] = clk; 5057e016cbcSKishon Vijay Abraham I 5067e016cbcSKishon Vijay Abraham I clk = devm_clk_get_optional(dev, "cmn_refclk1_dig_div"); 5077e016cbcSKishon Vijay Abraham I if (IS_ERR(clk)) { 5087e016cbcSKishon Vijay Abraham I dev_err(dev, "cmn_refclk1_dig_div clock not found\n"); 5097e016cbcSKishon Vijay Abraham I ret = PTR_ERR(clk); 5107e016cbcSKishon Vijay Abraham I return ret; 5117e016cbcSKishon Vijay Abraham I } 512a0c30cd7SKishon Vijay Abraham I sp->input_clks[CMN_REFCLK1_DIG_DIV] = clk; 5137e016cbcSKishon Vijay Abraham I 5147e016cbcSKishon Vijay Abraham I return 0; 5157e016cbcSKishon Vijay Abraham I } 5167e016cbcSKishon Vijay Abraham I 5171d5f40e0SKishon Vijay Abraham I static int cdns_sierra_phy_get_resets(struct cdns_sierra_phy *sp, 5181d5f40e0SKishon Vijay Abraham I struct device *dev) 5191d5f40e0SKishon Vijay Abraham I { 5201d5f40e0SKishon Vijay Abraham I struct reset_control *rst; 5211d5f40e0SKishon Vijay Abraham I 52215b0b82dSKishon Vijay Abraham I rst = devm_reset_control_get_exclusive(dev, "sierra_reset"); 5231d5f40e0SKishon Vijay Abraham I if (IS_ERR(rst)) { 5241d5f40e0SKishon Vijay Abraham I dev_err(dev, "failed to get reset\n"); 5251d5f40e0SKishon Vijay Abraham I return PTR_ERR(rst); 5261d5f40e0SKishon Vijay Abraham I } 5271d5f40e0SKishon Vijay Abraham I sp->phy_rst = rst; 5281d5f40e0SKishon Vijay Abraham I 52915b0b82dSKishon Vijay Abraham I rst = devm_reset_control_get_optional_exclusive(dev, "sierra_apb"); 5301d5f40e0SKishon Vijay Abraham I if (IS_ERR(rst)) { 5311d5f40e0SKishon Vijay Abraham I dev_err(dev, "failed to get apb reset\n"); 5321d5f40e0SKishon Vijay Abraham I return PTR_ERR(rst); 5331d5f40e0SKishon Vijay Abraham I } 5341d5f40e0SKishon Vijay Abraham I sp->apb_rst = rst; 5351d5f40e0SKishon Vijay Abraham I 5361d5f40e0SKishon Vijay Abraham I return 0; 5371d5f40e0SKishon Vijay Abraham I } 5381d5f40e0SKishon Vijay Abraham I 53944d30d62SAlan Douglas static int cdns_sierra_phy_probe(struct platform_device *pdev) 54044d30d62SAlan Douglas { 54144d30d62SAlan Douglas struct cdns_sierra_phy *sp; 54244d30d62SAlan Douglas struct phy_provider *phy_provider; 54344d30d62SAlan Douglas struct device *dev = &pdev->dev; 54444d30d62SAlan Douglas const struct of_device_id *match; 545380f5708SKishon Vijay Abraham I struct cdns_sierra_data *data; 546380f5708SKishon Vijay Abraham I unsigned int id_value; 54744d30d62SAlan Douglas int i, ret, node = 0; 548380f5708SKishon Vijay Abraham I void __iomem *base; 54944d30d62SAlan Douglas struct device_node *dn = dev->of_node, *child; 55044d30d62SAlan Douglas 55144d30d62SAlan Douglas if (of_get_child_count(dn) == 0) 55244d30d62SAlan Douglas return -ENODEV; 55344d30d62SAlan Douglas 554380f5708SKishon Vijay Abraham I /* Get init data for this PHY */ 555380f5708SKishon Vijay Abraham I match = of_match_device(cdns_sierra_id_table, dev); 556380f5708SKishon Vijay Abraham I if (!match) 557380f5708SKishon Vijay Abraham I return -EINVAL; 558380f5708SKishon Vijay Abraham I 559380f5708SKishon Vijay Abraham I data = (struct cdns_sierra_data *)match->data; 560380f5708SKishon Vijay Abraham I 56144d30d62SAlan Douglas sp = devm_kzalloc(dev, sizeof(*sp), GFP_KERNEL); 56244d30d62SAlan Douglas if (!sp) 56344d30d62SAlan Douglas return -ENOMEM; 56444d30d62SAlan Douglas dev_set_drvdata(dev, sp); 56544d30d62SAlan Douglas sp->dev = dev; 566380f5708SKishon Vijay Abraham I sp->init_data = data; 56744d30d62SAlan Douglas 568fa629094SChunfeng Yun base = devm_platform_ioremap_resource(pdev, 0); 569380f5708SKishon Vijay Abraham I if (IS_ERR(base)) { 57044d30d62SAlan Douglas dev_err(dev, "missing \"reg\"\n"); 571380f5708SKishon Vijay Abraham I return PTR_ERR(base); 57244d30d62SAlan Douglas } 57344d30d62SAlan Douglas 574380f5708SKishon Vijay Abraham I ret = cdns_regmap_init_blocks(sp, base, data->block_offset_shift, 575380f5708SKishon Vijay Abraham I data->reg_offset_shift); 576380f5708SKishon Vijay Abraham I if (ret) 577380f5708SKishon Vijay Abraham I return ret; 578380f5708SKishon Vijay Abraham I 579380f5708SKishon Vijay Abraham I ret = cdns_regfield_init(sp); 580380f5708SKishon Vijay Abraham I if (ret) 581380f5708SKishon Vijay Abraham I return ret; 58244d30d62SAlan Douglas 58344d30d62SAlan Douglas platform_set_drvdata(pdev, sp); 58444d30d62SAlan Douglas 5857e016cbcSKishon Vijay Abraham I ret = cdns_sierra_phy_get_clocks(sp, dev); 5867e016cbcSKishon Vijay Abraham I if (ret) 5877e016cbcSKishon Vijay Abraham I return ret; 58844d30d62SAlan Douglas 5891d5f40e0SKishon Vijay Abraham I ret = cdns_sierra_phy_get_resets(sp, dev); 5901d5f40e0SKishon Vijay Abraham I if (ret) 5911d5f40e0SKishon Vijay Abraham I return ret; 59244d30d62SAlan Douglas 593a0c30cd7SKishon Vijay Abraham I ret = clk_prepare_enable(sp->input_clks[PHY_CLK]); 59444d30d62SAlan Douglas if (ret) 59544d30d62SAlan Douglas return ret; 59644d30d62SAlan Douglas 59744d30d62SAlan Douglas /* Enable APB */ 59844d30d62SAlan Douglas reset_control_deassert(sp->apb_rst); 59944d30d62SAlan Douglas 60044d30d62SAlan Douglas /* Check that PHY is present */ 601380f5708SKishon Vijay Abraham I regmap_field_read(sp->macro_id_type, &id_value); 602380f5708SKishon Vijay Abraham I if (sp->init_data->id_value != id_value) { 60344d30d62SAlan Douglas ret = -EINVAL; 60444d30d62SAlan Douglas goto clk_disable; 60544d30d62SAlan Douglas } 60644d30d62SAlan Douglas 60744d30d62SAlan Douglas sp->autoconf = of_property_read_bool(dn, "cdns,autoconf"); 60844d30d62SAlan Douglas 60944d30d62SAlan Douglas for_each_available_child_of_node(dn, child) { 61044d30d62SAlan Douglas struct phy *gphy; 61144d30d62SAlan Douglas 61203ada5a3SKishon Vijay Abraham I if (!(of_node_name_eq(child, "phy") || 61303ada5a3SKishon Vijay Abraham I of_node_name_eq(child, "link"))) 61403ada5a3SKishon Vijay Abraham I continue; 61503ada5a3SKishon Vijay Abraham I 61644d30d62SAlan Douglas sp->phys[node].lnk_rst = 617b872936fSKishon Vijay Abraham I of_reset_control_array_get_exclusive(child); 61844d30d62SAlan Douglas 61944d30d62SAlan Douglas if (IS_ERR(sp->phys[node].lnk_rst)) { 62044d30d62SAlan Douglas dev_err(dev, "failed to get reset %s\n", 62144d30d62SAlan Douglas child->full_name); 62244d30d62SAlan Douglas ret = PTR_ERR(sp->phys[node].lnk_rst); 62344d30d62SAlan Douglas goto put_child2; 62444d30d62SAlan Douglas } 62544d30d62SAlan Douglas 62644d30d62SAlan Douglas if (!sp->autoconf) { 62744d30d62SAlan Douglas ret = cdns_sierra_get_optional(&sp->phys[node], child); 62844d30d62SAlan Douglas if (ret) { 62944d30d62SAlan Douglas dev_err(dev, "missing property in node %s\n", 63044d30d62SAlan Douglas child->name); 63144d30d62SAlan Douglas goto put_child; 63244d30d62SAlan Douglas } 63344d30d62SAlan Douglas } 63444d30d62SAlan Douglas 635a43f72aeSKishon Vijay Abraham I sp->num_lanes += sp->phys[node].num_lanes; 636a43f72aeSKishon Vijay Abraham I 63744d30d62SAlan Douglas gphy = devm_phy_create(dev, child, &ops); 63844d30d62SAlan Douglas 63944d30d62SAlan Douglas if (IS_ERR(gphy)) { 64044d30d62SAlan Douglas ret = PTR_ERR(gphy); 64144d30d62SAlan Douglas goto put_child; 64244d30d62SAlan Douglas } 64344d30d62SAlan Douglas sp->phys[node].phy = gphy; 64444d30d62SAlan Douglas phy_set_drvdata(gphy, &sp->phys[node]); 64544d30d62SAlan Douglas 64644d30d62SAlan Douglas node++; 64744d30d62SAlan Douglas } 64844d30d62SAlan Douglas sp->nsubnodes = node; 64944d30d62SAlan Douglas 650a43f72aeSKishon Vijay Abraham I if (sp->num_lanes > SIERRA_MAX_LANES) { 651a43f72aeSKishon Vijay Abraham I dev_err(dev, "Invalid lane configuration\n"); 652a43f72aeSKishon Vijay Abraham I goto put_child2; 653a43f72aeSKishon Vijay Abraham I } 654a43f72aeSKishon Vijay Abraham I 65544d30d62SAlan Douglas /* If more than one subnode, configure the PHY as multilink */ 65644d30d62SAlan Douglas if (!sp->autoconf && sp->nsubnodes > 1) 657380f5708SKishon Vijay Abraham I regmap_field_write(sp->phy_pll_cfg_1, 0x1); 65844d30d62SAlan Douglas 65944d30d62SAlan Douglas pm_runtime_enable(dev); 66044d30d62SAlan Douglas phy_provider = devm_of_phy_provider_register(dev, of_phy_simple_xlate); 66144d30d62SAlan Douglas return PTR_ERR_OR_ZERO(phy_provider); 66244d30d62SAlan Douglas 66344d30d62SAlan Douglas put_child: 66444d30d62SAlan Douglas node++; 66544d30d62SAlan Douglas put_child2: 66644d30d62SAlan Douglas for (i = 0; i < node; i++) 66744d30d62SAlan Douglas reset_control_put(sp->phys[i].lnk_rst); 66844d30d62SAlan Douglas of_node_put(child); 66944d30d62SAlan Douglas clk_disable: 670a0c30cd7SKishon Vijay Abraham I clk_disable_unprepare(sp->input_clks[PHY_CLK]); 67144d30d62SAlan Douglas reset_control_assert(sp->apb_rst); 67244d30d62SAlan Douglas return ret; 67344d30d62SAlan Douglas } 67444d30d62SAlan Douglas 67544d30d62SAlan Douglas static int cdns_sierra_phy_remove(struct platform_device *pdev) 67644d30d62SAlan Douglas { 677748e3456SKishon Vijay Abraham I struct cdns_sierra_phy *phy = platform_get_drvdata(pdev); 67844d30d62SAlan Douglas int i; 67944d30d62SAlan Douglas 68044d30d62SAlan Douglas reset_control_assert(phy->phy_rst); 68144d30d62SAlan Douglas reset_control_assert(phy->apb_rst); 68244d30d62SAlan Douglas pm_runtime_disable(&pdev->dev); 68344d30d62SAlan Douglas 68444d30d62SAlan Douglas /* 68544d30d62SAlan Douglas * The device level resets will be put automatically. 68644d30d62SAlan Douglas * Need to put the subnode resets here though. 68744d30d62SAlan Douglas */ 68844d30d62SAlan Douglas for (i = 0; i < phy->nsubnodes; i++) { 68944d30d62SAlan Douglas reset_control_assert(phy->phys[i].lnk_rst); 69044d30d62SAlan Douglas reset_control_put(phy->phys[i].lnk_rst); 69144d30d62SAlan Douglas } 692*29c2d02aSKishon Vijay Abraham I 693*29c2d02aSKishon Vijay Abraham I clk_disable_unprepare(phy->input_clks[PHY_CLK]); 694*29c2d02aSKishon Vijay Abraham I 69544d30d62SAlan Douglas return 0; 69644d30d62SAlan Douglas } 69744d30d62SAlan Douglas 698871002d7SAnil Varughese /* refclk100MHz_32b_PCIe_cmn_pll_ext_ssc */ 6993cfb0e8eSRikard Falkeborn static const struct cdns_reg_pairs cdns_pcie_cmn_regs_ext_ssc[] = { 700871002d7SAnil Varughese {0x2106, SIERRA_CMN_PLLLC_LF_COEFF_MODE1_PREG}, 701871002d7SAnil Varughese {0x2106, SIERRA_CMN_PLLLC_LF_COEFF_MODE0_PREG}, 702871002d7SAnil Varughese {0x8A06, SIERRA_CMN_PLLLC_BWCAL_MODE1_PREG}, 703871002d7SAnil Varughese {0x8A06, SIERRA_CMN_PLLLC_BWCAL_MODE0_PREG}, 704871002d7SAnil Varughese {0x1B1B, SIERRA_CMN_PLLLC_SS_TIME_STEPSIZE_MODE_PREG} 705871002d7SAnil Varughese }; 706871002d7SAnil Varughese 707871002d7SAnil Varughese /* refclk100MHz_32b_PCIe_ln_ext_ssc */ 7083cfb0e8eSRikard Falkeborn static const struct cdns_reg_pairs cdns_pcie_ln_regs_ext_ssc[] = { 709871002d7SAnil Varughese {0x813E, SIERRA_CLKPATHCTRL_TMR_PREG}, 710871002d7SAnil Varughese {0x8047, SIERRA_RX_CREQ_FLTR_A_MODE3_PREG}, 711871002d7SAnil Varughese {0x808F, SIERRA_RX_CREQ_FLTR_A_MODE2_PREG}, 712871002d7SAnil Varughese {0x808F, SIERRA_RX_CREQ_FLTR_A_MODE1_PREG}, 713871002d7SAnil Varughese {0x808F, SIERRA_RX_CREQ_FLTR_A_MODE0_PREG}, 714871002d7SAnil Varughese {0x033C, SIERRA_RX_CTLE_MAINTENANCE_PREG}, 715871002d7SAnil Varughese {0x44CC, SIERRA_CREQ_EQ_OPEN_EYE_THRESH_PREG} 716871002d7SAnil Varughese }; 717871002d7SAnil Varughese 718871002d7SAnil Varughese /* refclk100MHz_20b_USB_cmn_pll_ext_ssc */ 7193cfb0e8eSRikard Falkeborn static const struct cdns_reg_pairs cdns_usb_cmn_regs_ext_ssc[] = { 720871002d7SAnil Varughese {0x2085, SIERRA_CMN_PLLLC_LF_COEFF_MODE1_PREG}, 721871002d7SAnil Varughese {0x2085, SIERRA_CMN_PLLLC_LF_COEFF_MODE0_PREG}, 722871002d7SAnil Varughese {0x0000, SIERRA_CMN_PLLLC_BWCAL_MODE0_PREG}, 723871002d7SAnil Varughese {0x0000, SIERRA_CMN_PLLLC_SS_TIME_STEPSIZE_MODE_PREG} 724871002d7SAnil Varughese }; 725871002d7SAnil Varughese 726871002d7SAnil Varughese /* refclk100MHz_20b_USB_ln_ext_ssc */ 7273cfb0e8eSRikard Falkeborn static const struct cdns_reg_pairs cdns_usb_ln_regs_ext_ssc[] = { 728aead5fd6SKishon Vijay Abraham I {0xFE0A, SIERRA_DET_STANDEC_A_PREG}, 729aead5fd6SKishon Vijay Abraham I {0x000F, SIERRA_DET_STANDEC_B_PREG}, 7302bcf14caSSanket Parmar {0x55A5, SIERRA_DET_STANDEC_C_PREG}, 731871002d7SAnil Varughese {0x69ad, SIERRA_DET_STANDEC_D_PREG}, 732aead5fd6SKishon Vijay Abraham I {0x0241, SIERRA_DET_STANDEC_E_PREG}, 7332bcf14caSSanket Parmar {0x0110, SIERRA_PSM_LANECAL_DLY_A1_RESETS_PREG}, 734871002d7SAnil Varughese {0x0014, SIERRA_PSM_A0IN_TMR_PREG}, 735aead5fd6SKishon Vijay Abraham I {0xCF00, SIERRA_PSM_DIAG_PREG}, 736aead5fd6SKishon Vijay Abraham I {0x001F, SIERRA_PSC_TX_A0_PREG}, 737aead5fd6SKishon Vijay Abraham I {0x0007, SIERRA_PSC_TX_A1_PREG}, 738aead5fd6SKishon Vijay Abraham I {0x0003, SIERRA_PSC_TX_A2_PREG}, 739aead5fd6SKishon Vijay Abraham I {0x0003, SIERRA_PSC_TX_A3_PREG}, 740aead5fd6SKishon Vijay Abraham I {0x0FFF, SIERRA_PSC_RX_A0_PREG}, 7412bcf14caSSanket Parmar {0x0003, SIERRA_PSC_RX_A1_PREG}, 742aead5fd6SKishon Vijay Abraham I {0x0003, SIERRA_PSC_RX_A2_PREG}, 743aead5fd6SKishon Vijay Abraham I {0x0001, SIERRA_PSC_RX_A3_PREG}, 744aead5fd6SKishon Vijay Abraham I {0x0001, SIERRA_PLLCTRL_SUBRATE_PREG}, 745aead5fd6SKishon Vijay Abraham I {0x0406, SIERRA_PLLCTRL_GEN_D_PREG}, 746871002d7SAnil Varughese {0x5233, SIERRA_PLLCTRL_CPGAIN_MODE_PREG}, 747871002d7SAnil Varughese {0x00CA, SIERRA_CLKPATH_BIASTRIM_PREG}, 748871002d7SAnil Varughese {0x2512, SIERRA_DFE_BIASTRIM_PREG}, 749aead5fd6SKishon Vijay Abraham I {0x0000, SIERRA_DRVCTRL_ATTEN_PREG}, 7502bcf14caSSanket Parmar {0x823E, SIERRA_CLKPATHCTRL_TMR_PREG}, 7512bcf14caSSanket Parmar {0x078F, SIERRA_RX_CREQ_FLTR_A_MODE1_PREG}, 7522bcf14caSSanket Parmar {0x078F, SIERRA_RX_CREQ_FLTR_A_MODE0_PREG}, 753aead5fd6SKishon Vijay Abraham I {0x7B3C, SIERRA_CREQ_CCLKDET_MODE01_PREG}, 7542bcf14caSSanket Parmar {0x023C, SIERRA_RX_CTLE_MAINTENANCE_PREG}, 755aead5fd6SKishon Vijay Abraham I {0x3232, SIERRA_CREQ_FSMCLK_SEL_PREG}, 756871002d7SAnil Varughese {0x0000, SIERRA_CREQ_EQ_CTRL_PREG}, 7572bcf14caSSanket Parmar {0x0000, SIERRA_CREQ_SPARE_PREG}, 758871002d7SAnil Varughese {0xCC44, SIERRA_CREQ_EQ_OPEN_EYE_THRESH_PREG}, 7592bcf14caSSanket Parmar {0x8452, SIERRA_CTLELUT_CTRL_PREG}, 7602bcf14caSSanket Parmar {0x4121, SIERRA_DFE_ECMP_RATESEL_PREG}, 7612bcf14caSSanket Parmar {0x4121, SIERRA_DFE_SMP_RATESEL_PREG}, 7622bcf14caSSanket Parmar {0x0003, SIERRA_DEQ_PHALIGN_CTRL}, 763871002d7SAnil Varughese {0x3200, SIERRA_DEQ_CONCUR_CTRL1_PREG}, 764871002d7SAnil Varughese {0x5064, SIERRA_DEQ_CONCUR_CTRL2_PREG}, 765871002d7SAnil Varughese {0x0030, SIERRA_DEQ_EPIPWR_CTRL2_PREG}, 766871002d7SAnil Varughese {0x0048, SIERRA_DEQ_FAST_MAINT_CYCLES_PREG}, 767871002d7SAnil Varughese {0x5A5A, SIERRA_DEQ_ERRCMP_CTRL_PREG}, 768871002d7SAnil Varughese {0x02F5, SIERRA_DEQ_OFFSET_CTRL_PREG}, 769871002d7SAnil Varughese {0x02F5, SIERRA_DEQ_GAIN_CTRL_PREG}, 7702bcf14caSSanket Parmar {0x9999, SIERRA_DEQ_VGATUNE_CTRL_PREG}, 771871002d7SAnil Varughese {0x0014, SIERRA_DEQ_GLUT0}, 772871002d7SAnil Varughese {0x0014, SIERRA_DEQ_GLUT1}, 773871002d7SAnil Varughese {0x0014, SIERRA_DEQ_GLUT2}, 774871002d7SAnil Varughese {0x0014, SIERRA_DEQ_GLUT3}, 775871002d7SAnil Varughese {0x0014, SIERRA_DEQ_GLUT4}, 776871002d7SAnil Varughese {0x0014, SIERRA_DEQ_GLUT5}, 777871002d7SAnil Varughese {0x0014, SIERRA_DEQ_GLUT6}, 778871002d7SAnil Varughese {0x0014, SIERRA_DEQ_GLUT7}, 779871002d7SAnil Varughese {0x0014, SIERRA_DEQ_GLUT8}, 780871002d7SAnil Varughese {0x0014, SIERRA_DEQ_GLUT9}, 781871002d7SAnil Varughese {0x0014, SIERRA_DEQ_GLUT10}, 782871002d7SAnil Varughese {0x0014, SIERRA_DEQ_GLUT11}, 783871002d7SAnil Varughese {0x0014, SIERRA_DEQ_GLUT12}, 784871002d7SAnil Varughese {0x0014, SIERRA_DEQ_GLUT13}, 785871002d7SAnil Varughese {0x0014, SIERRA_DEQ_GLUT14}, 786871002d7SAnil Varughese {0x0014, SIERRA_DEQ_GLUT15}, 787871002d7SAnil Varughese {0x0014, SIERRA_DEQ_GLUT16}, 788871002d7SAnil Varughese {0x0BAE, SIERRA_DEQ_ALUT0}, 789871002d7SAnil Varughese {0x0AEB, SIERRA_DEQ_ALUT1}, 790871002d7SAnil Varughese {0x0A28, SIERRA_DEQ_ALUT2}, 791871002d7SAnil Varughese {0x0965, SIERRA_DEQ_ALUT3}, 792871002d7SAnil Varughese {0x08A2, SIERRA_DEQ_ALUT4}, 793871002d7SAnil Varughese {0x07DF, SIERRA_DEQ_ALUT5}, 794871002d7SAnil Varughese {0x071C, SIERRA_DEQ_ALUT6}, 795871002d7SAnil Varughese {0x0659, SIERRA_DEQ_ALUT7}, 796871002d7SAnil Varughese {0x0596, SIERRA_DEQ_ALUT8}, 797871002d7SAnil Varughese {0x0514, SIERRA_DEQ_ALUT9}, 798871002d7SAnil Varughese {0x0492, SIERRA_DEQ_ALUT10}, 799871002d7SAnil Varughese {0x0410, SIERRA_DEQ_ALUT11}, 800871002d7SAnil Varughese {0x038E, SIERRA_DEQ_ALUT12}, 801871002d7SAnil Varughese {0x030C, SIERRA_DEQ_ALUT13}, 802871002d7SAnil Varughese {0x03F4, SIERRA_DEQ_DFETAP_CTRL_PREG}, 803871002d7SAnil Varughese {0x0001, SIERRA_DFE_EN_1010_IGNORE_PREG}, 804871002d7SAnil Varughese {0x3C01, SIERRA_DEQ_TAU_CTRL1_FAST_MAINT_PREG}, 805871002d7SAnil Varughese {0x3C40, SIERRA_DEQ_TAU_CTRL1_SLOW_MAINT_PREG}, 806871002d7SAnil Varughese {0x1C08, SIERRA_DEQ_TAU_CTRL2_PREG}, 807871002d7SAnil Varughese {0x0033, SIERRA_DEQ_PICTRL_PREG}, 808871002d7SAnil Varughese {0x0400, SIERRA_CPICAL_TMRVAL_MODE1_PREG}, 809871002d7SAnil Varughese {0x0330, SIERRA_CPICAL_TMRVAL_MODE0_PREG}, 810871002d7SAnil Varughese {0x01FF, SIERRA_CPICAL_PICNT_MODE1_PREG}, 811aead5fd6SKishon Vijay Abraham I {0x0009, SIERRA_CPI_OUTBUF_RATESEL_PREG}, 812871002d7SAnil Varughese {0x3232, SIERRA_CPICAL_RES_STARTCODE_MODE23_PREG}, 813871002d7SAnil Varughese {0x0005, SIERRA_LFPSDET_SUPPORT_PREG}, 814aead5fd6SKishon Vijay Abraham I {0x000F, SIERRA_LFPSFILT_NS_PREG}, 815aead5fd6SKishon Vijay Abraham I {0x0009, SIERRA_LFPSFILT_RD_PREG}, 816aead5fd6SKishon Vijay Abraham I {0x0001, SIERRA_LFPSFILT_MP_PREG}, 8172bcf14caSSanket Parmar {0x6013, SIERRA_SIGDET_SUPPORT_PREG}, 818aead5fd6SKishon Vijay Abraham I {0x8013, SIERRA_SDFILT_H2L_A_PREG}, 819871002d7SAnil Varughese {0x8009, SIERRA_SDFILT_L2H_PREG}, 820871002d7SAnil Varughese {0x0024, SIERRA_RXBUFFER_CTLECTRL_PREG}, 821871002d7SAnil Varughese {0x0020, SIERRA_RXBUFFER_RCDFECTRL_PREG}, 822871002d7SAnil Varughese {0x4243, SIERRA_RXBUFFER_DFECTRL_PREG} 82344d30d62SAlan Douglas }; 82444d30d62SAlan Douglas 82544d30d62SAlan Douglas static const struct cdns_sierra_data cdns_map_sierra = { 82644d30d62SAlan Douglas SIERRA_MACRO_ID, 827380f5708SKishon Vijay Abraham I 0x2, 828380f5708SKishon Vijay Abraham I 0x2, 829871002d7SAnil Varughese ARRAY_SIZE(cdns_pcie_cmn_regs_ext_ssc), 830871002d7SAnil Varughese ARRAY_SIZE(cdns_pcie_ln_regs_ext_ssc), 831871002d7SAnil Varughese ARRAY_SIZE(cdns_usb_cmn_regs_ext_ssc), 832871002d7SAnil Varughese ARRAY_SIZE(cdns_usb_ln_regs_ext_ssc), 833871002d7SAnil Varughese cdns_pcie_cmn_regs_ext_ssc, 834871002d7SAnil Varughese cdns_pcie_ln_regs_ext_ssc, 835871002d7SAnil Varughese cdns_usb_cmn_regs_ext_ssc, 836871002d7SAnil Varughese cdns_usb_ln_regs_ext_ssc, 83744d30d62SAlan Douglas }; 83844d30d62SAlan Douglas 839367da978SKishon Vijay Abraham I static const struct cdns_sierra_data cdns_ti_map_sierra = { 840367da978SKishon Vijay Abraham I SIERRA_MACRO_ID, 841367da978SKishon Vijay Abraham I 0x0, 842367da978SKishon Vijay Abraham I 0x1, 843871002d7SAnil Varughese ARRAY_SIZE(cdns_pcie_cmn_regs_ext_ssc), 844871002d7SAnil Varughese ARRAY_SIZE(cdns_pcie_ln_regs_ext_ssc), 845871002d7SAnil Varughese ARRAY_SIZE(cdns_usb_cmn_regs_ext_ssc), 846871002d7SAnil Varughese ARRAY_SIZE(cdns_usb_ln_regs_ext_ssc), 847871002d7SAnil Varughese cdns_pcie_cmn_regs_ext_ssc, 848871002d7SAnil Varughese cdns_pcie_ln_regs_ext_ssc, 849871002d7SAnil Varughese cdns_usb_cmn_regs_ext_ssc, 850871002d7SAnil Varughese cdns_usb_ln_regs_ext_ssc, 851367da978SKishon Vijay Abraham I }; 852367da978SKishon Vijay Abraham I 85344d30d62SAlan Douglas static const struct of_device_id cdns_sierra_id_table[] = { 85444d30d62SAlan Douglas { 85544d30d62SAlan Douglas .compatible = "cdns,sierra-phy-t0", 85644d30d62SAlan Douglas .data = &cdns_map_sierra, 85744d30d62SAlan Douglas }, 858367da978SKishon Vijay Abraham I { 859367da978SKishon Vijay Abraham I .compatible = "ti,sierra-phy-t0", 860367da978SKishon Vijay Abraham I .data = &cdns_ti_map_sierra, 861367da978SKishon Vijay Abraham I }, 86244d30d62SAlan Douglas {} 86344d30d62SAlan Douglas }; 86444d30d62SAlan Douglas MODULE_DEVICE_TABLE(of, cdns_sierra_id_table); 86544d30d62SAlan Douglas 86644d30d62SAlan Douglas static struct platform_driver cdns_sierra_driver = { 86744d30d62SAlan Douglas .probe = cdns_sierra_phy_probe, 86844d30d62SAlan Douglas .remove = cdns_sierra_phy_remove, 86944d30d62SAlan Douglas .driver = { 87044d30d62SAlan Douglas .name = "cdns-sierra-phy", 87144d30d62SAlan Douglas .of_match_table = cdns_sierra_id_table, 87244d30d62SAlan Douglas }, 87344d30d62SAlan Douglas }; 87444d30d62SAlan Douglas module_platform_driver(cdns_sierra_driver); 87544d30d62SAlan Douglas 87644d30d62SAlan Douglas MODULE_ALIAS("platform:cdns_sierra"); 87744d30d62SAlan Douglas MODULE_AUTHOR("Cadence Design Systems"); 87844d30d62SAlan Douglas MODULE_DESCRIPTION("CDNS sierra phy driver"); 87944d30d62SAlan Douglas MODULE_LICENSE("GPL v2"); 880