Hauke Mehrtens | 7a7a4a59 | 2012-03-13 01:04:48 +0100 | [diff] [blame] | 1 | /* |
| 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 Stern | 99f9193 | 2012-11-01 11:13:08 -0400 | [diff] [blame] | 21 | #include <linux/kernel.h> |
Alan Stern | d1bb67a | 2012-11-02 10:13:24 -0400 | [diff] [blame] | 22 | #include <linux/hrtimer.h> |
| 23 | #include <linux/io.h> |
Alan Stern | 99f9193 | 2012-11-01 11:13:08 -0400 | [diff] [blame] | 24 | #include <linux/module.h> |
Hauke Mehrtens | 7a7a4a59 | 2012-03-13 01:04:48 +0100 | [diff] [blame] | 25 | #include <linux/platform_device.h> |
Alan Stern | 99f9193 | 2012-11-01 11:13:08 -0400 | [diff] [blame] | 26 | #include <linux/usb.h> |
| 27 | #include <linux/usb/hcd.h> |
Hauke Mehrtens | 7a7a4a59 | 2012-03-13 01:04:48 +0100 | [diff] [blame] | 28 | #include <linux/usb/ehci_pdriver.h> |
| 29 | |
Alan Stern | 99f9193 | 2012-11-01 11:13:08 -0400 | [diff] [blame] | 30 | #include "ehci.h" |
| 31 | |
| 32 | #define DRIVER_DESC "EHCI generic platform driver" |
| 33 | |
| 34 | static const char hcd_name[] = "ehci-platform"; |
| 35 | |
Hauke Mehrtens | 7a7a4a59 | 2012-03-13 01:04:48 +0100 | [diff] [blame] | 36 | static 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 Fainelli | 4534874 | 2012-10-08 15:11:21 +0200 | [diff] [blame] | 53 | if (pdata->no_io_watchdog) |
| 54 | ehci->need_io_watchdog = 0; |
Hauke Mehrtens | 7a7a4a59 | 2012-03-13 01:04:48 +0100 | [diff] [blame] | 55 | return 0; |
| 56 | } |
| 57 | |
Alan Stern | 99f9193 | 2012-11-01 11:13:08 -0400 | [diff] [blame] | 58 | static struct hc_driver __read_mostly ehci_platform_hc_driver; |
Hauke Mehrtens | 7a7a4a59 | 2012-03-13 01:04:48 +0100 | [diff] [blame] | 59 | |
Alan Stern | 1b36810 | 2012-11-07 16:12:47 -0500 | [diff] [blame] | 60 | static const struct ehci_driver_overrides platform_overrides __initdata = { |
Alan Stern | 99f9193 | 2012-11-01 11:13:08 -0400 | [diff] [blame] | 61 | .reset = ehci_platform_reset, |
Hauke Mehrtens | 7a7a4a59 | 2012-03-13 01:04:48 +0100 | [diff] [blame] | 62 | }; |
| 63 | |
| 64 | static int __devinit ehci_platform_probe(struct platform_device *dev) |
| 65 | { |
| 66 | struct usb_hcd *hcd; |
| 67 | struct resource *res_mem; |
Kuninori Morimoto | 86e4cb3 | 2012-08-06 18:06:53 -0700 | [diff] [blame] | 68 | struct usb_ehci_pdata *pdata = dev->dev.platform_data; |
Hauke Mehrtens | 7a7a4a59 | 2012-03-13 01:04:48 +0100 | [diff] [blame] | 69 | int irq; |
| 70 | int err = -ENOMEM; |
| 71 | |
Kuninori Morimoto | 86e4cb3 | 2012-08-06 18:06:53 -0700 | [diff] [blame] | 72 | if (!pdata) { |
| 73 | WARN_ON(1); |
| 74 | return -ENODEV; |
| 75 | } |
Hauke Mehrtens | 7a7a4a59 | 2012-03-13 01:04:48 +0100 | [diff] [blame] | 76 | |
| 77 | if (usb_disabled()) |
| 78 | return -ENODEV; |
| 79 | |
| 80 | irq = platform_get_irq(dev, 0); |
| 81 | if (irq < 0) { |
Florian Fainelli | 2350cb0 | 2012-10-08 15:11:41 +0200 | [diff] [blame] | 82 | dev_err(&dev->dev, "no irq provided"); |
Hauke Mehrtens | 7a7a4a59 | 2012-03-13 01:04:48 +0100 | [diff] [blame] | 83 | return irq; |
| 84 | } |
| 85 | res_mem = platform_get_resource(dev, IORESOURCE_MEM, 0); |
| 86 | if (!res_mem) { |
Florian Fainelli | 5c9b2b2 | 2012-10-08 15:11:43 +0200 | [diff] [blame] | 87 | dev_err(&dev->dev, "no memory resource provided"); |
Hauke Mehrtens | 7a7a4a59 | 2012-03-13 01:04:48 +0100 | [diff] [blame] | 88 | return -ENXIO; |
| 89 | } |
| 90 | |
Kuninori Morimoto | 04216be | 2012-08-06 18:08:39 -0700 | [diff] [blame] | 91 | if (pdata->power_on) { |
| 92 | err = pdata->power_on(dev); |
| 93 | if (err < 0) |
| 94 | return err; |
| 95 | } |
| 96 | |
Hauke Mehrtens | 7a7a4a59 | 2012-03-13 01:04:48 +0100 | [diff] [blame] | 97 | hcd = usb_create_hcd(&ehci_platform_hc_driver, &dev->dev, |
| 98 | dev_name(&dev->dev)); |
Kuninori Morimoto | 04216be | 2012-08-06 18:08:39 -0700 | [diff] [blame] | 99 | if (!hcd) { |
| 100 | err = -ENOMEM; |
| 101 | goto err_power; |
| 102 | } |
Hauke Mehrtens | 7a7a4a59 | 2012-03-13 01:04:48 +0100 | [diff] [blame] | 103 | |
| 104 | hcd->rsrc_start = res_mem->start; |
| 105 | hcd->rsrc_len = resource_size(res_mem); |
| 106 | |
Florian Fainelli | 61ff274 | 2012-10-08 15:11:45 +0200 | [diff] [blame] | 107 | hcd->regs = devm_request_and_ioremap(&dev->dev, res_mem); |
Julia Lawall | ec03ad8 | 2012-08-14 08:47:38 +0200 | [diff] [blame] | 108 | if (!hcd->regs) { |
| 109 | err = -ENOMEM; |
Florian Fainelli | 61ff274 | 2012-10-08 15:11:45 +0200 | [diff] [blame] | 110 | goto err_put_hcd; |
Julia Lawall | ec03ad8 | 2012-08-14 08:47:38 +0200 | [diff] [blame] | 111 | } |
Hauke Mehrtens | 7a7a4a59 | 2012-03-13 01:04:48 +0100 | [diff] [blame] | 112 | err = usb_add_hcd(hcd, irq, IRQF_SHARED); |
| 113 | if (err) |
Florian Fainelli | 61ff274 | 2012-10-08 15:11:45 +0200 | [diff] [blame] | 114 | goto err_put_hcd; |
Hauke Mehrtens | 7a7a4a59 | 2012-03-13 01:04:48 +0100 | [diff] [blame] | 115 | |
| 116 | platform_set_drvdata(dev, hcd); |
| 117 | |
| 118 | return err; |
| 119 | |
Hauke Mehrtens | 7a7a4a59 | 2012-03-13 01:04:48 +0100 | [diff] [blame] | 120 | err_put_hcd: |
| 121 | usb_put_hcd(hcd); |
Kuninori Morimoto | 04216be | 2012-08-06 18:08:39 -0700 | [diff] [blame] | 122 | err_power: |
| 123 | if (pdata->power_off) |
| 124 | pdata->power_off(dev); |
| 125 | |
Hauke Mehrtens | 7a7a4a59 | 2012-03-13 01:04:48 +0100 | [diff] [blame] | 126 | return err; |
| 127 | } |
| 128 | |
| 129 | static int __devexit ehci_platform_remove(struct platform_device *dev) |
| 130 | { |
| 131 | struct usb_hcd *hcd = platform_get_drvdata(dev); |
Kuninori Morimoto | 04216be | 2012-08-06 18:08:39 -0700 | [diff] [blame] | 132 | struct usb_ehci_pdata *pdata = dev->dev.platform_data; |
Hauke Mehrtens | 7a7a4a59 | 2012-03-13 01:04:48 +0100 | [diff] [blame] | 133 | |
| 134 | usb_remove_hcd(hcd); |
Hauke Mehrtens | 7a7a4a59 | 2012-03-13 01:04:48 +0100 | [diff] [blame] | 135 | usb_put_hcd(hcd); |
| 136 | platform_set_drvdata(dev, NULL); |
| 137 | |
Kuninori Morimoto | 04216be | 2012-08-06 18:08:39 -0700 | [diff] [blame] | 138 | if (pdata->power_off) |
| 139 | pdata->power_off(dev); |
| 140 | |
Hauke Mehrtens | 7a7a4a59 | 2012-03-13 01:04:48 +0100 | [diff] [blame] | 141 | return 0; |
| 142 | } |
| 143 | |
| 144 | #ifdef CONFIG_PM |
| 145 | |
| 146 | static int ehci_platform_suspend(struct device *dev) |
| 147 | { |
| 148 | struct usb_hcd *hcd = dev_get_drvdata(dev); |
Kuninori Morimoto | 04216be | 2012-08-06 18:08:39 -0700 | [diff] [blame] | 149 | struct usb_ehci_pdata *pdata = dev->platform_data; |
| 150 | struct platform_device *pdev = |
| 151 | container_of(dev, struct platform_device, dev); |
Alan Stern | c5cf921 | 2012-06-28 11:19:02 -0400 | [diff] [blame] | 152 | bool do_wakeup = device_may_wakeup(dev); |
Kuninori Morimoto | 04216be | 2012-08-06 18:08:39 -0700 | [diff] [blame] | 153 | int ret; |
Hauke Mehrtens | 7a7a4a59 | 2012-03-13 01:04:48 +0100 | [diff] [blame] | 154 | |
Kuninori Morimoto | 04216be | 2012-08-06 18:08:39 -0700 | [diff] [blame] | 155 | ret = ehci_suspend(hcd, do_wakeup); |
| 156 | |
| 157 | if (pdata->power_suspend) |
| 158 | pdata->power_suspend(pdev); |
| 159 | |
| 160 | return ret; |
Hauke Mehrtens | 7a7a4a59 | 2012-03-13 01:04:48 +0100 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | static int ehci_platform_resume(struct device *dev) |
| 164 | { |
| 165 | struct usb_hcd *hcd = dev_get_drvdata(dev); |
Kuninori Morimoto | 04216be | 2012-08-06 18:08:39 -0700 | [diff] [blame] | 166 | 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 Mehrtens | 7a7a4a59 | 2012-03-13 01:04:48 +0100 | [diff] [blame] | 175 | |
Alan Stern | c5cf921 | 2012-06-28 11:19:02 -0400 | [diff] [blame] | 176 | ehci_resume(hcd, false); |
Hauke Mehrtens | 7a7a4a59 | 2012-03-13 01:04:48 +0100 | [diff] [blame] | 177 | 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 | |
| 185 | static const struct platform_device_id ehci_platform_table[] = { |
| 186 | { "ehci-platform", 0 }, |
| 187 | { } |
| 188 | }; |
| 189 | MODULE_DEVICE_TABLE(platform, ehci_platform_table); |
| 190 | |
| 191 | static const struct dev_pm_ops ehci_platform_pm_ops = { |
| 192 | .suspend = ehci_platform_suspend, |
| 193 | .resume = ehci_platform_resume, |
| 194 | }; |
| 195 | |
| 196 | static struct platform_driver ehci_platform_driver = { |
| 197 | .id_table = ehci_platform_table, |
| 198 | .probe = ehci_platform_probe, |
Bill Pemberton | 7690417 | 2012-11-19 13:21:08 -0500 | [diff] [blame^] | 199 | .remove = ehci_platform_remove, |
Hauke Mehrtens | 7a7a4a59 | 2012-03-13 01:04:48 +0100 | [diff] [blame] | 200 | .shutdown = usb_hcd_platform_shutdown, |
| 201 | .driver = { |
| 202 | .owner = THIS_MODULE, |
| 203 | .name = "ehci-platform", |
| 204 | .pm = &ehci_platform_pm_ops, |
| 205 | } |
| 206 | }; |
Alan Stern | 99f9193 | 2012-11-01 11:13:08 -0400 | [diff] [blame] | 207 | |
| 208 | static 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 | } |
| 218 | module_init(ehci_platform_init); |
| 219 | |
| 220 | static void __exit ehci_platform_cleanup(void) |
| 221 | { |
| 222 | platform_driver_unregister(&ehci_platform_driver); |
| 223 | } |
| 224 | module_exit(ehci_platform_cleanup); |
| 225 | |
| 226 | MODULE_DESCRIPTION(DRIVER_DESC); |
| 227 | MODULE_AUTHOR("Hauke Mehrtens"); |
| 228 | MODULE_AUTHOR("Alan Stern"); |
| 229 | MODULE_LICENSE("GPL"); |