xhci-rcar.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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. static int xhci_rcar_is_gen3(struct device *dev)
  94. {
  95. struct device_node *node = dev->of_node;
  96. return of_device_is_compatible(node, "renesas,xhci-r8a7795") ||
  97. of_device_is_compatible(node, "renesas,xhci-r8a7796") ||
  98. of_device_is_compatible(node, "renesas,rcar-gen3-xhci");
  99. }
  100. void xhci_rcar_start(struct usb_hcd *hcd)
  101. {
  102. u32 temp;
  103. if (hcd->regs != NULL) {
  104. /* Interrupt Enable */
  105. temp = readl(hcd->regs + RCAR_USB3_INT_ENA);
  106. temp |= RCAR_USB3_INT_ENA_VAL;
  107. writel(temp, hcd->regs + RCAR_USB3_INT_ENA);
  108. if (xhci_rcar_is_gen2(hcd->self.controller))
  109. xhci_rcar_start_gen2(hcd);
  110. }
  111. }
  112. static int xhci_rcar_download_firmware(struct usb_hcd *hcd)
  113. {
  114. struct device *dev = hcd->self.controller;
  115. void __iomem *regs = hcd->regs;
  116. struct xhci_plat_priv *priv = hcd_to_xhci_priv(hcd);
  117. const struct firmware *fw;
  118. int retval, index, j, time;
  119. int timeout = 10000;
  120. u32 data, val, temp;
  121. u32 quirks = 0;
  122. const struct soc_device_attribute *attr;
  123. const char *firmware_name;
  124. attr = soc_device_match(rcar_quirks_match);
  125. if (attr)
  126. quirks = (uintptr_t)attr->data;
  127. if (quirks & RCAR_XHCI_FIRMWARE_V2)
  128. firmware_name = XHCI_RCAR_FIRMWARE_NAME_V2;
  129. else if (quirks & RCAR_XHCI_FIRMWARE_V3)
  130. firmware_name = XHCI_RCAR_FIRMWARE_NAME_V3;
  131. else
  132. firmware_name = priv->firmware_name;
  133. /* request R-Car USB3.0 firmware */
  134. retval = request_firmware(&fw, firmware_name, dev);
  135. if (retval)
  136. return retval;
  137. /* download R-Car USB3.0 firmware */
  138. temp = readl(regs + RCAR_USB3_DL_CTRL);
  139. temp |= RCAR_USB3_DL_CTRL_ENABLE;
  140. writel(temp, regs + RCAR_USB3_DL_CTRL);
  141. for (index = 0; index < fw->size; index += 4) {
  142. /* to avoid reading beyond the end of the buffer */
  143. for (data = 0, j = 3; j >= 0; j--) {
  144. if ((j + index) < fw->size)
  145. data |= fw->data[index + j] << (8 * j);
  146. }
  147. writel(data, regs + RCAR_USB3_FW_DATA0);
  148. temp = readl(regs + RCAR_USB3_DL_CTRL);
  149. temp |= RCAR_USB3_DL_CTRL_FW_SET_DATA0;
  150. writel(temp, regs + RCAR_USB3_DL_CTRL);
  151. for (time = 0; time < timeout; time++) {
  152. val = readl(regs + RCAR_USB3_DL_CTRL);
  153. if ((val & RCAR_USB3_DL_CTRL_FW_SET_DATA0) == 0)
  154. break;
  155. udelay(1);
  156. }
  157. if (time == timeout) {
  158. retval = -ETIMEDOUT;
  159. break;
  160. }
  161. }
  162. temp = readl(regs + RCAR_USB3_DL_CTRL);
  163. temp &= ~RCAR_USB3_DL_CTRL_ENABLE;
  164. writel(temp, regs + RCAR_USB3_DL_CTRL);
  165. for (time = 0; time < timeout; time++) {
  166. val = readl(regs + RCAR_USB3_DL_CTRL);
  167. if (val & RCAR_USB3_DL_CTRL_FW_SUCCESS) {
  168. retval = 0;
  169. break;
  170. }
  171. udelay(1);
  172. }
  173. if (time == timeout)
  174. retval = -ETIMEDOUT;
  175. release_firmware(fw);
  176. return retval;
  177. }
  178. static bool xhci_rcar_wait_for_pll_active(struct usb_hcd *hcd)
  179. {
  180. int timeout = 1000;
  181. u32 val, mask = RCAR_USB3_AXH_STA_PLL_ACTIVE_MASK;
  182. while (timeout > 0) {
  183. val = readl(hcd->regs + RCAR_USB3_AXH_STA);
  184. if ((val & mask) == mask)
  185. return true;
  186. udelay(1);
  187. timeout--;
  188. }
  189. return false;
  190. }
  191. /* This function needs to initialize a "phy" of usb before */
  192. int xhci_rcar_init_quirk(struct usb_hcd *hcd)
  193. {
  194. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  195. /* If hcd->regs is NULL, we don't just call the following function */
  196. if (!hcd->regs)
  197. return 0;
  198. /*
  199. * On R-Car Gen2 and Gen3, the AC64 bit (bit 0) of HCCPARAMS1 is set
  200. * to 1. However, these SoCs don't support 64-bit address memory
  201. * pointers. So, this driver clears the AC64 bit of xhci->hcc_params
  202. * to call dma_set_coherent_mask(dev, DMA_BIT_MASK(32)) in
  203. * xhci_gen_setup().
  204. *
  205. * And, since the firmware/internal CPU control the USBSTS.STS_HALT
  206. * and the process speed is down when the roothub port enters U3,
  207. * long delay for the handshake of STS_HALT is neeed in xhci_suspend().
  208. */
  209. if (xhci_rcar_is_gen2(hcd->self.controller) ||
  210. xhci_rcar_is_gen3(hcd->self.controller)) {
  211. xhci->quirks |= XHCI_NO_64BIT_SUPPORT | XHCI_SLOW_SUSPEND;
  212. }
  213. if (!xhci_rcar_wait_for_pll_active(hcd))
  214. return -ETIMEDOUT;
  215. xhci->quirks |= XHCI_TRUST_TX_LENGTH;
  216. return xhci_rcar_download_firmware(hcd);
  217. }
  218. int xhci_rcar_resume_quirk(struct usb_hcd *hcd)
  219. {
  220. int ret;
  221. ret = xhci_rcar_download_firmware(hcd);
  222. if (!ret)
  223. xhci_rcar_start(hcd);
  224. return ret;
  225. }