1 // SPDX-License-Identifier: GPL-2.0
3 * Renesas R-Car Gen3 for USB3.0 PHY driver
5 * Copyright (C) 2017 Renesas Electronics Corporation
9 #include <linux/delay.h>
11 #include <linux/module.h>
13 #include <linux/phy/phy.h>
14 #include <linux/platform_device.h>
15 #include <linux/pm_runtime.h>
17 #define USB30_CLKSET0 0x034
18 #define USB30_CLKSET1 0x036
19 #define USB30_SSC_SET 0x038
20 #define USB30_PHY_ENABLE 0x060
21 #define USB30_VBUS_EN 0x064
24 #define CLKSET0_PRIVATE 0x05c0
25 #define CLKSET0_USB30_FSEL_USB_EXTAL 0x0002
28 #define CLKSET1_USB30_PLL_MULTI_SHIFT 6
29 #define CLKSET1_USB30_PLL_MULTI_USB_EXTAL (0x64 << \
30 CLKSET1_USB30_PLL_MULTI_SHIFT)
31 #define CLKSET1_PHYRESET BIT(4) /* 1: reset */
32 #define CLKSET1_REF_CLKDIV BIT(3) /* 1: USB_EXTAL */
33 #define CLKSET1_PRIVATE_2_1 BIT(1) /* Write B'01 */
34 #define CLKSET1_REF_CLK_SEL BIT(0) /* 1: USB3S0_CLK_P */
37 #define SSC_SET_SSC_EN BIT(12)
38 #define SSC_SET_RANGE_SHIFT 9
39 #define SSC_SET_RANGE_4980 (0x0 << SSC_SET_RANGE_SHIFT)
40 #define SSC_SET_RANGE_4492 (0x1 << SSC_SET_RANGE_SHIFT)
41 #define SSC_SET_RANGE_4003 (0x2 << SSC_SET_RANGE_SHIFT)
43 /* USB30_PHY_ENABLE */
44 #define PHY_ENABLE_RESET_EN BIT(4)
47 #define VBUS_EN_VBUS_EN BIT(1)
49 struct rcar_gen3_usb3 {
57 static void write_clkset1_for_usb_extal(struct rcar_gen3_usb3 *r, bool reset)
59 u16 val = CLKSET1_USB30_PLL_MULTI_USB_EXTAL |
60 CLKSET1_REF_CLKDIV | CLKSET1_PRIVATE_2_1;
63 val |= CLKSET1_PHYRESET;
65 writew(val, r->base + USB30_CLKSET1);
68 static void rcar_gen3_phy_usb3_enable_ssc(struct rcar_gen3_usb3 *r)
70 u16 val = SSC_SET_SSC_EN;
72 switch (r->ssc_range) {
74 val |= SSC_SET_RANGE_4980;
77 val |= SSC_SET_RANGE_4492;
80 val |= SSC_SET_RANGE_4003;
83 dev_err(&r->phy->dev, "%s: unsupported range (%x)\n", __func__,
88 writew(val, r->base + USB30_SSC_SET);
91 static void rcar_gen3_phy_usb3_select_usb_extal(struct rcar_gen3_usb3 *r)
93 write_clkset1_for_usb_extal(r, false);
95 rcar_gen3_phy_usb3_enable_ssc(r);
96 writew(CLKSET0_PRIVATE | CLKSET0_USB30_FSEL_USB_EXTAL,
97 r->base + USB30_CLKSET0);
98 writew(PHY_ENABLE_RESET_EN, r->base + USB30_PHY_ENABLE);
99 write_clkset1_for_usb_extal(r, true);
100 usleep_range(10, 20);
101 write_clkset1_for_usb_extal(r, false);
104 static int rcar_gen3_phy_usb3_init(struct phy *p)
106 struct rcar_gen3_usb3 *r = phy_get_drvdata(p);
108 dev_vdbg(&r->phy->dev, "%s: enter (%d, %d, %d)\n", __func__,
109 r->usb3s_clk, r->usb_extal, r->ssc_range);
111 if (!r->usb3s_clk && r->usb_extal)
112 rcar_gen3_phy_usb3_select_usb_extal(r);
114 /* Enables VBUS detection anyway */
115 writew(VBUS_EN_VBUS_EN, r->base + USB30_VBUS_EN);
120 static const struct phy_ops rcar_gen3_phy_usb3_ops = {
121 .init = rcar_gen3_phy_usb3_init,
122 .owner = THIS_MODULE,
125 static const struct of_device_id rcar_gen3_phy_usb3_match_table[] = {
126 { .compatible = "renesas,rcar-gen3-usb3-phy" },
129 MODULE_DEVICE_TABLE(of, rcar_gen3_phy_usb3_match_table);
131 static int rcar_gen3_phy_usb3_probe(struct platform_device *pdev)
133 struct device *dev = &pdev->dev;
134 struct rcar_gen3_usb3 *r;
135 struct phy_provider *provider;
140 dev_err(dev, "This driver needs device tree\n");
144 r = devm_kzalloc(dev, sizeof(*r), GFP_KERNEL);
148 r->base = devm_platform_ioremap_resource(pdev, 0);
150 return PTR_ERR(r->base);
152 clk = devm_clk_get(dev, "usb3s_clk");
153 if (!IS_ERR(clk) && !clk_prepare_enable(clk)) {
154 r->usb3s_clk = !!clk_get_rate(clk);
155 clk_disable_unprepare(clk);
157 clk = devm_clk_get(dev, "usb_extal");
158 if (!IS_ERR(clk) && !clk_prepare_enable(clk)) {
159 r->usb_extal = !!clk_get_rate(clk);
160 clk_disable_unprepare(clk);
163 if (!r->usb3s_clk && !r->usb_extal) {
164 dev_err(dev, "This driver needs usb3s_clk and/or usb_extal\n");
170 * devm_phy_create() will call pm_runtime_enable(&phy->dev);
171 * And then, phy-core will manage runtime pm for this device.
173 pm_runtime_enable(dev);
175 r->phy = devm_phy_create(dev, NULL, &rcar_gen3_phy_usb3_ops);
176 if (IS_ERR(r->phy)) {
177 dev_err(dev, "Failed to create USB3 PHY\n");
178 ret = PTR_ERR(r->phy);
182 of_property_read_u32(dev->of_node, "renesas,ssc-range", &r->ssc_range);
184 platform_set_drvdata(pdev, r);
185 phy_set_drvdata(r->phy, r);
187 provider = devm_of_phy_provider_register(dev, of_phy_simple_xlate);
188 if (IS_ERR(provider)) {
189 dev_err(dev, "Failed to register PHY provider\n");
190 ret = PTR_ERR(provider);
197 pm_runtime_disable(dev);
202 static int rcar_gen3_phy_usb3_remove(struct platform_device *pdev)
204 pm_runtime_disable(&pdev->dev);
209 static struct platform_driver rcar_gen3_phy_usb3_driver = {
211 .name = "phy_rcar_gen3_usb3",
212 .of_match_table = rcar_gen3_phy_usb3_match_table,
214 .probe = rcar_gen3_phy_usb3_probe,
215 .remove = rcar_gen3_phy_usb3_remove,
217 module_platform_driver(rcar_gen3_phy_usb3_driver);
219 MODULE_LICENSE("GPL v2");
220 MODULE_DESCRIPTION("Renesas R-Car Gen3 USB 3.0 PHY");
221 MODULE_AUTHOR("Yoshihiro Shimoda <yoshihiro.shimoda.uh@renesas.com>");