1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Renesas R-Car Gen3 for USB3.0 PHY driver
4  *
5  * Copyright (C) 2017 Renesas Electronics Corporation
6  */
7 
8 #include <linux/clk.h>
9 #include <linux/delay.h>
10 #include <linux/io.h>
11 #include <linux/module.h>
12 #include <linux/of.h>
13 #include <linux/phy/phy.h>
14 #include <linux/platform_device.h>
15 #include <linux/pm_runtime.h>
16 
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
22 
23 /* USB30_CLKSET0 */
24 #define CLKSET0_PRIVATE			0x05c0
25 #define CLKSET0_USB30_FSEL_USB_EXTAL	0x0002
26 
27 /* USB30_CLKSET1 */
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 */
35 
36 /* USB30_SSC_SET */
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)
42 
43 /* USB30_PHY_ENABLE */
44 #define PHY_ENABLE_RESET_EN	BIT(4)
45 
46 /* USB30_VBUS_EN */
47 #define VBUS_EN_VBUS_EN		BIT(1)
48 
49 struct rcar_gen3_usb3 {
50 	void __iomem *base;
51 	struct phy *phy;
52 	u32 ssc_range;
53 	bool usb3s_clk;
54 	bool usb_extal;
55 };
56 
57 static void write_clkset1_for_usb_extal(struct rcar_gen3_usb3 *r, bool reset)
58 {
59 	u16 val = CLKSET1_USB30_PLL_MULTI_USB_EXTAL |
60 		  CLKSET1_REF_CLKDIV | CLKSET1_PRIVATE_2_1;
61 
62 	if (reset)
63 		val |= CLKSET1_PHYRESET;
64 
65 	writew(val, r->base + USB30_CLKSET1);
66 }
67 
68 static void rcar_gen3_phy_usb3_enable_ssc(struct rcar_gen3_usb3 *r)
69 {
70 	u16 val = SSC_SET_SSC_EN;
71 
72 	switch (r->ssc_range) {
73 	case 4980:
74 		val |= SSC_SET_RANGE_4980;
75 		break;
76 	case 4492:
77 		val |= SSC_SET_RANGE_4492;
78 		break;
79 	case 4003:
80 		val |= SSC_SET_RANGE_4003;
81 		break;
82 	default:
83 		dev_err(&r->phy->dev, "%s: unsupported range (%x)\n", __func__,
84 			r->ssc_range);
85 		return;
86 	}
87 
88 	writew(val, r->base + USB30_SSC_SET);
89 }
90 
91 static void rcar_gen3_phy_usb3_select_usb_extal(struct rcar_gen3_usb3 *r)
92 {
93 	write_clkset1_for_usb_extal(r, false);
94 	if (r->ssc_range)
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);
102 }
103 
104 static int rcar_gen3_phy_usb3_init(struct phy *p)
105 {
106 	struct rcar_gen3_usb3 *r = phy_get_drvdata(p);
107 
108 	dev_vdbg(&r->phy->dev, "%s: enter (%d, %d, %d)\n", __func__,
109 		 r->usb3s_clk, r->usb_extal, r->ssc_range);
110 
111 	if (!r->usb3s_clk && r->usb_extal)
112 		rcar_gen3_phy_usb3_select_usb_extal(r);
113 
114 	/* Enables VBUS detection anyway */
115 	writew(VBUS_EN_VBUS_EN, r->base + USB30_VBUS_EN);
116 
117 	return 0;
118 }
119 
120 static const struct phy_ops rcar_gen3_phy_usb3_ops = {
121 	.init		= rcar_gen3_phy_usb3_init,
122 	.owner		= THIS_MODULE,
123 };
124 
125 static const struct of_device_id rcar_gen3_phy_usb3_match_table[] = {
126 	{ .compatible = "renesas,rcar-gen3-usb3-phy" },
127 	{ }
128 };
129 MODULE_DEVICE_TABLE(of, rcar_gen3_phy_usb3_match_table);
130 
131 static int rcar_gen3_phy_usb3_probe(struct platform_device *pdev)
132 {
133 	struct device *dev = &pdev->dev;
134 	struct rcar_gen3_usb3 *r;
135 	struct phy_provider *provider;
136 	struct resource *res;
137 	int ret = 0;
138 	struct clk *clk;
139 
140 	if (!dev->of_node) {
141 		dev_err(dev, "This driver needs device tree\n");
142 		return -EINVAL;
143 	}
144 
145 	r = devm_kzalloc(dev, sizeof(*r), GFP_KERNEL);
146 	if (!r)
147 		return -ENOMEM;
148 
149 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
150 	r->base = devm_ioremap_resource(dev, res);
151 	if (IS_ERR(r->base))
152 		return PTR_ERR(r->base);
153 
154 	clk = devm_clk_get(dev, "usb3s_clk");
155 	if (!IS_ERR(clk) && !clk_prepare_enable(clk)) {
156 		r->usb3s_clk = !!clk_get_rate(clk);
157 		clk_disable_unprepare(clk);
158 	}
159 	clk = devm_clk_get(dev, "usb_extal");
160 	if (!IS_ERR(clk) && !clk_prepare_enable(clk)) {
161 		r->usb_extal = !!clk_get_rate(clk);
162 		clk_disable_unprepare(clk);
163 	}
164 
165 	if (!r->usb3s_clk && !r->usb_extal) {
166 		dev_err(dev, "This driver needs usb3s_clk and/or usb_extal\n");
167 		ret = -EINVAL;
168 		goto error;
169 	}
170 
171 	/*
172 	 * devm_phy_create() will call pm_runtime_enable(&phy->dev);
173 	 * And then, phy-core will manage runtime pm for this device.
174 	 */
175 	pm_runtime_enable(dev);
176 
177 	r->phy = devm_phy_create(dev, NULL, &rcar_gen3_phy_usb3_ops);
178 	if (IS_ERR(r->phy)) {
179 		dev_err(dev, "Failed to create USB3 PHY\n");
180 		ret = PTR_ERR(r->phy);
181 		goto error;
182 	}
183 
184 	of_property_read_u32(dev->of_node, "renesas,ssc-range", &r->ssc_range);
185 
186 	platform_set_drvdata(pdev, r);
187 	phy_set_drvdata(r->phy, r);
188 
189 	provider = devm_of_phy_provider_register(dev, of_phy_simple_xlate);
190 	if (IS_ERR(provider)) {
191 		dev_err(dev, "Failed to register PHY provider\n");
192 		ret = PTR_ERR(provider);
193 		goto error;
194 	}
195 
196 	return 0;
197 
198 error:
199 	pm_runtime_disable(dev);
200 
201 	return ret;
202 }
203 
204 static int rcar_gen3_phy_usb3_remove(struct platform_device *pdev)
205 {
206 	pm_runtime_disable(&pdev->dev);
207 
208 	return 0;
209 };
210 
211 static struct platform_driver rcar_gen3_phy_usb3_driver = {
212 	.driver = {
213 		.name		= "phy_rcar_gen3_usb3",
214 		.of_match_table	= rcar_gen3_phy_usb3_match_table,
215 	},
216 	.probe	= rcar_gen3_phy_usb3_probe,
217 	.remove = rcar_gen3_phy_usb3_remove,
218 };
219 module_platform_driver(rcar_gen3_phy_usb3_driver);
220 
221 MODULE_LICENSE("GPL v2");
222 MODULE_DESCRIPTION("Renesas R-Car Gen3 USB 3.0 PHY");
223 MODULE_AUTHOR("Yoshihiro Shimoda <yoshihiro.shimoda.uh@renesas.com>");
224