]> git.sur5r.net Git - u-boot/blob - drivers/usb/host/ehci-sunxi.c
usb: sunxi: ehci: get rid of ifdefs
[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 struct ehci_sunxi_cfg {
26         bool has_reset;
27         u32 extra_ahb_gate_mask;
28 };
29
30 struct ehci_sunxi_priv {
31         struct ehci_ctrl ehci;
32         struct sunxi_ccm_reg *ccm;
33         int ahb_gate_mask; /* Mask of ahb_gate0 clk gate bits for this hcd */
34         struct phy phy;
35         const struct ehci_sunxi_cfg *cfg;
36 };
37
38 static int ehci_usb_probe(struct udevice *dev)
39 {
40         struct usb_platdata *plat = dev_get_platdata(dev);
41         struct ehci_sunxi_priv *priv = dev_get_priv(dev);
42         struct ehci_hccr *hccr = (struct ehci_hccr *)devfdt_get_addr(dev);
43         struct ehci_hcor *hcor;
44         int extra_ahb_gate_mask = 0;
45         int phys, ret;
46
47         priv->cfg = (const struct ehci_sunxi_cfg *)dev_get_driver_data(dev);
48         priv->ccm = (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
49         if (IS_ERR(priv->ccm))
50                 return PTR_ERR(priv->ccm);
51
52         phys = dev_count_phandle_with_args(dev, "phys", "#phy-cells");
53         if (phys < 0) {
54                 phys = 0;
55                 goto no_phy;
56         }
57
58         ret = generic_phy_get_by_name(dev, "usb", &priv->phy);
59         if (ret) {
60                 pr_err("failed to get %s usb PHY\n", dev->name);
61                 return ret;
62         }
63
64         ret = generic_phy_init(&priv->phy);
65         if (ret) {
66                 pr_err("failed to init %s USB PHY\n", dev->name);
67                 return ret;
68         }
69
70         ret = generic_phy_power_on(&priv->phy);
71         if (ret) {
72                 pr_err("failed to power on %s USB PHY\n", dev->name);
73                 return ret;
74         }
75
76 no_phy:
77         /*
78          * This should go away once we've moved to the driver model for
79          * clocks resp. phys.
80          */
81         priv->ahb_gate_mask = 1 << AHB_GATE_OFFSET_USB_EHCI0;
82         extra_ahb_gate_mask = priv->cfg->extra_ahb_gate_mask;
83         priv->ahb_gate_mask <<= phys * AHB_CLK_DIST;
84         extra_ahb_gate_mask <<= phys * AHB_CLK_DIST;
85
86         setbits_le32(&priv->ccm->ahb_gate0,
87                      priv->ahb_gate_mask | extra_ahb_gate_mask);
88         if (priv->cfg->has_reset)
89                 setbits_le32(&priv->ccm->ahb_reset0_cfg,
90                              priv->ahb_gate_mask | extra_ahb_gate_mask);
91
92         hcor = (struct ehci_hcor *)((uintptr_t)hccr +
93                                     HC_LENGTH(ehci_readl(&hccr->cr_capbase)));
94
95         return ehci_register(dev, hccr, hcor, NULL, 0, plat->init_type);
96 }
97
98 static int ehci_usb_remove(struct udevice *dev)
99 {
100         struct ehci_sunxi_priv *priv = dev_get_priv(dev);
101         int ret;
102
103         if (generic_phy_valid(&priv->phy)) {
104                 ret = generic_phy_exit(&priv->phy);
105                 if (ret) {
106                         pr_err("failed to exit %s USB PHY\n", dev->name);
107                         return ret;
108                 }
109         }
110
111         ret = ehci_deregister(dev);
112         if (ret)
113                 return ret;
114
115         if (priv->cfg->has_reset)
116                 clrbits_le32(&priv->ccm->ahb_reset0_cfg, priv->ahb_gate_mask);
117         clrbits_le32(&priv->ccm->ahb_gate0, priv->ahb_gate_mask);
118
119         return 0;
120 }
121
122 static const struct ehci_sunxi_cfg sun4i_a10_cfg = {
123         .has_reset = false,
124 };
125
126 static const struct ehci_sunxi_cfg sun6i_a31_cfg = {
127         .has_reset = true,
128 };
129
130 static const struct ehci_sunxi_cfg sun8i_h3_cfg = {
131         .has_reset = true,
132         .extra_ahb_gate_mask = 1 << AHB_GATE_OFFSET_USB_OHCI0,
133 };
134
135 static const struct udevice_id ehci_usb_ids[] = {
136         {
137                 .compatible = "allwinner,sun4i-a10-ehci",
138                 .data = (ulong)&sun4i_a10_cfg,
139         },
140         {
141                 .compatible = "allwinner,sun5i-a13-ehci",
142                 .data = (ulong)&sun4i_a10_cfg,
143         },
144         {
145                 .compatible = "allwinner,sun6i-a31-ehci",
146                 .data = (ulong)&sun6i_a31_cfg,
147         },
148         {
149                 .compatible = "allwinner,sun7i-a20-ehci",
150                 .data = (ulong)&sun4i_a10_cfg,
151         },
152         {
153                 .compatible = "allwinner,sun8i-a23-ehci",
154                 .data = (ulong)&sun6i_a31_cfg,
155         },
156         {
157                 .compatible = "allwinner,sun8i-a83t-ehci",
158                 .data = (ulong)&sun6i_a31_cfg,
159         },
160         {
161                 .compatible = "allwinner,sun8i-h3-ehci",
162                 .data = (ulong)&sun8i_h3_cfg,
163         },
164         {
165                 .compatible = "allwinner,sun9i-a80-ehci",
166                 .data = (ulong)&sun6i_a31_cfg,
167         },
168         {
169                 .compatible = "allwinner,sun50i-a64-ehci",
170                 .data = (ulong)&sun8i_h3_cfg,
171         },
172         { /* sentinel */ }
173 };
174
175 U_BOOT_DRIVER(ehci_sunxi) = {
176         .name   = "ehci_sunxi",
177         .id     = UCLASS_USB,
178         .of_match = ehci_usb_ids,
179         .probe = ehci_usb_probe,
180         .remove = ehci_usb_remove,
181         .ops    = &ehci_usb_ops,
182         .platdata_auto_alloc_size = sizeof(struct usb_platdata),
183         .priv_auto_alloc_size = sizeof(struct ehci_sunxi_priv),
184         .flags  = DM_FLAG_ALLOC_PRIV_DMA,
185 };