Anoop | 22ced687 | 2011-02-24 19:26:28 +0530 | [diff] [blame] | 1 | /* |
| 2 | * PMC MSP EHCI (Host Controller Driver) for USB. |
| 3 | * |
| 4 | * (C) Copyright 2006-2010 PMC-Sierra Inc |
| 5 | * |
| 6 | * This file is subject to the terms and conditions of the GNU General Public |
| 7 | * License. See the file "COPYING" in the main directory of this archive |
| 8 | * for more details. |
| 9 | * |
| 10 | */ |
| 11 | |
| 12 | /* includes */ |
| 13 | #include <linux/platform_device.h> |
| 14 | #include <linux/gpio.h> |
| 15 | #include <linux/usb.h> |
| 16 | #include <msp_usb.h> |
| 17 | |
| 18 | /* stream disable*/ |
| 19 | #define USB_CTRL_MODE_STREAM_DISABLE 0x10 |
| 20 | |
| 21 | /* threshold */ |
| 22 | #define USB_CTRL_FIFO_THRESH 0x00300000 |
| 23 | |
| 24 | /* register offset for usb_mode */ |
| 25 | #define USB_EHCI_REG_USB_MODE 0x68 |
| 26 | |
| 27 | /* register offset for usb fifo */ |
| 28 | #define USB_EHCI_REG_USB_FIFO 0x24 |
| 29 | |
| 30 | /* register offset for usb status */ |
| 31 | #define USB_EHCI_REG_USB_STATUS 0x44 |
| 32 | |
| 33 | /* serial/parallel transceiver */ |
| 34 | #define USB_EHCI_REG_BIT_STAT_STS (1<<29) |
| 35 | |
| 36 | /* TWI USB0 host device pin */ |
| 37 | #define MSP_PIN_USB0_HOST_DEV 49 |
| 38 | |
| 39 | /* TWI USB1 host device pin */ |
| 40 | #define MSP_PIN_USB1_HOST_DEV 50 |
| 41 | |
| 42 | |
| 43 | static void usb_hcd_tdi_set_mode(struct ehci_hcd *ehci) |
| 44 | { |
| 45 | u8 *base; |
| 46 | u8 *statreg; |
| 47 | u8 *fiforeg; |
| 48 | u32 val; |
| 49 | struct ehci_regs *reg_base = ehci->regs; |
| 50 | |
| 51 | /* get register base */ |
| 52 | base = (u8 *)reg_base + USB_EHCI_REG_USB_MODE; |
| 53 | statreg = (u8 *)reg_base + USB_EHCI_REG_USB_STATUS; |
| 54 | fiforeg = (u8 *)reg_base + USB_EHCI_REG_USB_FIFO; |
| 55 | |
| 56 | /* Disable controller mode stream */ |
| 57 | val = ehci_readl(ehci, (u32 *)base); |
| 58 | ehci_writel(ehci, (val | USB_CTRL_MODE_STREAM_DISABLE), |
| 59 | (u32 *)base); |
| 60 | |
| 61 | /* clear STS to select parallel transceiver interface */ |
| 62 | val = ehci_readl(ehci, (u32 *)statreg); |
| 63 | val = val & ~USB_EHCI_REG_BIT_STAT_STS; |
| 64 | ehci_writel(ehci, val, (u32 *)statreg); |
| 65 | |
| 66 | /* write to set the proper fifo threshold */ |
| 67 | ehci_writel(ehci, USB_CTRL_FIFO_THRESH, (u32 *)fiforeg); |
| 68 | |
| 69 | /* set TWI GPIO USB_HOST_DEV pin high */ |
| 70 | gpio_direction_output(MSP_PIN_USB0_HOST_DEV, 1); |
| 71 | #ifdef CONFIG_MSP_HAS_DUAL_USB |
| 72 | gpio_direction_output(MSP_PIN_USB1_HOST_DEV, 1); |
| 73 | #endif |
| 74 | } |
| 75 | |
| 76 | /* called during probe() after chip reset completes */ |
| 77 | static int ehci_msp_setup(struct usb_hcd *hcd) |
| 78 | { |
| 79 | struct ehci_hcd *ehci = hcd_to_ehci(hcd); |
| 80 | int retval; |
Alan Stern | 1a49e2a | 2012-07-09 15:55:14 -0400 | [diff] [blame] | 81 | |
Anoop | 22ced687 | 2011-02-24 19:26:28 +0530 | [diff] [blame] | 82 | ehci->big_endian_mmio = 1; |
| 83 | ehci->big_endian_desc = 1; |
| 84 | |
| 85 | ehci->caps = hcd->regs; |
Anoop | 22ced687 | 2011-02-24 19:26:28 +0530 | [diff] [blame] | 86 | hcd->has_tt = 1; |
| 87 | |
Alan Stern | 1a49e2a | 2012-07-09 15:55:14 -0400 | [diff] [blame] | 88 | retval = ehci_setup(hcd); |
Anoop | 22ced687 | 2011-02-24 19:26:28 +0530 | [diff] [blame] | 89 | if (retval) |
| 90 | return retval; |
| 91 | |
| 92 | usb_hcd_tdi_set_mode(ehci); |
Anoop | 22ced687 | 2011-02-24 19:26:28 +0530 | [diff] [blame] | 93 | |
| 94 | return retval; |
| 95 | } |
| 96 | |
| 97 | |
| 98 | /* configure so an HC device and id are always provided |
| 99 | * always called with process context; sleeping is OK |
| 100 | */ |
| 101 | |
| 102 | static int usb_hcd_msp_map_regs(struct mspusb_device *dev) |
| 103 | { |
| 104 | struct resource *res; |
| 105 | struct platform_device *pdev = &dev->dev; |
| 106 | u32 res_len; |
| 107 | int retval; |
| 108 | |
| 109 | /* MAB register space */ |
| 110 | res = platform_get_resource(pdev, IORESOURCE_MEM, 1); |
| 111 | if (res == NULL) |
| 112 | return -ENOMEM; |
Joe Perches | 28f65c11 | 2011-06-09 09:13:32 -0700 | [diff] [blame] | 113 | res_len = resource_size(res); |
Anoop | 22ced687 | 2011-02-24 19:26:28 +0530 | [diff] [blame] | 114 | if (!request_mem_region(res->start, res_len, "mab regs")) |
| 115 | return -EBUSY; |
| 116 | |
| 117 | dev->mab_regs = ioremap_nocache(res->start, res_len); |
| 118 | if (dev->mab_regs == NULL) { |
| 119 | retval = -ENOMEM; |
| 120 | goto err1; |
| 121 | } |
| 122 | |
| 123 | /* MSP USB register space */ |
| 124 | res = platform_get_resource(pdev, IORESOURCE_MEM, 2); |
| 125 | if (res == NULL) { |
| 126 | retval = -ENOMEM; |
| 127 | goto err2; |
| 128 | } |
Joe Perches | 28f65c11 | 2011-06-09 09:13:32 -0700 | [diff] [blame] | 129 | res_len = resource_size(res); |
Anoop | 22ced687 | 2011-02-24 19:26:28 +0530 | [diff] [blame] | 130 | if (!request_mem_region(res->start, res_len, "usbid regs")) { |
| 131 | retval = -EBUSY; |
| 132 | goto err2; |
| 133 | } |
| 134 | dev->usbid_regs = ioremap_nocache(res->start, res_len); |
| 135 | if (dev->usbid_regs == NULL) { |
| 136 | retval = -ENOMEM; |
| 137 | goto err3; |
| 138 | } |
| 139 | |
| 140 | return 0; |
| 141 | err3: |
| 142 | res = platform_get_resource(pdev, IORESOURCE_MEM, 2); |
Joe Perches | 28f65c11 | 2011-06-09 09:13:32 -0700 | [diff] [blame] | 143 | res_len = resource_size(res); |
Anoop | 22ced687 | 2011-02-24 19:26:28 +0530 | [diff] [blame] | 144 | release_mem_region(res->start, res_len); |
| 145 | err2: |
| 146 | iounmap(dev->mab_regs); |
| 147 | err1: |
| 148 | res = platform_get_resource(pdev, IORESOURCE_MEM, 1); |
Joe Perches | 28f65c11 | 2011-06-09 09:13:32 -0700 | [diff] [blame] | 149 | res_len = resource_size(res); |
Anoop | 22ced687 | 2011-02-24 19:26:28 +0530 | [diff] [blame] | 150 | release_mem_region(res->start, res_len); |
| 151 | dev_err(&pdev->dev, "Failed to map non-EHCI regs.\n"); |
| 152 | return retval; |
| 153 | } |
| 154 | |
| 155 | /** |
| 156 | * usb_hcd_msp_probe - initialize PMC MSP-based HCDs |
| 157 | * Context: !in_interrupt() |
| 158 | * |
| 159 | * Allocates basic resources for this USB host controller, and |
| 160 | * then invokes the start() method for the HCD associated with it |
| 161 | * through the hotplug entry's driver_data. |
| 162 | * |
| 163 | */ |
| 164 | int usb_hcd_msp_probe(const struct hc_driver *driver, |
| 165 | struct platform_device *dev) |
| 166 | { |
| 167 | int retval; |
| 168 | struct usb_hcd *hcd; |
| 169 | struct resource *res; |
| 170 | struct ehci_hcd *ehci ; |
| 171 | |
| 172 | hcd = usb_create_hcd(driver, &dev->dev, "pmcmsp"); |
| 173 | if (!hcd) |
| 174 | return -ENOMEM; |
| 175 | |
| 176 | res = platform_get_resource(dev, IORESOURCE_MEM, 0); |
| 177 | if (res == NULL) { |
| 178 | pr_debug("No IOMEM resource info for %s.\n", dev->name); |
| 179 | retval = -ENOMEM; |
| 180 | goto err1; |
| 181 | } |
| 182 | hcd->rsrc_start = res->start; |
Joe Perches | 28f65c11 | 2011-06-09 09:13:32 -0700 | [diff] [blame] | 183 | hcd->rsrc_len = resource_size(res); |
Anoop | 22ced687 | 2011-02-24 19:26:28 +0530 | [diff] [blame] | 184 | if (!request_mem_region(hcd->rsrc_start, hcd->rsrc_len, dev->name)) { |
| 185 | retval = -EBUSY; |
| 186 | goto err1; |
| 187 | } |
| 188 | hcd->regs = ioremap_nocache(hcd->rsrc_start, hcd->rsrc_len); |
| 189 | if (!hcd->regs) { |
| 190 | pr_debug("ioremap failed"); |
| 191 | retval = -ENOMEM; |
| 192 | goto err2; |
| 193 | } |
| 194 | |
| 195 | res = platform_get_resource(dev, IORESOURCE_IRQ, 0); |
| 196 | if (res == NULL) { |
| 197 | dev_err(&dev->dev, "No IRQ resource info for %s.\n", dev->name); |
| 198 | retval = -ENOMEM; |
| 199 | goto err3; |
| 200 | } |
| 201 | |
| 202 | /* Map non-EHCI register spaces */ |
| 203 | retval = usb_hcd_msp_map_regs(to_mspusb_device(dev)); |
| 204 | if (retval != 0) |
| 205 | goto err3; |
| 206 | |
| 207 | ehci = hcd_to_ehci(hcd); |
| 208 | ehci->big_endian_mmio = 1; |
| 209 | ehci->big_endian_desc = 1; |
| 210 | |
| 211 | |
| 212 | retval = usb_add_hcd(hcd, res->start, IRQF_SHARED); |
| 213 | if (retval == 0) |
| 214 | return 0; |
| 215 | |
| 216 | usb_remove_hcd(hcd); |
| 217 | err3: |
| 218 | iounmap(hcd->regs); |
| 219 | err2: |
| 220 | release_mem_region(hcd->rsrc_start, hcd->rsrc_len); |
| 221 | err1: |
| 222 | usb_put_hcd(hcd); |
| 223 | |
| 224 | return retval; |
| 225 | } |
| 226 | |
| 227 | |
| 228 | |
| 229 | /** |
| 230 | * usb_hcd_msp_remove - shutdown processing for PMC MSP-based HCDs |
| 231 | * @dev: USB Host Controller being removed |
| 232 | * Context: !in_interrupt() |
| 233 | * |
| 234 | * Reverses the effect of usb_hcd_msp_probe(), first invoking |
| 235 | * the HCD's stop() method. It is always called from a thread |
| 236 | * context, normally "rmmod", "apmd", or something similar. |
| 237 | * |
| 238 | * may be called without controller electrically present |
| 239 | * may be called with controller, bus, and devices active |
| 240 | */ |
| 241 | void usb_hcd_msp_remove(struct usb_hcd *hcd, struct platform_device *dev) |
| 242 | { |
| 243 | usb_remove_hcd(hcd); |
| 244 | iounmap(hcd->regs); |
| 245 | release_mem_region(hcd->rsrc_start, hcd->rsrc_len); |
| 246 | usb_put_hcd(hcd); |
| 247 | } |
| 248 | |
| 249 | #ifdef CONFIG_MSP_HAS_DUAL_USB |
| 250 | /* |
| 251 | * Wrapper around the main ehci_irq. Since both USB host controllers are |
| 252 | * sharing the same IRQ, need to first determine whether we're the intended |
| 253 | * recipient of this interrupt. |
| 254 | */ |
| 255 | static irqreturn_t ehci_msp_irq(struct usb_hcd *hcd) |
| 256 | { |
| 257 | u32 int_src; |
| 258 | struct device *dev = hcd->self.controller; |
| 259 | struct platform_device *pdev; |
| 260 | struct mspusb_device *mdev; |
| 261 | struct ehci_hcd *ehci = hcd_to_ehci(hcd); |
| 262 | /* need to reverse-map a couple of containers to get our device */ |
| 263 | pdev = to_platform_device(dev); |
| 264 | mdev = to_mspusb_device(pdev); |
| 265 | |
| 266 | /* Check to see if this interrupt is for this host controller */ |
| 267 | int_src = ehci_readl(ehci, &mdev->mab_regs->int_stat); |
| 268 | if (int_src & (1 << pdev->id)) |
| 269 | return ehci_irq(hcd); |
| 270 | |
| 271 | /* Not for this device */ |
| 272 | return IRQ_NONE; |
| 273 | } |
| 274 | #endif /* DUAL_USB */ |
| 275 | |
| 276 | static const struct hc_driver ehci_msp_hc_driver = { |
| 277 | .description = hcd_name, |
| 278 | .product_desc = "PMC MSP EHCI", |
| 279 | .hcd_priv_size = sizeof(struct ehci_hcd), |
| 280 | |
| 281 | /* |
| 282 | * generic hardware linkage |
| 283 | */ |
| 284 | #ifdef CONFIG_MSP_HAS_DUAL_USB |
| 285 | .irq = ehci_msp_irq, |
| 286 | #else |
| 287 | .irq = ehci_irq, |
| 288 | #endif |
Greg Kroah-Hartman | 3b8d732 | 2013-09-12 13:58:42 -0700 | [diff] [blame] | 289 | .flags = HCD_MEMORY | HCD_USB2, |
Anoop | 22ced687 | 2011-02-24 19:26:28 +0530 | [diff] [blame] | 290 | |
| 291 | /* |
| 292 | * basic lifecycle operations |
| 293 | */ |
Emil Goode | a3b0f9b | 2013-06-16 13:46:38 +0200 | [diff] [blame] | 294 | .reset = ehci_msp_setup, |
Anoop | 22ced687 | 2011-02-24 19:26:28 +0530 | [diff] [blame] | 295 | .shutdown = ehci_shutdown, |
| 296 | .start = ehci_run, |
| 297 | .stop = ehci_stop, |
| 298 | |
| 299 | /* |
| 300 | * managing i/o requests and associated device resources |
| 301 | */ |
| 302 | .urb_enqueue = ehci_urb_enqueue, |
| 303 | .urb_dequeue = ehci_urb_dequeue, |
| 304 | .endpoint_disable = ehci_endpoint_disable, |
| 305 | .endpoint_reset = ehci_endpoint_reset, |
| 306 | |
| 307 | /* |
| 308 | * scheduling support |
| 309 | */ |
| 310 | .get_frame_number = ehci_get_frame, |
| 311 | |
| 312 | /* |
| 313 | * root hub support |
| 314 | */ |
| 315 | .hub_status_data = ehci_hub_status_data, |
| 316 | .hub_control = ehci_hub_control, |
| 317 | .bus_suspend = ehci_bus_suspend, |
| 318 | .bus_resume = ehci_bus_resume, |
| 319 | .relinquish_port = ehci_relinquish_port, |
| 320 | .port_handed_over = ehci_port_handed_over, |
| 321 | |
| 322 | .clear_tt_buffer_complete = ehci_clear_tt_buffer_complete, |
| 323 | }; |
| 324 | |
| 325 | static int ehci_hcd_msp_drv_probe(struct platform_device *pdev) |
| 326 | { |
| 327 | int ret; |
| 328 | |
| 329 | pr_debug("In ehci_hcd_msp_drv_probe"); |
| 330 | |
| 331 | if (usb_disabled()) |
| 332 | return -ENODEV; |
| 333 | |
| 334 | gpio_request(MSP_PIN_USB0_HOST_DEV, "USB0_HOST_DEV_GPIO"); |
| 335 | #ifdef CONFIG_MSP_HAS_DUAL_USB |
| 336 | gpio_request(MSP_PIN_USB1_HOST_DEV, "USB1_HOST_DEV_GPIO"); |
| 337 | #endif |
| 338 | |
| 339 | ret = usb_hcd_msp_probe(&ehci_msp_hc_driver, pdev); |
| 340 | |
| 341 | return ret; |
| 342 | } |
| 343 | |
| 344 | static int ehci_hcd_msp_drv_remove(struct platform_device *pdev) |
| 345 | { |
| 346 | struct usb_hcd *hcd = platform_get_drvdata(pdev); |
| 347 | |
| 348 | usb_hcd_msp_remove(hcd, pdev); |
| 349 | |
| 350 | /* free TWI GPIO USB_HOST_DEV pin */ |
| 351 | gpio_free(MSP_PIN_USB0_HOST_DEV); |
| 352 | #ifdef CONFIG_MSP_HAS_DUAL_USB |
| 353 | gpio_free(MSP_PIN_USB1_HOST_DEV); |
| 354 | #endif |
| 355 | |
| 356 | return 0; |
| 357 | } |
| 358 | |
| 359 | MODULE_ALIAS("pmcmsp-ehci"); |
| 360 | |
| 361 | static struct platform_driver ehci_hcd_msp_driver = { |
| 362 | .probe = ehci_hcd_msp_drv_probe, |
| 363 | .remove = ehci_hcd_msp_drv_remove, |
| 364 | .driver = { |
| 365 | .name = "pmcmsp-ehci", |
| 366 | .owner = THIS_MODULE, |
| 367 | }, |
| 368 | }; |