ehci-sunxi.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Sunxi ehci glue
  4. *
  5. * Copyright (C) 2015 Hans de Goede <hdegoede@redhat.com>
  6. * Copyright (C) 2014 Roman Byshko <rbyshko@gmail.com>
  7. *
  8. * Based on code from
  9. * Allwinner Technology Co., Ltd. <www.allwinnertech.com>
  10. */
  11. #include <common.h>
  12. #include <asm/arch/clock.h>
  13. #include <asm/io.h>
  14. #include <dm.h>
  15. #include "ehci.h"
  16. #include <generic-phy.h>
  17. #ifdef CONFIG_SUNXI_GEN_SUN4I
  18. #define BASE_DIST 0x8000
  19. #define AHB_CLK_DIST 2
  20. #else
  21. #define BASE_DIST 0x1000
  22. #define AHB_CLK_DIST 1
  23. #endif
  24. #define SUN6I_AHB_RESET0_CFG_OFFSET 0x2c0
  25. #define SUN9I_AHB_RESET0_CFG_OFFSET 0x5a0
  26. struct ehci_sunxi_cfg {
  27. bool has_reset;
  28. u32 extra_ahb_gate_mask;
  29. u32 reset0_cfg_offset;
  30. };
  31. struct ehci_sunxi_priv {
  32. struct ehci_ctrl ehci;
  33. struct sunxi_ccm_reg *ccm;
  34. u32 *reset0_cfg;
  35. int ahb_gate_mask; /* Mask of ahb_gate0 clk gate bits for this hcd */
  36. struct phy phy;
  37. const struct ehci_sunxi_cfg *cfg;
  38. };
  39. static int ehci_usb_probe(struct udevice *dev)
  40. {
  41. struct usb_platdata *plat = dev_get_platdata(dev);
  42. struct ehci_sunxi_priv *priv = dev_get_priv(dev);
  43. struct ehci_hccr *hccr = (struct ehci_hccr *)devfdt_get_addr(dev);
  44. struct ehci_hcor *hcor;
  45. int extra_ahb_gate_mask = 0;
  46. u8 reg_mask = 0;
  47. int phys, ret;
  48. priv->cfg = (const struct ehci_sunxi_cfg *)dev_get_driver_data(dev);
  49. priv->ccm = (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
  50. if (IS_ERR(priv->ccm))
  51. return PTR_ERR(priv->ccm);
  52. priv->reset0_cfg = (void *)priv->ccm +
  53. priv->cfg->reset0_cfg_offset;
  54. phys = dev_count_phandle_with_args(dev, "phys", "#phy-cells");
  55. if (phys < 0) {
  56. phys = 0;
  57. goto no_phy;
  58. }
  59. ret = generic_phy_get_by_name(dev, "usb", &priv->phy);
  60. if (ret) {
  61. pr_err("failed to get %s usb PHY\n", dev->name);
  62. return ret;
  63. }
  64. ret = generic_phy_init(&priv->phy);
  65. if (ret) {
  66. pr_err("failed to init %s USB PHY\n", dev->name);
  67. return ret;
  68. }
  69. ret = generic_phy_power_on(&priv->phy);
  70. if (ret) {
  71. pr_err("failed to power on %s USB PHY\n", dev->name);
  72. return ret;
  73. }
  74. no_phy:
  75. /*
  76. * This should go away once we've moved to the driver model for
  77. * clocks resp. phys.
  78. */
  79. reg_mask = ((uintptr_t)hccr - SUNXI_USB1_BASE) / BASE_DIST;
  80. priv->ahb_gate_mask = 1 << AHB_GATE_OFFSET_USB_EHCI0;
  81. extra_ahb_gate_mask = priv->cfg->extra_ahb_gate_mask;
  82. priv->ahb_gate_mask <<= reg_mask * AHB_CLK_DIST;
  83. extra_ahb_gate_mask <<= reg_mask * AHB_CLK_DIST;
  84. setbits_le32(&priv->ccm->ahb_gate0,
  85. priv->ahb_gate_mask | extra_ahb_gate_mask);
  86. if (priv->cfg->has_reset)
  87. setbits_le32(priv->reset0_cfg,
  88. priv->ahb_gate_mask | extra_ahb_gate_mask);
  89. hcor = (struct ehci_hcor *)((uintptr_t)hccr +
  90. HC_LENGTH(ehci_readl(&hccr->cr_capbase)));
  91. return ehci_register(dev, hccr, hcor, NULL, 0, plat->init_type);
  92. }
  93. static int ehci_usb_remove(struct udevice *dev)
  94. {
  95. struct ehci_sunxi_priv *priv = dev_get_priv(dev);
  96. int ret;
  97. if (generic_phy_valid(&priv->phy)) {
  98. ret = generic_phy_exit(&priv->phy);
  99. if (ret) {
  100. pr_err("failed to exit %s USB PHY\n", dev->name);
  101. return ret;
  102. }
  103. }
  104. ret = ehci_deregister(dev);
  105. if (ret)
  106. return ret;
  107. if (priv->cfg->has_reset)
  108. clrbits_le32(priv->reset0_cfg, priv->ahb_gate_mask);
  109. clrbits_le32(&priv->ccm->ahb_gate0, priv->ahb_gate_mask);
  110. return 0;
  111. }
  112. static const struct ehci_sunxi_cfg sun4i_a10_cfg = {
  113. .has_reset = false,
  114. };
  115. static const struct ehci_sunxi_cfg sun6i_a31_cfg = {
  116. .has_reset = true,
  117. .reset0_cfg_offset = SUN6I_AHB_RESET0_CFG_OFFSET,
  118. };
  119. static const struct ehci_sunxi_cfg sun8i_h3_cfg = {
  120. .has_reset = true,
  121. .extra_ahb_gate_mask = 1 << AHB_GATE_OFFSET_USB_OHCI0,
  122. .reset0_cfg_offset = SUN6I_AHB_RESET0_CFG_OFFSET,
  123. };
  124. static const struct ehci_sunxi_cfg sun9i_a80_cfg = {
  125. .has_reset = true,
  126. .reset0_cfg_offset = SUN9I_AHB_RESET0_CFG_OFFSET,
  127. };
  128. static const struct udevice_id ehci_usb_ids[] = {
  129. {
  130. .compatible = "allwinner,sun4i-a10-ehci",
  131. .data = (ulong)&sun4i_a10_cfg,
  132. },
  133. {
  134. .compatible = "allwinner,sun5i-a13-ehci",
  135. .data = (ulong)&sun4i_a10_cfg,
  136. },
  137. {
  138. .compatible = "allwinner,sun6i-a31-ehci",
  139. .data = (ulong)&sun6i_a31_cfg,
  140. },
  141. {
  142. .compatible = "allwinner,sun7i-a20-ehci",
  143. .data = (ulong)&sun4i_a10_cfg,
  144. },
  145. {
  146. .compatible = "allwinner,sun8i-a23-ehci",
  147. .data = (ulong)&sun6i_a31_cfg,
  148. },
  149. {
  150. .compatible = "allwinner,sun8i-a83t-ehci",
  151. .data = (ulong)&sun6i_a31_cfg,
  152. },
  153. {
  154. .compatible = "allwinner,sun8i-h3-ehci",
  155. .data = (ulong)&sun8i_h3_cfg,
  156. },
  157. {
  158. .compatible = "allwinner,sun9i-a80-ehci",
  159. .data = (ulong)&sun9i_a80_cfg,
  160. },
  161. {
  162. .compatible = "allwinner,sun50i-a64-ehci",
  163. .data = (ulong)&sun8i_h3_cfg,
  164. },
  165. { /* sentinel */ }
  166. };
  167. U_BOOT_DRIVER(ehci_sunxi) = {
  168. .name = "ehci_sunxi",
  169. .id = UCLASS_USB,
  170. .of_match = ehci_usb_ids,
  171. .probe = ehci_usb_probe,
  172. .remove = ehci_usb_remove,
  173. .ops = &ehci_usb_ops,
  174. .platdata_auto_alloc_size = sizeof(struct usb_platdata),
  175. .priv_auto_alloc_size = sizeof(struct ehci_sunxi_priv),
  176. .flags = DM_FLAG_ALLOC_PRIV_DMA,
  177. };