blob: 607adf9adb8309b60dd2ff19deb17d5632039908 [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 */
21#include <linux/platform_device.h>
22#include <linux/usb/ehci_pdriver.h>
23
24static int ehci_platform_reset(struct usb_hcd *hcd)
25{
26 struct platform_device *pdev = to_platform_device(hcd->self.controller);
27 struct usb_ehci_pdata *pdata = pdev->dev.platform_data;
28 struct ehci_hcd *ehci = hcd_to_ehci(hcd);
29 int retval;
30
31 hcd->has_tt = pdata->has_tt;
32 ehci->has_synopsys_hc_bug = pdata->has_synopsys_hc_bug;
33 ehci->big_endian_desc = pdata->big_endian_desc;
34 ehci->big_endian_mmio = pdata->big_endian_mmio;
35
36 ehci->caps = hcd->regs + pdata->caps_offset;
37 retval = ehci_setup(hcd);
38 if (retval)
39 return retval;
40
Florian Fainelli45348742012-10-08 15:11:21 +020041 if (pdata->no_io_watchdog)
42 ehci->need_io_watchdog = 0;
Hauke Mehrtens7a7a4a592012-03-13 01:04:48 +010043 if (pdata->port_power_on)
44 ehci_port_power(ehci, 1);
45 if (pdata->port_power_off)
46 ehci_port_power(ehci, 0);
47
48 return 0;
49}
50
51static const struct hc_driver ehci_platform_hc_driver = {
52 .description = hcd_name,
53 .product_desc = "Generic Platform EHCI Controller",
54 .hcd_priv_size = sizeof(struct ehci_hcd),
55
56 .irq = ehci_irq,
57 .flags = HCD_MEMORY | HCD_USB2,
58
59 .reset = ehci_platform_reset,
60 .start = ehci_run,
61 .stop = ehci_stop,
62 .shutdown = ehci_shutdown,
63
64 .urb_enqueue = ehci_urb_enqueue,
65 .urb_dequeue = ehci_urb_dequeue,
66 .endpoint_disable = ehci_endpoint_disable,
67 .endpoint_reset = ehci_endpoint_reset,
68
69 .get_frame_number = ehci_get_frame,
70
71 .hub_status_data = ehci_hub_status_data,
72 .hub_control = ehci_hub_control,
73#if defined(CONFIG_PM)
74 .bus_suspend = ehci_bus_suspend,
75 .bus_resume = ehci_bus_resume,
76#endif
77 .relinquish_port = ehci_relinquish_port,
78 .port_handed_over = ehci_port_handed_over,
79
Hauke Mehrtens7a7a4a592012-03-13 01:04:48 +010080 .clear_tt_buffer_complete = ehci_clear_tt_buffer_complete,
81};
82
83static int __devinit ehci_platform_probe(struct platform_device *dev)
84{
85 struct usb_hcd *hcd;
86 struct resource *res_mem;
Kuninori Morimoto86e4cb32012-08-06 18:06:53 -070087 struct usb_ehci_pdata *pdata = dev->dev.platform_data;
Hauke Mehrtens7a7a4a592012-03-13 01:04:48 +010088 int irq;
89 int err = -ENOMEM;
90
Kuninori Morimoto86e4cb32012-08-06 18:06:53 -070091 if (!pdata) {
92 WARN_ON(1);
93 return -ENODEV;
94 }
Hauke Mehrtens7a7a4a592012-03-13 01:04:48 +010095
96 if (usb_disabled())
97 return -ENODEV;
98
99 irq = platform_get_irq(dev, 0);
100 if (irq < 0) {
Masanari Iida1c1301d2012-04-19 00:04:46 +0900101 pr_err("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) {
Masanari Iida1c1301d2012-04-19 00:04:46 +0900106 pr_err("no memory recourse 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
126 if (!request_mem_region(hcd->rsrc_start, hcd->rsrc_len, hcd_name)) {
127 pr_err("controller already in use");
128 err = -EBUSY;
129 goto err_put_hcd;
130 }
131
132 hcd->regs = ioremap_nocache(hcd->rsrc_start, hcd->rsrc_len);
Julia Lawallec03ad82012-08-14 08:47:38 +0200133 if (!hcd->regs) {
134 err = -ENOMEM;
Hauke Mehrtens7a7a4a592012-03-13 01:04:48 +0100135 goto err_release_region;
Julia Lawallec03ad82012-08-14 08:47:38 +0200136 }
Hauke Mehrtens7a7a4a592012-03-13 01:04:48 +0100137 err = usb_add_hcd(hcd, irq, IRQF_SHARED);
138 if (err)
139 goto err_iounmap;
140
141 platform_set_drvdata(dev, hcd);
142
143 return err;
144
145err_iounmap:
146 iounmap(hcd->regs);
147err_release_region:
148 release_mem_region(hcd->rsrc_start, hcd->rsrc_len);
149err_put_hcd:
150 usb_put_hcd(hcd);
Kuninori Morimoto04216be2012-08-06 18:08:39 -0700151err_power:
152 if (pdata->power_off)
153 pdata->power_off(dev);
154
Hauke Mehrtens7a7a4a592012-03-13 01:04:48 +0100155 return err;
156}
157
158static int __devexit ehci_platform_remove(struct platform_device *dev)
159{
160 struct usb_hcd *hcd = platform_get_drvdata(dev);
Kuninori Morimoto04216be2012-08-06 18:08:39 -0700161 struct usb_ehci_pdata *pdata = dev->dev.platform_data;
Hauke Mehrtens7a7a4a592012-03-13 01:04:48 +0100162
163 usb_remove_hcd(hcd);
164 iounmap(hcd->regs);
165 release_mem_region(hcd->rsrc_start, hcd->rsrc_len);
166 usb_put_hcd(hcd);
167 platform_set_drvdata(dev, NULL);
168
Kuninori Morimoto04216be2012-08-06 18:08:39 -0700169 if (pdata->power_off)
170 pdata->power_off(dev);
171
Hauke Mehrtens7a7a4a592012-03-13 01:04:48 +0100172 return 0;
173}
174
175#ifdef CONFIG_PM
176
177static int ehci_platform_suspend(struct device *dev)
178{
179 struct usb_hcd *hcd = dev_get_drvdata(dev);
Kuninori Morimoto04216be2012-08-06 18:08:39 -0700180 struct usb_ehci_pdata *pdata = dev->platform_data;
181 struct platform_device *pdev =
182 container_of(dev, struct platform_device, dev);
Alan Sternc5cf9212012-06-28 11:19:02 -0400183 bool do_wakeup = device_may_wakeup(dev);
Kuninori Morimoto04216be2012-08-06 18:08:39 -0700184 int ret;
Hauke Mehrtens7a7a4a592012-03-13 01:04:48 +0100185
Kuninori Morimoto04216be2012-08-06 18:08:39 -0700186 ret = ehci_suspend(hcd, do_wakeup);
187
188 if (pdata->power_suspend)
189 pdata->power_suspend(pdev);
190
191 return ret;
Hauke Mehrtens7a7a4a592012-03-13 01:04:48 +0100192}
193
194static int ehci_platform_resume(struct device *dev)
195{
196 struct usb_hcd *hcd = dev_get_drvdata(dev);
Kuninori Morimoto04216be2012-08-06 18:08:39 -0700197 struct usb_ehci_pdata *pdata = dev->platform_data;
198 struct platform_device *pdev =
199 container_of(dev, struct platform_device, dev);
200
201 if (pdata->power_on) {
202 int err = pdata->power_on(pdev);
203 if (err < 0)
204 return err;
205 }
Hauke Mehrtens7a7a4a592012-03-13 01:04:48 +0100206
Alan Sternc5cf9212012-06-28 11:19:02 -0400207 ehci_resume(hcd, false);
Hauke Mehrtens7a7a4a592012-03-13 01:04:48 +0100208 return 0;
209}
210
211#else /* !CONFIG_PM */
212#define ehci_platform_suspend NULL
213#define ehci_platform_resume NULL
214#endif /* CONFIG_PM */
215
216static const struct platform_device_id ehci_platform_table[] = {
217 { "ehci-platform", 0 },
218 { }
219};
220MODULE_DEVICE_TABLE(platform, ehci_platform_table);
221
222static const struct dev_pm_ops ehci_platform_pm_ops = {
223 .suspend = ehci_platform_suspend,
224 .resume = ehci_platform_resume,
225};
226
227static struct platform_driver ehci_platform_driver = {
228 .id_table = ehci_platform_table,
229 .probe = ehci_platform_probe,
230 .remove = __devexit_p(ehci_platform_remove),
231 .shutdown = usb_hcd_platform_shutdown,
232 .driver = {
233 .owner = THIS_MODULE,
234 .name = "ehci-platform",
235 .pm = &ehci_platform_pm_ops,
236 }
237};