blob: 91ec9b2cd37868f600eca4dd41cdf6180eed806e [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>
Manjunath Goudar50a97e02013-09-21 16:38:38 +053015#include <linux/dma-mapping.h>
16#include <linux/io.h>
17#include <linux/kernel.h>
18#include <linux/module.h>
Vivek Gautamd5138932012-07-16 11:25:36 +053019#include <linux/of.h>
Jingoo Han62194242011-12-23 11:20:54 +090020#include <linux/platform_device.h>
Vivek Gautamed993bf2013-01-22 18:30:43 +053021#include <linux/usb/phy.h>
Vivek Gautamb506eeb2013-01-22 18:30:40 +053022#include <linux/usb/samsung_usb_phy.h>
Manjunath Goudar50a97e02013-09-21 16:38:38 +053023#include <linux/usb.h>
24#include <linux/usb/hcd.h>
25#include <linux/usb/otg.h>
26
27#include "ohci.h"
28
29#define DRIVER_DESC "OHCI EXYNOS driver"
30
31static const char hcd_name[] = "ohci-exynos";
32static struct hc_driver __read_mostly exynos_ohci_hc_driver;
33
34#define to_exynos_ohci(hcd) (struct exynos_ohci_hcd *)(hcd_to_ohci(hcd)->priv)
Jingoo Han62194242011-12-23 11:20:54 +090035
36struct exynos_ohci_hcd {
Jingoo Han62194242011-12-23 11:20:54 +090037 struct clk *clk;
Vivek Gautamed993bf2013-01-22 18:30:43 +053038 struct usb_phy *phy;
39 struct usb_otg *otg;
Jingoo Han62194242011-12-23 11:20:54 +090040};
41
Manjunath Goudar50a97e02013-09-21 16:38:38 +053042static void exynos_ohci_phy_enable(struct platform_device *pdev)
Vivek Gautamed993bf2013-01-22 18:30:43 +053043{
Manjunath Goudar50a97e02013-09-21 16:38:38 +053044 struct usb_hcd *hcd = platform_get_drvdata(pdev);
45 struct exynos_ohci_hcd *exynos_ohci = to_exynos_ohci(hcd);
Vivek Gautamed993bf2013-01-22 18:30:43 +053046
47 if (exynos_ohci->phy)
48 usb_phy_init(exynos_ohci->phy);
Vivek Gautamed993bf2013-01-22 18:30:43 +053049}
50
Manjunath Goudar50a97e02013-09-21 16:38:38 +053051static void exynos_ohci_phy_disable(struct platform_device *pdev)
Vivek Gautamed993bf2013-01-22 18:30:43 +053052{
Manjunath Goudar50a97e02013-09-21 16:38:38 +053053 struct usb_hcd *hcd = platform_get_drvdata(pdev);
54 struct exynos_ohci_hcd *exynos_ohci = to_exynos_ohci(hcd);
Vivek Gautamed993bf2013-01-22 18:30:43 +053055
56 if (exynos_ohci->phy)
57 usb_phy_shutdown(exynos_ohci->phy);
Vivek Gautamed993bf2013-01-22 18:30:43 +053058}
59
Bill Pemberton41ac7b32012-11-19 13:21:48 -050060static int exynos_ohci_probe(struct platform_device *pdev)
Jingoo Han62194242011-12-23 11:20:54 +090061{
Jingoo Han62194242011-12-23 11:20:54 +090062 struct exynos_ohci_hcd *exynos_ohci;
63 struct usb_hcd *hcd;
Jingoo Han62194242011-12-23 11:20:54 +090064 struct resource *res;
Vivek Gautamed993bf2013-01-22 18:30:43 +053065 struct usb_phy *phy;
Jingoo Han62194242011-12-23 11:20:54 +090066 int irq;
67 int err;
68
Vivek Gautamd5138932012-07-16 11:25:36 +053069 /*
70 * Right now device-tree probed devices don't get dma_mask set.
71 * Since shared usb code relies on it, set it here for now.
72 * Once we move to full device tree support this will vanish off.
73 */
Russell Kinge1fd7342013-06-27 12:36:37 +010074 err = dma_coerce_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
Russell King22d9d8e2013-06-10 16:28:49 +010075 if (err)
76 return err;
Vivek Gautamd5138932012-07-16 11:25:36 +053077
Manjunath Goudar50a97e02013-09-21 16:38:38 +053078 hcd = usb_create_hcd(&exynos_ohci_hc_driver,
79 &pdev->dev, dev_name(&pdev->dev));
80 if (!hcd) {
81 dev_err(&pdev->dev, "Unable to create HCD\n");
Jingoo Han62194242011-12-23 11:20:54 +090082 return -ENOMEM;
Manjunath Goudar50a97e02013-09-21 16:38:38 +053083 }
84
85 exynos_ohci = to_exynos_ohci(hcd);
Jingoo Han62194242011-12-23 11:20:54 +090086
Thomas Abraham28717822013-04-11 17:12:30 +053087 if (of_device_is_compatible(pdev->dev.of_node,
88 "samsung,exynos5440-ohci"))
89 goto skip_phy;
90
Vivek Gautamed993bf2013-01-22 18:30:43 +053091 phy = devm_usb_get_phy(&pdev->dev, USB_PHY_TYPE_USB2);
Felipe Balbi9ee1c7f2013-03-15 11:05:03 +020092 if (IS_ERR(phy)) {
Jingoo Hanc00809d2013-10-14 09:18:54 +090093 usb_put_hcd(hcd);
94 dev_warn(&pdev->dev, "no platform data or transceiver defined\n");
95 return -EPROBE_DEFER;
Vivek Gautamed993bf2013-01-22 18:30:43 +053096 } else {
97 exynos_ohci->phy = phy;
98 exynos_ohci->otg = phy->otg;
99 }
100
Thomas Abraham28717822013-04-11 17:12:30 +0530101skip_phy:
Jingoo Han60d80ad2012-10-04 16:11:50 +0900102 exynos_ohci->clk = devm_clk_get(&pdev->dev, "usbhost");
Jingoo Han62194242011-12-23 11:20:54 +0900103
104 if (IS_ERR(exynos_ohci->clk)) {
105 dev_err(&pdev->dev, "Failed to get usbhost clock\n");
106 err = PTR_ERR(exynos_ohci->clk);
107 goto fail_clk;
108 }
109
Thomas Abrahamc05c9462012-10-03 08:41:37 +0900110 err = clk_prepare_enable(exynos_ohci->clk);
Jingoo Han62194242011-12-23 11:20:54 +0900111 if (err)
Jingoo Han60d80ad2012-10-04 16:11:50 +0900112 goto fail_clk;
Jingoo Han62194242011-12-23 11:20:54 +0900113
114 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
115 if (!res) {
116 dev_err(&pdev->dev, "Failed to get I/O memory\n");
117 err = -ENXIO;
118 goto fail_io;
119 }
120
121 hcd->rsrc_start = res->start;
122 hcd->rsrc_len = resource_size(res);
Jingoo Han390a0a72012-06-28 16:30:30 +0900123 hcd->regs = devm_ioremap(&pdev->dev, res->start, hcd->rsrc_len);
Jingoo Han62194242011-12-23 11:20:54 +0900124 if (!hcd->regs) {
125 dev_err(&pdev->dev, "Failed to remap I/O memory\n");
126 err = -ENOMEM;
127 goto fail_io;
128 }
129
130 irq = platform_get_irq(pdev, 0);
131 if (!irq) {
132 dev_err(&pdev->dev, "Failed to get IRQ\n");
133 err = -ENODEV;
Jingoo Han390a0a72012-06-28 16:30:30 +0900134 goto fail_io;
Jingoo Han62194242011-12-23 11:20:54 +0900135 }
136
Vivek Gautamed993bf2013-01-22 18:30:43 +0530137 if (exynos_ohci->otg)
Manjunath Goudar50a97e02013-09-21 16:38:38 +0530138 exynos_ohci->otg->set_host(exynos_ohci->otg, &hcd->self);
Vivek Gautamed993bf2013-01-22 18:30:43 +0530139
Manjunath Goudar50a97e02013-09-21 16:38:38 +0530140 platform_set_drvdata(pdev, hcd);
Jingoo Han62194242011-12-23 11:20:54 +0900141
Manjunath Goudar50a97e02013-09-21 16:38:38 +0530142 exynos_ohci_phy_enable(pdev);
Jingoo Han62194242011-12-23 11:20:54 +0900143
144 err = usb_add_hcd(hcd, irq, IRQF_SHARED);
145 if (err) {
146 dev_err(&pdev->dev, "Failed to add USB HCD\n");
Vivek Gautamed993bf2013-01-22 18:30:43 +0530147 goto fail_add_hcd;
Jingoo Han62194242011-12-23 11:20:54 +0900148 }
Jingoo Han62194242011-12-23 11:20:54 +0900149 return 0;
150
Vivek Gautamed993bf2013-01-22 18:30:43 +0530151fail_add_hcd:
Manjunath Goudar50a97e02013-09-21 16:38:38 +0530152 exynos_ohci_phy_disable(pdev);
Jingoo Han62194242011-12-23 11:20:54 +0900153fail_io:
Thomas Abrahamc05c9462012-10-03 08:41:37 +0900154 clk_disable_unprepare(exynos_ohci->clk);
Jingoo Han62194242011-12-23 11:20:54 +0900155fail_clk:
156 usb_put_hcd(hcd);
Jingoo Han62194242011-12-23 11:20:54 +0900157 return err;
158}
159
Bill Pembertonfb4e98a2012-11-19 13:26:20 -0500160static int exynos_ohci_remove(struct platform_device *pdev)
Jingoo Han62194242011-12-23 11:20:54 +0900161{
Manjunath Goudar50a97e02013-09-21 16:38:38 +0530162 struct usb_hcd *hcd = platform_get_drvdata(pdev);
163 struct exynos_ohci_hcd *exynos_ohci = to_exynos_ohci(hcd);
Jingoo Han62194242011-12-23 11:20:54 +0900164
165 usb_remove_hcd(hcd);
166
Vivek Gautamed993bf2013-01-22 18:30:43 +0530167 if (exynos_ohci->otg)
Manjunath Goudar50a97e02013-09-21 16:38:38 +0530168 exynos_ohci->otg->set_host(exynos_ohci->otg, &hcd->self);
Vivek Gautamed993bf2013-01-22 18:30:43 +0530169
Manjunath Goudar50a97e02013-09-21 16:38:38 +0530170 exynos_ohci_phy_disable(pdev);
Jingoo Han62194242011-12-23 11:20:54 +0900171
Thomas Abrahamc05c9462012-10-03 08:41:37 +0900172 clk_disable_unprepare(exynos_ohci->clk);
Jingoo Han62194242011-12-23 11:20:54 +0900173
174 usb_put_hcd(hcd);
Jingoo Han62194242011-12-23 11:20:54 +0900175
176 return 0;
177}
178
179static void exynos_ohci_shutdown(struct platform_device *pdev)
180{
Manjunath Goudar50a97e02013-09-21 16:38:38 +0530181 struct usb_hcd *hcd = platform_get_drvdata(pdev);
Jingoo Han62194242011-12-23 11:20:54 +0900182
183 if (hcd->driver->shutdown)
184 hcd->driver->shutdown(hcd);
185}
186
187#ifdef CONFIG_PM
188static int exynos_ohci_suspend(struct device *dev)
189{
Manjunath Goudar50a97e02013-09-21 16:38:38 +0530190 struct usb_hcd *hcd = dev_get_drvdata(dev);
191 struct exynos_ohci_hcd *exynos_ohci = to_exynos_ohci(hcd);
Jingoo Han62194242011-12-23 11:20:54 +0900192 struct ohci_hcd *ohci = hcd_to_ohci(hcd);
Manjunath Goudar50a97e02013-09-21 16:38:38 +0530193 struct platform_device *pdev = to_platform_device(dev);
Jingoo Han62194242011-12-23 11:20:54 +0900194 unsigned long flags;
195 int rc = 0;
196
Greg Kroah-Hartman066ba8e2013-10-14 10:16:58 -0700197 /*
198 * Root hub was already suspended. Disable irq emission and
199 * mark HW unaccessible, bail out if RH has been resumed. Use
200 * the spinlock to properly synchronize with possible pending
201 * RH suspend or resume activity.
202 */
Jingoo Han62194242011-12-23 11:20:54 +0900203 spin_lock_irqsave(&ohci->lock, flags);
Greg Kroah-Hartman066ba8e2013-10-14 10:16:58 -0700204 if (ohci->rh_state != OHCI_RH_SUSPENDED &&
205 ohci->rh_state != OHCI_RH_HALTED) {
206 rc = -EINVAL;
207 goto fail;
208 }
209
210 clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
211
Vivek Gautamed993bf2013-01-22 18:30:43 +0530212 if (exynos_ohci->otg)
Manjunath Goudar50a97e02013-09-21 16:38:38 +0530213 exynos_ohci->otg->set_host(exynos_ohci->otg, &hcd->self);
Vivek Gautamed993bf2013-01-22 18:30:43 +0530214
Manjunath Goudar50a97e02013-09-21 16:38:38 +0530215 exynos_ohci_phy_disable(pdev);
Jingoo Hane864abe2012-06-28 16:49:42 +0900216
Thomas Abrahamc05c9462012-10-03 08:41:37 +0900217 clk_disable_unprepare(exynos_ohci->clk);
Jingoo Hane864abe2012-06-28 16:49:42 +0900218
Greg Kroah-Hartman066ba8e2013-10-14 10:16:58 -0700219fail:
Jingoo Han62194242011-12-23 11:20:54 +0900220 spin_unlock_irqrestore(&ohci->lock, flags);
221
222 return rc;
223}
224
225static int exynos_ohci_resume(struct device *dev)
226{
Manjunath Goudar50a97e02013-09-21 16:38:38 +0530227 struct usb_hcd *hcd = dev_get_drvdata(dev);
228 struct exynos_ohci_hcd *exynos_ohci = to_exynos_ohci(hcd);
229 struct platform_device *pdev = to_platform_device(dev);
Jingoo Han62194242011-12-23 11:20:54 +0900230
Thomas Abrahamc05c9462012-10-03 08:41:37 +0900231 clk_prepare_enable(exynos_ohci->clk);
Jingoo Hane864abe2012-06-28 16:49:42 +0900232
Vivek Gautamed993bf2013-01-22 18:30:43 +0530233 if (exynos_ohci->otg)
Manjunath Goudar50a97e02013-09-21 16:38:38 +0530234 exynos_ohci->otg->set_host(exynos_ohci->otg, &hcd->self);
Vivek Gautamed993bf2013-01-22 18:30:43 +0530235
Manjunath Goudar50a97e02013-09-21 16:38:38 +0530236 exynos_ohci_phy_enable(pdev);
Jingoo Han62194242011-12-23 11:20:54 +0900237
Florian Fainellicfa49b42012-10-08 15:11:29 +0200238 ohci_resume(hcd, false);
Jingoo Han62194242011-12-23 11:20:54 +0900239
240 return 0;
241}
242#else
243#define exynos_ohci_suspend NULL
244#define exynos_ohci_resume NULL
245#endif
246
Manjunath Goudar50a97e02013-09-21 16:38:38 +0530247static const struct ohci_driver_overrides exynos_overrides __initconst = {
248 .extra_priv_size = sizeof(struct exynos_ohci_hcd),
249};
250
Jingoo Han62194242011-12-23 11:20:54 +0900251static const struct dev_pm_ops exynos_ohci_pm_ops = {
252 .suspend = exynos_ohci_suspend,
253 .resume = exynos_ohci_resume,
254};
255
Vivek Gautamd5138932012-07-16 11:25:36 +0530256#ifdef CONFIG_OF
257static const struct of_device_id exynos_ohci_match[] = {
Vivek Gautam6e247772013-01-24 19:15:29 +0530258 { .compatible = "samsung,exynos4210-ohci" },
Thomas Abraham28717822013-04-11 17:12:30 +0530259 { .compatible = "samsung,exynos5440-ohci" },
Vivek Gautamd5138932012-07-16 11:25:36 +0530260 {},
261};
262MODULE_DEVICE_TABLE(of, exynos_ohci_match);
263#endif
264
Jingoo Han62194242011-12-23 11:20:54 +0900265static struct platform_driver exynos_ohci_driver = {
266 .probe = exynos_ohci_probe,
Bill Pemberton76904172012-11-19 13:21:08 -0500267 .remove = exynos_ohci_remove,
Jingoo Han62194242011-12-23 11:20:54 +0900268 .shutdown = exynos_ohci_shutdown,
269 .driver = {
270 .name = "exynos-ohci",
271 .owner = THIS_MODULE,
272 .pm = &exynos_ohci_pm_ops,
Vivek Gautamd5138932012-07-16 11:25:36 +0530273 .of_match_table = of_match_ptr(exynos_ohci_match),
Jingoo Han62194242011-12-23 11:20:54 +0900274 }
275};
Manjunath Goudar50a97e02013-09-21 16:38:38 +0530276static int __init ohci_exynos_init(void)
277{
278 if (usb_disabled())
279 return -ENODEV;
280
281 pr_info("%s: " DRIVER_DESC "\n", hcd_name);
282 ohci_init_driver(&exynos_ohci_hc_driver, &exynos_overrides);
283 return platform_driver_register(&exynos_ohci_driver);
284}
285module_init(ohci_exynos_init);
286
287static void __exit ohci_exynos_cleanup(void)
288{
289 platform_driver_unregister(&exynos_ohci_driver);
290}
291module_exit(ohci_exynos_cleanup);
Jingoo Han62194242011-12-23 11:20:54 +0900292
293MODULE_ALIAS("platform:exynos-ohci");
294MODULE_AUTHOR("Jingoo Han <jg1.han@samsung.com>");
Manjunath Goudar50a97e02013-09-21 16:38:38 +0530295MODULE_LICENSE("GPL v2");