blob: 6073c25eb3f299b5bf01d07a3b97b415986647d3 [file] [log] [blame]
Chunfeng Yundc7f1902015-09-29 11:01:36 +08001/*
2 * Copyright (c) 2015 MediaTek Inc.
3 * Author: Chunfeng Yun <chunfeng.yun@mediatek.com>
4 *
5 * This software is licensed under the terms of the GNU General Public
6 * License version 2, as published by the Free Software Foundation, and
7 * may be copied, distributed, and modified under those terms.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 */
15
16#include <dt-bindings/phy/phy.h>
17#include <linux/clk.h>
18#include <linux/delay.h>
19#include <linux/io.h>
Chunfeng Yun75f072f2015-12-04 10:11:05 +080020#include <linux/iopoll.h>
Chunfeng Yundc7f1902015-09-29 11:01:36 +080021#include <linux/module.h>
22#include <linux/of_address.h>
Chunfeng Yune4b227c2017-12-28 16:40:36 +053023#include <linux/of_device.h>
Chunfeng Yundc7f1902015-09-29 11:01:36 +080024#include <linux/phy/phy.h>
25#include <linux/platform_device.h>
26
Chunfeng Yun8d6e19572017-03-31 15:35:31 +080027/* version V1 sub-banks offset base address */
28/* banks shared by multiple phys */
29#define SSUSB_SIFSLV_V1_SPLLC 0x000 /* shared by u3 phys */
30#define SSUSB_SIFSLV_V1_U2FREQ 0x100 /* shared by u2 phys */
Chunfeng Yun554a56f2017-09-21 18:31:48 +080031#define SSUSB_SIFSLV_V1_CHIP 0x300 /* shared by u3 phys */
Chunfeng Yun8d6e19572017-03-31 15:35:31 +080032/* u2 phy bank */
33#define SSUSB_SIFSLV_V1_U2PHY_COM 0x000
Ryder Lee4ab26cb2017-08-03 18:01:01 +080034/* u3/pcie/sata phy banks */
Chunfeng Yun8d6e19572017-03-31 15:35:31 +080035#define SSUSB_SIFSLV_V1_U3PHYD 0x000
36#define SSUSB_SIFSLV_V1_U3PHYA 0x200
Chunfeng Yundc7f1902015-09-29 11:01:36 +080037
Chunfeng Yun8d6e19572017-03-31 15:35:31 +080038/* version V2 sub-banks offset base address */
39/* u2 phy banks */
40#define SSUSB_SIFSLV_V2_MISC 0x000
41#define SSUSB_SIFSLV_V2_U2FREQ 0x100
42#define SSUSB_SIFSLV_V2_U2PHY_COM 0x300
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +080043/* u3/pcie/sata phy banks */
Chunfeng Yun8d6e19572017-03-31 15:35:31 +080044#define SSUSB_SIFSLV_V2_SPLLC 0x000
45#define SSUSB_SIFSLV_V2_CHIP 0x100
46#define SSUSB_SIFSLV_V2_U3PHYD 0x200
47#define SSUSB_SIFSLV_V2_U3PHYA 0x400
Chunfeng Yundc7f1902015-09-29 11:01:36 +080048
Chunfeng Yun8d6e19572017-03-31 15:35:31 +080049#define U3P_USBPHYACR0 0x000
Chunfeng Yundc7f1902015-09-29 11:01:36 +080050#define PA0_RG_U2PLL_FORCE_ON BIT(15)
Chunfeng Yunc0250fe2017-03-31 15:35:32 +080051#define PA0_RG_USB20_INTR_EN BIT(5)
Chunfeng Yundc7f1902015-09-29 11:01:36 +080052
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 Yun43f53b12015-12-04 10:08:56 +080065#define PA6_RG_U2_SQTH GENMASK(3, 0)
66#define PA6_RG_U2_SQTH_VAL(x) (0xf & (x))
Chunfeng Yundc7f1902015-09-29 11:01:36 +080067
Chunfeng Yun8d6e19572017-03-31 15:35:31 +080068#define U3P_U2PHYACR4 0x020
Chunfeng Yundc7f1902015-09-29 11:01:36 +080069#define P2C_RG_USB20_GPIO_CTL BIT(9)
70#define P2C_USB20_GPIO_MODE BIT(8)
71#define P2C_U2_GPIO_CTR_MSK (P2C_RG_USB20_GPIO_CTL | P2C_USB20_GPIO_MODE)
72
Chunfeng Yun8d6e19572017-03-31 15:35:31 +080073#define U3D_U2PHYDCR0 0x060
Chunfeng Yundc7f1902015-09-29 11:01:36 +080074#define P2C_RG_SIF_U2PLL_FORCE_ON BIT(24)
75
Chunfeng Yun8d6e19572017-03-31 15:35:31 +080076#define U3P_U2PHYDTM0 0x068
Chunfeng Yundc7f1902015-09-29 11:01:36 +080077#define P2C_FORCE_UART_EN BIT(26)
78#define P2C_FORCE_DATAIN BIT(23)
79#define P2C_FORCE_DM_PULLDOWN BIT(21)
80#define P2C_FORCE_DP_PULLDOWN BIT(20)
81#define P2C_FORCE_XCVRSEL BIT(19)
82#define P2C_FORCE_SUSPENDM BIT(18)
83#define P2C_FORCE_TERMSEL BIT(17)
84#define P2C_RG_DATAIN GENMASK(13, 10)
85#define P2C_RG_DATAIN_VAL(x) ((0xf & (x)) << 10)
86#define P2C_RG_DMPULLDOWN BIT(7)
87#define P2C_RG_DPPULLDOWN BIT(6)
88#define P2C_RG_XCVRSEL GENMASK(5, 4)
89#define P2C_RG_XCVRSEL_VAL(x) ((0x3 & (x)) << 4)
90#define P2C_RG_SUSPENDM BIT(3)
91#define P2C_RG_TERMSEL BIT(2)
92#define P2C_DTM0_PART_MASK \
93 (P2C_FORCE_DATAIN | P2C_FORCE_DM_PULLDOWN | \
94 P2C_FORCE_DP_PULLDOWN | P2C_FORCE_XCVRSEL | \
95 P2C_FORCE_TERMSEL | P2C_RG_DMPULLDOWN | \
96 P2C_RG_DPPULLDOWN | P2C_RG_TERMSEL)
97
Chunfeng Yun8d6e19572017-03-31 15:35:31 +080098#define U3P_U2PHYDTM1 0x06C
Chunfeng Yundc7f1902015-09-29 11:01:36 +080099#define P2C_RG_UART_EN BIT(16)
Chunfeng Yun5954a102017-09-21 18:31:49 +0800100#define P2C_FORCE_IDDIG BIT(9)
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800101#define P2C_RG_VBUSVALID BIT(5)
102#define P2C_RG_SESSEND BIT(4)
103#define P2C_RG_AVALID BIT(2)
Chunfeng Yun5954a102017-09-21 18:31:49 +0800104#define P2C_RG_IDDIG BIT(1)
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800105
Ryder Lee44a6d6c2017-08-03 18:01:00 +0800106#define U3P_U3_CHIP_GPIO_CTLD 0x0c
107#define P3C_REG_IP_SW_RST BIT(31)
108#define P3C_MCU_BUS_CK_GATE_EN BIT(30)
109#define P3C_FORCE_IP_SW_RST BIT(29)
110
111#define U3P_U3_CHIP_GPIO_CTLE 0x10
112#define P3C_RG_SWRST_U3_PHYD BIT(25)
113#define P3C_RG_SWRST_U3_PHYD_FORCE_EN BIT(24)
114
115#define U3P_U3_PHYA_REG0 0x000
116#define P3A_RG_CLKDRV_OFF GENMASK(3, 2)
117#define P3A_RG_CLKDRV_OFF_VAL(x) ((0x3 & (x)) << 2)
118
119#define U3P_U3_PHYA_REG1 0x004
120#define P3A_RG_CLKDRV_AMP GENMASK(31, 29)
121#define P3A_RG_CLKDRV_AMP_VAL(x) ((0x7 & (x)) << 29)
122
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800123#define U3P_U3_PHYA_REG6 0x018
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800124#define P3A_RG_TX_EIDLE_CM GENMASK(31, 28)
125#define P3A_RG_TX_EIDLE_CM_VAL(x) ((0xf & (x)) << 28)
126
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800127#define U3P_U3_PHYA_REG9 0x024
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800128#define P3A_RG_RX_DAC_MUX GENMASK(5, 1)
129#define P3A_RG_RX_DAC_MUX_VAL(x) ((0x1f & (x)) << 1)
130
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800131#define U3P_U3_PHYA_DA_REG0 0x100
Ryder Lee44a6d6c2017-08-03 18:01:00 +0800132#define P3A_RG_XTAL_EXT_PE2H GENMASK(17, 16)
133#define P3A_RG_XTAL_EXT_PE2H_VAL(x) ((0x3 & (x)) << 16)
134#define P3A_RG_XTAL_EXT_PE1H GENMASK(13, 12)
135#define P3A_RG_XTAL_EXT_PE1H_VAL(x) ((0x3 & (x)) << 12)
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800136#define P3A_RG_XTAL_EXT_EN_U3 GENMASK(11, 10)
137#define P3A_RG_XTAL_EXT_EN_U3_VAL(x) ((0x3 & (x)) << 10)
138
Ryder Lee44a6d6c2017-08-03 18:01:00 +0800139#define U3P_U3_PHYA_DA_REG4 0x108
140#define P3A_RG_PLL_DIVEN_PE2H GENMASK(21, 19)
141#define P3A_RG_PLL_BC_PE2H GENMASK(7, 6)
142#define P3A_RG_PLL_BC_PE2H_VAL(x) ((0x3 & (x)) << 6)
143
144#define U3P_U3_PHYA_DA_REG5 0x10c
145#define P3A_RG_PLL_BR_PE2H GENMASK(29, 28)
146#define P3A_RG_PLL_BR_PE2H_VAL(x) ((0x3 & (x)) << 28)
147#define P3A_RG_PLL_IC_PE2H GENMASK(15, 12)
148#define P3A_RG_PLL_IC_PE2H_VAL(x) ((0xf & (x)) << 12)
149
150#define U3P_U3_PHYA_DA_REG6 0x110
151#define P3A_RG_PLL_IR_PE2H GENMASK(19, 16)
152#define P3A_RG_PLL_IR_PE2H_VAL(x) ((0xf & (x)) << 16)
153
154#define U3P_U3_PHYA_DA_REG7 0x114
155#define P3A_RG_PLL_BP_PE2H GENMASK(19, 16)
156#define P3A_RG_PLL_BP_PE2H_VAL(x) ((0xf & (x)) << 16)
157
158#define U3P_U3_PHYA_DA_REG20 0x13c
159#define P3A_RG_PLL_DELTA1_PE2H GENMASK(31, 16)
160#define P3A_RG_PLL_DELTA1_PE2H_VAL(x) ((0xffff & (x)) << 16)
161
162#define U3P_U3_PHYA_DA_REG25 0x148
163#define P3A_RG_PLL_DELTA_PE2H GENMASK(15, 0)
164#define P3A_RG_PLL_DELTA_PE2H_VAL(x) (0xffff & (x))
165
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800166#define U3P_U3_PHYD_LFPS1 0x00c
Chunfeng Yun98cd83a2017-03-31 15:35:28 +0800167#define P3D_RG_FWAKE_TH GENMASK(21, 16)
168#define P3D_RG_FWAKE_TH_VAL(x) ((0x3f & (x)) << 16)
169
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800170#define U3P_U3_PHYD_CDR1 0x05c
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800171#define P3D_RG_CDR_BIR_LTD1 GENMASK(28, 24)
172#define P3D_RG_CDR_BIR_LTD1_VAL(x) ((0x1f & (x)) << 24)
173#define P3D_RG_CDR_BIR_LTD0 GENMASK(12, 8)
174#define P3D_RG_CDR_BIR_LTD0_VAL(x) ((0x1f & (x)) << 8)
175
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800176#define U3P_U3_PHYD_RXDET1 0x128
Chunfeng Yun1969f692017-03-31 15:35:27 +0800177#define P3D_RG_RXDET_STB2_SET GENMASK(17, 9)
178#define P3D_RG_RXDET_STB2_SET_VAL(x) ((0x1ff & (x)) << 9)
179
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800180#define U3P_U3_PHYD_RXDET2 0x12c
Chunfeng Yun1969f692017-03-31 15:35:27 +0800181#define P3D_RG_RXDET_STB2_SET_P3 GENMASK(8, 0)
182#define P3D_RG_RXDET_STB2_SET_P3_VAL(x) (0x1ff & (x))
183
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800184#define U3P_SPLLC_XTALCTL3 0x018
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800185#define XC3_RG_U3_XTAL_RX_PWD BIT(9)
186#define XC3_RG_U3_FRC_XTAL_RX_PWD BIT(8)
187
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800188#define U3P_U2FREQ_FMCR0 0x00
Chunfeng Yun75f072f2015-12-04 10:11:05 +0800189#define P2F_RG_MONCLK_SEL GENMASK(27, 26)
190#define P2F_RG_MONCLK_SEL_VAL(x) ((0x3 & (x)) << 26)
191#define P2F_RG_FREQDET_EN BIT(24)
192#define P2F_RG_CYCLECNT GENMASK(23, 0)
193#define P2F_RG_CYCLECNT_VAL(x) ((P2F_RG_CYCLECNT) & (x))
194
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800195#define U3P_U2FREQ_VALUE 0x0c
Chunfeng Yun75f072f2015-12-04 10:11:05 +0800196
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800197#define U3P_U2FREQ_FMMONR1 0x10
Chunfeng Yun75f072f2015-12-04 10:11:05 +0800198#define P2F_USB_FM_VALID BIT(0)
199#define P2F_RG_FRCK_EN BIT(8)
200
201#define U3P_REF_CLK 26 /* MHZ */
202#define U3P_SLEW_RATE_COEF 28
203#define U3P_SR_COEF_DIVISOR 1000
204#define U3P_FM_DET_CYCLE_CNT 1024
205
Ryder Lee4ab26cb2017-08-03 18:01:01 +0800206/* SATA register setting */
207#define PHYD_CTRL_SIGNAL_MODE4 0x1c
208/* CDR Charge Pump P-path current adjustment */
209#define RG_CDR_BICLTD1_GEN1_MSK GENMASK(23, 20)
210#define RG_CDR_BICLTD1_GEN1_VAL(x) ((0xf & (x)) << 20)
211#define RG_CDR_BICLTD0_GEN1_MSK GENMASK(11, 8)
212#define RG_CDR_BICLTD0_GEN1_VAL(x) ((0xf & (x)) << 8)
213
214#define PHYD_DESIGN_OPTION2 0x24
215/* Symbol lock count selection */
216#define RG_LOCK_CNT_SEL_MSK GENMASK(5, 4)
217#define RG_LOCK_CNT_SEL_VAL(x) ((0x3 & (x)) << 4)
218
219#define PHYD_DESIGN_OPTION9 0x40
220/* COMWAK GAP width window */
221#define RG_TG_MAX_MSK GENMASK(20, 16)
222#define RG_TG_MAX_VAL(x) ((0x1f & (x)) << 16)
223/* COMINIT GAP width window */
224#define RG_T2_MAX_MSK GENMASK(13, 8)
225#define RG_T2_MAX_VAL(x) ((0x3f & (x)) << 8)
226/* COMWAK GAP width window */
227#define RG_TG_MIN_MSK GENMASK(7, 5)
228#define RG_TG_MIN_VAL(x) ((0x7 & (x)) << 5)
229/* COMINIT GAP width window */
230#define RG_T2_MIN_MSK GENMASK(4, 0)
231#define RG_T2_MIN_VAL(x) (0x1f & (x))
232
233#define ANA_RG_CTRL_SIGNAL1 0x4c
234/* TX driver tail current control for 0dB de-empahsis mdoe for Gen1 speed */
235#define RG_IDRV_0DB_GEN1_MSK GENMASK(13, 8)
236#define RG_IDRV_0DB_GEN1_VAL(x) ((0x3f & (x)) << 8)
237
238#define ANA_RG_CTRL_SIGNAL4 0x58
239#define RG_CDR_BICLTR_GEN1_MSK GENMASK(23, 20)
240#define RG_CDR_BICLTR_GEN1_VAL(x) ((0xf & (x)) << 20)
241/* Loop filter R1 resistance adjustment for Gen1 speed */
242#define RG_CDR_BR_GEN2_MSK GENMASK(10, 8)
243#define RG_CDR_BR_GEN2_VAL(x) ((0x7 & (x)) << 8)
244
245#define ANA_RG_CTRL_SIGNAL6 0x60
246/* I-path capacitance adjustment for Gen1 */
247#define RG_CDR_BC_GEN1_MSK GENMASK(28, 24)
248#define RG_CDR_BC_GEN1_VAL(x) ((0x1f & (x)) << 24)
249#define RG_CDR_BIRLTR_GEN1_MSK GENMASK(4, 0)
250#define RG_CDR_BIRLTR_GEN1_VAL(x) (0x1f & (x))
251
252#define ANA_EQ_EYE_CTRL_SIGNAL1 0x6c
253/* RX Gen1 LEQ tuning step */
254#define RG_EQ_DLEQ_LFI_GEN1_MSK GENMASK(11, 8)
255#define RG_EQ_DLEQ_LFI_GEN1_VAL(x) ((0xf & (x)) << 8)
256
257#define ANA_EQ_EYE_CTRL_SIGNAL4 0xd8
258#define RG_CDR_BIRLTD0_GEN1_MSK GENMASK(20, 16)
259#define RG_CDR_BIRLTD0_GEN1_VAL(x) ((0x1f & (x)) << 16)
260
261#define ANA_EQ_EYE_CTRL_SIGNAL5 0xdc
262#define RG_CDR_BIRLTD0_GEN3_MSK GENMASK(4, 0)
263#define RG_CDR_BIRLTD0_GEN3_VAL(x) (0x1f & (x))
264
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800265enum mtk_phy_version {
266 MTK_PHY_V1 = 1,
267 MTK_PHY_V2,
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800268};
269
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800270struct mtk_phy_pdata {
Chunfeng Yune1d76532016-04-20 08:14:02 +0800271 /* avoid RX sensitivity level degradation only for mt8173 */
272 bool avoid_rx_sen_degradation;
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800273 enum mtk_phy_version version;
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800274};
275
276struct u2phy_banks {
277 void __iomem *misc;
278 void __iomem *fmreg;
279 void __iomem *com;
280};
281
282struct u3phy_banks {
283 void __iomem *spllc;
284 void __iomem *chip;
285 void __iomem *phyd; /* include u3phyd_bank2 */
286 void __iomem *phya; /* include u3phya_da */
Chunfeng Yune1d76532016-04-20 08:14:02 +0800287};
288
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800289struct mtk_phy_instance {
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800290 struct phy *phy;
291 void __iomem *port_base;
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800292 union {
293 struct u2phy_banks u2_banks;
294 struct u3phy_banks u3_banks;
295 };
Chunfeng Yun15de15c2017-03-31 15:35:30 +0800296 struct clk *ref_clk; /* reference clock of anolog phy */
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800297 u32 index;
298 u8 type;
299};
300
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800301struct mtk_tphy {
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800302 struct device *dev;
Chunfeng Yun04466ef2017-03-31 15:35:29 +0800303 void __iomem *sif_base; /* only shared sif */
Chunfeng Yun15de15c2017-03-31 15:35:30 +0800304 /* deprecated, use @ref_clk instead in phy instance */
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800305 struct clk *u3phya_ref; /* reference clock of usb3 anolog phy */
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800306 const struct mtk_phy_pdata *pdata;
307 struct mtk_phy_instance **phys;
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800308 int nphys;
309};
310
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800311static void hs_slew_rate_calibrate(struct mtk_tphy *tphy,
312 struct mtk_phy_instance *instance)
Chunfeng Yun75f072f2015-12-04 10:11:05 +0800313{
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800314 struct u2phy_banks *u2_banks = &instance->u2_banks;
315 void __iomem *fmreg = u2_banks->fmreg;
316 void __iomem *com = u2_banks->com;
Chunfeng Yun75f072f2015-12-04 10:11:05 +0800317 int calibration_val;
318 int fm_out;
319 u32 tmp;
320
321 /* enable USB ring oscillator */
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800322 tmp = readl(com + U3P_USBPHYACR5);
Chunfeng Yun75f072f2015-12-04 10:11:05 +0800323 tmp |= PA5_RG_U2_HSTX_SRCAL_EN;
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800324 writel(tmp, com + U3P_USBPHYACR5);
Chunfeng Yun75f072f2015-12-04 10:11:05 +0800325 udelay(1);
326
327 /*enable free run clock */
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800328 tmp = readl(fmreg + U3P_U2FREQ_FMMONR1);
Chunfeng Yun75f072f2015-12-04 10:11:05 +0800329 tmp |= P2F_RG_FRCK_EN;
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800330 writel(tmp, fmreg + U3P_U2FREQ_FMMONR1);
Chunfeng Yun75f072f2015-12-04 10:11:05 +0800331
332 /* set cycle count as 1024, and select u2 channel */
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800333 tmp = readl(fmreg + U3P_U2FREQ_FMCR0);
Chunfeng Yun75f072f2015-12-04 10:11:05 +0800334 tmp &= ~(P2F_RG_CYCLECNT | P2F_RG_MONCLK_SEL);
335 tmp |= P2F_RG_CYCLECNT_VAL(U3P_FM_DET_CYCLE_CNT);
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800336 if (tphy->pdata->version == MTK_PHY_V1)
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800337 tmp |= P2F_RG_MONCLK_SEL_VAL(instance->index >> 1);
338
339 writel(tmp, fmreg + U3P_U2FREQ_FMCR0);
Chunfeng Yun75f072f2015-12-04 10:11:05 +0800340
341 /* enable frequency meter */
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800342 tmp = readl(fmreg + U3P_U2FREQ_FMCR0);
Chunfeng Yun75f072f2015-12-04 10:11:05 +0800343 tmp |= P2F_RG_FREQDET_EN;
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800344 writel(tmp, fmreg + U3P_U2FREQ_FMCR0);
Chunfeng Yun75f072f2015-12-04 10:11:05 +0800345
346 /* ignore return value */
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800347 readl_poll_timeout(fmreg + U3P_U2FREQ_FMMONR1, tmp,
348 (tmp & P2F_USB_FM_VALID), 10, 200);
Chunfeng Yun75f072f2015-12-04 10:11:05 +0800349
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800350 fm_out = readl(fmreg + U3P_U2FREQ_VALUE);
Chunfeng Yun75f072f2015-12-04 10:11:05 +0800351
352 /* disable frequency meter */
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800353 tmp = readl(fmreg + U3P_U2FREQ_FMCR0);
Chunfeng Yun75f072f2015-12-04 10:11:05 +0800354 tmp &= ~P2F_RG_FREQDET_EN;
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800355 writel(tmp, fmreg + U3P_U2FREQ_FMCR0);
Chunfeng Yun75f072f2015-12-04 10:11:05 +0800356
357 /*disable free run clock */
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800358 tmp = readl(fmreg + U3P_U2FREQ_FMMONR1);
Chunfeng Yun75f072f2015-12-04 10:11:05 +0800359 tmp &= ~P2F_RG_FRCK_EN;
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800360 writel(tmp, fmreg + U3P_U2FREQ_FMMONR1);
Chunfeng Yun75f072f2015-12-04 10:11:05 +0800361
362 if (fm_out) {
363 /* ( 1024 / FM_OUT ) x reference clock frequency x 0.028 */
364 tmp = U3P_FM_DET_CYCLE_CNT * U3P_REF_CLK * U3P_SLEW_RATE_COEF;
365 tmp /= fm_out;
366 calibration_val = DIV_ROUND_CLOSEST(tmp, U3P_SR_COEF_DIVISOR);
367 } else {
368 /* if FM detection fail, set default value */
369 calibration_val = 4;
370 }
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800371 dev_dbg(tphy->dev, "phy:%d, fm_out:%d, calib:%d\n",
Chunfeng Yun75f072f2015-12-04 10:11:05 +0800372 instance->index, fm_out, calibration_val);
373
374 /* set HS slew rate */
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800375 tmp = readl(com + U3P_USBPHYACR5);
Chunfeng Yun75f072f2015-12-04 10:11:05 +0800376 tmp &= ~PA5_RG_U2_HSTX_SRCTRL;
377 tmp |= PA5_RG_U2_HSTX_SRCTRL_VAL(calibration_val);
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800378 writel(tmp, com + U3P_USBPHYACR5);
Chunfeng Yun75f072f2015-12-04 10:11:05 +0800379
380 /* disable USB ring oscillator */
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800381 tmp = readl(com + U3P_USBPHYACR5);
Chunfeng Yun75f072f2015-12-04 10:11:05 +0800382 tmp &= ~PA5_RG_U2_HSTX_SRCAL_EN;
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800383 writel(tmp, com + U3P_USBPHYACR5);
Chunfeng Yun75f072f2015-12-04 10:11:05 +0800384}
385
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800386static void u3_phy_instance_init(struct mtk_tphy *tphy,
387 struct mtk_phy_instance *instance)
Chunfeng Yun04466ef2017-03-31 15:35:29 +0800388{
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800389 struct u3phy_banks *u3_banks = &instance->u3_banks;
Chunfeng Yun04466ef2017-03-31 15:35:29 +0800390 u32 tmp;
391
392 /* gating PCIe Analog XTAL clock */
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800393 tmp = readl(u3_banks->spllc + U3P_SPLLC_XTALCTL3);
Chunfeng Yun04466ef2017-03-31 15:35:29 +0800394 tmp |= XC3_RG_U3_XTAL_RX_PWD | XC3_RG_U3_FRC_XTAL_RX_PWD;
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800395 writel(tmp, u3_banks->spllc + U3P_SPLLC_XTALCTL3);
Chunfeng Yun04466ef2017-03-31 15:35:29 +0800396
397 /* gating XSQ */
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800398 tmp = readl(u3_banks->phya + U3P_U3_PHYA_DA_REG0);
Chunfeng Yun04466ef2017-03-31 15:35:29 +0800399 tmp &= ~P3A_RG_XTAL_EXT_EN_U3;
400 tmp |= P3A_RG_XTAL_EXT_EN_U3_VAL(2);
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800401 writel(tmp, u3_banks->phya + U3P_U3_PHYA_DA_REG0);
Chunfeng Yun04466ef2017-03-31 15:35:29 +0800402
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800403 tmp = readl(u3_banks->phya + U3P_U3_PHYA_REG9);
Chunfeng Yun04466ef2017-03-31 15:35:29 +0800404 tmp &= ~P3A_RG_RX_DAC_MUX;
405 tmp |= P3A_RG_RX_DAC_MUX_VAL(4);
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800406 writel(tmp, u3_banks->phya + U3P_U3_PHYA_REG9);
Chunfeng Yun04466ef2017-03-31 15:35:29 +0800407
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800408 tmp = readl(u3_banks->phya + U3P_U3_PHYA_REG6);
Chunfeng Yun04466ef2017-03-31 15:35:29 +0800409 tmp &= ~P3A_RG_TX_EIDLE_CM;
410 tmp |= P3A_RG_TX_EIDLE_CM_VAL(0xe);
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800411 writel(tmp, u3_banks->phya + U3P_U3_PHYA_REG6);
Chunfeng Yun04466ef2017-03-31 15:35:29 +0800412
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800413 tmp = readl(u3_banks->phyd + U3P_U3_PHYD_CDR1);
Chunfeng Yun04466ef2017-03-31 15:35:29 +0800414 tmp &= ~(P3D_RG_CDR_BIR_LTD0 | P3D_RG_CDR_BIR_LTD1);
415 tmp |= P3D_RG_CDR_BIR_LTD0_VAL(0xc) | P3D_RG_CDR_BIR_LTD1_VAL(0x3);
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800416 writel(tmp, u3_banks->phyd + U3P_U3_PHYD_CDR1);
Chunfeng Yun04466ef2017-03-31 15:35:29 +0800417
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800418 tmp = readl(u3_banks->phyd + U3P_U3_PHYD_LFPS1);
Chunfeng Yun04466ef2017-03-31 15:35:29 +0800419 tmp &= ~P3D_RG_FWAKE_TH;
420 tmp |= P3D_RG_FWAKE_TH_VAL(0x34);
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800421 writel(tmp, u3_banks->phyd + U3P_U3_PHYD_LFPS1);
Chunfeng Yun04466ef2017-03-31 15:35:29 +0800422
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800423 tmp = readl(u3_banks->phyd + U3P_U3_PHYD_RXDET1);
Chunfeng Yun04466ef2017-03-31 15:35:29 +0800424 tmp &= ~P3D_RG_RXDET_STB2_SET;
425 tmp |= P3D_RG_RXDET_STB2_SET_VAL(0x10);
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800426 writel(tmp, u3_banks->phyd + U3P_U3_PHYD_RXDET1);
Chunfeng Yun04466ef2017-03-31 15:35:29 +0800427
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800428 tmp = readl(u3_banks->phyd + U3P_U3_PHYD_RXDET2);
Chunfeng Yun04466ef2017-03-31 15:35:29 +0800429 tmp &= ~P3D_RG_RXDET_STB2_SET_P3;
430 tmp |= P3D_RG_RXDET_STB2_SET_P3_VAL(0x10);
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800431 writel(tmp, u3_banks->phyd + U3P_U3_PHYD_RXDET2);
Chunfeng Yun04466ef2017-03-31 15:35:29 +0800432
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800433 dev_dbg(tphy->dev, "%s(%d)\n", __func__, instance->index);
Chunfeng Yun04466ef2017-03-31 15:35:29 +0800434}
435
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800436static void u2_phy_instance_init(struct mtk_tphy *tphy,
437 struct mtk_phy_instance *instance)
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800438{
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800439 struct u2phy_banks *u2_banks = &instance->u2_banks;
440 void __iomem *com = u2_banks->com;
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800441 u32 index = instance->index;
442 u32 tmp;
443
Chunfeng Yun00c00922017-12-07 19:53:34 +0800444 /* switch to USB function, and enable usb pll */
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800445 tmp = readl(com + U3P_U2PHYDTM0);
Chunfeng Yun00c00922017-12-07 19:53:34 +0800446 tmp &= ~(P2C_FORCE_UART_EN | P2C_FORCE_SUSPENDM);
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800447 tmp |= P2C_RG_XCVRSEL_VAL(1) | P2C_RG_DATAIN_VAL(0);
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800448 writel(tmp, com + U3P_U2PHYDTM0);
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800449
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800450 tmp = readl(com + U3P_U2PHYDTM1);
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800451 tmp &= ~P2C_RG_UART_EN;
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800452 writel(tmp, com + U3P_U2PHYDTM1);
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800453
Chunfeng Yunc0250fe2017-03-31 15:35:32 +0800454 tmp = readl(com + U3P_USBPHYACR0);
455 tmp |= PA0_RG_USB20_INTR_EN;
456 writel(tmp, com + U3P_USBPHYACR0);
457
458 /* disable switch 100uA current to SSUSB */
459 tmp = readl(com + U3P_USBPHYACR5);
460 tmp &= ~PA5_RG_U2_HS_100U_U3_EN;
461 writel(tmp, com + U3P_USBPHYACR5);
462
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800463 if (!index) {
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800464 tmp = readl(com + U3P_U2PHYACR4);
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800465 tmp &= ~P2C_U2_GPIO_CTR_MSK;
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800466 writel(tmp, com + U3P_U2PHYACR4);
Chunfeng Yune1d76532016-04-20 08:14:02 +0800467 }
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800468
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800469 if (tphy->pdata->avoid_rx_sen_degradation) {
Chunfeng Yune1d76532016-04-20 08:14:02 +0800470 if (!index) {
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800471 tmp = readl(com + U3P_USBPHYACR2);
Chunfeng Yune1d76532016-04-20 08:14:02 +0800472 tmp |= PA2_RG_SIF_U2PLL_FORCE_EN;
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800473 writel(tmp, com + U3P_USBPHYACR2);
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800474
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800475 tmp = readl(com + U3D_U2PHYDCR0);
Chunfeng Yune1d76532016-04-20 08:14:02 +0800476 tmp &= ~P2C_RG_SIF_U2PLL_FORCE_ON;
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800477 writel(tmp, com + U3D_U2PHYDCR0);
Chunfeng Yune1d76532016-04-20 08:14:02 +0800478 } else {
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800479 tmp = readl(com + U3D_U2PHYDCR0);
Chunfeng Yune1d76532016-04-20 08:14:02 +0800480 tmp |= P2C_RG_SIF_U2PLL_FORCE_ON;
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800481 writel(tmp, com + U3D_U2PHYDCR0);
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800482
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800483 tmp = readl(com + U3P_U2PHYDTM0);
Chunfeng Yune1d76532016-04-20 08:14:02 +0800484 tmp |= P2C_RG_SUSPENDM | P2C_FORCE_SUSPENDM;
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800485 writel(tmp, com + U3P_U2PHYDTM0);
Chunfeng Yune1d76532016-04-20 08:14:02 +0800486 }
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800487 }
488
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800489 tmp = readl(com + U3P_USBPHYACR6);
Chunfeng Yun43f53b12015-12-04 10:08:56 +0800490 tmp &= ~PA6_RG_U2_BC11_SW_EN; /* DP/DM BC1.1 path Disable */
491 tmp &= ~PA6_RG_U2_SQTH;
492 tmp |= PA6_RG_U2_SQTH_VAL(2);
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800493 writel(tmp, com + U3P_USBPHYACR6);
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800494
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800495 dev_dbg(tphy->dev, "%s(%d)\n", __func__, index);
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800496}
497
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800498static void u2_phy_instance_power_on(struct mtk_tphy *tphy,
499 struct mtk_phy_instance *instance)
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800500{
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800501 struct u2phy_banks *u2_banks = &instance->u2_banks;
502 void __iomem *com = u2_banks->com;
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800503 u32 index = instance->index;
504 u32 tmp;
505
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800506 tmp = readl(com + U3P_U2PHYDTM0);
Chunfeng Yun00c00922017-12-07 19:53:34 +0800507 tmp &= ~(P2C_RG_XCVRSEL | P2C_RG_DATAIN | P2C_DTM0_PART_MASK);
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800508 writel(tmp, com + U3P_U2PHYDTM0);
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800509
510 /* OTG Enable */
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800511 tmp = readl(com + U3P_USBPHYACR6);
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800512 tmp |= PA6_RG_U2_OTG_VBUSCMP_EN;
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800513 writel(tmp, com + U3P_USBPHYACR6);
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800514
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800515 tmp = readl(com + U3P_U2PHYDTM1);
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800516 tmp |= P2C_RG_VBUSVALID | P2C_RG_AVALID;
517 tmp &= ~P2C_RG_SESSEND;
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800518 writel(tmp, com + U3P_U2PHYDTM1);
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800519
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800520 if (tphy->pdata->avoid_rx_sen_degradation && index) {
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800521 tmp = readl(com + U3D_U2PHYDCR0);
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800522 tmp |= P2C_RG_SIF_U2PLL_FORCE_ON;
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800523 writel(tmp, com + U3D_U2PHYDCR0);
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800524
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800525 tmp = readl(com + U3P_U2PHYDTM0);
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800526 tmp |= P2C_RG_SUSPENDM | P2C_FORCE_SUSPENDM;
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800527 writel(tmp, com + U3P_U2PHYDTM0);
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800528 }
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800529 dev_dbg(tphy->dev, "%s(%d)\n", __func__, index);
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800530}
531
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800532static void u2_phy_instance_power_off(struct mtk_tphy *tphy,
533 struct mtk_phy_instance *instance)
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800534{
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800535 struct u2phy_banks *u2_banks = &instance->u2_banks;
536 void __iomem *com = u2_banks->com;
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800537 u32 index = instance->index;
538 u32 tmp;
539
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800540 tmp = readl(com + U3P_U2PHYDTM0);
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800541 tmp &= ~(P2C_RG_XCVRSEL | P2C_RG_DATAIN);
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800542 writel(tmp, com + U3P_U2PHYDTM0);
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800543
544 /* OTG Disable */
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800545 tmp = readl(com + U3P_USBPHYACR6);
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800546 tmp &= ~PA6_RG_U2_OTG_VBUSCMP_EN;
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800547 writel(tmp, com + U3P_USBPHYACR6);
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800548
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800549 tmp = readl(com + U3P_U2PHYDTM1);
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800550 tmp &= ~(P2C_RG_VBUSVALID | P2C_RG_AVALID);
551 tmp |= P2C_RG_SESSEND;
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800552 writel(tmp, com + U3P_U2PHYDTM1);
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800553
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800554 if (tphy->pdata->avoid_rx_sen_degradation && index) {
Chunfeng Yun00c00922017-12-07 19:53:34 +0800555 tmp = readl(com + U3P_U2PHYDTM0);
556 tmp &= ~(P2C_RG_SUSPENDM | P2C_FORCE_SUSPENDM);
557 writel(tmp, com + U3P_U2PHYDTM0);
558
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800559 tmp = readl(com + U3D_U2PHYDCR0);
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800560 tmp &= ~P2C_RG_SIF_U2PLL_FORCE_ON;
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800561 writel(tmp, com + U3D_U2PHYDCR0);
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800562 }
563
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800564 dev_dbg(tphy->dev, "%s(%d)\n", __func__, index);
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800565}
566
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800567static void u2_phy_instance_exit(struct mtk_tphy *tphy,
568 struct mtk_phy_instance *instance)
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800569{
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800570 struct u2phy_banks *u2_banks = &instance->u2_banks;
571 void __iomem *com = u2_banks->com;
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800572 u32 index = instance->index;
573 u32 tmp;
574
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800575 if (tphy->pdata->avoid_rx_sen_degradation && index) {
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
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800580 tmp = readl(com + U3P_U2PHYDTM0);
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800581 tmp &= ~P2C_FORCE_SUSPENDM;
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800582 writel(tmp, com + U3P_U2PHYDTM0);
583 }
584}
585
Chunfeng Yun5954a102017-09-21 18:31:49 +0800586static void u2_phy_instance_set_mode(struct mtk_tphy *tphy,
587 struct mtk_phy_instance *instance,
588 enum phy_mode mode)
589{
590 struct u2phy_banks *u2_banks = &instance->u2_banks;
591 u32 tmp;
592
593 tmp = readl(u2_banks->com + U3P_U2PHYDTM1);
594 switch (mode) {
595 case PHY_MODE_USB_DEVICE:
596 tmp |= P2C_FORCE_IDDIG | P2C_RG_IDDIG;
597 break;
598 case PHY_MODE_USB_HOST:
599 tmp |= P2C_FORCE_IDDIG;
600 tmp &= ~P2C_RG_IDDIG;
601 break;
602 case PHY_MODE_USB_OTG:
603 tmp &= ~(P2C_FORCE_IDDIG | P2C_RG_IDDIG);
604 break;
605 default:
606 return;
607 }
608 writel(tmp, u2_banks->com + U3P_U2PHYDTM1);
609}
610
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800611static void pcie_phy_instance_init(struct mtk_tphy *tphy,
612 struct mtk_phy_instance *instance)
Ryder Lee44a6d6c2017-08-03 18:01:00 +0800613{
614 struct u3phy_banks *u3_banks = &instance->u3_banks;
615 u32 tmp;
616
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800617 if (tphy->pdata->version != MTK_PHY_V1)
Ryder Lee44a6d6c2017-08-03 18:01:00 +0800618 return;
619
620 tmp = readl(u3_banks->phya + U3P_U3_PHYA_DA_REG0);
621 tmp &= ~(P3A_RG_XTAL_EXT_PE1H | P3A_RG_XTAL_EXT_PE2H);
622 tmp |= P3A_RG_XTAL_EXT_PE1H_VAL(0x2) | P3A_RG_XTAL_EXT_PE2H_VAL(0x2);
623 writel(tmp, u3_banks->phya + U3P_U3_PHYA_DA_REG0);
624
625 /* ref clk drive */
626 tmp = readl(u3_banks->phya + U3P_U3_PHYA_REG1);
627 tmp &= ~P3A_RG_CLKDRV_AMP;
628 tmp |= P3A_RG_CLKDRV_AMP_VAL(0x4);
629 writel(tmp, u3_banks->phya + U3P_U3_PHYA_REG1);
630
631 tmp = readl(u3_banks->phya + U3P_U3_PHYA_REG0);
632 tmp &= ~P3A_RG_CLKDRV_OFF;
633 tmp |= P3A_RG_CLKDRV_OFF_VAL(0x1);
634 writel(tmp, u3_banks->phya + U3P_U3_PHYA_REG0);
635
636 /* SSC delta -5000ppm */
637 tmp = readl(u3_banks->phya + U3P_U3_PHYA_DA_REG20);
638 tmp &= ~P3A_RG_PLL_DELTA1_PE2H;
639 tmp |= P3A_RG_PLL_DELTA1_PE2H_VAL(0x3c);
640 writel(tmp, u3_banks->phya + U3P_U3_PHYA_DA_REG20);
641
642 tmp = readl(u3_banks->phya + U3P_U3_PHYA_DA_REG25);
643 tmp &= ~P3A_RG_PLL_DELTA_PE2H;
644 tmp |= P3A_RG_PLL_DELTA_PE2H_VAL(0x36);
645 writel(tmp, u3_banks->phya + U3P_U3_PHYA_DA_REG25);
646
647 /* change pll BW 0.6M */
648 tmp = readl(u3_banks->phya + U3P_U3_PHYA_DA_REG5);
649 tmp &= ~(P3A_RG_PLL_BR_PE2H | P3A_RG_PLL_IC_PE2H);
650 tmp |= P3A_RG_PLL_BR_PE2H_VAL(0x1) | P3A_RG_PLL_IC_PE2H_VAL(0x1);
651 writel(tmp, u3_banks->phya + U3P_U3_PHYA_DA_REG5);
652
653 tmp = readl(u3_banks->phya + U3P_U3_PHYA_DA_REG4);
654 tmp &= ~(P3A_RG_PLL_DIVEN_PE2H | P3A_RG_PLL_BC_PE2H);
655 tmp |= P3A_RG_PLL_BC_PE2H_VAL(0x3);
656 writel(tmp, u3_banks->phya + U3P_U3_PHYA_DA_REG4);
657
658 tmp = readl(u3_banks->phya + U3P_U3_PHYA_DA_REG6);
659 tmp &= ~P3A_RG_PLL_IR_PE2H;
660 tmp |= P3A_RG_PLL_IR_PE2H_VAL(0x2);
661 writel(tmp, u3_banks->phya + U3P_U3_PHYA_DA_REG6);
662
663 tmp = readl(u3_banks->phya + U3P_U3_PHYA_DA_REG7);
664 tmp &= ~P3A_RG_PLL_BP_PE2H;
665 tmp |= P3A_RG_PLL_BP_PE2H_VAL(0xa);
666 writel(tmp, u3_banks->phya + U3P_U3_PHYA_DA_REG7);
667
668 /* Tx Detect Rx Timing: 10us -> 5us */
669 tmp = readl(u3_banks->phyd + U3P_U3_PHYD_RXDET1);
670 tmp &= ~P3D_RG_RXDET_STB2_SET;
671 tmp |= P3D_RG_RXDET_STB2_SET_VAL(0x10);
672 writel(tmp, u3_banks->phyd + U3P_U3_PHYD_RXDET1);
673
674 tmp = readl(u3_banks->phyd + U3P_U3_PHYD_RXDET2);
675 tmp &= ~P3D_RG_RXDET_STB2_SET_P3;
676 tmp |= P3D_RG_RXDET_STB2_SET_P3_VAL(0x10);
677 writel(tmp, u3_banks->phyd + U3P_U3_PHYD_RXDET2);
678
679 /* wait for PCIe subsys register to active */
680 usleep_range(2500, 3000);
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800681 dev_dbg(tphy->dev, "%s(%d)\n", __func__, instance->index);
Ryder Lee44a6d6c2017-08-03 18:01:00 +0800682}
683
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800684static void pcie_phy_instance_power_on(struct mtk_tphy *tphy,
685 struct mtk_phy_instance *instance)
Ryder Lee44a6d6c2017-08-03 18:01:00 +0800686{
687 struct u3phy_banks *bank = &instance->u3_banks;
688 u32 tmp;
689
690 tmp = readl(bank->chip + U3P_U3_CHIP_GPIO_CTLD);
Chunfeng Yun40363252018-03-12 13:25:38 +0800691 tmp &= ~(P3C_FORCE_IP_SW_RST | P3C_REG_IP_SW_RST);
Ryder Lee44a6d6c2017-08-03 18:01:00 +0800692 writel(tmp, bank->chip + U3P_U3_CHIP_GPIO_CTLD);
693
694 tmp = readl(bank->chip + U3P_U3_CHIP_GPIO_CTLE);
695 tmp &= ~(P3C_RG_SWRST_U3_PHYD_FORCE_EN | P3C_RG_SWRST_U3_PHYD);
696 writel(tmp, bank->chip + U3P_U3_CHIP_GPIO_CTLE);
697}
698
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800699static void pcie_phy_instance_power_off(struct mtk_tphy *tphy,
700 struct mtk_phy_instance *instance)
Ryder Lee44a6d6c2017-08-03 18:01:00 +0800701
702{
703 struct u3phy_banks *bank = &instance->u3_banks;
704 u32 tmp;
705
706 tmp = readl(bank->chip + U3P_U3_CHIP_GPIO_CTLD);
707 tmp |= P3C_FORCE_IP_SW_RST | P3C_REG_IP_SW_RST;
708 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 sata_phy_instance_init(struct mtk_tphy *tphy,
716 struct mtk_phy_instance *instance)
Ryder Lee4ab26cb2017-08-03 18:01:01 +0800717{
718 struct u3phy_banks *u3_banks = &instance->u3_banks;
719 void __iomem *phyd = u3_banks->phyd;
720 u32 tmp;
721
722 /* charge current adjustment */
723 tmp = readl(phyd + ANA_RG_CTRL_SIGNAL6);
724 tmp &= ~(RG_CDR_BIRLTR_GEN1_MSK | RG_CDR_BC_GEN1_MSK);
725 tmp |= RG_CDR_BIRLTR_GEN1_VAL(0x6) | RG_CDR_BC_GEN1_VAL(0x1a);
726 writel(tmp, phyd + ANA_RG_CTRL_SIGNAL6);
727
728 tmp = readl(phyd + ANA_EQ_EYE_CTRL_SIGNAL4);
729 tmp &= ~RG_CDR_BIRLTD0_GEN1_MSK;
730 tmp |= RG_CDR_BIRLTD0_GEN1_VAL(0x18);
731 writel(tmp, phyd + ANA_EQ_EYE_CTRL_SIGNAL4);
732
733 tmp = readl(phyd + ANA_EQ_EYE_CTRL_SIGNAL5);
734 tmp &= ~RG_CDR_BIRLTD0_GEN3_MSK;
735 tmp |= RG_CDR_BIRLTD0_GEN3_VAL(0x06);
736 writel(tmp, phyd + ANA_EQ_EYE_CTRL_SIGNAL5);
737
738 tmp = readl(phyd + ANA_RG_CTRL_SIGNAL4);
739 tmp &= ~(RG_CDR_BICLTR_GEN1_MSK | RG_CDR_BR_GEN2_MSK);
740 tmp |= RG_CDR_BICLTR_GEN1_VAL(0x0c) | RG_CDR_BR_GEN2_VAL(0x07);
741 writel(tmp, phyd + ANA_RG_CTRL_SIGNAL4);
742
743 tmp = readl(phyd + PHYD_CTRL_SIGNAL_MODE4);
744 tmp &= ~(RG_CDR_BICLTD0_GEN1_MSK | RG_CDR_BICLTD1_GEN1_MSK);
745 tmp |= RG_CDR_BICLTD0_GEN1_VAL(0x08) | RG_CDR_BICLTD1_GEN1_VAL(0x02);
746 writel(tmp, phyd + PHYD_CTRL_SIGNAL_MODE4);
747
748 tmp = readl(phyd + PHYD_DESIGN_OPTION2);
749 tmp &= ~RG_LOCK_CNT_SEL_MSK;
750 tmp |= RG_LOCK_CNT_SEL_VAL(0x02);
751 writel(tmp, phyd + PHYD_DESIGN_OPTION2);
752
753 tmp = readl(phyd + PHYD_DESIGN_OPTION9);
754 tmp &= ~(RG_T2_MIN_MSK | RG_TG_MIN_MSK |
755 RG_T2_MAX_MSK | RG_TG_MAX_MSK);
756 tmp |= RG_T2_MIN_VAL(0x12) | RG_TG_MIN_VAL(0x04) |
757 RG_T2_MAX_VAL(0x31) | RG_TG_MAX_VAL(0x0e);
758 writel(tmp, phyd + PHYD_DESIGN_OPTION9);
759
760 tmp = readl(phyd + ANA_RG_CTRL_SIGNAL1);
761 tmp &= ~RG_IDRV_0DB_GEN1_MSK;
762 tmp |= RG_IDRV_0DB_GEN1_VAL(0x20);
763 writel(tmp, phyd + ANA_RG_CTRL_SIGNAL1);
764
765 tmp = readl(phyd + ANA_EQ_EYE_CTRL_SIGNAL1);
766 tmp &= ~RG_EQ_DLEQ_LFI_GEN1_MSK;
767 tmp |= RG_EQ_DLEQ_LFI_GEN1_VAL(0x03);
768 writel(tmp, phyd + ANA_EQ_EYE_CTRL_SIGNAL1);
769
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800770 dev_dbg(tphy->dev, "%s(%d)\n", __func__, instance->index);
Ryder Lee4ab26cb2017-08-03 18:01:01 +0800771}
772
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800773static void phy_v1_banks_init(struct mtk_tphy *tphy,
774 struct mtk_phy_instance *instance)
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800775{
776 struct u2phy_banks *u2_banks = &instance->u2_banks;
777 struct u3phy_banks *u3_banks = &instance->u3_banks;
778
Ryder Lee44a6d6c2017-08-03 18:01:00 +0800779 switch (instance->type) {
780 case PHY_TYPE_USB2:
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800781 u2_banks->misc = NULL;
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800782 u2_banks->fmreg = tphy->sif_base + SSUSB_SIFSLV_V1_U2FREQ;
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800783 u2_banks->com = instance->port_base + SSUSB_SIFSLV_V1_U2PHY_COM;
Ryder Lee44a6d6c2017-08-03 18:01:00 +0800784 break;
785 case PHY_TYPE_USB3:
786 case PHY_TYPE_PCIE:
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800787 u3_banks->spllc = tphy->sif_base + SSUSB_SIFSLV_V1_SPLLC;
Chunfeng Yun554a56f2017-09-21 18:31:48 +0800788 u3_banks->chip = tphy->sif_base + SSUSB_SIFSLV_V1_CHIP;
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800789 u3_banks->phyd = instance->port_base + SSUSB_SIFSLV_V1_U3PHYD;
790 u3_banks->phya = instance->port_base + SSUSB_SIFSLV_V1_U3PHYA;
Ryder Lee44a6d6c2017-08-03 18:01:00 +0800791 break;
Ryder Lee4ab26cb2017-08-03 18:01:01 +0800792 case PHY_TYPE_SATA:
793 u3_banks->phyd = instance->port_base + SSUSB_SIFSLV_V1_U3PHYD;
794 break;
Ryder Lee44a6d6c2017-08-03 18:01:00 +0800795 default:
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800796 dev_err(tphy->dev, "incompatible PHY type\n");
Ryder Lee44a6d6c2017-08-03 18:01:00 +0800797 return;
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800798 }
799}
800
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800801static void phy_v2_banks_init(struct mtk_tphy *tphy,
802 struct mtk_phy_instance *instance)
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800803{
804 struct u2phy_banks *u2_banks = &instance->u2_banks;
805 struct u3phy_banks *u3_banks = &instance->u3_banks;
806
Ryder Lee44a6d6c2017-08-03 18:01:00 +0800807 switch (instance->type) {
808 case PHY_TYPE_USB2:
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800809 u2_banks->misc = instance->port_base + SSUSB_SIFSLV_V2_MISC;
810 u2_banks->fmreg = instance->port_base + SSUSB_SIFSLV_V2_U2FREQ;
811 u2_banks->com = instance->port_base + SSUSB_SIFSLV_V2_U2PHY_COM;
Ryder Lee44a6d6c2017-08-03 18:01:00 +0800812 break;
813 case PHY_TYPE_USB3:
814 case PHY_TYPE_PCIE:
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800815 u3_banks->spllc = instance->port_base + SSUSB_SIFSLV_V2_SPLLC;
816 u3_banks->chip = instance->port_base + SSUSB_SIFSLV_V2_CHIP;
817 u3_banks->phyd = instance->port_base + SSUSB_SIFSLV_V2_U3PHYD;
818 u3_banks->phya = instance->port_base + SSUSB_SIFSLV_V2_U3PHYA;
Ryder Lee44a6d6c2017-08-03 18:01:00 +0800819 break;
820 default:
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800821 dev_err(tphy->dev, "incompatible PHY type\n");
Ryder Lee44a6d6c2017-08-03 18:01:00 +0800822 return;
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800823 }
824}
825
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800826static int mtk_phy_init(struct phy *phy)
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800827{
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800828 struct mtk_phy_instance *instance = phy_get_drvdata(phy);
829 struct mtk_tphy *tphy = dev_get_drvdata(phy->dev.parent);
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800830 int ret;
831
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800832 ret = clk_prepare_enable(tphy->u3phya_ref);
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800833 if (ret) {
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800834 dev_err(tphy->dev, "failed to enable u3phya_ref\n");
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800835 return ret;
836 }
837
Chunfeng Yun15de15c2017-03-31 15:35:30 +0800838 ret = clk_prepare_enable(instance->ref_clk);
839 if (ret) {
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800840 dev_err(tphy->dev, "failed to enable ref_clk\n");
Chunfeng Yun15de15c2017-03-31 15:35:30 +0800841 return ret;
842 }
843
Ryder Lee44a6d6c2017-08-03 18:01:00 +0800844 switch (instance->type) {
845 case PHY_TYPE_USB2:
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800846 u2_phy_instance_init(tphy, instance);
Ryder Lee44a6d6c2017-08-03 18:01:00 +0800847 break;
848 case PHY_TYPE_USB3:
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800849 u3_phy_instance_init(tphy, instance);
Ryder Lee44a6d6c2017-08-03 18:01:00 +0800850 break;
851 case PHY_TYPE_PCIE:
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800852 pcie_phy_instance_init(tphy, instance);
Ryder Lee44a6d6c2017-08-03 18:01:00 +0800853 break;
Ryder Lee4ab26cb2017-08-03 18:01:01 +0800854 case PHY_TYPE_SATA:
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800855 sata_phy_instance_init(tphy, instance);
Ryder Lee4ab26cb2017-08-03 18:01:01 +0800856 break;
Ryder Lee44a6d6c2017-08-03 18:01:00 +0800857 default:
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800858 dev_err(tphy->dev, "incompatible PHY type\n");
Ryder Lee44a6d6c2017-08-03 18:01:00 +0800859 return -EINVAL;
860 }
Chunfeng Yun04466ef2017-03-31 15:35:29 +0800861
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800862 return 0;
863}
864
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800865static int mtk_phy_power_on(struct phy *phy)
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800866{
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800867 struct mtk_phy_instance *instance = phy_get_drvdata(phy);
868 struct mtk_tphy *tphy = dev_get_drvdata(phy->dev.parent);
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800869
Chunfeng Yun04466ef2017-03-31 15:35:29 +0800870 if (instance->type == PHY_TYPE_USB2) {
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800871 u2_phy_instance_power_on(tphy, instance);
872 hs_slew_rate_calibrate(tphy, instance);
Ryder Lee44a6d6c2017-08-03 18:01:00 +0800873 } else if (instance->type == PHY_TYPE_PCIE) {
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800874 pcie_phy_instance_power_on(tphy, instance);
Chunfeng Yun04466ef2017-03-31 15:35:29 +0800875 }
Ryder Lee44a6d6c2017-08-03 18:01:00 +0800876
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800877 return 0;
878}
879
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800880static int mtk_phy_power_off(struct phy *phy)
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800881{
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800882 struct mtk_phy_instance *instance = phy_get_drvdata(phy);
883 struct mtk_tphy *tphy = dev_get_drvdata(phy->dev.parent);
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800884
Chunfeng Yun04466ef2017-03-31 15:35:29 +0800885 if (instance->type == PHY_TYPE_USB2)
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800886 u2_phy_instance_power_off(tphy, instance);
Ryder Lee44a6d6c2017-08-03 18:01:00 +0800887 else if (instance->type == PHY_TYPE_PCIE)
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800888 pcie_phy_instance_power_off(tphy, instance);
Chunfeng Yun04466ef2017-03-31 15:35:29 +0800889
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800890 return 0;
891}
892
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800893static int mtk_phy_exit(struct phy *phy)
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800894{
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800895 struct mtk_phy_instance *instance = phy_get_drvdata(phy);
896 struct mtk_tphy *tphy = dev_get_drvdata(phy->dev.parent);
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800897
Chunfeng Yun04466ef2017-03-31 15:35:29 +0800898 if (instance->type == PHY_TYPE_USB2)
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800899 u2_phy_instance_exit(tphy, instance);
Chunfeng Yun04466ef2017-03-31 15:35:29 +0800900
Chunfeng Yun15de15c2017-03-31 15:35:30 +0800901 clk_disable_unprepare(instance->ref_clk);
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800902 clk_disable_unprepare(tphy->u3phya_ref);
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800903 return 0;
904}
905
Chunfeng Yun5954a102017-09-21 18:31:49 +0800906static int mtk_phy_set_mode(struct phy *phy, enum phy_mode mode)
907{
908 struct mtk_phy_instance *instance = phy_get_drvdata(phy);
909 struct mtk_tphy *tphy = dev_get_drvdata(phy->dev.parent);
910
911 if (instance->type == PHY_TYPE_USB2)
912 u2_phy_instance_set_mode(tphy, instance, mode);
913
914 return 0;
915}
916
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800917static struct phy *mtk_phy_xlate(struct device *dev,
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800918 struct of_phandle_args *args)
919{
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800920 struct mtk_tphy *tphy = dev_get_drvdata(dev);
921 struct mtk_phy_instance *instance = NULL;
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800922 struct device_node *phy_np = args->np;
923 int index;
924
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800925 if (args->args_count != 1) {
926 dev_err(dev, "invalid number of cells in 'phy' property\n");
927 return ERR_PTR(-EINVAL);
928 }
929
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800930 for (index = 0; index < tphy->nphys; index++)
931 if (phy_np == tphy->phys[index]->phy->dev.of_node) {
932 instance = tphy->phys[index];
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800933 break;
934 }
935
936 if (!instance) {
937 dev_err(dev, "failed to find appropriate phy\n");
938 return ERR_PTR(-EINVAL);
939 }
940
941 instance->type = args->args[0];
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800942 if (!(instance->type == PHY_TYPE_USB2 ||
Ryder Lee44a6d6c2017-08-03 18:01:00 +0800943 instance->type == PHY_TYPE_USB3 ||
Ryder Lee4ab26cb2017-08-03 18:01:01 +0800944 instance->type == PHY_TYPE_PCIE ||
945 instance->type == PHY_TYPE_SATA)) {
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800946 dev_err(dev, "unsupported device type: %d\n", instance->type);
947 return ERR_PTR(-EINVAL);
948 }
949
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800950 if (tphy->pdata->version == MTK_PHY_V1) {
951 phy_v1_banks_init(tphy, instance);
952 } else if (tphy->pdata->version == MTK_PHY_V2) {
953 phy_v2_banks_init(tphy, instance);
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800954 } else {
955 dev_err(dev, "phy version is not supported\n");
956 return ERR_PTR(-EINVAL);
957 }
958
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800959 return instance->phy;
960}
961
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800962static const struct phy_ops mtk_tphy_ops = {
963 .init = mtk_phy_init,
964 .exit = mtk_phy_exit,
965 .power_on = mtk_phy_power_on,
966 .power_off = mtk_phy_power_off,
Chunfeng Yun5954a102017-09-21 18:31:49 +0800967 .set_mode = mtk_phy_set_mode,
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800968 .owner = THIS_MODULE,
969};
970
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800971static const struct mtk_phy_pdata tphy_v1_pdata = {
Chunfeng Yune1d76532016-04-20 08:14:02 +0800972 .avoid_rx_sen_degradation = false,
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800973 .version = MTK_PHY_V1,
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800974};
975
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800976static const struct mtk_phy_pdata tphy_v2_pdata = {
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800977 .avoid_rx_sen_degradation = false,
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800978 .version = MTK_PHY_V2,
Chunfeng Yune1d76532016-04-20 08:14:02 +0800979};
980
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800981static const struct mtk_phy_pdata mt8173_pdata = {
Chunfeng Yune1d76532016-04-20 08:14:02 +0800982 .avoid_rx_sen_degradation = true,
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800983 .version = MTK_PHY_V1,
Chunfeng Yune1d76532016-04-20 08:14:02 +0800984};
985
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800986static const struct of_device_id mtk_tphy_id_table[] = {
Ryder Lee44a6d6c2017-08-03 18:01:00 +0800987 { .compatible = "mediatek,mt2701-u3phy", .data = &tphy_v1_pdata },
Ryder Lee4ab26cb2017-08-03 18:01:01 +0800988 { .compatible = "mediatek,mt2712-u3phy", .data = &tphy_v2_pdata },
Chunfeng Yune1d76532016-04-20 08:14:02 +0800989 { .compatible = "mediatek,mt8173-u3phy", .data = &mt8173_pdata },
Ryder Lee44a6d6c2017-08-03 18:01:00 +0800990 { .compatible = "mediatek,generic-tphy-v1", .data = &tphy_v1_pdata },
Ryder Lee4ab26cb2017-08-03 18:01:01 +0800991 { .compatible = "mediatek,generic-tphy-v2", .data = &tphy_v2_pdata },
Chunfeng Yune1d76532016-04-20 08:14:02 +0800992 { },
993};
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800994MODULE_DEVICE_TABLE(of, mtk_tphy_id_table);
Chunfeng Yune1d76532016-04-20 08:14:02 +0800995
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800996static int mtk_tphy_probe(struct platform_device *pdev)
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800997{
998 struct device *dev = &pdev->dev;
999 struct device_node *np = dev->of_node;
1000 struct device_node *child_np;
1001 struct phy_provider *provider;
1002 struct resource *sif_res;
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +08001003 struct mtk_tphy *tphy;
Chunfeng Yundc7f1902015-09-29 11:01:36 +08001004 struct resource res;
Julia Lawall2bb80cc2015-11-16 12:33:15 +01001005 int port, retval;
Chunfeng Yundc7f1902015-09-29 11:01:36 +08001006
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +08001007 tphy = devm_kzalloc(dev, sizeof(*tphy), GFP_KERNEL);
1008 if (!tphy)
Chunfeng Yundc7f1902015-09-29 11:01:36 +08001009 return -ENOMEM;
1010
Chunfeng Yune4b227c2017-12-28 16:40:36 +05301011 tphy->pdata = of_device_get_match_data(dev);
1012 if (!tphy->pdata)
1013 return -EINVAL;
1014
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +08001015 tphy->nphys = of_get_child_count(np);
1016 tphy->phys = devm_kcalloc(dev, tphy->nphys,
1017 sizeof(*tphy->phys), GFP_KERNEL);
1018 if (!tphy->phys)
Chunfeng Yundc7f1902015-09-29 11:01:36 +08001019 return -ENOMEM;
1020
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +08001021 tphy->dev = dev;
1022 platform_set_drvdata(pdev, tphy);
Chunfeng Yundc7f1902015-09-29 11:01:36 +08001023
Chunfeng Yun93a04f42017-12-07 19:53:35 +08001024 sif_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1025 /* SATA phy of V1 needn't it if not shared with PCIe or USB */
1026 if (sif_res && tphy->pdata->version == MTK_PHY_V1) {
Chunfeng Yun8d6e19572017-03-31 15:35:31 +08001027 /* get banks shared by multiple phys */
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +08001028 tphy->sif_base = devm_ioremap_resource(dev, sif_res);
1029 if (IS_ERR(tphy->sif_base)) {
Chunfeng Yun8d6e19572017-03-31 15:35:31 +08001030 dev_err(dev, "failed to remap sif regs\n");
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +08001031 return PTR_ERR(tphy->sif_base);
Chunfeng Yun8d6e19572017-03-31 15:35:31 +08001032 }
Chunfeng Yundc7f1902015-09-29 11:01:36 +08001033 }
1034
Chunfeng Yun15de15c2017-03-31 15:35:30 +08001035 /* it's deprecated, make it optional for backward compatibility */
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +08001036 tphy->u3phya_ref = devm_clk_get(dev, "u3phya_ref");
1037 if (IS_ERR(tphy->u3phya_ref)) {
1038 if (PTR_ERR(tphy->u3phya_ref) == -EPROBE_DEFER)
Chunfeng Yun15de15c2017-03-31 15:35:30 +08001039 return -EPROBE_DEFER;
1040
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +08001041 tphy->u3phya_ref = NULL;
Chunfeng Yundc7f1902015-09-29 11:01:36 +08001042 }
1043
1044 port = 0;
1045 for_each_child_of_node(np, child_np) {
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +08001046 struct mtk_phy_instance *instance;
Chunfeng Yundc7f1902015-09-29 11:01:36 +08001047 struct phy *phy;
Chunfeng Yundc7f1902015-09-29 11:01:36 +08001048
1049 instance = devm_kzalloc(dev, sizeof(*instance), GFP_KERNEL);
Julia Lawall2bb80cc2015-11-16 12:33:15 +01001050 if (!instance) {
1051 retval = -ENOMEM;
1052 goto put_child;
1053 }
Chunfeng Yundc7f1902015-09-29 11:01:36 +08001054
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +08001055 tphy->phys[port] = instance;
Chunfeng Yundc7f1902015-09-29 11:01:36 +08001056
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +08001057 phy = devm_phy_create(dev, child_np, &mtk_tphy_ops);
Chunfeng Yundc7f1902015-09-29 11:01:36 +08001058 if (IS_ERR(phy)) {
1059 dev_err(dev, "failed to create phy\n");
Julia Lawall2bb80cc2015-11-16 12:33:15 +01001060 retval = PTR_ERR(phy);
1061 goto put_child;
Chunfeng Yundc7f1902015-09-29 11:01:36 +08001062 }
1063
1064 retval = of_address_to_resource(child_np, 0, &res);
1065 if (retval) {
1066 dev_err(dev, "failed to get address resource(id-%d)\n",
1067 port);
Julia Lawall2bb80cc2015-11-16 12:33:15 +01001068 goto put_child;
Chunfeng Yundc7f1902015-09-29 11:01:36 +08001069 }
1070
1071 instance->port_base = devm_ioremap_resource(&phy->dev, &res);
1072 if (IS_ERR(instance->port_base)) {
1073 dev_err(dev, "failed to remap phy regs\n");
Julia Lawall2bb80cc2015-11-16 12:33:15 +01001074 retval = PTR_ERR(instance->port_base);
1075 goto put_child;
Chunfeng Yundc7f1902015-09-29 11:01:36 +08001076 }
1077
1078 instance->phy = phy;
1079 instance->index = port;
1080 phy_set_drvdata(phy, instance);
1081 port++;
Chunfeng Yun15de15c2017-03-31 15:35:30 +08001082
1083 /* if deprecated clock is provided, ignore instance's one */
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +08001084 if (tphy->u3phya_ref)
Chunfeng Yun15de15c2017-03-31 15:35:30 +08001085 continue;
1086
1087 instance->ref_clk = devm_clk_get(&phy->dev, "ref");
1088 if (IS_ERR(instance->ref_clk)) {
1089 dev_err(dev, "failed to get ref_clk(id-%d)\n", port);
1090 retval = PTR_ERR(instance->ref_clk);
1091 goto put_child;
1092 }
Chunfeng Yundc7f1902015-09-29 11:01:36 +08001093 }
1094
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +08001095 provider = devm_of_phy_provider_register(dev, mtk_phy_xlate);
Chunfeng Yundc7f1902015-09-29 11:01:36 +08001096
1097 return PTR_ERR_OR_ZERO(provider);
Julia Lawall2bb80cc2015-11-16 12:33:15 +01001098put_child:
1099 of_node_put(child_np);
1100 return retval;
Chunfeng Yundc7f1902015-09-29 11:01:36 +08001101}
1102
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +08001103static struct platform_driver mtk_tphy_driver = {
1104 .probe = mtk_tphy_probe,
Chunfeng Yundc7f1902015-09-29 11:01:36 +08001105 .driver = {
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +08001106 .name = "mtk-tphy",
1107 .of_match_table = mtk_tphy_id_table,
Chunfeng Yundc7f1902015-09-29 11:01:36 +08001108 },
1109};
1110
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +08001111module_platform_driver(mtk_tphy_driver);
Chunfeng Yundc7f1902015-09-29 11:01:36 +08001112
1113MODULE_AUTHOR("Chunfeng Yun <chunfeng.yun@mediatek.com>");
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +08001114MODULE_DESCRIPTION("MediaTek T-PHY driver");
Chunfeng Yundc7f1902015-09-29 11:01:36 +08001115MODULE_LICENSE("GPL v2");