2 * Renesas R-Car Gen3 for USB3.0 PHY driver
4 * Copyright (C) 2017 Renesas Electronics Corporation
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
11 #include <linux/clk.h>
12 #include <linux/delay.h>
14 #include <linux/module.h>
16 #include <linux/phy/phy.h>
17 #include <linux/platform_device.h>
18 #include <linux/pm_runtime.h>
20 #define USB30_CLKSET0 0x034
21 #define USB30_CLKSET1 0x036
22 #define USB30_SSC_SET 0x038
23 #define USB30_PHY_ENABLE 0x060
24 #define USB30_VBUS_EN 0x064
27 #define CLKSET0_PRIVATE 0x05c0
28 #define CLKSET0_USB30_FSEL_USB_EXTAL 0x0002
31 #define CLKSET1_USB30_PLL_MULTI_SHIFT 6
32 #define CLKSET1_USB30_PLL_MULTI_USB_EXTAL (0x64 << \
33 CLKSET1_USB30_PLL_MULTI_SHIFT)
34 #define CLKSET1_PHYRESET BIT(4) /* 1: reset */
35 #define CLKSET1_REF_CLKDIV BIT(3) /* 1: USB_EXTAL */
36 #define CLKSET1_PRIVATE_2_1 BIT(1) /* Write B'01 */
37 #define CLKSET1_REF_CLK_SEL BIT(0) /* 1: USB3S0_CLK_P */
40 #define SSC_SET_SSC_EN BIT(12)
41 #define SSC_SET_RANGE_SHIFT 9
42 #define SSC_SET_RANGE_4980 (0x0 << SSC_SET_RANGE_SHIFT)
43 #define SSC_SET_RANGE_4492 (0x1 << SSC_SET_RANGE_SHIFT)
44 #define SSC_SET_RANGE_4003 (0x2 << SSC_SET_RANGE_SHIFT)
46 /* USB30_PHY_ENABLE */
47 #define PHY_ENABLE_RESET_EN BIT(4)
50 #define VBUS_EN_VBUS_EN BIT(1)
52 struct rcar_gen3_usb3 {
60 static void write_clkset1_for_usb_extal(struct rcar_gen3_usb3 *r, bool reset)
62 u16 val = CLKSET1_USB30_PLL_MULTI_USB_EXTAL |
63 CLKSET1_REF_CLKDIV | CLKSET1_PRIVATE_2_1;
66 val |= CLKSET1_PHYRESET;
68 writew(val, r->base + USB30_CLKSET1);
71 static void rcar_gen3_phy_usb3_enable_ssc(struct rcar_gen3_usb3 *r)
73 u16 val = SSC_SET_SSC_EN;
75 switch (r->ssc_range) {
77 val |= SSC_SET_RANGE_4980;
80 val |= SSC_SET_RANGE_4492;
83 val |= SSC_SET_RANGE_4003;
86 dev_err(&r->phy->dev, "%s: unsupported range (%x)\n", __func__,
91 writew(val, r->base + USB30_SSC_SET);
94 static void rcar_gen3_phy_usb3_select_usb_extal(struct rcar_gen3_usb3 *r)
96 write_clkset1_for_usb_extal(r, false);
98 rcar_gen3_phy_usb3_enable_ssc(r);
99 writew(CLKSET0_PRIVATE | CLKSET0_USB30_FSEL_USB_EXTAL,
100 r->base + USB30_CLKSET0);
101 writew(PHY_ENABLE_RESET_EN, r->base + USB30_PHY_ENABLE);
102 write_clkset1_for_usb_extal(r, true);
103 usleep_range(10, 20);
104 write_clkset1_for_usb_extal(r, false);
107 static int rcar_gen3_phy_usb3_init(struct phy *p)
109 struct rcar_gen3_usb3 *r = phy_get_drvdata(p);
111 dev_vdbg(&r->phy->dev, "%s: enter (%d, %d, %d)\n", __func__,
112 r->usb3s_clk, r->usb_extal, r->ssc_range);
114 if (!r->usb3s_clk && r->usb_extal)
115 rcar_gen3_phy_usb3_select_usb_extal(r);
117 /* Enables VBUS detection anyway */
118 writew(VBUS_EN_VBUS_EN, r->base + USB30_VBUS_EN);
123 static const struct phy_ops rcar_gen3_phy_usb3_ops = {
124 .init = rcar_gen3_phy_usb3_init,
125 .owner = THIS_MODULE,
128 static const struct of_device_id rcar_gen3_phy_usb3_match_table[] = {
129 { .compatible = "renesas,rcar-gen3-usb3-phy" },
132 MODULE_DEVICE_TABLE(of, rcar_gen3_phy_usb3_match_table);
134 static int rcar_gen3_phy_usb3_probe(struct platform_device *pdev)
136 struct device *dev = &pdev->dev;
137 struct rcar_gen3_usb3 *r;
138 struct phy_provider *provider;
139 struct resource *res;
144 dev_err(dev, "This driver needs device tree\n");
148 r = devm_kzalloc(dev, sizeof(*r), GFP_KERNEL);
152 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
153 r->base = devm_ioremap_resource(dev, res);
155 return PTR_ERR(r->base);
157 clk = devm_clk_get(dev, "usb3s_clk");
158 if (!IS_ERR(clk) && !clk_prepare_enable(clk)) {
159 r->usb3s_clk = !!clk_get_rate(clk);
160 clk_disable_unprepare(clk);
162 clk = devm_clk_get(dev, "usb_extal");
163 if (!IS_ERR(clk) && !clk_prepare_enable(clk)) {
164 r->usb_extal = !!clk_get_rate(clk);
165 clk_disable_unprepare(clk);
168 if (!r->usb3s_clk && !r->usb_extal) {
169 dev_err(dev, "This driver needs usb3s_clk and/or usb_extal\n");
175 * devm_phy_create() will call pm_runtime_enable(&phy->dev);
176 * And then, phy-core will manage runtime pm for this device.
178 pm_runtime_enable(dev);
180 r->phy = devm_phy_create(dev, NULL, &rcar_gen3_phy_usb3_ops);
181 if (IS_ERR(r->phy)) {
182 dev_err(dev, "Failed to create USB3 PHY\n");
183 ret = PTR_ERR(r->phy);
187 of_property_read_u32(dev->of_node, "renesas,ssc-range", &r->ssc_range);
189 platform_set_drvdata(pdev, r);
190 phy_set_drvdata(r->phy, r);
192 provider = devm_of_phy_provider_register(dev, of_phy_simple_xlate);
193 if (IS_ERR(provider)) {
194 dev_err(dev, "Failed to register PHY provider\n");
195 ret = PTR_ERR(provider);
202 pm_runtime_disable(dev);
207 static int rcar_gen3_phy_usb3_remove(struct platform_device *pdev)
209 pm_runtime_disable(&pdev->dev);
214 static struct platform_driver rcar_gen3_phy_usb3_driver = {
216 .name = "phy_rcar_gen3_usb3",
217 .of_match_table = rcar_gen3_phy_usb3_match_table,
219 .probe = rcar_gen3_phy_usb3_probe,
220 .remove = rcar_gen3_phy_usb3_remove,
222 module_platform_driver(rcar_gen3_phy_usb3_driver);
224 MODULE_LICENSE("GPL v2");
225 MODULE_DESCRIPTION("Renesas R-Car Gen3 USB 3.0 PHY");
226 MODULE_AUTHOR("Yoshihiro Shimoda <yoshihiro.shimoda.uh@renesas.com>");