blob: 8bcbdb5ade2d4d9d70bf8645f34d8ad299bcd410 [file] [log] [blame]
Jingoo Han62194242011-12-23 11:20:54 +09001/*
2 * SAMSUNG EXYNOS USB HOST OHCI Controller
3 *
4 * Copyright (C) 2011 Samsung Electronics Co.Ltd
5 * Author: Jingoo Han <jg1.han@samsung.com>
6 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2 of the License, or (at your
10 * option) any later version.
11 *
12 */
13
14#include <linux/clk.h>
15#include <linux/platform_device.h>
16#include <mach/ohci.h>
17#include <plat/usb-phy.h>
18
19struct exynos_ohci_hcd {
20 struct device *dev;
21 struct usb_hcd *hcd;
22 struct clk *clk;
23};
24
25static int ohci_exynos_start(struct usb_hcd *hcd)
26{
27 struct ohci_hcd *ohci = hcd_to_ohci(hcd);
28 int ret;
29
30 ohci_dbg(ohci, "ohci_exynos_start, ohci:%p", ohci);
31
32 ret = ohci_init(ohci);
33 if (ret < 0)
34 return ret;
35
36 ret = ohci_run(ohci);
37 if (ret < 0) {
Greg Kroah-Hartman5e415242012-04-27 11:24:41 -070038 dev_err(hcd->self.controller, "can't start %s\n",
39 hcd->self.bus_name);
Jingoo Han62194242011-12-23 11:20:54 +090040 ohci_stop(hcd);
41 return ret;
42 }
43
44 return 0;
45}
46
47static const struct hc_driver exynos_ohci_hc_driver = {
48 .description = hcd_name,
49 .product_desc = "EXYNOS OHCI Host Controller",
50 .hcd_priv_size = sizeof(struct ohci_hcd),
51
52 .irq = ohci_irq,
53 .flags = HCD_MEMORY|HCD_USB11,
54
55 .start = ohci_exynos_start,
56 .stop = ohci_stop,
57 .shutdown = ohci_shutdown,
58
59 .get_frame_number = ohci_get_frame,
60
61 .urb_enqueue = ohci_urb_enqueue,
62 .urb_dequeue = ohci_urb_dequeue,
63 .endpoint_disable = ohci_endpoint_disable,
64
65 .hub_status_data = ohci_hub_status_data,
66 .hub_control = ohci_hub_control,
67#ifdef CONFIG_PM
68 .bus_suspend = ohci_bus_suspend,
69 .bus_resume = ohci_bus_resume,
70#endif
71 .start_port_reset = ohci_start_port_reset,
72};
73
74static int __devinit exynos_ohci_probe(struct platform_device *pdev)
75{
76 struct exynos4_ohci_platdata *pdata;
77 struct exynos_ohci_hcd *exynos_ohci;
78 struct usb_hcd *hcd;
79 struct ohci_hcd *ohci;
80 struct resource *res;
81 int irq;
82 int err;
83
84 pdata = pdev->dev.platform_data;
85 if (!pdata) {
86 dev_err(&pdev->dev, "No platform data defined\n");
87 return -EINVAL;
88 }
89
Jingoo Han390a0a72012-06-28 16:30:30 +090090 exynos_ohci = devm_kzalloc(&pdev->dev, sizeof(struct exynos_ohci_hcd),
91 GFP_KERNEL);
Jingoo Han62194242011-12-23 11:20:54 +090092 if (!exynos_ohci)
93 return -ENOMEM;
94
95 exynos_ohci->dev = &pdev->dev;
96
97 hcd = usb_create_hcd(&exynos_ohci_hc_driver, &pdev->dev,
98 dev_name(&pdev->dev));
99 if (!hcd) {
100 dev_err(&pdev->dev, "Unable to create HCD\n");
Jingoo Han390a0a72012-06-28 16:30:30 +0900101 return -ENOMEM;
Jingoo Han62194242011-12-23 11:20:54 +0900102 }
103
104 exynos_ohci->hcd = hcd;
105 exynos_ohci->clk = clk_get(&pdev->dev, "usbhost");
106
107 if (IS_ERR(exynos_ohci->clk)) {
108 dev_err(&pdev->dev, "Failed to get usbhost clock\n");
109 err = PTR_ERR(exynos_ohci->clk);
110 goto fail_clk;
111 }
112
113 err = clk_enable(exynos_ohci->clk);
114 if (err)
115 goto fail_clken;
116
117 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
118 if (!res) {
119 dev_err(&pdev->dev, "Failed to get I/O memory\n");
120 err = -ENXIO;
121 goto fail_io;
122 }
123
124 hcd->rsrc_start = res->start;
125 hcd->rsrc_len = resource_size(res);
Jingoo Han390a0a72012-06-28 16:30:30 +0900126 hcd->regs = devm_ioremap(&pdev->dev, res->start, hcd->rsrc_len);
Jingoo Han62194242011-12-23 11:20:54 +0900127 if (!hcd->regs) {
128 dev_err(&pdev->dev, "Failed to remap I/O memory\n");
129 err = -ENOMEM;
130 goto fail_io;
131 }
132
133 irq = platform_get_irq(pdev, 0);
134 if (!irq) {
135 dev_err(&pdev->dev, "Failed to get IRQ\n");
136 err = -ENODEV;
Jingoo Han390a0a72012-06-28 16:30:30 +0900137 goto fail_io;
Jingoo Han62194242011-12-23 11:20:54 +0900138 }
139
140 if (pdata->phy_init)
141 pdata->phy_init(pdev, S5P_USB_PHY_HOST);
142
143 ohci = hcd_to_ohci(hcd);
144 ohci_hcd_init(ohci);
145
146 err = usb_add_hcd(hcd, irq, IRQF_SHARED);
147 if (err) {
148 dev_err(&pdev->dev, "Failed to add USB HCD\n");
Jingoo Han390a0a72012-06-28 16:30:30 +0900149 goto fail_io;
Jingoo Han62194242011-12-23 11:20:54 +0900150 }
151
152 platform_set_drvdata(pdev, exynos_ohci);
153
154 return 0;
155
Jingoo Han62194242011-12-23 11:20:54 +0900156fail_io:
157 clk_disable(exynos_ohci->clk);
158fail_clken:
159 clk_put(exynos_ohci->clk);
160fail_clk:
161 usb_put_hcd(hcd);
Jingoo Han62194242011-12-23 11:20:54 +0900162 return err;
163}
164
165static int __devexit exynos_ohci_remove(struct platform_device *pdev)
166{
167 struct exynos4_ohci_platdata *pdata = pdev->dev.platform_data;
168 struct exynos_ohci_hcd *exynos_ohci = platform_get_drvdata(pdev);
169 struct usb_hcd *hcd = exynos_ohci->hcd;
170
171 usb_remove_hcd(hcd);
172
173 if (pdata && pdata->phy_exit)
174 pdata->phy_exit(pdev, S5P_USB_PHY_HOST);
175
Jingoo Han62194242011-12-23 11:20:54 +0900176 clk_disable(exynos_ohci->clk);
177 clk_put(exynos_ohci->clk);
178
179 usb_put_hcd(hcd);
Jingoo Han62194242011-12-23 11:20:54 +0900180
181 return 0;
182}
183
184static void exynos_ohci_shutdown(struct platform_device *pdev)
185{
186 struct exynos_ohci_hcd *exynos_ohci = platform_get_drvdata(pdev);
187 struct usb_hcd *hcd = exynos_ohci->hcd;
188
189 if (hcd->driver->shutdown)
190 hcd->driver->shutdown(hcd);
191}
192
193#ifdef CONFIG_PM
194static int exynos_ohci_suspend(struct device *dev)
195{
196 struct exynos_ohci_hcd *exynos_ohci = dev_get_drvdata(dev);
197 struct usb_hcd *hcd = exynos_ohci->hcd;
198 struct ohci_hcd *ohci = hcd_to_ohci(hcd);
199 struct platform_device *pdev = to_platform_device(dev);
200 struct exynos4_ohci_platdata *pdata = pdev->dev.platform_data;
201 unsigned long flags;
202 int rc = 0;
203
204 /*
205 * Root hub was already suspended. Disable irq emission and
206 * mark HW unaccessible, bail out if RH has been resumed. Use
207 * the spinlock to properly synchronize with possible pending
208 * RH suspend or resume activity.
Jingoo Han62194242011-12-23 11:20:54 +0900209 */
210 spin_lock_irqsave(&ohci->lock, flags);
Jingoo Han2b4ffe32012-02-23 17:26:33 +0900211 if (ohci->rh_state != OHCI_RH_SUSPENDED &&
212 ohci->rh_state != OHCI_RH_HALTED) {
Jingoo Han62194242011-12-23 11:20:54 +0900213 rc = -EINVAL;
214 goto fail;
215 }
216
217 clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
218
219 if (pdata && pdata->phy_exit)
220 pdata->phy_exit(pdev, S5P_USB_PHY_HOST);
Jingoo Hane864abe2012-06-28 16:49:42 +0900221
222 clk_disable(exynos_ohci->clk);
223
Jingoo Han62194242011-12-23 11:20:54 +0900224fail:
225 spin_unlock_irqrestore(&ohci->lock, flags);
226
227 return rc;
228}
229
230static int exynos_ohci_resume(struct device *dev)
231{
232 struct exynos_ohci_hcd *exynos_ohci = dev_get_drvdata(dev);
233 struct usb_hcd *hcd = exynos_ohci->hcd;
234 struct platform_device *pdev = to_platform_device(dev);
235 struct exynos4_ohci_platdata *pdata = pdev->dev.platform_data;
236
Jingoo Hane864abe2012-06-28 16:49:42 +0900237 clk_enable(exynos_ohci->clk);
238
Jingoo Han62194242011-12-23 11:20:54 +0900239 if (pdata && pdata->phy_init)
240 pdata->phy_init(pdev, S5P_USB_PHY_HOST);
241
242 /* Mark hardware accessible again as we are out of D3 state by now */
243 set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
244
245 ohci_finish_controller_resume(hcd);
246
247 return 0;
248}
249#else
250#define exynos_ohci_suspend NULL
251#define exynos_ohci_resume NULL
252#endif
253
254static const struct dev_pm_ops exynos_ohci_pm_ops = {
255 .suspend = exynos_ohci_suspend,
256 .resume = exynos_ohci_resume,
257};
258
259static struct platform_driver exynos_ohci_driver = {
260 .probe = exynos_ohci_probe,
261 .remove = __devexit_p(exynos_ohci_remove),
262 .shutdown = exynos_ohci_shutdown,
263 .driver = {
264 .name = "exynos-ohci",
265 .owner = THIS_MODULE,
266 .pm = &exynos_ohci_pm_ops,
267 }
268};
269
270MODULE_ALIAS("platform:exynos-ohci");
271MODULE_AUTHOR("Jingoo Han <jg1.han@samsung.com>");