Deepak Sikri | c8c38de | 2010-11-10 14:33:18 +0530 | [diff] [blame] | 1 | /* |
| 2 | * Driver for EHCI HCD on SPEAR SOC |
| 3 | * |
| 4 | * Copyright (C) 2010 ST Micro Electronics, |
| 5 | * Deepak Sikri <deepak.sikri@st.com> |
| 6 | * |
| 7 | * Based on various ehci-*.c drivers |
| 8 | * |
| 9 | * This file is subject to the terms and conditions of the GNU General Public |
| 10 | * License. See the file COPYING in the main directory of this archive for |
| 11 | * more details. |
| 12 | */ |
| 13 | |
Deepak Sikri | c8c38de | 2010-11-10 14:33:18 +0530 | [diff] [blame] | 14 | #include <linux/clk.h> |
Deepak Sikri | 8c1b369 | 2012-02-24 14:49:31 +0530 | [diff] [blame] | 15 | #include <linux/jiffies.h> |
Stefan Roese | 56fafb9 | 2012-04-16 09:08:08 +0530 | [diff] [blame] | 16 | #include <linux/of.h> |
Deepak Sikri | 8c1b369 | 2012-02-24 14:49:31 +0530 | [diff] [blame] | 17 | #include <linux/platform_device.h> |
| 18 | #include <linux/pm.h> |
Deepak Sikri | c8c38de | 2010-11-10 14:33:18 +0530 | [diff] [blame] | 19 | |
| 20 | struct spear_ehci { |
| 21 | struct ehci_hcd ehci; |
| 22 | struct clk *clk; |
| 23 | }; |
| 24 | |
| 25 | #define to_spear_ehci(hcd) (struct spear_ehci *)hcd_to_ehci(hcd) |
| 26 | |
| 27 | static void spear_start_ehci(struct spear_ehci *ehci) |
| 28 | { |
Viresh Kumar | 15c9d50 | 2012-04-17 17:08:50 +0530 | [diff] [blame] | 29 | clk_prepare_enable(ehci->clk); |
Deepak Sikri | c8c38de | 2010-11-10 14:33:18 +0530 | [diff] [blame] | 30 | } |
| 31 | |
| 32 | static void spear_stop_ehci(struct spear_ehci *ehci) |
| 33 | { |
Viresh Kumar | 15c9d50 | 2012-04-17 17:08:50 +0530 | [diff] [blame] | 34 | clk_disable_unprepare(ehci->clk); |
Deepak Sikri | c8c38de | 2010-11-10 14:33:18 +0530 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | static int ehci_spear_setup(struct usb_hcd *hcd) |
| 38 | { |
| 39 | struct ehci_hcd *ehci = hcd_to_ehci(hcd); |
Deepak Sikri | c8c38de | 2010-11-10 14:33:18 +0530 | [diff] [blame] | 40 | |
| 41 | /* registers start at offset 0x0 */ |
| 42 | ehci->caps = hcd->regs; |
Alan Stern | 1a49e2a | 2012-07-09 15:55:14 -0400 | [diff] [blame] | 43 | |
Alan Stern | c73cee7 | 2012-10-31 13:21:06 -0400 | [diff] [blame] | 44 | return ehci_setup(hcd); |
Deepak Sikri | c8c38de | 2010-11-10 14:33:18 +0530 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | static const struct hc_driver ehci_spear_hc_driver = { |
| 48 | .description = hcd_name, |
| 49 | .product_desc = "SPEAr EHCI", |
| 50 | .hcd_priv_size = sizeof(struct spear_ehci), |
| 51 | |
| 52 | /* generic hardware linkage */ |
| 53 | .irq = ehci_irq, |
| 54 | .flags = HCD_MEMORY | HCD_USB2, |
| 55 | |
| 56 | /* basic lifecycle operations */ |
| 57 | .reset = ehci_spear_setup, |
| 58 | .start = ehci_run, |
| 59 | .stop = ehci_stop, |
| 60 | .shutdown = ehci_shutdown, |
| 61 | |
| 62 | /* managing i/o requests and associated device resources */ |
| 63 | .urb_enqueue = ehci_urb_enqueue, |
| 64 | .urb_dequeue = ehci_urb_dequeue, |
| 65 | .endpoint_disable = ehci_endpoint_disable, |
| 66 | .endpoint_reset = ehci_endpoint_reset, |
| 67 | |
| 68 | /* scheduling support */ |
| 69 | .get_frame_number = ehci_get_frame, |
| 70 | |
| 71 | /* root hub support */ |
| 72 | .hub_status_data = ehci_hub_status_data, |
| 73 | .hub_control = ehci_hub_control, |
| 74 | .bus_suspend = ehci_bus_suspend, |
| 75 | .bus_resume = ehci_bus_resume, |
| 76 | .relinquish_port = ehci_relinquish_port, |
| 77 | .port_handed_over = ehci_port_handed_over, |
| 78 | .clear_tt_buffer_complete = ehci_clear_tt_buffer_complete, |
| 79 | }; |
| 80 | |
Jingoo Han | ab1f046 | 2013-03-26 15:48:08 +0900 | [diff] [blame^] | 81 | #ifdef CONFIG_PM_SLEEP |
Deepak Sikri | 8c1b369 | 2012-02-24 14:49:31 +0530 | [diff] [blame] | 82 | static int ehci_spear_drv_suspend(struct device *dev) |
| 83 | { |
| 84 | struct usb_hcd *hcd = dev_get_drvdata(dev); |
Alan Stern | c5cf921 | 2012-06-28 11:19:02 -0400 | [diff] [blame] | 85 | bool do_wakeup = device_may_wakeup(dev); |
Deepak Sikri | 8c1b369 | 2012-02-24 14:49:31 +0530 | [diff] [blame] | 86 | |
Alan Stern | c5cf921 | 2012-06-28 11:19:02 -0400 | [diff] [blame] | 87 | return ehci_suspend(hcd, do_wakeup); |
Deepak Sikri | 8c1b369 | 2012-02-24 14:49:31 +0530 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | static int ehci_spear_drv_resume(struct device *dev) |
| 91 | { |
| 92 | struct usb_hcd *hcd = dev_get_drvdata(dev); |
Deepak Sikri | 8c1b369 | 2012-02-24 14:49:31 +0530 | [diff] [blame] | 93 | |
Alan Stern | c5cf921 | 2012-06-28 11:19:02 -0400 | [diff] [blame] | 94 | ehci_resume(hcd, false); |
Deepak Sikri | 8c1b369 | 2012-02-24 14:49:31 +0530 | [diff] [blame] | 95 | return 0; |
| 96 | } |
Jingoo Han | ab1f046 | 2013-03-26 15:48:08 +0900 | [diff] [blame^] | 97 | #endif /* CONFIG_PM_SLEEP */ |
Deepak Sikri | 8c1b369 | 2012-02-24 14:49:31 +0530 | [diff] [blame] | 98 | |
| 99 | static SIMPLE_DEV_PM_OPS(ehci_spear_pm_ops, ehci_spear_drv_suspend, |
| 100 | ehci_spear_drv_resume); |
| 101 | |
Stefan Roese | 56fafb9 | 2012-04-16 09:08:08 +0530 | [diff] [blame] | 102 | static u64 spear_ehci_dma_mask = DMA_BIT_MASK(32); |
| 103 | |
Deepak Sikri | c8c38de | 2010-11-10 14:33:18 +0530 | [diff] [blame] | 104 | static int spear_ehci_hcd_drv_probe(struct platform_device *pdev) |
| 105 | { |
| 106 | struct usb_hcd *hcd ; |
| 107 | struct spear_ehci *ehci; |
| 108 | struct resource *res; |
| 109 | struct clk *usbh_clk; |
| 110 | const struct hc_driver *driver = &ehci_spear_hc_driver; |
Deepak Sikri | c8c38de | 2010-11-10 14:33:18 +0530 | [diff] [blame] | 111 | int irq, retval; |
Deepak Sikri | c8c38de | 2010-11-10 14:33:18 +0530 | [diff] [blame] | 112 | |
| 113 | if (usb_disabled()) |
| 114 | return -ENODEV; |
| 115 | |
| 116 | irq = platform_get_irq(pdev, 0); |
| 117 | if (irq < 0) { |
| 118 | retval = irq; |
Viresh Kumar | 98515e5 | 2012-11-08 20:37:59 +0530 | [diff] [blame] | 119 | goto fail; |
Deepak Sikri | c8c38de | 2010-11-10 14:33:18 +0530 | [diff] [blame] | 120 | } |
| 121 | |
Stefan Roese | 56fafb9 | 2012-04-16 09:08:08 +0530 | [diff] [blame] | 122 | /* |
| 123 | * Right now device-tree probed devices don't get dma_mask set. |
| 124 | * Since shared usb code relies on it, set it here for now. |
| 125 | * Once we have dma capability bindings this can go away. |
| 126 | */ |
| 127 | if (!pdev->dev.dma_mask) |
| 128 | pdev->dev.dma_mask = &spear_ehci_dma_mask; |
| 129 | |
Viresh Kumar | 98515e5 | 2012-11-08 20:37:59 +0530 | [diff] [blame] | 130 | usbh_clk = devm_clk_get(&pdev->dev, NULL); |
Deepak Sikri | c8c38de | 2010-11-10 14:33:18 +0530 | [diff] [blame] | 131 | if (IS_ERR(usbh_clk)) { |
| 132 | dev_err(&pdev->dev, "Error getting interface clock\n"); |
| 133 | retval = PTR_ERR(usbh_clk); |
Viresh Kumar | 98515e5 | 2012-11-08 20:37:59 +0530 | [diff] [blame] | 134 | goto fail; |
Deepak Sikri | c8c38de | 2010-11-10 14:33:18 +0530 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | hcd = usb_create_hcd(driver, &pdev->dev, dev_name(&pdev->dev)); |
| 138 | if (!hcd) { |
| 139 | retval = -ENOMEM; |
Viresh Kumar | 98515e5 | 2012-11-08 20:37:59 +0530 | [diff] [blame] | 140 | goto fail; |
Deepak Sikri | c8c38de | 2010-11-10 14:33:18 +0530 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 144 | if (!res) { |
| 145 | retval = -ENODEV; |
Viresh Kumar | 98515e5 | 2012-11-08 20:37:59 +0530 | [diff] [blame] | 146 | goto err_put_hcd; |
Deepak Sikri | c8c38de | 2010-11-10 14:33:18 +0530 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | hcd->rsrc_start = res->start; |
| 150 | hcd->rsrc_len = resource_size(res); |
Viresh Kumar | 98515e5 | 2012-11-08 20:37:59 +0530 | [diff] [blame] | 151 | if (!devm_request_mem_region(&pdev->dev, hcd->rsrc_start, hcd->rsrc_len, |
Deepak Sikri | c8c38de | 2010-11-10 14:33:18 +0530 | [diff] [blame] | 152 | driver->description)) { |
| 153 | retval = -EBUSY; |
Viresh Kumar | 98515e5 | 2012-11-08 20:37:59 +0530 | [diff] [blame] | 154 | goto err_put_hcd; |
Deepak Sikri | c8c38de | 2010-11-10 14:33:18 +0530 | [diff] [blame] | 155 | } |
| 156 | |
Viresh Kumar | 98515e5 | 2012-11-08 20:37:59 +0530 | [diff] [blame] | 157 | hcd->regs = devm_ioremap(&pdev->dev, hcd->rsrc_start, hcd->rsrc_len); |
Deepak Sikri | c8c38de | 2010-11-10 14:33:18 +0530 | [diff] [blame] | 158 | if (hcd->regs == NULL) { |
| 159 | dev_dbg(&pdev->dev, "error mapping memory\n"); |
| 160 | retval = -ENOMEM; |
Viresh Kumar | 98515e5 | 2012-11-08 20:37:59 +0530 | [diff] [blame] | 161 | goto err_put_hcd; |
Deepak Sikri | c8c38de | 2010-11-10 14:33:18 +0530 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | ehci = (struct spear_ehci *)hcd_to_ehci(hcd); |
| 165 | ehci->clk = usbh_clk; |
| 166 | |
| 167 | spear_start_ehci(ehci); |
Yong Zhang | b5dd18d | 2011-09-07 16:10:52 +0800 | [diff] [blame] | 168 | retval = usb_add_hcd(hcd, irq, IRQF_SHARED); |
Deepak Sikri | c8c38de | 2010-11-10 14:33:18 +0530 | [diff] [blame] | 169 | if (retval) |
Viresh Kumar | 98515e5 | 2012-11-08 20:37:59 +0530 | [diff] [blame] | 170 | goto err_stop_ehci; |
Deepak Sikri | c8c38de | 2010-11-10 14:33:18 +0530 | [diff] [blame] | 171 | |
| 172 | return retval; |
| 173 | |
Viresh Kumar | 98515e5 | 2012-11-08 20:37:59 +0530 | [diff] [blame] | 174 | err_stop_ehci: |
Deepak Sikri | c8c38de | 2010-11-10 14:33:18 +0530 | [diff] [blame] | 175 | spear_stop_ehci(ehci); |
Viresh Kumar | 98515e5 | 2012-11-08 20:37:59 +0530 | [diff] [blame] | 176 | err_put_hcd: |
Deepak Sikri | c8c38de | 2010-11-10 14:33:18 +0530 | [diff] [blame] | 177 | usb_put_hcd(hcd); |
Viresh Kumar | 98515e5 | 2012-11-08 20:37:59 +0530 | [diff] [blame] | 178 | fail: |
Deepak Sikri | c8c38de | 2010-11-10 14:33:18 +0530 | [diff] [blame] | 179 | dev_err(&pdev->dev, "init fail, %d\n", retval); |
| 180 | |
| 181 | return retval ; |
| 182 | } |
| 183 | |
| 184 | static int spear_ehci_hcd_drv_remove(struct platform_device *pdev) |
| 185 | { |
| 186 | struct usb_hcd *hcd = platform_get_drvdata(pdev); |
| 187 | struct spear_ehci *ehci_p = to_spear_ehci(hcd); |
| 188 | |
| 189 | if (!hcd) |
| 190 | return 0; |
| 191 | if (in_interrupt()) |
| 192 | BUG(); |
| 193 | usb_remove_hcd(hcd); |
| 194 | |
| 195 | if (ehci_p->clk) |
| 196 | spear_stop_ehci(ehci_p); |
Deepak Sikri | c8c38de | 2010-11-10 14:33:18 +0530 | [diff] [blame] | 197 | usb_put_hcd(hcd); |
| 198 | |
Deepak Sikri | c8c38de | 2010-11-10 14:33:18 +0530 | [diff] [blame] | 199 | return 0; |
| 200 | } |
| 201 | |
Bill Pemberton | d3608b6 | 2012-11-19 13:24:34 -0500 | [diff] [blame] | 202 | static struct of_device_id spear_ehci_id_table[] = { |
Stefan Roese | 56fafb9 | 2012-04-16 09:08:08 +0530 | [diff] [blame] | 203 | { .compatible = "st,spear600-ehci", }, |
| 204 | { }, |
| 205 | }; |
| 206 | |
Deepak Sikri | c8c38de | 2010-11-10 14:33:18 +0530 | [diff] [blame] | 207 | static struct platform_driver spear_ehci_hcd_driver = { |
| 208 | .probe = spear_ehci_hcd_drv_probe, |
| 209 | .remove = spear_ehci_hcd_drv_remove, |
| 210 | .shutdown = usb_hcd_platform_shutdown, |
| 211 | .driver = { |
| 212 | .name = "spear-ehci", |
Deepak Sikri | 8c1b369 | 2012-02-24 14:49:31 +0530 | [diff] [blame] | 213 | .bus = &platform_bus_type, |
| 214 | .pm = &ehci_spear_pm_ops, |
Stefan Roese | 56fafb9 | 2012-04-16 09:08:08 +0530 | [diff] [blame] | 215 | .of_match_table = of_match_ptr(spear_ehci_id_table), |
Deepak Sikri | c8c38de | 2010-11-10 14:33:18 +0530 | [diff] [blame] | 216 | } |
| 217 | }; |
| 218 | |
| 219 | MODULE_ALIAS("platform:spear-ehci"); |