blob: b807648876be2e48f46dc8d5761e49342a86add7 [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 */
Alan Stern99f91932012-11-01 11:13:08 -040021#include <linux/kernel.h>
Alan Sternd1bb67a2012-11-02 10:13:24 -040022#include <linux/hrtimer.h>
23#include <linux/io.h>
Alan Stern99f91932012-11-01 11:13:08 -040024#include <linux/module.h>
Hauke Mehrtens7a7a4a592012-03-13 01:04:48 +010025#include <linux/platform_device.h>
Alan Stern99f91932012-11-01 11:13:08 -040026#include <linux/usb.h>
27#include <linux/usb/hcd.h>
Hauke Mehrtens7a7a4a592012-03-13 01:04:48 +010028#include <linux/usb/ehci_pdriver.h>
29
Alan Stern99f91932012-11-01 11:13:08 -040030#include "ehci.h"
31
32#define DRIVER_DESC "EHCI generic platform driver"
33
34static const char hcd_name[] = "ehci-platform";
35
Hauke Mehrtens7a7a4a592012-03-13 01:04:48 +010036static int ehci_platform_reset(struct usb_hcd *hcd)
37{
38 struct platform_device *pdev = to_platform_device(hcd->self.controller);
39 struct usb_ehci_pdata *pdata = pdev->dev.platform_data;
40 struct ehci_hcd *ehci = hcd_to_ehci(hcd);
41 int retval;
42
43 hcd->has_tt = pdata->has_tt;
44 ehci->has_synopsys_hc_bug = pdata->has_synopsys_hc_bug;
45 ehci->big_endian_desc = pdata->big_endian_desc;
46 ehci->big_endian_mmio = pdata->big_endian_mmio;
47
48 ehci->caps = hcd->regs + pdata->caps_offset;
49 retval = ehci_setup(hcd);
50 if (retval)
51 return retval;
52
Florian Fainelli45348742012-10-08 15:11:21 +020053 if (pdata->no_io_watchdog)
54 ehci->need_io_watchdog = 0;
Hauke Mehrtens7a7a4a592012-03-13 01:04:48 +010055 return 0;
56}
57
Alan Stern99f91932012-11-01 11:13:08 -040058static struct hc_driver __read_mostly ehci_platform_hc_driver;
Hauke Mehrtens7a7a4a592012-03-13 01:04:48 +010059
Alan Stern1b368102012-11-07 16:12:47 -050060static const struct ehci_driver_overrides platform_overrides __initdata = {
Alan Stern99f91932012-11-01 11:13:08 -040061 .reset = ehci_platform_reset,
Hauke Mehrtens7a7a4a592012-03-13 01:04:48 +010062};
63
64static int __devinit ehci_platform_probe(struct platform_device *dev)
65{
66 struct usb_hcd *hcd;
67 struct resource *res_mem;
Kuninori Morimoto86e4cb32012-08-06 18:06:53 -070068 struct usb_ehci_pdata *pdata = dev->dev.platform_data;
Hauke Mehrtens7a7a4a592012-03-13 01:04:48 +010069 int irq;
70 int err = -ENOMEM;
71
Kuninori Morimoto86e4cb32012-08-06 18:06:53 -070072 if (!pdata) {
73 WARN_ON(1);
74 return -ENODEV;
75 }
Hauke Mehrtens7a7a4a592012-03-13 01:04:48 +010076
77 if (usb_disabled())
78 return -ENODEV;
79
80 irq = platform_get_irq(dev, 0);
81 if (irq < 0) {
Florian Fainelli2350cb02012-10-08 15:11:41 +020082 dev_err(&dev->dev, "no irq provided");
Hauke Mehrtens7a7a4a592012-03-13 01:04:48 +010083 return irq;
84 }
85 res_mem = platform_get_resource(dev, IORESOURCE_MEM, 0);
86 if (!res_mem) {
Florian Fainelli5c9b2b22012-10-08 15:11:43 +020087 dev_err(&dev->dev, "no memory resource provided");
Hauke Mehrtens7a7a4a592012-03-13 01:04:48 +010088 return -ENXIO;
89 }
90
Kuninori Morimoto04216be2012-08-06 18:08:39 -070091 if (pdata->power_on) {
92 err = pdata->power_on(dev);
93 if (err < 0)
94 return err;
95 }
96
Hauke Mehrtens7a7a4a592012-03-13 01:04:48 +010097 hcd = usb_create_hcd(&ehci_platform_hc_driver, &dev->dev,
98 dev_name(&dev->dev));
Kuninori Morimoto04216be2012-08-06 18:08:39 -070099 if (!hcd) {
100 err = -ENOMEM;
101 goto err_power;
102 }
Hauke Mehrtens7a7a4a592012-03-13 01:04:48 +0100103
104 hcd->rsrc_start = res_mem->start;
105 hcd->rsrc_len = resource_size(res_mem);
106
Florian Fainelli61ff2742012-10-08 15:11:45 +0200107 hcd->regs = devm_request_and_ioremap(&dev->dev, res_mem);
Julia Lawallec03ad82012-08-14 08:47:38 +0200108 if (!hcd->regs) {
109 err = -ENOMEM;
Florian Fainelli61ff2742012-10-08 15:11:45 +0200110 goto err_put_hcd;
Julia Lawallec03ad82012-08-14 08:47:38 +0200111 }
Hauke Mehrtens7a7a4a592012-03-13 01:04:48 +0100112 err = usb_add_hcd(hcd, irq, IRQF_SHARED);
113 if (err)
Florian Fainelli61ff2742012-10-08 15:11:45 +0200114 goto err_put_hcd;
Hauke Mehrtens7a7a4a592012-03-13 01:04:48 +0100115
116 platform_set_drvdata(dev, hcd);
117
118 return err;
119
Hauke Mehrtens7a7a4a592012-03-13 01:04:48 +0100120err_put_hcd:
121 usb_put_hcd(hcd);
Kuninori Morimoto04216be2012-08-06 18:08:39 -0700122err_power:
123 if (pdata->power_off)
124 pdata->power_off(dev);
125
Hauke Mehrtens7a7a4a592012-03-13 01:04:48 +0100126 return err;
127}
128
129static int __devexit ehci_platform_remove(struct platform_device *dev)
130{
131 struct usb_hcd *hcd = platform_get_drvdata(dev);
Kuninori Morimoto04216be2012-08-06 18:08:39 -0700132 struct usb_ehci_pdata *pdata = dev->dev.platform_data;
Hauke Mehrtens7a7a4a592012-03-13 01:04:48 +0100133
134 usb_remove_hcd(hcd);
Hauke Mehrtens7a7a4a592012-03-13 01:04:48 +0100135 usb_put_hcd(hcd);
136 platform_set_drvdata(dev, NULL);
137
Kuninori Morimoto04216be2012-08-06 18:08:39 -0700138 if (pdata->power_off)
139 pdata->power_off(dev);
140
Hauke Mehrtens7a7a4a592012-03-13 01:04:48 +0100141 return 0;
142}
143
144#ifdef CONFIG_PM
145
146static int ehci_platform_suspend(struct device *dev)
147{
148 struct usb_hcd *hcd = dev_get_drvdata(dev);
Kuninori Morimoto04216be2012-08-06 18:08:39 -0700149 struct usb_ehci_pdata *pdata = dev->platform_data;
150 struct platform_device *pdev =
151 container_of(dev, struct platform_device, dev);
Alan Sternc5cf9212012-06-28 11:19:02 -0400152 bool do_wakeup = device_may_wakeup(dev);
Kuninori Morimoto04216be2012-08-06 18:08:39 -0700153 int ret;
Hauke Mehrtens7a7a4a592012-03-13 01:04:48 +0100154
Kuninori Morimoto04216be2012-08-06 18:08:39 -0700155 ret = ehci_suspend(hcd, do_wakeup);
156
157 if (pdata->power_suspend)
158 pdata->power_suspend(pdev);
159
160 return ret;
Hauke Mehrtens7a7a4a592012-03-13 01:04:48 +0100161}
162
163static int ehci_platform_resume(struct device *dev)
164{
165 struct usb_hcd *hcd = dev_get_drvdata(dev);
Kuninori Morimoto04216be2012-08-06 18:08:39 -0700166 struct usb_ehci_pdata *pdata = dev->platform_data;
167 struct platform_device *pdev =
168 container_of(dev, struct platform_device, dev);
169
170 if (pdata->power_on) {
171 int err = pdata->power_on(pdev);
172 if (err < 0)
173 return err;
174 }
Hauke Mehrtens7a7a4a592012-03-13 01:04:48 +0100175
Alan Sternc5cf9212012-06-28 11:19:02 -0400176 ehci_resume(hcd, false);
Hauke Mehrtens7a7a4a592012-03-13 01:04:48 +0100177 return 0;
178}
179
180#else /* !CONFIG_PM */
181#define ehci_platform_suspend NULL
182#define ehci_platform_resume NULL
183#endif /* CONFIG_PM */
184
185static const struct platform_device_id ehci_platform_table[] = {
186 { "ehci-platform", 0 },
187 { }
188};
189MODULE_DEVICE_TABLE(platform, ehci_platform_table);
190
191static const struct dev_pm_ops ehci_platform_pm_ops = {
192 .suspend = ehci_platform_suspend,
193 .resume = ehci_platform_resume,
194};
195
196static struct platform_driver ehci_platform_driver = {
197 .id_table = ehci_platform_table,
198 .probe = ehci_platform_probe,
Bill Pemberton76904172012-11-19 13:21:08 -0500199 .remove = ehci_platform_remove,
Hauke Mehrtens7a7a4a592012-03-13 01:04:48 +0100200 .shutdown = usb_hcd_platform_shutdown,
201 .driver = {
202 .owner = THIS_MODULE,
203 .name = "ehci-platform",
204 .pm = &ehci_platform_pm_ops,
205 }
206};
Alan Stern99f91932012-11-01 11:13:08 -0400207
208static int __init ehci_platform_init(void)
209{
210 if (usb_disabled())
211 return -ENODEV;
212
213 pr_info("%s: " DRIVER_DESC "\n", hcd_name);
214
215 ehci_init_driver(&ehci_platform_hc_driver, &platform_overrides);
216 return platform_driver_register(&ehci_platform_driver);
217}
218module_init(ehci_platform_init);
219
220static void __exit ehci_platform_cleanup(void)
221{
222 platform_driver_unregister(&ehci_platform_driver);
223}
224module_exit(ehci_platform_cleanup);
225
226MODULE_DESCRIPTION(DRIVER_DESC);
227MODULE_AUTHOR("Hauke Mehrtens");
228MODULE_AUTHOR("Alan Stern");
229MODULE_LICENSE("GPL");