blob: 45e1ad3f56b20e4004a09b1deebf9e1973a80fcc [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>
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
31#define DRIVER_DESC "EHCI s5p 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
Manjunath Goudar7edb3da2013-04-02 18:24:01 +020042static const char hcd_name[] = "ehci-s5p";
43static struct hc_driver __read_mostly s5p_ehci_hc_driver;
44
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +090045struct s5p_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
Manjunath Goudar7edb3da2013-04-02 18:24:01 +020051#define to_s5p_ehci(hcd) (struct s5p_ehci_hcd *)(hcd_to_ehci(hcd)->priv)
Vivek Gautamd233c192013-01-22 18:30:42 +053052
Vivek Gautamfd81d592012-07-17 10:10:50 +053053static void s5p_setup_vbus_gpio(struct platform_device *pdev)
54{
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
Bill Pemberton41ac7b32012-11-19 13:21:48 -050072static int s5p_ehci_probe(struct platform_device *pdev)
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +090073{
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +090074 struct s5p_ehci_hcd *s5p_ehci;
75 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 */
87 if (!pdev->dev.dma_mask)
Stephen Warren3b9561e2013-05-07 16:53:52 -060088 pdev->dev.dma_mask = &pdev->dev.coherent_dma_mask;
Vivek Gautam2c026e22012-07-16 11:25:37 +053089 if (!pdev->dev.coherent_dma_mask)
90 pdev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
91
Vivek Gautamfd81d592012-07-17 10:10:50 +053092 s5p_setup_vbus_gpio(pdev);
93
Manjunath Goudar7edb3da2013-04-02 18:24:01 +020094 hcd = usb_create_hcd(&s5p_ehci_hc_driver,
95 &pdev->dev, dev_name(&pdev->dev));
96 if (!hcd) {
97 dev_err(&pdev->dev, "Unable to create HCD\n");
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +090098 return -ENOMEM;
Manjunath Goudar7edb3da2013-04-02 18:24:01 +020099 }
100 s5p_ehci = to_s5p_ehci(hcd);
Thomas Abrahame6b0166f2013-05-27 18:42:12 +0900101
102 if (of_device_is_compatible(pdev->dev.of_node,
Jingoo Han57ae1602013-10-10 16:41:19 +0900103 "samsung,exynos5440-ehci"))
Thomas Abrahame6b0166f2013-05-27 18:42:12 +0900104 goto skip_phy;
Thomas Abrahame6b0166f2013-05-27 18:42:12 +0900105
Vivek Gautamd233c192013-01-22 18:30:42 +0530106 phy = devm_usb_get_phy(&pdev->dev, USB_PHY_TYPE_USB2);
Felipe Balbia16283e2013-03-15 11:04:15 +0200107 if (IS_ERR(phy)) {
Jingoo Han57ae1602013-10-10 16:41:19 +0900108 usb_put_hcd(hcd);
109 dev_warn(&pdev->dev, "no platform data or transceiver defined\n");
110 return -EPROBE_DEFER;
Vivek Gautamd233c192013-01-22 18:30:42 +0530111 } else {
112 s5p_ehci->phy = phy;
113 s5p_ehci->otg = phy->otg;
114 }
115
Thomas Abrahame6b0166f2013-05-27 18:42:12 +0900116skip_phy:
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);
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900158
159 ehci = hcd_to_ehci(hcd);
160 ehci->caps = hcd->regs;
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900161
Jingoo Han88555a62012-03-05 10:40:14 +0900162 /* DMA burst Enable */
163 writel(EHCI_INSNREG00_ENABLE_DMA_BURST, EHCI_INSNREG00(hcd->regs));
164
Yong Zhangb5dd18d2011-09-07 16:10:52 +0800165 err = usb_add_hcd(hcd, irq, IRQF_SHARED);
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900166 if (err) {
167 dev_err(&pdev->dev, "Failed to add USB HCD\n");
Vivek Gautamd233c192013-01-22 18:30:42 +0530168 goto fail_add_hcd;
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900169 }
170
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:
Manjunath Goudar7edb3da2013-04-02 18:24:01 +0200176 if (s5p_ehci->phy)
177 usb_phy_shutdown(s5p_ehci->phy);
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900178fail_io:
Thomas Abrahame1deb562012-10-03 08:40:42 +0900179 clk_disable_unprepare(s5p_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
Bill Pembertonfb4e98a2012-11-19 13:26:20 -0500185static int s5p_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);
188 struct s5p_ehci_hcd *s5p_ehci = to_s5p_ehci(hcd);
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900189
190 usb_remove_hcd(hcd);
191
Vivek Gautamd233c192013-01-22 18:30:42 +0530192 if (s5p_ehci->otg)
Manjunath Goudar7edb3da2013-04-02 18:24:01 +0200193 s5p_ehci->otg->set_host(s5p_ehci->otg, &hcd->self);
Vivek Gautamd233c192013-01-22 18:30:42 +0530194
Manjunath Goudar7edb3da2013-04-02 18:24:01 +0200195 if (s5p_ehci->phy)
196 usb_phy_shutdown(s5p_ehci->phy);
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900197
Thomas Abrahame1deb562012-10-03 08:40:42 +0900198 clk_disable_unprepare(s5p_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
206static int s5p_ehci_suspend(struct device *dev)
207{
Manjunath Goudar7edb3da2013-04-02 18:24:01 +0200208 struct usb_hcd *hcd = dev_get_drvdata(dev);
209 struct s5p_ehci_hcd *s5p_ehci = to_s5p_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
Vivek Gautamd233c192013-01-22 18:30:42 +0530216 if (s5p_ehci->otg)
Manjunath Goudar7edb3da2013-04-02 18:24:01 +0200217 s5p_ehci->otg->set_host(s5p_ehci->otg, &hcd->self);
Vivek Gautamd233c192013-01-22 18:30:42 +0530218
Manjunath Goudar7edb3da2013-04-02 18:24:01 +0200219 if (s5p_ehci->phy)
220 usb_phy_shutdown(s5p_ehci->phy);
Jingoo Han1acb30e2011-05-20 20:48:33 +0900221
Thomas Abrahame1deb562012-10-03 08:40:42 +0900222 clk_disable_unprepare(s5p_ehci->clk);
Jingoo Han8b4fc8c2012-04-13 11:06:36 +0900223
Jingoo Han1acb30e2011-05-20 20:48:33 +0900224 return rc;
225}
226
227static int s5p_ehci_resume(struct device *dev)
228{
Manjunath Goudar7edb3da2013-04-02 18:24:01 +0200229 struct usb_hcd *hcd = dev_get_drvdata(dev);
230 struct s5p_ehci_hcd *s5p_ehci = to_s5p_ehci(hcd);
Jingoo Han1acb30e2011-05-20 20:48:33 +0900231
Thomas Abrahame1deb562012-10-03 08:40:42 +0900232 clk_prepare_enable(s5p_ehci->clk);
Jingoo Han8b4fc8c2012-04-13 11:06:36 +0900233
Vivek Gautamd233c192013-01-22 18:30:42 +0530234 if (s5p_ehci->otg)
Manjunath Goudar7edb3da2013-04-02 18:24:01 +0200235 s5p_ehci->otg->set_host(s5p_ehci->otg, &hcd->self);
Vivek Gautamd233c192013-01-22 18:30:42 +0530236
Manjunath Goudar7edb3da2013-04-02 18:24:01 +0200237 if (s5p_ehci->phy)
238 usb_phy_init(s5p_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
247#define s5p_ehci_suspend NULL
248#define s5p_ehci_resume NULL
249#endif
250
251static const struct dev_pm_ops s5p_ehci_pm_ops = {
252 .suspend = s5p_ehci_suspend,
253 .resume = s5p_ehci_resume,
254};
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
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900265static struct platform_driver s5p_ehci_driver = {
266 .probe = s5p_ehci_probe,
Bill Pemberton76904172012-11-19 13:21:08 -0500267 .remove = s5p_ehci_remove,
Roger Quadrosaaf6b522013-07-22 15:04:50 +0300268 .shutdown = usb_hcd_platform_shutdown,
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900269 .driver = {
270 .name = "s5p-ehci",
271 .owner = THIS_MODULE,
Jingoo Han1acb30e2011-05-20 20:48:33 +0900272 .pm = &s5p_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};
Manjunath Goudar7edb3da2013-04-02 18:24:01 +0200276static const struct ehci_driver_overrides s5p_overrides __initdata = {
277 .extra_priv_size = sizeof(struct s5p_ehci_hcd),
278};
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900279
Manjunath Goudar7edb3da2013-04-02 18:24:01 +0200280static int __init ehci_s5p_init(void)
281{
282 if (usb_disabled())
283 return -ENODEV;
284
285 pr_info("%s: " DRIVER_DESC "\n", hcd_name);
286 ehci_init_driver(&s5p_ehci_hc_driver, &s5p_overrides);
287 return platform_driver_register(&s5p_ehci_driver);
288}
289module_init(ehci_s5p_init);
290
291static void __exit ehci_s5p_cleanup(void)
292{
293 platform_driver_unregister(&s5p_ehci_driver);
294}
295module_exit(ehci_s5p_cleanup);
296
297MODULE_DESCRIPTION(DRIVER_DESC);
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900298MODULE_ALIAS("platform:s5p-ehci");
Manjunath Goudar7edb3da2013-04-02 18:24:01 +0200299MODULE_AUTHOR("Jingoo Han");
300MODULE_AUTHOR("Joonyoung Shim");
301MODULE_LICENSE("GPL v2");