blob: 0bd6f47dfdc4f150088d4f9c565050b2770c23fe [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>
Jingoo Hanb8271852013-02-12 15:29:15 -080017#include <linux/platform_data/usb-ohci-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 Hanb8271852013-02-12 15:29:15 -080020
Jingoo Han62194242011-12-23 11:20:54 +090021#include <plat/usb-phy.h>
22
23struct exynos_ohci_hcd {
24 struct device *dev;
25 struct usb_hcd *hcd;
26 struct clk *clk;
Vivek Gautamed993bf2013-01-22 18:30:43 +053027 struct usb_phy *phy;
28 struct usb_otg *otg;
29 struct exynos4_ohci_platdata *pdata;
Jingoo Han62194242011-12-23 11:20:54 +090030};
31
Vivek Gautamed993bf2013-01-22 18:30:43 +053032static void exynos_ohci_phy_enable(struct exynos_ohci_hcd *exynos_ohci)
33{
34 struct platform_device *pdev = to_platform_device(exynos_ohci->dev);
35
36 if (exynos_ohci->phy)
37 usb_phy_init(exynos_ohci->phy);
38 else if (exynos_ohci->pdata->phy_init)
39 exynos_ohci->pdata->phy_init(pdev, USB_PHY_TYPE_HOST);
40}
41
42static void exynos_ohci_phy_disable(struct exynos_ohci_hcd *exynos_ohci)
43{
44 struct platform_device *pdev = to_platform_device(exynos_ohci->dev);
45
46 if (exynos_ohci->phy)
47 usb_phy_shutdown(exynos_ohci->phy);
48 else if (exynos_ohci->pdata->phy_exit)
49 exynos_ohci->pdata->phy_exit(pdev, USB_PHY_TYPE_HOST);
50}
51
Vincent Palatin57465102012-11-01 11:05:28 -070052static int ohci_exynos_reset(struct usb_hcd *hcd)
53{
54 return ohci_init(hcd_to_ohci(hcd));
55}
56
Jingoo Han62194242011-12-23 11:20:54 +090057static int ohci_exynos_start(struct usb_hcd *hcd)
58{
59 struct ohci_hcd *ohci = hcd_to_ohci(hcd);
60 int ret;
61
62 ohci_dbg(ohci, "ohci_exynos_start, ohci:%p", ohci);
63
Jingoo Han62194242011-12-23 11:20:54 +090064 ret = ohci_run(ohci);
65 if (ret < 0) {
Greg Kroah-Hartman5e415242012-04-27 11:24:41 -070066 dev_err(hcd->self.controller, "can't start %s\n",
67 hcd->self.bus_name);
Jingoo Han62194242011-12-23 11:20:54 +090068 ohci_stop(hcd);
69 return ret;
70 }
71
72 return 0;
73}
74
75static const struct hc_driver exynos_ohci_hc_driver = {
76 .description = hcd_name,
77 .product_desc = "EXYNOS OHCI Host Controller",
78 .hcd_priv_size = sizeof(struct ohci_hcd),
79
80 .irq = ohci_irq,
81 .flags = HCD_MEMORY|HCD_USB11,
82
Vincent Palatin57465102012-11-01 11:05:28 -070083 .reset = ohci_exynos_reset,
Jingoo Han62194242011-12-23 11:20:54 +090084 .start = ohci_exynos_start,
85 .stop = ohci_stop,
86 .shutdown = ohci_shutdown,
87
88 .get_frame_number = ohci_get_frame,
89
90 .urb_enqueue = ohci_urb_enqueue,
91 .urb_dequeue = ohci_urb_dequeue,
92 .endpoint_disable = ohci_endpoint_disable,
93
94 .hub_status_data = ohci_hub_status_data,
95 .hub_control = ohci_hub_control,
96#ifdef CONFIG_PM
97 .bus_suspend = ohci_bus_suspend,
98 .bus_resume = ohci_bus_resume,
99#endif
100 .start_port_reset = ohci_start_port_reset,
101};
102
Vivek Gautamd5138932012-07-16 11:25:36 +0530103static u64 ohci_exynos_dma_mask = DMA_BIT_MASK(32);
104
Bill Pemberton41ac7b32012-11-19 13:21:48 -0500105static int exynos_ohci_probe(struct platform_device *pdev)
Jingoo Han62194242011-12-23 11:20:54 +0900106{
Vivek Gautamed993bf2013-01-22 18:30:43 +0530107 struct exynos4_ohci_platdata *pdata = pdev->dev.platform_data;
Jingoo Han62194242011-12-23 11:20:54 +0900108 struct exynos_ohci_hcd *exynos_ohci;
109 struct usb_hcd *hcd;
110 struct ohci_hcd *ohci;
111 struct resource *res;
Vivek Gautamed993bf2013-01-22 18:30:43 +0530112 struct usb_phy *phy;
Jingoo Han62194242011-12-23 11:20:54 +0900113 int irq;
114 int err;
115
Vivek Gautamd5138932012-07-16 11:25:36 +0530116 /*
117 * Right now device-tree probed devices don't get dma_mask set.
118 * Since shared usb code relies on it, set it here for now.
119 * Once we move to full device tree support this will vanish off.
120 */
121 if (!pdev->dev.dma_mask)
122 pdev->dev.dma_mask = &ohci_exynos_dma_mask;
123 if (!pdev->dev.coherent_dma_mask)
124 pdev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
125
Jingoo Han390a0a72012-06-28 16:30:30 +0900126 exynos_ohci = devm_kzalloc(&pdev->dev, sizeof(struct exynos_ohci_hcd),
127 GFP_KERNEL);
Jingoo Han62194242011-12-23 11:20:54 +0900128 if (!exynos_ohci)
129 return -ENOMEM;
130
Vivek Gautamed993bf2013-01-22 18:30:43 +0530131 phy = devm_usb_get_phy(&pdev->dev, USB_PHY_TYPE_USB2);
132 if (IS_ERR_OR_NULL(phy)) {
133 /* Fallback to pdata */
134 if (!pdata) {
135 dev_warn(&pdev->dev, "no platform data or transceiver defined\n");
136 return -EPROBE_DEFER;
137 } else {
138 exynos_ohci->pdata = pdata;
139 }
140 } else {
141 exynos_ohci->phy = phy;
142 exynos_ohci->otg = phy->otg;
143 }
144
Jingoo Han62194242011-12-23 11:20:54 +0900145 exynos_ohci->dev = &pdev->dev;
146
147 hcd = usb_create_hcd(&exynos_ohci_hc_driver, &pdev->dev,
148 dev_name(&pdev->dev));
149 if (!hcd) {
150 dev_err(&pdev->dev, "Unable to create HCD\n");
Jingoo Han390a0a72012-06-28 16:30:30 +0900151 return -ENOMEM;
Jingoo Han62194242011-12-23 11:20:54 +0900152 }
153
154 exynos_ohci->hcd = hcd;
Jingoo Han60d80ad2012-10-04 16:11:50 +0900155 exynos_ohci->clk = devm_clk_get(&pdev->dev, "usbhost");
Jingoo Han62194242011-12-23 11:20:54 +0900156
157 if (IS_ERR(exynos_ohci->clk)) {
158 dev_err(&pdev->dev, "Failed to get usbhost clock\n");
159 err = PTR_ERR(exynos_ohci->clk);
160 goto fail_clk;
161 }
162
Thomas Abrahamc05c9462012-10-03 08:41:37 +0900163 err = clk_prepare_enable(exynos_ohci->clk);
Jingoo Han62194242011-12-23 11:20:54 +0900164 if (err)
Jingoo Han60d80ad2012-10-04 16:11:50 +0900165 goto fail_clk;
Jingoo Han62194242011-12-23 11:20:54 +0900166
167 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
168 if (!res) {
169 dev_err(&pdev->dev, "Failed to get I/O memory\n");
170 err = -ENXIO;
171 goto fail_io;
172 }
173
174 hcd->rsrc_start = res->start;
175 hcd->rsrc_len = resource_size(res);
Jingoo Han390a0a72012-06-28 16:30:30 +0900176 hcd->regs = devm_ioremap(&pdev->dev, res->start, hcd->rsrc_len);
Jingoo Han62194242011-12-23 11:20:54 +0900177 if (!hcd->regs) {
178 dev_err(&pdev->dev, "Failed to remap I/O memory\n");
179 err = -ENOMEM;
180 goto fail_io;
181 }
182
183 irq = platform_get_irq(pdev, 0);
184 if (!irq) {
185 dev_err(&pdev->dev, "Failed to get IRQ\n");
186 err = -ENODEV;
Jingoo Han390a0a72012-06-28 16:30:30 +0900187 goto fail_io;
Jingoo Han62194242011-12-23 11:20:54 +0900188 }
189
Vivek Gautamed993bf2013-01-22 18:30:43 +0530190 if (exynos_ohci->otg)
191 exynos_ohci->otg->set_host(exynos_ohci->otg,
192 &exynos_ohci->hcd->self);
193
194 exynos_ohci_phy_enable(exynos_ohci);
Jingoo Han62194242011-12-23 11:20:54 +0900195
196 ohci = hcd_to_ohci(hcd);
197 ohci_hcd_init(ohci);
198
199 err = usb_add_hcd(hcd, irq, IRQF_SHARED);
200 if (err) {
201 dev_err(&pdev->dev, "Failed to add USB HCD\n");
Vivek Gautamed993bf2013-01-22 18:30:43 +0530202 goto fail_add_hcd;
Jingoo Han62194242011-12-23 11:20:54 +0900203 }
204
205 platform_set_drvdata(pdev, exynos_ohci);
206
207 return 0;
208
Vivek Gautamed993bf2013-01-22 18:30:43 +0530209fail_add_hcd:
210 exynos_ohci_phy_disable(exynos_ohci);
Jingoo Han62194242011-12-23 11:20:54 +0900211fail_io:
Thomas Abrahamc05c9462012-10-03 08:41:37 +0900212 clk_disable_unprepare(exynos_ohci->clk);
Jingoo Han62194242011-12-23 11:20:54 +0900213fail_clk:
214 usb_put_hcd(hcd);
Jingoo Han62194242011-12-23 11:20:54 +0900215 return err;
216}
217
Bill Pembertonfb4e98a2012-11-19 13:26:20 -0500218static int exynos_ohci_remove(struct platform_device *pdev)
Jingoo Han62194242011-12-23 11:20:54 +0900219{
Jingoo Han62194242011-12-23 11:20:54 +0900220 struct exynos_ohci_hcd *exynos_ohci = platform_get_drvdata(pdev);
221 struct usb_hcd *hcd = exynos_ohci->hcd;
222
223 usb_remove_hcd(hcd);
224
Vivek Gautamed993bf2013-01-22 18:30:43 +0530225 if (exynos_ohci->otg)
226 exynos_ohci->otg->set_host(exynos_ohci->otg,
227 &exynos_ohci->hcd->self);
228
229 exynos_ohci_phy_disable(exynos_ohci);
Jingoo Han62194242011-12-23 11:20:54 +0900230
Thomas Abrahamc05c9462012-10-03 08:41:37 +0900231 clk_disable_unprepare(exynos_ohci->clk);
Jingoo Han62194242011-12-23 11:20:54 +0900232
233 usb_put_hcd(hcd);
Jingoo Han62194242011-12-23 11:20:54 +0900234
235 return 0;
236}
237
238static void exynos_ohci_shutdown(struct platform_device *pdev)
239{
240 struct exynos_ohci_hcd *exynos_ohci = platform_get_drvdata(pdev);
241 struct usb_hcd *hcd = exynos_ohci->hcd;
242
243 if (hcd->driver->shutdown)
244 hcd->driver->shutdown(hcd);
245}
246
247#ifdef CONFIG_PM
248static int exynos_ohci_suspend(struct device *dev)
249{
250 struct exynos_ohci_hcd *exynos_ohci = dev_get_drvdata(dev);
251 struct usb_hcd *hcd = exynos_ohci->hcd;
252 struct ohci_hcd *ohci = hcd_to_ohci(hcd);
Jingoo Han62194242011-12-23 11:20:54 +0900253 unsigned long flags;
254 int rc = 0;
255
256 /*
257 * Root hub was already suspended. Disable irq emission and
258 * mark HW unaccessible, bail out if RH has been resumed. Use
259 * the spinlock to properly synchronize with possible pending
260 * RH suspend or resume activity.
Jingoo Han62194242011-12-23 11:20:54 +0900261 */
262 spin_lock_irqsave(&ohci->lock, flags);
Jingoo Han2b4ffe32012-02-23 17:26:33 +0900263 if (ohci->rh_state != OHCI_RH_SUSPENDED &&
264 ohci->rh_state != OHCI_RH_HALTED) {
Jingoo Han62194242011-12-23 11:20:54 +0900265 rc = -EINVAL;
266 goto fail;
267 }
268
269 clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
270
Vivek Gautamed993bf2013-01-22 18:30:43 +0530271 if (exynos_ohci->otg)
272 exynos_ohci->otg->set_host(exynos_ohci->otg,
273 &exynos_ohci->hcd->self);
274
275 exynos_ohci_phy_disable(exynos_ohci);
Jingoo Hane864abe2012-06-28 16:49:42 +0900276
Thomas Abrahamc05c9462012-10-03 08:41:37 +0900277 clk_disable_unprepare(exynos_ohci->clk);
Jingoo Hane864abe2012-06-28 16:49:42 +0900278
Jingoo Han62194242011-12-23 11:20:54 +0900279fail:
280 spin_unlock_irqrestore(&ohci->lock, flags);
281
282 return rc;
283}
284
285static int exynos_ohci_resume(struct device *dev)
286{
287 struct exynos_ohci_hcd *exynos_ohci = dev_get_drvdata(dev);
288 struct usb_hcd *hcd = exynos_ohci->hcd;
Jingoo Han62194242011-12-23 11:20:54 +0900289
Thomas Abrahamc05c9462012-10-03 08:41:37 +0900290 clk_prepare_enable(exynos_ohci->clk);
Jingoo Hane864abe2012-06-28 16:49:42 +0900291
Vivek Gautamed993bf2013-01-22 18:30:43 +0530292 if (exynos_ohci->otg)
293 exynos_ohci->otg->set_host(exynos_ohci->otg,
294 &exynos_ohci->hcd->self);
295
296 exynos_ohci_phy_enable(exynos_ohci);
Jingoo Han62194242011-12-23 11:20:54 +0900297
Florian Fainellicfa49b42012-10-08 15:11:29 +0200298 ohci_resume(hcd, false);
Jingoo Han62194242011-12-23 11:20:54 +0900299
300 return 0;
301}
302#else
303#define exynos_ohci_suspend NULL
304#define exynos_ohci_resume NULL
305#endif
306
307static const struct dev_pm_ops exynos_ohci_pm_ops = {
308 .suspend = exynos_ohci_suspend,
309 .resume = exynos_ohci_resume,
310};
311
Vivek Gautamd5138932012-07-16 11:25:36 +0530312#ifdef CONFIG_OF
313static const struct of_device_id exynos_ohci_match[] = {
Vivek Gautam6e247772013-01-24 19:15:29 +0530314 { .compatible = "samsung,exynos4210-ohci" },
Vivek Gautamd5138932012-07-16 11:25:36 +0530315 {},
316};
317MODULE_DEVICE_TABLE(of, exynos_ohci_match);
318#endif
319
Jingoo Han62194242011-12-23 11:20:54 +0900320static struct platform_driver exynos_ohci_driver = {
321 .probe = exynos_ohci_probe,
Bill Pemberton76904172012-11-19 13:21:08 -0500322 .remove = exynos_ohci_remove,
Jingoo Han62194242011-12-23 11:20:54 +0900323 .shutdown = exynos_ohci_shutdown,
324 .driver = {
325 .name = "exynos-ohci",
326 .owner = THIS_MODULE,
327 .pm = &exynos_ohci_pm_ops,
Vivek Gautamd5138932012-07-16 11:25:36 +0530328 .of_match_table = of_match_ptr(exynos_ohci_match),
Jingoo Han62194242011-12-23 11:20:54 +0900329 }
330};
331
332MODULE_ALIAS("platform:exynos-ohci");
333MODULE_AUTHOR("Jingoo Han <jg1.han@samsung.com>");