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