1 // SPDX-License-Identifier: GPL-2.0+
5 * Copyright (C) 2015 Hans de Goede <hdegoede@redhat.com>
8 * Allwinner Technology Co., Ltd. <www.allwinnertech.com>
12 #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 ohci_sunxi_cfg {
30 u32 extra_ahb_gate_mask;
31 u32 extra_usb_gate_mask;
32 u32 reset0_cfg_offset;
35 struct ohci_sunxi_priv {
37 struct sunxi_ccm_reg *ccm;
39 int ahb_gate_mask; /* Mask of ahb_gate0 clk gate bits for this hcd */
40 int usb_gate_mask; /* Mask of usb_clk_cfg clk gate bits for this hcd */
42 const struct ohci_sunxi_cfg *cfg;
45 static int ohci_usb_probe(struct udevice *dev)
47 struct usb_bus_priv *bus_priv = dev_get_uclass_priv(dev);
48 struct ohci_sunxi_priv *priv = dev_get_priv(dev);
49 struct ohci_regs *regs = (struct ohci_regs *)devfdt_get_addr(dev);
50 int extra_ahb_gate_mask = 0;
53 priv->cfg = (const struct ohci_sunxi_cfg *)dev_get_driver_data(dev);
54 priv->ccm = (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
55 if (IS_ERR(priv->ccm))
56 return PTR_ERR(priv->ccm);
58 priv->reset0_cfg = (void *)priv->ccm +
59 priv->cfg->reset0_cfg_offset;
61 phys = dev_count_phandle_with_args(dev, "phys", "#phy-cells");
67 ret = generic_phy_get_by_name(dev, "usb", &priv->phy);
69 pr_err("failed to get %s usb PHY\n", dev->name);
73 ret = generic_phy_init(&priv->phy);
75 pr_err("failed to init %s USB PHY\n", dev->name);
79 ret = generic_phy_power_on(&priv->phy);
81 pr_err("failed to power on %s USB PHY\n", dev->name);
86 bus_priv->companion = true;
89 * This should go away once we've moved to the driver model for
92 priv->ahb_gate_mask = 1 << AHB_GATE_OFFSET_USB_OHCI0;
93 extra_ahb_gate_mask = priv->cfg->extra_ahb_gate_mask;
94 priv->usb_gate_mask = CCM_USB_CTRL_OHCI0_CLK;
95 priv->ahb_gate_mask <<= phys * AHB_CLK_DIST;
96 extra_ahb_gate_mask <<= phys * AHB_CLK_DIST;
97 priv->usb_gate_mask <<= phys;
99 setbits_le32(&priv->ccm->ahb_gate0,
100 priv->ahb_gate_mask | extra_ahb_gate_mask);
101 setbits_le32(&priv->ccm->usb_clk_cfg,
102 priv->usb_gate_mask | priv->cfg->extra_usb_gate_mask);
103 if (priv->cfg->has_reset)
104 setbits_le32(priv->reset0_cfg,
105 priv->ahb_gate_mask | extra_ahb_gate_mask);
107 return ohci_register(dev, regs);
110 static int ohci_usb_remove(struct udevice *dev)
112 struct ohci_sunxi_priv *priv = dev_get_priv(dev);
115 if (generic_phy_valid(&priv->phy)) {
116 ret = generic_phy_exit(&priv->phy);
118 pr_err("failed to exit %s USB PHY\n", dev->name);
123 ret = ohci_deregister(dev);
127 if (priv->cfg->has_reset)
128 clrbits_le32(priv->reset0_cfg, priv->ahb_gate_mask);
129 clrbits_le32(&priv->ccm->usb_clk_cfg, priv->usb_gate_mask);
130 clrbits_le32(&priv->ccm->ahb_gate0, priv->ahb_gate_mask);
135 static const struct ohci_sunxi_cfg sun4i_a10_cfg = {
139 static const struct ohci_sunxi_cfg sun6i_a31_cfg = {
141 .reset0_cfg_offset = SUN6I_AHB_RESET0_CFG_OFFSET,
144 static const struct ohci_sunxi_cfg sun8i_h3_cfg = {
146 .extra_ahb_gate_mask = 1 << AHB_GATE_OFFSET_USB_EHCI0,
147 .reset0_cfg_offset = SUN6I_AHB_RESET0_CFG_OFFSET,
150 static const struct ohci_sunxi_cfg sun9i_a80_cfg = {
152 .reset0_cfg_offset = SUN9I_AHB_RESET0_CFG_OFFSET,
155 static const struct ohci_sunxi_cfg sun50i_a64_cfg = {
157 .extra_ahb_gate_mask = 1 << AHB_GATE_OFFSET_USB_EHCI0,
158 .extra_usb_gate_mask = CCM_USB_CTRL_OHCI0_CLK,
159 .reset0_cfg_offset = SUN6I_AHB_RESET0_CFG_OFFSET,
162 static const struct udevice_id ohci_usb_ids[] = {
164 .compatible = "allwinner,sun4i-a10-ohci",
165 .data = (ulong)&sun4i_a10_cfg,
168 .compatible = "allwinner,sun5i-a13-ohci",
169 .data = (ulong)&sun4i_a10_cfg,
172 .compatible = "allwinner,sun6i-a31-ohci",
173 .data = (ulong)&sun6i_a31_cfg,
176 .compatible = "allwinner,sun7i-a20-ohci",
177 .data = (ulong)&sun4i_a10_cfg,
180 .compatible = "allwinner,sun8i-a23-ohci",
181 .data = (ulong)&sun6i_a31_cfg,
184 .compatible = "allwinner,sun8i-a83t-ohci",
185 .data = (ulong)&sun6i_a31_cfg,
188 .compatible = "allwinner,sun8i-h3-ohci",
189 .data = (ulong)&sun8i_h3_cfg,
192 .compatible = "allwinner,sun9i-a80-ohci",
193 .data = (ulong)&sun9i_a80_cfg,
196 .compatible = "allwinner,sun50i-a64-ohci",
197 .data = (ulong)&sun50i_a64_cfg,
202 U_BOOT_DRIVER(usb_ohci) = {
203 .name = "ohci_sunxi",
205 .of_match = ohci_usb_ids,
206 .probe = ohci_usb_probe,
207 .remove = ohci_usb_remove,
208 .ops = &ohci_usb_ops,
209 .platdata_auto_alloc_size = sizeof(struct usb_platdata),
210 .priv_auto_alloc_size = sizeof(struct ohci_sunxi_priv),
211 .flags = DM_FLAG_ALLOC_PRIV_DMA,