xhci-fsl.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /*
  2. * Copyright 2015,2016 Freescale Semiconductor, Inc.
  3. *
  4. * FSL USB HOST xHCI Controller
  5. *
  6. * Author: Ramneek Mehresh<ramneek.mehresh@freescale.com>
  7. *
  8. * SPDX-License-Identifier: GPL-2.0+
  9. */
  10. #include <common.h>
  11. #include <usb.h>
  12. #include <asm-generic/errno.h>
  13. #include <linux/compat.h>
  14. #include <linux/usb/xhci-fsl.h>
  15. #include <linux/usb/dwc3.h>
  16. #include "xhci.h"
  17. #include <fsl_errata.h>
  18. #include <fsl_usb.h>
  19. #include <dm.h>
  20. /* Declare global data pointer */
  21. DECLARE_GLOBAL_DATA_PTR;
  22. #ifndef CONFIG_DM_USB
  23. static struct fsl_xhci fsl_xhci;
  24. unsigned long ctr_addr[] = FSL_USB_XHCI_ADDR;
  25. #else
  26. struct xhci_fsl_priv {
  27. struct xhci_ctrl xhci;
  28. fdt_addr_t hcd_base;
  29. struct fsl_xhci ctx;
  30. };
  31. #endif
  32. __weak int __board_usb_init(int index, enum usb_init_type init)
  33. {
  34. return 0;
  35. }
  36. static int erratum_a008751(void)
  37. {
  38. #if defined(CONFIG_TARGET_LS2080AQDS) || defined(CONFIG_TARGET_LS2080ARDB)
  39. u32 __iomem *scfg = (u32 __iomem *)SCFG_BASE;
  40. writel(SCFG_USB3PRM1CR_INIT, scfg + SCFG_USB3PRM1CR / 4);
  41. return 0;
  42. #endif
  43. return 1;
  44. }
  45. static void fsl_apply_xhci_errata(void)
  46. {
  47. int ret;
  48. if (has_erratum_a008751()) {
  49. ret = erratum_a008751();
  50. if (ret != 0)
  51. puts("Failed to apply erratum a008751\n");
  52. }
  53. }
  54. static int fsl_xhci_core_init(struct fsl_xhci *fsl_xhci)
  55. {
  56. int ret = 0;
  57. ret = dwc3_core_init(fsl_xhci->dwc3_reg);
  58. if (ret) {
  59. debug("%s:failed to initialize core\n", __func__);
  60. return ret;
  61. }
  62. /* We are hard-coding DWC3 core to Host Mode */
  63. dwc3_set_mode(fsl_xhci->dwc3_reg, DWC3_GCTL_PRTCAP_HOST);
  64. /* Set GFLADJ_30MHZ as 20h as per XHCI spec default value */
  65. dwc3_set_fladj(fsl_xhci->dwc3_reg, GFLADJ_30MHZ_DEFAULT);
  66. return ret;
  67. }
  68. static int fsl_xhci_core_exit(struct fsl_xhci *fsl_xhci)
  69. {
  70. /*
  71. * Currently fsl socs do not support PHY shutdown from
  72. * sw. But this support may be added in future socs.
  73. */
  74. return 0;
  75. }
  76. #ifdef CONFIG_DM_USB
  77. static int xhci_fsl_probe(struct udevice *dev)
  78. {
  79. struct xhci_fsl_priv *priv = dev_get_priv(dev);
  80. struct xhci_hccr *hccr;
  81. struct xhci_hcor *hcor;
  82. int ret = 0;
  83. /*
  84. * Get the base address for XHCI controller from the device node
  85. */
  86. priv->hcd_base = dev_get_addr(dev);
  87. if (priv->hcd_base == FDT_ADDR_T_NONE) {
  88. debug("Can't get the XHCI register base address\n");
  89. return -ENXIO;
  90. }
  91. priv->ctx.hcd = (struct xhci_hccr *)priv->hcd_base;
  92. priv->ctx.dwc3_reg = (struct dwc3 *)((char *)(priv->hcd_base) +
  93. DWC3_REG_OFFSET);
  94. fsl_apply_xhci_errata();
  95. ret = fsl_xhci_core_init(&priv->ctx);
  96. if (ret < 0) {
  97. puts("Failed to initialize xhci\n");
  98. return ret;
  99. }
  100. hccr = (struct xhci_hccr *)(priv->ctx.hcd);
  101. hcor = (struct xhci_hcor *)((uintptr_t) hccr
  102. + HC_LENGTH(xhci_readl(&hccr->cr_capbase)));
  103. debug("xhci-fsl: init hccr %lx and hcor %lx hc_length %lx\n",
  104. (uintptr_t)hccr, (uintptr_t)hcor,
  105. (uintptr_t)HC_LENGTH(xhci_readl(&hccr->cr_capbase)));
  106. return xhci_register(dev, hccr, hcor);
  107. }
  108. static int xhci_fsl_remove(struct udevice *dev)
  109. {
  110. struct xhci_fsl_priv *priv = dev_get_priv(dev);
  111. int ret;
  112. fsl_xhci_core_exit(&priv->ctx);
  113. ret = xhci_deregister(dev);
  114. if (ret)
  115. return ret;
  116. return 0;
  117. }
  118. static const struct udevice_id xhci_usb_ids[] = {
  119. { .compatible = "fsl,layerscape-dwc3", },
  120. { }
  121. };
  122. U_BOOT_DRIVER(xhci_fsl) = {
  123. .name = "xhci_fsl",
  124. .id = UCLASS_USB,
  125. .of_match = xhci_usb_ids,
  126. .probe = xhci_fsl_probe,
  127. .remove = xhci_fsl_remove,
  128. .ops = &xhci_usb_ops,
  129. .platdata_auto_alloc_size = sizeof(struct usb_platdata),
  130. .priv_auto_alloc_size = sizeof(struct xhci_fsl_priv),
  131. .flags = DM_FLAG_ALLOC_PRIV_DMA,
  132. };
  133. #else
  134. int xhci_hcd_init(int index, struct xhci_hccr **hccr, struct xhci_hcor **hcor)
  135. {
  136. struct fsl_xhci *ctx = &fsl_xhci;
  137. int ret = 0;
  138. ctx->hcd = (struct xhci_hccr *)ctr_addr[index];
  139. ctx->dwc3_reg = (struct dwc3 *)((char *)(ctx->hcd) + DWC3_REG_OFFSET);
  140. ret = board_usb_init(index, USB_INIT_HOST);
  141. if (ret != 0) {
  142. puts("Failed to initialize board for USB\n");
  143. return ret;
  144. }
  145. fsl_apply_xhci_errata();
  146. ret = fsl_xhci_core_init(ctx);
  147. if (ret < 0) {
  148. puts("Failed to initialize xhci\n");
  149. return ret;
  150. }
  151. *hccr = (struct xhci_hccr *)ctx->hcd;
  152. *hcor = (struct xhci_hcor *)((uintptr_t) *hccr
  153. + HC_LENGTH(xhci_readl(&(*hccr)->cr_capbase)));
  154. debug("fsl-xhci: init hccr %lx and hcor %lx hc_length %lx\n",
  155. (uintptr_t)*hccr, (uintptr_t)*hcor,
  156. (uintptr_t)HC_LENGTH(xhci_readl(&(*hccr)->cr_capbase)));
  157. return ret;
  158. }
  159. void xhci_hcd_stop(int index)
  160. {
  161. struct fsl_xhci *ctx = &fsl_xhci;
  162. fsl_xhci_core_exit(ctx);
  163. }
  164. #endif