1 /* 2 * Renesas USB driver 3 * 4 * Copyright (C) 2011 Renesas Solutions Corp. 5 * Kuninori Morimoto <kuninori.morimoto.gx@renesas.com> 6 * 7 * This program is distributed in the hope that it will be useful, 8 * but WITHOUT ANY WARRANTY; without even the implied warranty of 9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 10 * GNU General Public License for more details. 11 * 12 * You should have received a copy of the GNU General Public License 13 * along with this program; if not, write to the Free Software 14 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 15 * 16 */ 17 #include <linux/err.h> 18 #include <linux/gpio.h> 19 #include <linux/io.h> 20 #include <linux/module.h> 21 #include <linux/pm_runtime.h> 22 #include <linux/slab.h> 23 #include <linux/sysfs.h> 24 #include "common.h" 25 #include "rcar2.h" 26 27 /* 28 * image of renesas_usbhs 29 * 30 * ex) gadget case 31 32 * mod.c 33 * mod_gadget.c 34 * mod_host.c pipe.c fifo.c 35 * 36 * +-------+ +-----------+ 37 * | pipe0 |------>| fifo pio | 38 * +------------+ +-------+ +-----------+ 39 * | mod_gadget |=====> | pipe1 |--+ 40 * +------------+ +-------+ | +-----------+ 41 * | pipe2 | | +-| fifo dma0 | 42 * +------------+ +-------+ | | +-----------+ 43 * | mod_host | | pipe3 |<-|--+ 44 * +------------+ +-------+ | +-----------+ 45 * | .... | +--->| fifo dma1 | 46 * | .... | +-----------+ 47 */ 48 49 50 #define USBHSF_RUNTIME_PWCTRL (1 << 0) 51 52 /* status */ 53 #define usbhsc_flags_init(p) do {(p)->flags = 0; } while (0) 54 #define usbhsc_flags_set(p, b) ((p)->flags |= (b)) 55 #define usbhsc_flags_clr(p, b) ((p)->flags &= ~(b)) 56 #define usbhsc_flags_has(p, b) ((p)->flags & (b)) 57 58 /* 59 * platform call back 60 * 61 * renesas usb support platform callback function. 62 * Below macro call it. 63 * if platform doesn't have callback, it return 0 (no error) 64 */ 65 #define usbhs_platform_call(priv, func, args...)\ 66 (!(priv) ? -ENODEV : \ 67 !((priv)->pfunc.func) ? 0 : \ 68 (priv)->pfunc.func(args)) 69 70 /* 71 * common functions 72 */ 73 u16 usbhs_read(struct usbhs_priv *priv, u32 reg) 74 { 75 return ioread16(priv->base + reg); 76 } 77 78 void usbhs_write(struct usbhs_priv *priv, u32 reg, u16 data) 79 { 80 iowrite16(data, priv->base + reg); 81 } 82 83 void usbhs_bset(struct usbhs_priv *priv, u32 reg, u16 mask, u16 data) 84 { 85 u16 val = usbhs_read(priv, reg); 86 87 val &= ~mask; 88 val |= data & mask; 89 90 usbhs_write(priv, reg, val); 91 } 92 93 struct usbhs_priv *usbhs_pdev_to_priv(struct platform_device *pdev) 94 { 95 return dev_get_drvdata(&pdev->dev); 96 } 97 98 /* 99 * syscfg functions 100 */ 101 static void usbhs_sys_clock_ctrl(struct usbhs_priv *priv, int enable) 102 { 103 usbhs_bset(priv, SYSCFG, SCKE, enable ? SCKE : 0); 104 } 105 106 void usbhs_sys_host_ctrl(struct usbhs_priv *priv, int enable) 107 { 108 u16 mask = DCFM | DRPD | DPRPU | HSE | USBE; 109 u16 val = DCFM | DRPD | HSE | USBE; 110 int has_otg = usbhs_get_dparam(priv, has_otg); 111 112 if (has_otg) 113 usbhs_bset(priv, DVSTCTR, (EXTLP | PWEN), (EXTLP | PWEN)); 114 115 /* 116 * if enable 117 * 118 * - select Host mode 119 * - D+ Line/D- Line Pull-down 120 */ 121 usbhs_bset(priv, SYSCFG, mask, enable ? val : 0); 122 } 123 124 void usbhs_sys_function_ctrl(struct usbhs_priv *priv, int enable) 125 { 126 u16 mask = DCFM | DRPD | DPRPU | HSE | USBE; 127 u16 val = DPRPU | HSE | USBE; 128 129 /* 130 * if enable 131 * 132 * - select Function mode 133 * - D+ Line Pull-up 134 */ 135 usbhs_bset(priv, SYSCFG, mask, enable ? val : 0); 136 } 137 138 void usbhs_sys_function_pullup(struct usbhs_priv *priv, int enable) 139 { 140 usbhs_bset(priv, SYSCFG, DPRPU, enable ? DPRPU : 0); 141 } 142 143 void usbhs_sys_set_test_mode(struct usbhs_priv *priv, u16 mode) 144 { 145 usbhs_write(priv, TESTMODE, mode); 146 } 147 148 /* 149 * frame functions 150 */ 151 int usbhs_frame_get_num(struct usbhs_priv *priv) 152 { 153 return usbhs_read(priv, FRMNUM) & FRNM_MASK; 154 } 155 156 /* 157 * usb request functions 158 */ 159 void usbhs_usbreq_get_val(struct usbhs_priv *priv, struct usb_ctrlrequest *req) 160 { 161 u16 val; 162 163 val = usbhs_read(priv, USBREQ); 164 req->bRequest = (val >> 8) & 0xFF; 165 req->bRequestType = (val >> 0) & 0xFF; 166 167 req->wValue = usbhs_read(priv, USBVAL); 168 req->wIndex = usbhs_read(priv, USBINDX); 169 req->wLength = usbhs_read(priv, USBLENG); 170 } 171 172 void usbhs_usbreq_set_val(struct usbhs_priv *priv, struct usb_ctrlrequest *req) 173 { 174 usbhs_write(priv, USBREQ, (req->bRequest << 8) | req->bRequestType); 175 usbhs_write(priv, USBVAL, req->wValue); 176 usbhs_write(priv, USBINDX, req->wIndex); 177 usbhs_write(priv, USBLENG, req->wLength); 178 179 usbhs_bset(priv, DCPCTR, SUREQ, SUREQ); 180 } 181 182 /* 183 * bus/vbus functions 184 */ 185 void usbhs_bus_send_sof_enable(struct usbhs_priv *priv) 186 { 187 u16 status = usbhs_read(priv, DVSTCTR) & (USBRST | UACT); 188 189 if (status != USBRST) { 190 struct device *dev = usbhs_priv_to_dev(priv); 191 dev_err(dev, "usbhs should be reset\n"); 192 } 193 194 usbhs_bset(priv, DVSTCTR, (USBRST | UACT), UACT); 195 } 196 197 void usbhs_bus_send_reset(struct usbhs_priv *priv) 198 { 199 usbhs_bset(priv, DVSTCTR, (USBRST | UACT), USBRST); 200 } 201 202 int usbhs_bus_get_speed(struct usbhs_priv *priv) 203 { 204 u16 dvstctr = usbhs_read(priv, DVSTCTR); 205 206 switch (RHST & dvstctr) { 207 case RHST_LOW_SPEED: 208 return USB_SPEED_LOW; 209 case RHST_FULL_SPEED: 210 return USB_SPEED_FULL; 211 case RHST_HIGH_SPEED: 212 return USB_SPEED_HIGH; 213 } 214 215 return USB_SPEED_UNKNOWN; 216 } 217 218 int usbhs_vbus_ctrl(struct usbhs_priv *priv, int enable) 219 { 220 struct platform_device *pdev = usbhs_priv_to_pdev(priv); 221 222 return usbhs_platform_call(priv, set_vbus, pdev, enable); 223 } 224 225 static void usbhsc_bus_init(struct usbhs_priv *priv) 226 { 227 usbhs_write(priv, DVSTCTR, 0); 228 229 usbhs_vbus_ctrl(priv, 0); 230 } 231 232 /* 233 * device configuration 234 */ 235 int usbhs_set_device_config(struct usbhs_priv *priv, int devnum, 236 u16 upphub, u16 hubport, u16 speed) 237 { 238 struct device *dev = usbhs_priv_to_dev(priv); 239 u16 usbspd = 0; 240 u32 reg = DEVADD0 + (2 * devnum); 241 242 if (devnum > 10) { 243 dev_err(dev, "cannot set speed to unknown device %d\n", devnum); 244 return -EIO; 245 } 246 247 if (upphub > 0xA) { 248 dev_err(dev, "unsupported hub number %d\n", upphub); 249 return -EIO; 250 } 251 252 switch (speed) { 253 case USB_SPEED_LOW: 254 usbspd = USBSPD_SPEED_LOW; 255 break; 256 case USB_SPEED_FULL: 257 usbspd = USBSPD_SPEED_FULL; 258 break; 259 case USB_SPEED_HIGH: 260 usbspd = USBSPD_SPEED_HIGH; 261 break; 262 default: 263 dev_err(dev, "unsupported speed %d\n", speed); 264 return -EIO; 265 } 266 267 usbhs_write(priv, reg, UPPHUB(upphub) | 268 HUBPORT(hubport)| 269 USBSPD(usbspd)); 270 271 return 0; 272 } 273 274 /* 275 * local functions 276 */ 277 static void usbhsc_set_buswait(struct usbhs_priv *priv) 278 { 279 int wait = usbhs_get_dparam(priv, buswait_bwait); 280 281 /* set bus wait if platform have */ 282 if (wait) 283 usbhs_bset(priv, BUSWAIT, 0x000F, wait); 284 } 285 286 /* 287 * platform default param 288 */ 289 290 /* commonly used on old SH-Mobile SoCs */ 291 static u32 usbhsc_default_pipe_type[] = { 292 USB_ENDPOINT_XFER_CONTROL, 293 USB_ENDPOINT_XFER_ISOC, 294 USB_ENDPOINT_XFER_ISOC, 295 USB_ENDPOINT_XFER_BULK, 296 USB_ENDPOINT_XFER_BULK, 297 USB_ENDPOINT_XFER_BULK, 298 USB_ENDPOINT_XFER_INT, 299 USB_ENDPOINT_XFER_INT, 300 USB_ENDPOINT_XFER_INT, 301 USB_ENDPOINT_XFER_INT, 302 }; 303 304 /* commonly used on newer SH-Mobile and R-Car SoCs */ 305 static u32 usbhsc_new_pipe_type[] = { 306 USB_ENDPOINT_XFER_CONTROL, 307 USB_ENDPOINT_XFER_ISOC, 308 USB_ENDPOINT_XFER_ISOC, 309 USB_ENDPOINT_XFER_BULK, 310 USB_ENDPOINT_XFER_BULK, 311 USB_ENDPOINT_XFER_BULK, 312 USB_ENDPOINT_XFER_INT, 313 USB_ENDPOINT_XFER_INT, 314 USB_ENDPOINT_XFER_INT, 315 USB_ENDPOINT_XFER_BULK, 316 USB_ENDPOINT_XFER_BULK, 317 USB_ENDPOINT_XFER_BULK, 318 USB_ENDPOINT_XFER_BULK, 319 USB_ENDPOINT_XFER_BULK, 320 USB_ENDPOINT_XFER_BULK, 321 USB_ENDPOINT_XFER_BULK, 322 }; 323 324 /* 325 * power control 326 */ 327 static void usbhsc_power_ctrl(struct usbhs_priv *priv, int enable) 328 { 329 struct platform_device *pdev = usbhs_priv_to_pdev(priv); 330 struct device *dev = usbhs_priv_to_dev(priv); 331 332 if (enable) { 333 /* enable PM */ 334 pm_runtime_get_sync(dev); 335 336 /* enable platform power */ 337 usbhs_platform_call(priv, power_ctrl, pdev, priv->base, enable); 338 339 /* USB on */ 340 usbhs_sys_clock_ctrl(priv, enable); 341 } else { 342 /* USB off */ 343 usbhs_sys_clock_ctrl(priv, enable); 344 345 /* disable platform power */ 346 usbhs_platform_call(priv, power_ctrl, pdev, priv->base, enable); 347 348 /* disable PM */ 349 pm_runtime_put_sync(dev); 350 } 351 } 352 353 /* 354 * hotplug 355 */ 356 static void usbhsc_hotplug(struct usbhs_priv *priv) 357 { 358 struct platform_device *pdev = usbhs_priv_to_pdev(priv); 359 struct usbhs_mod *mod = usbhs_mod_get_current(priv); 360 int id; 361 int enable; 362 int ret; 363 364 /* 365 * get vbus status from platform 366 */ 367 enable = usbhs_platform_call(priv, get_vbus, pdev); 368 369 /* 370 * get id from platform 371 */ 372 id = usbhs_platform_call(priv, get_id, pdev); 373 374 if (enable && !mod) { 375 ret = usbhs_mod_change(priv, id); 376 if (ret < 0) 377 return; 378 379 dev_dbg(&pdev->dev, "%s enable\n", __func__); 380 381 /* power on */ 382 if (usbhsc_flags_has(priv, USBHSF_RUNTIME_PWCTRL)) 383 usbhsc_power_ctrl(priv, enable); 384 385 /* bus init */ 386 usbhsc_set_buswait(priv); 387 usbhsc_bus_init(priv); 388 389 /* module start */ 390 usbhs_mod_call(priv, start, priv); 391 392 } else if (!enable && mod) { 393 dev_dbg(&pdev->dev, "%s disable\n", __func__); 394 395 /* module stop */ 396 usbhs_mod_call(priv, stop, priv); 397 398 /* bus init */ 399 usbhsc_bus_init(priv); 400 401 /* power off */ 402 if (usbhsc_flags_has(priv, USBHSF_RUNTIME_PWCTRL)) 403 usbhsc_power_ctrl(priv, enable); 404 405 usbhs_mod_change(priv, -1); 406 407 /* reset phy for next connection */ 408 usbhs_platform_call(priv, phy_reset, pdev); 409 } 410 } 411 412 /* 413 * notify hotplug 414 */ 415 static void usbhsc_notify_hotplug(struct work_struct *work) 416 { 417 struct usbhs_priv *priv = container_of(work, 418 struct usbhs_priv, 419 notify_hotplug_work.work); 420 usbhsc_hotplug(priv); 421 } 422 423 static int usbhsc_drvcllbck_notify_hotplug(struct platform_device *pdev) 424 { 425 struct usbhs_priv *priv = usbhs_pdev_to_priv(pdev); 426 int delay = usbhs_get_dparam(priv, detection_delay); 427 428 /* 429 * This functions will be called in interrupt. 430 * To make sure safety context, 431 * use workqueue for usbhs_notify_hotplug 432 */ 433 schedule_delayed_work(&priv->notify_hotplug_work, 434 msecs_to_jiffies(delay)); 435 return 0; 436 } 437 438 /* 439 * platform functions 440 */ 441 static int usbhs_probe(struct platform_device *pdev) 442 { 443 struct renesas_usbhs_platform_info *info = dev_get_platdata(&pdev->dev); 444 struct renesas_usbhs_driver_callback *dfunc; 445 struct usbhs_priv *priv; 446 struct resource *res, *irq_res; 447 int ret; 448 449 /* check platform information */ 450 if (!info) { 451 dev_err(&pdev->dev, "no platform information\n"); 452 return -EINVAL; 453 } 454 455 /* platform data */ 456 res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 457 irq_res = platform_get_resource(pdev, IORESOURCE_IRQ, 0); 458 if (!res || !irq_res) { 459 dev_err(&pdev->dev, "Not enough Renesas USB platform resources.\n"); 460 return -ENODEV; 461 } 462 463 /* usb private data */ 464 priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL); 465 if (!priv) { 466 dev_err(&pdev->dev, "Could not allocate priv\n"); 467 return -ENOMEM; 468 } 469 470 priv->base = devm_ioremap_resource(&pdev->dev, res); 471 if (IS_ERR(priv->base)) 472 return PTR_ERR(priv->base); 473 474 /* 475 * care platform info 476 */ 477 478 memcpy(&priv->dparam, 479 &info->driver_param, 480 sizeof(struct renesas_usbhs_driver_param)); 481 482 switch (priv->dparam.type) { 483 case USBHS_TYPE_R8A7790: 484 case USBHS_TYPE_R8A7791: 485 priv->pfunc = usbhs_rcar2_ops; 486 if (!priv->dparam.pipe_type) { 487 priv->dparam.pipe_type = usbhsc_new_pipe_type; 488 priv->dparam.pipe_size = 489 ARRAY_SIZE(usbhsc_new_pipe_type); 490 } 491 break; 492 default: 493 if (!info->platform_callback.get_id) { 494 dev_err(&pdev->dev, "no platform callbacks"); 495 return -EINVAL; 496 } 497 memcpy(&priv->pfunc, 498 &info->platform_callback, 499 sizeof(struct renesas_usbhs_platform_callback)); 500 break; 501 } 502 503 /* set driver callback functions for platform */ 504 dfunc = &info->driver_callback; 505 dfunc->notify_hotplug = usbhsc_drvcllbck_notify_hotplug; 506 507 /* set default param if platform doesn't have */ 508 if (!priv->dparam.pipe_type) { 509 priv->dparam.pipe_type = usbhsc_default_pipe_type; 510 priv->dparam.pipe_size = ARRAY_SIZE(usbhsc_default_pipe_type); 511 } 512 if (!priv->dparam.pio_dma_border) 513 priv->dparam.pio_dma_border = 64; /* 64byte */ 514 515 /* FIXME */ 516 /* runtime power control ? */ 517 if (priv->pfunc.get_vbus) 518 usbhsc_flags_set(priv, USBHSF_RUNTIME_PWCTRL); 519 520 /* 521 * priv settings 522 */ 523 priv->irq = irq_res->start; 524 if (irq_res->flags & IORESOURCE_IRQ_SHAREABLE) 525 priv->irqflags = IRQF_SHARED; 526 priv->pdev = pdev; 527 INIT_DELAYED_WORK(&priv->notify_hotplug_work, usbhsc_notify_hotplug); 528 spin_lock_init(usbhs_priv_to_lock(priv)); 529 530 /* call pipe and module init */ 531 ret = usbhs_pipe_probe(priv); 532 if (ret < 0) 533 return ret; 534 535 ret = usbhs_fifo_probe(priv); 536 if (ret < 0) 537 goto probe_end_pipe_exit; 538 539 ret = usbhs_mod_probe(priv); 540 if (ret < 0) 541 goto probe_end_fifo_exit; 542 543 /* dev_set_drvdata should be called after usbhs_mod_init */ 544 platform_set_drvdata(pdev, priv); 545 546 /* 547 * deviece reset here because 548 * USB device might be used in boot loader. 549 */ 550 usbhs_sys_clock_ctrl(priv, 0); 551 552 /* check GPIO determining if USB function should be enabled */ 553 if (priv->dparam.enable_gpio) { 554 gpio_request_one(priv->dparam.enable_gpio, GPIOF_IN, NULL); 555 ret = !gpio_get_value(priv->dparam.enable_gpio); 556 gpio_free(priv->dparam.enable_gpio); 557 if (ret) { 558 dev_warn(&pdev->dev, 559 "USB function not selected (GPIO %d)\n", 560 priv->dparam.enable_gpio); 561 ret = -ENOTSUPP; 562 goto probe_end_mod_exit; 563 } 564 } 565 566 /* 567 * platform call 568 * 569 * USB phy setup might depend on CPU/Board. 570 * If platform has its callback functions, 571 * call it here. 572 */ 573 ret = usbhs_platform_call(priv, hardware_init, pdev); 574 if (ret < 0) { 575 dev_err(&pdev->dev, "platform prove failed.\n"); 576 goto probe_end_mod_exit; 577 } 578 579 /* reset phy for connection */ 580 usbhs_platform_call(priv, phy_reset, pdev); 581 582 /* power control */ 583 pm_runtime_enable(&pdev->dev); 584 if (!usbhsc_flags_has(priv, USBHSF_RUNTIME_PWCTRL)) { 585 usbhsc_power_ctrl(priv, 1); 586 usbhs_mod_autonomy_mode(priv); 587 } 588 589 /* 590 * manual call notify_hotplug for cold plug 591 */ 592 ret = usbhsc_drvcllbck_notify_hotplug(pdev); 593 if (ret < 0) 594 goto probe_end_call_remove; 595 596 dev_info(&pdev->dev, "probed\n"); 597 598 return ret; 599 600 probe_end_call_remove: 601 usbhs_platform_call(priv, hardware_exit, pdev); 602 probe_end_mod_exit: 603 usbhs_mod_remove(priv); 604 probe_end_fifo_exit: 605 usbhs_fifo_remove(priv); 606 probe_end_pipe_exit: 607 usbhs_pipe_remove(priv); 608 609 dev_info(&pdev->dev, "probe failed\n"); 610 611 return ret; 612 } 613 614 static int usbhs_remove(struct platform_device *pdev) 615 { 616 struct usbhs_priv *priv = usbhs_pdev_to_priv(pdev); 617 struct renesas_usbhs_platform_info *info = dev_get_platdata(&pdev->dev); 618 struct renesas_usbhs_driver_callback *dfunc = &info->driver_callback; 619 620 dev_dbg(&pdev->dev, "usb remove\n"); 621 622 dfunc->notify_hotplug = NULL; 623 624 /* power off */ 625 if (!usbhsc_flags_has(priv, USBHSF_RUNTIME_PWCTRL)) 626 usbhsc_power_ctrl(priv, 0); 627 628 pm_runtime_disable(&pdev->dev); 629 630 usbhs_platform_call(priv, hardware_exit, pdev); 631 usbhs_mod_remove(priv); 632 usbhs_fifo_remove(priv); 633 usbhs_pipe_remove(priv); 634 635 return 0; 636 } 637 638 static int usbhsc_suspend(struct device *dev) 639 { 640 struct usbhs_priv *priv = dev_get_drvdata(dev); 641 struct usbhs_mod *mod = usbhs_mod_get_current(priv); 642 643 if (mod) { 644 usbhs_mod_call(priv, stop, priv); 645 usbhs_mod_change(priv, -1); 646 } 647 648 if (mod || !usbhsc_flags_has(priv, USBHSF_RUNTIME_PWCTRL)) 649 usbhsc_power_ctrl(priv, 0); 650 651 return 0; 652 } 653 654 static int usbhsc_resume(struct device *dev) 655 { 656 struct usbhs_priv *priv = dev_get_drvdata(dev); 657 struct platform_device *pdev = usbhs_priv_to_pdev(priv); 658 659 if (!usbhsc_flags_has(priv, USBHSF_RUNTIME_PWCTRL)) 660 usbhsc_power_ctrl(priv, 1); 661 662 usbhs_platform_call(priv, phy_reset, pdev); 663 664 usbhsc_drvcllbck_notify_hotplug(pdev); 665 666 return 0; 667 } 668 669 static int usbhsc_runtime_nop(struct device *dev) 670 { 671 /* Runtime PM callback shared between ->runtime_suspend() 672 * and ->runtime_resume(). Simply returns success. 673 * 674 * This driver re-initializes all registers after 675 * pm_runtime_get_sync() anyway so there is no need 676 * to save and restore registers here. 677 */ 678 return 0; 679 } 680 681 static const struct dev_pm_ops usbhsc_pm_ops = { 682 .suspend = usbhsc_suspend, 683 .resume = usbhsc_resume, 684 .runtime_suspend = usbhsc_runtime_nop, 685 .runtime_resume = usbhsc_runtime_nop, 686 }; 687 688 static struct platform_driver renesas_usbhs_driver = { 689 .driver = { 690 .name = "renesas_usbhs", 691 .pm = &usbhsc_pm_ops, 692 }, 693 .probe = usbhs_probe, 694 .remove = usbhs_remove, 695 }; 696 697 module_platform_driver(renesas_usbhs_driver); 698 699 MODULE_LICENSE("GPL"); 700 MODULE_DESCRIPTION("Renesas USB driver"); 701 MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>"); 702