blob: a81465ed48db9e78391d377b3fc59a7c2f2dd01c [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) {
108 dev_warn(&pdev->dev, "no platform data or transceiver defined\n");
109 return -EPROBE_DEFER;
110 } else {
111 s5p_ehci->pdata = pdata;
112 }
113 } else {
114 s5p_ehci->phy = phy;
115 s5p_ehci->otg = phy->otg;
116 }
117
Julia Lawall63fd0ae2012-07-30 16:43:44 +0200118 s5p_ehci->clk = devm_clk_get(&pdev->dev, "usbhost");
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900119
120 if (IS_ERR(s5p_ehci->clk)) {
121 dev_err(&pdev->dev, "Failed to get usbhost clock\n");
122 err = PTR_ERR(s5p_ehci->clk);
123 goto fail_clk;
124 }
125
Thomas Abrahame1deb562012-10-03 08:40:42 +0900126 err = clk_prepare_enable(s5p_ehci->clk);
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900127 if (err)
Julia Lawall63fd0ae2012-07-30 16:43:44 +0200128 goto fail_clk;
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900129
130 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
131 if (!res) {
132 dev_err(&pdev->dev, "Failed to get I/O memory\n");
133 err = -ENXIO;
134 goto fail_io;
135 }
136
137 hcd->rsrc_start = res->start;
138 hcd->rsrc_len = resource_size(res);
Jingoo Han9cb07562012-06-28 16:29:46 +0900139 hcd->regs = devm_ioremap(&pdev->dev, res->start, hcd->rsrc_len);
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900140 if (!hcd->regs) {
141 dev_err(&pdev->dev, "Failed to remap I/O memory\n");
142 err = -ENOMEM;
143 goto fail_io;
144 }
145
146 irq = platform_get_irq(pdev, 0);
147 if (!irq) {
148 dev_err(&pdev->dev, "Failed to get IRQ\n");
149 err = -ENODEV;
Jingoo Han9cb07562012-06-28 16:29:46 +0900150 goto fail_io;
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900151 }
152
Vivek Gautamd233c192013-01-22 18:30:42 +0530153 if (s5p_ehci->otg)
Manjunath Goudar7edb3da2013-04-02 18:24:01 +0200154 s5p_ehci->otg->set_host(s5p_ehci->otg, &hcd->self);
Vivek Gautamd233c192013-01-22 18:30:42 +0530155
Manjunath Goudar7edb3da2013-04-02 18:24:01 +0200156 if (s5p_ehci->phy)
157 usb_phy_init(s5p_ehci->phy);
158 else if (s5p_ehci->pdata->phy_init)
159 s5p_ehci->pdata->phy_init(pdev, USB_PHY_TYPE_HOST);
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900160
161 ehci = hcd_to_ehci(hcd);
162 ehci->caps = hcd->regs;
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900163
Jingoo Han88555a62012-03-05 10:40:14 +0900164 /* DMA burst Enable */
165 writel(EHCI_INSNREG00_ENABLE_DMA_BURST, EHCI_INSNREG00(hcd->regs));
166
Yong Zhangb5dd18d2011-09-07 16:10:52 +0800167 err = usb_add_hcd(hcd, irq, IRQF_SHARED);
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900168 if (err) {
169 dev_err(&pdev->dev, "Failed to add USB HCD\n");
Vivek Gautamd233c192013-01-22 18:30:42 +0530170 goto fail_add_hcd;
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900171 }
172
Vivek Gautambbcd85a2013-04-09 18:42:11 +0530173 platform_set_drvdata(pdev, hcd);
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900174
175 return 0;
176
Vivek Gautamd233c192013-01-22 18:30:42 +0530177fail_add_hcd:
Manjunath Goudar7edb3da2013-04-02 18:24:01 +0200178 if (s5p_ehci->phy)
179 usb_phy_shutdown(s5p_ehci->phy);
180 else if (s5p_ehci->pdata->phy_exit)
181 s5p_ehci->pdata->phy_exit(pdev, USB_PHY_TYPE_HOST);
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900182fail_io:
Thomas Abrahame1deb562012-10-03 08:40:42 +0900183 clk_disable_unprepare(s5p_ehci->clk);
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900184fail_clk:
185 usb_put_hcd(hcd);
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900186 return err;
187}
188
Bill Pembertonfb4e98a2012-11-19 13:26:20 -0500189static int s5p_ehci_remove(struct platform_device *pdev)
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900190{
Manjunath Goudar7edb3da2013-04-02 18:24:01 +0200191 struct usb_hcd *hcd = platform_get_drvdata(pdev);
192 struct s5p_ehci_hcd *s5p_ehci = to_s5p_ehci(hcd);
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900193
194 usb_remove_hcd(hcd);
195
Vivek Gautamd233c192013-01-22 18:30:42 +0530196 if (s5p_ehci->otg)
Manjunath Goudar7edb3da2013-04-02 18:24:01 +0200197 s5p_ehci->otg->set_host(s5p_ehci->otg, &hcd->self);
Vivek Gautamd233c192013-01-22 18:30:42 +0530198
Manjunath Goudar7edb3da2013-04-02 18:24:01 +0200199 if (s5p_ehci->phy)
200 usb_phy_shutdown(s5p_ehci->phy);
201 else if (s5p_ehci->pdata->phy_exit)
202 s5p_ehci->pdata->phy_exit(pdev, USB_PHY_TYPE_HOST);
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900203
Thomas Abrahame1deb562012-10-03 08:40:42 +0900204 clk_disable_unprepare(s5p_ehci->clk);
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900205
206 usb_put_hcd(hcd);
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900207
208 return 0;
209}
210
211static void s5p_ehci_shutdown(struct platform_device *pdev)
212{
Manjunath Goudar7edb3da2013-04-02 18:24:01 +0200213 struct usb_hcd *hcd = platform_get_drvdata(pdev);
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900214
215 if (hcd->driver->shutdown)
216 hcd->driver->shutdown(hcd);
217}
218
Jingoo Han1acb30e2011-05-20 20:48:33 +0900219#ifdef CONFIG_PM
220static int s5p_ehci_suspend(struct device *dev)
221{
Manjunath Goudar7edb3da2013-04-02 18:24:01 +0200222 struct usb_hcd *hcd = dev_get_drvdata(dev);
223 struct s5p_ehci_hcd *s5p_ehci = to_s5p_ehci(hcd);
224 struct platform_device *pdev = to_platform_device(dev);
225
Alan Sternc5cf9212012-06-28 11:19:02 -0400226 bool do_wakeup = device_may_wakeup(dev);
Alan Sternc5cf9212012-06-28 11:19:02 -0400227 int rc;
Jingoo Han1acb30e2011-05-20 20:48:33 +0900228
Alan Sternc5cf9212012-06-28 11:19:02 -0400229 rc = ehci_suspend(hcd, do_wakeup);
Jingoo Han1acb30e2011-05-20 20:48:33 +0900230
Vivek Gautamd233c192013-01-22 18:30:42 +0530231 if (s5p_ehci->otg)
Manjunath Goudar7edb3da2013-04-02 18:24:01 +0200232 s5p_ehci->otg->set_host(s5p_ehci->otg, &hcd->self);
Vivek Gautamd233c192013-01-22 18:30:42 +0530233
Manjunath Goudar7edb3da2013-04-02 18:24:01 +0200234 if (s5p_ehci->phy)
235 usb_phy_shutdown(s5p_ehci->phy);
236 else if (s5p_ehci->pdata->phy_exit)
237 s5p_ehci->pdata->phy_exit(pdev, USB_PHY_TYPE_HOST);
Jingoo Han1acb30e2011-05-20 20:48:33 +0900238
Thomas Abrahame1deb562012-10-03 08:40:42 +0900239 clk_disable_unprepare(s5p_ehci->clk);
Jingoo Han8b4fc8c2012-04-13 11:06:36 +0900240
Jingoo Han1acb30e2011-05-20 20:48:33 +0900241 return rc;
242}
243
244static int s5p_ehci_resume(struct device *dev)
245{
Manjunath Goudar7edb3da2013-04-02 18:24:01 +0200246 struct usb_hcd *hcd = dev_get_drvdata(dev);
247 struct s5p_ehci_hcd *s5p_ehci = to_s5p_ehci(hcd);
248 struct platform_device *pdev = to_platform_device(dev);
Jingoo Han1acb30e2011-05-20 20:48:33 +0900249
Thomas Abrahame1deb562012-10-03 08:40:42 +0900250 clk_prepare_enable(s5p_ehci->clk);
Jingoo Han8b4fc8c2012-04-13 11:06:36 +0900251
Vivek Gautamd233c192013-01-22 18:30:42 +0530252 if (s5p_ehci->otg)
Manjunath Goudar7edb3da2013-04-02 18:24:01 +0200253 s5p_ehci->otg->set_host(s5p_ehci->otg, &hcd->self);
Vivek Gautamd233c192013-01-22 18:30:42 +0530254
Manjunath Goudar7edb3da2013-04-02 18:24:01 +0200255 if (s5p_ehci->phy)
256 usb_phy_init(s5p_ehci->phy);
257 else if (s5p_ehci->pdata->phy_init)
258 s5p_ehci->pdata->phy_init(pdev, USB_PHY_TYPE_HOST);
Jingoo Han1acb30e2011-05-20 20:48:33 +0900259
Jingoo Han88555a62012-03-05 10:40:14 +0900260 /* DMA burst Enable */
261 writel(EHCI_INSNREG00_ENABLE_DMA_BURST, EHCI_INSNREG00(hcd->regs));
262
Alan Sternc5cf9212012-06-28 11:19:02 -0400263 ehci_resume(hcd, false);
Jingoo Han1acb30e2011-05-20 20:48:33 +0900264 return 0;
265}
266#else
267#define s5p_ehci_suspend NULL
268#define s5p_ehci_resume NULL
269#endif
270
271static const struct dev_pm_ops s5p_ehci_pm_ops = {
272 .suspend = s5p_ehci_suspend,
273 .resume = s5p_ehci_resume,
274};
275
Vivek Gautam2c026e22012-07-16 11:25:37 +0530276#ifdef CONFIG_OF
277static const struct of_device_id exynos_ehci_match[] = {
Vivek Gautam6e247772013-01-24 19:15:29 +0530278 { .compatible = "samsung,exynos4210-ehci" },
Vivek Gautam2c026e22012-07-16 11:25:37 +0530279 {},
280};
281MODULE_DEVICE_TABLE(of, exynos_ehci_match);
282#endif
283
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900284static struct platform_driver s5p_ehci_driver = {
285 .probe = s5p_ehci_probe,
Bill Pemberton76904172012-11-19 13:21:08 -0500286 .remove = s5p_ehci_remove,
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900287 .shutdown = s5p_ehci_shutdown,
288 .driver = {
289 .name = "s5p-ehci",
290 .owner = THIS_MODULE,
Jingoo Han1acb30e2011-05-20 20:48:33 +0900291 .pm = &s5p_ehci_pm_ops,
Vivek Gautam2c026e22012-07-16 11:25:37 +0530292 .of_match_table = of_match_ptr(exynos_ehci_match),
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900293 }
294};
Manjunath Goudar7edb3da2013-04-02 18:24:01 +0200295static const struct ehci_driver_overrides s5p_overrides __initdata = {
296 .extra_priv_size = sizeof(struct s5p_ehci_hcd),
297};
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900298
Manjunath Goudar7edb3da2013-04-02 18:24:01 +0200299static int __init ehci_s5p_init(void)
300{
301 if (usb_disabled())
302 return -ENODEV;
303
304 pr_info("%s: " DRIVER_DESC "\n", hcd_name);
305 ehci_init_driver(&s5p_ehci_hc_driver, &s5p_overrides);
306 return platform_driver_register(&s5p_ehci_driver);
307}
308module_init(ehci_s5p_init);
309
310static void __exit ehci_s5p_cleanup(void)
311{
312 platform_driver_unregister(&s5p_ehci_driver);
313}
314module_exit(ehci_s5p_cleanup);
315
316MODULE_DESCRIPTION(DRIVER_DESC);
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900317MODULE_ALIAS("platform:s5p-ehci");
Manjunath Goudar7edb3da2013-04-02 18:24:01 +0200318MODULE_AUTHOR("Jingoo Han");
319MODULE_AUTHOR("Joonyoung Shim");
320MODULE_LICENSE("GPL v2");