Deepak Sikri | c8c38de | 2010-11-10 14:33:18 +0530 | [diff] [blame] | 1 | /* |
| 2 | * OHCI HCD (Host Controller Driver) for USB. |
| 3 | * |
| 4 | * Copyright (C) 2010 ST Microelectronics. |
| 5 | * Deepak Sikri<deepak.sikri@st.com> |
| 6 | * |
| 7 | * Based on various ohci-*.c drivers |
| 8 | * |
| 9 | * This file is licensed under the terms of the GNU General Public |
| 10 | * License version 2. This program is licensed "as is" without any |
| 11 | * warranty of any kind, whether express or implied. |
| 12 | */ |
| 13 | |
| 14 | #include <linux/signal.h> |
| 15 | #include <linux/platform_device.h> |
| 16 | #include <linux/clk.h> |
Stefan Roese | 56fafb9 | 2012-04-16 09:08:08 +0530 | [diff] [blame] | 17 | #include <linux/of.h> |
Deepak Sikri | c8c38de | 2010-11-10 14:33:18 +0530 | [diff] [blame] | 18 | |
| 19 | struct spear_ohci { |
| 20 | struct ohci_hcd ohci; |
| 21 | struct clk *clk; |
| 22 | }; |
| 23 | |
| 24 | #define to_spear_ohci(hcd) (struct spear_ohci *)hcd_to_ohci(hcd) |
| 25 | |
| 26 | static void spear_start_ohci(struct spear_ohci *ohci) |
| 27 | { |
Viresh Kumar | 15c9d50 | 2012-04-17 17:08:50 +0530 | [diff] [blame] | 28 | clk_prepare_enable(ohci->clk); |
Deepak Sikri | c8c38de | 2010-11-10 14:33:18 +0530 | [diff] [blame] | 29 | } |
| 30 | |
| 31 | static void spear_stop_ohci(struct spear_ohci *ohci) |
| 32 | { |
Viresh Kumar | 15c9d50 | 2012-04-17 17:08:50 +0530 | [diff] [blame] | 33 | clk_disable_unprepare(ohci->clk); |
Deepak Sikri | c8c38de | 2010-11-10 14:33:18 +0530 | [diff] [blame] | 34 | } |
| 35 | |
Bill Pemberton | 41ac7b3 | 2012-11-19 13:21:48 -0500 | [diff] [blame] | 36 | static int ohci_spear_start(struct usb_hcd *hcd) |
Deepak Sikri | c8c38de | 2010-11-10 14:33:18 +0530 | [diff] [blame] | 37 | { |
| 38 | struct ohci_hcd *ohci = hcd_to_ohci(hcd); |
| 39 | int ret; |
| 40 | |
| 41 | ret = ohci_init(ohci); |
| 42 | if (ret < 0) |
| 43 | return ret; |
| 44 | ohci->regs = hcd->regs; |
| 45 | |
| 46 | ret = ohci_run(ohci); |
| 47 | if (ret < 0) { |
| 48 | dev_err(hcd->self.controller, "can't start\n"); |
| 49 | ohci_stop(hcd); |
| 50 | return ret; |
| 51 | } |
| 52 | |
| 53 | create_debug_files(ohci); |
| 54 | |
| 55 | #ifdef DEBUG |
| 56 | ohci_dump(ohci, 1); |
| 57 | #endif |
| 58 | return 0; |
| 59 | } |
| 60 | |
| 61 | static const struct hc_driver ohci_spear_hc_driver = { |
| 62 | .description = hcd_name, |
| 63 | .product_desc = "SPEAr OHCI", |
| 64 | .hcd_priv_size = sizeof(struct spear_ohci), |
| 65 | |
| 66 | /* generic hardware linkage */ |
| 67 | .irq = ohci_irq, |
| 68 | .flags = HCD_USB11 | HCD_MEMORY, |
| 69 | |
| 70 | /* basic lifecycle operations */ |
| 71 | .start = ohci_spear_start, |
| 72 | .stop = ohci_stop, |
| 73 | .shutdown = ohci_shutdown, |
| 74 | #ifdef CONFIG_PM |
| 75 | .bus_suspend = ohci_bus_suspend, |
| 76 | .bus_resume = ohci_bus_resume, |
| 77 | #endif |
| 78 | |
| 79 | /* managing i/o requests and associated device resources */ |
| 80 | .urb_enqueue = ohci_urb_enqueue, |
| 81 | .urb_dequeue = ohci_urb_dequeue, |
| 82 | .endpoint_disable = ohci_endpoint_disable, |
| 83 | |
| 84 | /* scheduling support */ |
| 85 | .get_frame_number = ohci_get_frame, |
| 86 | |
| 87 | /* root hub support */ |
| 88 | .hub_status_data = ohci_hub_status_data, |
| 89 | .hub_control = ohci_hub_control, |
| 90 | |
| 91 | .start_port_reset = ohci_start_port_reset, |
| 92 | }; |
| 93 | |
Stefan Roese | 56fafb9 | 2012-04-16 09:08:08 +0530 | [diff] [blame] | 94 | static u64 spear_ohci_dma_mask = DMA_BIT_MASK(32); |
| 95 | |
Deepak Sikri | c8c38de | 2010-11-10 14:33:18 +0530 | [diff] [blame] | 96 | static int spear_ohci_hcd_drv_probe(struct platform_device *pdev) |
| 97 | { |
| 98 | const struct hc_driver *driver = &ohci_spear_hc_driver; |
| 99 | struct usb_hcd *hcd = NULL; |
| 100 | struct clk *usbh_clk; |
| 101 | struct spear_ohci *ohci_p; |
| 102 | struct resource *res; |
| 103 | int retval, irq; |
Deepak Sikri | c8c38de | 2010-11-10 14:33:18 +0530 | [diff] [blame] | 104 | |
| 105 | irq = platform_get_irq(pdev, 0); |
| 106 | if (irq < 0) { |
| 107 | retval = irq; |
Viresh Kumar | 98515e5 | 2012-11-08 20:37:59 +0530 | [diff] [blame] | 108 | goto fail; |
Deepak Sikri | c8c38de | 2010-11-10 14:33:18 +0530 | [diff] [blame] | 109 | } |
| 110 | |
Stefan Roese | 56fafb9 | 2012-04-16 09:08:08 +0530 | [diff] [blame] | 111 | /* |
| 112 | * Right now device-tree probed devices don't get dma_mask set. |
| 113 | * Since shared usb code relies on it, set it here for now. |
| 114 | * Once we have dma capability bindings this can go away. |
| 115 | */ |
| 116 | if (!pdev->dev.dma_mask) |
| 117 | pdev->dev.dma_mask = &spear_ohci_dma_mask; |
| 118 | |
Viresh Kumar | 98515e5 | 2012-11-08 20:37:59 +0530 | [diff] [blame] | 119 | usbh_clk = devm_clk_get(&pdev->dev, NULL); |
Deepak Sikri | c8c38de | 2010-11-10 14:33:18 +0530 | [diff] [blame] | 120 | if (IS_ERR(usbh_clk)) { |
| 121 | dev_err(&pdev->dev, "Error getting interface clock\n"); |
| 122 | retval = PTR_ERR(usbh_clk); |
Viresh Kumar | 98515e5 | 2012-11-08 20:37:59 +0530 | [diff] [blame] | 123 | goto fail; |
Deepak Sikri | c8c38de | 2010-11-10 14:33:18 +0530 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | hcd = usb_create_hcd(driver, &pdev->dev, dev_name(&pdev->dev)); |
| 127 | if (!hcd) { |
| 128 | retval = -ENOMEM; |
Viresh Kumar | 98515e5 | 2012-11-08 20:37:59 +0530 | [diff] [blame] | 129 | goto fail; |
Deepak Sikri | c8c38de | 2010-11-10 14:33:18 +0530 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 133 | if (!res) { |
| 134 | retval = -ENODEV; |
Viresh Kumar | 98515e5 | 2012-11-08 20:37:59 +0530 | [diff] [blame] | 135 | goto err_put_hcd; |
Deepak Sikri | c8c38de | 2010-11-10 14:33:18 +0530 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | hcd->rsrc_start = pdev->resource[0].start; |
| 139 | hcd->rsrc_len = resource_size(res); |
Viresh Kumar | 98515e5 | 2012-11-08 20:37:59 +0530 | [diff] [blame] | 140 | if (!devm_request_mem_region(&pdev->dev, hcd->rsrc_start, hcd->rsrc_len, |
| 141 | hcd_name)) { |
Deepak Sikri | c8c38de | 2010-11-10 14:33:18 +0530 | [diff] [blame] | 142 | dev_dbg(&pdev->dev, "request_mem_region failed\n"); |
| 143 | retval = -EBUSY; |
Viresh Kumar | 98515e5 | 2012-11-08 20:37:59 +0530 | [diff] [blame] | 144 | goto err_put_hcd; |
Deepak Sikri | c8c38de | 2010-11-10 14:33:18 +0530 | [diff] [blame] | 145 | } |
| 146 | |
Viresh Kumar | 98515e5 | 2012-11-08 20:37:59 +0530 | [diff] [blame] | 147 | hcd->regs = devm_ioremap(&pdev->dev, hcd->rsrc_start, hcd->rsrc_len); |
Deepak Sikri | c8c38de | 2010-11-10 14:33:18 +0530 | [diff] [blame] | 148 | if (!hcd->regs) { |
| 149 | dev_dbg(&pdev->dev, "ioremap failed\n"); |
| 150 | retval = -ENOMEM; |
Viresh Kumar | 98515e5 | 2012-11-08 20:37:59 +0530 | [diff] [blame] | 151 | goto err_put_hcd; |
Deepak Sikri | c8c38de | 2010-11-10 14:33:18 +0530 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | ohci_p = (struct spear_ohci *)hcd_to_ohci(hcd); |
| 155 | ohci_p->clk = usbh_clk; |
| 156 | spear_start_ohci(ohci_p); |
| 157 | ohci_hcd_init(hcd_to_ohci(hcd)); |
| 158 | |
Yong Zhang | b5dd18d | 2011-09-07 16:10:52 +0800 | [diff] [blame] | 159 | retval = usb_add_hcd(hcd, platform_get_irq(pdev, 0), 0); |
Deepak Sikri | c8c38de | 2010-11-10 14:33:18 +0530 | [diff] [blame] | 160 | if (retval == 0) |
| 161 | return retval; |
| 162 | |
| 163 | spear_stop_ohci(ohci_p); |
Viresh Kumar | 98515e5 | 2012-11-08 20:37:59 +0530 | [diff] [blame] | 164 | err_put_hcd: |
Deepak Sikri | c8c38de | 2010-11-10 14:33:18 +0530 | [diff] [blame] | 165 | usb_put_hcd(hcd); |
Viresh Kumar | 98515e5 | 2012-11-08 20:37:59 +0530 | [diff] [blame] | 166 | fail: |
Deepak Sikri | c8c38de | 2010-11-10 14:33:18 +0530 | [diff] [blame] | 167 | dev_err(&pdev->dev, "init fail, %d\n", retval); |
| 168 | |
| 169 | return retval; |
| 170 | } |
| 171 | |
| 172 | static int spear_ohci_hcd_drv_remove(struct platform_device *pdev) |
| 173 | { |
| 174 | struct usb_hcd *hcd = platform_get_drvdata(pdev); |
| 175 | struct spear_ohci *ohci_p = to_spear_ohci(hcd); |
| 176 | |
| 177 | usb_remove_hcd(hcd); |
| 178 | if (ohci_p->clk) |
| 179 | spear_stop_ohci(ohci_p); |
| 180 | |
Deepak Sikri | c8c38de | 2010-11-10 14:33:18 +0530 | [diff] [blame] | 181 | usb_put_hcd(hcd); |
Deepak Sikri | c8c38de | 2010-11-10 14:33:18 +0530 | [diff] [blame] | 182 | return 0; |
| 183 | } |
| 184 | |
| 185 | #if defined(CONFIG_PM) |
| 186 | static int spear_ohci_hcd_drv_suspend(struct platform_device *dev, |
| 187 | pm_message_t message) |
| 188 | { |
| 189 | struct usb_hcd *hcd = platform_get_drvdata(dev); |
| 190 | struct ohci_hcd *ohci = hcd_to_ohci(hcd); |
| 191 | struct spear_ohci *ohci_p = to_spear_ohci(hcd); |
| 192 | |
| 193 | if (time_before(jiffies, ohci->next_statechange)) |
| 194 | msleep(5); |
| 195 | ohci->next_statechange = jiffies; |
| 196 | |
| 197 | spear_stop_ohci(ohci_p); |
Deepak Sikri | c8c38de | 2010-11-10 14:33:18 +0530 | [diff] [blame] | 198 | return 0; |
| 199 | } |
| 200 | |
| 201 | static int spear_ohci_hcd_drv_resume(struct platform_device *dev) |
| 202 | { |
| 203 | struct usb_hcd *hcd = platform_get_drvdata(dev); |
| 204 | struct ohci_hcd *ohci = hcd_to_ohci(hcd); |
| 205 | struct spear_ohci *ohci_p = to_spear_ohci(hcd); |
| 206 | |
| 207 | if (time_before(jiffies, ohci->next_statechange)) |
| 208 | msleep(5); |
| 209 | ohci->next_statechange = jiffies; |
| 210 | |
| 211 | spear_start_ohci(ohci_p); |
Florian Fainelli | cfa49b4 | 2012-10-08 15:11:29 +0200 | [diff] [blame] | 212 | ohci_resume(hcd, false); |
Deepak Sikri | c8c38de | 2010-11-10 14:33:18 +0530 | [diff] [blame] | 213 | return 0; |
| 214 | } |
| 215 | #endif |
| 216 | |
Bill Pemberton | d3608b6 | 2012-11-19 13:24:34 -0500 | [diff] [blame] | 217 | static struct of_device_id spear_ohci_id_table[] = { |
Stefan Roese | 56fafb9 | 2012-04-16 09:08:08 +0530 | [diff] [blame] | 218 | { .compatible = "st,spear600-ohci", }, |
| 219 | { }, |
| 220 | }; |
| 221 | |
Deepak Sikri | c8c38de | 2010-11-10 14:33:18 +0530 | [diff] [blame] | 222 | /* Driver definition to register with the platform bus */ |
| 223 | static struct platform_driver spear_ohci_hcd_driver = { |
| 224 | .probe = spear_ohci_hcd_drv_probe, |
| 225 | .remove = spear_ohci_hcd_drv_remove, |
| 226 | #ifdef CONFIG_PM |
| 227 | .suspend = spear_ohci_hcd_drv_suspend, |
| 228 | .resume = spear_ohci_hcd_drv_resume, |
| 229 | #endif |
| 230 | .driver = { |
| 231 | .owner = THIS_MODULE, |
| 232 | .name = "spear-ohci", |
Sachin Kamat | c0d6f0b | 2013-05-21 17:17:20 +0530 | [diff] [blame^] | 233 | .of_match_table = spear_ohci_id_table, |
Deepak Sikri | c8c38de | 2010-11-10 14:33:18 +0530 | [diff] [blame] | 234 | }, |
| 235 | }; |
| 236 | |
| 237 | MODULE_ALIAS("platform:spear-ohci"); |