1 // SPDX-License-Identifier: GPL-2.0
3 * MediaTek USB3.1 gen2 xsphy Driver
5 * Copyright (c) 2018 MediaTek Inc.
6 * Author: Chunfeng Yun <chunfeng.yun@mediatek.com>
10 #include <dt-bindings/phy/phy.h>
11 #include <linux/clk.h>
12 #include <linux/delay.h>
13 #include <linux/iopoll.h>
14 #include <linux/module.h>
15 #include <linux/of_address.h>
16 #include <linux/phy/phy.h>
17 #include <linux/platform_device.h>
19 #include "phy-mtk-io.h"
22 #define SSUSB_SIFSLV_MISC 0x000
23 #define SSUSB_SIFSLV_U2FREQ 0x100
24 #define SSUSB_SIFSLV_U2PHY_COM 0x300
26 /* u3 phy shared banks */
27 #define SSPXTP_SIFSLV_DIG_GLB 0x000
28 #define SSPXTP_SIFSLV_PHYA_GLB 0x100
31 #define SSPXTP_SIFSLV_DIG_LN_TOP 0x000
32 #define SSPXTP_SIFSLV_DIG_LN_TX0 0x100
33 #define SSPXTP_SIFSLV_DIG_LN_RX0 0x200
34 #define SSPXTP_SIFSLV_DIG_LN_DAIF 0x300
35 #define SSPXTP_SIFSLV_PHYA_LN 0x400
37 #define XSP_U2FREQ_FMCR0 ((SSUSB_SIFSLV_U2FREQ) + 0x00)
38 #define P2F_RG_FREQDET_EN BIT(24)
39 #define P2F_RG_CYCLECNT GENMASK(23, 0)
40 #define P2F_RG_CYCLECNT_VAL(x) ((P2F_RG_CYCLECNT) & (x))
42 #define XSP_U2FREQ_MMONR0 ((SSUSB_SIFSLV_U2FREQ) + 0x0c)
44 #define XSP_U2FREQ_FMMONR1 ((SSUSB_SIFSLV_U2FREQ) + 0x10)
45 #define P2F_RG_FRCK_EN BIT(8)
46 #define P2F_USB_FM_VALID BIT(0)
48 #define XSP_USBPHYACR0 ((SSUSB_SIFSLV_U2PHY_COM) + 0x00)
49 #define P2A0_RG_INTR_EN BIT(5)
51 #define XSP_USBPHYACR1 ((SSUSB_SIFSLV_U2PHY_COM) + 0x04)
52 #define P2A1_RG_INTR_CAL GENMASK(23, 19)
53 #define P2A1_RG_INTR_CAL_VAL(x) ((0x1f & (x)) << 19)
54 #define P2A1_RG_VRT_SEL GENMASK(14, 12)
55 #define P2A1_RG_VRT_SEL_VAL(x) ((0x7 & (x)) << 12)
56 #define P2A1_RG_TERM_SEL GENMASK(10, 8)
57 #define P2A1_RG_TERM_SEL_VAL(x) ((0x7 & (x)) << 8)
59 #define XSP_USBPHYACR5 ((SSUSB_SIFSLV_U2PHY_COM) + 0x014)
60 #define P2A5_RG_HSTX_SRCAL_EN BIT(15)
61 #define P2A5_RG_HSTX_SRCTRL GENMASK(14, 12)
62 #define P2A5_RG_HSTX_SRCTRL_VAL(x) ((0x7 & (x)) << 12)
64 #define XSP_USBPHYACR6 ((SSUSB_SIFSLV_U2PHY_COM) + 0x018)
65 #define P2A6_RG_BC11_SW_EN BIT(23)
66 #define P2A6_RG_OTG_VBUSCMP_EN BIT(20)
68 #define XSP_U2PHYDTM1 ((SSUSB_SIFSLV_U2PHY_COM) + 0x06C)
69 #define P2D_FORCE_IDDIG BIT(9)
70 #define P2D_RG_VBUSVALID BIT(5)
71 #define P2D_RG_SESSEND BIT(4)
72 #define P2D_RG_AVALID BIT(2)
73 #define P2D_RG_IDDIG BIT(1)
75 #define SSPXTP_PHYA_GLB_00 ((SSPXTP_SIFSLV_PHYA_GLB) + 0x00)
76 #define RG_XTP_GLB_BIAS_INTR_CTRL GENMASK(21, 16)
77 #define RG_XTP_GLB_BIAS_INTR_CTRL_VAL(x) ((0x3f & (x)) << 16)
79 #define SSPXTP_PHYA_LN_04 ((SSPXTP_SIFSLV_PHYA_LN) + 0x04)
80 #define RG_XTP_LN0_TX_IMPSEL GENMASK(4, 0)
81 #define RG_XTP_LN0_TX_IMPSEL_VAL(x) (0x1f & (x))
83 #define SSPXTP_PHYA_LN_14 ((SSPXTP_SIFSLV_PHYA_LN) + 0x014)
84 #define RG_XTP_LN0_RX_IMPSEL GENMASK(4, 0)
85 #define RG_XTP_LN0_RX_IMPSEL_VAL(x) (0x1f & (x))
87 #define XSP_REF_CLK 26 /* MHZ */
88 #define XSP_SLEW_RATE_COEF 17
89 #define XSP_SR_COEF_DIVISOR 1000
90 #define XSP_FM_DET_CYCLE_CNT 1024
92 struct xsphy_instance {
94 void __iomem *port_base;
95 struct clk *ref_clk; /* reference clock of anolog phy */
98 /* only for HQA test */
110 void __iomem *glb_base; /* only shared u3 sif */
111 struct xsphy_instance **phys;
113 int src_ref_clk; /* MHZ, reference clock for slew rate calibrate */
114 int src_coef; /* coefficient for slew rate calibrate */
117 static void u2_phy_slew_rate_calibrate(struct mtk_xsphy *xsphy,
118 struct xsphy_instance *inst)
120 void __iomem *pbase = inst->port_base;
125 /* use force value */
129 /* enable USB ring oscillator */
130 mtk_phy_set_bits(pbase + XSP_USBPHYACR5, P2A5_RG_HSTX_SRCAL_EN);
131 udelay(1); /* wait clock stable */
133 /* enable free run clock */
134 mtk_phy_set_bits(pbase + XSP_U2FREQ_FMMONR1, P2F_RG_FRCK_EN);
136 /* set cycle count as 1024 */
137 mtk_phy_update_bits(pbase + XSP_U2FREQ_FMCR0, P2F_RG_CYCLECNT,
138 P2F_RG_CYCLECNT_VAL(XSP_FM_DET_CYCLE_CNT));
140 /* enable frequency meter */
141 mtk_phy_set_bits(pbase + XSP_U2FREQ_FMCR0, P2F_RG_FREQDET_EN);
143 /* ignore return value */
144 readl_poll_timeout(pbase + XSP_U2FREQ_FMMONR1, tmp,
145 (tmp & P2F_USB_FM_VALID), 10, 200);
147 fm_out = readl(pbase + XSP_U2FREQ_MMONR0);
149 /* disable frequency meter */
150 mtk_phy_clear_bits(pbase + XSP_U2FREQ_FMCR0, P2F_RG_FREQDET_EN);
152 /* disable free run clock */
153 mtk_phy_clear_bits(pbase + XSP_U2FREQ_FMMONR1, P2F_RG_FRCK_EN);
156 /* (1024 / FM_OUT) x reference clock frequency x coefficient */
157 tmp = xsphy->src_ref_clk * xsphy->src_coef;
158 tmp = (tmp * XSP_FM_DET_CYCLE_CNT) / fm_out;
159 calib_val = DIV_ROUND_CLOSEST(tmp, XSP_SR_COEF_DIVISOR);
161 /* if FM detection fail, set default value */
164 dev_dbg(xsphy->dev, "phy.%d, fm_out:%d, calib:%d (clk:%d, coef:%d)\n",
165 inst->index, fm_out, calib_val,
166 xsphy->src_ref_clk, xsphy->src_coef);
168 /* set HS slew rate */
169 mtk_phy_update_bits(pbase + XSP_USBPHYACR5, P2A5_RG_HSTX_SRCTRL,
170 P2A5_RG_HSTX_SRCTRL_VAL(calib_val));
172 /* disable USB ring oscillator */
173 mtk_phy_clear_bits(pbase + XSP_USBPHYACR5, P2A5_RG_HSTX_SRCAL_EN);
176 static void u2_phy_instance_init(struct mtk_xsphy *xsphy,
177 struct xsphy_instance *inst)
179 void __iomem *pbase = inst->port_base;
181 /* DP/DM BC1.1 path Disable */
182 mtk_phy_clear_bits(pbase + XSP_USBPHYACR6, P2A6_RG_BC11_SW_EN);
184 mtk_phy_set_bits(pbase + XSP_USBPHYACR0, P2A0_RG_INTR_EN);
187 static void u2_phy_instance_power_on(struct mtk_xsphy *xsphy,
188 struct xsphy_instance *inst)
190 void __iomem *pbase = inst->port_base;
191 u32 index = inst->index;
193 mtk_phy_set_bits(pbase + XSP_USBPHYACR6, P2A6_RG_OTG_VBUSCMP_EN);
195 mtk_phy_update_bits(pbase + XSP_U2PHYDTM1,
196 P2D_RG_VBUSVALID | P2D_RG_AVALID | P2D_RG_SESSEND,
197 P2D_RG_VBUSVALID | P2D_RG_AVALID);
199 dev_dbg(xsphy->dev, "%s(%d)\n", __func__, index);
202 static void u2_phy_instance_power_off(struct mtk_xsphy *xsphy,
203 struct xsphy_instance *inst)
205 void __iomem *pbase = inst->port_base;
206 u32 index = inst->index;
208 mtk_phy_clear_bits(pbase + XSP_USBPHYACR6, P2A6_RG_OTG_VBUSCMP_EN);
210 mtk_phy_update_bits(pbase + XSP_U2PHYDTM1,
211 P2D_RG_VBUSVALID | P2D_RG_AVALID | P2D_RG_SESSEND,
214 dev_dbg(xsphy->dev, "%s(%d)\n", __func__, index);
217 static void u2_phy_instance_set_mode(struct mtk_xsphy *xsphy,
218 struct xsphy_instance *inst,
223 tmp = readl(inst->port_base + XSP_U2PHYDTM1);
225 case PHY_MODE_USB_DEVICE:
226 tmp |= P2D_FORCE_IDDIG | P2D_RG_IDDIG;
228 case PHY_MODE_USB_HOST:
229 tmp |= P2D_FORCE_IDDIG;
230 tmp &= ~P2D_RG_IDDIG;
232 case PHY_MODE_USB_OTG:
233 tmp &= ~(P2D_FORCE_IDDIG | P2D_RG_IDDIG);
238 writel(tmp, inst->port_base + XSP_U2PHYDTM1);
241 static void phy_parse_property(struct mtk_xsphy *xsphy,
242 struct xsphy_instance *inst)
244 struct device *dev = &inst->phy->dev;
246 switch (inst->type) {
248 device_property_read_u32(dev, "mediatek,efuse-intr",
250 device_property_read_u32(dev, "mediatek,eye-src",
252 device_property_read_u32(dev, "mediatek,eye-vrt",
254 device_property_read_u32(dev, "mediatek,eye-term",
256 dev_dbg(dev, "intr:%d, src:%d, vrt:%d, term:%d\n",
257 inst->efuse_intr, inst->eye_src,
258 inst->eye_vrt, inst->eye_term);
261 device_property_read_u32(dev, "mediatek,efuse-intr",
263 device_property_read_u32(dev, "mediatek,efuse-tx-imp",
264 &inst->efuse_tx_imp);
265 device_property_read_u32(dev, "mediatek,efuse-rx-imp",
266 &inst->efuse_rx_imp);
267 dev_dbg(dev, "intr:%d, tx-imp:%d, rx-imp:%d\n",
268 inst->efuse_intr, inst->efuse_tx_imp,
272 dev_err(xsphy->dev, "incompatible phy type\n");
277 static void u2_phy_props_set(struct mtk_xsphy *xsphy,
278 struct xsphy_instance *inst)
280 void __iomem *pbase = inst->port_base;
282 if (inst->efuse_intr)
283 mtk_phy_update_bits(pbase + XSP_USBPHYACR1, P2A1_RG_INTR_CAL,
284 P2A1_RG_INTR_CAL_VAL(inst->efuse_intr));
287 mtk_phy_update_bits(pbase + XSP_USBPHYACR5, P2A5_RG_HSTX_SRCTRL,
288 P2A5_RG_HSTX_SRCTRL_VAL(inst->eye_src));
291 mtk_phy_update_bits(pbase + XSP_USBPHYACR1, P2A1_RG_VRT_SEL,
292 P2A1_RG_VRT_SEL_VAL(inst->eye_vrt));
295 mtk_phy_update_bits(pbase + XSP_USBPHYACR1, P2A1_RG_TERM_SEL,
296 P2A1_RG_TERM_SEL_VAL(inst->eye_term));
299 static void u3_phy_props_set(struct mtk_xsphy *xsphy,
300 struct xsphy_instance *inst)
302 void __iomem *pbase = inst->port_base;
304 if (inst->efuse_intr)
305 mtk_phy_update_bits(xsphy->glb_base + SSPXTP_PHYA_GLB_00,
306 RG_XTP_GLB_BIAS_INTR_CTRL,
307 RG_XTP_GLB_BIAS_INTR_CTRL_VAL(inst->efuse_intr));
309 if (inst->efuse_tx_imp)
310 mtk_phy_update_bits(pbase + SSPXTP_PHYA_LN_04,
311 RG_XTP_LN0_TX_IMPSEL,
312 RG_XTP_LN0_TX_IMPSEL_VAL(inst->efuse_tx_imp));
314 if (inst->efuse_rx_imp)
315 mtk_phy_update_bits(pbase + SSPXTP_PHYA_LN_14,
316 RG_XTP_LN0_RX_IMPSEL,
317 RG_XTP_LN0_RX_IMPSEL_VAL(inst->efuse_rx_imp));
320 static int mtk_phy_init(struct phy *phy)
322 struct xsphy_instance *inst = phy_get_drvdata(phy);
323 struct mtk_xsphy *xsphy = dev_get_drvdata(phy->dev.parent);
326 ret = clk_prepare_enable(inst->ref_clk);
328 dev_err(xsphy->dev, "failed to enable ref_clk\n");
332 switch (inst->type) {
334 u2_phy_instance_init(xsphy, inst);
335 u2_phy_props_set(xsphy, inst);
338 u3_phy_props_set(xsphy, inst);
341 dev_err(xsphy->dev, "incompatible phy type\n");
342 clk_disable_unprepare(inst->ref_clk);
349 static int mtk_phy_power_on(struct phy *phy)
351 struct xsphy_instance *inst = phy_get_drvdata(phy);
352 struct mtk_xsphy *xsphy = dev_get_drvdata(phy->dev.parent);
354 if (inst->type == PHY_TYPE_USB2) {
355 u2_phy_instance_power_on(xsphy, inst);
356 u2_phy_slew_rate_calibrate(xsphy, inst);
362 static int mtk_phy_power_off(struct phy *phy)
364 struct xsphy_instance *inst = phy_get_drvdata(phy);
365 struct mtk_xsphy *xsphy = dev_get_drvdata(phy->dev.parent);
367 if (inst->type == PHY_TYPE_USB2)
368 u2_phy_instance_power_off(xsphy, inst);
373 static int mtk_phy_exit(struct phy *phy)
375 struct xsphy_instance *inst = phy_get_drvdata(phy);
377 clk_disable_unprepare(inst->ref_clk);
381 static int mtk_phy_set_mode(struct phy *phy, enum phy_mode mode, int submode)
383 struct xsphy_instance *inst = phy_get_drvdata(phy);
384 struct mtk_xsphy *xsphy = dev_get_drvdata(phy->dev.parent);
386 if (inst->type == PHY_TYPE_USB2)
387 u2_phy_instance_set_mode(xsphy, inst, mode);
392 static struct phy *mtk_phy_xlate(struct device *dev,
393 struct of_phandle_args *args)
395 struct mtk_xsphy *xsphy = dev_get_drvdata(dev);
396 struct xsphy_instance *inst = NULL;
397 struct device_node *phy_np = args->np;
400 if (args->args_count != 1) {
401 dev_err(dev, "invalid number of cells in 'phy' property\n");
402 return ERR_PTR(-EINVAL);
405 for (index = 0; index < xsphy->nphys; index++)
406 if (phy_np == xsphy->phys[index]->phy->dev.of_node) {
407 inst = xsphy->phys[index];
412 dev_err(dev, "failed to find appropriate phy\n");
413 return ERR_PTR(-EINVAL);
416 inst->type = args->args[0];
417 if (!(inst->type == PHY_TYPE_USB2 ||
418 inst->type == PHY_TYPE_USB3)) {
419 dev_err(dev, "unsupported phy type: %d\n", inst->type);
420 return ERR_PTR(-EINVAL);
423 phy_parse_property(xsphy, inst);
428 static const struct phy_ops mtk_xsphy_ops = {
429 .init = mtk_phy_init,
430 .exit = mtk_phy_exit,
431 .power_on = mtk_phy_power_on,
432 .power_off = mtk_phy_power_off,
433 .set_mode = mtk_phy_set_mode,
434 .owner = THIS_MODULE,
437 static const struct of_device_id mtk_xsphy_id_table[] = {
438 { .compatible = "mediatek,xsphy", },
441 MODULE_DEVICE_TABLE(of, mtk_xsphy_id_table);
443 static int mtk_xsphy_probe(struct platform_device *pdev)
445 struct device *dev = &pdev->dev;
446 struct device_node *np = dev->of_node;
447 struct device_node *child_np;
448 struct phy_provider *provider;
449 struct resource *glb_res;
450 struct mtk_xsphy *xsphy;
454 xsphy = devm_kzalloc(dev, sizeof(*xsphy), GFP_KERNEL);
458 xsphy->nphys = of_get_child_count(np);
459 xsphy->phys = devm_kcalloc(dev, xsphy->nphys,
460 sizeof(*xsphy->phys), GFP_KERNEL);
465 platform_set_drvdata(pdev, xsphy);
467 glb_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
468 /* optional, may not exist if no u3 phys */
470 /* get banks shared by multiple u3 phys */
471 xsphy->glb_base = devm_ioremap_resource(dev, glb_res);
472 if (IS_ERR(xsphy->glb_base)) {
473 dev_err(dev, "failed to remap glb regs\n");
474 return PTR_ERR(xsphy->glb_base);
478 xsphy->src_ref_clk = XSP_REF_CLK;
479 xsphy->src_coef = XSP_SLEW_RATE_COEF;
480 /* update parameters of slew rate calibrate if exist */
481 device_property_read_u32(dev, "mediatek,src-ref-clk-mhz",
482 &xsphy->src_ref_clk);
483 device_property_read_u32(dev, "mediatek,src-coef", &xsphy->src_coef);
486 for_each_child_of_node(np, child_np) {
487 struct xsphy_instance *inst;
490 inst = devm_kzalloc(dev, sizeof(*inst), GFP_KERNEL);
496 xsphy->phys[port] = inst;
498 phy = devm_phy_create(dev, child_np, &mtk_xsphy_ops);
500 dev_err(dev, "failed to create phy\n");
501 retval = PTR_ERR(phy);
505 retval = of_address_to_resource(child_np, 0, &res);
507 dev_err(dev, "failed to get address resource(id-%d)\n",
512 inst->port_base = devm_ioremap_resource(&phy->dev, &res);
513 if (IS_ERR(inst->port_base)) {
514 dev_err(dev, "failed to remap phy regs\n");
515 retval = PTR_ERR(inst->port_base);
521 phy_set_drvdata(phy, inst);
524 inst->ref_clk = devm_clk_get(&phy->dev, "ref");
525 if (IS_ERR(inst->ref_clk)) {
526 dev_err(dev, "failed to get ref_clk(id-%d)\n", port);
527 retval = PTR_ERR(inst->ref_clk);
532 provider = devm_of_phy_provider_register(dev, mtk_phy_xlate);
533 return PTR_ERR_OR_ZERO(provider);
536 of_node_put(child_np);
540 static struct platform_driver mtk_xsphy_driver = {
541 .probe = mtk_xsphy_probe,
544 .of_match_table = mtk_xsphy_id_table,
548 module_platform_driver(mtk_xsphy_driver);
550 MODULE_AUTHOR("Chunfeng Yun <chunfeng.yun@mediatek.com>");
551 MODULE_DESCRIPTION("MediaTek USB XS-PHY driver");
552 MODULE_LICENSE("GPL v2");