xhci-rcar.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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/module.h>
  9. #include <linux/platform_device.h>
  10. #include <linux/of.h>
  11. #include <linux/usb/phy.h>
  12. #include <linux/sys_soc.h>
  13. #include "xhci.h"
  14. #include "xhci-plat.h"
  15. #include "xhci-rcar.h"
  16. /*
  17. * - The V3 firmware is for almost all R-Car Gen3 (except r8a7795 ES1.x)
  18. * - The V2 firmware is for r8a7795 ES1.x.
  19. * - The V2 firmware is possible to use on R-Car Gen2. However, the V2 causes
  20. * performance degradation. So, this driver continues to use the V1 if R-Car
  21. * Gen2.
  22. * - The V1 firmware is impossible to use on R-Car Gen3.
  23. */
  24. MODULE_FIRMWARE(XHCI_RCAR_FIRMWARE_NAME_V1);
  25. MODULE_FIRMWARE(XHCI_RCAR_FIRMWARE_NAME_V2);
  26. MODULE_FIRMWARE(XHCI_RCAR_FIRMWARE_NAME_V3);
  27. /*** Register Offset ***/
  28. #define RCAR_USB3_AXH_STA 0x104 /* AXI Host Control Status */
  29. #define RCAR_USB3_INT_ENA 0x224 /* Interrupt Enable */
  30. #define RCAR_USB3_DL_CTRL 0x250 /* FW Download Control & Status */
  31. #define RCAR_USB3_FW_DATA0 0x258 /* FW Data0 */
  32. #define RCAR_USB3_LCLK 0xa44 /* LCLK Select */
  33. #define RCAR_USB3_CONF1 0xa48 /* USB3.0 Configuration1 */
  34. #define RCAR_USB3_CONF2 0xa5c /* USB3.0 Configuration2 */
  35. #define RCAR_USB3_CONF3 0xaa8 /* USB3.0 Configuration3 */
  36. #define RCAR_USB3_RX_POL 0xab0 /* USB3.0 RX Polarity */
  37. #define RCAR_USB3_TX_POL 0xab8 /* USB3.0 TX Polarity */
  38. /*** Register Settings ***/
  39. /* AXI Host Control Status */
  40. #define RCAR_USB3_AXH_STA_B3_PLL_ACTIVE 0x00010000
  41. #define RCAR_USB3_AXH_STA_B2_PLL_ACTIVE 0x00000001
  42. #define RCAR_USB3_AXH_STA_PLL_ACTIVE_MASK (RCAR_USB3_AXH_STA_B3_PLL_ACTIVE | \
  43. RCAR_USB3_AXH_STA_B2_PLL_ACTIVE)
  44. /* Interrupt Enable */
  45. #define RCAR_USB3_INT_XHC_ENA 0x00000001
  46. #define RCAR_USB3_INT_PME_ENA 0x00000002
  47. #define RCAR_USB3_INT_HSE_ENA 0x00000004
  48. #define RCAR_USB3_INT_ENA_VAL (RCAR_USB3_INT_XHC_ENA | \
  49. RCAR_USB3_INT_PME_ENA | RCAR_USB3_INT_HSE_ENA)
  50. /* FW Download Control & Status */
  51. #define RCAR_USB3_DL_CTRL_ENABLE 0x00000001
  52. #define RCAR_USB3_DL_CTRL_FW_SUCCESS 0x00000010
  53. #define RCAR_USB3_DL_CTRL_FW_SET_DATA0 0x00000100
  54. /* LCLK Select */
  55. #define RCAR_USB3_LCLK_ENA_VAL 0x01030001
  56. /* USB3.0 Configuration */
  57. #define RCAR_USB3_CONF1_VAL 0x00030204
  58. #define RCAR_USB3_CONF2_VAL 0x00030300
  59. #define RCAR_USB3_CONF3_VAL 0x13802007
  60. /* USB3.0 Polarity */
  61. #define RCAR_USB3_RX_POL_VAL BIT(21)
  62. #define RCAR_USB3_TX_POL_VAL BIT(4)
  63. /* For soc_device_attribute */
  64. #define RCAR_XHCI_FIRMWARE_V2 BIT(0) /* FIRMWARE V2 */
  65. #define RCAR_XHCI_FIRMWARE_V3 BIT(1) /* FIRMWARE V3 */
  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, time;
  112. int timeout = 10000;
  113. u32 data, val, temp;
  114. u32 quirks = 0;
  115. const struct soc_device_attribute *attr;
  116. const char *firmware_name;
  117. /*
  118. * According to the datasheet, "Upon the completion of FW Download,
  119. * there is no need to write or reload FW".
  120. */
  121. if (readl(regs + RCAR_USB3_DL_CTRL) & RCAR_USB3_DL_CTRL_FW_SUCCESS)
  122. return 0;
  123. attr = soc_device_match(rcar_quirks_match);
  124. if (attr)
  125. quirks = (uintptr_t)attr->data;
  126. if (quirks & RCAR_XHCI_FIRMWARE_V2)
  127. firmware_name = XHCI_RCAR_FIRMWARE_NAME_V2;
  128. else if (quirks & RCAR_XHCI_FIRMWARE_V3)
  129. firmware_name = XHCI_RCAR_FIRMWARE_NAME_V3;
  130. else
  131. firmware_name = priv->firmware_name;
  132. /* request R-Car USB3.0 firmware */
  133. retval = request_firmware(&fw, firmware_name, dev);
  134. if (retval)
  135. return retval;
  136. /* download R-Car USB3.0 firmware */
  137. temp = readl(regs + RCAR_USB3_DL_CTRL);
  138. temp |= RCAR_USB3_DL_CTRL_ENABLE;
  139. writel(temp, regs + RCAR_USB3_DL_CTRL);
  140. for (index = 0; index < fw->size; index += 4) {
  141. /* to avoid reading beyond the end of the buffer */
  142. for (data = 0, j = 3; j >= 0; j--) {
  143. if ((j + index) < fw->size)
  144. data |= fw->data[index + j] << (8 * j);
  145. }
  146. writel(data, regs + RCAR_USB3_FW_DATA0);
  147. temp = readl(regs + RCAR_USB3_DL_CTRL);
  148. temp |= RCAR_USB3_DL_CTRL_FW_SET_DATA0;
  149. writel(temp, regs + RCAR_USB3_DL_CTRL);
  150. for (time = 0; time < timeout; time++) {
  151. val = readl(regs + RCAR_USB3_DL_CTRL);
  152. if ((val & RCAR_USB3_DL_CTRL_FW_SET_DATA0) == 0)
  153. break;
  154. udelay(1);
  155. }
  156. if (time == timeout) {
  157. retval = -ETIMEDOUT;
  158. break;
  159. }
  160. }
  161. temp = readl(regs + RCAR_USB3_DL_CTRL);
  162. temp &= ~RCAR_USB3_DL_CTRL_ENABLE;
  163. writel(temp, regs + RCAR_USB3_DL_CTRL);
  164. for (time = 0; time < timeout; time++) {
  165. val = readl(regs + RCAR_USB3_DL_CTRL);
  166. if (val & RCAR_USB3_DL_CTRL_FW_SUCCESS) {
  167. retval = 0;
  168. break;
  169. }
  170. udelay(1);
  171. }
  172. if (time == timeout)
  173. retval = -ETIMEDOUT;
  174. release_firmware(fw);
  175. return retval;
  176. }
  177. static bool xhci_rcar_wait_for_pll_active(struct usb_hcd *hcd)
  178. {
  179. int timeout = 1000;
  180. u32 val, mask = RCAR_USB3_AXH_STA_PLL_ACTIVE_MASK;
  181. while (timeout > 0) {
  182. val = readl(hcd->regs + RCAR_USB3_AXH_STA);
  183. if ((val & mask) == mask)
  184. return true;
  185. udelay(1);
  186. timeout--;
  187. }
  188. return false;
  189. }
  190. /* This function needs to initialize a "phy" of usb before */
  191. int xhci_rcar_init_quirk(struct usb_hcd *hcd)
  192. {
  193. /* If hcd->regs is NULL, we don't just call the following function */
  194. if (!hcd->regs)
  195. return 0;
  196. if (!xhci_rcar_wait_for_pll_active(hcd))
  197. return -ETIMEDOUT;
  198. return xhci_rcar_download_firmware(hcd);
  199. }
  200. int xhci_rcar_resume_quirk(struct usb_hcd *hcd)
  201. {
  202. int ret;
  203. ret = xhci_rcar_download_firmware(hcd);
  204. if (!ret)
  205. xhci_rcar_start(hcd);
  206. return ret;
  207. }