1 // SPDX-License-Identifier: GPL-1.0+ 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, 52 .flags = HCD_USB11 | HCD_MEMORY | HCD_LOCAL_MEM, 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, 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; 94 struct usb_hcd *hcd = NULL; 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 107 if (!request_mem_region(mem->start, resource_size(mem), pdev->name)) { 108 dev_err(dev, "request_mem_region failed\n"); 109 retval = -EBUSY; 110 goto err0; 111 } 112 113 /* The sm501 chip is equipped with local memory that may be used 114 * by on-chip devices such as the video controller and the usb host. 115 * This driver uses dma_declare_coherent_memory() to make sure 116 * usb allocations with dma_alloc_coherent() allocate from 117 * this local memory. The dma_handle returned by dma_alloc_coherent() 118 * will be an offset starting from 0 for the first local memory byte. 119 * 120 * So as long as data is allocated using dma_alloc_coherent() all is 121 * fine. This is however not always the case - buffers may be allocated 122 * using kmalloc() - so the usb core needs to be told that it must copy 123 * data into our local memory if the buffers happen to be placed in 124 * regular memory. The HCD_LOCAL_MEM flag does just that. 125 */ 126 127 retval = dma_declare_coherent_memory(dev, mem->start, 128 mem->start - mem->parent->start, 129 resource_size(mem)); 130 if (retval) { 131 dev_err(dev, "cannot declare coherent memory\n"); 132 goto err1; 133 } 134 135 /* allocate, reserve and remap resources for registers */ 136 res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 137 if (res == NULL) { 138 dev_err(dev, "no resource definition for registers\n"); 139 retval = -ENOENT; 140 goto err2; 141 } 142 143 hcd = usb_create_hcd(driver, &pdev->dev, dev_name(&pdev->dev)); 144 if (!hcd) { 145 retval = -ENOMEM; 146 goto err2; 147 } 148 149 hcd->rsrc_start = res->start; 150 hcd->rsrc_len = resource_size(res); 151 152 if (!request_mem_region(hcd->rsrc_start, hcd->rsrc_len, pdev->name)) { 153 dev_err(dev, "request_mem_region failed\n"); 154 retval = -EBUSY; 155 goto err3; 156 } 157 158 hcd->regs = ioremap(hcd->rsrc_start, hcd->rsrc_len); 159 if (hcd->regs == NULL) { 160 dev_err(dev, "cannot remap registers\n"); 161 retval = -ENXIO; 162 goto err4; 163 } 164 165 ohci_hcd_init(hcd_to_ohci(hcd)); 166 167 retval = usb_add_hcd(hcd, irq, IRQF_SHARED); 168 if (retval) 169 goto err5; 170 device_wakeup_enable(hcd->self.controller); 171 172 /* enable power and unmask interrupts */ 173 174 sm501_unit_power(dev->parent, SM501_GATE_USB_HOST, 1); 175 sm501_modify_reg(dev->parent, SM501_IRQ_MASK, 1 << 6, 0); 176 177 return 0; 178 err5: 179 iounmap(hcd->regs); 180 err4: 181 release_mem_region(hcd->rsrc_start, hcd->rsrc_len); 182 err3: 183 usb_put_hcd(hcd); 184 err2: 185 dma_release_declared_memory(dev); 186 err1: 187 release_mem_region(mem->start, resource_size(mem)); 188 err0: 189 return retval; 190 } 191 192 static int ohci_hcd_sm501_drv_remove(struct platform_device *pdev) 193 { 194 struct usb_hcd *hcd = platform_get_drvdata(pdev); 195 struct resource *mem; 196 197 usb_remove_hcd(hcd); 198 release_mem_region(hcd->rsrc_start, hcd->rsrc_len); 199 usb_put_hcd(hcd); 200 dma_release_declared_memory(&pdev->dev); 201 mem = platform_get_resource(pdev, IORESOURCE_MEM, 1); 202 if (mem) 203 release_mem_region(mem->start, resource_size(mem)); 204 205 /* mask interrupts and disable power */ 206 207 sm501_modify_reg(pdev->dev.parent, SM501_IRQ_MASK, 0, 1 << 6); 208 sm501_unit_power(pdev->dev.parent, SM501_GATE_USB_HOST, 0); 209 210 return 0; 211 } 212 213 /*-------------------------------------------------------------------------*/ 214 215 #ifdef CONFIG_PM 216 static int ohci_sm501_suspend(struct platform_device *pdev, pm_message_t msg) 217 { 218 struct device *dev = &pdev->dev; 219 struct usb_hcd *hcd = platform_get_drvdata(pdev); 220 struct ohci_hcd *ohci = hcd_to_ohci(hcd); 221 bool do_wakeup = device_may_wakeup(dev); 222 int ret; 223 224 if (time_before(jiffies, ohci->next_statechange)) 225 msleep(5); 226 ohci->next_statechange = jiffies; 227 228 ret = ohci_suspend(hcd, do_wakeup); 229 if (ret) 230 return ret; 231 232 sm501_unit_power(dev->parent, SM501_GATE_USB_HOST, 0); 233 return ret; 234 } 235 236 static int ohci_sm501_resume(struct platform_device *pdev) 237 { 238 struct device *dev = &pdev->dev; 239 struct usb_hcd *hcd = platform_get_drvdata(pdev); 240 struct ohci_hcd *ohci = hcd_to_ohci(hcd); 241 242 if (time_before(jiffies, ohci->next_statechange)) 243 msleep(5); 244 ohci->next_statechange = jiffies; 245 246 sm501_unit_power(dev->parent, SM501_GATE_USB_HOST, 1); 247 ohci_resume(hcd, false); 248 return 0; 249 } 250 #else 251 #define ohci_sm501_suspend NULL 252 #define ohci_sm501_resume NULL 253 #endif 254 255 /*-------------------------------------------------------------------------*/ 256 257 /* 258 * Driver definition to register with the SM501 bus 259 */ 260 static struct platform_driver ohci_hcd_sm501_driver = { 261 .probe = ohci_hcd_sm501_drv_probe, 262 .remove = ohci_hcd_sm501_drv_remove, 263 .shutdown = usb_hcd_platform_shutdown, 264 .suspend = ohci_sm501_suspend, 265 .resume = ohci_sm501_resume, 266 .driver = { 267 .name = "sm501-usb", 268 }, 269 }; 270 MODULE_ALIAS("platform:sm501-usb"); 271