1 // SPDX-License-Identifier: GPL-2.0-only
6 #include <linux/module.h>
8 #include <linux/phy/phy.h>
9 #include <linux/platform_device.h>
10 #include <linux/delay.h>
11 #include <linux/regmap.h>
12 #include <linux/mfd/syscon.h>
13 #include <linux/bitfield.h>
15 /* USB QSCRATCH Hardware registers */
16 #define QSCRATCH_GENERAL_CFG (0x08)
17 #define HSUSB_PHY_CTRL_REG (0x10)
20 #define HSUSB_CTRL_DMSEHV_CLAMP BIT(24)
21 #define HSUSB_CTRL_USB2_SUSPEND BIT(23)
22 #define HSUSB_CTRL_UTMI_CLK_EN BIT(21)
23 #define HSUSB_CTRL_UTMI_OTG_VBUS_VALID BIT(20)
24 #define HSUSB_CTRL_USE_CLKCORE BIT(18)
25 #define HSUSB_CTRL_DPSEHV_CLAMP BIT(17)
26 #define HSUSB_CTRL_COMMONONN BIT(11)
27 #define HSUSB_CTRL_ID_HV_CLAMP BIT(9)
28 #define HSUSB_CTRL_OTGSESSVLD_CLAMP BIT(8)
29 #define HSUSB_CTRL_CLAMP_EN BIT(7)
30 #define HSUSB_CTRL_RETENABLEN BIT(1)
31 #define HSUSB_CTRL_POR BIT(0)
33 /* QSCRATCH_GENERAL_CFG */
34 #define HSUSB_GCFG_XHCI_REV BIT(2)
36 /* USB QSCRATCH Hardware registers */
37 #define SSUSB_PHY_CTRL_REG (0x00)
38 #define SSUSB_PHY_PARAM_CTRL_1 (0x04)
39 #define SSUSB_PHY_PARAM_CTRL_2 (0x08)
40 #define CR_PROTOCOL_DATA_IN_REG (0x0c)
41 #define CR_PROTOCOL_DATA_OUT_REG (0x10)
42 #define CR_PROTOCOL_CAP_ADDR_REG (0x14)
43 #define CR_PROTOCOL_CAP_DATA_REG (0x18)
44 #define CR_PROTOCOL_READ_REG (0x1c)
45 #define CR_PROTOCOL_WRITE_REG (0x20)
48 #define SSUSB_CTRL_REF_USE_PAD BIT(28)
49 #define SSUSB_CTRL_TEST_POWERDOWN BIT(27)
50 #define SSUSB_CTRL_LANE0_PWR_PRESENT BIT(24)
51 #define SSUSB_CTRL_SS_PHY_EN BIT(8)
52 #define SSUSB_CTRL_SS_PHY_RESET BIT(7)
54 /* SSPHY control registers - Does this need 0x30? */
55 #define SSPHY_CTRL_RX_OVRD_IN_HI(lane) (0x1006 + 0x100 * (lane))
56 #define SSPHY_CTRL_TX_OVRD_DRV_LO(lane) (0x1002 + 0x100 * (lane))
58 /* SSPHY SoC version specific values */
59 #define SSPHY_RX_EQ_VALUE 4 /* Override value for rx_eq */
60 /* Override value for transmit preemphasis */
61 #define SSPHY_TX_DEEMPH_3_5DB 23
62 /* Override value for mpll */
63 #define SSPHY_MPLL_VALUE 0
65 /* QSCRATCH PHY_PARAM_CTRL1 fields */
66 #define PHY_PARAM_CTRL1_TX_FULL_SWING_MASK GENMASK(26, 19)
67 #define PHY_PARAM_CTRL1_TX_DEEMPH_6DB_MASK GENMASK(19, 13)
68 #define PHY_PARAM_CTRL1_TX_DEEMPH_3_5DB_MASK GENMASK(13, 7)
69 #define PHY_PARAM_CTRL1_LOS_BIAS_MASK GENMASK(7, 2)
71 #define PHY_PARAM_CTRL1_MASK \
72 (PHY_PARAM_CTRL1_TX_FULL_SWING_MASK | \
73 PHY_PARAM_CTRL1_TX_DEEMPH_6DB_MASK | \
74 PHY_PARAM_CTRL1_TX_DEEMPH_3_5DB_MASK | \
75 PHY_PARAM_CTRL1_LOS_BIAS_MASK)
77 #define PHY_PARAM_CTRL1_TX_FULL_SWING(x) \
78 FIELD_PREP(PHY_PARAM_CTRL1_TX_FULL_SWING_MASK, (x))
79 #define PHY_PARAM_CTRL1_TX_DEEMPH_6DB(x) \
80 FIELD_PREP(PHY_PARAM_CTRL1_TX_DEEMPH_6DB_MASK, (x))
81 #define PHY_PARAM_CTRL1_TX_DEEMPH_3_5DB(x) \
82 FIELD_PREP(PHY_PARAM_CTRL1_TX_DEEMPH_3_5DB_MASK, x)
83 #define PHY_PARAM_CTRL1_LOS_BIAS(x) \
84 FIELD_PREP(PHY_PARAM_CTRL1_LOS_BIAS_MASK, (x))
86 /* RX OVRD IN HI bits */
87 #define RX_OVRD_IN_HI_RX_RESET_OVRD BIT(13)
88 #define RX_OVRD_IN_HI_RX_RX_RESET BIT(12)
89 #define RX_OVRD_IN_HI_RX_EQ_OVRD BIT(11)
90 #define RX_OVRD_IN_HI_RX_EQ_MASK GENMASK(10, 7)
91 #define RX_OVRD_IN_HI_RX_EQ(x) FIELD_PREP(RX_OVRD_IN_HI_RX_EQ_MASK, (x))
92 #define RX_OVRD_IN_HI_RX_EQ_EN_OVRD BIT(7)
93 #define RX_OVRD_IN_HI_RX_EQ_EN BIT(6)
94 #define RX_OVRD_IN_HI_RX_LOS_FILTER_OVRD BIT(5)
95 #define RX_OVRD_IN_HI_RX_LOS_FILTER_MASK GENMASK(4, 2)
96 #define RX_OVRD_IN_HI_RX_RATE_OVRD BIT(2)
97 #define RX_OVRD_IN_HI_RX_RATE_MASK GENMASK(2, 0)
99 /* TX OVRD DRV LO register bits */
100 #define TX_OVRD_DRV_LO_AMPLITUDE_MASK GENMASK(6, 0)
101 #define TX_OVRD_DRV_LO_PREEMPH_MASK GENMASK(13, 6)
102 #define TX_OVRD_DRV_LO_PREEMPH(x) ((x) << 7)
103 #define TX_OVRD_DRV_LO_EN BIT(14)
106 #define SSPHY_MPLL_MASK GENMASK(8, 5)
107 #define SSPHY_MPLL(x) ((x) << 5)
109 /* SS CAP register bits */
110 #define SS_CR_CAP_ADDR_REG BIT(0)
111 #define SS_CR_CAP_DATA_REG BIT(0)
112 #define SS_CR_READ_REG BIT(0)
113 #define SS_CR_WRITE_REG BIT(0)
115 #define LATCH_SLEEP 40
116 #define LATCH_TIMEOUT 100
134 * usb_phy_write_readback() - Write register and read back masked value to
135 * confirm it is written
137 * @phy_dwc3: QCOM DWC3 phy context
138 * @offset: register offset.
139 * @mask: register bitmask specifying what should be updated
140 * @val: value to write.
142 static inline void usb_phy_write_readback(struct usb_phy
*phy_dwc3
,
144 const u32 mask
, u32 val
)
146 u32 write_val
, tmp
= readl(phy_dwc3
->base
+ offset
);
148 tmp
&= ~mask
; /* retain other bits */
149 write_val
= tmp
| val
;
151 writel(write_val
, phy_dwc3
->base
+ offset
);
153 /* Read back to see if val was written */
154 tmp
= readl(phy_dwc3
->base
+ offset
);
155 tmp
&= mask
; /* clear other bits */
158 dev_err(phy_dwc3
->dev
, "write: %x to QSCRATCH: %x FAILED\n", val
, offset
);
161 static int wait_for_latch(void __iomem
*addr
)
165 return readl_poll_timeout(addr
, val
, !val
, LATCH_SLEEP
, LATCH_TIMEOUT
);
169 * usb_ss_write_phycreg() - Write SSPHY register
171 * @phy_dwc3: QCOM DWC3 phy context
172 * @addr: SSPHY address to write.
173 * @val: value to write.
175 static int usb_ss_write_phycreg(struct usb_phy
*phy_dwc3
,
180 writel(addr
, phy_dwc3
->base
+ CR_PROTOCOL_DATA_IN_REG
);
181 writel(SS_CR_CAP_ADDR_REG
,
182 phy_dwc3
->base
+ CR_PROTOCOL_CAP_ADDR_REG
);
184 ret
= wait_for_latch(phy_dwc3
->base
+ CR_PROTOCOL_CAP_ADDR_REG
);
188 writel(val
, phy_dwc3
->base
+ CR_PROTOCOL_DATA_IN_REG
);
189 writel(SS_CR_CAP_DATA_REG
,
190 phy_dwc3
->base
+ CR_PROTOCOL_CAP_DATA_REG
);
192 ret
= wait_for_latch(phy_dwc3
->base
+ CR_PROTOCOL_CAP_DATA_REG
);
196 writel(SS_CR_WRITE_REG
, phy_dwc3
->base
+ CR_PROTOCOL_WRITE_REG
);
198 ret
= wait_for_latch(phy_dwc3
->base
+ CR_PROTOCOL_WRITE_REG
);
202 dev_err(phy_dwc3
->dev
, "timeout waiting for latch\n");
207 * usb_ss_read_phycreg() - Read SSPHY register.
209 * @phy_dwc3: QCOM DWC3 phy context
210 * @addr: SSPHY address to read.
211 * @val: pointer in which read is store.
213 static int usb_ss_read_phycreg(struct usb_phy
*phy_dwc3
,
218 writel(addr
, phy_dwc3
->base
+ CR_PROTOCOL_DATA_IN_REG
);
219 writel(SS_CR_CAP_ADDR_REG
,
220 phy_dwc3
->base
+ CR_PROTOCOL_CAP_ADDR_REG
);
222 ret
= wait_for_latch(phy_dwc3
->base
+ CR_PROTOCOL_CAP_ADDR_REG
);
227 * Due to hardware bug, first read of SSPHY register might be
228 * incorrect. Hence as workaround, SW should perform SSPHY register
229 * read twice, but use only second read and ignore first read.
231 writel(SS_CR_READ_REG
, phy_dwc3
->base
+ CR_PROTOCOL_READ_REG
);
233 ret
= wait_for_latch(phy_dwc3
->base
+ CR_PROTOCOL_READ_REG
);
238 readl(phy_dwc3
->base
+ CR_PROTOCOL_DATA_OUT_REG
);
240 writel(SS_CR_READ_REG
, phy_dwc3
->base
+ CR_PROTOCOL_READ_REG
);
242 ret
= wait_for_latch(phy_dwc3
->base
+ CR_PROTOCOL_READ_REG
);
246 *val
= readl(phy_dwc3
->base
+ CR_PROTOCOL_DATA_OUT_REG
);
252 static int qcom_ipq806x_usb_hs_phy_init(struct phy
*phy
)
254 struct usb_phy
*phy_dwc3
= phy_get_drvdata(phy
);
258 ret
= clk_prepare_enable(phy_dwc3
->xo_clk
);
262 ret
= clk_prepare_enable(phy_dwc3
->ref_clk
);
264 clk_disable_unprepare(phy_dwc3
->xo_clk
);
269 * HSPHY Initialization: Enable UTMI clock, select 19.2MHz fsel
270 * enable clamping, and disable RETENTION (power-on default is ENABLED)
272 val
= HSUSB_CTRL_DPSEHV_CLAMP
| HSUSB_CTRL_DMSEHV_CLAMP
|
273 HSUSB_CTRL_RETENABLEN
| HSUSB_CTRL_COMMONONN
|
274 HSUSB_CTRL_OTGSESSVLD_CLAMP
| HSUSB_CTRL_ID_HV_CLAMP
|
275 HSUSB_CTRL_UTMI_OTG_VBUS_VALID
| HSUSB_CTRL_UTMI_CLK_EN
|
276 HSUSB_CTRL_CLAMP_EN
| 0x70;
278 /* use core clock if external reference is not present */
279 if (!phy_dwc3
->xo_clk
)
280 val
|= HSUSB_CTRL_USE_CLKCORE
;
282 writel(val
, phy_dwc3
->base
+ HSUSB_PHY_CTRL_REG
);
283 usleep_range(2000, 2200);
285 /* Disable (bypass) VBUS and ID filters */
286 writel(HSUSB_GCFG_XHCI_REV
, phy_dwc3
->base
+ QSCRATCH_GENERAL_CFG
);
291 static int qcom_ipq806x_usb_hs_phy_exit(struct phy
*phy
)
293 struct usb_phy
*phy_dwc3
= phy_get_drvdata(phy
);
295 clk_disable_unprepare(phy_dwc3
->ref_clk
);
296 clk_disable_unprepare(phy_dwc3
->xo_clk
);
301 static int qcom_ipq806x_usb_ss_phy_init(struct phy
*phy
)
303 struct usb_phy
*phy_dwc3
= phy_get_drvdata(phy
);
307 ret
= clk_prepare_enable(phy_dwc3
->xo_clk
);
311 ret
= clk_prepare_enable(phy_dwc3
->ref_clk
);
313 clk_disable_unprepare(phy_dwc3
->xo_clk
);
318 data
= readl(phy_dwc3
->base
+ SSUSB_PHY_CTRL_REG
);
319 writel(data
| SSUSB_CTRL_SS_PHY_RESET
,
320 phy_dwc3
->base
+ SSUSB_PHY_CTRL_REG
);
321 usleep_range(2000, 2200);
322 writel(data
, phy_dwc3
->base
+ SSUSB_PHY_CTRL_REG
);
324 /* clear REF_PAD if we don't have XO clk */
325 if (!phy_dwc3
->xo_clk
)
326 data
&= ~SSUSB_CTRL_REF_USE_PAD
;
328 data
|= SSUSB_CTRL_REF_USE_PAD
;
330 writel(data
, phy_dwc3
->base
+ SSUSB_PHY_CTRL_REG
);
332 /* wait for ref clk to become stable, this can take up to 30ms */
335 data
|= SSUSB_CTRL_SS_PHY_EN
| SSUSB_CTRL_LANE0_PWR_PRESENT
;
336 writel(data
, phy_dwc3
->base
+ SSUSB_PHY_CTRL_REG
);
339 * WORKAROUND: There is SSPHY suspend bug due to which USB enumerates
340 * in HS mode instead of SS mode. Workaround it by asserting
341 * LANE0.TX_ALT_BLOCK.EN_ALT_BUS to enable TX to use alt bus mode
343 ret
= usb_ss_read_phycreg(phy_dwc3
, 0x102D, &data
);
348 ret
= usb_ss_write_phycreg(phy_dwc3
, 0x102D, data
);
352 ret
= usb_ss_read_phycreg(phy_dwc3
, 0x1010, &data
);
358 ret
= usb_ss_write_phycreg(phy_dwc3
, 0x1010, data
);
363 * Fix RX Equalization setting as follows
364 * LANE0.RX_OVRD_IN_HI. RX_EQ_EN set to 0
365 * LANE0.RX_OVRD_IN_HI.RX_EQ_EN_OVRD set to 1
366 * LANE0.RX_OVRD_IN_HI.RX_EQ set based on SoC version
367 * LANE0.RX_OVRD_IN_HI.RX_EQ_OVRD set to 1
369 ret
= usb_ss_read_phycreg(phy_dwc3
, SSPHY_CTRL_RX_OVRD_IN_HI(0), &data
);
373 data
&= ~RX_OVRD_IN_HI_RX_EQ_EN
;
374 data
|= RX_OVRD_IN_HI_RX_EQ_EN_OVRD
;
375 data
&= ~RX_OVRD_IN_HI_RX_EQ_MASK
;
376 data
|= RX_OVRD_IN_HI_RX_EQ(phy_dwc3
->rx_eq
);
377 data
|= RX_OVRD_IN_HI_RX_EQ_OVRD
;
378 ret
= usb_ss_write_phycreg(phy_dwc3
,
379 SSPHY_CTRL_RX_OVRD_IN_HI(0), data
);
384 * Set EQ and TX launch amplitudes as follows
385 * LANE0.TX_OVRD_DRV_LO.PREEMPH set based on SoC version
386 * LANE0.TX_OVRD_DRV_LO.AMPLITUDE set to 110
387 * LANE0.TX_OVRD_DRV_LO.EN set to 1.
389 ret
= usb_ss_read_phycreg(phy_dwc3
,
390 SSPHY_CTRL_TX_OVRD_DRV_LO(0), &data
);
394 data
&= ~TX_OVRD_DRV_LO_PREEMPH_MASK
;
395 data
|= TX_OVRD_DRV_LO_PREEMPH(phy_dwc3
->tx_deamp_3_5db
);
396 data
&= ~TX_OVRD_DRV_LO_AMPLITUDE_MASK
;
398 data
|= TX_OVRD_DRV_LO_EN
;
399 ret
= usb_ss_write_phycreg(phy_dwc3
,
400 SSPHY_CTRL_TX_OVRD_DRV_LO(0), data
);
405 data
&= ~SSPHY_MPLL_MASK
;
406 data
|= SSPHY_MPLL(phy_dwc3
->mpll
);
407 usb_ss_write_phycreg(phy_dwc3
, 0x30, data
);
410 * Set the QSCRATCH PHY_PARAM_CTRL1 parameters as follows
411 * TX_FULL_SWING [26:20] amplitude to 110
412 * TX_DEEMPH_6DB [19:14] to 32
413 * TX_DEEMPH_3_5DB [13:8] set based on SoC version
414 * LOS_BIAS [7:3] to 9
416 data
= readl(phy_dwc3
->base
+ SSUSB_PHY_PARAM_CTRL_1
);
418 data
&= ~PHY_PARAM_CTRL1_MASK
;
420 data
|= PHY_PARAM_CTRL1_TX_FULL_SWING(0x6e) |
421 PHY_PARAM_CTRL1_TX_DEEMPH_6DB(0x20) |
422 PHY_PARAM_CTRL1_TX_DEEMPH_3_5DB(phy_dwc3
->tx_deamp_3_5db
) |
423 PHY_PARAM_CTRL1_LOS_BIAS(0x9);
425 usb_phy_write_readback(phy_dwc3
, SSUSB_PHY_PARAM_CTRL_1
,
426 PHY_PARAM_CTRL1_MASK
, data
);
432 static int qcom_ipq806x_usb_ss_phy_exit(struct phy
*phy
)
434 struct usb_phy
*phy_dwc3
= phy_get_drvdata(phy
);
436 /* Sequence to put SSPHY in low power state:
437 * 1. Clear REF_PHY_EN in PHY_CTRL_REG
438 * 2. Clear REF_USE_PAD in PHY_CTRL_REG
439 * 3. Set TEST_POWERED_DOWN in PHY_CTRL_REG to enable PHY retention
441 usb_phy_write_readback(phy_dwc3
, SSUSB_PHY_CTRL_REG
,
442 SSUSB_CTRL_SS_PHY_EN
, 0x0);
443 usb_phy_write_readback(phy_dwc3
, SSUSB_PHY_CTRL_REG
,
444 SSUSB_CTRL_REF_USE_PAD
, 0x0);
445 usb_phy_write_readback(phy_dwc3
, SSUSB_PHY_CTRL_REG
,
446 SSUSB_CTRL_TEST_POWERDOWN
, 0x0);
448 clk_disable_unprepare(phy_dwc3
->ref_clk
);
449 clk_disable_unprepare(phy_dwc3
->xo_clk
);
454 static const struct phy_drvdata qcom_ipq806x_usb_hs_drvdata
= {
456 .init
= qcom_ipq806x_usb_hs_phy_init
,
457 .exit
= qcom_ipq806x_usb_hs_phy_exit
,
458 .owner
= THIS_MODULE
,
460 .clk_rate
= 60000000,
463 static const struct phy_drvdata qcom_ipq806x_usb_ss_drvdata
= {
465 .init
= qcom_ipq806x_usb_ss_phy_init
,
466 .exit
= qcom_ipq806x_usb_ss_phy_exit
,
467 .owner
= THIS_MODULE
,
469 .clk_rate
= 125000000,
472 static const struct of_device_id qcom_ipq806x_usb_phy_table
[] = {
473 { .compatible
= "qcom,ipq806x-usb-phy-hs",
474 .data
= &qcom_ipq806x_usb_hs_drvdata
},
475 { .compatible
= "qcom,ipq806x-usb-phy-ss",
476 .data
= &qcom_ipq806x_usb_ss_drvdata
},
479 MODULE_DEVICE_TABLE(of
, qcom_ipq806x_usb_phy_table
);
481 static int qcom_ipq806x_usb_phy_probe(struct platform_device
*pdev
)
483 struct resource
*res
;
484 resource_size_t size
;
485 struct phy
*generic_phy
;
486 struct usb_phy
*phy_dwc3
;
487 const struct phy_drvdata
*data
;
488 struct phy_provider
*phy_provider
;
490 phy_dwc3
= devm_kzalloc(&pdev
->dev
, sizeof(*phy_dwc3
), GFP_KERNEL
);
494 data
= of_device_get_match_data(&pdev
->dev
);
496 phy_dwc3
->dev
= &pdev
->dev
;
498 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
501 size
= resource_size(res
);
502 phy_dwc3
->base
= devm_ioremap(phy_dwc3
->dev
, res
->start
, size
);
504 if (!phy_dwc3
->base
) {
505 dev_err(phy_dwc3
->dev
, "failed to map reg\n");
509 phy_dwc3
->ref_clk
= devm_clk_get(phy_dwc3
->dev
, "ref");
510 if (IS_ERR(phy_dwc3
->ref_clk
)) {
511 dev_dbg(phy_dwc3
->dev
, "cannot get reference clock\n");
512 return PTR_ERR(phy_dwc3
->ref_clk
);
515 clk_set_rate(phy_dwc3
->ref_clk
, data
->clk_rate
);
517 phy_dwc3
->xo_clk
= devm_clk_get(phy_dwc3
->dev
, "xo");
518 if (IS_ERR(phy_dwc3
->xo_clk
)) {
519 dev_dbg(phy_dwc3
->dev
, "cannot get TCXO clock\n");
520 phy_dwc3
->xo_clk
= NULL
;
523 /* Parse device node to probe HSIO settings */
524 if (device_property_read_u32(&pdev
->dev
, "qcom,rx-eq",
526 phy_dwc3
->rx_eq
= SSPHY_RX_EQ_VALUE
;
528 if (device_property_read_u32(&pdev
->dev
, "qcom,tx-deamp_3_5db",
529 &phy_dwc3
->tx_deamp_3_5db
))
530 phy_dwc3
->tx_deamp_3_5db
= SSPHY_TX_DEEMPH_3_5DB
;
532 if (device_property_read_u32(&pdev
->dev
, "qcom,mpll", &phy_dwc3
->mpll
))
533 phy_dwc3
->mpll
= SSPHY_MPLL_VALUE
;
535 generic_phy
= devm_phy_create(phy_dwc3
->dev
, pdev
->dev
.of_node
, &data
->ops
);
537 if (IS_ERR(generic_phy
))
538 return PTR_ERR(generic_phy
);
540 phy_set_drvdata(generic_phy
, phy_dwc3
);
541 platform_set_drvdata(pdev
, phy_dwc3
);
543 phy_provider
= devm_of_phy_provider_register(phy_dwc3
->dev
,
544 of_phy_simple_xlate
);
546 if (IS_ERR(phy_provider
))
547 return PTR_ERR(phy_provider
);
552 static struct platform_driver qcom_ipq806x_usb_phy_driver
= {
553 .probe
= qcom_ipq806x_usb_phy_probe
,
555 .name
= "qcom-ipq806x-usb-phy",
556 .of_match_table
= qcom_ipq806x_usb_phy_table
,
560 module_platform_driver(qcom_ipq806x_usb_phy_driver
);
562 MODULE_ALIAS("platform:phy-qcom-ipq806x-usb");
563 MODULE_LICENSE("GPL v2");
564 MODULE_AUTHOR("Andy Gross <agross@codeaurora.org>");
565 MODULE_AUTHOR("Ivan T. Ivanov <iivanov@mm-sol.com>");
566 MODULE_DESCRIPTION("DesignWare USB3 QCOM PHY driver");