blob: aba645bd4866b27bf5d0e11124cf33f1997aeecf [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
Thomas Abrahame6b0166f2013-05-27 18:42:12 +090053static struct s5p_ehci_platdata empty_platdata;
54
Manjunath Goudar7edb3da2013-04-02 18:24:01 +020055#define to_s5p_ehci(hcd) (struct s5p_ehci_hcd *)(hcd_to_ehci(hcd)->priv)
Vivek Gautamd233c192013-01-22 18:30:42 +053056
Vivek Gautamfd81d592012-07-17 10:10:50 +053057static void s5p_setup_vbus_gpio(struct platform_device *pdev)
58{
Doug Anderson3f3b55b2013-03-14 20:15:37 -070059 struct device *dev = &pdev->dev;
Vivek Gautamfd81d592012-07-17 10:10:50 +053060 int err;
61 int gpio;
62
Doug Anderson3f3b55b2013-03-14 20:15:37 -070063 if (!dev->of_node)
Vivek Gautamfd81d592012-07-17 10:10:50 +053064 return;
65
Doug Anderson3f3b55b2013-03-14 20:15:37 -070066 gpio = of_get_named_gpio(dev->of_node, "samsung,vbus-gpio", 0);
Vivek Gautamfd81d592012-07-17 10:10:50 +053067 if (!gpio_is_valid(gpio))
68 return;
69
Doug Anderson3f3b55b2013-03-14 20:15:37 -070070 err = devm_gpio_request_one(dev, gpio, GPIOF_OUT_INIT_HIGH,
71 "ehci_vbus_gpio");
Vivek Gautamfd81d592012-07-17 10:10:50 +053072 if (err)
Doug Anderson3f3b55b2013-03-14 20:15:37 -070073 dev_err(dev, "can't request ehci vbus gpio %d", gpio);
Vivek Gautamfd81d592012-07-17 10:10:50 +053074}
75
Bill Pemberton41ac7b32012-11-19 13:21:48 -050076static int s5p_ehci_probe(struct platform_device *pdev)
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +090077{
Vivek Gautamd233c192013-01-22 18:30:42 +053078 struct s5p_ehci_platdata *pdata = pdev->dev.platform_data;
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +090079 struct s5p_ehci_hcd *s5p_ehci;
80 struct usb_hcd *hcd;
81 struct ehci_hcd *ehci;
82 struct resource *res;
Vivek Gautamd233c192013-01-22 18:30:42 +053083 struct usb_phy *phy;
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +090084 int irq;
85 int err;
86
Vivek Gautam2c026e22012-07-16 11:25:37 +053087 /*
88 * Right now device-tree probed devices don't get dma_mask set.
89 * Since shared usb code relies on it, set it here for now.
90 * Once we move to full device tree support this will vanish off.
91 */
92 if (!pdev->dev.dma_mask)
Stephen Warren3b9561e2013-05-07 16:53:52 -060093 pdev->dev.dma_mask = &pdev->dev.coherent_dma_mask;
Vivek Gautam2c026e22012-07-16 11:25:37 +053094 if (!pdev->dev.coherent_dma_mask)
95 pdev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
96
Vivek Gautamfd81d592012-07-17 10:10:50 +053097 s5p_setup_vbus_gpio(pdev);
98
Manjunath Goudar7edb3da2013-04-02 18:24:01 +020099 hcd = usb_create_hcd(&s5p_ehci_hc_driver,
100 &pdev->dev, dev_name(&pdev->dev));
101 if (!hcd) {
102 dev_err(&pdev->dev, "Unable to create HCD\n");
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900103 return -ENOMEM;
Manjunath Goudar7edb3da2013-04-02 18:24:01 +0200104 }
105 s5p_ehci = to_s5p_ehci(hcd);
Thomas Abrahame6b0166f2013-05-27 18:42:12 +0900106
107 if (of_device_is_compatible(pdev->dev.of_node,
108 "samsung,exynos5440-ehci")) {
109 s5p_ehci->pdata = &empty_platdata;
110 goto skip_phy;
111 }
112
Vivek Gautamd233c192013-01-22 18:30:42 +0530113 phy = devm_usb_get_phy(&pdev->dev, USB_PHY_TYPE_USB2);
Felipe Balbia16283e2013-03-15 11:04:15 +0200114 if (IS_ERR(phy)) {
Vivek Gautamd233c192013-01-22 18:30:42 +0530115 /* Fallback to pdata */
116 if (!pdata) {
Libo Chen9a9ef732013-05-09 12:58:08 +0800117 usb_put_hcd(hcd);
Vivek Gautamd233c192013-01-22 18:30:42 +0530118 dev_warn(&pdev->dev, "no platform data or transceiver defined\n");
119 return -EPROBE_DEFER;
120 } else {
121 s5p_ehci->pdata = pdata;
122 }
123 } else {
124 s5p_ehci->phy = phy;
125 s5p_ehci->otg = phy->otg;
126 }
127
Thomas Abrahame6b0166f2013-05-27 18:42:12 +0900128skip_phy:
129
Julia Lawall63fd0ae2012-07-30 16:43:44 +0200130 s5p_ehci->clk = devm_clk_get(&pdev->dev, "usbhost");
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900131
132 if (IS_ERR(s5p_ehci->clk)) {
133 dev_err(&pdev->dev, "Failed to get usbhost clock\n");
134 err = PTR_ERR(s5p_ehci->clk);
135 goto fail_clk;
136 }
137
Thomas Abrahame1deb562012-10-03 08:40:42 +0900138 err = clk_prepare_enable(s5p_ehci->clk);
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900139 if (err)
Julia Lawall63fd0ae2012-07-30 16:43:44 +0200140 goto fail_clk;
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900141
142 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
143 if (!res) {
144 dev_err(&pdev->dev, "Failed to get I/O memory\n");
145 err = -ENXIO;
146 goto fail_io;
147 }
148
149 hcd->rsrc_start = res->start;
150 hcd->rsrc_len = resource_size(res);
Jingoo Han9cb07562012-06-28 16:29:46 +0900151 hcd->regs = devm_ioremap(&pdev->dev, res->start, hcd->rsrc_len);
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900152 if (!hcd->regs) {
153 dev_err(&pdev->dev, "Failed to remap I/O memory\n");
154 err = -ENOMEM;
155 goto fail_io;
156 }
157
158 irq = platform_get_irq(pdev, 0);
159 if (!irq) {
160 dev_err(&pdev->dev, "Failed to get IRQ\n");
161 err = -ENODEV;
Jingoo Han9cb07562012-06-28 16:29:46 +0900162 goto fail_io;
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900163 }
164
Vivek Gautamd233c192013-01-22 18:30:42 +0530165 if (s5p_ehci->otg)
Manjunath Goudar7edb3da2013-04-02 18:24:01 +0200166 s5p_ehci->otg->set_host(s5p_ehci->otg, &hcd->self);
Vivek Gautamd233c192013-01-22 18:30:42 +0530167
Manjunath Goudar7edb3da2013-04-02 18:24:01 +0200168 if (s5p_ehci->phy)
169 usb_phy_init(s5p_ehci->phy);
170 else if (s5p_ehci->pdata->phy_init)
171 s5p_ehci->pdata->phy_init(pdev, USB_PHY_TYPE_HOST);
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900172
173 ehci = hcd_to_ehci(hcd);
174 ehci->caps = hcd->regs;
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900175
Jingoo Han88555a62012-03-05 10:40:14 +0900176 /* DMA burst Enable */
177 writel(EHCI_INSNREG00_ENABLE_DMA_BURST, EHCI_INSNREG00(hcd->regs));
178
Yong Zhangb5dd18d2011-09-07 16:10:52 +0800179 err = usb_add_hcd(hcd, irq, IRQF_SHARED);
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900180 if (err) {
181 dev_err(&pdev->dev, "Failed to add USB HCD\n");
Vivek Gautamd233c192013-01-22 18:30:42 +0530182 goto fail_add_hcd;
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900183 }
184
Vivek Gautambbcd85a2013-04-09 18:42:11 +0530185 platform_set_drvdata(pdev, hcd);
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900186
187 return 0;
188
Vivek Gautamd233c192013-01-22 18:30:42 +0530189fail_add_hcd:
Manjunath Goudar7edb3da2013-04-02 18:24:01 +0200190 if (s5p_ehci->phy)
191 usb_phy_shutdown(s5p_ehci->phy);
192 else if (s5p_ehci->pdata->phy_exit)
193 s5p_ehci->pdata->phy_exit(pdev, USB_PHY_TYPE_HOST);
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900194fail_io:
Thomas Abrahame1deb562012-10-03 08:40:42 +0900195 clk_disable_unprepare(s5p_ehci->clk);
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900196fail_clk:
197 usb_put_hcd(hcd);
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900198 return err;
199}
200
Bill Pembertonfb4e98a2012-11-19 13:26:20 -0500201static int s5p_ehci_remove(struct platform_device *pdev)
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900202{
Manjunath Goudar7edb3da2013-04-02 18:24:01 +0200203 struct usb_hcd *hcd = platform_get_drvdata(pdev);
204 struct s5p_ehci_hcd *s5p_ehci = to_s5p_ehci(hcd);
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900205
206 usb_remove_hcd(hcd);
207
Vivek Gautamd233c192013-01-22 18:30:42 +0530208 if (s5p_ehci->otg)
Manjunath Goudar7edb3da2013-04-02 18:24:01 +0200209 s5p_ehci->otg->set_host(s5p_ehci->otg, &hcd->self);
Vivek Gautamd233c192013-01-22 18:30:42 +0530210
Manjunath Goudar7edb3da2013-04-02 18:24:01 +0200211 if (s5p_ehci->phy)
212 usb_phy_shutdown(s5p_ehci->phy);
213 else if (s5p_ehci->pdata->phy_exit)
214 s5p_ehci->pdata->phy_exit(pdev, USB_PHY_TYPE_HOST);
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900215
Thomas Abrahame1deb562012-10-03 08:40:42 +0900216 clk_disable_unprepare(s5p_ehci->clk);
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900217
218 usb_put_hcd(hcd);
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900219
220 return 0;
221}
222
Jingoo Han1acb30e2011-05-20 20:48:33 +0900223#ifdef CONFIG_PM
224static int s5p_ehci_suspend(struct device *dev)
225{
Manjunath Goudar7edb3da2013-04-02 18:24:01 +0200226 struct usb_hcd *hcd = dev_get_drvdata(dev);
227 struct s5p_ehci_hcd *s5p_ehci = to_s5p_ehci(hcd);
228 struct platform_device *pdev = to_platform_device(dev);
229
Alan Sternc5cf9212012-06-28 11:19:02 -0400230 bool do_wakeup = device_may_wakeup(dev);
Alan Sternc5cf9212012-06-28 11:19:02 -0400231 int rc;
Jingoo Han1acb30e2011-05-20 20:48:33 +0900232
Alan Sternc5cf9212012-06-28 11:19:02 -0400233 rc = ehci_suspend(hcd, do_wakeup);
Jingoo Han1acb30e2011-05-20 20:48:33 +0900234
Vivek Gautamd233c192013-01-22 18:30:42 +0530235 if (s5p_ehci->otg)
Manjunath Goudar7edb3da2013-04-02 18:24:01 +0200236 s5p_ehci->otg->set_host(s5p_ehci->otg, &hcd->self);
Vivek Gautamd233c192013-01-22 18:30:42 +0530237
Manjunath Goudar7edb3da2013-04-02 18:24:01 +0200238 if (s5p_ehci->phy)
239 usb_phy_shutdown(s5p_ehci->phy);
240 else if (s5p_ehci->pdata->phy_exit)
241 s5p_ehci->pdata->phy_exit(pdev, USB_PHY_TYPE_HOST);
Jingoo Han1acb30e2011-05-20 20:48:33 +0900242
Thomas Abrahame1deb562012-10-03 08:40:42 +0900243 clk_disable_unprepare(s5p_ehci->clk);
Jingoo Han8b4fc8c2012-04-13 11:06:36 +0900244
Jingoo Han1acb30e2011-05-20 20:48:33 +0900245 return rc;
246}
247
248static int s5p_ehci_resume(struct device *dev)
249{
Manjunath Goudar7edb3da2013-04-02 18:24:01 +0200250 struct usb_hcd *hcd = dev_get_drvdata(dev);
251 struct s5p_ehci_hcd *s5p_ehci = to_s5p_ehci(hcd);
252 struct platform_device *pdev = to_platform_device(dev);
Jingoo Han1acb30e2011-05-20 20:48:33 +0900253
Thomas Abrahame1deb562012-10-03 08:40:42 +0900254 clk_prepare_enable(s5p_ehci->clk);
Jingoo Han8b4fc8c2012-04-13 11:06:36 +0900255
Vivek Gautamd233c192013-01-22 18:30:42 +0530256 if (s5p_ehci->otg)
Manjunath Goudar7edb3da2013-04-02 18:24:01 +0200257 s5p_ehci->otg->set_host(s5p_ehci->otg, &hcd->self);
Vivek Gautamd233c192013-01-22 18:30:42 +0530258
Manjunath Goudar7edb3da2013-04-02 18:24:01 +0200259 if (s5p_ehci->phy)
260 usb_phy_init(s5p_ehci->phy);
261 else if (s5p_ehci->pdata->phy_init)
262 s5p_ehci->pdata->phy_init(pdev, USB_PHY_TYPE_HOST);
Jingoo Han1acb30e2011-05-20 20:48:33 +0900263
Jingoo Han88555a62012-03-05 10:40:14 +0900264 /* DMA burst Enable */
265 writel(EHCI_INSNREG00_ENABLE_DMA_BURST, EHCI_INSNREG00(hcd->regs));
266
Alan Sternc5cf9212012-06-28 11:19:02 -0400267 ehci_resume(hcd, false);
Jingoo Han1acb30e2011-05-20 20:48:33 +0900268 return 0;
269}
270#else
271#define s5p_ehci_suspend NULL
272#define s5p_ehci_resume NULL
273#endif
274
275static const struct dev_pm_ops s5p_ehci_pm_ops = {
276 .suspend = s5p_ehci_suspend,
277 .resume = s5p_ehci_resume,
278};
279
Vivek Gautam2c026e22012-07-16 11:25:37 +0530280#ifdef CONFIG_OF
281static const struct of_device_id exynos_ehci_match[] = {
Vivek Gautam6e247772013-01-24 19:15:29 +0530282 { .compatible = "samsung,exynos4210-ehci" },
Thomas Abrahame6b0166f2013-05-27 18:42:12 +0900283 { .compatible = "samsung,exynos5440-ehci" },
Vivek Gautam2c026e22012-07-16 11:25:37 +0530284 {},
285};
286MODULE_DEVICE_TABLE(of, exynos_ehci_match);
287#endif
288
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900289static struct platform_driver s5p_ehci_driver = {
290 .probe = s5p_ehci_probe,
Bill Pemberton76904172012-11-19 13:21:08 -0500291 .remove = s5p_ehci_remove,
Roger Quadrosaaf6b522013-07-22 15:04:50 +0300292 .shutdown = usb_hcd_platform_shutdown,
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900293 .driver = {
294 .name = "s5p-ehci",
295 .owner = THIS_MODULE,
Jingoo Han1acb30e2011-05-20 20:48:33 +0900296 .pm = &s5p_ehci_pm_ops,
Vivek Gautam2c026e22012-07-16 11:25:37 +0530297 .of_match_table = of_match_ptr(exynos_ehci_match),
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900298 }
299};
Manjunath Goudar7edb3da2013-04-02 18:24:01 +0200300static const struct ehci_driver_overrides s5p_overrides __initdata = {
301 .extra_priv_size = sizeof(struct s5p_ehci_hcd),
302};
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900303
Manjunath Goudar7edb3da2013-04-02 18:24:01 +0200304static int __init ehci_s5p_init(void)
305{
306 if (usb_disabled())
307 return -ENODEV;
308
309 pr_info("%s: " DRIVER_DESC "\n", hcd_name);
310 ehci_init_driver(&s5p_ehci_hc_driver, &s5p_overrides);
311 return platform_driver_register(&s5p_ehci_driver);
312}
313module_init(ehci_s5p_init);
314
315static void __exit ehci_s5p_cleanup(void)
316{
317 platform_driver_unregister(&s5p_ehci_driver);
318}
319module_exit(ehci_s5p_cleanup);
320
321MODULE_DESCRIPTION(DRIVER_DESC);
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900322MODULE_ALIAS("platform:s5p-ehci");
Manjunath Goudar7edb3da2013-04-02 18:24:01 +0200323MODULE_AUTHOR("Jingoo Han");
324MODULE_AUTHOR("Joonyoung Shim");
325MODULE_LICENSE("GPL v2");