Sebastian Andrzej Siewior | 3429e91 | 2012-03-13 16:57:41 +0200 | [diff] [blame] | 1 | /* |
| 2 | * xhci-plat.c - xHCI host controller driver platform Bus Glue. |
| 3 | * |
| 4 | * Copyright (C) 2012 Texas Instruments Incorporated - http://www.ti.com |
| 5 | * Author: Sebastian Andrzej Siewior <bigeasy@linutronix.de> |
| 6 | * |
| 7 | * A lot of code borrowed from the Linux xHCI driver. |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or |
| 10 | * modify it under the terms of the GNU General Public License |
| 11 | * version 2 as published by the Free Software Foundation. |
| 12 | */ |
| 13 | |
Gregory CLEMENT | 4718c17 | 2014-05-15 12:17:32 +0200 | [diff] [blame] | 14 | #include <linux/clk.h> |
Xenia Ragiadakou | c10cf11 | 2013-08-14 05:55:19 +0300 | [diff] [blame] | 15 | #include <linux/dma-mapping.h> |
Gregory CLEMENT | 48157bb | 2014-05-15 12:17:31 +0200 | [diff] [blame] | 16 | #include <linux/module.h> |
| 17 | #include <linux/of.h> |
| 18 | #include <linux/platform_device.h> |
| 19 | #include <linux/slab.h> |
Pratyush Anand | 20f6fdd | 2014-07-04 17:01:25 +0300 | [diff] [blame^] | 20 | #include <linux/usb/xhci_pdriver.h> |
Sebastian Andrzej Siewior | 3429e91 | 2012-03-13 16:57:41 +0200 | [diff] [blame] | 21 | |
| 22 | #include "xhci.h" |
Gregory CLEMENT | 9737479 | 2014-05-15 12:17:33 +0200 | [diff] [blame] | 23 | #include "xhci-mvebu.h" |
Sebastian Andrzej Siewior | 3429e91 | 2012-03-13 16:57:41 +0200 | [diff] [blame] | 24 | |
| 25 | static void xhci_plat_quirks(struct device *dev, struct xhci_hcd *xhci) |
| 26 | { |
| 27 | /* |
| 28 | * As of now platform drivers don't provide MSI support so we ensure |
| 29 | * here that the generic code does not try to make a pci_dev from our |
| 30 | * dev struct in order to setup MSI |
| 31 | */ |
Sarah Sharp | 52fb612 | 2013-08-08 10:08:34 -0700 | [diff] [blame] | 32 | xhci->quirks |= XHCI_PLAT; |
Sebastian Andrzej Siewior | 3429e91 | 2012-03-13 16:57:41 +0200 | [diff] [blame] | 33 | } |
| 34 | |
| 35 | /* called during probe() after chip reset completes */ |
| 36 | static int xhci_plat_setup(struct usb_hcd *hcd) |
| 37 | { |
| 38 | return xhci_gen_setup(hcd, xhci_plat_quirks); |
| 39 | } |
| 40 | |
Yoshihiro Shimoda | 94adcdc | 2014-05-28 20:22:58 +0900 | [diff] [blame] | 41 | static int xhci_plat_start(struct usb_hcd *hcd) |
| 42 | { |
| 43 | return xhci_run(hcd); |
| 44 | } |
| 45 | |
Sebastian Andrzej Siewior | 3429e91 | 2012-03-13 16:57:41 +0200 | [diff] [blame] | 46 | static const struct hc_driver xhci_plat_xhci_driver = { |
| 47 | .description = "xhci-hcd", |
| 48 | .product_desc = "xHCI Host Controller", |
| 49 | .hcd_priv_size = sizeof(struct xhci_hcd *), |
| 50 | |
| 51 | /* |
| 52 | * generic hardware linkage |
| 53 | */ |
| 54 | .irq = xhci_irq, |
| 55 | .flags = HCD_MEMORY | HCD_USB3 | HCD_SHARED, |
| 56 | |
| 57 | /* |
| 58 | * basic lifecycle operations |
| 59 | */ |
| 60 | .reset = xhci_plat_setup, |
Yoshihiro Shimoda | 94adcdc | 2014-05-28 20:22:58 +0900 | [diff] [blame] | 61 | .start = xhci_plat_start, |
Sebastian Andrzej Siewior | 3429e91 | 2012-03-13 16:57:41 +0200 | [diff] [blame] | 62 | .stop = xhci_stop, |
| 63 | .shutdown = xhci_shutdown, |
| 64 | |
| 65 | /* |
| 66 | * managing i/o requests and associated device resources |
| 67 | */ |
| 68 | .urb_enqueue = xhci_urb_enqueue, |
| 69 | .urb_dequeue = xhci_urb_dequeue, |
| 70 | .alloc_dev = xhci_alloc_dev, |
| 71 | .free_dev = xhci_free_dev, |
| 72 | .alloc_streams = xhci_alloc_streams, |
| 73 | .free_streams = xhci_free_streams, |
| 74 | .add_endpoint = xhci_add_endpoint, |
| 75 | .drop_endpoint = xhci_drop_endpoint, |
| 76 | .endpoint_reset = xhci_endpoint_reset, |
| 77 | .check_bandwidth = xhci_check_bandwidth, |
| 78 | .reset_bandwidth = xhci_reset_bandwidth, |
| 79 | .address_device = xhci_address_device, |
Dan Williams | 48fc7db | 2013-12-05 17:07:27 -0800 | [diff] [blame] | 80 | .enable_device = xhci_enable_device, |
Sebastian Andrzej Siewior | 3429e91 | 2012-03-13 16:57:41 +0200 | [diff] [blame] | 81 | .update_hub_device = xhci_update_hub_device, |
| 82 | .reset_device = xhci_discover_or_reset_device, |
| 83 | |
| 84 | /* |
| 85 | * scheduling support |
| 86 | */ |
| 87 | .get_frame_number = xhci_get_frame, |
| 88 | |
| 89 | /* Root hub support */ |
| 90 | .hub_control = xhci_hub_control, |
| 91 | .hub_status_data = xhci_hub_status_data, |
| 92 | .bus_suspend = xhci_bus_suspend, |
| 93 | .bus_resume = xhci_bus_resume, |
Pratyush Anand | 94ef3d5 | 2014-07-04 17:01:24 +0300 | [diff] [blame] | 94 | |
| 95 | .enable_usb3_lpm_timeout = xhci_enable_usb3_lpm_timeout, |
| 96 | .disable_usb3_lpm_timeout = xhci_disable_usb3_lpm_timeout, |
Sebastian Andrzej Siewior | 3429e91 | 2012-03-13 16:57:41 +0200 | [diff] [blame] | 97 | }; |
| 98 | |
| 99 | static int xhci_plat_probe(struct platform_device *pdev) |
| 100 | { |
Pratyush Anand | 20f6fdd | 2014-07-04 17:01:25 +0300 | [diff] [blame^] | 101 | struct device_node *node = pdev->dev.of_node; |
| 102 | struct usb_xhci_pdata *pdata = dev_get_platdata(&pdev->dev); |
Sebastian Andrzej Siewior | 3429e91 | 2012-03-13 16:57:41 +0200 | [diff] [blame] | 103 | const struct hc_driver *driver; |
| 104 | struct xhci_hcd *xhci; |
| 105 | struct resource *res; |
| 106 | struct usb_hcd *hcd; |
Gregory CLEMENT | 4718c17 | 2014-05-15 12:17:32 +0200 | [diff] [blame] | 107 | struct clk *clk; |
Sebastian Andrzej Siewior | 3429e91 | 2012-03-13 16:57:41 +0200 | [diff] [blame] | 108 | int ret; |
| 109 | int irq; |
| 110 | |
| 111 | if (usb_disabled()) |
| 112 | return -ENODEV; |
| 113 | |
| 114 | driver = &xhci_plat_xhci_driver; |
| 115 | |
| 116 | irq = platform_get_irq(pdev, 0); |
| 117 | if (irq < 0) |
| 118 | return -ENODEV; |
| 119 | |
| 120 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 121 | if (!res) |
| 122 | return -ENODEV; |
| 123 | |
Gregory CLEMENT | 9737479 | 2014-05-15 12:17:33 +0200 | [diff] [blame] | 124 | if (of_device_is_compatible(pdev->dev.of_node, |
| 125 | "marvell,armada-375-xhci") || |
| 126 | of_device_is_compatible(pdev->dev.of_node, |
| 127 | "marvell,armada-380-xhci")) { |
| 128 | ret = xhci_mvebu_mbus_init_quirk(pdev); |
| 129 | if (ret) |
| 130 | return ret; |
| 131 | } |
| 132 | |
Xenia Ragiadakou | c10cf11 | 2013-08-14 05:55:19 +0300 | [diff] [blame] | 133 | /* Initialize dma_mask and coherent_dma_mask to 32-bits */ |
| 134 | ret = dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(32)); |
| 135 | if (ret) |
| 136 | return ret; |
| 137 | if (!pdev->dev.dma_mask) |
| 138 | pdev->dev.dma_mask = &pdev->dev.coherent_dma_mask; |
| 139 | else |
| 140 | dma_set_mask(&pdev->dev, DMA_BIT_MASK(32)); |
| 141 | |
Sebastian Andrzej Siewior | 3429e91 | 2012-03-13 16:57:41 +0200 | [diff] [blame] | 142 | hcd = usb_create_hcd(driver, &pdev->dev, dev_name(&pdev->dev)); |
| 143 | if (!hcd) |
| 144 | return -ENOMEM; |
| 145 | |
| 146 | hcd->rsrc_start = res->start; |
| 147 | hcd->rsrc_len = resource_size(res); |
| 148 | |
| 149 | if (!request_mem_region(hcd->rsrc_start, hcd->rsrc_len, |
| 150 | driver->description)) { |
| 151 | dev_dbg(&pdev->dev, "controller already in use\n"); |
| 152 | ret = -EBUSY; |
| 153 | goto put_hcd; |
| 154 | } |
| 155 | |
Ruchika Kharwar | 319acdf | 2012-08-10 09:58:30 +0300 | [diff] [blame] | 156 | hcd->regs = ioremap_nocache(hcd->rsrc_start, hcd->rsrc_len); |
Sebastian Andrzej Siewior | 3429e91 | 2012-03-13 16:57:41 +0200 | [diff] [blame] | 157 | if (!hcd->regs) { |
| 158 | dev_dbg(&pdev->dev, "error mapping memory\n"); |
| 159 | ret = -EFAULT; |
| 160 | goto release_mem_region; |
| 161 | } |
| 162 | |
Gregory CLEMENT | 4718c17 | 2014-05-15 12:17:32 +0200 | [diff] [blame] | 163 | /* |
| 164 | * Not all platforms have a clk so it is not an error if the |
| 165 | * clock does not exists. |
| 166 | */ |
| 167 | clk = devm_clk_get(&pdev->dev, NULL); |
| 168 | if (!IS_ERR(clk)) { |
| 169 | ret = clk_prepare_enable(clk); |
| 170 | if (ret) |
| 171 | goto unmap_registers; |
| 172 | } |
| 173 | |
Sebastian Andrzej Siewior | 3429e91 | 2012-03-13 16:57:41 +0200 | [diff] [blame] | 174 | ret = usb_add_hcd(hcd, irq, IRQF_SHARED); |
| 175 | if (ret) |
Gregory CLEMENT | 4718c17 | 2014-05-15 12:17:32 +0200 | [diff] [blame] | 176 | goto disable_clk; |
| 177 | |
Peter Chen | 3c9740a | 2013-11-05 10:46:02 +0800 | [diff] [blame] | 178 | device_wakeup_enable(hcd->self.controller); |
Sebastian Andrzej Siewior | 3429e91 | 2012-03-13 16:57:41 +0200 | [diff] [blame] | 179 | |
| 180 | /* USB 2.0 roothub is stored in the platform_device now. */ |
Jingoo Han | 477527b | 2013-05-23 19:18:39 +0900 | [diff] [blame] | 181 | hcd = platform_get_drvdata(pdev); |
Sebastian Andrzej Siewior | 3429e91 | 2012-03-13 16:57:41 +0200 | [diff] [blame] | 182 | xhci = hcd_to_xhci(hcd); |
Gregory CLEMENT | 4718c17 | 2014-05-15 12:17:32 +0200 | [diff] [blame] | 183 | xhci->clk = clk; |
Sebastian Andrzej Siewior | 3429e91 | 2012-03-13 16:57:41 +0200 | [diff] [blame] | 184 | xhci->shared_hcd = usb_create_shared_hcd(driver, &pdev->dev, |
| 185 | dev_name(&pdev->dev), hcd); |
| 186 | if (!xhci->shared_hcd) { |
| 187 | ret = -ENOMEM; |
| 188 | goto dealloc_usb2_hcd; |
| 189 | } |
| 190 | |
Pratyush Anand | 20f6fdd | 2014-07-04 17:01:25 +0300 | [diff] [blame^] | 191 | if ((node && of_property_read_bool(node, "usb3-lpm-capable")) || |
| 192 | (pdata && pdata->usb3_lpm_capable)) |
| 193 | xhci->quirks |= XHCI_LPM_SUPPORT; |
Sebastian Andrzej Siewior | 3429e91 | 2012-03-13 16:57:41 +0200 | [diff] [blame] | 194 | /* |
| 195 | * Set the xHCI pointer before xhci_plat_setup() (aka hcd_driver.reset) |
| 196 | * is called by usb_add_hcd(). |
| 197 | */ |
| 198 | *((struct xhci_hcd **) xhci->shared_hcd->hcd_priv) = xhci; |
| 199 | |
Oliver Neukum | 14aec58 | 2014-02-11 20:36:04 +0100 | [diff] [blame] | 200 | if (HCC_MAX_PSA(xhci->hcc_params) >= 4) |
| 201 | xhci->shared_hcd->can_do_streams = 1; |
| 202 | |
Sebastian Andrzej Siewior | 3429e91 | 2012-03-13 16:57:41 +0200 | [diff] [blame] | 203 | ret = usb_add_hcd(xhci->shared_hcd, irq, IRQF_SHARED); |
| 204 | if (ret) |
| 205 | goto put_usb3_hcd; |
| 206 | |
| 207 | return 0; |
| 208 | |
| 209 | put_usb3_hcd: |
| 210 | usb_put_hcd(xhci->shared_hcd); |
| 211 | |
| 212 | dealloc_usb2_hcd: |
| 213 | usb_remove_hcd(hcd); |
| 214 | |
Gregory CLEMENT | 4718c17 | 2014-05-15 12:17:32 +0200 | [diff] [blame] | 215 | disable_clk: |
| 216 | if (!IS_ERR(clk)) |
| 217 | clk_disable_unprepare(clk); |
| 218 | |
Sebastian Andrzej Siewior | 3429e91 | 2012-03-13 16:57:41 +0200 | [diff] [blame] | 219 | unmap_registers: |
| 220 | iounmap(hcd->regs); |
| 221 | |
| 222 | release_mem_region: |
| 223 | release_mem_region(hcd->rsrc_start, hcd->rsrc_len); |
| 224 | |
| 225 | put_hcd: |
| 226 | usb_put_hcd(hcd); |
| 227 | |
| 228 | return ret; |
| 229 | } |
| 230 | |
| 231 | static int xhci_plat_remove(struct platform_device *dev) |
| 232 | { |
| 233 | struct usb_hcd *hcd = platform_get_drvdata(dev); |
| 234 | struct xhci_hcd *xhci = hcd_to_xhci(hcd); |
Gregory CLEMENT | 4718c17 | 2014-05-15 12:17:32 +0200 | [diff] [blame] | 235 | struct clk *clk = xhci->clk; |
Sebastian Andrzej Siewior | 3429e91 | 2012-03-13 16:57:41 +0200 | [diff] [blame] | 236 | |
| 237 | usb_remove_hcd(xhci->shared_hcd); |
| 238 | usb_put_hcd(xhci->shared_hcd); |
| 239 | |
| 240 | usb_remove_hcd(hcd); |
Gregory CLEMENT | 4718c17 | 2014-05-15 12:17:32 +0200 | [diff] [blame] | 241 | if (!IS_ERR(clk)) |
| 242 | clk_disable_unprepare(clk); |
Sebastian Andrzej Siewior | 3429e91 | 2012-03-13 16:57:41 +0200 | [diff] [blame] | 243 | iounmap(hcd->regs); |
George Cherian | 5388a3a | 2013-06-21 13:59:08 +0530 | [diff] [blame] | 244 | release_mem_region(hcd->rsrc_start, hcd->rsrc_len); |
Sebastian Andrzej Siewior | 3429e91 | 2012-03-13 16:57:41 +0200 | [diff] [blame] | 245 | usb_put_hcd(hcd); |
| 246 | kfree(xhci); |
| 247 | |
| 248 | return 0; |
| 249 | } |
| 250 | |
Arnd Bergmann | 274f6af | 2014-05-08 15:52:19 +0200 | [diff] [blame] | 251 | #ifdef CONFIG_PM_SLEEP |
Vikas Sajjan | 57d04eb | 2013-02-11 12:58:00 +0200 | [diff] [blame] | 252 | static int xhci_plat_suspend(struct device *dev) |
| 253 | { |
| 254 | struct usb_hcd *hcd = dev_get_drvdata(dev); |
| 255 | struct xhci_hcd *xhci = hcd_to_xhci(hcd); |
| 256 | |
| 257 | return xhci_suspend(xhci); |
| 258 | } |
| 259 | |
| 260 | static int xhci_plat_resume(struct device *dev) |
| 261 | { |
| 262 | struct usb_hcd *hcd = dev_get_drvdata(dev); |
| 263 | struct xhci_hcd *xhci = hcd_to_xhci(hcd); |
| 264 | |
| 265 | return xhci_resume(xhci, 0); |
| 266 | } |
| 267 | |
| 268 | static const struct dev_pm_ops xhci_plat_pm_ops = { |
| 269 | SET_SYSTEM_SLEEP_PM_OPS(xhci_plat_suspend, xhci_plat_resume) |
| 270 | }; |
| 271 | #define DEV_PM_OPS (&xhci_plat_pm_ops) |
| 272 | #else |
| 273 | #define DEV_PM_OPS NULL |
| 274 | #endif /* CONFIG_PM */ |
| 275 | |
Al Cooper | 1fe6c45 | 2013-07-25 19:04:44 -0400 | [diff] [blame] | 276 | #ifdef CONFIG_OF |
| 277 | static const struct of_device_id usb_xhci_of_match[] = { |
Hans de Goede | 0f94388 | 2014-02-11 17:54:46 +0100 | [diff] [blame] | 278 | { .compatible = "generic-xhci" }, |
Al Cooper | 1fe6c45 | 2013-07-25 19:04:44 -0400 | [diff] [blame] | 279 | { .compatible = "xhci-platform" }, |
Gregory CLEMENT | 9737479 | 2014-05-15 12:17:33 +0200 | [diff] [blame] | 280 | { .compatible = "marvell,armada-375-xhci"}, |
| 281 | { .compatible = "marvell,armada-380-xhci"}, |
Al Cooper | 1fe6c45 | 2013-07-25 19:04:44 -0400 | [diff] [blame] | 282 | { }, |
| 283 | }; |
| 284 | MODULE_DEVICE_TABLE(of, usb_xhci_of_match); |
| 285 | #endif |
| 286 | |
Sebastian Andrzej Siewior | 3429e91 | 2012-03-13 16:57:41 +0200 | [diff] [blame] | 287 | static struct platform_driver usb_xhci_driver = { |
| 288 | .probe = xhci_plat_probe, |
| 289 | .remove = xhci_plat_remove, |
| 290 | .driver = { |
| 291 | .name = "xhci-hcd", |
Vikas Sajjan | 57d04eb | 2013-02-11 12:58:00 +0200 | [diff] [blame] | 292 | .pm = DEV_PM_OPS, |
Al Cooper | 1fe6c45 | 2013-07-25 19:04:44 -0400 | [diff] [blame] | 293 | .of_match_table = of_match_ptr(usb_xhci_of_match), |
Sebastian Andrzej Siewior | 3429e91 | 2012-03-13 16:57:41 +0200 | [diff] [blame] | 294 | }, |
| 295 | }; |
| 296 | MODULE_ALIAS("platform:xhci-hcd"); |
| 297 | |
| 298 | int xhci_register_plat(void) |
| 299 | { |
| 300 | return platform_driver_register(&usb_xhci_driver); |
| 301 | } |
| 302 | |
| 303 | void xhci_unregister_plat(void) |
| 304 | { |
| 305 | platform_driver_unregister(&usb_xhci_driver); |
| 306 | } |