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_priv {
26 struct ehci_ctrl ehci;
27 struct sunxi_ccm_reg *ccm;
28 int ahb_gate_mask; /* Mask of ahb_gate0 clk gate bits for this hcd */
32 static int ehci_usb_probe(struct udevice *dev)
34 struct usb_platdata *plat = dev_get_platdata(dev);
35 struct ehci_sunxi_priv *priv = dev_get_priv(dev);
36 struct ehci_hccr *hccr = (struct ehci_hccr *)devfdt_get_addr(dev);
37 struct ehci_hcor *hcor;
38 int extra_ahb_gate_mask = 0;
41 priv->ccm = (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
42 if (IS_ERR(priv->ccm))
43 return PTR_ERR(priv->ccm);
45 phys = dev_count_phandle_with_args(dev, "phys", "#phy-cells");
51 ret = generic_phy_get_by_name(dev, "usb", &priv->phy);
53 pr_err("failed to get %s usb PHY\n", dev->name);
57 ret = generic_phy_init(&priv->phy);
59 pr_err("failed to init %s USB PHY\n", dev->name);
63 ret = generic_phy_power_on(&priv->phy);
65 pr_err("failed to power on %s USB PHY\n", dev->name);
71 * This should go away once we've moved to the driver model for
74 priv->ahb_gate_mask = 1 << AHB_GATE_OFFSET_USB_EHCI0;
75 #if defined(CONFIG_MACH_SUNXI_H3_H5) || defined(CONFIG_MACH_SUN50I)
76 extra_ahb_gate_mask = 1 << AHB_GATE_OFFSET_USB_OHCI0;
78 priv->ahb_gate_mask <<= phys * AHB_CLK_DIST;
79 extra_ahb_gate_mask <<= phys * AHB_CLK_DIST;
81 setbits_le32(&priv->ccm->ahb_gate0,
82 priv->ahb_gate_mask | extra_ahb_gate_mask);
83 #ifdef CONFIG_SUNXI_GEN_SUN6I
84 setbits_le32(&priv->ccm->ahb_reset0_cfg,
85 priv->ahb_gate_mask | extra_ahb_gate_mask);
88 hcor = (struct ehci_hcor *)((uintptr_t)hccr +
89 HC_LENGTH(ehci_readl(&hccr->cr_capbase)));
91 return ehci_register(dev, hccr, hcor, NULL, 0, plat->init_type);
94 static int ehci_usb_remove(struct udevice *dev)
96 struct ehci_sunxi_priv *priv = dev_get_priv(dev);
99 if (generic_phy_valid(&priv->phy)) {
100 ret = generic_phy_exit(&priv->phy);
102 pr_err("failed to exit %s USB PHY\n", dev->name);
107 ret = ehci_deregister(dev);
111 #ifdef CONFIG_SUNXI_GEN_SUN6I
112 clrbits_le32(&priv->ccm->ahb_reset0_cfg, priv->ahb_gate_mask);
114 clrbits_le32(&priv->ccm->ahb_gate0, priv->ahb_gate_mask);
119 static const struct udevice_id ehci_usb_ids[] = {
120 { .compatible = "allwinner,sun4i-a10-ehci", },
121 { .compatible = "allwinner,sun5i-a13-ehci", },
122 { .compatible = "allwinner,sun6i-a31-ehci", },
123 { .compatible = "allwinner,sun7i-a20-ehci", },
124 { .compatible = "allwinner,sun8i-a23-ehci", },
125 { .compatible = "allwinner,sun8i-a83t-ehci", },
126 { .compatible = "allwinner,sun8i-h3-ehci", },
127 { .compatible = "allwinner,sun9i-a80-ehci", },
128 { .compatible = "allwinner,sun50i-a64-ehci", },
132 U_BOOT_DRIVER(ehci_sunxi) = {
133 .name = "ehci_sunxi",
135 .of_match = ehci_usb_ids,
136 .probe = ehci_usb_probe,
137 .remove = ehci_usb_remove,
138 .ops = &ehci_usb_ops,
139 .platdata_auto_alloc_size = sizeof(struct usb_platdata),
140 .priv_auto_alloc_size = sizeof(struct ehci_sunxi_priv),
141 .flags = DM_FLAG_ALLOC_PRIV_DMA,