Magnus Damm | f54aab6 | 2008-01-23 15:58:46 +0900 | [diff] [blame] | 1 | /* |
| 2 | * OHCI HCD (Host Controller Driver) for USB. |
| 3 | * |
| 4 | * (C) Copyright 1999 Roman Weissgaerber <weissg@vienna.at> |
| 5 | * (C) Copyright 2000-2005 David Brownell |
| 6 | * (C) Copyright 2002 Hewlett-Packard Company |
| 7 | * (C) Copyright 2008 Magnus Damm |
| 8 | * |
| 9 | * SM501 Bus Glue - based on ohci-omap.c |
| 10 | * |
| 11 | * This file is licenced under the GPL. |
| 12 | */ |
| 13 | |
| 14 | #include <linux/interrupt.h> |
| 15 | #include <linux/jiffies.h> |
| 16 | #include <linux/platform_device.h> |
| 17 | #include <linux/dma-mapping.h> |
| 18 | #include <linux/sm501.h> |
| 19 | #include <linux/sm501-regs.h> |
| 20 | |
| 21 | static int ohci_sm501_init(struct usb_hcd *hcd) |
| 22 | { |
| 23 | return ohci_init(hcd_to_ohci(hcd)); |
| 24 | } |
| 25 | |
| 26 | static int ohci_sm501_start(struct usb_hcd *hcd) |
| 27 | { |
| 28 | struct device *dev = hcd->self.controller; |
| 29 | int ret; |
| 30 | |
| 31 | ret = ohci_run(hcd_to_ohci(hcd)); |
| 32 | if (ret < 0) { |
| 33 | dev_err(dev, "can't start %s", hcd->self.bus_name); |
| 34 | ohci_stop(hcd); |
| 35 | } |
| 36 | |
| 37 | return ret; |
| 38 | } |
| 39 | |
| 40 | /*-------------------------------------------------------------------------*/ |
| 41 | |
| 42 | static const struct hc_driver ohci_sm501_hc_driver = { |
| 43 | .description = hcd_name, |
| 44 | .product_desc = "SM501 OHCI", |
| 45 | .hcd_priv_size = sizeof(struct ohci_hcd), |
| 46 | |
| 47 | /* |
| 48 | * generic hardware linkage |
| 49 | */ |
| 50 | .irq = ohci_irq, |
| 51 | .flags = HCD_USB11 | HCD_MEMORY | HCD_LOCAL_MEM, |
| 52 | |
| 53 | /* |
| 54 | * basic lifecycle operations |
| 55 | */ |
| 56 | .reset = ohci_sm501_init, |
| 57 | .start = ohci_sm501_start, |
| 58 | .stop = ohci_stop, |
| 59 | .shutdown = ohci_shutdown, |
| 60 | |
| 61 | /* |
| 62 | * managing i/o requests and associated device resources |
| 63 | */ |
| 64 | .urb_enqueue = ohci_urb_enqueue, |
| 65 | .urb_dequeue = ohci_urb_dequeue, |
| 66 | .endpoint_disable = ohci_endpoint_disable, |
| 67 | |
| 68 | /* |
| 69 | * scheduling support |
| 70 | */ |
| 71 | .get_frame_number = ohci_get_frame, |
| 72 | |
| 73 | /* |
| 74 | * root hub support |
| 75 | */ |
| 76 | .hub_status_data = ohci_hub_status_data, |
| 77 | .hub_control = ohci_hub_control, |
Magnus Damm | f54aab6 | 2008-01-23 15:58:46 +0900 | [diff] [blame] | 78 | #ifdef CONFIG_PM |
| 79 | .bus_suspend = ohci_bus_suspend, |
| 80 | .bus_resume = ohci_bus_resume, |
| 81 | #endif |
| 82 | .start_port_reset = ohci_start_port_reset, |
| 83 | }; |
| 84 | |
| 85 | /*-------------------------------------------------------------------------*/ |
| 86 | |
| 87 | static int ohci_hcd_sm501_drv_probe(struct platform_device *pdev) |
| 88 | { |
| 89 | const struct hc_driver *driver = &ohci_sm501_hc_driver; |
| 90 | struct device *dev = &pdev->dev; |
| 91 | struct resource *res, *mem; |
| 92 | int retval, irq; |
Harvey Harrison | af3d305 | 2008-04-30 15:03:41 -0700 | [diff] [blame] | 93 | struct usb_hcd *hcd = NULL; |
Magnus Damm | f54aab6 | 2008-01-23 15:58:46 +0900 | [diff] [blame] | 94 | |
| 95 | irq = retval = platform_get_irq(pdev, 0); |
| 96 | if (retval < 0) |
| 97 | goto err0; |
| 98 | |
| 99 | mem = platform_get_resource(pdev, IORESOURCE_MEM, 1); |
| 100 | if (mem == NULL) { |
| 101 | dev_err(dev, "no resource definition for memory\n"); |
| 102 | retval = -ENOENT; |
| 103 | goto err0; |
| 104 | } |
| 105 | |
Joe Perches | 28f65c11 | 2011-06-09 09:13:32 -0700 | [diff] [blame] | 106 | if (!request_mem_region(mem->start, resource_size(mem), pdev->name)) { |
Magnus Damm | f54aab6 | 2008-01-23 15:58:46 +0900 | [diff] [blame] | 107 | dev_err(dev, "request_mem_region failed\n"); |
| 108 | retval = -EBUSY; |
| 109 | goto err0; |
| 110 | } |
| 111 | |
| 112 | /* The sm501 chip is equipped with local memory that may be used |
| 113 | * by on-chip devices such as the video controller and the usb host. |
| 114 | * This driver uses dma_declare_coherent_memory() to make sure |
| 115 | * usb allocations with dma_alloc_coherent() allocate from |
| 116 | * this local memory. The dma_handle returned by dma_alloc_coherent() |
| 117 | * will be an offset starting from 0 for the first local memory byte. |
| 118 | * |
| 119 | * So as long as data is allocated using dma_alloc_coherent() all is |
| 120 | * fine. This is however not always the case - buffers may be allocated |
| 121 | * using kmalloc() - so the usb core needs to be told that it must copy |
| 122 | * data into our local memory if the buffers happen to be placed in |
| 123 | * regular memory. The HCD_LOCAL_MEM flag does just that. |
| 124 | */ |
| 125 | |
| 126 | if (!dma_declare_coherent_memory(dev, mem->start, |
| 127 | mem->start - mem->parent->start, |
Joe Perches | 28f65c11 | 2011-06-09 09:13:32 -0700 | [diff] [blame] | 128 | resource_size(mem), |
Magnus Damm | f54aab6 | 2008-01-23 15:58:46 +0900 | [diff] [blame] | 129 | DMA_MEMORY_MAP | |
| 130 | DMA_MEMORY_EXCLUSIVE)) { |
| 131 | dev_err(dev, "cannot declare coherent memory\n"); |
| 132 | retval = -ENXIO; |
| 133 | goto err1; |
| 134 | } |
| 135 | |
| 136 | /* allocate, reserve and remap resources for registers */ |
| 137 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 138 | if (res == NULL) { |
| 139 | dev_err(dev, "no resource definition for registers\n"); |
| 140 | retval = -ENOENT; |
| 141 | goto err2; |
| 142 | } |
| 143 | |
Kay Sievers | 7071a3c | 2008-05-02 06:02:41 +0200 | [diff] [blame] | 144 | hcd = usb_create_hcd(driver, &pdev->dev, dev_name(&pdev->dev)); |
Magnus Damm | f54aab6 | 2008-01-23 15:58:46 +0900 | [diff] [blame] | 145 | if (!hcd) { |
| 146 | retval = -ENOMEM; |
| 147 | goto err2; |
| 148 | } |
| 149 | |
| 150 | hcd->rsrc_start = res->start; |
Joe Perches | 28f65c11 | 2011-06-09 09:13:32 -0700 | [diff] [blame] | 151 | hcd->rsrc_len = resource_size(res); |
Magnus Damm | f54aab6 | 2008-01-23 15:58:46 +0900 | [diff] [blame] | 152 | |
| 153 | if (!request_mem_region(hcd->rsrc_start, hcd->rsrc_len, pdev->name)) { |
| 154 | dev_err(dev, "request_mem_region failed\n"); |
| 155 | retval = -EBUSY; |
| 156 | goto err3; |
| 157 | } |
| 158 | |
| 159 | hcd->regs = ioremap(hcd->rsrc_start, hcd->rsrc_len); |
| 160 | if (hcd->regs == NULL) { |
| 161 | dev_err(dev, "cannot remap registers\n"); |
| 162 | retval = -ENXIO; |
| 163 | goto err4; |
| 164 | } |
| 165 | |
| 166 | ohci_hcd_init(hcd_to_ohci(hcd)); |
| 167 | |
Yong Zhang | b5dd18d | 2011-09-07 16:10:52 +0800 | [diff] [blame] | 168 | retval = usb_add_hcd(hcd, irq, IRQF_SHARED); |
Magnus Damm | f54aab6 | 2008-01-23 15:58:46 +0900 | [diff] [blame] | 169 | if (retval) |
Dan Carpenter | 637ed74 | 2010-09-10 21:35:15 +0200 | [diff] [blame] | 170 | goto err5; |
Peter Chen | 3c9740a | 2013-11-05 10:46:02 +0800 | [diff] [blame] | 171 | device_wakeup_enable(hcd->self.controller); |
Magnus Damm | f54aab6 | 2008-01-23 15:58:46 +0900 | [diff] [blame] | 172 | |
| 173 | /* enable power and unmask interrupts */ |
| 174 | |
| 175 | sm501_unit_power(dev->parent, SM501_GATE_USB_HOST, 1); |
| 176 | sm501_modify_reg(dev->parent, SM501_IRQ_MASK, 1 << 6, 0); |
| 177 | |
| 178 | return 0; |
Dan Carpenter | 637ed74 | 2010-09-10 21:35:15 +0200 | [diff] [blame] | 179 | err5: |
| 180 | iounmap(hcd->regs); |
Magnus Damm | f54aab6 | 2008-01-23 15:58:46 +0900 | [diff] [blame] | 181 | err4: |
| 182 | release_mem_region(hcd->rsrc_start, hcd->rsrc_len); |
| 183 | err3: |
| 184 | usb_put_hcd(hcd); |
| 185 | err2: |
| 186 | dma_release_declared_memory(dev); |
| 187 | err1: |
Joe Perches | 28f65c11 | 2011-06-09 09:13:32 -0700 | [diff] [blame] | 188 | release_mem_region(mem->start, resource_size(mem)); |
Magnus Damm | f54aab6 | 2008-01-23 15:58:46 +0900 | [diff] [blame] | 189 | err0: |
| 190 | return retval; |
| 191 | } |
| 192 | |
| 193 | static int ohci_hcd_sm501_drv_remove(struct platform_device *pdev) |
| 194 | { |
| 195 | struct usb_hcd *hcd = platform_get_drvdata(pdev); |
| 196 | struct resource *mem; |
| 197 | |
| 198 | usb_remove_hcd(hcd); |
| 199 | release_mem_region(hcd->rsrc_start, hcd->rsrc_len); |
| 200 | usb_put_hcd(hcd); |
| 201 | dma_release_declared_memory(&pdev->dev); |
| 202 | mem = platform_get_resource(pdev, IORESOURCE_MEM, 1); |
Jesper Juhl | 119fc8c | 2008-03-21 22:55:45 +0100 | [diff] [blame] | 203 | if (mem) |
Joe Perches | 28f65c11 | 2011-06-09 09:13:32 -0700 | [diff] [blame] | 204 | release_mem_region(mem->start, resource_size(mem)); |
Magnus Damm | f54aab6 | 2008-01-23 15:58:46 +0900 | [diff] [blame] | 205 | |
| 206 | /* mask interrupts and disable power */ |
| 207 | |
| 208 | sm501_modify_reg(pdev->dev.parent, SM501_IRQ_MASK, 0, 1 << 6); |
| 209 | sm501_unit_power(pdev->dev.parent, SM501_GATE_USB_HOST, 0); |
| 210 | |
Magnus Damm | f54aab6 | 2008-01-23 15:58:46 +0900 | [diff] [blame] | 211 | return 0; |
| 212 | } |
| 213 | |
| 214 | /*-------------------------------------------------------------------------*/ |
| 215 | |
| 216 | #ifdef CONFIG_PM |
| 217 | static int ohci_sm501_suspend(struct platform_device *pdev, pm_message_t msg) |
| 218 | { |
| 219 | struct device *dev = &pdev->dev; |
Manjunath Goudar | f3c6059 | 2013-10-04 09:58:16 +0530 | [diff] [blame] | 220 | struct usb_hcd *hcd = platform_get_drvdata(pdev); |
| 221 | struct ohci_hcd *ohci = hcd_to_ohci(hcd); |
| 222 | bool do_wakeup = device_may_wakeup(dev); |
| 223 | int ret; |
Magnus Damm | f54aab6 | 2008-01-23 15:58:46 +0900 | [diff] [blame] | 224 | |
| 225 | if (time_before(jiffies, ohci->next_statechange)) |
| 226 | msleep(5); |
| 227 | ohci->next_statechange = jiffies; |
| 228 | |
Manjunath Goudar | f3c6059 | 2013-10-04 09:58:16 +0530 | [diff] [blame] | 229 | ret = ohci_suspend(hcd, do_wakeup); |
| 230 | if (ret) |
| 231 | return ret; |
| 232 | |
Magnus Damm | f54aab6 | 2008-01-23 15:58:46 +0900 | [diff] [blame] | 233 | sm501_unit_power(dev->parent, SM501_GATE_USB_HOST, 0); |
Manjunath Goudar | f3c6059 | 2013-10-04 09:58:16 +0530 | [diff] [blame] | 234 | return ret; |
Magnus Damm | f54aab6 | 2008-01-23 15:58:46 +0900 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | static int ohci_sm501_resume(struct platform_device *pdev) |
| 238 | { |
| 239 | struct device *dev = &pdev->dev; |
Alan Stern | 43bbb7e | 2008-04-03 18:03:17 -0400 | [diff] [blame] | 240 | struct usb_hcd *hcd = platform_get_drvdata(pdev); |
| 241 | struct ohci_hcd *ohci = hcd_to_ohci(hcd); |
Magnus Damm | f54aab6 | 2008-01-23 15:58:46 +0900 | [diff] [blame] | 242 | |
| 243 | if (time_before(jiffies, ohci->next_statechange)) |
| 244 | msleep(5); |
| 245 | ohci->next_statechange = jiffies; |
| 246 | |
| 247 | sm501_unit_power(dev->parent, SM501_GATE_USB_HOST, 1); |
Florian Fainelli | 068b054 | 2012-10-23 12:14:37 +0200 | [diff] [blame] | 248 | ohci_resume(hcd, false); |
Magnus Damm | f54aab6 | 2008-01-23 15:58:46 +0900 | [diff] [blame] | 249 | return 0; |
| 250 | } |
Andrew Morton | 1409e8e0 | 2008-02-04 23:57:48 -0800 | [diff] [blame] | 251 | #else |
| 252 | #define ohci_sm501_suspend NULL |
| 253 | #define ohci_sm501_resume NULL |
Magnus Damm | f54aab6 | 2008-01-23 15:58:46 +0900 | [diff] [blame] | 254 | #endif |
| 255 | |
| 256 | /*-------------------------------------------------------------------------*/ |
| 257 | |
| 258 | /* |
| 259 | * Driver definition to register with the SM501 bus |
| 260 | */ |
| 261 | static struct platform_driver ohci_hcd_sm501_driver = { |
| 262 | .probe = ohci_hcd_sm501_drv_probe, |
| 263 | .remove = ohci_hcd_sm501_drv_remove, |
| 264 | .shutdown = usb_hcd_platform_shutdown, |
Magnus Damm | f54aab6 | 2008-01-23 15:58:46 +0900 | [diff] [blame] | 265 | .suspend = ohci_sm501_suspend, |
| 266 | .resume = ohci_sm501_resume, |
Magnus Damm | f54aab6 | 2008-01-23 15:58:46 +0900 | [diff] [blame] | 267 | .driver = { |
| 268 | .owner = THIS_MODULE, |
| 269 | .name = "sm501-usb", |
| 270 | }, |
| 271 | }; |
Kay Sievers | f4fce61 | 2008-04-10 21:29:22 -0700 | [diff] [blame] | 272 | MODULE_ALIAS("platform:sm501-usb"); |