blob: 8ee7682b8e93eb86346d1e55e1f6963a5f8f77d0 [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>
Chunfeng Yun75f072f2015-12-04 10:11:05 +080011#include <linux/iopoll.h>
Chunfeng Yun39099a42021-08-17 17:19:41 +080012#include <linux/mfd/syscon.h>
Chunfeng Yundc7f1902015-09-29 11:01:36 +080013#include <linux/module.h>
Chunfeng Yun6f2b0332021-12-18 16:27:59 +080014#include <linux/nvmem-consumer.h>
Chunfeng Yundc7f1902015-09-29 11:01:36 +080015#include <linux/of_address.h>
Chunfeng Yune4b227c2017-12-28 16:40:36 +053016#include <linux/of_device.h>
Chunfeng Yundc7f1902015-09-29 11:01:36 +080017#include <linux/phy/phy.h>
18#include <linux/platform_device.h>
Chunfeng Yun39099a42021-08-17 17:19:41 +080019#include <linux/regmap.h>
Chunfeng Yundc7f1902015-09-29 11:01:36 +080020
Chunfeng Yun33d18742021-12-18 16:28:02 +080021#include "phy-mtk-io.h"
22
Chunfeng Yun8d6e19572017-03-31 15:35:31 +080023/* version V1 sub-banks offset base address */
24/* banks shared by multiple phys */
25#define SSUSB_SIFSLV_V1_SPLLC 0x000 /* shared by u3 phys */
26#define SSUSB_SIFSLV_V1_U2FREQ 0x100 /* shared by u2 phys */
Chunfeng Yun554a56f2017-09-21 18:31:48 +080027#define SSUSB_SIFSLV_V1_CHIP 0x300 /* shared by u3 phys */
Chunfeng Yun8d6e19572017-03-31 15:35:31 +080028/* u2 phy bank */
29#define SSUSB_SIFSLV_V1_U2PHY_COM 0x000
Ryder Lee4ab26cb2017-08-03 18:01:01 +080030/* u3/pcie/sata phy banks */
Chunfeng Yun8d6e19572017-03-31 15:35:31 +080031#define SSUSB_SIFSLV_V1_U3PHYD 0x000
32#define SSUSB_SIFSLV_V1_U3PHYA 0x200
Chunfeng Yundc7f1902015-09-29 11:01:36 +080033
Chunfeng Yun27974e62021-07-23 16:22:41 +080034/* version V2/V3 sub-banks offset base address */
35/* V3: U2FREQ is not used anymore, but reserved */
Chunfeng Yun8d6e19572017-03-31 15:35:31 +080036/* u2 phy banks */
37#define SSUSB_SIFSLV_V2_MISC 0x000
38#define SSUSB_SIFSLV_V2_U2FREQ 0x100
39#define SSUSB_SIFSLV_V2_U2PHY_COM 0x300
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +080040/* u3/pcie/sata phy banks */
Chunfeng Yun8d6e19572017-03-31 15:35:31 +080041#define SSUSB_SIFSLV_V2_SPLLC 0x000
42#define SSUSB_SIFSLV_V2_CHIP 0x100
43#define SSUSB_SIFSLV_V2_U3PHYD 0x200
44#define SSUSB_SIFSLV_V2_U3PHYA 0x400
Chunfeng Yundc7f1902015-09-29 11:01:36 +080045
Chunfeng Yun6f2b0332021-12-18 16:27:59 +080046#define U3P_MISC_REG1 0x04
47#define MR1_EFUSE_AUTO_LOAD_DIS BIT(6)
48
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 Yuna69f29c2021-07-23 16:22:42 +080051#define PA0_USB20_PLL_PREDIV GENMASK(7, 6)
52#define PA0_USB20_PLL_PREDIV_VAL(x) ((0x3 & (x)) << 6)
Chunfeng Yunc0250fe2017-03-31 15:35:32 +080053#define PA0_RG_USB20_INTR_EN BIT(5)
Chunfeng Yundc7f1902015-09-29 11:01:36 +080054
Chunfeng Yun8158e912018-06-29 10:20:29 +080055#define U3P_USBPHYACR1 0x004
Chunfeng Yun410572e2020-02-11 11:21:12 +080056#define PA1_RG_INTR_CAL GENMASK(23, 19)
57#define PA1_RG_INTR_CAL_VAL(x) ((0x1f & (x)) << 19)
Chunfeng Yun8158e912018-06-29 10:20:29 +080058#define PA1_RG_VRT_SEL GENMASK(14, 12)
59#define PA1_RG_VRT_SEL_VAL(x) ((0x7 & (x)) << 12)
60#define PA1_RG_TERM_SEL GENMASK(10, 8)
61#define PA1_RG_TERM_SEL_VAL(x) ((0x7 & (x)) << 8)
62
Chunfeng Yun8d6e19572017-03-31 15:35:31 +080063#define U3P_USBPHYACR2 0x008
Chunfeng Yuna69f29c2021-07-23 16:22:42 +080064#define PA2_RG_U2PLL_BW GENMASK(21, 19)
65#define PA2_RG_U2PLL_BW_VAL(x) ((0x7 & (x)) << 19)
Chunfeng Yundc7f1902015-09-29 11:01:36 +080066#define PA2_RG_SIF_U2PLL_FORCE_EN BIT(18)
67
Chunfeng Yun8d6e19572017-03-31 15:35:31 +080068#define U3P_USBPHYACR5 0x014
Chunfeng Yun75f072f2015-12-04 10:11:05 +080069#define PA5_RG_U2_HSTX_SRCAL_EN BIT(15)
Chunfeng Yundc7f1902015-09-29 11:01:36 +080070#define PA5_RG_U2_HSTX_SRCTRL GENMASK(14, 12)
71#define PA5_RG_U2_HSTX_SRCTRL_VAL(x) ((0x7 & (x)) << 12)
72#define PA5_RG_U2_HS_100U_U3_EN BIT(11)
73
Chunfeng Yun8d6e19572017-03-31 15:35:31 +080074#define U3P_USBPHYACR6 0x018
Chunfeng Yundc7f1902015-09-29 11:01:36 +080075#define PA6_RG_U2_BC11_SW_EN BIT(23)
76#define PA6_RG_U2_OTG_VBUSCMP_EN BIT(20)
Chunfeng Yun8be5a672020-01-08 09:52:01 +080077#define PA6_RG_U2_DISCTH GENMASK(7, 4)
78#define PA6_RG_U2_DISCTH_VAL(x) ((0xf & (x)) << 4)
Chunfeng Yun43f53b12015-12-04 10:08:56 +080079#define PA6_RG_U2_SQTH GENMASK(3, 0)
80#define PA6_RG_U2_SQTH_VAL(x) (0xf & (x))
Chunfeng Yundc7f1902015-09-29 11:01:36 +080081
Chunfeng Yun8d6e19572017-03-31 15:35:31 +080082#define U3P_U2PHYACR4 0x020
Chunfeng Yundc7f1902015-09-29 11:01:36 +080083#define P2C_RG_USB20_GPIO_CTL BIT(9)
84#define P2C_USB20_GPIO_MODE BIT(8)
85#define P2C_U2_GPIO_CTR_MSK (P2C_RG_USB20_GPIO_CTL | P2C_USB20_GPIO_MODE)
86
Chunfeng Yuna69f29c2021-07-23 16:22:42 +080087#define U3P_U2PHYA_RESV 0x030
88#define P2R_RG_U2PLL_FBDIV_26M 0x1bb13b
89#define P2R_RG_U2PLL_FBDIV_48M 0x3c0000
90
91#define U3P_U2PHYA_RESV1 0x044
92#define P2R_RG_U2PLL_REFCLK_SEL BIT(5)
93#define P2R_RG_U2PLL_FRA_EN BIT(3)
94
Chunfeng Yun8d6e19572017-03-31 15:35:31 +080095#define U3D_U2PHYDCR0 0x060
Chunfeng Yundc7f1902015-09-29 11:01:36 +080096#define P2C_RG_SIF_U2PLL_FORCE_ON BIT(24)
97
Chunfeng Yun8d6e19572017-03-31 15:35:31 +080098#define U3P_U2PHYDTM0 0x068
Chunfeng Yundc7f1902015-09-29 11:01:36 +080099#define P2C_FORCE_UART_EN BIT(26)
100#define P2C_FORCE_DATAIN BIT(23)
101#define P2C_FORCE_DM_PULLDOWN BIT(21)
102#define P2C_FORCE_DP_PULLDOWN BIT(20)
103#define P2C_FORCE_XCVRSEL BIT(19)
104#define P2C_FORCE_SUSPENDM BIT(18)
105#define P2C_FORCE_TERMSEL BIT(17)
106#define P2C_RG_DATAIN GENMASK(13, 10)
107#define P2C_RG_DATAIN_VAL(x) ((0xf & (x)) << 10)
108#define P2C_RG_DMPULLDOWN BIT(7)
109#define P2C_RG_DPPULLDOWN BIT(6)
110#define P2C_RG_XCVRSEL GENMASK(5, 4)
111#define P2C_RG_XCVRSEL_VAL(x) ((0x3 & (x)) << 4)
112#define P2C_RG_SUSPENDM BIT(3)
113#define P2C_RG_TERMSEL BIT(2)
114#define P2C_DTM0_PART_MASK \
115 (P2C_FORCE_DATAIN | P2C_FORCE_DM_PULLDOWN | \
116 P2C_FORCE_DP_PULLDOWN | P2C_FORCE_XCVRSEL | \
117 P2C_FORCE_TERMSEL | P2C_RG_DMPULLDOWN | \
118 P2C_RG_DPPULLDOWN | P2C_RG_TERMSEL)
119
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800120#define U3P_U2PHYDTM1 0x06C
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800121#define P2C_RG_UART_EN BIT(16)
Chunfeng Yun5954a102017-09-21 18:31:49 +0800122#define P2C_FORCE_IDDIG BIT(9)
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800123#define P2C_RG_VBUSVALID BIT(5)
124#define P2C_RG_SESSEND BIT(4)
125#define P2C_RG_AVALID BIT(2)
Chunfeng Yun5954a102017-09-21 18:31:49 +0800126#define P2C_RG_IDDIG BIT(1)
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800127
Chunfeng Yund4f97f12018-06-29 10:20:30 +0800128#define U3P_U2PHYBC12C 0x080
129#define P2C_RG_CHGDT_EN BIT(0)
130
Ryder Lee44a6d6c2017-08-03 18:01:00 +0800131#define U3P_U3_CHIP_GPIO_CTLD 0x0c
132#define P3C_REG_IP_SW_RST BIT(31)
133#define P3C_MCU_BUS_CK_GATE_EN BIT(30)
134#define P3C_FORCE_IP_SW_RST BIT(29)
135
136#define U3P_U3_CHIP_GPIO_CTLE 0x10
137#define P3C_RG_SWRST_U3_PHYD BIT(25)
138#define P3C_RG_SWRST_U3_PHYD_FORCE_EN BIT(24)
139
140#define U3P_U3_PHYA_REG0 0x000
Chunfeng Yun6f2b0332021-12-18 16:27:59 +0800141#define P3A_RG_IEXT_INTR GENMASK(15, 10)
142#define P3A_RG_IEXT_INTR_VAL(x) ((0x3f & (x)) << 10)
Ryder Lee44a6d6c2017-08-03 18:01:00 +0800143#define P3A_RG_CLKDRV_OFF GENMASK(3, 2)
144#define P3A_RG_CLKDRV_OFF_VAL(x) ((0x3 & (x)) << 2)
145
146#define U3P_U3_PHYA_REG1 0x004
147#define P3A_RG_CLKDRV_AMP GENMASK(31, 29)
148#define P3A_RG_CLKDRV_AMP_VAL(x) ((0x7 & (x)) << 29)
149
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800150#define U3P_U3_PHYA_REG6 0x018
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800151#define P3A_RG_TX_EIDLE_CM GENMASK(31, 28)
152#define P3A_RG_TX_EIDLE_CM_VAL(x) ((0xf & (x)) << 28)
153
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800154#define U3P_U3_PHYA_REG9 0x024
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800155#define P3A_RG_RX_DAC_MUX GENMASK(5, 1)
156#define P3A_RG_RX_DAC_MUX_VAL(x) ((0x1f & (x)) << 1)
157
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800158#define U3P_U3_PHYA_DA_REG0 0x100
Ryder Lee44a6d6c2017-08-03 18:01:00 +0800159#define P3A_RG_XTAL_EXT_PE2H GENMASK(17, 16)
160#define P3A_RG_XTAL_EXT_PE2H_VAL(x) ((0x3 & (x)) << 16)
161#define P3A_RG_XTAL_EXT_PE1H GENMASK(13, 12)
162#define P3A_RG_XTAL_EXT_PE1H_VAL(x) ((0x3 & (x)) << 12)
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800163#define P3A_RG_XTAL_EXT_EN_U3 GENMASK(11, 10)
164#define P3A_RG_XTAL_EXT_EN_U3_VAL(x) ((0x3 & (x)) << 10)
165
Ryder Lee44a6d6c2017-08-03 18:01:00 +0800166#define U3P_U3_PHYA_DA_REG4 0x108
167#define P3A_RG_PLL_DIVEN_PE2H GENMASK(21, 19)
168#define P3A_RG_PLL_BC_PE2H GENMASK(7, 6)
169#define P3A_RG_PLL_BC_PE2H_VAL(x) ((0x3 & (x)) << 6)
170
171#define U3P_U3_PHYA_DA_REG5 0x10c
172#define P3A_RG_PLL_BR_PE2H GENMASK(29, 28)
173#define P3A_RG_PLL_BR_PE2H_VAL(x) ((0x3 & (x)) << 28)
174#define P3A_RG_PLL_IC_PE2H GENMASK(15, 12)
175#define P3A_RG_PLL_IC_PE2H_VAL(x) ((0xf & (x)) << 12)
176
177#define U3P_U3_PHYA_DA_REG6 0x110
178#define P3A_RG_PLL_IR_PE2H GENMASK(19, 16)
179#define P3A_RG_PLL_IR_PE2H_VAL(x) ((0xf & (x)) << 16)
180
181#define U3P_U3_PHYA_DA_REG7 0x114
182#define P3A_RG_PLL_BP_PE2H GENMASK(19, 16)
183#define P3A_RG_PLL_BP_PE2H_VAL(x) ((0xf & (x)) << 16)
184
185#define U3P_U3_PHYA_DA_REG20 0x13c
186#define P3A_RG_PLL_DELTA1_PE2H GENMASK(31, 16)
187#define P3A_RG_PLL_DELTA1_PE2H_VAL(x) ((0xffff & (x)) << 16)
188
189#define U3P_U3_PHYA_DA_REG25 0x148
190#define P3A_RG_PLL_DELTA_PE2H GENMASK(15, 0)
191#define P3A_RG_PLL_DELTA_PE2H_VAL(x) (0xffff & (x))
192
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800193#define U3P_U3_PHYD_LFPS1 0x00c
Chunfeng Yun98cd83a2017-03-31 15:35:28 +0800194#define P3D_RG_FWAKE_TH GENMASK(21, 16)
195#define P3D_RG_FWAKE_TH_VAL(x) ((0x3f & (x)) << 16)
196
Chunfeng Yun6f2b0332021-12-18 16:27:59 +0800197#define U3P_U3_PHYD_IMPCAL0 0x010
198#define P3D_RG_FORCE_TX_IMPEL BIT(31)
199#define P3D_RG_TX_IMPEL GENMASK(28, 24)
200#define P3D_RG_TX_IMPEL_VAL(x) ((0x1f & (x)) << 24)
201
202#define U3P_U3_PHYD_IMPCAL1 0x014
203#define P3D_RG_FORCE_RX_IMPEL BIT(31)
204#define P3D_RG_RX_IMPEL GENMASK(28, 24)
205#define P3D_RG_RX_IMPEL_VAL(x) ((0x1f & (x)) << 24)
206
207#define U3P_U3_PHYD_RSV 0x054
208#define P3D_RG_EFUSE_AUTO_LOAD_DIS BIT(12)
209
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800210#define U3P_U3_PHYD_CDR1 0x05c
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800211#define P3D_RG_CDR_BIR_LTD1 GENMASK(28, 24)
212#define P3D_RG_CDR_BIR_LTD1_VAL(x) ((0x1f & (x)) << 24)
213#define P3D_RG_CDR_BIR_LTD0 GENMASK(12, 8)
214#define P3D_RG_CDR_BIR_LTD0_VAL(x) ((0x1f & (x)) << 8)
215
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800216#define U3P_U3_PHYD_RXDET1 0x128
Chunfeng Yun1969f692017-03-31 15:35:27 +0800217#define P3D_RG_RXDET_STB2_SET GENMASK(17, 9)
218#define P3D_RG_RXDET_STB2_SET_VAL(x) ((0x1ff & (x)) << 9)
219
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800220#define U3P_U3_PHYD_RXDET2 0x12c
Chunfeng Yun1969f692017-03-31 15:35:27 +0800221#define P3D_RG_RXDET_STB2_SET_P3 GENMASK(8, 0)
222#define P3D_RG_RXDET_STB2_SET_P3_VAL(x) (0x1ff & (x))
223
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800224#define U3P_SPLLC_XTALCTL3 0x018
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800225#define XC3_RG_U3_XTAL_RX_PWD BIT(9)
226#define XC3_RG_U3_FRC_XTAL_RX_PWD BIT(8)
227
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800228#define U3P_U2FREQ_FMCR0 0x00
Chunfeng Yun75f072f2015-12-04 10:11:05 +0800229#define P2F_RG_MONCLK_SEL GENMASK(27, 26)
230#define P2F_RG_MONCLK_SEL_VAL(x) ((0x3 & (x)) << 26)
231#define P2F_RG_FREQDET_EN BIT(24)
232#define P2F_RG_CYCLECNT GENMASK(23, 0)
233#define P2F_RG_CYCLECNT_VAL(x) ((P2F_RG_CYCLECNT) & (x))
234
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800235#define U3P_U2FREQ_VALUE 0x0c
Chunfeng Yun75f072f2015-12-04 10:11:05 +0800236
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800237#define U3P_U2FREQ_FMMONR1 0x10
Chunfeng Yun75f072f2015-12-04 10:11:05 +0800238#define P2F_USB_FM_VALID BIT(0)
239#define P2F_RG_FRCK_EN BIT(8)
240
241#define U3P_REF_CLK 26 /* MHZ */
242#define U3P_SLEW_RATE_COEF 28
243#define U3P_SR_COEF_DIVISOR 1000
244#define U3P_FM_DET_CYCLE_CNT 1024
245
Ryder Lee4ab26cb2017-08-03 18:01:01 +0800246/* SATA register setting */
247#define PHYD_CTRL_SIGNAL_MODE4 0x1c
248/* CDR Charge Pump P-path current adjustment */
249#define RG_CDR_BICLTD1_GEN1_MSK GENMASK(23, 20)
250#define RG_CDR_BICLTD1_GEN1_VAL(x) ((0xf & (x)) << 20)
251#define RG_CDR_BICLTD0_GEN1_MSK GENMASK(11, 8)
252#define RG_CDR_BICLTD0_GEN1_VAL(x) ((0xf & (x)) << 8)
253
254#define PHYD_DESIGN_OPTION2 0x24
255/* Symbol lock count selection */
256#define RG_LOCK_CNT_SEL_MSK GENMASK(5, 4)
257#define RG_LOCK_CNT_SEL_VAL(x) ((0x3 & (x)) << 4)
258
259#define PHYD_DESIGN_OPTION9 0x40
260/* COMWAK GAP width window */
261#define RG_TG_MAX_MSK GENMASK(20, 16)
262#define RG_TG_MAX_VAL(x) ((0x1f & (x)) << 16)
263/* COMINIT GAP width window */
264#define RG_T2_MAX_MSK GENMASK(13, 8)
265#define RG_T2_MAX_VAL(x) ((0x3f & (x)) << 8)
266/* COMWAK GAP width window */
267#define RG_TG_MIN_MSK GENMASK(7, 5)
268#define RG_TG_MIN_VAL(x) ((0x7 & (x)) << 5)
269/* COMINIT GAP width window */
270#define RG_T2_MIN_MSK GENMASK(4, 0)
271#define RG_T2_MIN_VAL(x) (0x1f & (x))
272
273#define ANA_RG_CTRL_SIGNAL1 0x4c
274/* TX driver tail current control for 0dB de-empahsis mdoe for Gen1 speed */
275#define RG_IDRV_0DB_GEN1_MSK GENMASK(13, 8)
276#define RG_IDRV_0DB_GEN1_VAL(x) ((0x3f & (x)) << 8)
277
278#define ANA_RG_CTRL_SIGNAL4 0x58
279#define RG_CDR_BICLTR_GEN1_MSK GENMASK(23, 20)
280#define RG_CDR_BICLTR_GEN1_VAL(x) ((0xf & (x)) << 20)
281/* Loop filter R1 resistance adjustment for Gen1 speed */
282#define RG_CDR_BR_GEN2_MSK GENMASK(10, 8)
283#define RG_CDR_BR_GEN2_VAL(x) ((0x7 & (x)) << 8)
284
285#define ANA_RG_CTRL_SIGNAL6 0x60
286/* I-path capacitance adjustment for Gen1 */
287#define RG_CDR_BC_GEN1_MSK GENMASK(28, 24)
288#define RG_CDR_BC_GEN1_VAL(x) ((0x1f & (x)) << 24)
289#define RG_CDR_BIRLTR_GEN1_MSK GENMASK(4, 0)
290#define RG_CDR_BIRLTR_GEN1_VAL(x) (0x1f & (x))
291
292#define ANA_EQ_EYE_CTRL_SIGNAL1 0x6c
293/* RX Gen1 LEQ tuning step */
294#define RG_EQ_DLEQ_LFI_GEN1_MSK GENMASK(11, 8)
295#define RG_EQ_DLEQ_LFI_GEN1_VAL(x) ((0xf & (x)) << 8)
296
297#define ANA_EQ_EYE_CTRL_SIGNAL4 0xd8
298#define RG_CDR_BIRLTD0_GEN1_MSK GENMASK(20, 16)
299#define RG_CDR_BIRLTD0_GEN1_VAL(x) ((0x1f & (x)) << 16)
300
301#define ANA_EQ_EYE_CTRL_SIGNAL5 0xdc
302#define RG_CDR_BIRLTD0_GEN3_MSK GENMASK(4, 0)
303#define RG_CDR_BIRLTD0_GEN3_VAL(x) (0x1f & (x))
304
Chunfeng Yun39099a42021-08-17 17:19:41 +0800305/* PHY switch between pcie/usb3/sgmii/sata */
306#define USB_PHY_SWITCH_CTRL 0x0
307#define RG_PHY_SW_TYPE GENMASK(3, 0)
308#define RG_PHY_SW_PCIE 0x0
309#define RG_PHY_SW_USB3 0x1
310#define RG_PHY_SW_SGMII 0x2
311#define RG_PHY_SW_SATA 0x3
312
Chunfeng Yun3fd66112021-08-17 17:19:40 +0800313#define TPHY_CLKS_CNT 2
314
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800315enum mtk_phy_version {
316 MTK_PHY_V1 = 1,
317 MTK_PHY_V2,
Chunfeng Yun27974e62021-07-23 16:22:41 +0800318 MTK_PHY_V3,
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800319};
320
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800321struct mtk_phy_pdata {
Chunfeng Yune1d76532016-04-20 08:14:02 +0800322 /* avoid RX sensitivity level degradation only for mt8173 */
323 bool avoid_rx_sen_degradation;
Chunfeng Yuna69f29c2021-07-23 16:22:42 +0800324 /*
325 * workaround only for mt8195, HW fix it for others of V3,
326 * u2phy should use integer mode instead of fractional mode of
327 * 48M PLL, fix it by switching PLL to 26M from default 48M
328 */
329 bool sw_pll_48m_to_26m;
Chunfeng Yun6f2b0332021-12-18 16:27:59 +0800330 /*
331 * Some SoCs (e.g. mt8195) drop a bit when use auto load efuse,
332 * support sw way, also support it for v2/v3 optionally.
333 */
334 bool sw_efuse_supported;
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800335 enum mtk_phy_version version;
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800336};
337
338struct u2phy_banks {
339 void __iomem *misc;
340 void __iomem *fmreg;
341 void __iomem *com;
342};
343
344struct u3phy_banks {
345 void __iomem *spllc;
346 void __iomem *chip;
347 void __iomem *phyd; /* include u3phyd_bank2 */
348 void __iomem *phya; /* include u3phya_da */
Chunfeng Yune1d76532016-04-20 08:14:02 +0800349};
350
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800351struct mtk_phy_instance {
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800352 struct phy *phy;
353 void __iomem *port_base;
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800354 union {
355 struct u2phy_banks u2_banks;
356 struct u3phy_banks u3_banks;
357 };
Chunfeng Yun3fd66112021-08-17 17:19:40 +0800358 struct clk_bulk_data clks[TPHY_CLKS_CNT];
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800359 u32 index;
Chunfeng Yun39099a42021-08-17 17:19:41 +0800360 u32 type;
361 struct regmap *type_sw;
362 u32 type_sw_reg;
363 u32 type_sw_index;
Chunfeng Yun6f2b0332021-12-18 16:27:59 +0800364 u32 efuse_sw_en;
365 u32 efuse_intr;
366 u32 efuse_tx_imp;
367 u32 efuse_rx_imp;
Chunfeng Yun8158e912018-06-29 10:20:29 +0800368 int eye_src;
369 int eye_vrt;
370 int eye_term;
Chunfeng Yun410572e2020-02-11 11:21:12 +0800371 int intr;
Chunfeng Yun8be5a672020-01-08 09:52:01 +0800372 int discth;
Chunfeng Yund4f97f12018-06-29 10:20:30 +0800373 bool bc12_en;
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800374};
375
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800376struct mtk_tphy {
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800377 struct device *dev;
Chunfeng Yun04466ef2017-03-31 15:35:29 +0800378 void __iomem *sif_base; /* only shared sif */
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800379 const struct mtk_phy_pdata *pdata;
380 struct mtk_phy_instance **phys;
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800381 int nphys;
Chunfeng Yun8833ebf42018-03-12 13:25:39 +0800382 int src_ref_clk; /* MHZ, reference clock for slew rate calibrate */
383 int src_coef; /* coefficient for slew rate calibrate */
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800384};
385
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800386static void hs_slew_rate_calibrate(struct mtk_tphy *tphy,
387 struct mtk_phy_instance *instance)
Chunfeng Yun75f072f2015-12-04 10:11:05 +0800388{
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800389 struct u2phy_banks *u2_banks = &instance->u2_banks;
390 void __iomem *fmreg = u2_banks->fmreg;
391 void __iomem *com = u2_banks->com;
Chunfeng Yun75f072f2015-12-04 10:11:05 +0800392 int calibration_val;
393 int fm_out;
394 u32 tmp;
395
Chunfeng Yun27974e62021-07-23 16:22:41 +0800396 /* HW V3 doesn't support slew rate cal anymore */
397 if (tphy->pdata->version == MTK_PHY_V3)
398 return;
399
Chunfeng Yun8158e912018-06-29 10:20:29 +0800400 /* use force value */
401 if (instance->eye_src)
402 return;
403
Chunfeng Yun75f072f2015-12-04 10:11:05 +0800404 /* enable USB ring oscillator */
Chunfeng Yun33d18742021-12-18 16:28:02 +0800405 mtk_phy_set_bits(com + U3P_USBPHYACR5, PA5_RG_U2_HSTX_SRCAL_EN);
Chunfeng Yun75f072f2015-12-04 10:11:05 +0800406 udelay(1);
407
408 /*enable free run clock */
Chunfeng Yun33d18742021-12-18 16:28:02 +0800409 mtk_phy_set_bits(fmreg + U3P_U2FREQ_FMMONR1, P2F_RG_FRCK_EN);
Chunfeng Yun75f072f2015-12-04 10:11:05 +0800410
411 /* set cycle count as 1024, and select u2 channel */
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800412 tmp = readl(fmreg + U3P_U2FREQ_FMCR0);
Chunfeng Yun75f072f2015-12-04 10:11:05 +0800413 tmp &= ~(P2F_RG_CYCLECNT | P2F_RG_MONCLK_SEL);
414 tmp |= P2F_RG_CYCLECNT_VAL(U3P_FM_DET_CYCLE_CNT);
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800415 if (tphy->pdata->version == MTK_PHY_V1)
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800416 tmp |= P2F_RG_MONCLK_SEL_VAL(instance->index >> 1);
417
418 writel(tmp, fmreg + U3P_U2FREQ_FMCR0);
Chunfeng Yun75f072f2015-12-04 10:11:05 +0800419
420 /* enable frequency meter */
Chunfeng Yun33d18742021-12-18 16:28:02 +0800421 mtk_phy_set_bits(fmreg + U3P_U2FREQ_FMCR0, P2F_RG_FREQDET_EN);
Chunfeng Yun75f072f2015-12-04 10:11:05 +0800422
423 /* ignore return value */
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800424 readl_poll_timeout(fmreg + U3P_U2FREQ_FMMONR1, tmp,
425 (tmp & P2F_USB_FM_VALID), 10, 200);
Chunfeng Yun75f072f2015-12-04 10:11:05 +0800426
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800427 fm_out = readl(fmreg + U3P_U2FREQ_VALUE);
Chunfeng Yun75f072f2015-12-04 10:11:05 +0800428
429 /* disable frequency meter */
Chunfeng Yun33d18742021-12-18 16:28:02 +0800430 mtk_phy_clear_bits(fmreg + U3P_U2FREQ_FMCR0, P2F_RG_FREQDET_EN);
Chunfeng Yun75f072f2015-12-04 10:11:05 +0800431
432 /*disable free run clock */
Chunfeng Yun33d18742021-12-18 16:28:02 +0800433 mtk_phy_clear_bits(fmreg + U3P_U2FREQ_FMMONR1, P2F_RG_FRCK_EN);
Chunfeng Yun75f072f2015-12-04 10:11:05 +0800434
435 if (fm_out) {
Chunfeng Yun8833ebf42018-03-12 13:25:39 +0800436 /* ( 1024 / FM_OUT ) x reference clock frequency x coef */
437 tmp = tphy->src_ref_clk * tphy->src_coef;
438 tmp = (tmp * U3P_FM_DET_CYCLE_CNT) / fm_out;
Chunfeng Yun75f072f2015-12-04 10:11:05 +0800439 calibration_val = DIV_ROUND_CLOSEST(tmp, U3P_SR_COEF_DIVISOR);
440 } else {
441 /* if FM detection fail, set default value */
442 calibration_val = 4;
443 }
Chunfeng Yun8833ebf42018-03-12 13:25:39 +0800444 dev_dbg(tphy->dev, "phy:%d, fm_out:%d, calib:%d (clk:%d, coef:%d)\n",
445 instance->index, fm_out, calibration_val,
446 tphy->src_ref_clk, tphy->src_coef);
Chunfeng Yun75f072f2015-12-04 10:11:05 +0800447
448 /* set HS slew rate */
Chunfeng Yun33d18742021-12-18 16:28:02 +0800449 mtk_phy_update_bits(com + U3P_USBPHYACR5, PA5_RG_U2_HSTX_SRCTRL,
450 PA5_RG_U2_HSTX_SRCTRL_VAL(calibration_val));
Chunfeng Yun75f072f2015-12-04 10:11:05 +0800451
452 /* disable USB ring oscillator */
Chunfeng Yun33d18742021-12-18 16:28:02 +0800453 mtk_phy_clear_bits(com + U3P_USBPHYACR5, PA5_RG_U2_HSTX_SRCAL_EN);
Chunfeng Yun75f072f2015-12-04 10:11:05 +0800454}
455
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800456static void u3_phy_instance_init(struct mtk_tphy *tphy,
457 struct mtk_phy_instance *instance)
Chunfeng Yun04466ef2017-03-31 15:35:29 +0800458{
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800459 struct u3phy_banks *u3_banks = &instance->u3_banks;
Chunfeng Yun04466ef2017-03-31 15:35:29 +0800460
461 /* gating PCIe Analog XTAL clock */
Chunfeng Yun33d18742021-12-18 16:28:02 +0800462 mtk_phy_set_bits(u3_banks->spllc + U3P_SPLLC_XTALCTL3,
463 XC3_RG_U3_XTAL_RX_PWD | XC3_RG_U3_FRC_XTAL_RX_PWD);
Chunfeng Yun04466ef2017-03-31 15:35:29 +0800464
465 /* gating XSQ */
Chunfeng Yun33d18742021-12-18 16:28:02 +0800466 mtk_phy_update_bits(u3_banks->phya + U3P_U3_PHYA_DA_REG0,
467 P3A_RG_XTAL_EXT_EN_U3, P3A_RG_XTAL_EXT_EN_U3_VAL(2));
Chunfeng Yun04466ef2017-03-31 15:35:29 +0800468
Chunfeng Yun33d18742021-12-18 16:28:02 +0800469 mtk_phy_update_bits(u3_banks->phya + U3P_U3_PHYA_REG9,
470 P3A_RG_RX_DAC_MUX, P3A_RG_RX_DAC_MUX_VAL(4));
Chunfeng Yun04466ef2017-03-31 15:35:29 +0800471
Chunfeng Yun33d18742021-12-18 16:28:02 +0800472 mtk_phy_update_bits(u3_banks->phya + U3P_U3_PHYA_REG6,
473 P3A_RG_TX_EIDLE_CM, P3A_RG_TX_EIDLE_CM_VAL(0xe));
Chunfeng Yun04466ef2017-03-31 15:35:29 +0800474
Chunfeng Yun33d18742021-12-18 16:28:02 +0800475 mtk_phy_update_bits(u3_banks->phyd + U3P_U3_PHYD_CDR1,
476 P3D_RG_CDR_BIR_LTD0 | P3D_RG_CDR_BIR_LTD1,
477 P3D_RG_CDR_BIR_LTD0_VAL(0xc) | P3D_RG_CDR_BIR_LTD1_VAL(0x3));
Chunfeng Yun04466ef2017-03-31 15:35:29 +0800478
Chunfeng Yun33d18742021-12-18 16:28:02 +0800479 mtk_phy_update_bits(u3_banks->phyd + U3P_U3_PHYD_LFPS1,
480 P3D_RG_FWAKE_TH, P3D_RG_FWAKE_TH_VAL(0x34));
Chunfeng Yun04466ef2017-03-31 15:35:29 +0800481
Chunfeng Yun33d18742021-12-18 16:28:02 +0800482 mtk_phy_update_bits(u3_banks->phyd + U3P_U3_PHYD_RXDET1,
483 P3D_RG_RXDET_STB2_SET, P3D_RG_RXDET_STB2_SET_VAL(0x10));
Chunfeng Yun04466ef2017-03-31 15:35:29 +0800484
Chunfeng Yun33d18742021-12-18 16:28:02 +0800485 mtk_phy_update_bits(u3_banks->phyd + U3P_U3_PHYD_RXDET2,
486 P3D_RG_RXDET_STB2_SET_P3, P3D_RG_RXDET_STB2_SET_P3_VAL(0x10));
Chunfeng Yun04466ef2017-03-31 15:35:29 +0800487
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800488 dev_dbg(tphy->dev, "%s(%d)\n", __func__, instance->index);
Chunfeng Yun04466ef2017-03-31 15:35:29 +0800489}
490
Chunfeng Yuna69f29c2021-07-23 16:22:42 +0800491static void u2_phy_pll_26m_set(struct mtk_tphy *tphy,
492 struct mtk_phy_instance *instance)
493{
494 struct u2phy_banks *u2_banks = &instance->u2_banks;
495 void __iomem *com = u2_banks->com;
Chunfeng Yuna69f29c2021-07-23 16:22:42 +0800496
497 if (!tphy->pdata->sw_pll_48m_to_26m)
498 return;
499
Chunfeng Yun33d18742021-12-18 16:28:02 +0800500 mtk_phy_update_bits(com + U3P_USBPHYACR0, PA0_USB20_PLL_PREDIV,
501 PA0_USB20_PLL_PREDIV_VAL(0));
Chunfeng Yuna69f29c2021-07-23 16:22:42 +0800502
Chunfeng Yun33d18742021-12-18 16:28:02 +0800503 mtk_phy_update_bits(com + U3P_USBPHYACR2, PA2_RG_U2PLL_BW,
504 PA2_RG_U2PLL_BW_VAL(3));
Chunfeng Yuna69f29c2021-07-23 16:22:42 +0800505
506 writel(P2R_RG_U2PLL_FBDIV_26M, com + U3P_U2PHYA_RESV);
507
Chunfeng Yun33d18742021-12-18 16:28:02 +0800508 mtk_phy_set_bits(com + U3P_U2PHYA_RESV1,
509 P2R_RG_U2PLL_FRA_EN | P2R_RG_U2PLL_REFCLK_SEL);
Chunfeng Yuna69f29c2021-07-23 16:22:42 +0800510}
511
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800512static void u2_phy_instance_init(struct mtk_tphy *tphy,
513 struct mtk_phy_instance *instance)
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800514{
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800515 struct u2phy_banks *u2_banks = &instance->u2_banks;
516 void __iomem *com = u2_banks->com;
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800517 u32 index = instance->index;
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800518
Chunfeng Yun00c00922017-12-07 19:53:34 +0800519 /* switch to USB function, and enable usb pll */
Chunfeng Yun33d18742021-12-18 16:28:02 +0800520 mtk_phy_clear_bits(com + U3P_U2PHYDTM0, P2C_FORCE_UART_EN | P2C_FORCE_SUSPENDM);
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800521
Chunfeng Yun33d18742021-12-18 16:28:02 +0800522 mtk_phy_update_bits(com + U3P_U2PHYDTM0, P2C_RG_XCVRSEL | P2C_RG_DATAIN,
523 P2C_RG_XCVRSEL_VAL(1) | P2C_RG_DATAIN_VAL(0));
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800524
Chunfeng Yun33d18742021-12-18 16:28:02 +0800525 mtk_phy_clear_bits(com + U3P_U2PHYDTM1, P2C_RG_UART_EN);
526
527 mtk_phy_set_bits(com + U3P_USBPHYACR0, PA0_RG_USB20_INTR_EN);
Chunfeng Yunc0250fe2017-03-31 15:35:32 +0800528
529 /* disable switch 100uA current to SSUSB */
Chunfeng Yun33d18742021-12-18 16:28:02 +0800530 mtk_phy_clear_bits(com + U3P_USBPHYACR5, PA5_RG_U2_HS_100U_U3_EN);
Chunfeng Yunc0250fe2017-03-31 15:35:32 +0800531
Chunfeng Yun33d18742021-12-18 16:28:02 +0800532 if (!index)
533 mtk_phy_clear_bits(com + U3P_U2PHYACR4, P2C_U2_GPIO_CTR_MSK);
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800534
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800535 if (tphy->pdata->avoid_rx_sen_degradation) {
Chunfeng Yune1d76532016-04-20 08:14:02 +0800536 if (!index) {
Chunfeng Yun33d18742021-12-18 16:28:02 +0800537 mtk_phy_set_bits(com + U3P_USBPHYACR2, PA2_RG_SIF_U2PLL_FORCE_EN);
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800538
Chunfeng Yun33d18742021-12-18 16:28:02 +0800539 mtk_phy_clear_bits(com + U3D_U2PHYDCR0, P2C_RG_SIF_U2PLL_FORCE_ON);
Chunfeng Yune1d76532016-04-20 08:14:02 +0800540 } else {
Chunfeng Yun33d18742021-12-18 16:28:02 +0800541 mtk_phy_set_bits(com + U3D_U2PHYDCR0, P2C_RG_SIF_U2PLL_FORCE_ON);
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800542
Chunfeng Yun33d18742021-12-18 16:28:02 +0800543 mtk_phy_set_bits(com + U3P_U2PHYDTM0,
544 P2C_RG_SUSPENDM | P2C_FORCE_SUSPENDM);
Chunfeng Yune1d76532016-04-20 08:14:02 +0800545 }
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800546 }
547
Chunfeng Yun33d18742021-12-18 16:28:02 +0800548 /* DP/DM BC1.1 path Disable */
549 mtk_phy_clear_bits(com + U3P_USBPHYACR6, PA6_RG_U2_BC11_SW_EN);
550
551 mtk_phy_update_bits(com + U3P_USBPHYACR6, PA6_RG_U2_SQTH, PA6_RG_U2_SQTH_VAL(2));
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800552
Chunfeng Yuna69f29c2021-07-23 16:22:42 +0800553 /* Workaround only for mt8195, HW fix it for others (V3) */
554 u2_phy_pll_26m_set(tphy, instance);
555
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800556 dev_dbg(tphy->dev, "%s(%d)\n", __func__, index);
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800557}
558
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800559static void u2_phy_instance_power_on(struct mtk_tphy *tphy,
560 struct mtk_phy_instance *instance)
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800561{
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800562 struct u2phy_banks *u2_banks = &instance->u2_banks;
563 void __iomem *com = u2_banks->com;
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800564 u32 index = instance->index;
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800565
Chunfeng Yun33d18742021-12-18 16:28:02 +0800566 mtk_phy_clear_bits(com + U3P_U2PHYDTM0,
567 P2C_RG_XCVRSEL | P2C_RG_DATAIN | P2C_DTM0_PART_MASK);
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800568
569 /* OTG Enable */
Chunfeng Yun33d18742021-12-18 16:28:02 +0800570 mtk_phy_set_bits(com + U3P_USBPHYACR6, PA6_RG_U2_OTG_VBUSCMP_EN);
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800571
Chunfeng Yun33d18742021-12-18 16:28:02 +0800572 mtk_phy_set_bits(com + U3P_U2PHYDTM1, P2C_RG_VBUSVALID | P2C_RG_AVALID);
573
574 mtk_phy_clear_bits(com + U3P_U2PHYDTM1, P2C_RG_SESSEND);
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800575
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800576 if (tphy->pdata->avoid_rx_sen_degradation && index) {
Chunfeng Yun33d18742021-12-18 16:28:02 +0800577 mtk_phy_set_bits(com + U3D_U2PHYDCR0, P2C_RG_SIF_U2PLL_FORCE_ON);
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800578
Chunfeng Yun33d18742021-12-18 16:28:02 +0800579 mtk_phy_set_bits(com + U3P_U2PHYDTM0, P2C_RG_SUSPENDM | P2C_FORCE_SUSPENDM);
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800580 }
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800581 dev_dbg(tphy->dev, "%s(%d)\n", __func__, index);
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800582}
583
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800584static void u2_phy_instance_power_off(struct mtk_tphy *tphy,
585 struct mtk_phy_instance *instance)
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800586{
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800587 struct u2phy_banks *u2_banks = &instance->u2_banks;
588 void __iomem *com = u2_banks->com;
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800589 u32 index = instance->index;
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800590
Chunfeng Yun33d18742021-12-18 16:28:02 +0800591 mtk_phy_clear_bits(com + U3P_U2PHYDTM0, P2C_RG_XCVRSEL | P2C_RG_DATAIN);
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800592
593 /* OTG Disable */
Chunfeng Yun33d18742021-12-18 16:28:02 +0800594 mtk_phy_clear_bits(com + U3P_USBPHYACR6, PA6_RG_U2_OTG_VBUSCMP_EN);
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800595
Chunfeng Yun33d18742021-12-18 16:28:02 +0800596 mtk_phy_clear_bits(com + U3P_U2PHYDTM1, P2C_RG_VBUSVALID | P2C_RG_AVALID);
597
598 mtk_phy_set_bits(com + U3P_U2PHYDTM1, P2C_RG_SESSEND);
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800599
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800600 if (tphy->pdata->avoid_rx_sen_degradation && index) {
Chunfeng Yun33d18742021-12-18 16:28:02 +0800601 mtk_phy_clear_bits(com + U3P_U2PHYDTM0, P2C_RG_SUSPENDM | P2C_FORCE_SUSPENDM);
Chunfeng Yun00c00922017-12-07 19:53:34 +0800602
Chunfeng Yun33d18742021-12-18 16:28:02 +0800603 mtk_phy_clear_bits(com + U3D_U2PHYDCR0, P2C_RG_SIF_U2PLL_FORCE_ON);
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800604 }
605
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800606 dev_dbg(tphy->dev, "%s(%d)\n", __func__, index);
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800607}
608
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800609static void u2_phy_instance_exit(struct mtk_tphy *tphy,
610 struct mtk_phy_instance *instance)
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800611{
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800612 struct u2phy_banks *u2_banks = &instance->u2_banks;
613 void __iomem *com = u2_banks->com;
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800614 u32 index = instance->index;
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800615
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800616 if (tphy->pdata->avoid_rx_sen_degradation && index) {
Chunfeng Yun33d18742021-12-18 16:28:02 +0800617 mtk_phy_clear_bits(com + U3D_U2PHYDCR0, P2C_RG_SIF_U2PLL_FORCE_ON);
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800618
Chunfeng Yun33d18742021-12-18 16:28:02 +0800619 mtk_phy_clear_bits(com + U3P_U2PHYDTM0, P2C_FORCE_SUSPENDM);
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800620 }
621}
622
Chunfeng Yun5954a102017-09-21 18:31:49 +0800623static void u2_phy_instance_set_mode(struct mtk_tphy *tphy,
624 struct mtk_phy_instance *instance,
625 enum phy_mode mode)
626{
627 struct u2phy_banks *u2_banks = &instance->u2_banks;
628 u32 tmp;
629
630 tmp = readl(u2_banks->com + U3P_U2PHYDTM1);
631 switch (mode) {
632 case PHY_MODE_USB_DEVICE:
633 tmp |= P2C_FORCE_IDDIG | P2C_RG_IDDIG;
634 break;
635 case PHY_MODE_USB_HOST:
636 tmp |= P2C_FORCE_IDDIG;
637 tmp &= ~P2C_RG_IDDIG;
638 break;
639 case PHY_MODE_USB_OTG:
640 tmp &= ~(P2C_FORCE_IDDIG | P2C_RG_IDDIG);
641 break;
642 default:
643 return;
644 }
645 writel(tmp, u2_banks->com + U3P_U2PHYDTM1);
646}
647
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800648static void pcie_phy_instance_init(struct mtk_tphy *tphy,
649 struct mtk_phy_instance *instance)
Ryder Lee44a6d6c2017-08-03 18:01:00 +0800650{
651 struct u3phy_banks *u3_banks = &instance->u3_banks;
Chunfeng Yun33d18742021-12-18 16:28:02 +0800652 void __iomem *phya = u3_banks->phya;
Ryder Lee44a6d6c2017-08-03 18:01:00 +0800653
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800654 if (tphy->pdata->version != MTK_PHY_V1)
Ryder Lee44a6d6c2017-08-03 18:01:00 +0800655 return;
656
Chunfeng Yun33d18742021-12-18 16:28:02 +0800657 mtk_phy_update_bits(phya + U3P_U3_PHYA_DA_REG0,
658 P3A_RG_XTAL_EXT_PE1H | P3A_RG_XTAL_EXT_PE2H,
659 P3A_RG_XTAL_EXT_PE1H_VAL(0x2) | P3A_RG_XTAL_EXT_PE2H_VAL(0x2));
Ryder Lee44a6d6c2017-08-03 18:01:00 +0800660
661 /* ref clk drive */
Chunfeng Yun33d18742021-12-18 16:28:02 +0800662 mtk_phy_update_bits(phya + U3P_U3_PHYA_REG1, P3A_RG_CLKDRV_AMP,
663 P3A_RG_CLKDRV_AMP_VAL(0x4));
Ryder Lee44a6d6c2017-08-03 18:01:00 +0800664
Chunfeng Yun33d18742021-12-18 16:28:02 +0800665 mtk_phy_update_bits(phya + U3P_U3_PHYA_REG0, P3A_RG_CLKDRV_OFF,
666 P3A_RG_CLKDRV_OFF_VAL(0x1));
Ryder Lee44a6d6c2017-08-03 18:01:00 +0800667
668 /* SSC delta -5000ppm */
Chunfeng Yun33d18742021-12-18 16:28:02 +0800669 mtk_phy_update_bits(phya + U3P_U3_PHYA_DA_REG20, P3A_RG_PLL_DELTA1_PE2H,
670 P3A_RG_PLL_DELTA1_PE2H_VAL(0x3c));
Ryder Lee44a6d6c2017-08-03 18:01:00 +0800671
Chunfeng Yun33d18742021-12-18 16:28:02 +0800672 mtk_phy_update_bits(phya + U3P_U3_PHYA_DA_REG25, P3A_RG_PLL_DELTA_PE2H,
673 P3A_RG_PLL_DELTA_PE2H_VAL(0x36));
Ryder Lee44a6d6c2017-08-03 18:01:00 +0800674
675 /* change pll BW 0.6M */
Chunfeng Yun33d18742021-12-18 16:28:02 +0800676 mtk_phy_update_bits(phya + U3P_U3_PHYA_DA_REG5,
677 P3A_RG_PLL_BR_PE2H | P3A_RG_PLL_IC_PE2H,
678 P3A_RG_PLL_BR_PE2H_VAL(0x1) | P3A_RG_PLL_IC_PE2H_VAL(0x1));
Ryder Lee44a6d6c2017-08-03 18:01:00 +0800679
Chunfeng Yun33d18742021-12-18 16:28:02 +0800680 mtk_phy_update_bits(phya + U3P_U3_PHYA_DA_REG4,
681 P3A_RG_PLL_DIVEN_PE2H | P3A_RG_PLL_BC_PE2H,
682 P3A_RG_PLL_BC_PE2H_VAL(0x3));
Ryder Lee44a6d6c2017-08-03 18:01:00 +0800683
Chunfeng Yun33d18742021-12-18 16:28:02 +0800684 mtk_phy_update_bits(phya + U3P_U3_PHYA_DA_REG6, P3A_RG_PLL_IR_PE2H,
685 P3A_RG_PLL_IR_PE2H_VAL(0x2));
Ryder Lee44a6d6c2017-08-03 18:01:00 +0800686
Chunfeng Yun33d18742021-12-18 16:28:02 +0800687 mtk_phy_update_bits(phya + U3P_U3_PHYA_DA_REG7, P3A_RG_PLL_BP_PE2H,
688 P3A_RG_PLL_BP_PE2H_VAL(0xa));
Ryder Lee44a6d6c2017-08-03 18:01:00 +0800689
690 /* Tx Detect Rx Timing: 10us -> 5us */
Chunfeng Yun33d18742021-12-18 16:28:02 +0800691 mtk_phy_update_bits(u3_banks->phyd + U3P_U3_PHYD_RXDET1,
692 P3D_RG_RXDET_STB2_SET, P3D_RG_RXDET_STB2_SET_VAL(0x10));
Ryder Lee44a6d6c2017-08-03 18:01:00 +0800693
Chunfeng Yun33d18742021-12-18 16:28:02 +0800694 mtk_phy_update_bits(u3_banks->phyd + U3P_U3_PHYD_RXDET2,
695 P3D_RG_RXDET_STB2_SET_P3, P3D_RG_RXDET_STB2_SET_P3_VAL(0x10));
Ryder Lee44a6d6c2017-08-03 18:01:00 +0800696
697 /* wait for PCIe subsys register to active */
698 usleep_range(2500, 3000);
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800699 dev_dbg(tphy->dev, "%s(%d)\n", __func__, instance->index);
Ryder Lee44a6d6c2017-08-03 18:01:00 +0800700}
701
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800702static void pcie_phy_instance_power_on(struct mtk_tphy *tphy,
703 struct mtk_phy_instance *instance)
Ryder Lee44a6d6c2017-08-03 18:01:00 +0800704{
705 struct u3phy_banks *bank = &instance->u3_banks;
Ryder Lee44a6d6c2017-08-03 18:01:00 +0800706
Chunfeng Yun33d18742021-12-18 16:28:02 +0800707 mtk_phy_clear_bits(bank->chip + U3P_U3_CHIP_GPIO_CTLD,
708 P3C_FORCE_IP_SW_RST | P3C_REG_IP_SW_RST);
Ryder Lee44a6d6c2017-08-03 18:01:00 +0800709
Chunfeng Yun33d18742021-12-18 16:28:02 +0800710 mtk_phy_clear_bits(bank->chip + U3P_U3_CHIP_GPIO_CTLE,
711 P3C_RG_SWRST_U3_PHYD_FORCE_EN | P3C_RG_SWRST_U3_PHYD);
Ryder Lee44a6d6c2017-08-03 18:01:00 +0800712}
713
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800714static void pcie_phy_instance_power_off(struct mtk_tphy *tphy,
715 struct mtk_phy_instance *instance)
Ryder Lee44a6d6c2017-08-03 18:01:00 +0800716
717{
718 struct u3phy_banks *bank = &instance->u3_banks;
Ryder Lee44a6d6c2017-08-03 18:01:00 +0800719
Chunfeng Yun33d18742021-12-18 16:28:02 +0800720 mtk_phy_set_bits(bank->chip + U3P_U3_CHIP_GPIO_CTLD,
721 P3C_FORCE_IP_SW_RST | P3C_REG_IP_SW_RST);
Ryder Lee44a6d6c2017-08-03 18:01:00 +0800722
Chunfeng Yun33d18742021-12-18 16:28:02 +0800723 mtk_phy_set_bits(bank->chip + U3P_U3_CHIP_GPIO_CTLE,
724 P3C_RG_SWRST_U3_PHYD_FORCE_EN | P3C_RG_SWRST_U3_PHYD);
Ryder Lee44a6d6c2017-08-03 18:01:00 +0800725}
726
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800727static void sata_phy_instance_init(struct mtk_tphy *tphy,
728 struct mtk_phy_instance *instance)
Ryder Lee4ab26cb2017-08-03 18:01:01 +0800729{
730 struct u3phy_banks *u3_banks = &instance->u3_banks;
731 void __iomem *phyd = u3_banks->phyd;
Ryder Lee4ab26cb2017-08-03 18:01:01 +0800732
733 /* charge current adjustment */
Chunfeng Yun33d18742021-12-18 16:28:02 +0800734 mtk_phy_update_bits(phyd + ANA_RG_CTRL_SIGNAL6,
735 RG_CDR_BIRLTR_GEN1_MSK | RG_CDR_BC_GEN1_MSK,
736 RG_CDR_BIRLTR_GEN1_VAL(0x6) | RG_CDR_BC_GEN1_VAL(0x1a));
Ryder Lee4ab26cb2017-08-03 18:01:01 +0800737
Chunfeng Yun33d18742021-12-18 16:28:02 +0800738 mtk_phy_update_bits(phyd + ANA_EQ_EYE_CTRL_SIGNAL4, RG_CDR_BIRLTD0_GEN1_MSK,
739 RG_CDR_BIRLTD0_GEN1_VAL(0x18));
Ryder Lee4ab26cb2017-08-03 18:01:01 +0800740
Chunfeng Yun33d18742021-12-18 16:28:02 +0800741 mtk_phy_update_bits(phyd + ANA_EQ_EYE_CTRL_SIGNAL5, RG_CDR_BIRLTD0_GEN3_MSK,
742 RG_CDR_BIRLTD0_GEN3_VAL(0x06));
Ryder Lee4ab26cb2017-08-03 18:01:01 +0800743
Chunfeng Yun33d18742021-12-18 16:28:02 +0800744 mtk_phy_update_bits(phyd + ANA_RG_CTRL_SIGNAL4,
745 RG_CDR_BICLTR_GEN1_MSK | RG_CDR_BR_GEN2_MSK,
746 RG_CDR_BICLTR_GEN1_VAL(0x0c) | RG_CDR_BR_GEN2_VAL(0x07));
Ryder Lee4ab26cb2017-08-03 18:01:01 +0800747
Chunfeng Yun33d18742021-12-18 16:28:02 +0800748 mtk_phy_update_bits(phyd + PHYD_CTRL_SIGNAL_MODE4,
749 RG_CDR_BICLTD0_GEN1_MSK | RG_CDR_BICLTD1_GEN1_MSK,
750 RG_CDR_BICLTD0_GEN1_VAL(0x08) | RG_CDR_BICLTD1_GEN1_VAL(0x02));
Ryder Lee4ab26cb2017-08-03 18:01:01 +0800751
Chunfeng Yun33d18742021-12-18 16:28:02 +0800752 mtk_phy_update_bits(phyd + PHYD_DESIGN_OPTION2, RG_LOCK_CNT_SEL_MSK,
753 RG_LOCK_CNT_SEL_VAL(0x02));
Ryder Lee4ab26cb2017-08-03 18:01:01 +0800754
Chunfeng Yun33d18742021-12-18 16:28:02 +0800755 mtk_phy_update_bits(phyd + PHYD_DESIGN_OPTION9,
756 RG_T2_MIN_MSK | RG_TG_MIN_MSK,
757 RG_T2_MIN_VAL(0x12) | RG_TG_MIN_VAL(0x04));
Ryder Lee4ab26cb2017-08-03 18:01:01 +0800758
Chunfeng Yun33d18742021-12-18 16:28:02 +0800759 mtk_phy_update_bits(phyd + PHYD_DESIGN_OPTION9,
760 RG_T2_MAX_MSK | RG_TG_MAX_MSK,
761 RG_T2_MAX_VAL(0x31) | RG_TG_MAX_VAL(0x0e));
Ryder Lee4ab26cb2017-08-03 18:01:01 +0800762
Chunfeng Yun33d18742021-12-18 16:28:02 +0800763 mtk_phy_update_bits(phyd + ANA_RG_CTRL_SIGNAL1, RG_IDRV_0DB_GEN1_MSK,
764 RG_IDRV_0DB_GEN1_VAL(0x20));
765
766 mtk_phy_update_bits(phyd + ANA_EQ_EYE_CTRL_SIGNAL1, RG_EQ_DLEQ_LFI_GEN1_MSK,
767 RG_EQ_DLEQ_LFI_GEN1_VAL(0x03));
Ryder Lee4ab26cb2017-08-03 18:01:01 +0800768
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800769 dev_dbg(tphy->dev, "%s(%d)\n", __func__, instance->index);
Ryder Lee4ab26cb2017-08-03 18:01:01 +0800770}
771
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800772static void phy_v1_banks_init(struct mtk_tphy *tphy,
773 struct mtk_phy_instance *instance)
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800774{
775 struct u2phy_banks *u2_banks = &instance->u2_banks;
776 struct u3phy_banks *u3_banks = &instance->u3_banks;
777
Ryder Lee44a6d6c2017-08-03 18:01:00 +0800778 switch (instance->type) {
779 case PHY_TYPE_USB2:
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800780 u2_banks->misc = NULL;
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800781 u2_banks->fmreg = tphy->sif_base + SSUSB_SIFSLV_V1_U2FREQ;
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800782 u2_banks->com = instance->port_base + SSUSB_SIFSLV_V1_U2PHY_COM;
Ryder Lee44a6d6c2017-08-03 18:01:00 +0800783 break;
784 case PHY_TYPE_USB3:
785 case PHY_TYPE_PCIE:
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800786 u3_banks->spllc = tphy->sif_base + SSUSB_SIFSLV_V1_SPLLC;
Chunfeng Yun554a56f2017-09-21 18:31:48 +0800787 u3_banks->chip = tphy->sif_base + SSUSB_SIFSLV_V1_CHIP;
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800788 u3_banks->phyd = instance->port_base + SSUSB_SIFSLV_V1_U3PHYD;
789 u3_banks->phya = instance->port_base + SSUSB_SIFSLV_V1_U3PHYA;
Ryder Lee44a6d6c2017-08-03 18:01:00 +0800790 break;
Ryder Lee4ab26cb2017-08-03 18:01:01 +0800791 case PHY_TYPE_SATA:
792 u3_banks->phyd = instance->port_base + SSUSB_SIFSLV_V1_U3PHYD;
793 break;
Ryder Lee44a6d6c2017-08-03 18:01:00 +0800794 default:
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800795 dev_err(tphy->dev, "incompatible PHY type\n");
Ryder Lee44a6d6c2017-08-03 18:01:00 +0800796 return;
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800797 }
798}
799
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800800static void phy_v2_banks_init(struct mtk_tphy *tphy,
801 struct mtk_phy_instance *instance)
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800802{
803 struct u2phy_banks *u2_banks = &instance->u2_banks;
804 struct u3phy_banks *u3_banks = &instance->u3_banks;
805
Ryder Lee44a6d6c2017-08-03 18:01:00 +0800806 switch (instance->type) {
807 case PHY_TYPE_USB2:
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800808 u2_banks->misc = instance->port_base + SSUSB_SIFSLV_V2_MISC;
809 u2_banks->fmreg = instance->port_base + SSUSB_SIFSLV_V2_U2FREQ;
810 u2_banks->com = instance->port_base + SSUSB_SIFSLV_V2_U2PHY_COM;
Ryder Lee44a6d6c2017-08-03 18:01:00 +0800811 break;
812 case PHY_TYPE_USB3:
813 case PHY_TYPE_PCIE:
Chunfeng Yun8d6e19572017-03-31 15:35:31 +0800814 u3_banks->spllc = instance->port_base + SSUSB_SIFSLV_V2_SPLLC;
815 u3_banks->chip = instance->port_base + SSUSB_SIFSLV_V2_CHIP;
816 u3_banks->phyd = instance->port_base + SSUSB_SIFSLV_V2_U3PHYD;
817 u3_banks->phya = instance->port_base + SSUSB_SIFSLV_V2_U3PHYA;
Ryder Lee44a6d6c2017-08-03 18:01:00 +0800818 break;
819 default:
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +0800820 dev_err(tphy->dev, "incompatible PHY type\n");
Ryder Lee44a6d6c2017-08-03 18:01:00 +0800821 return;
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800822 }
823}
824
Chunfeng Yun8158e912018-06-29 10:20:29 +0800825static void phy_parse_property(struct mtk_tphy *tphy,
826 struct mtk_phy_instance *instance)
827{
828 struct device *dev = &instance->phy->dev;
829
830 if (instance->type != PHY_TYPE_USB2)
831 return;
832
Chunfeng Yund4f97f12018-06-29 10:20:30 +0800833 instance->bc12_en = device_property_read_bool(dev, "mediatek,bc12");
Chunfeng Yun8158e912018-06-29 10:20:29 +0800834 device_property_read_u32(dev, "mediatek,eye-src",
835 &instance->eye_src);
836 device_property_read_u32(dev, "mediatek,eye-vrt",
837 &instance->eye_vrt);
838 device_property_read_u32(dev, "mediatek,eye-term",
839 &instance->eye_term);
Chunfeng Yun410572e2020-02-11 11:21:12 +0800840 device_property_read_u32(dev, "mediatek,intr",
841 &instance->intr);
Chunfeng Yun8be5a672020-01-08 09:52:01 +0800842 device_property_read_u32(dev, "mediatek,discth",
843 &instance->discth);
Chunfeng Yun410572e2020-02-11 11:21:12 +0800844 dev_dbg(dev, "bc12:%d, src:%d, vrt:%d, term:%d, intr:%d, disc:%d\n",
Chunfeng Yund4f97f12018-06-29 10:20:30 +0800845 instance->bc12_en, instance->eye_src,
Chunfeng Yun8be5a672020-01-08 09:52:01 +0800846 instance->eye_vrt, instance->eye_term,
Chunfeng Yun410572e2020-02-11 11:21:12 +0800847 instance->intr, instance->discth);
Chunfeng Yun8158e912018-06-29 10:20:29 +0800848}
849
850static void u2_phy_props_set(struct mtk_tphy *tphy,
851 struct mtk_phy_instance *instance)
852{
853 struct u2phy_banks *u2_banks = &instance->u2_banks;
854 void __iomem *com = u2_banks->com;
Chunfeng Yun8158e912018-06-29 10:20:29 +0800855
Chunfeng Yun33d18742021-12-18 16:28:02 +0800856 if (instance->bc12_en) /* BC1.2 path Enable */
857 mtk_phy_set_bits(com + U3P_U2PHYBC12C, P2C_RG_CHGDT_EN);
Chunfeng Yun8158e912018-06-29 10:20:29 +0800858
Chunfeng Yun33d18742021-12-18 16:28:02 +0800859 if (tphy->pdata->version < MTK_PHY_V3 && instance->eye_src)
860 mtk_phy_update_bits(com + U3P_USBPHYACR5, PA5_RG_U2_HSTX_SRCTRL,
861 PA5_RG_U2_HSTX_SRCTRL_VAL(instance->eye_src));
Chunfeng Yun8158e912018-06-29 10:20:29 +0800862
Chunfeng Yun33d18742021-12-18 16:28:02 +0800863 if (instance->eye_vrt)
864 mtk_phy_update_bits(com + U3P_USBPHYACR1, PA1_RG_VRT_SEL,
865 PA1_RG_VRT_SEL_VAL(instance->eye_vrt));
Chunfeng Yun8158e912018-06-29 10:20:29 +0800866
Chunfeng Yun33d18742021-12-18 16:28:02 +0800867 if (instance->eye_term)
868 mtk_phy_update_bits(com + U3P_USBPHYACR1, PA1_RG_TERM_SEL,
869 PA1_RG_TERM_SEL_VAL(instance->eye_term));
Chunfeng Yun8be5a672020-01-08 09:52:01 +0800870
Chunfeng Yun33d18742021-12-18 16:28:02 +0800871 if (instance->intr)
872 mtk_phy_update_bits(com + U3P_USBPHYACR1, PA1_RG_INTR_CAL,
873 PA1_RG_INTR_CAL_VAL(instance->intr));
Chunfeng Yun410572e2020-02-11 11:21:12 +0800874
Chunfeng Yun33d18742021-12-18 16:28:02 +0800875 if (instance->discth)
876 mtk_phy_update_bits(com + U3P_USBPHYACR6, PA6_RG_U2_DISCTH,
877 PA6_RG_U2_DISCTH_VAL(instance->discth));
Chunfeng Yun8158e912018-06-29 10:20:29 +0800878}
879
Chunfeng Yun39099a42021-08-17 17:19:41 +0800880/* type switch for usb3/pcie/sgmii/sata */
881static int phy_type_syscon_get(struct mtk_phy_instance *instance,
882 struct device_node *dn)
883{
884 struct of_phandle_args args;
885 int ret;
886
887 /* type switch function is optional */
888 if (!of_property_read_bool(dn, "mediatek,syscon-type"))
889 return 0;
890
891 ret = of_parse_phandle_with_fixed_args(dn, "mediatek,syscon-type",
892 2, 0, &args);
893 if (ret)
894 return ret;
895
896 instance->type_sw_reg = args.args[0];
897 instance->type_sw_index = args.args[1] & 0x3; /* <=3 */
898 instance->type_sw = syscon_node_to_regmap(args.np);
899 of_node_put(args.np);
900 dev_info(&instance->phy->dev, "type_sw - reg %#x, index %d\n",
901 instance->type_sw_reg, instance->type_sw_index);
902
903 return PTR_ERR_OR_ZERO(instance->type_sw);
904}
905
906static int phy_type_set(struct mtk_phy_instance *instance)
907{
908 int type;
909 u32 mask;
910
911 if (!instance->type_sw)
912 return 0;
913
914 switch (instance->type) {
915 case PHY_TYPE_USB3:
916 type = RG_PHY_SW_USB3;
917 break;
918 case PHY_TYPE_PCIE:
919 type = RG_PHY_SW_PCIE;
920 break;
921 case PHY_TYPE_SGMII:
922 type = RG_PHY_SW_SGMII;
923 break;
924 case PHY_TYPE_SATA:
925 type = RG_PHY_SW_SATA;
926 break;
927 case PHY_TYPE_USB2:
928 default:
929 return 0;
930 }
931
932 mask = RG_PHY_SW_TYPE << (instance->type_sw_index * BITS_PER_BYTE);
933 regmap_update_bits(instance->type_sw, instance->type_sw_reg, mask, type);
934
935 return 0;
936}
937
Chunfeng Yun6f2b0332021-12-18 16:27:59 +0800938static int phy_efuse_get(struct mtk_tphy *tphy, struct mtk_phy_instance *instance)
939{
940 struct device *dev = &instance->phy->dev;
941 int ret = 0;
942
943 /* tphy v1 doesn't support sw efuse, skip it */
944 if (!tphy->pdata->sw_efuse_supported) {
945 instance->efuse_sw_en = 0;
946 return 0;
947 }
948
949 /* software efuse is optional */
950 instance->efuse_sw_en = device_property_read_bool(dev, "nvmem-cells");
951 if (!instance->efuse_sw_en)
952 return 0;
953
954 switch (instance->type) {
955 case PHY_TYPE_USB2:
956 ret = nvmem_cell_read_variable_le_u32(dev, "intr", &instance->efuse_intr);
957 if (ret) {
958 dev_err(dev, "fail to get u2 intr efuse, %d\n", ret);
959 break;
960 }
961
962 /* no efuse, ignore it */
963 if (!instance->efuse_intr) {
964 dev_warn(dev, "no u2 intr efuse, but dts enable it\n");
965 instance->efuse_sw_en = 0;
966 break;
967 }
968
969 dev_dbg(dev, "u2 efuse - intr %x\n", instance->efuse_intr);
970 break;
971
972 case PHY_TYPE_USB3:
973 case PHY_TYPE_PCIE:
974 ret = nvmem_cell_read_variable_le_u32(dev, "intr", &instance->efuse_intr);
975 if (ret) {
976 dev_err(dev, "fail to get u3 intr efuse, %d\n", ret);
977 break;
978 }
979
980 ret = nvmem_cell_read_variable_le_u32(dev, "rx_imp", &instance->efuse_rx_imp);
981 if (ret) {
982 dev_err(dev, "fail to get u3 rx_imp efuse, %d\n", ret);
983 break;
984 }
985
986 ret = nvmem_cell_read_variable_le_u32(dev, "tx_imp", &instance->efuse_tx_imp);
987 if (ret) {
988 dev_err(dev, "fail to get u3 tx_imp efuse, %d\n", ret);
989 break;
990 }
991
992 /* no efuse, ignore it */
993 if (!instance->efuse_intr &&
994 !instance->efuse_rx_imp &&
Wan Jiabing46e99472022-01-07 10:50:50 +0800995 !instance->efuse_tx_imp) {
Chunfeng Yun6f2b0332021-12-18 16:27:59 +0800996 dev_warn(dev, "no u3 intr efuse, but dts enable it\n");
997 instance->efuse_sw_en = 0;
998 break;
999 }
1000
1001 dev_dbg(dev, "u3 efuse - intr %x, rx_imp %x, tx_imp %x\n",
1002 instance->efuse_intr, instance->efuse_rx_imp,instance->efuse_tx_imp);
1003 break;
1004 default:
1005 dev_err(dev, "no sw efuse for type %d\n", instance->type);
1006 ret = -EINVAL;
1007 }
1008
1009 return ret;
1010}
1011
1012static void phy_efuse_set(struct mtk_phy_instance *instance)
1013{
1014 struct device *dev = &instance->phy->dev;
1015 struct u2phy_banks *u2_banks = &instance->u2_banks;
1016 struct u3phy_banks *u3_banks = &instance->u3_banks;
Chunfeng Yun6f2b0332021-12-18 16:27:59 +08001017
1018 if (!instance->efuse_sw_en)
1019 return;
1020
1021 switch (instance->type) {
1022 case PHY_TYPE_USB2:
Chunfeng Yun33d18742021-12-18 16:28:02 +08001023 mtk_phy_set_bits(u2_banks->misc + U3P_MISC_REG1, MR1_EFUSE_AUTO_LOAD_DIS);
Chunfeng Yun6f2b0332021-12-18 16:27:59 +08001024
Chunfeng Yun33d18742021-12-18 16:28:02 +08001025 mtk_phy_update_bits(u2_banks->com + U3P_USBPHYACR1, PA1_RG_INTR_CAL,
1026 PA1_RG_INTR_CAL_VAL(instance->efuse_intr));
Chunfeng Yun6f2b0332021-12-18 16:27:59 +08001027 break;
1028 case PHY_TYPE_USB3:
1029 case PHY_TYPE_PCIE:
Chunfeng Yun33d18742021-12-18 16:28:02 +08001030 mtk_phy_set_bits(u3_banks->phyd + U3P_U3_PHYD_RSV, P3D_RG_EFUSE_AUTO_LOAD_DIS);
Chunfeng Yun6f2b0332021-12-18 16:27:59 +08001031
Chunfeng Yun33d18742021-12-18 16:28:02 +08001032 mtk_phy_update_bits(u3_banks->phyd + U3P_U3_PHYD_IMPCAL0, P3D_RG_TX_IMPEL,
1033 P3D_RG_TX_IMPEL_VAL(instance->efuse_tx_imp));
1034 mtk_phy_set_bits(u3_banks->phyd + U3P_U3_PHYD_IMPCAL0, P3D_RG_FORCE_TX_IMPEL);
Chunfeng Yun6f2b0332021-12-18 16:27:59 +08001035
Chunfeng Yun33d18742021-12-18 16:28:02 +08001036 mtk_phy_update_bits(u3_banks->phyd + U3P_U3_PHYD_IMPCAL1, P3D_RG_RX_IMPEL,
1037 P3D_RG_RX_IMPEL_VAL(instance->efuse_rx_imp));
1038 mtk_phy_set_bits(u3_banks->phyd + U3P_U3_PHYD_IMPCAL1, P3D_RG_FORCE_RX_IMPEL);
Chunfeng Yun6f2b0332021-12-18 16:27:59 +08001039
Chunfeng Yun33d18742021-12-18 16:28:02 +08001040 mtk_phy_update_bits(u3_banks->phya + U3P_U3_PHYA_REG0, P3A_RG_IEXT_INTR,
1041 P3A_RG_IEXT_INTR_VAL(instance->efuse_intr));
Chunfeng Yun6f2b0332021-12-18 16:27:59 +08001042 break;
1043 default:
1044 dev_warn(dev, "no sw efuse for type %d\n", instance->type);
1045 break;
1046 }
1047}
1048
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +08001049static int mtk_phy_init(struct phy *phy)
Chunfeng Yundc7f1902015-09-29 11:01:36 +08001050{
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +08001051 struct mtk_phy_instance *instance = phy_get_drvdata(phy);
1052 struct mtk_tphy *tphy = dev_get_drvdata(phy->dev.parent);
Chunfeng Yundc7f1902015-09-29 11:01:36 +08001053 int ret;
1054
Chunfeng Yun3fd66112021-08-17 17:19:40 +08001055 ret = clk_bulk_prepare_enable(TPHY_CLKS_CNT, instance->clks);
1056 if (ret)
Chunfeng Yun15de15c2017-03-31 15:35:30 +08001057 return ret;
Chunfeng Yun12d0c0b2020-02-11 11:21:15 +08001058
Chunfeng Yun6f2b0332021-12-18 16:27:59 +08001059 phy_efuse_set(instance);
1060
Ryder Lee44a6d6c2017-08-03 18:01:00 +08001061 switch (instance->type) {
1062 case PHY_TYPE_USB2:
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +08001063 u2_phy_instance_init(tphy, instance);
Chunfeng Yun8158e912018-06-29 10:20:29 +08001064 u2_phy_props_set(tphy, instance);
Ryder Lee44a6d6c2017-08-03 18:01:00 +08001065 break;
1066 case PHY_TYPE_USB3:
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +08001067 u3_phy_instance_init(tphy, instance);
Ryder Lee44a6d6c2017-08-03 18:01:00 +08001068 break;
1069 case PHY_TYPE_PCIE:
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +08001070 pcie_phy_instance_init(tphy, instance);
Ryder Lee44a6d6c2017-08-03 18:01:00 +08001071 break;
Ryder Lee4ab26cb2017-08-03 18:01:01 +08001072 case PHY_TYPE_SATA:
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +08001073 sata_phy_instance_init(tphy, instance);
Ryder Lee4ab26cb2017-08-03 18:01:01 +08001074 break;
Chunfeng Yun39099a42021-08-17 17:19:41 +08001075 case PHY_TYPE_SGMII:
1076 /* nothing to do, only used to set type */
1077 break;
Ryder Lee44a6d6c2017-08-03 18:01:00 +08001078 default:
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +08001079 dev_err(tphy->dev, "incompatible PHY type\n");
Chunfeng Yun3fd66112021-08-17 17:19:40 +08001080 clk_bulk_disable_unprepare(TPHY_CLKS_CNT, instance->clks);
Ryder Lee44a6d6c2017-08-03 18:01:00 +08001081 return -EINVAL;
1082 }
Chunfeng Yun04466ef2017-03-31 15:35:29 +08001083
Chunfeng Yundc7f1902015-09-29 11:01:36 +08001084 return 0;
1085}
1086
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +08001087static int mtk_phy_power_on(struct phy *phy)
Chunfeng Yundc7f1902015-09-29 11:01:36 +08001088{
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +08001089 struct mtk_phy_instance *instance = phy_get_drvdata(phy);
1090 struct mtk_tphy *tphy = dev_get_drvdata(phy->dev.parent);
Chunfeng Yundc7f1902015-09-29 11:01:36 +08001091
Chunfeng Yun04466ef2017-03-31 15:35:29 +08001092 if (instance->type == PHY_TYPE_USB2) {
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +08001093 u2_phy_instance_power_on(tphy, instance);
1094 hs_slew_rate_calibrate(tphy, instance);
Ryder Lee44a6d6c2017-08-03 18:01:00 +08001095 } else if (instance->type == PHY_TYPE_PCIE) {
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +08001096 pcie_phy_instance_power_on(tphy, instance);
Chunfeng Yun04466ef2017-03-31 15:35:29 +08001097 }
Ryder Lee44a6d6c2017-08-03 18:01:00 +08001098
Chunfeng Yundc7f1902015-09-29 11:01:36 +08001099 return 0;
1100}
1101
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +08001102static int mtk_phy_power_off(struct phy *phy)
Chunfeng Yundc7f1902015-09-29 11:01:36 +08001103{
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +08001104 struct mtk_phy_instance *instance = phy_get_drvdata(phy);
1105 struct mtk_tphy *tphy = dev_get_drvdata(phy->dev.parent);
Chunfeng Yundc7f1902015-09-29 11:01:36 +08001106
Chunfeng Yun04466ef2017-03-31 15:35:29 +08001107 if (instance->type == PHY_TYPE_USB2)
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +08001108 u2_phy_instance_power_off(tphy, instance);
Ryder Lee44a6d6c2017-08-03 18:01:00 +08001109 else if (instance->type == PHY_TYPE_PCIE)
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +08001110 pcie_phy_instance_power_off(tphy, instance);
Chunfeng Yun04466ef2017-03-31 15:35:29 +08001111
Chunfeng Yundc7f1902015-09-29 11:01:36 +08001112 return 0;
1113}
1114
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +08001115static int mtk_phy_exit(struct phy *phy)
Chunfeng Yundc7f1902015-09-29 11:01:36 +08001116{
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +08001117 struct mtk_phy_instance *instance = phy_get_drvdata(phy);
1118 struct mtk_tphy *tphy = dev_get_drvdata(phy->dev.parent);
Chunfeng Yundc7f1902015-09-29 11:01:36 +08001119
Chunfeng Yun04466ef2017-03-31 15:35:29 +08001120 if (instance->type == PHY_TYPE_USB2)
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +08001121 u2_phy_instance_exit(tphy, instance);
Chunfeng Yun04466ef2017-03-31 15:35:29 +08001122
Chunfeng Yun3fd66112021-08-17 17:19:40 +08001123 clk_bulk_disable_unprepare(TPHY_CLKS_CNT, instance->clks);
Chunfeng Yundc7f1902015-09-29 11:01:36 +08001124 return 0;
1125}
1126
Grygorii Strashko79a5a182018-11-19 19:24:20 -06001127static int mtk_phy_set_mode(struct phy *phy, enum phy_mode mode, int submode)
Chunfeng Yun5954a102017-09-21 18:31:49 +08001128{
1129 struct mtk_phy_instance *instance = phy_get_drvdata(phy);
1130 struct mtk_tphy *tphy = dev_get_drvdata(phy->dev.parent);
1131
1132 if (instance->type == PHY_TYPE_USB2)
1133 u2_phy_instance_set_mode(tphy, instance, mode);
1134
1135 return 0;
1136}
1137
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +08001138static struct phy *mtk_phy_xlate(struct device *dev,
Chunfeng Yundc7f1902015-09-29 11:01:36 +08001139 struct of_phandle_args *args)
1140{
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +08001141 struct mtk_tphy *tphy = dev_get_drvdata(dev);
1142 struct mtk_phy_instance *instance = NULL;
Chunfeng Yundc7f1902015-09-29 11:01:36 +08001143 struct device_node *phy_np = args->np;
1144 int index;
Chunfeng Yun6f2b0332021-12-18 16:27:59 +08001145 int ret;
Chunfeng Yundc7f1902015-09-29 11:01:36 +08001146
Chunfeng Yundc7f1902015-09-29 11:01:36 +08001147 if (args->args_count != 1) {
1148 dev_err(dev, "invalid number of cells in 'phy' property\n");
1149 return ERR_PTR(-EINVAL);
1150 }
1151
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +08001152 for (index = 0; index < tphy->nphys; index++)
1153 if (phy_np == tphy->phys[index]->phy->dev.of_node) {
1154 instance = tphy->phys[index];
Chunfeng Yundc7f1902015-09-29 11:01:36 +08001155 break;
1156 }
1157
1158 if (!instance) {
1159 dev_err(dev, "failed to find appropriate phy\n");
1160 return ERR_PTR(-EINVAL);
1161 }
1162
1163 instance->type = args->args[0];
Chunfeng Yundc7f1902015-09-29 11:01:36 +08001164 if (!(instance->type == PHY_TYPE_USB2 ||
Ryder Lee44a6d6c2017-08-03 18:01:00 +08001165 instance->type == PHY_TYPE_USB3 ||
Ryder Lee4ab26cb2017-08-03 18:01:01 +08001166 instance->type == PHY_TYPE_PCIE ||
Chunfeng Yun39099a42021-08-17 17:19:41 +08001167 instance->type == PHY_TYPE_SATA ||
1168 instance->type == PHY_TYPE_SGMII)) {
Chunfeng Yundc7f1902015-09-29 11:01:36 +08001169 dev_err(dev, "unsupported device type: %d\n", instance->type);
1170 return ERR_PTR(-EINVAL);
1171 }
1172
Chunfeng Yun27974e62021-07-23 16:22:41 +08001173 switch (tphy->pdata->version) {
1174 case MTK_PHY_V1:
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +08001175 phy_v1_banks_init(tphy, instance);
Chunfeng Yun27974e62021-07-23 16:22:41 +08001176 break;
1177 case MTK_PHY_V2:
1178 case MTK_PHY_V3:
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +08001179 phy_v2_banks_init(tphy, instance);
Chunfeng Yun27974e62021-07-23 16:22:41 +08001180 break;
1181 default:
Chunfeng Yun8d6e19572017-03-31 15:35:31 +08001182 dev_err(dev, "phy version is not supported\n");
1183 return ERR_PTR(-EINVAL);
1184 }
1185
Chunfeng Yun6f2b0332021-12-18 16:27:59 +08001186 ret = phy_efuse_get(tphy, instance);
1187 if (ret)
1188 return ERR_PTR(ret);
1189
Chunfeng Yun8158e912018-06-29 10:20:29 +08001190 phy_parse_property(tphy, instance);
Chunfeng Yun39099a42021-08-17 17:19:41 +08001191 phy_type_set(instance);
Chunfeng Yun8158e912018-06-29 10:20:29 +08001192
Chunfeng Yundc7f1902015-09-29 11:01:36 +08001193 return instance->phy;
1194}
1195
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +08001196static const struct phy_ops mtk_tphy_ops = {
1197 .init = mtk_phy_init,
1198 .exit = mtk_phy_exit,
1199 .power_on = mtk_phy_power_on,
1200 .power_off = mtk_phy_power_off,
Chunfeng Yun5954a102017-09-21 18:31:49 +08001201 .set_mode = mtk_phy_set_mode,
Chunfeng Yundc7f1902015-09-29 11:01:36 +08001202 .owner = THIS_MODULE,
1203};
1204
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +08001205static const struct mtk_phy_pdata tphy_v1_pdata = {
Chunfeng Yune1d76532016-04-20 08:14:02 +08001206 .avoid_rx_sen_degradation = false,
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +08001207 .version = MTK_PHY_V1,
Chunfeng Yun8d6e19572017-03-31 15:35:31 +08001208};
1209
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +08001210static const struct mtk_phy_pdata tphy_v2_pdata = {
Chunfeng Yun8d6e19572017-03-31 15:35:31 +08001211 .avoid_rx_sen_degradation = false,
Chunfeng Yun6f2b0332021-12-18 16:27:59 +08001212 .sw_efuse_supported = true,
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +08001213 .version = MTK_PHY_V2,
Chunfeng Yune1d76532016-04-20 08:14:02 +08001214};
1215
Chunfeng Yun27974e62021-07-23 16:22:41 +08001216static const struct mtk_phy_pdata tphy_v3_pdata = {
Chunfeng Yun6f2b0332021-12-18 16:27:59 +08001217 .sw_efuse_supported = true,
Chunfeng Yun27974e62021-07-23 16:22:41 +08001218 .version = MTK_PHY_V3,
1219};
1220
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +08001221static const struct mtk_phy_pdata mt8173_pdata = {
Chunfeng Yune1d76532016-04-20 08:14:02 +08001222 .avoid_rx_sen_degradation = true,
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +08001223 .version = MTK_PHY_V1,
Chunfeng Yune1d76532016-04-20 08:14:02 +08001224};
1225
Chunfeng Yuna69f29c2021-07-23 16:22:42 +08001226static const struct mtk_phy_pdata mt8195_pdata = {
1227 .sw_pll_48m_to_26m = true,
Chunfeng Yun6f2b0332021-12-18 16:27:59 +08001228 .sw_efuse_supported = true,
Chunfeng Yuna69f29c2021-07-23 16:22:42 +08001229 .version = MTK_PHY_V3,
1230};
1231
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +08001232static const struct of_device_id mtk_tphy_id_table[] = {
Ryder Lee44a6d6c2017-08-03 18:01:00 +08001233 { .compatible = "mediatek,mt2701-u3phy", .data = &tphy_v1_pdata },
Ryder Lee4ab26cb2017-08-03 18:01:01 +08001234 { .compatible = "mediatek,mt2712-u3phy", .data = &tphy_v2_pdata },
Chunfeng Yune1d76532016-04-20 08:14:02 +08001235 { .compatible = "mediatek,mt8173-u3phy", .data = &mt8173_pdata },
Chunfeng Yuna69f29c2021-07-23 16:22:42 +08001236 { .compatible = "mediatek,mt8195-tphy", .data = &mt8195_pdata },
Ryder Lee44a6d6c2017-08-03 18:01:00 +08001237 { .compatible = "mediatek,generic-tphy-v1", .data = &tphy_v1_pdata },
Ryder Lee4ab26cb2017-08-03 18:01:01 +08001238 { .compatible = "mediatek,generic-tphy-v2", .data = &tphy_v2_pdata },
Chunfeng Yun27974e62021-07-23 16:22:41 +08001239 { .compatible = "mediatek,generic-tphy-v3", .data = &tphy_v3_pdata },
Chunfeng Yune1d76532016-04-20 08:14:02 +08001240 { },
1241};
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +08001242MODULE_DEVICE_TABLE(of, mtk_tphy_id_table);
Chunfeng Yune1d76532016-04-20 08:14:02 +08001243
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +08001244static int mtk_tphy_probe(struct platform_device *pdev)
Chunfeng Yundc7f1902015-09-29 11:01:36 +08001245{
1246 struct device *dev = &pdev->dev;
1247 struct device_node *np = dev->of_node;
1248 struct device_node *child_np;
1249 struct phy_provider *provider;
1250 struct resource *sif_res;
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +08001251 struct mtk_tphy *tphy;
Chunfeng Yundc7f1902015-09-29 11:01:36 +08001252 struct resource res;
Julia Lawall2bb80cc2015-11-16 12:33:15 +01001253 int port, retval;
Chunfeng Yundc7f1902015-09-29 11:01:36 +08001254
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +08001255 tphy = devm_kzalloc(dev, sizeof(*tphy), GFP_KERNEL);
1256 if (!tphy)
Chunfeng Yundc7f1902015-09-29 11:01:36 +08001257 return -ENOMEM;
1258
Chunfeng Yune4b227c2017-12-28 16:40:36 +05301259 tphy->pdata = of_device_get_match_data(dev);
1260 if (!tphy->pdata)
1261 return -EINVAL;
1262
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +08001263 tphy->nphys = of_get_child_count(np);
1264 tphy->phys = devm_kcalloc(dev, tphy->nphys,
1265 sizeof(*tphy->phys), GFP_KERNEL);
1266 if (!tphy->phys)
Chunfeng Yundc7f1902015-09-29 11:01:36 +08001267 return -ENOMEM;
1268
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +08001269 tphy->dev = dev;
1270 platform_set_drvdata(pdev, tphy);
Chunfeng Yundc7f1902015-09-29 11:01:36 +08001271
Chunfeng Yun93a04f42017-12-07 19:53:35 +08001272 sif_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1273 /* SATA phy of V1 needn't it if not shared with PCIe or USB */
1274 if (sif_res && tphy->pdata->version == MTK_PHY_V1) {
Chunfeng Yun8d6e19572017-03-31 15:35:31 +08001275 /* get banks shared by multiple phys */
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +08001276 tphy->sif_base = devm_ioremap_resource(dev, sif_res);
1277 if (IS_ERR(tphy->sif_base)) {
Chunfeng Yun8d6e19572017-03-31 15:35:31 +08001278 dev_err(dev, "failed to remap sif regs\n");
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +08001279 return PTR_ERR(tphy->sif_base);
Chunfeng Yun8d6e19572017-03-31 15:35:31 +08001280 }
Chunfeng Yundc7f1902015-09-29 11:01:36 +08001281 }
1282
Chunfeng Yun27974e62021-07-23 16:22:41 +08001283 if (tphy->pdata->version < MTK_PHY_V3) {
1284 tphy->src_ref_clk = U3P_REF_CLK;
1285 tphy->src_coef = U3P_SLEW_RATE_COEF;
1286 /* update parameters of slew rate calibrate if exist */
1287 device_property_read_u32(dev, "mediatek,src-ref-clk-mhz",
1288 &tphy->src_ref_clk);
1289 device_property_read_u32(dev, "mediatek,src-coef",
1290 &tphy->src_coef);
1291 }
Chunfeng Yun8833ebf42018-03-12 13:25:39 +08001292
Chunfeng Yundc7f1902015-09-29 11:01:36 +08001293 port = 0;
1294 for_each_child_of_node(np, child_np) {
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +08001295 struct mtk_phy_instance *instance;
Chunfeng Yun3fd66112021-08-17 17:19:40 +08001296 struct clk_bulk_data *clks;
Chunfeng Yun926b83e2021-08-17 17:19:42 +08001297 struct device *subdev;
Chunfeng Yundc7f1902015-09-29 11:01:36 +08001298 struct phy *phy;
Chunfeng Yundc7f1902015-09-29 11:01:36 +08001299
1300 instance = devm_kzalloc(dev, sizeof(*instance), GFP_KERNEL);
Julia Lawall2bb80cc2015-11-16 12:33:15 +01001301 if (!instance) {
1302 retval = -ENOMEM;
1303 goto put_child;
1304 }
Chunfeng Yundc7f1902015-09-29 11:01:36 +08001305
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +08001306 tphy->phys[port] = instance;
Chunfeng Yundc7f1902015-09-29 11:01:36 +08001307
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +08001308 phy = devm_phy_create(dev, child_np, &mtk_tphy_ops);
Chunfeng Yundc7f1902015-09-29 11:01:36 +08001309 if (IS_ERR(phy)) {
1310 dev_err(dev, "failed to create phy\n");
Julia Lawall2bb80cc2015-11-16 12:33:15 +01001311 retval = PTR_ERR(phy);
1312 goto put_child;
Chunfeng Yundc7f1902015-09-29 11:01:36 +08001313 }
1314
Chunfeng Yun926b83e2021-08-17 17:19:42 +08001315 subdev = &phy->dev;
Chunfeng Yundc7f1902015-09-29 11:01:36 +08001316 retval = of_address_to_resource(child_np, 0, &res);
1317 if (retval) {
Chunfeng Yun926b83e2021-08-17 17:19:42 +08001318 dev_err(subdev, "failed to get address resource(id-%d)\n",
Chunfeng Yundc7f1902015-09-29 11:01:36 +08001319 port);
Julia Lawall2bb80cc2015-11-16 12:33:15 +01001320 goto put_child;
Chunfeng Yundc7f1902015-09-29 11:01:36 +08001321 }
1322
Chunfeng Yun926b83e2021-08-17 17:19:42 +08001323 instance->port_base = devm_ioremap_resource(subdev, &res);
Chunfeng Yundc7f1902015-09-29 11:01:36 +08001324 if (IS_ERR(instance->port_base)) {
Julia Lawall2bb80cc2015-11-16 12:33:15 +01001325 retval = PTR_ERR(instance->port_base);
1326 goto put_child;
Chunfeng Yundc7f1902015-09-29 11:01:36 +08001327 }
1328
1329 instance->phy = phy;
1330 instance->index = port;
1331 phy_set_drvdata(phy, instance);
1332 port++;
Chunfeng Yun15de15c2017-03-31 15:35:30 +08001333
Chunfeng Yun3fd66112021-08-17 17:19:40 +08001334 clks = instance->clks;
1335 clks[0].id = "ref"; /* digital (& analog) clock */
1336 clks[1].id = "da_ref"; /* analog clock */
Chunfeng Yun926b83e2021-08-17 17:19:42 +08001337 retval = devm_clk_bulk_get_optional(subdev, TPHY_CLKS_CNT, clks);
Chunfeng Yun3fd66112021-08-17 17:19:40 +08001338 if (retval)
Chunfeng Yun15de15c2017-03-31 15:35:30 +08001339 goto put_child;
Chunfeng Yun39099a42021-08-17 17:19:41 +08001340
1341 retval = phy_type_syscon_get(instance, child_np);
1342 if (retval)
1343 goto put_child;
Chunfeng Yundc7f1902015-09-29 11:01:36 +08001344 }
1345
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +08001346 provider = devm_of_phy_provider_register(dev, mtk_phy_xlate);
Chunfeng Yundc7f1902015-09-29 11:01:36 +08001347
1348 return PTR_ERR_OR_ZERO(provider);
Julia Lawall2bb80cc2015-11-16 12:33:15 +01001349put_child:
1350 of_node_put(child_np);
1351 return retval;
Chunfeng Yundc7f1902015-09-29 11:01:36 +08001352}
1353
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +08001354static struct platform_driver mtk_tphy_driver = {
1355 .probe = mtk_tphy_probe,
Chunfeng Yundc7f1902015-09-29 11:01:36 +08001356 .driver = {
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +08001357 .name = "mtk-tphy",
1358 .of_match_table = mtk_tphy_id_table,
Chunfeng Yundc7f1902015-09-29 11:01:36 +08001359 },
1360};
1361
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +08001362module_platform_driver(mtk_tphy_driver);
Chunfeng Yundc7f1902015-09-29 11:01:36 +08001363
1364MODULE_AUTHOR("Chunfeng Yun <chunfeng.yun@mediatek.com>");
Chunfeng Yuncd4ec4b2017-08-03 18:01:02 +08001365MODULE_DESCRIPTION("MediaTek T-PHY driver");
Chunfeng Yundc7f1902015-09-29 11:01:36 +08001366MODULE_LICENSE("GPL v2");