ohci-sunxi.c 4.4 KB

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