blob: 68588d8a09bb60834d9e2740b488650d7fb42550 [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 }
Peter Chen3c9740a2013-11-05 10:46:02 +0800149 device_wakeup_enable(hcd->self.controller);
Jingoo Han62194242011-12-23 11:20:54 +0900150 return 0;
151
Vivek Gautamed993bf2013-01-22 18:30:43 +0530152fail_add_hcd:
Manjunath Goudar50a97e02013-09-21 16:38:38 +0530153 exynos_ohci_phy_disable(pdev);
Jingoo Han62194242011-12-23 11:20:54 +0900154fail_io:
Thomas Abrahamc05c9462012-10-03 08:41:37 +0900155 clk_disable_unprepare(exynos_ohci->clk);
Jingoo Han62194242011-12-23 11:20:54 +0900156fail_clk:
157 usb_put_hcd(hcd);
Jingoo Han62194242011-12-23 11:20:54 +0900158 return err;
159}
160
Bill Pembertonfb4e98a2012-11-19 13:26:20 -0500161static int exynos_ohci_remove(struct platform_device *pdev)
Jingoo Han62194242011-12-23 11:20:54 +0900162{
Manjunath Goudar50a97e02013-09-21 16:38:38 +0530163 struct usb_hcd *hcd = platform_get_drvdata(pdev);
164 struct exynos_ohci_hcd *exynos_ohci = to_exynos_ohci(hcd);
Jingoo Han62194242011-12-23 11:20:54 +0900165
166 usb_remove_hcd(hcd);
167
Vivek Gautamed993bf2013-01-22 18:30:43 +0530168 if (exynos_ohci->otg)
Manjunath Goudar50a97e02013-09-21 16:38:38 +0530169 exynos_ohci->otg->set_host(exynos_ohci->otg, &hcd->self);
Vivek Gautamed993bf2013-01-22 18:30:43 +0530170
Manjunath Goudar50a97e02013-09-21 16:38:38 +0530171 exynos_ohci_phy_disable(pdev);
Jingoo Han62194242011-12-23 11:20:54 +0900172
Thomas Abrahamc05c9462012-10-03 08:41:37 +0900173 clk_disable_unprepare(exynos_ohci->clk);
Jingoo Han62194242011-12-23 11:20:54 +0900174
175 usb_put_hcd(hcd);
Jingoo Han62194242011-12-23 11:20:54 +0900176
177 return 0;
178}
179
180static void exynos_ohci_shutdown(struct platform_device *pdev)
181{
Manjunath Goudar50a97e02013-09-21 16:38:38 +0530182 struct usb_hcd *hcd = platform_get_drvdata(pdev);
Jingoo Han62194242011-12-23 11:20:54 +0900183
184 if (hcd->driver->shutdown)
185 hcd->driver->shutdown(hcd);
186}
187
188#ifdef CONFIG_PM
189static int exynos_ohci_suspend(struct device *dev)
190{
Manjunath Goudar50a97e02013-09-21 16:38:38 +0530191 struct usb_hcd *hcd = dev_get_drvdata(dev);
192 struct exynos_ohci_hcd *exynos_ohci = to_exynos_ohci(hcd);
Jingoo Han62194242011-12-23 11:20:54 +0900193 struct ohci_hcd *ohci = hcd_to_ohci(hcd);
Manjunath Goudar50a97e02013-09-21 16:38:38 +0530194 struct platform_device *pdev = to_platform_device(dev);
Majunath Goudar14982e32013-11-13 17:40:20 +0530195 bool do_wakeup = device_may_wakeup(dev);
Jingoo Han62194242011-12-23 11:20:54 +0900196 unsigned long flags;
Majunath Goudar14982e32013-11-13 17:40:20 +0530197 int rc = ohci_suspend(hcd, do_wakeup);
Jingoo Han62194242011-12-23 11:20:54 +0900198
Majunath Goudar14982e32013-11-13 17:40:20 +0530199 if (rc)
200 return rc;
201
Jingoo Han62194242011-12-23 11:20:54 +0900202 spin_lock_irqsave(&ohci->lock, flags);
Greg Kroah-Hartman066ba8e2013-10-14 10:16:58 -0700203
Vivek Gautamed993bf2013-01-22 18:30:43 +0530204 if (exynos_ohci->otg)
Manjunath Goudar50a97e02013-09-21 16:38:38 +0530205 exynos_ohci->otg->set_host(exynos_ohci->otg, &hcd->self);
Vivek Gautamed993bf2013-01-22 18:30:43 +0530206
Manjunath Goudar50a97e02013-09-21 16:38:38 +0530207 exynos_ohci_phy_disable(pdev);
Jingoo Hane864abe2012-06-28 16:49:42 +0900208
Thomas Abrahamc05c9462012-10-03 08:41:37 +0900209 clk_disable_unprepare(exynos_ohci->clk);
Jingoo Hane864abe2012-06-28 16:49:42 +0900210
Jingoo Han62194242011-12-23 11:20:54 +0900211 spin_unlock_irqrestore(&ohci->lock, flags);
212
Majunath Goudar14982e32013-11-13 17:40:20 +0530213 return 0;
Jingoo Han62194242011-12-23 11:20:54 +0900214}
215
216static int exynos_ohci_resume(struct device *dev)
217{
Manjunath Goudar50a97e02013-09-21 16:38:38 +0530218 struct usb_hcd *hcd = dev_get_drvdata(dev);
219 struct exynos_ohci_hcd *exynos_ohci = to_exynos_ohci(hcd);
220 struct platform_device *pdev = to_platform_device(dev);
Jingoo Han62194242011-12-23 11:20:54 +0900221
Thomas Abrahamc05c9462012-10-03 08:41:37 +0900222 clk_prepare_enable(exynos_ohci->clk);
Jingoo Hane864abe2012-06-28 16:49:42 +0900223
Vivek Gautamed993bf2013-01-22 18:30:43 +0530224 if (exynos_ohci->otg)
Manjunath Goudar50a97e02013-09-21 16:38:38 +0530225 exynos_ohci->otg->set_host(exynos_ohci->otg, &hcd->self);
Vivek Gautamed993bf2013-01-22 18:30:43 +0530226
Manjunath Goudar50a97e02013-09-21 16:38:38 +0530227 exynos_ohci_phy_enable(pdev);
Jingoo Han62194242011-12-23 11:20:54 +0900228
Florian Fainellicfa49b42012-10-08 15:11:29 +0200229 ohci_resume(hcd, false);
Jingoo Han62194242011-12-23 11:20:54 +0900230
231 return 0;
232}
233#else
234#define exynos_ohci_suspend NULL
235#define exynos_ohci_resume NULL
236#endif
237
Manjunath Goudar50a97e02013-09-21 16:38:38 +0530238static const struct ohci_driver_overrides exynos_overrides __initconst = {
239 .extra_priv_size = sizeof(struct exynos_ohci_hcd),
240};
241
Jingoo Han62194242011-12-23 11:20:54 +0900242static const struct dev_pm_ops exynos_ohci_pm_ops = {
243 .suspend = exynos_ohci_suspend,
244 .resume = exynos_ohci_resume,
245};
246
Vivek Gautamd5138932012-07-16 11:25:36 +0530247#ifdef CONFIG_OF
248static const struct of_device_id exynos_ohci_match[] = {
Vivek Gautam6e247772013-01-24 19:15:29 +0530249 { .compatible = "samsung,exynos4210-ohci" },
Thomas Abraham28717822013-04-11 17:12:30 +0530250 { .compatible = "samsung,exynos5440-ohci" },
Vivek Gautamd5138932012-07-16 11:25:36 +0530251 {},
252};
253MODULE_DEVICE_TABLE(of, exynos_ohci_match);
254#endif
255
Jingoo Han62194242011-12-23 11:20:54 +0900256static struct platform_driver exynos_ohci_driver = {
257 .probe = exynos_ohci_probe,
Bill Pemberton76904172012-11-19 13:21:08 -0500258 .remove = exynos_ohci_remove,
Jingoo Han62194242011-12-23 11:20:54 +0900259 .shutdown = exynos_ohci_shutdown,
260 .driver = {
261 .name = "exynos-ohci",
262 .owner = THIS_MODULE,
263 .pm = &exynos_ohci_pm_ops,
Vivek Gautamd5138932012-07-16 11:25:36 +0530264 .of_match_table = of_match_ptr(exynos_ohci_match),
Jingoo Han62194242011-12-23 11:20:54 +0900265 }
266};
Manjunath Goudar50a97e02013-09-21 16:38:38 +0530267static int __init ohci_exynos_init(void)
268{
269 if (usb_disabled())
270 return -ENODEV;
271
272 pr_info("%s: " DRIVER_DESC "\n", hcd_name);
273 ohci_init_driver(&exynos_ohci_hc_driver, &exynos_overrides);
274 return platform_driver_register(&exynos_ohci_driver);
275}
276module_init(ohci_exynos_init);
277
278static void __exit ohci_exynos_cleanup(void)
279{
280 platform_driver_unregister(&exynos_ohci_driver);
281}
282module_exit(ohci_exynos_cleanup);
Jingoo Han62194242011-12-23 11:20:54 +0900283
284MODULE_ALIAS("platform:exynos-ohci");
285MODULE_AUTHOR("Jingoo Han <jg1.han@samsung.com>");
Manjunath Goudar50a97e02013-09-21 16:38:38 +0530286MODULE_LICENSE("GPL v2");