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