| xj | b04a402 | 2021-11-25 15:01:52 +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 | #include <linux/sysfs.h> |
| 19 | |
| 20 | /* version V1 sub-banks offset base address */ |
| 21 | /* banks shared by multiple phys */ |
| 22 | #define SSUSB_SIFSLV_V1_SPLLC 0x000 /* shared by u3 phys */ |
| 23 | #define SSUSB_SIFSLV_V1_U2FREQ 0x100 /* shared by u2 phys */ |
| 24 | #define SSUSB_SIFSLV_V1_CHIP 0x300 /* shared by u3 phys */ |
| 25 | /* u2 phy bank */ |
| 26 | #define SSUSB_SIFSLV_V1_U2PHY_COM 0x000 |
| 27 | /* u3/pcie/sata phy banks */ |
| 28 | #define SSUSB_SIFSLV_V1_U3PHYD 0x000 |
| 29 | #define SSUSB_SIFSLV_V1_U3PHYA 0x200 |
| 30 | |
| 31 | /* version V2 sub-banks offset base address */ |
| 32 | /* u2 phy banks */ |
| 33 | #define SSUSB_SIFSLV_V2_MISC 0x000 |
| 34 | #define SSUSB_SIFSLV_V2_U2FREQ 0x100 |
| 35 | #define SSUSB_SIFSLV_V2_U2PHY_COM 0x300 |
| 36 | /* u3/pcie/sata phy banks */ |
| 37 | #define SSUSB_SIFSLV_V2_SPLLC 0x000 |
| 38 | #define SSUSB_SIFSLV_V2_CHIP 0x100 |
| 39 | #define SSUSB_SIFSLV_V2_U3PHYD 0x200 |
| 40 | #define SSUSB_SIFSLV_V2_U3PHYA 0x400 |
| 41 | |
| 42 | #define U3P_USBPHYACR0 0x000 |
| 43 | #define PA0_RG_U2PLL_FORCE_ON BIT(15) |
| 44 | #define PA0_RG_USB20_INTR_EN BIT(5) |
| 45 | |
| 46 | #define U3P_USBPHYACR1 0x004 |
| 47 | #define PA1_RG_VRT_SEL GENMASK(14, 12) |
| 48 | #define PA1_RG_VRT_SEL_VAL(x) ((0x7 & (x)) << 12) |
| 49 | #define PA1_RG_VRT_SEL_MASK (0x7) |
| 50 | #define PA1_RG_VRT_SEL_OFST (12) |
| 51 | #define PA1_RG_TERM_SEL GENMASK(10, 8) |
| 52 | #define PA1_RG_TERM_SEL_VAL(x) ((0x7 & (x)) << 8) |
| 53 | #define PA1_RG_TERM_SEL_MASK (0x7) |
| 54 | #define PA1_RG_TERM_SEL_OFST (8) |
| 55 | |
| 56 | #define U3P_USBPHYACR2 0x008 |
| 57 | #define PA2_RG_SIF_U2PLL_FORCE_EN BIT(18) |
| 58 | #define PA2_RG_CLKREF_REV_PLL_POSDIV GENMASK(14, 12) |
| 59 | #define PA2_RG_CLKREF_REV_PLL_POSDIV_VAL(x) ((0x7 & (x)) << 12) |
| 60 | #define PA2_RG_CLKREF_REV_PLL_FBKSEL BIT(11) |
| 61 | |
| 62 | #define U3P_USBPHYACR5 0x014 |
| 63 | #define PA5_RG_U2_HSTX_SRCAL_EN BIT(15) |
| 64 | #define PA5_RG_U2_HSTX_SRCTRL GENMASK(14, 12) |
| 65 | #define PA5_RG_U2_HSTX_SRCTRL_VAL(x) ((0x7 & (x)) << 12) |
| 66 | #define PA5_RG_U2_HS_100U_U3_EN BIT(11) |
| 67 | |
| 68 | #define U3P_USBPHYACR6 0x018 |
| 69 | #define PA6_RG_U2_PHY_REV6 GENMASK(31, 30) |
| 70 | #define PA6_RG_U2_PHY_REV6_VAL(x) ((0x3 & (x)) << 30) |
| 71 | #define PA6_RG_U2_PHY_REV6_MASK (0x3) |
| 72 | #define PA6_RG_U2_PHY_REV6_OFET (30) |
| 73 | #define PA6_RG_U2_PHY_REV4 BIT(28) |
| 74 | #define PA6_RG_U2_PHY_REV1 BIT(25) |
| 75 | #define PA6_RG_U2_BC11_SW_EN BIT(23) |
| 76 | #define PA6_RG_U2_OTG_VBUSCMP_EN BIT(20) |
| 77 | #define PA6_RG_U2_DISCTH GENMASK(7, 4) |
| 78 | #define PA6_RG_U2_DISCTH_VAL(x) ((0xf & (x)) << 4) |
| 79 | #define PA6_RG_U2_DISCTH_MASK (0xf) |
| 80 | #define PA6_RG_U2_DISCTH_OFET (4) |
| 81 | #define PA6_RG_U2_SQTH GENMASK(3, 0) |
| 82 | #define PA6_RG_U2_SQTH_VAL(x) (0xf & (x)) |
| 83 | |
| 84 | #define U3P_U2PHYACR4 0x020 |
| 85 | #define P2C_RG_USB20_DP_100K_MODE BIT(18) |
| 86 | #define P2C_RG_USB20_DM_100K_EN BIT(17) |
| 87 | #define P2C_USB20_DP_100K_EN BIT(16) |
| 88 | #define P2C_RG_USB20_GPIO_CTL BIT(9) |
| 89 | #define P2C_USB20_GPIO_MODE BIT(8) |
| 90 | #define P2C_U2_GPIO_CTR_MSK (P2C_RG_USB20_GPIO_CTL | P2C_USB20_GPIO_MODE) |
| 91 | |
| 92 | #define U3D_U2PHYDCR0 0x060 |
| 93 | #define P2C_RG_SIF_U2PLL_FORCE_ON BIT(24) |
| 94 | |
| 95 | #define U3P_U2PHYDTM0 0x068 |
| 96 | #define P2C_FORCE_UART_EN BIT(26) |
| 97 | #define P2C_FORCE_DATAIN BIT(23) |
| 98 | #define P2C_FORCE_DM_PULLDOWN BIT(21) |
| 99 | #define P2C_FORCE_DP_PULLDOWN BIT(20) |
| 100 | #define P2C_FORCE_XCVRSEL BIT(19) |
| 101 | #define P2C_FORCE_SUSPENDM BIT(18) |
| 102 | #define P2C_FORCE_TERMSEL BIT(17) |
| 103 | #define P2C_RG_DATAIN GENMASK(13, 10) |
| 104 | #define P2C_RG_DATAIN_VAL(x) ((0xf & (x)) << 10) |
| 105 | #define P2C_RG_DMPULLDOWN BIT(7) |
| 106 | #define P2C_RG_DPPULLDOWN BIT(6) |
| 107 | #define P2C_RG_XCVRSEL GENMASK(5, 4) |
| 108 | #define P2C_RG_XCVRSEL_VAL(x) ((0x3 & (x)) << 4) |
| 109 | #define P2C_RG_SUSPENDM BIT(3) |
| 110 | #define P2C_RG_TERMSEL BIT(2) |
| 111 | #define P2C_DTM0_PART_MASK \ |
| 112 | (P2C_FORCE_DATAIN | P2C_FORCE_DM_PULLDOWN | \ |
| 113 | P2C_FORCE_DP_PULLDOWN | P2C_FORCE_XCVRSEL | \ |
| 114 | P2C_FORCE_TERMSEL | P2C_RG_DMPULLDOWN | \ |
| 115 | P2C_RG_DPPULLDOWN | P2C_RG_TERMSEL) |
| 116 | |
| 117 | #define U3P_U2PHYDTM1 0x06C |
| 118 | #define P2C_RG_UART_EN BIT(16) |
| 119 | #define P2C_FORCE_IDDIG BIT(9) |
| 120 | #define P2C_RG_VBUSVALID BIT(5) |
| 121 | #define P2C_RG_SESSEND BIT(4) |
| 122 | #define P2C_RG_AVALID BIT(2) |
| 123 | #define P2C_RG_IDDIG BIT(1) |
| 124 | |
| 125 | #define U3P_U2PHYBC12C 0x080 |
| 126 | #define P2C_RG_CHGDT_EN BIT(0) |
| 127 | |
| 128 | #define U3P_U3_CHIP_GPIO_CTLD 0x0c |
| 129 | #define P3C_REG_IP_SW_RST BIT(31) |
| 130 | #define P3C_MCU_BUS_CK_GATE_EN BIT(30) |
| 131 | #define P3C_FORCE_IP_SW_RST BIT(29) |
| 132 | |
| 133 | #define U3P_U3_CHIP_GPIO_CTLE 0x10 |
| 134 | #define P3C_RG_SWRST_U3_PHYD BIT(25) |
| 135 | #define P3C_RG_SWRST_U3_PHYD_FORCE_EN BIT(24) |
| 136 | |
| 137 | #define U3P_U3_PHYA_REG0 0x000 |
| 138 | #define P3A_RG_CLKDRV_OFF GENMASK(3, 2) |
| 139 | #define P3A_RG_CLKDRV_OFF_VAL(x) ((0x3 & (x)) << 2) |
| 140 | |
| 141 | #define U3P_U3_PHYA_REG1 0x004 |
| 142 | #define P3A_RG_CLKDRV_AMP GENMASK(31, 29) |
| 143 | #define P3A_RG_CLKDRV_AMP_VAL(x) ((0x7 & (x)) << 29) |
| 144 | #define RG_SSUSB_VUSB10_ON (1<<29) |
| 145 | #define RG_SSUSB_VUSB10_ON_OFST (29) |
| 146 | |
| 147 | #define U3P_U3_PHYA_REG6 0x018 |
| 148 | #define P3A_RG_TX_EIDLE_CM GENMASK(31, 28) |
| 149 | #define P3A_RG_TX_EIDLE_CM_VAL(x) ((0xf & (x)) << 28) |
| 150 | |
| 151 | #define U3P_U3_PHYA_REG9 0x024 |
| 152 | #define P3A_RG_RX_DAC_MUX GENMASK(5, 1) |
| 153 | #define P3A_RG_RX_DAC_MUX_VAL(x) ((0x1f & (x)) << 1) |
| 154 | |
| 155 | #define U3P_U3_PHYA_REG11 0x044 |
| 156 | #define RG_SSUSB_PLL_IBAND_FIX_EN BIT(3) |
| 157 | |
| 158 | #define U3P_U3_PHYA_DA_REG0 0x100 |
| 159 | #define P3A_RG_XTAL_EXT_PE2H GENMASK(17, 16) |
| 160 | #define P3A_RG_XTAL_EXT_PE2H_VAL(x) ((0x3 & (x)) << 16) |
| 161 | #define P3A_RG_XTAL_EXT_PE1H GENMASK(13, 12) |
| 162 | #define P3A_RG_XTAL_EXT_PE1H_VAL(x) ((0x3 & (x)) << 12) |
| 163 | #define P3A_RG_XTAL_EXT_EN_U3 GENMASK(11, 10) |
| 164 | #define P3A_RG_XTAL_EXT_EN_U3_VAL(x) ((0x3 & (x)) << 10) |
| 165 | |
| 166 | #define U3P_U3_PHYA_DA_REG4 0x108 |
| 167 | #define P3A_RG_PLL_DIVEN_PE2H GENMASK(21, 19) |
| 168 | #define P3A_RG_PLL_BC_PE2H GENMASK(7, 6) |
| 169 | #define P3A_RG_PLL_BC_PE2H_VAL(x) ((0x3 & (x)) << 6) |
| 170 | |
| 171 | #define U3P_U3_PHYA_DA_REG5 0x10c |
| 172 | #define P3A_RG_PLL_BR_PE2H GENMASK(29, 28) |
| 173 | #define P3A_RG_PLL_BR_PE2H_VAL(x) ((0x3 & (x)) << 28) |
| 174 | #define P3A_RG_PLL_IC_PE2H GENMASK(15, 12) |
| 175 | #define P3A_RG_PLL_IC_PE2H_VAL(x) ((0xf & (x)) << 12) |
| 176 | |
| 177 | #define U3P_U3_PHYA_DA_REG6 0x110 |
| 178 | #define P3A_RG_PLL_IR_PE2H GENMASK(19, 16) |
| 179 | #define P3A_RG_PLL_IR_PE2H_VAL(x) ((0xf & (x)) << 16) |
| 180 | |
| 181 | #define U3P_U3_PHYA_DA_REG7 0x114 |
| 182 | #define P3A_RG_PLL_BP_PE2H GENMASK(19, 16) |
| 183 | #define P3A_RG_PLL_BP_PE2H_VAL(x) ((0xf & (x)) << 16) |
| 184 | |
| 185 | #define U3P_U3_PHYA_DA_REG20 0x13c |
| 186 | #define P3A_RG_PLL_DELTA1_PE2H GENMASK(31, 16) |
| 187 | #define P3A_RG_PLL_DELTA1_PE2H_VAL(x) ((0xffff & (x)) << 16) |
| 188 | |
| 189 | #define U3P_U3_PHYA_DA_REG25 0x148 |
| 190 | #define P3A_RG_PLL_DELTA_PE2H GENMASK(15, 0) |
| 191 | #define P3A_RG_PLL_DELTA_PE2H_VAL(x) (0xffff & (x)) |
| 192 | |
| 193 | #define U3P_U3_PHYD_MIX0 0x000 |
| 194 | |
| 195 | #define U3P_U3_PHYD_LFPS1 0x00c |
| 196 | #define P3D_RG_FWAKE_TH GENMASK(21, 16) |
| 197 | #define P3D_RG_FWAKE_TH_VAL(x) ((0x3f & (x)) << 16) |
| 198 | |
| 199 | #define U3P_U3_PHYD_RX0 0x02c |
| 200 | |
| 201 | #define U3P_U3_PHYD_T2RLB 0x030 |
| 202 | |
| 203 | #define U3P_U3_PHYD_PIPE0 0x040 |
| 204 | |
| 205 | #define U3P_U3_PHYD_CDR1 0x05c |
| 206 | #define P3D_RG_CDR_BIR_LTD1 GENMASK(28, 24) |
| 207 | #define P3D_RG_CDR_BIR_LTD1_VAL(x) ((0x1f & (x)) << 24) |
| 208 | #define P3D_RG_CDR_BIR_LTD0 GENMASK(12, 8) |
| 209 | #define P3D_RG_CDR_BIR_LTD0_VAL(x) ((0x1f & (x)) << 8) |
| 210 | |
| 211 | #define U3P_U3_PHYD_RXDET1 0x128 |
| 212 | #define P3D_RG_RXDET_STB2_SET GENMASK(17, 9) |
| 213 | #define P3D_RG_RXDET_STB2_SET_VAL(x) ((0x1ff & (x)) << 9) |
| 214 | |
| 215 | #define U3P_U3_PHYD_RXDET2 0x12c |
| 216 | #define P3D_RG_RXDET_STB2_SET_P3 GENMASK(8, 0) |
| 217 | #define P3D_RG_RXDET_STB2_SET_P3_VAL(x) (0x1ff & (x)) |
| 218 | |
| 219 | #define U3P_SPLLC_XTALCTL3 0x018 |
| 220 | #define XC3_RG_U3_XTAL_RX_PWD BIT(9) |
| 221 | #define XC3_RG_U3_FRC_XTAL_RX_PWD BIT(8) |
| 222 | |
| 223 | #define U3P_U2FREQ_FMCR0 0x00 |
| 224 | #define P2F_RG_MONCLK_SEL GENMASK(27, 26) |
| 225 | #define P2F_RG_MONCLK_SEL_VAL(x) ((0x3 & (x)) << 26) |
| 226 | #define P2F_RG_FREQDET_EN BIT(24) |
| 227 | #define P2F_RG_CYCLECNT GENMASK(23, 0) |
| 228 | #define P2F_RG_CYCLECNT_VAL(x) ((P2F_RG_CYCLECNT) & (x)) |
| 229 | |
| 230 | #define U3P_U2FREQ_VALUE 0x0c |
| 231 | |
| 232 | #define U3P_U2FREQ_FMMONR1 0x10 |
| 233 | #define P2F_USB_FM_VALID BIT(0) |
| 234 | #define P2F_RG_FRCK_EN BIT(8) |
| 235 | |
| 236 | #define U3P_REF_CLK 26 /* MHZ */ |
| 237 | #define U3P_SLEW_RATE_COEF 28 |
| 238 | #define U3P_SR_COEF_DIVISOR 1000 |
| 239 | #define U3P_FM_DET_CYCLE_CNT 1024 |
| 240 | |
| 241 | /* SATA register setting */ |
| 242 | #define PHYD_CTRL_SIGNAL_MODE4 0x1c |
| 243 | /* CDR Charge Pump P-path current adjustment */ |
| 244 | #define RG_CDR_BICLTD1_GEN1_MSK GENMASK(23, 20) |
| 245 | #define RG_CDR_BICLTD1_GEN1_VAL(x) ((0xf & (x)) << 20) |
| 246 | #define RG_CDR_BICLTD0_GEN1_MSK GENMASK(11, 8) |
| 247 | #define RG_CDR_BICLTD0_GEN1_VAL(x) ((0xf & (x)) << 8) |
| 248 | |
| 249 | #define PHYD_DESIGN_OPTION2 0x24 |
| 250 | /* Symbol lock count selection */ |
| 251 | #define RG_LOCK_CNT_SEL_MSK GENMASK(5, 4) |
| 252 | #define RG_LOCK_CNT_SEL_VAL(x) ((0x3 & (x)) << 4) |
| 253 | |
| 254 | #define PHYD_DESIGN_OPTION9 0x40 |
| 255 | /* COMWAK GAP width window */ |
| 256 | #define RG_TG_MAX_MSK GENMASK(20, 16) |
| 257 | #define RG_TG_MAX_VAL(x) ((0x1f & (x)) << 16) |
| 258 | /* COMINIT GAP width window */ |
| 259 | #define RG_T2_MAX_MSK GENMASK(13, 8) |
| 260 | #define RG_T2_MAX_VAL(x) ((0x3f & (x)) << 8) |
| 261 | /* COMWAK GAP width window */ |
| 262 | #define RG_TG_MIN_MSK GENMASK(7, 5) |
| 263 | #define RG_TG_MIN_VAL(x) ((0x7 & (x)) << 5) |
| 264 | /* COMINIT GAP width window */ |
| 265 | #define RG_T2_MIN_MSK GENMASK(4, 0) |
| 266 | #define RG_T2_MIN_VAL(x) (0x1f & (x)) |
| 267 | |
| 268 | #define ANA_RG_CTRL_SIGNAL1 0x4c |
| 269 | /* TX driver tail current control for 0dB de-empahsis mdoe for Gen1 speed */ |
| 270 | #define RG_IDRV_0DB_GEN1_MSK GENMASK(13, 8) |
| 271 | #define RG_IDRV_0DB_GEN1_VAL(x) ((0x3f & (x)) << 8) |
| 272 | |
| 273 | #define ANA_RG_CTRL_SIGNAL4 0x58 |
| 274 | #define RG_CDR_BICLTR_GEN1_MSK GENMASK(23, 20) |
| 275 | #define RG_CDR_BICLTR_GEN1_VAL(x) ((0xf & (x)) << 20) |
| 276 | /* Loop filter R1 resistance adjustment for Gen1 speed */ |
| 277 | #define RG_CDR_BR_GEN2_MSK GENMASK(10, 8) |
| 278 | #define RG_CDR_BR_GEN2_VAL(x) ((0x7 & (x)) << 8) |
| 279 | |
| 280 | #define ANA_RG_CTRL_SIGNAL6 0x60 |
| 281 | /* I-path capacitance adjustment for Gen1 */ |
| 282 | #define RG_CDR_BC_GEN1_MSK GENMASK(28, 24) |
| 283 | #define RG_CDR_BC_GEN1_VAL(x) ((0x1f & (x)) << 24) |
| 284 | #define RG_CDR_BIRLTR_GEN1_MSK GENMASK(4, 0) |
| 285 | #define RG_CDR_BIRLTR_GEN1_VAL(x) (0x1f & (x)) |
| 286 | |
| 287 | #define ANA_EQ_EYE_CTRL_SIGNAL1 0x6c |
| 288 | /* RX Gen1 LEQ tuning step */ |
| 289 | #define RG_EQ_DLEQ_LFI_GEN1_MSK GENMASK(11, 8) |
| 290 | #define RG_EQ_DLEQ_LFI_GEN1_VAL(x) ((0xf & (x)) << 8) |
| 291 | |
| 292 | #define ANA_EQ_EYE_CTRL_SIGNAL4 0xd8 |
| 293 | #define RG_CDR_BIRLTD0_GEN1_MSK GENMASK(20, 16) |
| 294 | #define RG_CDR_BIRLTD0_GEN1_VAL(x) ((0x1f & (x)) << 16) |
| 295 | |
| 296 | #define ANA_EQ_EYE_CTRL_SIGNAL5 0xdc |
| 297 | #define RG_CDR_BIRLTD0_GEN3_MSK GENMASK(4, 0) |
| 298 | #define RG_CDR_BIRLTD0_GEN3_VAL(x) (0x1f & (x)) |
| 299 | |
| 300 | #define PHY_MODE_BC11_SW_SET 1 |
| 301 | #define PHY_MODE_BC11_SW_CLR 2 |
| 302 | |
| 303 | enum mtk_phy_version { |
| 304 | MTK_PHY_V1 = 1, |
| 305 | MTK_PHY_V2, |
| 306 | }; |
| 307 | |
| 308 | struct mtk_phy_pdata { |
| 309 | /* avoid RX sensitivity level degradation only for mt8173 */ |
| 310 | bool avoid_rx_sen_degradation; |
| 311 | enum mtk_phy_version version; |
| 312 | }; |
| 313 | |
| 314 | struct u2phy_banks { |
| 315 | void __iomem *misc; |
| 316 | void __iomem *fmreg; |
| 317 | void __iomem *com; |
| 318 | }; |
| 319 | |
| 320 | struct u3phy_banks { |
| 321 | void __iomem *spllc; |
| 322 | void __iomem *chip; |
| 323 | void __iomem *phyd; /* include u3phyd_bank2 */ |
| 324 | void __iomem *phya; /* include u3phya_da */ |
| 325 | }; |
| 326 | |
| 327 | struct mtk_phy_instance { |
| 328 | struct phy *phy; |
| 329 | void __iomem *port_base; |
| 330 | union { |
| 331 | struct u2phy_banks u2_banks; |
| 332 | struct u3phy_banks u3_banks; |
| 333 | }; |
| 334 | struct clk *ref_clk; /* reference clock of anolog phy */ |
| 335 | u32 index; |
| 336 | u8 type; |
| 337 | int eye_src; |
| 338 | int eye_vrt; |
| 339 | int eye_term; |
| 340 | int eye_rev6; |
| 341 | int eye_disc; |
| 342 | int rx_sqth; |
| 343 | bool bc12_en; |
| 344 | }; |
| 345 | |
| 346 | struct mtk_tphy { |
| 347 | struct device *dev; |
| 348 | void __iomem *sif_base; /* only shared sif */ |
| 349 | /* deprecated, use @ref_clk instead in phy instance */ |
| 350 | struct clk *u3phya_ref; /* reference clock of usb3 anolog phy */ |
| 351 | const struct mtk_phy_pdata *pdata; |
| 352 | struct mtk_phy_instance **phys; |
| 353 | int nphys; |
| 354 | int src_ref_clk; /* MHZ, reference clock for slew rate calibrate */ |
| 355 | int src_coef; /* coefficient for slew rate calibrate */ |
| 356 | }; |
| 357 | |
| 358 | void cover_val_to_str(u32 val, u8 width, char *str) |
| 359 | { |
| 360 | int i, temp; |
| 361 | |
| 362 | temp = val; |
| 363 | str[width] = '\0'; |
| 364 | for (i = (width - 1); i >= 0; i--) { |
| 365 | if (val % 2) |
| 366 | str[i] = '1'; |
| 367 | else |
| 368 | str[i] = '0'; |
| 369 | val /= 2; |
| 370 | } |
| 371 | } |
| 372 | |
| 373 | /* |
| 374 | * loopback_test: default test pattern |
| 375 | * readl(U3D_PHYD_PIPE0) & |
| 376 | * ~(0x01<<30)) | 0x01<<30, |
| 377 | * ~(0x01<<28)) | 0x00<<28, |
| 378 | * ~(0x03<<26)) | 0x01<<26, |
| 379 | * ~(0x03<<24)) | 0x00<<24, |
| 380 | * ~(0x01<<22)) | 0x00<<22, |
| 381 | * ~(0x01<<21)) | 0x00<<21, |
| 382 | * ~(0x01<<20)) | 0x01<<20. |
| 383 | */ |
| 384 | #define U3P_U3_PHYD_PIPE0_CLR_PATTERN 0x5f700000 |
| 385 | #define U3P_U3_PHYD_PIPE0_SET_PATTERN 0x44100000 |
| 386 | |
| 387 | static ssize_t loopback_test_show(struct device *dev, |
| 388 | struct device_attribute *attr, char *buf) |
| 389 | { |
| 390 | struct mtk_phy_instance *instance = phy_get_drvdata(to_phy(dev)); |
| 391 | struct u3phy_banks *u3_banks = &instance->u3_banks; |
| 392 | int r_pipe0, r_rx0, r_mix0, r_t2rlb; |
| 393 | bool ret = false; |
| 394 | u32 tmp; |
| 395 | |
| 396 | /* VA10 is shared by U3/UFS */ |
| 397 | /* default on and set voltage by PMIC */ |
| 398 | /* off/on in SPM suspend/resume */ |
| 399 | |
| 400 | r_mix0 = readl(u3_banks->phyd + U3P_U3_PHYD_MIX0); |
| 401 | r_rx0 = readl(u3_banks->phyd + U3P_U3_PHYD_RX0); |
| 402 | r_t2rlb = readl(u3_banks->phyd + U3P_U3_PHYD_T2RLB); |
| 403 | r_pipe0 = readl(u3_banks->phyd + U3P_U3_PHYD_PIPE0); |
| 404 | |
| 405 | tmp = readl(u3_banks->phyd + U3P_U3_PHYD_PIPE0); |
| 406 | tmp &= ~(U3P_U3_PHYD_PIPE0_CLR_PATTERN); |
| 407 | tmp |= U3P_U3_PHYD_PIPE0_SET_PATTERN; |
| 408 | writel(tmp, u3_banks->phyd + U3P_U3_PHYD_PIPE0); |
| 409 | |
| 410 | mdelay(10); |
| 411 | |
| 412 | /* T2R loop back disable */ |
| 413 | tmp = readl(u3_banks->phyd + U3P_U3_PHYD_RX0); |
| 414 | tmp &= ~(0x01<<15); |
| 415 | tmp |= 0x00<<15; |
| 416 | writel(tmp, u3_banks->phyd + U3P_U3_PHYD_RX0); |
| 417 | |
| 418 | mdelay(10); |
| 419 | |
| 420 | /* TSEQ lock detect threshold */ |
| 421 | tmp = readl(u3_banks->phyd + U3P_U3_PHYD_MIX0); |
| 422 | tmp &= ~(0x07<<24); |
| 423 | tmp |= 0x07<<24; |
| 424 | writel(tmp, u3_banks->phyd + U3P_U3_PHYD_MIX0); |
| 425 | |
| 426 | /* set default TSEQ polarity check value = 1 */ |
| 427 | tmp = readl(u3_banks->phyd + U3P_U3_PHYD_MIX0); |
| 428 | tmp &= ~(0x01<<28); |
| 429 | tmp |= 0x01<<28; |
| 430 | writel(tmp, u3_banks->phyd + U3P_U3_PHYD_MIX0); |
| 431 | |
| 432 | /* TSEQ polarity check enable */ |
| 433 | tmp = readl(u3_banks->phyd + U3P_U3_PHYD_MIX0); |
| 434 | tmp &= ~(0x01<<29); |
| 435 | tmp |= 0x01<<29; |
| 436 | writel(tmp, u3_banks->phyd + U3P_U3_PHYD_MIX0); |
| 437 | |
| 438 | /* TSEQ decoder enable */ |
| 439 | tmp = readl(u3_banks->phyd + U3P_U3_PHYD_MIX0); |
| 440 | tmp &= ~(0x01<<30); |
| 441 | tmp |= 0x01<<30; |
| 442 | writel(tmp, u3_banks->phyd + U3P_U3_PHYD_MIX0); |
| 443 | |
| 444 | mdelay(10); |
| 445 | |
| 446 | /* set T2R loop back TSEQ length (x 16us) */ |
| 447 | tmp = readl(u3_banks->phyd + U3P_U3_PHYD_T2RLB); |
| 448 | tmp &= ~(0xff<<0); |
| 449 | tmp |= 0xf0<<0; |
| 450 | writel(tmp, u3_banks->phyd + U3P_U3_PHYD_T2RLB); |
| 451 | |
| 452 | /* set T2R loop back BDAT reset period (x 16us) */ |
| 453 | tmp = readl(u3_banks->phyd + U3P_U3_PHYD_T2RLB); |
| 454 | tmp &= ~(0x0f<<12); |
| 455 | tmp |= 0x0f<<12; |
| 456 | writel(tmp, u3_banks->phyd + U3P_U3_PHYD_T2RLB); |
| 457 | |
| 458 | /* T2R loop back pattern select */ |
| 459 | tmp = readl(u3_banks->phyd + U3P_U3_PHYD_T2RLB); |
| 460 | tmp &= ~(0x03<<8); |
| 461 | tmp |= 0x00<<8; |
| 462 | writel(tmp, u3_banks->phyd + U3P_U3_PHYD_T2RLB); |
| 463 | |
| 464 | mdelay(10); |
| 465 | |
| 466 | /* T2R loop back serial mode */ |
| 467 | tmp = readl(u3_banks->phyd + U3P_U3_PHYD_RX0); |
| 468 | tmp &= ~(0x01<<13); |
| 469 | tmp |= 0x01<<13; |
| 470 | writel(tmp, u3_banks->phyd + U3P_U3_PHYD_RX0); |
| 471 | |
| 472 | /* T2R loop back parallel mode = 0 */ |
| 473 | tmp = readl(u3_banks->phyd + U3P_U3_PHYD_RX0); |
| 474 | tmp &= ~(0x01<<12); |
| 475 | tmp |= 0x00<<12; |
| 476 | writel(tmp, u3_banks->phyd + U3P_U3_PHYD_RX0); |
| 477 | |
| 478 | /* T2R loop back mode enable */ |
| 479 | tmp = readl(u3_banks->phyd + U3P_U3_PHYD_RX0); |
| 480 | tmp &= ~(0x01<<11); |
| 481 | tmp |= 0x01<<11; |
| 482 | writel(tmp, u3_banks->phyd + U3P_U3_PHYD_RX0); |
| 483 | |
| 484 | /* T2R loop back enable */ |
| 485 | tmp = readl(u3_banks->phyd + U3P_U3_PHYD_RX0); |
| 486 | tmp &= ~(0x01<<15); |
| 487 | tmp |= 0x01<<15; |
| 488 | writel(tmp, u3_banks->phyd + U3P_U3_PHYD_RX0); |
| 489 | mdelay(100); |
| 490 | |
| 491 | dev_info(dev, "%s, U3 loop back started\n", __func__); |
| 492 | |
| 493 | /* check result */ |
| 494 | tmp = readl(u3_banks->phyd + 0x0b4); |
| 495 | |
| 496 | /* verbose dump */ |
| 497 | dev_info(dev, "rb back : 0x%x\n", tmp); |
| 498 | dev_info(dev, "rb t2rlb_lock : %d\n", (tmp >> 2) & 0x01); |
| 499 | dev_info(dev, "rb t2rlb_pass : %d\n", (tmp >> 3) & 0x01); |
| 500 | dev_info(dev, "rb t2rlb_passth: %d\n", (tmp >> 4) & 0x01); |
| 501 | |
| 502 | /* return result */ |
| 503 | tmp &= 0x0E; |
| 504 | if (tmp == 0x0E) |
| 505 | ret = true; |
| 506 | else |
| 507 | ret = false; |
| 508 | |
| 509 | /* restore settings */ |
| 510 | writel(r_rx0, u3_banks->phyd + U3P_U3_PHYD_RX0); |
| 511 | writel(r_pipe0, u3_banks->phyd + U3P_U3_PHYD_PIPE0); |
| 512 | writel(r_mix0, u3_banks->phyd + U3P_U3_PHYD_MIX0); |
| 513 | writel(r_t2rlb, u3_banks->phyd + U3P_U3_PHYD_T2RLB); |
| 514 | |
| 515 | dev_info(dev, "%s, loopback_test=0x%x\n", __func__, tmp); |
| 516 | |
| 517 | return sprintf(buf, "%d\n", ret); |
| 518 | } |
| 519 | static DEVICE_ATTR_RO(loopback_test); |
| 520 | |
| 521 | static struct attribute *u3_phy_attrs[] = { |
| 522 | &dev_attr_loopback_test.attr, |
| 523 | NULL |
| 524 | }; |
| 525 | |
| 526 | static const struct attribute_group u3_phy_group = { |
| 527 | .attrs = u3_phy_attrs, |
| 528 | }; |
| 529 | |
| 530 | static int u3_phy_sysfs_init(struct mtk_tphy *tphy, |
| 531 | struct mtk_phy_instance *instance) |
| 532 | { |
| 533 | struct device *dev = &instance->phy->dev; |
| 534 | int ret; |
| 535 | |
| 536 | ret = sysfs_create_group(&dev->kobj, &u3_phy_group); |
| 537 | if (ret) |
| 538 | dev_err(dev, "failed to creat sysfs attributes\n"); |
| 539 | |
| 540 | ret = sysfs_create_link(&dev->parent->kobj, &dev->kobj, "u3_phy"); |
| 541 | if (ret) |
| 542 | dev_err(dev, "failed to creat link\n"); |
| 543 | |
| 544 | return ret; |
| 545 | } |
| 546 | |
| 547 | static int u3_phy_sysfs_exit(struct mtk_tphy *tphy, |
| 548 | struct mtk_phy_instance *instance) |
| 549 | { |
| 550 | struct device *dev = &instance->phy->dev; |
| 551 | |
| 552 | sysfs_remove_link(&dev->parent->kobj, "u3_phy"); |
| 553 | sysfs_remove_group(&dev->kobj, &u3_phy_group); |
| 554 | return 0; |
| 555 | } |
| 556 | |
| 557 | static ssize_t vrt_sel_store(struct device *dev, |
| 558 | struct device_attribute *attr, |
| 559 | const char *buf, size_t count) |
| 560 | { |
| 561 | struct mtk_phy_instance *instance = phy_get_drvdata(to_phy(dev)); |
| 562 | struct u2phy_banks *u2_banks = &instance->u2_banks; |
| 563 | void __iomem *com = u2_banks->com; |
| 564 | u32 tmp, val; |
| 565 | |
| 566 | if (kstrtouint(buf, 2, &val)) |
| 567 | return -EINVAL; |
| 568 | |
| 569 | tmp = readl(com + U3P_USBPHYACR1); |
| 570 | tmp &= ~PA1_RG_VRT_SEL; |
| 571 | tmp |= PA1_RG_VRT_SEL_VAL(val); |
| 572 | writel(tmp, com + U3P_USBPHYACR1); |
| 573 | |
| 574 | dev_info(dev, "%s, vrt_sel=%x\n", __func__, val); |
| 575 | return count; |
| 576 | } |
| 577 | |
| 578 | static ssize_t vrt_sel_show(struct device *dev, |
| 579 | struct device_attribute *attr, char *buf) |
| 580 | { |
| 581 | struct mtk_phy_instance *instance = phy_get_drvdata(to_phy(dev)); |
| 582 | struct u2phy_banks *u2_banks = &instance->u2_banks; |
| 583 | void __iomem *com = u2_banks->com; |
| 584 | u32 tmp; |
| 585 | char str[16] = {0}; |
| 586 | |
| 587 | tmp = readl(com + U3P_USBPHYACR1); |
| 588 | tmp >>= PA1_RG_VRT_SEL_OFST; |
| 589 | tmp &= PA1_RG_VRT_SEL_MASK; |
| 590 | |
| 591 | cover_val_to_str(tmp, 3, str); |
| 592 | |
| 593 | dev_info(dev, "%s, vrt_sel=%s\n", __func__, str); |
| 594 | return sprintf(buf, "vrt_sel = %s\n", str); |
| 595 | } |
| 596 | static DEVICE_ATTR_RW(vrt_sel); |
| 597 | |
| 598 | static ssize_t term_sel_store(struct device *dev, |
| 599 | struct device_attribute *attr, |
| 600 | const char *buf, size_t count) |
| 601 | { |
| 602 | struct mtk_phy_instance *instance = phy_get_drvdata(to_phy(dev)); |
| 603 | struct u2phy_banks *u2_banks = &instance->u2_banks; |
| 604 | void __iomem *com = u2_banks->com; |
| 605 | u32 tmp, val; |
| 606 | |
| 607 | if (kstrtouint(buf, 2, &val)) |
| 608 | return -EINVAL; |
| 609 | |
| 610 | tmp = readl(com + U3P_USBPHYACR1); |
| 611 | tmp &= ~PA1_RG_TERM_SEL; |
| 612 | tmp |= PA1_RG_TERM_SEL_VAL(val); |
| 613 | writel(tmp, com + U3P_USBPHYACR1); |
| 614 | |
| 615 | dev_info(dev, "%s, term_sel=%x\n", __func__, val); |
| 616 | return count; |
| 617 | } |
| 618 | |
| 619 | static ssize_t term_sel_show(struct device *dev, |
| 620 | struct device_attribute *attr, char *buf) |
| 621 | { |
| 622 | struct mtk_phy_instance *instance = phy_get_drvdata(to_phy(dev)); |
| 623 | struct u2phy_banks *u2_banks = &instance->u2_banks; |
| 624 | void __iomem *com = u2_banks->com; |
| 625 | u32 tmp; |
| 626 | char str[16] = {0}; |
| 627 | |
| 628 | tmp = readl(com + U3P_USBPHYACR1); |
| 629 | tmp >>= PA1_RG_TERM_SEL_OFST; |
| 630 | tmp &= PA1_RG_TERM_SEL_MASK; |
| 631 | |
| 632 | cover_val_to_str(tmp, 3, str); |
| 633 | |
| 634 | dev_info(dev, "%s, term_sel=%s\n", __func__, str); |
| 635 | return sprintf(buf, "term_sel = %s\n", str); |
| 636 | } |
| 637 | static DEVICE_ATTR_RW(term_sel); |
| 638 | |
| 639 | static ssize_t phy_rev6_store(struct device *dev, |
| 640 | struct device_attribute *attr, |
| 641 | const char *buf, size_t count) |
| 642 | { |
| 643 | struct mtk_phy_instance *instance = phy_get_drvdata(to_phy(dev)); |
| 644 | struct u2phy_banks *u2_banks = &instance->u2_banks; |
| 645 | void __iomem *com = u2_banks->com; |
| 646 | u32 tmp, val; |
| 647 | |
| 648 | if (kstrtouint(buf, 2, &val)) |
| 649 | return -EINVAL; |
| 650 | |
| 651 | tmp = readl(com + U3P_USBPHYACR6); |
| 652 | tmp &= ~PA6_RG_U2_PHY_REV6; |
| 653 | tmp |= PA6_RG_U2_PHY_REV6_VAL(val); |
| 654 | writel(tmp, com + U3P_USBPHYACR6); |
| 655 | |
| 656 | dev_info(dev, "%s, phy_rev6=%x\n", __func__, val); |
| 657 | return count; |
| 658 | } |
| 659 | |
| 660 | static ssize_t phy_rev6_show(struct device *dev, |
| 661 | struct device_attribute *attr, char *buf) |
| 662 | { |
| 663 | struct mtk_phy_instance *instance = phy_get_drvdata(to_phy(dev)); |
| 664 | struct u2phy_banks *u2_banks = &instance->u2_banks; |
| 665 | void __iomem *com = u2_banks->com; |
| 666 | u32 tmp; |
| 667 | char str[16] = {0}; |
| 668 | |
| 669 | tmp = readl(com + U3P_USBPHYACR6); |
| 670 | tmp >>= PA6_RG_U2_PHY_REV6_OFET; |
| 671 | tmp &= PA6_RG_U2_PHY_REV6_MASK; |
| 672 | |
| 673 | cover_val_to_str(tmp, 2, str); |
| 674 | |
| 675 | dev_info(dev, "%s, phy_rev6=%s\n", __func__, str); |
| 676 | return sprintf(buf, "phy_rev6 = %s\n", str); |
| 677 | } |
| 678 | static DEVICE_ATTR_RW(phy_rev6); |
| 679 | |
| 680 | |
| 681 | static ssize_t discth_store(struct device *dev, |
| 682 | struct device_attribute *attr, |
| 683 | const char *buf, size_t count) |
| 684 | { |
| 685 | struct mtk_phy_instance *instance = phy_get_drvdata(to_phy(dev)); |
| 686 | struct u2phy_banks *u2_banks = &instance->u2_banks; |
| 687 | void __iomem *com = u2_banks->com; |
| 688 | u32 tmp, val; |
| 689 | |
| 690 | if (kstrtouint(buf, 2, &val)) |
| 691 | return -EINVAL; |
| 692 | |
| 693 | tmp = readl(com + U3P_USBPHYACR6); |
| 694 | tmp &= ~PA6_RG_U2_DISCTH; |
| 695 | tmp |= PA6_RG_U2_DISCTH_VAL(val); |
| 696 | writel(tmp, com + U3P_USBPHYACR6); |
| 697 | |
| 698 | dev_info(dev, "%s, discth=%x\n", __func__, val); |
| 699 | return count; |
| 700 | } |
| 701 | |
| 702 | static ssize_t discth_show(struct device *dev, |
| 703 | struct device_attribute *attr, char *buf) |
| 704 | { |
| 705 | struct mtk_phy_instance *instance = phy_get_drvdata(to_phy(dev)); |
| 706 | struct u2phy_banks *u2_banks = &instance->u2_banks; |
| 707 | void __iomem *com = u2_banks->com; |
| 708 | u32 tmp; |
| 709 | char str[16] = {0}; |
| 710 | |
| 711 | tmp = readl(com + U3P_USBPHYACR6); |
| 712 | tmp >>= PA6_RG_U2_DISCTH_OFET; |
| 713 | tmp &= PA6_RG_U2_DISCTH_MASK; |
| 714 | |
| 715 | cover_val_to_str(tmp, 4, str); |
| 716 | |
| 717 | dev_info(dev, "%s, discth=%s\n", __func__, str); |
| 718 | return sprintf(buf, "discth = %s\n", str); |
| 719 | } |
| 720 | static DEVICE_ATTR_RW(discth); |
| 721 | |
| 722 | static struct attribute *u2_phy_attrs[] = { |
| 723 | &dev_attr_vrt_sel.attr, |
| 724 | &dev_attr_term_sel.attr, |
| 725 | &dev_attr_phy_rev6.attr, |
| 726 | &dev_attr_discth.attr, |
| 727 | NULL |
| 728 | }; |
| 729 | |
| 730 | static const struct attribute_group u2_phy_group = { |
| 731 | .attrs = u2_phy_attrs, |
| 732 | }; |
| 733 | |
| 734 | static int u2_phy_sysfs_init(struct mtk_tphy *tphy, |
| 735 | struct mtk_phy_instance *instance) |
| 736 | { |
| 737 | struct device *dev = &instance->phy->dev; |
| 738 | int ret; |
| 739 | |
| 740 | ret = sysfs_create_group(&dev->kobj, &u2_phy_group); |
| 741 | if (ret) |
| 742 | dev_err(dev, "failed to creat sysfs attributes\n"); |
| 743 | |
| 744 | ret = sysfs_create_link(&dev->parent->kobj, &dev->kobj, "u2_phy"); |
| 745 | if (ret) |
| 746 | dev_err(dev, "failed to creat link\n"); |
| 747 | |
| 748 | return ret; |
| 749 | } |
| 750 | |
| 751 | static int u2_phy_sysfs_exit(struct mtk_tphy *tphy, |
| 752 | struct mtk_phy_instance *instance) |
| 753 | { |
| 754 | struct device *dev = &instance->phy->dev; |
| 755 | |
| 756 | sysfs_remove_link(&dev->parent->kobj, "u2_phy"); |
| 757 | sysfs_remove_group(&dev->kobj, &u2_phy_group); |
| 758 | return 0; |
| 759 | } |
| 760 | |
| 761 | static void hs_slew_rate_calibrate(struct mtk_tphy *tphy, |
| 762 | struct mtk_phy_instance *instance) |
| 763 | { |
| 764 | struct u2phy_banks *u2_banks = &instance->u2_banks; |
| 765 | void __iomem *fmreg = u2_banks->fmreg; |
| 766 | void __iomem *com = u2_banks->com; |
| 767 | int calibration_val; |
| 768 | int fm_out; |
| 769 | u32 tmp; |
| 770 | |
| 771 | /* use force value */ |
| 772 | if (instance->eye_src) |
| 773 | return; |
| 774 | |
| 775 | /* enable USB ring oscillator */ |
| 776 | tmp = readl(com + U3P_USBPHYACR5); |
| 777 | tmp |= PA5_RG_U2_HSTX_SRCAL_EN; |
| 778 | writel(tmp, com + U3P_USBPHYACR5); |
| 779 | udelay(1); |
| 780 | |
| 781 | /*enable free run clock */ |
| 782 | tmp = readl(fmreg + U3P_U2FREQ_FMMONR1); |
| 783 | tmp |= P2F_RG_FRCK_EN; |
| 784 | writel(tmp, fmreg + U3P_U2FREQ_FMMONR1); |
| 785 | |
| 786 | /* set cycle count as 1024, and select u2 channel */ |
| 787 | tmp = readl(fmreg + U3P_U2FREQ_FMCR0); |
| 788 | tmp &= ~(P2F_RG_CYCLECNT | P2F_RG_MONCLK_SEL); |
| 789 | tmp |= P2F_RG_CYCLECNT_VAL(U3P_FM_DET_CYCLE_CNT); |
| 790 | if (tphy->pdata->version == MTK_PHY_V1) |
| 791 | tmp |= P2F_RG_MONCLK_SEL_VAL(instance->index >> 1); |
| 792 | |
| 793 | writel(tmp, fmreg + U3P_U2FREQ_FMCR0); |
| 794 | |
| 795 | /* enable frequency meter */ |
| 796 | tmp = readl(fmreg + U3P_U2FREQ_FMCR0); |
| 797 | tmp |= P2F_RG_FREQDET_EN; |
| 798 | writel(tmp, fmreg + U3P_U2FREQ_FMCR0); |
| 799 | |
| 800 | /* ignore return value */ |
| 801 | readl_poll_timeout(fmreg + U3P_U2FREQ_FMMONR1, tmp, |
| 802 | (tmp & P2F_USB_FM_VALID), 10, 200); |
| 803 | |
| 804 | fm_out = readl(fmreg + U3P_U2FREQ_VALUE); |
| 805 | |
| 806 | /* disable frequency meter */ |
| 807 | tmp = readl(fmreg + U3P_U2FREQ_FMCR0); |
| 808 | tmp &= ~P2F_RG_FREQDET_EN; |
| 809 | writel(tmp, fmreg + U3P_U2FREQ_FMCR0); |
| 810 | |
| 811 | /*disable free run clock */ |
| 812 | tmp = readl(fmreg + U3P_U2FREQ_FMMONR1); |
| 813 | tmp &= ~P2F_RG_FRCK_EN; |
| 814 | writel(tmp, fmreg + U3P_U2FREQ_FMMONR1); |
| 815 | |
| 816 | if (fm_out) { |
| 817 | /* ( 1024 / FM_OUT ) x reference clock frequency x coef */ |
| 818 | tmp = tphy->src_ref_clk * tphy->src_coef; |
| 819 | tmp = (tmp * U3P_FM_DET_CYCLE_CNT) / fm_out; |
| 820 | calibration_val = DIV_ROUND_CLOSEST(tmp, U3P_SR_COEF_DIVISOR); |
| 821 | } else { |
| 822 | /* if FM detection fail, set default value */ |
| 823 | calibration_val = 4; |
| 824 | } |
| 825 | dev_dbg(tphy->dev, "phy:%d, fm_out:%d, calib:%d (clk:%d, coef:%d)\n", |
| 826 | instance->index, fm_out, calibration_val, |
| 827 | tphy->src_ref_clk, tphy->src_coef); |
| 828 | |
| 829 | /* set HS slew rate */ |
| 830 | tmp = readl(com + U3P_USBPHYACR5); |
| 831 | tmp &= ~PA5_RG_U2_HSTX_SRCTRL; |
| 832 | tmp |= PA5_RG_U2_HSTX_SRCTRL_VAL(calibration_val); |
| 833 | writel(tmp, com + U3P_USBPHYACR5); |
| 834 | |
| 835 | /* disable USB ring oscillator */ |
| 836 | tmp = readl(com + U3P_USBPHYACR5); |
| 837 | tmp &= ~PA5_RG_U2_HSTX_SRCAL_EN; |
| 838 | writel(tmp, com + U3P_USBPHYACR5); |
| 839 | } |
| 840 | |
| 841 | static void u3_phy_instance_init(struct mtk_tphy *tphy, |
| 842 | struct mtk_phy_instance *instance) |
| 843 | { |
| 844 | struct u3phy_banks *u3_banks = &instance->u3_banks; |
| 845 | u32 tmp; |
| 846 | |
| 847 | tmp = readl(u3_banks->phya + U3P_U3_PHYA_REG1); |
| 848 | tmp &= ~RG_SSUSB_VUSB10_ON; |
| 849 | tmp |= ((1 << RG_SSUSB_VUSB10_ON_OFST) & RG_SSUSB_VUSB10_ON); |
| 850 | writel(tmp, u3_banks->phya + U3P_U3_PHYA_REG1); |
| 851 | |
| 852 | /* gating PCIe Analog XTAL clock */ |
| 853 | tmp = readl(u3_banks->spllc + U3P_SPLLC_XTALCTL3); |
| 854 | tmp |= XC3_RG_U3_XTAL_RX_PWD | XC3_RG_U3_FRC_XTAL_RX_PWD; |
| 855 | writel(tmp, u3_banks->spllc + U3P_SPLLC_XTALCTL3); |
| 856 | |
| 857 | /* gating XSQ */ |
| 858 | tmp = readl(u3_banks->phya + U3P_U3_PHYA_DA_REG0); |
| 859 | tmp &= ~P3A_RG_XTAL_EXT_EN_U3; |
| 860 | tmp |= P3A_RG_XTAL_EXT_EN_U3_VAL(2); |
| 861 | writel(tmp, u3_banks->phya + U3P_U3_PHYA_DA_REG0); |
| 862 | |
| 863 | tmp = readl(u3_banks->phya + U3P_U3_PHYA_REG9); |
| 864 | tmp &= ~P3A_RG_RX_DAC_MUX; |
| 865 | tmp |= P3A_RG_RX_DAC_MUX_VAL(4); |
| 866 | writel(tmp, u3_banks->phya + U3P_U3_PHYA_REG9); |
| 867 | |
| 868 | tmp = readl(u3_banks->phya + U3P_U3_PHYA_REG6); |
| 869 | tmp &= ~P3A_RG_TX_EIDLE_CM; |
| 870 | tmp |= P3A_RG_TX_EIDLE_CM_VAL(0xe); |
| 871 | writel(tmp, u3_banks->phya + U3P_U3_PHYA_REG6); |
| 872 | |
| 873 | tmp = readl(u3_banks->phyd + U3P_U3_PHYD_CDR1); |
| 874 | tmp &= ~(P3D_RG_CDR_BIR_LTD0 | P3D_RG_CDR_BIR_LTD1); |
| 875 | tmp |= P3D_RG_CDR_BIR_LTD0_VAL(0xc) | P3D_RG_CDR_BIR_LTD1_VAL(0x3); |
| 876 | writel(tmp, u3_banks->phyd + U3P_U3_PHYD_CDR1); |
| 877 | |
| 878 | tmp = readl(u3_banks->phyd + U3P_U3_PHYD_LFPS1); |
| 879 | tmp &= ~P3D_RG_FWAKE_TH; |
| 880 | tmp |= P3D_RG_FWAKE_TH_VAL(0x34); |
| 881 | writel(tmp, u3_banks->phyd + U3P_U3_PHYD_LFPS1); |
| 882 | |
| 883 | tmp = readl(u3_banks->phyd + U3P_U3_PHYD_RXDET1); |
| 884 | tmp &= ~P3D_RG_RXDET_STB2_SET; |
| 885 | tmp |= P3D_RG_RXDET_STB2_SET_VAL(0x10); |
| 886 | writel(tmp, u3_banks->phyd + U3P_U3_PHYD_RXDET1); |
| 887 | |
| 888 | tmp = readl(u3_banks->phyd + U3P_U3_PHYD_RXDET2); |
| 889 | tmp &= ~P3D_RG_RXDET_STB2_SET_P3; |
| 890 | tmp |= P3D_RG_RXDET_STB2_SET_P3_VAL(0x10); |
| 891 | writel(tmp, u3_banks->phyd + U3P_U3_PHYD_RXDET2); |
| 892 | |
| 893 | tmp = readl(u3_banks->phya + U3P_U3_PHYA_REG11); |
| 894 | tmp &= ~RG_SSUSB_PLL_IBAND_FIX_EN; |
| 895 | writel(tmp, u3_banks->phya + U3P_U3_PHYA_REG11); |
| 896 | |
| 897 | dev_dbg(tphy->dev, "%s(%d)\n", __func__, instance->index); |
| 898 | } |
| 899 | |
| 900 | static void u2_phy_instance_init(struct mtk_tphy *tphy, |
| 901 | struct mtk_phy_instance *instance) |
| 902 | { |
| 903 | struct u2phy_banks *u2_banks = &instance->u2_banks; |
| 904 | void __iomem *com = u2_banks->com; |
| 905 | u32 index = instance->index; |
| 906 | u32 tmp; |
| 907 | |
| 908 | dev_info(tphy->dev, "%s(%d) ++\n", __func__, index); |
| 909 | |
| 910 | udelay(250); |
| 911 | tmp = readl(com + U3P_U2PHYDTM0); |
| 912 | tmp &= ~(P2C_FORCE_UART_EN); |
| 913 | writel(tmp, com + U3P_U2PHYDTM0); |
| 914 | |
| 915 | tmp = readl(com + U3P_U2PHYDTM1); |
| 916 | tmp &= ~P2C_RG_UART_EN; |
| 917 | writel(tmp, com + U3P_U2PHYDTM1); |
| 918 | |
| 919 | if (!index) { |
| 920 | tmp = readl(com + U3P_U2PHYACR4); |
| 921 | tmp &= ~P2C_U2_GPIO_CTR_MSK; |
| 922 | writel(tmp, com + U3P_U2PHYACR4); |
| 923 | } |
| 924 | |
| 925 | tmp = readl(com + U3P_USBPHYACR6); |
| 926 | tmp &= ~PA6_RG_U2_BC11_SW_EN; /* DP/DM BC1.1 path Disable */ |
| 927 | writel(tmp, com + U3P_USBPHYACR6); |
| 928 | |
| 929 | tmp = readl(com + U3P_U2PHYACR4); |
| 930 | tmp |= P2C_RG_USB20_DP_100K_MODE; |
| 931 | writel(tmp, com + U3P_U2PHYACR4); |
| 932 | |
| 933 | tmp = readl(com + U3P_U2PHYACR4); |
| 934 | tmp &= ~(P2C_USB20_DP_100K_EN | P2C_RG_USB20_DM_100K_EN); |
| 935 | writel(tmp, com + U3P_U2PHYACR4); |
| 936 | |
| 937 | /* OTG Enable */ |
| 938 | tmp = readl(com + U3P_USBPHYACR6); |
| 939 | tmp |= PA6_RG_U2_OTG_VBUSCMP_EN; |
| 940 | writel(tmp, com + U3P_USBPHYACR6); |
| 941 | |
| 942 | tmp = readl(com + U3P_U2PHYDTM0); |
| 943 | tmp &= ~P2C_FORCE_SUSPENDM; |
| 944 | writel(tmp, com + U3P_U2PHYDTM0); |
| 945 | |
| 946 | tmp = readl(com + U3P_USBPHYACR6); |
| 947 | tmp &= ~(PA6_RG_U2_PHY_REV6 | PA6_RG_U2_PHY_REV1); |
| 948 | tmp |= PA6_RG_U2_PHY_REV6_VAL(1); |
| 949 | writel(tmp, com + U3P_USBPHYACR6); |
| 950 | |
| 951 | udelay(800); |
| 952 | |
| 953 | dev_info(tphy->dev, "%s(%d) --\n", __func__, index); |
| 954 | } |
| 955 | |
| 956 | static void u2_phy_instance_power_on(struct mtk_tphy *tphy, |
| 957 | struct mtk_phy_instance *instance) |
| 958 | { |
| 959 | struct u2phy_banks *u2_banks = &instance->u2_banks; |
| 960 | void __iomem *com = u2_banks->com; |
| 961 | u32 index = instance->index; |
| 962 | u32 tmp; |
| 963 | |
| 964 | dev_info(tphy->dev, "%s(%d) ++\n", __func__, index); |
| 965 | |
| 966 | tmp = readl(com + U3P_U2PHYDTM0); |
| 967 | tmp &= ~(P2C_FORCE_UART_EN); |
| 968 | writel(tmp, com + U3P_U2PHYDTM0); |
| 969 | |
| 970 | tmp = readl(com + U3P_U2PHYDTM1); |
| 971 | tmp &= ~P2C_RG_UART_EN; |
| 972 | writel(tmp, com + U3P_U2PHYDTM1); |
| 973 | |
| 974 | if (!index) { |
| 975 | tmp = readl(com + U3P_U2PHYACR4); |
| 976 | tmp &= ~P2C_U2_GPIO_CTR_MSK; |
| 977 | writel(tmp, com + U3P_U2PHYACR4); |
| 978 | } |
| 979 | |
| 980 | tmp = readl(com + U3P_U2PHYDTM0); |
| 981 | tmp &= ~P2C_FORCE_SUSPENDM; |
| 982 | writel(tmp, com + U3P_U2PHYDTM0); |
| 983 | |
| 984 | tmp = readl(com + U3P_U2PHYDTM0); |
| 985 | tmp &= ~(P2C_RG_DPPULLDOWN | P2C_RG_DMPULLDOWN | P2C_RG_XCVRSEL | |
| 986 | P2C_RG_TERMSEL | P2C_RG_DATAIN | |
| 987 | P2C_FORCE_DP_PULLDOWN | P2C_FORCE_DM_PULLDOWN | |
| 988 | P2C_FORCE_XCVRSEL | P2C_FORCE_TERMSEL | P2C_FORCE_DATAIN); |
| 989 | writel(tmp, com + U3P_U2PHYDTM0); |
| 990 | |
| 991 | tmp = readl(com + U3P_USBPHYACR6); |
| 992 | tmp &= ~PA6_RG_U2_BC11_SW_EN; /* DP/DM BC1.1 path Disable */ |
| 993 | writel(tmp, com + U3P_USBPHYACR6); |
| 994 | |
| 995 | /* OTG Enable */ |
| 996 | tmp = readl(com + U3P_USBPHYACR6); |
| 997 | tmp |= PA6_RG_U2_OTG_VBUSCMP_EN; |
| 998 | writel(tmp, com + U3P_USBPHYACR6); |
| 999 | |
| 1000 | tmp = readl(com + U3P_USBPHYACR6); |
| 1001 | tmp &= ~(PA6_RG_U2_PHY_REV6 | PA6_RG_U2_PHY_REV1); |
| 1002 | tmp |= PA6_RG_U2_PHY_REV6_VAL(1); |
| 1003 | writel(tmp, com + U3P_USBPHYACR6); |
| 1004 | |
| 1005 | udelay(800); |
| 1006 | |
| 1007 | dev_info(tphy->dev, "%s(%d) --\n", __func__, index); |
| 1008 | } |
| 1009 | |
| 1010 | static void u2_phy_instance_power_off(struct mtk_tphy *tphy, |
| 1011 | struct mtk_phy_instance *instance) |
| 1012 | { |
| 1013 | struct u2phy_banks *u2_banks = &instance->u2_banks; |
| 1014 | void __iomem *com = u2_banks->com; |
| 1015 | u32 index = instance->index; |
| 1016 | u32 tmp; |
| 1017 | |
| 1018 | dev_info(tphy->dev, "%s(%d) ++\n", __func__, index); |
| 1019 | |
| 1020 | tmp = readl(com + U3P_U2PHYDTM0); |
| 1021 | tmp &= ~(P2C_FORCE_UART_EN); |
| 1022 | writel(tmp, com + U3P_U2PHYDTM0); |
| 1023 | |
| 1024 | tmp = readl(com + U3P_U2PHYDTM1); |
| 1025 | tmp &= ~P2C_RG_UART_EN; |
| 1026 | writel(tmp, com + U3P_U2PHYDTM1); |
| 1027 | |
| 1028 | if (!index) { |
| 1029 | tmp = readl(com + U3P_U2PHYACR4); |
| 1030 | tmp &= ~P2C_U2_GPIO_CTR_MSK; |
| 1031 | writel(tmp, com + U3P_U2PHYACR4); |
| 1032 | } |
| 1033 | |
| 1034 | tmp = readl(com + U3P_USBPHYACR6); |
| 1035 | tmp &= ~PA6_RG_U2_BC11_SW_EN; /* DP/DM BC1.1 path Disable */ |
| 1036 | writel(tmp, com + U3P_USBPHYACR6); |
| 1037 | |
| 1038 | /* OTG Disable */ |
| 1039 | tmp = readl(com + U3P_USBPHYACR6); |
| 1040 | tmp &= ~PA6_RG_U2_OTG_VBUSCMP_EN; |
| 1041 | writel(tmp, com + U3P_USBPHYACR6); |
| 1042 | |
| 1043 | tmp = readl(com + U3P_U2PHYDTM0); |
| 1044 | tmp |= P2C_RG_SUSPENDM | P2C_FORCE_SUSPENDM; |
| 1045 | writel(tmp, com + U3P_U2PHYDTM0); |
| 1046 | |
| 1047 | mdelay(2); |
| 1048 | |
| 1049 | tmp = readl(com + U3P_U2PHYDTM0); |
| 1050 | tmp &= ~P2C_RG_DATAIN; |
| 1051 | tmp |= (P2C_RG_DPPULLDOWN | P2C_RG_DMPULLDOWN | |
| 1052 | P2C_RG_XCVRSEL_VAL(1) | P2C_RG_TERMSEL | |
| 1053 | P2C_FORCE_DP_PULLDOWN | P2C_FORCE_DM_PULLDOWN | |
| 1054 | P2C_FORCE_XCVRSEL | P2C_FORCE_TERMSEL | P2C_FORCE_DATAIN); |
| 1055 | writel(tmp, com + U3P_U2PHYDTM0); |
| 1056 | |
| 1057 | tmp = readl(com + U3P_USBPHYACR6); |
| 1058 | tmp |= (PA6_RG_U2_PHY_REV6_VAL(1) | PA6_RG_U2_PHY_REV1); |
| 1059 | writel(tmp, com + U3P_USBPHYACR6); |
| 1060 | |
| 1061 | udelay(800); |
| 1062 | tmp = readl(com + U3P_U2PHYDTM0); |
| 1063 | tmp &= ~P2C_RG_SUSPENDM; |
| 1064 | writel(tmp, com + U3P_U2PHYDTM0); |
| 1065 | |
| 1066 | udelay(1); |
| 1067 | |
| 1068 | dev_info(tphy->dev, "%s(%d) --\n", __func__, index); |
| 1069 | } |
| 1070 | |
| 1071 | static void u2_phy_instance_exit(struct mtk_tphy *tphy, |
| 1072 | struct mtk_phy_instance *instance) |
| 1073 | { |
| 1074 | struct u2phy_banks *u2_banks = &instance->u2_banks; |
| 1075 | void __iomem *com = u2_banks->com; |
| 1076 | u32 index = instance->index; |
| 1077 | u32 tmp; |
| 1078 | |
| 1079 | if (tphy->pdata->avoid_rx_sen_degradation && index) { |
| 1080 | tmp = readl(com + U3D_U2PHYDCR0); |
| 1081 | tmp &= ~P2C_RG_SIF_U2PLL_FORCE_ON; |
| 1082 | writel(tmp, com + U3D_U2PHYDCR0); |
| 1083 | |
| 1084 | tmp = readl(com + U3P_U2PHYDTM0); |
| 1085 | tmp &= ~P2C_FORCE_SUSPENDM; |
| 1086 | writel(tmp, com + U3P_U2PHYDTM0); |
| 1087 | } |
| 1088 | } |
| 1089 | |
| 1090 | static void u2_phy_instance_set_mode(struct mtk_tphy *tphy, |
| 1091 | struct mtk_phy_instance *instance, |
| 1092 | enum phy_mode mode) |
| 1093 | { |
| 1094 | struct u2phy_banks *u2_banks = &instance->u2_banks; |
| 1095 | u32 tmp; |
| 1096 | |
| 1097 | tmp = readl(u2_banks->com + U3P_U2PHYDTM1); |
| 1098 | switch (mode) { |
| 1099 | case PHY_MODE_USB_DEVICE: |
| 1100 | tmp |= P2C_FORCE_IDDIG | P2C_RG_IDDIG; |
| 1101 | break; |
| 1102 | case PHY_MODE_USB_HOST: |
| 1103 | tmp |= P2C_FORCE_IDDIG; |
| 1104 | tmp &= ~P2C_RG_IDDIG; |
| 1105 | break; |
| 1106 | case PHY_MODE_USB_OTG: |
| 1107 | tmp &= ~(P2C_FORCE_IDDIG | P2C_RG_IDDIG); |
| 1108 | break; |
| 1109 | default: |
| 1110 | return; |
| 1111 | } |
| 1112 | writel(tmp, u2_banks->com + U3P_U2PHYDTM1); |
| 1113 | } |
| 1114 | |
| 1115 | static void pcie_phy_instance_init(struct mtk_tphy *tphy, |
| 1116 | struct mtk_phy_instance *instance) |
| 1117 | { |
| 1118 | struct u3phy_banks *u3_banks = &instance->u3_banks; |
| 1119 | u32 tmp; |
| 1120 | |
| 1121 | if (tphy->pdata->version != MTK_PHY_V1) |
| 1122 | return; |
| 1123 | |
| 1124 | tmp = readl(u3_banks->phya + U3P_U3_PHYA_DA_REG0); |
| 1125 | tmp &= ~(P3A_RG_XTAL_EXT_PE1H | P3A_RG_XTAL_EXT_PE2H); |
| 1126 | tmp |= P3A_RG_XTAL_EXT_PE1H_VAL(0x2) | P3A_RG_XTAL_EXT_PE2H_VAL(0x2); |
| 1127 | writel(tmp, u3_banks->phya + U3P_U3_PHYA_DA_REG0); |
| 1128 | |
| 1129 | /* ref clk drive */ |
| 1130 | tmp = readl(u3_banks->phya + U3P_U3_PHYA_REG1); |
| 1131 | tmp &= ~P3A_RG_CLKDRV_AMP; |
| 1132 | tmp |= P3A_RG_CLKDRV_AMP_VAL(0x4); |
| 1133 | writel(tmp, u3_banks->phya + U3P_U3_PHYA_REG1); |
| 1134 | |
| 1135 | tmp = readl(u3_banks->phya + U3P_U3_PHYA_REG0); |
| 1136 | tmp &= ~P3A_RG_CLKDRV_OFF; |
| 1137 | tmp |= P3A_RG_CLKDRV_OFF_VAL(0x1); |
| 1138 | writel(tmp, u3_banks->phya + U3P_U3_PHYA_REG0); |
| 1139 | |
| 1140 | /* SSC delta -5000ppm */ |
| 1141 | tmp = readl(u3_banks->phya + U3P_U3_PHYA_DA_REG20); |
| 1142 | tmp &= ~P3A_RG_PLL_DELTA1_PE2H; |
| 1143 | tmp |= P3A_RG_PLL_DELTA1_PE2H_VAL(0x3c); |
| 1144 | writel(tmp, u3_banks->phya + U3P_U3_PHYA_DA_REG20); |
| 1145 | |
| 1146 | tmp = readl(u3_banks->phya + U3P_U3_PHYA_DA_REG25); |
| 1147 | tmp &= ~P3A_RG_PLL_DELTA_PE2H; |
| 1148 | tmp |= P3A_RG_PLL_DELTA_PE2H_VAL(0x36); |
| 1149 | writel(tmp, u3_banks->phya + U3P_U3_PHYA_DA_REG25); |
| 1150 | |
| 1151 | /* change pll BW 0.6M */ |
| 1152 | tmp = readl(u3_banks->phya + U3P_U3_PHYA_DA_REG5); |
| 1153 | tmp &= ~(P3A_RG_PLL_BR_PE2H | P3A_RG_PLL_IC_PE2H); |
| 1154 | tmp |= P3A_RG_PLL_BR_PE2H_VAL(0x1) | P3A_RG_PLL_IC_PE2H_VAL(0x1); |
| 1155 | writel(tmp, u3_banks->phya + U3P_U3_PHYA_DA_REG5); |
| 1156 | |
| 1157 | tmp = readl(u3_banks->phya + U3P_U3_PHYA_DA_REG4); |
| 1158 | tmp &= ~(P3A_RG_PLL_DIVEN_PE2H | P3A_RG_PLL_BC_PE2H); |
| 1159 | tmp |= P3A_RG_PLL_BC_PE2H_VAL(0x3); |
| 1160 | writel(tmp, u3_banks->phya + U3P_U3_PHYA_DA_REG4); |
| 1161 | |
| 1162 | tmp = readl(u3_banks->phya + U3P_U3_PHYA_DA_REG6); |
| 1163 | tmp &= ~P3A_RG_PLL_IR_PE2H; |
| 1164 | tmp |= P3A_RG_PLL_IR_PE2H_VAL(0x2); |
| 1165 | writel(tmp, u3_banks->phya + U3P_U3_PHYA_DA_REG6); |
| 1166 | |
| 1167 | tmp = readl(u3_banks->phya + U3P_U3_PHYA_DA_REG7); |
| 1168 | tmp &= ~P3A_RG_PLL_BP_PE2H; |
| 1169 | tmp |= P3A_RG_PLL_BP_PE2H_VAL(0xa); |
| 1170 | writel(tmp, u3_banks->phya + U3P_U3_PHYA_DA_REG7); |
| 1171 | |
| 1172 | /* Tx Detect Rx Timing: 10us -> 5us */ |
| 1173 | tmp = readl(u3_banks->phyd + U3P_U3_PHYD_RXDET1); |
| 1174 | tmp &= ~P3D_RG_RXDET_STB2_SET; |
| 1175 | tmp |= P3D_RG_RXDET_STB2_SET_VAL(0x10); |
| 1176 | writel(tmp, u3_banks->phyd + U3P_U3_PHYD_RXDET1); |
| 1177 | |
| 1178 | tmp = readl(u3_banks->phyd + U3P_U3_PHYD_RXDET2); |
| 1179 | tmp &= ~P3D_RG_RXDET_STB2_SET_P3; |
| 1180 | tmp |= P3D_RG_RXDET_STB2_SET_P3_VAL(0x10); |
| 1181 | writel(tmp, u3_banks->phyd + U3P_U3_PHYD_RXDET2); |
| 1182 | |
| 1183 | /* wait for PCIe subsys register to active */ |
| 1184 | usleep_range(2500, 3000); |
| 1185 | dev_dbg(tphy->dev, "%s(%d)\n", __func__, instance->index); |
| 1186 | } |
| 1187 | |
| 1188 | static void pcie_phy_instance_power_on(struct mtk_tphy *tphy, |
| 1189 | struct mtk_phy_instance *instance) |
| 1190 | { |
| 1191 | struct u3phy_banks *bank = &instance->u3_banks; |
| 1192 | u32 tmp; |
| 1193 | |
| 1194 | tmp = readl(bank->chip + U3P_U3_CHIP_GPIO_CTLD); |
| 1195 | tmp &= ~(P3C_FORCE_IP_SW_RST | P3C_REG_IP_SW_RST); |
| 1196 | writel(tmp, bank->chip + U3P_U3_CHIP_GPIO_CTLD); |
| 1197 | |
| 1198 | tmp = readl(bank->chip + U3P_U3_CHIP_GPIO_CTLE); |
| 1199 | tmp &= ~(P3C_RG_SWRST_U3_PHYD_FORCE_EN | P3C_RG_SWRST_U3_PHYD); |
| 1200 | writel(tmp, bank->chip + U3P_U3_CHIP_GPIO_CTLE); |
| 1201 | } |
| 1202 | |
| 1203 | static void pcie_phy_instance_power_off(struct mtk_tphy *tphy, |
| 1204 | struct mtk_phy_instance *instance) |
| 1205 | |
| 1206 | { |
| 1207 | struct u3phy_banks *bank = &instance->u3_banks; |
| 1208 | u32 tmp; |
| 1209 | |
| 1210 | tmp = readl(bank->chip + U3P_U3_CHIP_GPIO_CTLD); |
| 1211 | tmp |= P3C_FORCE_IP_SW_RST | P3C_REG_IP_SW_RST; |
| 1212 | writel(tmp, bank->chip + U3P_U3_CHIP_GPIO_CTLD); |
| 1213 | |
| 1214 | tmp = readl(bank->chip + U3P_U3_CHIP_GPIO_CTLE); |
| 1215 | tmp |= P3C_RG_SWRST_U3_PHYD_FORCE_EN | P3C_RG_SWRST_U3_PHYD; |
| 1216 | writel(tmp, bank->chip + U3P_U3_CHIP_GPIO_CTLE); |
| 1217 | } |
| 1218 | |
| 1219 | static void sata_phy_instance_init(struct mtk_tphy *tphy, |
| 1220 | struct mtk_phy_instance *instance) |
| 1221 | { |
| 1222 | struct u3phy_banks *u3_banks = &instance->u3_banks; |
| 1223 | void __iomem *phyd = u3_banks->phyd; |
| 1224 | u32 tmp; |
| 1225 | |
| 1226 | /* charge current adjustment */ |
| 1227 | tmp = readl(phyd + ANA_RG_CTRL_SIGNAL6); |
| 1228 | tmp &= ~(RG_CDR_BIRLTR_GEN1_MSK | RG_CDR_BC_GEN1_MSK); |
| 1229 | tmp |= RG_CDR_BIRLTR_GEN1_VAL(0x6) | RG_CDR_BC_GEN1_VAL(0x1a); |
| 1230 | writel(tmp, phyd + ANA_RG_CTRL_SIGNAL6); |
| 1231 | |
| 1232 | tmp = readl(phyd + ANA_EQ_EYE_CTRL_SIGNAL4); |
| 1233 | tmp &= ~RG_CDR_BIRLTD0_GEN1_MSK; |
| 1234 | tmp |= RG_CDR_BIRLTD0_GEN1_VAL(0x18); |
| 1235 | writel(tmp, phyd + ANA_EQ_EYE_CTRL_SIGNAL4); |
| 1236 | |
| 1237 | tmp = readl(phyd + ANA_EQ_EYE_CTRL_SIGNAL5); |
| 1238 | tmp &= ~RG_CDR_BIRLTD0_GEN3_MSK; |
| 1239 | tmp |= RG_CDR_BIRLTD0_GEN3_VAL(0x06); |
| 1240 | writel(tmp, phyd + ANA_EQ_EYE_CTRL_SIGNAL5); |
| 1241 | |
| 1242 | tmp = readl(phyd + ANA_RG_CTRL_SIGNAL4); |
| 1243 | tmp &= ~(RG_CDR_BICLTR_GEN1_MSK | RG_CDR_BR_GEN2_MSK); |
| 1244 | tmp |= RG_CDR_BICLTR_GEN1_VAL(0x0c) | RG_CDR_BR_GEN2_VAL(0x07); |
| 1245 | writel(tmp, phyd + ANA_RG_CTRL_SIGNAL4); |
| 1246 | |
| 1247 | tmp = readl(phyd + PHYD_CTRL_SIGNAL_MODE4); |
| 1248 | tmp &= ~(RG_CDR_BICLTD0_GEN1_MSK | RG_CDR_BICLTD1_GEN1_MSK); |
| 1249 | tmp |= RG_CDR_BICLTD0_GEN1_VAL(0x08) | RG_CDR_BICLTD1_GEN1_VAL(0x02); |
| 1250 | writel(tmp, phyd + PHYD_CTRL_SIGNAL_MODE4); |
| 1251 | |
| 1252 | tmp = readl(phyd + PHYD_DESIGN_OPTION2); |
| 1253 | tmp &= ~RG_LOCK_CNT_SEL_MSK; |
| 1254 | tmp |= RG_LOCK_CNT_SEL_VAL(0x02); |
| 1255 | writel(tmp, phyd + PHYD_DESIGN_OPTION2); |
| 1256 | |
| 1257 | tmp = readl(phyd + PHYD_DESIGN_OPTION9); |
| 1258 | tmp &= ~(RG_T2_MIN_MSK | RG_TG_MIN_MSK | |
| 1259 | RG_T2_MAX_MSK | RG_TG_MAX_MSK); |
| 1260 | tmp |= RG_T2_MIN_VAL(0x12) | RG_TG_MIN_VAL(0x04) | |
| 1261 | RG_T2_MAX_VAL(0x31) | RG_TG_MAX_VAL(0x0e); |
| 1262 | writel(tmp, phyd + PHYD_DESIGN_OPTION9); |
| 1263 | |
| 1264 | tmp = readl(phyd + ANA_RG_CTRL_SIGNAL1); |
| 1265 | tmp &= ~RG_IDRV_0DB_GEN1_MSK; |
| 1266 | tmp |= RG_IDRV_0DB_GEN1_VAL(0x20); |
| 1267 | writel(tmp, phyd + ANA_RG_CTRL_SIGNAL1); |
| 1268 | |
| 1269 | tmp = readl(phyd + ANA_EQ_EYE_CTRL_SIGNAL1); |
| 1270 | tmp &= ~RG_EQ_DLEQ_LFI_GEN1_MSK; |
| 1271 | tmp |= RG_EQ_DLEQ_LFI_GEN1_VAL(0x03); |
| 1272 | writel(tmp, phyd + ANA_EQ_EYE_CTRL_SIGNAL1); |
| 1273 | |
| 1274 | dev_dbg(tphy->dev, "%s(%d)\n", __func__, instance->index); |
| 1275 | } |
| 1276 | |
| 1277 | static void phy_v1_banks_init(struct mtk_tphy *tphy, |
| 1278 | struct mtk_phy_instance *instance) |
| 1279 | { |
| 1280 | struct u2phy_banks *u2_banks = &instance->u2_banks; |
| 1281 | struct u3phy_banks *u3_banks = &instance->u3_banks; |
| 1282 | |
| 1283 | switch (instance->type) { |
| 1284 | case PHY_TYPE_USB2: |
| 1285 | u2_banks->misc = NULL; |
| 1286 | u2_banks->fmreg = tphy->sif_base + SSUSB_SIFSLV_V1_U2FREQ; |
| 1287 | u2_banks->com = instance->port_base + SSUSB_SIFSLV_V1_U2PHY_COM; |
| 1288 | break; |
| 1289 | case PHY_TYPE_USB3: |
| 1290 | case PHY_TYPE_PCIE: |
| 1291 | u3_banks->spllc = tphy->sif_base + SSUSB_SIFSLV_V1_SPLLC; |
| 1292 | u3_banks->chip = tphy->sif_base + SSUSB_SIFSLV_V1_CHIP; |
| 1293 | u3_banks->phyd = instance->port_base + SSUSB_SIFSLV_V1_U3PHYD; |
| 1294 | u3_banks->phya = instance->port_base + SSUSB_SIFSLV_V1_U3PHYA; |
| 1295 | break; |
| 1296 | case PHY_TYPE_SATA: |
| 1297 | u3_banks->phyd = instance->port_base + SSUSB_SIFSLV_V1_U3PHYD; |
| 1298 | break; |
| 1299 | default: |
| 1300 | dev_err(tphy->dev, "incompatible PHY type\n"); |
| 1301 | return; |
| 1302 | } |
| 1303 | } |
| 1304 | |
| 1305 | static void phy_v2_banks_init(struct mtk_tphy *tphy, |
| 1306 | struct mtk_phy_instance *instance) |
| 1307 | { |
| 1308 | struct u2phy_banks *u2_banks = &instance->u2_banks; |
| 1309 | struct u3phy_banks *u3_banks = &instance->u3_banks; |
| 1310 | |
| 1311 | switch (instance->type) { |
| 1312 | case PHY_TYPE_USB2: |
| 1313 | u2_banks->misc = instance->port_base + SSUSB_SIFSLV_V2_MISC; |
| 1314 | u2_banks->fmreg = instance->port_base + SSUSB_SIFSLV_V2_U2FREQ; |
| 1315 | u2_banks->com = instance->port_base + SSUSB_SIFSLV_V2_U2PHY_COM; |
| 1316 | break; |
| 1317 | case PHY_TYPE_USB3: |
| 1318 | case PHY_TYPE_PCIE: |
| 1319 | u3_banks->spllc = instance->port_base + SSUSB_SIFSLV_V2_SPLLC; |
| 1320 | u3_banks->chip = instance->port_base + SSUSB_SIFSLV_V2_CHIP; |
| 1321 | u3_banks->phyd = instance->port_base + SSUSB_SIFSLV_V2_U3PHYD; |
| 1322 | u3_banks->phya = instance->port_base + SSUSB_SIFSLV_V2_U3PHYA; |
| 1323 | break; |
| 1324 | default: |
| 1325 | dev_err(tphy->dev, "incompatible PHY type\n"); |
| 1326 | return; |
| 1327 | } |
| 1328 | } |
| 1329 | |
| 1330 | static void phy_parse_property(struct mtk_tphy *tphy, |
| 1331 | struct mtk_phy_instance *instance) |
| 1332 | { |
| 1333 | struct device *dev = &instance->phy->dev; |
| 1334 | |
| 1335 | if (instance->type != PHY_TYPE_USB2) |
| 1336 | return; |
| 1337 | |
| 1338 | instance->bc12_en = device_property_read_bool(dev, "mediatek,bc12"); |
| 1339 | device_property_read_u32(dev, "mediatek,eye-src", |
| 1340 | &instance->eye_src); |
| 1341 | device_property_read_u32(dev, "mediatek,eye-vrt", |
| 1342 | &instance->eye_vrt); |
| 1343 | device_property_read_u32(dev, "mediatek,eye-term", |
| 1344 | &instance->eye_term); |
| 1345 | device_property_read_u32(dev, "mediatek,eye-rev6", |
| 1346 | &instance->eye_rev6); |
| 1347 | device_property_read_u32(dev, "mediatek,eye-disc", |
| 1348 | &instance->eye_disc); |
| 1349 | device_property_read_u32(dev, "mediatek,rx-sqth", |
| 1350 | &instance->rx_sqth); |
| 1351 | dev_info(dev, "bc12:%d src:%d vrt:%d term:%d rev6:%d disc:%d sqth:%d\n", |
| 1352 | instance->bc12_en, instance->eye_src, |
| 1353 | instance->eye_vrt, instance->eye_term, |
| 1354 | instance->eye_rev6, instance->eye_disc, instance->rx_sqth); |
| 1355 | } |
| 1356 | |
| 1357 | static void u2_phy_props_set(struct mtk_tphy *tphy, |
| 1358 | struct mtk_phy_instance *instance) |
| 1359 | { |
| 1360 | struct u2phy_banks *u2_banks = &instance->u2_banks; |
| 1361 | void __iomem *com = u2_banks->com; |
| 1362 | u32 tmp; |
| 1363 | |
| 1364 | if (instance->bc12_en) { |
| 1365 | tmp = readl(com + U3P_U2PHYBC12C); |
| 1366 | tmp |= P2C_RG_CHGDT_EN; /* BC1.2 path Enable */ |
| 1367 | writel(tmp, com + U3P_U2PHYBC12C); |
| 1368 | } |
| 1369 | |
| 1370 | if (instance->eye_src) { |
| 1371 | tmp = readl(com + U3P_USBPHYACR5); |
| 1372 | tmp &= ~PA5_RG_U2_HSTX_SRCTRL; |
| 1373 | tmp |= PA5_RG_U2_HSTX_SRCTRL_VAL(instance->eye_src); |
| 1374 | writel(tmp, com + U3P_USBPHYACR5); |
| 1375 | } |
| 1376 | |
| 1377 | if (instance->eye_vrt) { |
| 1378 | tmp = readl(com + U3P_USBPHYACR1); |
| 1379 | tmp &= ~PA1_RG_VRT_SEL; |
| 1380 | tmp |= PA1_RG_VRT_SEL_VAL(instance->eye_vrt); |
| 1381 | writel(tmp, com + U3P_USBPHYACR1); |
| 1382 | } |
| 1383 | |
| 1384 | if (instance->eye_term) { |
| 1385 | tmp = readl(com + U3P_USBPHYACR1); |
| 1386 | tmp &= ~PA1_RG_TERM_SEL; |
| 1387 | tmp |= PA1_RG_TERM_SEL_VAL(instance->eye_term); |
| 1388 | writel(tmp, com + U3P_USBPHYACR1); |
| 1389 | } |
| 1390 | |
| 1391 | if (instance->eye_rev6) { |
| 1392 | tmp = readl(com + U3P_USBPHYACR6); |
| 1393 | tmp &= ~PA6_RG_U2_PHY_REV6; |
| 1394 | tmp |= PA6_RG_U2_PHY_REV6_VAL(instance->eye_rev6); |
| 1395 | writel(tmp, com + U3P_USBPHYACR6); |
| 1396 | } |
| 1397 | |
| 1398 | if (instance->eye_disc) { |
| 1399 | tmp = readl(com + U3P_USBPHYACR6); |
| 1400 | tmp &= ~PA6_RG_U2_DISCTH; |
| 1401 | tmp |= PA6_RG_U2_DISCTH_VAL(instance->eye_disc); |
| 1402 | writel(tmp, com + U3P_USBPHYACR6); |
| 1403 | } |
| 1404 | |
| 1405 | if (instance->rx_sqth) { |
| 1406 | tmp = readl(com + U3P_USBPHYACR6); |
| 1407 | tmp &= ~PA6_RG_U2_SQTH; |
| 1408 | tmp |= PA6_RG_U2_SQTH_VAL(instance->rx_sqth); |
| 1409 | tmp |= PA6_RG_U2_PHY_REV4; |
| 1410 | writel(tmp, com + U3P_USBPHYACR6); |
| 1411 | } |
| 1412 | |
| 1413 | tmp = readl(com + U3P_USBPHYACR2); |
| 1414 | tmp &= ~PA2_RG_CLKREF_REV_PLL_POSDIV; |
| 1415 | tmp |= PA2_RG_CLKREF_REV_PLL_POSDIV_VAL(1); |
| 1416 | tmp |= PA2_RG_CLKREF_REV_PLL_FBKSEL; |
| 1417 | writel(tmp, com + U3P_USBPHYACR2); |
| 1418 | } |
| 1419 | |
| 1420 | static int mtk_phy_init(struct phy *phy) |
| 1421 | { |
| 1422 | struct mtk_phy_instance *instance = phy_get_drvdata(phy); |
| 1423 | struct mtk_tphy *tphy = dev_get_drvdata(phy->dev.parent); |
| 1424 | int ret; |
| 1425 | |
| 1426 | ret = clk_prepare_enable(tphy->u3phya_ref); |
| 1427 | if (ret) { |
| 1428 | dev_err(tphy->dev, "failed to enable u3phya_ref\n"); |
| 1429 | return ret; |
| 1430 | } |
| 1431 | |
| 1432 | ret = clk_prepare_enable(instance->ref_clk); |
| 1433 | if (ret) { |
| 1434 | dev_err(tphy->dev, "failed to enable ref_clk\n"); |
| 1435 | return ret; |
| 1436 | } |
| 1437 | |
| 1438 | switch (instance->type) { |
| 1439 | case PHY_TYPE_USB2: |
| 1440 | u2_phy_instance_init(tphy, instance); |
| 1441 | u2_phy_props_set(tphy, instance); |
| 1442 | u2_phy_sysfs_init(tphy, instance); |
| 1443 | break; |
| 1444 | case PHY_TYPE_USB3: |
| 1445 | u3_phy_instance_init(tphy, instance); |
| 1446 | u3_phy_sysfs_init(tphy, instance); |
| 1447 | break; |
| 1448 | case PHY_TYPE_PCIE: |
| 1449 | pcie_phy_instance_init(tphy, instance); |
| 1450 | break; |
| 1451 | case PHY_TYPE_SATA: |
| 1452 | sata_phy_instance_init(tphy, instance); |
| 1453 | break; |
| 1454 | default: |
| 1455 | dev_err(tphy->dev, "incompatible PHY type\n"); |
| 1456 | return -EINVAL; |
| 1457 | } |
| 1458 | |
| 1459 | return 0; |
| 1460 | } |
| 1461 | |
| 1462 | static int mtk_phy_power_on(struct phy *phy) |
| 1463 | { |
| 1464 | struct mtk_phy_instance *instance = phy_get_drvdata(phy); |
| 1465 | struct mtk_tphy *tphy = dev_get_drvdata(phy->dev.parent); |
| 1466 | |
| 1467 | if (instance->type == PHY_TYPE_USB2) { |
| 1468 | u2_phy_instance_power_on(tphy, instance); |
| 1469 | hs_slew_rate_calibrate(tphy, instance); |
| 1470 | } else if (instance->type == PHY_TYPE_PCIE) { |
| 1471 | pcie_phy_instance_power_on(tphy, instance); |
| 1472 | } |
| 1473 | |
| 1474 | return 0; |
| 1475 | } |
| 1476 | |
| 1477 | static int mtk_phy_power_off(struct phy *phy) |
| 1478 | { |
| 1479 | struct mtk_phy_instance *instance = phy_get_drvdata(phy); |
| 1480 | struct mtk_tphy *tphy = dev_get_drvdata(phy->dev.parent); |
| 1481 | |
| 1482 | if (instance->type == PHY_TYPE_USB2) |
| 1483 | u2_phy_instance_power_off(tphy, instance); |
| 1484 | else if (instance->type == PHY_TYPE_PCIE) |
| 1485 | pcie_phy_instance_power_off(tphy, instance); |
| 1486 | |
| 1487 | return 0; |
| 1488 | } |
| 1489 | |
| 1490 | static int mtk_phy_exit(struct phy *phy) |
| 1491 | { |
| 1492 | struct mtk_phy_instance *instance = phy_get_drvdata(phy); |
| 1493 | struct mtk_tphy *tphy = dev_get_drvdata(phy->dev.parent); |
| 1494 | |
| 1495 | if (instance->type == PHY_TYPE_USB2) { |
| 1496 | u2_phy_instance_exit(tphy, instance); |
| 1497 | u2_phy_sysfs_exit(tphy, instance); |
| 1498 | } |
| 1499 | |
| 1500 | if (instance->type == PHY_TYPE_USB3) |
| 1501 | u3_phy_sysfs_exit(tphy, instance); |
| 1502 | |
| 1503 | clk_disable_unprepare(instance->ref_clk); |
| 1504 | clk_disable_unprepare(tphy->u3phya_ref); |
| 1505 | return 0; |
| 1506 | } |
| 1507 | |
| 1508 | static int mtk_phy_set_mode(struct phy *phy, enum phy_mode mode) |
| 1509 | { |
| 1510 | struct mtk_phy_instance *instance = phy_get_drvdata(phy); |
| 1511 | struct mtk_tphy *tphy = dev_get_drvdata(phy->dev.parent); |
| 1512 | |
| 1513 | if (instance->type == PHY_TYPE_USB2) |
| 1514 | u2_phy_instance_set_mode(tphy, instance, mode); |
| 1515 | |
| 1516 | return 0; |
| 1517 | } |
| 1518 | |
| 1519 | static struct phy *mtk_phy_xlate(struct device *dev, |
| 1520 | struct of_phandle_args *args) |
| 1521 | { |
| 1522 | struct mtk_tphy *tphy = dev_get_drvdata(dev); |
| 1523 | struct mtk_phy_instance *instance = NULL; |
| 1524 | struct device_node *phy_np = args->np; |
| 1525 | int index; |
| 1526 | |
| 1527 | if (args->args_count != 1) { |
| 1528 | dev_err(dev, "invalid number of cells in 'phy' property\n"); |
| 1529 | return ERR_PTR(-EINVAL); |
| 1530 | } |
| 1531 | |
| 1532 | for (index = 0; index < tphy->nphys; index++) |
| 1533 | if (phy_np == tphy->phys[index]->phy->dev.of_node) { |
| 1534 | instance = tphy->phys[index]; |
| 1535 | break; |
| 1536 | } |
| 1537 | |
| 1538 | if (!instance) { |
| 1539 | dev_err(dev, "failed to find appropriate phy\n"); |
| 1540 | return ERR_PTR(-EINVAL); |
| 1541 | } |
| 1542 | |
| 1543 | instance->type = args->args[0]; |
| 1544 | if (!(instance->type == PHY_TYPE_USB2 || |
| 1545 | instance->type == PHY_TYPE_USB3 || |
| 1546 | instance->type == PHY_TYPE_PCIE || |
| 1547 | instance->type == PHY_TYPE_SATA)) { |
| 1548 | dev_err(dev, "unsupported device type: %d\n", instance->type); |
| 1549 | return ERR_PTR(-EINVAL); |
| 1550 | } |
| 1551 | |
| 1552 | if (tphy->pdata->version == MTK_PHY_V1) { |
| 1553 | phy_v1_banks_init(tphy, instance); |
| 1554 | } else if (tphy->pdata->version == MTK_PHY_V2) { |
| 1555 | phy_v2_banks_init(tphy, instance); |
| 1556 | } else { |
| 1557 | dev_err(dev, "phy version is not supported\n"); |
| 1558 | return ERR_PTR(-EINVAL); |
| 1559 | } |
| 1560 | |
| 1561 | phy_parse_property(tphy, instance); |
| 1562 | |
| 1563 | return instance->phy; |
| 1564 | } |
| 1565 | |
| 1566 | static const struct phy_ops mtk_tphy_ops = { |
| 1567 | .init = mtk_phy_init, |
| 1568 | .exit = mtk_phy_exit, |
| 1569 | .power_on = mtk_phy_power_on, |
| 1570 | .power_off = mtk_phy_power_off, |
| 1571 | .set_mode = mtk_phy_set_mode, |
| 1572 | .owner = THIS_MODULE, |
| 1573 | }; |
| 1574 | |
| 1575 | static const struct mtk_phy_pdata tphy_v1_pdata = { |
| 1576 | .avoid_rx_sen_degradation = false, |
| 1577 | .version = MTK_PHY_V1, |
| 1578 | }; |
| 1579 | |
| 1580 | static const struct mtk_phy_pdata tphy_v2_pdata = { |
| 1581 | .avoid_rx_sen_degradation = false, |
| 1582 | .version = MTK_PHY_V2, |
| 1583 | }; |
| 1584 | |
| 1585 | static const struct mtk_phy_pdata mt8173_pdata = { |
| 1586 | .avoid_rx_sen_degradation = true, |
| 1587 | .version = MTK_PHY_V1, |
| 1588 | }; |
| 1589 | |
| 1590 | static const struct of_device_id mtk_tphy_id_table[] = { |
| 1591 | { .compatible = "mediatek,mt2701-u3phy", .data = &tphy_v1_pdata }, |
| 1592 | { .compatible = "mediatek,mt2712-u3phy", .data = &tphy_v2_pdata }, |
| 1593 | { .compatible = "mediatek,mt8173-u3phy", .data = &mt8173_pdata }, |
| 1594 | { .compatible = "mediatek,generic-tphy-v1", .data = &tphy_v1_pdata }, |
| 1595 | { .compatible = "mediatek,generic-tphy-v2", .data = &tphy_v2_pdata }, |
| 1596 | { }, |
| 1597 | }; |
| 1598 | MODULE_DEVICE_TABLE(of, mtk_tphy_id_table); |
| 1599 | |
| 1600 | static int mtk_tphy_probe(struct platform_device *pdev) |
| 1601 | { |
| 1602 | struct device *dev = &pdev->dev; |
| 1603 | struct device_node *np = dev->of_node; |
| 1604 | struct device_node *child_np; |
| 1605 | struct phy_provider *provider; |
| 1606 | struct resource *sif_res; |
| 1607 | struct mtk_tphy *tphy; |
| 1608 | struct resource res; |
| 1609 | int port, retval; |
| 1610 | |
| 1611 | tphy = devm_kzalloc(dev, sizeof(*tphy), GFP_KERNEL); |
| 1612 | if (!tphy) |
| 1613 | return -ENOMEM; |
| 1614 | |
| 1615 | tphy->pdata = of_device_get_match_data(dev); |
| 1616 | if (!tphy->pdata) |
| 1617 | return -EINVAL; |
| 1618 | |
| 1619 | tphy->nphys = of_get_child_count(np); |
| 1620 | tphy->phys = devm_kcalloc(dev, tphy->nphys, |
| 1621 | sizeof(*tphy->phys), GFP_KERNEL); |
| 1622 | if (!tphy->phys) |
| 1623 | return -ENOMEM; |
| 1624 | |
| 1625 | tphy->dev = dev; |
| 1626 | platform_set_drvdata(pdev, tphy); |
| 1627 | |
| 1628 | sif_res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 1629 | /* SATA phy of V1 needn't it if not shared with PCIe or USB */ |
| 1630 | if (sif_res && tphy->pdata->version == MTK_PHY_V1) { |
| 1631 | /* get banks shared by multiple phys */ |
| 1632 | tphy->sif_base = devm_ioremap_resource(dev, sif_res); |
| 1633 | if (IS_ERR(tphy->sif_base)) { |
| 1634 | dev_err(dev, "failed to remap sif regs\n"); |
| 1635 | return PTR_ERR(tphy->sif_base); |
| 1636 | } |
| 1637 | } |
| 1638 | |
| 1639 | /* it's deprecated, make it optional for backward compatibility */ |
| 1640 | tphy->u3phya_ref = devm_clk_get_optional(dev, "u3phya_ref"); |
| 1641 | if (IS_ERR(tphy->u3phya_ref)) |
| 1642 | return PTR_ERR(tphy->u3phya_ref); |
| 1643 | |
| 1644 | tphy->src_ref_clk = U3P_REF_CLK; |
| 1645 | tphy->src_coef = U3P_SLEW_RATE_COEF; |
| 1646 | /* update parameters of slew rate calibrate if exist */ |
| 1647 | device_property_read_u32(dev, "mediatek,src-ref-clk-mhz", |
| 1648 | &tphy->src_ref_clk); |
| 1649 | device_property_read_u32(dev, "mediatek,src-coef", &tphy->src_coef); |
| 1650 | |
| 1651 | port = 0; |
| 1652 | for_each_child_of_node(np, child_np) { |
| 1653 | struct mtk_phy_instance *instance; |
| 1654 | struct phy *phy; |
| 1655 | |
| 1656 | instance = devm_kzalloc(dev, sizeof(*instance), GFP_KERNEL); |
| 1657 | if (!instance) { |
| 1658 | retval = -ENOMEM; |
| 1659 | goto put_child; |
| 1660 | } |
| 1661 | |
| 1662 | tphy->phys[port] = instance; |
| 1663 | |
| 1664 | phy = devm_phy_create(dev, child_np, &mtk_tphy_ops); |
| 1665 | if (IS_ERR(phy)) { |
| 1666 | dev_err(dev, "failed to create phy\n"); |
| 1667 | retval = PTR_ERR(phy); |
| 1668 | goto put_child; |
| 1669 | } |
| 1670 | |
| 1671 | retval = of_address_to_resource(child_np, 0, &res); |
| 1672 | if (retval) { |
| 1673 | dev_err(dev, "failed to get address resource(id-%d)\n", |
| 1674 | port); |
| 1675 | goto put_child; |
| 1676 | } |
| 1677 | |
| 1678 | instance->port_base = devm_ioremap_resource(&phy->dev, &res); |
| 1679 | if (IS_ERR(instance->port_base)) { |
| 1680 | dev_err(dev, "failed to remap phy regs\n"); |
| 1681 | retval = PTR_ERR(instance->port_base); |
| 1682 | goto put_child; |
| 1683 | } |
| 1684 | |
| 1685 | instance->phy = phy; |
| 1686 | instance->index = port; |
| 1687 | phy_set_drvdata(phy, instance); |
| 1688 | port++; |
| 1689 | |
| 1690 | /* if deprecated clock is provided, ignore instance's one */ |
| 1691 | if (tphy->u3phya_ref) |
| 1692 | continue; |
| 1693 | |
| 1694 | instance->ref_clk = devm_clk_get(&phy->dev, "ref"); |
| 1695 | if (IS_ERR(instance->ref_clk)) { |
| 1696 | dev_err(dev, "failed to get ref_clk(id-%d)\n", port); |
| 1697 | retval = PTR_ERR(instance->ref_clk); |
| 1698 | goto put_child; |
| 1699 | } |
| 1700 | } |
| 1701 | |
| 1702 | provider = devm_of_phy_provider_register(dev, mtk_phy_xlate); |
| 1703 | |
| 1704 | return PTR_ERR_OR_ZERO(provider); |
| 1705 | put_child: |
| 1706 | of_node_put(child_np); |
| 1707 | return retval; |
| 1708 | } |
| 1709 | |
| 1710 | static struct platform_driver mtk_tphy_driver = { |
| 1711 | .probe = mtk_tphy_probe, |
| 1712 | .driver = { |
| 1713 | .name = "mtk-tphy", |
| 1714 | .of_match_table = mtk_tphy_id_table, |
| 1715 | }, |
| 1716 | }; |
| 1717 | |
| 1718 | module_platform_driver(mtk_tphy_driver); |
| 1719 | |
| 1720 | MODULE_AUTHOR("Chunfeng Yun <chunfeng.yun@mediatek.com>"); |
| 1721 | MODULE_DESCRIPTION("MediaTek T-PHY driver"); |
| 1722 | MODULE_LICENSE("GPL v2"); |