]> git.sur5r.net Git - u-boot/blob - drivers/usb/host/ehci-sunxi.c
usb: sunxi: Simplify ccm reg base code
[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/arch/usb_phy.h>
15 #include <asm/io.h>
16 #include <dm.h>
17 #include "ehci.h"
18
19 #ifdef CONFIG_SUNXI_GEN_SUN4I
20 #define BASE_DIST               0x8000
21 #define AHB_CLK_DIST            2
22 #else
23 #define BASE_DIST               0x1000
24 #define AHB_CLK_DIST            1
25 #endif
26
27 struct ehci_sunxi_priv {
28         struct ehci_ctrl ehci;
29         struct sunxi_ccm_reg *ccm;
30         int ahb_gate_mask; /* Mask of ahb_gate0 clk gate bits for this hcd */
31         int phy_index;     /* Index of the usb-phy attached to this hcd */
32 };
33
34 static int ehci_usb_probe(struct udevice *dev)
35 {
36         struct usb_platdata *plat = dev_get_platdata(dev);
37         struct ehci_sunxi_priv *priv = dev_get_priv(dev);
38         struct ehci_hccr *hccr = (struct ehci_hccr *)devfdt_get_addr(dev);
39         struct ehci_hcor *hcor;
40         int extra_ahb_gate_mask = 0;
41
42         priv->ccm = (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
43         if (IS_ERR(priv->ccm))
44                 return PTR_ERR(priv->ccm);
45
46         /*
47          * This should go away once we've moved to the driver model for
48          * clocks resp. phys.
49          */
50         priv->ahb_gate_mask = 1 << AHB_GATE_OFFSET_USB_EHCI0;
51 #if defined(CONFIG_MACH_SUNXI_H3_H5) || defined(CONFIG_MACH_SUN50I)
52         extra_ahb_gate_mask = 1 << AHB_GATE_OFFSET_USB_OHCI0;
53 #endif
54         priv->phy_index = ((uintptr_t)hccr - SUNXI_USB1_BASE) / BASE_DIST;
55         priv->ahb_gate_mask <<= priv->phy_index * AHB_CLK_DIST;
56         extra_ahb_gate_mask <<= priv->phy_index * AHB_CLK_DIST;
57         priv->phy_index++; /* Non otg phys start at 1 */
58
59         setbits_le32(&priv->ccm->ahb_gate0,
60                      priv->ahb_gate_mask | extra_ahb_gate_mask);
61 #ifdef CONFIG_SUNXI_GEN_SUN6I
62         setbits_le32(&priv->ccm->ahb_reset0_cfg,
63                      priv->ahb_gate_mask | extra_ahb_gate_mask);
64 #endif
65
66         sunxi_usb_phy_init(priv->phy_index);
67         sunxi_usb_phy_power_on(priv->phy_index);
68
69         hcor = (struct ehci_hcor *)((uintptr_t)hccr +
70                                     HC_LENGTH(ehci_readl(&hccr->cr_capbase)));
71
72         return ehci_register(dev, hccr, hcor, NULL, 0, plat->init_type);
73 }
74
75 static int ehci_usb_remove(struct udevice *dev)
76 {
77         struct ehci_sunxi_priv *priv = dev_get_priv(dev);
78         int ret;
79
80         ret = ehci_deregister(dev);
81         if (ret)
82                 return ret;
83
84         sunxi_usb_phy_exit(priv->phy_index);
85
86 #ifdef CONFIG_SUNXI_GEN_SUN6I
87         clrbits_le32(&priv->ccm->ahb_reset0_cfg, priv->ahb_gate_mask);
88 #endif
89         clrbits_le32(&priv->ccm->ahb_gate0, priv->ahb_gate_mask);
90
91         return 0;
92 }
93
94 static const struct udevice_id ehci_usb_ids[] = {
95         { .compatible = "allwinner,sun4i-a10-ehci", },
96         { .compatible = "allwinner,sun5i-a13-ehci", },
97         { .compatible = "allwinner,sun6i-a31-ehci", },
98         { .compatible = "allwinner,sun7i-a20-ehci", },
99         { .compatible = "allwinner,sun8i-a23-ehci", },
100         { .compatible = "allwinner,sun8i-a83t-ehci", },
101         { .compatible = "allwinner,sun8i-h3-ehci",  },
102         { .compatible = "allwinner,sun9i-a80-ehci", },
103         { .compatible = "allwinner,sun50i-a64-ehci", },
104         { }
105 };
106
107 U_BOOT_DRIVER(ehci_sunxi) = {
108         .name   = "ehci_sunxi",
109         .id     = UCLASS_USB,
110         .of_match = ehci_usb_ids,
111         .probe = ehci_usb_probe,
112         .remove = ehci_usb_remove,
113         .ops    = &ehci_usb_ops,
114         .platdata_auto_alloc_size = sizeof(struct usb_platdata),
115         .priv_auto_alloc_size = sizeof(struct ehci_sunxi_priv),
116         .flags  = DM_FLAG_ALLOC_PRIV_DMA,
117 };