rjw | 1f88458 | 2022-01-06 17:20:42 +0800 | [diff] [blame^] | 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | /* |
| 3 | * Copyright (c) 2015 MediaTek Inc. |
| 4 | * Author: Chunfeng Yun <chunfeng.yun@mediatek.com> |
| 5 | * |
| 6 | */ |
| 7 | |
| 8 | #include <dt-bindings/phy/phy.h> |
| 9 | #include <linux/clk.h> |
| 10 | #include <linux/delay.h> |
| 11 | #include <linux/io.h> |
| 12 | #include <linux/iopoll.h> |
| 13 | #include <linux/module.h> |
| 14 | #include <linux/of_address.h> |
| 15 | #include <linux/of_device.h> |
| 16 | #include <linux/phy/phy.h> |
| 17 | #include <linux/platform_device.h> |
| 18 | |
| 19 | #ifdef CONFIG_MTK_PMIC_CHIP_MT6389 |
| 20 | #include "clkbuf_v1/mtk_clkbuf_ctl.h" |
| 21 | #endif |
| 22 | |
| 23 | /* version V1 sub-banks offset base address */ |
| 24 | /* banks shared by multiple phys */ |
| 25 | #define SSUSB_SIFSLV_V1_SPLLC 0x000 /* shared by u3 phys */ |
| 26 | #define SSUSB_SIFSLV_V1_U2FREQ 0x100 /* shared by u2 phys */ |
| 27 | #define SSUSB_SIFSLV_V1_CHIP 0x300 /* shared by u3 phys */ |
| 28 | /* u2 phy bank */ |
| 29 | #define SSUSB_SIFSLV_V1_U2PHY_COM 0x000 |
| 30 | /* u3/pcie/sata phy banks */ |
| 31 | #define SSUSB_SIFSLV_V1_U3PHYD 0x000 |
| 32 | #define SSUSB_SIFSLV_V1_U3PHYA 0x200 |
| 33 | |
| 34 | /* version V2 sub-banks offset base address */ |
| 35 | /* u2 phy banks */ |
| 36 | #define SSUSB_SIFSLV_V2_MISC 0x000 |
| 37 | #define SSUSB_SIFSLV_V2_U2FREQ 0x100 |
| 38 | #define SSUSB_SIFSLV_V2_U2PHY_COM 0x300 |
| 39 | /* u3/pcie/sata phy banks */ |
| 40 | #define SSUSB_SIFSLV_V2_SPLLC 0x000 |
| 41 | #define SSUSB_SIFSLV_V2_CHIP 0x100 |
| 42 | #define SSUSB_SIFSLV_V2_U3PHYD 0x200 |
| 43 | #define SSUSB_SIFSLV_V2_U3PHYA 0x400 |
| 44 | |
| 45 | #define U3P_USBPHYACR0 0x000 |
| 46 | #define PA0_RG_U2PLL_FORCE_ON BIT(15) |
| 47 | #define PA0_RG_USB20_INTR_EN BIT(5) |
| 48 | |
| 49 | #define U3P_USBPHYACR1 0x004 |
| 50 | #define PA1_RG_VRT_SEL GENMASK(14, 12) |
| 51 | #define PA1_RG_VRT_SEL_VAL(x) ((0x7 & (x)) << 12) |
| 52 | #define PA1_RG_TERM_SEL GENMASK(10, 8) |
| 53 | #define PA1_RG_TERM_SEL_VAL(x) ((0x7 & (x)) << 8) |
| 54 | |
| 55 | #define U3P_USBPHYACR2 0x008 |
| 56 | #define PA2_RG_SIF_U2PLL_FORCE_EN BIT(18) |
| 57 | |
| 58 | #define U3P_USBPHYACR5 0x014 |
| 59 | #define PA5_RG_U2_HSTX_SRCAL_EN BIT(15) |
| 60 | #define PA5_RG_U2_HSTX_SRCTRL GENMASK(14, 12) |
| 61 | #define PA5_RG_U2_HSTX_SRCTRL_VAL(x) ((0x7 & (x)) << 12) |
| 62 | #define PA5_RG_U2_HS_100U_U3_EN BIT(11) |
| 63 | |
| 64 | #define U3P_USBPHYACR6 0x018 |
| 65 | #define PA6_RG_U2_BC11_SW_EN BIT(23) |
| 66 | #define PA6_RG_U2_OTG_VBUSCMP_EN BIT(20) |
| 67 | #define PA6_RG_U2_SQTH GENMASK(3, 0) |
| 68 | #define PA6_RG_U2_SQTH_VAL(x) (0xf & (x)) |
| 69 | |
| 70 | #define U3P_U2PHYACR4 0x020 |
| 71 | #define P2C_RG_USB20_GPIO_CTL BIT(9) |
| 72 | #define P2C_USB20_GPIO_MODE BIT(8) |
| 73 | #define P2C_U2_GPIO_CTR_MSK (P2C_RG_USB20_GPIO_CTL | P2C_USB20_GPIO_MODE) |
| 74 | |
| 75 | #define U3D_U2PHYDCR0 0x060 |
| 76 | #define P2C_RG_SIF_U2PLL_FORCE_ON BIT(24) |
| 77 | |
| 78 | #define U3P_U2PHYDTM0 0x068 |
| 79 | #define P2C_FORCE_UART_EN BIT(26) |
| 80 | #define P2C_FORCE_DATAIN BIT(23) |
| 81 | #define P2C_FORCE_DM_PULLDOWN BIT(21) |
| 82 | #define P2C_FORCE_DP_PULLDOWN BIT(20) |
| 83 | #define P2C_FORCE_XCVRSEL BIT(19) |
| 84 | #define P2C_FORCE_SUSPENDM BIT(18) |
| 85 | #define P2C_FORCE_TERMSEL BIT(17) |
| 86 | #define P2C_RG_DATAIN GENMASK(13, 10) |
| 87 | #define P2C_RG_DATAIN_VAL(x) ((0xf & (x)) << 10) |
| 88 | #define P2C_RG_DMPULLDOWN BIT(7) |
| 89 | #define P2C_RG_DPPULLDOWN BIT(6) |
| 90 | #define P2C_RG_XCVRSEL GENMASK(5, 4) |
| 91 | #define P2C_RG_XCVRSEL_VAL(x) ((0x3 & (x)) << 4) |
| 92 | #define P2C_RG_SUSPENDM BIT(3) |
| 93 | #define P2C_RG_TERMSEL BIT(2) |
| 94 | #define P2C_DTM0_PART_MASK \ |
| 95 | (P2C_FORCE_DATAIN | P2C_FORCE_DM_PULLDOWN | \ |
| 96 | P2C_FORCE_DP_PULLDOWN | P2C_FORCE_XCVRSEL | \ |
| 97 | P2C_FORCE_TERMSEL | P2C_RG_DMPULLDOWN | \ |
| 98 | P2C_RG_DPPULLDOWN | P2C_RG_TERMSEL) |
| 99 | |
| 100 | #define U3P_U2PHYDTM1 0x06C |
| 101 | #define P2C_RG_UART_EN BIT(16) |
| 102 | #define P2C_FORCE_IDDIG BIT(9) |
| 103 | #define P2C_RG_VBUSVALID BIT(5) |
| 104 | #define P2C_RG_SESSEND BIT(4) |
| 105 | #define P2C_RG_AVALID BIT(2) |
| 106 | #define P2C_RG_IDDIG BIT(1) |
| 107 | |
| 108 | #define U3P_U2PHYBC12C 0x080 |
| 109 | #define P2C_RG_CHGDT_EN BIT(0) |
| 110 | |
| 111 | #define U3P_U3_CHIP_GPIO_CTLD 0x0c |
| 112 | #define P3C_REG_IP_SW_RST BIT(31) |
| 113 | #define P3C_MCU_BUS_CK_GATE_EN BIT(30) |
| 114 | #define P3C_FORCE_IP_SW_RST BIT(29) |
| 115 | |
| 116 | #define U3P_U3_CHIP_GPIO_CTLE 0x10 |
| 117 | #define P3C_RG_SWRST_U3_PHYD BIT(25) |
| 118 | #define P3C_RG_SWRST_U3_PHYD_FORCE_EN BIT(24) |
| 119 | |
| 120 | #define U3P_U3_PHYA_REG0 0x000 |
| 121 | #define P3A_RG_CLKDRV_OFF GENMASK(3, 2) |
| 122 | #define P3A_RG_CLKDRV_OFF_VAL(x) ((0x3 & (x)) << 2) |
| 123 | |
| 124 | #define U3P_U3_PHYA_REG1 0x004 |
| 125 | #define P3A_RG_CLKDRV_AMP GENMASK(31, 29) |
| 126 | #define P3A_RG_CLKDRV_AMP_VAL(x) ((0x7 & (x)) << 29) |
| 127 | |
| 128 | #define U3P_U3_PHYA_REG6 0x018 |
| 129 | #define P3A_RG_TX_EIDLE_CM GENMASK(31, 28) |
| 130 | #define P3A_RG_TX_EIDLE_CM_VAL(x) ((0xf & (x)) << 28) |
| 131 | |
| 132 | #define U3P_U3_PHYA_REG9 0x024 |
| 133 | #define P3A_RG_RX_DAC_MUX GENMASK(5, 1) |
| 134 | #define P3A_RG_RX_DAC_MUX_VAL(x) ((0x1f & (x)) << 1) |
| 135 | |
| 136 | #define U3P_U3_PHYA_REGD 0x034 |
| 137 | #define P3A_RG_SSUSB_SYSPLL_FBDIV GENMASK(31, 0) |
| 138 | #define P3A_RG_SSUSB_SYSPLL_FBDIV_VAL(x) ((0xffffffff & (x)) << 0) |
| 139 | #define U3P_SSUSB_XTAL_CK_26M 0x60276276 |
| 140 | |
| 141 | #define U3P_U3_PHYA_DA_REG0 0x100 |
| 142 | #define P3A_RG_XTAL_EXT_PE2H GENMASK(17, 16) |
| 143 | #define P3A_RG_XTAL_EXT_PE2H_VAL(x) ((0x3 & (x)) << 16) |
| 144 | #define P3A_RG_XTAL_EXT_PE1H GENMASK(13, 12) |
| 145 | #define P3A_RG_XTAL_EXT_PE1H_VAL(x) ((0x3 & (x)) << 12) |
| 146 | #define P3A_RG_XTAL_EXT_EN_U3 GENMASK(11, 10) |
| 147 | #define P3A_RG_XTAL_EXT_EN_U3_VAL(x) ((0x3 & (x)) << 10) |
| 148 | |
| 149 | #define U3P_U3_PHYA_DA_REG4 0x108 |
| 150 | #define P3A_RG_PLL_DIVEN_PE2H GENMASK(21, 19) |
| 151 | #define P3A_RG_PLL_BC_PE2H GENMASK(7, 6) |
| 152 | #define P3A_RG_PLL_BC_PE2H_VAL(x) ((0x3 & (x)) << 6) |
| 153 | |
| 154 | #define U3P_U3_PHYA_DA_REG5 0x10c |
| 155 | #define P3A_RG_PLL_BR_PE2H GENMASK(29, 28) |
| 156 | #define P3A_RG_PLL_BR_PE2H_VAL(x) ((0x3 & (x)) << 28) |
| 157 | #define P3A_RG_PLL_IC_PE2H GENMASK(15, 12) |
| 158 | #define P3A_RG_PLL_IC_PE2H_VAL(x) ((0xf & (x)) << 12) |
| 159 | |
| 160 | #define U3P_U3_PHYA_DA_REG6 0x110 |
| 161 | #define P3A_RG_PLL_IR_PE2H GENMASK(19, 16) |
| 162 | #define P3A_RG_PLL_IR_PE2H_VAL(x) ((0xf & (x)) << 16) |
| 163 | |
| 164 | #define U3P_U3_PHYA_DA_REG7 0x114 |
| 165 | #define P3A_RG_PLL_BP_PE2H GENMASK(19, 16) |
| 166 | #define P3A_RG_PLL_BP_PE2H_VAL(x) ((0xf & (x)) << 16) |
| 167 | |
| 168 | #define U3P_U3_PHYA_DA_REG20 0x13c |
| 169 | #define P3A_RG_PLL_DELTA1_PE2H GENMASK(31, 16) |
| 170 | #define P3A_RG_PLL_DELTA1_PE2H_VAL(x) ((0xffff & (x)) << 16) |
| 171 | |
| 172 | #define U3P_U3_PHYA_DA_REG25 0x148 |
| 173 | #define P3A_RG_PLL_DELTA_PE2H GENMASK(15, 0) |
| 174 | #define P3A_RG_PLL_DELTA_PE2H_VAL(x) (0xffff & (x)) |
| 175 | |
| 176 | #define U3P_U3_PHYD_LFPS1 0x00c |
| 177 | #define P3D_RG_FWAKE_TH GENMASK(21, 16) |
| 178 | #define P3D_RG_FWAKE_TH_VAL(x) ((0x3f & (x)) << 16) |
| 179 | |
| 180 | #define U3P_U3_PHYD_CDR1 0x05c |
| 181 | #define P3D_RG_CDR_BIR_LTD1 GENMASK(28, 24) |
| 182 | #define P3D_RG_CDR_BIR_LTD1_VAL(x) ((0x1f & (x)) << 24) |
| 183 | #define P3D_RG_CDR_BIR_LTD0 GENMASK(12, 8) |
| 184 | #define P3D_RG_CDR_BIR_LTD0_VAL(x) ((0x1f & (x)) << 8) |
| 185 | |
| 186 | #define U3P_U3_PHYD_EQ3 0x7c |
| 187 | #define P3D_RG_SSUSB_EQ_DLEQ_LFI_GEN2 GENMASK(31, 28) |
| 188 | #define P3D_RG_SSUSB_EQ_DLEQ_LFI_GEN2_VAL(x) ((0xf & (x)) << 28) |
| 189 | |
| 190 | #define U3P_U3_PHYD_CPPAT2 0xd8 |
| 191 | #define P3D_RG_SSUSB_EQ_DLEQ_LFI_GEN3 GENMASK(27, 24) |
| 192 | #define P3D_RG_SSUSB_EQ_DLEQ_LFI_GEN3_VAL(x) ((0xf & (x)) << 24) |
| 193 | |
| 194 | #define U3P_U3_PHYD_RXDET1 0x128 |
| 195 | #define P3D_RG_RXDET_STB2_SET GENMASK(17, 9) |
| 196 | #define P3D_RG_RXDET_STB2_SET_VAL(x) ((0x1ff & (x)) << 9) |
| 197 | |
| 198 | #define U3P_U3_PHYD_RXDET2 0x12c |
| 199 | #define P3D_RG_RXDET_STB2_SET_P3 GENMASK(8, 0) |
| 200 | #define P3D_RG_RXDET_STB2_SET_P3_VAL(x) (0x1ff & (x)) |
| 201 | |
| 202 | #define U3P_SPLLC_XTALCTL3 0x018 |
| 203 | #define XC3_RG_U3_XTAL_RX_PWD BIT(9) |
| 204 | #define XC3_RG_U3_FRC_XTAL_RX_PWD BIT(8) |
| 205 | |
| 206 | #define U3P_U2FREQ_FMCR0 0x00 |
| 207 | #define P2F_RG_MONCLK_SEL GENMASK(27, 26) |
| 208 | #define P2F_RG_MONCLK_SEL_VAL(x) ((0x3 & (x)) << 26) |
| 209 | #define P2F_RG_FREQDET_EN BIT(24) |
| 210 | #define P2F_RG_CYCLECNT GENMASK(23, 0) |
| 211 | #define P2F_RG_CYCLECNT_VAL(x) ((P2F_RG_CYCLECNT) & (x)) |
| 212 | |
| 213 | #define U3P_U2FREQ_VALUE 0x0c |
| 214 | |
| 215 | #define U3P_U2FREQ_FMMONR1 0x10 |
| 216 | #define P2F_USB_FM_VALID BIT(0) |
| 217 | #define P2F_RG_FRCK_EN BIT(8) |
| 218 | |
| 219 | #define U3P_REF_CLK 26 /* MHZ */ |
| 220 | #define U3P_SLEW_RATE_COEF 28 |
| 221 | #define U3P_SR_COEF_DIVISOR 1000 |
| 222 | #define U3P_FM_DET_CYCLE_CNT 1024 |
| 223 | |
| 224 | /* SATA register setting */ |
| 225 | #define PHYD_CTRL_SIGNAL_MODE4 0x1c |
| 226 | /* CDR Charge Pump P-path current adjustment */ |
| 227 | #define RG_CDR_BICLTD1_GEN1_MSK GENMASK(23, 20) |
| 228 | #define RG_CDR_BICLTD1_GEN1_VAL(x) ((0xf & (x)) << 20) |
| 229 | #define RG_CDR_BICLTD0_GEN1_MSK GENMASK(11, 8) |
| 230 | #define RG_CDR_BICLTD0_GEN1_VAL(x) ((0xf & (x)) << 8) |
| 231 | |
| 232 | #define PHYD_DESIGN_OPTION2 0x24 |
| 233 | /* Symbol lock count selection */ |
| 234 | #define RG_LOCK_CNT_SEL_MSK GENMASK(5, 4) |
| 235 | #define RG_LOCK_CNT_SEL_VAL(x) ((0x3 & (x)) << 4) |
| 236 | |
| 237 | #define PHYD_DESIGN_OPTION9 0x40 |
| 238 | /* COMWAK GAP width window */ |
| 239 | #define RG_TG_MAX_MSK GENMASK(20, 16) |
| 240 | #define RG_TG_MAX_VAL(x) ((0x1f & (x)) << 16) |
| 241 | /* COMINIT GAP width window */ |
| 242 | #define RG_T2_MAX_MSK GENMASK(13, 8) |
| 243 | #define RG_T2_MAX_VAL(x) ((0x3f & (x)) << 8) |
| 244 | /* COMWAK GAP width window */ |
| 245 | #define RG_TG_MIN_MSK GENMASK(7, 5) |
| 246 | #define RG_TG_MIN_VAL(x) ((0x7 & (x)) << 5) |
| 247 | /* COMINIT GAP width window */ |
| 248 | #define RG_T2_MIN_MSK GENMASK(4, 0) |
| 249 | #define RG_T2_MIN_VAL(x) (0x1f & (x)) |
| 250 | |
| 251 | #define ANA_RG_CTRL_SIGNAL1 0x4c |
| 252 | /* TX driver tail current control for 0dB de-empahsis mdoe for Gen1 speed */ |
| 253 | #define RG_IDRV_0DB_GEN1_MSK GENMASK(13, 8) |
| 254 | #define RG_IDRV_0DB_GEN1_VAL(x) ((0x3f & (x)) << 8) |
| 255 | |
| 256 | #define ANA_RG_CTRL_SIGNAL4 0x58 |
| 257 | #define RG_CDR_BICLTR_GEN1_MSK GENMASK(23, 20) |
| 258 | #define RG_CDR_BICLTR_GEN1_VAL(x) ((0xf & (x)) << 20) |
| 259 | /* Loop filter R1 resistance adjustment for Gen1 speed */ |
| 260 | #define RG_CDR_BR_GEN2_MSK GENMASK(10, 8) |
| 261 | #define RG_CDR_BR_GEN2_VAL(x) ((0x7 & (x)) << 8) |
| 262 | |
| 263 | #define ANA_RG_CTRL_SIGNAL6 0x60 |
| 264 | /* I-path capacitance adjustment for Gen1 */ |
| 265 | #define RG_CDR_BC_GEN1_MSK GENMASK(28, 24) |
| 266 | #define RG_CDR_BC_GEN1_VAL(x) ((0x1f & (x)) << 24) |
| 267 | #define RG_CDR_BIRLTR_GEN1_MSK GENMASK(4, 0) |
| 268 | #define RG_CDR_BIRLTR_GEN1_VAL(x) (0x1f & (x)) |
| 269 | |
| 270 | #define ANA_EQ_EYE_CTRL_SIGNAL1 0x6c |
| 271 | /* RX Gen1 LEQ tuning step */ |
| 272 | #define RG_EQ_DLEQ_LFI_GEN1_MSK GENMASK(11, 8) |
| 273 | #define RG_EQ_DLEQ_LFI_GEN1_VAL(x) ((0xf & (x)) << 8) |
| 274 | |
| 275 | #define ANA_EQ_EYE_CTRL_SIGNAL4 0xd8 |
| 276 | #define RG_CDR_BIRLTD0_GEN1_MSK GENMASK(20, 16) |
| 277 | #define RG_CDR_BIRLTD0_GEN1_VAL(x) ((0x1f & (x)) << 16) |
| 278 | |
| 279 | #define ANA_EQ_EYE_CTRL_SIGNAL5 0xdc |
| 280 | #define RG_CDR_BIRLTD0_GEN3_MSK GENMASK(4, 0) |
| 281 | #define RG_CDR_BIRLTD0_GEN3_VAL(x) (0x1f & (x)) |
| 282 | |
| 283 | enum mtk_phy_version { |
| 284 | MTK_PHY_V1 = 1, |
| 285 | MTK_PHY_V2, |
| 286 | }; |
| 287 | |
| 288 | struct mtk_phy_pdata { |
| 289 | /* avoid RX sensitivity level degradation only for mt8173 */ |
| 290 | bool avoid_rx_sen_degradation; |
| 291 | enum mtk_phy_version version; |
| 292 | }; |
| 293 | |
| 294 | struct u2phy_banks { |
| 295 | void __iomem *misc; |
| 296 | void __iomem *fmreg; |
| 297 | void __iomem *com; |
| 298 | }; |
| 299 | |
| 300 | struct u3phy_banks { |
| 301 | void __iomem *spllc; |
| 302 | void __iomem *chip; |
| 303 | void __iomem *phyd; /* include u3phyd_bank2 */ |
| 304 | void __iomem *phya; /* include u3phya_da */ |
| 305 | }; |
| 306 | |
| 307 | struct mtk_phy_instance { |
| 308 | struct phy *phy; |
| 309 | void __iomem *port_base; |
| 310 | union { |
| 311 | struct u2phy_banks u2_banks; |
| 312 | struct u3phy_banks u3_banks; |
| 313 | }; |
| 314 | struct clk *ref_clk; /* reference clock of anolog phy */ |
| 315 | u32 index; |
| 316 | u8 type; |
| 317 | int eye_src; |
| 318 | int eye_vrt; |
| 319 | int eye_term; |
| 320 | bool bc12_en; |
| 321 | }; |
| 322 | |
| 323 | struct mtk_tphy { |
| 324 | struct device *dev; |
| 325 | void __iomem *sif_base; /* only shared sif */ |
| 326 | /* deprecated, use @ref_clk instead in phy instance */ |
| 327 | struct clk *u3phya_ref; /* reference clock of usb3 anolog phy */ |
| 328 | const struct mtk_phy_pdata *pdata; |
| 329 | struct mtk_phy_instance **phys; |
| 330 | int nphys; |
| 331 | int src_ref_clk; /* MHZ, reference clock for slew rate calibrate */ |
| 332 | int src_coef; /* coefficient for slew rate calibrate */ |
| 333 | }; |
| 334 | |
| 335 | static void hs_slew_rate_calibrate(struct mtk_tphy *tphy, |
| 336 | struct mtk_phy_instance *instance) |
| 337 | { |
| 338 | struct u2phy_banks *u2_banks = &instance->u2_banks; |
| 339 | void __iomem *fmreg = u2_banks->fmreg; |
| 340 | void __iomem *com = u2_banks->com; |
| 341 | int calibration_val; |
| 342 | int fm_out; |
| 343 | u32 tmp; |
| 344 | |
| 345 | /* use force value */ |
| 346 | if (instance->eye_src) |
| 347 | return; |
| 348 | |
| 349 | /* enable USB ring oscillator */ |
| 350 | tmp = readl(com + U3P_USBPHYACR5); |
| 351 | tmp |= PA5_RG_U2_HSTX_SRCAL_EN; |
| 352 | writel(tmp, com + U3P_USBPHYACR5); |
| 353 | udelay(1); |
| 354 | |
| 355 | /*enable free run clock */ |
| 356 | tmp = readl(fmreg + U3P_U2FREQ_FMMONR1); |
| 357 | tmp |= P2F_RG_FRCK_EN; |
| 358 | writel(tmp, fmreg + U3P_U2FREQ_FMMONR1); |
| 359 | |
| 360 | /* set cycle count as 1024, and select u2 channel */ |
| 361 | tmp = readl(fmreg + U3P_U2FREQ_FMCR0); |
| 362 | tmp &= ~(P2F_RG_CYCLECNT | P2F_RG_MONCLK_SEL); |
| 363 | tmp |= P2F_RG_CYCLECNT_VAL(U3P_FM_DET_CYCLE_CNT); |
| 364 | if (tphy->pdata->version == MTK_PHY_V1) |
| 365 | tmp |= P2F_RG_MONCLK_SEL_VAL(instance->index >> 1); |
| 366 | |
| 367 | writel(tmp, fmreg + U3P_U2FREQ_FMCR0); |
| 368 | |
| 369 | /* enable frequency meter */ |
| 370 | tmp = readl(fmreg + U3P_U2FREQ_FMCR0); |
| 371 | tmp |= P2F_RG_FREQDET_EN; |
| 372 | writel(tmp, fmreg + U3P_U2FREQ_FMCR0); |
| 373 | |
| 374 | /* ignore return value */ |
| 375 | readl_poll_timeout(fmreg + U3P_U2FREQ_FMMONR1, tmp, |
| 376 | (tmp & P2F_USB_FM_VALID), 10, 200); |
| 377 | |
| 378 | fm_out = readl(fmreg + U3P_U2FREQ_VALUE); |
| 379 | |
| 380 | /* disable frequency meter */ |
| 381 | tmp = readl(fmreg + U3P_U2FREQ_FMCR0); |
| 382 | tmp &= ~P2F_RG_FREQDET_EN; |
| 383 | writel(tmp, fmreg + U3P_U2FREQ_FMCR0); |
| 384 | |
| 385 | /*disable free run clock */ |
| 386 | tmp = readl(fmreg + U3P_U2FREQ_FMMONR1); |
| 387 | tmp &= ~P2F_RG_FRCK_EN; |
| 388 | writel(tmp, fmreg + U3P_U2FREQ_FMMONR1); |
| 389 | |
| 390 | if (fm_out) { |
| 391 | /* ( 1024 / FM_OUT ) x reference clock frequency x coef */ |
| 392 | tmp = tphy->src_ref_clk * tphy->src_coef; |
| 393 | tmp = (tmp * U3P_FM_DET_CYCLE_CNT) / fm_out; |
| 394 | calibration_val = DIV_ROUND_CLOSEST(tmp, U3P_SR_COEF_DIVISOR); |
| 395 | } else { |
| 396 | /* if FM detection fail, set default value */ |
| 397 | calibration_val = 4; |
| 398 | } |
| 399 | dev_dbg(tphy->dev, "phy:%d, fm_out:%d, calib:%d (clk:%d, coef:%d)\n", |
| 400 | instance->index, fm_out, calibration_val, |
| 401 | tphy->src_ref_clk, tphy->src_coef); |
| 402 | |
| 403 | /* set HS slew rate */ |
| 404 | tmp = readl(com + U3P_USBPHYACR5); |
| 405 | tmp &= ~PA5_RG_U2_HSTX_SRCTRL; |
| 406 | tmp |= PA5_RG_U2_HSTX_SRCTRL_VAL(calibration_val); |
| 407 | writel(tmp, com + U3P_USBPHYACR5); |
| 408 | |
| 409 | /* disable USB ring oscillator */ |
| 410 | tmp = readl(com + U3P_USBPHYACR5); |
| 411 | tmp &= ~PA5_RG_U2_HSTX_SRCAL_EN; |
| 412 | writel(tmp, com + U3P_USBPHYACR5); |
| 413 | } |
| 414 | |
| 415 | static void u3_phy_instance_init(struct mtk_tphy *tphy, |
| 416 | struct mtk_phy_instance *instance) |
| 417 | { |
| 418 | struct u3phy_banks *u3_banks = &instance->u3_banks; |
| 419 | u32 tmp; |
| 420 | |
| 421 | /* set SSUSB_XTAL_CK to 26M */ |
| 422 | tmp = readl(u3_banks->phya + U3P_U3_PHYA_REGD); |
| 423 | tmp &= ~P3A_RG_SSUSB_SYSPLL_FBDIV; |
| 424 | tmp |= P3A_RG_SSUSB_SYSPLL_FBDIV_VAL(U3P_SSUSB_XTAL_CK_26M); |
| 425 | writel(tmp, u3_banks->phya + U3P_U3_PHYA_REGD); |
| 426 | |
| 427 | /* set RG_SSUSB_EQ_DLEQ_LFI_GEN2 to SSUSB */ |
| 428 | tmp = readl(u3_banks->phyd + U3P_U3_PHYD_EQ3); |
| 429 | tmp &= ~P3D_RG_SSUSB_EQ_DLEQ_LFI_GEN2; |
| 430 | tmp |= P3D_RG_SSUSB_EQ_DLEQ_LFI_GEN2_VAL(0x8); |
| 431 | writel(tmp, u3_banks->phyd + U3P_U3_PHYD_EQ3); |
| 432 | |
| 433 | /* set RG_SSUSB_EQ_DLEQ_LFI_GEN3 to SSUSB */ |
| 434 | tmp = readl(u3_banks->phyd + U3P_U3_PHYD_CPPAT2); |
| 435 | tmp &= ~P3D_RG_SSUSB_EQ_DLEQ_LFI_GEN3; |
| 436 | tmp |= P3D_RG_SSUSB_EQ_DLEQ_LFI_GEN3_VAL(0x8); |
| 437 | writel(tmp, u3_banks->phyd + U3P_U3_PHYD_CPPAT2); |
| 438 | |
| 439 | /* gating PCIe Analog XTAL clock */ |
| 440 | tmp = readl(u3_banks->spllc + U3P_SPLLC_XTALCTL3); |
| 441 | tmp |= XC3_RG_U3_XTAL_RX_PWD | XC3_RG_U3_FRC_XTAL_RX_PWD; |
| 442 | writel(tmp, u3_banks->spllc + U3P_SPLLC_XTALCTL3); |
| 443 | |
| 444 | /* gating XSQ */ |
| 445 | tmp = readl(u3_banks->phya + U3P_U3_PHYA_DA_REG0); |
| 446 | tmp &= ~P3A_RG_XTAL_EXT_EN_U3; |
| 447 | tmp |= P3A_RG_XTAL_EXT_EN_U3_VAL(2); |
| 448 | writel(tmp, u3_banks->phya + U3P_U3_PHYA_DA_REG0); |
| 449 | |
| 450 | tmp = readl(u3_banks->phya + U3P_U3_PHYA_REG9); |
| 451 | tmp &= ~P3A_RG_RX_DAC_MUX; |
| 452 | tmp |= P3A_RG_RX_DAC_MUX_VAL(4); |
| 453 | writel(tmp, u3_banks->phya + U3P_U3_PHYA_REG9); |
| 454 | |
| 455 | tmp = readl(u3_banks->phya + U3P_U3_PHYA_REG6); |
| 456 | tmp &= ~P3A_RG_TX_EIDLE_CM; |
| 457 | tmp |= P3A_RG_TX_EIDLE_CM_VAL(0xe); |
| 458 | writel(tmp, u3_banks->phya + U3P_U3_PHYA_REG6); |
| 459 | |
| 460 | tmp = readl(u3_banks->phyd + U3P_U3_PHYD_CDR1); |
| 461 | tmp &= ~(P3D_RG_CDR_BIR_LTD0 | P3D_RG_CDR_BIR_LTD1); |
| 462 | tmp |= P3D_RG_CDR_BIR_LTD0_VAL(0xc) | P3D_RG_CDR_BIR_LTD1_VAL(0x3); |
| 463 | writel(tmp, u3_banks->phyd + U3P_U3_PHYD_CDR1); |
| 464 | |
| 465 | tmp = readl(u3_banks->phyd + U3P_U3_PHYD_LFPS1); |
| 466 | tmp &= ~P3D_RG_FWAKE_TH; |
| 467 | tmp |= P3D_RG_FWAKE_TH_VAL(0x34); |
| 468 | writel(tmp, u3_banks->phyd + U3P_U3_PHYD_LFPS1); |
| 469 | |
| 470 | tmp = readl(u3_banks->phyd + U3P_U3_PHYD_RXDET1); |
| 471 | tmp &= ~P3D_RG_RXDET_STB2_SET; |
| 472 | tmp |= P3D_RG_RXDET_STB2_SET_VAL(0x10); |
| 473 | writel(tmp, u3_banks->phyd + U3P_U3_PHYD_RXDET1); |
| 474 | |
| 475 | tmp = readl(u3_banks->phyd + U3P_U3_PHYD_RXDET2); |
| 476 | tmp &= ~P3D_RG_RXDET_STB2_SET_P3; |
| 477 | tmp |= P3D_RG_RXDET_STB2_SET_P3_VAL(0x10); |
| 478 | writel(tmp, u3_banks->phyd + U3P_U3_PHYD_RXDET2); |
| 479 | |
| 480 | dev_dbg(tphy->dev, "%s(%d)\n", __func__, instance->index); |
| 481 | } |
| 482 | |
| 483 | static void u2_phy_instance_init(struct mtk_tphy *tphy, |
| 484 | struct mtk_phy_instance *instance) |
| 485 | { |
| 486 | struct u2phy_banks *u2_banks = &instance->u2_banks; |
| 487 | void __iomem *com = u2_banks->com; |
| 488 | u32 index = instance->index; |
| 489 | u32 tmp; |
| 490 | |
| 491 | /* switch to USB function, and enable usb pll */ |
| 492 | tmp = readl(com + U3P_U2PHYDTM0); |
| 493 | tmp &= ~(P2C_FORCE_UART_EN | P2C_FORCE_SUSPENDM); |
| 494 | tmp |= P2C_RG_XCVRSEL_VAL(1) | P2C_RG_DATAIN_VAL(0); |
| 495 | writel(tmp, com + U3P_U2PHYDTM0); |
| 496 | |
| 497 | tmp = readl(com + U3P_U2PHYDTM1); |
| 498 | tmp &= ~P2C_RG_UART_EN; |
| 499 | writel(tmp, com + U3P_U2PHYDTM1); |
| 500 | |
| 501 | tmp = readl(com + U3P_USBPHYACR0); |
| 502 | tmp |= PA0_RG_USB20_INTR_EN; |
| 503 | writel(tmp, com + U3P_USBPHYACR0); |
| 504 | |
| 505 | /* disable switch 100uA current to SSUSB */ |
| 506 | tmp = readl(com + U3P_USBPHYACR5); |
| 507 | tmp &= ~PA5_RG_U2_HS_100U_U3_EN; |
| 508 | writel(tmp, com + U3P_USBPHYACR5); |
| 509 | |
| 510 | if (!index) { |
| 511 | tmp = readl(com + U3P_U2PHYACR4); |
| 512 | tmp &= ~P2C_U2_GPIO_CTR_MSK; |
| 513 | writel(tmp, com + U3P_U2PHYACR4); |
| 514 | } |
| 515 | |
| 516 | if (tphy->pdata->avoid_rx_sen_degradation) { |
| 517 | if (!index) { |
| 518 | tmp = readl(com + U3P_USBPHYACR2); |
| 519 | tmp |= PA2_RG_SIF_U2PLL_FORCE_EN; |
| 520 | writel(tmp, com + U3P_USBPHYACR2); |
| 521 | |
| 522 | tmp = readl(com + U3D_U2PHYDCR0); |
| 523 | tmp &= ~P2C_RG_SIF_U2PLL_FORCE_ON; |
| 524 | writel(tmp, com + U3D_U2PHYDCR0); |
| 525 | } else { |
| 526 | tmp = readl(com + U3D_U2PHYDCR0); |
| 527 | tmp |= P2C_RG_SIF_U2PLL_FORCE_ON; |
| 528 | writel(tmp, com + U3D_U2PHYDCR0); |
| 529 | |
| 530 | tmp = readl(com + U3P_U2PHYDTM0); |
| 531 | tmp |= P2C_RG_SUSPENDM | P2C_FORCE_SUSPENDM; |
| 532 | writel(tmp, com + U3P_U2PHYDTM0); |
| 533 | } |
| 534 | } |
| 535 | |
| 536 | tmp = readl(com + U3P_USBPHYACR6); |
| 537 | tmp &= ~PA6_RG_U2_BC11_SW_EN; /* DP/DM BC1.1 path Disable */ |
| 538 | tmp &= ~PA6_RG_U2_SQTH; |
| 539 | tmp |= PA6_RG_U2_SQTH_VAL(2); |
| 540 | writel(tmp, com + U3P_USBPHYACR6); |
| 541 | |
| 542 | dev_dbg(tphy->dev, "%s(%d)\n", __func__, index); |
| 543 | } |
| 544 | |
| 545 | static void u2_phy_instance_power_on(struct mtk_tphy *tphy, |
| 546 | struct mtk_phy_instance *instance) |
| 547 | { |
| 548 | struct u2phy_banks *u2_banks = &instance->u2_banks; |
| 549 | void __iomem *com = u2_banks->com; |
| 550 | u32 index = instance->index; |
| 551 | u32 tmp; |
| 552 | |
| 553 | tmp = readl(com + U3P_U2PHYDTM0); |
| 554 | tmp &= ~(P2C_RG_XCVRSEL | P2C_RG_DATAIN | P2C_DTM0_PART_MASK); |
| 555 | writel(tmp, com + U3P_U2PHYDTM0); |
| 556 | |
| 557 | /* OTG Enable */ |
| 558 | tmp = readl(com + U3P_USBPHYACR6); |
| 559 | tmp |= PA6_RG_U2_OTG_VBUSCMP_EN; |
| 560 | writel(tmp, com + U3P_USBPHYACR6); |
| 561 | |
| 562 | tmp = readl(com + U3P_U2PHYDTM1); |
| 563 | tmp |= P2C_RG_VBUSVALID | P2C_RG_AVALID; |
| 564 | tmp &= ~P2C_RG_SESSEND; |
| 565 | writel(tmp, com + U3P_U2PHYDTM1); |
| 566 | |
| 567 | if (tphy->pdata->avoid_rx_sen_degradation && index) { |
| 568 | tmp = readl(com + U3D_U2PHYDCR0); |
| 569 | tmp |= P2C_RG_SIF_U2PLL_FORCE_ON; |
| 570 | writel(tmp, com + U3D_U2PHYDCR0); |
| 571 | |
| 572 | tmp = readl(com + U3P_U2PHYDTM0); |
| 573 | tmp |= P2C_RG_SUSPENDM | P2C_FORCE_SUSPENDM; |
| 574 | writel(tmp, com + U3P_U2PHYDTM0); |
| 575 | } |
| 576 | dev_dbg(tphy->dev, "%s(%d)\n", __func__, index); |
| 577 | } |
| 578 | |
| 579 | static void u2_phy_instance_power_off(struct mtk_tphy *tphy, |
| 580 | struct mtk_phy_instance *instance) |
| 581 | { |
| 582 | struct u2phy_banks *u2_banks = &instance->u2_banks; |
| 583 | void __iomem *com = u2_banks->com; |
| 584 | u32 index = instance->index; |
| 585 | u32 tmp; |
| 586 | |
| 587 | tmp = readl(com + U3P_U2PHYDTM0); |
| 588 | tmp &= ~(P2C_RG_XCVRSEL | P2C_RG_DATAIN); |
| 589 | writel(tmp, com + U3P_U2PHYDTM0); |
| 590 | |
| 591 | /* OTG Disable */ |
| 592 | tmp = readl(com + U3P_USBPHYACR6); |
| 593 | tmp &= ~PA6_RG_U2_OTG_VBUSCMP_EN; |
| 594 | writel(tmp, com + U3P_USBPHYACR6); |
| 595 | |
| 596 | tmp = readl(com + U3P_U2PHYDTM1); |
| 597 | tmp &= ~(P2C_RG_VBUSVALID | P2C_RG_AVALID); |
| 598 | tmp |= P2C_RG_SESSEND; |
| 599 | writel(tmp, com + U3P_U2PHYDTM1); |
| 600 | |
| 601 | if (tphy->pdata->avoid_rx_sen_degradation && index) { |
| 602 | tmp = readl(com + U3P_U2PHYDTM0); |
| 603 | tmp &= ~(P2C_RG_SUSPENDM | P2C_FORCE_SUSPENDM); |
| 604 | writel(tmp, com + U3P_U2PHYDTM0); |
| 605 | |
| 606 | tmp = readl(com + U3D_U2PHYDCR0); |
| 607 | tmp &= ~P2C_RG_SIF_U2PLL_FORCE_ON; |
| 608 | writel(tmp, com + U3D_U2PHYDCR0); |
| 609 | } |
| 610 | |
| 611 | dev_dbg(tphy->dev, "%s(%d)\n", __func__, index); |
| 612 | } |
| 613 | |
| 614 | static void u2_phy_instance_exit(struct mtk_tphy *tphy, |
| 615 | struct mtk_phy_instance *instance) |
| 616 | { |
| 617 | struct u2phy_banks *u2_banks = &instance->u2_banks; |
| 618 | void __iomem *com = u2_banks->com; |
| 619 | u32 index = instance->index; |
| 620 | u32 tmp; |
| 621 | |
| 622 | if (tphy->pdata->avoid_rx_sen_degradation && index) { |
| 623 | tmp = readl(com + U3D_U2PHYDCR0); |
| 624 | tmp &= ~P2C_RG_SIF_U2PLL_FORCE_ON; |
| 625 | writel(tmp, com + U3D_U2PHYDCR0); |
| 626 | |
| 627 | tmp = readl(com + U3P_U2PHYDTM0); |
| 628 | tmp &= ~P2C_FORCE_SUSPENDM; |
| 629 | writel(tmp, com + U3P_U2PHYDTM0); |
| 630 | } |
| 631 | } |
| 632 | |
| 633 | static void u2_phy_instance_set_mode(struct mtk_tphy *tphy, |
| 634 | struct mtk_phy_instance *instance, |
| 635 | enum phy_mode mode) |
| 636 | { |
| 637 | struct u2phy_banks *u2_banks = &instance->u2_banks; |
| 638 | u32 tmp; |
| 639 | |
| 640 | tmp = readl(u2_banks->com + U3P_U2PHYDTM1); |
| 641 | switch (mode) { |
| 642 | case PHY_MODE_USB_DEVICE: |
| 643 | tmp |= P2C_FORCE_IDDIG | P2C_RG_IDDIG; |
| 644 | break; |
| 645 | case PHY_MODE_USB_HOST: |
| 646 | tmp |= P2C_FORCE_IDDIG; |
| 647 | tmp &= ~P2C_RG_IDDIG; |
| 648 | break; |
| 649 | case PHY_MODE_USB_OTG: |
| 650 | tmp &= ~(P2C_FORCE_IDDIG | P2C_RG_IDDIG); |
| 651 | break; |
| 652 | default: |
| 653 | return; |
| 654 | } |
| 655 | writel(tmp, u2_banks->com + U3P_U2PHYDTM1); |
| 656 | } |
| 657 | |
| 658 | static void pcie_phy_instance_init(struct mtk_tphy *tphy, |
| 659 | struct mtk_phy_instance *instance) |
| 660 | { |
| 661 | struct u3phy_banks *u3_banks = &instance->u3_banks; |
| 662 | u32 tmp; |
| 663 | |
| 664 | #ifdef CONFIG_MTK_PMIC_CHIP_MT6389 |
| 665 | /* Phy clock is from clkbuf_ext. */ |
| 666 | if (!clk_buf_ctrl(CLK_BUF_EXT, true)) { |
| 667 | dev_err(tphy->dev, "clkbuf_ext enable failed\n"); |
| 668 | return; |
| 669 | } |
| 670 | #endif |
| 671 | |
| 672 | if (tphy->pdata->version != MTK_PHY_V1) |
| 673 | return; |
| 674 | |
| 675 | tmp = readl(u3_banks->phya + U3P_U3_PHYA_DA_REG0); |
| 676 | tmp &= ~(P3A_RG_XTAL_EXT_PE1H | P3A_RG_XTAL_EXT_PE2H); |
| 677 | tmp |= P3A_RG_XTAL_EXT_PE1H_VAL(0x2) | P3A_RG_XTAL_EXT_PE2H_VAL(0x2); |
| 678 | writel(tmp, u3_banks->phya + U3P_U3_PHYA_DA_REG0); |
| 679 | |
| 680 | /* ref clk drive */ |
| 681 | tmp = readl(u3_banks->phya + U3P_U3_PHYA_REG1); |
| 682 | tmp &= ~P3A_RG_CLKDRV_AMP; |
| 683 | tmp |= P3A_RG_CLKDRV_AMP_VAL(0x4); |
| 684 | writel(tmp, u3_banks->phya + U3P_U3_PHYA_REG1); |
| 685 | |
| 686 | tmp = readl(u3_banks->phya + U3P_U3_PHYA_REG0); |
| 687 | tmp &= ~P3A_RG_CLKDRV_OFF; |
| 688 | tmp |= P3A_RG_CLKDRV_OFF_VAL(0x1); |
| 689 | writel(tmp, u3_banks->phya + U3P_U3_PHYA_REG0); |
| 690 | |
| 691 | /* SSC delta -5000ppm */ |
| 692 | tmp = readl(u3_banks->phya + U3P_U3_PHYA_DA_REG20); |
| 693 | tmp &= ~P3A_RG_PLL_DELTA1_PE2H; |
| 694 | tmp |= P3A_RG_PLL_DELTA1_PE2H_VAL(0x3c); |
| 695 | writel(tmp, u3_banks->phya + U3P_U3_PHYA_DA_REG20); |
| 696 | |
| 697 | tmp = readl(u3_banks->phya + U3P_U3_PHYA_DA_REG25); |
| 698 | tmp &= ~P3A_RG_PLL_DELTA_PE2H; |
| 699 | tmp |= P3A_RG_PLL_DELTA_PE2H_VAL(0x36); |
| 700 | writel(tmp, u3_banks->phya + U3P_U3_PHYA_DA_REG25); |
| 701 | |
| 702 | /* change pll BW 0.6M */ |
| 703 | tmp = readl(u3_banks->phya + U3P_U3_PHYA_DA_REG5); |
| 704 | tmp &= ~(P3A_RG_PLL_BR_PE2H | P3A_RG_PLL_IC_PE2H); |
| 705 | tmp |= P3A_RG_PLL_BR_PE2H_VAL(0x1) | P3A_RG_PLL_IC_PE2H_VAL(0x1); |
| 706 | writel(tmp, u3_banks->phya + U3P_U3_PHYA_DA_REG5); |
| 707 | |
| 708 | tmp = readl(u3_banks->phya + U3P_U3_PHYA_DA_REG4); |
| 709 | tmp &= ~(P3A_RG_PLL_DIVEN_PE2H | P3A_RG_PLL_BC_PE2H); |
| 710 | tmp |= P3A_RG_PLL_BC_PE2H_VAL(0x3); |
| 711 | writel(tmp, u3_banks->phya + U3P_U3_PHYA_DA_REG4); |
| 712 | |
| 713 | tmp = readl(u3_banks->phya + U3P_U3_PHYA_DA_REG6); |
| 714 | tmp &= ~P3A_RG_PLL_IR_PE2H; |
| 715 | tmp |= P3A_RG_PLL_IR_PE2H_VAL(0x2); |
| 716 | writel(tmp, u3_banks->phya + U3P_U3_PHYA_DA_REG6); |
| 717 | |
| 718 | tmp = readl(u3_banks->phya + U3P_U3_PHYA_DA_REG7); |
| 719 | tmp &= ~P3A_RG_PLL_BP_PE2H; |
| 720 | tmp |= P3A_RG_PLL_BP_PE2H_VAL(0xa); |
| 721 | writel(tmp, u3_banks->phya + U3P_U3_PHYA_DA_REG7); |
| 722 | |
| 723 | /* Tx Detect Rx Timing: 10us -> 5us */ |
| 724 | tmp = readl(u3_banks->phyd + U3P_U3_PHYD_RXDET1); |
| 725 | tmp &= ~P3D_RG_RXDET_STB2_SET; |
| 726 | tmp |= P3D_RG_RXDET_STB2_SET_VAL(0x10); |
| 727 | writel(tmp, u3_banks->phyd + U3P_U3_PHYD_RXDET1); |
| 728 | |
| 729 | tmp = readl(u3_banks->phyd + U3P_U3_PHYD_RXDET2); |
| 730 | tmp &= ~P3D_RG_RXDET_STB2_SET_P3; |
| 731 | tmp |= P3D_RG_RXDET_STB2_SET_P3_VAL(0x10); |
| 732 | writel(tmp, u3_banks->phyd + U3P_U3_PHYD_RXDET2); |
| 733 | |
| 734 | /* wait for PCIe subsys register to active */ |
| 735 | usleep_range(2500, 3000); |
| 736 | dev_dbg(tphy->dev, "%s(%d)\n", __func__, instance->index); |
| 737 | } |
| 738 | |
| 739 | static void pcie_phy_instance_exit(struct mtk_tphy *tphy, |
| 740 | struct mtk_phy_instance *instance) |
| 741 | { |
| 742 | #ifdef CONFIG_MTK_PMIC_CHIP_MT6389 |
| 743 | /* Disable the clkbuf_ext. */ |
| 744 | clk_buf_ctrl(CLK_BUF_EXT, false); |
| 745 | #endif |
| 746 | } |
| 747 | |
| 748 | static void pcie_phy_instance_power_on(struct mtk_tphy *tphy, |
| 749 | struct mtk_phy_instance *instance) |
| 750 | { |
| 751 | struct u3phy_banks *bank = &instance->u3_banks; |
| 752 | u32 tmp; |
| 753 | |
| 754 | tmp = readl(bank->chip + U3P_U3_CHIP_GPIO_CTLD); |
| 755 | tmp &= ~(P3C_FORCE_IP_SW_RST | P3C_REG_IP_SW_RST); |
| 756 | writel(tmp, bank->chip + U3P_U3_CHIP_GPIO_CTLD); |
| 757 | |
| 758 | tmp = readl(bank->chip + U3P_U3_CHIP_GPIO_CTLE); |
| 759 | tmp &= ~(P3C_RG_SWRST_U3_PHYD_FORCE_EN | P3C_RG_SWRST_U3_PHYD); |
| 760 | writel(tmp, bank->chip + U3P_U3_CHIP_GPIO_CTLE); |
| 761 | } |
| 762 | |
| 763 | static void pcie_phy_instance_power_off(struct mtk_tphy *tphy, |
| 764 | struct mtk_phy_instance *instance) |
| 765 | |
| 766 | { |
| 767 | struct u3phy_banks *bank = &instance->u3_banks; |
| 768 | u32 tmp; |
| 769 | |
| 770 | tmp = readl(bank->chip + U3P_U3_CHIP_GPIO_CTLD); |
| 771 | tmp |= P3C_FORCE_IP_SW_RST | P3C_REG_IP_SW_RST; |
| 772 | writel(tmp, bank->chip + U3P_U3_CHIP_GPIO_CTLD); |
| 773 | |
| 774 | tmp = readl(bank->chip + U3P_U3_CHIP_GPIO_CTLE); |
| 775 | tmp |= P3C_RG_SWRST_U3_PHYD_FORCE_EN | P3C_RG_SWRST_U3_PHYD; |
| 776 | writel(tmp, bank->chip + U3P_U3_CHIP_GPIO_CTLE); |
| 777 | } |
| 778 | |
| 779 | static void pcie_phy_instance_set_mode(struct mtk_tphy *tphy, |
| 780 | struct mtk_phy_instance *instance, |
| 781 | enum phy_mode mode) |
| 782 | { |
| 783 | struct u3phy_banks *bank = &instance->u3_banks; |
| 784 | u32 tmp; |
| 785 | |
| 786 | if (mode == PHY_MODE_PCIE) { |
| 787 | /* PCIe EP by default, here back to RC */ |
| 788 | tmp = readl(bank->phya + U3P_U3_PHYA_REGD); |
| 789 | tmp &= ~P3A_RG_SSUSB_SYSPLL_FBDIV; |
| 790 | tmp |= P3A_RG_SSUSB_SYSPLL_FBDIV_VAL(U3P_SSUSB_XTAL_CK_26M); |
| 791 | writel(tmp, bank->phya + U3P_U3_PHYA_REGD); |
| 792 | |
| 793 | /* Reset phy to make above change take effect for PCIe */ |
| 794 | tmp = readl(bank->chip + U3P_U3_CHIP_GPIO_CTLD); |
| 795 | tmp |= P3C_FORCE_IP_SW_RST | P3C_REG_IP_SW_RST; |
| 796 | writel(tmp, bank->chip + U3P_U3_CHIP_GPIO_CTLD); |
| 797 | |
| 798 | tmp = readl(bank->chip + U3P_U3_CHIP_GPIO_CTLE); |
| 799 | tmp |= P3C_RG_SWRST_U3_PHYD_FORCE_EN | P3C_RG_SWRST_U3_PHYD; |
| 800 | writel(tmp, bank->chip + U3P_U3_CHIP_GPIO_CTLE); |
| 801 | |
| 802 | tmp = readl(bank->chip + U3P_U3_CHIP_GPIO_CTLD); |
| 803 | tmp &= ~(P3C_FORCE_IP_SW_RST | P3C_REG_IP_SW_RST); |
| 804 | writel(tmp, bank->chip + U3P_U3_CHIP_GPIO_CTLD); |
| 805 | |
| 806 | tmp = readl(bank->chip + U3P_U3_CHIP_GPIO_CTLE); |
| 807 | tmp &= ~(P3C_RG_SWRST_U3_PHYD_FORCE_EN | P3C_RG_SWRST_U3_PHYD); |
| 808 | writel(tmp, bank->chip + U3P_U3_CHIP_GPIO_CTLE); |
| 809 | } |
| 810 | } |
| 811 | |
| 812 | static void sata_phy_instance_init(struct mtk_tphy *tphy, |
| 813 | struct mtk_phy_instance *instance) |
| 814 | { |
| 815 | struct u3phy_banks *u3_banks = &instance->u3_banks; |
| 816 | void __iomem *phyd = u3_banks->phyd; |
| 817 | u32 tmp; |
| 818 | |
| 819 | /* charge current adjustment */ |
| 820 | tmp = readl(phyd + ANA_RG_CTRL_SIGNAL6); |
| 821 | tmp &= ~(RG_CDR_BIRLTR_GEN1_MSK | RG_CDR_BC_GEN1_MSK); |
| 822 | tmp |= RG_CDR_BIRLTR_GEN1_VAL(0x6) | RG_CDR_BC_GEN1_VAL(0x1a); |
| 823 | writel(tmp, phyd + ANA_RG_CTRL_SIGNAL6); |
| 824 | |
| 825 | tmp = readl(phyd + ANA_EQ_EYE_CTRL_SIGNAL4); |
| 826 | tmp &= ~RG_CDR_BIRLTD0_GEN1_MSK; |
| 827 | tmp |= RG_CDR_BIRLTD0_GEN1_VAL(0x18); |
| 828 | writel(tmp, phyd + ANA_EQ_EYE_CTRL_SIGNAL4); |
| 829 | |
| 830 | tmp = readl(phyd + ANA_EQ_EYE_CTRL_SIGNAL5); |
| 831 | tmp &= ~RG_CDR_BIRLTD0_GEN3_MSK; |
| 832 | tmp |= RG_CDR_BIRLTD0_GEN3_VAL(0x06); |
| 833 | writel(tmp, phyd + ANA_EQ_EYE_CTRL_SIGNAL5); |
| 834 | |
| 835 | tmp = readl(phyd + ANA_RG_CTRL_SIGNAL4); |
| 836 | tmp &= ~(RG_CDR_BICLTR_GEN1_MSK | RG_CDR_BR_GEN2_MSK); |
| 837 | tmp |= RG_CDR_BICLTR_GEN1_VAL(0x0c) | RG_CDR_BR_GEN2_VAL(0x07); |
| 838 | writel(tmp, phyd + ANA_RG_CTRL_SIGNAL4); |
| 839 | |
| 840 | tmp = readl(phyd + PHYD_CTRL_SIGNAL_MODE4); |
| 841 | tmp &= ~(RG_CDR_BICLTD0_GEN1_MSK | RG_CDR_BICLTD1_GEN1_MSK); |
| 842 | tmp |= RG_CDR_BICLTD0_GEN1_VAL(0x08) | RG_CDR_BICLTD1_GEN1_VAL(0x02); |
| 843 | writel(tmp, phyd + PHYD_CTRL_SIGNAL_MODE4); |
| 844 | |
| 845 | tmp = readl(phyd + PHYD_DESIGN_OPTION2); |
| 846 | tmp &= ~RG_LOCK_CNT_SEL_MSK; |
| 847 | tmp |= RG_LOCK_CNT_SEL_VAL(0x02); |
| 848 | writel(tmp, phyd + PHYD_DESIGN_OPTION2); |
| 849 | |
| 850 | tmp = readl(phyd + PHYD_DESIGN_OPTION9); |
| 851 | tmp &= ~(RG_T2_MIN_MSK | RG_TG_MIN_MSK | |
| 852 | RG_T2_MAX_MSK | RG_TG_MAX_MSK); |
| 853 | tmp |= RG_T2_MIN_VAL(0x12) | RG_TG_MIN_VAL(0x04) | |
| 854 | RG_T2_MAX_VAL(0x31) | RG_TG_MAX_VAL(0x0e); |
| 855 | writel(tmp, phyd + PHYD_DESIGN_OPTION9); |
| 856 | |
| 857 | tmp = readl(phyd + ANA_RG_CTRL_SIGNAL1); |
| 858 | tmp &= ~RG_IDRV_0DB_GEN1_MSK; |
| 859 | tmp |= RG_IDRV_0DB_GEN1_VAL(0x20); |
| 860 | writel(tmp, phyd + ANA_RG_CTRL_SIGNAL1); |
| 861 | |
| 862 | tmp = readl(phyd + ANA_EQ_EYE_CTRL_SIGNAL1); |
| 863 | tmp &= ~RG_EQ_DLEQ_LFI_GEN1_MSK; |
| 864 | tmp |= RG_EQ_DLEQ_LFI_GEN1_VAL(0x03); |
| 865 | writel(tmp, phyd + ANA_EQ_EYE_CTRL_SIGNAL1); |
| 866 | |
| 867 | dev_dbg(tphy->dev, "%s(%d)\n", __func__, instance->index); |
| 868 | } |
| 869 | |
| 870 | static void phy_v1_banks_init(struct mtk_tphy *tphy, |
| 871 | struct mtk_phy_instance *instance) |
| 872 | { |
| 873 | struct u2phy_banks *u2_banks = &instance->u2_banks; |
| 874 | struct u3phy_banks *u3_banks = &instance->u3_banks; |
| 875 | |
| 876 | switch (instance->type) { |
| 877 | case PHY_TYPE_USB2: |
| 878 | u2_banks->misc = NULL; |
| 879 | u2_banks->fmreg = tphy->sif_base + SSUSB_SIFSLV_V1_U2FREQ; |
| 880 | u2_banks->com = instance->port_base + SSUSB_SIFSLV_V1_U2PHY_COM; |
| 881 | break; |
| 882 | case PHY_TYPE_USB3: |
| 883 | case PHY_TYPE_PCIE: |
| 884 | u3_banks->spllc = tphy->sif_base + SSUSB_SIFSLV_V1_SPLLC; |
| 885 | u3_banks->chip = tphy->sif_base + SSUSB_SIFSLV_V1_CHIP; |
| 886 | u3_banks->phyd = instance->port_base + SSUSB_SIFSLV_V1_U3PHYD; |
| 887 | u3_banks->phya = instance->port_base + SSUSB_SIFSLV_V1_U3PHYA; |
| 888 | break; |
| 889 | case PHY_TYPE_SATA: |
| 890 | u3_banks->phyd = instance->port_base + SSUSB_SIFSLV_V1_U3PHYD; |
| 891 | break; |
| 892 | default: |
| 893 | dev_err(tphy->dev, "incompatible PHY type\n"); |
| 894 | return; |
| 895 | } |
| 896 | } |
| 897 | |
| 898 | static void phy_v2_banks_init(struct mtk_tphy *tphy, |
| 899 | struct mtk_phy_instance *instance) |
| 900 | { |
| 901 | struct u2phy_banks *u2_banks = &instance->u2_banks; |
| 902 | struct u3phy_banks *u3_banks = &instance->u3_banks; |
| 903 | |
| 904 | switch (instance->type) { |
| 905 | case PHY_TYPE_USB2: |
| 906 | u2_banks->misc = instance->port_base + SSUSB_SIFSLV_V2_MISC; |
| 907 | u2_banks->fmreg = instance->port_base + SSUSB_SIFSLV_V2_U2FREQ; |
| 908 | u2_banks->com = instance->port_base + SSUSB_SIFSLV_V2_U2PHY_COM; |
| 909 | break; |
| 910 | case PHY_TYPE_USB3: |
| 911 | case PHY_TYPE_PCIE: |
| 912 | u3_banks->spllc = instance->port_base + SSUSB_SIFSLV_V2_SPLLC; |
| 913 | u3_banks->chip = instance->port_base + SSUSB_SIFSLV_V2_CHIP; |
| 914 | u3_banks->phyd = instance->port_base + SSUSB_SIFSLV_V2_U3PHYD; |
| 915 | u3_banks->phya = instance->port_base + SSUSB_SIFSLV_V2_U3PHYA; |
| 916 | break; |
| 917 | default: |
| 918 | dev_err(tphy->dev, "incompatible PHY type\n"); |
| 919 | return; |
| 920 | } |
| 921 | } |
| 922 | |
| 923 | static void phy_parse_property(struct mtk_tphy *tphy, |
| 924 | struct mtk_phy_instance *instance) |
| 925 | { |
| 926 | struct device *dev = &instance->phy->dev; |
| 927 | |
| 928 | if (instance->type != PHY_TYPE_USB2) |
| 929 | return; |
| 930 | |
| 931 | instance->bc12_en = device_property_read_bool(dev, "mediatek,bc12"); |
| 932 | device_property_read_u32(dev, "mediatek,eye-src", |
| 933 | &instance->eye_src); |
| 934 | device_property_read_u32(dev, "mediatek,eye-vrt", |
| 935 | &instance->eye_vrt); |
| 936 | device_property_read_u32(dev, "mediatek,eye-term", |
| 937 | &instance->eye_term); |
| 938 | dev_dbg(dev, "bc12:%d, src:%d, vrt:%d, term:%d\n", |
| 939 | instance->bc12_en, instance->eye_src, |
| 940 | instance->eye_vrt, instance->eye_term); |
| 941 | } |
| 942 | |
| 943 | static void u2_phy_props_set(struct mtk_tphy *tphy, |
| 944 | struct mtk_phy_instance *instance) |
| 945 | { |
| 946 | struct u2phy_banks *u2_banks = &instance->u2_banks; |
| 947 | void __iomem *com = u2_banks->com; |
| 948 | u32 tmp; |
| 949 | |
| 950 | if (instance->bc12_en) { |
| 951 | tmp = readl(com + U3P_U2PHYBC12C); |
| 952 | tmp |= P2C_RG_CHGDT_EN; /* BC1.2 path Enable */ |
| 953 | writel(tmp, com + U3P_U2PHYBC12C); |
| 954 | } |
| 955 | |
| 956 | if (instance->eye_src) { |
| 957 | tmp = readl(com + U3P_USBPHYACR5); |
| 958 | tmp &= ~PA5_RG_U2_HSTX_SRCTRL; |
| 959 | tmp |= PA5_RG_U2_HSTX_SRCTRL_VAL(instance->eye_src); |
| 960 | writel(tmp, com + U3P_USBPHYACR5); |
| 961 | } |
| 962 | |
| 963 | if (instance->eye_vrt) { |
| 964 | tmp = readl(com + U3P_USBPHYACR1); |
| 965 | tmp &= ~PA1_RG_VRT_SEL; |
| 966 | tmp |= PA1_RG_VRT_SEL_VAL(instance->eye_vrt); |
| 967 | writel(tmp, com + U3P_USBPHYACR1); |
| 968 | } |
| 969 | |
| 970 | if (instance->eye_term) { |
| 971 | tmp = readl(com + U3P_USBPHYACR1); |
| 972 | tmp &= ~PA1_RG_TERM_SEL; |
| 973 | tmp |= PA1_RG_TERM_SEL_VAL(instance->eye_term); |
| 974 | writel(tmp, com + U3P_USBPHYACR1); |
| 975 | } |
| 976 | } |
| 977 | |
| 978 | static int mtk_phy_init(struct phy *phy) |
| 979 | { |
| 980 | struct mtk_phy_instance *instance = phy_get_drvdata(phy); |
| 981 | struct mtk_tphy *tphy = dev_get_drvdata(phy->dev.parent); |
| 982 | int ret; |
| 983 | |
| 984 | ret = clk_prepare_enable(tphy->u3phya_ref); |
| 985 | if (ret) { |
| 986 | dev_err(tphy->dev, "failed to enable u3phya_ref\n"); |
| 987 | return ret; |
| 988 | } |
| 989 | |
| 990 | ret = clk_prepare_enable(instance->ref_clk); |
| 991 | if (ret) { |
| 992 | dev_err(tphy->dev, "failed to enable ref_clk\n"); |
| 993 | return ret; |
| 994 | } |
| 995 | |
| 996 | switch (instance->type) { |
| 997 | case PHY_TYPE_USB2: |
| 998 | u2_phy_instance_init(tphy, instance); |
| 999 | u2_phy_props_set(tphy, instance); |
| 1000 | break; |
| 1001 | case PHY_TYPE_USB3: |
| 1002 | u3_phy_instance_init(tphy, instance); |
| 1003 | break; |
| 1004 | case PHY_TYPE_PCIE: |
| 1005 | pcie_phy_instance_init(tphy, instance); |
| 1006 | break; |
| 1007 | case PHY_TYPE_SATA: |
| 1008 | sata_phy_instance_init(tphy, instance); |
| 1009 | break; |
| 1010 | default: |
| 1011 | dev_err(tphy->dev, "incompatible PHY type\n"); |
| 1012 | return -EINVAL; |
| 1013 | } |
| 1014 | |
| 1015 | return 0; |
| 1016 | } |
| 1017 | |
| 1018 | static int mtk_phy_power_on(struct phy *phy) |
| 1019 | { |
| 1020 | struct mtk_phy_instance *instance = phy_get_drvdata(phy); |
| 1021 | struct mtk_tphy *tphy = dev_get_drvdata(phy->dev.parent); |
| 1022 | |
| 1023 | if (instance->type == PHY_TYPE_USB2) { |
| 1024 | u2_phy_instance_power_on(tphy, instance); |
| 1025 | hs_slew_rate_calibrate(tphy, instance); |
| 1026 | } else if (instance->type == PHY_TYPE_PCIE) { |
| 1027 | pcie_phy_instance_power_on(tphy, instance); |
| 1028 | } |
| 1029 | |
| 1030 | return 0; |
| 1031 | } |
| 1032 | |
| 1033 | static int mtk_phy_power_off(struct phy *phy) |
| 1034 | { |
| 1035 | struct mtk_phy_instance *instance = phy_get_drvdata(phy); |
| 1036 | struct mtk_tphy *tphy = dev_get_drvdata(phy->dev.parent); |
| 1037 | |
| 1038 | if (instance->type == PHY_TYPE_USB2) |
| 1039 | u2_phy_instance_power_off(tphy, instance); |
| 1040 | else if (instance->type == PHY_TYPE_PCIE) |
| 1041 | pcie_phy_instance_power_off(tphy, instance); |
| 1042 | |
| 1043 | return 0; |
| 1044 | } |
| 1045 | |
| 1046 | static int mtk_phy_exit(struct phy *phy) |
| 1047 | { |
| 1048 | struct mtk_phy_instance *instance = phy_get_drvdata(phy); |
| 1049 | struct mtk_tphy *tphy = dev_get_drvdata(phy->dev.parent); |
| 1050 | |
| 1051 | if (instance->type == PHY_TYPE_USB2) |
| 1052 | u2_phy_instance_exit(tphy, instance); |
| 1053 | else if (instance->type == PHY_TYPE_PCIE) |
| 1054 | pcie_phy_instance_exit(tphy, instance); |
| 1055 | |
| 1056 | clk_disable_unprepare(instance->ref_clk); |
| 1057 | clk_disable_unprepare(tphy->u3phya_ref); |
| 1058 | return 0; |
| 1059 | } |
| 1060 | |
| 1061 | static int mtk_phy_set_mode(struct phy *phy, enum phy_mode mode) |
| 1062 | { |
| 1063 | struct mtk_phy_instance *instance = phy_get_drvdata(phy); |
| 1064 | struct mtk_tphy *tphy = dev_get_drvdata(phy->dev.parent); |
| 1065 | |
| 1066 | if (instance->type == PHY_TYPE_USB2) |
| 1067 | u2_phy_instance_set_mode(tphy, instance, mode); |
| 1068 | else if (instance->type == PHY_TYPE_PCIE) |
| 1069 | pcie_phy_instance_set_mode(tphy, instance, mode); |
| 1070 | |
| 1071 | return 0; |
| 1072 | } |
| 1073 | |
| 1074 | static struct phy *mtk_phy_xlate(struct device *dev, |
| 1075 | struct of_phandle_args *args) |
| 1076 | { |
| 1077 | struct mtk_tphy *tphy = dev_get_drvdata(dev); |
| 1078 | struct mtk_phy_instance *instance = NULL; |
| 1079 | struct device_node *phy_np = args->np; |
| 1080 | int index; |
| 1081 | |
| 1082 | if (args->args_count != 1) { |
| 1083 | dev_err(dev, "invalid number of cells in 'phy' property\n"); |
| 1084 | return ERR_PTR(-EINVAL); |
| 1085 | } |
| 1086 | |
| 1087 | for (index = 0; index < tphy->nphys; index++) |
| 1088 | if (phy_np == tphy->phys[index]->phy->dev.of_node) { |
| 1089 | instance = tphy->phys[index]; |
| 1090 | break; |
| 1091 | } |
| 1092 | |
| 1093 | if (!instance) { |
| 1094 | dev_err(dev, "failed to find appropriate phy\n"); |
| 1095 | return ERR_PTR(-EINVAL); |
| 1096 | } |
| 1097 | |
| 1098 | instance->type = args->args[0]; |
| 1099 | if (!(instance->type == PHY_TYPE_USB2 || |
| 1100 | instance->type == PHY_TYPE_USB3 || |
| 1101 | instance->type == PHY_TYPE_PCIE || |
| 1102 | instance->type == PHY_TYPE_SATA)) { |
| 1103 | dev_err(dev, "unsupported device type: %d\n", instance->type); |
| 1104 | return ERR_PTR(-EINVAL); |
| 1105 | } |
| 1106 | |
| 1107 | if (tphy->pdata->version == MTK_PHY_V1) { |
| 1108 | phy_v1_banks_init(tphy, instance); |
| 1109 | } else if (tphy->pdata->version == MTK_PHY_V2) { |
| 1110 | phy_v2_banks_init(tphy, instance); |
| 1111 | } else { |
| 1112 | dev_err(dev, "phy version is not supported\n"); |
| 1113 | return ERR_PTR(-EINVAL); |
| 1114 | } |
| 1115 | |
| 1116 | phy_parse_property(tphy, instance); |
| 1117 | |
| 1118 | return instance->phy; |
| 1119 | } |
| 1120 | |
| 1121 | static const struct phy_ops mtk_tphy_ops = { |
| 1122 | .init = mtk_phy_init, |
| 1123 | .exit = mtk_phy_exit, |
| 1124 | .power_on = mtk_phy_power_on, |
| 1125 | .power_off = mtk_phy_power_off, |
| 1126 | .set_mode = mtk_phy_set_mode, |
| 1127 | .owner = THIS_MODULE, |
| 1128 | }; |
| 1129 | |
| 1130 | static const struct mtk_phy_pdata tphy_v1_pdata = { |
| 1131 | .avoid_rx_sen_degradation = false, |
| 1132 | .version = MTK_PHY_V1, |
| 1133 | }; |
| 1134 | |
| 1135 | static const struct mtk_phy_pdata tphy_v2_pdata = { |
| 1136 | .avoid_rx_sen_degradation = false, |
| 1137 | .version = MTK_PHY_V2, |
| 1138 | }; |
| 1139 | |
| 1140 | static const struct mtk_phy_pdata mt8173_pdata = { |
| 1141 | .avoid_rx_sen_degradation = true, |
| 1142 | .version = MTK_PHY_V1, |
| 1143 | }; |
| 1144 | |
| 1145 | static const struct of_device_id mtk_tphy_id_table[] = { |
| 1146 | { .compatible = "mediatek,mt2701-u3phy", .data = &tphy_v1_pdata }, |
| 1147 | { .compatible = "mediatek,mt2712-u3phy", .data = &tphy_v2_pdata }, |
| 1148 | { .compatible = "mediatek,mt8173-u3phy", .data = &mt8173_pdata }, |
| 1149 | { .compatible = "mediatek,generic-tphy-v1", .data = &tphy_v1_pdata }, |
| 1150 | { .compatible = "mediatek,generic-tphy-v2", .data = &tphy_v2_pdata }, |
| 1151 | { }, |
| 1152 | }; |
| 1153 | MODULE_DEVICE_TABLE(of, mtk_tphy_id_table); |
| 1154 | |
| 1155 | static int mtk_tphy_probe(struct platform_device *pdev) |
| 1156 | { |
| 1157 | struct device *dev = &pdev->dev; |
| 1158 | struct device_node *np = dev->of_node; |
| 1159 | struct device_node *child_np; |
| 1160 | struct phy_provider *provider; |
| 1161 | struct resource *sif_res; |
| 1162 | struct mtk_tphy *tphy; |
| 1163 | struct resource res; |
| 1164 | int port, retval; |
| 1165 | |
| 1166 | tphy = devm_kzalloc(dev, sizeof(*tphy), GFP_KERNEL); |
| 1167 | if (!tphy) |
| 1168 | return -ENOMEM; |
| 1169 | |
| 1170 | tphy->pdata = of_device_get_match_data(dev); |
| 1171 | if (!tphy->pdata) |
| 1172 | return -EINVAL; |
| 1173 | |
| 1174 | tphy->nphys = of_get_child_count(np); |
| 1175 | tphy->phys = devm_kcalloc(dev, tphy->nphys, |
| 1176 | sizeof(*tphy->phys), GFP_KERNEL); |
| 1177 | if (!tphy->phys) |
| 1178 | return -ENOMEM; |
| 1179 | |
| 1180 | tphy->dev = dev; |
| 1181 | platform_set_drvdata(pdev, tphy); |
| 1182 | |
| 1183 | sif_res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 1184 | /* SATA phy of V1 needn't it if not shared with PCIe or USB */ |
| 1185 | if (sif_res && tphy->pdata->version == MTK_PHY_V1) { |
| 1186 | /* get banks shared by multiple phys */ |
| 1187 | tphy->sif_base = devm_ioremap_resource(dev, sif_res); |
| 1188 | if (IS_ERR(tphy->sif_base)) { |
| 1189 | dev_err(dev, "failed to remap sif regs\n"); |
| 1190 | return PTR_ERR(tphy->sif_base); |
| 1191 | } |
| 1192 | } |
| 1193 | |
| 1194 | /* it's deprecated, make it optional for backward compatibility */ |
| 1195 | tphy->u3phya_ref = devm_clk_get(dev, "u3phya_ref"); |
| 1196 | if (IS_ERR(tphy->u3phya_ref)) { |
| 1197 | if (PTR_ERR(tphy->u3phya_ref) == -EPROBE_DEFER) |
| 1198 | return -EPROBE_DEFER; |
| 1199 | |
| 1200 | tphy->u3phya_ref = NULL; |
| 1201 | } |
| 1202 | |
| 1203 | tphy->src_ref_clk = U3P_REF_CLK; |
| 1204 | tphy->src_coef = U3P_SLEW_RATE_COEF; |
| 1205 | /* update parameters of slew rate calibrate if exist */ |
| 1206 | device_property_read_u32(dev, "mediatek,src-ref-clk-mhz", |
| 1207 | &tphy->src_ref_clk); |
| 1208 | device_property_read_u32(dev, "mediatek,src-coef", &tphy->src_coef); |
| 1209 | |
| 1210 | port = 0; |
| 1211 | for_each_child_of_node(np, child_np) { |
| 1212 | struct mtk_phy_instance *instance; |
| 1213 | struct phy *phy; |
| 1214 | |
| 1215 | instance = devm_kzalloc(dev, sizeof(*instance), GFP_KERNEL); |
| 1216 | if (!instance) { |
| 1217 | retval = -ENOMEM; |
| 1218 | goto put_child; |
| 1219 | } |
| 1220 | |
| 1221 | tphy->phys[port] = instance; |
| 1222 | |
| 1223 | phy = devm_phy_create(dev, child_np, &mtk_tphy_ops); |
| 1224 | if (IS_ERR(phy)) { |
| 1225 | dev_err(dev, "failed to create phy\n"); |
| 1226 | retval = PTR_ERR(phy); |
| 1227 | goto put_child; |
| 1228 | } |
| 1229 | |
| 1230 | retval = of_address_to_resource(child_np, 0, &res); |
| 1231 | if (retval) { |
| 1232 | dev_err(dev, "failed to get address resource(id-%d)\n", |
| 1233 | port); |
| 1234 | goto put_child; |
| 1235 | } |
| 1236 | |
| 1237 | instance->port_base = devm_ioremap_resource(&phy->dev, &res); |
| 1238 | if (IS_ERR(instance->port_base)) { |
| 1239 | dev_err(dev, "failed to remap phy regs\n"); |
| 1240 | retval = PTR_ERR(instance->port_base); |
| 1241 | goto put_child; |
| 1242 | } |
| 1243 | |
| 1244 | instance->phy = phy; |
| 1245 | instance->index = port; |
| 1246 | phy_set_drvdata(phy, instance); |
| 1247 | port++; |
| 1248 | |
| 1249 | /* if deprecated clock is provided, ignore instance's one */ |
| 1250 | if (tphy->u3phya_ref) |
| 1251 | continue; |
| 1252 | |
| 1253 | instance->ref_clk = devm_clk_get(&phy->dev, "ref"); |
| 1254 | if (IS_ERR(instance->ref_clk)) { |
| 1255 | dev_err(dev, "failed to get ref_clk(id-%d)\n", port); |
| 1256 | retval = PTR_ERR(instance->ref_clk); |
| 1257 | goto put_child; |
| 1258 | } |
| 1259 | } |
| 1260 | |
| 1261 | provider = devm_of_phy_provider_register(dev, mtk_phy_xlate); |
| 1262 | |
| 1263 | return PTR_ERR_OR_ZERO(provider); |
| 1264 | put_child: |
| 1265 | of_node_put(child_np); |
| 1266 | return retval; |
| 1267 | } |
| 1268 | |
| 1269 | static struct platform_driver mtk_tphy_driver = { |
| 1270 | .probe = mtk_tphy_probe, |
| 1271 | .driver = { |
| 1272 | .name = "mtk-tphy", |
| 1273 | .of_match_table = mtk_tphy_id_table, |
| 1274 | }, |
| 1275 | }; |
| 1276 | |
| 1277 | module_platform_driver(mtk_tphy_driver); |
| 1278 | |
| 1279 | MODULE_AUTHOR("Chunfeng Yun <chunfeng.yun@mediatek.com>"); |
| 1280 | MODULE_DESCRIPTION("MediaTek T-PHY driver"); |
| 1281 | MODULE_LICENSE("GPL v2"); |