blob: 59b110f795c3b2b48bee58238c2628ec7b023739 [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>
23#include <linux/phy/phy.h>
24#include <linux/platform_device.h>
25
Chunfeng Yun8d6e1952017-03-31 15:35:31 +080026/* version V1 sub-banks offset base address */
27/* banks shared by multiple phys */
28#define SSUSB_SIFSLV_V1_SPLLC 0x000 /* shared by u3 phys */
29#define SSUSB_SIFSLV_V1_U2FREQ 0x100 /* shared by u2 phys */
30/* u2 phy bank */
31#define SSUSB_SIFSLV_V1_U2PHY_COM 0x000
32/* u3 phy banks */
33#define SSUSB_SIFSLV_V1_U3PHYD 0x000
34#define SSUSB_SIFSLV_V1_U3PHYA 0x200
Chunfeng Yundc7f1902015-09-29 11:01:36 +080035
Chunfeng Yun8d6e1952017-03-31 15:35:31 +080036/* version V2 sub-banks offset base address */
37/* u2 phy banks */
38#define SSUSB_SIFSLV_V2_MISC 0x000
39#define SSUSB_SIFSLV_V2_U2FREQ 0x100
40#define SSUSB_SIFSLV_V2_U2PHY_COM 0x300
41/* u3 phy banks */
42#define SSUSB_SIFSLV_V2_SPLLC 0x000
43#define SSUSB_SIFSLV_V2_CHIP 0x100
44#define SSUSB_SIFSLV_V2_U3PHYD 0x200
45#define SSUSB_SIFSLV_V2_U3PHYA 0x400
Chunfeng Yundc7f1902015-09-29 11:01:36 +080046
Chunfeng Yun8d6e1952017-03-31 15:35:31 +080047#define U3P_USBPHYACR0 0x000
Chunfeng Yundc7f1902015-09-29 11:01:36 +080048#define PA0_RG_U2PLL_FORCE_ON BIT(15)
Chunfeng Yunc0250fe2017-03-31 15:35:32 +080049#define PA0_RG_USB20_INTR_EN BIT(5)
Chunfeng Yundc7f1902015-09-29 11:01:36 +080050
Chunfeng Yun8d6e1952017-03-31 15:35:31 +080051#define U3P_USBPHYACR2 0x008
Chunfeng Yundc7f1902015-09-29 11:01:36 +080052#define PA2_RG_SIF_U2PLL_FORCE_EN BIT(18)
53
Chunfeng Yun8d6e1952017-03-31 15:35:31 +080054#define U3P_USBPHYACR5 0x014
Chunfeng Yun75f072f2015-12-04 10:11:05 +080055#define PA5_RG_U2_HSTX_SRCAL_EN BIT(15)
Chunfeng Yundc7f1902015-09-29 11:01:36 +080056#define PA5_RG_U2_HSTX_SRCTRL GENMASK(14, 12)
57#define PA5_RG_U2_HSTX_SRCTRL_VAL(x) ((0x7 & (x)) << 12)
58#define PA5_RG_U2_HS_100U_U3_EN BIT(11)
59
Chunfeng Yun8d6e1952017-03-31 15:35:31 +080060#define U3P_USBPHYACR6 0x018
Chunfeng Yundc7f1902015-09-29 11:01:36 +080061#define PA6_RG_U2_BC11_SW_EN BIT(23)
62#define PA6_RG_U2_OTG_VBUSCMP_EN BIT(20)
Chunfeng Yun43f53b12015-12-04 10:08:56 +080063#define PA6_RG_U2_SQTH GENMASK(3, 0)
64#define PA6_RG_U2_SQTH_VAL(x) (0xf & (x))
Chunfeng Yundc7f1902015-09-29 11:01:36 +080065
Chunfeng Yun8d6e1952017-03-31 15:35:31 +080066#define U3P_U2PHYACR4 0x020
Chunfeng Yundc7f1902015-09-29 11:01:36 +080067#define P2C_RG_USB20_GPIO_CTL BIT(9)
68#define P2C_USB20_GPIO_MODE BIT(8)
69#define P2C_U2_GPIO_CTR_MSK (P2C_RG_USB20_GPIO_CTL | P2C_USB20_GPIO_MODE)
70
Chunfeng Yun8d6e1952017-03-31 15:35:31 +080071#define U3D_U2PHYDCR0 0x060
Chunfeng Yundc7f1902015-09-29 11:01:36 +080072#define P2C_RG_SIF_U2PLL_FORCE_ON BIT(24)
73
Chunfeng Yun8d6e1952017-03-31 15:35:31 +080074#define U3P_U2PHYDTM0 0x068
Chunfeng Yundc7f1902015-09-29 11:01:36 +080075#define P2C_FORCE_UART_EN BIT(26)
76#define P2C_FORCE_DATAIN BIT(23)
77#define P2C_FORCE_DM_PULLDOWN BIT(21)
78#define P2C_FORCE_DP_PULLDOWN BIT(20)
79#define P2C_FORCE_XCVRSEL BIT(19)
80#define P2C_FORCE_SUSPENDM BIT(18)
81#define P2C_FORCE_TERMSEL BIT(17)
82#define P2C_RG_DATAIN GENMASK(13, 10)
83#define P2C_RG_DATAIN_VAL(x) ((0xf & (x)) << 10)
84#define P2C_RG_DMPULLDOWN BIT(7)
85#define P2C_RG_DPPULLDOWN BIT(6)
86#define P2C_RG_XCVRSEL GENMASK(5, 4)
87#define P2C_RG_XCVRSEL_VAL(x) ((0x3 & (x)) << 4)
88#define P2C_RG_SUSPENDM BIT(3)
89#define P2C_RG_TERMSEL BIT(2)
90#define P2C_DTM0_PART_MASK \
91 (P2C_FORCE_DATAIN | P2C_FORCE_DM_PULLDOWN | \
92 P2C_FORCE_DP_PULLDOWN | P2C_FORCE_XCVRSEL | \
93 P2C_FORCE_TERMSEL | P2C_RG_DMPULLDOWN | \
94 P2C_RG_DPPULLDOWN | P2C_RG_TERMSEL)
95
Chunfeng Yun8d6e1952017-03-31 15:35:31 +080096#define U3P_U2PHYDTM1 0x06C
Chunfeng Yundc7f1902015-09-29 11:01:36 +080097#define P2C_RG_UART_EN BIT(16)
98#define P2C_RG_VBUSVALID BIT(5)
99#define P2C_RG_SESSEND BIT(4)
100#define P2C_RG_AVALID BIT(2)
101
Chunfeng Yun8d6e1952017-03-31 15:35:31 +0800102#define U3P_U3_PHYA_REG6 0x018
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800103#define P3A_RG_TX_EIDLE_CM GENMASK(31, 28)
104#define P3A_RG_TX_EIDLE_CM_VAL(x) ((0xf & (x)) << 28)
105
Chunfeng Yun8d6e1952017-03-31 15:35:31 +0800106#define U3P_U3_PHYA_REG9 0x024
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800107#define P3A_RG_RX_DAC_MUX GENMASK(5, 1)
108#define P3A_RG_RX_DAC_MUX_VAL(x) ((0x1f & (x)) << 1)
109
Chunfeng Yun8d6e1952017-03-31 15:35:31 +0800110#define U3P_U3_PHYA_DA_REG0 0x100
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800111#define P3A_RG_XTAL_EXT_EN_U3 GENMASK(11, 10)
112#define P3A_RG_XTAL_EXT_EN_U3_VAL(x) ((0x3 & (x)) << 10)
113
Chunfeng Yun8d6e1952017-03-31 15:35:31 +0800114#define U3P_U3_PHYD_LFPS1 0x00c
Chunfeng Yun98cd83a2017-03-31 15:35:28 +0800115#define P3D_RG_FWAKE_TH GENMASK(21, 16)
116#define P3D_RG_FWAKE_TH_VAL(x) ((0x3f & (x)) << 16)
117
Chunfeng Yun8d6e1952017-03-31 15:35:31 +0800118#define U3P_U3_PHYD_CDR1 0x05c
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800119#define P3D_RG_CDR_BIR_LTD1 GENMASK(28, 24)
120#define P3D_RG_CDR_BIR_LTD1_VAL(x) ((0x1f & (x)) << 24)
121#define P3D_RG_CDR_BIR_LTD0 GENMASK(12, 8)
122#define P3D_RG_CDR_BIR_LTD0_VAL(x) ((0x1f & (x)) << 8)
123
Chunfeng Yun8d6e1952017-03-31 15:35:31 +0800124#define U3P_U3_PHYD_RXDET1 0x128
Chunfeng Yun1969f692017-03-31 15:35:27 +0800125#define P3D_RG_RXDET_STB2_SET GENMASK(17, 9)
126#define P3D_RG_RXDET_STB2_SET_VAL(x) ((0x1ff & (x)) << 9)
127
Chunfeng Yun8d6e1952017-03-31 15:35:31 +0800128#define U3P_U3_PHYD_RXDET2 0x12c
Chunfeng Yun1969f692017-03-31 15:35:27 +0800129#define P3D_RG_RXDET_STB2_SET_P3 GENMASK(8, 0)
130#define P3D_RG_RXDET_STB2_SET_P3_VAL(x) (0x1ff & (x))
131
Chunfeng Yun8d6e1952017-03-31 15:35:31 +0800132#define U3P_SPLLC_XTALCTL3 0x018
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800133#define XC3_RG_U3_XTAL_RX_PWD BIT(9)
134#define XC3_RG_U3_FRC_XTAL_RX_PWD BIT(8)
135
Chunfeng Yun8d6e1952017-03-31 15:35:31 +0800136#define U3P_U2FREQ_FMCR0 0x00
Chunfeng Yun75f072f2015-12-04 10:11:05 +0800137#define P2F_RG_MONCLK_SEL GENMASK(27, 26)
138#define P2F_RG_MONCLK_SEL_VAL(x) ((0x3 & (x)) << 26)
139#define P2F_RG_FREQDET_EN BIT(24)
140#define P2F_RG_CYCLECNT GENMASK(23, 0)
141#define P2F_RG_CYCLECNT_VAL(x) ((P2F_RG_CYCLECNT) & (x))
142
Chunfeng Yun8d6e1952017-03-31 15:35:31 +0800143#define U3P_U2FREQ_VALUE 0x0c
Chunfeng Yun75f072f2015-12-04 10:11:05 +0800144
Chunfeng Yun8d6e1952017-03-31 15:35:31 +0800145#define U3P_U2FREQ_FMMONR1 0x10
Chunfeng Yun75f072f2015-12-04 10:11:05 +0800146#define P2F_USB_FM_VALID BIT(0)
147#define P2F_RG_FRCK_EN BIT(8)
148
149#define U3P_REF_CLK 26 /* MHZ */
150#define U3P_SLEW_RATE_COEF 28
151#define U3P_SR_COEF_DIVISOR 1000
152#define U3P_FM_DET_CYCLE_CNT 1024
153
Chunfeng Yun8d6e1952017-03-31 15:35:31 +0800154enum mt_phy_version {
155 MT_PHY_V1 = 1,
156 MT_PHY_V2,
157};
158
Chunfeng Yune1d76532016-04-20 08:14:02 +0800159struct mt65xx_phy_pdata {
160 /* avoid RX sensitivity level degradation only for mt8173 */
161 bool avoid_rx_sen_degradation;
Chunfeng Yun8d6e1952017-03-31 15:35:31 +0800162 enum mt_phy_version version;
163};
164
165struct u2phy_banks {
166 void __iomem *misc;
167 void __iomem *fmreg;
168 void __iomem *com;
169};
170
171struct u3phy_banks {
172 void __iomem *spllc;
173 void __iomem *chip;
174 void __iomem *phyd; /* include u3phyd_bank2 */
175 void __iomem *phya; /* include u3phya_da */
Chunfeng Yune1d76532016-04-20 08:14:02 +0800176};
177
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800178struct mt65xx_phy_instance {
179 struct phy *phy;
180 void __iomem *port_base;
Chunfeng Yun8d6e1952017-03-31 15:35:31 +0800181 union {
182 struct u2phy_banks u2_banks;
183 struct u3phy_banks u3_banks;
184 };
Chunfeng Yun15de15c2017-03-31 15:35:30 +0800185 struct clk *ref_clk; /* reference clock of anolog phy */
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800186 u32 index;
187 u8 type;
188};
189
190struct mt65xx_u3phy {
191 struct device *dev;
Chunfeng Yun04466ef2017-03-31 15:35:29 +0800192 void __iomem *sif_base; /* only shared sif */
Chunfeng Yun15de15c2017-03-31 15:35:30 +0800193 /* deprecated, use @ref_clk instead in phy instance */
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800194 struct clk *u3phya_ref; /* reference clock of usb3 anolog phy */
Chunfeng Yune1d76532016-04-20 08:14:02 +0800195 const struct mt65xx_phy_pdata *pdata;
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800196 struct mt65xx_phy_instance **phys;
197 int nphys;
198};
199
Chunfeng Yun75f072f2015-12-04 10:11:05 +0800200static void hs_slew_rate_calibrate(struct mt65xx_u3phy *u3phy,
201 struct mt65xx_phy_instance *instance)
202{
Chunfeng Yun8d6e1952017-03-31 15:35:31 +0800203 struct u2phy_banks *u2_banks = &instance->u2_banks;
204 void __iomem *fmreg = u2_banks->fmreg;
205 void __iomem *com = u2_banks->com;
Chunfeng Yun75f072f2015-12-04 10:11:05 +0800206 int calibration_val;
207 int fm_out;
208 u32 tmp;
209
210 /* enable USB ring oscillator */
Chunfeng Yun8d6e1952017-03-31 15:35:31 +0800211 tmp = readl(com + U3P_USBPHYACR5);
Chunfeng Yun75f072f2015-12-04 10:11:05 +0800212 tmp |= PA5_RG_U2_HSTX_SRCAL_EN;
Chunfeng Yun8d6e1952017-03-31 15:35:31 +0800213 writel(tmp, com + U3P_USBPHYACR5);
Chunfeng Yun75f072f2015-12-04 10:11:05 +0800214 udelay(1);
215
216 /*enable free run clock */
Chunfeng Yun8d6e1952017-03-31 15:35:31 +0800217 tmp = readl(fmreg + U3P_U2FREQ_FMMONR1);
Chunfeng Yun75f072f2015-12-04 10:11:05 +0800218 tmp |= P2F_RG_FRCK_EN;
Chunfeng Yun8d6e1952017-03-31 15:35:31 +0800219 writel(tmp, fmreg + U3P_U2FREQ_FMMONR1);
Chunfeng Yun75f072f2015-12-04 10:11:05 +0800220
221 /* set cycle count as 1024, and select u2 channel */
Chunfeng Yun8d6e1952017-03-31 15:35:31 +0800222 tmp = readl(fmreg + U3P_U2FREQ_FMCR0);
Chunfeng Yun75f072f2015-12-04 10:11:05 +0800223 tmp &= ~(P2F_RG_CYCLECNT | P2F_RG_MONCLK_SEL);
224 tmp |= P2F_RG_CYCLECNT_VAL(U3P_FM_DET_CYCLE_CNT);
Chunfeng Yun8d6e1952017-03-31 15:35:31 +0800225 if (u3phy->pdata->version == MT_PHY_V1)
226 tmp |= P2F_RG_MONCLK_SEL_VAL(instance->index >> 1);
227
228 writel(tmp, fmreg + U3P_U2FREQ_FMCR0);
Chunfeng Yun75f072f2015-12-04 10:11:05 +0800229
230 /* enable frequency meter */
Chunfeng Yun8d6e1952017-03-31 15:35:31 +0800231 tmp = readl(fmreg + U3P_U2FREQ_FMCR0);
Chunfeng Yun75f072f2015-12-04 10:11:05 +0800232 tmp |= P2F_RG_FREQDET_EN;
Chunfeng Yun8d6e1952017-03-31 15:35:31 +0800233 writel(tmp, fmreg + U3P_U2FREQ_FMCR0);
Chunfeng Yun75f072f2015-12-04 10:11:05 +0800234
235 /* ignore return value */
Chunfeng Yun8d6e1952017-03-31 15:35:31 +0800236 readl_poll_timeout(fmreg + U3P_U2FREQ_FMMONR1, tmp,
237 (tmp & P2F_USB_FM_VALID), 10, 200);
Chunfeng Yun75f072f2015-12-04 10:11:05 +0800238
Chunfeng Yun8d6e1952017-03-31 15:35:31 +0800239 fm_out = readl(fmreg + U3P_U2FREQ_VALUE);
Chunfeng Yun75f072f2015-12-04 10:11:05 +0800240
241 /* disable frequency meter */
Chunfeng Yun8d6e1952017-03-31 15:35:31 +0800242 tmp = readl(fmreg + U3P_U2FREQ_FMCR0);
Chunfeng Yun75f072f2015-12-04 10:11:05 +0800243 tmp &= ~P2F_RG_FREQDET_EN;
Chunfeng Yun8d6e1952017-03-31 15:35:31 +0800244 writel(tmp, fmreg + U3P_U2FREQ_FMCR0);
Chunfeng Yun75f072f2015-12-04 10:11:05 +0800245
246 /*disable free run clock */
Chunfeng Yun8d6e1952017-03-31 15:35:31 +0800247 tmp = readl(fmreg + U3P_U2FREQ_FMMONR1);
Chunfeng Yun75f072f2015-12-04 10:11:05 +0800248 tmp &= ~P2F_RG_FRCK_EN;
Chunfeng Yun8d6e1952017-03-31 15:35:31 +0800249 writel(tmp, fmreg + U3P_U2FREQ_FMMONR1);
Chunfeng Yun75f072f2015-12-04 10:11:05 +0800250
251 if (fm_out) {
252 /* ( 1024 / FM_OUT ) x reference clock frequency x 0.028 */
253 tmp = U3P_FM_DET_CYCLE_CNT * U3P_REF_CLK * U3P_SLEW_RATE_COEF;
254 tmp /= fm_out;
255 calibration_val = DIV_ROUND_CLOSEST(tmp, U3P_SR_COEF_DIVISOR);
256 } else {
257 /* if FM detection fail, set default value */
258 calibration_val = 4;
259 }
260 dev_dbg(u3phy->dev, "phy:%d, fm_out:%d, calib:%d\n",
261 instance->index, fm_out, calibration_val);
262
263 /* set HS slew rate */
Chunfeng Yun8d6e1952017-03-31 15:35:31 +0800264 tmp = readl(com + U3P_USBPHYACR5);
Chunfeng Yun75f072f2015-12-04 10:11:05 +0800265 tmp &= ~PA5_RG_U2_HSTX_SRCTRL;
266 tmp |= PA5_RG_U2_HSTX_SRCTRL_VAL(calibration_val);
Chunfeng Yun8d6e1952017-03-31 15:35:31 +0800267 writel(tmp, com + U3P_USBPHYACR5);
Chunfeng Yun75f072f2015-12-04 10:11:05 +0800268
269 /* disable USB ring oscillator */
Chunfeng Yun8d6e1952017-03-31 15:35:31 +0800270 tmp = readl(com + U3P_USBPHYACR5);
Chunfeng Yun75f072f2015-12-04 10:11:05 +0800271 tmp &= ~PA5_RG_U2_HSTX_SRCAL_EN;
Chunfeng Yun8d6e1952017-03-31 15:35:31 +0800272 writel(tmp, com + U3P_USBPHYACR5);
Chunfeng Yun75f072f2015-12-04 10:11:05 +0800273}
274
Chunfeng Yun04466ef2017-03-31 15:35:29 +0800275static void u3_phy_instance_init(struct mt65xx_u3phy *u3phy,
276 struct mt65xx_phy_instance *instance)
277{
Chunfeng Yun8d6e1952017-03-31 15:35:31 +0800278 struct u3phy_banks *u3_banks = &instance->u3_banks;
Chunfeng Yun04466ef2017-03-31 15:35:29 +0800279 u32 tmp;
280
281 /* gating PCIe Analog XTAL clock */
Chunfeng Yun8d6e1952017-03-31 15:35:31 +0800282 tmp = readl(u3_banks->spllc + U3P_SPLLC_XTALCTL3);
Chunfeng Yun04466ef2017-03-31 15:35:29 +0800283 tmp |= XC3_RG_U3_XTAL_RX_PWD | XC3_RG_U3_FRC_XTAL_RX_PWD;
Chunfeng Yun8d6e1952017-03-31 15:35:31 +0800284 writel(tmp, u3_banks->spllc + U3P_SPLLC_XTALCTL3);
Chunfeng Yun04466ef2017-03-31 15:35:29 +0800285
286 /* gating XSQ */
Chunfeng Yun8d6e1952017-03-31 15:35:31 +0800287 tmp = readl(u3_banks->phya + U3P_U3_PHYA_DA_REG0);
Chunfeng Yun04466ef2017-03-31 15:35:29 +0800288 tmp &= ~P3A_RG_XTAL_EXT_EN_U3;
289 tmp |= P3A_RG_XTAL_EXT_EN_U3_VAL(2);
Chunfeng Yun8d6e1952017-03-31 15:35:31 +0800290 writel(tmp, u3_banks->phya + U3P_U3_PHYA_DA_REG0);
Chunfeng Yun04466ef2017-03-31 15:35:29 +0800291
Chunfeng Yun8d6e1952017-03-31 15:35:31 +0800292 tmp = readl(u3_banks->phya + U3P_U3_PHYA_REG9);
Chunfeng Yun04466ef2017-03-31 15:35:29 +0800293 tmp &= ~P3A_RG_RX_DAC_MUX;
294 tmp |= P3A_RG_RX_DAC_MUX_VAL(4);
Chunfeng Yun8d6e1952017-03-31 15:35:31 +0800295 writel(tmp, u3_banks->phya + U3P_U3_PHYA_REG9);
Chunfeng Yun04466ef2017-03-31 15:35:29 +0800296
Chunfeng Yun8d6e1952017-03-31 15:35:31 +0800297 tmp = readl(u3_banks->phya + U3P_U3_PHYA_REG6);
Chunfeng Yun04466ef2017-03-31 15:35:29 +0800298 tmp &= ~P3A_RG_TX_EIDLE_CM;
299 tmp |= P3A_RG_TX_EIDLE_CM_VAL(0xe);
Chunfeng Yun8d6e1952017-03-31 15:35:31 +0800300 writel(tmp, u3_banks->phya + U3P_U3_PHYA_REG6);
Chunfeng Yun04466ef2017-03-31 15:35:29 +0800301
Chunfeng Yun8d6e1952017-03-31 15:35:31 +0800302 tmp = readl(u3_banks->phyd + U3P_U3_PHYD_CDR1);
Chunfeng Yun04466ef2017-03-31 15:35:29 +0800303 tmp &= ~(P3D_RG_CDR_BIR_LTD0 | P3D_RG_CDR_BIR_LTD1);
304 tmp |= P3D_RG_CDR_BIR_LTD0_VAL(0xc) | P3D_RG_CDR_BIR_LTD1_VAL(0x3);
Chunfeng Yun8d6e1952017-03-31 15:35:31 +0800305 writel(tmp, u3_banks->phyd + U3P_U3_PHYD_CDR1);
Chunfeng Yun04466ef2017-03-31 15:35:29 +0800306
Chunfeng Yun8d6e1952017-03-31 15:35:31 +0800307 tmp = readl(u3_banks->phyd + U3P_U3_PHYD_LFPS1);
Chunfeng Yun04466ef2017-03-31 15:35:29 +0800308 tmp &= ~P3D_RG_FWAKE_TH;
309 tmp |= P3D_RG_FWAKE_TH_VAL(0x34);
Chunfeng Yun8d6e1952017-03-31 15:35:31 +0800310 writel(tmp, u3_banks->phyd + U3P_U3_PHYD_LFPS1);
Chunfeng Yun04466ef2017-03-31 15:35:29 +0800311
Chunfeng Yun8d6e1952017-03-31 15:35:31 +0800312 tmp = readl(u3_banks->phyd + U3P_U3_PHYD_RXDET1);
Chunfeng Yun04466ef2017-03-31 15:35:29 +0800313 tmp &= ~P3D_RG_RXDET_STB2_SET;
314 tmp |= P3D_RG_RXDET_STB2_SET_VAL(0x10);
Chunfeng Yun8d6e1952017-03-31 15:35:31 +0800315 writel(tmp, u3_banks->phyd + U3P_U3_PHYD_RXDET1);
Chunfeng Yun04466ef2017-03-31 15:35:29 +0800316
Chunfeng Yun8d6e1952017-03-31 15:35:31 +0800317 tmp = readl(u3_banks->phyd + U3P_U3_PHYD_RXDET2);
Chunfeng Yun04466ef2017-03-31 15:35:29 +0800318 tmp &= ~P3D_RG_RXDET_STB2_SET_P3;
319 tmp |= P3D_RG_RXDET_STB2_SET_P3_VAL(0x10);
Chunfeng Yun8d6e1952017-03-31 15:35:31 +0800320 writel(tmp, u3_banks->phyd + U3P_U3_PHYD_RXDET2);
Chunfeng Yun04466ef2017-03-31 15:35:29 +0800321
322 dev_dbg(u3phy->dev, "%s(%d)\n", __func__, instance->index);
323}
324
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800325static void phy_instance_init(struct mt65xx_u3phy *u3phy,
326 struct mt65xx_phy_instance *instance)
327{
Chunfeng Yun8d6e1952017-03-31 15:35:31 +0800328 struct u2phy_banks *u2_banks = &instance->u2_banks;
329 void __iomem *com = u2_banks->com;
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800330 u32 index = instance->index;
331 u32 tmp;
332
333 /* switch to USB function. (system register, force ip into usb mode) */
Chunfeng Yun8d6e1952017-03-31 15:35:31 +0800334 tmp = readl(com + U3P_U2PHYDTM0);
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800335 tmp &= ~P2C_FORCE_UART_EN;
336 tmp |= P2C_RG_XCVRSEL_VAL(1) | P2C_RG_DATAIN_VAL(0);
Chunfeng Yun8d6e1952017-03-31 15:35:31 +0800337 writel(tmp, com + U3P_U2PHYDTM0);
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800338
Chunfeng Yun8d6e1952017-03-31 15:35:31 +0800339 tmp = readl(com + U3P_U2PHYDTM1);
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800340 tmp &= ~P2C_RG_UART_EN;
Chunfeng Yun8d6e1952017-03-31 15:35:31 +0800341 writel(tmp, com + U3P_U2PHYDTM1);
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800342
Chunfeng Yunc0250fe2017-03-31 15:35:32 +0800343 tmp = readl(com + U3P_USBPHYACR0);
344 tmp |= PA0_RG_USB20_INTR_EN;
345 writel(tmp, com + U3P_USBPHYACR0);
346
347 /* disable switch 100uA current to SSUSB */
348 tmp = readl(com + U3P_USBPHYACR5);
349 tmp &= ~PA5_RG_U2_HS_100U_U3_EN;
350 writel(tmp, com + U3P_USBPHYACR5);
351
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800352 if (!index) {
Chunfeng Yun8d6e1952017-03-31 15:35:31 +0800353 tmp = readl(com + U3P_U2PHYACR4);
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800354 tmp &= ~P2C_U2_GPIO_CTR_MSK;
Chunfeng Yun8d6e1952017-03-31 15:35:31 +0800355 writel(tmp, com + U3P_U2PHYACR4);
Chunfeng Yune1d76532016-04-20 08:14:02 +0800356 }
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800357
Chunfeng Yune1d76532016-04-20 08:14:02 +0800358 if (u3phy->pdata->avoid_rx_sen_degradation) {
359 if (!index) {
Chunfeng Yun8d6e1952017-03-31 15:35:31 +0800360 tmp = readl(com + U3P_USBPHYACR2);
Chunfeng Yune1d76532016-04-20 08:14:02 +0800361 tmp |= PA2_RG_SIF_U2PLL_FORCE_EN;
Chunfeng Yun8d6e1952017-03-31 15:35:31 +0800362 writel(tmp, com + U3P_USBPHYACR2);
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800363
Chunfeng Yun8d6e1952017-03-31 15:35:31 +0800364 tmp = readl(com + U3D_U2PHYDCR0);
Chunfeng Yune1d76532016-04-20 08:14:02 +0800365 tmp &= ~P2C_RG_SIF_U2PLL_FORCE_ON;
Chunfeng Yun8d6e1952017-03-31 15:35:31 +0800366 writel(tmp, com + U3D_U2PHYDCR0);
Chunfeng Yune1d76532016-04-20 08:14:02 +0800367 } else {
Chunfeng Yun8d6e1952017-03-31 15:35:31 +0800368 tmp = readl(com + U3D_U2PHYDCR0);
Chunfeng Yune1d76532016-04-20 08:14:02 +0800369 tmp |= P2C_RG_SIF_U2PLL_FORCE_ON;
Chunfeng Yun8d6e1952017-03-31 15:35:31 +0800370 writel(tmp, com + U3D_U2PHYDCR0);
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800371
Chunfeng Yun8d6e1952017-03-31 15:35:31 +0800372 tmp = readl(com + U3P_U2PHYDTM0);
Chunfeng Yune1d76532016-04-20 08:14:02 +0800373 tmp |= P2C_RG_SUSPENDM | P2C_FORCE_SUSPENDM;
Chunfeng Yun8d6e1952017-03-31 15:35:31 +0800374 writel(tmp, com + U3P_U2PHYDTM0);
Chunfeng Yune1d76532016-04-20 08:14:02 +0800375 }
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800376 }
377
Chunfeng Yun8d6e1952017-03-31 15:35:31 +0800378 tmp = readl(com + U3P_USBPHYACR6);
Chunfeng Yun43f53b12015-12-04 10:08:56 +0800379 tmp &= ~PA6_RG_U2_BC11_SW_EN; /* DP/DM BC1.1 path Disable */
380 tmp &= ~PA6_RG_U2_SQTH;
381 tmp |= PA6_RG_U2_SQTH_VAL(2);
Chunfeng Yun8d6e1952017-03-31 15:35:31 +0800382 writel(tmp, com + U3P_USBPHYACR6);
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800383
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800384 dev_dbg(u3phy->dev, "%s(%d)\n", __func__, index);
385}
386
387static void phy_instance_power_on(struct mt65xx_u3phy *u3phy,
388 struct mt65xx_phy_instance *instance)
389{
Chunfeng Yun8d6e1952017-03-31 15:35:31 +0800390 struct u2phy_banks *u2_banks = &instance->u2_banks;
391 void __iomem *com = u2_banks->com;
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800392 u32 index = instance->index;
393 u32 tmp;
394
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800395 /* (force_suspendm=0) (let suspendm=1, enable usb 480MHz pll) */
Chunfeng Yun8d6e1952017-03-31 15:35:31 +0800396 tmp = readl(com + U3P_U2PHYDTM0);
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800397 tmp &= ~(P2C_FORCE_SUSPENDM | P2C_RG_XCVRSEL);
398 tmp &= ~(P2C_RG_DATAIN | P2C_DTM0_PART_MASK);
Chunfeng Yun8d6e1952017-03-31 15:35:31 +0800399 writel(tmp, com + U3P_U2PHYDTM0);
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800400
401 /* OTG Enable */
Chunfeng Yun8d6e1952017-03-31 15:35:31 +0800402 tmp = readl(com + U3P_USBPHYACR6);
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800403 tmp |= PA6_RG_U2_OTG_VBUSCMP_EN;
Chunfeng Yun8d6e1952017-03-31 15:35:31 +0800404 writel(tmp, com + U3P_USBPHYACR6);
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800405
Chunfeng Yun8d6e1952017-03-31 15:35:31 +0800406 tmp = readl(com + U3P_U2PHYDTM1);
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800407 tmp |= P2C_RG_VBUSVALID | P2C_RG_AVALID;
408 tmp &= ~P2C_RG_SESSEND;
Chunfeng Yun8d6e1952017-03-31 15:35:31 +0800409 writel(tmp, com + U3P_U2PHYDTM1);
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800410
Chunfeng Yune1d76532016-04-20 08:14:02 +0800411 if (u3phy->pdata->avoid_rx_sen_degradation && index) {
Chunfeng Yun8d6e1952017-03-31 15:35:31 +0800412 tmp = readl(com + U3D_U2PHYDCR0);
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800413 tmp |= P2C_RG_SIF_U2PLL_FORCE_ON;
Chunfeng Yun8d6e1952017-03-31 15:35:31 +0800414 writel(tmp, com + U3D_U2PHYDCR0);
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800415
Chunfeng Yun8d6e1952017-03-31 15:35:31 +0800416 tmp = readl(com + U3P_U2PHYDTM0);
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800417 tmp |= P2C_RG_SUSPENDM | P2C_FORCE_SUSPENDM;
Chunfeng Yun8d6e1952017-03-31 15:35:31 +0800418 writel(tmp, com + U3P_U2PHYDTM0);
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800419 }
420 dev_dbg(u3phy->dev, "%s(%d)\n", __func__, index);
421}
422
423static void phy_instance_power_off(struct mt65xx_u3phy *u3phy,
424 struct mt65xx_phy_instance *instance)
425{
Chunfeng Yun8d6e1952017-03-31 15:35:31 +0800426 struct u2phy_banks *u2_banks = &instance->u2_banks;
427 void __iomem *com = u2_banks->com;
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800428 u32 index = instance->index;
429 u32 tmp;
430
Chunfeng Yun8d6e1952017-03-31 15:35:31 +0800431 tmp = readl(com + U3P_U2PHYDTM0);
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800432 tmp &= ~(P2C_RG_XCVRSEL | P2C_RG_DATAIN);
433 tmp |= P2C_FORCE_SUSPENDM;
Chunfeng Yun8d6e1952017-03-31 15:35:31 +0800434 writel(tmp, com + U3P_U2PHYDTM0);
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800435
436 /* OTG Disable */
Chunfeng Yun8d6e1952017-03-31 15:35:31 +0800437 tmp = readl(com + U3P_USBPHYACR6);
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800438 tmp &= ~PA6_RG_U2_OTG_VBUSCMP_EN;
Chunfeng Yun8d6e1952017-03-31 15:35:31 +0800439 writel(tmp, com + U3P_USBPHYACR6);
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800440
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800441 /* let suspendm=0, set utmi into analog power down */
Chunfeng Yun8d6e1952017-03-31 15:35:31 +0800442 tmp = readl(com + U3P_U2PHYDTM0);
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800443 tmp &= ~P2C_RG_SUSPENDM;
Chunfeng Yun8d6e1952017-03-31 15:35:31 +0800444 writel(tmp, com + U3P_U2PHYDTM0);
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800445 udelay(1);
446
Chunfeng Yun8d6e1952017-03-31 15:35:31 +0800447 tmp = readl(com + U3P_U2PHYDTM1);
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800448 tmp &= ~(P2C_RG_VBUSVALID | P2C_RG_AVALID);
449 tmp |= P2C_RG_SESSEND;
Chunfeng Yun8d6e1952017-03-31 15:35:31 +0800450 writel(tmp, com + U3P_U2PHYDTM1);
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800451
Chunfeng Yune1d76532016-04-20 08:14:02 +0800452 if (u3phy->pdata->avoid_rx_sen_degradation && index) {
Chunfeng Yun8d6e1952017-03-31 15:35:31 +0800453 tmp = readl(com + U3D_U2PHYDCR0);
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800454 tmp &= ~P2C_RG_SIF_U2PLL_FORCE_ON;
Chunfeng Yun8d6e1952017-03-31 15:35:31 +0800455 writel(tmp, com + U3D_U2PHYDCR0);
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800456 }
457
458 dev_dbg(u3phy->dev, "%s(%d)\n", __func__, index);
459}
460
461static void phy_instance_exit(struct mt65xx_u3phy *u3phy,
462 struct mt65xx_phy_instance *instance)
463{
Chunfeng Yun8d6e1952017-03-31 15:35:31 +0800464 struct u2phy_banks *u2_banks = &instance->u2_banks;
465 void __iomem *com = u2_banks->com;
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800466 u32 index = instance->index;
467 u32 tmp;
468
Chunfeng Yune1d76532016-04-20 08:14:02 +0800469 if (u3phy->pdata->avoid_rx_sen_degradation && index) {
Chunfeng Yun8d6e1952017-03-31 15:35:31 +0800470 tmp = readl(com + U3D_U2PHYDCR0);
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800471 tmp &= ~P2C_RG_SIF_U2PLL_FORCE_ON;
Chunfeng Yun8d6e1952017-03-31 15:35:31 +0800472 writel(tmp, com + U3D_U2PHYDCR0);
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800473
Chunfeng Yun8d6e1952017-03-31 15:35:31 +0800474 tmp = readl(com + U3P_U2PHYDTM0);
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800475 tmp &= ~P2C_FORCE_SUSPENDM;
Chunfeng Yun8d6e1952017-03-31 15:35:31 +0800476 writel(tmp, com + U3P_U2PHYDTM0);
477 }
478}
479
480static void phy_v1_banks_init(struct mt65xx_u3phy *u3phy,
481 struct mt65xx_phy_instance *instance)
482{
483 struct u2phy_banks *u2_banks = &instance->u2_banks;
484 struct u3phy_banks *u3_banks = &instance->u3_banks;
485
486 if (instance->type == PHY_TYPE_USB2) {
487 u2_banks->misc = NULL;
488 u2_banks->fmreg = u3phy->sif_base + SSUSB_SIFSLV_V1_U2FREQ;
489 u2_banks->com = instance->port_base + SSUSB_SIFSLV_V1_U2PHY_COM;
490 } else if (instance->type == PHY_TYPE_USB3) {
491 u3_banks->spllc = u3phy->sif_base + SSUSB_SIFSLV_V1_SPLLC;
492 u3_banks->chip = NULL;
493 u3_banks->phyd = instance->port_base + SSUSB_SIFSLV_V1_U3PHYD;
494 u3_banks->phya = instance->port_base + SSUSB_SIFSLV_V1_U3PHYA;
495 }
496}
497
498static void phy_v2_banks_init(struct mt65xx_u3phy *u3phy,
499 struct mt65xx_phy_instance *instance)
500{
501 struct u2phy_banks *u2_banks = &instance->u2_banks;
502 struct u3phy_banks *u3_banks = &instance->u3_banks;
503
504 if (instance->type == PHY_TYPE_USB2) {
505 u2_banks->misc = instance->port_base + SSUSB_SIFSLV_V2_MISC;
506 u2_banks->fmreg = instance->port_base + SSUSB_SIFSLV_V2_U2FREQ;
507 u2_banks->com = instance->port_base + SSUSB_SIFSLV_V2_U2PHY_COM;
508 } else if (instance->type == PHY_TYPE_USB3) {
509 u3_banks->spllc = instance->port_base + SSUSB_SIFSLV_V2_SPLLC;
510 u3_banks->chip = instance->port_base + SSUSB_SIFSLV_V2_CHIP;
511 u3_banks->phyd = instance->port_base + SSUSB_SIFSLV_V2_U3PHYD;
512 u3_banks->phya = instance->port_base + SSUSB_SIFSLV_V2_U3PHYA;
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800513 }
514}
515
516static int mt65xx_phy_init(struct phy *phy)
517{
518 struct mt65xx_phy_instance *instance = phy_get_drvdata(phy);
519 struct mt65xx_u3phy *u3phy = dev_get_drvdata(phy->dev.parent);
520 int ret;
521
522 ret = clk_prepare_enable(u3phy->u3phya_ref);
523 if (ret) {
524 dev_err(u3phy->dev, "failed to enable u3phya_ref\n");
525 return ret;
526 }
527
Chunfeng Yun15de15c2017-03-31 15:35:30 +0800528 ret = clk_prepare_enable(instance->ref_clk);
529 if (ret) {
530 dev_err(u3phy->dev, "failed to enable ref_clk\n");
531 return ret;
532 }
533
Chunfeng Yun04466ef2017-03-31 15:35:29 +0800534 if (instance->type == PHY_TYPE_USB2)
535 phy_instance_init(u3phy, instance);
536 else
537 u3_phy_instance_init(u3phy, instance);
538
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800539 return 0;
540}
541
542static int mt65xx_phy_power_on(struct phy *phy)
543{
544 struct mt65xx_phy_instance *instance = phy_get_drvdata(phy);
545 struct mt65xx_u3phy *u3phy = dev_get_drvdata(phy->dev.parent);
546
Chunfeng Yun04466ef2017-03-31 15:35:29 +0800547 if (instance->type == PHY_TYPE_USB2) {
548 phy_instance_power_on(u3phy, instance);
549 hs_slew_rate_calibrate(u3phy, instance);
550 }
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800551 return 0;
552}
553
554static int mt65xx_phy_power_off(struct phy *phy)
555{
556 struct mt65xx_phy_instance *instance = phy_get_drvdata(phy);
557 struct mt65xx_u3phy *u3phy = dev_get_drvdata(phy->dev.parent);
558
Chunfeng Yun04466ef2017-03-31 15:35:29 +0800559 if (instance->type == PHY_TYPE_USB2)
560 phy_instance_power_off(u3phy, instance);
561
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800562 return 0;
563}
564
565static int mt65xx_phy_exit(struct phy *phy)
566{
567 struct mt65xx_phy_instance *instance = phy_get_drvdata(phy);
568 struct mt65xx_u3phy *u3phy = dev_get_drvdata(phy->dev.parent);
569
Chunfeng Yun04466ef2017-03-31 15:35:29 +0800570 if (instance->type == PHY_TYPE_USB2)
571 phy_instance_exit(u3phy, instance);
572
Chunfeng Yun15de15c2017-03-31 15:35:30 +0800573 clk_disable_unprepare(instance->ref_clk);
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800574 clk_disable_unprepare(u3phy->u3phya_ref);
575 return 0;
576}
577
578static struct phy *mt65xx_phy_xlate(struct device *dev,
579 struct of_phandle_args *args)
580{
581 struct mt65xx_u3phy *u3phy = dev_get_drvdata(dev);
582 struct mt65xx_phy_instance *instance = NULL;
583 struct device_node *phy_np = args->np;
584 int index;
585
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800586 if (args->args_count != 1) {
587 dev_err(dev, "invalid number of cells in 'phy' property\n");
588 return ERR_PTR(-EINVAL);
589 }
590
591 for (index = 0; index < u3phy->nphys; index++)
592 if (phy_np == u3phy->phys[index]->phy->dev.of_node) {
593 instance = u3phy->phys[index];
594 break;
595 }
596
597 if (!instance) {
598 dev_err(dev, "failed to find appropriate phy\n");
599 return ERR_PTR(-EINVAL);
600 }
601
602 instance->type = args->args[0];
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800603 if (!(instance->type == PHY_TYPE_USB2 ||
604 instance->type == PHY_TYPE_USB3)) {
605 dev_err(dev, "unsupported device type: %d\n", instance->type);
606 return ERR_PTR(-EINVAL);
607 }
608
Chunfeng Yun8d6e1952017-03-31 15:35:31 +0800609 if (u3phy->pdata->version == MT_PHY_V1) {
610 phy_v1_banks_init(u3phy, instance);
611 } else if (u3phy->pdata->version == MT_PHY_V2) {
612 phy_v2_banks_init(u3phy, instance);
613 } else {
614 dev_err(dev, "phy version is not supported\n");
615 return ERR_PTR(-EINVAL);
616 }
617
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800618 return instance->phy;
619}
620
Bhumika Goyala8df2762017-01-08 16:05:56 +0530621static const struct phy_ops mt65xx_u3phy_ops = {
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800622 .init = mt65xx_phy_init,
623 .exit = mt65xx_phy_exit,
624 .power_on = mt65xx_phy_power_on,
625 .power_off = mt65xx_phy_power_off,
626 .owner = THIS_MODULE,
627};
628
Chunfeng Yune1d76532016-04-20 08:14:02 +0800629static const struct mt65xx_phy_pdata mt2701_pdata = {
630 .avoid_rx_sen_degradation = false,
Chunfeng Yun8d6e1952017-03-31 15:35:31 +0800631 .version = MT_PHY_V1,
632};
633
634static const struct mt65xx_phy_pdata mt2712_pdata = {
635 .avoid_rx_sen_degradation = false,
636 .version = MT_PHY_V2,
Chunfeng Yune1d76532016-04-20 08:14:02 +0800637};
638
639static const struct mt65xx_phy_pdata mt8173_pdata = {
640 .avoid_rx_sen_degradation = true,
Chunfeng Yun8d6e1952017-03-31 15:35:31 +0800641 .version = MT_PHY_V1,
Chunfeng Yune1d76532016-04-20 08:14:02 +0800642};
643
644static const struct of_device_id mt65xx_u3phy_id_table[] = {
645 { .compatible = "mediatek,mt2701-u3phy", .data = &mt2701_pdata },
Chunfeng Yun8d6e1952017-03-31 15:35:31 +0800646 { .compatible = "mediatek,mt2712-u3phy", .data = &mt2712_pdata },
Chunfeng Yune1d76532016-04-20 08:14:02 +0800647 { .compatible = "mediatek,mt8173-u3phy", .data = &mt8173_pdata },
648 { },
649};
650MODULE_DEVICE_TABLE(of, mt65xx_u3phy_id_table);
651
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800652static int mt65xx_u3phy_probe(struct platform_device *pdev)
653{
Chunfeng Yune1d76532016-04-20 08:14:02 +0800654 const struct of_device_id *match;
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800655 struct device *dev = &pdev->dev;
656 struct device_node *np = dev->of_node;
657 struct device_node *child_np;
658 struct phy_provider *provider;
659 struct resource *sif_res;
660 struct mt65xx_u3phy *u3phy;
661 struct resource res;
Julia Lawall2bb80cc2015-11-16 12:33:15 +0100662 int port, retval;
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800663
Chunfeng Yune1d76532016-04-20 08:14:02 +0800664 match = of_match_node(mt65xx_u3phy_id_table, pdev->dev.of_node);
665 if (!match)
666 return -EINVAL;
667
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800668 u3phy = devm_kzalloc(dev, sizeof(*u3phy), GFP_KERNEL);
669 if (!u3phy)
670 return -ENOMEM;
671
Chunfeng Yune1d76532016-04-20 08:14:02 +0800672 u3phy->pdata = match->data;
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800673 u3phy->nphys = of_get_child_count(np);
674 u3phy->phys = devm_kcalloc(dev, u3phy->nphys,
675 sizeof(*u3phy->phys), GFP_KERNEL);
676 if (!u3phy->phys)
677 return -ENOMEM;
678
679 u3phy->dev = dev;
680 platform_set_drvdata(pdev, u3phy);
681
Chunfeng Yun8d6e1952017-03-31 15:35:31 +0800682 if (u3phy->pdata->version == MT_PHY_V1) {
683 /* get banks shared by multiple phys */
684 sif_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
685 u3phy->sif_base = devm_ioremap_resource(dev, sif_res);
686 if (IS_ERR(u3phy->sif_base)) {
687 dev_err(dev, "failed to remap sif regs\n");
688 return PTR_ERR(u3phy->sif_base);
689 }
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800690 }
691
Chunfeng Yun15de15c2017-03-31 15:35:30 +0800692 /* it's deprecated, make it optional for backward compatibility */
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800693 u3phy->u3phya_ref = devm_clk_get(dev, "u3phya_ref");
694 if (IS_ERR(u3phy->u3phya_ref)) {
Chunfeng Yun15de15c2017-03-31 15:35:30 +0800695 if (PTR_ERR(u3phy->u3phya_ref) == -EPROBE_DEFER)
696 return -EPROBE_DEFER;
697
698 u3phy->u3phya_ref = NULL;
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800699 }
700
701 port = 0;
702 for_each_child_of_node(np, child_np) {
703 struct mt65xx_phy_instance *instance;
704 struct phy *phy;
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800705
706 instance = devm_kzalloc(dev, sizeof(*instance), GFP_KERNEL);
Julia Lawall2bb80cc2015-11-16 12:33:15 +0100707 if (!instance) {
708 retval = -ENOMEM;
709 goto put_child;
710 }
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800711
712 u3phy->phys[port] = instance;
713
714 phy = devm_phy_create(dev, child_np, &mt65xx_u3phy_ops);
715 if (IS_ERR(phy)) {
716 dev_err(dev, "failed to create phy\n");
Julia Lawall2bb80cc2015-11-16 12:33:15 +0100717 retval = PTR_ERR(phy);
718 goto put_child;
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800719 }
720
721 retval = of_address_to_resource(child_np, 0, &res);
722 if (retval) {
723 dev_err(dev, "failed to get address resource(id-%d)\n",
724 port);
Julia Lawall2bb80cc2015-11-16 12:33:15 +0100725 goto put_child;
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800726 }
727
728 instance->port_base = devm_ioremap_resource(&phy->dev, &res);
729 if (IS_ERR(instance->port_base)) {
730 dev_err(dev, "failed to remap phy regs\n");
Julia Lawall2bb80cc2015-11-16 12:33:15 +0100731 retval = PTR_ERR(instance->port_base);
732 goto put_child;
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800733 }
734
735 instance->phy = phy;
736 instance->index = port;
737 phy_set_drvdata(phy, instance);
738 port++;
Chunfeng Yun15de15c2017-03-31 15:35:30 +0800739
740 /* if deprecated clock is provided, ignore instance's one */
741 if (u3phy->u3phya_ref)
742 continue;
743
744 instance->ref_clk = devm_clk_get(&phy->dev, "ref");
745 if (IS_ERR(instance->ref_clk)) {
746 dev_err(dev, "failed to get ref_clk(id-%d)\n", port);
747 retval = PTR_ERR(instance->ref_clk);
748 goto put_child;
749 }
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800750 }
751
752 provider = devm_of_phy_provider_register(dev, mt65xx_phy_xlate);
753
754 return PTR_ERR_OR_ZERO(provider);
Julia Lawall2bb80cc2015-11-16 12:33:15 +0100755put_child:
756 of_node_put(child_np);
757 return retval;
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800758}
759
Chunfeng Yundc7f1902015-09-29 11:01:36 +0800760static struct platform_driver mt65xx_u3phy_driver = {
761 .probe = mt65xx_u3phy_probe,
762 .driver = {
763 .name = "mt65xx-u3phy",
764 .of_match_table = mt65xx_u3phy_id_table,
765 },
766};
767
768module_platform_driver(mt65xx_u3phy_driver);
769
770MODULE_AUTHOR("Chunfeng Yun <chunfeng.yun@mediatek.com>");
771MODULE_DESCRIPTION("mt65xx USB PHY driver");
772MODULE_LICENSE("GPL v2");