blob: 114583a8e92b36dec178cf91afc7b9003aa37ddb [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
21struct exynos_ohci_hcd {
22 struct device *dev;
23 struct usb_hcd *hcd;
24 struct clk *clk;
Vivek Gautamed993bf2013-01-22 18:30:43 +053025 struct usb_phy *phy;
26 struct usb_otg *otg;
27 struct exynos4_ohci_platdata *pdata;
Jingoo Han62194242011-12-23 11:20:54 +090028};
29
Vivek Gautamed993bf2013-01-22 18:30:43 +053030static void exynos_ohci_phy_enable(struct exynos_ohci_hcd *exynos_ohci)
31{
32 struct platform_device *pdev = to_platform_device(exynos_ohci->dev);
33
34 if (exynos_ohci->phy)
35 usb_phy_init(exynos_ohci->phy);
Thomas Abraham28717822013-04-11 17:12:30 +053036 else if (exynos_ohci->pdata && exynos_ohci->pdata->phy_init)
Vivek Gautamed993bf2013-01-22 18:30:43 +053037 exynos_ohci->pdata->phy_init(pdev, USB_PHY_TYPE_HOST);
38}
39
40static void exynos_ohci_phy_disable(struct exynos_ohci_hcd *exynos_ohci)
41{
42 struct platform_device *pdev = to_platform_device(exynos_ohci->dev);
43
44 if (exynos_ohci->phy)
45 usb_phy_shutdown(exynos_ohci->phy);
Thomas Abraham28717822013-04-11 17:12:30 +053046 else if (exynos_ohci->pdata && exynos_ohci->pdata->phy_exit)
Vivek Gautamed993bf2013-01-22 18:30:43 +053047 exynos_ohci->pdata->phy_exit(pdev, USB_PHY_TYPE_HOST);
48}
49
Vincent Palatin57465102012-11-01 11:05:28 -070050static int ohci_exynos_reset(struct usb_hcd *hcd)
51{
52 return ohci_init(hcd_to_ohci(hcd));
53}
54
Jingoo Han62194242011-12-23 11:20:54 +090055static int ohci_exynos_start(struct usb_hcd *hcd)
56{
57 struct ohci_hcd *ohci = hcd_to_ohci(hcd);
58 int ret;
59
60 ohci_dbg(ohci, "ohci_exynos_start, ohci:%p", ohci);
61
Jingoo Han62194242011-12-23 11:20:54 +090062 ret = ohci_run(ohci);
63 if (ret < 0) {
Greg Kroah-Hartman5e415242012-04-27 11:24:41 -070064 dev_err(hcd->self.controller, "can't start %s\n",
65 hcd->self.bus_name);
Jingoo Han62194242011-12-23 11:20:54 +090066 ohci_stop(hcd);
67 return ret;
68 }
69
70 return 0;
71}
72
73static const struct hc_driver exynos_ohci_hc_driver = {
74 .description = hcd_name,
75 .product_desc = "EXYNOS OHCI Host Controller",
76 .hcd_priv_size = sizeof(struct ohci_hcd),
77
78 .irq = ohci_irq,
79 .flags = HCD_MEMORY|HCD_USB11,
80
Vincent Palatin57465102012-11-01 11:05:28 -070081 .reset = ohci_exynos_reset,
Jingoo Han62194242011-12-23 11:20:54 +090082 .start = ohci_exynos_start,
83 .stop = ohci_stop,
84 .shutdown = ohci_shutdown,
85
86 .get_frame_number = ohci_get_frame,
87
88 .urb_enqueue = ohci_urb_enqueue,
89 .urb_dequeue = ohci_urb_dequeue,
90 .endpoint_disable = ohci_endpoint_disable,
91
92 .hub_status_data = ohci_hub_status_data,
93 .hub_control = ohci_hub_control,
94#ifdef CONFIG_PM
95 .bus_suspend = ohci_bus_suspend,
96 .bus_resume = ohci_bus_resume,
97#endif
98 .start_port_reset = ohci_start_port_reset,
99};
100
Vivek Gautamd5138932012-07-16 11:25:36 +0530101static u64 ohci_exynos_dma_mask = DMA_BIT_MASK(32);
102
Bill Pemberton41ac7b32012-11-19 13:21:48 -0500103static int exynos_ohci_probe(struct platform_device *pdev)
Jingoo Han62194242011-12-23 11:20:54 +0900104{
Vivek Gautamed993bf2013-01-22 18:30:43 +0530105 struct exynos4_ohci_platdata *pdata = pdev->dev.platform_data;
Jingoo Han62194242011-12-23 11:20:54 +0900106 struct exynos_ohci_hcd *exynos_ohci;
107 struct usb_hcd *hcd;
108 struct ohci_hcd *ohci;
109 struct resource *res;
Vivek Gautamed993bf2013-01-22 18:30:43 +0530110 struct usb_phy *phy;
Jingoo Han62194242011-12-23 11:20:54 +0900111 int irq;
112 int err;
113
Vivek Gautamd5138932012-07-16 11:25:36 +0530114 /*
115 * Right now device-tree probed devices don't get dma_mask set.
116 * Since shared usb code relies on it, set it here for now.
117 * Once we move to full device tree support this will vanish off.
118 */
119 if (!pdev->dev.dma_mask)
120 pdev->dev.dma_mask = &ohci_exynos_dma_mask;
121 if (!pdev->dev.coherent_dma_mask)
122 pdev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
123
Jingoo Han390a0a72012-06-28 16:30:30 +0900124 exynos_ohci = devm_kzalloc(&pdev->dev, sizeof(struct exynos_ohci_hcd),
125 GFP_KERNEL);
Jingoo Han62194242011-12-23 11:20:54 +0900126 if (!exynos_ohci)
127 return -ENOMEM;
128
Thomas Abraham28717822013-04-11 17:12:30 +0530129 if (of_device_is_compatible(pdev->dev.of_node,
130 "samsung,exynos5440-ohci"))
131 goto skip_phy;
132
Vivek Gautamed993bf2013-01-22 18:30:43 +0530133 phy = devm_usb_get_phy(&pdev->dev, USB_PHY_TYPE_USB2);
Felipe Balbi9ee1c7f2013-03-15 11:05:03 +0200134 if (IS_ERR(phy)) {
Vivek Gautamed993bf2013-01-22 18:30:43 +0530135 /* Fallback to pdata */
136 if (!pdata) {
137 dev_warn(&pdev->dev, "no platform data or transceiver defined\n");
138 return -EPROBE_DEFER;
139 } else {
140 exynos_ohci->pdata = pdata;
141 }
142 } else {
143 exynos_ohci->phy = phy;
144 exynos_ohci->otg = phy->otg;
145 }
146
Thomas Abraham28717822013-04-11 17:12:30 +0530147skip_phy:
148
Jingoo Han62194242011-12-23 11:20:54 +0900149 exynos_ohci->dev = &pdev->dev;
150
151 hcd = usb_create_hcd(&exynos_ohci_hc_driver, &pdev->dev,
152 dev_name(&pdev->dev));
153 if (!hcd) {
154 dev_err(&pdev->dev, "Unable to create HCD\n");
Jingoo Han390a0a72012-06-28 16:30:30 +0900155 return -ENOMEM;
Jingoo Han62194242011-12-23 11:20:54 +0900156 }
157
158 exynos_ohci->hcd = hcd;
Jingoo Han60d80ad2012-10-04 16:11:50 +0900159 exynos_ohci->clk = devm_clk_get(&pdev->dev, "usbhost");
Jingoo Han62194242011-12-23 11:20:54 +0900160
161 if (IS_ERR(exynos_ohci->clk)) {
162 dev_err(&pdev->dev, "Failed to get usbhost clock\n");
163 err = PTR_ERR(exynos_ohci->clk);
164 goto fail_clk;
165 }
166
Thomas Abrahamc05c9462012-10-03 08:41:37 +0900167 err = clk_prepare_enable(exynos_ohci->clk);
Jingoo Han62194242011-12-23 11:20:54 +0900168 if (err)
Jingoo Han60d80ad2012-10-04 16:11:50 +0900169 goto fail_clk;
Jingoo Han62194242011-12-23 11:20:54 +0900170
171 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
172 if (!res) {
173 dev_err(&pdev->dev, "Failed to get I/O memory\n");
174 err = -ENXIO;
175 goto fail_io;
176 }
177
178 hcd->rsrc_start = res->start;
179 hcd->rsrc_len = resource_size(res);
Jingoo Han390a0a72012-06-28 16:30:30 +0900180 hcd->regs = devm_ioremap(&pdev->dev, res->start, hcd->rsrc_len);
Jingoo Han62194242011-12-23 11:20:54 +0900181 if (!hcd->regs) {
182 dev_err(&pdev->dev, "Failed to remap I/O memory\n");
183 err = -ENOMEM;
184 goto fail_io;
185 }
186
187 irq = platform_get_irq(pdev, 0);
188 if (!irq) {
189 dev_err(&pdev->dev, "Failed to get IRQ\n");
190 err = -ENODEV;
Jingoo Han390a0a72012-06-28 16:30:30 +0900191 goto fail_io;
Jingoo Han62194242011-12-23 11:20:54 +0900192 }
193
Vivek Gautamed993bf2013-01-22 18:30:43 +0530194 if (exynos_ohci->otg)
195 exynos_ohci->otg->set_host(exynos_ohci->otg,
196 &exynos_ohci->hcd->self);
197
198 exynos_ohci_phy_enable(exynos_ohci);
Jingoo Han62194242011-12-23 11:20:54 +0900199
200 ohci = hcd_to_ohci(hcd);
201 ohci_hcd_init(ohci);
202
203 err = usb_add_hcd(hcd, irq, IRQF_SHARED);
204 if (err) {
205 dev_err(&pdev->dev, "Failed to add USB HCD\n");
Vivek Gautamed993bf2013-01-22 18:30:43 +0530206 goto fail_add_hcd;
Jingoo Han62194242011-12-23 11:20:54 +0900207 }
208
209 platform_set_drvdata(pdev, exynos_ohci);
210
211 return 0;
212
Vivek Gautamed993bf2013-01-22 18:30:43 +0530213fail_add_hcd:
214 exynos_ohci_phy_disable(exynos_ohci);
Jingoo Han62194242011-12-23 11:20:54 +0900215fail_io:
Thomas Abrahamc05c9462012-10-03 08:41:37 +0900216 clk_disable_unprepare(exynos_ohci->clk);
Jingoo Han62194242011-12-23 11:20:54 +0900217fail_clk:
218 usb_put_hcd(hcd);
Jingoo Han62194242011-12-23 11:20:54 +0900219 return err;
220}
221
Bill Pembertonfb4e98a2012-11-19 13:26:20 -0500222static int exynos_ohci_remove(struct platform_device *pdev)
Jingoo Han62194242011-12-23 11:20:54 +0900223{
Jingoo Han62194242011-12-23 11:20:54 +0900224 struct exynos_ohci_hcd *exynos_ohci = platform_get_drvdata(pdev);
225 struct usb_hcd *hcd = exynos_ohci->hcd;
226
227 usb_remove_hcd(hcd);
228
Vivek Gautamed993bf2013-01-22 18:30:43 +0530229 if (exynos_ohci->otg)
230 exynos_ohci->otg->set_host(exynos_ohci->otg,
231 &exynos_ohci->hcd->self);
232
233 exynos_ohci_phy_disable(exynos_ohci);
Jingoo Han62194242011-12-23 11:20:54 +0900234
Thomas Abrahamc05c9462012-10-03 08:41:37 +0900235 clk_disable_unprepare(exynos_ohci->clk);
Jingoo Han62194242011-12-23 11:20:54 +0900236
237 usb_put_hcd(hcd);
Jingoo Han62194242011-12-23 11:20:54 +0900238
239 return 0;
240}
241
242static void exynos_ohci_shutdown(struct platform_device *pdev)
243{
244 struct exynos_ohci_hcd *exynos_ohci = platform_get_drvdata(pdev);
245 struct usb_hcd *hcd = exynos_ohci->hcd;
246
247 if (hcd->driver->shutdown)
248 hcd->driver->shutdown(hcd);
249}
250
251#ifdef CONFIG_PM
252static int exynos_ohci_suspend(struct device *dev)
253{
254 struct exynos_ohci_hcd *exynos_ohci = dev_get_drvdata(dev);
255 struct usb_hcd *hcd = exynos_ohci->hcd;
256 struct ohci_hcd *ohci = hcd_to_ohci(hcd);
Jingoo Han62194242011-12-23 11:20:54 +0900257 unsigned long flags;
258 int rc = 0;
259
260 /*
261 * Root hub was already suspended. Disable irq emission and
262 * mark HW unaccessible, bail out if RH has been resumed. Use
263 * the spinlock to properly synchronize with possible pending
264 * RH suspend or resume activity.
Jingoo Han62194242011-12-23 11:20:54 +0900265 */
266 spin_lock_irqsave(&ohci->lock, flags);
Jingoo Han2b4ffe32012-02-23 17:26:33 +0900267 if (ohci->rh_state != OHCI_RH_SUSPENDED &&
268 ohci->rh_state != OHCI_RH_HALTED) {
Jingoo Han62194242011-12-23 11:20:54 +0900269 rc = -EINVAL;
270 goto fail;
271 }
272
273 clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
274
Vivek Gautamed993bf2013-01-22 18:30:43 +0530275 if (exynos_ohci->otg)
276 exynos_ohci->otg->set_host(exynos_ohci->otg,
277 &exynos_ohci->hcd->self);
278
279 exynos_ohci_phy_disable(exynos_ohci);
Jingoo Hane864abe2012-06-28 16:49:42 +0900280
Thomas Abrahamc05c9462012-10-03 08:41:37 +0900281 clk_disable_unprepare(exynos_ohci->clk);
Jingoo Hane864abe2012-06-28 16:49:42 +0900282
Jingoo Han62194242011-12-23 11:20:54 +0900283fail:
284 spin_unlock_irqrestore(&ohci->lock, flags);
285
286 return rc;
287}
288
289static int exynos_ohci_resume(struct device *dev)
290{
291 struct exynos_ohci_hcd *exynos_ohci = dev_get_drvdata(dev);
292 struct usb_hcd *hcd = exynos_ohci->hcd;
Jingoo Han62194242011-12-23 11:20:54 +0900293
Thomas Abrahamc05c9462012-10-03 08:41:37 +0900294 clk_prepare_enable(exynos_ohci->clk);
Jingoo Hane864abe2012-06-28 16:49:42 +0900295
Vivek Gautamed993bf2013-01-22 18:30:43 +0530296 if (exynos_ohci->otg)
297 exynos_ohci->otg->set_host(exynos_ohci->otg,
298 &exynos_ohci->hcd->self);
299
300 exynos_ohci_phy_enable(exynos_ohci);
Jingoo Han62194242011-12-23 11:20:54 +0900301
Florian Fainellicfa49b42012-10-08 15:11:29 +0200302 ohci_resume(hcd, false);
Jingoo Han62194242011-12-23 11:20:54 +0900303
304 return 0;
305}
306#else
307#define exynos_ohci_suspend NULL
308#define exynos_ohci_resume NULL
309#endif
310
311static const struct dev_pm_ops exynos_ohci_pm_ops = {
312 .suspend = exynos_ohci_suspend,
313 .resume = exynos_ohci_resume,
314};
315
Vivek Gautamd5138932012-07-16 11:25:36 +0530316#ifdef CONFIG_OF
317static const struct of_device_id exynos_ohci_match[] = {
Vivek Gautam6e247772013-01-24 19:15:29 +0530318 { .compatible = "samsung,exynos4210-ohci" },
Thomas Abraham28717822013-04-11 17:12:30 +0530319 { .compatible = "samsung,exynos5440-ohci" },
Vivek Gautamd5138932012-07-16 11:25:36 +0530320 {},
321};
322MODULE_DEVICE_TABLE(of, exynos_ohci_match);
323#endif
324
Jingoo Han62194242011-12-23 11:20:54 +0900325static struct platform_driver exynos_ohci_driver = {
326 .probe = exynos_ohci_probe,
Bill Pemberton76904172012-11-19 13:21:08 -0500327 .remove = exynos_ohci_remove,
Jingoo Han62194242011-12-23 11:20:54 +0900328 .shutdown = exynos_ohci_shutdown,
329 .driver = {
330 .name = "exynos-ohci",
331 .owner = THIS_MODULE,
332 .pm = &exynos_ohci_pm_ops,
Vivek Gautamd5138932012-07-16 11:25:36 +0530333 .of_match_table = of_match_ptr(exynos_ohci_match),
Jingoo Han62194242011-12-23 11:20:54 +0900334 }
335};
336
337MODULE_ALIAS("platform:exynos-ohci");
338MODULE_AUTHOR("Jingoo Han <jg1.han@samsung.com>");