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 | */ |
| 21 | #include <linux/platform_device.h> |
| 22 | #include <linux/usb/ehci_pdriver.h> |
| 23 | |
| 24 | static 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 Fainelli | 4534874 | 2012-10-08 15:11:21 +0200 | [diff] [blame] | 41 | if (pdata->no_io_watchdog) |
| 42 | ehci->need_io_watchdog = 0; |
Hauke Mehrtens | 7a7a4a59 | 2012-03-13 01:04:48 +0100 | [diff] [blame] | 43 | 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 | |
| 51 | static 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 Mehrtens | 7a7a4a59 | 2012-03-13 01:04:48 +0100 | [diff] [blame] | 80 | .clear_tt_buffer_complete = ehci_clear_tt_buffer_complete, |
| 81 | }; |
| 82 | |
| 83 | static int __devinit ehci_platform_probe(struct platform_device *dev) |
| 84 | { |
| 85 | struct usb_hcd *hcd; |
| 86 | struct resource *res_mem; |
Kuninori Morimoto | 86e4cb3 | 2012-08-06 18:06:53 -0700 | [diff] [blame] | 87 | struct usb_ehci_pdata *pdata = dev->dev.platform_data; |
Hauke Mehrtens | 7a7a4a59 | 2012-03-13 01:04:48 +0100 | [diff] [blame] | 88 | int irq; |
| 89 | int err = -ENOMEM; |
| 90 | |
Kuninori Morimoto | 86e4cb3 | 2012-08-06 18:06:53 -0700 | [diff] [blame] | 91 | if (!pdata) { |
| 92 | WARN_ON(1); |
| 93 | return -ENODEV; |
| 94 | } |
Hauke Mehrtens | 7a7a4a59 | 2012-03-13 01:04:48 +0100 | [diff] [blame] | 95 | |
| 96 | if (usb_disabled()) |
| 97 | return -ENODEV; |
| 98 | |
| 99 | irq = platform_get_irq(dev, 0); |
| 100 | if (irq < 0) { |
Florian Fainelli | 2350cb0 | 2012-10-08 15:11:41 +0200 | [diff] [blame] | 101 | dev_err(&dev->dev, "no irq provided"); |
Hauke Mehrtens | 7a7a4a59 | 2012-03-13 01:04:48 +0100 | [diff] [blame] | 102 | return irq; |
| 103 | } |
| 104 | res_mem = platform_get_resource(dev, IORESOURCE_MEM, 0); |
| 105 | if (!res_mem) { |
Florian Fainelli | 5c9b2b2 | 2012-10-08 15:11:43 +0200 | [diff] [blame] | 106 | dev_err(&dev->dev, "no memory resource provided"); |
Hauke Mehrtens | 7a7a4a59 | 2012-03-13 01:04:48 +0100 | [diff] [blame] | 107 | return -ENXIO; |
| 108 | } |
| 109 | |
Kuninori Morimoto | 04216be | 2012-08-06 18:08:39 -0700 | [diff] [blame] | 110 | if (pdata->power_on) { |
| 111 | err = pdata->power_on(dev); |
| 112 | if (err < 0) |
| 113 | return err; |
| 114 | } |
| 115 | |
Hauke Mehrtens | 7a7a4a59 | 2012-03-13 01:04:48 +0100 | [diff] [blame] | 116 | hcd = usb_create_hcd(&ehci_platform_hc_driver, &dev->dev, |
| 117 | dev_name(&dev->dev)); |
Kuninori Morimoto | 04216be | 2012-08-06 18:08:39 -0700 | [diff] [blame] | 118 | if (!hcd) { |
| 119 | err = -ENOMEM; |
| 120 | goto err_power; |
| 121 | } |
Hauke Mehrtens | 7a7a4a59 | 2012-03-13 01:04:48 +0100 | [diff] [blame] | 122 | |
| 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)) { |
Florian Fainelli | 2350cb0 | 2012-10-08 15:11:41 +0200 | [diff] [blame] | 127 | dev_err(&dev->dev, "controller already in use"); |
Hauke Mehrtens | 7a7a4a59 | 2012-03-13 01:04:48 +0100 | [diff] [blame] | 128 | err = -EBUSY; |
| 129 | goto err_put_hcd; |
| 130 | } |
| 131 | |
| 132 | hcd->regs = ioremap_nocache(hcd->rsrc_start, hcd->rsrc_len); |
Julia Lawall | ec03ad8 | 2012-08-14 08:47:38 +0200 | [diff] [blame] | 133 | if (!hcd->regs) { |
| 134 | err = -ENOMEM; |
Hauke Mehrtens | 7a7a4a59 | 2012-03-13 01:04:48 +0100 | [diff] [blame] | 135 | goto err_release_region; |
Julia Lawall | ec03ad8 | 2012-08-14 08:47:38 +0200 | [diff] [blame] | 136 | } |
Hauke Mehrtens | 7a7a4a59 | 2012-03-13 01:04:48 +0100 | [diff] [blame] | 137 | 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 | |
| 145 | err_iounmap: |
| 146 | iounmap(hcd->regs); |
| 147 | err_release_region: |
| 148 | release_mem_region(hcd->rsrc_start, hcd->rsrc_len); |
| 149 | err_put_hcd: |
| 150 | usb_put_hcd(hcd); |
Kuninori Morimoto | 04216be | 2012-08-06 18:08:39 -0700 | [diff] [blame] | 151 | err_power: |
| 152 | if (pdata->power_off) |
| 153 | pdata->power_off(dev); |
| 154 | |
Hauke Mehrtens | 7a7a4a59 | 2012-03-13 01:04:48 +0100 | [diff] [blame] | 155 | return err; |
| 156 | } |
| 157 | |
| 158 | static int __devexit ehci_platform_remove(struct platform_device *dev) |
| 159 | { |
| 160 | struct usb_hcd *hcd = platform_get_drvdata(dev); |
Kuninori Morimoto | 04216be | 2012-08-06 18:08:39 -0700 | [diff] [blame] | 161 | struct usb_ehci_pdata *pdata = dev->dev.platform_data; |
Hauke Mehrtens | 7a7a4a59 | 2012-03-13 01:04:48 +0100 | [diff] [blame] | 162 | |
| 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 Morimoto | 04216be | 2012-08-06 18:08:39 -0700 | [diff] [blame] | 169 | if (pdata->power_off) |
| 170 | pdata->power_off(dev); |
| 171 | |
Hauke Mehrtens | 7a7a4a59 | 2012-03-13 01:04:48 +0100 | [diff] [blame] | 172 | return 0; |
| 173 | } |
| 174 | |
| 175 | #ifdef CONFIG_PM |
| 176 | |
| 177 | static int ehci_platform_suspend(struct device *dev) |
| 178 | { |
| 179 | struct usb_hcd *hcd = dev_get_drvdata(dev); |
Kuninori Morimoto | 04216be | 2012-08-06 18:08:39 -0700 | [diff] [blame] | 180 | struct usb_ehci_pdata *pdata = dev->platform_data; |
| 181 | struct platform_device *pdev = |
| 182 | container_of(dev, struct platform_device, dev); |
Alan Stern | c5cf921 | 2012-06-28 11:19:02 -0400 | [diff] [blame] | 183 | bool do_wakeup = device_may_wakeup(dev); |
Kuninori Morimoto | 04216be | 2012-08-06 18:08:39 -0700 | [diff] [blame] | 184 | int ret; |
Hauke Mehrtens | 7a7a4a59 | 2012-03-13 01:04:48 +0100 | [diff] [blame] | 185 | |
Kuninori Morimoto | 04216be | 2012-08-06 18:08:39 -0700 | [diff] [blame] | 186 | ret = ehci_suspend(hcd, do_wakeup); |
| 187 | |
| 188 | if (pdata->power_suspend) |
| 189 | pdata->power_suspend(pdev); |
| 190 | |
| 191 | return ret; |
Hauke Mehrtens | 7a7a4a59 | 2012-03-13 01:04:48 +0100 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | static int ehci_platform_resume(struct device *dev) |
| 195 | { |
| 196 | struct usb_hcd *hcd = dev_get_drvdata(dev); |
Kuninori Morimoto | 04216be | 2012-08-06 18:08:39 -0700 | [diff] [blame] | 197 | 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 Mehrtens | 7a7a4a59 | 2012-03-13 01:04:48 +0100 | [diff] [blame] | 206 | |
Alan Stern | c5cf921 | 2012-06-28 11:19:02 -0400 | [diff] [blame] | 207 | ehci_resume(hcd, false); |
Hauke Mehrtens | 7a7a4a59 | 2012-03-13 01:04:48 +0100 | [diff] [blame] | 208 | 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 | |
| 216 | static const struct platform_device_id ehci_platform_table[] = { |
| 217 | { "ehci-platform", 0 }, |
| 218 | { } |
| 219 | }; |
| 220 | MODULE_DEVICE_TABLE(platform, ehci_platform_table); |
| 221 | |
| 222 | static const struct dev_pm_ops ehci_platform_pm_ops = { |
| 223 | .suspend = ehci_platform_suspend, |
| 224 | .resume = ehci_platform_resume, |
| 225 | }; |
| 226 | |
| 227 | static 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 | }; |