blob: cdbcc49f71152cf479ab1a35eabeb8b663469f10 [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 Yun8d6e19572017-03-31 15:35:31 +080030/* version V2 sub-banks offset base address */
31/* u2 phy banks */
32#define SSUSB_SIFSLV_V2_MISC 0x000
33#define SSUSB_SIFSLV_V2_U2FREQ 0x100
34#define SSUSB_SIFSLV_V2_U2PHY_COM 0x300
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +080035/* u3/pcie/sata phy banks */
Chunfeng Yun8d6e19572017-03-31 15:35:31 +080036#define SSUSB_SIFSLV_V2_SPLLC 0x000
37#define SSUSB_SIFSLV_V2_CHIP 0x100
38#define SSUSB_SIFSLV_V2_U3PHYD 0x200
39#define SSUSB_SIFSLV_V2_U3PHYA 0x400
Chunfeng Yundc7f1902015-09-29 11:01:36 +080040
Chunfeng Yun8d6e19572017-03-31 15:35:31 +080041#define U3P_USBPHYACR0 0x000
Chunfeng Yundc7f1902015-09-29 11:01:36 +080042#define PA0_RG_U2PLL_FORCE_ON BIT(15)
Chunfeng Yunc0250fe2017-03-31 15:35:32 +080043#define PA0_RG_USB20_INTR_EN BIT(5)
Chunfeng Yundc7f1902015-09-29 11:01:36 +080044
Chunfeng Yun8158e912018-06-29 10:20:29 +080045#define U3P_USBPHYACR1 0x004
Chunfeng Yun410572e2020-02-11 11:21:12 +080046#define PA1_RG_INTR_CAL GENMASK(23, 19)
47#define PA1_RG_INTR_CAL_VAL(x) ((0x1f & (x)) << 19)
Chunfeng Yun8158e912018-06-29 10:20:29 +080048#define PA1_RG_VRT_SEL GENMASK(14, 12)
49#define PA1_RG_VRT_SEL_VAL(x) ((0x7 & (x)) << 12)
50#define PA1_RG_TERM_SEL GENMASK(10, 8)
51#define PA1_RG_TERM_SEL_VAL(x) ((0x7 & (x)) << 8)
52
Chunfeng Yun8d6e19572017-03-31 15:35:31 +080053#define U3P_USBPHYACR2 0x008
Chunfeng Yundc7f1902015-09-29 11:01:36 +080054#define PA2_RG_SIF_U2PLL_FORCE_EN BIT(18)
55
Chunfeng Yun8d6e19572017-03-31 15:35:31 +080056#define U3P_USBPHYACR5 0x014
Chunfeng Yun75f072f2015-12-04 10:11:05 +080057#define PA5_RG_U2_HSTX_SRCAL_EN BIT(15)
Chunfeng Yundc7f1902015-09-29 11:01:36 +080058#define PA5_RG_U2_HSTX_SRCTRL GENMASK(14, 12)
59#define PA5_RG_U2_HSTX_SRCTRL_VAL(x) ((0x7 & (x)) << 12)
60#define PA5_RG_U2_HS_100U_U3_EN BIT(11)
61
Chunfeng Yun8d6e19572017-03-31 15:35:31 +080062#define U3P_USBPHYACR6 0x018
Chunfeng Yundc7f1902015-09-29 11:01:36 +080063#define PA6_RG_U2_BC11_SW_EN BIT(23)
64#define PA6_RG_U2_OTG_VBUSCMP_EN BIT(20)
Chunfeng Yun8be5a672020-01-08 09:52:01 +080065#define PA6_RG_U2_DISCTH GENMASK(7, 4)
66#define PA6_RG_U2_DISCTH_VAL(x) ((0xf & (x)) << 4)
Chunfeng Yun43f53b12015-12-04 10:08:56 +080067#define PA6_RG_U2_SQTH GENMASK(3, 0)
68#define PA6_RG_U2_SQTH_VAL(x) (0xf & (x))
Chunfeng Yundc7f1902015-09-29 11:01:36 +080069
Chunfeng Yun8d6e19572017-03-31 15:35:31 +080070#define U3P_U2PHYACR4 0x020
Chunfeng Yundc7f1902015-09-29 11:01:36 +080071#define P2C_RG_USB20_GPIO_CTL BIT(9)
72#define P2C_USB20_GPIO_MODE BIT(8)
73#define P2C_U2_GPIO_CTR_MSK (P2C_RG_USB20_GPIO_CTL | P2C_USB20_GPIO_MODE)
74
Chunfeng Yun8d6e19572017-03-31 15:35:31 +080075#define U3D_U2PHYDCR0 0x060
Chunfeng Yundc7f1902015-09-29 11:01:36 +080076#define P2C_RG_SIF_U2PLL_FORCE_ON BIT(24)
77
Chunfeng Yun8d6e19572017-03-31 15:35:31 +080078#define U3P_U2PHYDTM0 0x068
Chunfeng Yundc7f1902015-09-29 11:01:36 +080079#define P2C_FORCE_UART_EN BIT(26)
80#define P2C_FORCE_DATAIN BIT(23)
81#define P2C_FORCE_DM_PULLDOWN BIT(21)
82#define P2C_FORCE_DP_PULLDOWN BIT(20)
83#define P2C_FORCE_XCVRSEL BIT(19)
84#define P2C_FORCE_SUSPENDM BIT(18)
85#define P2C_FORCE_TERMSEL BIT(17)
86#define P2C_RG_DATAIN GENMASK(13, 10)
87#define P2C_RG_DATAIN_VAL(x) ((0xf & (x)) << 10)
88#define P2C_RG_DMPULLDOWN BIT(7)
89#define P2C_RG_DPPULLDOWN BIT(6)
90#define P2C_RG_XCVRSEL GENMASK(5, 4)
91#define P2C_RG_XCVRSEL_VAL(x) ((0x3 & (x)) << 4)
92#define P2C_RG_SUSPENDM BIT(3)
93#define P2C_RG_TERMSEL BIT(2)
94#define P2C_DTM0_PART_MASK \
95 (P2C_FORCE_DATAIN | P2C_FORCE_DM_PULLDOWN | \
96 P2C_FORCE_DP_PULLDOWN | P2C_FORCE_XCVRSEL | \
97 P2C_FORCE_TERMSEL | P2C_RG_DMPULLDOWN | \
98 P2C_RG_DPPULLDOWN | P2C_RG_TERMSEL)
99
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800100#define U3P_U2PHYDTM1 0x06C
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800101#define P2C_RG_UART_EN BIT(16)
Chunfeng Yun5954a102017-09-21 18:31:49 +0800102#define P2C_FORCE_IDDIG BIT(9)
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800103#define P2C_RG_VBUSVALID BIT(5)
104#define P2C_RG_SESSEND BIT(4)
105#define P2C_RG_AVALID BIT(2)
Chunfeng Yun5954a102017-09-21 18:31:49 +0800106#define P2C_RG_IDDIG BIT(1)
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800107
Chunfeng Yund4f97f12018-06-29 10:20:30 +0800108#define U3P_U2PHYBC12C 0x080
109#define P2C_RG_CHGDT_EN BIT(0)
110
Ryder Lee44a6d6c2017-08-03 18:01:00 +0800111#define U3P_U3_CHIP_GPIO_CTLD 0x0c
112#define P3C_REG_IP_SW_RST BIT(31)
113#define P3C_MCU_BUS_CK_GATE_EN BIT(30)
114#define P3C_FORCE_IP_SW_RST BIT(29)
115
116#define U3P_U3_CHIP_GPIO_CTLE 0x10
117#define P3C_RG_SWRST_U3_PHYD BIT(25)
118#define P3C_RG_SWRST_U3_PHYD_FORCE_EN BIT(24)
119
120#define U3P_U3_PHYA_REG0 0x000
121#define P3A_RG_CLKDRV_OFF GENMASK(3, 2)
122#define P3A_RG_CLKDRV_OFF_VAL(x) ((0x3 & (x)) << 2)
123
124#define U3P_U3_PHYA_REG1 0x004
125#define P3A_RG_CLKDRV_AMP GENMASK(31, 29)
126#define P3A_RG_CLKDRV_AMP_VAL(x) ((0x7 & (x)) << 29)
127
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800128#define U3P_U3_PHYA_REG6 0x018
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800129#define P3A_RG_TX_EIDLE_CM GENMASK(31, 28)
130#define P3A_RG_TX_EIDLE_CM_VAL(x) ((0xf & (x)) << 28)
131
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800132#define U3P_U3_PHYA_REG9 0x024
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800133#define P3A_RG_RX_DAC_MUX GENMASK(5, 1)
134#define P3A_RG_RX_DAC_MUX_VAL(x) ((0x1f & (x)) << 1)
135
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800136#define U3P_U3_PHYA_DA_REG0 0x100
Ryder Lee44a6d6c2017-08-03 18:01:00 +0800137#define P3A_RG_XTAL_EXT_PE2H GENMASK(17, 16)
138#define P3A_RG_XTAL_EXT_PE2H_VAL(x) ((0x3 & (x)) << 16)
139#define P3A_RG_XTAL_EXT_PE1H GENMASK(13, 12)
140#define P3A_RG_XTAL_EXT_PE1H_VAL(x) ((0x3 & (x)) << 12)
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800141#define P3A_RG_XTAL_EXT_EN_U3 GENMASK(11, 10)
142#define P3A_RG_XTAL_EXT_EN_U3_VAL(x) ((0x3 & (x)) << 10)
143
Ryder Lee44a6d6c2017-08-03 18:01:00 +0800144#define U3P_U3_PHYA_DA_REG4 0x108
145#define P3A_RG_PLL_DIVEN_PE2H GENMASK(21, 19)
146#define P3A_RG_PLL_BC_PE2H GENMASK(7, 6)
147#define P3A_RG_PLL_BC_PE2H_VAL(x) ((0x3 & (x)) << 6)
148
149#define U3P_U3_PHYA_DA_REG5 0x10c
150#define P3A_RG_PLL_BR_PE2H GENMASK(29, 28)
151#define P3A_RG_PLL_BR_PE2H_VAL(x) ((0x3 & (x)) << 28)
152#define P3A_RG_PLL_IC_PE2H GENMASK(15, 12)
153#define P3A_RG_PLL_IC_PE2H_VAL(x) ((0xf & (x)) << 12)
154
155#define U3P_U3_PHYA_DA_REG6 0x110
156#define P3A_RG_PLL_IR_PE2H GENMASK(19, 16)
157#define P3A_RG_PLL_IR_PE2H_VAL(x) ((0xf & (x)) << 16)
158
159#define U3P_U3_PHYA_DA_REG7 0x114
160#define P3A_RG_PLL_BP_PE2H GENMASK(19, 16)
161#define P3A_RG_PLL_BP_PE2H_VAL(x) ((0xf & (x)) << 16)
162
163#define U3P_U3_PHYA_DA_REG20 0x13c
164#define P3A_RG_PLL_DELTA1_PE2H GENMASK(31, 16)
165#define P3A_RG_PLL_DELTA1_PE2H_VAL(x) ((0xffff & (x)) << 16)
166
167#define U3P_U3_PHYA_DA_REG25 0x148
168#define P3A_RG_PLL_DELTA_PE2H GENMASK(15, 0)
169#define P3A_RG_PLL_DELTA_PE2H_VAL(x) (0xffff & (x))
170
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800171#define U3P_U3_PHYD_LFPS1 0x00c
Chunfeng Yun98cd83a2017-03-31 15:35:28 +0800172#define P3D_RG_FWAKE_TH GENMASK(21, 16)
173#define P3D_RG_FWAKE_TH_VAL(x) ((0x3f & (x)) << 16)
174
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800175#define U3P_U3_PHYD_CDR1 0x05c
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800176#define P3D_RG_CDR_BIR_LTD1 GENMASK(28, 24)
177#define P3D_RG_CDR_BIR_LTD1_VAL(x) ((0x1f & (x)) << 24)
178#define P3D_RG_CDR_BIR_LTD0 GENMASK(12, 8)
179#define P3D_RG_CDR_BIR_LTD0_VAL(x) ((0x1f & (x)) << 8)
180
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800181#define U3P_U3_PHYD_RXDET1 0x128
Chunfeng Yun1969f692017-03-31 15:35:27 +0800182#define P3D_RG_RXDET_STB2_SET GENMASK(17, 9)
183#define P3D_RG_RXDET_STB2_SET_VAL(x) ((0x1ff & (x)) << 9)
184
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800185#define U3P_U3_PHYD_RXDET2 0x12c
Chunfeng Yun1969f692017-03-31 15:35:27 +0800186#define P3D_RG_RXDET_STB2_SET_P3 GENMASK(8, 0)
187#define P3D_RG_RXDET_STB2_SET_P3_VAL(x) (0x1ff & (x))
188
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800189#define U3P_SPLLC_XTALCTL3 0x018
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800190#define XC3_RG_U3_XTAL_RX_PWD BIT(9)
191#define XC3_RG_U3_FRC_XTAL_RX_PWD BIT(8)
192
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800193#define U3P_U2FREQ_FMCR0 0x00
Chunfeng Yun75f072f2015-12-04 10:11:05 +0800194#define P2F_RG_MONCLK_SEL GENMASK(27, 26)
195#define P2F_RG_MONCLK_SEL_VAL(x) ((0x3 & (x)) << 26)
196#define P2F_RG_FREQDET_EN BIT(24)
197#define P2F_RG_CYCLECNT GENMASK(23, 0)
198#define P2F_RG_CYCLECNT_VAL(x) ((P2F_RG_CYCLECNT) & (x))
199
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800200#define U3P_U2FREQ_VALUE 0x0c
Chunfeng Yun75f072f2015-12-04 10:11:05 +0800201
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800202#define U3P_U2FREQ_FMMONR1 0x10
Chunfeng Yun75f072f2015-12-04 10:11:05 +0800203#define P2F_USB_FM_VALID BIT(0)
204#define P2F_RG_FRCK_EN BIT(8)
205
206#define U3P_REF_CLK 26 /* MHZ */
207#define U3P_SLEW_RATE_COEF 28
208#define U3P_SR_COEF_DIVISOR 1000
209#define U3P_FM_DET_CYCLE_CNT 1024
210
Ryder Lee4ab26cb2017-08-03 18:01:01 +0800211/* SATA register setting */
212#define PHYD_CTRL_SIGNAL_MODE4 0x1c
213/* CDR Charge Pump P-path current adjustment */
214#define RG_CDR_BICLTD1_GEN1_MSK GENMASK(23, 20)
215#define RG_CDR_BICLTD1_GEN1_VAL(x) ((0xf & (x)) << 20)
216#define RG_CDR_BICLTD0_GEN1_MSK GENMASK(11, 8)
217#define RG_CDR_BICLTD0_GEN1_VAL(x) ((0xf & (x)) << 8)
218
219#define PHYD_DESIGN_OPTION2 0x24
220/* Symbol lock count selection */
221#define RG_LOCK_CNT_SEL_MSK GENMASK(5, 4)
222#define RG_LOCK_CNT_SEL_VAL(x) ((0x3 & (x)) << 4)
223
224#define PHYD_DESIGN_OPTION9 0x40
225/* COMWAK GAP width window */
226#define RG_TG_MAX_MSK GENMASK(20, 16)
227#define RG_TG_MAX_VAL(x) ((0x1f & (x)) << 16)
228/* COMINIT GAP width window */
229#define RG_T2_MAX_MSK GENMASK(13, 8)
230#define RG_T2_MAX_VAL(x) ((0x3f & (x)) << 8)
231/* COMWAK GAP width window */
232#define RG_TG_MIN_MSK GENMASK(7, 5)
233#define RG_TG_MIN_VAL(x) ((0x7 & (x)) << 5)
234/* COMINIT GAP width window */
235#define RG_T2_MIN_MSK GENMASK(4, 0)
236#define RG_T2_MIN_VAL(x) (0x1f & (x))
237
238#define ANA_RG_CTRL_SIGNAL1 0x4c
239/* TX driver tail current control for 0dB de-empahsis mdoe for Gen1 speed */
240#define RG_IDRV_0DB_GEN1_MSK GENMASK(13, 8)
241#define RG_IDRV_0DB_GEN1_VAL(x) ((0x3f & (x)) << 8)
242
243#define ANA_RG_CTRL_SIGNAL4 0x58
244#define RG_CDR_BICLTR_GEN1_MSK GENMASK(23, 20)
245#define RG_CDR_BICLTR_GEN1_VAL(x) ((0xf & (x)) << 20)
246/* Loop filter R1 resistance adjustment for Gen1 speed */
247#define RG_CDR_BR_GEN2_MSK GENMASK(10, 8)
248#define RG_CDR_BR_GEN2_VAL(x) ((0x7 & (x)) << 8)
249
250#define ANA_RG_CTRL_SIGNAL6 0x60
251/* I-path capacitance adjustment for Gen1 */
252#define RG_CDR_BC_GEN1_MSK GENMASK(28, 24)
253#define RG_CDR_BC_GEN1_VAL(x) ((0x1f & (x)) << 24)
254#define RG_CDR_BIRLTR_GEN1_MSK GENMASK(4, 0)
255#define RG_CDR_BIRLTR_GEN1_VAL(x) (0x1f & (x))
256
257#define ANA_EQ_EYE_CTRL_SIGNAL1 0x6c
258/* RX Gen1 LEQ tuning step */
259#define RG_EQ_DLEQ_LFI_GEN1_MSK GENMASK(11, 8)
260#define RG_EQ_DLEQ_LFI_GEN1_VAL(x) ((0xf & (x)) << 8)
261
262#define ANA_EQ_EYE_CTRL_SIGNAL4 0xd8
263#define RG_CDR_BIRLTD0_GEN1_MSK GENMASK(20, 16)
264#define RG_CDR_BIRLTD0_GEN1_VAL(x) ((0x1f & (x)) << 16)
265
266#define ANA_EQ_EYE_CTRL_SIGNAL5 0xdc
267#define RG_CDR_BIRLTD0_GEN3_MSK GENMASK(4, 0)
268#define RG_CDR_BIRLTD0_GEN3_VAL(x) (0x1f & (x))
269
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800270enum mtk_phy_version {
271 MTK_PHY_V1 = 1,
272 MTK_PHY_V2,
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800273};
274
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800275struct mtk_phy_pdata {
Chunfeng Yune1d76532016-04-20 08:14:02 +0800276 /* avoid RX sensitivity level degradation only for mt8173 */
277 bool avoid_rx_sen_degradation;
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800278 enum mtk_phy_version version;
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800279};
280
281struct u2phy_banks {
282 void __iomem *misc;
283 void __iomem *fmreg;
284 void __iomem *com;
285};
286
287struct u3phy_banks {
288 void __iomem *spllc;
289 void __iomem *chip;
290 void __iomem *phyd; /* include u3phyd_bank2 */
291 void __iomem *phya; /* include u3phya_da */
Chunfeng Yune1d76532016-04-20 08:14:02 +0800292};
293
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800294struct mtk_phy_instance {
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800295 struct phy *phy;
296 void __iomem *port_base;
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800297 union {
298 struct u2phy_banks u2_banks;
299 struct u3phy_banks u3_banks;
300 };
Chunfeng Yun12d0c0b2020-02-11 11:21:15 +0800301 struct clk *ref_clk; /* reference clock of (digital) phy */
302 struct clk *da_ref_clk; /* reference clock of analog phy */
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800303 u32 index;
304 u8 type;
Chunfeng Yun8158e912018-06-29 10:20:29 +0800305 int eye_src;
306 int eye_vrt;
307 int eye_term;
Chunfeng Yun410572e2020-02-11 11:21:12 +0800308 int intr;
Chunfeng Yun8be5a672020-01-08 09:52:01 +0800309 int discth;
Chunfeng Yund4f97f12018-06-29 10:20:30 +0800310 bool bc12_en;
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800311};
312
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800313struct mtk_tphy {
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800314 struct device *dev;
Chunfeng Yun04466ef2017-03-31 15:35:29 +0800315 void __iomem *sif_base; /* only shared sif */
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800316 const struct mtk_phy_pdata *pdata;
317 struct mtk_phy_instance **phys;
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800318 int nphys;
Chunfeng Yun8833ebf42018-03-12 13:25:39 +0800319 int src_ref_clk; /* MHZ, reference clock for slew rate calibrate */
320 int src_coef; /* coefficient for slew rate calibrate */
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800321};
322
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800323static void hs_slew_rate_calibrate(struct mtk_tphy *tphy,
324 struct mtk_phy_instance *instance)
Chunfeng Yun75f072f2015-12-04 10:11:05 +0800325{
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800326 struct u2phy_banks *u2_banks = &instance->u2_banks;
327 void __iomem *fmreg = u2_banks->fmreg;
328 void __iomem *com = u2_banks->com;
Chunfeng Yun75f072f2015-12-04 10:11:05 +0800329 int calibration_val;
330 int fm_out;
331 u32 tmp;
332
Chunfeng Yun8158e912018-06-29 10:20:29 +0800333 /* use force value */
334 if (instance->eye_src)
335 return;
336
Chunfeng Yun75f072f2015-12-04 10:11:05 +0800337 /* enable USB ring oscillator */
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800338 tmp = readl(com + U3P_USBPHYACR5);
Chunfeng Yun75f072f2015-12-04 10:11:05 +0800339 tmp |= PA5_RG_U2_HSTX_SRCAL_EN;
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800340 writel(tmp, com + U3P_USBPHYACR5);
Chunfeng Yun75f072f2015-12-04 10:11:05 +0800341 udelay(1);
342
343 /*enable free run clock */
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800344 tmp = readl(fmreg + U3P_U2FREQ_FMMONR1);
Chunfeng Yun75f072f2015-12-04 10:11:05 +0800345 tmp |= P2F_RG_FRCK_EN;
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800346 writel(tmp, fmreg + U3P_U2FREQ_FMMONR1);
Chunfeng Yun75f072f2015-12-04 10:11:05 +0800347
348 /* set cycle count as 1024, and select u2 channel */
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800349 tmp = readl(fmreg + U3P_U2FREQ_FMCR0);
Chunfeng Yun75f072f2015-12-04 10:11:05 +0800350 tmp &= ~(P2F_RG_CYCLECNT | P2F_RG_MONCLK_SEL);
351 tmp |= P2F_RG_CYCLECNT_VAL(U3P_FM_DET_CYCLE_CNT);
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800352 if (tphy->pdata->version == MTK_PHY_V1)
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800353 tmp |= P2F_RG_MONCLK_SEL_VAL(instance->index >> 1);
354
355 writel(tmp, fmreg + U3P_U2FREQ_FMCR0);
Chunfeng Yun75f072f2015-12-04 10:11:05 +0800356
357 /* enable frequency meter */
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800358 tmp = readl(fmreg + U3P_U2FREQ_FMCR0);
Chunfeng Yun75f072f2015-12-04 10:11:05 +0800359 tmp |= P2F_RG_FREQDET_EN;
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800360 writel(tmp, fmreg + U3P_U2FREQ_FMCR0);
Chunfeng Yun75f072f2015-12-04 10:11:05 +0800361
362 /* ignore return value */
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800363 readl_poll_timeout(fmreg + U3P_U2FREQ_FMMONR1, tmp,
364 (tmp & P2F_USB_FM_VALID), 10, 200);
Chunfeng Yun75f072f2015-12-04 10:11:05 +0800365
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800366 fm_out = readl(fmreg + U3P_U2FREQ_VALUE);
Chunfeng Yun75f072f2015-12-04 10:11:05 +0800367
368 /* disable frequency meter */
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800369 tmp = readl(fmreg + U3P_U2FREQ_FMCR0);
Chunfeng Yun75f072f2015-12-04 10:11:05 +0800370 tmp &= ~P2F_RG_FREQDET_EN;
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800371 writel(tmp, fmreg + U3P_U2FREQ_FMCR0);
Chunfeng Yun75f072f2015-12-04 10:11:05 +0800372
373 /*disable free run clock */
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800374 tmp = readl(fmreg + U3P_U2FREQ_FMMONR1);
Chunfeng Yun75f072f2015-12-04 10:11:05 +0800375 tmp &= ~P2F_RG_FRCK_EN;
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800376 writel(tmp, fmreg + U3P_U2FREQ_FMMONR1);
Chunfeng Yun75f072f2015-12-04 10:11:05 +0800377
378 if (fm_out) {
Chunfeng Yun8833ebf42018-03-12 13:25:39 +0800379 /* ( 1024 / FM_OUT ) x reference clock frequency x coef */
380 tmp = tphy->src_ref_clk * tphy->src_coef;
381 tmp = (tmp * U3P_FM_DET_CYCLE_CNT) / fm_out;
Chunfeng Yun75f072f2015-12-04 10:11:05 +0800382 calibration_val = DIV_ROUND_CLOSEST(tmp, U3P_SR_COEF_DIVISOR);
383 } else {
384 /* if FM detection fail, set default value */
385 calibration_val = 4;
386 }
Chunfeng Yun8833ebf42018-03-12 13:25:39 +0800387 dev_dbg(tphy->dev, "phy:%d, fm_out:%d, calib:%d (clk:%d, coef:%d)\n",
388 instance->index, fm_out, calibration_val,
389 tphy->src_ref_clk, tphy->src_coef);
Chunfeng Yun75f072f2015-12-04 10:11:05 +0800390
391 /* set HS slew rate */
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800392 tmp = readl(com + U3P_USBPHYACR5);
Chunfeng Yun75f072f2015-12-04 10:11:05 +0800393 tmp &= ~PA5_RG_U2_HSTX_SRCTRL;
394 tmp |= PA5_RG_U2_HSTX_SRCTRL_VAL(calibration_val);
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800395 writel(tmp, com + U3P_USBPHYACR5);
Chunfeng Yun75f072f2015-12-04 10:11:05 +0800396
397 /* disable USB ring oscillator */
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_SRCAL_EN;
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800400 writel(tmp, com + U3P_USBPHYACR5);
Chunfeng Yun75f072f2015-12-04 10:11:05 +0800401}
402
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800403static void u3_phy_instance_init(struct mtk_tphy *tphy,
404 struct mtk_phy_instance *instance)
Chunfeng Yun04466ef2017-03-31 15:35:29 +0800405{
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800406 struct u3phy_banks *u3_banks = &instance->u3_banks;
Chunfeng Yun04466ef2017-03-31 15:35:29 +0800407 u32 tmp;
408
409 /* gating PCIe Analog XTAL clock */
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800410 tmp = readl(u3_banks->spllc + U3P_SPLLC_XTALCTL3);
Chunfeng Yun04466ef2017-03-31 15:35:29 +0800411 tmp |= XC3_RG_U3_XTAL_RX_PWD | XC3_RG_U3_FRC_XTAL_RX_PWD;
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800412 writel(tmp, u3_banks->spllc + U3P_SPLLC_XTALCTL3);
Chunfeng Yun04466ef2017-03-31 15:35:29 +0800413
414 /* gating XSQ */
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800415 tmp = readl(u3_banks->phya + U3P_U3_PHYA_DA_REG0);
Chunfeng Yun04466ef2017-03-31 15:35:29 +0800416 tmp &= ~P3A_RG_XTAL_EXT_EN_U3;
417 tmp |= P3A_RG_XTAL_EXT_EN_U3_VAL(2);
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800418 writel(tmp, u3_banks->phya + U3P_U3_PHYA_DA_REG0);
Chunfeng Yun04466ef2017-03-31 15:35:29 +0800419
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800420 tmp = readl(u3_banks->phya + U3P_U3_PHYA_REG9);
Chunfeng Yun04466ef2017-03-31 15:35:29 +0800421 tmp &= ~P3A_RG_RX_DAC_MUX;
422 tmp |= P3A_RG_RX_DAC_MUX_VAL(4);
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800423 writel(tmp, u3_banks->phya + U3P_U3_PHYA_REG9);
Chunfeng Yun04466ef2017-03-31 15:35:29 +0800424
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800425 tmp = readl(u3_banks->phya + U3P_U3_PHYA_REG6);
Chunfeng Yun04466ef2017-03-31 15:35:29 +0800426 tmp &= ~P3A_RG_TX_EIDLE_CM;
427 tmp |= P3A_RG_TX_EIDLE_CM_VAL(0xe);
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800428 writel(tmp, u3_banks->phya + U3P_U3_PHYA_REG6);
Chunfeng Yun04466ef2017-03-31 15:35:29 +0800429
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800430 tmp = readl(u3_banks->phyd + U3P_U3_PHYD_CDR1);
Chunfeng Yun04466ef2017-03-31 15:35:29 +0800431 tmp &= ~(P3D_RG_CDR_BIR_LTD0 | P3D_RG_CDR_BIR_LTD1);
432 tmp |= P3D_RG_CDR_BIR_LTD0_VAL(0xc) | P3D_RG_CDR_BIR_LTD1_VAL(0x3);
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800433 writel(tmp, u3_banks->phyd + U3P_U3_PHYD_CDR1);
Chunfeng Yun04466ef2017-03-31 15:35:29 +0800434
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800435 tmp = readl(u3_banks->phyd + U3P_U3_PHYD_LFPS1);
Chunfeng Yun04466ef2017-03-31 15:35:29 +0800436 tmp &= ~P3D_RG_FWAKE_TH;
437 tmp |= P3D_RG_FWAKE_TH_VAL(0x34);
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800438 writel(tmp, u3_banks->phyd + U3P_U3_PHYD_LFPS1);
Chunfeng Yun04466ef2017-03-31 15:35:29 +0800439
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800440 tmp = readl(u3_banks->phyd + U3P_U3_PHYD_RXDET1);
Chunfeng Yun04466ef2017-03-31 15:35:29 +0800441 tmp &= ~P3D_RG_RXDET_STB2_SET;
442 tmp |= P3D_RG_RXDET_STB2_SET_VAL(0x10);
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800443 writel(tmp, u3_banks->phyd + U3P_U3_PHYD_RXDET1);
Chunfeng Yun04466ef2017-03-31 15:35:29 +0800444
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800445 tmp = readl(u3_banks->phyd + U3P_U3_PHYD_RXDET2);
Chunfeng Yun04466ef2017-03-31 15:35:29 +0800446 tmp &= ~P3D_RG_RXDET_STB2_SET_P3;
447 tmp |= P3D_RG_RXDET_STB2_SET_P3_VAL(0x10);
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800448 writel(tmp, u3_banks->phyd + U3P_U3_PHYD_RXDET2);
Chunfeng Yun04466ef2017-03-31 15:35:29 +0800449
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800450 dev_dbg(tphy->dev, "%s(%d)\n", __func__, instance->index);
Chunfeng Yun04466ef2017-03-31 15:35:29 +0800451}
452
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800453static void u2_phy_instance_init(struct mtk_tphy *tphy,
454 struct mtk_phy_instance *instance)
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800455{
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800456 struct u2phy_banks *u2_banks = &instance->u2_banks;
457 void __iomem *com = u2_banks->com;
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800458 u32 index = instance->index;
459 u32 tmp;
460
Chunfeng Yun00c00922017-12-07 19:53:34 +0800461 /* switch to USB function, and enable usb pll */
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800462 tmp = readl(com + U3P_U2PHYDTM0);
Chunfeng Yun00c00922017-12-07 19:53:34 +0800463 tmp &= ~(P2C_FORCE_UART_EN | P2C_FORCE_SUSPENDM);
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800464 tmp |= P2C_RG_XCVRSEL_VAL(1) | P2C_RG_DATAIN_VAL(0);
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800465 writel(tmp, com + U3P_U2PHYDTM0);
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800466
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800467 tmp = readl(com + U3P_U2PHYDTM1);
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800468 tmp &= ~P2C_RG_UART_EN;
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800469 writel(tmp, com + U3P_U2PHYDTM1);
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800470
Chunfeng Yunc0250fe2017-03-31 15:35:32 +0800471 tmp = readl(com + U3P_USBPHYACR0);
472 tmp |= PA0_RG_USB20_INTR_EN;
473 writel(tmp, com + U3P_USBPHYACR0);
474
475 /* disable switch 100uA current to SSUSB */
476 tmp = readl(com + U3P_USBPHYACR5);
477 tmp &= ~PA5_RG_U2_HS_100U_U3_EN;
478 writel(tmp, com + U3P_USBPHYACR5);
479
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800480 if (!index) {
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800481 tmp = readl(com + U3P_U2PHYACR4);
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800482 tmp &= ~P2C_U2_GPIO_CTR_MSK;
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800483 writel(tmp, com + U3P_U2PHYACR4);
Chunfeng Yune1d76532016-04-20 08:14:02 +0800484 }
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800485
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800486 if (tphy->pdata->avoid_rx_sen_degradation) {
Chunfeng Yune1d76532016-04-20 08:14:02 +0800487 if (!index) {
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800488 tmp = readl(com + U3P_USBPHYACR2);
Chunfeng Yune1d76532016-04-20 08:14:02 +0800489 tmp |= PA2_RG_SIF_U2PLL_FORCE_EN;
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800490 writel(tmp, com + U3P_USBPHYACR2);
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800491
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800492 tmp = readl(com + U3D_U2PHYDCR0);
Chunfeng Yune1d76532016-04-20 08:14:02 +0800493 tmp &= ~P2C_RG_SIF_U2PLL_FORCE_ON;
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800494 writel(tmp, com + U3D_U2PHYDCR0);
Chunfeng Yune1d76532016-04-20 08:14:02 +0800495 } else {
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800496 tmp = readl(com + U3D_U2PHYDCR0);
Chunfeng Yune1d76532016-04-20 08:14:02 +0800497 tmp |= P2C_RG_SIF_U2PLL_FORCE_ON;
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800498 writel(tmp, com + U3D_U2PHYDCR0);
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800499
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800500 tmp = readl(com + U3P_U2PHYDTM0);
Chunfeng Yune1d76532016-04-20 08:14:02 +0800501 tmp |= P2C_RG_SUSPENDM | P2C_FORCE_SUSPENDM;
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800502 writel(tmp, com + U3P_U2PHYDTM0);
Chunfeng Yune1d76532016-04-20 08:14:02 +0800503 }
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800504 }
505
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800506 tmp = readl(com + U3P_USBPHYACR6);
Chunfeng Yun43f53b12015-12-04 10:08:56 +0800507 tmp &= ~PA6_RG_U2_BC11_SW_EN; /* DP/DM BC1.1 path Disable */
508 tmp &= ~PA6_RG_U2_SQTH;
509 tmp |= PA6_RG_U2_SQTH_VAL(2);
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800510 writel(tmp, com + U3P_USBPHYACR6);
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800511
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800512 dev_dbg(tphy->dev, "%s(%d)\n", __func__, index);
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800513}
514
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800515static void u2_phy_instance_power_on(struct mtk_tphy *tphy,
516 struct mtk_phy_instance *instance)
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800517{
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800518 struct u2phy_banks *u2_banks = &instance->u2_banks;
519 void __iomem *com = u2_banks->com;
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800520 u32 index = instance->index;
521 u32 tmp;
522
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800523 tmp = readl(com + U3P_U2PHYDTM0);
Chunfeng Yun00c00922017-12-07 19:53:34 +0800524 tmp &= ~(P2C_RG_XCVRSEL | P2C_RG_DATAIN | P2C_DTM0_PART_MASK);
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800525 writel(tmp, com + U3P_U2PHYDTM0);
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800526
527 /* OTG Enable */
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800528 tmp = readl(com + U3P_USBPHYACR6);
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800529 tmp |= PA6_RG_U2_OTG_VBUSCMP_EN;
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800530 writel(tmp, com + U3P_USBPHYACR6);
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800531
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800532 tmp = readl(com + U3P_U2PHYDTM1);
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800533 tmp |= P2C_RG_VBUSVALID | P2C_RG_AVALID;
534 tmp &= ~P2C_RG_SESSEND;
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800535 writel(tmp, com + U3P_U2PHYDTM1);
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800536
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800537 if (tphy->pdata->avoid_rx_sen_degradation && index) {
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800538 tmp = readl(com + U3D_U2PHYDCR0);
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800539 tmp |= P2C_RG_SIF_U2PLL_FORCE_ON;
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800540 writel(tmp, com + U3D_U2PHYDCR0);
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800541
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800542 tmp = readl(com + U3P_U2PHYDTM0);
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800543 tmp |= P2C_RG_SUSPENDM | P2C_FORCE_SUSPENDM;
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800544 writel(tmp, com + U3P_U2PHYDTM0);
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800545 }
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800546 dev_dbg(tphy->dev, "%s(%d)\n", __func__, index);
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800547}
548
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800549static void u2_phy_instance_power_off(struct mtk_tphy *tphy,
550 struct mtk_phy_instance *instance)
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800551{
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800552 struct u2phy_banks *u2_banks = &instance->u2_banks;
553 void __iomem *com = u2_banks->com;
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800554 u32 index = instance->index;
555 u32 tmp;
556
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800557 tmp = readl(com + U3P_U2PHYDTM0);
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800558 tmp &= ~(P2C_RG_XCVRSEL | P2C_RG_DATAIN);
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800559 writel(tmp, com + U3P_U2PHYDTM0);
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800560
561 /* OTG Disable */
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800562 tmp = readl(com + U3P_USBPHYACR6);
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800563 tmp &= ~PA6_RG_U2_OTG_VBUSCMP_EN;
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800564 writel(tmp, com + U3P_USBPHYACR6);
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800565
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800566 tmp = readl(com + U3P_U2PHYDTM1);
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800567 tmp &= ~(P2C_RG_VBUSVALID | P2C_RG_AVALID);
568 tmp |= P2C_RG_SESSEND;
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800569 writel(tmp, com + U3P_U2PHYDTM1);
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800570
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800571 if (tphy->pdata->avoid_rx_sen_degradation && index) {
Chunfeng Yun00c00922017-12-07 19:53:34 +0800572 tmp = readl(com + U3P_U2PHYDTM0);
573 tmp &= ~(P2C_RG_SUSPENDM | P2C_FORCE_SUSPENDM);
574 writel(tmp, com + U3P_U2PHYDTM0);
575
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800576 tmp = readl(com + U3D_U2PHYDCR0);
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800577 tmp &= ~P2C_RG_SIF_U2PLL_FORCE_ON;
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800578 writel(tmp, com + U3D_U2PHYDCR0);
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800579 }
580
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800581 dev_dbg(tphy->dev, "%s(%d)\n", __func__, index);
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800582}
583
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800584static void u2_phy_instance_exit(struct mtk_tphy *tphy,
585 struct mtk_phy_instance *instance)
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800586{
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800587 struct u2phy_banks *u2_banks = &instance->u2_banks;
588 void __iomem *com = u2_banks->com;
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800589 u32 index = instance->index;
590 u32 tmp;
591
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800592 if (tphy->pdata->avoid_rx_sen_degradation && index) {
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800593 tmp = readl(com + U3D_U2PHYDCR0);
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800594 tmp &= ~P2C_RG_SIF_U2PLL_FORCE_ON;
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800595 writel(tmp, com + U3D_U2PHYDCR0);
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800596
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800597 tmp = readl(com + U3P_U2PHYDTM0);
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800598 tmp &= ~P2C_FORCE_SUSPENDM;
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800599 writel(tmp, com + U3P_U2PHYDTM0);
600 }
601}
602
Chunfeng Yun5954a102017-09-21 18:31:49 +0800603static void u2_phy_instance_set_mode(struct mtk_tphy *tphy,
604 struct mtk_phy_instance *instance,
605 enum phy_mode mode)
606{
607 struct u2phy_banks *u2_banks = &instance->u2_banks;
608 u32 tmp;
609
610 tmp = readl(u2_banks->com + U3P_U2PHYDTM1);
611 switch (mode) {
612 case PHY_MODE_USB_DEVICE:
613 tmp |= P2C_FORCE_IDDIG | P2C_RG_IDDIG;
614 break;
615 case PHY_MODE_USB_HOST:
616 tmp |= P2C_FORCE_IDDIG;
617 tmp &= ~P2C_RG_IDDIG;
618 break;
619 case PHY_MODE_USB_OTG:
620 tmp &= ~(P2C_FORCE_IDDIG | P2C_RG_IDDIG);
621 break;
622 default:
623 return;
624 }
625 writel(tmp, u2_banks->com + U3P_U2PHYDTM1);
626}
627
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800628static void pcie_phy_instance_init(struct mtk_tphy *tphy,
629 struct mtk_phy_instance *instance)
Ryder Lee44a6d6c2017-08-03 18:01:00 +0800630{
631 struct u3phy_banks *u3_banks = &instance->u3_banks;
632 u32 tmp;
633
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800634 if (tphy->pdata->version != MTK_PHY_V1)
Ryder Lee44a6d6c2017-08-03 18:01:00 +0800635 return;
636
637 tmp = readl(u3_banks->phya + U3P_U3_PHYA_DA_REG0);
638 tmp &= ~(P3A_RG_XTAL_EXT_PE1H | P3A_RG_XTAL_EXT_PE2H);
639 tmp |= P3A_RG_XTAL_EXT_PE1H_VAL(0x2) | P3A_RG_XTAL_EXT_PE2H_VAL(0x2);
640 writel(tmp, u3_banks->phya + U3P_U3_PHYA_DA_REG0);
641
642 /* ref clk drive */
643 tmp = readl(u3_banks->phya + U3P_U3_PHYA_REG1);
644 tmp &= ~P3A_RG_CLKDRV_AMP;
645 tmp |= P3A_RG_CLKDRV_AMP_VAL(0x4);
646 writel(tmp, u3_banks->phya + U3P_U3_PHYA_REG1);
647
648 tmp = readl(u3_banks->phya + U3P_U3_PHYA_REG0);
649 tmp &= ~P3A_RG_CLKDRV_OFF;
650 tmp |= P3A_RG_CLKDRV_OFF_VAL(0x1);
651 writel(tmp, u3_banks->phya + U3P_U3_PHYA_REG0);
652
653 /* SSC delta -5000ppm */
654 tmp = readl(u3_banks->phya + U3P_U3_PHYA_DA_REG20);
655 tmp &= ~P3A_RG_PLL_DELTA1_PE2H;
656 tmp |= P3A_RG_PLL_DELTA1_PE2H_VAL(0x3c);
657 writel(tmp, u3_banks->phya + U3P_U3_PHYA_DA_REG20);
658
659 tmp = readl(u3_banks->phya + U3P_U3_PHYA_DA_REG25);
660 tmp &= ~P3A_RG_PLL_DELTA_PE2H;
661 tmp |= P3A_RG_PLL_DELTA_PE2H_VAL(0x36);
662 writel(tmp, u3_banks->phya + U3P_U3_PHYA_DA_REG25);
663
664 /* change pll BW 0.6M */
665 tmp = readl(u3_banks->phya + U3P_U3_PHYA_DA_REG5);
666 tmp &= ~(P3A_RG_PLL_BR_PE2H | P3A_RG_PLL_IC_PE2H);
667 tmp |= P3A_RG_PLL_BR_PE2H_VAL(0x1) | P3A_RG_PLL_IC_PE2H_VAL(0x1);
668 writel(tmp, u3_banks->phya + U3P_U3_PHYA_DA_REG5);
669
670 tmp = readl(u3_banks->phya + U3P_U3_PHYA_DA_REG4);
671 tmp &= ~(P3A_RG_PLL_DIVEN_PE2H | P3A_RG_PLL_BC_PE2H);
672 tmp |= P3A_RG_PLL_BC_PE2H_VAL(0x3);
673 writel(tmp, u3_banks->phya + U3P_U3_PHYA_DA_REG4);
674
675 tmp = readl(u3_banks->phya + U3P_U3_PHYA_DA_REG6);
676 tmp &= ~P3A_RG_PLL_IR_PE2H;
677 tmp |= P3A_RG_PLL_IR_PE2H_VAL(0x2);
678 writel(tmp, u3_banks->phya + U3P_U3_PHYA_DA_REG6);
679
680 tmp = readl(u3_banks->phya + U3P_U3_PHYA_DA_REG7);
681 tmp &= ~P3A_RG_PLL_BP_PE2H;
682 tmp |= P3A_RG_PLL_BP_PE2H_VAL(0xa);
683 writel(tmp, u3_banks->phya + U3P_U3_PHYA_DA_REG7);
684
685 /* Tx Detect Rx Timing: 10us -> 5us */
686 tmp = readl(u3_banks->phyd + U3P_U3_PHYD_RXDET1);
687 tmp &= ~P3D_RG_RXDET_STB2_SET;
688 tmp |= P3D_RG_RXDET_STB2_SET_VAL(0x10);
689 writel(tmp, u3_banks->phyd + U3P_U3_PHYD_RXDET1);
690
691 tmp = readl(u3_banks->phyd + U3P_U3_PHYD_RXDET2);
692 tmp &= ~P3D_RG_RXDET_STB2_SET_P3;
693 tmp |= P3D_RG_RXDET_STB2_SET_P3_VAL(0x10);
694 writel(tmp, u3_banks->phyd + U3P_U3_PHYD_RXDET2);
695
696 /* wait for PCIe subsys register to active */
697 usleep_range(2500, 3000);
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800698 dev_dbg(tphy->dev, "%s(%d)\n", __func__, instance->index);
Ryder Lee44a6d6c2017-08-03 18:01:00 +0800699}
700
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800701static void pcie_phy_instance_power_on(struct mtk_tphy *tphy,
702 struct mtk_phy_instance *instance)
Ryder Lee44a6d6c2017-08-03 18:01:00 +0800703{
704 struct u3phy_banks *bank = &instance->u3_banks;
705 u32 tmp;
706
707 tmp = readl(bank->chip + U3P_U3_CHIP_GPIO_CTLD);
Chunfeng Yun40363252018-03-12 13:25:38 +0800708 tmp &= ~(P3C_FORCE_IP_SW_RST | P3C_REG_IP_SW_RST);
Ryder Lee44a6d6c2017-08-03 18:01:00 +0800709 writel(tmp, bank->chip + U3P_U3_CHIP_GPIO_CTLD);
710
711 tmp = readl(bank->chip + U3P_U3_CHIP_GPIO_CTLE);
712 tmp &= ~(P3C_RG_SWRST_U3_PHYD_FORCE_EN | P3C_RG_SWRST_U3_PHYD);
713 writel(tmp, bank->chip + U3P_U3_CHIP_GPIO_CTLE);
714}
715
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800716static void pcie_phy_instance_power_off(struct mtk_tphy *tphy,
717 struct mtk_phy_instance *instance)
Ryder Lee44a6d6c2017-08-03 18:01:00 +0800718
719{
720 struct u3phy_banks *bank = &instance->u3_banks;
721 u32 tmp;
722
723 tmp = readl(bank->chip + U3P_U3_CHIP_GPIO_CTLD);
724 tmp |= P3C_FORCE_IP_SW_RST | P3C_REG_IP_SW_RST;
725 writel(tmp, bank->chip + U3P_U3_CHIP_GPIO_CTLD);
726
727 tmp = readl(bank->chip + U3P_U3_CHIP_GPIO_CTLE);
728 tmp |= P3C_RG_SWRST_U3_PHYD_FORCE_EN | P3C_RG_SWRST_U3_PHYD;
729 writel(tmp, bank->chip + U3P_U3_CHIP_GPIO_CTLE);
730}
731
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800732static void sata_phy_instance_init(struct mtk_tphy *tphy,
733 struct mtk_phy_instance *instance)
Ryder Lee4ab26cb2017-08-03 18:01:01 +0800734{
735 struct u3phy_banks *u3_banks = &instance->u3_banks;
736 void __iomem *phyd = u3_banks->phyd;
737 u32 tmp;
738
739 /* charge current adjustment */
740 tmp = readl(phyd + ANA_RG_CTRL_SIGNAL6);
741 tmp &= ~(RG_CDR_BIRLTR_GEN1_MSK | RG_CDR_BC_GEN1_MSK);
742 tmp |= RG_CDR_BIRLTR_GEN1_VAL(0x6) | RG_CDR_BC_GEN1_VAL(0x1a);
743 writel(tmp, phyd + ANA_RG_CTRL_SIGNAL6);
744
745 tmp = readl(phyd + ANA_EQ_EYE_CTRL_SIGNAL4);
746 tmp &= ~RG_CDR_BIRLTD0_GEN1_MSK;
747 tmp |= RG_CDR_BIRLTD0_GEN1_VAL(0x18);
748 writel(tmp, phyd + ANA_EQ_EYE_CTRL_SIGNAL4);
749
750 tmp = readl(phyd + ANA_EQ_EYE_CTRL_SIGNAL5);
751 tmp &= ~RG_CDR_BIRLTD0_GEN3_MSK;
752 tmp |= RG_CDR_BIRLTD0_GEN3_VAL(0x06);
753 writel(tmp, phyd + ANA_EQ_EYE_CTRL_SIGNAL5);
754
755 tmp = readl(phyd + ANA_RG_CTRL_SIGNAL4);
756 tmp &= ~(RG_CDR_BICLTR_GEN1_MSK | RG_CDR_BR_GEN2_MSK);
757 tmp |= RG_CDR_BICLTR_GEN1_VAL(0x0c) | RG_CDR_BR_GEN2_VAL(0x07);
758 writel(tmp, phyd + ANA_RG_CTRL_SIGNAL4);
759
760 tmp = readl(phyd + PHYD_CTRL_SIGNAL_MODE4);
761 tmp &= ~(RG_CDR_BICLTD0_GEN1_MSK | RG_CDR_BICLTD1_GEN1_MSK);
762 tmp |= RG_CDR_BICLTD0_GEN1_VAL(0x08) | RG_CDR_BICLTD1_GEN1_VAL(0x02);
763 writel(tmp, phyd + PHYD_CTRL_SIGNAL_MODE4);
764
765 tmp = readl(phyd + PHYD_DESIGN_OPTION2);
766 tmp &= ~RG_LOCK_CNT_SEL_MSK;
767 tmp |= RG_LOCK_CNT_SEL_VAL(0x02);
768 writel(tmp, phyd + PHYD_DESIGN_OPTION2);
769
770 tmp = readl(phyd + PHYD_DESIGN_OPTION9);
771 tmp &= ~(RG_T2_MIN_MSK | RG_TG_MIN_MSK |
772 RG_T2_MAX_MSK | RG_TG_MAX_MSK);
773 tmp |= RG_T2_MIN_VAL(0x12) | RG_TG_MIN_VAL(0x04) |
774 RG_T2_MAX_VAL(0x31) | RG_TG_MAX_VAL(0x0e);
775 writel(tmp, phyd + PHYD_DESIGN_OPTION9);
776
777 tmp = readl(phyd + ANA_RG_CTRL_SIGNAL1);
778 tmp &= ~RG_IDRV_0DB_GEN1_MSK;
779 tmp |= RG_IDRV_0DB_GEN1_VAL(0x20);
780 writel(tmp, phyd + ANA_RG_CTRL_SIGNAL1);
781
782 tmp = readl(phyd + ANA_EQ_EYE_CTRL_SIGNAL1);
783 tmp &= ~RG_EQ_DLEQ_LFI_GEN1_MSK;
784 tmp |= RG_EQ_DLEQ_LFI_GEN1_VAL(0x03);
785 writel(tmp, phyd + ANA_EQ_EYE_CTRL_SIGNAL1);
786
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800787 dev_dbg(tphy->dev, "%s(%d)\n", __func__, instance->index);
Ryder Lee4ab26cb2017-08-03 18:01:01 +0800788}
789
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800790static void phy_v1_banks_init(struct mtk_tphy *tphy,
791 struct mtk_phy_instance *instance)
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800792{
793 struct u2phy_banks *u2_banks = &instance->u2_banks;
794 struct u3phy_banks *u3_banks = &instance->u3_banks;
795
Ryder Lee44a6d6c2017-08-03 18:01:00 +0800796 switch (instance->type) {
797 case PHY_TYPE_USB2:
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800798 u2_banks->misc = NULL;
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800799 u2_banks->fmreg = tphy->sif_base + SSUSB_SIFSLV_V1_U2FREQ;
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800800 u2_banks->com = instance->port_base + SSUSB_SIFSLV_V1_U2PHY_COM;
Ryder Lee44a6d6c2017-08-03 18:01:00 +0800801 break;
802 case PHY_TYPE_USB3:
803 case PHY_TYPE_PCIE:
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800804 u3_banks->spllc = tphy->sif_base + SSUSB_SIFSLV_V1_SPLLC;
Chunfeng Yun554a56f2017-09-21 18:31:48 +0800805 u3_banks->chip = tphy->sif_base + SSUSB_SIFSLV_V1_CHIP;
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800806 u3_banks->phyd = instance->port_base + SSUSB_SIFSLV_V1_U3PHYD;
807 u3_banks->phya = instance->port_base + SSUSB_SIFSLV_V1_U3PHYA;
Ryder Lee44a6d6c2017-08-03 18:01:00 +0800808 break;
Ryder Lee4ab26cb2017-08-03 18:01:01 +0800809 case PHY_TYPE_SATA:
810 u3_banks->phyd = instance->port_base + SSUSB_SIFSLV_V1_U3PHYD;
811 break;
Ryder Lee44a6d6c2017-08-03 18:01:00 +0800812 default:
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800813 dev_err(tphy->dev, "incompatible PHY type\n");
Ryder Lee44a6d6c2017-08-03 18:01:00 +0800814 return;
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800815 }
816}
817
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800818static void phy_v2_banks_init(struct mtk_tphy *tphy,
819 struct mtk_phy_instance *instance)
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800820{
821 struct u2phy_banks *u2_banks = &instance->u2_banks;
822 struct u3phy_banks *u3_banks = &instance->u3_banks;
823
Ryder Lee44a6d6c2017-08-03 18:01:00 +0800824 switch (instance->type) {
825 case PHY_TYPE_USB2:
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800826 u2_banks->misc = instance->port_base + SSUSB_SIFSLV_V2_MISC;
827 u2_banks->fmreg = instance->port_base + SSUSB_SIFSLV_V2_U2FREQ;
828 u2_banks->com = instance->port_base + SSUSB_SIFSLV_V2_U2PHY_COM;
Ryder Lee44a6d6c2017-08-03 18:01:00 +0800829 break;
830 case PHY_TYPE_USB3:
831 case PHY_TYPE_PCIE:
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800832 u3_banks->spllc = instance->port_base + SSUSB_SIFSLV_V2_SPLLC;
833 u3_banks->chip = instance->port_base + SSUSB_SIFSLV_V2_CHIP;
834 u3_banks->phyd = instance->port_base + SSUSB_SIFSLV_V2_U3PHYD;
835 u3_banks->phya = instance->port_base + SSUSB_SIFSLV_V2_U3PHYA;
Ryder Lee44a6d6c2017-08-03 18:01:00 +0800836 break;
837 default:
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800838 dev_err(tphy->dev, "incompatible PHY type\n");
Ryder Lee44a6d6c2017-08-03 18:01:00 +0800839 return;
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800840 }
841}
842
Chunfeng Yun8158e912018-06-29 10:20:29 +0800843static void phy_parse_property(struct mtk_tphy *tphy,
844 struct mtk_phy_instance *instance)
845{
846 struct device *dev = &instance->phy->dev;
847
848 if (instance->type != PHY_TYPE_USB2)
849 return;
850
Chunfeng Yund4f97f12018-06-29 10:20:30 +0800851 instance->bc12_en = device_property_read_bool(dev, "mediatek,bc12");
Chunfeng Yun8158e912018-06-29 10:20:29 +0800852 device_property_read_u32(dev, "mediatek,eye-src",
853 &instance->eye_src);
854 device_property_read_u32(dev, "mediatek,eye-vrt",
855 &instance->eye_vrt);
856 device_property_read_u32(dev, "mediatek,eye-term",
857 &instance->eye_term);
Chunfeng Yun410572e2020-02-11 11:21:12 +0800858 device_property_read_u32(dev, "mediatek,intr",
859 &instance->intr);
Chunfeng Yun8be5a672020-01-08 09:52:01 +0800860 device_property_read_u32(dev, "mediatek,discth",
861 &instance->discth);
Chunfeng Yun410572e2020-02-11 11:21:12 +0800862 dev_dbg(dev, "bc12:%d, src:%d, vrt:%d, term:%d, intr:%d, disc:%d\n",
Chunfeng Yund4f97f12018-06-29 10:20:30 +0800863 instance->bc12_en, instance->eye_src,
Chunfeng Yun8be5a672020-01-08 09:52:01 +0800864 instance->eye_vrt, instance->eye_term,
Chunfeng Yun410572e2020-02-11 11:21:12 +0800865 instance->intr, instance->discth);
Chunfeng Yun8158e912018-06-29 10:20:29 +0800866}
867
868static void u2_phy_props_set(struct mtk_tphy *tphy,
869 struct mtk_phy_instance *instance)
870{
871 struct u2phy_banks *u2_banks = &instance->u2_banks;
872 void __iomem *com = u2_banks->com;
873 u32 tmp;
874
Chunfeng Yund4f97f12018-06-29 10:20:30 +0800875 if (instance->bc12_en) {
876 tmp = readl(com + U3P_U2PHYBC12C);
877 tmp |= P2C_RG_CHGDT_EN; /* BC1.2 path Enable */
878 writel(tmp, com + U3P_U2PHYBC12C);
879 }
Chunfeng Yun8158e912018-06-29 10:20:29 +0800880
881 if (instance->eye_src) {
882 tmp = readl(com + U3P_USBPHYACR5);
883 tmp &= ~PA5_RG_U2_HSTX_SRCTRL;
884 tmp |= PA5_RG_U2_HSTX_SRCTRL_VAL(instance->eye_src);
885 writel(tmp, com + U3P_USBPHYACR5);
886 }
887
888 if (instance->eye_vrt) {
889 tmp = readl(com + U3P_USBPHYACR1);
890 tmp &= ~PA1_RG_VRT_SEL;
891 tmp |= PA1_RG_VRT_SEL_VAL(instance->eye_vrt);
892 writel(tmp, com + U3P_USBPHYACR1);
893 }
894
895 if (instance->eye_term) {
896 tmp = readl(com + U3P_USBPHYACR1);
897 tmp &= ~PA1_RG_TERM_SEL;
898 tmp |= PA1_RG_TERM_SEL_VAL(instance->eye_term);
899 writel(tmp, com + U3P_USBPHYACR1);
900 }
Chunfeng Yun8be5a672020-01-08 09:52:01 +0800901
Chunfeng Yun410572e2020-02-11 11:21:12 +0800902 if (instance->intr) {
903 tmp = readl(com + U3P_USBPHYACR1);
904 tmp &= ~PA1_RG_INTR_CAL;
905 tmp |= PA1_RG_INTR_CAL_VAL(instance->intr);
906 writel(tmp, com + U3P_USBPHYACR1);
907 }
908
Chunfeng Yun8be5a672020-01-08 09:52:01 +0800909 if (instance->discth) {
910 tmp = readl(com + U3P_USBPHYACR6);
911 tmp &= ~PA6_RG_U2_DISCTH;
912 tmp |= PA6_RG_U2_DISCTH_VAL(instance->discth);
913 writel(tmp, com + U3P_USBPHYACR6);
914 }
Chunfeng Yun8158e912018-06-29 10:20:29 +0800915}
916
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800917static int mtk_phy_init(struct phy *phy)
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800918{
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800919 struct mtk_phy_instance *instance = phy_get_drvdata(phy);
920 struct mtk_tphy *tphy = dev_get_drvdata(phy->dev.parent);
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800921 int ret;
922
Chunfeng Yun15de15c2017-03-31 15:35:30 +0800923 ret = clk_prepare_enable(instance->ref_clk);
924 if (ret) {
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800925 dev_err(tphy->dev, "failed to enable ref_clk\n");
Chunfeng Yun15de15c2017-03-31 15:35:30 +0800926 return ret;
927 }
928
Chunfeng Yun12d0c0b2020-02-11 11:21:15 +0800929 ret = clk_prepare_enable(instance->da_ref_clk);
930 if (ret) {
931 dev_err(tphy->dev, "failed to enable da_ref\n");
932 clk_disable_unprepare(instance->ref_clk);
933 return ret;
934 }
935
Ryder Lee44a6d6c2017-08-03 18:01:00 +0800936 switch (instance->type) {
937 case PHY_TYPE_USB2:
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800938 u2_phy_instance_init(tphy, instance);
Chunfeng Yun8158e912018-06-29 10:20:29 +0800939 u2_phy_props_set(tphy, instance);
Ryder Lee44a6d6c2017-08-03 18:01:00 +0800940 break;
941 case PHY_TYPE_USB3:
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800942 u3_phy_instance_init(tphy, instance);
Ryder Lee44a6d6c2017-08-03 18:01:00 +0800943 break;
944 case PHY_TYPE_PCIE:
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800945 pcie_phy_instance_init(tphy, instance);
Ryder Lee44a6d6c2017-08-03 18:01:00 +0800946 break;
Ryder Lee4ab26cb2017-08-03 18:01:01 +0800947 case PHY_TYPE_SATA:
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800948 sata_phy_instance_init(tphy, instance);
Ryder Lee4ab26cb2017-08-03 18:01:01 +0800949 break;
Ryder Lee44a6d6c2017-08-03 18:01:00 +0800950 default:
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800951 dev_err(tphy->dev, "incompatible PHY type\n");
Ryder Lee44a6d6c2017-08-03 18:01:00 +0800952 return -EINVAL;
953 }
Chunfeng Yun04466ef2017-03-31 15:35:29 +0800954
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800955 return 0;
956}
957
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800958static int mtk_phy_power_on(struct phy *phy)
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800959{
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800960 struct mtk_phy_instance *instance = phy_get_drvdata(phy);
961 struct mtk_tphy *tphy = dev_get_drvdata(phy->dev.parent);
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800962
Chunfeng Yun04466ef2017-03-31 15:35:29 +0800963 if (instance->type == PHY_TYPE_USB2) {
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800964 u2_phy_instance_power_on(tphy, instance);
965 hs_slew_rate_calibrate(tphy, instance);
Ryder Lee44a6d6c2017-08-03 18:01:00 +0800966 } else if (instance->type == PHY_TYPE_PCIE) {
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800967 pcie_phy_instance_power_on(tphy, instance);
Chunfeng Yun04466ef2017-03-31 15:35:29 +0800968 }
Ryder Lee44a6d6c2017-08-03 18:01:00 +0800969
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800970 return 0;
971}
972
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800973static int mtk_phy_power_off(struct phy *phy)
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800974{
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800975 struct mtk_phy_instance *instance = phy_get_drvdata(phy);
976 struct mtk_tphy *tphy = dev_get_drvdata(phy->dev.parent);
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800977
Chunfeng Yun04466ef2017-03-31 15:35:29 +0800978 if (instance->type == PHY_TYPE_USB2)
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800979 u2_phy_instance_power_off(tphy, instance);
Ryder Lee44a6d6c2017-08-03 18:01:00 +0800980 else if (instance->type == PHY_TYPE_PCIE)
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800981 pcie_phy_instance_power_off(tphy, instance);
Chunfeng Yun04466ef2017-03-31 15:35:29 +0800982
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800983 return 0;
984}
985
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800986static int mtk_phy_exit(struct phy *phy)
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800987{
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800988 struct mtk_phy_instance *instance = phy_get_drvdata(phy);
989 struct mtk_tphy *tphy = dev_get_drvdata(phy->dev.parent);
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800990
Chunfeng Yun04466ef2017-03-31 15:35:29 +0800991 if (instance->type == PHY_TYPE_USB2)
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800992 u2_phy_instance_exit(tphy, instance);
Chunfeng Yun04466ef2017-03-31 15:35:29 +0800993
Chunfeng Yun15de15c2017-03-31 15:35:30 +0800994 clk_disable_unprepare(instance->ref_clk);
Chunfeng Yun12d0c0b2020-02-11 11:21:15 +0800995 clk_disable_unprepare(instance->da_ref_clk);
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800996 return 0;
997}
998
Grygorii Strashko79a5a182018-11-19 19:24:20 -0600999static int mtk_phy_set_mode(struct phy *phy, enum phy_mode mode, int submode)
Chunfeng Yun5954a102017-09-21 18:31:49 +08001000{
1001 struct mtk_phy_instance *instance = phy_get_drvdata(phy);
1002 struct mtk_tphy *tphy = dev_get_drvdata(phy->dev.parent);
1003
1004 if (instance->type == PHY_TYPE_USB2)
1005 u2_phy_instance_set_mode(tphy, instance, mode);
1006
1007 return 0;
1008}
1009
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +08001010static struct phy *mtk_phy_xlate(struct device *dev,
Chunfeng Yundc7f1902015-09-29 11:01:36 +08001011 struct of_phandle_args *args)
1012{
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +08001013 struct mtk_tphy *tphy = dev_get_drvdata(dev);
1014 struct mtk_phy_instance *instance = NULL;
Chunfeng Yundc7f1902015-09-29 11:01:36 +08001015 struct device_node *phy_np = args->np;
1016 int index;
1017
Chunfeng Yundc7f1902015-09-29 11:01:36 +08001018 if (args->args_count != 1) {
1019 dev_err(dev, "invalid number of cells in 'phy' property\n");
1020 return ERR_PTR(-EINVAL);
1021 }
1022
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +08001023 for (index = 0; index < tphy->nphys; index++)
1024 if (phy_np == tphy->phys[index]->phy->dev.of_node) {
1025 instance = tphy->phys[index];
Chunfeng Yundc7f1902015-09-29 11:01:36 +08001026 break;
1027 }
1028
1029 if (!instance) {
1030 dev_err(dev, "failed to find appropriate phy\n");
1031 return ERR_PTR(-EINVAL);
1032 }
1033
1034 instance->type = args->args[0];
Chunfeng Yundc7f1902015-09-29 11:01:36 +08001035 if (!(instance->type == PHY_TYPE_USB2 ||
Ryder Lee44a6d6c2017-08-03 18:01:00 +08001036 instance->type == PHY_TYPE_USB3 ||
Ryder Lee4ab26cb2017-08-03 18:01:01 +08001037 instance->type == PHY_TYPE_PCIE ||
1038 instance->type == PHY_TYPE_SATA)) {
Chunfeng Yundc7f1902015-09-29 11:01:36 +08001039 dev_err(dev, "unsupported device type: %d\n", instance->type);
1040 return ERR_PTR(-EINVAL);
1041 }
1042
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +08001043 if (tphy->pdata->version == MTK_PHY_V1) {
1044 phy_v1_banks_init(tphy, instance);
1045 } else if (tphy->pdata->version == MTK_PHY_V2) {
1046 phy_v2_banks_init(tphy, instance);
Chunfeng Yun8d6e19572017-03-31 15:35:31 +08001047 } else {
1048 dev_err(dev, "phy version is not supported\n");
1049 return ERR_PTR(-EINVAL);
1050 }
1051
Chunfeng Yun8158e912018-06-29 10:20:29 +08001052 phy_parse_property(tphy, instance);
1053
Chunfeng Yundc7f1902015-09-29 11:01:36 +08001054 return instance->phy;
1055}
1056
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +08001057static const struct phy_ops mtk_tphy_ops = {
1058 .init = mtk_phy_init,
1059 .exit = mtk_phy_exit,
1060 .power_on = mtk_phy_power_on,
1061 .power_off = mtk_phy_power_off,
Chunfeng Yun5954a102017-09-21 18:31:49 +08001062 .set_mode = mtk_phy_set_mode,
Chunfeng Yundc7f1902015-09-29 11:01:36 +08001063 .owner = THIS_MODULE,
1064};
1065
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +08001066static const struct mtk_phy_pdata tphy_v1_pdata = {
Chunfeng Yune1d76532016-04-20 08:14:02 +08001067 .avoid_rx_sen_degradation = false,
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +08001068 .version = MTK_PHY_V1,
Chunfeng Yun8d6e19572017-03-31 15:35:31 +08001069};
1070
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +08001071static const struct mtk_phy_pdata tphy_v2_pdata = {
Chunfeng Yun8d6e19572017-03-31 15:35:31 +08001072 .avoid_rx_sen_degradation = false,
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +08001073 .version = MTK_PHY_V2,
Chunfeng Yune1d76532016-04-20 08:14:02 +08001074};
1075
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +08001076static const struct mtk_phy_pdata mt8173_pdata = {
Chunfeng Yune1d76532016-04-20 08:14:02 +08001077 .avoid_rx_sen_degradation = true,
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +08001078 .version = MTK_PHY_V1,
Chunfeng Yune1d76532016-04-20 08:14:02 +08001079};
1080
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +08001081static const struct of_device_id mtk_tphy_id_table[] = {
Ryder Lee44a6d6c2017-08-03 18:01:00 +08001082 { .compatible = "mediatek,mt2701-u3phy", .data = &tphy_v1_pdata },
Ryder Lee4ab26cb2017-08-03 18:01:01 +08001083 { .compatible = "mediatek,mt2712-u3phy", .data = &tphy_v2_pdata },
Chunfeng Yune1d76532016-04-20 08:14:02 +08001084 { .compatible = "mediatek,mt8173-u3phy", .data = &mt8173_pdata },
Ryder Lee44a6d6c2017-08-03 18:01:00 +08001085 { .compatible = "mediatek,generic-tphy-v1", .data = &tphy_v1_pdata },
Ryder Lee4ab26cb2017-08-03 18:01:01 +08001086 { .compatible = "mediatek,generic-tphy-v2", .data = &tphy_v2_pdata },
Chunfeng Yune1d76532016-04-20 08:14:02 +08001087 { },
1088};
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +08001089MODULE_DEVICE_TABLE(of, mtk_tphy_id_table);
Chunfeng Yune1d76532016-04-20 08:14:02 +08001090
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +08001091static int mtk_tphy_probe(struct platform_device *pdev)
Chunfeng Yundc7f1902015-09-29 11:01:36 +08001092{
1093 struct device *dev = &pdev->dev;
1094 struct device_node *np = dev->of_node;
1095 struct device_node *child_np;
1096 struct phy_provider *provider;
1097 struct resource *sif_res;
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +08001098 struct mtk_tphy *tphy;
Chunfeng Yundc7f1902015-09-29 11:01:36 +08001099 struct resource res;
Julia Lawall2bb80cc2015-11-16 12:33:15 +01001100 int port, retval;
Chunfeng Yundc7f1902015-09-29 11:01:36 +08001101
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +08001102 tphy = devm_kzalloc(dev, sizeof(*tphy), GFP_KERNEL);
1103 if (!tphy)
Chunfeng Yundc7f1902015-09-29 11:01:36 +08001104 return -ENOMEM;
1105
Chunfeng Yune4b227c2017-12-28 16:40:36 +05301106 tphy->pdata = of_device_get_match_data(dev);
1107 if (!tphy->pdata)
1108 return -EINVAL;
1109
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +08001110 tphy->nphys = of_get_child_count(np);
1111 tphy->phys = devm_kcalloc(dev, tphy->nphys,
1112 sizeof(*tphy->phys), GFP_KERNEL);
1113 if (!tphy->phys)
Chunfeng Yundc7f1902015-09-29 11:01:36 +08001114 return -ENOMEM;
1115
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +08001116 tphy->dev = dev;
1117 platform_set_drvdata(pdev, tphy);
Chunfeng Yundc7f1902015-09-29 11:01:36 +08001118
Chunfeng Yun93a04f42017-12-07 19:53:35 +08001119 sif_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1120 /* SATA phy of V1 needn't it if not shared with PCIe or USB */
1121 if (sif_res && tphy->pdata->version == MTK_PHY_V1) {
Chunfeng Yun8d6e19572017-03-31 15:35:31 +08001122 /* get banks shared by multiple phys */
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +08001123 tphy->sif_base = devm_ioremap_resource(dev, sif_res);
1124 if (IS_ERR(tphy->sif_base)) {
Chunfeng Yun8d6e19572017-03-31 15:35:31 +08001125 dev_err(dev, "failed to remap sif regs\n");
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +08001126 return PTR_ERR(tphy->sif_base);
Chunfeng Yun8d6e19572017-03-31 15:35:31 +08001127 }
Chunfeng Yundc7f1902015-09-29 11:01:36 +08001128 }
1129
Chunfeng Yun8833ebf42018-03-12 13:25:39 +08001130 tphy->src_ref_clk = U3P_REF_CLK;
1131 tphy->src_coef = U3P_SLEW_RATE_COEF;
1132 /* update parameters of slew rate calibrate if exist */
1133 device_property_read_u32(dev, "mediatek,src-ref-clk-mhz",
1134 &tphy->src_ref_clk);
1135 device_property_read_u32(dev, "mediatek,src-coef", &tphy->src_coef);
1136
Chunfeng Yundc7f1902015-09-29 11:01:36 +08001137 port = 0;
1138 for_each_child_of_node(np, child_np) {
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +08001139 struct mtk_phy_instance *instance;
Chunfeng Yundc7f1902015-09-29 11:01:36 +08001140 struct phy *phy;
Chunfeng Yundc7f1902015-09-29 11:01:36 +08001141
1142 instance = devm_kzalloc(dev, sizeof(*instance), GFP_KERNEL);
Julia Lawall2bb80cc2015-11-16 12:33:15 +01001143 if (!instance) {
1144 retval = -ENOMEM;
1145 goto put_child;
1146 }
Chunfeng Yundc7f1902015-09-29 11:01:36 +08001147
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +08001148 tphy->phys[port] = instance;
Chunfeng Yundc7f1902015-09-29 11:01:36 +08001149
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +08001150 phy = devm_phy_create(dev, child_np, &mtk_tphy_ops);
Chunfeng Yundc7f1902015-09-29 11:01:36 +08001151 if (IS_ERR(phy)) {
1152 dev_err(dev, "failed to create phy\n");
Julia Lawall2bb80cc2015-11-16 12:33:15 +01001153 retval = PTR_ERR(phy);
1154 goto put_child;
Chunfeng Yundc7f1902015-09-29 11:01:36 +08001155 }
1156
1157 retval = of_address_to_resource(child_np, 0, &res);
1158 if (retval) {
1159 dev_err(dev, "failed to get address resource(id-%d)\n",
1160 port);
Julia Lawall2bb80cc2015-11-16 12:33:15 +01001161 goto put_child;
Chunfeng Yundc7f1902015-09-29 11:01:36 +08001162 }
1163
1164 instance->port_base = devm_ioremap_resource(&phy->dev, &res);
1165 if (IS_ERR(instance->port_base)) {
1166 dev_err(dev, "failed to remap phy regs\n");
Julia Lawall2bb80cc2015-11-16 12:33:15 +01001167 retval = PTR_ERR(instance->port_base);
1168 goto put_child;
Chunfeng Yundc7f1902015-09-29 11:01:36 +08001169 }
1170
1171 instance->phy = phy;
1172 instance->index = port;
1173 phy_set_drvdata(phy, instance);
1174 port++;
Chunfeng Yun15de15c2017-03-31 15:35:30 +08001175
Chunfeng Yun657a9ed2020-02-11 11:21:13 +08001176 instance->ref_clk = devm_clk_get_optional(&phy->dev, "ref");
Chunfeng Yun15de15c2017-03-31 15:35:30 +08001177 if (IS_ERR(instance->ref_clk)) {
1178 dev_err(dev, "failed to get ref_clk(id-%d)\n", port);
1179 retval = PTR_ERR(instance->ref_clk);
1180 goto put_child;
1181 }
Chunfeng Yun12d0c0b2020-02-11 11:21:15 +08001182
1183 instance->da_ref_clk =
1184 devm_clk_get_optional(&phy->dev, "da_ref");
1185 if (IS_ERR(instance->da_ref_clk)) {
1186 dev_err(dev, "failed to get da_ref_clk(id-%d)\n", port);
1187 retval = PTR_ERR(instance->da_ref_clk);
1188 goto put_child;
1189 }
Chunfeng Yundc7f1902015-09-29 11:01:36 +08001190 }
1191
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +08001192 provider = devm_of_phy_provider_register(dev, mtk_phy_xlate);
Chunfeng Yundc7f1902015-09-29 11:01:36 +08001193
1194 return PTR_ERR_OR_ZERO(provider);
Julia Lawall2bb80cc2015-11-16 12:33:15 +01001195put_child:
1196 of_node_put(child_np);
1197 return retval;
Chunfeng Yundc7f1902015-09-29 11:01:36 +08001198}
1199
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +08001200static struct platform_driver mtk_tphy_driver = {
1201 .probe = mtk_tphy_probe,
Chunfeng Yundc7f1902015-09-29 11:01:36 +08001202 .driver = {
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +08001203 .name = "mtk-tphy",
1204 .of_match_table = mtk_tphy_id_table,
Chunfeng Yundc7f1902015-09-29 11:01:36 +08001205 },
1206};
1207
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +08001208module_platform_driver(mtk_tphy_driver);
Chunfeng Yundc7f1902015-09-29 11:01:36 +08001209
1210MODULE_AUTHOR("Chunfeng Yun <chunfeng.yun@mediatek.com>");
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +08001211MODULE_DESCRIPTION("MediaTek T-PHY driver");
Chunfeng Yundc7f1902015-09-29 11:01:36 +08001212MODULE_LICENSE("GPL v2");