xref: /openbmc/linux/drivers/usb/host/ehci-exynos.c (revision a2cab953)
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Samsung Exynos USB HOST EHCI Controller
4  *
5  * Copyright (C) 2011 Samsung Electronics Co.Ltd
6  * Author: Jingoo Han <jg1.han@samsung.com>
7  * Author: Joonyoung Shim <jy0922.shim@samsung.com>
8  */
9 
10 #include <linux/clk.h>
11 #include <linux/dma-mapping.h>
12 #include <linux/io.h>
13 #include <linux/kernel.h>
14 #include <linux/module.h>
15 #include <linux/of.h>
16 #include <linux/gpio/consumer.h>
17 #include <linux/phy/phy.h>
18 #include <linux/platform_device.h>
19 #include <linux/usb.h>
20 #include <linux/usb/hcd.h>
21 
22 #include "ehci.h"
23 
24 #define DRIVER_DESC "EHCI Exynos driver"
25 
26 #define EHCI_INSNREG00(base)			(base + 0x90)
27 #define EHCI_INSNREG00_ENA_INCR16		(0x1 << 25)
28 #define EHCI_INSNREG00_ENA_INCR8		(0x1 << 24)
29 #define EHCI_INSNREG00_ENA_INCR4		(0x1 << 23)
30 #define EHCI_INSNREG00_ENA_INCRX_ALIGN		(0x1 << 22)
31 #define EHCI_INSNREG00_ENABLE_DMA_BURST	\
32 	(EHCI_INSNREG00_ENA_INCR16 | EHCI_INSNREG00_ENA_INCR8 |	\
33 	 EHCI_INSNREG00_ENA_INCR4 | EHCI_INSNREG00_ENA_INCRX_ALIGN)
34 
35 static struct hc_driver __read_mostly exynos_ehci_hc_driver;
36 
37 #define PHY_NUMBER 3
38 
39 struct exynos_ehci_hcd {
40 	struct clk *clk;
41 	struct device_node *of_node;
42 	struct phy *phy[PHY_NUMBER];
43 	bool legacy_phy;
44 };
45 
46 #define to_exynos_ehci(hcd) (struct exynos_ehci_hcd *)(hcd_to_ehci(hcd)->priv)
47 
48 static int exynos_ehci_get_phy(struct device *dev,
49 				struct exynos_ehci_hcd *exynos_ehci)
50 {
51 	struct device_node *child;
52 	struct phy *phy;
53 	int phy_number, num_phys;
54 	int ret;
55 
56 	/* Get PHYs for the controller */
57 	num_phys = of_count_phandle_with_args(dev->of_node, "phys",
58 					      "#phy-cells");
59 	for (phy_number = 0; phy_number < num_phys; phy_number++) {
60 		phy = devm_of_phy_get_by_index(dev, dev->of_node, phy_number);
61 		if (IS_ERR(phy))
62 			return PTR_ERR(phy);
63 		exynos_ehci->phy[phy_number] = phy;
64 	}
65 	if (num_phys > 0)
66 		return 0;
67 
68 	/* Get PHYs using legacy bindings */
69 	for_each_available_child_of_node(dev->of_node, child) {
70 		ret = of_property_read_u32(child, "reg", &phy_number);
71 		if (ret) {
72 			dev_err(dev, "Failed to parse device tree\n");
73 			of_node_put(child);
74 			return ret;
75 		}
76 
77 		if (phy_number >= PHY_NUMBER) {
78 			dev_err(dev, "Invalid number of PHYs\n");
79 			of_node_put(child);
80 			return -EINVAL;
81 		}
82 
83 		phy = devm_of_phy_get(dev, child, NULL);
84 		exynos_ehci->phy[phy_number] = phy;
85 		if (IS_ERR(phy)) {
86 			ret = PTR_ERR(phy);
87 			if (ret == -EPROBE_DEFER) {
88 				of_node_put(child);
89 				return ret;
90 			} else if (ret != -ENOSYS && ret != -ENODEV) {
91 				dev_err(dev,
92 					"Error retrieving usb2 phy: %d\n", ret);
93 				of_node_put(child);
94 				return ret;
95 			}
96 		}
97 	}
98 
99 	exynos_ehci->legacy_phy = true;
100 	return 0;
101 }
102 
103 static int exynos_ehci_phy_enable(struct device *dev)
104 {
105 	struct usb_hcd *hcd = dev_get_drvdata(dev);
106 	struct exynos_ehci_hcd *exynos_ehci = to_exynos_ehci(hcd);
107 	int i;
108 	int ret = 0;
109 
110 	for (i = 0; ret == 0 && i < PHY_NUMBER; i++)
111 		if (!IS_ERR(exynos_ehci->phy[i]))
112 			ret = phy_power_on(exynos_ehci->phy[i]);
113 	if (ret)
114 		for (i--; i >= 0; i--)
115 			if (!IS_ERR(exynos_ehci->phy[i]))
116 				phy_power_off(exynos_ehci->phy[i]);
117 
118 	return ret;
119 }
120 
121 static void exynos_ehci_phy_disable(struct device *dev)
122 {
123 	struct usb_hcd *hcd = dev_get_drvdata(dev);
124 	struct exynos_ehci_hcd *exynos_ehci = to_exynos_ehci(hcd);
125 	int i;
126 
127 	for (i = 0; i < PHY_NUMBER; i++)
128 		if (!IS_ERR(exynos_ehci->phy[i]))
129 			phy_power_off(exynos_ehci->phy[i]);
130 }
131 
132 static void exynos_setup_vbus_gpio(struct device *dev)
133 {
134 	struct gpio_desc *gpio;
135 	int err;
136 
137 	gpio = devm_gpiod_get_optional(dev, "samsung,vbus", GPIOD_OUT_HIGH);
138 	err = PTR_ERR_OR_ZERO(gpio);
139 	if (err)
140 		dev_err(dev, "can't request ehci vbus gpio: %d\n", err);
141 }
142 
143 static int exynos_ehci_probe(struct platform_device *pdev)
144 {
145 	struct exynos_ehci_hcd *exynos_ehci;
146 	struct usb_hcd *hcd;
147 	struct ehci_hcd *ehci;
148 	struct resource *res;
149 	int irq;
150 	int err;
151 
152 	/*
153 	 * Right now device-tree probed devices don't get dma_mask set.
154 	 * Since shared usb code relies on it, set it here for now.
155 	 * Once we move to full device tree support this will vanish off.
156 	 */
157 	err = dma_coerce_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
158 	if (err)
159 		return err;
160 
161 	exynos_setup_vbus_gpio(&pdev->dev);
162 
163 	hcd = usb_create_hcd(&exynos_ehci_hc_driver,
164 			     &pdev->dev, dev_name(&pdev->dev));
165 	if (!hcd) {
166 		dev_err(&pdev->dev, "Unable to create HCD\n");
167 		return -ENOMEM;
168 	}
169 	exynos_ehci = to_exynos_ehci(hcd);
170 
171 	err = exynos_ehci_get_phy(&pdev->dev, exynos_ehci);
172 	if (err)
173 		goto fail_clk;
174 
175 	exynos_ehci->clk = devm_clk_get(&pdev->dev, "usbhost");
176 
177 	if (IS_ERR(exynos_ehci->clk)) {
178 		dev_err(&pdev->dev, "Failed to get usbhost clock\n");
179 		err = PTR_ERR(exynos_ehci->clk);
180 		goto fail_clk;
181 	}
182 
183 	err = clk_prepare_enable(exynos_ehci->clk);
184 	if (err)
185 		goto fail_clk;
186 
187 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
188 	hcd->regs = devm_ioremap_resource(&pdev->dev, res);
189 	if (IS_ERR(hcd->regs)) {
190 		err = PTR_ERR(hcd->regs);
191 		goto fail_io;
192 	}
193 
194 	hcd->rsrc_start = res->start;
195 	hcd->rsrc_len = resource_size(res);
196 
197 	irq = platform_get_irq(pdev, 0);
198 	if (irq < 0) {
199 		err = irq;
200 		goto fail_io;
201 	}
202 
203 	err = exynos_ehci_phy_enable(&pdev->dev);
204 	if (err) {
205 		dev_err(&pdev->dev, "Failed to enable USB phy\n");
206 		goto fail_io;
207 	}
208 
209 	ehci = hcd_to_ehci(hcd);
210 	ehci->caps = hcd->regs;
211 
212 	/*
213 	 * Workaround: reset of_node pointer to avoid conflict between legacy
214 	 * Exynos EHCI port subnodes and generic USB device bindings
215 	 */
216 	exynos_ehci->of_node = pdev->dev.of_node;
217 	if (exynos_ehci->legacy_phy)
218 		pdev->dev.of_node = NULL;
219 
220 	/* DMA burst Enable */
221 	writel(EHCI_INSNREG00_ENABLE_DMA_BURST, EHCI_INSNREG00(hcd->regs));
222 
223 	err = usb_add_hcd(hcd, irq, IRQF_SHARED);
224 	if (err) {
225 		dev_err(&pdev->dev, "Failed to add USB HCD\n");
226 		goto fail_add_hcd;
227 	}
228 	device_wakeup_enable(hcd->self.controller);
229 
230 	platform_set_drvdata(pdev, hcd);
231 
232 	return 0;
233 
234 fail_add_hcd:
235 	exynos_ehci_phy_disable(&pdev->dev);
236 	pdev->dev.of_node = exynos_ehci->of_node;
237 fail_io:
238 	clk_disable_unprepare(exynos_ehci->clk);
239 fail_clk:
240 	usb_put_hcd(hcd);
241 	return err;
242 }
243 
244 static int exynos_ehci_remove(struct platform_device *pdev)
245 {
246 	struct usb_hcd *hcd = platform_get_drvdata(pdev);
247 	struct exynos_ehci_hcd *exynos_ehci = to_exynos_ehci(hcd);
248 
249 	pdev->dev.of_node = exynos_ehci->of_node;
250 
251 	usb_remove_hcd(hcd);
252 
253 	exynos_ehci_phy_disable(&pdev->dev);
254 
255 	clk_disable_unprepare(exynos_ehci->clk);
256 
257 	usb_put_hcd(hcd);
258 
259 	return 0;
260 }
261 
262 #ifdef CONFIG_PM
263 static int exynos_ehci_suspend(struct device *dev)
264 {
265 	struct usb_hcd *hcd = dev_get_drvdata(dev);
266 	struct exynos_ehci_hcd *exynos_ehci = to_exynos_ehci(hcd);
267 
268 	bool do_wakeup = device_may_wakeup(dev);
269 	int rc;
270 
271 	rc = ehci_suspend(hcd, do_wakeup);
272 	if (rc)
273 		return rc;
274 
275 	exynos_ehci_phy_disable(dev);
276 
277 	clk_disable_unprepare(exynos_ehci->clk);
278 
279 	return rc;
280 }
281 
282 static int exynos_ehci_resume(struct device *dev)
283 {
284 	struct usb_hcd *hcd = dev_get_drvdata(dev);
285 	struct exynos_ehci_hcd *exynos_ehci = to_exynos_ehci(hcd);
286 	int ret;
287 
288 	ret = clk_prepare_enable(exynos_ehci->clk);
289 	if (ret)
290 		return ret;
291 
292 	ret = exynos_ehci_phy_enable(dev);
293 	if (ret) {
294 		dev_err(dev, "Failed to enable USB phy\n");
295 		clk_disable_unprepare(exynos_ehci->clk);
296 		return ret;
297 	}
298 
299 	/* DMA burst Enable */
300 	writel(EHCI_INSNREG00_ENABLE_DMA_BURST, EHCI_INSNREG00(hcd->regs));
301 
302 	ehci_resume(hcd, false);
303 	return 0;
304 }
305 #else
306 #define exynos_ehci_suspend	NULL
307 #define exynos_ehci_resume	NULL
308 #endif
309 
310 static const struct dev_pm_ops exynos_ehci_pm_ops = {
311 	.suspend	= exynos_ehci_suspend,
312 	.resume		= exynos_ehci_resume,
313 };
314 
315 #ifdef CONFIG_OF
316 static const struct of_device_id exynos_ehci_match[] = {
317 	{ .compatible = "samsung,exynos4210-ehci" },
318 	{},
319 };
320 MODULE_DEVICE_TABLE(of, exynos_ehci_match);
321 #endif
322 
323 static struct platform_driver exynos_ehci_driver = {
324 	.probe		= exynos_ehci_probe,
325 	.remove		= exynos_ehci_remove,
326 	.shutdown	= usb_hcd_platform_shutdown,
327 	.driver = {
328 		.name	= "exynos-ehci",
329 		.pm	= &exynos_ehci_pm_ops,
330 		.of_match_table = of_match_ptr(exynos_ehci_match),
331 	}
332 };
333 static const struct ehci_driver_overrides exynos_overrides __initconst = {
334 	.extra_priv_size = sizeof(struct exynos_ehci_hcd),
335 };
336 
337 static int __init ehci_exynos_init(void)
338 {
339 	if (usb_disabled())
340 		return -ENODEV;
341 
342 	ehci_init_driver(&exynos_ehci_hc_driver, &exynos_overrides);
343 	return platform_driver_register(&exynos_ehci_driver);
344 }
345 module_init(ehci_exynos_init);
346 
347 static void __exit ehci_exynos_cleanup(void)
348 {
349 	platform_driver_unregister(&exynos_ehci_driver);
350 }
351 module_exit(ehci_exynos_cleanup);
352 
353 MODULE_DESCRIPTION(DRIVER_DESC);
354 MODULE_ALIAS("platform:exynos-ehci");
355 MODULE_AUTHOR("Jingoo Han");
356 MODULE_AUTHOR("Joonyoung Shim");
357 MODULE_LICENSE("GPL v2");
358