]> git.sur5r.net Git - u-boot/blob - drivers/usb/host/ehci-sunxi.c
usb: sunxi: access ahb_reset0_cfg in CCM using its offset
[u-boot] / drivers / usb / host / ehci-sunxi.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Sunxi ehci glue
4  *
5  * Copyright (C) 2015 Hans de Goede <hdegoede@redhat.com>
6  * Copyright (C) 2014 Roman Byshko <rbyshko@gmail.com>
7  *
8  * Based on code from
9  * Allwinner Technology Co., Ltd. <www.allwinnertech.com>
10  */
11
12 #include <common.h>
13 #include <asm/arch/clock.h>
14 #include <asm/io.h>
15 #include <dm.h>
16 #include "ehci.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 ehci_sunxi_cfg {
29         bool has_reset;
30         u32 extra_ahb_gate_mask;
31         u32 reset0_cfg_offset;
32 };
33
34 struct ehci_sunxi_priv {
35         struct ehci_ctrl ehci;
36         struct sunxi_ccm_reg *ccm;
37         u32 *reset0_cfg;
38         int ahb_gate_mask; /* Mask of ahb_gate0 clk gate bits for this hcd */
39         struct phy phy;
40         const struct ehci_sunxi_cfg *cfg;
41 };
42
43 static int ehci_usb_probe(struct udevice *dev)
44 {
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;
50         int phys, ret;
51
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);
56
57         priv->reset0_cfg = (void *)priv->ccm +
58                                    priv->cfg->reset0_cfg_offset;
59
60         phys = dev_count_phandle_with_args(dev, "phys", "#phy-cells");
61         if (phys < 0) {
62                 phys = 0;
63                 goto no_phy;
64         }
65
66         ret = generic_phy_get_by_name(dev, "usb", &priv->phy);
67         if (ret) {
68                 pr_err("failed to get %s usb PHY\n", dev->name);
69                 return ret;
70         }
71
72         ret = generic_phy_init(&priv->phy);
73         if (ret) {
74                 pr_err("failed to init %s USB PHY\n", dev->name);
75                 return ret;
76         }
77
78         ret = generic_phy_power_on(&priv->phy);
79         if (ret) {
80                 pr_err("failed to power on %s USB PHY\n", dev->name);
81                 return ret;
82         }
83
84 no_phy:
85         /*
86          * This should go away once we've moved to the driver model for
87          * clocks resp. phys.
88          */
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;
93
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);
99
100         hcor = (struct ehci_hcor *)((uintptr_t)hccr +
101                                     HC_LENGTH(ehci_readl(&hccr->cr_capbase)));
102
103         return ehci_register(dev, hccr, hcor, NULL, 0, plat->init_type);
104 }
105
106 static int ehci_usb_remove(struct udevice *dev)
107 {
108         struct ehci_sunxi_priv *priv = dev_get_priv(dev);
109         int ret;
110
111         if (generic_phy_valid(&priv->phy)) {
112                 ret = generic_phy_exit(&priv->phy);
113                 if (ret) {
114                         pr_err("failed to exit %s USB PHY\n", dev->name);
115                         return ret;
116                 }
117         }
118
119         ret = ehci_deregister(dev);
120         if (ret)
121                 return ret;
122
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);
126
127         return 0;
128 }
129
130 static const struct ehci_sunxi_cfg sun4i_a10_cfg = {
131         .has_reset = false,
132 };
133
134 static const struct ehci_sunxi_cfg sun6i_a31_cfg = {
135         .has_reset = true,
136         .reset0_cfg_offset = SUN6I_AHB_RESET0_CFG_OFFSET,
137 };
138
139 static const struct ehci_sunxi_cfg sun8i_h3_cfg = {
140         .has_reset = true,
141         .extra_ahb_gate_mask = 1 << AHB_GATE_OFFSET_USB_OHCI0,
142         .reset0_cfg_offset = SUN6I_AHB_RESET0_CFG_OFFSET,
143 };
144
145 static const struct ehci_sunxi_cfg sun9i_a80_cfg = {
146         .has_reset = true,
147         .reset0_cfg_offset = SUN9I_AHB_RESET0_CFG_OFFSET,
148 };
149
150 static const struct udevice_id ehci_usb_ids[] = {
151         {
152                 .compatible = "allwinner,sun4i-a10-ehci",
153                 .data = (ulong)&sun4i_a10_cfg,
154         },
155         {
156                 .compatible = "allwinner,sun5i-a13-ehci",
157                 .data = (ulong)&sun4i_a10_cfg,
158         },
159         {
160                 .compatible = "allwinner,sun6i-a31-ehci",
161                 .data = (ulong)&sun6i_a31_cfg,
162         },
163         {
164                 .compatible = "allwinner,sun7i-a20-ehci",
165                 .data = (ulong)&sun4i_a10_cfg,
166         },
167         {
168                 .compatible = "allwinner,sun8i-a23-ehci",
169                 .data = (ulong)&sun6i_a31_cfg,
170         },
171         {
172                 .compatible = "allwinner,sun8i-a83t-ehci",
173                 .data = (ulong)&sun6i_a31_cfg,
174         },
175         {
176                 .compatible = "allwinner,sun8i-h3-ehci",
177                 .data = (ulong)&sun8i_h3_cfg,
178         },
179         {
180                 .compatible = "allwinner,sun9i-a80-ehci",
181                 .data = (ulong)&sun9i_a80_cfg,
182         },
183         {
184                 .compatible = "allwinner,sun50i-a64-ehci",
185                 .data = (ulong)&sun8i_h3_cfg,
186         },
187         { /* sentinel */ }
188 };
189
190 U_BOOT_DRIVER(ehci_sunxi) = {
191         .name   = "ehci_sunxi",
192         .id     = UCLASS_USB,
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,
200 };