blob: 01536cfd361da2bb5f275cd1812ae2eb2e9b9729 [file] [log] [blame]
Hauke Mehrtens7a7a4a592012-03-13 01:04:48 +01001/*
2 * Generic platform ehci driver
3 *
4 * Copyright 2007 Steven Brown <sbrown@cortland.com>
5 * Copyright 2010-2012 Hauke Mehrtens <hauke@hauke-m.de>
6 *
7 * Derived from the ohci-ssb driver
8 * Copyright 2007 Michael Buesch <m@bues.ch>
9 *
10 * Derived from the EHCI-PCI driver
11 * Copyright (c) 2000-2004 by David Brownell
12 *
13 * Derived from the ohci-pci driver
14 * Copyright 1999 Roman Weissgaerber
15 * Copyright 2000-2002 David Brownell
16 * Copyright 1999 Linus Torvalds
17 * Copyright 1999 Gregory P. Smith
18 *
19 * Licensed under the GNU/GPL. See COPYING for details.
20 */
Arnd Bergmannf3bc64d2013-03-27 21:44:22 +000021#include <linux/dma-mapping.h>
Thierry Reding148e1132013-01-21 11:09:22 +010022#include <linux/err.h>
Alan Stern99f91932012-11-01 11:13:08 -040023#include <linux/kernel.h>
Alan Sternd1bb67a2012-11-02 10:13:24 -040024#include <linux/hrtimer.h>
25#include <linux/io.h>
Alan Stern99f91932012-11-01 11:13:08 -040026#include <linux/module.h>
Arnd Bergmannf3bc64d2013-03-27 21:44:22 +000027#include <linux/of.h>
Hauke Mehrtens7a7a4a592012-03-13 01:04:48 +010028#include <linux/platform_device.h>
Alan Stern99f91932012-11-01 11:13:08 -040029#include <linux/usb.h>
30#include <linux/usb/hcd.h>
Hauke Mehrtens7a7a4a592012-03-13 01:04:48 +010031#include <linux/usb/ehci_pdriver.h>
32
Alan Stern99f91932012-11-01 11:13:08 -040033#include "ehci.h"
34
35#define DRIVER_DESC "EHCI generic platform driver"
36
37static const char hcd_name[] = "ehci-platform";
38
Hauke Mehrtens7a7a4a592012-03-13 01:04:48 +010039static int ehci_platform_reset(struct usb_hcd *hcd)
40{
41 struct platform_device *pdev = to_platform_device(hcd->self.controller);
Jingoo Hand4f09e22013-07-30 19:59:40 +090042 struct usb_ehci_pdata *pdata = dev_get_platdata(&pdev->dev);
Hauke Mehrtens7a7a4a592012-03-13 01:04:48 +010043 struct ehci_hcd *ehci = hcd_to_ehci(hcd);
44 int retval;
45
46 hcd->has_tt = pdata->has_tt;
47 ehci->has_synopsys_hc_bug = pdata->has_synopsys_hc_bug;
48 ehci->big_endian_desc = pdata->big_endian_desc;
49 ehci->big_endian_mmio = pdata->big_endian_mmio;
50
Sergei Shtylyov743fcce2013-06-02 01:33:56 +040051 if (pdata->pre_setup) {
52 retval = pdata->pre_setup(hcd);
53 if (retval < 0)
54 return retval;
55 }
56
Hauke Mehrtens7a7a4a592012-03-13 01:04:48 +010057 ehci->caps = hcd->regs + pdata->caps_offset;
58 retval = ehci_setup(hcd);
59 if (retval)
60 return retval;
61
Florian Fainelli45348742012-10-08 15:11:21 +020062 if (pdata->no_io_watchdog)
63 ehci->need_io_watchdog = 0;
Hauke Mehrtens7a7a4a592012-03-13 01:04:48 +010064 return 0;
65}
66
Alan Stern99f91932012-11-01 11:13:08 -040067static struct hc_driver __read_mostly ehci_platform_hc_driver;
Hauke Mehrtens7a7a4a592012-03-13 01:04:48 +010068
Andi Kleen62d08a12013-04-22 09:44:56 -070069static const struct ehci_driver_overrides platform_overrides __initconst = {
Alan Stern99f91932012-11-01 11:13:08 -040070 .reset = ehci_platform_reset,
Hauke Mehrtens7a7a4a592012-03-13 01:04:48 +010071};
72
Arnd Bergmannf3bc64d2013-03-27 21:44:22 +000073static struct usb_ehci_pdata ehci_platform_defaults;
74
Bill Pemberton41ac7b32012-11-19 13:21:48 -050075static int ehci_platform_probe(struct platform_device *dev)
Hauke Mehrtens7a7a4a592012-03-13 01:04:48 +010076{
77 struct usb_hcd *hcd;
78 struct resource *res_mem;
Arnd Bergmannf3bc64d2013-03-27 21:44:22 +000079 struct usb_ehci_pdata *pdata;
Hauke Mehrtens7a7a4a592012-03-13 01:04:48 +010080 int irq;
Russell King22d9d8e2013-06-10 16:28:49 +010081 int err;
Hauke Mehrtens7a7a4a592012-03-13 01:04:48 +010082
Hauke Mehrtens7a7a4a592012-03-13 01:04:48 +010083 if (usb_disabled())
84 return -ENODEV;
85
Arnd Bergmannf3bc64d2013-03-27 21:44:22 +000086 /*
87 * use reasonable defaults so platforms don't have to provide these.
88 * with DT probing on ARM, none of these are set.
89 */
Jingoo Hand4f09e22013-07-30 19:59:40 +090090 if (!dev_get_platdata(&dev->dev))
Arnd Bergmannf3bc64d2013-03-27 21:44:22 +000091 dev->dev.platform_data = &ehci_platform_defaults;
Russell Kinge1fd7342013-06-27 12:36:37 +010092
93 err = dma_coerce_mask_and_coherent(&dev->dev, DMA_BIT_MASK(32));
Russell King22d9d8e2013-06-10 16:28:49 +010094 if (err)
95 return err;
Arnd Bergmannf3bc64d2013-03-27 21:44:22 +000096
Jingoo Hand4f09e22013-07-30 19:59:40 +090097 pdata = dev_get_platdata(&dev->dev);
Arnd Bergmannf3bc64d2013-03-27 21:44:22 +000098
Hauke Mehrtens7a7a4a592012-03-13 01:04:48 +010099 irq = platform_get_irq(dev, 0);
100 if (irq < 0) {
Florian Fainelli2350cb02012-10-08 15:11:41 +0200101 dev_err(&dev->dev, "no irq provided");
Hauke Mehrtens7a7a4a592012-03-13 01:04:48 +0100102 return irq;
103 }
104 res_mem = platform_get_resource(dev, IORESOURCE_MEM, 0);
105 if (!res_mem) {
Florian Fainelli5c9b2b22012-10-08 15:11:43 +0200106 dev_err(&dev->dev, "no memory resource provided");
Hauke Mehrtens7a7a4a592012-03-13 01:04:48 +0100107 return -ENXIO;
108 }
109
Kuninori Morimoto04216be2012-08-06 18:08:39 -0700110 if (pdata->power_on) {
111 err = pdata->power_on(dev);
112 if (err < 0)
113 return err;
114 }
115
Hauke Mehrtens7a7a4a592012-03-13 01:04:48 +0100116 hcd = usb_create_hcd(&ehci_platform_hc_driver, &dev->dev,
117 dev_name(&dev->dev));
Kuninori Morimoto04216be2012-08-06 18:08:39 -0700118 if (!hcd) {
119 err = -ENOMEM;
120 goto err_power;
121 }
Hauke Mehrtens7a7a4a592012-03-13 01:04:48 +0100122
123 hcd->rsrc_start = res_mem->start;
124 hcd->rsrc_len = resource_size(res_mem);
125
Thierry Reding148e1132013-01-21 11:09:22 +0100126 hcd->regs = devm_ioremap_resource(&dev->dev, res_mem);
127 if (IS_ERR(hcd->regs)) {
128 err = PTR_ERR(hcd->regs);
Florian Fainelli61ff2742012-10-08 15:11:45 +0200129 goto err_put_hcd;
Julia Lawallec03ad82012-08-14 08:47:38 +0200130 }
Hauke Mehrtens7a7a4a592012-03-13 01:04:48 +0100131 err = usb_add_hcd(hcd, irq, IRQF_SHARED);
132 if (err)
Florian Fainelli61ff2742012-10-08 15:11:45 +0200133 goto err_put_hcd;
Hauke Mehrtens7a7a4a592012-03-13 01:04:48 +0100134
Peter Chen3c9740a2013-11-05 10:46:02 +0800135 device_wakeup_enable(hcd->self.controller);
Hauke Mehrtens7a7a4a592012-03-13 01:04:48 +0100136 platform_set_drvdata(dev, hcd);
137
138 return err;
139
Hauke Mehrtens7a7a4a592012-03-13 01:04:48 +0100140err_put_hcd:
141 usb_put_hcd(hcd);
Kuninori Morimoto04216be2012-08-06 18:08:39 -0700142err_power:
143 if (pdata->power_off)
144 pdata->power_off(dev);
145
Hauke Mehrtens7a7a4a592012-03-13 01:04:48 +0100146 return err;
147}
148
Bill Pembertonfb4e98a2012-11-19 13:26:20 -0500149static int ehci_platform_remove(struct platform_device *dev)
Hauke Mehrtens7a7a4a592012-03-13 01:04:48 +0100150{
151 struct usb_hcd *hcd = platform_get_drvdata(dev);
Jingoo Hand4f09e22013-07-30 19:59:40 +0900152 struct usb_ehci_pdata *pdata = dev_get_platdata(&dev->dev);
Hauke Mehrtens7a7a4a592012-03-13 01:04:48 +0100153
154 usb_remove_hcd(hcd);
Hauke Mehrtens7a7a4a592012-03-13 01:04:48 +0100155 usb_put_hcd(hcd);
Hauke Mehrtens7a7a4a592012-03-13 01:04:48 +0100156
Kuninori Morimoto04216be2012-08-06 18:08:39 -0700157 if (pdata->power_off)
158 pdata->power_off(dev);
159
Arnd Bergmannf3bc64d2013-03-27 21:44:22 +0000160 if (pdata == &ehci_platform_defaults)
161 dev->dev.platform_data = NULL;
162
Hauke Mehrtens7a7a4a592012-03-13 01:04:48 +0100163 return 0;
164}
165
166#ifdef CONFIG_PM
167
168static int ehci_platform_suspend(struct device *dev)
169{
170 struct usb_hcd *hcd = dev_get_drvdata(dev);
Jingoo Hand4f09e22013-07-30 19:59:40 +0900171 struct usb_ehci_pdata *pdata = dev_get_platdata(dev);
Kuninori Morimoto04216be2012-08-06 18:08:39 -0700172 struct platform_device *pdev =
173 container_of(dev, struct platform_device, dev);
Alan Sternc5cf9212012-06-28 11:19:02 -0400174 bool do_wakeup = device_may_wakeup(dev);
Kuninori Morimoto04216be2012-08-06 18:08:39 -0700175 int ret;
Hauke Mehrtens7a7a4a592012-03-13 01:04:48 +0100176
Kuninori Morimoto04216be2012-08-06 18:08:39 -0700177 ret = ehci_suspend(hcd, do_wakeup);
178
179 if (pdata->power_suspend)
180 pdata->power_suspend(pdev);
181
182 return ret;
Hauke Mehrtens7a7a4a592012-03-13 01:04:48 +0100183}
184
185static int ehci_platform_resume(struct device *dev)
186{
187 struct usb_hcd *hcd = dev_get_drvdata(dev);
Jingoo Hand4f09e22013-07-30 19:59:40 +0900188 struct usb_ehci_pdata *pdata = dev_get_platdata(dev);
Kuninori Morimoto04216be2012-08-06 18:08:39 -0700189 struct platform_device *pdev =
190 container_of(dev, struct platform_device, dev);
191
192 if (pdata->power_on) {
193 int err = pdata->power_on(pdev);
194 if (err < 0)
195 return err;
196 }
Hauke Mehrtens7a7a4a592012-03-13 01:04:48 +0100197
Alan Sternc5cf9212012-06-28 11:19:02 -0400198 ehci_resume(hcd, false);
Hauke Mehrtens7a7a4a592012-03-13 01:04:48 +0100199 return 0;
200}
201
202#else /* !CONFIG_PM */
203#define ehci_platform_suspend NULL
204#define ehci_platform_resume NULL
205#endif /* CONFIG_PM */
206
Arnd Bergmannf3bc64d2013-03-27 21:44:22 +0000207static const struct of_device_id vt8500_ehci_ids[] = {
208 { .compatible = "via,vt8500-ehci", },
209 { .compatible = "wm,prizm-ehci", },
210 {}
211};
212
Hauke Mehrtens7a7a4a592012-03-13 01:04:48 +0100213static const struct platform_device_id ehci_platform_table[] = {
214 { "ehci-platform", 0 },
215 { }
216};
217MODULE_DEVICE_TABLE(platform, ehci_platform_table);
218
219static const struct dev_pm_ops ehci_platform_pm_ops = {
220 .suspend = ehci_platform_suspend,
221 .resume = ehci_platform_resume,
222};
223
224static struct platform_driver ehci_platform_driver = {
225 .id_table = ehci_platform_table,
226 .probe = ehci_platform_probe,
Bill Pemberton76904172012-11-19 13:21:08 -0500227 .remove = ehci_platform_remove,
Hauke Mehrtens7a7a4a592012-03-13 01:04:48 +0100228 .shutdown = usb_hcd_platform_shutdown,
229 .driver = {
230 .owner = THIS_MODULE,
231 .name = "ehci-platform",
232 .pm = &ehci_platform_pm_ops,
Sachin Kamatae5e5f72013-05-21 17:17:16 +0530233 .of_match_table = vt8500_ehci_ids,
Hauke Mehrtens7a7a4a592012-03-13 01:04:48 +0100234 }
235};
Alan Stern99f91932012-11-01 11:13:08 -0400236
237static int __init ehci_platform_init(void)
238{
239 if (usb_disabled())
240 return -ENODEV;
241
242 pr_info("%s: " DRIVER_DESC "\n", hcd_name);
243
244 ehci_init_driver(&ehci_platform_hc_driver, &platform_overrides);
245 return platform_driver_register(&ehci_platform_driver);
246}
247module_init(ehci_platform_init);
248
249static void __exit ehci_platform_cleanup(void)
250{
251 platform_driver_unregister(&ehci_platform_driver);
252}
253module_exit(ehci_platform_cleanup);
254
255MODULE_DESCRIPTION(DRIVER_DESC);
256MODULE_AUTHOR("Hauke Mehrtens");
257MODULE_AUTHOR("Alan Stern");
258MODULE_LICENSE("GPL");