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