blob: 7189f2e32ac2f1b27a793699d2906c2851bd577a [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>
Kamil Debski1c176752014-05-05 10:32:28 +053022#include <linux/phy/phy.h>
Manjunath Goudar7edb3da2013-04-02 18:24:01 +020023#include <linux/platform_device.h>
Manjunath Goudar7edb3da2013-04-02 18:24:01 +020024#include <linux/usb.h>
25#include <linux/usb/hcd.h>
Manjunath Goudar7edb3da2013-04-02 18:24:01 +020026
27#include "ehci.h"
28
Jingoo Han29824c12013-10-10 16:42:47 +090029#define DRIVER_DESC "EHCI EXYNOS driver"
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +090030
Jingoo Han88555a62012-03-05 10:40:14 +090031#define EHCI_INSNREG00(base) (base + 0x90)
32#define EHCI_INSNREG00_ENA_INCR16 (0x1 << 25)
33#define EHCI_INSNREG00_ENA_INCR8 (0x1 << 24)
34#define EHCI_INSNREG00_ENA_INCR4 (0x1 << 23)
35#define EHCI_INSNREG00_ENA_INCRX_ALIGN (0x1 << 22)
36#define EHCI_INSNREG00_ENABLE_DMA_BURST \
37 (EHCI_INSNREG00_ENA_INCR16 | EHCI_INSNREG00_ENA_INCR8 | \
38 EHCI_INSNREG00_ENA_INCR4 | EHCI_INSNREG00_ENA_INCRX_ALIGN)
39
Jingoo Han29824c12013-10-10 16:42:47 +090040static const char hcd_name[] = "ehci-exynos";
41static struct hc_driver __read_mostly exynos_ehci_hc_driver;
Manjunath Goudar7edb3da2013-04-02 18:24:01 +020042
Kamil Debski1c176752014-05-05 10:32:28 +053043#define PHY_NUMBER 3
44
Jingoo Han29824c12013-10-10 16:42:47 +090045struct exynos_ehci_hcd {
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +090046 struct clk *clk;
Vivek Gautam46c1cda2014-09-29 11:54:14 +053047 struct phy *phy[PHY_NUMBER];
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +090048};
49
Jingoo Han29824c12013-10-10 16:42:47 +090050#define to_exynos_ehci(hcd) (struct exynos_ehci_hcd *)(hcd_to_ehci(hcd)->priv)
Vivek Gautamd233c192013-01-22 18:30:42 +053051
Kamil Debski1c176752014-05-05 10:32:28 +053052static int exynos_ehci_get_phy(struct device *dev,
53 struct exynos_ehci_hcd *exynos_ehci)
54{
55 struct device_node *child;
56 struct phy *phy;
57 int phy_number;
Vivek Gautam46c1cda2014-09-29 11:54:14 +053058 int ret;
Kamil Debski1c176752014-05-05 10:32:28 +053059
Vivek Gautam46c1cda2014-09-29 11:54:14 +053060 /* Get PHYs for the controller */
Kamil Debski1c176752014-05-05 10:32:28 +053061 for_each_available_child_of_node(dev->of_node, child) {
62 ret = of_property_read_u32(child, "reg", &phy_number);
63 if (ret) {
64 dev_err(dev, "Failed to parse device tree\n");
65 of_node_put(child);
66 return ret;
67 }
68
69 if (phy_number >= PHY_NUMBER) {
70 dev_err(dev, "Invalid number of PHYs\n");
71 of_node_put(child);
72 return -EINVAL;
73 }
74
Sachin Kamat14ad5a92014-06-06 14:13:45 +053075 phy = devm_of_phy_get(dev, child, NULL);
Vivek Gautam46c1cda2014-09-29 11:54:14 +053076 exynos_ehci->phy[phy_number] = phy;
Kamil Debski1c176752014-05-05 10:32:28 +053077 of_node_put(child);
Vivek Gautam46c1cda2014-09-29 11:54:14 +053078 if (IS_ERR(phy)) {
79 ret = PTR_ERR(phy);
80 if (ret == -EPROBE_DEFER) {
81 return ret;
82 } else if (ret != -ENOSYS && ret != -ENODEV) {
83 dev_err(dev,
84 "Error retrieving usb2 phy: %d\n", ret);
85 return ret;
86 }
87 }
Vivek Gautam2f7f41c2014-08-05 16:09:08 +053088 }
89
90 return 0;
Kamil Debski1c176752014-05-05 10:32:28 +053091}
92
93static int exynos_ehci_phy_enable(struct device *dev)
94{
95 struct usb_hcd *hcd = dev_get_drvdata(dev);
96 struct exynos_ehci_hcd *exynos_ehci = to_exynos_ehci(hcd);
97 int i;
98 int ret = 0;
99
Kamil Debski1c176752014-05-05 10:32:28 +0530100 for (i = 0; ret == 0 && i < PHY_NUMBER; i++)
Vivek Gautam46c1cda2014-09-29 11:54:14 +0530101 if (!IS_ERR(exynos_ehci->phy[i]))
102 ret = phy_power_on(exynos_ehci->phy[i]);
Kamil Debski1c176752014-05-05 10:32:28 +0530103 if (ret)
104 for (i--; i >= 0; i--)
Vivek Gautam46c1cda2014-09-29 11:54:14 +0530105 if (!IS_ERR(exynos_ehci->phy[i]))
106 phy_power_off(exynos_ehci->phy[i]);
Kamil Debski1c176752014-05-05 10:32:28 +0530107
108 return ret;
109}
110
111static void exynos_ehci_phy_disable(struct device *dev)
112{
113 struct usb_hcd *hcd = dev_get_drvdata(dev);
114 struct exynos_ehci_hcd *exynos_ehci = to_exynos_ehci(hcd);
115 int i;
116
Kamil Debski1c176752014-05-05 10:32:28 +0530117 for (i = 0; i < PHY_NUMBER; i++)
Vivek Gautam46c1cda2014-09-29 11:54:14 +0530118 if (!IS_ERR(exynos_ehci->phy[i]))
119 phy_power_off(exynos_ehci->phy[i]);
Kamil Debski1c176752014-05-05 10:32:28 +0530120}
121
Vivek Gautam91a96772014-05-05 10:34:25 +0530122static void exynos_setup_vbus_gpio(struct device *dev)
Vivek Gautamfd81d592012-07-17 10:10:50 +0530123{
124 int err;
125 int gpio;
126
Doug Anderson3f3b55b2013-03-14 20:15:37 -0700127 if (!dev->of_node)
Vivek Gautamfd81d592012-07-17 10:10:50 +0530128 return;
129
Doug Anderson3f3b55b2013-03-14 20:15:37 -0700130 gpio = of_get_named_gpio(dev->of_node, "samsung,vbus-gpio", 0);
Vivek Gautamfd81d592012-07-17 10:10:50 +0530131 if (!gpio_is_valid(gpio))
132 return;
133
Doug Anderson3f3b55b2013-03-14 20:15:37 -0700134 err = devm_gpio_request_one(dev, gpio, GPIOF_OUT_INIT_HIGH,
135 "ehci_vbus_gpio");
Vivek Gautamfd81d592012-07-17 10:10:50 +0530136 if (err)
Doug Anderson3f3b55b2013-03-14 20:15:37 -0700137 dev_err(dev, "can't request ehci vbus gpio %d", gpio);
Vivek Gautamfd81d592012-07-17 10:10:50 +0530138}
139
Jingoo Han29824c12013-10-10 16:42:47 +0900140static int exynos_ehci_probe(struct platform_device *pdev)
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900141{
Jingoo Han29824c12013-10-10 16:42:47 +0900142 struct exynos_ehci_hcd *exynos_ehci;
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900143 struct usb_hcd *hcd;
144 struct ehci_hcd *ehci;
145 struct resource *res;
146 int irq;
147 int err;
148
Vivek Gautam2c026e22012-07-16 11:25:37 +0530149 /*
150 * Right now device-tree probed devices don't get dma_mask set.
151 * Since shared usb code relies on it, set it here for now.
152 * Once we move to full device tree support this will vanish off.
153 */
Linus Torvalds8ceafbf2013-11-14 07:55:21 +0900154 err = dma_coerce_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
155 if (err)
156 return err;
Vivek Gautam2c026e22012-07-16 11:25:37 +0530157
Vivek Gautam91a96772014-05-05 10:34:25 +0530158 exynos_setup_vbus_gpio(&pdev->dev);
Vivek Gautamfd81d592012-07-17 10:10:50 +0530159
Jingoo Han29824c12013-10-10 16:42:47 +0900160 hcd = usb_create_hcd(&exynos_ehci_hc_driver,
Manjunath Goudar7edb3da2013-04-02 18:24:01 +0200161 &pdev->dev, dev_name(&pdev->dev));
162 if (!hcd) {
163 dev_err(&pdev->dev, "Unable to create HCD\n");
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900164 return -ENOMEM;
Manjunath Goudar7edb3da2013-04-02 18:24:01 +0200165 }
Jingoo Han29824c12013-10-10 16:42:47 +0900166 exynos_ehci = to_exynos_ehci(hcd);
Thomas Abrahame6b0166f2013-05-27 18:42:12 +0900167
168 if (of_device_is_compatible(pdev->dev.of_node,
Jingoo Han57ae1602013-10-10 16:41:19 +0900169 "samsung,exynos5440-ehci"))
Thomas Abrahame6b0166f2013-05-27 18:42:12 +0900170 goto skip_phy;
Thomas Abrahame6b0166f2013-05-27 18:42:12 +0900171
Kamil Debski1c176752014-05-05 10:32:28 +0530172 err = exynos_ehci_get_phy(&pdev->dev, exynos_ehci);
173 if (err)
174 goto fail_clk;
Vivek Gautamd233c192013-01-22 18:30:42 +0530175
Thomas Abrahame6b0166f2013-05-27 18:42:12 +0900176skip_phy:
177
Jingoo Han29824c12013-10-10 16:42:47 +0900178 exynos_ehci->clk = devm_clk_get(&pdev->dev, "usbhost");
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900179
Jingoo Han29824c12013-10-10 16:42:47 +0900180 if (IS_ERR(exynos_ehci->clk)) {
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900181 dev_err(&pdev->dev, "Failed to get usbhost clock\n");
Jingoo Han29824c12013-10-10 16:42:47 +0900182 err = PTR_ERR(exynos_ehci->clk);
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900183 goto fail_clk;
184 }
185
Jingoo Han29824c12013-10-10 16:42:47 +0900186 err = clk_prepare_enable(exynos_ehci->clk);
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900187 if (err)
Julia Lawall63fd0ae2012-07-30 16:43:44 +0200188 goto fail_clk;
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900189
190 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
191 if (!res) {
192 dev_err(&pdev->dev, "Failed to get I/O memory\n");
193 err = -ENXIO;
194 goto fail_io;
195 }
196
197 hcd->rsrc_start = res->start;
198 hcd->rsrc_len = resource_size(res);
Vivek Gautam4e24bde2014-05-10 17:30:05 +0530199 hcd->regs = devm_ioremap_resource(&pdev->dev, res);
200 if (IS_ERR(hcd->regs)) {
201 err = PTR_ERR(hcd->regs);
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900202 goto fail_io;
203 }
204
205 irq = platform_get_irq(pdev, 0);
206 if (!irq) {
207 dev_err(&pdev->dev, "Failed to get IRQ\n");
208 err = -ENODEV;
Jingoo Han9cb07562012-06-28 16:29:46 +0900209 goto fail_io;
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900210 }
211
Kamil Debski1c176752014-05-05 10:32:28 +0530212 err = exynos_ehci_phy_enable(&pdev->dev);
213 if (err) {
214 dev_err(&pdev->dev, "Failed to enable USB phy\n");
215 goto fail_io;
216 }
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900217
218 ehci = hcd_to_ehci(hcd);
219 ehci->caps = hcd->regs;
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900220
Jingoo Han88555a62012-03-05 10:40:14 +0900221 /* DMA burst Enable */
222 writel(EHCI_INSNREG00_ENABLE_DMA_BURST, EHCI_INSNREG00(hcd->regs));
223
Yong Zhangb5dd18d2011-09-07 16:10:52 +0800224 err = usb_add_hcd(hcd, irq, IRQF_SHARED);
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900225 if (err) {
226 dev_err(&pdev->dev, "Failed to add USB HCD\n");
Vivek Gautamd233c192013-01-22 18:30:42 +0530227 goto fail_add_hcd;
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900228 }
Peter Chen3c9740a2013-11-05 10:46:02 +0800229 device_wakeup_enable(hcd->self.controller);
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900230
Vivek Gautambbcd85a2013-04-09 18:42:11 +0530231 platform_set_drvdata(pdev, hcd);
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900232
233 return 0;
234
Vivek Gautamd233c192013-01-22 18:30:42 +0530235fail_add_hcd:
Kamil Debski1c176752014-05-05 10:32:28 +0530236 exynos_ehci_phy_disable(&pdev->dev);
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900237fail_io:
Jingoo Han29824c12013-10-10 16:42:47 +0900238 clk_disable_unprepare(exynos_ehci->clk);
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900239fail_clk:
240 usb_put_hcd(hcd);
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900241 return err;
242}
243
Jingoo Han29824c12013-10-10 16:42:47 +0900244static int exynos_ehci_remove(struct platform_device *pdev)
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900245{
Manjunath Goudar7edb3da2013-04-02 18:24:01 +0200246 struct usb_hcd *hcd = platform_get_drvdata(pdev);
Jingoo Han29824c12013-10-10 16:42:47 +0900247 struct exynos_ehci_hcd *exynos_ehci = to_exynos_ehci(hcd);
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900248
249 usb_remove_hcd(hcd);
250
Kamil Debski1c176752014-05-05 10:32:28 +0530251 exynos_ehci_phy_disable(&pdev->dev);
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900252
Jingoo Han29824c12013-10-10 16:42:47 +0900253 clk_disable_unprepare(exynos_ehci->clk);
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900254
255 usb_put_hcd(hcd);
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900256
257 return 0;
258}
259
Jingoo Han1acb30e2011-05-20 20:48:33 +0900260#ifdef CONFIG_PM
Jingoo Han29824c12013-10-10 16:42:47 +0900261static int exynos_ehci_suspend(struct device *dev)
Jingoo Han1acb30e2011-05-20 20:48:33 +0900262{
Manjunath Goudar7edb3da2013-04-02 18:24:01 +0200263 struct usb_hcd *hcd = dev_get_drvdata(dev);
Jingoo Han29824c12013-10-10 16:42:47 +0900264 struct exynos_ehci_hcd *exynos_ehci = to_exynos_ehci(hcd);
Manjunath Goudar7edb3da2013-04-02 18:24:01 +0200265
Alan Sternc5cf9212012-06-28 11:19:02 -0400266 bool do_wakeup = device_may_wakeup(dev);
Alan Sternc5cf9212012-06-28 11:19:02 -0400267 int rc;
Jingoo Han1acb30e2011-05-20 20:48:33 +0900268
Alan Sternc5cf9212012-06-28 11:19:02 -0400269 rc = ehci_suspend(hcd, do_wakeup);
Vivek Gautamd7217512014-04-10 15:58:01 +0530270 if (rc)
271 return rc;
Jingoo Han1acb30e2011-05-20 20:48:33 +0900272
Kamil Debski1c176752014-05-05 10:32:28 +0530273 exynos_ehci_phy_disable(dev);
Jingoo Han1acb30e2011-05-20 20:48:33 +0900274
Jingoo Han29824c12013-10-10 16:42:47 +0900275 clk_disable_unprepare(exynos_ehci->clk);
Jingoo Han8b4fc8c2012-04-13 11:06:36 +0900276
Jingoo Han1acb30e2011-05-20 20:48:33 +0900277 return rc;
278}
279
Jingoo Han29824c12013-10-10 16:42:47 +0900280static int exynos_ehci_resume(struct device *dev)
Jingoo Han1acb30e2011-05-20 20:48:33 +0900281{
Manjunath Goudar7edb3da2013-04-02 18:24:01 +0200282 struct usb_hcd *hcd = dev_get_drvdata(dev);
Jingoo Han29824c12013-10-10 16:42:47 +0900283 struct exynos_ehci_hcd *exynos_ehci = to_exynos_ehci(hcd);
Kamil Debski1c176752014-05-05 10:32:28 +0530284 int ret;
Jingoo Han1acb30e2011-05-20 20:48:33 +0900285
Jingoo Han29824c12013-10-10 16:42:47 +0900286 clk_prepare_enable(exynos_ehci->clk);
Jingoo Han8b4fc8c2012-04-13 11:06:36 +0900287
Kamil Debski1c176752014-05-05 10:32:28 +0530288 ret = exynos_ehci_phy_enable(dev);
289 if (ret) {
290 dev_err(dev, "Failed to enable USB phy\n");
291 clk_disable_unprepare(exynos_ehci->clk);
292 return ret;
293 }
Jingoo Han1acb30e2011-05-20 20:48:33 +0900294
Jingoo Han88555a62012-03-05 10:40:14 +0900295 /* DMA burst Enable */
296 writel(EHCI_INSNREG00_ENABLE_DMA_BURST, EHCI_INSNREG00(hcd->regs));
297
Alan Sternc5cf9212012-06-28 11:19:02 -0400298 ehci_resume(hcd, false);
Jingoo Han1acb30e2011-05-20 20:48:33 +0900299 return 0;
300}
301#else
Jingoo Han29824c12013-10-10 16:42:47 +0900302#define exynos_ehci_suspend NULL
303#define exynos_ehci_resume NULL
Jingoo Han1acb30e2011-05-20 20:48:33 +0900304#endif
305
Jingoo Han29824c12013-10-10 16:42:47 +0900306static const struct dev_pm_ops exynos_ehci_pm_ops = {
307 .suspend = exynos_ehci_suspend,
308 .resume = exynos_ehci_resume,
Jingoo Han1acb30e2011-05-20 20:48:33 +0900309};
310
Vivek Gautam2c026e22012-07-16 11:25:37 +0530311#ifdef CONFIG_OF
312static const struct of_device_id exynos_ehci_match[] = {
Vivek Gautam6e247772013-01-24 19:15:29 +0530313 { .compatible = "samsung,exynos4210-ehci" },
Thomas Abrahame6b0166f2013-05-27 18:42:12 +0900314 { .compatible = "samsung,exynos5440-ehci" },
Vivek Gautam2c026e22012-07-16 11:25:37 +0530315 {},
316};
317MODULE_DEVICE_TABLE(of, exynos_ehci_match);
318#endif
319
Jingoo Han29824c12013-10-10 16:42:47 +0900320static struct platform_driver exynos_ehci_driver = {
321 .probe = exynos_ehci_probe,
322 .remove = exynos_ehci_remove,
Roger Quadrosaaf6b522013-07-22 15:04:50 +0300323 .shutdown = usb_hcd_platform_shutdown,
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900324 .driver = {
Jingoo Han29824c12013-10-10 16:42:47 +0900325 .name = "exynos-ehci",
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900326 .owner = THIS_MODULE,
Jingoo Han29824c12013-10-10 16:42:47 +0900327 .pm = &exynos_ehci_pm_ops,
Vivek Gautam2c026e22012-07-16 11:25:37 +0530328 .of_match_table = of_match_ptr(exynos_ehci_match),
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900329 }
330};
Jingoo Han29824c12013-10-10 16:42:47 +0900331static const struct ehci_driver_overrides exynos_overrides __initdata = {
332 .extra_priv_size = sizeof(struct exynos_ehci_hcd),
Manjunath Goudar7edb3da2013-04-02 18:24:01 +0200333};
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900334
Jingoo Han29824c12013-10-10 16:42:47 +0900335static int __init ehci_exynos_init(void)
Manjunath Goudar7edb3da2013-04-02 18:24:01 +0200336{
337 if (usb_disabled())
338 return -ENODEV;
339
340 pr_info("%s: " DRIVER_DESC "\n", hcd_name);
Jingoo Han29824c12013-10-10 16:42:47 +0900341 ehci_init_driver(&exynos_ehci_hc_driver, &exynos_overrides);
342 return platform_driver_register(&exynos_ehci_driver);
Manjunath Goudar7edb3da2013-04-02 18:24:01 +0200343}
Jingoo Han29824c12013-10-10 16:42:47 +0900344module_init(ehci_exynos_init);
Manjunath Goudar7edb3da2013-04-02 18:24:01 +0200345
Jingoo Han29824c12013-10-10 16:42:47 +0900346static void __exit ehci_exynos_cleanup(void)
Manjunath Goudar7edb3da2013-04-02 18:24:01 +0200347{
Jingoo Han29824c12013-10-10 16:42:47 +0900348 platform_driver_unregister(&exynos_ehci_driver);
Manjunath Goudar7edb3da2013-04-02 18:24:01 +0200349}
Jingoo Han29824c12013-10-10 16:42:47 +0900350module_exit(ehci_exynos_cleanup);
Manjunath Goudar7edb3da2013-04-02 18:24:01 +0200351
352MODULE_DESCRIPTION(DRIVER_DESC);
Jingoo Han29824c12013-10-10 16:42:47 +0900353MODULE_ALIAS("platform:exynos-ehci");
Manjunath Goudar7edb3da2013-04-02 18:24:01 +0200354MODULE_AUTHOR("Jingoo Han");
355MODULE_AUTHOR("Joonyoung Shim");
356MODULE_LICENSE("GPL v2");