xhci-rcar.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /*
  2. * xHCI host controller driver for R-Car SoCs
  3. *
  4. * Copyright (C) 2014 Renesas Electronics Corporation
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * version 2 as published by the Free Software Foundation.
  9. */
  10. #include <linux/firmware.h>
  11. #include <linux/module.h>
  12. #include <linux/platform_device.h>
  13. #include <linux/usb/phy.h>
  14. #include "xhci.h"
  15. #include "xhci-plat.h"
  16. #include "xhci-rcar.h"
  17. /*
  18. * - The V2 firmware is possible to use on R-Car Gen2. However, the V2 causes
  19. * performance degradation. So, this driver continues to use the V1 if R-Car
  20. * Gen2.
  21. * - The V1 firmware is impossible to use on R-Car Gen3.
  22. */
  23. MODULE_FIRMWARE(XHCI_RCAR_FIRMWARE_NAME_V1);
  24. MODULE_FIRMWARE(XHCI_RCAR_FIRMWARE_NAME_V2);
  25. /*** Register Offset ***/
  26. #define RCAR_USB3_INT_ENA 0x224 /* Interrupt Enable */
  27. #define RCAR_USB3_DL_CTRL 0x250 /* FW Download Control & Status */
  28. #define RCAR_USB3_FW_DATA0 0x258 /* FW Data0 */
  29. #define RCAR_USB3_LCLK 0xa44 /* LCLK Select */
  30. #define RCAR_USB3_CONF1 0xa48 /* USB3.0 Configuration1 */
  31. #define RCAR_USB3_CONF2 0xa5c /* USB3.0 Configuration2 */
  32. #define RCAR_USB3_CONF3 0xaa8 /* USB3.0 Configuration3 */
  33. #define RCAR_USB3_RX_POL 0xab0 /* USB3.0 RX Polarity */
  34. #define RCAR_USB3_TX_POL 0xab8 /* USB3.0 TX Polarity */
  35. /*** Register Settings ***/
  36. /* Interrupt Enable */
  37. #define RCAR_USB3_INT_XHC_ENA 0x00000001
  38. #define RCAR_USB3_INT_PME_ENA 0x00000002
  39. #define RCAR_USB3_INT_HSE_ENA 0x00000004
  40. #define RCAR_USB3_INT_ENA_VAL (RCAR_USB3_INT_XHC_ENA | \
  41. RCAR_USB3_INT_PME_ENA | RCAR_USB3_INT_HSE_ENA)
  42. /* FW Download Control & Status */
  43. #define RCAR_USB3_DL_CTRL_ENABLE 0x00000001
  44. #define RCAR_USB3_DL_CTRL_FW_SUCCESS 0x00000010
  45. #define RCAR_USB3_DL_CTRL_FW_SET_DATA0 0x00000100
  46. /* LCLK Select */
  47. #define RCAR_USB3_LCLK_ENA_VAL 0x01030001
  48. /* USB3.0 Configuration */
  49. #define RCAR_USB3_CONF1_VAL 0x00030204
  50. #define RCAR_USB3_CONF2_VAL 0x00030300
  51. #define RCAR_USB3_CONF3_VAL 0x13802007
  52. /* USB3.0 Polarity */
  53. #define RCAR_USB3_RX_POL_VAL BIT(21)
  54. #define RCAR_USB3_TX_POL_VAL BIT(4)
  55. static void xhci_rcar_start_gen2(struct usb_hcd *hcd)
  56. {
  57. /* LCLK Select */
  58. writel(RCAR_USB3_LCLK_ENA_VAL, hcd->regs + RCAR_USB3_LCLK);
  59. /* USB3.0 Configuration */
  60. writel(RCAR_USB3_CONF1_VAL, hcd->regs + RCAR_USB3_CONF1);
  61. writel(RCAR_USB3_CONF2_VAL, hcd->regs + RCAR_USB3_CONF2);
  62. writel(RCAR_USB3_CONF3_VAL, hcd->regs + RCAR_USB3_CONF3);
  63. /* USB3.0 Polarity */
  64. writel(RCAR_USB3_RX_POL_VAL, hcd->regs + RCAR_USB3_RX_POL);
  65. writel(RCAR_USB3_TX_POL_VAL, hcd->regs + RCAR_USB3_TX_POL);
  66. }
  67. void xhci_rcar_start(struct usb_hcd *hcd)
  68. {
  69. u32 temp;
  70. if (hcd->regs != NULL) {
  71. /* Interrupt Enable */
  72. temp = readl(hcd->regs + RCAR_USB3_INT_ENA);
  73. temp |= RCAR_USB3_INT_ENA_VAL;
  74. writel(temp, hcd->regs + RCAR_USB3_INT_ENA);
  75. if (xhci_plat_type_is(hcd, XHCI_PLAT_TYPE_RENESAS_RCAR_GEN2))
  76. xhci_rcar_start_gen2(hcd);
  77. }
  78. }
  79. static int xhci_rcar_download_firmware(struct usb_hcd *hcd)
  80. {
  81. struct device *dev = hcd->self.controller;
  82. void __iomem *regs = hcd->regs;
  83. struct xhci_plat_priv *priv = hcd_to_xhci_priv(hcd);
  84. const struct firmware *fw;
  85. int retval, index, j, time;
  86. int timeout = 10000;
  87. u32 data, val, temp;
  88. /* request R-Car USB3.0 firmware */
  89. retval = request_firmware(&fw, priv->firmware_name, dev);
  90. if (retval)
  91. return retval;
  92. /* download R-Car USB3.0 firmware */
  93. temp = readl(regs + RCAR_USB3_DL_CTRL);
  94. temp |= RCAR_USB3_DL_CTRL_ENABLE;
  95. writel(temp, regs + RCAR_USB3_DL_CTRL);
  96. for (index = 0; index < fw->size; index += 4) {
  97. /* to avoid reading beyond the end of the buffer */
  98. for (data = 0, j = 3; j >= 0; j--) {
  99. if ((j + index) < fw->size)
  100. data |= fw->data[index + j] << (8 * j);
  101. }
  102. writel(data, regs + RCAR_USB3_FW_DATA0);
  103. temp = readl(regs + RCAR_USB3_DL_CTRL);
  104. temp |= RCAR_USB3_DL_CTRL_FW_SET_DATA0;
  105. writel(temp, regs + RCAR_USB3_DL_CTRL);
  106. for (time = 0; time < timeout; time++) {
  107. val = readl(regs + RCAR_USB3_DL_CTRL);
  108. if ((val & RCAR_USB3_DL_CTRL_FW_SET_DATA0) == 0)
  109. break;
  110. udelay(1);
  111. }
  112. if (time == timeout) {
  113. retval = -ETIMEDOUT;
  114. break;
  115. }
  116. }
  117. temp = readl(regs + RCAR_USB3_DL_CTRL);
  118. temp &= ~RCAR_USB3_DL_CTRL_ENABLE;
  119. writel(temp, regs + RCAR_USB3_DL_CTRL);
  120. for (time = 0; time < timeout; time++) {
  121. val = readl(regs + RCAR_USB3_DL_CTRL);
  122. if (val & RCAR_USB3_DL_CTRL_FW_SUCCESS) {
  123. retval = 0;
  124. break;
  125. }
  126. udelay(1);
  127. }
  128. if (time == timeout)
  129. retval = -ETIMEDOUT;
  130. release_firmware(fw);
  131. return retval;
  132. }
  133. /* This function needs to initialize a "phy" of usb before */
  134. int xhci_rcar_init_quirk(struct usb_hcd *hcd)
  135. {
  136. /* If hcd->regs is NULL, we don't just call the following function */
  137. if (!hcd->regs)
  138. return 0;
  139. return xhci_rcar_download_firmware(hcd);
  140. }