blob: 509fa515248cad480c74b0566153b53367f3b7ab [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 Gautamed993bf2013-01-22 18:30:43 +053018#include <linux/usb/phy.h>
Vivek Gautamb506eeb2013-01-22 18:30:40 +053019#include <linux/usb/samsung_usb_phy.h>
Jingoo Han62194242011-12-23 11:20:54 +090020#include <plat/usb-phy.h>
21
22struct exynos_ohci_hcd {
23 struct device *dev;
24 struct usb_hcd *hcd;
25 struct clk *clk;
Vivek Gautamed993bf2013-01-22 18:30:43 +053026 struct usb_phy *phy;
27 struct usb_otg *otg;
28 struct exynos4_ohci_platdata *pdata;
Jingoo Han62194242011-12-23 11:20:54 +090029};
30
Vivek Gautamed993bf2013-01-22 18:30:43 +053031static void exynos_ohci_phy_enable(struct exynos_ohci_hcd *exynos_ohci)
32{
33 struct platform_device *pdev = to_platform_device(exynos_ohci->dev);
34
35 if (exynos_ohci->phy)
36 usb_phy_init(exynos_ohci->phy);
Thomas Abraham28717822013-04-11 17:12:30 +053037 else if (exynos_ohci->pdata && exynos_ohci->pdata->phy_init)
Vivek Gautamed993bf2013-01-22 18:30:43 +053038 exynos_ohci->pdata->phy_init(pdev, USB_PHY_TYPE_HOST);
39}
40
41static void exynos_ohci_phy_disable(struct exynos_ohci_hcd *exynos_ohci)
42{
43 struct platform_device *pdev = to_platform_device(exynos_ohci->dev);
44
45 if (exynos_ohci->phy)
46 usb_phy_shutdown(exynos_ohci->phy);
Thomas Abraham28717822013-04-11 17:12:30 +053047 else if (exynos_ohci->pdata && exynos_ohci->pdata->phy_exit)
Vivek Gautamed993bf2013-01-22 18:30:43 +053048 exynos_ohci->pdata->phy_exit(pdev, USB_PHY_TYPE_HOST);
49}
50
Vincent Palatin57465102012-11-01 11:05:28 -070051static int ohci_exynos_reset(struct usb_hcd *hcd)
52{
53 return ohci_init(hcd_to_ohci(hcd));
54}
55
Jingoo Han62194242011-12-23 11:20:54 +090056static int ohci_exynos_start(struct usb_hcd *hcd)
57{
58 struct ohci_hcd *ohci = hcd_to_ohci(hcd);
59 int ret;
60
61 ohci_dbg(ohci, "ohci_exynos_start, ohci:%p", ohci);
62
Jingoo Han62194242011-12-23 11:20:54 +090063 ret = ohci_run(ohci);
64 if (ret < 0) {
Greg Kroah-Hartman5e415242012-04-27 11:24:41 -070065 dev_err(hcd->self.controller, "can't start %s\n",
66 hcd->self.bus_name);
Jingoo Han62194242011-12-23 11:20:54 +090067 ohci_stop(hcd);
68 return ret;
69 }
70
71 return 0;
72}
73
74static const struct hc_driver exynos_ohci_hc_driver = {
75 .description = hcd_name,
76 .product_desc = "EXYNOS OHCI Host Controller",
77 .hcd_priv_size = sizeof(struct ohci_hcd),
78
79 .irq = ohci_irq,
80 .flags = HCD_MEMORY|HCD_USB11,
81
Vincent Palatin57465102012-11-01 11:05:28 -070082 .reset = ohci_exynos_reset,
Jingoo Han62194242011-12-23 11:20:54 +090083 .start = ohci_exynos_start,
84 .stop = ohci_stop,
85 .shutdown = ohci_shutdown,
86
87 .get_frame_number = ohci_get_frame,
88
89 .urb_enqueue = ohci_urb_enqueue,
90 .urb_dequeue = ohci_urb_dequeue,
91 .endpoint_disable = ohci_endpoint_disable,
92
93 .hub_status_data = ohci_hub_status_data,
94 .hub_control = ohci_hub_control,
95#ifdef CONFIG_PM
96 .bus_suspend = ohci_bus_suspend,
97 .bus_resume = ohci_bus_resume,
98#endif
99 .start_port_reset = ohci_start_port_reset,
100};
101
Vivek Gautamd5138932012-07-16 11:25:36 +0530102static u64 ohci_exynos_dma_mask = DMA_BIT_MASK(32);
103
Bill Pemberton41ac7b32012-11-19 13:21:48 -0500104static int exynos_ohci_probe(struct platform_device *pdev)
Jingoo Han62194242011-12-23 11:20:54 +0900105{
Vivek Gautamed993bf2013-01-22 18:30:43 +0530106 struct exynos4_ohci_platdata *pdata = pdev->dev.platform_data;
Jingoo Han62194242011-12-23 11:20:54 +0900107 struct exynos_ohci_hcd *exynos_ohci;
108 struct usb_hcd *hcd;
109 struct ohci_hcd *ohci;
110 struct resource *res;
Vivek Gautamed993bf2013-01-22 18:30:43 +0530111 struct usb_phy *phy;
Jingoo Han62194242011-12-23 11:20:54 +0900112 int irq;
113 int err;
114
Vivek Gautamd5138932012-07-16 11:25:36 +0530115 /*
116 * Right now device-tree probed devices don't get dma_mask set.
117 * Since shared usb code relies on it, set it here for now.
118 * Once we move to full device tree support this will vanish off.
119 */
120 if (!pdev->dev.dma_mask)
121 pdev->dev.dma_mask = &ohci_exynos_dma_mask;
122 if (!pdev->dev.coherent_dma_mask)
123 pdev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
124
Jingoo Han390a0a72012-06-28 16:30:30 +0900125 exynos_ohci = devm_kzalloc(&pdev->dev, sizeof(struct exynos_ohci_hcd),
126 GFP_KERNEL);
Jingoo Han62194242011-12-23 11:20:54 +0900127 if (!exynos_ohci)
128 return -ENOMEM;
129
Thomas Abraham28717822013-04-11 17:12:30 +0530130 if (of_device_is_compatible(pdev->dev.of_node,
131 "samsung,exynos5440-ohci"))
132 goto skip_phy;
133
Vivek Gautamed993bf2013-01-22 18:30:43 +0530134 phy = devm_usb_get_phy(&pdev->dev, USB_PHY_TYPE_USB2);
Felipe Balbi9ee1c7f2013-03-15 11:05:03 +0200135 if (IS_ERR(phy)) {
Vivek Gautamed993bf2013-01-22 18:30:43 +0530136 /* Fallback to pdata */
137 if (!pdata) {
138 dev_warn(&pdev->dev, "no platform data or transceiver defined\n");
139 return -EPROBE_DEFER;
140 } else {
141 exynos_ohci->pdata = pdata;
142 }
143 } else {
144 exynos_ohci->phy = phy;
145 exynos_ohci->otg = phy->otg;
146 }
147
Thomas Abraham28717822013-04-11 17:12:30 +0530148skip_phy:
149
Jingoo Han62194242011-12-23 11:20:54 +0900150 exynos_ohci->dev = &pdev->dev;
151
152 hcd = usb_create_hcd(&exynos_ohci_hc_driver, &pdev->dev,
153 dev_name(&pdev->dev));
154 if (!hcd) {
155 dev_err(&pdev->dev, "Unable to create HCD\n");
Jingoo Han390a0a72012-06-28 16:30:30 +0900156 return -ENOMEM;
Jingoo Han62194242011-12-23 11:20:54 +0900157 }
158
159 exynos_ohci->hcd = hcd;
Jingoo Han60d80ad2012-10-04 16:11:50 +0900160 exynos_ohci->clk = devm_clk_get(&pdev->dev, "usbhost");
Jingoo Han62194242011-12-23 11:20:54 +0900161
162 if (IS_ERR(exynos_ohci->clk)) {
163 dev_err(&pdev->dev, "Failed to get usbhost clock\n");
164 err = PTR_ERR(exynos_ohci->clk);
165 goto fail_clk;
166 }
167
Thomas Abrahamc05c9462012-10-03 08:41:37 +0900168 err = clk_prepare_enable(exynos_ohci->clk);
Jingoo Han62194242011-12-23 11:20:54 +0900169 if (err)
Jingoo Han60d80ad2012-10-04 16:11:50 +0900170 goto fail_clk;
Jingoo Han62194242011-12-23 11:20:54 +0900171
172 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
173 if (!res) {
174 dev_err(&pdev->dev, "Failed to get I/O memory\n");
175 err = -ENXIO;
176 goto fail_io;
177 }
178
179 hcd->rsrc_start = res->start;
180 hcd->rsrc_len = resource_size(res);
Jingoo Han390a0a72012-06-28 16:30:30 +0900181 hcd->regs = devm_ioremap(&pdev->dev, res->start, hcd->rsrc_len);
Jingoo Han62194242011-12-23 11:20:54 +0900182 if (!hcd->regs) {
183 dev_err(&pdev->dev, "Failed to remap I/O memory\n");
184 err = -ENOMEM;
185 goto fail_io;
186 }
187
188 irq = platform_get_irq(pdev, 0);
189 if (!irq) {
190 dev_err(&pdev->dev, "Failed to get IRQ\n");
191 err = -ENODEV;
Jingoo Han390a0a72012-06-28 16:30:30 +0900192 goto fail_io;
Jingoo Han62194242011-12-23 11:20:54 +0900193 }
194
Vivek Gautamed993bf2013-01-22 18:30:43 +0530195 if (exynos_ohci->otg)
196 exynos_ohci->otg->set_host(exynos_ohci->otg,
197 &exynos_ohci->hcd->self);
198
199 exynos_ohci_phy_enable(exynos_ohci);
Jingoo Han62194242011-12-23 11:20:54 +0900200
201 ohci = hcd_to_ohci(hcd);
202 ohci_hcd_init(ohci);
203
204 err = usb_add_hcd(hcd, irq, IRQF_SHARED);
205 if (err) {
206 dev_err(&pdev->dev, "Failed to add USB HCD\n");
Vivek Gautamed993bf2013-01-22 18:30:43 +0530207 goto fail_add_hcd;
Jingoo Han62194242011-12-23 11:20:54 +0900208 }
209
210 platform_set_drvdata(pdev, exynos_ohci);
211
212 return 0;
213
Vivek Gautamed993bf2013-01-22 18:30:43 +0530214fail_add_hcd:
215 exynos_ohci_phy_disable(exynos_ohci);
Jingoo Han62194242011-12-23 11:20:54 +0900216fail_io:
Thomas Abrahamc05c9462012-10-03 08:41:37 +0900217 clk_disable_unprepare(exynos_ohci->clk);
Jingoo Han62194242011-12-23 11:20:54 +0900218fail_clk:
219 usb_put_hcd(hcd);
Jingoo Han62194242011-12-23 11:20:54 +0900220 return err;
221}
222
Bill Pembertonfb4e98a2012-11-19 13:26:20 -0500223static int exynos_ohci_remove(struct platform_device *pdev)
Jingoo Han62194242011-12-23 11:20:54 +0900224{
Jingoo Han62194242011-12-23 11:20:54 +0900225 struct exynos_ohci_hcd *exynos_ohci = platform_get_drvdata(pdev);
226 struct usb_hcd *hcd = exynos_ohci->hcd;
227
228 usb_remove_hcd(hcd);
229
Vivek Gautamed993bf2013-01-22 18:30:43 +0530230 if (exynos_ohci->otg)
231 exynos_ohci->otg->set_host(exynos_ohci->otg,
232 &exynos_ohci->hcd->self);
233
234 exynos_ohci_phy_disable(exynos_ohci);
Jingoo Han62194242011-12-23 11:20:54 +0900235
Thomas Abrahamc05c9462012-10-03 08:41:37 +0900236 clk_disable_unprepare(exynos_ohci->clk);
Jingoo Han62194242011-12-23 11:20:54 +0900237
238 usb_put_hcd(hcd);
Jingoo Han62194242011-12-23 11:20:54 +0900239
240 return 0;
241}
242
243static void exynos_ohci_shutdown(struct platform_device *pdev)
244{
245 struct exynos_ohci_hcd *exynos_ohci = platform_get_drvdata(pdev);
246 struct usb_hcd *hcd = exynos_ohci->hcd;
247
248 if (hcd->driver->shutdown)
249 hcd->driver->shutdown(hcd);
250}
251
252#ifdef CONFIG_PM
253static int exynos_ohci_suspend(struct device *dev)
254{
255 struct exynos_ohci_hcd *exynos_ohci = dev_get_drvdata(dev);
256 struct usb_hcd *hcd = exynos_ohci->hcd;
257 struct ohci_hcd *ohci = hcd_to_ohci(hcd);
Jingoo Han62194242011-12-23 11:20:54 +0900258 unsigned long flags;
259 int rc = 0;
260
261 /*
262 * Root hub was already suspended. Disable irq emission and
263 * mark HW unaccessible, bail out if RH has been resumed. Use
264 * the spinlock to properly synchronize with possible pending
265 * RH suspend or resume activity.
Jingoo Han62194242011-12-23 11:20:54 +0900266 */
267 spin_lock_irqsave(&ohci->lock, flags);
Jingoo Han2b4ffe32012-02-23 17:26:33 +0900268 if (ohci->rh_state != OHCI_RH_SUSPENDED &&
269 ohci->rh_state != OHCI_RH_HALTED) {
Jingoo Han62194242011-12-23 11:20:54 +0900270 rc = -EINVAL;
271 goto fail;
272 }
273
274 clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
275
Vivek Gautamed993bf2013-01-22 18:30:43 +0530276 if (exynos_ohci->otg)
277 exynos_ohci->otg->set_host(exynos_ohci->otg,
278 &exynos_ohci->hcd->self);
279
280 exynos_ohci_phy_disable(exynos_ohci);
Jingoo Hane864abe2012-06-28 16:49:42 +0900281
Thomas Abrahamc05c9462012-10-03 08:41:37 +0900282 clk_disable_unprepare(exynos_ohci->clk);
Jingoo Hane864abe2012-06-28 16:49:42 +0900283
Jingoo Han62194242011-12-23 11:20:54 +0900284fail:
285 spin_unlock_irqrestore(&ohci->lock, flags);
286
287 return rc;
288}
289
290static int exynos_ohci_resume(struct device *dev)
291{
292 struct exynos_ohci_hcd *exynos_ohci = dev_get_drvdata(dev);
293 struct usb_hcd *hcd = exynos_ohci->hcd;
Jingoo Han62194242011-12-23 11:20:54 +0900294
Thomas Abrahamc05c9462012-10-03 08:41:37 +0900295 clk_prepare_enable(exynos_ohci->clk);
Jingoo Hane864abe2012-06-28 16:49:42 +0900296
Vivek Gautamed993bf2013-01-22 18:30:43 +0530297 if (exynos_ohci->otg)
298 exynos_ohci->otg->set_host(exynos_ohci->otg,
299 &exynos_ohci->hcd->self);
300
301 exynos_ohci_phy_enable(exynos_ohci);
Jingoo Han62194242011-12-23 11:20:54 +0900302
Florian Fainellicfa49b42012-10-08 15:11:29 +0200303 ohci_resume(hcd, false);
Jingoo Han62194242011-12-23 11:20:54 +0900304
305 return 0;
306}
307#else
308#define exynos_ohci_suspend NULL
309#define exynos_ohci_resume NULL
310#endif
311
312static const struct dev_pm_ops exynos_ohci_pm_ops = {
313 .suspend = exynos_ohci_suspend,
314 .resume = exynos_ohci_resume,
315};
316
Vivek Gautamd5138932012-07-16 11:25:36 +0530317#ifdef CONFIG_OF
318static const struct of_device_id exynos_ohci_match[] = {
Vivek Gautam6e247772013-01-24 19:15:29 +0530319 { .compatible = "samsung,exynos4210-ohci" },
Thomas Abraham28717822013-04-11 17:12:30 +0530320 { .compatible = "samsung,exynos5440-ohci" },
Vivek Gautamd5138932012-07-16 11:25:36 +0530321 {},
322};
323MODULE_DEVICE_TABLE(of, exynos_ohci_match);
324#endif
325
Jingoo Han62194242011-12-23 11:20:54 +0900326static struct platform_driver exynos_ohci_driver = {
327 .probe = exynos_ohci_probe,
Bill Pemberton76904172012-11-19 13:21:08 -0500328 .remove = exynos_ohci_remove,
Jingoo Han62194242011-12-23 11:20:54 +0900329 .shutdown = exynos_ohci_shutdown,
330 .driver = {
331 .name = "exynos-ohci",
332 .owner = THIS_MODULE,
333 .pm = &exynos_ohci_pm_ops,
Vivek Gautamd5138932012-07-16 11:25:36 +0530334 .of_match_table = of_match_ptr(exynos_ohci_match),
Jingoo Han62194242011-12-23 11:20:54 +0900335 }
336};
337
338MODULE_ALIAS("platform:exynos-ohci");
339MODULE_AUTHOR("Jingoo Han <jg1.han@samsung.com>");