]> git.sur5r.net Git - u-boot/blob - drivers/usb/host/ohci-sunxi.c
usb: sunxi: ohci: make ohci_t the first member in private data
[u-boot] / drivers / usb / host / ohci-sunxi.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Sunxi ohci glue
4  *
5  * Copyright (C) 2015 Hans de Goede <hdegoede@redhat.com>
6  *
7  * Based on code from
8  * Allwinner Technology Co., Ltd. <www.allwinnertech.com>
9  */
10
11 #include <common.h>
12 #include <asm/arch/clock.h>
13 #include <asm/io.h>
14 #include <dm.h>
15 #include <usb.h>
16 #include "ohci.h"
17 #include <generic-phy.h>
18
19 #ifdef CONFIG_SUNXI_GEN_SUN4I
20 #define AHB_CLK_DIST            2
21 #else
22 #define AHB_CLK_DIST            1
23 #endif
24
25 #define SUN6I_AHB_RESET0_CFG_OFFSET 0x2c0
26 #define SUN9I_AHB_RESET0_CFG_OFFSET 0x5a0
27
28 struct ohci_sunxi_cfg {
29         bool has_reset;
30         u32 extra_ahb_gate_mask;
31         u32 extra_usb_gate_mask;
32         u32 reset0_cfg_offset;
33 };
34
35 struct ohci_sunxi_priv {
36         ohci_t ohci;
37         struct sunxi_ccm_reg *ccm;
38         u32 *reset0_cfg;
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 */
41         struct phy phy;
42         const struct ohci_sunxi_cfg *cfg;
43 };
44
45 static int ohci_usb_probe(struct udevice *dev)
46 {
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;
51         int phys, ret;
52
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);
57
58         priv->reset0_cfg = (void *)priv->ccm +
59                                    priv->cfg->reset0_cfg_offset;
60
61         phys = dev_count_phandle_with_args(dev, "phys", "#phy-cells");
62         if (phys < 0) {
63                 phys = 0;
64                 goto no_phy;
65         }
66
67         ret = generic_phy_get_by_name(dev, "usb", &priv->phy);
68         if (ret) {
69                 pr_err("failed to get %s usb PHY\n", dev->name);
70                 return ret;
71         }
72
73         ret = generic_phy_init(&priv->phy);
74         if (ret) {
75                 pr_err("failed to init %s USB PHY\n", dev->name);
76                 return ret;
77         }
78
79         ret = generic_phy_power_on(&priv->phy);
80         if (ret) {
81                 pr_err("failed to power on %s USB PHY\n", dev->name);
82                 return ret;
83         }
84
85 no_phy:
86         bus_priv->companion = true;
87
88         /*
89          * This should go away once we've moved to the driver model for
90          * clocks resp. phys.
91          */
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;
98
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);
106
107         return ohci_register(dev, regs);
108 }
109
110 static int ohci_usb_remove(struct udevice *dev)
111 {
112         struct ohci_sunxi_priv *priv = dev_get_priv(dev);
113         int ret;
114
115         if (generic_phy_valid(&priv->phy)) {
116                 ret = generic_phy_exit(&priv->phy);
117                 if (ret) {
118                         pr_err("failed to exit %s USB PHY\n", dev->name);
119                         return ret;
120                 }
121         }
122
123         ret = ohci_deregister(dev);
124         if (ret)
125                 return ret;
126
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);
131
132         return 0;
133 }
134
135 static const struct ohci_sunxi_cfg sun4i_a10_cfg = {
136         .has_reset = false,
137 };
138
139 static const struct ohci_sunxi_cfg sun6i_a31_cfg = {
140         .has_reset = true,
141         .reset0_cfg_offset = SUN6I_AHB_RESET0_CFG_OFFSET,
142 };
143
144 static const struct ohci_sunxi_cfg sun8i_h3_cfg = {
145         .has_reset = true,
146         .extra_ahb_gate_mask = 1 << AHB_GATE_OFFSET_USB_EHCI0,
147         .reset0_cfg_offset = SUN6I_AHB_RESET0_CFG_OFFSET,
148 };
149
150 static const struct ohci_sunxi_cfg sun9i_a80_cfg = {
151         .has_reset = true,
152         .reset0_cfg_offset = SUN9I_AHB_RESET0_CFG_OFFSET,
153 };
154
155 static const struct ohci_sunxi_cfg sun50i_a64_cfg = {
156         .has_reset = true,
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,
160 };
161
162 static const struct udevice_id ohci_usb_ids[] = {
163         {
164                 .compatible = "allwinner,sun4i-a10-ohci",
165                 .data = (ulong)&sun4i_a10_cfg,
166         },
167         {
168                 .compatible = "allwinner,sun5i-a13-ohci",
169                 .data = (ulong)&sun4i_a10_cfg,
170         },
171         {
172                 .compatible = "allwinner,sun6i-a31-ohci",
173                 .data = (ulong)&sun6i_a31_cfg,
174         },
175         {
176                 .compatible = "allwinner,sun7i-a20-ohci",
177                 .data = (ulong)&sun4i_a10_cfg,
178         },
179         {
180                 .compatible = "allwinner,sun8i-a23-ohci",
181                 .data = (ulong)&sun6i_a31_cfg,
182         },
183         {
184                 .compatible = "allwinner,sun8i-a83t-ohci",
185                 .data = (ulong)&sun6i_a31_cfg,
186         },
187         {
188                 .compatible = "allwinner,sun8i-h3-ohci",
189                 .data = (ulong)&sun8i_h3_cfg,
190         },
191         {
192                 .compatible = "allwinner,sun9i-a80-ohci",
193                 .data = (ulong)&sun9i_a80_cfg,
194         },
195         {
196                 .compatible = "allwinner,sun50i-a64-ohci",
197                 .data = (ulong)&sun50i_a64_cfg,
198         },
199         { /* sentinel */ }
200 };
201
202 U_BOOT_DRIVER(usb_ohci) = {
203         .name   = "ohci_sunxi",
204         .id     = UCLASS_USB,
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,
212 };