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> 1028081b72SKishon Vijay Abraham I #include <linux/clk-provider.h> 1144d30d62SAlan Douglas #include <linux/delay.h> 1244d30d62SAlan Douglas #include <linux/err.h> 1344d30d62SAlan Douglas #include <linux/io.h> 1444d30d62SAlan Douglas #include <linux/module.h> 1544d30d62SAlan Douglas #include <linux/phy/phy.h> 1644d30d62SAlan Douglas #include <linux/platform_device.h> 1744d30d62SAlan Douglas #include <linux/pm_runtime.h> 1844d30d62SAlan Douglas #include <linux/regmap.h> 1944d30d62SAlan Douglas #include <linux/reset.h> 2044d30d62SAlan Douglas #include <linux/slab.h> 2144d30d62SAlan Douglas #include <linux/of.h> 2244d30d62SAlan Douglas #include <linux/of_platform.h> 2344d30d62SAlan Douglas #include <dt-bindings/phy/phy.h> 2428081b72SKishon Vijay Abraham I #include <dt-bindings/phy/phy-cadence.h> 2544d30d62SAlan Douglas 2644d30d62SAlan Douglas /* PHY register offsets */ 27380f5708SKishon Vijay Abraham I #define SIERRA_COMMON_CDB_OFFSET 0x0 28380f5708SKishon Vijay Abraham I #define SIERRA_MACRO_ID_REG 0x0 2928081b72SKishon Vijay Abraham I #define SIERRA_CMN_PLLLC_GEN_PREG 0x42 30871002d7SAnil Varughese #define SIERRA_CMN_PLLLC_MODE_PREG 0x48 31871002d7SAnil Varughese #define SIERRA_CMN_PLLLC_LF_COEFF_MODE1_PREG 0x49 32871002d7SAnil Varughese #define SIERRA_CMN_PLLLC_LF_COEFF_MODE0_PREG 0x4A 33871002d7SAnil Varughese #define SIERRA_CMN_PLLLC_LOCK_CNTSTART_PREG 0x4B 34871002d7SAnil Varughese #define SIERRA_CMN_PLLLC_BWCAL_MODE1_PREG 0x4F 35871002d7SAnil Varughese #define SIERRA_CMN_PLLLC_BWCAL_MODE0_PREG 0x50 36871002d7SAnil Varughese #define SIERRA_CMN_PLLLC_SS_TIME_STEPSIZE_MODE_PREG 0x62 3728081b72SKishon Vijay Abraham I #define SIERRA_CMN_REFRCV_PREG 0x98 3828081b72SKishon Vijay Abraham I #define SIERRA_CMN_REFRCV1_PREG 0xB8 3928081b72SKishon Vijay Abraham I #define SIERRA_CMN_PLLLC1_GEN_PREG 0xC2 40380f5708SKishon Vijay Abraham I 41380f5708SKishon Vijay Abraham I #define SIERRA_LANE_CDB_OFFSET(ln, block_offset, reg_offset) \ 42380f5708SKishon Vijay Abraham I ((0x4000 << (block_offset)) + \ 43380f5708SKishon Vijay Abraham I (((ln) << 9) << (reg_offset))) 44aead5fd6SKishon Vijay Abraham I 45aead5fd6SKishon Vijay Abraham I #define SIERRA_DET_STANDEC_A_PREG 0x000 46aead5fd6SKishon Vijay Abraham I #define SIERRA_DET_STANDEC_B_PREG 0x001 47aead5fd6SKishon Vijay Abraham I #define SIERRA_DET_STANDEC_C_PREG 0x002 48aead5fd6SKishon Vijay Abraham I #define SIERRA_DET_STANDEC_D_PREG 0x003 49aead5fd6SKishon Vijay Abraham I #define SIERRA_DET_STANDEC_E_PREG 0x004 50871002d7SAnil Varughese #define SIERRA_PSM_LANECAL_DLY_A1_RESETS_PREG 0x008 51871002d7SAnil Varughese #define SIERRA_PSM_A0IN_TMR_PREG 0x009 52aead5fd6SKishon Vijay Abraham I #define SIERRA_PSM_DIAG_PREG 0x015 53aead5fd6SKishon Vijay Abraham I #define SIERRA_PSC_TX_A0_PREG 0x028 54aead5fd6SKishon Vijay Abraham I #define SIERRA_PSC_TX_A1_PREG 0x029 55aead5fd6SKishon Vijay Abraham I #define SIERRA_PSC_TX_A2_PREG 0x02A 56aead5fd6SKishon Vijay Abraham I #define SIERRA_PSC_TX_A3_PREG 0x02B 57aead5fd6SKishon Vijay Abraham I #define SIERRA_PSC_RX_A0_PREG 0x030 58aead5fd6SKishon Vijay Abraham I #define SIERRA_PSC_RX_A1_PREG 0x031 59aead5fd6SKishon Vijay Abraham I #define SIERRA_PSC_RX_A2_PREG 0x032 60aead5fd6SKishon Vijay Abraham I #define SIERRA_PSC_RX_A3_PREG 0x033 61aead5fd6SKishon Vijay Abraham I #define SIERRA_PLLCTRL_SUBRATE_PREG 0x03A 62aead5fd6SKishon Vijay Abraham I #define SIERRA_PLLCTRL_GEN_D_PREG 0x03E 63871002d7SAnil Varughese #define SIERRA_PLLCTRL_CPGAIN_MODE_PREG 0x03F 64adc4bd6fSKishon Vijay Abraham I #define SIERRA_PLLCTRL_STATUS_PREG 0x044 65871002d7SAnil Varughese #define SIERRA_CLKPATH_BIASTRIM_PREG 0x04B 66871002d7SAnil Varughese #define SIERRA_DFE_BIASTRIM_PREG 0x04C 67aead5fd6SKishon Vijay Abraham I #define SIERRA_DRVCTRL_ATTEN_PREG 0x06A 68aead5fd6SKishon Vijay Abraham I #define SIERRA_CLKPATHCTRL_TMR_PREG 0x081 69871002d7SAnil Varughese #define SIERRA_RX_CREQ_FLTR_A_MODE3_PREG 0x085 70871002d7SAnil Varughese #define SIERRA_RX_CREQ_FLTR_A_MODE2_PREG 0x086 71aead5fd6SKishon Vijay Abraham I #define SIERRA_RX_CREQ_FLTR_A_MODE1_PREG 0x087 72aead5fd6SKishon Vijay Abraham I #define SIERRA_RX_CREQ_FLTR_A_MODE0_PREG 0x088 73aead5fd6SKishon Vijay Abraham I #define SIERRA_CREQ_CCLKDET_MODE01_PREG 0x08E 74aead5fd6SKishon Vijay Abraham I #define SIERRA_RX_CTLE_MAINTENANCE_PREG 0x091 75aead5fd6SKishon Vijay Abraham I #define SIERRA_CREQ_FSMCLK_SEL_PREG 0x092 76871002d7SAnil Varughese #define SIERRA_CREQ_EQ_CTRL_PREG 0x093 77871002d7SAnil Varughese #define SIERRA_CREQ_SPARE_PREG 0x096 78871002d7SAnil Varughese #define SIERRA_CREQ_EQ_OPEN_EYE_THRESH_PREG 0x097 79aead5fd6SKishon Vijay Abraham I #define SIERRA_CTLELUT_CTRL_PREG 0x098 80aead5fd6SKishon Vijay Abraham I #define SIERRA_DFE_ECMP_RATESEL_PREG 0x0C0 81aead5fd6SKishon Vijay Abraham I #define SIERRA_DFE_SMP_RATESEL_PREG 0x0C1 82871002d7SAnil Varughese #define SIERRA_DEQ_PHALIGN_CTRL 0x0C4 83871002d7SAnil Varughese #define SIERRA_DEQ_CONCUR_CTRL1_PREG 0x0C8 84871002d7SAnil Varughese #define SIERRA_DEQ_CONCUR_CTRL2_PREG 0x0C9 85871002d7SAnil Varughese #define SIERRA_DEQ_EPIPWR_CTRL2_PREG 0x0CD 86871002d7SAnil Varughese #define SIERRA_DEQ_FAST_MAINT_CYCLES_PREG 0x0CE 87871002d7SAnil Varughese #define SIERRA_DEQ_ERRCMP_CTRL_PREG 0x0D0 88871002d7SAnil Varughese #define SIERRA_DEQ_OFFSET_CTRL_PREG 0x0D8 89871002d7SAnil Varughese #define SIERRA_DEQ_GAIN_CTRL_PREG 0x0E0 90aead5fd6SKishon Vijay Abraham I #define SIERRA_DEQ_VGATUNE_CTRL_PREG 0x0E1 91871002d7SAnil Varughese #define SIERRA_DEQ_GLUT0 0x0E8 92871002d7SAnil Varughese #define SIERRA_DEQ_GLUT1 0x0E9 93871002d7SAnil Varughese #define SIERRA_DEQ_GLUT2 0x0EA 94871002d7SAnil Varughese #define SIERRA_DEQ_GLUT3 0x0EB 95871002d7SAnil Varughese #define SIERRA_DEQ_GLUT4 0x0EC 96871002d7SAnil Varughese #define SIERRA_DEQ_GLUT5 0x0ED 97871002d7SAnil Varughese #define SIERRA_DEQ_GLUT6 0x0EE 98871002d7SAnil Varughese #define SIERRA_DEQ_GLUT7 0x0EF 99871002d7SAnil Varughese #define SIERRA_DEQ_GLUT8 0x0F0 100871002d7SAnil Varughese #define SIERRA_DEQ_GLUT9 0x0F1 101871002d7SAnil Varughese #define SIERRA_DEQ_GLUT10 0x0F2 102871002d7SAnil Varughese #define SIERRA_DEQ_GLUT11 0x0F3 103871002d7SAnil Varughese #define SIERRA_DEQ_GLUT12 0x0F4 104871002d7SAnil Varughese #define SIERRA_DEQ_GLUT13 0x0F5 105871002d7SAnil Varughese #define SIERRA_DEQ_GLUT14 0x0F6 106871002d7SAnil Varughese #define SIERRA_DEQ_GLUT15 0x0F7 107871002d7SAnil Varughese #define SIERRA_DEQ_GLUT16 0x0F8 108871002d7SAnil Varughese #define SIERRA_DEQ_ALUT0 0x108 109871002d7SAnil Varughese #define SIERRA_DEQ_ALUT1 0x109 110871002d7SAnil Varughese #define SIERRA_DEQ_ALUT2 0x10A 111871002d7SAnil Varughese #define SIERRA_DEQ_ALUT3 0x10B 112871002d7SAnil Varughese #define SIERRA_DEQ_ALUT4 0x10C 113871002d7SAnil Varughese #define SIERRA_DEQ_ALUT5 0x10D 114871002d7SAnil Varughese #define SIERRA_DEQ_ALUT6 0x10E 115871002d7SAnil Varughese #define SIERRA_DEQ_ALUT7 0x10F 116871002d7SAnil Varughese #define SIERRA_DEQ_ALUT8 0x110 117871002d7SAnil Varughese #define SIERRA_DEQ_ALUT9 0x111 118871002d7SAnil Varughese #define SIERRA_DEQ_ALUT10 0x112 119871002d7SAnil Varughese #define SIERRA_DEQ_ALUT11 0x113 120871002d7SAnil Varughese #define SIERRA_DEQ_ALUT12 0x114 121871002d7SAnil Varughese #define SIERRA_DEQ_ALUT13 0x115 122871002d7SAnil Varughese #define SIERRA_DEQ_DFETAP_CTRL_PREG 0x128 123871002d7SAnil Varughese #define SIERRA_DFE_EN_1010_IGNORE_PREG 0x134 124871002d7SAnil Varughese #define SIERRA_DEQ_TAU_CTRL1_SLOW_MAINT_PREG 0x150 125871002d7SAnil Varughese #define SIERRA_DEQ_TAU_CTRL2_PREG 0x151 126871002d7SAnil Varughese #define SIERRA_DEQ_PICTRL_PREG 0x161 127871002d7SAnil Varughese #define SIERRA_CPICAL_TMRVAL_MODE1_PREG 0x170 128871002d7SAnil Varughese #define SIERRA_CPICAL_TMRVAL_MODE0_PREG 0x171 129871002d7SAnil Varughese #define SIERRA_CPICAL_PICNT_MODE1_PREG 0x174 130aead5fd6SKishon Vijay Abraham I #define SIERRA_CPI_OUTBUF_RATESEL_PREG 0x17C 131871002d7SAnil Varughese #define SIERRA_CPICAL_RES_STARTCODE_MODE23_PREG 0x183 132871002d7SAnil Varughese #define SIERRA_LFPSDET_SUPPORT_PREG 0x188 133aead5fd6SKishon Vijay Abraham I #define SIERRA_LFPSFILT_NS_PREG 0x18A 134aead5fd6SKishon Vijay Abraham I #define SIERRA_LFPSFILT_RD_PREG 0x18B 135aead5fd6SKishon Vijay Abraham I #define SIERRA_LFPSFILT_MP_PREG 0x18C 136871002d7SAnil Varughese #define SIERRA_SIGDET_SUPPORT_PREG 0x190 137aead5fd6SKishon Vijay Abraham I #define SIERRA_SDFILT_H2L_A_PREG 0x191 138871002d7SAnil Varughese #define SIERRA_SDFILT_L2H_PREG 0x193 139871002d7SAnil Varughese #define SIERRA_RXBUFFER_CTLECTRL_PREG 0x19E 140871002d7SAnil Varughese #define SIERRA_RXBUFFER_RCDFECTRL_PREG 0x19F 141871002d7SAnil Varughese #define SIERRA_RXBUFFER_DFECTRL_PREG 0x1A0 142871002d7SAnil Varughese #define SIERRA_DEQ_TAU_CTRL1_FAST_MAINT_PREG 0x14F 143871002d7SAnil Varughese #define SIERRA_DEQ_TAU_CTRL1_SLOW_MAINT_PREG 0x150 144380f5708SKishon Vijay Abraham I 145380f5708SKishon Vijay Abraham I #define SIERRA_PHY_CONFIG_CTRL_OFFSET(block_offset) \ 146380f5708SKishon Vijay Abraham I (0xc000 << (block_offset)) 147380f5708SKishon Vijay Abraham I #define SIERRA_PHY_PLL_CFG 0xe 14844d30d62SAlan Douglas 14944d30d62SAlan Douglas #define SIERRA_MACRO_ID 0x00007364 150a43f72aeSKishon Vijay Abraham I #define SIERRA_MAX_LANES 16 151adc4bd6fSKishon Vijay Abraham I #define PLL_LOCK_TIME 100000 15244d30d62SAlan Douglas 15328081b72SKishon Vijay Abraham I #define CDNS_SIERRA_OUTPUT_CLOCKS 2 15428081b72SKishon Vijay Abraham I #define CDNS_SIERRA_INPUT_CLOCKS 5 155a0c30cd7SKishon Vijay Abraham I enum cdns_sierra_clock_input { 156a0c30cd7SKishon Vijay Abraham I PHY_CLK, 157a0c30cd7SKishon Vijay Abraham I CMN_REFCLK_DIG_DIV, 158a0c30cd7SKishon Vijay Abraham I CMN_REFCLK1_DIG_DIV, 15928081b72SKishon Vijay Abraham I PLL0_REFCLK, 16028081b72SKishon Vijay Abraham I PLL1_REFCLK, 161a0c30cd7SKishon Vijay Abraham I }; 162a0c30cd7SKishon Vijay Abraham I 16328081b72SKishon Vijay Abraham I #define SIERRA_NUM_CMN_PLLC 2 16428081b72SKishon Vijay Abraham I #define SIERRA_NUM_CMN_PLLC_PARENTS 2 16528081b72SKishon Vijay Abraham I 166380f5708SKishon Vijay Abraham I static const struct reg_field macro_id_type = 167380f5708SKishon Vijay Abraham I REG_FIELD(SIERRA_MACRO_ID_REG, 0, 15); 168380f5708SKishon Vijay Abraham I static const struct reg_field phy_pll_cfg_1 = 169380f5708SKishon Vijay Abraham I REG_FIELD(SIERRA_PHY_PLL_CFG, 1, 1); 170adc4bd6fSKishon Vijay Abraham I static const struct reg_field pllctrl_lock = 171adc4bd6fSKishon Vijay Abraham I REG_FIELD(SIERRA_PLLCTRL_STATUS_PREG, 0, 0); 172380f5708SKishon Vijay Abraham I 17328081b72SKishon Vijay Abraham I static const char * const clk_names[] = { 17428081b72SKishon Vijay Abraham I [CDNS_SIERRA_PLL_CMNLC] = "pll_cmnlc", 17528081b72SKishon Vijay Abraham I [CDNS_SIERRA_PLL_CMNLC1] = "pll_cmnlc1", 17628081b72SKishon Vijay Abraham I }; 17728081b72SKishon Vijay Abraham I 17828081b72SKishon Vijay Abraham I enum cdns_sierra_cmn_plllc { 17928081b72SKishon Vijay Abraham I CMN_PLLLC, 18028081b72SKishon Vijay Abraham I CMN_PLLLC1, 18128081b72SKishon Vijay Abraham I }; 18228081b72SKishon Vijay Abraham I 18328081b72SKishon Vijay Abraham I struct cdns_sierra_pll_mux_reg_fields { 18428081b72SKishon Vijay Abraham I struct reg_field pfdclk_sel_preg; 18528081b72SKishon Vijay Abraham I struct reg_field plllc1en_field; 18628081b72SKishon Vijay Abraham I struct reg_field termen_field; 18728081b72SKishon Vijay Abraham I }; 18828081b72SKishon Vijay Abraham I 18928081b72SKishon Vijay Abraham I static const struct cdns_sierra_pll_mux_reg_fields cmn_plllc_pfdclk1_sel_preg[] = { 19028081b72SKishon Vijay Abraham I [CMN_PLLLC] = { 19128081b72SKishon Vijay Abraham I .pfdclk_sel_preg = REG_FIELD(SIERRA_CMN_PLLLC_GEN_PREG, 1, 1), 19228081b72SKishon Vijay Abraham I .plllc1en_field = REG_FIELD(SIERRA_CMN_REFRCV1_PREG, 8, 8), 19328081b72SKishon Vijay Abraham I .termen_field = REG_FIELD(SIERRA_CMN_REFRCV1_PREG, 0, 0), 19428081b72SKishon Vijay Abraham I }, 19528081b72SKishon Vijay Abraham I [CMN_PLLLC1] = { 19628081b72SKishon Vijay Abraham I .pfdclk_sel_preg = REG_FIELD(SIERRA_CMN_PLLLC1_GEN_PREG, 1, 1), 19728081b72SKishon Vijay Abraham I .plllc1en_field = REG_FIELD(SIERRA_CMN_REFRCV_PREG, 8, 8), 19828081b72SKishon Vijay Abraham I .termen_field = REG_FIELD(SIERRA_CMN_REFRCV_PREG, 0, 0), 19928081b72SKishon Vijay Abraham I }, 20028081b72SKishon Vijay Abraham I }; 20128081b72SKishon Vijay Abraham I 20228081b72SKishon Vijay Abraham I struct cdns_sierra_pll_mux { 20328081b72SKishon Vijay Abraham I struct clk_hw hw; 20428081b72SKishon Vijay Abraham I struct regmap_field *pfdclk_sel_preg; 20528081b72SKishon Vijay Abraham I struct regmap_field *plllc1en_field; 20628081b72SKishon Vijay Abraham I struct regmap_field *termen_field; 20728081b72SKishon Vijay Abraham I struct clk_init_data clk_data; 20828081b72SKishon Vijay Abraham I }; 20928081b72SKishon Vijay Abraham I 21028081b72SKishon Vijay Abraham I #define to_cdns_sierra_pll_mux(_hw) \ 21128081b72SKishon Vijay Abraham I container_of(_hw, struct cdns_sierra_pll_mux, hw) 21228081b72SKishon Vijay Abraham I 21328081b72SKishon Vijay Abraham I static const int pll_mux_parent_index[][SIERRA_NUM_CMN_PLLC_PARENTS] = { 21428081b72SKishon Vijay Abraham I [CMN_PLLLC] = { PLL0_REFCLK, PLL1_REFCLK }, 21528081b72SKishon Vijay Abraham I [CMN_PLLLC1] = { PLL1_REFCLK, PLL0_REFCLK }, 21628081b72SKishon Vijay Abraham I }; 21728081b72SKishon Vijay Abraham I 21828081b72SKishon Vijay Abraham I static u32 cdns_sierra_pll_mux_table[] = { 0, 1 }; 21928081b72SKishon Vijay Abraham I 22044d30d62SAlan Douglas struct cdns_sierra_inst { 22144d30d62SAlan Douglas struct phy *phy; 22244d30d62SAlan Douglas u32 phy_type; 22344d30d62SAlan Douglas u32 num_lanes; 22444d30d62SAlan Douglas u32 mlane; 22544d30d62SAlan Douglas struct reset_control *lnk_rst; 22644d30d62SAlan Douglas }; 22744d30d62SAlan Douglas 22844d30d62SAlan Douglas struct cdns_reg_pairs { 22944d30d62SAlan Douglas u16 val; 23044d30d62SAlan Douglas u32 off; 23144d30d62SAlan Douglas }; 23244d30d62SAlan Douglas 23344d30d62SAlan Douglas struct cdns_sierra_data { 23444d30d62SAlan Douglas u32 id_value; 235380f5708SKishon Vijay Abraham I u8 block_offset_shift; 236380f5708SKishon Vijay Abraham I u8 reg_offset_shift; 237871002d7SAnil Varughese u32 pcie_cmn_regs; 238871002d7SAnil Varughese u32 pcie_ln_regs; 239871002d7SAnil Varughese u32 usb_cmn_regs; 240871002d7SAnil Varughese u32 usb_ln_regs; 2413cfb0e8eSRikard Falkeborn const struct cdns_reg_pairs *pcie_cmn_vals; 2423cfb0e8eSRikard Falkeborn const struct cdns_reg_pairs *pcie_ln_vals; 2433cfb0e8eSRikard Falkeborn const struct cdns_reg_pairs *usb_cmn_vals; 2443cfb0e8eSRikard Falkeborn const struct cdns_reg_pairs *usb_ln_vals; 24544d30d62SAlan Douglas }; 24644d30d62SAlan Douglas 247380f5708SKishon Vijay Abraham I struct cdns_regmap_cdb_context { 24844d30d62SAlan Douglas struct device *dev; 24944d30d62SAlan Douglas void __iomem *base; 250380f5708SKishon Vijay Abraham I u8 reg_offset_shift; 251380f5708SKishon Vijay Abraham I }; 252380f5708SKishon Vijay Abraham I 253380f5708SKishon Vijay Abraham I struct cdns_sierra_phy { 254380f5708SKishon Vijay Abraham I struct device *dev; 255380f5708SKishon Vijay Abraham I struct regmap *regmap; 25644d30d62SAlan Douglas struct cdns_sierra_data *init_data; 25744d30d62SAlan Douglas struct cdns_sierra_inst phys[SIERRA_MAX_LANES]; 25844d30d62SAlan Douglas struct reset_control *phy_rst; 25944d30d62SAlan Douglas struct reset_control *apb_rst; 260380f5708SKishon Vijay Abraham I struct regmap *regmap_lane_cdb[SIERRA_MAX_LANES]; 261380f5708SKishon Vijay Abraham I struct regmap *regmap_phy_config_ctrl; 262380f5708SKishon Vijay Abraham I struct regmap *regmap_common_cdb; 263380f5708SKishon Vijay Abraham I struct regmap_field *macro_id_type; 264380f5708SKishon Vijay Abraham I struct regmap_field *phy_pll_cfg_1; 265adc4bd6fSKishon Vijay Abraham I struct regmap_field *pllctrl_lock[SIERRA_MAX_LANES]; 26628081b72SKishon Vijay Abraham I struct regmap_field *cmn_refrcv_refclk_plllc1en_preg[SIERRA_NUM_CMN_PLLC]; 26728081b72SKishon Vijay Abraham I struct regmap_field *cmn_refrcv_refclk_termen_preg[SIERRA_NUM_CMN_PLLC]; 26828081b72SKishon Vijay Abraham I struct regmap_field *cmn_plllc_pfdclk1_sel_preg[SIERRA_NUM_CMN_PLLC]; 269a0c30cd7SKishon Vijay Abraham I struct clk *input_clks[CDNS_SIERRA_INPUT_CLOCKS]; 27044d30d62SAlan Douglas int nsubnodes; 271a43f72aeSKishon Vijay Abraham I u32 num_lanes; 27244d30d62SAlan Douglas bool autoconf; 27328081b72SKishon Vijay Abraham I struct clk_onecell_data clk_data; 27428081b72SKishon Vijay Abraham I struct clk *output_clks[CDNS_SIERRA_OUTPUT_CLOCKS]; 27544d30d62SAlan Douglas }; 27644d30d62SAlan Douglas 277380f5708SKishon Vijay Abraham I static int cdns_regmap_write(void *context, unsigned int reg, unsigned int val) 278380f5708SKishon Vijay Abraham I { 279380f5708SKishon Vijay Abraham I struct cdns_regmap_cdb_context *ctx = context; 280380f5708SKishon Vijay Abraham I u32 offset = reg << ctx->reg_offset_shift; 281380f5708SKishon Vijay Abraham I 282380f5708SKishon Vijay Abraham I writew(val, ctx->base + offset); 283380f5708SKishon Vijay Abraham I 284380f5708SKishon Vijay Abraham I return 0; 285380f5708SKishon Vijay Abraham I } 286380f5708SKishon Vijay Abraham I 287380f5708SKishon Vijay Abraham I static int cdns_regmap_read(void *context, unsigned int reg, unsigned int *val) 288380f5708SKishon Vijay Abraham I { 289380f5708SKishon Vijay Abraham I struct cdns_regmap_cdb_context *ctx = context; 290380f5708SKishon Vijay Abraham I u32 offset = reg << ctx->reg_offset_shift; 291380f5708SKishon Vijay Abraham I 292380f5708SKishon Vijay Abraham I *val = readw(ctx->base + offset); 293380f5708SKishon Vijay Abraham I return 0; 294380f5708SKishon Vijay Abraham I } 295380f5708SKishon Vijay Abraham I 296380f5708SKishon Vijay Abraham I #define SIERRA_LANE_CDB_REGMAP_CONF(n) \ 297380f5708SKishon Vijay Abraham I { \ 298380f5708SKishon Vijay Abraham I .name = "sierra_lane" n "_cdb", \ 299380f5708SKishon Vijay Abraham I .reg_stride = 1, \ 300380f5708SKishon Vijay Abraham I .fast_io = true, \ 301380f5708SKishon Vijay Abraham I .reg_write = cdns_regmap_write, \ 302380f5708SKishon Vijay Abraham I .reg_read = cdns_regmap_read, \ 303380f5708SKishon Vijay Abraham I } 304380f5708SKishon Vijay Abraham I 3053cfb0e8eSRikard Falkeborn static const struct regmap_config cdns_sierra_lane_cdb_config[] = { 306380f5708SKishon Vijay Abraham I SIERRA_LANE_CDB_REGMAP_CONF("0"), 307380f5708SKishon Vijay Abraham I SIERRA_LANE_CDB_REGMAP_CONF("1"), 308380f5708SKishon Vijay Abraham I SIERRA_LANE_CDB_REGMAP_CONF("2"), 309380f5708SKishon Vijay Abraham I SIERRA_LANE_CDB_REGMAP_CONF("3"), 310a43f72aeSKishon Vijay Abraham I SIERRA_LANE_CDB_REGMAP_CONF("4"), 311a43f72aeSKishon Vijay Abraham I SIERRA_LANE_CDB_REGMAP_CONF("5"), 312a43f72aeSKishon Vijay Abraham I SIERRA_LANE_CDB_REGMAP_CONF("6"), 313a43f72aeSKishon Vijay Abraham I SIERRA_LANE_CDB_REGMAP_CONF("7"), 314a43f72aeSKishon Vijay Abraham I SIERRA_LANE_CDB_REGMAP_CONF("8"), 315a43f72aeSKishon Vijay Abraham I SIERRA_LANE_CDB_REGMAP_CONF("9"), 316a43f72aeSKishon Vijay Abraham I SIERRA_LANE_CDB_REGMAP_CONF("10"), 317a43f72aeSKishon Vijay Abraham I SIERRA_LANE_CDB_REGMAP_CONF("11"), 318a43f72aeSKishon Vijay Abraham I SIERRA_LANE_CDB_REGMAP_CONF("12"), 319a43f72aeSKishon Vijay Abraham I SIERRA_LANE_CDB_REGMAP_CONF("13"), 320a43f72aeSKishon Vijay Abraham I SIERRA_LANE_CDB_REGMAP_CONF("14"), 321a43f72aeSKishon Vijay Abraham I SIERRA_LANE_CDB_REGMAP_CONF("15"), 322380f5708SKishon Vijay Abraham I }; 323380f5708SKishon Vijay Abraham I 3243cfb0e8eSRikard Falkeborn static const struct regmap_config cdns_sierra_common_cdb_config = { 325380f5708SKishon Vijay Abraham I .name = "sierra_common_cdb", 326380f5708SKishon Vijay Abraham I .reg_stride = 1, 327380f5708SKishon Vijay Abraham I .fast_io = true, 328380f5708SKishon Vijay Abraham I .reg_write = cdns_regmap_write, 329380f5708SKishon Vijay Abraham I .reg_read = cdns_regmap_read, 330380f5708SKishon Vijay Abraham I }; 331380f5708SKishon Vijay Abraham I 3323cfb0e8eSRikard Falkeborn static const struct regmap_config cdns_sierra_phy_config_ctrl_config = { 333380f5708SKishon Vijay Abraham I .name = "sierra_phy_config_ctrl", 334380f5708SKishon Vijay Abraham I .reg_stride = 1, 335380f5708SKishon Vijay Abraham I .fast_io = true, 336380f5708SKishon Vijay Abraham I .reg_write = cdns_regmap_write, 337380f5708SKishon Vijay Abraham I .reg_read = cdns_regmap_read, 338380f5708SKishon Vijay Abraham I }; 339380f5708SKishon Vijay Abraham I 340cedcc2e2SKishon Vijay Abraham I static int cdns_sierra_phy_init(struct phy *gphy) 34144d30d62SAlan Douglas { 34244d30d62SAlan Douglas struct cdns_sierra_inst *ins = phy_get_drvdata(gphy); 34344d30d62SAlan Douglas struct cdns_sierra_phy *phy = dev_get_drvdata(gphy->dev.parent); 34480f96fb1SColin Ian King struct regmap *regmap; 34544d30d62SAlan Douglas int i, j; 3463cfb0e8eSRikard Falkeborn const struct cdns_reg_pairs *cmn_vals, *ln_vals; 347871002d7SAnil Varughese u32 num_cmn_regs, num_ln_regs; 34844d30d62SAlan Douglas 349cedcc2e2SKishon Vijay Abraham I /* Initialise the PHY registers, unless auto configured */ 350cedcc2e2SKishon Vijay Abraham I if (phy->autoconf) 351cedcc2e2SKishon Vijay Abraham I return 0; 352cedcc2e2SKishon Vijay Abraham I 353a0c30cd7SKishon Vijay Abraham I clk_set_rate(phy->input_clks[CMN_REFCLK_DIG_DIV], 25000000); 354a0c30cd7SKishon Vijay Abraham I clk_set_rate(phy->input_clks[CMN_REFCLK1_DIG_DIV], 25000000); 35544d30d62SAlan Douglas if (ins->phy_type == PHY_TYPE_PCIE) { 356871002d7SAnil Varughese num_cmn_regs = phy->init_data->pcie_cmn_regs; 357871002d7SAnil Varughese num_ln_regs = phy->init_data->pcie_ln_regs; 358871002d7SAnil Varughese cmn_vals = phy->init_data->pcie_cmn_vals; 359871002d7SAnil Varughese ln_vals = phy->init_data->pcie_ln_vals; 36044d30d62SAlan Douglas } else if (ins->phy_type == PHY_TYPE_USB3) { 361871002d7SAnil Varughese num_cmn_regs = phy->init_data->usb_cmn_regs; 362871002d7SAnil Varughese num_ln_regs = phy->init_data->usb_ln_regs; 363871002d7SAnil Varughese cmn_vals = phy->init_data->usb_cmn_vals; 364871002d7SAnil Varughese ln_vals = phy->init_data->usb_ln_vals; 36544d30d62SAlan Douglas } else { 366cedcc2e2SKishon Vijay Abraham I return -EINVAL; 36744d30d62SAlan Douglas } 368871002d7SAnil Varughese 369871002d7SAnil Varughese regmap = phy->regmap_common_cdb; 370871002d7SAnil Varughese for (j = 0; j < num_cmn_regs ; j++) 371871002d7SAnil Varughese regmap_write(regmap, cmn_vals[j].off, cmn_vals[j].val); 372871002d7SAnil Varughese 373380f5708SKishon Vijay Abraham I for (i = 0; i < ins->num_lanes; i++) { 374871002d7SAnil Varughese for (j = 0; j < num_ln_regs ; j++) { 375380f5708SKishon Vijay Abraham I regmap = phy->regmap_lane_cdb[i + ins->mlane]; 376871002d7SAnil Varughese regmap_write(regmap, ln_vals[j].off, ln_vals[j].val); 377380f5708SKishon Vijay Abraham I } 378380f5708SKishon Vijay Abraham I } 379cedcc2e2SKishon Vijay Abraham I 380cedcc2e2SKishon Vijay Abraham I return 0; 38144d30d62SAlan Douglas } 38244d30d62SAlan Douglas 38344d30d62SAlan Douglas static int cdns_sierra_phy_on(struct phy *gphy) 38444d30d62SAlan Douglas { 385adc4bd6fSKishon Vijay Abraham I struct cdns_sierra_phy *sp = dev_get_drvdata(gphy->dev.parent); 38644d30d62SAlan Douglas struct cdns_sierra_inst *ins = phy_get_drvdata(gphy); 387adc4bd6fSKishon Vijay Abraham I struct device *dev = sp->dev; 388adc4bd6fSKishon Vijay Abraham I u32 val; 389adc4bd6fSKishon Vijay Abraham I int ret; 39044d30d62SAlan Douglas 3915b4f5757SKishon Vijay Abraham I ret = reset_control_deassert(sp->phy_rst); 3925b4f5757SKishon Vijay Abraham I if (ret) { 3935b4f5757SKishon Vijay Abraham I dev_err(dev, "Failed to take the PHY out of reset\n"); 3945b4f5757SKishon Vijay Abraham I return ret; 3955b4f5757SKishon Vijay Abraham I } 3965b4f5757SKishon Vijay Abraham I 39744d30d62SAlan Douglas /* Take the PHY lane group out of reset */ 398adc4bd6fSKishon Vijay Abraham I ret = reset_control_deassert(ins->lnk_rst); 399adc4bd6fSKishon Vijay Abraham I if (ret) { 400adc4bd6fSKishon Vijay Abraham I dev_err(dev, "Failed to take the PHY lane out of reset\n"); 401adc4bd6fSKishon Vijay Abraham I return ret; 402adc4bd6fSKishon Vijay Abraham I } 403adc4bd6fSKishon Vijay Abraham I 404adc4bd6fSKishon Vijay Abraham I ret = regmap_field_read_poll_timeout(sp->pllctrl_lock[ins->mlane], 405adc4bd6fSKishon Vijay Abraham I val, val, 1000, PLL_LOCK_TIME); 406adc4bd6fSKishon Vijay Abraham I if (ret < 0) 407adc4bd6fSKishon Vijay Abraham I dev_err(dev, "PLL lock of lane failed\n"); 408adc4bd6fSKishon Vijay Abraham I 409adc4bd6fSKishon Vijay Abraham I return ret; 41044d30d62SAlan Douglas } 41144d30d62SAlan Douglas 41244d30d62SAlan Douglas static int cdns_sierra_phy_off(struct phy *gphy) 41344d30d62SAlan Douglas { 41444d30d62SAlan Douglas struct cdns_sierra_inst *ins = phy_get_drvdata(gphy); 41544d30d62SAlan Douglas 41644d30d62SAlan Douglas return reset_control_assert(ins->lnk_rst); 41744d30d62SAlan Douglas } 41844d30d62SAlan Douglas 4197904e15bSRoger Quadros static int cdns_sierra_phy_reset(struct phy *gphy) 4207904e15bSRoger Quadros { 4217904e15bSRoger Quadros struct cdns_sierra_phy *sp = dev_get_drvdata(gphy->dev.parent); 4227904e15bSRoger Quadros 4237904e15bSRoger Quadros reset_control_assert(sp->phy_rst); 4247904e15bSRoger Quadros reset_control_deassert(sp->phy_rst); 4257904e15bSRoger Quadros return 0; 4267904e15bSRoger Quadros }; 4277904e15bSRoger Quadros 42844d30d62SAlan Douglas static const struct phy_ops ops = { 429cedcc2e2SKishon Vijay Abraham I .init = cdns_sierra_phy_init, 43044d30d62SAlan Douglas .power_on = cdns_sierra_phy_on, 43144d30d62SAlan Douglas .power_off = cdns_sierra_phy_off, 4327904e15bSRoger Quadros .reset = cdns_sierra_phy_reset, 43344d30d62SAlan Douglas .owner = THIS_MODULE, 43444d30d62SAlan Douglas }; 43544d30d62SAlan Douglas 43628081b72SKishon Vijay Abraham I static u8 cdns_sierra_pll_mux_get_parent(struct clk_hw *hw) 43728081b72SKishon Vijay Abraham I { 43828081b72SKishon Vijay Abraham I struct cdns_sierra_pll_mux *mux = to_cdns_sierra_pll_mux(hw); 43928081b72SKishon Vijay Abraham I struct regmap_field *field = mux->pfdclk_sel_preg; 44028081b72SKishon Vijay Abraham I unsigned int val; 44128081b72SKishon Vijay Abraham I 44228081b72SKishon Vijay Abraham I regmap_field_read(field, &val); 44328081b72SKishon Vijay Abraham I return clk_mux_val_to_index(hw, cdns_sierra_pll_mux_table, 0, val); 44428081b72SKishon Vijay Abraham I } 44528081b72SKishon Vijay Abraham I 44628081b72SKishon Vijay Abraham I static int cdns_sierra_pll_mux_set_parent(struct clk_hw *hw, u8 index) 44728081b72SKishon Vijay Abraham I { 44828081b72SKishon Vijay Abraham I struct cdns_sierra_pll_mux *mux = to_cdns_sierra_pll_mux(hw); 44928081b72SKishon Vijay Abraham I struct regmap_field *plllc1en_field = mux->plllc1en_field; 45028081b72SKishon Vijay Abraham I struct regmap_field *termen_field = mux->termen_field; 45128081b72SKishon Vijay Abraham I struct regmap_field *field = mux->pfdclk_sel_preg; 45228081b72SKishon Vijay Abraham I int val, ret; 45328081b72SKishon Vijay Abraham I 45428081b72SKishon Vijay Abraham I ret = regmap_field_write(plllc1en_field, 0); 45528081b72SKishon Vijay Abraham I ret |= regmap_field_write(termen_field, 0); 45628081b72SKishon Vijay Abraham I if (index == 1) { 45728081b72SKishon Vijay Abraham I ret |= regmap_field_write(plllc1en_field, 1); 45828081b72SKishon Vijay Abraham I ret |= regmap_field_write(termen_field, 1); 45928081b72SKishon Vijay Abraham I } 46028081b72SKishon Vijay Abraham I 46128081b72SKishon Vijay Abraham I val = cdns_sierra_pll_mux_table[index]; 46228081b72SKishon Vijay Abraham I ret |= regmap_field_write(field, val); 46328081b72SKishon Vijay Abraham I 46428081b72SKishon Vijay Abraham I return ret; 46528081b72SKishon Vijay Abraham I } 46628081b72SKishon Vijay Abraham I 46728081b72SKishon Vijay Abraham I static const struct clk_ops cdns_sierra_pll_mux_ops = { 46828081b72SKishon Vijay Abraham I .set_parent = cdns_sierra_pll_mux_set_parent, 46928081b72SKishon Vijay Abraham I .get_parent = cdns_sierra_pll_mux_get_parent, 47028081b72SKishon Vijay Abraham I }; 47128081b72SKishon Vijay Abraham I 47228081b72SKishon Vijay Abraham I static int cdns_sierra_pll_mux_register(struct cdns_sierra_phy *sp, 47328081b72SKishon Vijay Abraham I struct regmap_field *pfdclk1_sel_field, 47428081b72SKishon Vijay Abraham I struct regmap_field *plllc1en_field, 47528081b72SKishon Vijay Abraham I struct regmap_field *termen_field, 47628081b72SKishon Vijay Abraham I int clk_index) 47728081b72SKishon Vijay Abraham I { 47828081b72SKishon Vijay Abraham I struct cdns_sierra_pll_mux *mux; 47928081b72SKishon Vijay Abraham I struct device *dev = sp->dev; 48028081b72SKishon Vijay Abraham I struct clk_init_data *init; 48128081b72SKishon Vijay Abraham I const char **parent_names; 48228081b72SKishon Vijay Abraham I unsigned int num_parents; 48328081b72SKishon Vijay Abraham I char clk_name[100]; 48428081b72SKishon Vijay Abraham I struct clk *clk; 48528081b72SKishon Vijay Abraham I int i; 48628081b72SKishon Vijay Abraham I 48728081b72SKishon Vijay Abraham I mux = devm_kzalloc(dev, sizeof(*mux), GFP_KERNEL); 48828081b72SKishon Vijay Abraham I if (!mux) 48928081b72SKishon Vijay Abraham I return -ENOMEM; 49028081b72SKishon Vijay Abraham I 49128081b72SKishon Vijay Abraham I num_parents = SIERRA_NUM_CMN_PLLC_PARENTS; 49228081b72SKishon Vijay Abraham I parent_names = devm_kzalloc(dev, (sizeof(char *) * num_parents), GFP_KERNEL); 49328081b72SKishon Vijay Abraham I if (!parent_names) 49428081b72SKishon Vijay Abraham I return -ENOMEM; 49528081b72SKishon Vijay Abraham I 49628081b72SKishon Vijay Abraham I for (i = 0; i < num_parents; i++) { 49728081b72SKishon Vijay Abraham I clk = sp->input_clks[pll_mux_parent_index[clk_index][i]]; 49828081b72SKishon Vijay Abraham I if (IS_ERR_OR_NULL(clk)) { 49928081b72SKishon Vijay Abraham I dev_err(dev, "No parent clock for derived_refclk\n"); 50028081b72SKishon Vijay Abraham I return PTR_ERR(clk); 50128081b72SKishon Vijay Abraham I } 50228081b72SKishon Vijay Abraham I parent_names[i] = __clk_get_name(clk); 50328081b72SKishon Vijay Abraham I } 50428081b72SKishon Vijay Abraham I 50528081b72SKishon Vijay Abraham I snprintf(clk_name, sizeof(clk_name), "%s_%s", dev_name(dev), clk_names[clk_index]); 50628081b72SKishon Vijay Abraham I 50728081b72SKishon Vijay Abraham I init = &mux->clk_data; 50828081b72SKishon Vijay Abraham I 50928081b72SKishon Vijay Abraham I init->ops = &cdns_sierra_pll_mux_ops; 51028081b72SKishon Vijay Abraham I init->flags = CLK_SET_RATE_NO_REPARENT; 51128081b72SKishon Vijay Abraham I init->parent_names = parent_names; 51228081b72SKishon Vijay Abraham I init->num_parents = num_parents; 51328081b72SKishon Vijay Abraham I init->name = clk_name; 51428081b72SKishon Vijay Abraham I 51528081b72SKishon Vijay Abraham I mux->pfdclk_sel_preg = pfdclk1_sel_field; 51628081b72SKishon Vijay Abraham I mux->plllc1en_field = plllc1en_field; 51728081b72SKishon Vijay Abraham I mux->termen_field = termen_field; 51828081b72SKishon Vijay Abraham I mux->hw.init = init; 51928081b72SKishon Vijay Abraham I 52028081b72SKishon Vijay Abraham I clk = devm_clk_register(dev, &mux->hw); 52128081b72SKishon Vijay Abraham I if (IS_ERR(clk)) 52228081b72SKishon Vijay Abraham I return PTR_ERR(clk); 52328081b72SKishon Vijay Abraham I 52428081b72SKishon Vijay Abraham I sp->output_clks[clk_index] = clk; 52528081b72SKishon Vijay Abraham I 52628081b72SKishon Vijay Abraham I return 0; 52728081b72SKishon Vijay Abraham I } 52828081b72SKishon Vijay Abraham I 52928081b72SKishon Vijay Abraham I static int cdns_sierra_phy_register_pll_mux(struct cdns_sierra_phy *sp) 53028081b72SKishon Vijay Abraham I { 53128081b72SKishon Vijay Abraham I struct regmap_field *pfdclk1_sel_field; 53228081b72SKishon Vijay Abraham I struct regmap_field *plllc1en_field; 53328081b72SKishon Vijay Abraham I struct regmap_field *termen_field; 53428081b72SKishon Vijay Abraham I struct device *dev = sp->dev; 53528081b72SKishon Vijay Abraham I int ret = 0, i, clk_index; 53628081b72SKishon Vijay Abraham I 53728081b72SKishon Vijay Abraham I clk_index = CDNS_SIERRA_PLL_CMNLC; 53828081b72SKishon Vijay Abraham I for (i = 0; i < SIERRA_NUM_CMN_PLLC; i++, clk_index++) { 53928081b72SKishon Vijay Abraham I pfdclk1_sel_field = sp->cmn_plllc_pfdclk1_sel_preg[i]; 54028081b72SKishon Vijay Abraham I plllc1en_field = sp->cmn_refrcv_refclk_plllc1en_preg[i]; 54128081b72SKishon Vijay Abraham I termen_field = sp->cmn_refrcv_refclk_termen_preg[i]; 54228081b72SKishon Vijay Abraham I 54328081b72SKishon Vijay Abraham I ret = cdns_sierra_pll_mux_register(sp, pfdclk1_sel_field, plllc1en_field, 54428081b72SKishon Vijay Abraham I termen_field, clk_index); 54528081b72SKishon Vijay Abraham I if (ret) { 54628081b72SKishon Vijay Abraham I dev_err(dev, "Fail to register cmn plllc mux\n"); 54728081b72SKishon Vijay Abraham I return ret; 54828081b72SKishon Vijay Abraham I } 54928081b72SKishon Vijay Abraham I } 55028081b72SKishon Vijay Abraham I 55128081b72SKishon Vijay Abraham I return 0; 55228081b72SKishon Vijay Abraham I } 55328081b72SKishon Vijay Abraham I 55428081b72SKishon Vijay Abraham I static void cdns_sierra_clk_unregister(struct cdns_sierra_phy *sp) 55528081b72SKishon Vijay Abraham I { 55628081b72SKishon Vijay Abraham I struct device *dev = sp->dev; 55728081b72SKishon Vijay Abraham I struct device_node *node = dev->of_node; 55828081b72SKishon Vijay Abraham I 55928081b72SKishon Vijay Abraham I of_clk_del_provider(node); 56028081b72SKishon Vijay Abraham I } 56128081b72SKishon Vijay Abraham I 56228081b72SKishon Vijay Abraham I static int cdns_sierra_clk_register(struct cdns_sierra_phy *sp) 56328081b72SKishon Vijay Abraham I { 56428081b72SKishon Vijay Abraham I struct device *dev = sp->dev; 56528081b72SKishon Vijay Abraham I struct device_node *node = dev->of_node; 56628081b72SKishon Vijay Abraham I int ret; 56728081b72SKishon Vijay Abraham I 56828081b72SKishon Vijay Abraham I ret = cdns_sierra_phy_register_pll_mux(sp); 56928081b72SKishon Vijay Abraham I if (ret) { 57028081b72SKishon Vijay Abraham I dev_err(dev, "Failed to pll mux clocks\n"); 57128081b72SKishon Vijay Abraham I return ret; 57228081b72SKishon Vijay Abraham I } 57328081b72SKishon Vijay Abraham I 57428081b72SKishon Vijay Abraham I sp->clk_data.clks = sp->output_clks; 57528081b72SKishon Vijay Abraham I sp->clk_data.clk_num = CDNS_SIERRA_OUTPUT_CLOCKS; 57628081b72SKishon Vijay Abraham I ret = of_clk_add_provider(node, of_clk_src_onecell_get, &sp->clk_data); 57728081b72SKishon Vijay Abraham I if (ret) 57828081b72SKishon Vijay Abraham I dev_err(dev, "Failed to add clock provider: %s\n", node->name); 57928081b72SKishon Vijay Abraham I 58028081b72SKishon Vijay Abraham I return ret; 58128081b72SKishon Vijay Abraham I } 58228081b72SKishon Vijay Abraham I 58344d30d62SAlan Douglas static int cdns_sierra_get_optional(struct cdns_sierra_inst *inst, 58444d30d62SAlan Douglas struct device_node *child) 58544d30d62SAlan Douglas { 58644d30d62SAlan Douglas if (of_property_read_u32(child, "reg", &inst->mlane)) 58744d30d62SAlan Douglas return -EINVAL; 58844d30d62SAlan Douglas 58944d30d62SAlan Douglas if (of_property_read_u32(child, "cdns,num-lanes", &inst->num_lanes)) 59044d30d62SAlan Douglas return -EINVAL; 59144d30d62SAlan Douglas 59244d30d62SAlan Douglas if (of_property_read_u32(child, "cdns,phy-type", &inst->phy_type)) 59344d30d62SAlan Douglas return -EINVAL; 59444d30d62SAlan Douglas 59544d30d62SAlan Douglas return 0; 59644d30d62SAlan Douglas } 59744d30d62SAlan Douglas 59844d30d62SAlan Douglas static const struct of_device_id cdns_sierra_id_table[]; 59944d30d62SAlan Douglas 600380f5708SKishon Vijay Abraham I static struct regmap *cdns_regmap_init(struct device *dev, void __iomem *base, 601380f5708SKishon Vijay Abraham I u32 block_offset, u8 reg_offset_shift, 602380f5708SKishon Vijay Abraham I const struct regmap_config *config) 603380f5708SKishon Vijay Abraham I { 604380f5708SKishon Vijay Abraham I struct cdns_regmap_cdb_context *ctx; 605380f5708SKishon Vijay Abraham I 606380f5708SKishon Vijay Abraham I ctx = devm_kzalloc(dev, sizeof(*ctx), GFP_KERNEL); 607380f5708SKishon Vijay Abraham I if (!ctx) 608380f5708SKishon Vijay Abraham I return ERR_PTR(-ENOMEM); 609380f5708SKishon Vijay Abraham I 610380f5708SKishon Vijay Abraham I ctx->dev = dev; 611380f5708SKishon Vijay Abraham I ctx->base = base + block_offset; 612380f5708SKishon Vijay Abraham I ctx->reg_offset_shift = reg_offset_shift; 613380f5708SKishon Vijay Abraham I 614380f5708SKishon Vijay Abraham I return devm_regmap_init(dev, NULL, ctx, config); 615380f5708SKishon Vijay Abraham I } 616380f5708SKishon Vijay Abraham I 617380f5708SKishon Vijay Abraham I static int cdns_regfield_init(struct cdns_sierra_phy *sp) 618380f5708SKishon Vijay Abraham I { 619380f5708SKishon Vijay Abraham I struct device *dev = sp->dev; 620380f5708SKishon Vijay Abraham I struct regmap_field *field; 62128081b72SKishon Vijay Abraham I struct reg_field reg_field; 622380f5708SKishon Vijay Abraham I struct regmap *regmap; 623adc4bd6fSKishon Vijay Abraham I int i; 624380f5708SKishon Vijay Abraham I 625380f5708SKishon Vijay Abraham I regmap = sp->regmap_common_cdb; 626380f5708SKishon Vijay Abraham I field = devm_regmap_field_alloc(dev, regmap, macro_id_type); 627380f5708SKishon Vijay Abraham I if (IS_ERR(field)) { 628380f5708SKishon Vijay Abraham I dev_err(dev, "MACRO_ID_TYPE reg field init failed\n"); 629380f5708SKishon Vijay Abraham I return PTR_ERR(field); 630380f5708SKishon Vijay Abraham I } 631380f5708SKishon Vijay Abraham I sp->macro_id_type = field; 632380f5708SKishon Vijay Abraham I 63328081b72SKishon Vijay Abraham I for (i = 0; i < SIERRA_NUM_CMN_PLLC; i++) { 63428081b72SKishon Vijay Abraham I reg_field = cmn_plllc_pfdclk1_sel_preg[i].pfdclk_sel_preg; 63528081b72SKishon Vijay Abraham I field = devm_regmap_field_alloc(dev, regmap, reg_field); 63628081b72SKishon Vijay Abraham I if (IS_ERR(field)) { 63728081b72SKishon Vijay Abraham I dev_err(dev, "PLLLC%d_PFDCLK1_SEL failed\n", i); 63828081b72SKishon Vijay Abraham I return PTR_ERR(field); 63928081b72SKishon Vijay Abraham I } 64028081b72SKishon Vijay Abraham I sp->cmn_plllc_pfdclk1_sel_preg[i] = field; 64128081b72SKishon Vijay Abraham I 64228081b72SKishon Vijay Abraham I reg_field = cmn_plllc_pfdclk1_sel_preg[i].plllc1en_field; 64328081b72SKishon Vijay Abraham I field = devm_regmap_field_alloc(dev, regmap, reg_field); 64428081b72SKishon Vijay Abraham I if (IS_ERR(field)) { 64528081b72SKishon Vijay Abraham I dev_err(dev, "REFRCV%d_REFCLK_PLLLC1EN failed\n", i); 64628081b72SKishon Vijay Abraham I return PTR_ERR(field); 64728081b72SKishon Vijay Abraham I } 64828081b72SKishon Vijay Abraham I sp->cmn_refrcv_refclk_plllc1en_preg[i] = field; 64928081b72SKishon Vijay Abraham I 65028081b72SKishon Vijay Abraham I reg_field = cmn_plllc_pfdclk1_sel_preg[i].termen_field; 65128081b72SKishon Vijay Abraham I field = devm_regmap_field_alloc(dev, regmap, reg_field); 65228081b72SKishon Vijay Abraham I if (IS_ERR(field)) { 65328081b72SKishon Vijay Abraham I dev_err(dev, "REFRCV%d_REFCLK_TERMEN failed\n", i); 65428081b72SKishon Vijay Abraham I return PTR_ERR(field); 65528081b72SKishon Vijay Abraham I } 65628081b72SKishon Vijay Abraham I sp->cmn_refrcv_refclk_termen_preg[i] = field; 65728081b72SKishon Vijay Abraham I } 65828081b72SKishon Vijay Abraham I 659380f5708SKishon Vijay Abraham I regmap = sp->regmap_phy_config_ctrl; 660380f5708SKishon Vijay Abraham I field = devm_regmap_field_alloc(dev, regmap, phy_pll_cfg_1); 661380f5708SKishon Vijay Abraham I if (IS_ERR(field)) { 662380f5708SKishon Vijay Abraham I dev_err(dev, "PHY_PLL_CFG_1 reg field init failed\n"); 663380f5708SKishon Vijay Abraham I return PTR_ERR(field); 664380f5708SKishon Vijay Abraham I } 665380f5708SKishon Vijay Abraham I sp->phy_pll_cfg_1 = field; 666380f5708SKishon Vijay Abraham I 667adc4bd6fSKishon Vijay Abraham I for (i = 0; i < SIERRA_MAX_LANES; i++) { 668adc4bd6fSKishon Vijay Abraham I regmap = sp->regmap_lane_cdb[i]; 669adc4bd6fSKishon Vijay Abraham I field = devm_regmap_field_alloc(dev, regmap, pllctrl_lock); 670adc4bd6fSKishon Vijay Abraham I if (IS_ERR(field)) { 671adc4bd6fSKishon Vijay Abraham I dev_err(dev, "P%d_ENABLE reg field init failed\n", i); 672adc4bd6fSKishon Vijay Abraham I return PTR_ERR(field); 673adc4bd6fSKishon Vijay Abraham I } 674adc4bd6fSKishon Vijay Abraham I sp->pllctrl_lock[i] = field; 675adc4bd6fSKishon Vijay Abraham I } 676adc4bd6fSKishon Vijay Abraham I 677380f5708SKishon Vijay Abraham I return 0; 678380f5708SKishon Vijay Abraham I } 679380f5708SKishon Vijay Abraham I 680380f5708SKishon Vijay Abraham I static int cdns_regmap_init_blocks(struct cdns_sierra_phy *sp, 681380f5708SKishon Vijay Abraham I void __iomem *base, u8 block_offset_shift, 682380f5708SKishon Vijay Abraham I u8 reg_offset_shift) 683380f5708SKishon Vijay Abraham I { 684380f5708SKishon Vijay Abraham I struct device *dev = sp->dev; 685380f5708SKishon Vijay Abraham I struct regmap *regmap; 686380f5708SKishon Vijay Abraham I u32 block_offset; 687380f5708SKishon Vijay Abraham I int i; 688380f5708SKishon Vijay Abraham I 689380f5708SKishon Vijay Abraham I for (i = 0; i < SIERRA_MAX_LANES; i++) { 690380f5708SKishon Vijay Abraham I block_offset = SIERRA_LANE_CDB_OFFSET(i, block_offset_shift, 691380f5708SKishon Vijay Abraham I reg_offset_shift); 692380f5708SKishon Vijay Abraham I regmap = cdns_regmap_init(dev, base, block_offset, 693380f5708SKishon Vijay Abraham I reg_offset_shift, 694380f5708SKishon Vijay Abraham I &cdns_sierra_lane_cdb_config[i]); 695380f5708SKishon Vijay Abraham I if (IS_ERR(regmap)) { 696380f5708SKishon Vijay Abraham I dev_err(dev, "Failed to init lane CDB regmap\n"); 697380f5708SKishon Vijay Abraham I return PTR_ERR(regmap); 698380f5708SKishon Vijay Abraham I } 699380f5708SKishon Vijay Abraham I sp->regmap_lane_cdb[i] = regmap; 700380f5708SKishon Vijay Abraham I } 701380f5708SKishon Vijay Abraham I 702380f5708SKishon Vijay Abraham I regmap = cdns_regmap_init(dev, base, SIERRA_COMMON_CDB_OFFSET, 703380f5708SKishon Vijay Abraham I reg_offset_shift, 704380f5708SKishon Vijay Abraham I &cdns_sierra_common_cdb_config); 705380f5708SKishon Vijay Abraham I if (IS_ERR(regmap)) { 706380f5708SKishon Vijay Abraham I dev_err(dev, "Failed to init common CDB regmap\n"); 707380f5708SKishon Vijay Abraham I return PTR_ERR(regmap); 708380f5708SKishon Vijay Abraham I } 709380f5708SKishon Vijay Abraham I sp->regmap_common_cdb = regmap; 710380f5708SKishon Vijay Abraham I 711380f5708SKishon Vijay Abraham I block_offset = SIERRA_PHY_CONFIG_CTRL_OFFSET(block_offset_shift); 712380f5708SKishon Vijay Abraham I regmap = cdns_regmap_init(dev, base, block_offset, reg_offset_shift, 713380f5708SKishon Vijay Abraham I &cdns_sierra_phy_config_ctrl_config); 714380f5708SKishon Vijay Abraham I if (IS_ERR(regmap)) { 715380f5708SKishon Vijay Abraham I dev_err(dev, "Failed to init PHY config and control regmap\n"); 716380f5708SKishon Vijay Abraham I return PTR_ERR(regmap); 717380f5708SKishon Vijay Abraham I } 718380f5708SKishon Vijay Abraham I sp->regmap_phy_config_ctrl = regmap; 719380f5708SKishon Vijay Abraham I 720380f5708SKishon Vijay Abraham I return 0; 721380f5708SKishon Vijay Abraham I } 722380f5708SKishon Vijay Abraham I 7237e016cbcSKishon Vijay Abraham I static int cdns_sierra_phy_get_clocks(struct cdns_sierra_phy *sp, 7247e016cbcSKishon Vijay Abraham I struct device *dev) 7257e016cbcSKishon Vijay Abraham I { 7267e016cbcSKishon Vijay Abraham I struct clk *clk; 7277e016cbcSKishon Vijay Abraham I int ret; 7287e016cbcSKishon Vijay Abraham I 7297e016cbcSKishon Vijay Abraham I clk = devm_clk_get_optional(dev, "phy_clk"); 7307e016cbcSKishon Vijay Abraham I if (IS_ERR(clk)) { 7317e016cbcSKishon Vijay Abraham I dev_err(dev, "failed to get clock phy_clk\n"); 7327e016cbcSKishon Vijay Abraham I return PTR_ERR(clk); 7337e016cbcSKishon Vijay Abraham I } 734a0c30cd7SKishon Vijay Abraham I sp->input_clks[PHY_CLK] = clk; 7357e016cbcSKishon Vijay Abraham I 7367e016cbcSKishon Vijay Abraham I clk = devm_clk_get_optional(dev, "cmn_refclk_dig_div"); 7377e016cbcSKishon Vijay Abraham I if (IS_ERR(clk)) { 7387e016cbcSKishon Vijay Abraham I dev_err(dev, "cmn_refclk_dig_div clock not found\n"); 7397e016cbcSKishon Vijay Abraham I ret = PTR_ERR(clk); 7407e016cbcSKishon Vijay Abraham I return ret; 7417e016cbcSKishon Vijay Abraham I } 742a0c30cd7SKishon Vijay Abraham I sp->input_clks[CMN_REFCLK_DIG_DIV] = clk; 7437e016cbcSKishon Vijay Abraham I 7447e016cbcSKishon Vijay Abraham I clk = devm_clk_get_optional(dev, "cmn_refclk1_dig_div"); 7457e016cbcSKishon Vijay Abraham I if (IS_ERR(clk)) { 7467e016cbcSKishon Vijay Abraham I dev_err(dev, "cmn_refclk1_dig_div clock not found\n"); 7477e016cbcSKishon Vijay Abraham I ret = PTR_ERR(clk); 7487e016cbcSKishon Vijay Abraham I return ret; 7497e016cbcSKishon Vijay Abraham I } 750a0c30cd7SKishon Vijay Abraham I sp->input_clks[CMN_REFCLK1_DIG_DIV] = clk; 7517e016cbcSKishon Vijay Abraham I 75228081b72SKishon Vijay Abraham I clk = devm_clk_get_optional(dev, "pll0_refclk"); 75328081b72SKishon Vijay Abraham I if (IS_ERR(clk)) { 75428081b72SKishon Vijay Abraham I dev_err(dev, "pll0_refclk clock not found\n"); 75528081b72SKishon Vijay Abraham I ret = PTR_ERR(clk); 75628081b72SKishon Vijay Abraham I return ret; 75728081b72SKishon Vijay Abraham I } 75828081b72SKishon Vijay Abraham I sp->input_clks[PLL0_REFCLK] = clk; 75928081b72SKishon Vijay Abraham I 76028081b72SKishon Vijay Abraham I clk = devm_clk_get_optional(dev, "pll1_refclk"); 76128081b72SKishon Vijay Abraham I if (IS_ERR(clk)) { 76228081b72SKishon Vijay Abraham I dev_err(dev, "pll1_refclk clock not found\n"); 76328081b72SKishon Vijay Abraham I ret = PTR_ERR(clk); 76428081b72SKishon Vijay Abraham I return ret; 76528081b72SKishon Vijay Abraham I } 76628081b72SKishon Vijay Abraham I sp->input_clks[PLL1_REFCLK] = clk; 76728081b72SKishon Vijay Abraham I 7687e016cbcSKishon Vijay Abraham I return 0; 7697e016cbcSKishon Vijay Abraham I } 7707e016cbcSKishon Vijay Abraham I 771*1436ec30SKishon Vijay Abraham I static int cdns_sierra_phy_enable_clocks(struct cdns_sierra_phy *sp) 772*1436ec30SKishon Vijay Abraham I { 773*1436ec30SKishon Vijay Abraham I int ret; 774*1436ec30SKishon Vijay Abraham I 775*1436ec30SKishon Vijay Abraham I ret = clk_prepare_enable(sp->input_clks[PHY_CLK]); 776*1436ec30SKishon Vijay Abraham I if (ret) 777*1436ec30SKishon Vijay Abraham I return ret; 778*1436ec30SKishon Vijay Abraham I 779*1436ec30SKishon Vijay Abraham I ret = clk_prepare_enable(sp->output_clks[CDNS_SIERRA_PLL_CMNLC]); 780*1436ec30SKishon Vijay Abraham I if (ret) 781*1436ec30SKishon Vijay Abraham I goto err_pll_cmnlc; 782*1436ec30SKishon Vijay Abraham I 783*1436ec30SKishon Vijay Abraham I ret = clk_prepare_enable(sp->output_clks[CDNS_SIERRA_PLL_CMNLC1]); 784*1436ec30SKishon Vijay Abraham I if (ret) 785*1436ec30SKishon Vijay Abraham I goto err_pll_cmnlc1; 786*1436ec30SKishon Vijay Abraham I 787*1436ec30SKishon Vijay Abraham I return 0; 788*1436ec30SKishon Vijay Abraham I 789*1436ec30SKishon Vijay Abraham I err_pll_cmnlc1: 790*1436ec30SKishon Vijay Abraham I clk_disable_unprepare(sp->output_clks[CDNS_SIERRA_PLL_CMNLC]); 791*1436ec30SKishon Vijay Abraham I 792*1436ec30SKishon Vijay Abraham I err_pll_cmnlc: 793*1436ec30SKishon Vijay Abraham I clk_disable_unprepare(sp->input_clks[PHY_CLK]); 794*1436ec30SKishon Vijay Abraham I 795*1436ec30SKishon Vijay Abraham I return ret; 796*1436ec30SKishon Vijay Abraham I } 797*1436ec30SKishon Vijay Abraham I 798*1436ec30SKishon Vijay Abraham I static void cdns_sierra_phy_disable_clocks(struct cdns_sierra_phy *sp) 799*1436ec30SKishon Vijay Abraham I { 800*1436ec30SKishon Vijay Abraham I clk_disable_unprepare(sp->output_clks[CDNS_SIERRA_PLL_CMNLC1]); 801*1436ec30SKishon Vijay Abraham I clk_disable_unprepare(sp->output_clks[CDNS_SIERRA_PLL_CMNLC]); 802*1436ec30SKishon Vijay Abraham I clk_disable_unprepare(sp->input_clks[PHY_CLK]); 803*1436ec30SKishon Vijay Abraham I } 804*1436ec30SKishon Vijay Abraham I 8051d5f40e0SKishon Vijay Abraham I static int cdns_sierra_phy_get_resets(struct cdns_sierra_phy *sp, 8061d5f40e0SKishon Vijay Abraham I struct device *dev) 8071d5f40e0SKishon Vijay Abraham I { 8081d5f40e0SKishon Vijay Abraham I struct reset_control *rst; 8091d5f40e0SKishon Vijay Abraham I 81015b0b82dSKishon Vijay Abraham I rst = devm_reset_control_get_exclusive(dev, "sierra_reset"); 8111d5f40e0SKishon Vijay Abraham I if (IS_ERR(rst)) { 8121d5f40e0SKishon Vijay Abraham I dev_err(dev, "failed to get reset\n"); 8131d5f40e0SKishon Vijay Abraham I return PTR_ERR(rst); 8141d5f40e0SKishon Vijay Abraham I } 8151d5f40e0SKishon Vijay Abraham I sp->phy_rst = rst; 8161d5f40e0SKishon Vijay Abraham I 81715b0b82dSKishon Vijay Abraham I rst = devm_reset_control_get_optional_exclusive(dev, "sierra_apb"); 8181d5f40e0SKishon Vijay Abraham I if (IS_ERR(rst)) { 8191d5f40e0SKishon Vijay Abraham I dev_err(dev, "failed to get apb reset\n"); 8201d5f40e0SKishon Vijay Abraham I return PTR_ERR(rst); 8211d5f40e0SKishon Vijay Abraham I } 8221d5f40e0SKishon Vijay Abraham I sp->apb_rst = rst; 8231d5f40e0SKishon Vijay Abraham I 8241d5f40e0SKishon Vijay Abraham I return 0; 8251d5f40e0SKishon Vijay Abraham I } 8261d5f40e0SKishon Vijay Abraham I 82744d30d62SAlan Douglas static int cdns_sierra_phy_probe(struct platform_device *pdev) 82844d30d62SAlan Douglas { 82944d30d62SAlan Douglas struct cdns_sierra_phy *sp; 83044d30d62SAlan Douglas struct phy_provider *phy_provider; 83144d30d62SAlan Douglas struct device *dev = &pdev->dev; 83244d30d62SAlan Douglas const struct of_device_id *match; 833380f5708SKishon Vijay Abraham I struct cdns_sierra_data *data; 834380f5708SKishon Vijay Abraham I unsigned int id_value; 83544d30d62SAlan Douglas int i, ret, node = 0; 836380f5708SKishon Vijay Abraham I void __iomem *base; 83744d30d62SAlan Douglas struct device_node *dn = dev->of_node, *child; 83844d30d62SAlan Douglas 83944d30d62SAlan Douglas if (of_get_child_count(dn) == 0) 84044d30d62SAlan Douglas return -ENODEV; 84144d30d62SAlan Douglas 842380f5708SKishon Vijay Abraham I /* Get init data for this PHY */ 843380f5708SKishon Vijay Abraham I match = of_match_device(cdns_sierra_id_table, dev); 844380f5708SKishon Vijay Abraham I if (!match) 845380f5708SKishon Vijay Abraham I return -EINVAL; 846380f5708SKishon Vijay Abraham I 847380f5708SKishon Vijay Abraham I data = (struct cdns_sierra_data *)match->data; 848380f5708SKishon Vijay Abraham I 84944d30d62SAlan Douglas sp = devm_kzalloc(dev, sizeof(*sp), GFP_KERNEL); 85044d30d62SAlan Douglas if (!sp) 85144d30d62SAlan Douglas return -ENOMEM; 85244d30d62SAlan Douglas dev_set_drvdata(dev, sp); 85344d30d62SAlan Douglas sp->dev = dev; 854380f5708SKishon Vijay Abraham I sp->init_data = data; 85544d30d62SAlan Douglas 856fa629094SChunfeng Yun base = devm_platform_ioremap_resource(pdev, 0); 857380f5708SKishon Vijay Abraham I if (IS_ERR(base)) { 85844d30d62SAlan Douglas dev_err(dev, "missing \"reg\"\n"); 859380f5708SKishon Vijay Abraham I return PTR_ERR(base); 86044d30d62SAlan Douglas } 86144d30d62SAlan Douglas 862380f5708SKishon Vijay Abraham I ret = cdns_regmap_init_blocks(sp, base, data->block_offset_shift, 863380f5708SKishon Vijay Abraham I data->reg_offset_shift); 864380f5708SKishon Vijay Abraham I if (ret) 865380f5708SKishon Vijay Abraham I return ret; 866380f5708SKishon Vijay Abraham I 867380f5708SKishon Vijay Abraham I ret = cdns_regfield_init(sp); 868380f5708SKishon Vijay Abraham I if (ret) 869380f5708SKishon Vijay Abraham I return ret; 87044d30d62SAlan Douglas 87144d30d62SAlan Douglas platform_set_drvdata(pdev, sp); 87244d30d62SAlan Douglas 8737e016cbcSKishon Vijay Abraham I ret = cdns_sierra_phy_get_clocks(sp, dev); 8747e016cbcSKishon Vijay Abraham I if (ret) 8757e016cbcSKishon Vijay Abraham I return ret; 87644d30d62SAlan Douglas 87728081b72SKishon Vijay Abraham I ret = cdns_sierra_clk_register(sp); 8781d5f40e0SKishon Vijay Abraham I if (ret) 8791d5f40e0SKishon Vijay Abraham I return ret; 88044d30d62SAlan Douglas 88128081b72SKishon Vijay Abraham I ret = cdns_sierra_phy_get_resets(sp, dev); 88228081b72SKishon Vijay Abraham I if (ret) 88328081b72SKishon Vijay Abraham I goto unregister_clk; 88428081b72SKishon Vijay Abraham I 885*1436ec30SKishon Vijay Abraham I ret = cdns_sierra_phy_enable_clocks(sp); 88644d30d62SAlan Douglas if (ret) 88728081b72SKishon Vijay Abraham I goto unregister_clk; 88844d30d62SAlan Douglas 88944d30d62SAlan Douglas /* Enable APB */ 89044d30d62SAlan Douglas reset_control_deassert(sp->apb_rst); 89144d30d62SAlan Douglas 89244d30d62SAlan Douglas /* Check that PHY is present */ 893380f5708SKishon Vijay Abraham I regmap_field_read(sp->macro_id_type, &id_value); 894380f5708SKishon Vijay Abraham I if (sp->init_data->id_value != id_value) { 89544d30d62SAlan Douglas ret = -EINVAL; 89644d30d62SAlan Douglas goto clk_disable; 89744d30d62SAlan Douglas } 89844d30d62SAlan Douglas 89944d30d62SAlan Douglas sp->autoconf = of_property_read_bool(dn, "cdns,autoconf"); 90044d30d62SAlan Douglas 90144d30d62SAlan Douglas for_each_available_child_of_node(dn, child) { 90244d30d62SAlan Douglas struct phy *gphy; 90344d30d62SAlan Douglas 90403ada5a3SKishon Vijay Abraham I if (!(of_node_name_eq(child, "phy") || 90503ada5a3SKishon Vijay Abraham I of_node_name_eq(child, "link"))) 90603ada5a3SKishon Vijay Abraham I continue; 90703ada5a3SKishon Vijay Abraham I 90844d30d62SAlan Douglas sp->phys[node].lnk_rst = 909b872936fSKishon Vijay Abraham I of_reset_control_array_get_exclusive(child); 91044d30d62SAlan Douglas 91144d30d62SAlan Douglas if (IS_ERR(sp->phys[node].lnk_rst)) { 91244d30d62SAlan Douglas dev_err(dev, "failed to get reset %s\n", 91344d30d62SAlan Douglas child->full_name); 91444d30d62SAlan Douglas ret = PTR_ERR(sp->phys[node].lnk_rst); 91544d30d62SAlan Douglas goto put_child2; 91644d30d62SAlan Douglas } 91744d30d62SAlan Douglas 91844d30d62SAlan Douglas if (!sp->autoconf) { 91944d30d62SAlan Douglas ret = cdns_sierra_get_optional(&sp->phys[node], child); 92044d30d62SAlan Douglas if (ret) { 92144d30d62SAlan Douglas dev_err(dev, "missing property in node %s\n", 92244d30d62SAlan Douglas child->name); 92344d30d62SAlan Douglas goto put_child; 92444d30d62SAlan Douglas } 92544d30d62SAlan Douglas } 92644d30d62SAlan Douglas 927a43f72aeSKishon Vijay Abraham I sp->num_lanes += sp->phys[node].num_lanes; 928a43f72aeSKishon Vijay Abraham I 92944d30d62SAlan Douglas gphy = devm_phy_create(dev, child, &ops); 93044d30d62SAlan Douglas 93144d30d62SAlan Douglas if (IS_ERR(gphy)) { 93244d30d62SAlan Douglas ret = PTR_ERR(gphy); 93344d30d62SAlan Douglas goto put_child; 93444d30d62SAlan Douglas } 93544d30d62SAlan Douglas sp->phys[node].phy = gphy; 93644d30d62SAlan Douglas phy_set_drvdata(gphy, &sp->phys[node]); 93744d30d62SAlan Douglas 93844d30d62SAlan Douglas node++; 93944d30d62SAlan Douglas } 94044d30d62SAlan Douglas sp->nsubnodes = node; 94144d30d62SAlan Douglas 942a43f72aeSKishon Vijay Abraham I if (sp->num_lanes > SIERRA_MAX_LANES) { 943a43f72aeSKishon Vijay Abraham I dev_err(dev, "Invalid lane configuration\n"); 944a43f72aeSKishon Vijay Abraham I goto put_child2; 945a43f72aeSKishon Vijay Abraham I } 946a43f72aeSKishon Vijay Abraham I 94744d30d62SAlan Douglas /* If more than one subnode, configure the PHY as multilink */ 94844d30d62SAlan Douglas if (!sp->autoconf && sp->nsubnodes > 1) 949380f5708SKishon Vijay Abraham I regmap_field_write(sp->phy_pll_cfg_1, 0x1); 95044d30d62SAlan Douglas 95144d30d62SAlan Douglas pm_runtime_enable(dev); 95244d30d62SAlan Douglas phy_provider = devm_of_phy_provider_register(dev, of_phy_simple_xlate); 95344d30d62SAlan Douglas return PTR_ERR_OR_ZERO(phy_provider); 95444d30d62SAlan Douglas 95544d30d62SAlan Douglas put_child: 95644d30d62SAlan Douglas node++; 95744d30d62SAlan Douglas put_child2: 95844d30d62SAlan Douglas for (i = 0; i < node; i++) 95944d30d62SAlan Douglas reset_control_put(sp->phys[i].lnk_rst); 96044d30d62SAlan Douglas of_node_put(child); 96144d30d62SAlan Douglas clk_disable: 962*1436ec30SKishon Vijay Abraham I cdns_sierra_phy_disable_clocks(sp); 96344d30d62SAlan Douglas reset_control_assert(sp->apb_rst); 96428081b72SKishon Vijay Abraham I unregister_clk: 96528081b72SKishon Vijay Abraham I cdns_sierra_clk_unregister(sp); 96644d30d62SAlan Douglas return ret; 96744d30d62SAlan Douglas } 96844d30d62SAlan Douglas 96944d30d62SAlan Douglas static int cdns_sierra_phy_remove(struct platform_device *pdev) 97044d30d62SAlan Douglas { 971748e3456SKishon Vijay Abraham I struct cdns_sierra_phy *phy = platform_get_drvdata(pdev); 97244d30d62SAlan Douglas int i; 97344d30d62SAlan Douglas 97444d30d62SAlan Douglas reset_control_assert(phy->phy_rst); 97544d30d62SAlan Douglas reset_control_assert(phy->apb_rst); 97644d30d62SAlan Douglas pm_runtime_disable(&pdev->dev); 97744d30d62SAlan Douglas 978*1436ec30SKishon Vijay Abraham I cdns_sierra_phy_disable_clocks(phy); 97944d30d62SAlan Douglas /* 98044d30d62SAlan Douglas * The device level resets will be put automatically. 98144d30d62SAlan Douglas * Need to put the subnode resets here though. 98244d30d62SAlan Douglas */ 98344d30d62SAlan Douglas for (i = 0; i < phy->nsubnodes; i++) { 98444d30d62SAlan Douglas reset_control_assert(phy->phys[i].lnk_rst); 98544d30d62SAlan Douglas reset_control_put(phy->phys[i].lnk_rst); 98644d30d62SAlan Douglas } 98729c2d02aSKishon Vijay Abraham I 98828081b72SKishon Vijay Abraham I cdns_sierra_clk_unregister(phy); 98929c2d02aSKishon Vijay Abraham I 99044d30d62SAlan Douglas return 0; 99144d30d62SAlan Douglas } 99244d30d62SAlan Douglas 993871002d7SAnil Varughese /* refclk100MHz_32b_PCIe_cmn_pll_ext_ssc */ 9943cfb0e8eSRikard Falkeborn static const struct cdns_reg_pairs cdns_pcie_cmn_regs_ext_ssc[] = { 995871002d7SAnil Varughese {0x2106, SIERRA_CMN_PLLLC_LF_COEFF_MODE1_PREG}, 996871002d7SAnil Varughese {0x2106, SIERRA_CMN_PLLLC_LF_COEFF_MODE0_PREG}, 997871002d7SAnil Varughese {0x8A06, SIERRA_CMN_PLLLC_BWCAL_MODE1_PREG}, 998871002d7SAnil Varughese {0x8A06, SIERRA_CMN_PLLLC_BWCAL_MODE0_PREG}, 999871002d7SAnil Varughese {0x1B1B, SIERRA_CMN_PLLLC_SS_TIME_STEPSIZE_MODE_PREG} 1000871002d7SAnil Varughese }; 1001871002d7SAnil Varughese 1002871002d7SAnil Varughese /* refclk100MHz_32b_PCIe_ln_ext_ssc */ 10033cfb0e8eSRikard Falkeborn static const struct cdns_reg_pairs cdns_pcie_ln_regs_ext_ssc[] = { 1004871002d7SAnil Varughese {0x813E, SIERRA_CLKPATHCTRL_TMR_PREG}, 1005871002d7SAnil Varughese {0x8047, SIERRA_RX_CREQ_FLTR_A_MODE3_PREG}, 1006871002d7SAnil Varughese {0x808F, SIERRA_RX_CREQ_FLTR_A_MODE2_PREG}, 1007871002d7SAnil Varughese {0x808F, SIERRA_RX_CREQ_FLTR_A_MODE1_PREG}, 1008871002d7SAnil Varughese {0x808F, SIERRA_RX_CREQ_FLTR_A_MODE0_PREG}, 1009871002d7SAnil Varughese {0x033C, SIERRA_RX_CTLE_MAINTENANCE_PREG}, 1010871002d7SAnil Varughese {0x44CC, SIERRA_CREQ_EQ_OPEN_EYE_THRESH_PREG} 1011871002d7SAnil Varughese }; 1012871002d7SAnil Varughese 1013871002d7SAnil Varughese /* refclk100MHz_20b_USB_cmn_pll_ext_ssc */ 10143cfb0e8eSRikard Falkeborn static const struct cdns_reg_pairs cdns_usb_cmn_regs_ext_ssc[] = { 1015871002d7SAnil Varughese {0x2085, SIERRA_CMN_PLLLC_LF_COEFF_MODE1_PREG}, 1016871002d7SAnil Varughese {0x2085, SIERRA_CMN_PLLLC_LF_COEFF_MODE0_PREG}, 1017871002d7SAnil Varughese {0x0000, SIERRA_CMN_PLLLC_BWCAL_MODE0_PREG}, 1018871002d7SAnil Varughese {0x0000, SIERRA_CMN_PLLLC_SS_TIME_STEPSIZE_MODE_PREG} 1019871002d7SAnil Varughese }; 1020871002d7SAnil Varughese 1021871002d7SAnil Varughese /* refclk100MHz_20b_USB_ln_ext_ssc */ 10223cfb0e8eSRikard Falkeborn static const struct cdns_reg_pairs cdns_usb_ln_regs_ext_ssc[] = { 1023aead5fd6SKishon Vijay Abraham I {0xFE0A, SIERRA_DET_STANDEC_A_PREG}, 1024aead5fd6SKishon Vijay Abraham I {0x000F, SIERRA_DET_STANDEC_B_PREG}, 10252bcf14caSSanket Parmar {0x55A5, SIERRA_DET_STANDEC_C_PREG}, 1026871002d7SAnil Varughese {0x69ad, SIERRA_DET_STANDEC_D_PREG}, 1027aead5fd6SKishon Vijay Abraham I {0x0241, SIERRA_DET_STANDEC_E_PREG}, 10282bcf14caSSanket Parmar {0x0110, SIERRA_PSM_LANECAL_DLY_A1_RESETS_PREG}, 1029871002d7SAnil Varughese {0x0014, SIERRA_PSM_A0IN_TMR_PREG}, 1030aead5fd6SKishon Vijay Abraham I {0xCF00, SIERRA_PSM_DIAG_PREG}, 1031aead5fd6SKishon Vijay Abraham I {0x001F, SIERRA_PSC_TX_A0_PREG}, 1032aead5fd6SKishon Vijay Abraham I {0x0007, SIERRA_PSC_TX_A1_PREG}, 1033aead5fd6SKishon Vijay Abraham I {0x0003, SIERRA_PSC_TX_A2_PREG}, 1034aead5fd6SKishon Vijay Abraham I {0x0003, SIERRA_PSC_TX_A3_PREG}, 1035aead5fd6SKishon Vijay Abraham I {0x0FFF, SIERRA_PSC_RX_A0_PREG}, 10362bcf14caSSanket Parmar {0x0003, SIERRA_PSC_RX_A1_PREG}, 1037aead5fd6SKishon Vijay Abraham I {0x0003, SIERRA_PSC_RX_A2_PREG}, 1038aead5fd6SKishon Vijay Abraham I {0x0001, SIERRA_PSC_RX_A3_PREG}, 1039aead5fd6SKishon Vijay Abraham I {0x0001, SIERRA_PLLCTRL_SUBRATE_PREG}, 1040aead5fd6SKishon Vijay Abraham I {0x0406, SIERRA_PLLCTRL_GEN_D_PREG}, 1041871002d7SAnil Varughese {0x5233, SIERRA_PLLCTRL_CPGAIN_MODE_PREG}, 1042871002d7SAnil Varughese {0x00CA, SIERRA_CLKPATH_BIASTRIM_PREG}, 1043871002d7SAnil Varughese {0x2512, SIERRA_DFE_BIASTRIM_PREG}, 1044aead5fd6SKishon Vijay Abraham I {0x0000, SIERRA_DRVCTRL_ATTEN_PREG}, 10452bcf14caSSanket Parmar {0x823E, SIERRA_CLKPATHCTRL_TMR_PREG}, 10462bcf14caSSanket Parmar {0x078F, SIERRA_RX_CREQ_FLTR_A_MODE1_PREG}, 10472bcf14caSSanket Parmar {0x078F, SIERRA_RX_CREQ_FLTR_A_MODE0_PREG}, 1048aead5fd6SKishon Vijay Abraham I {0x7B3C, SIERRA_CREQ_CCLKDET_MODE01_PREG}, 10492bcf14caSSanket Parmar {0x023C, SIERRA_RX_CTLE_MAINTENANCE_PREG}, 1050aead5fd6SKishon Vijay Abraham I {0x3232, SIERRA_CREQ_FSMCLK_SEL_PREG}, 1051871002d7SAnil Varughese {0x0000, SIERRA_CREQ_EQ_CTRL_PREG}, 10522bcf14caSSanket Parmar {0x0000, SIERRA_CREQ_SPARE_PREG}, 1053871002d7SAnil Varughese {0xCC44, SIERRA_CREQ_EQ_OPEN_EYE_THRESH_PREG}, 10542bcf14caSSanket Parmar {0x8452, SIERRA_CTLELUT_CTRL_PREG}, 10552bcf14caSSanket Parmar {0x4121, SIERRA_DFE_ECMP_RATESEL_PREG}, 10562bcf14caSSanket Parmar {0x4121, SIERRA_DFE_SMP_RATESEL_PREG}, 10572bcf14caSSanket Parmar {0x0003, SIERRA_DEQ_PHALIGN_CTRL}, 1058871002d7SAnil Varughese {0x3200, SIERRA_DEQ_CONCUR_CTRL1_PREG}, 1059871002d7SAnil Varughese {0x5064, SIERRA_DEQ_CONCUR_CTRL2_PREG}, 1060871002d7SAnil Varughese {0x0030, SIERRA_DEQ_EPIPWR_CTRL2_PREG}, 1061871002d7SAnil Varughese {0x0048, SIERRA_DEQ_FAST_MAINT_CYCLES_PREG}, 1062871002d7SAnil Varughese {0x5A5A, SIERRA_DEQ_ERRCMP_CTRL_PREG}, 1063871002d7SAnil Varughese {0x02F5, SIERRA_DEQ_OFFSET_CTRL_PREG}, 1064871002d7SAnil Varughese {0x02F5, SIERRA_DEQ_GAIN_CTRL_PREG}, 10652bcf14caSSanket Parmar {0x9999, SIERRA_DEQ_VGATUNE_CTRL_PREG}, 1066871002d7SAnil Varughese {0x0014, SIERRA_DEQ_GLUT0}, 1067871002d7SAnil Varughese {0x0014, SIERRA_DEQ_GLUT1}, 1068871002d7SAnil Varughese {0x0014, SIERRA_DEQ_GLUT2}, 1069871002d7SAnil Varughese {0x0014, SIERRA_DEQ_GLUT3}, 1070871002d7SAnil Varughese {0x0014, SIERRA_DEQ_GLUT4}, 1071871002d7SAnil Varughese {0x0014, SIERRA_DEQ_GLUT5}, 1072871002d7SAnil Varughese {0x0014, SIERRA_DEQ_GLUT6}, 1073871002d7SAnil Varughese {0x0014, SIERRA_DEQ_GLUT7}, 1074871002d7SAnil Varughese {0x0014, SIERRA_DEQ_GLUT8}, 1075871002d7SAnil Varughese {0x0014, SIERRA_DEQ_GLUT9}, 1076871002d7SAnil Varughese {0x0014, SIERRA_DEQ_GLUT10}, 1077871002d7SAnil Varughese {0x0014, SIERRA_DEQ_GLUT11}, 1078871002d7SAnil Varughese {0x0014, SIERRA_DEQ_GLUT12}, 1079871002d7SAnil Varughese {0x0014, SIERRA_DEQ_GLUT13}, 1080871002d7SAnil Varughese {0x0014, SIERRA_DEQ_GLUT14}, 1081871002d7SAnil Varughese {0x0014, SIERRA_DEQ_GLUT15}, 1082871002d7SAnil Varughese {0x0014, SIERRA_DEQ_GLUT16}, 1083871002d7SAnil Varughese {0x0BAE, SIERRA_DEQ_ALUT0}, 1084871002d7SAnil Varughese {0x0AEB, SIERRA_DEQ_ALUT1}, 1085871002d7SAnil Varughese {0x0A28, SIERRA_DEQ_ALUT2}, 1086871002d7SAnil Varughese {0x0965, SIERRA_DEQ_ALUT3}, 1087871002d7SAnil Varughese {0x08A2, SIERRA_DEQ_ALUT4}, 1088871002d7SAnil Varughese {0x07DF, SIERRA_DEQ_ALUT5}, 1089871002d7SAnil Varughese {0x071C, SIERRA_DEQ_ALUT6}, 1090871002d7SAnil Varughese {0x0659, SIERRA_DEQ_ALUT7}, 1091871002d7SAnil Varughese {0x0596, SIERRA_DEQ_ALUT8}, 1092871002d7SAnil Varughese {0x0514, SIERRA_DEQ_ALUT9}, 1093871002d7SAnil Varughese {0x0492, SIERRA_DEQ_ALUT10}, 1094871002d7SAnil Varughese {0x0410, SIERRA_DEQ_ALUT11}, 1095871002d7SAnil Varughese {0x038E, SIERRA_DEQ_ALUT12}, 1096871002d7SAnil Varughese {0x030C, SIERRA_DEQ_ALUT13}, 1097871002d7SAnil Varughese {0x03F4, SIERRA_DEQ_DFETAP_CTRL_PREG}, 1098871002d7SAnil Varughese {0x0001, SIERRA_DFE_EN_1010_IGNORE_PREG}, 1099871002d7SAnil Varughese {0x3C01, SIERRA_DEQ_TAU_CTRL1_FAST_MAINT_PREG}, 1100871002d7SAnil Varughese {0x3C40, SIERRA_DEQ_TAU_CTRL1_SLOW_MAINT_PREG}, 1101871002d7SAnil Varughese {0x1C08, SIERRA_DEQ_TAU_CTRL2_PREG}, 1102871002d7SAnil Varughese {0x0033, SIERRA_DEQ_PICTRL_PREG}, 1103871002d7SAnil Varughese {0x0400, SIERRA_CPICAL_TMRVAL_MODE1_PREG}, 1104871002d7SAnil Varughese {0x0330, SIERRA_CPICAL_TMRVAL_MODE0_PREG}, 1105871002d7SAnil Varughese {0x01FF, SIERRA_CPICAL_PICNT_MODE1_PREG}, 1106aead5fd6SKishon Vijay Abraham I {0x0009, SIERRA_CPI_OUTBUF_RATESEL_PREG}, 1107871002d7SAnil Varughese {0x3232, SIERRA_CPICAL_RES_STARTCODE_MODE23_PREG}, 1108871002d7SAnil Varughese {0x0005, SIERRA_LFPSDET_SUPPORT_PREG}, 1109aead5fd6SKishon Vijay Abraham I {0x000F, SIERRA_LFPSFILT_NS_PREG}, 1110aead5fd6SKishon Vijay Abraham I {0x0009, SIERRA_LFPSFILT_RD_PREG}, 1111aead5fd6SKishon Vijay Abraham I {0x0001, SIERRA_LFPSFILT_MP_PREG}, 11122bcf14caSSanket Parmar {0x6013, SIERRA_SIGDET_SUPPORT_PREG}, 1113aead5fd6SKishon Vijay Abraham I {0x8013, SIERRA_SDFILT_H2L_A_PREG}, 1114871002d7SAnil Varughese {0x8009, SIERRA_SDFILT_L2H_PREG}, 1115871002d7SAnil Varughese {0x0024, SIERRA_RXBUFFER_CTLECTRL_PREG}, 1116871002d7SAnil Varughese {0x0020, SIERRA_RXBUFFER_RCDFECTRL_PREG}, 1117871002d7SAnil Varughese {0x4243, SIERRA_RXBUFFER_DFECTRL_PREG} 111844d30d62SAlan Douglas }; 111944d30d62SAlan Douglas 112044d30d62SAlan Douglas static const struct cdns_sierra_data cdns_map_sierra = { 112144d30d62SAlan Douglas SIERRA_MACRO_ID, 1122380f5708SKishon Vijay Abraham I 0x2, 1123380f5708SKishon Vijay Abraham I 0x2, 1124871002d7SAnil Varughese ARRAY_SIZE(cdns_pcie_cmn_regs_ext_ssc), 1125871002d7SAnil Varughese ARRAY_SIZE(cdns_pcie_ln_regs_ext_ssc), 1126871002d7SAnil Varughese ARRAY_SIZE(cdns_usb_cmn_regs_ext_ssc), 1127871002d7SAnil Varughese ARRAY_SIZE(cdns_usb_ln_regs_ext_ssc), 1128871002d7SAnil Varughese cdns_pcie_cmn_regs_ext_ssc, 1129871002d7SAnil Varughese cdns_pcie_ln_regs_ext_ssc, 1130871002d7SAnil Varughese cdns_usb_cmn_regs_ext_ssc, 1131871002d7SAnil Varughese cdns_usb_ln_regs_ext_ssc, 113244d30d62SAlan Douglas }; 113344d30d62SAlan Douglas 1134367da978SKishon Vijay Abraham I static const struct cdns_sierra_data cdns_ti_map_sierra = { 1135367da978SKishon Vijay Abraham I SIERRA_MACRO_ID, 1136367da978SKishon Vijay Abraham I 0x0, 1137367da978SKishon Vijay Abraham I 0x1, 1138871002d7SAnil Varughese ARRAY_SIZE(cdns_pcie_cmn_regs_ext_ssc), 1139871002d7SAnil Varughese ARRAY_SIZE(cdns_pcie_ln_regs_ext_ssc), 1140871002d7SAnil Varughese ARRAY_SIZE(cdns_usb_cmn_regs_ext_ssc), 1141871002d7SAnil Varughese ARRAY_SIZE(cdns_usb_ln_regs_ext_ssc), 1142871002d7SAnil Varughese cdns_pcie_cmn_regs_ext_ssc, 1143871002d7SAnil Varughese cdns_pcie_ln_regs_ext_ssc, 1144871002d7SAnil Varughese cdns_usb_cmn_regs_ext_ssc, 1145871002d7SAnil Varughese cdns_usb_ln_regs_ext_ssc, 1146367da978SKishon Vijay Abraham I }; 1147367da978SKishon Vijay Abraham I 114844d30d62SAlan Douglas static const struct of_device_id cdns_sierra_id_table[] = { 114944d30d62SAlan Douglas { 115044d30d62SAlan Douglas .compatible = "cdns,sierra-phy-t0", 115144d30d62SAlan Douglas .data = &cdns_map_sierra, 115244d30d62SAlan Douglas }, 1153367da978SKishon Vijay Abraham I { 1154367da978SKishon Vijay Abraham I .compatible = "ti,sierra-phy-t0", 1155367da978SKishon Vijay Abraham I .data = &cdns_ti_map_sierra, 1156367da978SKishon Vijay Abraham I }, 115744d30d62SAlan Douglas {} 115844d30d62SAlan Douglas }; 115944d30d62SAlan Douglas MODULE_DEVICE_TABLE(of, cdns_sierra_id_table); 116044d30d62SAlan Douglas 116144d30d62SAlan Douglas static struct platform_driver cdns_sierra_driver = { 116244d30d62SAlan Douglas .probe = cdns_sierra_phy_probe, 116344d30d62SAlan Douglas .remove = cdns_sierra_phy_remove, 116444d30d62SAlan Douglas .driver = { 116544d30d62SAlan Douglas .name = "cdns-sierra-phy", 116644d30d62SAlan Douglas .of_match_table = cdns_sierra_id_table, 116744d30d62SAlan Douglas }, 116844d30d62SAlan Douglas }; 116944d30d62SAlan Douglas module_platform_driver(cdns_sierra_driver); 117044d30d62SAlan Douglas 117144d30d62SAlan Douglas MODULE_ALIAS("platform:cdns_sierra"); 117244d30d62SAlan Douglas MODULE_AUTHOR("Cadence Design Systems"); 117344d30d62SAlan Douglas MODULE_DESCRIPTION("CDNS sierra phy driver"); 117444d30d62SAlan Douglas MODULE_LICENSE("GPL v2"); 1175