1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2023, Linaro Limited
6 #include <linux/bitfield.h>
8 #include <linux/delay.h>
9 #include <linux/iopoll.h>
10 #include <linux/phy/phy.h>
11 #include <linux/platform_device.h>
12 #include <linux/regulator/consumer.h>
13 #include <linux/reset.h>
15 #define USB_PHY_UTMI_CTRL0 (0x3c)
17 #define OPMODE_MASK GENMASK(4, 3)
18 #define OPMODE_NONDRIVING BIT(3)
20 #define USB_PHY_UTMI_CTRL5 (0x50)
23 #define USB_PHY_HS_PHY_CTRL_COMMON0 (0x54)
24 #define PHY_ENABLE BIT(0)
25 #define SIDDQ_SEL BIT(1)
27 #define RETENABLEN BIT(3)
28 #define FSEL_MASK GENMASK(6, 4)
29 #define FSEL_19_2_MHZ_VAL (0x0)
30 #define FSEL_38_4_MHZ_VAL (0x4)
32 #define USB_PHY_CFG_CTRL_1 (0x58)
33 #define PHY_CFG_PLL_CPBIAS_CNTRL_MASK GENMASK(7, 1)
35 #define USB_PHY_CFG_CTRL_2 (0x5c)
36 #define PHY_CFG_PLL_FB_DIV_7_0_MASK GENMASK(7, 0)
37 #define DIV_7_0_19_2_MHZ_VAL (0x90)
38 #define DIV_7_0_38_4_MHZ_VAL (0xc8)
40 #define USB_PHY_CFG_CTRL_3 (0x60)
41 #define PHY_CFG_PLL_FB_DIV_11_8_MASK GENMASK(3, 0)
42 #define DIV_11_8_19_2_MHZ_VAL (0x1)
43 #define DIV_11_8_38_4_MHZ_VAL (0x0)
45 #define PHY_CFG_PLL_REF_DIV GENMASK(7, 4)
46 #define PLL_REF_DIV_VAL (0x0)
48 #define USB_PHY_HS_PHY_CTRL2 (0x64)
49 #define VBUSVLDEXT0 BIT(0)
50 #define USB2_SUSPEND_N BIT(2)
51 #define USB2_SUSPEND_N_SEL BIT(3)
52 #define VBUS_DET_EXT_SEL BIT(4)
54 #define USB_PHY_CFG_CTRL_4 (0x68)
55 #define PHY_CFG_PLL_GMP_CNTRL_MASK GENMASK(1, 0)
56 #define PHY_CFG_PLL_INT_CNTRL_MASK GENMASK(7, 2)
58 #define USB_PHY_CFG_CTRL_5 (0x6c)
59 #define PHY_CFG_PLL_PROP_CNTRL_MASK GENMASK(4, 0)
60 #define PHY_CFG_PLL_VREF_TUNE_MASK GENMASK(7, 6)
62 #define USB_PHY_CFG_CTRL_6 (0x70)
63 #define PHY_CFG_PLL_VCO_CNTRL_MASK GENMASK(2, 0)
65 #define USB_PHY_CFG_CTRL_7 (0x74)
67 #define USB_PHY_CFG_CTRL_8 (0x78)
68 #define PHY_CFG_TX_FSLS_VREF_TUNE_MASK GENMASK(1, 0)
69 #define PHY_CFG_TX_FSLS_VREG_BYPASS BIT(2)
70 #define PHY_CFG_TX_HS_VREF_TUNE_MASK GENMASK(5, 3)
71 #define PHY_CFG_TX_HS_XV_TUNE_MASK GENMASK(7, 6)
73 #define USB_PHY_CFG_CTRL_9 (0x7c)
74 #define PHY_CFG_TX_PREEMP_TUNE_MASK GENMASK(2, 0)
75 #define PHY_CFG_TX_RES_TUNE_MASK GENMASK(4, 3)
76 #define PHY_CFG_TX_RISE_TUNE_MASK GENMASK(6, 5)
77 #define PHY_CFG_RCAL_BYPASS BIT(7)
79 #define USB_PHY_CFG_CTRL_10 (0x80)
81 #define USB_PHY_CFG0 (0x94)
82 #define DATAPATH_CTRL_OVERRIDE_EN BIT(0)
83 #define CMN_CTRL_OVERRIDE_EN BIT(1)
85 #define UTMI_PHY_CMN_CTRL0 (0x98)
86 #define TESTBURNIN BIT(6)
88 #define USB_PHY_FSEL_SEL (0xb8)
89 #define FSEL_SEL BIT(0)
91 #define USB_PHY_APB_ACCESS_CMD (0x130)
92 #define RW_ACCESS BIT(0)
93 #define APB_START_CMD BIT(1)
94 #define APB_LOGIC_RESET BIT(2)
96 #define USB_PHY_APB_ACCESS_STATUS (0x134)
97 #define ACCESS_DONE BIT(0)
98 #define TIMED_OUT BIT(1)
99 #define ACCESS_ERROR BIT(2)
100 #define ACCESS_IN_PROGRESS BIT(3)
102 #define USB_PHY_APB_ADDRESS (0x138)
103 #define APB_REG_ADDR_MASK GENMASK(7, 0)
105 #define USB_PHY_APB_WRDATA_LSB (0x13c)
106 #define APB_REG_WRDATA_7_0_MASK GENMASK(3, 0)
108 #define USB_PHY_APB_WRDATA_MSB (0x140)
109 #define APB_REG_WRDATA_15_8_MASK GENMASK(7, 4)
111 #define USB_PHY_APB_RDDATA_LSB (0x144)
112 #define APB_REG_RDDATA_7_0_MASK GENMASK(3, 0)
114 #define USB_PHY_APB_RDDATA_MSB (0x148)
115 #define APB_REG_RDDATA_15_8_MASK GENMASK(7, 4)
117 static const char * const eusb2_hsphy_vreg_names[] = {
121 #define EUSB2_NUM_VREGS ARRAY_SIZE(eusb2_hsphy_vreg_names)
123 struct qcom_snps_eusb2_hsphy {
128 struct reset_control *phy_reset;
130 struct regulator_bulk_data vregs[EUSB2_NUM_VREGS];
134 struct phy *repeater;
137 static int qcom_snps_eusb2_hsphy_set_mode(struct phy *p, enum phy_mode mode, int submode)
139 struct qcom_snps_eusb2_hsphy *phy = phy_get_drvdata(p);
143 return phy_set_mode_ext(phy->repeater, mode, submode);
146 static void qcom_snps_eusb2_hsphy_write_mask(void __iomem *base, u32 offset,
151 reg = readl_relaxed(base + offset);
154 writel_relaxed(reg, base + offset);
156 /* Ensure above write is completed */
157 readl_relaxed(base + offset);
160 static void qcom_eusb2_default_parameters(struct qcom_snps_eusb2_hsphy *phy)
162 /* default parameters: tx pre-emphasis */
163 qcom_snps_eusb2_hsphy_write_mask(phy->base, USB_PHY_CFG_CTRL_9,
164 PHY_CFG_TX_PREEMP_TUNE_MASK,
165 FIELD_PREP(PHY_CFG_TX_PREEMP_TUNE_MASK, 0));
167 /* tx rise/fall time */
168 qcom_snps_eusb2_hsphy_write_mask(phy->base, USB_PHY_CFG_CTRL_9,
169 PHY_CFG_TX_RISE_TUNE_MASK,
170 FIELD_PREP(PHY_CFG_TX_RISE_TUNE_MASK, 0x2));
172 /* source impedance adjustment */
173 qcom_snps_eusb2_hsphy_write_mask(phy->base, USB_PHY_CFG_CTRL_9,
174 PHY_CFG_TX_RES_TUNE_MASK,
175 FIELD_PREP(PHY_CFG_TX_RES_TUNE_MASK, 0x1));
177 /* dc voltage level adjustement */
178 qcom_snps_eusb2_hsphy_write_mask(phy->base, USB_PHY_CFG_CTRL_8,
179 PHY_CFG_TX_HS_VREF_TUNE_MASK,
180 FIELD_PREP(PHY_CFG_TX_HS_VREF_TUNE_MASK, 0x3));
182 /* transmitter HS crossover adjustement */
183 qcom_snps_eusb2_hsphy_write_mask(phy->base, USB_PHY_CFG_CTRL_8,
184 PHY_CFG_TX_HS_XV_TUNE_MASK,
185 FIELD_PREP(PHY_CFG_TX_HS_XV_TUNE_MASK, 0x0));
188 static int qcom_eusb2_ref_clk_init(struct qcom_snps_eusb2_hsphy *phy)
190 unsigned long ref_clk_freq = clk_get_rate(phy->ref_clk);
192 switch (ref_clk_freq) {
194 qcom_snps_eusb2_hsphy_write_mask(phy->base, USB_PHY_HS_PHY_CTRL_COMMON0,
196 FIELD_PREP(FSEL_MASK, FSEL_19_2_MHZ_VAL));
198 qcom_snps_eusb2_hsphy_write_mask(phy->base, USB_PHY_CFG_CTRL_2,
199 PHY_CFG_PLL_FB_DIV_7_0_MASK,
200 DIV_7_0_19_2_MHZ_VAL);
202 qcom_snps_eusb2_hsphy_write_mask(phy->base, USB_PHY_CFG_CTRL_3,
203 PHY_CFG_PLL_FB_DIV_11_8_MASK,
204 DIV_11_8_19_2_MHZ_VAL);
208 qcom_snps_eusb2_hsphy_write_mask(phy->base, USB_PHY_HS_PHY_CTRL_COMMON0,
210 FIELD_PREP(FSEL_MASK, FSEL_38_4_MHZ_VAL));
212 qcom_snps_eusb2_hsphy_write_mask(phy->base, USB_PHY_CFG_CTRL_2,
213 PHY_CFG_PLL_FB_DIV_7_0_MASK,
214 DIV_7_0_38_4_MHZ_VAL);
216 qcom_snps_eusb2_hsphy_write_mask(phy->base, USB_PHY_CFG_CTRL_3,
217 PHY_CFG_PLL_FB_DIV_11_8_MASK,
218 DIV_11_8_38_4_MHZ_VAL);
222 dev_err(&phy->phy->dev, "unsupported ref_clk_freq:%lu\n", ref_clk_freq);
226 qcom_snps_eusb2_hsphy_write_mask(phy->base, USB_PHY_CFG_CTRL_3,
227 PHY_CFG_PLL_REF_DIV, PLL_REF_DIV_VAL);
232 static int qcom_snps_eusb2_hsphy_init(struct phy *p)
234 struct qcom_snps_eusb2_hsphy *phy = phy_get_drvdata(p);
237 ret = regulator_bulk_enable(ARRAY_SIZE(phy->vregs), phy->vregs);
241 ret = phy_init(phy->repeater);
243 dev_err(&p->dev, "repeater init failed. %d\n", ret);
247 ret = clk_prepare_enable(phy->ref_clk);
249 dev_err(&p->dev, "failed to enable ref clock, %d\n", ret);
253 ret = reset_control_assert(phy->phy_reset);
255 dev_err(&p->dev, "failed to assert phy_reset, %d\n", ret);
256 goto disable_ref_clk;
259 usleep_range(100, 150);
261 ret = reset_control_deassert(phy->phy_reset);
263 dev_err(&p->dev, "failed to de-assert phy_reset, %d\n", ret);
264 goto disable_ref_clk;
267 qcom_snps_eusb2_hsphy_write_mask(phy->base, USB_PHY_CFG0,
268 CMN_CTRL_OVERRIDE_EN, CMN_CTRL_OVERRIDE_EN);
270 qcom_snps_eusb2_hsphy_write_mask(phy->base, USB_PHY_UTMI_CTRL5, POR, POR);
272 qcom_snps_eusb2_hsphy_write_mask(phy->base, USB_PHY_HS_PHY_CTRL_COMMON0,
273 PHY_ENABLE | RETENABLEN, PHY_ENABLE | RETENABLEN);
275 qcom_snps_eusb2_hsphy_write_mask(phy->base, USB_PHY_APB_ACCESS_CMD,
276 APB_LOGIC_RESET, APB_LOGIC_RESET);
278 qcom_snps_eusb2_hsphy_write_mask(phy->base, UTMI_PHY_CMN_CTRL0, TESTBURNIN, 0);
280 qcom_snps_eusb2_hsphy_write_mask(phy->base, USB_PHY_FSEL_SEL,
283 /* update ref_clk related registers */
284 ret = qcom_eusb2_ref_clk_init(phy);
286 goto disable_ref_clk;
288 qcom_snps_eusb2_hsphy_write_mask(phy->base, USB_PHY_CFG_CTRL_1,
289 PHY_CFG_PLL_CPBIAS_CNTRL_MASK,
290 FIELD_PREP(PHY_CFG_PLL_CPBIAS_CNTRL_MASK, 0x1));
292 qcom_snps_eusb2_hsphy_write_mask(phy->base, USB_PHY_CFG_CTRL_4,
293 PHY_CFG_PLL_INT_CNTRL_MASK,
294 FIELD_PREP(PHY_CFG_PLL_INT_CNTRL_MASK, 0x8));
296 qcom_snps_eusb2_hsphy_write_mask(phy->base, USB_PHY_CFG_CTRL_4,
297 PHY_CFG_PLL_GMP_CNTRL_MASK,
298 FIELD_PREP(PHY_CFG_PLL_GMP_CNTRL_MASK, 0x1));
300 qcom_snps_eusb2_hsphy_write_mask(phy->base, USB_PHY_CFG_CTRL_5,
301 PHY_CFG_PLL_PROP_CNTRL_MASK,
302 FIELD_PREP(PHY_CFG_PLL_PROP_CNTRL_MASK, 0x10));
304 qcom_snps_eusb2_hsphy_write_mask(phy->base, USB_PHY_CFG_CTRL_6,
305 PHY_CFG_PLL_VCO_CNTRL_MASK,
306 FIELD_PREP(PHY_CFG_PLL_VCO_CNTRL_MASK, 0x0));
308 qcom_snps_eusb2_hsphy_write_mask(phy->base, USB_PHY_CFG_CTRL_5,
309 PHY_CFG_PLL_VREF_TUNE_MASK,
310 FIELD_PREP(PHY_CFG_PLL_VREF_TUNE_MASK, 0x1));
312 qcom_snps_eusb2_hsphy_write_mask(phy->base, USB_PHY_HS_PHY_CTRL2,
313 VBUS_DET_EXT_SEL, VBUS_DET_EXT_SEL);
315 /* set default parameters */
316 qcom_eusb2_default_parameters(phy);
318 qcom_snps_eusb2_hsphy_write_mask(phy->base, USB_PHY_HS_PHY_CTRL2,
319 USB2_SUSPEND_N_SEL | USB2_SUSPEND_N,
320 USB2_SUSPEND_N_SEL | USB2_SUSPEND_N);
322 qcom_snps_eusb2_hsphy_write_mask(phy->base, USB_PHY_UTMI_CTRL0, SLEEPM, SLEEPM);
324 qcom_snps_eusb2_hsphy_write_mask(phy->base, USB_PHY_HS_PHY_CTRL_COMMON0,
325 SIDDQ_SEL, SIDDQ_SEL);
327 qcom_snps_eusb2_hsphy_write_mask(phy->base, USB_PHY_HS_PHY_CTRL_COMMON0,
330 qcom_snps_eusb2_hsphy_write_mask(phy->base, USB_PHY_UTMI_CTRL5, POR, 0);
332 qcom_snps_eusb2_hsphy_write_mask(phy->base, USB_PHY_HS_PHY_CTRL2,
333 USB2_SUSPEND_N_SEL, 0);
338 clk_disable_unprepare(phy->ref_clk);
341 regulator_bulk_disable(ARRAY_SIZE(phy->vregs), phy->vregs);
346 static int qcom_snps_eusb2_hsphy_exit(struct phy *p)
348 struct qcom_snps_eusb2_hsphy *phy = phy_get_drvdata(p);
350 clk_disable_unprepare(phy->ref_clk);
352 regulator_bulk_disable(ARRAY_SIZE(phy->vregs), phy->vregs);
354 phy_exit(phy->repeater);
359 static const struct phy_ops qcom_snps_eusb2_hsphy_ops = {
360 .init = qcom_snps_eusb2_hsphy_init,
361 .exit = qcom_snps_eusb2_hsphy_exit,
362 .set_mode = qcom_snps_eusb2_hsphy_set_mode,
363 .owner = THIS_MODULE,
366 static int qcom_snps_eusb2_hsphy_probe(struct platform_device *pdev)
368 struct device *dev = &pdev->dev;
369 struct device_node *np = dev->of_node;
370 struct qcom_snps_eusb2_hsphy *phy;
371 struct phy_provider *phy_provider;
372 struct phy *generic_phy;
376 phy = devm_kzalloc(dev, sizeof(*phy), GFP_KERNEL);
380 phy->base = devm_platform_ioremap_resource(pdev, 0);
381 if (IS_ERR(phy->base))
382 return PTR_ERR(phy->base);
384 phy->phy_reset = devm_reset_control_get_exclusive(dev, NULL);
385 if (IS_ERR(phy->phy_reset))
386 return PTR_ERR(phy->phy_reset);
388 phy->ref_clk = devm_clk_get(dev, "ref");
389 if (IS_ERR(phy->ref_clk))
390 return dev_err_probe(dev, PTR_ERR(phy->ref_clk),
391 "failed to get ref clk\n");
393 num = ARRAY_SIZE(phy->vregs);
394 for (i = 0; i < num; i++)
395 phy->vregs[i].supply = eusb2_hsphy_vreg_names[i];
397 ret = devm_regulator_bulk_get(dev, num, phy->vregs);
399 return dev_err_probe(dev, ret,
400 "failed to get regulator supplies\n");
402 phy->repeater = devm_of_phy_get_by_index(dev, np, 0);
403 if (IS_ERR(phy->repeater))
404 return dev_err_probe(dev, PTR_ERR(phy->repeater),
405 "failed to get repeater\n");
407 generic_phy = devm_phy_create(dev, NULL, &qcom_snps_eusb2_hsphy_ops);
408 if (IS_ERR(generic_phy)) {
409 dev_err(dev, "failed to create phy %d\n", ret);
410 return PTR_ERR(generic_phy);
413 dev_set_drvdata(dev, phy);
414 phy_set_drvdata(generic_phy, phy);
416 phy_provider = devm_of_phy_provider_register(dev, of_phy_simple_xlate);
417 if (IS_ERR(phy_provider))
418 return PTR_ERR(phy_provider);
420 dev_info(dev, "Registered Qcom-eUSB2 phy\n");
425 static const struct of_device_id qcom_snps_eusb2_hsphy_of_match_table[] = {
426 { .compatible = "qcom,sm8550-snps-eusb2-phy", },
429 MODULE_DEVICE_TABLE(of, qcom_snps_eusb2_hsphy_of_match_table);
431 static struct platform_driver qcom_snps_eusb2_hsphy_driver = {
432 .probe = qcom_snps_eusb2_hsphy_probe,
434 .name = "qcom-snps-eusb2-hsphy",
435 .of_match_table = qcom_snps_eusb2_hsphy_of_match_table,
439 module_platform_driver(qcom_snps_eusb2_hsphy_driver);
440 MODULE_DESCRIPTION("Qualcomm SNPS eUSB2 HS PHY driver");
441 MODULE_LICENSE("GPL");