Greg Kroah-Hartman | 5fd54ac | 2017-11-03 11:28:30 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Deepak Sikri | c8c38de | 2010-11-10 14:33:18 +0530 | [diff] [blame] | 2 | /* |
Manjunath Goudar | 7675d6b | 2013-04-02 18:24:00 +0200 | [diff] [blame] | 3 | * Driver for EHCI HCD on SPEAr SOC |
Deepak Sikri | c8c38de | 2010-11-10 14:33:18 +0530 | [diff] [blame] | 4 | * |
| 5 | * Copyright (C) 2010 ST Micro Electronics, |
| 6 | * Deepak Sikri <deepak.sikri@st.com> |
| 7 | * |
| 8 | * Based on various ehci-*.c drivers |
Deepak Sikri | c8c38de | 2010-11-10 14:33:18 +0530 | [diff] [blame] | 9 | */ |
| 10 | |
Deepak Sikri | c8c38de | 2010-11-10 14:33:18 +0530 | [diff] [blame] | 11 | #include <linux/clk.h> |
Manjunath Goudar | 7675d6b | 2013-04-02 18:24:00 +0200 | [diff] [blame] | 12 | #include <linux/dma-mapping.h> |
| 13 | #include <linux/io.h> |
Deepak Sikri | 8c1b369 | 2012-02-24 14:49:31 +0530 | [diff] [blame] | 14 | #include <linux/jiffies.h> |
Manjunath Goudar | 7675d6b | 2013-04-02 18:24:00 +0200 | [diff] [blame] | 15 | #include <linux/kernel.h> |
| 16 | #include <linux/module.h> |
Stefan Roese | 56fafb9 | 2012-04-16 09:08:08 +0530 | [diff] [blame] | 17 | #include <linux/of.h> |
Deepak Sikri | 8c1b369 | 2012-02-24 14:49:31 +0530 | [diff] [blame] | 18 | #include <linux/platform_device.h> |
| 19 | #include <linux/pm.h> |
Manjunath Goudar | 7675d6b | 2013-04-02 18:24:00 +0200 | [diff] [blame] | 20 | #include <linux/usb.h> |
| 21 | #include <linux/usb/hcd.h> |
| 22 | |
| 23 | #include "ehci.h" |
| 24 | |
| 25 | #define DRIVER_DESC "EHCI SPEAr driver" |
| 26 | |
| 27 | static const char hcd_name[] = "SPEAr-ehci"; |
Deepak Sikri | c8c38de | 2010-11-10 14:33:18 +0530 | [diff] [blame] | 28 | |
| 29 | struct spear_ehci { |
Deepak Sikri | c8c38de | 2010-11-10 14:33:18 +0530 | [diff] [blame] | 30 | struct clk *clk; |
| 31 | }; |
| 32 | |
Manjunath Goudar | 7675d6b | 2013-04-02 18:24:00 +0200 | [diff] [blame] | 33 | #define to_spear_ehci(hcd) (struct spear_ehci *)(hcd_to_ehci(hcd)->priv) |
Deepak Sikri | c8c38de | 2010-11-10 14:33:18 +0530 | [diff] [blame] | 34 | |
Manjunath Goudar | 7675d6b | 2013-04-02 18:24:00 +0200 | [diff] [blame] | 35 | static struct hc_driver __read_mostly ehci_spear_hc_driver; |
Deepak Sikri | c8c38de | 2010-11-10 14:33:18 +0530 | [diff] [blame] | 36 | |
Paul Cercueil | 1874b63 | 2020-09-03 13:25:36 +0200 | [diff] [blame] | 37 | static int __maybe_unused ehci_spear_drv_suspend(struct device *dev) |
Deepak Sikri | 8c1b369 | 2012-02-24 14:49:31 +0530 | [diff] [blame] | 38 | { |
| 39 | struct usb_hcd *hcd = dev_get_drvdata(dev); |
Alan Stern | c5cf921 | 2012-06-28 11:19:02 -0400 | [diff] [blame] | 40 | bool do_wakeup = device_may_wakeup(dev); |
Deepak Sikri | 8c1b369 | 2012-02-24 14:49:31 +0530 | [diff] [blame] | 41 | |
Alan Stern | c5cf921 | 2012-06-28 11:19:02 -0400 | [diff] [blame] | 42 | return ehci_suspend(hcd, do_wakeup); |
Deepak Sikri | 8c1b369 | 2012-02-24 14:49:31 +0530 | [diff] [blame] | 43 | } |
| 44 | |
Paul Cercueil | 1874b63 | 2020-09-03 13:25:36 +0200 | [diff] [blame] | 45 | static int __maybe_unused ehci_spear_drv_resume(struct device *dev) |
Deepak Sikri | 8c1b369 | 2012-02-24 14:49:31 +0530 | [diff] [blame] | 46 | { |
| 47 | struct usb_hcd *hcd = dev_get_drvdata(dev); |
Deepak Sikri | 8c1b369 | 2012-02-24 14:49:31 +0530 | [diff] [blame] | 48 | |
Alan Stern | c5cf921 | 2012-06-28 11:19:02 -0400 | [diff] [blame] | 49 | ehci_resume(hcd, false); |
Deepak Sikri | 8c1b369 | 2012-02-24 14:49:31 +0530 | [diff] [blame] | 50 | return 0; |
| 51 | } |
Deepak Sikri | 8c1b369 | 2012-02-24 14:49:31 +0530 | [diff] [blame] | 52 | |
| 53 | static SIMPLE_DEV_PM_OPS(ehci_spear_pm_ops, ehci_spear_drv_suspend, |
| 54 | ehci_spear_drv_resume); |
| 55 | |
Deepak Sikri | c8c38de | 2010-11-10 14:33:18 +0530 | [diff] [blame] | 56 | static int spear_ehci_hcd_drv_probe(struct platform_device *pdev) |
| 57 | { |
| 58 | struct usb_hcd *hcd ; |
Manjunath Goudar | 7675d6b | 2013-04-02 18:24:00 +0200 | [diff] [blame] | 59 | struct spear_ehci *sehci; |
Deepak Sikri | c8c38de | 2010-11-10 14:33:18 +0530 | [diff] [blame] | 60 | struct resource *res; |
| 61 | struct clk *usbh_clk; |
| 62 | const struct hc_driver *driver = &ehci_spear_hc_driver; |
Deepak Sikri | c8c38de | 2010-11-10 14:33:18 +0530 | [diff] [blame] | 63 | int irq, retval; |
Deepak Sikri | c8c38de | 2010-11-10 14:33:18 +0530 | [diff] [blame] | 64 | |
| 65 | if (usb_disabled()) |
| 66 | return -ENODEV; |
| 67 | |
| 68 | irq = platform_get_irq(pdev, 0); |
| 69 | if (irq < 0) { |
| 70 | retval = irq; |
Viresh Kumar | 98515e5 | 2012-11-08 20:37:59 +0530 | [diff] [blame] | 71 | goto fail; |
Deepak Sikri | c8c38de | 2010-11-10 14:33:18 +0530 | [diff] [blame] | 72 | } |
| 73 | |
Stefan Roese | 56fafb9 | 2012-04-16 09:08:08 +0530 | [diff] [blame] | 74 | /* |
| 75 | * Right now device-tree probed devices don't get dma_mask set. |
| 76 | * Since shared usb code relies on it, set it here for now. |
| 77 | * Once we have dma capability bindings this can go away. |
| 78 | */ |
Russell King | e1fd734 | 2013-06-27 12:36:37 +0100 | [diff] [blame] | 79 | retval = dma_coerce_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32)); |
Russell King | 22d9d8e | 2013-06-10 16:28:49 +0100 | [diff] [blame] | 80 | if (retval) |
| 81 | goto fail; |
Stefan Roese | 56fafb9 | 2012-04-16 09:08:08 +0530 | [diff] [blame] | 82 | |
Viresh Kumar | 98515e5 | 2012-11-08 20:37:59 +0530 | [diff] [blame] | 83 | usbh_clk = devm_clk_get(&pdev->dev, NULL); |
Deepak Sikri | c8c38de | 2010-11-10 14:33:18 +0530 | [diff] [blame] | 84 | if (IS_ERR(usbh_clk)) { |
| 85 | dev_err(&pdev->dev, "Error getting interface clock\n"); |
| 86 | retval = PTR_ERR(usbh_clk); |
Viresh Kumar | 98515e5 | 2012-11-08 20:37:59 +0530 | [diff] [blame] | 87 | goto fail; |
Deepak Sikri | c8c38de | 2010-11-10 14:33:18 +0530 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | hcd = usb_create_hcd(driver, &pdev->dev, dev_name(&pdev->dev)); |
| 91 | if (!hcd) { |
| 92 | retval = -ENOMEM; |
Viresh Kumar | 98515e5 | 2012-11-08 20:37:59 +0530 | [diff] [blame] | 93 | goto fail; |
Deepak Sikri | c8c38de | 2010-11-10 14:33:18 +0530 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
Vivek Gautam | e0f77a9 | 2014-05-10 17:30:08 +0530 | [diff] [blame] | 97 | hcd->regs = devm_ioremap_resource(&pdev->dev, res); |
| 98 | if (IS_ERR(hcd->regs)) { |
| 99 | retval = PTR_ERR(hcd->regs); |
Viresh Kumar | 98515e5 | 2012-11-08 20:37:59 +0530 | [diff] [blame] | 100 | goto err_put_hcd; |
Deepak Sikri | c8c38de | 2010-11-10 14:33:18 +0530 | [diff] [blame] | 101 | } |
Varka Bhadram | 3b59d31 | 2014-11-04 07:51:26 +0530 | [diff] [blame] | 102 | hcd->rsrc_start = res->start; |
| 103 | hcd->rsrc_len = resource_size(res); |
Deepak Sikri | c8c38de | 2010-11-10 14:33:18 +0530 | [diff] [blame] | 104 | |
Manjunath Goudar | 7675d6b | 2013-04-02 18:24:00 +0200 | [diff] [blame] | 105 | sehci = to_spear_ehci(hcd); |
| 106 | sehci->clk = usbh_clk; |
Deepak Sikri | c8c38de | 2010-11-10 14:33:18 +0530 | [diff] [blame] | 107 | |
Manjunath Goudar | 7675d6b | 2013-04-02 18:24:00 +0200 | [diff] [blame] | 108 | /* registers start at offset 0x0 */ |
| 109 | hcd_to_ehci(hcd)->caps = hcd->regs; |
| 110 | |
| 111 | clk_prepare_enable(sehci->clk); |
Yong Zhang | b5dd18d | 2011-09-07 16:10:52 +0800 | [diff] [blame] | 112 | retval = usb_add_hcd(hcd, irq, IRQF_SHARED); |
Deepak Sikri | c8c38de | 2010-11-10 14:33:18 +0530 | [diff] [blame] | 113 | if (retval) |
Viresh Kumar | 98515e5 | 2012-11-08 20:37:59 +0530 | [diff] [blame] | 114 | goto err_stop_ehci; |
Deepak Sikri | c8c38de | 2010-11-10 14:33:18 +0530 | [diff] [blame] | 115 | |
Peter Chen | 3c9740a | 2013-11-05 10:46:02 +0800 | [diff] [blame] | 116 | device_wakeup_enable(hcd->self.controller); |
Deepak Sikri | c8c38de | 2010-11-10 14:33:18 +0530 | [diff] [blame] | 117 | return retval; |
| 118 | |
Viresh Kumar | 98515e5 | 2012-11-08 20:37:59 +0530 | [diff] [blame] | 119 | err_stop_ehci: |
Manjunath Goudar | 7675d6b | 2013-04-02 18:24:00 +0200 | [diff] [blame] | 120 | clk_disable_unprepare(sehci->clk); |
Viresh Kumar | 98515e5 | 2012-11-08 20:37:59 +0530 | [diff] [blame] | 121 | err_put_hcd: |
Deepak Sikri | c8c38de | 2010-11-10 14:33:18 +0530 | [diff] [blame] | 122 | usb_put_hcd(hcd); |
Viresh Kumar | 98515e5 | 2012-11-08 20:37:59 +0530 | [diff] [blame] | 123 | fail: |
Deepak Sikri | c8c38de | 2010-11-10 14:33:18 +0530 | [diff] [blame] | 124 | dev_err(&pdev->dev, "init fail, %d\n", retval); |
| 125 | |
| 126 | return retval ; |
| 127 | } |
| 128 | |
| 129 | static int spear_ehci_hcd_drv_remove(struct platform_device *pdev) |
| 130 | { |
| 131 | struct usb_hcd *hcd = platform_get_drvdata(pdev); |
Manjunath Goudar | 7675d6b | 2013-04-02 18:24:00 +0200 | [diff] [blame] | 132 | struct spear_ehci *sehci = to_spear_ehci(hcd); |
Deepak Sikri | c8c38de | 2010-11-10 14:33:18 +0530 | [diff] [blame] | 133 | |
Deepak Sikri | c8c38de | 2010-11-10 14:33:18 +0530 | [diff] [blame] | 134 | usb_remove_hcd(hcd); |
| 135 | |
Manjunath Goudar | 7675d6b | 2013-04-02 18:24:00 +0200 | [diff] [blame] | 136 | if (sehci->clk) |
| 137 | clk_disable_unprepare(sehci->clk); |
Deepak Sikri | c8c38de | 2010-11-10 14:33:18 +0530 | [diff] [blame] | 138 | usb_put_hcd(hcd); |
| 139 | |
Deepak Sikri | c8c38de | 2010-11-10 14:33:18 +0530 | [diff] [blame] | 140 | return 0; |
| 141 | } |
| 142 | |
Jingoo Han | 3632eba | 2014-06-18 13:35:41 +0900 | [diff] [blame] | 143 | static const struct of_device_id spear_ehci_id_table[] = { |
Stefan Roese | 56fafb9 | 2012-04-16 09:08:08 +0530 | [diff] [blame] | 144 | { .compatible = "st,spear600-ehci", }, |
| 145 | { }, |
| 146 | }; |
Luis de Bethencourt | e76eaef | 2015-09-19 14:10:56 +0100 | [diff] [blame] | 147 | MODULE_DEVICE_TABLE(of, spear_ehci_id_table); |
Stefan Roese | 56fafb9 | 2012-04-16 09:08:08 +0530 | [diff] [blame] | 148 | |
Deepak Sikri | c8c38de | 2010-11-10 14:33:18 +0530 | [diff] [blame] | 149 | static struct platform_driver spear_ehci_hcd_driver = { |
| 150 | .probe = spear_ehci_hcd_drv_probe, |
| 151 | .remove = spear_ehci_hcd_drv_remove, |
| 152 | .shutdown = usb_hcd_platform_shutdown, |
| 153 | .driver = { |
| 154 | .name = "spear-ehci", |
Deepak Sikri | 8c1b369 | 2012-02-24 14:49:31 +0530 | [diff] [blame] | 155 | .bus = &platform_bus_type, |
Paul Cercueil | 1874b63 | 2020-09-03 13:25:36 +0200 | [diff] [blame] | 156 | .pm = pm_ptr(&ehci_spear_pm_ops), |
Sachin Kamat | 2c398f3 | 2013-05-21 17:17:17 +0530 | [diff] [blame] | 157 | .of_match_table = spear_ehci_id_table, |
Deepak Sikri | c8c38de | 2010-11-10 14:33:18 +0530 | [diff] [blame] | 158 | } |
| 159 | }; |
| 160 | |
Nicolas Pitre | edc8c54 | 2016-04-27 13:28:32 -0400 | [diff] [blame] | 161 | static const struct ehci_driver_overrides spear_overrides __initconst = { |
Manjunath Goudar | 7675d6b | 2013-04-02 18:24:00 +0200 | [diff] [blame] | 162 | .extra_priv_size = sizeof(struct spear_ehci), |
| 163 | }; |
| 164 | |
| 165 | static int __init ehci_spear_init(void) |
| 166 | { |
| 167 | if (usb_disabled()) |
| 168 | return -ENODEV; |
| 169 | |
| 170 | pr_info("%s: " DRIVER_DESC "\n", hcd_name); |
| 171 | |
| 172 | ehci_init_driver(&ehci_spear_hc_driver, &spear_overrides); |
| 173 | return platform_driver_register(&spear_ehci_hcd_driver); |
| 174 | } |
| 175 | module_init(ehci_spear_init); |
| 176 | |
| 177 | static void __exit ehci_spear_cleanup(void) |
| 178 | { |
| 179 | platform_driver_unregister(&spear_ehci_hcd_driver); |
| 180 | } |
| 181 | module_exit(ehci_spear_cleanup); |
| 182 | |
| 183 | MODULE_DESCRIPTION(DRIVER_DESC); |
Deepak Sikri | c8c38de | 2010-11-10 14:33:18 +0530 | [diff] [blame] | 184 | MODULE_ALIAS("platform:spear-ehci"); |
Manjunath Goudar | 7675d6b | 2013-04-02 18:24:00 +0200 | [diff] [blame] | 185 | MODULE_AUTHOR("Deepak Sikri"); |
| 186 | MODULE_LICENSE("GPL"); |