blob: feeedf840117de45b2249f23dfda774f60a21cbe [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 Stern99f91932012-11-01 11:13:08 -040060static const struct ehci_driver_overrides platform_overrides = {
61 .product_desc = "Generic Platform EHCI controller",
62 .reset = ehci_platform_reset,
Hauke Mehrtens7a7a4a592012-03-13 01:04:48 +010063};
64
65static int __devinit ehci_platform_probe(struct platform_device *dev)
66{
67 struct usb_hcd *hcd;
68 struct resource *res_mem;
Kuninori Morimoto86e4cb32012-08-06 18:06:53 -070069 struct usb_ehci_pdata *pdata = dev->dev.platform_data;
Hauke Mehrtens7a7a4a592012-03-13 01:04:48 +010070 int irq;
71 int err = -ENOMEM;
72
Kuninori Morimoto86e4cb32012-08-06 18:06:53 -070073 if (!pdata) {
74 WARN_ON(1);
75 return -ENODEV;
76 }
Hauke Mehrtens7a7a4a592012-03-13 01:04:48 +010077
78 if (usb_disabled())
79 return -ENODEV;
80
81 irq = platform_get_irq(dev, 0);
82 if (irq < 0) {
Florian Fainelli2350cb02012-10-08 15:11:41 +020083 dev_err(&dev->dev, "no irq provided");
Hauke Mehrtens7a7a4a592012-03-13 01:04:48 +010084 return irq;
85 }
86 res_mem = platform_get_resource(dev, IORESOURCE_MEM, 0);
87 if (!res_mem) {
Florian Fainelli5c9b2b22012-10-08 15:11:43 +020088 dev_err(&dev->dev, "no memory resource provided");
Hauke Mehrtens7a7a4a592012-03-13 01:04:48 +010089 return -ENXIO;
90 }
91
Kuninori Morimoto04216be2012-08-06 18:08:39 -070092 if (pdata->power_on) {
93 err = pdata->power_on(dev);
94 if (err < 0)
95 return err;
96 }
97
Hauke Mehrtens7a7a4a592012-03-13 01:04:48 +010098 hcd = usb_create_hcd(&ehci_platform_hc_driver, &dev->dev,
99 dev_name(&dev->dev));
Kuninori Morimoto04216be2012-08-06 18:08:39 -0700100 if (!hcd) {
101 err = -ENOMEM;
102 goto err_power;
103 }
Hauke Mehrtens7a7a4a592012-03-13 01:04:48 +0100104
105 hcd->rsrc_start = res_mem->start;
106 hcd->rsrc_len = resource_size(res_mem);
107
Florian Fainelli61ff2742012-10-08 15:11:45 +0200108 hcd->regs = devm_request_and_ioremap(&dev->dev, res_mem);
Julia Lawallec03ad82012-08-14 08:47:38 +0200109 if (!hcd->regs) {
110 err = -ENOMEM;
Florian Fainelli61ff2742012-10-08 15:11:45 +0200111 goto err_put_hcd;
Julia Lawallec03ad82012-08-14 08:47:38 +0200112 }
Hauke Mehrtens7a7a4a592012-03-13 01:04:48 +0100113 err = usb_add_hcd(hcd, irq, IRQF_SHARED);
114 if (err)
Florian Fainelli61ff2742012-10-08 15:11:45 +0200115 goto err_put_hcd;
Hauke Mehrtens7a7a4a592012-03-13 01:04:48 +0100116
117 platform_set_drvdata(dev, hcd);
118
119 return err;
120
Hauke Mehrtens7a7a4a592012-03-13 01:04:48 +0100121err_put_hcd:
122 usb_put_hcd(hcd);
Kuninori Morimoto04216be2012-08-06 18:08:39 -0700123err_power:
124 if (pdata->power_off)
125 pdata->power_off(dev);
126
Hauke Mehrtens7a7a4a592012-03-13 01:04:48 +0100127 return err;
128}
129
130static int __devexit ehci_platform_remove(struct platform_device *dev)
131{
132 struct usb_hcd *hcd = platform_get_drvdata(dev);
Kuninori Morimoto04216be2012-08-06 18:08:39 -0700133 struct usb_ehci_pdata *pdata = dev->dev.platform_data;
Hauke Mehrtens7a7a4a592012-03-13 01:04:48 +0100134
135 usb_remove_hcd(hcd);
Hauke Mehrtens7a7a4a592012-03-13 01:04:48 +0100136 usb_put_hcd(hcd);
137 platform_set_drvdata(dev, NULL);
138
Kuninori Morimoto04216be2012-08-06 18:08:39 -0700139 if (pdata->power_off)
140 pdata->power_off(dev);
141
Hauke Mehrtens7a7a4a592012-03-13 01:04:48 +0100142 return 0;
143}
144
145#ifdef CONFIG_PM
146
147static int ehci_platform_suspend(struct device *dev)
148{
149 struct usb_hcd *hcd = dev_get_drvdata(dev);
Kuninori Morimoto04216be2012-08-06 18:08:39 -0700150 struct usb_ehci_pdata *pdata = dev->platform_data;
151 struct platform_device *pdev =
152 container_of(dev, struct platform_device, dev);
Alan Sternc5cf9212012-06-28 11:19:02 -0400153 bool do_wakeup = device_may_wakeup(dev);
Kuninori Morimoto04216be2012-08-06 18:08:39 -0700154 int ret;
Hauke Mehrtens7a7a4a592012-03-13 01:04:48 +0100155
Kuninori Morimoto04216be2012-08-06 18:08:39 -0700156 ret = ehci_suspend(hcd, do_wakeup);
157
158 if (pdata->power_suspend)
159 pdata->power_suspend(pdev);
160
161 return ret;
Hauke Mehrtens7a7a4a592012-03-13 01:04:48 +0100162}
163
164static int ehci_platform_resume(struct device *dev)
165{
166 struct usb_hcd *hcd = dev_get_drvdata(dev);
Kuninori Morimoto04216be2012-08-06 18:08:39 -0700167 struct usb_ehci_pdata *pdata = dev->platform_data;
168 struct platform_device *pdev =
169 container_of(dev, struct platform_device, dev);
170
171 if (pdata->power_on) {
172 int err = pdata->power_on(pdev);
173 if (err < 0)
174 return err;
175 }
Hauke Mehrtens7a7a4a592012-03-13 01:04:48 +0100176
Alan Sternc5cf9212012-06-28 11:19:02 -0400177 ehci_resume(hcd, false);
Hauke Mehrtens7a7a4a592012-03-13 01:04:48 +0100178 return 0;
179}
180
181#else /* !CONFIG_PM */
182#define ehci_platform_suspend NULL
183#define ehci_platform_resume NULL
184#endif /* CONFIG_PM */
185
186static const struct platform_device_id ehci_platform_table[] = {
187 { "ehci-platform", 0 },
188 { }
189};
190MODULE_DEVICE_TABLE(platform, ehci_platform_table);
191
192static const struct dev_pm_ops ehci_platform_pm_ops = {
193 .suspend = ehci_platform_suspend,
194 .resume = ehci_platform_resume,
195};
196
197static struct platform_driver ehci_platform_driver = {
198 .id_table = ehci_platform_table,
199 .probe = ehci_platform_probe,
200 .remove = __devexit_p(ehci_platform_remove),
201 .shutdown = usb_hcd_platform_shutdown,
202 .driver = {
203 .owner = THIS_MODULE,
204 .name = "ehci-platform",
205 .pm = &ehci_platform_pm_ops,
206 }
207};
Alan Stern99f91932012-11-01 11:13:08 -0400208
209static int __init ehci_platform_init(void)
210{
211 if (usb_disabled())
212 return -ENODEV;
213
214 pr_info("%s: " DRIVER_DESC "\n", hcd_name);
215
216 ehci_init_driver(&ehci_platform_hc_driver, &platform_overrides);
217 return platform_driver_register(&ehci_platform_driver);
218}
219module_init(ehci_platform_init);
220
221static void __exit ehci_platform_cleanup(void)
222{
223 platform_driver_unregister(&ehci_platform_driver);
224}
225module_exit(ehci_platform_cleanup);
226
227MODULE_DESCRIPTION(DRIVER_DESC);
228MODULE_AUTHOR("Hauke Mehrtens");
229MODULE_AUTHOR("Alan Stern");
230MODULE_LICENSE("GPL");