xhci-rcar.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * xHCI host controller driver for R-Car SoCs
  4. *
  5. * Copyright (C) 2014 Renesas Electronics Corporation
  6. */
  7. #include <linux/firmware.h>
  8. #include <linux/iopoll.h>
  9. #include <linux/module.h>
  10. #include <linux/platform_device.h>
  11. #include <linux/of.h>
  12. #include <linux/usb/phy.h>
  13. #include <linux/sys_soc.h>
  14. #include "xhci.h"
  15. #include "xhci-plat.h"
  16. #include "xhci-rcar.h"
  17. /*
  18. * - The V3 firmware is for almost all R-Car Gen3 (except r8a7795 ES1.x)
  19. * - The V2 firmware is for r8a7795 ES1.x.
  20. * - The V2 firmware is possible to use on R-Car Gen2. However, the V2 causes
  21. * performance degradation. So, this driver continues to use the V1 if R-Car
  22. * Gen2.
  23. * - The V1 firmware is impossible to use on R-Car Gen3.
  24. */
  25. MODULE_FIRMWARE(XHCI_RCAR_FIRMWARE_NAME_V1);
  26. MODULE_FIRMWARE(XHCI_RCAR_FIRMWARE_NAME_V2);
  27. MODULE_FIRMWARE(XHCI_RCAR_FIRMWARE_NAME_V3);
  28. /*** Register Offset ***/
  29. #define RCAR_USB3_AXH_STA 0x104 /* AXI Host Control Status */
  30. #define RCAR_USB3_INT_ENA 0x224 /* Interrupt Enable */
  31. #define RCAR_USB3_DL_CTRL 0x250 /* FW Download Control & Status */
  32. #define RCAR_USB3_FW_DATA0 0x258 /* FW Data0 */
  33. #define RCAR_USB3_LCLK 0xa44 /* LCLK Select */
  34. #define RCAR_USB3_CONF1 0xa48 /* USB3.0 Configuration1 */
  35. #define RCAR_USB3_CONF2 0xa5c /* USB3.0 Configuration2 */
  36. #define RCAR_USB3_CONF3 0xaa8 /* USB3.0 Configuration3 */
  37. #define RCAR_USB3_RX_POL 0xab0 /* USB3.0 RX Polarity */
  38. #define RCAR_USB3_TX_POL 0xab8 /* USB3.0 TX Polarity */
  39. /*** Register Settings ***/
  40. /* AXI Host Control Status */
  41. #define RCAR_USB3_AXH_STA_B3_PLL_ACTIVE 0x00010000
  42. #define RCAR_USB3_AXH_STA_B2_PLL_ACTIVE 0x00000001
  43. #define RCAR_USB3_AXH_STA_PLL_ACTIVE_MASK (RCAR_USB3_AXH_STA_B3_PLL_ACTIVE | \
  44. RCAR_USB3_AXH_STA_B2_PLL_ACTIVE)
  45. /* Interrupt Enable */
  46. #define RCAR_USB3_INT_XHC_ENA 0x00000001
  47. #define RCAR_USB3_INT_PME_ENA 0x00000002
  48. #define RCAR_USB3_INT_HSE_ENA 0x00000004
  49. #define RCAR_USB3_INT_ENA_VAL (RCAR_USB3_INT_XHC_ENA | \
  50. RCAR_USB3_INT_PME_ENA | RCAR_USB3_INT_HSE_ENA)
  51. /* FW Download Control & Status */
  52. #define RCAR_USB3_DL_CTRL_ENABLE 0x00000001
  53. #define RCAR_USB3_DL_CTRL_FW_SUCCESS 0x00000010
  54. #define RCAR_USB3_DL_CTRL_FW_SET_DATA0 0x00000100
  55. /* LCLK Select */
  56. #define RCAR_USB3_LCLK_ENA_VAL 0x01030001
  57. /* USB3.0 Configuration */
  58. #define RCAR_USB3_CONF1_VAL 0x00030204
  59. #define RCAR_USB3_CONF2_VAL 0x00030300
  60. #define RCAR_USB3_CONF3_VAL 0x13802007
  61. /* USB3.0 Polarity */
  62. #define RCAR_USB3_RX_POL_VAL BIT(21)
  63. #define RCAR_USB3_TX_POL_VAL BIT(4)
  64. /* For soc_device_attribute */
  65. #define RCAR_XHCI_FIRMWARE_V2 BIT(0) /* FIRMWARE V2 */
  66. static const struct soc_device_attribute rcar_quirks_match[] = {
  67. {
  68. .soc_id = "r8a7795", .revision = "ES1.*",
  69. .data = (void *)RCAR_XHCI_FIRMWARE_V2,
  70. },
  71. { /* sentinel */ }
  72. };
  73. static void xhci_rcar_start_gen2(struct usb_hcd *hcd)
  74. {
  75. /* LCLK Select */
  76. writel(RCAR_USB3_LCLK_ENA_VAL, hcd->regs + RCAR_USB3_LCLK);
  77. /* USB3.0 Configuration */
  78. writel(RCAR_USB3_CONF1_VAL, hcd->regs + RCAR_USB3_CONF1);
  79. writel(RCAR_USB3_CONF2_VAL, hcd->regs + RCAR_USB3_CONF2);
  80. writel(RCAR_USB3_CONF3_VAL, hcd->regs + RCAR_USB3_CONF3);
  81. /* USB3.0 Polarity */
  82. writel(RCAR_USB3_RX_POL_VAL, hcd->regs + RCAR_USB3_RX_POL);
  83. writel(RCAR_USB3_TX_POL_VAL, hcd->regs + RCAR_USB3_TX_POL);
  84. }
  85. static int xhci_rcar_is_gen2(struct device *dev)
  86. {
  87. struct device_node *node = dev->of_node;
  88. return of_device_is_compatible(node, "renesas,xhci-r8a7790") ||
  89. of_device_is_compatible(node, "renesas,xhci-r8a7791") ||
  90. of_device_is_compatible(node, "renesas,xhci-r8a7793") ||
  91. of_device_is_compatible(node, "renesas,rcar-gen2-xhci");
  92. }
  93. void xhci_rcar_start(struct usb_hcd *hcd)
  94. {
  95. u32 temp;
  96. if (hcd->regs != NULL) {
  97. /* Interrupt Enable */
  98. temp = readl(hcd->regs + RCAR_USB3_INT_ENA);
  99. temp |= RCAR_USB3_INT_ENA_VAL;
  100. writel(temp, hcd->regs + RCAR_USB3_INT_ENA);
  101. if (xhci_rcar_is_gen2(hcd->self.controller))
  102. xhci_rcar_start_gen2(hcd);
  103. }
  104. }
  105. static int xhci_rcar_download_firmware(struct usb_hcd *hcd)
  106. {
  107. struct device *dev = hcd->self.controller;
  108. void __iomem *regs = hcd->regs;
  109. struct xhci_plat_priv *priv = hcd_to_xhci_priv(hcd);
  110. const struct firmware *fw;
  111. int retval, index, j;
  112. u32 data, val, temp;
  113. u32 quirks = 0;
  114. const struct soc_device_attribute *attr;
  115. const char *firmware_name;
  116. /*
  117. * According to the datasheet, "Upon the completion of FW Download,
  118. * there is no need to write or reload FW".
  119. */
  120. if (readl(regs + RCAR_USB3_DL_CTRL) & RCAR_USB3_DL_CTRL_FW_SUCCESS)
  121. return 0;
  122. attr = soc_device_match(rcar_quirks_match);
  123. if (attr)
  124. quirks = (uintptr_t)attr->data;
  125. if (quirks & RCAR_XHCI_FIRMWARE_V2)
  126. firmware_name = XHCI_RCAR_FIRMWARE_NAME_V2;
  127. else
  128. firmware_name = priv->firmware_name;
  129. /* request R-Car USB3.0 firmware */
  130. retval = request_firmware(&fw, firmware_name, dev);
  131. if (retval)
  132. return retval;
  133. /* download R-Car USB3.0 firmware */
  134. temp = readl(regs + RCAR_USB3_DL_CTRL);
  135. temp |= RCAR_USB3_DL_CTRL_ENABLE;
  136. writel(temp, regs + RCAR_USB3_DL_CTRL);
  137. for (index = 0; index < fw->size; index += 4) {
  138. /* to avoid reading beyond the end of the buffer */
  139. for (data = 0, j = 3; j >= 0; j--) {
  140. if ((j + index) < fw->size)
  141. data |= fw->data[index + j] << (8 * j);
  142. }
  143. writel(data, regs + RCAR_USB3_FW_DATA0);
  144. temp = readl(regs + RCAR_USB3_DL_CTRL);
  145. temp |= RCAR_USB3_DL_CTRL_FW_SET_DATA0;
  146. writel(temp, regs + RCAR_USB3_DL_CTRL);
  147. retval = readl_poll_timeout_atomic(regs + RCAR_USB3_DL_CTRL,
  148. val, !(val & RCAR_USB3_DL_CTRL_FW_SET_DATA0),
  149. 1, 10000);
  150. if (retval < 0)
  151. break;
  152. }
  153. temp = readl(regs + RCAR_USB3_DL_CTRL);
  154. temp &= ~RCAR_USB3_DL_CTRL_ENABLE;
  155. writel(temp, regs + RCAR_USB3_DL_CTRL);
  156. retval = readl_poll_timeout_atomic((regs + RCAR_USB3_DL_CTRL),
  157. val, val & RCAR_USB3_DL_CTRL_FW_SUCCESS, 1, 10000);
  158. release_firmware(fw);
  159. return retval;
  160. }
  161. static bool xhci_rcar_wait_for_pll_active(struct usb_hcd *hcd)
  162. {
  163. int retval;
  164. u32 val, mask = RCAR_USB3_AXH_STA_PLL_ACTIVE_MASK;
  165. retval = readl_poll_timeout_atomic(hcd->regs + RCAR_USB3_AXH_STA,
  166. val, (val & mask) == mask, 1, 1000);
  167. return !retval;
  168. }
  169. /* This function needs to initialize a "phy" of usb before */
  170. int xhci_rcar_init_quirk(struct usb_hcd *hcd)
  171. {
  172. /* If hcd->regs is NULL, we don't just call the following function */
  173. if (!hcd->regs)
  174. return 0;
  175. if (!xhci_rcar_wait_for_pll_active(hcd))
  176. return -ETIMEDOUT;
  177. return xhci_rcar_download_firmware(hcd);
  178. }
  179. int xhci_rcar_resume_quirk(struct usb_hcd *hcd)
  180. {
  181. int ret;
  182. ret = xhci_rcar_download_firmware(hcd);
  183. if (!ret)
  184. xhci_rcar_start(hcd);
  185. return ret;
  186. }