1/* 2 * OHCI HCD (Host Controller Driver) for USB. 3 * 4 * Copyright (C) 2004 SAN People (Pty) Ltd. 5 * Copyright (C) 2005 Thibaut VARENE <varenet@parisc-linux.org> 6 * 7 * AT91 Bus Glue 8 * 9 * Based on fragments of 2.4 driver by Rick Bronson. 10 * Based on ohci-omap.c 11 * 12 * This file is licenced under the GPL. 13 */ 14 15#include <linux/clk.h> 16#include <linux/dma-mapping.h> 17#include <linux/of_platform.h> 18#include <linux/of_gpio.h> 19#include <linux/platform_device.h> 20#include <linux/platform_data/atmel.h> 21#include <linux/io.h> 22#include <linux/kernel.h> 23#include <linux/module.h> 24#include <linux/usb.h> 25#include <linux/usb/hcd.h> 26 27#include <mach/hardware.h> 28#include <asm/gpio.h> 29 30#include <mach/cpu.h> 31 32 33#include "ohci.h" 34 35#define valid_port(index) ((index) >= 0 && (index) < AT91_MAX_USBH_PORTS) 36#define at91_for_each_port(index) \ 37 for ((index) = 0; (index) < AT91_MAX_USBH_PORTS; (index)++) 38 39/* interface, function and usb clocks; sometimes also an AHB clock */ 40static struct clk *iclk, *fclk, *uclk, *hclk; 41/* interface and function clocks; sometimes also an AHB clock */ 42 43#define DRIVER_DESC "OHCI Atmel driver" 44 45static const char hcd_name[] = "ohci-atmel"; 46 47static struct hc_driver __read_mostly ohci_at91_hc_driver; 48static int clocked; 49 50extern int usb_disabled(void); 51 52/*-------------------------------------------------------------------------*/ 53 54static void at91_start_clock(void) 55{ 56 if (IS_ENABLED(CONFIG_COMMON_CLK)) { 57 clk_set_rate(uclk, 48000000); 58 clk_prepare_enable(uclk); 59 } 60 clk_prepare_enable(hclk); 61 clk_prepare_enable(iclk); 62 clk_prepare_enable(fclk); 63 clocked = 1; 64} 65 66static void at91_stop_clock(void) 67{ 68 clk_disable_unprepare(fclk); 69 clk_disable_unprepare(iclk); 70 clk_disable_unprepare(hclk); 71 if (IS_ENABLED(CONFIG_COMMON_CLK)) 72 clk_disable_unprepare(uclk); 73 clocked = 0; 74} 75 76static void at91_start_hc(struct platform_device *pdev) 77{ 78 struct usb_hcd *hcd = platform_get_drvdata(pdev); 79 struct ohci_regs __iomem *regs = hcd->regs; 80 81 dev_dbg(&pdev->dev, "start\n"); 82 83 /* 84 * Start the USB clocks. 85 */ 86 at91_start_clock(); 87 88 /* 89 * The USB host controller must remain in reset. 90 */ 91 writel(0, ®s->control); 92} 93 94static void at91_stop_hc(struct platform_device *pdev) 95{ 96 struct usb_hcd *hcd = platform_get_drvdata(pdev); 97 struct ohci_regs __iomem *regs = hcd->regs; 98 99 dev_dbg(&pdev->dev, "stop\n"); 100 101 /* 102 * Put the USB host controller into reset. 103 */ 104 writel(0, ®s->control); 105 106 /* 107 * Stop the USB clocks. 108 */ 109 at91_stop_clock(); 110} 111 112 113/*-------------------------------------------------------------------------*/ 114 115static void usb_hcd_at91_remove (struct usb_hcd *, struct platform_device *); 116 117/* configure so an HC device and id are always provided */ 118/* always called with process context; sleeping is OK */ 119 120 121/** 122 * usb_hcd_at91_probe - initialize AT91-based HCDs 123 * Context: !in_interrupt() 124 * 125 * Allocates basic resources for this USB host controller, and 126 * then invokes the start() method for the HCD associated with it 127 * through the hotplug entry's driver_data. 128 */ 129static int usb_hcd_at91_probe(const struct hc_driver *driver, 130 struct platform_device *pdev) 131{ 132 struct at91_usbh_data *board; 133 struct ohci_hcd *ohci; 134 int retval; 135 struct usb_hcd *hcd = NULL; 136 struct device *dev = &pdev->dev; 137 struct resource *res; 138 int irq; 139 140 res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 141 if (!res) { 142 dev_dbg(dev, "hcd probe: missing memory resource\n"); 143 return -ENXIO; 144 } 145 146 irq = platform_get_irq(pdev, 0); 147 if (irq < 0) { 148 dev_dbg(dev, "hcd probe: missing irq resource\n"); 149 return irq; 150 } 151 152 hcd = usb_create_hcd(driver, dev, "at91"); 153 if (!hcd) 154 return -ENOMEM; 155 hcd->rsrc_start = res->start; 156 hcd->rsrc_len = resource_size(res); 157 158 hcd->regs = devm_ioremap_resource(dev, res); 159 if (IS_ERR(hcd->regs)) { 160 retval = PTR_ERR(hcd->regs); 161 goto err; 162 } 163 164 iclk = devm_clk_get(dev, "ohci_clk"); 165 if (IS_ERR(iclk)) { 166 dev_err(dev, "failed to get ohci_clk\n"); 167 retval = PTR_ERR(iclk); 168 goto err; 169 } 170 fclk = devm_clk_get(dev, "uhpck"); 171 if (IS_ERR(fclk)) { 172 dev_err(dev, "failed to get uhpck\n"); 173 retval = PTR_ERR(fclk); 174 goto err; 175 } 176 hclk = devm_clk_get(dev, "hclk"); 177 if (IS_ERR(hclk)) { 178 dev_err(dev, "failed to get hclk\n"); 179 retval = PTR_ERR(hclk); 180 goto err; 181 } 182 if (IS_ENABLED(CONFIG_COMMON_CLK)) { 183 uclk = devm_clk_get(dev, "usb_clk"); 184 if (IS_ERR(uclk)) { 185 dev_err(dev, "failed to get uclk\n"); 186 retval = PTR_ERR(uclk); 187 goto err; 188 } 189 } 190 191 board = hcd->self.controller->platform_data; 192 ohci = hcd_to_ohci(hcd); 193 ohci->num_ports = board->ports; 194 at91_start_hc(pdev); 195 196 retval = usb_add_hcd(hcd, irq, IRQF_SHARED); 197 if (retval == 0) { 198 device_wakeup_enable(hcd->self.controller); 199 return retval; 200 } 201 202 /* Error handling */ 203 at91_stop_hc(pdev); 204 205 err: 206 usb_put_hcd(hcd); 207 return retval; 208} 209 210 211/* may be called with controller, bus, and devices active */ 212 213/** 214 * usb_hcd_at91_remove - shutdown processing for AT91-based HCDs 215 * @dev: USB Host Controller being removed 216 * Context: !in_interrupt() 217 * 218 * Reverses the effect of usb_hcd_at91_probe(), first invoking 219 * the HCD's stop() method. It is always called from a thread 220 * context, "rmmod" or something similar. 221 * 222 */ 223static void usb_hcd_at91_remove(struct usb_hcd *hcd, 224 struct platform_device *pdev) 225{ 226 usb_remove_hcd(hcd); 227 at91_stop_hc(pdev); 228 usb_put_hcd(hcd); 229} 230 231/*-------------------------------------------------------------------------*/ 232static void ohci_at91_usb_set_power(struct at91_usbh_data *pdata, int port, int enable) 233{ 234 if (!valid_port(port)) 235 return; 236 237 if (!gpio_is_valid(pdata->vbus_pin[port])) 238 return; 239 240 gpio_set_value(pdata->vbus_pin[port], 241 pdata->vbus_pin_active_low[port] ^ enable); 242} 243 244static int ohci_at91_usb_get_power(struct at91_usbh_data *pdata, int port) 245{ 246 if (!valid_port(port)) 247 return -EINVAL; 248 249 if (!gpio_is_valid(pdata->vbus_pin[port])) 250 return -EINVAL; 251 252 return gpio_get_value(pdata->vbus_pin[port]) ^ 253 pdata->vbus_pin_active_low[port]; 254} 255 256/* 257 * Update the status data from the hub with the over-current indicator change. 258 */ 259static int ohci_at91_hub_status_data(struct usb_hcd *hcd, char *buf) 260{ 261 struct at91_usbh_data *pdata = hcd->self.controller->platform_data; 262 int length = ohci_hub_status_data(hcd, buf); 263 int port; 264 265 at91_for_each_port(port) { 266 if (pdata->overcurrent_changed[port]) { 267 if (!length) 268 length = 1; 269 buf[0] |= 1 << (port + 1); 270 } 271 } 272 273 return length; 274} 275 276/* 277 * Look at the control requests to the root hub and see if we need to override. 278 */ 279static int ohci_at91_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue, 280 u16 wIndex, char *buf, u16 wLength) 281{ 282 struct at91_usbh_data *pdata = dev_get_platdata(hcd->self.controller); 283 struct usb_hub_descriptor *desc; 284 int ret = -EINVAL; 285 u32 *data = (u32 *)buf; 286 287 dev_dbg(hcd->self.controller, 288 "ohci_at91_hub_control(%p,0x%04x,0x%04x,0x%04x,%p,%04x)\n", 289 hcd, typeReq, wValue, wIndex, buf, wLength); 290 291 wIndex--; 292 293 switch (typeReq) { 294 case SetPortFeature: 295 if (wValue == USB_PORT_FEAT_POWER) { 296 dev_dbg(hcd->self.controller, "SetPortFeat: POWER\n"); 297 if (valid_port(wIndex)) { 298 ohci_at91_usb_set_power(pdata, wIndex, 1); 299 ret = 0; 300 } 301 302 goto out; 303 } 304 break; 305 306 case ClearPortFeature: 307 switch (wValue) { 308 case USB_PORT_FEAT_C_OVER_CURRENT: 309 dev_dbg(hcd->self.controller, 310 "ClearPortFeature: C_OVER_CURRENT\n"); 311 312 if (valid_port(wIndex)) { 313 pdata->overcurrent_changed[wIndex] = 0; 314 pdata->overcurrent_status[wIndex] = 0; 315 } 316 317 goto out; 318 319 case USB_PORT_FEAT_OVER_CURRENT: 320 dev_dbg(hcd->self.controller, 321 "ClearPortFeature: OVER_CURRENT\n"); 322 323 if (valid_port(wIndex)) 324 pdata->overcurrent_status[wIndex] = 0; 325 326 goto out; 327 328 case USB_PORT_FEAT_POWER: 329 dev_dbg(hcd->self.controller, 330 "ClearPortFeature: POWER\n"); 331 332 if (valid_port(wIndex)) { 333 ohci_at91_usb_set_power(pdata, wIndex, 0); 334 return 0; 335 } 336 } 337 break; 338 } 339 340 ret = ohci_hub_control(hcd, typeReq, wValue, wIndex + 1, buf, wLength); 341 if (ret) 342 goto out; 343 344 switch (typeReq) { 345 case GetHubDescriptor: 346 347 /* update the hub's descriptor */ 348 349 desc = (struct usb_hub_descriptor *)buf; 350 351 dev_dbg(hcd->self.controller, "wHubCharacteristics 0x%04x\n", 352 desc->wHubCharacteristics); 353 354 /* remove the old configurations for power-switching, and 355 * over-current protection, and insert our new configuration 356 */ 357 358 desc->wHubCharacteristics &= ~cpu_to_le16(HUB_CHAR_LPSM); 359 desc->wHubCharacteristics |= cpu_to_le16(0x0001); 360 361 if (pdata->overcurrent_supported) { 362 desc->wHubCharacteristics &= ~cpu_to_le16(HUB_CHAR_OCPM); 363 desc->wHubCharacteristics |= cpu_to_le16(0x0008|0x0001); 364 } 365 366 dev_dbg(hcd->self.controller, "wHubCharacteristics after 0x%04x\n", 367 desc->wHubCharacteristics); 368 369 return ret; 370 371 case GetPortStatus: 372 /* check port status */ 373 374 dev_dbg(hcd->self.controller, "GetPortStatus(%d)\n", wIndex); 375 376 if (valid_port(wIndex)) { 377 if (!ohci_at91_usb_get_power(pdata, wIndex)) 378 *data &= ~cpu_to_le32(RH_PS_PPS); 379 380 if (pdata->overcurrent_changed[wIndex]) 381 *data |= cpu_to_le32(RH_PS_OCIC); 382 383 if (pdata->overcurrent_status[wIndex]) 384 *data |= cpu_to_le32(RH_PS_POCI); 385 } 386 } 387 388 out: 389 return ret; 390} 391 392/*-------------------------------------------------------------------------*/ 393 394static irqreturn_t ohci_hcd_at91_overcurrent_irq(int irq, void *data) 395{ 396 struct platform_device *pdev = data; 397 struct at91_usbh_data *pdata = dev_get_platdata(&pdev->dev); 398 int val, gpio, port; 399 400 /* From the GPIO notifying the over-current situation, find 401 * out the corresponding port */ 402 at91_for_each_port(port) { 403 if (gpio_is_valid(pdata->overcurrent_pin[port]) && 404 gpio_to_irq(pdata->overcurrent_pin[port]) == irq) { 405 gpio = pdata->overcurrent_pin[port]; 406 break; 407 } 408 } 409 410 if (port == AT91_MAX_USBH_PORTS) { 411 dev_err(& pdev->dev, "overcurrent interrupt from unknown GPIO\n"); 412 return IRQ_HANDLED; 413 } 414 415 val = gpio_get_value(gpio); 416 417 /* When notified of an over-current situation, disable power 418 on the corresponding port, and mark this port in 419 over-current. */ 420 if (!val) { 421 ohci_at91_usb_set_power(pdata, port, 0); 422 pdata->overcurrent_status[port] = 1; 423 pdata->overcurrent_changed[port] = 1; 424 } 425 426 dev_dbg(& pdev->dev, "overcurrent situation %s\n", 427 val ? "exited" : "notified"); 428 429 return IRQ_HANDLED; 430} 431 432#ifdef CONFIG_OF 433static const struct of_device_id at91_ohci_dt_ids[] = { 434 { .compatible = "atmel,at91rm9200-ohci" }, 435 { /* sentinel */ } 436}; 437 438MODULE_DEVICE_TABLE(of, at91_ohci_dt_ids); 439 440static int ohci_at91_of_init(struct platform_device *pdev) 441{ 442 struct device_node *np = pdev->dev.of_node; 443 int i, gpio, ret; 444 enum of_gpio_flags flags; 445 struct at91_usbh_data *pdata; 446 u32 ports; 447 448 if (!np) 449 return 0; 450 451 /* Right now device-tree probed devices don't get dma_mask set. 452 * Since shared usb code relies on it, set it here for now. 453 * Once we have dma capability bindings this can go away. 454 */ 455 ret = dma_coerce_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32)); 456 if (ret) 457 return ret; 458 459 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL); 460 if (!pdata) 461 return -ENOMEM; 462 463 if (!of_property_read_u32(np, "num-ports", &ports)) 464 pdata->ports = ports; 465 466 at91_for_each_port(i) { 467 gpio = of_get_named_gpio_flags(np, "atmel,vbus-gpio", i, &flags); 468 pdata->vbus_pin[i] = gpio; 469 if (!gpio_is_valid(gpio)) 470 continue; 471 pdata->vbus_pin_active_low[i] = flags & OF_GPIO_ACTIVE_LOW; 472 } 473 474 at91_for_each_port(i) 475 pdata->overcurrent_pin[i] = 476 of_get_named_gpio_flags(np, "atmel,oc-gpio", i, &flags); 477 478 pdev->dev.platform_data = pdata; 479 480 return 0; 481} 482#else 483static int ohci_at91_of_init(struct platform_device *pdev) 484{ 485 return 0; 486} 487#endif 488 489/*-------------------------------------------------------------------------*/ 490 491static int ohci_hcd_at91_drv_probe(struct platform_device *pdev) 492{ 493 struct at91_usbh_data *pdata; 494 int i; 495 int gpio; 496 int ret; 497 498 ret = ohci_at91_of_init(pdev); 499 if (ret) 500 return ret; 501 502 pdata = dev_get_platdata(&pdev->dev); 503 504 if (pdata) { 505 at91_for_each_port(i) { 506 /* 507 * do not configure PIO if not in relation with 508 * real USB port on board 509 */ 510 if (i >= pdata->ports) { 511 pdata->vbus_pin[i] = -EINVAL; 512 pdata->overcurrent_pin[i] = -EINVAL; 513 break; 514 } 515 516 if (!gpio_is_valid(pdata->vbus_pin[i])) 517 continue; 518 gpio = pdata->vbus_pin[i]; 519 520 ret = gpio_request(gpio, "ohci_vbus"); 521 if (ret) { 522 dev_err(&pdev->dev, 523 "can't request vbus gpio %d\n", gpio); 524 continue; 525 } 526 ret = gpio_direction_output(gpio, 527 !pdata->vbus_pin_active_low[i]); 528 if (ret) { 529 dev_err(&pdev->dev, 530 "can't put vbus gpio %d as output %d\n", 531 gpio, !pdata->vbus_pin_active_low[i]); 532 gpio_free(gpio); 533 continue; 534 } 535 536 ohci_at91_usb_set_power(pdata, i, 1); 537 } 538 539 at91_for_each_port(i) { 540 if (!gpio_is_valid(pdata->overcurrent_pin[i])) 541 continue; 542 gpio = pdata->overcurrent_pin[i]; 543 544 ret = gpio_request(gpio, "ohci_overcurrent"); 545 if (ret) { 546 dev_err(&pdev->dev, 547 "can't request overcurrent gpio %d\n", 548 gpio); 549 continue; 550 } 551 552 ret = gpio_direction_input(gpio); 553 if (ret) { 554 dev_err(&pdev->dev, 555 "can't configure overcurrent gpio %d as input\n", 556 gpio); 557 gpio_free(gpio); 558 continue; 559 } 560 561 ret = request_irq(gpio_to_irq(gpio), 562 ohci_hcd_at91_overcurrent_irq, 563 IRQF_SHARED, "ohci_overcurrent", pdev); 564 if (ret) { 565 gpio_free(gpio); 566 dev_err(&pdev->dev, 567 "can't get gpio IRQ for overcurrent\n"); 568 } 569 } 570 } 571 572 device_init_wakeup(&pdev->dev, 1); 573 return usb_hcd_at91_probe(&ohci_at91_hc_driver, pdev); 574} 575 576static int ohci_hcd_at91_drv_remove(struct platform_device *pdev) 577{ 578 struct at91_usbh_data *pdata = dev_get_platdata(&pdev->dev); 579 int i; 580 581 if (pdata) { 582 at91_for_each_port(i) { 583 if (!gpio_is_valid(pdata->vbus_pin[i])) 584 continue; 585 ohci_at91_usb_set_power(pdata, i, 0); 586 gpio_free(pdata->vbus_pin[i]); 587 } 588 589 at91_for_each_port(i) { 590 if (!gpio_is_valid(pdata->overcurrent_pin[i])) 591 continue; 592 free_irq(gpio_to_irq(pdata->overcurrent_pin[i]), pdev); 593 gpio_free(pdata->overcurrent_pin[i]); 594 } 595 } 596 597 device_init_wakeup(&pdev->dev, 0); 598 usb_hcd_at91_remove(platform_get_drvdata(pdev), pdev); 599 return 0; 600} 601 602#ifdef CONFIG_PM 603 604static int 605ohci_hcd_at91_drv_suspend(struct platform_device *pdev, pm_message_t mesg) 606{ 607 struct usb_hcd *hcd = platform_get_drvdata(pdev); 608 struct ohci_hcd *ohci = hcd_to_ohci(hcd); 609 bool do_wakeup = device_may_wakeup(&pdev->dev); 610 int ret; 611 612 if (do_wakeup) 613 enable_irq_wake(hcd->irq); 614 615 ret = ohci_suspend(hcd, do_wakeup); 616 if (ret) { 617 disable_irq_wake(hcd->irq); 618 return ret; 619 } 620 /* 621 * The integrated transceivers seem unable to notice disconnect, 622 * reconnect, or wakeup without the 48 MHz clock active. so for 623 * correctness, always discard connection state (using reset). 624 * 625 * REVISIT: some boards will be able to turn VBUS off... 626 */ 627 if (at91_suspend_entering_slow_clock()) { 628 ohci->hc_control = ohci_readl(ohci, &ohci->regs->control); 629 ohci->hc_control &= OHCI_CTRL_RWC; 630 ohci_writel(ohci, ohci->hc_control, &ohci->regs->control); 631 ohci->rh_state = OHCI_RH_HALTED; 632 633 /* flush the writes */ 634 (void) ohci_readl (ohci, &ohci->regs->control); 635 at91_stop_clock(); 636 } 637 638 return ret; 639} 640 641static int ohci_hcd_at91_drv_resume(struct platform_device *pdev) 642{ 643 struct usb_hcd *hcd = platform_get_drvdata(pdev); 644 645 if (device_may_wakeup(&pdev->dev)) 646 disable_irq_wake(hcd->irq); 647 648 if (!clocked) 649 at91_start_clock(); 650 651 ohci_resume(hcd, false); 652 return 0; 653} 654#else 655#define ohci_hcd_at91_drv_suspend NULL 656#define ohci_hcd_at91_drv_resume NULL 657#endif 658 659static struct platform_driver ohci_hcd_at91_driver = { 660 .probe = ohci_hcd_at91_drv_probe, 661 .remove = ohci_hcd_at91_drv_remove, 662 .shutdown = usb_hcd_platform_shutdown, 663 .suspend = ohci_hcd_at91_drv_suspend, 664 .resume = ohci_hcd_at91_drv_resume, 665 .driver = { 666 .name = "at91_ohci", 667 .owner = THIS_MODULE, 668 .of_match_table = of_match_ptr(at91_ohci_dt_ids), 669 }, 670}; 671 672static int __init ohci_at91_init(void) 673{ 674 if (usb_disabled()) 675 return -ENODEV; 676 677 pr_info("%s: " DRIVER_DESC "\n", hcd_name); 678 ohci_init_driver(&ohci_at91_hc_driver, NULL); 679 680 /* 681 * The Atmel HW has some unusual quirks, which require Atmel-specific 682 * workarounds. We override certain hc_driver functions here to 683 * achieve that. We explicitly do not enhance ohci_driver_overrides to 684 * allow this more easily, since this is an unusual case, and we don't 685 * want to encourage others to override these functions by making it 686 * too easy. 687 */ 688 689 ohci_at91_hc_driver.hub_status_data = ohci_at91_hub_status_data; 690 ohci_at91_hc_driver.hub_control = ohci_at91_hub_control; 691 692 return platform_driver_register(&ohci_hcd_at91_driver); 693} 694module_init(ohci_at91_init); 695 696static void __exit ohci_at91_cleanup(void) 697{ 698 platform_driver_unregister(&ohci_hcd_at91_driver); 699} 700module_exit(ohci_at91_cleanup); 701 702MODULE_DESCRIPTION(DRIVER_DESC); 703MODULE_LICENSE("GPL"); 704MODULE_ALIAS("platform:at91_ohci"); 705