blob: 804fb62a888c478cbbe6913e8dc486ece21e181d [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>
Vivek Gautamb506eeb2013-01-22 18:30:40 +053018#include <linux/usb/samsung_usb_phy.h>
Jingoo Han62194242011-12-23 11:20:54 +090019#include <plat/usb-phy.h>
20
21struct exynos_ohci_hcd {
22 struct device *dev;
23 struct usb_hcd *hcd;
24 struct clk *clk;
25};
26
Vincent Palatin57465102012-11-01 11:05:28 -070027static int ohci_exynos_reset(struct usb_hcd *hcd)
28{
29 return ohci_init(hcd_to_ohci(hcd));
30}
31
Jingoo Han62194242011-12-23 11:20:54 +090032static int ohci_exynos_start(struct usb_hcd *hcd)
33{
34 struct ohci_hcd *ohci = hcd_to_ohci(hcd);
35 int ret;
36
37 ohci_dbg(ohci, "ohci_exynos_start, ohci:%p", ohci);
38
Jingoo Han62194242011-12-23 11:20:54 +090039 ret = ohci_run(ohci);
40 if (ret < 0) {
Greg Kroah-Hartman5e415242012-04-27 11:24:41 -070041 dev_err(hcd->self.controller, "can't start %s\n",
42 hcd->self.bus_name);
Jingoo Han62194242011-12-23 11:20:54 +090043 ohci_stop(hcd);
44 return ret;
45 }
46
47 return 0;
48}
49
50static const struct hc_driver exynos_ohci_hc_driver = {
51 .description = hcd_name,
52 .product_desc = "EXYNOS OHCI Host Controller",
53 .hcd_priv_size = sizeof(struct ohci_hcd),
54
55 .irq = ohci_irq,
56 .flags = HCD_MEMORY|HCD_USB11,
57
Vincent Palatin57465102012-11-01 11:05:28 -070058 .reset = ohci_exynos_reset,
Jingoo Han62194242011-12-23 11:20:54 +090059 .start = ohci_exynos_start,
60 .stop = ohci_stop,
61 .shutdown = ohci_shutdown,
62
63 .get_frame_number = ohci_get_frame,
64
65 .urb_enqueue = ohci_urb_enqueue,
66 .urb_dequeue = ohci_urb_dequeue,
67 .endpoint_disable = ohci_endpoint_disable,
68
69 .hub_status_data = ohci_hub_status_data,
70 .hub_control = ohci_hub_control,
71#ifdef CONFIG_PM
72 .bus_suspend = ohci_bus_suspend,
73 .bus_resume = ohci_bus_resume,
74#endif
75 .start_port_reset = ohci_start_port_reset,
76};
77
Vivek Gautamd5138932012-07-16 11:25:36 +053078static u64 ohci_exynos_dma_mask = DMA_BIT_MASK(32);
79
Bill Pemberton41ac7b32012-11-19 13:21:48 -050080static int exynos_ohci_probe(struct platform_device *pdev)
Jingoo Han62194242011-12-23 11:20:54 +090081{
82 struct exynos4_ohci_platdata *pdata;
83 struct exynos_ohci_hcd *exynos_ohci;
84 struct usb_hcd *hcd;
85 struct ohci_hcd *ohci;
86 struct resource *res;
87 int irq;
88 int err;
89
90 pdata = pdev->dev.platform_data;
91 if (!pdata) {
92 dev_err(&pdev->dev, "No platform data defined\n");
93 return -EINVAL;
94 }
95
Vivek Gautamd5138932012-07-16 11:25:36 +053096 /*
97 * Right now device-tree probed devices don't get dma_mask set.
98 * Since shared usb code relies on it, set it here for now.
99 * Once we move to full device tree support this will vanish off.
100 */
101 if (!pdev->dev.dma_mask)
102 pdev->dev.dma_mask = &ohci_exynos_dma_mask;
103 if (!pdev->dev.coherent_dma_mask)
104 pdev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
105
Jingoo Han390a0a72012-06-28 16:30:30 +0900106 exynos_ohci = devm_kzalloc(&pdev->dev, sizeof(struct exynos_ohci_hcd),
107 GFP_KERNEL);
Jingoo Han62194242011-12-23 11:20:54 +0900108 if (!exynos_ohci)
109 return -ENOMEM;
110
111 exynos_ohci->dev = &pdev->dev;
112
113 hcd = usb_create_hcd(&exynos_ohci_hc_driver, &pdev->dev,
114 dev_name(&pdev->dev));
115 if (!hcd) {
116 dev_err(&pdev->dev, "Unable to create HCD\n");
Jingoo Han390a0a72012-06-28 16:30:30 +0900117 return -ENOMEM;
Jingoo Han62194242011-12-23 11:20:54 +0900118 }
119
120 exynos_ohci->hcd = hcd;
Jingoo Han60d80ad2012-10-04 16:11:50 +0900121 exynos_ohci->clk = devm_clk_get(&pdev->dev, "usbhost");
Jingoo Han62194242011-12-23 11:20:54 +0900122
123 if (IS_ERR(exynos_ohci->clk)) {
124 dev_err(&pdev->dev, "Failed to get usbhost clock\n");
125 err = PTR_ERR(exynos_ohci->clk);
126 goto fail_clk;
127 }
128
Thomas Abrahamc05c9462012-10-03 08:41:37 +0900129 err = clk_prepare_enable(exynos_ohci->clk);
Jingoo Han62194242011-12-23 11:20:54 +0900130 if (err)
Jingoo Han60d80ad2012-10-04 16:11:50 +0900131 goto fail_clk;
Jingoo Han62194242011-12-23 11:20:54 +0900132
133 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
134 if (!res) {
135 dev_err(&pdev->dev, "Failed to get I/O memory\n");
136 err = -ENXIO;
137 goto fail_io;
138 }
139
140 hcd->rsrc_start = res->start;
141 hcd->rsrc_len = resource_size(res);
Jingoo Han390a0a72012-06-28 16:30:30 +0900142 hcd->regs = devm_ioremap(&pdev->dev, res->start, hcd->rsrc_len);
Jingoo Han62194242011-12-23 11:20:54 +0900143 if (!hcd->regs) {
144 dev_err(&pdev->dev, "Failed to remap I/O memory\n");
145 err = -ENOMEM;
146 goto fail_io;
147 }
148
149 irq = platform_get_irq(pdev, 0);
150 if (!irq) {
151 dev_err(&pdev->dev, "Failed to get IRQ\n");
152 err = -ENODEV;
Jingoo Han390a0a72012-06-28 16:30:30 +0900153 goto fail_io;
Jingoo Han62194242011-12-23 11:20:54 +0900154 }
155
156 if (pdata->phy_init)
Vivek Gautamb506eeb2013-01-22 18:30:40 +0530157 pdata->phy_init(pdev, USB_PHY_TYPE_HOST);
Jingoo Han62194242011-12-23 11:20:54 +0900158
159 ohci = hcd_to_ohci(hcd);
160 ohci_hcd_init(ohci);
161
162 err = usb_add_hcd(hcd, irq, IRQF_SHARED);
163 if (err) {
164 dev_err(&pdev->dev, "Failed to add USB HCD\n");
Jingoo Han390a0a72012-06-28 16:30:30 +0900165 goto fail_io;
Jingoo Han62194242011-12-23 11:20:54 +0900166 }
167
168 platform_set_drvdata(pdev, exynos_ohci);
169
170 return 0;
171
Jingoo Han62194242011-12-23 11:20:54 +0900172fail_io:
Thomas Abrahamc05c9462012-10-03 08:41:37 +0900173 clk_disable_unprepare(exynos_ohci->clk);
Jingoo Han62194242011-12-23 11:20:54 +0900174fail_clk:
175 usb_put_hcd(hcd);
Jingoo Han62194242011-12-23 11:20:54 +0900176 return err;
177}
178
Bill Pembertonfb4e98a2012-11-19 13:26:20 -0500179static int exynos_ohci_remove(struct platform_device *pdev)
Jingoo Han62194242011-12-23 11:20:54 +0900180{
181 struct exynos4_ohci_platdata *pdata = pdev->dev.platform_data;
182 struct exynos_ohci_hcd *exynos_ohci = platform_get_drvdata(pdev);
183 struct usb_hcd *hcd = exynos_ohci->hcd;
184
185 usb_remove_hcd(hcd);
186
187 if (pdata && pdata->phy_exit)
Vivek Gautamb506eeb2013-01-22 18:30:40 +0530188 pdata->phy_exit(pdev, USB_PHY_TYPE_HOST);
Jingoo Han62194242011-12-23 11:20:54 +0900189
Thomas Abrahamc05c9462012-10-03 08:41:37 +0900190 clk_disable_unprepare(exynos_ohci->clk);
Jingoo Han62194242011-12-23 11:20:54 +0900191
192 usb_put_hcd(hcd);
Jingoo Han62194242011-12-23 11:20:54 +0900193
194 return 0;
195}
196
197static void exynos_ohci_shutdown(struct platform_device *pdev)
198{
199 struct exynos_ohci_hcd *exynos_ohci = platform_get_drvdata(pdev);
200 struct usb_hcd *hcd = exynos_ohci->hcd;
201
202 if (hcd->driver->shutdown)
203 hcd->driver->shutdown(hcd);
204}
205
206#ifdef CONFIG_PM
207static int exynos_ohci_suspend(struct device *dev)
208{
209 struct exynos_ohci_hcd *exynos_ohci = dev_get_drvdata(dev);
210 struct usb_hcd *hcd = exynos_ohci->hcd;
211 struct ohci_hcd *ohci = hcd_to_ohci(hcd);
212 struct platform_device *pdev = to_platform_device(dev);
213 struct exynos4_ohci_platdata *pdata = pdev->dev.platform_data;
214 unsigned long flags;
215 int rc = 0;
216
217 /*
218 * Root hub was already suspended. Disable irq emission and
219 * mark HW unaccessible, bail out if RH has been resumed. Use
220 * the spinlock to properly synchronize with possible pending
221 * RH suspend or resume activity.
Jingoo Han62194242011-12-23 11:20:54 +0900222 */
223 spin_lock_irqsave(&ohci->lock, flags);
Jingoo Han2b4ffe32012-02-23 17:26:33 +0900224 if (ohci->rh_state != OHCI_RH_SUSPENDED &&
225 ohci->rh_state != OHCI_RH_HALTED) {
Jingoo Han62194242011-12-23 11:20:54 +0900226 rc = -EINVAL;
227 goto fail;
228 }
229
230 clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
231
232 if (pdata && pdata->phy_exit)
Vivek Gautamb506eeb2013-01-22 18:30:40 +0530233 pdata->phy_exit(pdev, USB_PHY_TYPE_HOST);
Jingoo Hane864abe2012-06-28 16:49:42 +0900234
Thomas Abrahamc05c9462012-10-03 08:41:37 +0900235 clk_disable_unprepare(exynos_ohci->clk);
Jingoo Hane864abe2012-06-28 16:49:42 +0900236
Jingoo Han62194242011-12-23 11:20:54 +0900237fail:
238 spin_unlock_irqrestore(&ohci->lock, flags);
239
240 return rc;
241}
242
243static int exynos_ohci_resume(struct device *dev)
244{
245 struct exynos_ohci_hcd *exynos_ohci = dev_get_drvdata(dev);
246 struct usb_hcd *hcd = exynos_ohci->hcd;
247 struct platform_device *pdev = to_platform_device(dev);
248 struct exynos4_ohci_platdata *pdata = pdev->dev.platform_data;
249
Thomas Abrahamc05c9462012-10-03 08:41:37 +0900250 clk_prepare_enable(exynos_ohci->clk);
Jingoo Hane864abe2012-06-28 16:49:42 +0900251
Jingoo Han62194242011-12-23 11:20:54 +0900252 if (pdata && pdata->phy_init)
Vivek Gautamb506eeb2013-01-22 18:30:40 +0530253 pdata->phy_init(pdev, USB_PHY_TYPE_HOST);
Jingoo Han62194242011-12-23 11:20:54 +0900254
Florian Fainellicfa49b42012-10-08 15:11:29 +0200255 ohci_resume(hcd, false);
Jingoo Han62194242011-12-23 11:20:54 +0900256
257 return 0;
258}
259#else
260#define exynos_ohci_suspend NULL
261#define exynos_ohci_resume NULL
262#endif
263
264static const struct dev_pm_ops exynos_ohci_pm_ops = {
265 .suspend = exynos_ohci_suspend,
266 .resume = exynos_ohci_resume,
267};
268
Vivek Gautamd5138932012-07-16 11:25:36 +0530269#ifdef CONFIG_OF
270static const struct of_device_id exynos_ohci_match[] = {
271 { .compatible = "samsung,exynos-ohci" },
272 {},
273};
274MODULE_DEVICE_TABLE(of, exynos_ohci_match);
275#endif
276
Jingoo Han62194242011-12-23 11:20:54 +0900277static struct platform_driver exynos_ohci_driver = {
278 .probe = exynos_ohci_probe,
Bill Pemberton76904172012-11-19 13:21:08 -0500279 .remove = exynos_ohci_remove,
Jingoo Han62194242011-12-23 11:20:54 +0900280 .shutdown = exynos_ohci_shutdown,
281 .driver = {
282 .name = "exynos-ohci",
283 .owner = THIS_MODULE,
284 .pm = &exynos_ohci_pm_ops,
Vivek Gautamd5138932012-07-16 11:25:36 +0530285 .of_match_table = of_match_ptr(exynos_ohci_match),
Jingoo Han62194242011-12-23 11:20:54 +0900286 }
287};
288
289MODULE_ALIAS("platform:exynos-ohci");
290MODULE_AUTHOR("Jingoo Han <jg1.han@samsung.com>");