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