blob: d1d8c47777c58935698d0959856d2ac9237f3274 [file] [log] [blame]
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +09001/*
Jingoo Han29824c12013-10-10 16:42:47 +09002 * SAMSUNG EXYNOS USB HOST EHCI Controller
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +09003 *
4 * Copyright (C) 2011 Samsung Electronics Co.Ltd
5 * Author: Jingoo Han <jg1.han@samsung.com>
6 * Author: Joonyoung Shim <jy0922.shim@samsung.com>
7 *
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; either version 2 of the License, or (at your
11 * option) any later version.
12 *
13 */
14
15#include <linux/clk.h>
Manjunath Goudar7edb3da2013-04-02 18:24:01 +020016#include <linux/dma-mapping.h>
17#include <linux/io.h>
18#include <linux/kernel.h>
19#include <linux/module.h>
Vivek Gautam2c026e22012-07-16 11:25:37 +053020#include <linux/of.h>
Vivek Gautamfd81d592012-07-17 10:10:50 +053021#include <linux/of_gpio.h>
Manjunath Goudar7edb3da2013-04-02 18:24:01 +020022#include <linux/platform_device.h>
Vivek Gautamd233c192013-01-22 18:30:42 +053023#include <linux/usb/phy.h>
Vivek Gautamb506eeb2013-01-22 18:30:40 +053024#include <linux/usb/samsung_usb_phy.h>
Manjunath Goudar7edb3da2013-04-02 18:24:01 +020025#include <linux/usb.h>
26#include <linux/usb/hcd.h>
27#include <linux/usb/otg.h>
28
29#include "ehci.h"
30
Jingoo Han29824c12013-10-10 16:42:47 +090031#define DRIVER_DESC "EHCI EXYNOS driver"
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +090032
Jingoo Han88555a62012-03-05 10:40:14 +090033#define EHCI_INSNREG00(base) (base + 0x90)
34#define EHCI_INSNREG00_ENA_INCR16 (0x1 << 25)
35#define EHCI_INSNREG00_ENA_INCR8 (0x1 << 24)
36#define EHCI_INSNREG00_ENA_INCR4 (0x1 << 23)
37#define EHCI_INSNREG00_ENA_INCRX_ALIGN (0x1 << 22)
38#define EHCI_INSNREG00_ENABLE_DMA_BURST \
39 (EHCI_INSNREG00_ENA_INCR16 | EHCI_INSNREG00_ENA_INCR8 | \
40 EHCI_INSNREG00_ENA_INCR4 | EHCI_INSNREG00_ENA_INCRX_ALIGN)
41
Jingoo Han29824c12013-10-10 16:42:47 +090042static const char hcd_name[] = "ehci-exynos";
43static struct hc_driver __read_mostly exynos_ehci_hc_driver;
Manjunath Goudar7edb3da2013-04-02 18:24:01 +020044
Jingoo Han29824c12013-10-10 16:42:47 +090045struct exynos_ehci_hcd {
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +090046 struct clk *clk;
Vivek Gautamd233c192013-01-22 18:30:42 +053047 struct usb_phy *phy;
48 struct usb_otg *otg;
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +090049};
50
Jingoo Han29824c12013-10-10 16:42:47 +090051#define to_exynos_ehci(hcd) (struct exynos_ehci_hcd *)(hcd_to_ehci(hcd)->priv)
Vivek Gautamd233c192013-01-22 18:30:42 +053052
Jingoo Han29824c12013-10-10 16:42:47 +090053static void exynos_setup_vbus_gpio(struct platform_device *pdev)
Vivek Gautamfd81d592012-07-17 10:10:50 +053054{
Doug Anderson3f3b55b2013-03-14 20:15:37 -070055 struct device *dev = &pdev->dev;
Vivek Gautamfd81d592012-07-17 10:10:50 +053056 int err;
57 int gpio;
58
Doug Anderson3f3b55b2013-03-14 20:15:37 -070059 if (!dev->of_node)
Vivek Gautamfd81d592012-07-17 10:10:50 +053060 return;
61
Doug Anderson3f3b55b2013-03-14 20:15:37 -070062 gpio = of_get_named_gpio(dev->of_node, "samsung,vbus-gpio", 0);
Vivek Gautamfd81d592012-07-17 10:10:50 +053063 if (!gpio_is_valid(gpio))
64 return;
65
Doug Anderson3f3b55b2013-03-14 20:15:37 -070066 err = devm_gpio_request_one(dev, gpio, GPIOF_OUT_INIT_HIGH,
67 "ehci_vbus_gpio");
Vivek Gautamfd81d592012-07-17 10:10:50 +053068 if (err)
Doug Anderson3f3b55b2013-03-14 20:15:37 -070069 dev_err(dev, "can't request ehci vbus gpio %d", gpio);
Vivek Gautamfd81d592012-07-17 10:10:50 +053070}
71
Jingoo Han29824c12013-10-10 16:42:47 +090072static int exynos_ehci_probe(struct platform_device *pdev)
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +090073{
Jingoo Han29824c12013-10-10 16:42:47 +090074 struct exynos_ehci_hcd *exynos_ehci;
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +090075 struct usb_hcd *hcd;
76 struct ehci_hcd *ehci;
77 struct resource *res;
Vivek Gautamd233c192013-01-22 18:30:42 +053078 struct usb_phy *phy;
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +090079 int irq;
80 int err;
81
Vivek Gautam2c026e22012-07-16 11:25:37 +053082 /*
83 * Right now device-tree probed devices don't get dma_mask set.
84 * Since shared usb code relies on it, set it here for now.
85 * Once we move to full device tree support this will vanish off.
86 */
Linus Torvalds8ceafbf2013-11-14 07:55:21 +090087 err = dma_coerce_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
88 if (err)
89 return err;
Vivek Gautam2c026e22012-07-16 11:25:37 +053090
Jingoo Han29824c12013-10-10 16:42:47 +090091 exynos_setup_vbus_gpio(pdev);
Vivek Gautamfd81d592012-07-17 10:10:50 +053092
Jingoo Han29824c12013-10-10 16:42:47 +090093 hcd = usb_create_hcd(&exynos_ehci_hc_driver,
Manjunath Goudar7edb3da2013-04-02 18:24:01 +020094 &pdev->dev, dev_name(&pdev->dev));
95 if (!hcd) {
96 dev_err(&pdev->dev, "Unable to create HCD\n");
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +090097 return -ENOMEM;
Manjunath Goudar7edb3da2013-04-02 18:24:01 +020098 }
Jingoo Han29824c12013-10-10 16:42:47 +090099 exynos_ehci = to_exynos_ehci(hcd);
Thomas Abrahame6b0166f2013-05-27 18:42:12 +0900100
101 if (of_device_is_compatible(pdev->dev.of_node,
Jingoo Han57ae1602013-10-10 16:41:19 +0900102 "samsung,exynos5440-ehci"))
Thomas Abrahame6b0166f2013-05-27 18:42:12 +0900103 goto skip_phy;
Thomas Abrahame6b0166f2013-05-27 18:42:12 +0900104
Vivek Gautamd233c192013-01-22 18:30:42 +0530105 phy = devm_usb_get_phy(&pdev->dev, USB_PHY_TYPE_USB2);
Felipe Balbia16283e2013-03-15 11:04:15 +0200106 if (IS_ERR(phy)) {
Jingoo Han57ae1602013-10-10 16:41:19 +0900107 usb_put_hcd(hcd);
108 dev_warn(&pdev->dev, "no platform data or transceiver defined\n");
109 return -EPROBE_DEFER;
Vivek Gautamd233c192013-01-22 18:30:42 +0530110 } else {
Jingoo Han29824c12013-10-10 16:42:47 +0900111 exynos_ehci->phy = phy;
112 exynos_ehci->otg = phy->otg;
Vivek Gautamd233c192013-01-22 18:30:42 +0530113 }
114
Thomas Abrahame6b0166f2013-05-27 18:42:12 +0900115skip_phy:
116
Jingoo Han29824c12013-10-10 16:42:47 +0900117 exynos_ehci->clk = devm_clk_get(&pdev->dev, "usbhost");
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900118
Jingoo Han29824c12013-10-10 16:42:47 +0900119 if (IS_ERR(exynos_ehci->clk)) {
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900120 dev_err(&pdev->dev, "Failed to get usbhost clock\n");
Jingoo Han29824c12013-10-10 16:42:47 +0900121 err = PTR_ERR(exynos_ehci->clk);
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900122 goto fail_clk;
123 }
124
Jingoo Han29824c12013-10-10 16:42:47 +0900125 err = clk_prepare_enable(exynos_ehci->clk);
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900126 if (err)
Julia Lawall63fd0ae2012-07-30 16:43:44 +0200127 goto fail_clk;
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900128
129 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
130 if (!res) {
131 dev_err(&pdev->dev, "Failed to get I/O memory\n");
132 err = -ENXIO;
133 goto fail_io;
134 }
135
136 hcd->rsrc_start = res->start;
137 hcd->rsrc_len = resource_size(res);
Jingoo Han9cb07562012-06-28 16:29:46 +0900138 hcd->regs = devm_ioremap(&pdev->dev, res->start, hcd->rsrc_len);
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900139 if (!hcd->regs) {
140 dev_err(&pdev->dev, "Failed to remap I/O memory\n");
141 err = -ENOMEM;
142 goto fail_io;
143 }
144
145 irq = platform_get_irq(pdev, 0);
146 if (!irq) {
147 dev_err(&pdev->dev, "Failed to get IRQ\n");
148 err = -ENODEV;
Jingoo Han9cb07562012-06-28 16:29:46 +0900149 goto fail_io;
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900150 }
151
Jingoo Han29824c12013-10-10 16:42:47 +0900152 if (exynos_ehci->otg)
153 exynos_ehci->otg->set_host(exynos_ehci->otg, &hcd->self);
Vivek Gautamd233c192013-01-22 18:30:42 +0530154
Jingoo Han29824c12013-10-10 16:42:47 +0900155 if (exynos_ehci->phy)
156 usb_phy_init(exynos_ehci->phy);
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900157
158 ehci = hcd_to_ehci(hcd);
159 ehci->caps = hcd->regs;
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900160
Jingoo Han88555a62012-03-05 10:40:14 +0900161 /* DMA burst Enable */
162 writel(EHCI_INSNREG00_ENABLE_DMA_BURST, EHCI_INSNREG00(hcd->regs));
163
Yong Zhangb5dd18d2011-09-07 16:10:52 +0800164 err = usb_add_hcd(hcd, irq, IRQF_SHARED);
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900165 if (err) {
166 dev_err(&pdev->dev, "Failed to add USB HCD\n");
Vivek Gautamd233c192013-01-22 18:30:42 +0530167 goto fail_add_hcd;
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900168 }
Peter Chen3c9740a2013-11-05 10:46:02 +0800169 device_wakeup_enable(hcd->self.controller);
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900170
Vivek Gautambbcd85a2013-04-09 18:42:11 +0530171 platform_set_drvdata(pdev, hcd);
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900172
173 return 0;
174
Vivek Gautamd233c192013-01-22 18:30:42 +0530175fail_add_hcd:
Jingoo Han29824c12013-10-10 16:42:47 +0900176 if (exynos_ehci->phy)
177 usb_phy_shutdown(exynos_ehci->phy);
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900178fail_io:
Jingoo Han29824c12013-10-10 16:42:47 +0900179 clk_disable_unprepare(exynos_ehci->clk);
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900180fail_clk:
181 usb_put_hcd(hcd);
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900182 return err;
183}
184
Jingoo Han29824c12013-10-10 16:42:47 +0900185static int exynos_ehci_remove(struct platform_device *pdev)
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900186{
Manjunath Goudar7edb3da2013-04-02 18:24:01 +0200187 struct usb_hcd *hcd = platform_get_drvdata(pdev);
Jingoo Han29824c12013-10-10 16:42:47 +0900188 struct exynos_ehci_hcd *exynos_ehci = to_exynos_ehci(hcd);
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900189
190 usb_remove_hcd(hcd);
191
Jingoo Han29824c12013-10-10 16:42:47 +0900192 if (exynos_ehci->otg)
193 exynos_ehci->otg->set_host(exynos_ehci->otg, &hcd->self);
Vivek Gautamd233c192013-01-22 18:30:42 +0530194
Jingoo Han29824c12013-10-10 16:42:47 +0900195 if (exynos_ehci->phy)
196 usb_phy_shutdown(exynos_ehci->phy);
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900197
Jingoo Han29824c12013-10-10 16:42:47 +0900198 clk_disable_unprepare(exynos_ehci->clk);
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900199
200 usb_put_hcd(hcd);
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900201
202 return 0;
203}
204
Jingoo Han1acb30e2011-05-20 20:48:33 +0900205#ifdef CONFIG_PM
Jingoo Han29824c12013-10-10 16:42:47 +0900206static int exynos_ehci_suspend(struct device *dev)
Jingoo Han1acb30e2011-05-20 20:48:33 +0900207{
Manjunath Goudar7edb3da2013-04-02 18:24:01 +0200208 struct usb_hcd *hcd = dev_get_drvdata(dev);
Jingoo Han29824c12013-10-10 16:42:47 +0900209 struct exynos_ehci_hcd *exynos_ehci = to_exynos_ehci(hcd);
Manjunath Goudar7edb3da2013-04-02 18:24:01 +0200210
Alan Sternc5cf9212012-06-28 11:19:02 -0400211 bool do_wakeup = device_may_wakeup(dev);
Alan Sternc5cf9212012-06-28 11:19:02 -0400212 int rc;
Jingoo Han1acb30e2011-05-20 20:48:33 +0900213
Alan Sternc5cf9212012-06-28 11:19:02 -0400214 rc = ehci_suspend(hcd, do_wakeup);
Jingoo Han1acb30e2011-05-20 20:48:33 +0900215
Jingoo Han29824c12013-10-10 16:42:47 +0900216 if (exynos_ehci->otg)
217 exynos_ehci->otg->set_host(exynos_ehci->otg, &hcd->self);
Vivek Gautamd233c192013-01-22 18:30:42 +0530218
Jingoo Han29824c12013-10-10 16:42:47 +0900219 if (exynos_ehci->phy)
220 usb_phy_shutdown(exynos_ehci->phy);
Jingoo Han1acb30e2011-05-20 20:48:33 +0900221
Jingoo Han29824c12013-10-10 16:42:47 +0900222 clk_disable_unprepare(exynos_ehci->clk);
Jingoo Han8b4fc8c2012-04-13 11:06:36 +0900223
Jingoo Han1acb30e2011-05-20 20:48:33 +0900224 return rc;
225}
226
Jingoo Han29824c12013-10-10 16:42:47 +0900227static int exynos_ehci_resume(struct device *dev)
Jingoo Han1acb30e2011-05-20 20:48:33 +0900228{
Manjunath Goudar7edb3da2013-04-02 18:24:01 +0200229 struct usb_hcd *hcd = dev_get_drvdata(dev);
Jingoo Han29824c12013-10-10 16:42:47 +0900230 struct exynos_ehci_hcd *exynos_ehci = to_exynos_ehci(hcd);
Jingoo Han1acb30e2011-05-20 20:48:33 +0900231
Jingoo Han29824c12013-10-10 16:42:47 +0900232 clk_prepare_enable(exynos_ehci->clk);
Jingoo Han8b4fc8c2012-04-13 11:06:36 +0900233
Jingoo Han29824c12013-10-10 16:42:47 +0900234 if (exynos_ehci->otg)
235 exynos_ehci->otg->set_host(exynos_ehci->otg, &hcd->self);
Vivek Gautamd233c192013-01-22 18:30:42 +0530236
Jingoo Han29824c12013-10-10 16:42:47 +0900237 if (exynos_ehci->phy)
238 usb_phy_init(exynos_ehci->phy);
Jingoo Han1acb30e2011-05-20 20:48:33 +0900239
Jingoo Han88555a62012-03-05 10:40:14 +0900240 /* DMA burst Enable */
241 writel(EHCI_INSNREG00_ENABLE_DMA_BURST, EHCI_INSNREG00(hcd->regs));
242
Alan Sternc5cf9212012-06-28 11:19:02 -0400243 ehci_resume(hcd, false);
Jingoo Han1acb30e2011-05-20 20:48:33 +0900244 return 0;
245}
246#else
Jingoo Han29824c12013-10-10 16:42:47 +0900247#define exynos_ehci_suspend NULL
248#define exynos_ehci_resume NULL
Jingoo Han1acb30e2011-05-20 20:48:33 +0900249#endif
250
Jingoo Han29824c12013-10-10 16:42:47 +0900251static const struct dev_pm_ops exynos_ehci_pm_ops = {
252 .suspend = exynos_ehci_suspend,
253 .resume = exynos_ehci_resume,
Jingoo Han1acb30e2011-05-20 20:48:33 +0900254};
255
Vivek Gautam2c026e22012-07-16 11:25:37 +0530256#ifdef CONFIG_OF
257static const struct of_device_id exynos_ehci_match[] = {
Vivek Gautam6e247772013-01-24 19:15:29 +0530258 { .compatible = "samsung,exynos4210-ehci" },
Thomas Abrahame6b0166f2013-05-27 18:42:12 +0900259 { .compatible = "samsung,exynos5440-ehci" },
Vivek Gautam2c026e22012-07-16 11:25:37 +0530260 {},
261};
262MODULE_DEVICE_TABLE(of, exynos_ehci_match);
263#endif
264
Jingoo Han29824c12013-10-10 16:42:47 +0900265static struct platform_driver exynos_ehci_driver = {
266 .probe = exynos_ehci_probe,
267 .remove = exynos_ehci_remove,
Roger Quadrosaaf6b522013-07-22 15:04:50 +0300268 .shutdown = usb_hcd_platform_shutdown,
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900269 .driver = {
Jingoo Han29824c12013-10-10 16:42:47 +0900270 .name = "exynos-ehci",
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900271 .owner = THIS_MODULE,
Jingoo Han29824c12013-10-10 16:42:47 +0900272 .pm = &exynos_ehci_pm_ops,
Vivek Gautam2c026e22012-07-16 11:25:37 +0530273 .of_match_table = of_match_ptr(exynos_ehci_match),
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900274 }
275};
Jingoo Han29824c12013-10-10 16:42:47 +0900276static const struct ehci_driver_overrides exynos_overrides __initdata = {
277 .extra_priv_size = sizeof(struct exynos_ehci_hcd),
Manjunath Goudar7edb3da2013-04-02 18:24:01 +0200278};
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900279
Jingoo Han29824c12013-10-10 16:42:47 +0900280static int __init ehci_exynos_init(void)
Manjunath Goudar7edb3da2013-04-02 18:24:01 +0200281{
282 if (usb_disabled())
283 return -ENODEV;
284
285 pr_info("%s: " DRIVER_DESC "\n", hcd_name);
Jingoo Han29824c12013-10-10 16:42:47 +0900286 ehci_init_driver(&exynos_ehci_hc_driver, &exynos_overrides);
287 return platform_driver_register(&exynos_ehci_driver);
Manjunath Goudar7edb3da2013-04-02 18:24:01 +0200288}
Jingoo Han29824c12013-10-10 16:42:47 +0900289module_init(ehci_exynos_init);
Manjunath Goudar7edb3da2013-04-02 18:24:01 +0200290
Jingoo Han29824c12013-10-10 16:42:47 +0900291static void __exit ehci_exynos_cleanup(void)
Manjunath Goudar7edb3da2013-04-02 18:24:01 +0200292{
Jingoo Han29824c12013-10-10 16:42:47 +0900293 platform_driver_unregister(&exynos_ehci_driver);
Manjunath Goudar7edb3da2013-04-02 18:24:01 +0200294}
Jingoo Han29824c12013-10-10 16:42:47 +0900295module_exit(ehci_exynos_cleanup);
Manjunath Goudar7edb3da2013-04-02 18:24:01 +0200296
297MODULE_DESCRIPTION(DRIVER_DESC);
Jingoo Han29824c12013-10-10 16:42:47 +0900298MODULE_ALIAS("platform:exynos-ehci");
Manjunath Goudar7edb3da2013-04-02 18:24:01 +0200299MODULE_AUTHOR("Jingoo Han");
300MODULE_AUTHOR("Joonyoung Shim");
301MODULE_LICENSE("GPL v2");