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