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