xhci-rcar.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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/of.h>
  14. #include <linux/usb/phy.h>
  15. #include "xhci.h"
  16. #include "xhci-plat.h"
  17. #include "xhci-rcar.h"
  18. /*
  19. * - The V3 firmware is for r8a7796 (with good performance).
  20. * - The V2 firmware can be used on both r8a7795 (es1.x) and r8a7796.
  21. * - The V2 firmware is possible to use on R-Car Gen2. However, the V2 causes
  22. * performance degradation. So, this driver continues to use the V1 if R-Car
  23. * Gen2.
  24. * - The V1 firmware is impossible to use on R-Car Gen3.
  25. */
  26. MODULE_FIRMWARE(XHCI_RCAR_FIRMWARE_NAME_V1);
  27. MODULE_FIRMWARE(XHCI_RCAR_FIRMWARE_NAME_V2);
  28. MODULE_FIRMWARE(XHCI_RCAR_FIRMWARE_NAME_V3);
  29. /*** Register Offset ***/
  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. /* Interrupt Enable */
  41. #define RCAR_USB3_INT_XHC_ENA 0x00000001
  42. #define RCAR_USB3_INT_PME_ENA 0x00000002
  43. #define RCAR_USB3_INT_HSE_ENA 0x00000004
  44. #define RCAR_USB3_INT_ENA_VAL (RCAR_USB3_INT_XHC_ENA | \
  45. RCAR_USB3_INT_PME_ENA | RCAR_USB3_INT_HSE_ENA)
  46. /* FW Download Control & Status */
  47. #define RCAR_USB3_DL_CTRL_ENABLE 0x00000001
  48. #define RCAR_USB3_DL_CTRL_FW_SUCCESS 0x00000010
  49. #define RCAR_USB3_DL_CTRL_FW_SET_DATA0 0x00000100
  50. /* LCLK Select */
  51. #define RCAR_USB3_LCLK_ENA_VAL 0x01030001
  52. /* USB3.0 Configuration */
  53. #define RCAR_USB3_CONF1_VAL 0x00030204
  54. #define RCAR_USB3_CONF2_VAL 0x00030300
  55. #define RCAR_USB3_CONF3_VAL 0x13802007
  56. /* USB3.0 Polarity */
  57. #define RCAR_USB3_RX_POL_VAL BIT(21)
  58. #define RCAR_USB3_TX_POL_VAL BIT(4)
  59. static void xhci_rcar_start_gen2(struct usb_hcd *hcd)
  60. {
  61. /* LCLK Select */
  62. writel(RCAR_USB3_LCLK_ENA_VAL, hcd->regs + RCAR_USB3_LCLK);
  63. /* USB3.0 Configuration */
  64. writel(RCAR_USB3_CONF1_VAL, hcd->regs + RCAR_USB3_CONF1);
  65. writel(RCAR_USB3_CONF2_VAL, hcd->regs + RCAR_USB3_CONF2);
  66. writel(RCAR_USB3_CONF3_VAL, hcd->regs + RCAR_USB3_CONF3);
  67. /* USB3.0 Polarity */
  68. writel(RCAR_USB3_RX_POL_VAL, hcd->regs + RCAR_USB3_RX_POL);
  69. writel(RCAR_USB3_TX_POL_VAL, hcd->regs + RCAR_USB3_TX_POL);
  70. }
  71. static int xhci_rcar_is_gen2(struct device *dev)
  72. {
  73. struct device_node *node = dev->of_node;
  74. return of_device_is_compatible(node, "renesas,xhci-r8a7790") ||
  75. of_device_is_compatible(node, "renesas,xhci-r8a7791") ||
  76. of_device_is_compatible(node, "renesas,xhci-r8a7793") ||
  77. of_device_is_compatible(node, "renensas,rcar-gen2-xhci");
  78. }
  79. static int xhci_rcar_is_gen3(struct device *dev)
  80. {
  81. struct device_node *node = dev->of_node;
  82. return of_device_is_compatible(node, "renesas,xhci-r8a7795") ||
  83. of_device_is_compatible(node, "renesas,xhci-r8a7796") ||
  84. of_device_is_compatible(node, "renesas,rcar-gen3-xhci");
  85. }
  86. void xhci_rcar_start(struct usb_hcd *hcd)
  87. {
  88. u32 temp;
  89. if (hcd->regs != NULL) {
  90. /* Interrupt Enable */
  91. temp = readl(hcd->regs + RCAR_USB3_INT_ENA);
  92. temp |= RCAR_USB3_INT_ENA_VAL;
  93. writel(temp, hcd->regs + RCAR_USB3_INT_ENA);
  94. if (xhci_rcar_is_gen2(hcd->self.controller))
  95. xhci_rcar_start_gen2(hcd);
  96. }
  97. }
  98. static int xhci_rcar_download_firmware(struct usb_hcd *hcd)
  99. {
  100. struct device *dev = hcd->self.controller;
  101. void __iomem *regs = hcd->regs;
  102. struct xhci_plat_priv *priv = hcd_to_xhci_priv(hcd);
  103. const struct firmware *fw;
  104. int retval, index, j, time;
  105. int timeout = 10000;
  106. u32 data, val, temp;
  107. /* request R-Car USB3.0 firmware */
  108. retval = request_firmware(&fw, priv->firmware_name, dev);
  109. if (retval)
  110. return retval;
  111. /* download R-Car USB3.0 firmware */
  112. temp = readl(regs + RCAR_USB3_DL_CTRL);
  113. temp |= RCAR_USB3_DL_CTRL_ENABLE;
  114. writel(temp, regs + RCAR_USB3_DL_CTRL);
  115. for (index = 0; index < fw->size; index += 4) {
  116. /* to avoid reading beyond the end of the buffer */
  117. for (data = 0, j = 3; j >= 0; j--) {
  118. if ((j + index) < fw->size)
  119. data |= fw->data[index + j] << (8 * j);
  120. }
  121. writel(data, regs + RCAR_USB3_FW_DATA0);
  122. temp = readl(regs + RCAR_USB3_DL_CTRL);
  123. temp |= RCAR_USB3_DL_CTRL_FW_SET_DATA0;
  124. writel(temp, regs + RCAR_USB3_DL_CTRL);
  125. for (time = 0; time < timeout; time++) {
  126. val = readl(regs + RCAR_USB3_DL_CTRL);
  127. if ((val & RCAR_USB3_DL_CTRL_FW_SET_DATA0) == 0)
  128. break;
  129. udelay(1);
  130. }
  131. if (time == timeout) {
  132. retval = -ETIMEDOUT;
  133. break;
  134. }
  135. }
  136. temp = readl(regs + RCAR_USB3_DL_CTRL);
  137. temp &= ~RCAR_USB3_DL_CTRL_ENABLE;
  138. writel(temp, regs + RCAR_USB3_DL_CTRL);
  139. for (time = 0; time < timeout; time++) {
  140. val = readl(regs + RCAR_USB3_DL_CTRL);
  141. if (val & RCAR_USB3_DL_CTRL_FW_SUCCESS) {
  142. retval = 0;
  143. break;
  144. }
  145. udelay(1);
  146. }
  147. if (time == timeout)
  148. retval = -ETIMEDOUT;
  149. release_firmware(fw);
  150. return retval;
  151. }
  152. /* This function needs to initialize a "phy" of usb before */
  153. int xhci_rcar_init_quirk(struct usb_hcd *hcd)
  154. {
  155. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  156. /* If hcd->regs is NULL, we don't just call the following function */
  157. if (!hcd->regs)
  158. return 0;
  159. /*
  160. * On R-Car Gen2 and Gen3, the AC64 bit (bit 0) of HCCPARAMS1 is set
  161. * to 1. However, these SoCs don't support 64-bit address memory
  162. * pointers. So, this driver clears the AC64 bit of xhci->hcc_params
  163. * to call dma_set_coherent_mask(dev, DMA_BIT_MASK(32)) in
  164. * xhci_gen_setup().
  165. */
  166. if (xhci_rcar_is_gen2(hcd->self.controller) ||
  167. xhci_rcar_is_gen3(hcd->self.controller))
  168. xhci->quirks |= XHCI_NO_64BIT_SUPPORT;
  169. return xhci_rcar_download_firmware(hcd);
  170. }