blob: 580548ad8530594ece02098497b18c64a7a267a6 [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>
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +090026#include <plat/usb-phy.h>
Manjunath Goudar7edb3da2013-04-02 18:24:01 +020027#include <linux/usb.h>
28#include <linux/usb/hcd.h>
29#include <linux/usb/otg.h>
30
31#include "ehci.h"
32
33#define DRIVER_DESC "EHCI s5p driver"
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +090034
Jingoo Han88555a62012-03-05 10:40:14 +090035#define EHCI_INSNREG00(base) (base + 0x90)
36#define EHCI_INSNREG00_ENA_INCR16 (0x1 << 25)
37#define EHCI_INSNREG00_ENA_INCR8 (0x1 << 24)
38#define EHCI_INSNREG00_ENA_INCR4 (0x1 << 23)
39#define EHCI_INSNREG00_ENA_INCRX_ALIGN (0x1 << 22)
40#define EHCI_INSNREG00_ENABLE_DMA_BURST \
41 (EHCI_INSNREG00_ENA_INCR16 | EHCI_INSNREG00_ENA_INCR8 | \
42 EHCI_INSNREG00_ENA_INCR4 | EHCI_INSNREG00_ENA_INCRX_ALIGN)
43
Manjunath Goudar7edb3da2013-04-02 18:24:01 +020044static const char hcd_name[] = "ehci-s5p";
45static struct hc_driver __read_mostly s5p_ehci_hc_driver;
46
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +090047struct s5p_ehci_hcd {
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +090048 struct clk *clk;
Vivek Gautamd233c192013-01-22 18:30:42 +053049 struct usb_phy *phy;
50 struct usb_otg *otg;
51 struct s5p_ehci_platdata *pdata;
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +090052};
53
Manjunath Goudar7edb3da2013-04-02 18:24:01 +020054#define to_s5p_ehci(hcd) (struct s5p_ehci_hcd *)(hcd_to_ehci(hcd)->priv)
Vivek Gautamd233c192013-01-22 18:30:42 +053055
Vivek Gautamfd81d592012-07-17 10:10:50 +053056static void s5p_setup_vbus_gpio(struct platform_device *pdev)
57{
Doug Anderson3f3b55b2013-03-14 20:15:37 -070058 struct device *dev = &pdev->dev;
Vivek Gautamfd81d592012-07-17 10:10:50 +053059 int err;
60 int gpio;
61
Doug Anderson3f3b55b2013-03-14 20:15:37 -070062 if (!dev->of_node)
Vivek Gautamfd81d592012-07-17 10:10:50 +053063 return;
64
Doug Anderson3f3b55b2013-03-14 20:15:37 -070065 gpio = of_get_named_gpio(dev->of_node, "samsung,vbus-gpio", 0);
Vivek Gautamfd81d592012-07-17 10:10:50 +053066 if (!gpio_is_valid(gpio))
67 return;
68
Doug Anderson3f3b55b2013-03-14 20:15:37 -070069 err = devm_gpio_request_one(dev, gpio, GPIOF_OUT_INIT_HIGH,
70 "ehci_vbus_gpio");
Vivek Gautamfd81d592012-07-17 10:10:50 +053071 if (err)
Doug Anderson3f3b55b2013-03-14 20:15:37 -070072 dev_err(dev, "can't request ehci vbus gpio %d", gpio);
Vivek Gautamfd81d592012-07-17 10:10:50 +053073}
74
Vivek Gautam2c026e22012-07-16 11:25:37 +053075static u64 ehci_s5p_dma_mask = DMA_BIT_MASK(32);
76
Bill Pemberton41ac7b32012-11-19 13:21:48 -050077static int s5p_ehci_probe(struct platform_device *pdev)
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +090078{
Vivek Gautamd233c192013-01-22 18:30:42 +053079 struct s5p_ehci_platdata *pdata = pdev->dev.platform_data;
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +090080 struct s5p_ehci_hcd *s5p_ehci;
81 struct usb_hcd *hcd;
82 struct ehci_hcd *ehci;
83 struct resource *res;
Vivek Gautamd233c192013-01-22 18:30:42 +053084 struct usb_phy *phy;
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +090085 int irq;
86 int err;
87
Vivek Gautam2c026e22012-07-16 11:25:37 +053088 /*
89 * Right now device-tree probed devices don't get dma_mask set.
90 * Since shared usb code relies on it, set it here for now.
91 * Once we move to full device tree support this will vanish off.
92 */
93 if (!pdev->dev.dma_mask)
94 pdev->dev.dma_mask = &ehci_s5p_dma_mask;
95 if (!pdev->dev.coherent_dma_mask)
96 pdev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
97
Vivek Gautamfd81d592012-07-17 10:10:50 +053098 s5p_setup_vbus_gpio(pdev);
99
Manjunath Goudar7edb3da2013-04-02 18:24:01 +0200100 hcd = usb_create_hcd(&s5p_ehci_hc_driver,
101 &pdev->dev, dev_name(&pdev->dev));
102 if (!hcd) {
103 dev_err(&pdev->dev, "Unable to create HCD\n");
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900104 return -ENOMEM;
Manjunath Goudar7edb3da2013-04-02 18:24:01 +0200105 }
106 s5p_ehci = to_s5p_ehci(hcd);
Vivek Gautamd233c192013-01-22 18:30:42 +0530107 phy = devm_usb_get_phy(&pdev->dev, USB_PHY_TYPE_USB2);
Felipe Balbia16283e2013-03-15 11:04:15 +0200108 if (IS_ERR(phy)) {
Vivek Gautamd233c192013-01-22 18:30:42 +0530109 /* Fallback to pdata */
110 if (!pdata) {
111 dev_warn(&pdev->dev, "no platform data or transceiver defined\n");
112 return -EPROBE_DEFER;
113 } else {
114 s5p_ehci->pdata = pdata;
115 }
116 } else {
117 s5p_ehci->phy = phy;
118 s5p_ehci->otg = phy->otg;
119 }
120
Julia Lawall63fd0ae2012-07-30 16:43:44 +0200121 s5p_ehci->clk = devm_clk_get(&pdev->dev, "usbhost");
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900122
123 if (IS_ERR(s5p_ehci->clk)) {
124 dev_err(&pdev->dev, "Failed to get usbhost clock\n");
125 err = PTR_ERR(s5p_ehci->clk);
126 goto fail_clk;
127 }
128
Thomas Abrahame1deb562012-10-03 08:40:42 +0900129 err = clk_prepare_enable(s5p_ehci->clk);
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900130 if (err)
Julia Lawall63fd0ae2012-07-30 16:43:44 +0200131 goto fail_clk;
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900132
133 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
134 if (!res) {
135 dev_err(&pdev->dev, "Failed to get I/O memory\n");
136 err = -ENXIO;
137 goto fail_io;
138 }
139
140 hcd->rsrc_start = res->start;
141 hcd->rsrc_len = resource_size(res);
Jingoo Han9cb07562012-06-28 16:29:46 +0900142 hcd->regs = devm_ioremap(&pdev->dev, res->start, hcd->rsrc_len);
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900143 if (!hcd->regs) {
144 dev_err(&pdev->dev, "Failed to remap I/O memory\n");
145 err = -ENOMEM;
146 goto fail_io;
147 }
148
149 irq = platform_get_irq(pdev, 0);
150 if (!irq) {
151 dev_err(&pdev->dev, "Failed to get IRQ\n");
152 err = -ENODEV;
Jingoo Han9cb07562012-06-28 16:29:46 +0900153 goto fail_io;
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900154 }
155
Vivek Gautamd233c192013-01-22 18:30:42 +0530156 if (s5p_ehci->otg)
Manjunath Goudar7edb3da2013-04-02 18:24:01 +0200157 s5p_ehci->otg->set_host(s5p_ehci->otg, &hcd->self);
Vivek Gautamd233c192013-01-22 18:30:42 +0530158
Manjunath Goudar7edb3da2013-04-02 18:24:01 +0200159 if (s5p_ehci->phy)
160 usb_phy_init(s5p_ehci->phy);
161 else if (s5p_ehci->pdata->phy_init)
162 s5p_ehci->pdata->phy_init(pdev, USB_PHY_TYPE_HOST);
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900163
164 ehci = hcd_to_ehci(hcd);
165 ehci->caps = hcd->regs;
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900166
Jingoo Han88555a62012-03-05 10:40:14 +0900167 /* DMA burst Enable */
168 writel(EHCI_INSNREG00_ENABLE_DMA_BURST, EHCI_INSNREG00(hcd->regs));
169
Yong Zhangb5dd18d2011-09-07 16:10:52 +0800170 err = usb_add_hcd(hcd, irq, IRQF_SHARED);
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900171 if (err) {
172 dev_err(&pdev->dev, "Failed to add USB HCD\n");
Vivek Gautamd233c192013-01-22 18:30:42 +0530173 goto fail_add_hcd;
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900174 }
175
Vivek Gautambbcd85a2013-04-09 18:42:11 +0530176 platform_set_drvdata(pdev, hcd);
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900177
178 return 0;
179
Vivek Gautamd233c192013-01-22 18:30:42 +0530180fail_add_hcd:
Manjunath Goudar7edb3da2013-04-02 18:24:01 +0200181 if (s5p_ehci->phy)
182 usb_phy_shutdown(s5p_ehci->phy);
183 else if (s5p_ehci->pdata->phy_exit)
184 s5p_ehci->pdata->phy_exit(pdev, USB_PHY_TYPE_HOST);
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900185fail_io:
Thomas Abrahame1deb562012-10-03 08:40:42 +0900186 clk_disable_unprepare(s5p_ehci->clk);
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900187fail_clk:
188 usb_put_hcd(hcd);
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900189 return err;
190}
191
Bill Pembertonfb4e98a2012-11-19 13:26:20 -0500192static int s5p_ehci_remove(struct platform_device *pdev)
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900193{
Manjunath Goudar7edb3da2013-04-02 18:24:01 +0200194 struct usb_hcd *hcd = platform_get_drvdata(pdev);
195 struct s5p_ehci_hcd *s5p_ehci = to_s5p_ehci(hcd);
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900196
197 usb_remove_hcd(hcd);
198
Vivek Gautamd233c192013-01-22 18:30:42 +0530199 if (s5p_ehci->otg)
Manjunath Goudar7edb3da2013-04-02 18:24:01 +0200200 s5p_ehci->otg->set_host(s5p_ehci->otg, &hcd->self);
Vivek Gautamd233c192013-01-22 18:30:42 +0530201
Manjunath Goudar7edb3da2013-04-02 18:24:01 +0200202 if (s5p_ehci->phy)
203 usb_phy_shutdown(s5p_ehci->phy);
204 else if (s5p_ehci->pdata->phy_exit)
205 s5p_ehci->pdata->phy_exit(pdev, USB_PHY_TYPE_HOST);
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900206
Thomas Abrahame1deb562012-10-03 08:40:42 +0900207 clk_disable_unprepare(s5p_ehci->clk);
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900208
209 usb_put_hcd(hcd);
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900210
211 return 0;
212}
213
214static void s5p_ehci_shutdown(struct platform_device *pdev)
215{
Manjunath Goudar7edb3da2013-04-02 18:24:01 +0200216 struct usb_hcd *hcd = platform_get_drvdata(pdev);
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900217
218 if (hcd->driver->shutdown)
219 hcd->driver->shutdown(hcd);
220}
221
Jingoo Han1acb30e2011-05-20 20:48:33 +0900222#ifdef CONFIG_PM
223static int s5p_ehci_suspend(struct device *dev)
224{
Manjunath Goudar7edb3da2013-04-02 18:24:01 +0200225 struct usb_hcd *hcd = dev_get_drvdata(dev);
226 struct s5p_ehci_hcd *s5p_ehci = to_s5p_ehci(hcd);
227 struct platform_device *pdev = to_platform_device(dev);
228
Alan Sternc5cf9212012-06-28 11:19:02 -0400229 bool do_wakeup = device_may_wakeup(dev);
Alan Sternc5cf9212012-06-28 11:19:02 -0400230 int rc;
Jingoo Han1acb30e2011-05-20 20:48:33 +0900231
Alan Sternc5cf9212012-06-28 11:19:02 -0400232 rc = ehci_suspend(hcd, do_wakeup);
Jingoo Han1acb30e2011-05-20 20:48:33 +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_shutdown(s5p_ehci->phy);
239 else if (s5p_ehci->pdata->phy_exit)
240 s5p_ehci->pdata->phy_exit(pdev, USB_PHY_TYPE_HOST);
Jingoo Han1acb30e2011-05-20 20:48:33 +0900241
Thomas Abrahame1deb562012-10-03 08:40:42 +0900242 clk_disable_unprepare(s5p_ehci->clk);
Jingoo Han8b4fc8c2012-04-13 11:06:36 +0900243
Jingoo Han1acb30e2011-05-20 20:48:33 +0900244 return rc;
245}
246
247static int s5p_ehci_resume(struct device *dev)
248{
Manjunath Goudar7edb3da2013-04-02 18:24:01 +0200249 struct usb_hcd *hcd = dev_get_drvdata(dev);
250 struct s5p_ehci_hcd *s5p_ehci = to_s5p_ehci(hcd);
251 struct platform_device *pdev = to_platform_device(dev);
Jingoo Han1acb30e2011-05-20 20:48:33 +0900252
Thomas Abrahame1deb562012-10-03 08:40:42 +0900253 clk_prepare_enable(s5p_ehci->clk);
Jingoo Han8b4fc8c2012-04-13 11:06:36 +0900254
Vivek Gautamd233c192013-01-22 18:30:42 +0530255 if (s5p_ehci->otg)
Manjunath Goudar7edb3da2013-04-02 18:24:01 +0200256 s5p_ehci->otg->set_host(s5p_ehci->otg, &hcd->self);
Vivek Gautamd233c192013-01-22 18:30:42 +0530257
Manjunath Goudar7edb3da2013-04-02 18:24:01 +0200258 if (s5p_ehci->phy)
259 usb_phy_init(s5p_ehci->phy);
260 else if (s5p_ehci->pdata->phy_init)
261 s5p_ehci->pdata->phy_init(pdev, USB_PHY_TYPE_HOST);
Jingoo Han1acb30e2011-05-20 20:48:33 +0900262
Jingoo Han88555a62012-03-05 10:40:14 +0900263 /* DMA burst Enable */
264 writel(EHCI_INSNREG00_ENABLE_DMA_BURST, EHCI_INSNREG00(hcd->regs));
265
Alan Sternc5cf9212012-06-28 11:19:02 -0400266 ehci_resume(hcd, false);
Jingoo Han1acb30e2011-05-20 20:48:33 +0900267 return 0;
268}
269#else
270#define s5p_ehci_suspend NULL
271#define s5p_ehci_resume NULL
272#endif
273
274static const struct dev_pm_ops s5p_ehci_pm_ops = {
275 .suspend = s5p_ehci_suspend,
276 .resume = s5p_ehci_resume,
277};
278
Vivek Gautam2c026e22012-07-16 11:25:37 +0530279#ifdef CONFIG_OF
280static const struct of_device_id exynos_ehci_match[] = {
Vivek Gautam6e247772013-01-24 19:15:29 +0530281 { .compatible = "samsung,exynos4210-ehci" },
Vivek Gautam2c026e22012-07-16 11:25:37 +0530282 {},
283};
284MODULE_DEVICE_TABLE(of, exynos_ehci_match);
285#endif
286
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900287static struct platform_driver s5p_ehci_driver = {
288 .probe = s5p_ehci_probe,
Bill Pemberton76904172012-11-19 13:21:08 -0500289 .remove = s5p_ehci_remove,
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900290 .shutdown = s5p_ehci_shutdown,
291 .driver = {
292 .name = "s5p-ehci",
293 .owner = THIS_MODULE,
Jingoo Han1acb30e2011-05-20 20:48:33 +0900294 .pm = &s5p_ehci_pm_ops,
Vivek Gautam2c026e22012-07-16 11:25:37 +0530295 .of_match_table = of_match_ptr(exynos_ehci_match),
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900296 }
297};
Manjunath Goudar7edb3da2013-04-02 18:24:01 +0200298static const struct ehci_driver_overrides s5p_overrides __initdata = {
299 .extra_priv_size = sizeof(struct s5p_ehci_hcd),
300};
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900301
Manjunath Goudar7edb3da2013-04-02 18:24:01 +0200302static int __init ehci_s5p_init(void)
303{
304 if (usb_disabled())
305 return -ENODEV;
306
307 pr_info("%s: " DRIVER_DESC "\n", hcd_name);
308 ehci_init_driver(&s5p_ehci_hc_driver, &s5p_overrides);
309 return platform_driver_register(&s5p_ehci_driver);
310}
311module_init(ehci_s5p_init);
312
313static void __exit ehci_s5p_cleanup(void)
314{
315 platform_driver_unregister(&s5p_ehci_driver);
316}
317module_exit(ehci_s5p_cleanup);
318
319MODULE_DESCRIPTION(DRIVER_DESC);
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900320MODULE_ALIAS("platform:s5p-ehci");
Manjunath Goudar7edb3da2013-04-02 18:24:01 +0200321MODULE_AUTHOR("Jingoo Han");
322MODULE_AUTHOR("Joonyoung Shim");
323MODULE_LICENSE("GPL v2");