blob: e97c198e052fd88aa99a98679952769678728a75 [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 }
169
Vivek Gautambbcd85a2013-04-09 18:42:11 +0530170 platform_set_drvdata(pdev, hcd);
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900171
172 return 0;
173
Vivek Gautamd233c192013-01-22 18:30:42 +0530174fail_add_hcd:
Jingoo Han29824c12013-10-10 16:42:47 +0900175 if (exynos_ehci->phy)
176 usb_phy_shutdown(exynos_ehci->phy);
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900177fail_io:
Jingoo Han29824c12013-10-10 16:42:47 +0900178 clk_disable_unprepare(exynos_ehci->clk);
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900179fail_clk:
180 usb_put_hcd(hcd);
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900181 return err;
182}
183
Jingoo Han29824c12013-10-10 16:42:47 +0900184static int exynos_ehci_remove(struct platform_device *pdev)
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900185{
Manjunath Goudar7edb3da2013-04-02 18:24:01 +0200186 struct usb_hcd *hcd = platform_get_drvdata(pdev);
Jingoo Han29824c12013-10-10 16:42:47 +0900187 struct exynos_ehci_hcd *exynos_ehci = to_exynos_ehci(hcd);
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900188
189 usb_remove_hcd(hcd);
190
Jingoo Han29824c12013-10-10 16:42:47 +0900191 if (exynos_ehci->otg)
192 exynos_ehci->otg->set_host(exynos_ehci->otg, &hcd->self);
Vivek Gautamd233c192013-01-22 18:30:42 +0530193
Jingoo Han29824c12013-10-10 16:42:47 +0900194 if (exynos_ehci->phy)
195 usb_phy_shutdown(exynos_ehci->phy);
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900196
Jingoo Han29824c12013-10-10 16:42:47 +0900197 clk_disable_unprepare(exynos_ehci->clk);
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900198
199 usb_put_hcd(hcd);
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900200
201 return 0;
202}
203
Jingoo Han1acb30e2011-05-20 20:48:33 +0900204#ifdef CONFIG_PM
Jingoo Han29824c12013-10-10 16:42:47 +0900205static int exynos_ehci_suspend(struct device *dev)
Jingoo Han1acb30e2011-05-20 20:48:33 +0900206{
Manjunath Goudar7edb3da2013-04-02 18:24:01 +0200207 struct usb_hcd *hcd = dev_get_drvdata(dev);
Jingoo Han29824c12013-10-10 16:42:47 +0900208 struct exynos_ehci_hcd *exynos_ehci = to_exynos_ehci(hcd);
Manjunath Goudar7edb3da2013-04-02 18:24:01 +0200209
Alan Sternc5cf9212012-06-28 11:19:02 -0400210 bool do_wakeup = device_may_wakeup(dev);
Alan Sternc5cf9212012-06-28 11:19:02 -0400211 int rc;
Jingoo Han1acb30e2011-05-20 20:48:33 +0900212
Alan Sternc5cf9212012-06-28 11:19:02 -0400213 rc = ehci_suspend(hcd, do_wakeup);
Jingoo Han1acb30e2011-05-20 20:48:33 +0900214
Jingoo Han29824c12013-10-10 16:42:47 +0900215 if (exynos_ehci->otg)
216 exynos_ehci->otg->set_host(exynos_ehci->otg, &hcd->self);
Vivek Gautamd233c192013-01-22 18:30:42 +0530217
Jingoo Han29824c12013-10-10 16:42:47 +0900218 if (exynos_ehci->phy)
219 usb_phy_shutdown(exynos_ehci->phy);
Jingoo Han1acb30e2011-05-20 20:48:33 +0900220
Jingoo Han29824c12013-10-10 16:42:47 +0900221 clk_disable_unprepare(exynos_ehci->clk);
Jingoo Han8b4fc8c2012-04-13 11:06:36 +0900222
Jingoo Han1acb30e2011-05-20 20:48:33 +0900223 return rc;
224}
225
Jingoo Han29824c12013-10-10 16:42:47 +0900226static int exynos_ehci_resume(struct device *dev)
Jingoo Han1acb30e2011-05-20 20:48:33 +0900227{
Manjunath Goudar7edb3da2013-04-02 18:24:01 +0200228 struct usb_hcd *hcd = dev_get_drvdata(dev);
Jingoo Han29824c12013-10-10 16:42:47 +0900229 struct exynos_ehci_hcd *exynos_ehci = to_exynos_ehci(hcd);
Jingoo Han1acb30e2011-05-20 20:48:33 +0900230
Jingoo Han29824c12013-10-10 16:42:47 +0900231 clk_prepare_enable(exynos_ehci->clk);
Jingoo Han8b4fc8c2012-04-13 11:06:36 +0900232
Jingoo Han29824c12013-10-10 16:42:47 +0900233 if (exynos_ehci->otg)
234 exynos_ehci->otg->set_host(exynos_ehci->otg, &hcd->self);
Vivek Gautamd233c192013-01-22 18:30:42 +0530235
Jingoo Han29824c12013-10-10 16:42:47 +0900236 if (exynos_ehci->phy)
237 usb_phy_init(exynos_ehci->phy);
Jingoo Han1acb30e2011-05-20 20:48:33 +0900238
Jingoo Han88555a62012-03-05 10:40:14 +0900239 /* DMA burst Enable */
240 writel(EHCI_INSNREG00_ENABLE_DMA_BURST, EHCI_INSNREG00(hcd->regs));
241
Alan Sternc5cf9212012-06-28 11:19:02 -0400242 ehci_resume(hcd, false);
Jingoo Han1acb30e2011-05-20 20:48:33 +0900243 return 0;
244}
245#else
Jingoo Han29824c12013-10-10 16:42:47 +0900246#define exynos_ehci_suspend NULL
247#define exynos_ehci_resume NULL
Jingoo Han1acb30e2011-05-20 20:48:33 +0900248#endif
249
Jingoo Han29824c12013-10-10 16:42:47 +0900250static const struct dev_pm_ops exynos_ehci_pm_ops = {
251 .suspend = exynos_ehci_suspend,
252 .resume = exynos_ehci_resume,
Jingoo Han1acb30e2011-05-20 20:48:33 +0900253};
254
Vivek Gautam2c026e22012-07-16 11:25:37 +0530255#ifdef CONFIG_OF
256static const struct of_device_id exynos_ehci_match[] = {
Vivek Gautam6e247772013-01-24 19:15:29 +0530257 { .compatible = "samsung,exynos4210-ehci" },
Thomas Abrahame6b0166f2013-05-27 18:42:12 +0900258 { .compatible = "samsung,exynos5440-ehci" },
Vivek Gautam2c026e22012-07-16 11:25:37 +0530259 {},
260};
261MODULE_DEVICE_TABLE(of, exynos_ehci_match);
262#endif
263
Jingoo Han29824c12013-10-10 16:42:47 +0900264static struct platform_driver exynos_ehci_driver = {
265 .probe = exynos_ehci_probe,
266 .remove = exynos_ehci_remove,
Roger Quadrosaaf6b522013-07-22 15:04:50 +0300267 .shutdown = usb_hcd_platform_shutdown,
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900268 .driver = {
Jingoo Han29824c12013-10-10 16:42:47 +0900269 .name = "exynos-ehci",
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900270 .owner = THIS_MODULE,
Jingoo Han29824c12013-10-10 16:42:47 +0900271 .pm = &exynos_ehci_pm_ops,
Vivek Gautam2c026e22012-07-16 11:25:37 +0530272 .of_match_table = of_match_ptr(exynos_ehci_match),
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900273 }
274};
Jingoo Han29824c12013-10-10 16:42:47 +0900275static const struct ehci_driver_overrides exynos_overrides __initdata = {
276 .extra_priv_size = sizeof(struct exynos_ehci_hcd),
Manjunath Goudar7edb3da2013-04-02 18:24:01 +0200277};
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900278
Jingoo Han29824c12013-10-10 16:42:47 +0900279static int __init ehci_exynos_init(void)
Manjunath Goudar7edb3da2013-04-02 18:24:01 +0200280{
281 if (usb_disabled())
282 return -ENODEV;
283
284 pr_info("%s: " DRIVER_DESC "\n", hcd_name);
Jingoo Han29824c12013-10-10 16:42:47 +0900285 ehci_init_driver(&exynos_ehci_hc_driver, &exynos_overrides);
286 return platform_driver_register(&exynos_ehci_driver);
Manjunath Goudar7edb3da2013-04-02 18:24:01 +0200287}
Jingoo Han29824c12013-10-10 16:42:47 +0900288module_init(ehci_exynos_init);
Manjunath Goudar7edb3da2013-04-02 18:24:01 +0200289
Jingoo Han29824c12013-10-10 16:42:47 +0900290static void __exit ehci_exynos_cleanup(void)
Manjunath Goudar7edb3da2013-04-02 18:24:01 +0200291{
Jingoo Han29824c12013-10-10 16:42:47 +0900292 platform_driver_unregister(&exynos_ehci_driver);
Manjunath Goudar7edb3da2013-04-02 18:24:01 +0200293}
Jingoo Han29824c12013-10-10 16:42:47 +0900294module_exit(ehci_exynos_cleanup);
Manjunath Goudar7edb3da2013-04-02 18:24:01 +0200295
296MODULE_DESCRIPTION(DRIVER_DESC);
Jingoo Han29824c12013-10-10 16:42:47 +0900297MODULE_ALIAS("platform:exynos-ehci");
Manjunath Goudar7edb3da2013-04-02 18:24:01 +0200298MODULE_AUTHOR("Jingoo Han");
299MODULE_AUTHOR("Joonyoung Shim");
300MODULE_LICENSE("GPL v2");