xhci-rcar.c 5.7 KB

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