blob: c6424fd2a06ddeb92d743721e8fd537df2771bdf [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 Yun15de15c2017-03-31 15:35:30 +0800301 struct clk *ref_clk; /* reference clock of anolog phy */
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800302 u32 index;
303 u8 type;
Chunfeng Yun8158e912018-06-29 10:20:29 +0800304 int eye_src;
305 int eye_vrt;
306 int eye_term;
Chunfeng Yun410572e2020-02-11 11:21:12 +0800307 int intr;
Chunfeng Yun8be5a672020-01-08 09:52:01 +0800308 int discth;
Chunfeng Yund4f97f12018-06-29 10:20:30 +0800309 bool bc12_en;
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800310};
311
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800312struct mtk_tphy {
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800313 struct device *dev;
Chunfeng Yun04466ef2017-03-31 15:35:29 +0800314 void __iomem *sif_base; /* only shared sif */
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800315 const struct mtk_phy_pdata *pdata;
316 struct mtk_phy_instance **phys;
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800317 int nphys;
Chunfeng Yun8833ebf42018-03-12 13:25:39 +0800318 int src_ref_clk; /* MHZ, reference clock for slew rate calibrate */
319 int src_coef; /* coefficient for slew rate calibrate */
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800320};
321
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800322static void hs_slew_rate_calibrate(struct mtk_tphy *tphy,
323 struct mtk_phy_instance *instance)
Chunfeng Yun75f072f2015-12-04 10:11:05 +0800324{
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800325 struct u2phy_banks *u2_banks = &instance->u2_banks;
326 void __iomem *fmreg = u2_banks->fmreg;
327 void __iomem *com = u2_banks->com;
Chunfeng Yun75f072f2015-12-04 10:11:05 +0800328 int calibration_val;
329 int fm_out;
330 u32 tmp;
331
Chunfeng Yun8158e912018-06-29 10:20:29 +0800332 /* use force value */
333 if (instance->eye_src)
334 return;
335
Chunfeng Yun75f072f2015-12-04 10:11:05 +0800336 /* enable USB ring oscillator */
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800337 tmp = readl(com + U3P_USBPHYACR5);
Chunfeng Yun75f072f2015-12-04 10:11:05 +0800338 tmp |= PA5_RG_U2_HSTX_SRCAL_EN;
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800339 writel(tmp, com + U3P_USBPHYACR5);
Chunfeng Yun75f072f2015-12-04 10:11:05 +0800340 udelay(1);
341
342 /*enable free run clock */
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800343 tmp = readl(fmreg + U3P_U2FREQ_FMMONR1);
Chunfeng Yun75f072f2015-12-04 10:11:05 +0800344 tmp |= P2F_RG_FRCK_EN;
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800345 writel(tmp, fmreg + U3P_U2FREQ_FMMONR1);
Chunfeng Yun75f072f2015-12-04 10:11:05 +0800346
347 /* set cycle count as 1024, and select u2 channel */
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800348 tmp = readl(fmreg + U3P_U2FREQ_FMCR0);
Chunfeng Yun75f072f2015-12-04 10:11:05 +0800349 tmp &= ~(P2F_RG_CYCLECNT | P2F_RG_MONCLK_SEL);
350 tmp |= P2F_RG_CYCLECNT_VAL(U3P_FM_DET_CYCLE_CNT);
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800351 if (tphy->pdata->version == MTK_PHY_V1)
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800352 tmp |= P2F_RG_MONCLK_SEL_VAL(instance->index >> 1);
353
354 writel(tmp, fmreg + U3P_U2FREQ_FMCR0);
Chunfeng Yun75f072f2015-12-04 10:11:05 +0800355
356 /* enable frequency meter */
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800357 tmp = readl(fmreg + U3P_U2FREQ_FMCR0);
Chunfeng Yun75f072f2015-12-04 10:11:05 +0800358 tmp |= P2F_RG_FREQDET_EN;
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800359 writel(tmp, fmreg + U3P_U2FREQ_FMCR0);
Chunfeng Yun75f072f2015-12-04 10:11:05 +0800360
361 /* ignore return value */
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800362 readl_poll_timeout(fmreg + U3P_U2FREQ_FMMONR1, tmp,
363 (tmp & P2F_USB_FM_VALID), 10, 200);
Chunfeng Yun75f072f2015-12-04 10:11:05 +0800364
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800365 fm_out = readl(fmreg + U3P_U2FREQ_VALUE);
Chunfeng Yun75f072f2015-12-04 10:11:05 +0800366
367 /* disable frequency meter */
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800368 tmp = readl(fmreg + U3P_U2FREQ_FMCR0);
Chunfeng Yun75f072f2015-12-04 10:11:05 +0800369 tmp &= ~P2F_RG_FREQDET_EN;
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800370 writel(tmp, fmreg + U3P_U2FREQ_FMCR0);
Chunfeng Yun75f072f2015-12-04 10:11:05 +0800371
372 /*disable free run clock */
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800373 tmp = readl(fmreg + U3P_U2FREQ_FMMONR1);
Chunfeng Yun75f072f2015-12-04 10:11:05 +0800374 tmp &= ~P2F_RG_FRCK_EN;
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800375 writel(tmp, fmreg + U3P_U2FREQ_FMMONR1);
Chunfeng Yun75f072f2015-12-04 10:11:05 +0800376
377 if (fm_out) {
Chunfeng Yun8833ebf42018-03-12 13:25:39 +0800378 /* ( 1024 / FM_OUT ) x reference clock frequency x coef */
379 tmp = tphy->src_ref_clk * tphy->src_coef;
380 tmp = (tmp * U3P_FM_DET_CYCLE_CNT) / fm_out;
Chunfeng Yun75f072f2015-12-04 10:11:05 +0800381 calibration_val = DIV_ROUND_CLOSEST(tmp, U3P_SR_COEF_DIVISOR);
382 } else {
383 /* if FM detection fail, set default value */
384 calibration_val = 4;
385 }
Chunfeng Yun8833ebf42018-03-12 13:25:39 +0800386 dev_dbg(tphy->dev, "phy:%d, fm_out:%d, calib:%d (clk:%d, coef:%d)\n",
387 instance->index, fm_out, calibration_val,
388 tphy->src_ref_clk, tphy->src_coef);
Chunfeng Yun75f072f2015-12-04 10:11:05 +0800389
390 /* set HS slew rate */
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800391 tmp = readl(com + U3P_USBPHYACR5);
Chunfeng Yun75f072f2015-12-04 10:11:05 +0800392 tmp &= ~PA5_RG_U2_HSTX_SRCTRL;
393 tmp |= PA5_RG_U2_HSTX_SRCTRL_VAL(calibration_val);
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800394 writel(tmp, com + U3P_USBPHYACR5);
Chunfeng Yun75f072f2015-12-04 10:11:05 +0800395
396 /* disable USB ring oscillator */
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800397 tmp = readl(com + U3P_USBPHYACR5);
Chunfeng Yun75f072f2015-12-04 10:11:05 +0800398 tmp &= ~PA5_RG_U2_HSTX_SRCAL_EN;
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800399 writel(tmp, com + U3P_USBPHYACR5);
Chunfeng Yun75f072f2015-12-04 10:11:05 +0800400}
401
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800402static void u3_phy_instance_init(struct mtk_tphy *tphy,
403 struct mtk_phy_instance *instance)
Chunfeng Yun04466ef2017-03-31 15:35:29 +0800404{
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800405 struct u3phy_banks *u3_banks = &instance->u3_banks;
Chunfeng Yun04466ef2017-03-31 15:35:29 +0800406 u32 tmp;
407
408 /* gating PCIe Analog XTAL clock */
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800409 tmp = readl(u3_banks->spllc + U3P_SPLLC_XTALCTL3);
Chunfeng Yun04466ef2017-03-31 15:35:29 +0800410 tmp |= XC3_RG_U3_XTAL_RX_PWD | XC3_RG_U3_FRC_XTAL_RX_PWD;
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800411 writel(tmp, u3_banks->spllc + U3P_SPLLC_XTALCTL3);
Chunfeng Yun04466ef2017-03-31 15:35:29 +0800412
413 /* gating XSQ */
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800414 tmp = readl(u3_banks->phya + U3P_U3_PHYA_DA_REG0);
Chunfeng Yun04466ef2017-03-31 15:35:29 +0800415 tmp &= ~P3A_RG_XTAL_EXT_EN_U3;
416 tmp |= P3A_RG_XTAL_EXT_EN_U3_VAL(2);
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800417 writel(tmp, u3_banks->phya + U3P_U3_PHYA_DA_REG0);
Chunfeng Yun04466ef2017-03-31 15:35:29 +0800418
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800419 tmp = readl(u3_banks->phya + U3P_U3_PHYA_REG9);
Chunfeng Yun04466ef2017-03-31 15:35:29 +0800420 tmp &= ~P3A_RG_RX_DAC_MUX;
421 tmp |= P3A_RG_RX_DAC_MUX_VAL(4);
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800422 writel(tmp, u3_banks->phya + U3P_U3_PHYA_REG9);
Chunfeng Yun04466ef2017-03-31 15:35:29 +0800423
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800424 tmp = readl(u3_banks->phya + U3P_U3_PHYA_REG6);
Chunfeng Yun04466ef2017-03-31 15:35:29 +0800425 tmp &= ~P3A_RG_TX_EIDLE_CM;
426 tmp |= P3A_RG_TX_EIDLE_CM_VAL(0xe);
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800427 writel(tmp, u3_banks->phya + U3P_U3_PHYA_REG6);
Chunfeng Yun04466ef2017-03-31 15:35:29 +0800428
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800429 tmp = readl(u3_banks->phyd + U3P_U3_PHYD_CDR1);
Chunfeng Yun04466ef2017-03-31 15:35:29 +0800430 tmp &= ~(P3D_RG_CDR_BIR_LTD0 | P3D_RG_CDR_BIR_LTD1);
431 tmp |= P3D_RG_CDR_BIR_LTD0_VAL(0xc) | P3D_RG_CDR_BIR_LTD1_VAL(0x3);
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800432 writel(tmp, u3_banks->phyd + U3P_U3_PHYD_CDR1);
Chunfeng Yun04466ef2017-03-31 15:35:29 +0800433
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800434 tmp = readl(u3_banks->phyd + U3P_U3_PHYD_LFPS1);
Chunfeng Yun04466ef2017-03-31 15:35:29 +0800435 tmp &= ~P3D_RG_FWAKE_TH;
436 tmp |= P3D_RG_FWAKE_TH_VAL(0x34);
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800437 writel(tmp, u3_banks->phyd + U3P_U3_PHYD_LFPS1);
Chunfeng Yun04466ef2017-03-31 15:35:29 +0800438
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800439 tmp = readl(u3_banks->phyd + U3P_U3_PHYD_RXDET1);
Chunfeng Yun04466ef2017-03-31 15:35:29 +0800440 tmp &= ~P3D_RG_RXDET_STB2_SET;
441 tmp |= P3D_RG_RXDET_STB2_SET_VAL(0x10);
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800442 writel(tmp, u3_banks->phyd + U3P_U3_PHYD_RXDET1);
Chunfeng Yun04466ef2017-03-31 15:35:29 +0800443
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800444 tmp = readl(u3_banks->phyd + U3P_U3_PHYD_RXDET2);
Chunfeng Yun04466ef2017-03-31 15:35:29 +0800445 tmp &= ~P3D_RG_RXDET_STB2_SET_P3;
446 tmp |= P3D_RG_RXDET_STB2_SET_P3_VAL(0x10);
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800447 writel(tmp, u3_banks->phyd + U3P_U3_PHYD_RXDET2);
Chunfeng Yun04466ef2017-03-31 15:35:29 +0800448
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800449 dev_dbg(tphy->dev, "%s(%d)\n", __func__, instance->index);
Chunfeng Yun04466ef2017-03-31 15:35:29 +0800450}
451
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800452static void u2_phy_instance_init(struct mtk_tphy *tphy,
453 struct mtk_phy_instance *instance)
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800454{
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800455 struct u2phy_banks *u2_banks = &instance->u2_banks;
456 void __iomem *com = u2_banks->com;
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800457 u32 index = instance->index;
458 u32 tmp;
459
Chunfeng Yun00c00922017-12-07 19:53:34 +0800460 /* switch to USB function, and enable usb pll */
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800461 tmp = readl(com + U3P_U2PHYDTM0);
Chunfeng Yun00c00922017-12-07 19:53:34 +0800462 tmp &= ~(P2C_FORCE_UART_EN | P2C_FORCE_SUSPENDM);
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800463 tmp |= P2C_RG_XCVRSEL_VAL(1) | P2C_RG_DATAIN_VAL(0);
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800464 writel(tmp, com + U3P_U2PHYDTM0);
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800465
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800466 tmp = readl(com + U3P_U2PHYDTM1);
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800467 tmp &= ~P2C_RG_UART_EN;
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800468 writel(tmp, com + U3P_U2PHYDTM1);
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800469
Chunfeng Yunc0250fe2017-03-31 15:35:32 +0800470 tmp = readl(com + U3P_USBPHYACR0);
471 tmp |= PA0_RG_USB20_INTR_EN;
472 writel(tmp, com + U3P_USBPHYACR0);
473
474 /* disable switch 100uA current to SSUSB */
475 tmp = readl(com + U3P_USBPHYACR5);
476 tmp &= ~PA5_RG_U2_HS_100U_U3_EN;
477 writel(tmp, com + U3P_USBPHYACR5);
478
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800479 if (!index) {
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800480 tmp = readl(com + U3P_U2PHYACR4);
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800481 tmp &= ~P2C_U2_GPIO_CTR_MSK;
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800482 writel(tmp, com + U3P_U2PHYACR4);
Chunfeng Yune1d76532016-04-20 08:14:02 +0800483 }
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800484
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800485 if (tphy->pdata->avoid_rx_sen_degradation) {
Chunfeng Yune1d76532016-04-20 08:14:02 +0800486 if (!index) {
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800487 tmp = readl(com + U3P_USBPHYACR2);
Chunfeng Yune1d76532016-04-20 08:14:02 +0800488 tmp |= PA2_RG_SIF_U2PLL_FORCE_EN;
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800489 writel(tmp, com + U3P_USBPHYACR2);
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800490
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800491 tmp = readl(com + U3D_U2PHYDCR0);
Chunfeng Yune1d76532016-04-20 08:14:02 +0800492 tmp &= ~P2C_RG_SIF_U2PLL_FORCE_ON;
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800493 writel(tmp, com + U3D_U2PHYDCR0);
Chunfeng Yune1d76532016-04-20 08:14:02 +0800494 } else {
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800495 tmp = readl(com + U3D_U2PHYDCR0);
Chunfeng Yune1d76532016-04-20 08:14:02 +0800496 tmp |= P2C_RG_SIF_U2PLL_FORCE_ON;
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800497 writel(tmp, com + U3D_U2PHYDCR0);
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800498
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800499 tmp = readl(com + U3P_U2PHYDTM0);
Chunfeng Yune1d76532016-04-20 08:14:02 +0800500 tmp |= P2C_RG_SUSPENDM | P2C_FORCE_SUSPENDM;
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800501 writel(tmp, com + U3P_U2PHYDTM0);
Chunfeng Yune1d76532016-04-20 08:14:02 +0800502 }
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800503 }
504
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800505 tmp = readl(com + U3P_USBPHYACR6);
Chunfeng Yun43f53b12015-12-04 10:08:56 +0800506 tmp &= ~PA6_RG_U2_BC11_SW_EN; /* DP/DM BC1.1 path Disable */
507 tmp &= ~PA6_RG_U2_SQTH;
508 tmp |= PA6_RG_U2_SQTH_VAL(2);
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800509 writel(tmp, com + U3P_USBPHYACR6);
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800510
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800511 dev_dbg(tphy->dev, "%s(%d)\n", __func__, index);
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800512}
513
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800514static void u2_phy_instance_power_on(struct mtk_tphy *tphy,
515 struct mtk_phy_instance *instance)
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800516{
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800517 struct u2phy_banks *u2_banks = &instance->u2_banks;
518 void __iomem *com = u2_banks->com;
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800519 u32 index = instance->index;
520 u32 tmp;
521
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800522 tmp = readl(com + U3P_U2PHYDTM0);
Chunfeng Yun00c00922017-12-07 19:53:34 +0800523 tmp &= ~(P2C_RG_XCVRSEL | P2C_RG_DATAIN | P2C_DTM0_PART_MASK);
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800524 writel(tmp, com + U3P_U2PHYDTM0);
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800525
526 /* OTG Enable */
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800527 tmp = readl(com + U3P_USBPHYACR6);
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800528 tmp |= PA6_RG_U2_OTG_VBUSCMP_EN;
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800529 writel(tmp, com + U3P_USBPHYACR6);
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800530
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800531 tmp = readl(com + U3P_U2PHYDTM1);
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800532 tmp |= P2C_RG_VBUSVALID | P2C_RG_AVALID;
533 tmp &= ~P2C_RG_SESSEND;
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800534 writel(tmp, com + U3P_U2PHYDTM1);
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800535
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800536 if (tphy->pdata->avoid_rx_sen_degradation && index) {
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800537 tmp = readl(com + U3D_U2PHYDCR0);
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800538 tmp |= P2C_RG_SIF_U2PLL_FORCE_ON;
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800539 writel(tmp, com + U3D_U2PHYDCR0);
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800540
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800541 tmp = readl(com + U3P_U2PHYDTM0);
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800542 tmp |= P2C_RG_SUSPENDM | P2C_FORCE_SUSPENDM;
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800543 writel(tmp, com + U3P_U2PHYDTM0);
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800544 }
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800545 dev_dbg(tphy->dev, "%s(%d)\n", __func__, index);
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800546}
547
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800548static void u2_phy_instance_power_off(struct mtk_tphy *tphy,
549 struct mtk_phy_instance *instance)
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800550{
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800551 struct u2phy_banks *u2_banks = &instance->u2_banks;
552 void __iomem *com = u2_banks->com;
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800553 u32 index = instance->index;
554 u32 tmp;
555
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800556 tmp = readl(com + U3P_U2PHYDTM0);
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800557 tmp &= ~(P2C_RG_XCVRSEL | P2C_RG_DATAIN);
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800558 writel(tmp, com + U3P_U2PHYDTM0);
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800559
560 /* OTG Disable */
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800561 tmp = readl(com + U3P_USBPHYACR6);
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800562 tmp &= ~PA6_RG_U2_OTG_VBUSCMP_EN;
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800563 writel(tmp, com + U3P_USBPHYACR6);
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800564
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800565 tmp = readl(com + U3P_U2PHYDTM1);
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800566 tmp &= ~(P2C_RG_VBUSVALID | P2C_RG_AVALID);
567 tmp |= P2C_RG_SESSEND;
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800568 writel(tmp, com + U3P_U2PHYDTM1);
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800569
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800570 if (tphy->pdata->avoid_rx_sen_degradation && index) {
Chunfeng Yun00c00922017-12-07 19:53:34 +0800571 tmp = readl(com + U3P_U2PHYDTM0);
572 tmp &= ~(P2C_RG_SUSPENDM | P2C_FORCE_SUSPENDM);
573 writel(tmp, com + U3P_U2PHYDTM0);
574
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800575 tmp = readl(com + U3D_U2PHYDCR0);
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800576 tmp &= ~P2C_RG_SIF_U2PLL_FORCE_ON;
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800577 writel(tmp, com + U3D_U2PHYDCR0);
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800578 }
579
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800580 dev_dbg(tphy->dev, "%s(%d)\n", __func__, index);
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800581}
582
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800583static void u2_phy_instance_exit(struct mtk_tphy *tphy,
584 struct mtk_phy_instance *instance)
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800585{
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800586 struct u2phy_banks *u2_banks = &instance->u2_banks;
587 void __iomem *com = u2_banks->com;
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800588 u32 index = instance->index;
589 u32 tmp;
590
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800591 if (tphy->pdata->avoid_rx_sen_degradation && index) {
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800592 tmp = readl(com + U3D_U2PHYDCR0);
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800593 tmp &= ~P2C_RG_SIF_U2PLL_FORCE_ON;
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800594 writel(tmp, com + U3D_U2PHYDCR0);
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800595
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800596 tmp = readl(com + U3P_U2PHYDTM0);
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800597 tmp &= ~P2C_FORCE_SUSPENDM;
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800598 writel(tmp, com + U3P_U2PHYDTM0);
599 }
600}
601
Chunfeng Yun5954a102017-09-21 18:31:49 +0800602static void u2_phy_instance_set_mode(struct mtk_tphy *tphy,
603 struct mtk_phy_instance *instance,
604 enum phy_mode mode)
605{
606 struct u2phy_banks *u2_banks = &instance->u2_banks;
607 u32 tmp;
608
609 tmp = readl(u2_banks->com + U3P_U2PHYDTM1);
610 switch (mode) {
611 case PHY_MODE_USB_DEVICE:
612 tmp |= P2C_FORCE_IDDIG | P2C_RG_IDDIG;
613 break;
614 case PHY_MODE_USB_HOST:
615 tmp |= P2C_FORCE_IDDIG;
616 tmp &= ~P2C_RG_IDDIG;
617 break;
618 case PHY_MODE_USB_OTG:
619 tmp &= ~(P2C_FORCE_IDDIG | P2C_RG_IDDIG);
620 break;
621 default:
622 return;
623 }
624 writel(tmp, u2_banks->com + U3P_U2PHYDTM1);
625}
626
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800627static void pcie_phy_instance_init(struct mtk_tphy *tphy,
628 struct mtk_phy_instance *instance)
Ryder Lee44a6d6c2017-08-03 18:01:00 +0800629{
630 struct u3phy_banks *u3_banks = &instance->u3_banks;
631 u32 tmp;
632
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800633 if (tphy->pdata->version != MTK_PHY_V1)
Ryder Lee44a6d6c2017-08-03 18:01:00 +0800634 return;
635
636 tmp = readl(u3_banks->phya + U3P_U3_PHYA_DA_REG0);
637 tmp &= ~(P3A_RG_XTAL_EXT_PE1H | P3A_RG_XTAL_EXT_PE2H);
638 tmp |= P3A_RG_XTAL_EXT_PE1H_VAL(0x2) | P3A_RG_XTAL_EXT_PE2H_VAL(0x2);
639 writel(tmp, u3_banks->phya + U3P_U3_PHYA_DA_REG0);
640
641 /* ref clk drive */
642 tmp = readl(u3_banks->phya + U3P_U3_PHYA_REG1);
643 tmp &= ~P3A_RG_CLKDRV_AMP;
644 tmp |= P3A_RG_CLKDRV_AMP_VAL(0x4);
645 writel(tmp, u3_banks->phya + U3P_U3_PHYA_REG1);
646
647 tmp = readl(u3_banks->phya + U3P_U3_PHYA_REG0);
648 tmp &= ~P3A_RG_CLKDRV_OFF;
649 tmp |= P3A_RG_CLKDRV_OFF_VAL(0x1);
650 writel(tmp, u3_banks->phya + U3P_U3_PHYA_REG0);
651
652 /* SSC delta -5000ppm */
653 tmp = readl(u3_banks->phya + U3P_U3_PHYA_DA_REG20);
654 tmp &= ~P3A_RG_PLL_DELTA1_PE2H;
655 tmp |= P3A_RG_PLL_DELTA1_PE2H_VAL(0x3c);
656 writel(tmp, u3_banks->phya + U3P_U3_PHYA_DA_REG20);
657
658 tmp = readl(u3_banks->phya + U3P_U3_PHYA_DA_REG25);
659 tmp &= ~P3A_RG_PLL_DELTA_PE2H;
660 tmp |= P3A_RG_PLL_DELTA_PE2H_VAL(0x36);
661 writel(tmp, u3_banks->phya + U3P_U3_PHYA_DA_REG25);
662
663 /* change pll BW 0.6M */
664 tmp = readl(u3_banks->phya + U3P_U3_PHYA_DA_REG5);
665 tmp &= ~(P3A_RG_PLL_BR_PE2H | P3A_RG_PLL_IC_PE2H);
666 tmp |= P3A_RG_PLL_BR_PE2H_VAL(0x1) | P3A_RG_PLL_IC_PE2H_VAL(0x1);
667 writel(tmp, u3_banks->phya + U3P_U3_PHYA_DA_REG5);
668
669 tmp = readl(u3_banks->phya + U3P_U3_PHYA_DA_REG4);
670 tmp &= ~(P3A_RG_PLL_DIVEN_PE2H | P3A_RG_PLL_BC_PE2H);
671 tmp |= P3A_RG_PLL_BC_PE2H_VAL(0x3);
672 writel(tmp, u3_banks->phya + U3P_U3_PHYA_DA_REG4);
673
674 tmp = readl(u3_banks->phya + U3P_U3_PHYA_DA_REG6);
675 tmp &= ~P3A_RG_PLL_IR_PE2H;
676 tmp |= P3A_RG_PLL_IR_PE2H_VAL(0x2);
677 writel(tmp, u3_banks->phya + U3P_U3_PHYA_DA_REG6);
678
679 tmp = readl(u3_banks->phya + U3P_U3_PHYA_DA_REG7);
680 tmp &= ~P3A_RG_PLL_BP_PE2H;
681 tmp |= P3A_RG_PLL_BP_PE2H_VAL(0xa);
682 writel(tmp, u3_banks->phya + U3P_U3_PHYA_DA_REG7);
683
684 /* Tx Detect Rx Timing: 10us -> 5us */
685 tmp = readl(u3_banks->phyd + U3P_U3_PHYD_RXDET1);
686 tmp &= ~P3D_RG_RXDET_STB2_SET;
687 tmp |= P3D_RG_RXDET_STB2_SET_VAL(0x10);
688 writel(tmp, u3_banks->phyd + U3P_U3_PHYD_RXDET1);
689
690 tmp = readl(u3_banks->phyd + U3P_U3_PHYD_RXDET2);
691 tmp &= ~P3D_RG_RXDET_STB2_SET_P3;
692 tmp |= P3D_RG_RXDET_STB2_SET_P3_VAL(0x10);
693 writel(tmp, u3_banks->phyd + U3P_U3_PHYD_RXDET2);
694
695 /* wait for PCIe subsys register to active */
696 usleep_range(2500, 3000);
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800697 dev_dbg(tphy->dev, "%s(%d)\n", __func__, instance->index);
Ryder Lee44a6d6c2017-08-03 18:01:00 +0800698}
699
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800700static void pcie_phy_instance_power_on(struct mtk_tphy *tphy,
701 struct mtk_phy_instance *instance)
Ryder Lee44a6d6c2017-08-03 18:01:00 +0800702{
703 struct u3phy_banks *bank = &instance->u3_banks;
704 u32 tmp;
705
706 tmp = readl(bank->chip + U3P_U3_CHIP_GPIO_CTLD);
Chunfeng Yun40363252018-03-12 13:25:38 +0800707 tmp &= ~(P3C_FORCE_IP_SW_RST | P3C_REG_IP_SW_RST);
Ryder Lee44a6d6c2017-08-03 18:01:00 +0800708 writel(tmp, bank->chip + U3P_U3_CHIP_GPIO_CTLD);
709
710 tmp = readl(bank->chip + U3P_U3_CHIP_GPIO_CTLE);
711 tmp &= ~(P3C_RG_SWRST_U3_PHYD_FORCE_EN | P3C_RG_SWRST_U3_PHYD);
712 writel(tmp, bank->chip + U3P_U3_CHIP_GPIO_CTLE);
713}
714
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800715static void pcie_phy_instance_power_off(struct mtk_tphy *tphy,
716 struct mtk_phy_instance *instance)
Ryder Lee44a6d6c2017-08-03 18:01:00 +0800717
718{
719 struct u3phy_banks *bank = &instance->u3_banks;
720 u32 tmp;
721
722 tmp = readl(bank->chip + U3P_U3_CHIP_GPIO_CTLD);
723 tmp |= P3C_FORCE_IP_SW_RST | P3C_REG_IP_SW_RST;
724 writel(tmp, bank->chip + U3P_U3_CHIP_GPIO_CTLD);
725
726 tmp = readl(bank->chip + U3P_U3_CHIP_GPIO_CTLE);
727 tmp |= P3C_RG_SWRST_U3_PHYD_FORCE_EN | P3C_RG_SWRST_U3_PHYD;
728 writel(tmp, bank->chip + U3P_U3_CHIP_GPIO_CTLE);
729}
730
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800731static void sata_phy_instance_init(struct mtk_tphy *tphy,
732 struct mtk_phy_instance *instance)
Ryder Lee4ab26cb2017-08-03 18:01:01 +0800733{
734 struct u3phy_banks *u3_banks = &instance->u3_banks;
735 void __iomem *phyd = u3_banks->phyd;
736 u32 tmp;
737
738 /* charge current adjustment */
739 tmp = readl(phyd + ANA_RG_CTRL_SIGNAL6);
740 tmp &= ~(RG_CDR_BIRLTR_GEN1_MSK | RG_CDR_BC_GEN1_MSK);
741 tmp |= RG_CDR_BIRLTR_GEN1_VAL(0x6) | RG_CDR_BC_GEN1_VAL(0x1a);
742 writel(tmp, phyd + ANA_RG_CTRL_SIGNAL6);
743
744 tmp = readl(phyd + ANA_EQ_EYE_CTRL_SIGNAL4);
745 tmp &= ~RG_CDR_BIRLTD0_GEN1_MSK;
746 tmp |= RG_CDR_BIRLTD0_GEN1_VAL(0x18);
747 writel(tmp, phyd + ANA_EQ_EYE_CTRL_SIGNAL4);
748
749 tmp = readl(phyd + ANA_EQ_EYE_CTRL_SIGNAL5);
750 tmp &= ~RG_CDR_BIRLTD0_GEN3_MSK;
751 tmp |= RG_CDR_BIRLTD0_GEN3_VAL(0x06);
752 writel(tmp, phyd + ANA_EQ_EYE_CTRL_SIGNAL5);
753
754 tmp = readl(phyd + ANA_RG_CTRL_SIGNAL4);
755 tmp &= ~(RG_CDR_BICLTR_GEN1_MSK | RG_CDR_BR_GEN2_MSK);
756 tmp |= RG_CDR_BICLTR_GEN1_VAL(0x0c) | RG_CDR_BR_GEN2_VAL(0x07);
757 writel(tmp, phyd + ANA_RG_CTRL_SIGNAL4);
758
759 tmp = readl(phyd + PHYD_CTRL_SIGNAL_MODE4);
760 tmp &= ~(RG_CDR_BICLTD0_GEN1_MSK | RG_CDR_BICLTD1_GEN1_MSK);
761 tmp |= RG_CDR_BICLTD0_GEN1_VAL(0x08) | RG_CDR_BICLTD1_GEN1_VAL(0x02);
762 writel(tmp, phyd + PHYD_CTRL_SIGNAL_MODE4);
763
764 tmp = readl(phyd + PHYD_DESIGN_OPTION2);
765 tmp &= ~RG_LOCK_CNT_SEL_MSK;
766 tmp |= RG_LOCK_CNT_SEL_VAL(0x02);
767 writel(tmp, phyd + PHYD_DESIGN_OPTION2);
768
769 tmp = readl(phyd + PHYD_DESIGN_OPTION9);
770 tmp &= ~(RG_T2_MIN_MSK | RG_TG_MIN_MSK |
771 RG_T2_MAX_MSK | RG_TG_MAX_MSK);
772 tmp |= RG_T2_MIN_VAL(0x12) | RG_TG_MIN_VAL(0x04) |
773 RG_T2_MAX_VAL(0x31) | RG_TG_MAX_VAL(0x0e);
774 writel(tmp, phyd + PHYD_DESIGN_OPTION9);
775
776 tmp = readl(phyd + ANA_RG_CTRL_SIGNAL1);
777 tmp &= ~RG_IDRV_0DB_GEN1_MSK;
778 tmp |= RG_IDRV_0DB_GEN1_VAL(0x20);
779 writel(tmp, phyd + ANA_RG_CTRL_SIGNAL1);
780
781 tmp = readl(phyd + ANA_EQ_EYE_CTRL_SIGNAL1);
782 tmp &= ~RG_EQ_DLEQ_LFI_GEN1_MSK;
783 tmp |= RG_EQ_DLEQ_LFI_GEN1_VAL(0x03);
784 writel(tmp, phyd + ANA_EQ_EYE_CTRL_SIGNAL1);
785
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800786 dev_dbg(tphy->dev, "%s(%d)\n", __func__, instance->index);
Ryder Lee4ab26cb2017-08-03 18:01:01 +0800787}
788
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800789static void phy_v1_banks_init(struct mtk_tphy *tphy,
790 struct mtk_phy_instance *instance)
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800791{
792 struct u2phy_banks *u2_banks = &instance->u2_banks;
793 struct u3phy_banks *u3_banks = &instance->u3_banks;
794
Ryder Lee44a6d6c2017-08-03 18:01:00 +0800795 switch (instance->type) {
796 case PHY_TYPE_USB2:
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800797 u2_banks->misc = NULL;
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800798 u2_banks->fmreg = tphy->sif_base + SSUSB_SIFSLV_V1_U2FREQ;
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800799 u2_banks->com = instance->port_base + SSUSB_SIFSLV_V1_U2PHY_COM;
Ryder Lee44a6d6c2017-08-03 18:01:00 +0800800 break;
801 case PHY_TYPE_USB3:
802 case PHY_TYPE_PCIE:
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800803 u3_banks->spllc = tphy->sif_base + SSUSB_SIFSLV_V1_SPLLC;
Chunfeng Yun554a56f2017-09-21 18:31:48 +0800804 u3_banks->chip = tphy->sif_base + SSUSB_SIFSLV_V1_CHIP;
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800805 u3_banks->phyd = instance->port_base + SSUSB_SIFSLV_V1_U3PHYD;
806 u3_banks->phya = instance->port_base + SSUSB_SIFSLV_V1_U3PHYA;
Ryder Lee44a6d6c2017-08-03 18:01:00 +0800807 break;
Ryder Lee4ab26cb2017-08-03 18:01:01 +0800808 case PHY_TYPE_SATA:
809 u3_banks->phyd = instance->port_base + SSUSB_SIFSLV_V1_U3PHYD;
810 break;
Ryder Lee44a6d6c2017-08-03 18:01:00 +0800811 default:
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800812 dev_err(tphy->dev, "incompatible PHY type\n");
Ryder Lee44a6d6c2017-08-03 18:01:00 +0800813 return;
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800814 }
815}
816
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800817static void phy_v2_banks_init(struct mtk_tphy *tphy,
818 struct mtk_phy_instance *instance)
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800819{
820 struct u2phy_banks *u2_banks = &instance->u2_banks;
821 struct u3phy_banks *u3_banks = &instance->u3_banks;
822
Ryder Lee44a6d6c2017-08-03 18:01:00 +0800823 switch (instance->type) {
824 case PHY_TYPE_USB2:
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800825 u2_banks->misc = instance->port_base + SSUSB_SIFSLV_V2_MISC;
826 u2_banks->fmreg = instance->port_base + SSUSB_SIFSLV_V2_U2FREQ;
827 u2_banks->com = instance->port_base + SSUSB_SIFSLV_V2_U2PHY_COM;
Ryder Lee44a6d6c2017-08-03 18:01:00 +0800828 break;
829 case PHY_TYPE_USB3:
830 case PHY_TYPE_PCIE:
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800831 u3_banks->spllc = instance->port_base + SSUSB_SIFSLV_V2_SPLLC;
832 u3_banks->chip = instance->port_base + SSUSB_SIFSLV_V2_CHIP;
833 u3_banks->phyd = instance->port_base + SSUSB_SIFSLV_V2_U3PHYD;
834 u3_banks->phya = instance->port_base + SSUSB_SIFSLV_V2_U3PHYA;
Ryder Lee44a6d6c2017-08-03 18:01:00 +0800835 break;
836 default:
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800837 dev_err(tphy->dev, "incompatible PHY type\n");
Ryder Lee44a6d6c2017-08-03 18:01:00 +0800838 return;
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800839 }
840}
841
Chunfeng Yun8158e912018-06-29 10:20:29 +0800842static void phy_parse_property(struct mtk_tphy *tphy,
843 struct mtk_phy_instance *instance)
844{
845 struct device *dev = &instance->phy->dev;
846
847 if (instance->type != PHY_TYPE_USB2)
848 return;
849
Chunfeng Yund4f97f12018-06-29 10:20:30 +0800850 instance->bc12_en = device_property_read_bool(dev, "mediatek,bc12");
Chunfeng Yun8158e912018-06-29 10:20:29 +0800851 device_property_read_u32(dev, "mediatek,eye-src",
852 &instance->eye_src);
853 device_property_read_u32(dev, "mediatek,eye-vrt",
854 &instance->eye_vrt);
855 device_property_read_u32(dev, "mediatek,eye-term",
856 &instance->eye_term);
Chunfeng Yun410572e2020-02-11 11:21:12 +0800857 device_property_read_u32(dev, "mediatek,intr",
858 &instance->intr);
Chunfeng Yun8be5a672020-01-08 09:52:01 +0800859 device_property_read_u32(dev, "mediatek,discth",
860 &instance->discth);
Chunfeng Yun410572e2020-02-11 11:21:12 +0800861 dev_dbg(dev, "bc12:%d, src:%d, vrt:%d, term:%d, intr:%d, disc:%d\n",
Chunfeng Yund4f97f12018-06-29 10:20:30 +0800862 instance->bc12_en, instance->eye_src,
Chunfeng Yun8be5a672020-01-08 09:52:01 +0800863 instance->eye_vrt, instance->eye_term,
Chunfeng Yun410572e2020-02-11 11:21:12 +0800864 instance->intr, instance->discth);
Chunfeng Yun8158e912018-06-29 10:20:29 +0800865}
866
867static void u2_phy_props_set(struct mtk_tphy *tphy,
868 struct mtk_phy_instance *instance)
869{
870 struct u2phy_banks *u2_banks = &instance->u2_banks;
871 void __iomem *com = u2_banks->com;
872 u32 tmp;
873
Chunfeng Yund4f97f12018-06-29 10:20:30 +0800874 if (instance->bc12_en) {
875 tmp = readl(com + U3P_U2PHYBC12C);
876 tmp |= P2C_RG_CHGDT_EN; /* BC1.2 path Enable */
877 writel(tmp, com + U3P_U2PHYBC12C);
878 }
Chunfeng Yun8158e912018-06-29 10:20:29 +0800879
880 if (instance->eye_src) {
881 tmp = readl(com + U3P_USBPHYACR5);
882 tmp &= ~PA5_RG_U2_HSTX_SRCTRL;
883 tmp |= PA5_RG_U2_HSTX_SRCTRL_VAL(instance->eye_src);
884 writel(tmp, com + U3P_USBPHYACR5);
885 }
886
887 if (instance->eye_vrt) {
888 tmp = readl(com + U3P_USBPHYACR1);
889 tmp &= ~PA1_RG_VRT_SEL;
890 tmp |= PA1_RG_VRT_SEL_VAL(instance->eye_vrt);
891 writel(tmp, com + U3P_USBPHYACR1);
892 }
893
894 if (instance->eye_term) {
895 tmp = readl(com + U3P_USBPHYACR1);
896 tmp &= ~PA1_RG_TERM_SEL;
897 tmp |= PA1_RG_TERM_SEL_VAL(instance->eye_term);
898 writel(tmp, com + U3P_USBPHYACR1);
899 }
Chunfeng Yun8be5a672020-01-08 09:52:01 +0800900
Chunfeng Yun410572e2020-02-11 11:21:12 +0800901 if (instance->intr) {
902 tmp = readl(com + U3P_USBPHYACR1);
903 tmp &= ~PA1_RG_INTR_CAL;
904 tmp |= PA1_RG_INTR_CAL_VAL(instance->intr);
905 writel(tmp, com + U3P_USBPHYACR1);
906 }
907
Chunfeng Yun8be5a672020-01-08 09:52:01 +0800908 if (instance->discth) {
909 tmp = readl(com + U3P_USBPHYACR6);
910 tmp &= ~PA6_RG_U2_DISCTH;
911 tmp |= PA6_RG_U2_DISCTH_VAL(instance->discth);
912 writel(tmp, com + U3P_USBPHYACR6);
913 }
Chunfeng Yun8158e912018-06-29 10:20:29 +0800914}
915
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800916static int mtk_phy_init(struct phy *phy)
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800917{
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800918 struct mtk_phy_instance *instance = phy_get_drvdata(phy);
919 struct mtk_tphy *tphy = dev_get_drvdata(phy->dev.parent);
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800920 int ret;
921
Chunfeng Yun15de15c2017-03-31 15:35:30 +0800922 ret = clk_prepare_enable(instance->ref_clk);
923 if (ret) {
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800924 dev_err(tphy->dev, "failed to enable ref_clk\n");
Chunfeng Yun15de15c2017-03-31 15:35:30 +0800925 return ret;
926 }
927
Ryder Lee44a6d6c2017-08-03 18:01:00 +0800928 switch (instance->type) {
929 case PHY_TYPE_USB2:
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800930 u2_phy_instance_init(tphy, instance);
Chunfeng Yun8158e912018-06-29 10:20:29 +0800931 u2_phy_props_set(tphy, instance);
Ryder Lee44a6d6c2017-08-03 18:01:00 +0800932 break;
933 case PHY_TYPE_USB3:
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800934 u3_phy_instance_init(tphy, instance);
Ryder Lee44a6d6c2017-08-03 18:01:00 +0800935 break;
936 case PHY_TYPE_PCIE:
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800937 pcie_phy_instance_init(tphy, instance);
Ryder Lee44a6d6c2017-08-03 18:01:00 +0800938 break;
Ryder Lee4ab26cb2017-08-03 18:01:01 +0800939 case PHY_TYPE_SATA:
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800940 sata_phy_instance_init(tphy, instance);
Ryder Lee4ab26cb2017-08-03 18:01:01 +0800941 break;
Ryder Lee44a6d6c2017-08-03 18:01:00 +0800942 default:
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800943 dev_err(tphy->dev, "incompatible PHY type\n");
Ryder Lee44a6d6c2017-08-03 18:01:00 +0800944 return -EINVAL;
945 }
Chunfeng Yun04466ef2017-03-31 15:35:29 +0800946
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800947 return 0;
948}
949
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800950static int mtk_phy_power_on(struct phy *phy)
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800951{
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800952 struct mtk_phy_instance *instance = phy_get_drvdata(phy);
953 struct mtk_tphy *tphy = dev_get_drvdata(phy->dev.parent);
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800954
Chunfeng Yun04466ef2017-03-31 15:35:29 +0800955 if (instance->type == PHY_TYPE_USB2) {
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800956 u2_phy_instance_power_on(tphy, instance);
957 hs_slew_rate_calibrate(tphy, instance);
Ryder Lee44a6d6c2017-08-03 18:01:00 +0800958 } else if (instance->type == PHY_TYPE_PCIE) {
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800959 pcie_phy_instance_power_on(tphy, instance);
Chunfeng Yun04466ef2017-03-31 15:35:29 +0800960 }
Ryder Lee44a6d6c2017-08-03 18:01:00 +0800961
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800962 return 0;
963}
964
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800965static int mtk_phy_power_off(struct phy *phy)
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800966{
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800967 struct mtk_phy_instance *instance = phy_get_drvdata(phy);
968 struct mtk_tphy *tphy = dev_get_drvdata(phy->dev.parent);
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800969
Chunfeng Yun04466ef2017-03-31 15:35:29 +0800970 if (instance->type == PHY_TYPE_USB2)
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800971 u2_phy_instance_power_off(tphy, instance);
Ryder Lee44a6d6c2017-08-03 18:01:00 +0800972 else if (instance->type == PHY_TYPE_PCIE)
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800973 pcie_phy_instance_power_off(tphy, instance);
Chunfeng Yun04466ef2017-03-31 15:35:29 +0800974
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800975 return 0;
976}
977
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800978static int mtk_phy_exit(struct phy *phy)
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800979{
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800980 struct mtk_phy_instance *instance = phy_get_drvdata(phy);
981 struct mtk_tphy *tphy = dev_get_drvdata(phy->dev.parent);
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800982
Chunfeng Yun04466ef2017-03-31 15:35:29 +0800983 if (instance->type == PHY_TYPE_USB2)
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800984 u2_phy_instance_exit(tphy, instance);
Chunfeng Yun04466ef2017-03-31 15:35:29 +0800985
Chunfeng Yun15de15c2017-03-31 15:35:30 +0800986 clk_disable_unprepare(instance->ref_clk);
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800987 return 0;
988}
989
Grygorii Strashko79a5a182018-11-19 19:24:20 -0600990static int mtk_phy_set_mode(struct phy *phy, enum phy_mode mode, int submode)
Chunfeng Yun5954a102017-09-21 18:31:49 +0800991{
992 struct mtk_phy_instance *instance = phy_get_drvdata(phy);
993 struct mtk_tphy *tphy = dev_get_drvdata(phy->dev.parent);
994
995 if (instance->type == PHY_TYPE_USB2)
996 u2_phy_instance_set_mode(tphy, instance, mode);
997
998 return 0;
999}
1000
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +08001001static struct phy *mtk_phy_xlate(struct device *dev,
Chunfeng Yundc7f1902015-09-29 11:01:36 +08001002 struct of_phandle_args *args)
1003{
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +08001004 struct mtk_tphy *tphy = dev_get_drvdata(dev);
1005 struct mtk_phy_instance *instance = NULL;
Chunfeng Yundc7f1902015-09-29 11:01:36 +08001006 struct device_node *phy_np = args->np;
1007 int index;
1008
Chunfeng Yundc7f1902015-09-29 11:01:36 +08001009 if (args->args_count != 1) {
1010 dev_err(dev, "invalid number of cells in 'phy' property\n");
1011 return ERR_PTR(-EINVAL);
1012 }
1013
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +08001014 for (index = 0; index < tphy->nphys; index++)
1015 if (phy_np == tphy->phys[index]->phy->dev.of_node) {
1016 instance = tphy->phys[index];
Chunfeng Yundc7f1902015-09-29 11:01:36 +08001017 break;
1018 }
1019
1020 if (!instance) {
1021 dev_err(dev, "failed to find appropriate phy\n");
1022 return ERR_PTR(-EINVAL);
1023 }
1024
1025 instance->type = args->args[0];
Chunfeng Yundc7f1902015-09-29 11:01:36 +08001026 if (!(instance->type == PHY_TYPE_USB2 ||
Ryder Lee44a6d6c2017-08-03 18:01:00 +08001027 instance->type == PHY_TYPE_USB3 ||
Ryder Lee4ab26cb2017-08-03 18:01:01 +08001028 instance->type == PHY_TYPE_PCIE ||
1029 instance->type == PHY_TYPE_SATA)) {
Chunfeng Yundc7f1902015-09-29 11:01:36 +08001030 dev_err(dev, "unsupported device type: %d\n", instance->type);
1031 return ERR_PTR(-EINVAL);
1032 }
1033
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +08001034 if (tphy->pdata->version == MTK_PHY_V1) {
1035 phy_v1_banks_init(tphy, instance);
1036 } else if (tphy->pdata->version == MTK_PHY_V2) {
1037 phy_v2_banks_init(tphy, instance);
Chunfeng Yun8d6e19572017-03-31 15:35:31 +08001038 } else {
1039 dev_err(dev, "phy version is not supported\n");
1040 return ERR_PTR(-EINVAL);
1041 }
1042
Chunfeng Yun8158e912018-06-29 10:20:29 +08001043 phy_parse_property(tphy, instance);
1044
Chunfeng Yundc7f1902015-09-29 11:01:36 +08001045 return instance->phy;
1046}
1047
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +08001048static const struct phy_ops mtk_tphy_ops = {
1049 .init = mtk_phy_init,
1050 .exit = mtk_phy_exit,
1051 .power_on = mtk_phy_power_on,
1052 .power_off = mtk_phy_power_off,
Chunfeng Yun5954a102017-09-21 18:31:49 +08001053 .set_mode = mtk_phy_set_mode,
Chunfeng Yundc7f1902015-09-29 11:01:36 +08001054 .owner = THIS_MODULE,
1055};
1056
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +08001057static const struct mtk_phy_pdata tphy_v1_pdata = {
Chunfeng Yune1d76532016-04-20 08:14:02 +08001058 .avoid_rx_sen_degradation = false,
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +08001059 .version = MTK_PHY_V1,
Chunfeng Yun8d6e19572017-03-31 15:35:31 +08001060};
1061
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +08001062static const struct mtk_phy_pdata tphy_v2_pdata = {
Chunfeng Yun8d6e19572017-03-31 15:35:31 +08001063 .avoid_rx_sen_degradation = false,
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +08001064 .version = MTK_PHY_V2,
Chunfeng Yune1d76532016-04-20 08:14:02 +08001065};
1066
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +08001067static const struct mtk_phy_pdata mt8173_pdata = {
Chunfeng Yune1d76532016-04-20 08:14:02 +08001068 .avoid_rx_sen_degradation = true,
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +08001069 .version = MTK_PHY_V1,
Chunfeng Yune1d76532016-04-20 08:14:02 +08001070};
1071
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +08001072static const struct of_device_id mtk_tphy_id_table[] = {
Ryder Lee44a6d6c2017-08-03 18:01:00 +08001073 { .compatible = "mediatek,mt2701-u3phy", .data = &tphy_v1_pdata },
Ryder Lee4ab26cb2017-08-03 18:01:01 +08001074 { .compatible = "mediatek,mt2712-u3phy", .data = &tphy_v2_pdata },
Chunfeng Yune1d76532016-04-20 08:14:02 +08001075 { .compatible = "mediatek,mt8173-u3phy", .data = &mt8173_pdata },
Ryder Lee44a6d6c2017-08-03 18:01:00 +08001076 { .compatible = "mediatek,generic-tphy-v1", .data = &tphy_v1_pdata },
Ryder Lee4ab26cb2017-08-03 18:01:01 +08001077 { .compatible = "mediatek,generic-tphy-v2", .data = &tphy_v2_pdata },
Chunfeng Yune1d76532016-04-20 08:14:02 +08001078 { },
1079};
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +08001080MODULE_DEVICE_TABLE(of, mtk_tphy_id_table);
Chunfeng Yune1d76532016-04-20 08:14:02 +08001081
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +08001082static int mtk_tphy_probe(struct platform_device *pdev)
Chunfeng Yundc7f1902015-09-29 11:01:36 +08001083{
1084 struct device *dev = &pdev->dev;
1085 struct device_node *np = dev->of_node;
1086 struct device_node *child_np;
1087 struct phy_provider *provider;
1088 struct resource *sif_res;
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +08001089 struct mtk_tphy *tphy;
Chunfeng Yundc7f1902015-09-29 11:01:36 +08001090 struct resource res;
Julia Lawall2bb80cc2015-11-16 12:33:15 +01001091 int port, retval;
Chunfeng Yundc7f1902015-09-29 11:01:36 +08001092
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +08001093 tphy = devm_kzalloc(dev, sizeof(*tphy), GFP_KERNEL);
1094 if (!tphy)
Chunfeng Yundc7f1902015-09-29 11:01:36 +08001095 return -ENOMEM;
1096
Chunfeng Yune4b227c2017-12-28 16:40:36 +05301097 tphy->pdata = of_device_get_match_data(dev);
1098 if (!tphy->pdata)
1099 return -EINVAL;
1100
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +08001101 tphy->nphys = of_get_child_count(np);
1102 tphy->phys = devm_kcalloc(dev, tphy->nphys,
1103 sizeof(*tphy->phys), GFP_KERNEL);
1104 if (!tphy->phys)
Chunfeng Yundc7f1902015-09-29 11:01:36 +08001105 return -ENOMEM;
1106
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +08001107 tphy->dev = dev;
1108 platform_set_drvdata(pdev, tphy);
Chunfeng Yundc7f1902015-09-29 11:01:36 +08001109
Chunfeng Yun93a04f42017-12-07 19:53:35 +08001110 sif_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1111 /* SATA phy of V1 needn't it if not shared with PCIe or USB */
1112 if (sif_res && tphy->pdata->version == MTK_PHY_V1) {
Chunfeng Yun8d6e19572017-03-31 15:35:31 +08001113 /* get banks shared by multiple phys */
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +08001114 tphy->sif_base = devm_ioremap_resource(dev, sif_res);
1115 if (IS_ERR(tphy->sif_base)) {
Chunfeng Yun8d6e19572017-03-31 15:35:31 +08001116 dev_err(dev, "failed to remap sif regs\n");
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +08001117 return PTR_ERR(tphy->sif_base);
Chunfeng Yun8d6e19572017-03-31 15:35:31 +08001118 }
Chunfeng Yundc7f1902015-09-29 11:01:36 +08001119 }
1120
Chunfeng Yun8833ebf42018-03-12 13:25:39 +08001121 tphy->src_ref_clk = U3P_REF_CLK;
1122 tphy->src_coef = U3P_SLEW_RATE_COEF;
1123 /* update parameters of slew rate calibrate if exist */
1124 device_property_read_u32(dev, "mediatek,src-ref-clk-mhz",
1125 &tphy->src_ref_clk);
1126 device_property_read_u32(dev, "mediatek,src-coef", &tphy->src_coef);
1127
Chunfeng Yundc7f1902015-09-29 11:01:36 +08001128 port = 0;
1129 for_each_child_of_node(np, child_np) {
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +08001130 struct mtk_phy_instance *instance;
Chunfeng Yundc7f1902015-09-29 11:01:36 +08001131 struct phy *phy;
Chunfeng Yundc7f1902015-09-29 11:01:36 +08001132
1133 instance = devm_kzalloc(dev, sizeof(*instance), GFP_KERNEL);
Julia Lawall2bb80cc2015-11-16 12:33:15 +01001134 if (!instance) {
1135 retval = -ENOMEM;
1136 goto put_child;
1137 }
Chunfeng Yundc7f1902015-09-29 11:01:36 +08001138
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +08001139 tphy->phys[port] = instance;
Chunfeng Yundc7f1902015-09-29 11:01:36 +08001140
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +08001141 phy = devm_phy_create(dev, child_np, &mtk_tphy_ops);
Chunfeng Yundc7f1902015-09-29 11:01:36 +08001142 if (IS_ERR(phy)) {
1143 dev_err(dev, "failed to create phy\n");
Julia Lawall2bb80cc2015-11-16 12:33:15 +01001144 retval = PTR_ERR(phy);
1145 goto put_child;
Chunfeng Yundc7f1902015-09-29 11:01:36 +08001146 }
1147
1148 retval = of_address_to_resource(child_np, 0, &res);
1149 if (retval) {
1150 dev_err(dev, "failed to get address resource(id-%d)\n",
1151 port);
Julia Lawall2bb80cc2015-11-16 12:33:15 +01001152 goto put_child;
Chunfeng Yundc7f1902015-09-29 11:01:36 +08001153 }
1154
1155 instance->port_base = devm_ioremap_resource(&phy->dev, &res);
1156 if (IS_ERR(instance->port_base)) {
1157 dev_err(dev, "failed to remap phy regs\n");
Julia Lawall2bb80cc2015-11-16 12:33:15 +01001158 retval = PTR_ERR(instance->port_base);
1159 goto put_child;
Chunfeng Yundc7f1902015-09-29 11:01:36 +08001160 }
1161
1162 instance->phy = phy;
1163 instance->index = port;
1164 phy_set_drvdata(phy, instance);
1165 port++;
Chunfeng Yun15de15c2017-03-31 15:35:30 +08001166
Chunfeng Yun657a9ed2020-02-11 11:21:13 +08001167 instance->ref_clk = devm_clk_get_optional(&phy->dev, "ref");
Chunfeng Yun15de15c2017-03-31 15:35:30 +08001168 if (IS_ERR(instance->ref_clk)) {
1169 dev_err(dev, "failed to get ref_clk(id-%d)\n", port);
1170 retval = PTR_ERR(instance->ref_clk);
1171 goto put_child;
1172 }
Chunfeng Yundc7f1902015-09-29 11:01:36 +08001173 }
1174
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +08001175 provider = devm_of_phy_provider_register(dev, mtk_phy_xlate);
Chunfeng Yundc7f1902015-09-29 11:01:36 +08001176
1177 return PTR_ERR_OR_ZERO(provider);
Julia Lawall2bb80cc2015-11-16 12:33:15 +01001178put_child:
1179 of_node_put(child_np);
1180 return retval;
Chunfeng Yundc7f1902015-09-29 11:01:36 +08001181}
1182
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +08001183static struct platform_driver mtk_tphy_driver = {
1184 .probe = mtk_tphy_probe,
Chunfeng Yundc7f1902015-09-29 11:01:36 +08001185 .driver = {
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +08001186 .name = "mtk-tphy",
1187 .of_match_table = mtk_tphy_id_table,
Chunfeng Yundc7f1902015-09-29 11:01:36 +08001188 },
1189};
1190
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +08001191module_platform_driver(mtk_tphy_driver);
Chunfeng Yundc7f1902015-09-29 11:01:36 +08001192
1193MODULE_AUTHOR("Chunfeng Yun <chunfeng.yun@mediatek.com>");
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +08001194MODULE_DESCRIPTION("MediaTek T-PHY driver");
Chunfeng Yundc7f1902015-09-29 11:01:36 +08001195MODULE_LICENSE("GPL v2");