ehci-fsl.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /*
  2. * (C) Copyright 2009, 2011 Freescale Semiconductor, Inc.
  3. *
  4. * (C) Copyright 2008, Excito Elektronik i Sk=E5ne AB
  5. *
  6. * Author: Tor Krill tor@excito.com
  7. *
  8. * SPDX-License-Identifier: GPL-2.0+
  9. */
  10. #include <common.h>
  11. #include <pci.h>
  12. #include <usb.h>
  13. #include <asm/io.h>
  14. #include <usb/ehci-ci.h>
  15. #include <hwconfig.h>
  16. #include <fsl_usb.h>
  17. #include <fdt_support.h>
  18. #include "ehci.h"
  19. #ifndef CONFIG_USB_MAX_CONTROLLER_COUNT
  20. #define CONFIG_USB_MAX_CONTROLLER_COUNT 1
  21. #endif
  22. static void set_txfifothresh(struct usb_ehci *, u32);
  23. /* Check USB PHY clock valid */
  24. static int usb_phy_clk_valid(struct usb_ehci *ehci)
  25. {
  26. if (!((in_be32(&ehci->control) & PHY_CLK_VALID) ||
  27. in_be32(&ehci->prictrl))) {
  28. printf("USB PHY clock invalid!\n");
  29. return 0;
  30. } else {
  31. return 1;
  32. }
  33. }
  34. /*
  35. * Create the appropriate control structures to manage
  36. * a new EHCI host controller.
  37. *
  38. * Excerpts from linux ehci fsl driver.
  39. */
  40. int ehci_hcd_init(int index, enum usb_init_type init,
  41. struct ehci_hccr **hccr, struct ehci_hcor **hcor)
  42. {
  43. struct usb_ehci *ehci = NULL;
  44. const char *phy_type = NULL;
  45. size_t len;
  46. char current_usb_controller[5];
  47. #ifdef CONFIG_SYS_FSL_USB_INTERNAL_UTMI_PHY
  48. char usb_phy[5];
  49. usb_phy[0] = '\0';
  50. #endif
  51. if (has_erratum_a007075()) {
  52. /*
  53. * A 5ms delay is needed after applying soft-reset to the
  54. * controller to let external ULPI phy come out of reset.
  55. * This delay needs to be added before re-initializing
  56. * the controller after soft-resetting completes
  57. */
  58. mdelay(5);
  59. }
  60. memset(current_usb_controller, '\0', 5);
  61. snprintf(current_usb_controller, sizeof(current_usb_controller),
  62. "usb%d", index+1);
  63. switch (index) {
  64. case 0:
  65. ehci = (struct usb_ehci *)CONFIG_SYS_FSL_USB1_ADDR;
  66. break;
  67. case 1:
  68. ehci = (struct usb_ehci *)CONFIG_SYS_FSL_USB2_ADDR;
  69. break;
  70. default:
  71. printf("ERROR: wrong controller index!!\n");
  72. return -EINVAL;
  73. };
  74. *hccr = (struct ehci_hccr *)((uint32_t)&ehci->caplength);
  75. *hcor = (struct ehci_hcor *)((uint32_t) *hccr +
  76. HC_LENGTH(ehci_readl(&(*hccr)->cr_capbase)));
  77. /* Set to Host mode */
  78. setbits_le32(&ehci->usbmode, CM_HOST);
  79. out_be32(&ehci->snoop1, SNOOP_SIZE_2GB);
  80. out_be32(&ehci->snoop2, 0x80000000 | SNOOP_SIZE_2GB);
  81. /* Init phy */
  82. if (hwconfig_sub(current_usb_controller, "phy_type"))
  83. phy_type = hwconfig_subarg(current_usb_controller,
  84. "phy_type", &len);
  85. else
  86. phy_type = getenv("usb_phy_type");
  87. if (!phy_type) {
  88. #ifdef CONFIG_SYS_FSL_USB_INTERNAL_UTMI_PHY
  89. /* if none specified assume internal UTMI */
  90. strcpy(usb_phy, "utmi");
  91. phy_type = usb_phy;
  92. #else
  93. printf("WARNING: USB phy type not defined !!\n");
  94. return -1;
  95. #endif
  96. }
  97. if (!strncmp(phy_type, "utmi", 4)) {
  98. #if defined(CONFIG_SYS_FSL_USB_INTERNAL_UTMI_PHY)
  99. clrsetbits_be32(&ehci->control, CONTROL_REGISTER_W1C_MASK,
  100. PHY_CLK_SEL_UTMI);
  101. clrsetbits_be32(&ehci->control, CONTROL_REGISTER_W1C_MASK,
  102. UTMI_PHY_EN);
  103. udelay(1000); /* delay required for PHY Clk to appear */
  104. #endif
  105. out_le32(&(*hcor)->or_portsc[0], PORT_PTS_UTMI);
  106. clrsetbits_be32(&ehci->control, CONTROL_REGISTER_W1C_MASK,
  107. USB_EN);
  108. } else {
  109. clrsetbits_be32(&ehci->control, CONTROL_REGISTER_W1C_MASK,
  110. PHY_CLK_SEL_ULPI);
  111. clrsetbits_be32(&ehci->control, UTMI_PHY_EN |
  112. CONTROL_REGISTER_W1C_MASK, USB_EN);
  113. udelay(1000); /* delay required for PHY Clk to appear */
  114. if (!usb_phy_clk_valid(ehci))
  115. return -EINVAL;
  116. out_le32(&(*hcor)->or_portsc[0], PORT_PTS_ULPI);
  117. }
  118. out_be32(&ehci->prictrl, 0x0000000c);
  119. out_be32(&ehci->age_cnt_limit, 0x00000040);
  120. out_be32(&ehci->sictrl, 0x00000001);
  121. in_le32(&ehci->usbmode);
  122. if (has_erratum_a007798())
  123. set_txfifothresh(ehci, TXFIFOTHRESH);
  124. if (has_erratum_a004477()) {
  125. /*
  126. * When reset is issued while any ULPI transaction is ongoing
  127. * then it may result to corruption of ULPI Function Control
  128. * Register which eventually causes phy clock to enter low
  129. * power mode which stops the clock. Thus delay is required
  130. * before reset to let ongoing ULPI transaction complete.
  131. */
  132. udelay(1);
  133. }
  134. return 0;
  135. }
  136. /*
  137. * Destroy the appropriate control structures corresponding
  138. * the the EHCI host controller.
  139. */
  140. int ehci_hcd_stop(int index)
  141. {
  142. return 0;
  143. }
  144. /*
  145. * Setting the value of TXFIFO_THRESH field in TXFILLTUNING register
  146. * to counter DDR latencies in writing data into Tx buffer.
  147. * This prevents Tx buffer from getting underrun
  148. */
  149. static void set_txfifothresh(struct usb_ehci *ehci, u32 txfifo_thresh)
  150. {
  151. u32 cmd;
  152. cmd = ehci_readl(&ehci->txfilltuning);
  153. cmd &= ~TXFIFO_THRESH_MASK;
  154. cmd |= TXFIFO_THRESH(txfifo_thresh);
  155. ehci_writel(&ehci->txfilltuning, cmd);
  156. }