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 #define SUN6I_AHB_RESET0_CFG_OFFSET 0x2c0
26 #define SUN9I_AHB_RESET0_CFG_OFFSET 0x5a0
28 struct ehci_sunxi_cfg {
30 u32 extra_ahb_gate_mask;
31 u32 reset0_cfg_offset;
34 struct ehci_sunxi_priv {
35 struct ehci_ctrl ehci;
36 struct sunxi_ccm_reg *ccm;
38 int ahb_gate_mask; /* Mask of ahb_gate0 clk gate bits for this hcd */
40 const struct ehci_sunxi_cfg *cfg;
43 static int ehci_usb_probe(struct udevice *dev)
45 struct usb_platdata *plat = dev_get_platdata(dev);
46 struct ehci_sunxi_priv *priv = dev_get_priv(dev);
47 struct ehci_hccr *hccr = (struct ehci_hccr *)devfdt_get_addr(dev);
48 struct ehci_hcor *hcor;
49 int extra_ahb_gate_mask = 0;
52 priv->cfg = (const struct ehci_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);
57 priv->reset0_cfg = (void *)priv->ccm +
58 priv->cfg->reset0_cfg_offset;
60 phys = dev_count_phandle_with_args(dev, "phys", "#phy-cells");
66 ret = generic_phy_get_by_name(dev, "usb", &priv->phy);
68 pr_err("failed to get %s usb PHY\n", dev->name);
72 ret = generic_phy_init(&priv->phy);
74 pr_err("failed to init %s USB PHY\n", dev->name);
78 ret = generic_phy_power_on(&priv->phy);
80 pr_err("failed to power on %s USB PHY\n", dev->name);
86 * This should go away once we've moved to the driver model for
89 priv->ahb_gate_mask = 1 << AHB_GATE_OFFSET_USB_EHCI0;
90 extra_ahb_gate_mask = priv->cfg->extra_ahb_gate_mask;
91 priv->ahb_gate_mask <<= phys * AHB_CLK_DIST;
92 extra_ahb_gate_mask <<= phys * AHB_CLK_DIST;
94 setbits_le32(&priv->ccm->ahb_gate0,
95 priv->ahb_gate_mask | extra_ahb_gate_mask);
96 if (priv->cfg->has_reset)
97 setbits_le32(priv->reset0_cfg,
98 priv->ahb_gate_mask | extra_ahb_gate_mask);
100 hcor = (struct ehci_hcor *)((uintptr_t)hccr +
101 HC_LENGTH(ehci_readl(&hccr->cr_capbase)));
103 return ehci_register(dev, hccr, hcor, NULL, 0, plat->init_type);
106 static int ehci_usb_remove(struct udevice *dev)
108 struct ehci_sunxi_priv *priv = dev_get_priv(dev);
111 if (generic_phy_valid(&priv->phy)) {
112 ret = generic_phy_exit(&priv->phy);
114 pr_err("failed to exit %s USB PHY\n", dev->name);
119 ret = ehci_deregister(dev);
123 if (priv->cfg->has_reset)
124 clrbits_le32(priv->reset0_cfg, priv->ahb_gate_mask);
125 clrbits_le32(&priv->ccm->ahb_gate0, priv->ahb_gate_mask);
130 static const struct ehci_sunxi_cfg sun4i_a10_cfg = {
134 static const struct ehci_sunxi_cfg sun6i_a31_cfg = {
136 .reset0_cfg_offset = SUN6I_AHB_RESET0_CFG_OFFSET,
139 static const struct ehci_sunxi_cfg sun8i_h3_cfg = {
141 .extra_ahb_gate_mask = 1 << AHB_GATE_OFFSET_USB_OHCI0,
142 .reset0_cfg_offset = SUN6I_AHB_RESET0_CFG_OFFSET,
145 static const struct ehci_sunxi_cfg sun9i_a80_cfg = {
147 .reset0_cfg_offset = SUN9I_AHB_RESET0_CFG_OFFSET,
150 static const struct udevice_id ehci_usb_ids[] = {
152 .compatible = "allwinner,sun4i-a10-ehci",
153 .data = (ulong)&sun4i_a10_cfg,
156 .compatible = "allwinner,sun5i-a13-ehci",
157 .data = (ulong)&sun4i_a10_cfg,
160 .compatible = "allwinner,sun6i-a31-ehci",
161 .data = (ulong)&sun6i_a31_cfg,
164 .compatible = "allwinner,sun7i-a20-ehci",
165 .data = (ulong)&sun4i_a10_cfg,
168 .compatible = "allwinner,sun8i-a23-ehci",
169 .data = (ulong)&sun6i_a31_cfg,
172 .compatible = "allwinner,sun8i-a83t-ehci",
173 .data = (ulong)&sun6i_a31_cfg,
176 .compatible = "allwinner,sun8i-h3-ehci",
177 .data = (ulong)&sun8i_h3_cfg,
180 .compatible = "allwinner,sun9i-a80-ehci",
181 .data = (ulong)&sun9i_a80_cfg,
184 .compatible = "allwinner,sun50i-a64-ehci",
185 .data = (ulong)&sun8i_h3_cfg,
190 U_BOOT_DRIVER(ehci_sunxi) = {
191 .name = "ehci_sunxi",
193 .of_match = ehci_usb_ids,
194 .probe = ehci_usb_probe,
195 .remove = ehci_usb_remove,
196 .ops = &ehci_usb_ops,
197 .platdata_auto_alloc_size = sizeof(struct usb_platdata),
198 .priv_auto_alloc_size = sizeof(struct ehci_sunxi_priv),
199 .flags = DM_FLAG_ALLOC_PRIV_DMA,