xref: /linux/drivers/usb/host/ehci-exynos.c (revision d163d60258c755845cbc9cfe0e45fca71e649488)
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_optional_get(dev, child, NULL);
84 		exynos_ehci->phy[phy_number] = phy;
85 		if (IS_ERR(phy)) {
86 			of_node_put(child);
87 			return PTR_ERR(phy);
88 		}
89 	}
90 
91 	exynos_ehci->legacy_phy = true;
92 	return 0;
93 }
94 
95 static int exynos_ehci_phy_enable(struct device *dev)
96 {
97 	struct usb_hcd *hcd = dev_get_drvdata(dev);
98 	struct exynos_ehci_hcd *exynos_ehci = to_exynos_ehci(hcd);
99 	int i;
100 	int ret = 0;
101 
102 	for (i = 0; ret == 0 && i < PHY_NUMBER; i++)
103 		ret = phy_power_on(exynos_ehci->phy[i]);
104 	if (ret)
105 		for (i--; i >= 0; i--)
106 			phy_power_off(exynos_ehci->phy[i]);
107 
108 	return ret;
109 }
110 
111 static void exynos_ehci_phy_disable(struct device *dev)
112 {
113 	struct usb_hcd *hcd = dev_get_drvdata(dev);
114 	struct exynos_ehci_hcd *exynos_ehci = to_exynos_ehci(hcd);
115 	int i;
116 
117 	for (i = 0; i < PHY_NUMBER; i++)
118 		phy_power_off(exynos_ehci->phy[i]);
119 }
120 
121 static void exynos_setup_vbus_gpio(struct device *dev)
122 {
123 	struct gpio_desc *gpio;
124 	int err;
125 
126 	gpio = devm_gpiod_get_optional(dev, "samsung,vbus", GPIOD_OUT_HIGH);
127 	err = PTR_ERR_OR_ZERO(gpio);
128 	if (err)
129 		dev_err(dev, "can't request ehci vbus gpio: %d\n", err);
130 }
131 
132 static int exynos_ehci_probe(struct platform_device *pdev)
133 {
134 	struct exynos_ehci_hcd *exynos_ehci;
135 	struct usb_hcd *hcd;
136 	struct ehci_hcd *ehci;
137 	struct resource *res;
138 	int irq;
139 	int err;
140 
141 	/*
142 	 * Right now device-tree probed devices don't get dma_mask set.
143 	 * Since shared usb code relies on it, set it here for now.
144 	 * Once we move to full device tree support this will vanish off.
145 	 */
146 	err = dma_coerce_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
147 	if (err)
148 		return err;
149 
150 	exynos_setup_vbus_gpio(&pdev->dev);
151 
152 	hcd = usb_create_hcd(&exynos_ehci_hc_driver,
153 			     &pdev->dev, dev_name(&pdev->dev));
154 	if (!hcd) {
155 		dev_err(&pdev->dev, "Unable to create HCD\n");
156 		return -ENOMEM;
157 	}
158 	exynos_ehci = to_exynos_ehci(hcd);
159 
160 	err = exynos_ehci_get_phy(&pdev->dev, exynos_ehci);
161 	if (err)
162 		goto fail_io;
163 
164 	exynos_ehci->clk = devm_clk_get_enabled(&pdev->dev, "usbhost");
165 
166 	if (IS_ERR(exynos_ehci->clk)) {
167 		dev_err(&pdev->dev, "Failed to get usbhost clock\n");
168 		err = PTR_ERR(exynos_ehci->clk);
169 		goto fail_io;
170 	}
171 
172 	hcd->regs = devm_platform_get_and_ioremap_resource(pdev, 0, &res);
173 	if (IS_ERR(hcd->regs)) {
174 		err = PTR_ERR(hcd->regs);
175 		goto fail_io;
176 	}
177 
178 	hcd->rsrc_start = res->start;
179 	hcd->rsrc_len = resource_size(res);
180 
181 	irq = platform_get_irq(pdev, 0);
182 	if (irq < 0) {
183 		err = irq;
184 		goto fail_io;
185 	}
186 
187 	err = exynos_ehci_phy_enable(&pdev->dev);
188 	if (err) {
189 		dev_err(&pdev->dev, "Failed to enable USB phy\n");
190 		goto fail_io;
191 	}
192 
193 	ehci = hcd_to_ehci(hcd);
194 	ehci->caps = hcd->regs;
195 
196 	/*
197 	 * Workaround: reset of_node pointer to avoid conflict between legacy
198 	 * Exynos EHCI port subnodes and generic USB device bindings
199 	 */
200 	exynos_ehci->of_node = pdev->dev.of_node;
201 	if (exynos_ehci->legacy_phy)
202 		pdev->dev.of_node = NULL;
203 
204 	/* DMA burst Enable */
205 	writel(EHCI_INSNREG00_ENABLE_DMA_BURST, EHCI_INSNREG00(hcd->regs));
206 
207 	err = usb_add_hcd(hcd, irq, IRQF_SHARED);
208 	if (err) {
209 		dev_err(&pdev->dev, "Failed to add USB HCD\n");
210 		goto fail_add_hcd;
211 	}
212 	device_wakeup_enable(hcd->self.controller);
213 
214 	platform_set_drvdata(pdev, hcd);
215 
216 	return 0;
217 
218 fail_add_hcd:
219 	exynos_ehci_phy_disable(&pdev->dev);
220 	pdev->dev.of_node = exynos_ehci->of_node;
221 fail_io:
222 	usb_put_hcd(hcd);
223 	return err;
224 }
225 
226 static void exynos_ehci_remove(struct platform_device *pdev)
227 {
228 	struct usb_hcd *hcd = platform_get_drvdata(pdev);
229 	struct exynos_ehci_hcd *exynos_ehci = to_exynos_ehci(hcd);
230 
231 	pdev->dev.of_node = exynos_ehci->of_node;
232 
233 	usb_remove_hcd(hcd);
234 
235 	exynos_ehci_phy_disable(&pdev->dev);
236 
237 	usb_put_hcd(hcd);
238 }
239 
240 static int exynos_ehci_suspend(struct device *dev)
241 {
242 	struct usb_hcd *hcd = dev_get_drvdata(dev);
243 	struct exynos_ehci_hcd *exynos_ehci = to_exynos_ehci(hcd);
244 
245 	bool do_wakeup = device_may_wakeup(dev);
246 	int rc;
247 
248 	rc = ehci_suspend(hcd, do_wakeup);
249 	if (rc)
250 		return rc;
251 
252 	exynos_ehci_phy_disable(dev);
253 
254 	clk_disable_unprepare(exynos_ehci->clk);
255 
256 	return rc;
257 }
258 
259 static int exynos_ehci_resume(struct device *dev)
260 {
261 	struct usb_hcd *hcd = dev_get_drvdata(dev);
262 	struct exynos_ehci_hcd *exynos_ehci = to_exynos_ehci(hcd);
263 	int ret;
264 
265 	ret = clk_prepare_enable(exynos_ehci->clk);
266 	if (ret)
267 		return ret;
268 
269 	ret = exynos_ehci_phy_enable(dev);
270 	if (ret) {
271 		dev_err(dev, "Failed to enable USB phy\n");
272 		clk_disable_unprepare(exynos_ehci->clk);
273 		return ret;
274 	}
275 
276 	/* DMA burst Enable */
277 	writel(EHCI_INSNREG00_ENABLE_DMA_BURST, EHCI_INSNREG00(hcd->regs));
278 
279 	ehci_resume(hcd, false);
280 	return 0;
281 }
282 
283 static DEFINE_SIMPLE_DEV_PM_OPS(exynos_ehci_pm_ops,
284 				exynos_ehci_suspend, exynos_ehci_resume);
285 
286 #ifdef CONFIG_OF
287 static const struct of_device_id exynos_ehci_match[] = {
288 	{ .compatible = "samsung,exynos4210-ehci" },
289 	{},
290 };
291 MODULE_DEVICE_TABLE(of, exynos_ehci_match);
292 #endif
293 
294 static struct platform_driver exynos_ehci_driver = {
295 	.probe		= exynos_ehci_probe,
296 	.remove_new	= exynos_ehci_remove,
297 	.shutdown	= usb_hcd_platform_shutdown,
298 	.driver = {
299 		.name	= "exynos-ehci",
300 		.pm	= pm_ptr(&exynos_ehci_pm_ops),
301 		.of_match_table = of_match_ptr(exynos_ehci_match),
302 	}
303 };
304 static const struct ehci_driver_overrides exynos_overrides __initconst = {
305 	.extra_priv_size = sizeof(struct exynos_ehci_hcd),
306 };
307 
308 static int __init ehci_exynos_init(void)
309 {
310 	if (usb_disabled())
311 		return -ENODEV;
312 
313 	ehci_init_driver(&exynos_ehci_hc_driver, &exynos_overrides);
314 	return platform_driver_register(&exynos_ehci_driver);
315 }
316 module_init(ehci_exynos_init);
317 
318 static void __exit ehci_exynos_cleanup(void)
319 {
320 	platform_driver_unregister(&exynos_ehci_driver);
321 }
322 module_exit(ehci_exynos_cleanup);
323 
324 MODULE_DESCRIPTION(DRIVER_DESC);
325 MODULE_ALIAS("platform:exynos-ehci");
326 MODULE_AUTHOR("Jingoo Han");
327 MODULE_AUTHOR("Joonyoung Shim");
328 MODULE_LICENSE("GPL v2");
329