1 // SPDX-License-Identifier: GPL-2.0+
5 * Copyright (C) 2015 Hans de Goede <hdegoede@redhat.com>
6 * Copyright (C) 2014 Roman Byshko <rbyshko@gmail.com>
9 * Allwinner Technology Co., Ltd. <www.allwinnertech.com>
13 #include <asm/arch/clock.h>
17 #include <generic-phy.h>
19 #ifdef CONFIG_SUNXI_GEN_SUN4I
20 #define AHB_CLK_DIST 2
22 #define AHB_CLK_DIST 1
25 struct ehci_sunxi_cfg {
27 u32 extra_ahb_gate_mask;
30 struct ehci_sunxi_priv {
31 struct ehci_ctrl ehci;
32 struct sunxi_ccm_reg *ccm;
33 int ahb_gate_mask; /* Mask of ahb_gate0 clk gate bits for this hcd */
35 const struct ehci_sunxi_cfg *cfg;
38 static int ehci_usb_probe(struct udevice *dev)
40 struct usb_platdata *plat = dev_get_platdata(dev);
41 struct ehci_sunxi_priv *priv = dev_get_priv(dev);
42 struct ehci_hccr *hccr = (struct ehci_hccr *)devfdt_get_addr(dev);
43 struct ehci_hcor *hcor;
44 int extra_ahb_gate_mask = 0;
47 priv->cfg = (const struct ehci_sunxi_cfg *)dev_get_driver_data(dev);
48 priv->ccm = (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
49 if (IS_ERR(priv->ccm))
50 return PTR_ERR(priv->ccm);
52 phys = dev_count_phandle_with_args(dev, "phys", "#phy-cells");
58 ret = generic_phy_get_by_name(dev, "usb", &priv->phy);
60 pr_err("failed to get %s usb PHY\n", dev->name);
64 ret = generic_phy_init(&priv->phy);
66 pr_err("failed to init %s USB PHY\n", dev->name);
70 ret = generic_phy_power_on(&priv->phy);
72 pr_err("failed to power on %s USB PHY\n", dev->name);
78 * This should go away once we've moved to the driver model for
81 priv->ahb_gate_mask = 1 << AHB_GATE_OFFSET_USB_EHCI0;
82 extra_ahb_gate_mask = priv->cfg->extra_ahb_gate_mask;
83 priv->ahb_gate_mask <<= phys * AHB_CLK_DIST;
84 extra_ahb_gate_mask <<= phys * AHB_CLK_DIST;
86 setbits_le32(&priv->ccm->ahb_gate0,
87 priv->ahb_gate_mask | extra_ahb_gate_mask);
88 if (priv->cfg->has_reset)
89 setbits_le32(&priv->ccm->ahb_reset0_cfg,
90 priv->ahb_gate_mask | extra_ahb_gate_mask);
92 hcor = (struct ehci_hcor *)((uintptr_t)hccr +
93 HC_LENGTH(ehci_readl(&hccr->cr_capbase)));
95 return ehci_register(dev, hccr, hcor, NULL, 0, plat->init_type);
98 static int ehci_usb_remove(struct udevice *dev)
100 struct ehci_sunxi_priv *priv = dev_get_priv(dev);
103 if (generic_phy_valid(&priv->phy)) {
104 ret = generic_phy_exit(&priv->phy);
106 pr_err("failed to exit %s USB PHY\n", dev->name);
111 ret = ehci_deregister(dev);
115 if (priv->cfg->has_reset)
116 clrbits_le32(&priv->ccm->ahb_reset0_cfg, priv->ahb_gate_mask);
117 clrbits_le32(&priv->ccm->ahb_gate0, priv->ahb_gate_mask);
122 static const struct ehci_sunxi_cfg sun4i_a10_cfg = {
126 static const struct ehci_sunxi_cfg sun6i_a31_cfg = {
130 static const struct ehci_sunxi_cfg sun8i_h3_cfg = {
132 .extra_ahb_gate_mask = 1 << AHB_GATE_OFFSET_USB_OHCI0,
135 static const struct udevice_id ehci_usb_ids[] = {
137 .compatible = "allwinner,sun4i-a10-ehci",
138 .data = (ulong)&sun4i_a10_cfg,
141 .compatible = "allwinner,sun5i-a13-ehci",
142 .data = (ulong)&sun4i_a10_cfg,
145 .compatible = "allwinner,sun6i-a31-ehci",
146 .data = (ulong)&sun6i_a31_cfg,
149 .compatible = "allwinner,sun7i-a20-ehci",
150 .data = (ulong)&sun4i_a10_cfg,
153 .compatible = "allwinner,sun8i-a23-ehci",
154 .data = (ulong)&sun6i_a31_cfg,
157 .compatible = "allwinner,sun8i-a83t-ehci",
158 .data = (ulong)&sun6i_a31_cfg,
161 .compatible = "allwinner,sun8i-h3-ehci",
162 .data = (ulong)&sun8i_h3_cfg,
165 .compatible = "allwinner,sun9i-a80-ehci",
166 .data = (ulong)&sun6i_a31_cfg,
169 .compatible = "allwinner,sun50i-a64-ehci",
170 .data = (ulong)&sun8i_h3_cfg,
175 U_BOOT_DRIVER(ehci_sunxi) = {
176 .name = "ehci_sunxi",
178 .of_match = ehci_usb_ids,
179 .probe = ehci_usb_probe,
180 .remove = ehci_usb_remove,
181 .ops = &ehci_usb_ops,
182 .platdata_auto_alloc_size = sizeof(struct usb_platdata),
183 .priv_auto_alloc_size = sizeof(struct ehci_sunxi_priv),
184 .flags = DM_FLAG_ALLOC_PRIV_DMA,