blob: 7f30b7168d5a53542cdbea786d9cba849a932579 [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
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
Bill Pembertonfb4e98a2012-11-19 13:26:20 -0500148static int ehci_platform_remove(struct platform_device *dev)
Hauke Mehrtens7a7a4a592012-03-13 01:04:48 +0100149{
150 struct usb_hcd *hcd = platform_get_drvdata(dev);
Jingoo Hand4f09e22013-07-30 19:59:40 +0900151 struct usb_ehci_pdata *pdata = dev_get_platdata(&dev->dev);
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);
Hauke Mehrtens7a7a4a592012-03-13 01:04:48 +0100155
Kuninori Morimoto04216be2012-08-06 18:08:39 -0700156 if (pdata->power_off)
157 pdata->power_off(dev);
158
Arnd Bergmannf3bc64d2013-03-27 21:44:22 +0000159 if (pdata == &ehci_platform_defaults)
160 dev->dev.platform_data = NULL;
161
Hauke Mehrtens7a7a4a592012-03-13 01:04:48 +0100162 return 0;
163}
164
165#ifdef CONFIG_PM
166
167static int ehci_platform_suspend(struct device *dev)
168{
169 struct usb_hcd *hcd = dev_get_drvdata(dev);
Jingoo Hand4f09e22013-07-30 19:59:40 +0900170 struct usb_ehci_pdata *pdata = dev_get_platdata(dev);
Kuninori Morimoto04216be2012-08-06 18:08:39 -0700171 struct platform_device *pdev =
172 container_of(dev, struct platform_device, dev);
Alan Sternc5cf9212012-06-28 11:19:02 -0400173 bool do_wakeup = device_may_wakeup(dev);
Kuninori Morimoto04216be2012-08-06 18:08:39 -0700174 int ret;
Hauke Mehrtens7a7a4a592012-03-13 01:04:48 +0100175
Kuninori Morimoto04216be2012-08-06 18:08:39 -0700176 ret = ehci_suspend(hcd, do_wakeup);
177
178 if (pdata->power_suspend)
179 pdata->power_suspend(pdev);
180
181 return ret;
Hauke Mehrtens7a7a4a592012-03-13 01:04:48 +0100182}
183
184static int ehci_platform_resume(struct device *dev)
185{
186 struct usb_hcd *hcd = dev_get_drvdata(dev);
Jingoo Hand4f09e22013-07-30 19:59:40 +0900187 struct usb_ehci_pdata *pdata = dev_get_platdata(dev);
Kuninori Morimoto04216be2012-08-06 18:08:39 -0700188 struct platform_device *pdev =
189 container_of(dev, struct platform_device, dev);
190
191 if (pdata->power_on) {
192 int err = pdata->power_on(pdev);
193 if (err < 0)
194 return err;
195 }
Hauke Mehrtens7a7a4a592012-03-13 01:04:48 +0100196
Alan Sternc5cf9212012-06-28 11:19:02 -0400197 ehci_resume(hcd, false);
Hauke Mehrtens7a7a4a592012-03-13 01:04:48 +0100198 return 0;
199}
200
201#else /* !CONFIG_PM */
202#define ehci_platform_suspend NULL
203#define ehci_platform_resume NULL
204#endif /* CONFIG_PM */
205
Arnd Bergmannf3bc64d2013-03-27 21:44:22 +0000206static const struct of_device_id vt8500_ehci_ids[] = {
207 { .compatible = "via,vt8500-ehci", },
208 { .compatible = "wm,prizm-ehci", },
209 {}
210};
211
Hauke Mehrtens7a7a4a592012-03-13 01:04:48 +0100212static const struct platform_device_id ehci_platform_table[] = {
213 { "ehci-platform", 0 },
214 { }
215};
216MODULE_DEVICE_TABLE(platform, ehci_platform_table);
217
218static const struct dev_pm_ops ehci_platform_pm_ops = {
219 .suspend = ehci_platform_suspend,
220 .resume = ehci_platform_resume,
221};
222
223static struct platform_driver ehci_platform_driver = {
224 .id_table = ehci_platform_table,
225 .probe = ehci_platform_probe,
Bill Pemberton76904172012-11-19 13:21:08 -0500226 .remove = ehci_platform_remove,
Hauke Mehrtens7a7a4a592012-03-13 01:04:48 +0100227 .shutdown = usb_hcd_platform_shutdown,
228 .driver = {
229 .owner = THIS_MODULE,
230 .name = "ehci-platform",
231 .pm = &ehci_platform_pm_ops,
Sachin Kamatae5e5f72013-05-21 17:17:16 +0530232 .of_match_table = vt8500_ehci_ids,
Hauke Mehrtens7a7a4a592012-03-13 01:04:48 +0100233 }
234};
Alan Stern99f91932012-11-01 11:13:08 -0400235
236static int __init ehci_platform_init(void)
237{
238 if (usb_disabled())
239 return -ENODEV;
240
241 pr_info("%s: " DRIVER_DESC "\n", hcd_name);
242
243 ehci_init_driver(&ehci_platform_hc_driver, &platform_overrides);
244 return platform_driver_register(&ehci_platform_driver);
245}
246module_init(ehci_platform_init);
247
248static void __exit ehci_platform_cleanup(void)
249{
250 platform_driver_unregister(&ehci_platform_driver);
251}
252module_exit(ehci_platform_cleanup);
253
254MODULE_DESCRIPTION(DRIVER_DESC);
255MODULE_AUTHOR("Hauke Mehrtens");
256MODULE_AUTHOR("Alan Stern");
257MODULE_LICENSE("GPL");