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/kernel.h> 22 #include <linux/hrtimer.h> 23 #include <linux/io.h> 24 #include <linux/module.h> 25 #include <linux/platform_device.h> 26 #include <linux/usb.h> 27 #include <linux/usb/hcd.h> 28 #include <linux/usb/ehci_pdriver.h> 29 30 #include "ehci.h" 31 32 #define DRIVER_DESC "EHCI generic platform driver" 33 34 static const char hcd_name[] = "ehci-platform"; 35 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 53 if (pdata->no_io_watchdog) 54 ehci->need_io_watchdog = 0; 55 return 0; 56 } 57 58 static struct hc_driver __read_mostly ehci_platform_hc_driver; 59 60 static const struct ehci_driver_overrides platform_overrides __initdata = { 61 .reset = ehci_platform_reset, 62 }; 63 64 static int ehci_platform_probe(struct platform_device *dev) 65 { 66 struct usb_hcd *hcd; 67 struct resource *res_mem; 68 struct usb_ehci_pdata *pdata = dev->dev.platform_data; 69 int irq; 70 int err = -ENOMEM; 71 72 if (!pdata) { 73 WARN_ON(1); 74 return -ENODEV; 75 } 76 77 if (usb_disabled()) 78 return -ENODEV; 79 80 irq = platform_get_irq(dev, 0); 81 if (irq < 0) { 82 dev_err(&dev->dev, "no irq provided"); 83 return irq; 84 } 85 res_mem = platform_get_resource(dev, IORESOURCE_MEM, 0); 86 if (!res_mem) { 87 dev_err(&dev->dev, "no memory resource provided"); 88 return -ENXIO; 89 } 90 91 if (pdata->power_on) { 92 err = pdata->power_on(dev); 93 if (err < 0) 94 return err; 95 } 96 97 hcd = usb_create_hcd(&ehci_platform_hc_driver, &dev->dev, 98 dev_name(&dev->dev)); 99 if (!hcd) { 100 err = -ENOMEM; 101 goto err_power; 102 } 103 104 hcd->rsrc_start = res_mem->start; 105 hcd->rsrc_len = resource_size(res_mem); 106 107 hcd->regs = devm_request_and_ioremap(&dev->dev, res_mem); 108 if (!hcd->regs) { 109 err = -ENOMEM; 110 goto err_put_hcd; 111 } 112 err = usb_add_hcd(hcd, irq, IRQF_SHARED); 113 if (err) 114 goto err_put_hcd; 115 116 platform_set_drvdata(dev, hcd); 117 118 return err; 119 120 err_put_hcd: 121 usb_put_hcd(hcd); 122 err_power: 123 if (pdata->power_off) 124 pdata->power_off(dev); 125 126 return err; 127 } 128 129 static int ehci_platform_remove(struct platform_device *dev) 130 { 131 struct usb_hcd *hcd = platform_get_drvdata(dev); 132 struct usb_ehci_pdata *pdata = dev->dev.platform_data; 133 134 usb_remove_hcd(hcd); 135 usb_put_hcd(hcd); 136 platform_set_drvdata(dev, NULL); 137 138 if (pdata->power_off) 139 pdata->power_off(dev); 140 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); 149 struct usb_ehci_pdata *pdata = dev->platform_data; 150 struct platform_device *pdev = 151 container_of(dev, struct platform_device, dev); 152 bool do_wakeup = device_may_wakeup(dev); 153 int ret; 154 155 ret = ehci_suspend(hcd, do_wakeup); 156 157 if (pdata->power_suspend) 158 pdata->power_suspend(pdev); 159 160 return ret; 161 } 162 163 static int ehci_platform_resume(struct device *dev) 164 { 165 struct usb_hcd *hcd = dev_get_drvdata(dev); 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 } 175 176 ehci_resume(hcd, false); 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, 199 .remove = ehci_platform_remove, 200 .shutdown = usb_hcd_platform_shutdown, 201 .driver = { 202 .owner = THIS_MODULE, 203 .name = "ehci-platform", 204 .pm = &ehci_platform_pm_ops, 205 } 206 }; 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"); 230