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); |
| 40 | int retval = 0; |
| 41 | |
| 42 | /* registers start at offset 0x0 */ |
| 43 | ehci->caps = hcd->regs; |
Jan Andersson | c430131 | 2011-05-03 20:11:57 +0200 | [diff] [blame] | 44 | ehci->regs = hcd->regs + HC_LENGTH(ehci, ehci_readl(ehci, |
Deepak Sikri | c8c38de | 2010-11-10 14:33:18 +0530 | [diff] [blame] | 45 | &ehci->caps->hc_capbase)); |
| 46 | /* cache this readonly data; minimize chip reads */ |
| 47 | ehci->hcs_params = ehci_readl(ehci, &ehci->caps->hcs_params); |
| 48 | retval = ehci_halt(ehci); |
| 49 | if (retval) |
| 50 | return retval; |
| 51 | |
| 52 | retval = ehci_init(hcd); |
| 53 | if (retval) |
| 54 | return retval; |
| 55 | |
| 56 | ehci_reset(ehci); |
| 57 | ehci_port_power(ehci, 0); |
| 58 | |
| 59 | return retval; |
| 60 | } |
| 61 | |
| 62 | static const struct hc_driver ehci_spear_hc_driver = { |
| 63 | .description = hcd_name, |
| 64 | .product_desc = "SPEAr EHCI", |
| 65 | .hcd_priv_size = sizeof(struct spear_ehci), |
| 66 | |
| 67 | /* generic hardware linkage */ |
| 68 | .irq = ehci_irq, |
| 69 | .flags = HCD_MEMORY | HCD_USB2, |
| 70 | |
| 71 | /* basic lifecycle operations */ |
| 72 | .reset = ehci_spear_setup, |
| 73 | .start = ehci_run, |
| 74 | .stop = ehci_stop, |
| 75 | .shutdown = ehci_shutdown, |
| 76 | |
| 77 | /* managing i/o requests and associated device resources */ |
| 78 | .urb_enqueue = ehci_urb_enqueue, |
| 79 | .urb_dequeue = ehci_urb_dequeue, |
| 80 | .endpoint_disable = ehci_endpoint_disable, |
| 81 | .endpoint_reset = ehci_endpoint_reset, |
| 82 | |
| 83 | /* scheduling support */ |
| 84 | .get_frame_number = ehci_get_frame, |
| 85 | |
| 86 | /* root hub support */ |
| 87 | .hub_status_data = ehci_hub_status_data, |
| 88 | .hub_control = ehci_hub_control, |
| 89 | .bus_suspend = ehci_bus_suspend, |
| 90 | .bus_resume = ehci_bus_resume, |
| 91 | .relinquish_port = ehci_relinquish_port, |
| 92 | .port_handed_over = ehci_port_handed_over, |
| 93 | .clear_tt_buffer_complete = ehci_clear_tt_buffer_complete, |
| 94 | }; |
| 95 | |
Deepak Sikri | 8c1b369 | 2012-02-24 14:49:31 +0530 | [diff] [blame] | 96 | #ifdef CONFIG_PM |
| 97 | static int ehci_spear_drv_suspend(struct device *dev) |
| 98 | { |
| 99 | struct usb_hcd *hcd = dev_get_drvdata(dev); |
Alan Stern | c5cf921 | 2012-06-28 11:19:02 -0400 | [diff] [blame^] | 100 | bool do_wakeup = device_may_wakeup(dev); |
Deepak Sikri | 8c1b369 | 2012-02-24 14:49:31 +0530 | [diff] [blame] | 101 | |
Alan Stern | c5cf921 | 2012-06-28 11:19:02 -0400 | [diff] [blame^] | 102 | return ehci_suspend(hcd, do_wakeup); |
Deepak Sikri | 8c1b369 | 2012-02-24 14:49:31 +0530 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | static int ehci_spear_drv_resume(struct device *dev) |
| 106 | { |
| 107 | struct usb_hcd *hcd = dev_get_drvdata(dev); |
Deepak Sikri | 8c1b369 | 2012-02-24 14:49:31 +0530 | [diff] [blame] | 108 | |
Alan Stern | c5cf921 | 2012-06-28 11:19:02 -0400 | [diff] [blame^] | 109 | ehci_resume(hcd, false); |
Deepak Sikri | 8c1b369 | 2012-02-24 14:49:31 +0530 | [diff] [blame] | 110 | return 0; |
| 111 | } |
| 112 | #endif /* CONFIG_PM */ |
| 113 | |
| 114 | static SIMPLE_DEV_PM_OPS(ehci_spear_pm_ops, ehci_spear_drv_suspend, |
| 115 | ehci_spear_drv_resume); |
| 116 | |
Stefan Roese | 56fafb9 | 2012-04-16 09:08:08 +0530 | [diff] [blame] | 117 | static u64 spear_ehci_dma_mask = DMA_BIT_MASK(32); |
| 118 | |
Deepak Sikri | c8c38de | 2010-11-10 14:33:18 +0530 | [diff] [blame] | 119 | static int spear_ehci_hcd_drv_probe(struct platform_device *pdev) |
| 120 | { |
| 121 | struct usb_hcd *hcd ; |
| 122 | struct spear_ehci *ehci; |
| 123 | struct resource *res; |
| 124 | struct clk *usbh_clk; |
| 125 | const struct hc_driver *driver = &ehci_spear_hc_driver; |
Deepak Sikri | c8c38de | 2010-11-10 14:33:18 +0530 | [diff] [blame] | 126 | int irq, retval; |
| 127 | char clk_name[20] = "usbh_clk"; |
Stefan Roese | 56fafb9 | 2012-04-16 09:08:08 +0530 | [diff] [blame] | 128 | static int instance = -1; |
Deepak Sikri | c8c38de | 2010-11-10 14:33:18 +0530 | [diff] [blame] | 129 | |
| 130 | if (usb_disabled()) |
| 131 | return -ENODEV; |
| 132 | |
| 133 | irq = platform_get_irq(pdev, 0); |
| 134 | if (irq < 0) { |
| 135 | retval = irq; |
| 136 | goto fail_irq_get; |
| 137 | } |
| 138 | |
Stefan Roese | 56fafb9 | 2012-04-16 09:08:08 +0530 | [diff] [blame] | 139 | /* |
| 140 | * Right now device-tree probed devices don't get dma_mask set. |
| 141 | * Since shared usb code relies on it, set it here for now. |
| 142 | * Once we have dma capability bindings this can go away. |
| 143 | */ |
| 144 | if (!pdev->dev.dma_mask) |
| 145 | pdev->dev.dma_mask = &spear_ehci_dma_mask; |
| 146 | |
| 147 | /* |
| 148 | * Increment the device instance, when probing via device-tree |
| 149 | */ |
| 150 | if (pdev->id < 0) |
| 151 | instance++; |
| 152 | else |
| 153 | instance = pdev->id; |
| 154 | sprintf(clk_name, "usbh.%01d_clk", instance); |
Deepak Sikri | c8c38de | 2010-11-10 14:33:18 +0530 | [diff] [blame] | 155 | |
| 156 | usbh_clk = clk_get(NULL, clk_name); |
| 157 | if (IS_ERR(usbh_clk)) { |
| 158 | dev_err(&pdev->dev, "Error getting interface clock\n"); |
| 159 | retval = PTR_ERR(usbh_clk); |
| 160 | goto fail_get_usbh_clk; |
| 161 | } |
| 162 | |
| 163 | hcd = usb_create_hcd(driver, &pdev->dev, dev_name(&pdev->dev)); |
| 164 | if (!hcd) { |
| 165 | retval = -ENOMEM; |
| 166 | goto fail_create_hcd; |
| 167 | } |
| 168 | |
| 169 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 170 | if (!res) { |
| 171 | retval = -ENODEV; |
| 172 | goto fail_request_resource; |
| 173 | } |
| 174 | |
| 175 | hcd->rsrc_start = res->start; |
| 176 | hcd->rsrc_len = resource_size(res); |
| 177 | if (!request_mem_region(hcd->rsrc_start, hcd->rsrc_len, |
| 178 | driver->description)) { |
| 179 | retval = -EBUSY; |
| 180 | goto fail_request_resource; |
| 181 | } |
| 182 | |
| 183 | hcd->regs = ioremap(hcd->rsrc_start, hcd->rsrc_len); |
| 184 | if (hcd->regs == NULL) { |
| 185 | dev_dbg(&pdev->dev, "error mapping memory\n"); |
| 186 | retval = -ENOMEM; |
| 187 | goto fail_ioremap; |
| 188 | } |
| 189 | |
| 190 | ehci = (struct spear_ehci *)hcd_to_ehci(hcd); |
| 191 | ehci->clk = usbh_clk; |
| 192 | |
| 193 | spear_start_ehci(ehci); |
Yong Zhang | b5dd18d | 2011-09-07 16:10:52 +0800 | [diff] [blame] | 194 | retval = usb_add_hcd(hcd, irq, IRQF_SHARED); |
Deepak Sikri | c8c38de | 2010-11-10 14:33:18 +0530 | [diff] [blame] | 195 | if (retval) |
| 196 | goto fail_add_hcd; |
| 197 | |
| 198 | return retval; |
| 199 | |
| 200 | fail_add_hcd: |
| 201 | spear_stop_ehci(ehci); |
| 202 | iounmap(hcd->regs); |
| 203 | fail_ioremap: |
| 204 | release_mem_region(hcd->rsrc_start, hcd->rsrc_len); |
| 205 | fail_request_resource: |
| 206 | usb_put_hcd(hcd); |
| 207 | fail_create_hcd: |
| 208 | clk_put(usbh_clk); |
| 209 | fail_get_usbh_clk: |
| 210 | fail_irq_get: |
| 211 | dev_err(&pdev->dev, "init fail, %d\n", retval); |
| 212 | |
| 213 | return retval ; |
| 214 | } |
| 215 | |
| 216 | static int spear_ehci_hcd_drv_remove(struct platform_device *pdev) |
| 217 | { |
| 218 | struct usb_hcd *hcd = platform_get_drvdata(pdev); |
| 219 | struct spear_ehci *ehci_p = to_spear_ehci(hcd); |
| 220 | |
| 221 | if (!hcd) |
| 222 | return 0; |
| 223 | if (in_interrupt()) |
| 224 | BUG(); |
| 225 | usb_remove_hcd(hcd); |
| 226 | |
| 227 | if (ehci_p->clk) |
| 228 | spear_stop_ehci(ehci_p); |
| 229 | iounmap(hcd->regs); |
| 230 | release_mem_region(hcd->rsrc_start, hcd->rsrc_len); |
| 231 | usb_put_hcd(hcd); |
| 232 | |
| 233 | if (ehci_p->clk) |
| 234 | clk_put(ehci_p->clk); |
| 235 | |
| 236 | return 0; |
| 237 | } |
| 238 | |
Stefan Roese | 56fafb9 | 2012-04-16 09:08:08 +0530 | [diff] [blame] | 239 | static struct of_device_id spear_ehci_id_table[] __devinitdata = { |
| 240 | { .compatible = "st,spear600-ehci", }, |
| 241 | { }, |
| 242 | }; |
| 243 | |
Deepak Sikri | c8c38de | 2010-11-10 14:33:18 +0530 | [diff] [blame] | 244 | static struct platform_driver spear_ehci_hcd_driver = { |
| 245 | .probe = spear_ehci_hcd_drv_probe, |
| 246 | .remove = spear_ehci_hcd_drv_remove, |
| 247 | .shutdown = usb_hcd_platform_shutdown, |
| 248 | .driver = { |
| 249 | .name = "spear-ehci", |
Deepak Sikri | 8c1b369 | 2012-02-24 14:49:31 +0530 | [diff] [blame] | 250 | .bus = &platform_bus_type, |
| 251 | .pm = &ehci_spear_pm_ops, |
Stefan Roese | 56fafb9 | 2012-04-16 09:08:08 +0530 | [diff] [blame] | 252 | .of_match_table = of_match_ptr(spear_ehci_id_table), |
Deepak Sikri | c8c38de | 2010-11-10 14:33:18 +0530 | [diff] [blame] | 253 | } |
| 254 | }; |
| 255 | |
| 256 | MODULE_ALIAS("platform:spear-ehci"); |