ohci-sunxi.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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 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 ohci_sunxi_cfg {
  27. bool has_reset;
  28. u32 extra_ahb_gate_mask;
  29. u32 extra_usb_gate_mask;
  30. u32 reset0_cfg_offset;
  31. };
  32. struct ohci_sunxi_priv {
  33. ohci_t ohci;
  34. struct sunxi_ccm_reg *ccm;
  35. u32 *reset0_cfg;
  36. int ahb_gate_mask; /* Mask of ahb_gate0 clk gate bits for this hcd */
  37. int usb_gate_mask; /* Mask of usb_clk_cfg clk gate bits for this hcd */
  38. struct phy phy;
  39. const struct ohci_sunxi_cfg *cfg;
  40. };
  41. static fdt_addr_t last_ohci_addr = 0;
  42. static int ohci_usb_probe(struct udevice *dev)
  43. {
  44. struct usb_bus_priv *bus_priv = dev_get_uclass_priv(dev);
  45. struct ohci_sunxi_priv *priv = dev_get_priv(dev);
  46. struct ohci_regs *regs = (struct ohci_regs *)devfdt_get_addr(dev);
  47. int extra_ahb_gate_mask = 0;
  48. u8 reg_mask = 0;
  49. int phys, ret;
  50. if ((fdt_addr_t)regs > last_ohci_addr)
  51. last_ohci_addr = (fdt_addr_t)regs;
  52. priv->cfg = (const struct ohci_sunxi_cfg *)dev_get_driver_data(dev);
  53. priv->ccm = (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
  54. if (IS_ERR(priv->ccm))
  55. return PTR_ERR(priv->ccm);
  56. priv->reset0_cfg = (void *)priv->ccm +
  57. priv->cfg->reset0_cfg_offset;
  58. phys = dev_count_phandle_with_args(dev, "phys", "#phy-cells");
  59. if (phys < 0) {
  60. phys = 0;
  61. goto no_phy;
  62. }
  63. ret = generic_phy_get_by_name(dev, "usb", &priv->phy);
  64. if (ret) {
  65. pr_err("failed to get %s usb PHY\n", dev->name);
  66. return ret;
  67. }
  68. ret = generic_phy_init(&priv->phy);
  69. if (ret) {
  70. pr_err("failed to init %s USB PHY\n", dev->name);
  71. return ret;
  72. }
  73. ret = generic_phy_power_on(&priv->phy);
  74. if (ret) {
  75. pr_err("failed to power on %s USB PHY\n", dev->name);
  76. return ret;
  77. }
  78. no_phy:
  79. bus_priv->companion = true;
  80. /*
  81. * This should go away once we've moved to the driver model for
  82. * clocks resp. phys.
  83. */
  84. reg_mask = ((uintptr_t)regs - (SUNXI_USB1_BASE + 0x400)) / BASE_DIST;
  85. priv->ahb_gate_mask = 1 << AHB_GATE_OFFSET_USB_OHCI0;
  86. extra_ahb_gate_mask = priv->cfg->extra_ahb_gate_mask;
  87. priv->usb_gate_mask = CCM_USB_CTRL_OHCI0_CLK;
  88. priv->ahb_gate_mask <<= reg_mask * AHB_CLK_DIST;
  89. extra_ahb_gate_mask <<= reg_mask * AHB_CLK_DIST;
  90. priv->usb_gate_mask <<= reg_mask;
  91. setbits_le32(&priv->ccm->ahb_gate0,
  92. priv->ahb_gate_mask | extra_ahb_gate_mask);
  93. setbits_le32(&priv->ccm->usb_clk_cfg,
  94. priv->usb_gate_mask | priv->cfg->extra_usb_gate_mask);
  95. if (priv->cfg->has_reset)
  96. setbits_le32(priv->reset0_cfg,
  97. priv->ahb_gate_mask | extra_ahb_gate_mask);
  98. return ohci_register(dev, regs);
  99. }
  100. static int ohci_usb_remove(struct udevice *dev)
  101. {
  102. struct ohci_sunxi_priv *priv = dev_get_priv(dev);
  103. fdt_addr_t base_addr = devfdt_get_addr(dev);
  104. int ret;
  105. if (generic_phy_valid(&priv->phy)) {
  106. ret = generic_phy_exit(&priv->phy);
  107. if (ret) {
  108. pr_err("failed to exit %s USB PHY\n", dev->name);
  109. return ret;
  110. }
  111. }
  112. ret = ohci_deregister(dev);
  113. if (ret)
  114. return ret;
  115. if (priv->cfg->has_reset)
  116. clrbits_le32(priv->reset0_cfg, priv->ahb_gate_mask);
  117. /*
  118. * On the A64 CLK_USB_OHCI0 is the parent of CLK_USB_OHCI1, so
  119. * we have to wait with bringing down any clock until the last
  120. * OHCI controller is removed.
  121. */
  122. if (!priv->cfg->extra_usb_gate_mask || base_addr == last_ohci_addr) {
  123. u32 usb_gate_mask = priv->usb_gate_mask;
  124. usb_gate_mask |= priv->cfg->extra_usb_gate_mask;
  125. clrbits_le32(&priv->ccm->usb_clk_cfg, usb_gate_mask);
  126. }
  127. clrbits_le32(&priv->ccm->ahb_gate0, priv->ahb_gate_mask);
  128. return 0;
  129. }
  130. static const struct ohci_sunxi_cfg sun4i_a10_cfg = {
  131. .has_reset = false,
  132. };
  133. static const struct ohci_sunxi_cfg sun6i_a31_cfg = {
  134. .has_reset = true,
  135. .reset0_cfg_offset = SUN6I_AHB_RESET0_CFG_OFFSET,
  136. };
  137. static const struct ohci_sunxi_cfg sun8i_h3_cfg = {
  138. .has_reset = true,
  139. .extra_ahb_gate_mask = 1 << AHB_GATE_OFFSET_USB_EHCI0,
  140. .reset0_cfg_offset = SUN6I_AHB_RESET0_CFG_OFFSET,
  141. };
  142. static const struct ohci_sunxi_cfg sun9i_a80_cfg = {
  143. .has_reset = true,
  144. .reset0_cfg_offset = SUN9I_AHB_RESET0_CFG_OFFSET,
  145. };
  146. static const struct ohci_sunxi_cfg sun50i_a64_cfg = {
  147. .has_reset = true,
  148. .extra_ahb_gate_mask = 1 << AHB_GATE_OFFSET_USB_EHCI0,
  149. .extra_usb_gate_mask = CCM_USB_CTRL_OHCI0_CLK,
  150. .reset0_cfg_offset = SUN6I_AHB_RESET0_CFG_OFFSET,
  151. };
  152. static const struct udevice_id ohci_usb_ids[] = {
  153. {
  154. .compatible = "allwinner,sun4i-a10-ohci",
  155. .data = (ulong)&sun4i_a10_cfg,
  156. },
  157. {
  158. .compatible = "allwinner,sun5i-a13-ohci",
  159. .data = (ulong)&sun4i_a10_cfg,
  160. },
  161. {
  162. .compatible = "allwinner,sun6i-a31-ohci",
  163. .data = (ulong)&sun6i_a31_cfg,
  164. },
  165. {
  166. .compatible = "allwinner,sun7i-a20-ohci",
  167. .data = (ulong)&sun4i_a10_cfg,
  168. },
  169. {
  170. .compatible = "allwinner,sun8i-a23-ohci",
  171. .data = (ulong)&sun6i_a31_cfg,
  172. },
  173. {
  174. .compatible = "allwinner,sun8i-a83t-ohci",
  175. .data = (ulong)&sun6i_a31_cfg,
  176. },
  177. {
  178. .compatible = "allwinner,sun8i-h3-ohci",
  179. .data = (ulong)&sun8i_h3_cfg,
  180. },
  181. {
  182. .compatible = "allwinner,sun9i-a80-ohci",
  183. .data = (ulong)&sun9i_a80_cfg,
  184. },
  185. {
  186. .compatible = "allwinner,sun50i-a64-ohci",
  187. .data = (ulong)&sun50i_a64_cfg,
  188. },
  189. { /* sentinel */ }
  190. };
  191. U_BOOT_DRIVER(usb_ohci) = {
  192. .name = "ohci_sunxi",
  193. .id = UCLASS_USB,
  194. .of_match = ohci_usb_ids,
  195. .probe = ohci_usb_probe,
  196. .remove = ohci_usb_remove,
  197. .ops = &ohci_usb_ops,
  198. .platdata_auto_alloc_size = sizeof(struct usb_platdata),
  199. .priv_auto_alloc_size = sizeof(struct ohci_sunxi_priv),
  200. .flags = DM_FLAG_ALLOC_PRIV_DMA,
  201. };