blob: 2f303295b428b0f10f867aaea5ac2daee4583e5e [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>
Vivek Gautamd5138932012-07-16 11:25:36 +053015#include <linux/of.h>
Jingoo Han62194242011-12-23 11:20:54 +090016#include <linux/platform_device.h>
Arnd Bergmann436d42c2012-08-24 15:22:12 +020017#include <linux/platform_data/usb-exynos.h>
Jingoo Han62194242011-12-23 11:20:54 +090018#include <plat/usb-phy.h>
19
20struct exynos_ohci_hcd {
21 struct device *dev;
22 struct usb_hcd *hcd;
23 struct clk *clk;
24};
25
26static int ohci_exynos_start(struct usb_hcd *hcd)
27{
28 struct ohci_hcd *ohci = hcd_to_ohci(hcd);
29 int ret;
30
31 ohci_dbg(ohci, "ohci_exynos_start, ohci:%p", ohci);
32
33 ret = ohci_init(ohci);
34 if (ret < 0)
35 return ret;
36
37 ret = ohci_run(ohci);
38 if (ret < 0) {
Greg Kroah-Hartman5e415242012-04-27 11:24:41 -070039 dev_err(hcd->self.controller, "can't start %s\n",
40 hcd->self.bus_name);
Jingoo Han62194242011-12-23 11:20:54 +090041 ohci_stop(hcd);
42 return ret;
43 }
44
45 return 0;
46}
47
48static const struct hc_driver exynos_ohci_hc_driver = {
49 .description = hcd_name,
50 .product_desc = "EXYNOS OHCI Host Controller",
51 .hcd_priv_size = sizeof(struct ohci_hcd),
52
53 .irq = ohci_irq,
54 .flags = HCD_MEMORY|HCD_USB11,
55
56 .start = ohci_exynos_start,
57 .stop = ohci_stop,
58 .shutdown = ohci_shutdown,
59
60 .get_frame_number = ohci_get_frame,
61
62 .urb_enqueue = ohci_urb_enqueue,
63 .urb_dequeue = ohci_urb_dequeue,
64 .endpoint_disable = ohci_endpoint_disable,
65
66 .hub_status_data = ohci_hub_status_data,
67 .hub_control = ohci_hub_control,
68#ifdef CONFIG_PM
69 .bus_suspend = ohci_bus_suspend,
70 .bus_resume = ohci_bus_resume,
71#endif
72 .start_port_reset = ohci_start_port_reset,
73};
74
Vivek Gautamd5138932012-07-16 11:25:36 +053075static u64 ohci_exynos_dma_mask = DMA_BIT_MASK(32);
76
Jingoo Han62194242011-12-23 11:20:54 +090077static int __devinit exynos_ohci_probe(struct platform_device *pdev)
78{
79 struct exynos4_ohci_platdata *pdata;
80 struct exynos_ohci_hcd *exynos_ohci;
81 struct usb_hcd *hcd;
82 struct ohci_hcd *ohci;
83 struct resource *res;
84 int irq;
85 int err;
86
87 pdata = pdev->dev.platform_data;
88 if (!pdata) {
89 dev_err(&pdev->dev, "No platform data defined\n");
90 return -EINVAL;
91 }
92
Vivek Gautamd5138932012-07-16 11:25:36 +053093 /*
94 * Right now device-tree probed devices don't get dma_mask set.
95 * Since shared usb code relies on it, set it here for now.
96 * Once we move to full device tree support this will vanish off.
97 */
98 if (!pdev->dev.dma_mask)
99 pdev->dev.dma_mask = &ohci_exynos_dma_mask;
100 if (!pdev->dev.coherent_dma_mask)
101 pdev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
102
Jingoo Han390a0a72012-06-28 16:30:30 +0900103 exynos_ohci = devm_kzalloc(&pdev->dev, sizeof(struct exynos_ohci_hcd),
104 GFP_KERNEL);
Jingoo Han62194242011-12-23 11:20:54 +0900105 if (!exynos_ohci)
106 return -ENOMEM;
107
108 exynos_ohci->dev = &pdev->dev;
109
110 hcd = usb_create_hcd(&exynos_ohci_hc_driver, &pdev->dev,
111 dev_name(&pdev->dev));
112 if (!hcd) {
113 dev_err(&pdev->dev, "Unable to create HCD\n");
Jingoo Han390a0a72012-06-28 16:30:30 +0900114 return -ENOMEM;
Jingoo Han62194242011-12-23 11:20:54 +0900115 }
116
117 exynos_ohci->hcd = hcd;
Jingoo Han60d80ad2012-10-04 16:11:50 +0900118 exynos_ohci->clk = devm_clk_get(&pdev->dev, "usbhost");
Jingoo Han62194242011-12-23 11:20:54 +0900119
120 if (IS_ERR(exynos_ohci->clk)) {
121 dev_err(&pdev->dev, "Failed to get usbhost clock\n");
122 err = PTR_ERR(exynos_ohci->clk);
123 goto fail_clk;
124 }
125
Thomas Abrahamc05c9462012-10-03 08:41:37 +0900126 err = clk_prepare_enable(exynos_ohci->clk);
Jingoo Han62194242011-12-23 11:20:54 +0900127 if (err)
Jingoo Han60d80ad2012-10-04 16:11:50 +0900128 goto fail_clk;
Jingoo Han62194242011-12-23 11:20:54 +0900129
130 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
131 if (!res) {
132 dev_err(&pdev->dev, "Failed to get I/O memory\n");
133 err = -ENXIO;
134 goto fail_io;
135 }
136
137 hcd->rsrc_start = res->start;
138 hcd->rsrc_len = resource_size(res);
Jingoo Han390a0a72012-06-28 16:30:30 +0900139 hcd->regs = devm_ioremap(&pdev->dev, res->start, hcd->rsrc_len);
Jingoo Han62194242011-12-23 11:20:54 +0900140 if (!hcd->regs) {
141 dev_err(&pdev->dev, "Failed to remap I/O memory\n");
142 err = -ENOMEM;
143 goto fail_io;
144 }
145
146 irq = platform_get_irq(pdev, 0);
147 if (!irq) {
148 dev_err(&pdev->dev, "Failed to get IRQ\n");
149 err = -ENODEV;
Jingoo Han390a0a72012-06-28 16:30:30 +0900150 goto fail_io;
Jingoo Han62194242011-12-23 11:20:54 +0900151 }
152
153 if (pdata->phy_init)
154 pdata->phy_init(pdev, S5P_USB_PHY_HOST);
155
156 ohci = hcd_to_ohci(hcd);
157 ohci_hcd_init(ohci);
158
159 err = usb_add_hcd(hcd, irq, IRQF_SHARED);
160 if (err) {
161 dev_err(&pdev->dev, "Failed to add USB HCD\n");
Jingoo Han390a0a72012-06-28 16:30:30 +0900162 goto fail_io;
Jingoo Han62194242011-12-23 11:20:54 +0900163 }
164
165 platform_set_drvdata(pdev, exynos_ohci);
166
167 return 0;
168
Jingoo Han62194242011-12-23 11:20:54 +0900169fail_io:
Thomas Abrahamc05c9462012-10-03 08:41:37 +0900170 clk_disable_unprepare(exynos_ohci->clk);
Jingoo Han62194242011-12-23 11:20:54 +0900171fail_clk:
172 usb_put_hcd(hcd);
Jingoo Han62194242011-12-23 11:20:54 +0900173 return err;
174}
175
176static int __devexit exynos_ohci_remove(struct platform_device *pdev)
177{
178 struct exynos4_ohci_platdata *pdata = pdev->dev.platform_data;
179 struct exynos_ohci_hcd *exynos_ohci = platform_get_drvdata(pdev);
180 struct usb_hcd *hcd = exynos_ohci->hcd;
181
182 usb_remove_hcd(hcd);
183
184 if (pdata && pdata->phy_exit)
185 pdata->phy_exit(pdev, S5P_USB_PHY_HOST);
186
Thomas Abrahamc05c9462012-10-03 08:41:37 +0900187 clk_disable_unprepare(exynos_ohci->clk);
Jingoo Han62194242011-12-23 11:20:54 +0900188
189 usb_put_hcd(hcd);
Jingoo Han62194242011-12-23 11:20:54 +0900190
191 return 0;
192}
193
194static void exynos_ohci_shutdown(struct platform_device *pdev)
195{
196 struct exynos_ohci_hcd *exynos_ohci = platform_get_drvdata(pdev);
197 struct usb_hcd *hcd = exynos_ohci->hcd;
198
199 if (hcd->driver->shutdown)
200 hcd->driver->shutdown(hcd);
201}
202
203#ifdef CONFIG_PM
204static int exynos_ohci_suspend(struct device *dev)
205{
206 struct exynos_ohci_hcd *exynos_ohci = dev_get_drvdata(dev);
207 struct usb_hcd *hcd = exynos_ohci->hcd;
208 struct ohci_hcd *ohci = hcd_to_ohci(hcd);
209 struct platform_device *pdev = to_platform_device(dev);
210 struct exynos4_ohci_platdata *pdata = pdev->dev.platform_data;
211 unsigned long flags;
212 int rc = 0;
213
214 /*
215 * Root hub was already suspended. Disable irq emission and
216 * mark HW unaccessible, bail out if RH has been resumed. Use
217 * the spinlock to properly synchronize with possible pending
218 * RH suspend or resume activity.
Jingoo Han62194242011-12-23 11:20:54 +0900219 */
220 spin_lock_irqsave(&ohci->lock, flags);
Jingoo Han2b4ffe32012-02-23 17:26:33 +0900221 if (ohci->rh_state != OHCI_RH_SUSPENDED &&
222 ohci->rh_state != OHCI_RH_HALTED) {
Jingoo Han62194242011-12-23 11:20:54 +0900223 rc = -EINVAL;
224 goto fail;
225 }
226
227 clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
228
229 if (pdata && pdata->phy_exit)
230 pdata->phy_exit(pdev, S5P_USB_PHY_HOST);
Jingoo Hane864abe2012-06-28 16:49:42 +0900231
Thomas Abrahamc05c9462012-10-03 08:41:37 +0900232 clk_disable_unprepare(exynos_ohci->clk);
Jingoo Hane864abe2012-06-28 16:49:42 +0900233
Jingoo Han62194242011-12-23 11:20:54 +0900234fail:
235 spin_unlock_irqrestore(&ohci->lock, flags);
236
237 return rc;
238}
239
240static int exynos_ohci_resume(struct device *dev)
241{
242 struct exynos_ohci_hcd *exynos_ohci = dev_get_drvdata(dev);
243 struct usb_hcd *hcd = exynos_ohci->hcd;
244 struct platform_device *pdev = to_platform_device(dev);
245 struct exynos4_ohci_platdata *pdata = pdev->dev.platform_data;
246
Thomas Abrahamc05c9462012-10-03 08:41:37 +0900247 clk_prepare_enable(exynos_ohci->clk);
Jingoo Hane864abe2012-06-28 16:49:42 +0900248
Jingoo Han62194242011-12-23 11:20:54 +0900249 if (pdata && pdata->phy_init)
250 pdata->phy_init(pdev, S5P_USB_PHY_HOST);
251
Florian Fainellicfa49b42012-10-08 15:11:29 +0200252 ohci_resume(hcd, false);
Jingoo Han62194242011-12-23 11:20:54 +0900253
254 return 0;
255}
256#else
257#define exynos_ohci_suspend NULL
258#define exynos_ohci_resume NULL
259#endif
260
261static const struct dev_pm_ops exynos_ohci_pm_ops = {
262 .suspend = exynos_ohci_suspend,
263 .resume = exynos_ohci_resume,
264};
265
Vivek Gautamd5138932012-07-16 11:25:36 +0530266#ifdef CONFIG_OF
267static const struct of_device_id exynos_ohci_match[] = {
268 { .compatible = "samsung,exynos-ohci" },
269 {},
270};
271MODULE_DEVICE_TABLE(of, exynos_ohci_match);
272#endif
273
Jingoo Han62194242011-12-23 11:20:54 +0900274static struct platform_driver exynos_ohci_driver = {
275 .probe = exynos_ohci_probe,
276 .remove = __devexit_p(exynos_ohci_remove),
277 .shutdown = exynos_ohci_shutdown,
278 .driver = {
279 .name = "exynos-ohci",
280 .owner = THIS_MODULE,
281 .pm = &exynos_ohci_pm_ops,
Vivek Gautamd5138932012-07-16 11:25:36 +0530282 .of_match_table = of_match_ptr(exynos_ohci_match),
Jingoo Han62194242011-12-23 11:20:54 +0900283 }
284};
285
286MODULE_ALIAS("platform:exynos-ohci");
287MODULE_AUTHOR("Jingoo Han <jg1.han@samsung.com>");