ehci-msm.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /*
  2. * Qualcomm EHCI driver
  3. *
  4. * (C) Copyright 2015 Mateusz Kulikowski <mateusz.kulikowski@gmail.com>
  5. *
  6. * Based on Linux driver
  7. *
  8. * SPDX-License-Identifier: GPL-2.0+
  9. */
  10. #include <common.h>
  11. #include <dm.h>
  12. #include <errno.h>
  13. #include <fdtdec.h>
  14. #include <libfdt.h>
  15. #include <usb.h>
  16. #include <usb/ehci-ci.h>
  17. #include <usb/ulpi.h>
  18. #include <wait_bit.h>
  19. #include <asm/gpio.h>
  20. #include <asm/io.h>
  21. #include <linux/compat.h>
  22. #include "ehci.h"
  23. /* PHY viewport regs */
  24. #define ULPI_MISC_A_READ 0x96
  25. #define ULPI_MISC_A_SET 0x97
  26. #define ULPI_MISC_A_CLEAR 0x98
  27. #define ULPI_MISC_A_VBUSVLDEXTSEL (1 << 1)
  28. #define ULPI_MISC_A_VBUSVLDEXT (1 << 0)
  29. #define GEN2_SESS_VLD_CTRL_EN (1 << 7)
  30. #define SESS_VLD_CTRL (1 << 25)
  31. struct msm_ehci_priv {
  32. struct ehci_ctrl ctrl; /* Needed by EHCI */
  33. struct usb_ehci *ehci; /* Start of IP core*/
  34. struct ulpi_viewport ulpi_vp; /* ULPI Viewport */
  35. };
  36. int __weak board_prepare_usb(enum usb_init_type type)
  37. {
  38. return 0;
  39. }
  40. static void setup_usb_phy(struct msm_ehci_priv *priv)
  41. {
  42. /* Select and enable external configuration with USB PHY */
  43. ulpi_write(&priv->ulpi_vp, (u8 *)ULPI_MISC_A_SET,
  44. ULPI_MISC_A_VBUSVLDEXTSEL | ULPI_MISC_A_VBUSVLDEXT);
  45. }
  46. static void reset_usb_phy(struct msm_ehci_priv *priv)
  47. {
  48. /* Disable VBUS mimicing in the controller. */
  49. ulpi_write(&priv->ulpi_vp, (u8 *)ULPI_MISC_A_CLEAR,
  50. ULPI_MISC_A_VBUSVLDEXTSEL | ULPI_MISC_A_VBUSVLDEXT);
  51. }
  52. static int msm_init_after_reset(struct ehci_ctrl *dev)
  53. {
  54. struct msm_ehci_priv *p = container_of(dev, struct msm_ehci_priv, ctrl);
  55. struct usb_ehci *ehci = p->ehci;
  56. /* select ULPI phy */
  57. writel(PORT_PTS_ULPI, &ehci->portsc);
  58. setup_usb_phy(p);
  59. /* Enable sess_vld */
  60. setbits_le32(&ehci->genconfig2, GEN2_SESS_VLD_CTRL_EN);
  61. /* Enable external vbus configuration in the LINK */
  62. setbits_le32(&ehci->usbcmd, SESS_VLD_CTRL);
  63. /* USB_OTG_HS_AHB_BURST */
  64. writel(0x0, &ehci->sbuscfg);
  65. /* USB_OTG_HS_AHB_MODE: HPROT_MODE */
  66. /* Bus access related config. */
  67. writel(0x08, &ehci->sbusmode);
  68. /* set mode to host controller */
  69. writel(CM_HOST, &ehci->usbmode);
  70. return 0;
  71. }
  72. static const struct ehci_ops msm_ehci_ops = {
  73. .init_after_reset = msm_init_after_reset
  74. };
  75. static int ehci_usb_probe(struct udevice *dev)
  76. {
  77. struct msm_ehci_priv *p = dev_get_priv(dev);
  78. struct usb_ehci *ehci = p->ehci;
  79. struct ehci_hccr *hccr;
  80. struct ehci_hcor *hcor;
  81. int ret;
  82. hccr = (struct ehci_hccr *)((phys_addr_t)&ehci->caplength);
  83. hcor = (struct ehci_hcor *)((phys_addr_t)hccr +
  84. HC_LENGTH(ehci_readl(&(hccr)->cr_capbase)));
  85. ret = board_prepare_usb(USB_INIT_HOST);
  86. if (ret < 0)
  87. return ret;
  88. return ehci_register(dev, hccr, hcor, &msm_ehci_ops, 0, USB_INIT_HOST);
  89. }
  90. static int ehci_usb_remove(struct udevice *dev)
  91. {
  92. struct msm_ehci_priv *p = dev_get_priv(dev);
  93. struct usb_ehci *ehci = p->ehci;
  94. int ret;
  95. ret = ehci_deregister(dev);
  96. if (ret)
  97. return ret;
  98. /* Stop controller. */
  99. clrbits_le32(&ehci->usbcmd, CMD_RUN);
  100. reset_usb_phy(p);
  101. ret = board_prepare_usb(USB_INIT_DEVICE); /* Board specific hook */
  102. if (ret < 0)
  103. return ret;
  104. /* Reset controller */
  105. setbits_le32(&ehci->usbcmd, CMD_RESET);
  106. /* Wait for reset */
  107. if (wait_for_bit(__func__, &ehci->usbcmd, CMD_RESET, false, 30,
  108. false)) {
  109. printf("Stuck on USB reset.\n");
  110. return -ETIMEDOUT;
  111. }
  112. return 0;
  113. }
  114. static int ehci_usb_ofdata_to_platdata(struct udevice *dev)
  115. {
  116. struct msm_ehci_priv *priv = dev_get_priv(dev);
  117. priv->ulpi_vp.port_num = 0;
  118. priv->ehci = (void *)dev_get_addr(dev);
  119. if (priv->ehci == (void *)FDT_ADDR_T_NONE)
  120. return -EINVAL;
  121. /* Warning: this will not work if viewport address is > 64 bit due to
  122. * ULPI design.
  123. */
  124. priv->ulpi_vp.viewport_addr = (phys_addr_t)&priv->ehci->ulpi_viewpoint;
  125. return 0;
  126. }
  127. static const struct udevice_id ehci_usb_ids[] = {
  128. { .compatible = "qcom,ehci-host", },
  129. { }
  130. };
  131. U_BOOT_DRIVER(usb_ehci) = {
  132. .name = "ehci_msm",
  133. .id = UCLASS_USB,
  134. .of_match = ehci_usb_ids,
  135. .ofdata_to_platdata = ehci_usb_ofdata_to_platdata,
  136. .probe = ehci_usb_probe,
  137. .remove = ehci_usb_remove,
  138. .ops = &ehci_usb_ops,
  139. .priv_auto_alloc_size = sizeof(struct msm_ehci_priv),
  140. .flags = DM_FLAG_ALLOC_PRIV_DMA,
  141. };