1 /*
2 * Sunxi ohci glue
3 *
4 * Copyright (C) 2015 Hans de Goede <hdegoede@redhat.com>
5 *
6 * Based on code from
7 * Allwinner Technology Co., Ltd. <www.allwinnertech.com>
8 *
9 * SPDX-License-Identifier: GPL-2.0+
10 */
11
12 #include <common.h>
13 #include <asm/arch/clock.h>
14 #include <asm/arch/usb_phy.h>
15 #include <asm/io.h>
16 #include <dm.h>
17 #include <usb.h>
18 #include "ohci.h"
19
20 #ifdef CONFIG_SUNXI_GEN_SUN4I
21 #define BASE_DIST 0x8000
22 #define AHB_CLK_DIST 2
23 #else
24 #define BASE_DIST 0x1000
25 #define AHB_CLK_DIST 1
26 #endif
27
28 struct ohci_sunxi_priv {
29 struct sunxi_ccm_reg *ccm;
30 ohci_t ohci;
31 int ahb_gate_mask; /* Mask of ahb_gate0 clk gate bits for this hcd */
32 int usb_gate_mask; /* Mask of usb_clk_cfg clk gate bits for this hcd */
33 int phy_index; /* Index of the usb-phy attached to this hcd */
34 };
35
ohci_usb_probe(struct udevice * dev)36 static int ohci_usb_probe(struct udevice *dev)
37 {
38 struct usb_bus_priv *bus_priv = dev_get_uclass_priv(dev);
39 struct ohci_sunxi_priv *priv = dev_get_priv(dev);
40 struct ohci_regs *regs = (struct ohci_regs *)devfdt_get_addr(dev);
41 int extra_ahb_gate_mask = 0;
42
43 priv->ccm = (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
44 if (IS_ERR(priv->ccm))
45 return PTR_ERR(priv->ccm);
46
47 bus_priv->companion = true;
48
49 /*
50 * This should go away once we've moved to the driver model for
51 * clocks resp. phys.
52 */
53 priv->ahb_gate_mask = 1 << AHB_GATE_OFFSET_USB_OHCI0;
54 #ifdef CONFIG_MACH_SUN8I_H3
55 extra_ahb_gate_mask = 1 << AHB_GATE_OFFSET_USB_EHCI0;
56 #endif
57 priv->usb_gate_mask = CCM_USB_CTRL_OHCI0_CLK;
58 priv->phy_index = ((uintptr_t)regs - (SUNXI_USB1_BASE + 0x400)) / BASE_DIST;
59 priv->ahb_gate_mask <<= priv->phy_index * AHB_CLK_DIST;
60 extra_ahb_gate_mask <<= priv->phy_index * AHB_CLK_DIST;
61 priv->usb_gate_mask <<= priv->phy_index;
62 priv->phy_index++; /* Non otg phys start at 1 */
63
64 setbits_le32(&priv->ccm->ahb_gate0,
65 priv->ahb_gate_mask | extra_ahb_gate_mask);
66 setbits_le32(&priv->ccm->usb_clk_cfg, priv->usb_gate_mask);
67 #ifdef CONFIG_SUNXI_GEN_SUN6I
68 setbits_le32(&priv->ccm->ahb_reset0_cfg,
69 priv->ahb_gate_mask | extra_ahb_gate_mask);
70 #endif
71
72 sunxi_usb_phy_init(priv->phy_index);
73 sunxi_usb_phy_power_on(priv->phy_index);
74
75 return ohci_register(dev, regs);
76 }
77
ohci_usb_remove(struct udevice * dev)78 static int ohci_usb_remove(struct udevice *dev)
79 {
80 struct ohci_sunxi_priv *priv = dev_get_priv(dev);
81 int ret;
82
83 ret = ohci_deregister(dev);
84 if (ret)
85 return ret;
86
87 sunxi_usb_phy_exit(priv->phy_index);
88
89 #ifdef CONFIG_SUNXI_GEN_SUN6I
90 clrbits_le32(&priv->ccm->ahb_reset0_cfg, priv->ahb_gate_mask);
91 #endif
92 clrbits_le32(&priv->ccm->usb_clk_cfg, priv->usb_gate_mask);
93 clrbits_le32(&priv->ccm->ahb_gate0, priv->ahb_gate_mask);
94
95 return 0;
96 }
97
98 static const struct udevice_id ohci_usb_ids[] = {
99 { .compatible = "allwinner,sun4i-a10-ohci", },
100 { .compatible = "allwinner,sun5i-a13-ohci", },
101 { .compatible = "allwinner,sun6i-a31-ohci", },
102 { .compatible = "allwinner,sun7i-a20-ohci", },
103 { .compatible = "allwinner,sun8i-a23-ohci", },
104 { .compatible = "allwinner,sun8i-a83t-ohci", },
105 { .compatible = "allwinner,sun8i-h3-ohci", },
106 { .compatible = "allwinner,sun9i-a80-ohci", },
107 { .compatible = "allwinner,sun50i-a64-ohci", },
108 { }
109 };
110
111 U_BOOT_DRIVER(usb_ohci) = {
112 .name = "ohci_sunxi",
113 .id = UCLASS_USB,
114 .of_match = ohci_usb_ids,
115 .probe = ohci_usb_probe,
116 .remove = ohci_usb_remove,
117 .ops = &ohci_usb_ops,
118 .platdata_auto_alloc_size = sizeof(struct usb_platdata),
119 .priv_auto_alloc_size = sizeof(struct ohci_sunxi_priv),
120 .flags = DM_FLAG_ALLOC_PRIV_DMA,
121 };
122