blob: 272728c48c9e6e2aa6b4cf7604a8d9280e195b0c [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) {
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
Florian Fainelli61ff2742012-10-08 15:11:45 +0200126 hcd->regs = devm_request_and_ioremap(&dev->dev, res_mem);
Julia Lawallec03ad82012-08-14 08:47:38 +0200127 if (!hcd->regs) {
128 err = -ENOMEM;
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
135 platform_set_drvdata(dev, hcd);
136
137 return err;
138
Hauke Mehrtens7a7a4a592012-03-13 01:04:48 +0100139err_put_hcd:
140 usb_put_hcd(hcd);
Kuninori Morimoto04216be2012-08-06 18:08:39 -0700141err_power:
142 if (pdata->power_off)
143 pdata->power_off(dev);
144
Hauke Mehrtens7a7a4a592012-03-13 01:04:48 +0100145 return err;
146}
147
148static int __devexit ehci_platform_remove(struct platform_device *dev)
149{
150 struct usb_hcd *hcd = platform_get_drvdata(dev);
Kuninori Morimoto04216be2012-08-06 18:08:39 -0700151 struct usb_ehci_pdata *pdata = dev->dev.platform_data;
Hauke Mehrtens7a7a4a592012-03-13 01:04:48 +0100152
153 usb_remove_hcd(hcd);
Hauke Mehrtens7a7a4a592012-03-13 01:04:48 +0100154 usb_put_hcd(hcd);
155 platform_set_drvdata(dev, NULL);
156
Kuninori Morimoto04216be2012-08-06 18:08:39 -0700157 if (pdata->power_off)
158 pdata->power_off(dev);
159
Hauke Mehrtens7a7a4a592012-03-13 01:04:48 +0100160 return 0;
161}
162
163#ifdef CONFIG_PM
164
165static int ehci_platform_suspend(struct device *dev)
166{
167 struct usb_hcd *hcd = dev_get_drvdata(dev);
Kuninori Morimoto04216be2012-08-06 18:08:39 -0700168 struct usb_ehci_pdata *pdata = dev->platform_data;
169 struct platform_device *pdev =
170 container_of(dev, struct platform_device, dev);
Alan Sternc5cf9212012-06-28 11:19:02 -0400171 bool do_wakeup = device_may_wakeup(dev);
Kuninori Morimoto04216be2012-08-06 18:08:39 -0700172 int ret;
Hauke Mehrtens7a7a4a592012-03-13 01:04:48 +0100173
Kuninori Morimoto04216be2012-08-06 18:08:39 -0700174 ret = ehci_suspend(hcd, do_wakeup);
175
176 if (pdata->power_suspend)
177 pdata->power_suspend(pdev);
178
179 return ret;
Hauke Mehrtens7a7a4a592012-03-13 01:04:48 +0100180}
181
182static int ehci_platform_resume(struct device *dev)
183{
184 struct usb_hcd *hcd = dev_get_drvdata(dev);
Kuninori Morimoto04216be2012-08-06 18:08:39 -0700185 struct usb_ehci_pdata *pdata = dev->platform_data;
186 struct platform_device *pdev =
187 container_of(dev, struct platform_device, dev);
188
189 if (pdata->power_on) {
190 int err = pdata->power_on(pdev);
191 if (err < 0)
192 return err;
193 }
Hauke Mehrtens7a7a4a592012-03-13 01:04:48 +0100194
Alan Sternc5cf9212012-06-28 11:19:02 -0400195 ehci_resume(hcd, false);
Hauke Mehrtens7a7a4a592012-03-13 01:04:48 +0100196 return 0;
197}
198
199#else /* !CONFIG_PM */
200#define ehci_platform_suspend NULL
201#define ehci_platform_resume NULL
202#endif /* CONFIG_PM */
203
204static const struct platform_device_id ehci_platform_table[] = {
205 { "ehci-platform", 0 },
206 { }
207};
208MODULE_DEVICE_TABLE(platform, ehci_platform_table);
209
210static const struct dev_pm_ops ehci_platform_pm_ops = {
211 .suspend = ehci_platform_suspend,
212 .resume = ehci_platform_resume,
213};
214
215static struct platform_driver ehci_platform_driver = {
216 .id_table = ehci_platform_table,
217 .probe = ehci_platform_probe,
218 .remove = __devexit_p(ehci_platform_remove),
219 .shutdown = usb_hcd_platform_shutdown,
220 .driver = {
221 .owner = THIS_MODULE,
222 .name = "ehci-platform",
223 .pm = &ehci_platform_pm_ops,
224 }
225};