blob: 379037f51a2fc41b7e7aaaf112a759ce70615ac5 [file] [log] [blame]
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +09001/*
2 * SAMSUNG S5P USB HOST EHCI Controller
3 *
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>
Arnd Bergmann436d42c2012-08-24 15:22:12 +020023#include <linux/platform_data/usb-ehci-s5p.h>
Vivek Gautamd233c192013-01-22 18:30:42 +053024#include <linux/usb/phy.h>
Vivek Gautamb506eeb2013-01-22 18:30:40 +053025#include <linux/usb/samsung_usb_phy.h>
Manjunath Goudar7edb3da2013-04-02 18:24:01 +020026#include <linux/usb.h>
27#include <linux/usb/hcd.h>
28#include <linux/usb/otg.h>
29
30#include "ehci.h"
31
32#define DRIVER_DESC "EHCI s5p driver"
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +090033
Jingoo Han88555a62012-03-05 10:40:14 +090034#define EHCI_INSNREG00(base) (base + 0x90)
35#define EHCI_INSNREG00_ENA_INCR16 (0x1 << 25)
36#define EHCI_INSNREG00_ENA_INCR8 (0x1 << 24)
37#define EHCI_INSNREG00_ENA_INCR4 (0x1 << 23)
38#define EHCI_INSNREG00_ENA_INCRX_ALIGN (0x1 << 22)
39#define EHCI_INSNREG00_ENABLE_DMA_BURST \
40 (EHCI_INSNREG00_ENA_INCR16 | EHCI_INSNREG00_ENA_INCR8 | \
41 EHCI_INSNREG00_ENA_INCR4 | EHCI_INSNREG00_ENA_INCRX_ALIGN)
42
Manjunath Goudar7edb3da2013-04-02 18:24:01 +020043static const char hcd_name[] = "ehci-s5p";
44static struct hc_driver __read_mostly s5p_ehci_hc_driver;
45
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +090046struct s5p_ehci_hcd {
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +090047 struct clk *clk;
Vivek Gautamd233c192013-01-22 18:30:42 +053048 struct usb_phy *phy;
49 struct usb_otg *otg;
50 struct s5p_ehci_platdata *pdata;
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +090051};
52
Manjunath Goudar7edb3da2013-04-02 18:24:01 +020053#define to_s5p_ehci(hcd) (struct s5p_ehci_hcd *)(hcd_to_ehci(hcd)->priv)
Vivek Gautamd233c192013-01-22 18:30:42 +053054
Vivek Gautamfd81d592012-07-17 10:10:50 +053055static void s5p_setup_vbus_gpio(struct platform_device *pdev)
56{
Doug Anderson3f3b55b2013-03-14 20:15:37 -070057 struct device *dev = &pdev->dev;
Vivek Gautamfd81d592012-07-17 10:10:50 +053058 int err;
59 int gpio;
60
Doug Anderson3f3b55b2013-03-14 20:15:37 -070061 if (!dev->of_node)
Vivek Gautamfd81d592012-07-17 10:10:50 +053062 return;
63
Doug Anderson3f3b55b2013-03-14 20:15:37 -070064 gpio = of_get_named_gpio(dev->of_node, "samsung,vbus-gpio", 0);
Vivek Gautamfd81d592012-07-17 10:10:50 +053065 if (!gpio_is_valid(gpio))
66 return;
67
Doug Anderson3f3b55b2013-03-14 20:15:37 -070068 err = devm_gpio_request_one(dev, gpio, GPIOF_OUT_INIT_HIGH,
69 "ehci_vbus_gpio");
Vivek Gautamfd81d592012-07-17 10:10:50 +053070 if (err)
Doug Anderson3f3b55b2013-03-14 20:15:37 -070071 dev_err(dev, "can't request ehci vbus gpio %d", gpio);
Vivek Gautamfd81d592012-07-17 10:10:50 +053072}
73
Bill Pemberton41ac7b32012-11-19 13:21:48 -050074static int s5p_ehci_probe(struct platform_device *pdev)
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +090075{
Vivek Gautamd233c192013-01-22 18:30:42 +053076 struct s5p_ehci_platdata *pdata = pdev->dev.platform_data;
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +090077 struct s5p_ehci_hcd *s5p_ehci;
78 struct usb_hcd *hcd;
79 struct ehci_hcd *ehci;
80 struct resource *res;
Vivek Gautamd233c192013-01-22 18:30:42 +053081 struct usb_phy *phy;
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +090082 int irq;
83 int err;
84
Vivek Gautam2c026e22012-07-16 11:25:37 +053085 /*
86 * Right now device-tree probed devices don't get dma_mask set.
87 * Since shared usb code relies on it, set it here for now.
88 * Once we move to full device tree support this will vanish off.
89 */
90 if (!pdev->dev.dma_mask)
Stephen Warren3b9561e2013-05-07 16:53:52 -060091 pdev->dev.dma_mask = &pdev->dev.coherent_dma_mask;
Vivek Gautam2c026e22012-07-16 11:25:37 +053092 if (!pdev->dev.coherent_dma_mask)
93 pdev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
94
Vivek Gautamfd81d592012-07-17 10:10:50 +053095 s5p_setup_vbus_gpio(pdev);
96
Manjunath Goudar7edb3da2013-04-02 18:24:01 +020097 hcd = usb_create_hcd(&s5p_ehci_hc_driver,
98 &pdev->dev, dev_name(&pdev->dev));
99 if (!hcd) {
100 dev_err(&pdev->dev, "Unable to create HCD\n");
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900101 return -ENOMEM;
Manjunath Goudar7edb3da2013-04-02 18:24:01 +0200102 }
103 s5p_ehci = to_s5p_ehci(hcd);
Vivek Gautamd233c192013-01-22 18:30:42 +0530104 phy = devm_usb_get_phy(&pdev->dev, USB_PHY_TYPE_USB2);
Felipe Balbia16283e2013-03-15 11:04:15 +0200105 if (IS_ERR(phy)) {
Vivek Gautamd233c192013-01-22 18:30:42 +0530106 /* Fallback to pdata */
107 if (!pdata) {
Libo Chen9a9ef732013-05-09 12:58:08 +0800108 usb_put_hcd(hcd);
Vivek Gautamd233c192013-01-22 18:30:42 +0530109 dev_warn(&pdev->dev, "no platform data or transceiver defined\n");
110 return -EPROBE_DEFER;
111 } else {
112 s5p_ehci->pdata = pdata;
113 }
114 } else {
115 s5p_ehci->phy = phy;
116 s5p_ehci->otg = phy->otg;
117 }
118
Julia Lawall63fd0ae2012-07-30 16:43:44 +0200119 s5p_ehci->clk = devm_clk_get(&pdev->dev, "usbhost");
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900120
121 if (IS_ERR(s5p_ehci->clk)) {
122 dev_err(&pdev->dev, "Failed to get usbhost clock\n");
123 err = PTR_ERR(s5p_ehci->clk);
124 goto fail_clk;
125 }
126
Thomas Abrahame1deb562012-10-03 08:40:42 +0900127 err = clk_prepare_enable(s5p_ehci->clk);
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900128 if (err)
Julia Lawall63fd0ae2012-07-30 16:43:44 +0200129 goto fail_clk;
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900130
131 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
132 if (!res) {
133 dev_err(&pdev->dev, "Failed to get I/O memory\n");
134 err = -ENXIO;
135 goto fail_io;
136 }
137
138 hcd->rsrc_start = res->start;
139 hcd->rsrc_len = resource_size(res);
Jingoo Han9cb07562012-06-28 16:29:46 +0900140 hcd->regs = devm_ioremap(&pdev->dev, res->start, hcd->rsrc_len);
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900141 if (!hcd->regs) {
142 dev_err(&pdev->dev, "Failed to remap I/O memory\n");
143 err = -ENOMEM;
144 goto fail_io;
145 }
146
147 irq = platform_get_irq(pdev, 0);
148 if (!irq) {
149 dev_err(&pdev->dev, "Failed to get IRQ\n");
150 err = -ENODEV;
Jingoo Han9cb07562012-06-28 16:29:46 +0900151 goto fail_io;
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900152 }
153
Vivek Gautamd233c192013-01-22 18:30:42 +0530154 if (s5p_ehci->otg)
Manjunath Goudar7edb3da2013-04-02 18:24:01 +0200155 s5p_ehci->otg->set_host(s5p_ehci->otg, &hcd->self);
Vivek Gautamd233c192013-01-22 18:30:42 +0530156
Manjunath Goudar7edb3da2013-04-02 18:24:01 +0200157 if (s5p_ehci->phy)
158 usb_phy_init(s5p_ehci->phy);
159 else if (s5p_ehci->pdata->phy_init)
160 s5p_ehci->pdata->phy_init(pdev, USB_PHY_TYPE_HOST);
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900161
162 ehci = hcd_to_ehci(hcd);
163 ehci->caps = hcd->regs;
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900164
Jingoo Han88555a62012-03-05 10:40:14 +0900165 /* DMA burst Enable */
166 writel(EHCI_INSNREG00_ENABLE_DMA_BURST, EHCI_INSNREG00(hcd->regs));
167
Yong Zhangb5dd18d2011-09-07 16:10:52 +0800168 err = usb_add_hcd(hcd, irq, IRQF_SHARED);
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900169 if (err) {
170 dev_err(&pdev->dev, "Failed to add USB HCD\n");
Vivek Gautamd233c192013-01-22 18:30:42 +0530171 goto fail_add_hcd;
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900172 }
173
Vivek Gautambbcd85a2013-04-09 18:42:11 +0530174 platform_set_drvdata(pdev, hcd);
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900175
176 return 0;
177
Vivek Gautamd233c192013-01-22 18:30:42 +0530178fail_add_hcd:
Manjunath Goudar7edb3da2013-04-02 18:24:01 +0200179 if (s5p_ehci->phy)
180 usb_phy_shutdown(s5p_ehci->phy);
181 else if (s5p_ehci->pdata->phy_exit)
182 s5p_ehci->pdata->phy_exit(pdev, USB_PHY_TYPE_HOST);
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900183fail_io:
Thomas Abrahame1deb562012-10-03 08:40:42 +0900184 clk_disable_unprepare(s5p_ehci->clk);
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900185fail_clk:
186 usb_put_hcd(hcd);
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900187 return err;
188}
189
Bill Pembertonfb4e98a2012-11-19 13:26:20 -0500190static int s5p_ehci_remove(struct platform_device *pdev)
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900191{
Manjunath Goudar7edb3da2013-04-02 18:24:01 +0200192 struct usb_hcd *hcd = platform_get_drvdata(pdev);
193 struct s5p_ehci_hcd *s5p_ehci = to_s5p_ehci(hcd);
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900194
195 usb_remove_hcd(hcd);
196
Vivek Gautamd233c192013-01-22 18:30:42 +0530197 if (s5p_ehci->otg)
Manjunath Goudar7edb3da2013-04-02 18:24:01 +0200198 s5p_ehci->otg->set_host(s5p_ehci->otg, &hcd->self);
Vivek Gautamd233c192013-01-22 18:30:42 +0530199
Manjunath Goudar7edb3da2013-04-02 18:24:01 +0200200 if (s5p_ehci->phy)
201 usb_phy_shutdown(s5p_ehci->phy);
202 else if (s5p_ehci->pdata->phy_exit)
203 s5p_ehci->pdata->phy_exit(pdev, USB_PHY_TYPE_HOST);
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900204
Thomas Abrahame1deb562012-10-03 08:40:42 +0900205 clk_disable_unprepare(s5p_ehci->clk);
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900206
207 usb_put_hcd(hcd);
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900208
209 return 0;
210}
211
212static void s5p_ehci_shutdown(struct platform_device *pdev)
213{
Manjunath Goudar7edb3da2013-04-02 18:24:01 +0200214 struct usb_hcd *hcd = platform_get_drvdata(pdev);
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900215
216 if (hcd->driver->shutdown)
217 hcd->driver->shutdown(hcd);
218}
219
Jingoo Han1acb30e2011-05-20 20:48:33 +0900220#ifdef CONFIG_PM
221static int s5p_ehci_suspend(struct device *dev)
222{
Manjunath Goudar7edb3da2013-04-02 18:24:01 +0200223 struct usb_hcd *hcd = dev_get_drvdata(dev);
224 struct s5p_ehci_hcd *s5p_ehci = to_s5p_ehci(hcd);
225 struct platform_device *pdev = to_platform_device(dev);
226
Alan Sternc5cf9212012-06-28 11:19:02 -0400227 bool do_wakeup = device_may_wakeup(dev);
Alan Sternc5cf9212012-06-28 11:19:02 -0400228 int rc;
Jingoo Han1acb30e2011-05-20 20:48:33 +0900229
Alan Sternc5cf9212012-06-28 11:19:02 -0400230 rc = ehci_suspend(hcd, do_wakeup);
Jingoo Han1acb30e2011-05-20 20:48:33 +0900231
Vivek Gautamd233c192013-01-22 18:30:42 +0530232 if (s5p_ehci->otg)
Manjunath Goudar7edb3da2013-04-02 18:24:01 +0200233 s5p_ehci->otg->set_host(s5p_ehci->otg, &hcd->self);
Vivek Gautamd233c192013-01-22 18:30:42 +0530234
Manjunath Goudar7edb3da2013-04-02 18:24:01 +0200235 if (s5p_ehci->phy)
236 usb_phy_shutdown(s5p_ehci->phy);
237 else if (s5p_ehci->pdata->phy_exit)
238 s5p_ehci->pdata->phy_exit(pdev, USB_PHY_TYPE_HOST);
Jingoo Han1acb30e2011-05-20 20:48:33 +0900239
Thomas Abrahame1deb562012-10-03 08:40:42 +0900240 clk_disable_unprepare(s5p_ehci->clk);
Jingoo Han8b4fc8c2012-04-13 11:06:36 +0900241
Jingoo Han1acb30e2011-05-20 20:48:33 +0900242 return rc;
243}
244
245static int s5p_ehci_resume(struct device *dev)
246{
Manjunath Goudar7edb3da2013-04-02 18:24:01 +0200247 struct usb_hcd *hcd = dev_get_drvdata(dev);
248 struct s5p_ehci_hcd *s5p_ehci = to_s5p_ehci(hcd);
249 struct platform_device *pdev = to_platform_device(dev);
Jingoo Han1acb30e2011-05-20 20:48:33 +0900250
Thomas Abrahame1deb562012-10-03 08:40:42 +0900251 clk_prepare_enable(s5p_ehci->clk);
Jingoo Han8b4fc8c2012-04-13 11:06:36 +0900252
Vivek Gautamd233c192013-01-22 18:30:42 +0530253 if (s5p_ehci->otg)
Manjunath Goudar7edb3da2013-04-02 18:24:01 +0200254 s5p_ehci->otg->set_host(s5p_ehci->otg, &hcd->self);
Vivek Gautamd233c192013-01-22 18:30:42 +0530255
Manjunath Goudar7edb3da2013-04-02 18:24:01 +0200256 if (s5p_ehci->phy)
257 usb_phy_init(s5p_ehci->phy);
258 else if (s5p_ehci->pdata->phy_init)
259 s5p_ehci->pdata->phy_init(pdev, USB_PHY_TYPE_HOST);
Jingoo Han1acb30e2011-05-20 20:48:33 +0900260
Jingoo Han88555a62012-03-05 10:40:14 +0900261 /* DMA burst Enable */
262 writel(EHCI_INSNREG00_ENABLE_DMA_BURST, EHCI_INSNREG00(hcd->regs));
263
Alan Sternc5cf9212012-06-28 11:19:02 -0400264 ehci_resume(hcd, false);
Jingoo Han1acb30e2011-05-20 20:48:33 +0900265 return 0;
266}
267#else
268#define s5p_ehci_suspend NULL
269#define s5p_ehci_resume NULL
270#endif
271
272static const struct dev_pm_ops s5p_ehci_pm_ops = {
273 .suspend = s5p_ehci_suspend,
274 .resume = s5p_ehci_resume,
275};
276
Vivek Gautam2c026e22012-07-16 11:25:37 +0530277#ifdef CONFIG_OF
278static const struct of_device_id exynos_ehci_match[] = {
Vivek Gautam6e247772013-01-24 19:15:29 +0530279 { .compatible = "samsung,exynos4210-ehci" },
Vivek Gautam2c026e22012-07-16 11:25:37 +0530280 {},
281};
282MODULE_DEVICE_TABLE(of, exynos_ehci_match);
283#endif
284
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900285static struct platform_driver s5p_ehci_driver = {
286 .probe = s5p_ehci_probe,
Bill Pemberton76904172012-11-19 13:21:08 -0500287 .remove = s5p_ehci_remove,
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900288 .shutdown = s5p_ehci_shutdown,
289 .driver = {
290 .name = "s5p-ehci",
291 .owner = THIS_MODULE,
Jingoo Han1acb30e2011-05-20 20:48:33 +0900292 .pm = &s5p_ehci_pm_ops,
Vivek Gautam2c026e22012-07-16 11:25:37 +0530293 .of_match_table = of_match_ptr(exynos_ehci_match),
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900294 }
295};
Manjunath Goudar7edb3da2013-04-02 18:24:01 +0200296static const struct ehci_driver_overrides s5p_overrides __initdata = {
297 .extra_priv_size = sizeof(struct s5p_ehci_hcd),
298};
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900299
Manjunath Goudar7edb3da2013-04-02 18:24:01 +0200300static int __init ehci_s5p_init(void)
301{
302 if (usb_disabled())
303 return -ENODEV;
304
305 pr_info("%s: " DRIVER_DESC "\n", hcd_name);
306 ehci_init_driver(&s5p_ehci_hc_driver, &s5p_overrides);
307 return platform_driver_register(&s5p_ehci_driver);
308}
309module_init(ehci_s5p_init);
310
311static void __exit ehci_s5p_cleanup(void)
312{
313 platform_driver_unregister(&s5p_ehci_driver);
314}
315module_exit(ehci_s5p_cleanup);
316
317MODULE_DESCRIPTION(DRIVER_DESC);
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900318MODULE_ALIAS("platform:s5p-ehci");
Manjunath Goudar7edb3da2013-04-02 18:24:01 +0200319MODULE_AUTHOR("Jingoo Han");
320MODULE_AUTHOR("Joonyoung Shim");
321MODULE_LICENSE("GPL v2");