1 // SPDX-License-Identifier: GPL-2.0-only
3 * Rockchip usb PHY driver
6 * Copyright (C) 2014 ROCKCHIP, Inc.
10 #include <linux/clk-provider.h>
12 #include <linux/kernel.h>
13 #include <linux/module.h>
14 #include <linux/mutex.h>
16 #include <linux/of_address.h>
17 #include <linux/of_platform.h>
18 #include <linux/phy/phy.h>
19 #include <linux/platform_device.h>
20 #include <linux/regulator/consumer.h>
21 #include <linux/reset.h>
22 #include <linux/regmap.h>
23 #include <linux/mfd/syscon.h>
24 #include <linux/delay.h>
26 static int enable_usb_uart;
28 #define HIWORD_UPDATE(val, mask) \
29 ((val) | (mask) << 16)
32 #define UOC_CON0_SIDDQ BIT(13)
33 #define UOC_CON0_DISABLE BIT(4)
34 #define UOC_CON0_COMMON_ON_N BIT(0)
37 #define UOC_CON2_SOFT_CON_SEL BIT(2)
40 /* bits present on rk3188 and rk3288 phys */
41 #define UOC_CON3_UTMI_TERMSEL_FULLSPEED BIT(5)
42 #define UOC_CON3_UTMI_XCVRSEELCT_FSTRANSC (1 << 3)
43 #define UOC_CON3_UTMI_XCVRSEELCT_MASK (3 << 3)
44 #define UOC_CON3_UTMI_OPMODE_NODRIVING (1 << 1)
45 #define UOC_CON3_UTMI_OPMODE_MASK (3 << 1)
46 #define UOC_CON3_UTMI_SUSPENDN BIT(0)
48 struct rockchip_usb_phys {
53 struct rockchip_usb_phy_base;
54 struct rockchip_usb_phy_pdata {
55 struct rockchip_usb_phys *phys;
56 int (*init_usb_uart)(struct regmap *grf,
57 const struct rockchip_usb_phy_pdata *pdata);
61 struct rockchip_usb_phy_base {
63 struct regmap *reg_base;
64 const struct rockchip_usb_phy_pdata *pdata;
67 struct rockchip_usb_phy {
68 struct rockchip_usb_phy_base *base;
69 struct device_node *np;
70 unsigned int reg_offset;
73 struct clk_hw clk480m_hw;
76 struct reset_control *reset;
77 struct regulator *vbus;
80 static int rockchip_usb_phy_power(struct rockchip_usb_phy *phy,
83 u32 val = HIWORD_UPDATE(siddq ? UOC_CON0_SIDDQ : 0, UOC_CON0_SIDDQ);
85 return regmap_write(phy->base->reg_base, phy->reg_offset, val);
88 static unsigned long rockchip_usb_phy480m_recalc_rate(struct clk_hw *hw,
89 unsigned long parent_rate)
94 static void rockchip_usb_phy480m_disable(struct clk_hw *hw)
96 struct rockchip_usb_phy *phy = container_of(hw,
97 struct rockchip_usb_phy,
101 regulator_disable(phy->vbus);
103 /* Power down usb phy analog blocks by set siddq 1 */
104 rockchip_usb_phy_power(phy, 1);
107 static int rockchip_usb_phy480m_enable(struct clk_hw *hw)
109 struct rockchip_usb_phy *phy = container_of(hw,
110 struct rockchip_usb_phy,
113 /* Power up usb phy analog blocks by set siddq 0 */
114 return rockchip_usb_phy_power(phy, 0);
117 static int rockchip_usb_phy480m_is_enabled(struct clk_hw *hw)
119 struct rockchip_usb_phy *phy = container_of(hw,
120 struct rockchip_usb_phy,
125 ret = regmap_read(phy->base->reg_base, phy->reg_offset, &val);
129 return (val & UOC_CON0_SIDDQ) ? 0 : 1;
132 static const struct clk_ops rockchip_usb_phy480m_ops = {
133 .enable = rockchip_usb_phy480m_enable,
134 .disable = rockchip_usb_phy480m_disable,
135 .is_enabled = rockchip_usb_phy480m_is_enabled,
136 .recalc_rate = rockchip_usb_phy480m_recalc_rate,
139 static int rockchip_usb_phy_power_off(struct phy *_phy)
141 struct rockchip_usb_phy *phy = phy_get_drvdata(_phy);
143 if (phy->uart_enabled)
146 clk_disable_unprepare(phy->clk480m);
151 static int rockchip_usb_phy_power_on(struct phy *_phy)
153 struct rockchip_usb_phy *phy = phy_get_drvdata(_phy);
155 if (phy->uart_enabled)
161 ret = regulator_enable(phy->vbus);
166 return clk_prepare_enable(phy->clk480m);
169 static int rockchip_usb_phy_reset(struct phy *_phy)
171 struct rockchip_usb_phy *phy = phy_get_drvdata(_phy);
174 reset_control_assert(phy->reset);
176 reset_control_deassert(phy->reset);
182 static const struct phy_ops ops = {
183 .power_on = rockchip_usb_phy_power_on,
184 .power_off = rockchip_usb_phy_power_off,
185 .reset = rockchip_usb_phy_reset,
186 .owner = THIS_MODULE,
189 static void rockchip_usb_phy_action(void *data)
191 struct rockchip_usb_phy *rk_phy = data;
193 if (!rk_phy->uart_enabled) {
194 of_clk_del_provider(rk_phy->np);
195 clk_unregister(rk_phy->clk480m);
199 clk_put(rk_phy->clk);
202 static int rockchip_usb_phy_init(struct rockchip_usb_phy_base *base,
203 struct device_node *child)
205 struct rockchip_usb_phy *rk_phy;
206 unsigned int reg_offset;
207 const char *clk_name;
208 struct clk_init_data init;
211 rk_phy = devm_kzalloc(base->dev, sizeof(*rk_phy), GFP_KERNEL);
218 if (of_property_read_u32(child, "reg", ®_offset)) {
219 dev_err(base->dev, "missing reg property in node %pOFn\n",
224 rk_phy->reset = of_reset_control_get(child, "phy-reset");
225 if (IS_ERR(rk_phy->reset))
226 rk_phy->reset = NULL;
228 rk_phy->reg_offset = reg_offset;
230 rk_phy->clk = of_clk_get_by_name(child, "phyclk");
231 if (IS_ERR(rk_phy->clk))
236 while (base->pdata->phys[i].reg) {
237 if (base->pdata->phys[i].reg == reg_offset) {
238 init.name = base->pdata->phys[i].pll_name;
245 dev_err(base->dev, "phy data not found\n");
249 if (enable_usb_uart && base->pdata->usb_uart_phy == i) {
250 dev_dbg(base->dev, "phy%d used as uart output\n", i);
251 rk_phy->uart_enabled = true;
254 clk_name = __clk_get_name(rk_phy->clk);
256 init.parent_names = &clk_name;
257 init.num_parents = 1;
260 init.parent_names = NULL;
261 init.num_parents = 0;
264 init.ops = &rockchip_usb_phy480m_ops;
265 rk_phy->clk480m_hw.init = &init;
267 rk_phy->clk480m = clk_register(base->dev, &rk_phy->clk480m_hw);
268 if (IS_ERR(rk_phy->clk480m)) {
269 err = PTR_ERR(rk_phy->clk480m);
273 err = of_clk_add_provider(child, of_clk_src_simple_get,
279 err = devm_add_action_or_reset(base->dev, rockchip_usb_phy_action,
284 rk_phy->phy = devm_phy_create(base->dev, child, &ops);
285 if (IS_ERR(rk_phy->phy)) {
286 dev_err(base->dev, "failed to create PHY\n");
287 return PTR_ERR(rk_phy->phy);
289 phy_set_drvdata(rk_phy->phy, rk_phy);
291 rk_phy->vbus = devm_regulator_get_optional(&rk_phy->phy->dev, "vbus");
292 if (IS_ERR(rk_phy->vbus)) {
293 if (PTR_ERR(rk_phy->vbus) == -EPROBE_DEFER)
294 return PTR_ERR(rk_phy->vbus);
299 * When acting as uart-pipe, just keep clock on otherwise
300 * only power up usb phy when it use, so disable it when init
302 if (rk_phy->uart_enabled)
303 return clk_prepare_enable(rk_phy->clk);
305 return rockchip_usb_phy_power(rk_phy, 1);
308 if (!rk_phy->uart_enabled)
309 clk_unregister(rk_phy->clk480m);
312 clk_put(rk_phy->clk);
316 static const struct rockchip_usb_phy_pdata rk3066a_pdata = {
317 .phys = (struct rockchip_usb_phys[]){
318 { .reg = 0x17c, .pll_name = "sclk_otgphy0_480m" },
319 { .reg = 0x188, .pll_name = "sclk_otgphy1_480m" },
324 static int __init rockchip_init_usb_uart_common(struct regmap *grf,
325 const struct rockchip_usb_phy_pdata *pdata)
327 int regoffs = pdata->phys[pdata->usb_uart_phy].reg;
332 * COMMON_ON and DISABLE settings are described in the TRM,
333 * but were not present in the original code.
334 * Also disable the analog phy components to save power.
336 val = HIWORD_UPDATE(UOC_CON0_COMMON_ON_N
342 ret = regmap_write(grf, regoffs + UOC_CON0, val);
346 val = HIWORD_UPDATE(UOC_CON2_SOFT_CON_SEL,
347 UOC_CON2_SOFT_CON_SEL);
348 ret = regmap_write(grf, regoffs + UOC_CON2, val);
352 val = HIWORD_UPDATE(UOC_CON3_UTMI_OPMODE_NODRIVING
353 | UOC_CON3_UTMI_XCVRSEELCT_FSTRANSC
354 | UOC_CON3_UTMI_TERMSEL_FULLSPEED,
355 UOC_CON3_UTMI_SUSPENDN
356 | UOC_CON3_UTMI_OPMODE_MASK
357 | UOC_CON3_UTMI_XCVRSEELCT_MASK
358 | UOC_CON3_UTMI_TERMSEL_FULLSPEED);
359 ret = regmap_write(grf, UOC_CON3, val);
366 #define RK3188_UOC0_CON0 0x10c
367 #define RK3188_UOC0_CON0_BYPASSSEL BIT(9)
368 #define RK3188_UOC0_CON0_BYPASSDMEN BIT(8)
371 * Enable the bypass of uart2 data through the otg usb phy.
372 * See description of rk3288-variant for details.
374 static int __init rk3188_init_usb_uart(struct regmap *grf,
375 const struct rockchip_usb_phy_pdata *pdata)
380 ret = rockchip_init_usb_uart_common(grf, pdata);
384 val = HIWORD_UPDATE(RK3188_UOC0_CON0_BYPASSSEL
385 | RK3188_UOC0_CON0_BYPASSDMEN,
386 RK3188_UOC0_CON0_BYPASSSEL
387 | RK3188_UOC0_CON0_BYPASSDMEN);
388 ret = regmap_write(grf, RK3188_UOC0_CON0, val);
395 static const struct rockchip_usb_phy_pdata rk3188_pdata = {
396 .phys = (struct rockchip_usb_phys[]){
397 { .reg = 0x10c, .pll_name = "sclk_otgphy0_480m" },
398 { .reg = 0x11c, .pll_name = "sclk_otgphy1_480m" },
401 .init_usb_uart = rk3188_init_usb_uart,
405 #define RK3288_UOC0_CON3 0x32c
406 #define RK3288_UOC0_CON3_BYPASSDMEN BIT(6)
407 #define RK3288_UOC0_CON3_BYPASSSEL BIT(7)
410 * Enable the bypass of uart2 data through the otg usb phy.
411 * Original description in the TRM.
412 * 1. Disable the OTG block by setting OTGDISABLE0 to 1’b1.
413 * 2. Disable the pull-up resistance on the D+ line by setting
414 * OPMODE0[1:0] to 2’b01.
415 * 3. To ensure that the XO, Bias, and PLL blocks are powered down in Suspend
416 * mode, set COMMONONN to 1’b1.
417 * 4. Place the USB PHY in Suspend mode by setting SUSPENDM0 to 1’b0.
418 * 5. Set BYPASSSEL0 to 1’b1.
419 * 6. To transmit data, controls BYPASSDMEN0, and BYPASSDMDATA0.
420 * To receive data, monitor FSVPLUS0.
422 * The actual code in the vendor kernel does some things differently.
424 static int __init rk3288_init_usb_uart(struct regmap *grf,
425 const struct rockchip_usb_phy_pdata *pdata)
430 ret = rockchip_init_usb_uart_common(grf, pdata);
434 val = HIWORD_UPDATE(RK3288_UOC0_CON3_BYPASSSEL
435 | RK3288_UOC0_CON3_BYPASSDMEN,
436 RK3288_UOC0_CON3_BYPASSSEL
437 | RK3288_UOC0_CON3_BYPASSDMEN);
438 ret = regmap_write(grf, RK3288_UOC0_CON3, val);
445 static const struct rockchip_usb_phy_pdata rk3288_pdata = {
446 .phys = (struct rockchip_usb_phys[]){
447 { .reg = 0x320, .pll_name = "sclk_otgphy0_480m" },
448 { .reg = 0x334, .pll_name = "sclk_otgphy1_480m" },
449 { .reg = 0x348, .pll_name = "sclk_otgphy2_480m" },
452 .init_usb_uart = rk3288_init_usb_uart,
456 static int rockchip_usb_phy_probe(struct platform_device *pdev)
458 struct device *dev = &pdev->dev;
459 struct rockchip_usb_phy_base *phy_base;
460 struct phy_provider *phy_provider;
461 const struct of_device_id *match;
462 struct device_node *child;
465 phy_base = devm_kzalloc(dev, sizeof(*phy_base), GFP_KERNEL);
469 match = of_match_device(dev->driver->of_match_table, dev);
470 if (!match || !match->data) {
471 dev_err(dev, "missing phy data\n");
475 phy_base->pdata = match->data;
478 phy_base->reg_base = ERR_PTR(-ENODEV);
479 if (dev->parent && dev->parent->of_node)
480 phy_base->reg_base = syscon_node_to_regmap(
481 dev->parent->of_node);
482 if (IS_ERR(phy_base->reg_base))
483 phy_base->reg_base = syscon_regmap_lookup_by_phandle(
484 dev->of_node, "rockchip,grf");
485 if (IS_ERR(phy_base->reg_base)) {
486 dev_err(&pdev->dev, "Missing rockchip,grf property\n");
487 return PTR_ERR(phy_base->reg_base);
490 for_each_available_child_of_node(dev->of_node, child) {
491 err = rockchip_usb_phy_init(phy_base, child);
498 phy_provider = devm_of_phy_provider_register(dev, of_phy_simple_xlate);
499 return PTR_ERR_OR_ZERO(phy_provider);
502 static const struct of_device_id rockchip_usb_phy_dt_ids[] = {
503 { .compatible = "rockchip,rk3066a-usb-phy", .data = &rk3066a_pdata },
504 { .compatible = "rockchip,rk3188-usb-phy", .data = &rk3188_pdata },
505 { .compatible = "rockchip,rk3288-usb-phy", .data = &rk3288_pdata },
509 MODULE_DEVICE_TABLE(of, rockchip_usb_phy_dt_ids);
511 static struct platform_driver rockchip_usb_driver = {
512 .probe = rockchip_usb_phy_probe,
514 .name = "rockchip-usb-phy",
515 .of_match_table = rockchip_usb_phy_dt_ids,
519 module_platform_driver(rockchip_usb_driver);
522 static int __init rockchip_init_usb_uart(void)
524 const struct of_device_id *match;
525 const struct rockchip_usb_phy_pdata *data;
526 struct device_node *np;
530 if (!enable_usb_uart)
533 np = of_find_matching_node_and_match(NULL, rockchip_usb_phy_dt_ids,
536 pr_err("%s: failed to find usbphy node\n", __func__);
540 pr_debug("%s: using settings for %s\n", __func__, match->compatible);
543 if (!data->init_usb_uart) {
544 pr_err("%s: usb-uart not available on %s\n",
545 __func__, match->compatible);
549 grf = ERR_PTR(-ENODEV);
551 grf = syscon_node_to_regmap(np->parent);
553 grf = syscon_regmap_lookup_by_phandle(np, "rockchip,grf");
555 pr_err("%s: Missing rockchip,grf property, %lu\n",
556 __func__, PTR_ERR(grf));
560 ret = data->init_usb_uart(grf, data);
562 pr_err("%s: could not init usb_uart, %d\n", __func__, ret);
569 early_initcall(rockchip_init_usb_uart);
571 static int __init rockchip_usb_uart(char *buf)
573 enable_usb_uart = true;
576 early_param("rockchip.usb_uart", rockchip_usb_uart);
580 MODULE_DESCRIPTION("Rockchip USB 2.0 PHY driver");
581 MODULE_LICENSE("GPL v2");