blob: 42a1174da6cc741f0d0c5946bf7abcbb7576e2c0 [file] [log] [blame]
Chunfeng Yun3003cfa2018-06-29 10:20:27 +08001// SPDX-License-Identifier: GPL-2.0
Chunfeng Yundc7f1902015-09-29 11:01:36 +08002/*
3 * Copyright (c) 2015 MediaTek Inc.
4 * Author: Chunfeng Yun <chunfeng.yun@mediatek.com>
5 *
Chunfeng Yundc7f1902015-09-29 11:01:36 +08006 */
7
8#include <dt-bindings/phy/phy.h>
9#include <linux/clk.h>
10#include <linux/delay.h>
11#include <linux/io.h>
Chunfeng Yun75f072f2015-12-04 10:11:05 +080012#include <linux/iopoll.h>
Chunfeng Yundc7f1902015-09-29 11:01:36 +080013#include <linux/module.h>
14#include <linux/of_address.h>
Chunfeng Yune4b227c2017-12-28 16:40:36 +053015#include <linux/of_device.h>
Chunfeng Yundc7f1902015-09-29 11:01:36 +080016#include <linux/phy/phy.h>
17#include <linux/platform_device.h>
18
Chunfeng Yun8d6e19572017-03-31 15:35:31 +080019/* version V1 sub-banks offset base address */
20/* banks shared by multiple phys */
21#define SSUSB_SIFSLV_V1_SPLLC 0x000 /* shared by u3 phys */
22#define SSUSB_SIFSLV_V1_U2FREQ 0x100 /* shared by u2 phys */
Chunfeng Yun554a56f2017-09-21 18:31:48 +080023#define SSUSB_SIFSLV_V1_CHIP 0x300 /* shared by u3 phys */
Chunfeng Yun8d6e19572017-03-31 15:35:31 +080024/* u2 phy bank */
25#define SSUSB_SIFSLV_V1_U2PHY_COM 0x000
Ryder Lee4ab26cb2017-08-03 18:01:01 +080026/* u3/pcie/sata phy banks */
Chunfeng Yun8d6e19572017-03-31 15:35:31 +080027#define SSUSB_SIFSLV_V1_U3PHYD 0x000
28#define SSUSB_SIFSLV_V1_U3PHYA 0x200
Chunfeng Yundc7f1902015-09-29 11:01:36 +080029
Chunfeng Yun27974e62021-07-23 16:22:41 +080030/* version V2/V3 sub-banks offset base address */
31/* V3: U2FREQ is not used anymore, but reserved */
Chunfeng Yun8d6e19572017-03-31 15:35:31 +080032/* 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
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +080036/* u3/pcie/sata phy banks */
Chunfeng Yun8d6e19572017-03-31 15:35:31 +080037#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
Chunfeng Yundc7f1902015-09-29 11:01:36 +080041
Chunfeng Yun8d6e19572017-03-31 15:35:31 +080042#define U3P_USBPHYACR0 0x000
Chunfeng Yundc7f1902015-09-29 11:01:36 +080043#define PA0_RG_U2PLL_FORCE_ON BIT(15)
Chunfeng Yunc0250fe2017-03-31 15:35:32 +080044#define PA0_RG_USB20_INTR_EN BIT(5)
Chunfeng Yundc7f1902015-09-29 11:01:36 +080045
Chunfeng Yun8158e912018-06-29 10:20:29 +080046#define U3P_USBPHYACR1 0x004
Chunfeng Yun410572e2020-02-11 11:21:12 +080047#define PA1_RG_INTR_CAL GENMASK(23, 19)
48#define PA1_RG_INTR_CAL_VAL(x) ((0x1f & (x)) << 19)
Chunfeng Yun8158e912018-06-29 10:20:29 +080049#define PA1_RG_VRT_SEL GENMASK(14, 12)
50#define PA1_RG_VRT_SEL_VAL(x) ((0x7 & (x)) << 12)
51#define PA1_RG_TERM_SEL GENMASK(10, 8)
52#define PA1_RG_TERM_SEL_VAL(x) ((0x7 & (x)) << 8)
53
Chunfeng Yun8d6e19572017-03-31 15:35:31 +080054#define U3P_USBPHYACR2 0x008
Chunfeng Yundc7f1902015-09-29 11:01:36 +080055#define PA2_RG_SIF_U2PLL_FORCE_EN BIT(18)
56
Chunfeng Yun8d6e19572017-03-31 15:35:31 +080057#define U3P_USBPHYACR5 0x014
Chunfeng Yun75f072f2015-12-04 10:11:05 +080058#define PA5_RG_U2_HSTX_SRCAL_EN BIT(15)
Chunfeng Yundc7f1902015-09-29 11:01:36 +080059#define PA5_RG_U2_HSTX_SRCTRL GENMASK(14, 12)
60#define PA5_RG_U2_HSTX_SRCTRL_VAL(x) ((0x7 & (x)) << 12)
61#define PA5_RG_U2_HS_100U_U3_EN BIT(11)
62
Chunfeng Yun8d6e19572017-03-31 15:35:31 +080063#define U3P_USBPHYACR6 0x018
Chunfeng Yundc7f1902015-09-29 11:01:36 +080064#define PA6_RG_U2_BC11_SW_EN BIT(23)
65#define PA6_RG_U2_OTG_VBUSCMP_EN BIT(20)
Chunfeng Yun8be5a672020-01-08 09:52:01 +080066#define PA6_RG_U2_DISCTH GENMASK(7, 4)
67#define PA6_RG_U2_DISCTH_VAL(x) ((0xf & (x)) << 4)
Chunfeng Yun43f53b12015-12-04 10:08:56 +080068#define PA6_RG_U2_SQTH GENMASK(3, 0)
69#define PA6_RG_U2_SQTH_VAL(x) (0xf & (x))
Chunfeng Yundc7f1902015-09-29 11:01:36 +080070
Chunfeng Yun8d6e19572017-03-31 15:35:31 +080071#define U3P_U2PHYACR4 0x020
Chunfeng Yundc7f1902015-09-29 11:01:36 +080072#define P2C_RG_USB20_GPIO_CTL BIT(9)
73#define P2C_USB20_GPIO_MODE BIT(8)
74#define P2C_U2_GPIO_CTR_MSK (P2C_RG_USB20_GPIO_CTL | P2C_USB20_GPIO_MODE)
75
Chunfeng Yun8d6e19572017-03-31 15:35:31 +080076#define U3D_U2PHYDCR0 0x060
Chunfeng Yundc7f1902015-09-29 11:01:36 +080077#define P2C_RG_SIF_U2PLL_FORCE_ON BIT(24)
78
Chunfeng Yun8d6e19572017-03-31 15:35:31 +080079#define U3P_U2PHYDTM0 0x068
Chunfeng Yundc7f1902015-09-29 11:01:36 +080080#define P2C_FORCE_UART_EN BIT(26)
81#define P2C_FORCE_DATAIN BIT(23)
82#define P2C_FORCE_DM_PULLDOWN BIT(21)
83#define P2C_FORCE_DP_PULLDOWN BIT(20)
84#define P2C_FORCE_XCVRSEL BIT(19)
85#define P2C_FORCE_SUSPENDM BIT(18)
86#define P2C_FORCE_TERMSEL BIT(17)
87#define P2C_RG_DATAIN GENMASK(13, 10)
88#define P2C_RG_DATAIN_VAL(x) ((0xf & (x)) << 10)
89#define P2C_RG_DMPULLDOWN BIT(7)
90#define P2C_RG_DPPULLDOWN BIT(6)
91#define P2C_RG_XCVRSEL GENMASK(5, 4)
92#define P2C_RG_XCVRSEL_VAL(x) ((0x3 & (x)) << 4)
93#define P2C_RG_SUSPENDM BIT(3)
94#define P2C_RG_TERMSEL BIT(2)
95#define P2C_DTM0_PART_MASK \
96 (P2C_FORCE_DATAIN | P2C_FORCE_DM_PULLDOWN | \
97 P2C_FORCE_DP_PULLDOWN | P2C_FORCE_XCVRSEL | \
98 P2C_FORCE_TERMSEL | P2C_RG_DMPULLDOWN | \
99 P2C_RG_DPPULLDOWN | P2C_RG_TERMSEL)
100
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800101#define U3P_U2PHYDTM1 0x06C
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800102#define P2C_RG_UART_EN BIT(16)
Chunfeng Yun5954a102017-09-21 18:31:49 +0800103#define P2C_FORCE_IDDIG BIT(9)
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800104#define P2C_RG_VBUSVALID BIT(5)
105#define P2C_RG_SESSEND BIT(4)
106#define P2C_RG_AVALID BIT(2)
Chunfeng Yun5954a102017-09-21 18:31:49 +0800107#define P2C_RG_IDDIG BIT(1)
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800108
Chunfeng Yund4f97f12018-06-29 10:20:30 +0800109#define U3P_U2PHYBC12C 0x080
110#define P2C_RG_CHGDT_EN BIT(0)
111
Ryder Lee44a6d6c2017-08-03 18:01:00 +0800112#define U3P_U3_CHIP_GPIO_CTLD 0x0c
113#define P3C_REG_IP_SW_RST BIT(31)
114#define P3C_MCU_BUS_CK_GATE_EN BIT(30)
115#define P3C_FORCE_IP_SW_RST BIT(29)
116
117#define U3P_U3_CHIP_GPIO_CTLE 0x10
118#define P3C_RG_SWRST_U3_PHYD BIT(25)
119#define P3C_RG_SWRST_U3_PHYD_FORCE_EN BIT(24)
120
121#define U3P_U3_PHYA_REG0 0x000
122#define P3A_RG_CLKDRV_OFF GENMASK(3, 2)
123#define P3A_RG_CLKDRV_OFF_VAL(x) ((0x3 & (x)) << 2)
124
125#define U3P_U3_PHYA_REG1 0x004
126#define P3A_RG_CLKDRV_AMP GENMASK(31, 29)
127#define P3A_RG_CLKDRV_AMP_VAL(x) ((0x7 & (x)) << 29)
128
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800129#define U3P_U3_PHYA_REG6 0x018
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800130#define P3A_RG_TX_EIDLE_CM GENMASK(31, 28)
131#define P3A_RG_TX_EIDLE_CM_VAL(x) ((0xf & (x)) << 28)
132
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800133#define U3P_U3_PHYA_REG9 0x024
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800134#define P3A_RG_RX_DAC_MUX GENMASK(5, 1)
135#define P3A_RG_RX_DAC_MUX_VAL(x) ((0x1f & (x)) << 1)
136
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800137#define U3P_U3_PHYA_DA_REG0 0x100
Ryder Lee44a6d6c2017-08-03 18:01:00 +0800138#define P3A_RG_XTAL_EXT_PE2H GENMASK(17, 16)
139#define P3A_RG_XTAL_EXT_PE2H_VAL(x) ((0x3 & (x)) << 16)
140#define P3A_RG_XTAL_EXT_PE1H GENMASK(13, 12)
141#define P3A_RG_XTAL_EXT_PE1H_VAL(x) ((0x3 & (x)) << 12)
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800142#define P3A_RG_XTAL_EXT_EN_U3 GENMASK(11, 10)
143#define P3A_RG_XTAL_EXT_EN_U3_VAL(x) ((0x3 & (x)) << 10)
144
Ryder Lee44a6d6c2017-08-03 18:01:00 +0800145#define U3P_U3_PHYA_DA_REG4 0x108
146#define P3A_RG_PLL_DIVEN_PE2H GENMASK(21, 19)
147#define P3A_RG_PLL_BC_PE2H GENMASK(7, 6)
148#define P3A_RG_PLL_BC_PE2H_VAL(x) ((0x3 & (x)) << 6)
149
150#define U3P_U3_PHYA_DA_REG5 0x10c
151#define P3A_RG_PLL_BR_PE2H GENMASK(29, 28)
152#define P3A_RG_PLL_BR_PE2H_VAL(x) ((0x3 & (x)) << 28)
153#define P3A_RG_PLL_IC_PE2H GENMASK(15, 12)
154#define P3A_RG_PLL_IC_PE2H_VAL(x) ((0xf & (x)) << 12)
155
156#define U3P_U3_PHYA_DA_REG6 0x110
157#define P3A_RG_PLL_IR_PE2H GENMASK(19, 16)
158#define P3A_RG_PLL_IR_PE2H_VAL(x) ((0xf & (x)) << 16)
159
160#define U3P_U3_PHYA_DA_REG7 0x114
161#define P3A_RG_PLL_BP_PE2H GENMASK(19, 16)
162#define P3A_RG_PLL_BP_PE2H_VAL(x) ((0xf & (x)) << 16)
163
164#define U3P_U3_PHYA_DA_REG20 0x13c
165#define P3A_RG_PLL_DELTA1_PE2H GENMASK(31, 16)
166#define P3A_RG_PLL_DELTA1_PE2H_VAL(x) ((0xffff & (x)) << 16)
167
168#define U3P_U3_PHYA_DA_REG25 0x148
169#define P3A_RG_PLL_DELTA_PE2H GENMASK(15, 0)
170#define P3A_RG_PLL_DELTA_PE2H_VAL(x) (0xffff & (x))
171
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800172#define U3P_U3_PHYD_LFPS1 0x00c
Chunfeng Yun98cd83a2017-03-31 15:35:28 +0800173#define P3D_RG_FWAKE_TH GENMASK(21, 16)
174#define P3D_RG_FWAKE_TH_VAL(x) ((0x3f & (x)) << 16)
175
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800176#define U3P_U3_PHYD_CDR1 0x05c
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800177#define P3D_RG_CDR_BIR_LTD1 GENMASK(28, 24)
178#define P3D_RG_CDR_BIR_LTD1_VAL(x) ((0x1f & (x)) << 24)
179#define P3D_RG_CDR_BIR_LTD0 GENMASK(12, 8)
180#define P3D_RG_CDR_BIR_LTD0_VAL(x) ((0x1f & (x)) << 8)
181
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800182#define U3P_U3_PHYD_RXDET1 0x128
Chunfeng Yun1969f692017-03-31 15:35:27 +0800183#define P3D_RG_RXDET_STB2_SET GENMASK(17, 9)
184#define P3D_RG_RXDET_STB2_SET_VAL(x) ((0x1ff & (x)) << 9)
185
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800186#define U3P_U3_PHYD_RXDET2 0x12c
Chunfeng Yun1969f692017-03-31 15:35:27 +0800187#define P3D_RG_RXDET_STB2_SET_P3 GENMASK(8, 0)
188#define P3D_RG_RXDET_STB2_SET_P3_VAL(x) (0x1ff & (x))
189
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800190#define U3P_SPLLC_XTALCTL3 0x018
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800191#define XC3_RG_U3_XTAL_RX_PWD BIT(9)
192#define XC3_RG_U3_FRC_XTAL_RX_PWD BIT(8)
193
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800194#define U3P_U2FREQ_FMCR0 0x00
Chunfeng Yun75f072f2015-12-04 10:11:05 +0800195#define P2F_RG_MONCLK_SEL GENMASK(27, 26)
196#define P2F_RG_MONCLK_SEL_VAL(x) ((0x3 & (x)) << 26)
197#define P2F_RG_FREQDET_EN BIT(24)
198#define P2F_RG_CYCLECNT GENMASK(23, 0)
199#define P2F_RG_CYCLECNT_VAL(x) ((P2F_RG_CYCLECNT) & (x))
200
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800201#define U3P_U2FREQ_VALUE 0x0c
Chunfeng Yun75f072f2015-12-04 10:11:05 +0800202
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800203#define U3P_U2FREQ_FMMONR1 0x10
Chunfeng Yun75f072f2015-12-04 10:11:05 +0800204#define P2F_USB_FM_VALID BIT(0)
205#define P2F_RG_FRCK_EN BIT(8)
206
207#define U3P_REF_CLK 26 /* MHZ */
208#define U3P_SLEW_RATE_COEF 28
209#define U3P_SR_COEF_DIVISOR 1000
210#define U3P_FM_DET_CYCLE_CNT 1024
211
Ryder Lee4ab26cb2017-08-03 18:01:01 +0800212/* SATA register setting */
213#define PHYD_CTRL_SIGNAL_MODE4 0x1c
214/* CDR Charge Pump P-path current adjustment */
215#define RG_CDR_BICLTD1_GEN1_MSK GENMASK(23, 20)
216#define RG_CDR_BICLTD1_GEN1_VAL(x) ((0xf & (x)) << 20)
217#define RG_CDR_BICLTD0_GEN1_MSK GENMASK(11, 8)
218#define RG_CDR_BICLTD0_GEN1_VAL(x) ((0xf & (x)) << 8)
219
220#define PHYD_DESIGN_OPTION2 0x24
221/* Symbol lock count selection */
222#define RG_LOCK_CNT_SEL_MSK GENMASK(5, 4)
223#define RG_LOCK_CNT_SEL_VAL(x) ((0x3 & (x)) << 4)
224
225#define PHYD_DESIGN_OPTION9 0x40
226/* COMWAK GAP width window */
227#define RG_TG_MAX_MSK GENMASK(20, 16)
228#define RG_TG_MAX_VAL(x) ((0x1f & (x)) << 16)
229/* COMINIT GAP width window */
230#define RG_T2_MAX_MSK GENMASK(13, 8)
231#define RG_T2_MAX_VAL(x) ((0x3f & (x)) << 8)
232/* COMWAK GAP width window */
233#define RG_TG_MIN_MSK GENMASK(7, 5)
234#define RG_TG_MIN_VAL(x) ((0x7 & (x)) << 5)
235/* COMINIT GAP width window */
236#define RG_T2_MIN_MSK GENMASK(4, 0)
237#define RG_T2_MIN_VAL(x) (0x1f & (x))
238
239#define ANA_RG_CTRL_SIGNAL1 0x4c
240/* TX driver tail current control for 0dB de-empahsis mdoe for Gen1 speed */
241#define RG_IDRV_0DB_GEN1_MSK GENMASK(13, 8)
242#define RG_IDRV_0DB_GEN1_VAL(x) ((0x3f & (x)) << 8)
243
244#define ANA_RG_CTRL_SIGNAL4 0x58
245#define RG_CDR_BICLTR_GEN1_MSK GENMASK(23, 20)
246#define RG_CDR_BICLTR_GEN1_VAL(x) ((0xf & (x)) << 20)
247/* Loop filter R1 resistance adjustment for Gen1 speed */
248#define RG_CDR_BR_GEN2_MSK GENMASK(10, 8)
249#define RG_CDR_BR_GEN2_VAL(x) ((0x7 & (x)) << 8)
250
251#define ANA_RG_CTRL_SIGNAL6 0x60
252/* I-path capacitance adjustment for Gen1 */
253#define RG_CDR_BC_GEN1_MSK GENMASK(28, 24)
254#define RG_CDR_BC_GEN1_VAL(x) ((0x1f & (x)) << 24)
255#define RG_CDR_BIRLTR_GEN1_MSK GENMASK(4, 0)
256#define RG_CDR_BIRLTR_GEN1_VAL(x) (0x1f & (x))
257
258#define ANA_EQ_EYE_CTRL_SIGNAL1 0x6c
259/* RX Gen1 LEQ tuning step */
260#define RG_EQ_DLEQ_LFI_GEN1_MSK GENMASK(11, 8)
261#define RG_EQ_DLEQ_LFI_GEN1_VAL(x) ((0xf & (x)) << 8)
262
263#define ANA_EQ_EYE_CTRL_SIGNAL4 0xd8
264#define RG_CDR_BIRLTD0_GEN1_MSK GENMASK(20, 16)
265#define RG_CDR_BIRLTD0_GEN1_VAL(x) ((0x1f & (x)) << 16)
266
267#define ANA_EQ_EYE_CTRL_SIGNAL5 0xdc
268#define RG_CDR_BIRLTD0_GEN3_MSK GENMASK(4, 0)
269#define RG_CDR_BIRLTD0_GEN3_VAL(x) (0x1f & (x))
270
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800271enum mtk_phy_version {
272 MTK_PHY_V1 = 1,
273 MTK_PHY_V2,
Chunfeng Yun27974e62021-07-23 16:22:41 +0800274 MTK_PHY_V3,
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800275};
276
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800277struct mtk_phy_pdata {
Chunfeng Yune1d76532016-04-20 08:14:02 +0800278 /* avoid RX sensitivity level degradation only for mt8173 */
279 bool avoid_rx_sen_degradation;
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800280 enum mtk_phy_version version;
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800281};
282
283struct u2phy_banks {
284 void __iomem *misc;
285 void __iomem *fmreg;
286 void __iomem *com;
287};
288
289struct u3phy_banks {
290 void __iomem *spllc;
291 void __iomem *chip;
292 void __iomem *phyd; /* include u3phyd_bank2 */
293 void __iomem *phya; /* include u3phya_da */
Chunfeng Yune1d76532016-04-20 08:14:02 +0800294};
295
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800296struct mtk_phy_instance {
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800297 struct phy *phy;
298 void __iomem *port_base;
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800299 union {
300 struct u2phy_banks u2_banks;
301 struct u3phy_banks u3_banks;
302 };
Chunfeng Yun12d0c0b2020-02-11 11:21:15 +0800303 struct clk *ref_clk; /* reference clock of (digital) phy */
304 struct clk *da_ref_clk; /* reference clock of analog phy */
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800305 u32 index;
306 u8 type;
Chunfeng Yun8158e912018-06-29 10:20:29 +0800307 int eye_src;
308 int eye_vrt;
309 int eye_term;
Chunfeng Yun410572e2020-02-11 11:21:12 +0800310 int intr;
Chunfeng Yun8be5a672020-01-08 09:52:01 +0800311 int discth;
Chunfeng Yund4f97f12018-06-29 10:20:30 +0800312 bool bc12_en;
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800313};
314
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800315struct mtk_tphy {
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800316 struct device *dev;
Chunfeng Yun04466ef2017-03-31 15:35:29 +0800317 void __iomem *sif_base; /* only shared sif */
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800318 const struct mtk_phy_pdata *pdata;
319 struct mtk_phy_instance **phys;
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800320 int nphys;
Chunfeng Yun8833ebf42018-03-12 13:25:39 +0800321 int src_ref_clk; /* MHZ, reference clock for slew rate calibrate */
322 int src_coef; /* coefficient for slew rate calibrate */
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800323};
324
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800325static void hs_slew_rate_calibrate(struct mtk_tphy *tphy,
326 struct mtk_phy_instance *instance)
Chunfeng Yun75f072f2015-12-04 10:11:05 +0800327{
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800328 struct u2phy_banks *u2_banks = &instance->u2_banks;
329 void __iomem *fmreg = u2_banks->fmreg;
330 void __iomem *com = u2_banks->com;
Chunfeng Yun75f072f2015-12-04 10:11:05 +0800331 int calibration_val;
332 int fm_out;
333 u32 tmp;
334
Chunfeng Yun27974e62021-07-23 16:22:41 +0800335 /* HW V3 doesn't support slew rate cal anymore */
336 if (tphy->pdata->version == MTK_PHY_V3)
337 return;
338
Chunfeng Yun8158e912018-06-29 10:20:29 +0800339 /* use force value */
340 if (instance->eye_src)
341 return;
342
Chunfeng Yun75f072f2015-12-04 10:11:05 +0800343 /* enable USB ring oscillator */
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800344 tmp = readl(com + U3P_USBPHYACR5);
Chunfeng Yun75f072f2015-12-04 10:11:05 +0800345 tmp |= PA5_RG_U2_HSTX_SRCAL_EN;
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800346 writel(tmp, com + U3P_USBPHYACR5);
Chunfeng Yun75f072f2015-12-04 10:11:05 +0800347 udelay(1);
348
349 /*enable free run clock */
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800350 tmp = readl(fmreg + U3P_U2FREQ_FMMONR1);
Chunfeng Yun75f072f2015-12-04 10:11:05 +0800351 tmp |= P2F_RG_FRCK_EN;
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800352 writel(tmp, fmreg + U3P_U2FREQ_FMMONR1);
Chunfeng Yun75f072f2015-12-04 10:11:05 +0800353
354 /* set cycle count as 1024, and select u2 channel */
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800355 tmp = readl(fmreg + U3P_U2FREQ_FMCR0);
Chunfeng Yun75f072f2015-12-04 10:11:05 +0800356 tmp &= ~(P2F_RG_CYCLECNT | P2F_RG_MONCLK_SEL);
357 tmp |= P2F_RG_CYCLECNT_VAL(U3P_FM_DET_CYCLE_CNT);
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800358 if (tphy->pdata->version == MTK_PHY_V1)
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800359 tmp |= P2F_RG_MONCLK_SEL_VAL(instance->index >> 1);
360
361 writel(tmp, fmreg + U3P_U2FREQ_FMCR0);
Chunfeng Yun75f072f2015-12-04 10:11:05 +0800362
363 /* enable frequency meter */
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800364 tmp = readl(fmreg + U3P_U2FREQ_FMCR0);
Chunfeng Yun75f072f2015-12-04 10:11:05 +0800365 tmp |= P2F_RG_FREQDET_EN;
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800366 writel(tmp, fmreg + U3P_U2FREQ_FMCR0);
Chunfeng Yun75f072f2015-12-04 10:11:05 +0800367
368 /* ignore return value */
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800369 readl_poll_timeout(fmreg + U3P_U2FREQ_FMMONR1, tmp,
370 (tmp & P2F_USB_FM_VALID), 10, 200);
Chunfeng Yun75f072f2015-12-04 10:11:05 +0800371
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800372 fm_out = readl(fmreg + U3P_U2FREQ_VALUE);
Chunfeng Yun75f072f2015-12-04 10:11:05 +0800373
374 /* disable frequency meter */
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800375 tmp = readl(fmreg + U3P_U2FREQ_FMCR0);
Chunfeng Yun75f072f2015-12-04 10:11:05 +0800376 tmp &= ~P2F_RG_FREQDET_EN;
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800377 writel(tmp, fmreg + U3P_U2FREQ_FMCR0);
Chunfeng Yun75f072f2015-12-04 10:11:05 +0800378
379 /*disable free run clock */
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800380 tmp = readl(fmreg + U3P_U2FREQ_FMMONR1);
Chunfeng Yun75f072f2015-12-04 10:11:05 +0800381 tmp &= ~P2F_RG_FRCK_EN;
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800382 writel(tmp, fmreg + U3P_U2FREQ_FMMONR1);
Chunfeng Yun75f072f2015-12-04 10:11:05 +0800383
384 if (fm_out) {
Chunfeng Yun8833ebf42018-03-12 13:25:39 +0800385 /* ( 1024 / FM_OUT ) x reference clock frequency x coef */
386 tmp = tphy->src_ref_clk * tphy->src_coef;
387 tmp = (tmp * U3P_FM_DET_CYCLE_CNT) / fm_out;
Chunfeng Yun75f072f2015-12-04 10:11:05 +0800388 calibration_val = DIV_ROUND_CLOSEST(tmp, U3P_SR_COEF_DIVISOR);
389 } else {
390 /* if FM detection fail, set default value */
391 calibration_val = 4;
392 }
Chunfeng Yun8833ebf42018-03-12 13:25:39 +0800393 dev_dbg(tphy->dev, "phy:%d, fm_out:%d, calib:%d (clk:%d, coef:%d)\n",
394 instance->index, fm_out, calibration_val,
395 tphy->src_ref_clk, tphy->src_coef);
Chunfeng Yun75f072f2015-12-04 10:11:05 +0800396
397 /* set HS slew rate */
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800398 tmp = readl(com + U3P_USBPHYACR5);
Chunfeng Yun75f072f2015-12-04 10:11:05 +0800399 tmp &= ~PA5_RG_U2_HSTX_SRCTRL;
400 tmp |= PA5_RG_U2_HSTX_SRCTRL_VAL(calibration_val);
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800401 writel(tmp, com + U3P_USBPHYACR5);
Chunfeng Yun75f072f2015-12-04 10:11:05 +0800402
403 /* disable USB ring oscillator */
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800404 tmp = readl(com + U3P_USBPHYACR5);
Chunfeng Yun75f072f2015-12-04 10:11:05 +0800405 tmp &= ~PA5_RG_U2_HSTX_SRCAL_EN;
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800406 writel(tmp, com + U3P_USBPHYACR5);
Chunfeng Yun75f072f2015-12-04 10:11:05 +0800407}
408
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800409static void u3_phy_instance_init(struct mtk_tphy *tphy,
410 struct mtk_phy_instance *instance)
Chunfeng Yun04466ef2017-03-31 15:35:29 +0800411{
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800412 struct u3phy_banks *u3_banks = &instance->u3_banks;
Chunfeng Yun04466ef2017-03-31 15:35:29 +0800413 u32 tmp;
414
415 /* gating PCIe Analog XTAL clock */
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800416 tmp = readl(u3_banks->spllc + U3P_SPLLC_XTALCTL3);
Chunfeng Yun04466ef2017-03-31 15:35:29 +0800417 tmp |= XC3_RG_U3_XTAL_RX_PWD | XC3_RG_U3_FRC_XTAL_RX_PWD;
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800418 writel(tmp, u3_banks->spllc + U3P_SPLLC_XTALCTL3);
Chunfeng Yun04466ef2017-03-31 15:35:29 +0800419
420 /* gating XSQ */
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800421 tmp = readl(u3_banks->phya + U3P_U3_PHYA_DA_REG0);
Chunfeng Yun04466ef2017-03-31 15:35:29 +0800422 tmp &= ~P3A_RG_XTAL_EXT_EN_U3;
423 tmp |= P3A_RG_XTAL_EXT_EN_U3_VAL(2);
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800424 writel(tmp, u3_banks->phya + U3P_U3_PHYA_DA_REG0);
Chunfeng Yun04466ef2017-03-31 15:35:29 +0800425
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800426 tmp = readl(u3_banks->phya + U3P_U3_PHYA_REG9);
Chunfeng Yun04466ef2017-03-31 15:35:29 +0800427 tmp &= ~P3A_RG_RX_DAC_MUX;
428 tmp |= P3A_RG_RX_DAC_MUX_VAL(4);
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800429 writel(tmp, u3_banks->phya + U3P_U3_PHYA_REG9);
Chunfeng Yun04466ef2017-03-31 15:35:29 +0800430
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800431 tmp = readl(u3_banks->phya + U3P_U3_PHYA_REG6);
Chunfeng Yun04466ef2017-03-31 15:35:29 +0800432 tmp &= ~P3A_RG_TX_EIDLE_CM;
433 tmp |= P3A_RG_TX_EIDLE_CM_VAL(0xe);
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800434 writel(tmp, u3_banks->phya + U3P_U3_PHYA_REG6);
Chunfeng Yun04466ef2017-03-31 15:35:29 +0800435
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800436 tmp = readl(u3_banks->phyd + U3P_U3_PHYD_CDR1);
Chunfeng Yun04466ef2017-03-31 15:35:29 +0800437 tmp &= ~(P3D_RG_CDR_BIR_LTD0 | P3D_RG_CDR_BIR_LTD1);
438 tmp |= P3D_RG_CDR_BIR_LTD0_VAL(0xc) | P3D_RG_CDR_BIR_LTD1_VAL(0x3);
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800439 writel(tmp, u3_banks->phyd + U3P_U3_PHYD_CDR1);
Chunfeng Yun04466ef2017-03-31 15:35:29 +0800440
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800441 tmp = readl(u3_banks->phyd + U3P_U3_PHYD_LFPS1);
Chunfeng Yun04466ef2017-03-31 15:35:29 +0800442 tmp &= ~P3D_RG_FWAKE_TH;
443 tmp |= P3D_RG_FWAKE_TH_VAL(0x34);
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800444 writel(tmp, u3_banks->phyd + U3P_U3_PHYD_LFPS1);
Chunfeng Yun04466ef2017-03-31 15:35:29 +0800445
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800446 tmp = readl(u3_banks->phyd + U3P_U3_PHYD_RXDET1);
Chunfeng Yun04466ef2017-03-31 15:35:29 +0800447 tmp &= ~P3D_RG_RXDET_STB2_SET;
448 tmp |= P3D_RG_RXDET_STB2_SET_VAL(0x10);
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800449 writel(tmp, u3_banks->phyd + U3P_U3_PHYD_RXDET1);
Chunfeng Yun04466ef2017-03-31 15:35:29 +0800450
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800451 tmp = readl(u3_banks->phyd + U3P_U3_PHYD_RXDET2);
Chunfeng Yun04466ef2017-03-31 15:35:29 +0800452 tmp &= ~P3D_RG_RXDET_STB2_SET_P3;
453 tmp |= P3D_RG_RXDET_STB2_SET_P3_VAL(0x10);
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800454 writel(tmp, u3_banks->phyd + U3P_U3_PHYD_RXDET2);
Chunfeng Yun04466ef2017-03-31 15:35:29 +0800455
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800456 dev_dbg(tphy->dev, "%s(%d)\n", __func__, instance->index);
Chunfeng Yun04466ef2017-03-31 15:35:29 +0800457}
458
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800459static void u2_phy_instance_init(struct mtk_tphy *tphy,
460 struct mtk_phy_instance *instance)
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800461{
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800462 struct u2phy_banks *u2_banks = &instance->u2_banks;
463 void __iomem *com = u2_banks->com;
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800464 u32 index = instance->index;
465 u32 tmp;
466
Chunfeng Yun00c00922017-12-07 19:53:34 +0800467 /* switch to USB function, and enable usb pll */
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800468 tmp = readl(com + U3P_U2PHYDTM0);
Chunfeng Yun00c00922017-12-07 19:53:34 +0800469 tmp &= ~(P2C_FORCE_UART_EN | P2C_FORCE_SUSPENDM);
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800470 tmp |= P2C_RG_XCVRSEL_VAL(1) | P2C_RG_DATAIN_VAL(0);
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800471 writel(tmp, com + U3P_U2PHYDTM0);
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800472
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800473 tmp = readl(com + U3P_U2PHYDTM1);
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800474 tmp &= ~P2C_RG_UART_EN;
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800475 writel(tmp, com + U3P_U2PHYDTM1);
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800476
Chunfeng Yunc0250fe2017-03-31 15:35:32 +0800477 tmp = readl(com + U3P_USBPHYACR0);
478 tmp |= PA0_RG_USB20_INTR_EN;
479 writel(tmp, com + U3P_USBPHYACR0);
480
481 /* disable switch 100uA current to SSUSB */
482 tmp = readl(com + U3P_USBPHYACR5);
483 tmp &= ~PA5_RG_U2_HS_100U_U3_EN;
484 writel(tmp, com + U3P_USBPHYACR5);
485
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800486 if (!index) {
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800487 tmp = readl(com + U3P_U2PHYACR4);
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800488 tmp &= ~P2C_U2_GPIO_CTR_MSK;
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800489 writel(tmp, com + U3P_U2PHYACR4);
Chunfeng Yune1d76532016-04-20 08:14:02 +0800490 }
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800491
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800492 if (tphy->pdata->avoid_rx_sen_degradation) {
Chunfeng Yune1d76532016-04-20 08:14:02 +0800493 if (!index) {
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800494 tmp = readl(com + U3P_USBPHYACR2);
Chunfeng Yune1d76532016-04-20 08:14:02 +0800495 tmp |= PA2_RG_SIF_U2PLL_FORCE_EN;
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800496 writel(tmp, com + U3P_USBPHYACR2);
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800497
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800498 tmp = readl(com + U3D_U2PHYDCR0);
Chunfeng Yune1d76532016-04-20 08:14:02 +0800499 tmp &= ~P2C_RG_SIF_U2PLL_FORCE_ON;
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800500 writel(tmp, com + U3D_U2PHYDCR0);
Chunfeng Yune1d76532016-04-20 08:14:02 +0800501 } else {
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800502 tmp = readl(com + U3D_U2PHYDCR0);
Chunfeng Yune1d76532016-04-20 08:14:02 +0800503 tmp |= P2C_RG_SIF_U2PLL_FORCE_ON;
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800504 writel(tmp, com + U3D_U2PHYDCR0);
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800505
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800506 tmp = readl(com + U3P_U2PHYDTM0);
Chunfeng Yune1d76532016-04-20 08:14:02 +0800507 tmp |= P2C_RG_SUSPENDM | P2C_FORCE_SUSPENDM;
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800508 writel(tmp, com + U3P_U2PHYDTM0);
Chunfeng Yune1d76532016-04-20 08:14:02 +0800509 }
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800510 }
511
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800512 tmp = readl(com + U3P_USBPHYACR6);
Chunfeng Yun43f53b12015-12-04 10:08:56 +0800513 tmp &= ~PA6_RG_U2_BC11_SW_EN; /* DP/DM BC1.1 path Disable */
514 tmp &= ~PA6_RG_U2_SQTH;
515 tmp |= PA6_RG_U2_SQTH_VAL(2);
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800516 writel(tmp, com + U3P_USBPHYACR6);
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800517
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800518 dev_dbg(tphy->dev, "%s(%d)\n", __func__, index);
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800519}
520
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800521static void u2_phy_instance_power_on(struct mtk_tphy *tphy,
522 struct mtk_phy_instance *instance)
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800523{
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800524 struct u2phy_banks *u2_banks = &instance->u2_banks;
525 void __iomem *com = u2_banks->com;
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800526 u32 index = instance->index;
527 u32 tmp;
528
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800529 tmp = readl(com + U3P_U2PHYDTM0);
Chunfeng Yun00c00922017-12-07 19:53:34 +0800530 tmp &= ~(P2C_RG_XCVRSEL | P2C_RG_DATAIN | P2C_DTM0_PART_MASK);
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800531 writel(tmp, com + U3P_U2PHYDTM0);
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800532
533 /* OTG Enable */
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800534 tmp = readl(com + U3P_USBPHYACR6);
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800535 tmp |= PA6_RG_U2_OTG_VBUSCMP_EN;
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800536 writel(tmp, com + U3P_USBPHYACR6);
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800537
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800538 tmp = readl(com + U3P_U2PHYDTM1);
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800539 tmp |= P2C_RG_VBUSVALID | P2C_RG_AVALID;
540 tmp &= ~P2C_RG_SESSEND;
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800541 writel(tmp, com + U3P_U2PHYDTM1);
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800542
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800543 if (tphy->pdata->avoid_rx_sen_degradation && index) {
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800544 tmp = readl(com + U3D_U2PHYDCR0);
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800545 tmp |= P2C_RG_SIF_U2PLL_FORCE_ON;
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800546 writel(tmp, com + U3D_U2PHYDCR0);
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800547
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800548 tmp = readl(com + U3P_U2PHYDTM0);
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800549 tmp |= P2C_RG_SUSPENDM | P2C_FORCE_SUSPENDM;
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800550 writel(tmp, com + U3P_U2PHYDTM0);
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800551 }
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800552 dev_dbg(tphy->dev, "%s(%d)\n", __func__, index);
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800553}
554
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800555static void u2_phy_instance_power_off(struct mtk_tphy *tphy,
556 struct mtk_phy_instance *instance)
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800557{
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800558 struct u2phy_banks *u2_banks = &instance->u2_banks;
559 void __iomem *com = u2_banks->com;
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800560 u32 index = instance->index;
561 u32 tmp;
562
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800563 tmp = readl(com + U3P_U2PHYDTM0);
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800564 tmp &= ~(P2C_RG_XCVRSEL | P2C_RG_DATAIN);
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800565 writel(tmp, com + U3P_U2PHYDTM0);
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800566
567 /* OTG Disable */
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800568 tmp = readl(com + U3P_USBPHYACR6);
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800569 tmp &= ~PA6_RG_U2_OTG_VBUSCMP_EN;
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800570 writel(tmp, com + U3P_USBPHYACR6);
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800571
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800572 tmp = readl(com + U3P_U2PHYDTM1);
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800573 tmp &= ~(P2C_RG_VBUSVALID | P2C_RG_AVALID);
574 tmp |= P2C_RG_SESSEND;
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800575 writel(tmp, com + U3P_U2PHYDTM1);
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800576
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800577 if (tphy->pdata->avoid_rx_sen_degradation && index) {
Chunfeng Yun00c00922017-12-07 19:53:34 +0800578 tmp = readl(com + U3P_U2PHYDTM0);
579 tmp &= ~(P2C_RG_SUSPENDM | P2C_FORCE_SUSPENDM);
580 writel(tmp, com + U3P_U2PHYDTM0);
581
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800582 tmp = readl(com + U3D_U2PHYDCR0);
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800583 tmp &= ~P2C_RG_SIF_U2PLL_FORCE_ON;
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800584 writel(tmp, com + U3D_U2PHYDCR0);
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800585 }
586
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800587 dev_dbg(tphy->dev, "%s(%d)\n", __func__, index);
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800588}
589
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800590static void u2_phy_instance_exit(struct mtk_tphy *tphy,
591 struct mtk_phy_instance *instance)
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800592{
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800593 struct u2phy_banks *u2_banks = &instance->u2_banks;
594 void __iomem *com = u2_banks->com;
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800595 u32 index = instance->index;
596 u32 tmp;
597
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800598 if (tphy->pdata->avoid_rx_sen_degradation && index) {
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800599 tmp = readl(com + U3D_U2PHYDCR0);
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800600 tmp &= ~P2C_RG_SIF_U2PLL_FORCE_ON;
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800601 writel(tmp, com + U3D_U2PHYDCR0);
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800602
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800603 tmp = readl(com + U3P_U2PHYDTM0);
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800604 tmp &= ~P2C_FORCE_SUSPENDM;
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800605 writel(tmp, com + U3P_U2PHYDTM0);
606 }
607}
608
Chunfeng Yun5954a102017-09-21 18:31:49 +0800609static void u2_phy_instance_set_mode(struct mtk_tphy *tphy,
610 struct mtk_phy_instance *instance,
611 enum phy_mode mode)
612{
613 struct u2phy_banks *u2_banks = &instance->u2_banks;
614 u32 tmp;
615
616 tmp = readl(u2_banks->com + U3P_U2PHYDTM1);
617 switch (mode) {
618 case PHY_MODE_USB_DEVICE:
619 tmp |= P2C_FORCE_IDDIG | P2C_RG_IDDIG;
620 break;
621 case PHY_MODE_USB_HOST:
622 tmp |= P2C_FORCE_IDDIG;
623 tmp &= ~P2C_RG_IDDIG;
624 break;
625 case PHY_MODE_USB_OTG:
626 tmp &= ~(P2C_FORCE_IDDIG | P2C_RG_IDDIG);
627 break;
628 default:
629 return;
630 }
631 writel(tmp, u2_banks->com + U3P_U2PHYDTM1);
632}
633
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800634static void pcie_phy_instance_init(struct mtk_tphy *tphy,
635 struct mtk_phy_instance *instance)
Ryder Lee44a6d6c2017-08-03 18:01:00 +0800636{
637 struct u3phy_banks *u3_banks = &instance->u3_banks;
638 u32 tmp;
639
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800640 if (tphy->pdata->version != MTK_PHY_V1)
Ryder Lee44a6d6c2017-08-03 18:01:00 +0800641 return;
642
643 tmp = readl(u3_banks->phya + U3P_U3_PHYA_DA_REG0);
644 tmp &= ~(P3A_RG_XTAL_EXT_PE1H | P3A_RG_XTAL_EXT_PE2H);
645 tmp |= P3A_RG_XTAL_EXT_PE1H_VAL(0x2) | P3A_RG_XTAL_EXT_PE2H_VAL(0x2);
646 writel(tmp, u3_banks->phya + U3P_U3_PHYA_DA_REG0);
647
648 /* ref clk drive */
649 tmp = readl(u3_banks->phya + U3P_U3_PHYA_REG1);
650 tmp &= ~P3A_RG_CLKDRV_AMP;
651 tmp |= P3A_RG_CLKDRV_AMP_VAL(0x4);
652 writel(tmp, u3_banks->phya + U3P_U3_PHYA_REG1);
653
654 tmp = readl(u3_banks->phya + U3P_U3_PHYA_REG0);
655 tmp &= ~P3A_RG_CLKDRV_OFF;
656 tmp |= P3A_RG_CLKDRV_OFF_VAL(0x1);
657 writel(tmp, u3_banks->phya + U3P_U3_PHYA_REG0);
658
659 /* SSC delta -5000ppm */
660 tmp = readl(u3_banks->phya + U3P_U3_PHYA_DA_REG20);
661 tmp &= ~P3A_RG_PLL_DELTA1_PE2H;
662 tmp |= P3A_RG_PLL_DELTA1_PE2H_VAL(0x3c);
663 writel(tmp, u3_banks->phya + U3P_U3_PHYA_DA_REG20);
664
665 tmp = readl(u3_banks->phya + U3P_U3_PHYA_DA_REG25);
666 tmp &= ~P3A_RG_PLL_DELTA_PE2H;
667 tmp |= P3A_RG_PLL_DELTA_PE2H_VAL(0x36);
668 writel(tmp, u3_banks->phya + U3P_U3_PHYA_DA_REG25);
669
670 /* change pll BW 0.6M */
671 tmp = readl(u3_banks->phya + U3P_U3_PHYA_DA_REG5);
672 tmp &= ~(P3A_RG_PLL_BR_PE2H | P3A_RG_PLL_IC_PE2H);
673 tmp |= P3A_RG_PLL_BR_PE2H_VAL(0x1) | P3A_RG_PLL_IC_PE2H_VAL(0x1);
674 writel(tmp, u3_banks->phya + U3P_U3_PHYA_DA_REG5);
675
676 tmp = readl(u3_banks->phya + U3P_U3_PHYA_DA_REG4);
677 tmp &= ~(P3A_RG_PLL_DIVEN_PE2H | P3A_RG_PLL_BC_PE2H);
678 tmp |= P3A_RG_PLL_BC_PE2H_VAL(0x3);
679 writel(tmp, u3_banks->phya + U3P_U3_PHYA_DA_REG4);
680
681 tmp = readl(u3_banks->phya + U3P_U3_PHYA_DA_REG6);
682 tmp &= ~P3A_RG_PLL_IR_PE2H;
683 tmp |= P3A_RG_PLL_IR_PE2H_VAL(0x2);
684 writel(tmp, u3_banks->phya + U3P_U3_PHYA_DA_REG6);
685
686 tmp = readl(u3_banks->phya + U3P_U3_PHYA_DA_REG7);
687 tmp &= ~P3A_RG_PLL_BP_PE2H;
688 tmp |= P3A_RG_PLL_BP_PE2H_VAL(0xa);
689 writel(tmp, u3_banks->phya + U3P_U3_PHYA_DA_REG7);
690
691 /* Tx Detect Rx Timing: 10us -> 5us */
692 tmp = readl(u3_banks->phyd + U3P_U3_PHYD_RXDET1);
693 tmp &= ~P3D_RG_RXDET_STB2_SET;
694 tmp |= P3D_RG_RXDET_STB2_SET_VAL(0x10);
695 writel(tmp, u3_banks->phyd + U3P_U3_PHYD_RXDET1);
696
697 tmp = readl(u3_banks->phyd + U3P_U3_PHYD_RXDET2);
698 tmp &= ~P3D_RG_RXDET_STB2_SET_P3;
699 tmp |= P3D_RG_RXDET_STB2_SET_P3_VAL(0x10);
700 writel(tmp, u3_banks->phyd + U3P_U3_PHYD_RXDET2);
701
702 /* wait for PCIe subsys register to active */
703 usleep_range(2500, 3000);
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800704 dev_dbg(tphy->dev, "%s(%d)\n", __func__, instance->index);
Ryder Lee44a6d6c2017-08-03 18:01:00 +0800705}
706
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800707static void pcie_phy_instance_power_on(struct mtk_tphy *tphy,
708 struct mtk_phy_instance *instance)
Ryder Lee44a6d6c2017-08-03 18:01:00 +0800709{
710 struct u3phy_banks *bank = &instance->u3_banks;
711 u32 tmp;
712
713 tmp = readl(bank->chip + U3P_U3_CHIP_GPIO_CTLD);
Chunfeng Yun40363252018-03-12 13:25:38 +0800714 tmp &= ~(P3C_FORCE_IP_SW_RST | P3C_REG_IP_SW_RST);
Ryder Lee44a6d6c2017-08-03 18:01:00 +0800715 writel(tmp, bank->chip + U3P_U3_CHIP_GPIO_CTLD);
716
717 tmp = readl(bank->chip + U3P_U3_CHIP_GPIO_CTLE);
718 tmp &= ~(P3C_RG_SWRST_U3_PHYD_FORCE_EN | P3C_RG_SWRST_U3_PHYD);
719 writel(tmp, bank->chip + U3P_U3_CHIP_GPIO_CTLE);
720}
721
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800722static void pcie_phy_instance_power_off(struct mtk_tphy *tphy,
723 struct mtk_phy_instance *instance)
Ryder Lee44a6d6c2017-08-03 18:01:00 +0800724
725{
726 struct u3phy_banks *bank = &instance->u3_banks;
727 u32 tmp;
728
729 tmp = readl(bank->chip + U3P_U3_CHIP_GPIO_CTLD);
730 tmp |= P3C_FORCE_IP_SW_RST | P3C_REG_IP_SW_RST;
731 writel(tmp, bank->chip + U3P_U3_CHIP_GPIO_CTLD);
732
733 tmp = readl(bank->chip + U3P_U3_CHIP_GPIO_CTLE);
734 tmp |= P3C_RG_SWRST_U3_PHYD_FORCE_EN | P3C_RG_SWRST_U3_PHYD;
735 writel(tmp, bank->chip + U3P_U3_CHIP_GPIO_CTLE);
736}
737
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800738static void sata_phy_instance_init(struct mtk_tphy *tphy,
739 struct mtk_phy_instance *instance)
Ryder Lee4ab26cb2017-08-03 18:01:01 +0800740{
741 struct u3phy_banks *u3_banks = &instance->u3_banks;
742 void __iomem *phyd = u3_banks->phyd;
743 u32 tmp;
744
745 /* charge current adjustment */
746 tmp = readl(phyd + ANA_RG_CTRL_SIGNAL6);
747 tmp &= ~(RG_CDR_BIRLTR_GEN1_MSK | RG_CDR_BC_GEN1_MSK);
748 tmp |= RG_CDR_BIRLTR_GEN1_VAL(0x6) | RG_CDR_BC_GEN1_VAL(0x1a);
749 writel(tmp, phyd + ANA_RG_CTRL_SIGNAL6);
750
751 tmp = readl(phyd + ANA_EQ_EYE_CTRL_SIGNAL4);
752 tmp &= ~RG_CDR_BIRLTD0_GEN1_MSK;
753 tmp |= RG_CDR_BIRLTD0_GEN1_VAL(0x18);
754 writel(tmp, phyd + ANA_EQ_EYE_CTRL_SIGNAL4);
755
756 tmp = readl(phyd + ANA_EQ_EYE_CTRL_SIGNAL5);
757 tmp &= ~RG_CDR_BIRLTD0_GEN3_MSK;
758 tmp |= RG_CDR_BIRLTD0_GEN3_VAL(0x06);
759 writel(tmp, phyd + ANA_EQ_EYE_CTRL_SIGNAL5);
760
761 tmp = readl(phyd + ANA_RG_CTRL_SIGNAL4);
762 tmp &= ~(RG_CDR_BICLTR_GEN1_MSK | RG_CDR_BR_GEN2_MSK);
763 tmp |= RG_CDR_BICLTR_GEN1_VAL(0x0c) | RG_CDR_BR_GEN2_VAL(0x07);
764 writel(tmp, phyd + ANA_RG_CTRL_SIGNAL4);
765
766 tmp = readl(phyd + PHYD_CTRL_SIGNAL_MODE4);
767 tmp &= ~(RG_CDR_BICLTD0_GEN1_MSK | RG_CDR_BICLTD1_GEN1_MSK);
768 tmp |= RG_CDR_BICLTD0_GEN1_VAL(0x08) | RG_CDR_BICLTD1_GEN1_VAL(0x02);
769 writel(tmp, phyd + PHYD_CTRL_SIGNAL_MODE4);
770
771 tmp = readl(phyd + PHYD_DESIGN_OPTION2);
772 tmp &= ~RG_LOCK_CNT_SEL_MSK;
773 tmp |= RG_LOCK_CNT_SEL_VAL(0x02);
774 writel(tmp, phyd + PHYD_DESIGN_OPTION2);
775
776 tmp = readl(phyd + PHYD_DESIGN_OPTION9);
777 tmp &= ~(RG_T2_MIN_MSK | RG_TG_MIN_MSK |
778 RG_T2_MAX_MSK | RG_TG_MAX_MSK);
779 tmp |= RG_T2_MIN_VAL(0x12) | RG_TG_MIN_VAL(0x04) |
780 RG_T2_MAX_VAL(0x31) | RG_TG_MAX_VAL(0x0e);
781 writel(tmp, phyd + PHYD_DESIGN_OPTION9);
782
783 tmp = readl(phyd + ANA_RG_CTRL_SIGNAL1);
784 tmp &= ~RG_IDRV_0DB_GEN1_MSK;
785 tmp |= RG_IDRV_0DB_GEN1_VAL(0x20);
786 writel(tmp, phyd + ANA_RG_CTRL_SIGNAL1);
787
788 tmp = readl(phyd + ANA_EQ_EYE_CTRL_SIGNAL1);
789 tmp &= ~RG_EQ_DLEQ_LFI_GEN1_MSK;
790 tmp |= RG_EQ_DLEQ_LFI_GEN1_VAL(0x03);
791 writel(tmp, phyd + ANA_EQ_EYE_CTRL_SIGNAL1);
792
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800793 dev_dbg(tphy->dev, "%s(%d)\n", __func__, instance->index);
Ryder Lee4ab26cb2017-08-03 18:01:01 +0800794}
795
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800796static void phy_v1_banks_init(struct mtk_tphy *tphy,
797 struct mtk_phy_instance *instance)
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800798{
799 struct u2phy_banks *u2_banks = &instance->u2_banks;
800 struct u3phy_banks *u3_banks = &instance->u3_banks;
801
Ryder Lee44a6d6c2017-08-03 18:01:00 +0800802 switch (instance->type) {
803 case PHY_TYPE_USB2:
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800804 u2_banks->misc = NULL;
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800805 u2_banks->fmreg = tphy->sif_base + SSUSB_SIFSLV_V1_U2FREQ;
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800806 u2_banks->com = instance->port_base + SSUSB_SIFSLV_V1_U2PHY_COM;
Ryder Lee44a6d6c2017-08-03 18:01:00 +0800807 break;
808 case PHY_TYPE_USB3:
809 case PHY_TYPE_PCIE:
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800810 u3_banks->spllc = tphy->sif_base + SSUSB_SIFSLV_V1_SPLLC;
Chunfeng Yun554a56f2017-09-21 18:31:48 +0800811 u3_banks->chip = tphy->sif_base + SSUSB_SIFSLV_V1_CHIP;
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800812 u3_banks->phyd = instance->port_base + SSUSB_SIFSLV_V1_U3PHYD;
813 u3_banks->phya = instance->port_base + SSUSB_SIFSLV_V1_U3PHYA;
Ryder Lee44a6d6c2017-08-03 18:01:00 +0800814 break;
Ryder Lee4ab26cb2017-08-03 18:01:01 +0800815 case PHY_TYPE_SATA:
816 u3_banks->phyd = instance->port_base + SSUSB_SIFSLV_V1_U3PHYD;
817 break;
Ryder Lee44a6d6c2017-08-03 18:01:00 +0800818 default:
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800819 dev_err(tphy->dev, "incompatible PHY type\n");
Ryder Lee44a6d6c2017-08-03 18:01:00 +0800820 return;
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800821 }
822}
823
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800824static void phy_v2_banks_init(struct mtk_tphy *tphy,
825 struct mtk_phy_instance *instance)
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800826{
827 struct u2phy_banks *u2_banks = &instance->u2_banks;
828 struct u3phy_banks *u3_banks = &instance->u3_banks;
829
Ryder Lee44a6d6c2017-08-03 18:01:00 +0800830 switch (instance->type) {
831 case PHY_TYPE_USB2:
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800832 u2_banks->misc = instance->port_base + SSUSB_SIFSLV_V2_MISC;
833 u2_banks->fmreg = instance->port_base + SSUSB_SIFSLV_V2_U2FREQ;
834 u2_banks->com = instance->port_base + SSUSB_SIFSLV_V2_U2PHY_COM;
Ryder Lee44a6d6c2017-08-03 18:01:00 +0800835 break;
836 case PHY_TYPE_USB3:
837 case PHY_TYPE_PCIE:
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800838 u3_banks->spllc = instance->port_base + SSUSB_SIFSLV_V2_SPLLC;
839 u3_banks->chip = instance->port_base + SSUSB_SIFSLV_V2_CHIP;
840 u3_banks->phyd = instance->port_base + SSUSB_SIFSLV_V2_U3PHYD;
841 u3_banks->phya = instance->port_base + SSUSB_SIFSLV_V2_U3PHYA;
Ryder Lee44a6d6c2017-08-03 18:01:00 +0800842 break;
843 default:
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800844 dev_err(tphy->dev, "incompatible PHY type\n");
Ryder Lee44a6d6c2017-08-03 18:01:00 +0800845 return;
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800846 }
847}
848
Chunfeng Yun8158e912018-06-29 10:20:29 +0800849static void phy_parse_property(struct mtk_tphy *tphy,
850 struct mtk_phy_instance *instance)
851{
852 struct device *dev = &instance->phy->dev;
853
854 if (instance->type != PHY_TYPE_USB2)
855 return;
856
Chunfeng Yund4f97f12018-06-29 10:20:30 +0800857 instance->bc12_en = device_property_read_bool(dev, "mediatek,bc12");
Chunfeng Yun8158e912018-06-29 10:20:29 +0800858 device_property_read_u32(dev, "mediatek,eye-src",
859 &instance->eye_src);
860 device_property_read_u32(dev, "mediatek,eye-vrt",
861 &instance->eye_vrt);
862 device_property_read_u32(dev, "mediatek,eye-term",
863 &instance->eye_term);
Chunfeng Yun410572e2020-02-11 11:21:12 +0800864 device_property_read_u32(dev, "mediatek,intr",
865 &instance->intr);
Chunfeng Yun8be5a672020-01-08 09:52:01 +0800866 device_property_read_u32(dev, "mediatek,discth",
867 &instance->discth);
Chunfeng Yun410572e2020-02-11 11:21:12 +0800868 dev_dbg(dev, "bc12:%d, src:%d, vrt:%d, term:%d, intr:%d, disc:%d\n",
Chunfeng Yund4f97f12018-06-29 10:20:30 +0800869 instance->bc12_en, instance->eye_src,
Chunfeng Yun8be5a672020-01-08 09:52:01 +0800870 instance->eye_vrt, instance->eye_term,
Chunfeng Yun410572e2020-02-11 11:21:12 +0800871 instance->intr, instance->discth);
Chunfeng Yun8158e912018-06-29 10:20:29 +0800872}
873
874static void u2_phy_props_set(struct mtk_tphy *tphy,
875 struct mtk_phy_instance *instance)
876{
877 struct u2phy_banks *u2_banks = &instance->u2_banks;
878 void __iomem *com = u2_banks->com;
879 u32 tmp;
880
Chunfeng Yund4f97f12018-06-29 10:20:30 +0800881 if (instance->bc12_en) {
882 tmp = readl(com + U3P_U2PHYBC12C);
883 tmp |= P2C_RG_CHGDT_EN; /* BC1.2 path Enable */
884 writel(tmp, com + U3P_U2PHYBC12C);
885 }
Chunfeng Yun8158e912018-06-29 10:20:29 +0800886
Chunfeng Yun27974e62021-07-23 16:22:41 +0800887 if (tphy->pdata->version < MTK_PHY_V3 && instance->eye_src) {
Chunfeng Yun8158e912018-06-29 10:20:29 +0800888 tmp = readl(com + U3P_USBPHYACR5);
889 tmp &= ~PA5_RG_U2_HSTX_SRCTRL;
890 tmp |= PA5_RG_U2_HSTX_SRCTRL_VAL(instance->eye_src);
891 writel(tmp, com + U3P_USBPHYACR5);
892 }
893
894 if (instance->eye_vrt) {
895 tmp = readl(com + U3P_USBPHYACR1);
896 tmp &= ~PA1_RG_VRT_SEL;
897 tmp |= PA1_RG_VRT_SEL_VAL(instance->eye_vrt);
898 writel(tmp, com + U3P_USBPHYACR1);
899 }
900
901 if (instance->eye_term) {
902 tmp = readl(com + U3P_USBPHYACR1);
903 tmp &= ~PA1_RG_TERM_SEL;
904 tmp |= PA1_RG_TERM_SEL_VAL(instance->eye_term);
905 writel(tmp, com + U3P_USBPHYACR1);
906 }
Chunfeng Yun8be5a672020-01-08 09:52:01 +0800907
Chunfeng Yun410572e2020-02-11 11:21:12 +0800908 if (instance->intr) {
909 tmp = readl(com + U3P_USBPHYACR1);
910 tmp &= ~PA1_RG_INTR_CAL;
911 tmp |= PA1_RG_INTR_CAL_VAL(instance->intr);
912 writel(tmp, com + U3P_USBPHYACR1);
913 }
914
Chunfeng Yun8be5a672020-01-08 09:52:01 +0800915 if (instance->discth) {
916 tmp = readl(com + U3P_USBPHYACR6);
917 tmp &= ~PA6_RG_U2_DISCTH;
918 tmp |= PA6_RG_U2_DISCTH_VAL(instance->discth);
919 writel(tmp, com + U3P_USBPHYACR6);
920 }
Chunfeng Yun8158e912018-06-29 10:20:29 +0800921}
922
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800923static int mtk_phy_init(struct phy *phy)
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800924{
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800925 struct mtk_phy_instance *instance = phy_get_drvdata(phy);
926 struct mtk_tphy *tphy = dev_get_drvdata(phy->dev.parent);
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800927 int ret;
928
Chunfeng Yun15de15c2017-03-31 15:35:30 +0800929 ret = clk_prepare_enable(instance->ref_clk);
930 if (ret) {
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800931 dev_err(tphy->dev, "failed to enable ref_clk\n");
Chunfeng Yun15de15c2017-03-31 15:35:30 +0800932 return ret;
933 }
934
Chunfeng Yun12d0c0b2020-02-11 11:21:15 +0800935 ret = clk_prepare_enable(instance->da_ref_clk);
936 if (ret) {
937 dev_err(tphy->dev, "failed to enable da_ref\n");
938 clk_disable_unprepare(instance->ref_clk);
939 return ret;
940 }
941
Ryder Lee44a6d6c2017-08-03 18:01:00 +0800942 switch (instance->type) {
943 case PHY_TYPE_USB2:
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800944 u2_phy_instance_init(tphy, instance);
Chunfeng Yun8158e912018-06-29 10:20:29 +0800945 u2_phy_props_set(tphy, instance);
Ryder Lee44a6d6c2017-08-03 18:01:00 +0800946 break;
947 case PHY_TYPE_USB3:
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800948 u3_phy_instance_init(tphy, instance);
Ryder Lee44a6d6c2017-08-03 18:01:00 +0800949 break;
950 case PHY_TYPE_PCIE:
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800951 pcie_phy_instance_init(tphy, instance);
Ryder Lee44a6d6c2017-08-03 18:01:00 +0800952 break;
Ryder Lee4ab26cb2017-08-03 18:01:01 +0800953 case PHY_TYPE_SATA:
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800954 sata_phy_instance_init(tphy, instance);
Ryder Lee4ab26cb2017-08-03 18:01:01 +0800955 break;
Ryder Lee44a6d6c2017-08-03 18:01:00 +0800956 default:
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800957 dev_err(tphy->dev, "incompatible PHY type\n");
Tiezhu Yangaaac9a12021-05-19 18:37:39 +0800958 clk_disable_unprepare(instance->ref_clk);
959 clk_disable_unprepare(instance->da_ref_clk);
Ryder Lee44a6d6c2017-08-03 18:01:00 +0800960 return -EINVAL;
961 }
Chunfeng Yun04466ef2017-03-31 15:35:29 +0800962
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800963 return 0;
964}
965
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800966static int mtk_phy_power_on(struct phy *phy)
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800967{
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800968 struct mtk_phy_instance *instance = phy_get_drvdata(phy);
969 struct mtk_tphy *tphy = dev_get_drvdata(phy->dev.parent);
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800970
Chunfeng Yun04466ef2017-03-31 15:35:29 +0800971 if (instance->type == PHY_TYPE_USB2) {
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800972 u2_phy_instance_power_on(tphy, instance);
973 hs_slew_rate_calibrate(tphy, instance);
Ryder Lee44a6d6c2017-08-03 18:01:00 +0800974 } else if (instance->type == PHY_TYPE_PCIE) {
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800975 pcie_phy_instance_power_on(tphy, instance);
Chunfeng Yun04466ef2017-03-31 15:35:29 +0800976 }
Ryder Lee44a6d6c2017-08-03 18:01:00 +0800977
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800978 return 0;
979}
980
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800981static int mtk_phy_power_off(struct phy *phy)
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800982{
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800983 struct mtk_phy_instance *instance = phy_get_drvdata(phy);
984 struct mtk_tphy *tphy = dev_get_drvdata(phy->dev.parent);
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800985
Chunfeng Yun04466ef2017-03-31 15:35:29 +0800986 if (instance->type == PHY_TYPE_USB2)
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800987 u2_phy_instance_power_off(tphy, instance);
Ryder Lee44a6d6c2017-08-03 18:01:00 +0800988 else if (instance->type == PHY_TYPE_PCIE)
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800989 pcie_phy_instance_power_off(tphy, instance);
Chunfeng Yun04466ef2017-03-31 15:35:29 +0800990
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800991 return 0;
992}
993
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800994static int mtk_phy_exit(struct phy *phy)
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800995{
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800996 struct mtk_phy_instance *instance = phy_get_drvdata(phy);
997 struct mtk_tphy *tphy = dev_get_drvdata(phy->dev.parent);
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800998
Chunfeng Yun04466ef2017-03-31 15:35:29 +0800999 if (instance->type == PHY_TYPE_USB2)
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +08001000 u2_phy_instance_exit(tphy, instance);
Chunfeng Yun04466ef2017-03-31 15:35:29 +08001001
Chunfeng Yun15de15c2017-03-31 15:35:30 +08001002 clk_disable_unprepare(instance->ref_clk);
Chunfeng Yun12d0c0b2020-02-11 11:21:15 +08001003 clk_disable_unprepare(instance->da_ref_clk);
Chunfeng Yundc7f1902015-09-29 11:01:36 +08001004 return 0;
1005}
1006
Grygorii Strashko79a5a182018-11-19 19:24:20 -06001007static int mtk_phy_set_mode(struct phy *phy, enum phy_mode mode, int submode)
Chunfeng Yun5954a102017-09-21 18:31:49 +08001008{
1009 struct mtk_phy_instance *instance = phy_get_drvdata(phy);
1010 struct mtk_tphy *tphy = dev_get_drvdata(phy->dev.parent);
1011
1012 if (instance->type == PHY_TYPE_USB2)
1013 u2_phy_instance_set_mode(tphy, instance, mode);
1014
1015 return 0;
1016}
1017
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +08001018static struct phy *mtk_phy_xlate(struct device *dev,
Chunfeng Yundc7f1902015-09-29 11:01:36 +08001019 struct of_phandle_args *args)
1020{
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +08001021 struct mtk_tphy *tphy = dev_get_drvdata(dev);
1022 struct mtk_phy_instance *instance = NULL;
Chunfeng Yundc7f1902015-09-29 11:01:36 +08001023 struct device_node *phy_np = args->np;
1024 int index;
1025
Chunfeng Yundc7f1902015-09-29 11:01:36 +08001026 if (args->args_count != 1) {
1027 dev_err(dev, "invalid number of cells in 'phy' property\n");
1028 return ERR_PTR(-EINVAL);
1029 }
1030
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +08001031 for (index = 0; index < tphy->nphys; index++)
1032 if (phy_np == tphy->phys[index]->phy->dev.of_node) {
1033 instance = tphy->phys[index];
Chunfeng Yundc7f1902015-09-29 11:01:36 +08001034 break;
1035 }
1036
1037 if (!instance) {
1038 dev_err(dev, "failed to find appropriate phy\n");
1039 return ERR_PTR(-EINVAL);
1040 }
1041
1042 instance->type = args->args[0];
Chunfeng Yundc7f1902015-09-29 11:01:36 +08001043 if (!(instance->type == PHY_TYPE_USB2 ||
Ryder Lee44a6d6c2017-08-03 18:01:00 +08001044 instance->type == PHY_TYPE_USB3 ||
Ryder Lee4ab26cb2017-08-03 18:01:01 +08001045 instance->type == PHY_TYPE_PCIE ||
1046 instance->type == PHY_TYPE_SATA)) {
Chunfeng Yundc7f1902015-09-29 11:01:36 +08001047 dev_err(dev, "unsupported device type: %d\n", instance->type);
1048 return ERR_PTR(-EINVAL);
1049 }
1050
Chunfeng Yun27974e62021-07-23 16:22:41 +08001051 switch (tphy->pdata->version) {
1052 case MTK_PHY_V1:
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +08001053 phy_v1_banks_init(tphy, instance);
Chunfeng Yun27974e62021-07-23 16:22:41 +08001054 break;
1055 case MTK_PHY_V2:
1056 case MTK_PHY_V3:
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +08001057 phy_v2_banks_init(tphy, instance);
Chunfeng Yun27974e62021-07-23 16:22:41 +08001058 break;
1059 default:
Chunfeng Yun8d6e19572017-03-31 15:35:31 +08001060 dev_err(dev, "phy version is not supported\n");
1061 return ERR_PTR(-EINVAL);
1062 }
1063
Chunfeng Yun8158e912018-06-29 10:20:29 +08001064 phy_parse_property(tphy, instance);
1065
Chunfeng Yundc7f1902015-09-29 11:01:36 +08001066 return instance->phy;
1067}
1068
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +08001069static const struct phy_ops mtk_tphy_ops = {
1070 .init = mtk_phy_init,
1071 .exit = mtk_phy_exit,
1072 .power_on = mtk_phy_power_on,
1073 .power_off = mtk_phy_power_off,
Chunfeng Yun5954a102017-09-21 18:31:49 +08001074 .set_mode = mtk_phy_set_mode,
Chunfeng Yundc7f1902015-09-29 11:01:36 +08001075 .owner = THIS_MODULE,
1076};
1077
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +08001078static const struct mtk_phy_pdata tphy_v1_pdata = {
Chunfeng Yune1d76532016-04-20 08:14:02 +08001079 .avoid_rx_sen_degradation = false,
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +08001080 .version = MTK_PHY_V1,
Chunfeng Yun8d6e19572017-03-31 15:35:31 +08001081};
1082
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +08001083static const struct mtk_phy_pdata tphy_v2_pdata = {
Chunfeng Yun8d6e19572017-03-31 15:35:31 +08001084 .avoid_rx_sen_degradation = false,
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +08001085 .version = MTK_PHY_V2,
Chunfeng Yune1d76532016-04-20 08:14:02 +08001086};
1087
Chunfeng Yun27974e62021-07-23 16:22:41 +08001088static const struct mtk_phy_pdata tphy_v3_pdata = {
1089 .version = MTK_PHY_V3,
1090};
1091
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +08001092static const struct mtk_phy_pdata mt8173_pdata = {
Chunfeng Yune1d76532016-04-20 08:14:02 +08001093 .avoid_rx_sen_degradation = true,
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +08001094 .version = MTK_PHY_V1,
Chunfeng Yune1d76532016-04-20 08:14:02 +08001095};
1096
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +08001097static const struct of_device_id mtk_tphy_id_table[] = {
Ryder Lee44a6d6c2017-08-03 18:01:00 +08001098 { .compatible = "mediatek,mt2701-u3phy", .data = &tphy_v1_pdata },
Ryder Lee4ab26cb2017-08-03 18:01:01 +08001099 { .compatible = "mediatek,mt2712-u3phy", .data = &tphy_v2_pdata },
Chunfeng Yune1d76532016-04-20 08:14:02 +08001100 { .compatible = "mediatek,mt8173-u3phy", .data = &mt8173_pdata },
Ryder Lee44a6d6c2017-08-03 18:01:00 +08001101 { .compatible = "mediatek,generic-tphy-v1", .data = &tphy_v1_pdata },
Ryder Lee4ab26cb2017-08-03 18:01:01 +08001102 { .compatible = "mediatek,generic-tphy-v2", .data = &tphy_v2_pdata },
Chunfeng Yun27974e62021-07-23 16:22:41 +08001103 { .compatible = "mediatek,generic-tphy-v3", .data = &tphy_v3_pdata },
Chunfeng Yune1d76532016-04-20 08:14:02 +08001104 { },
1105};
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +08001106MODULE_DEVICE_TABLE(of, mtk_tphy_id_table);
Chunfeng Yune1d76532016-04-20 08:14:02 +08001107
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +08001108static int mtk_tphy_probe(struct platform_device *pdev)
Chunfeng Yundc7f1902015-09-29 11:01:36 +08001109{
1110 struct device *dev = &pdev->dev;
1111 struct device_node *np = dev->of_node;
1112 struct device_node *child_np;
1113 struct phy_provider *provider;
1114 struct resource *sif_res;
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +08001115 struct mtk_tphy *tphy;
Chunfeng Yundc7f1902015-09-29 11:01:36 +08001116 struct resource res;
Julia Lawall2bb80cc2015-11-16 12:33:15 +01001117 int port, retval;
Chunfeng Yundc7f1902015-09-29 11:01:36 +08001118
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +08001119 tphy = devm_kzalloc(dev, sizeof(*tphy), GFP_KERNEL);
1120 if (!tphy)
Chunfeng Yundc7f1902015-09-29 11:01:36 +08001121 return -ENOMEM;
1122
Chunfeng Yune4b227c2017-12-28 16:40:36 +05301123 tphy->pdata = of_device_get_match_data(dev);
1124 if (!tphy->pdata)
1125 return -EINVAL;
1126
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +08001127 tphy->nphys = of_get_child_count(np);
1128 tphy->phys = devm_kcalloc(dev, tphy->nphys,
1129 sizeof(*tphy->phys), GFP_KERNEL);
1130 if (!tphy->phys)
Chunfeng Yundc7f1902015-09-29 11:01:36 +08001131 return -ENOMEM;
1132
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +08001133 tphy->dev = dev;
1134 platform_set_drvdata(pdev, tphy);
Chunfeng Yundc7f1902015-09-29 11:01:36 +08001135
Chunfeng Yun93a04f42017-12-07 19:53:35 +08001136 sif_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1137 /* SATA phy of V1 needn't it if not shared with PCIe or USB */
1138 if (sif_res && tphy->pdata->version == MTK_PHY_V1) {
Chunfeng Yun8d6e19572017-03-31 15:35:31 +08001139 /* get banks shared by multiple phys */
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +08001140 tphy->sif_base = devm_ioremap_resource(dev, sif_res);
1141 if (IS_ERR(tphy->sif_base)) {
Chunfeng Yun8d6e19572017-03-31 15:35:31 +08001142 dev_err(dev, "failed to remap sif regs\n");
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +08001143 return PTR_ERR(tphy->sif_base);
Chunfeng Yun8d6e19572017-03-31 15:35:31 +08001144 }
Chunfeng Yundc7f1902015-09-29 11:01:36 +08001145 }
1146
Chunfeng Yun27974e62021-07-23 16:22:41 +08001147 if (tphy->pdata->version < MTK_PHY_V3) {
1148 tphy->src_ref_clk = U3P_REF_CLK;
1149 tphy->src_coef = U3P_SLEW_RATE_COEF;
1150 /* update parameters of slew rate calibrate if exist */
1151 device_property_read_u32(dev, "mediatek,src-ref-clk-mhz",
1152 &tphy->src_ref_clk);
1153 device_property_read_u32(dev, "mediatek,src-coef",
1154 &tphy->src_coef);
1155 }
Chunfeng Yun8833ebf42018-03-12 13:25:39 +08001156
Chunfeng Yundc7f1902015-09-29 11:01:36 +08001157 port = 0;
1158 for_each_child_of_node(np, child_np) {
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +08001159 struct mtk_phy_instance *instance;
Chunfeng Yundc7f1902015-09-29 11:01:36 +08001160 struct phy *phy;
Chunfeng Yundc7f1902015-09-29 11:01:36 +08001161
1162 instance = devm_kzalloc(dev, sizeof(*instance), GFP_KERNEL);
Julia Lawall2bb80cc2015-11-16 12:33:15 +01001163 if (!instance) {
1164 retval = -ENOMEM;
1165 goto put_child;
1166 }
Chunfeng Yundc7f1902015-09-29 11:01:36 +08001167
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +08001168 tphy->phys[port] = instance;
Chunfeng Yundc7f1902015-09-29 11:01:36 +08001169
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +08001170 phy = devm_phy_create(dev, child_np, &mtk_tphy_ops);
Chunfeng Yundc7f1902015-09-29 11:01:36 +08001171 if (IS_ERR(phy)) {
1172 dev_err(dev, "failed to create phy\n");
Julia Lawall2bb80cc2015-11-16 12:33:15 +01001173 retval = PTR_ERR(phy);
1174 goto put_child;
Chunfeng Yundc7f1902015-09-29 11:01:36 +08001175 }
1176
1177 retval = of_address_to_resource(child_np, 0, &res);
1178 if (retval) {
1179 dev_err(dev, "failed to get address resource(id-%d)\n",
1180 port);
Julia Lawall2bb80cc2015-11-16 12:33:15 +01001181 goto put_child;
Chunfeng Yundc7f1902015-09-29 11:01:36 +08001182 }
1183
1184 instance->port_base = devm_ioremap_resource(&phy->dev, &res);
1185 if (IS_ERR(instance->port_base)) {
1186 dev_err(dev, "failed to remap phy regs\n");
Julia Lawall2bb80cc2015-11-16 12:33:15 +01001187 retval = PTR_ERR(instance->port_base);
1188 goto put_child;
Chunfeng Yundc7f1902015-09-29 11:01:36 +08001189 }
1190
1191 instance->phy = phy;
1192 instance->index = port;
1193 phy_set_drvdata(phy, instance);
1194 port++;
Chunfeng Yun15de15c2017-03-31 15:35:30 +08001195
Chunfeng Yun657a9ed2020-02-11 11:21:13 +08001196 instance->ref_clk = devm_clk_get_optional(&phy->dev, "ref");
Chunfeng Yun15de15c2017-03-31 15:35:30 +08001197 if (IS_ERR(instance->ref_clk)) {
1198 dev_err(dev, "failed to get ref_clk(id-%d)\n", port);
1199 retval = PTR_ERR(instance->ref_clk);
1200 goto put_child;
1201 }
Chunfeng Yun12d0c0b2020-02-11 11:21:15 +08001202
1203 instance->da_ref_clk =
1204 devm_clk_get_optional(&phy->dev, "da_ref");
1205 if (IS_ERR(instance->da_ref_clk)) {
1206 dev_err(dev, "failed to get da_ref_clk(id-%d)\n", port);
1207 retval = PTR_ERR(instance->da_ref_clk);
1208 goto put_child;
1209 }
Chunfeng Yundc7f1902015-09-29 11:01:36 +08001210 }
1211
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +08001212 provider = devm_of_phy_provider_register(dev, mtk_phy_xlate);
Chunfeng Yundc7f1902015-09-29 11:01:36 +08001213
1214 return PTR_ERR_OR_ZERO(provider);
Julia Lawall2bb80cc2015-11-16 12:33:15 +01001215put_child:
1216 of_node_put(child_np);
1217 return retval;
Chunfeng Yundc7f1902015-09-29 11:01:36 +08001218}
1219
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +08001220static struct platform_driver mtk_tphy_driver = {
1221 .probe = mtk_tphy_probe,
Chunfeng Yundc7f1902015-09-29 11:01:36 +08001222 .driver = {
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +08001223 .name = "mtk-tphy",
1224 .of_match_table = mtk_tphy_id_table,
Chunfeng Yundc7f1902015-09-29 11:01:36 +08001225 },
1226};
1227
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +08001228module_platform_driver(mtk_tphy_driver);
Chunfeng Yundc7f1902015-09-29 11:01:36 +08001229
1230MODULE_AUTHOR("Chunfeng Yun <chunfeng.yun@mediatek.com>");
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +08001231MODULE_DESCRIPTION("MediaTek T-PHY driver");
Chunfeng Yundc7f1902015-09-29 11:01:36 +08001232MODULE_LICENSE("GPL v2");