]> git.sur5r.net Git - u-boot/blob - drivers/usb/host/ohci-sunxi.c
Merge git://git.denx.de/u-boot-sunxi
[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/arch/usb_phy.h>
14 #include <asm/io.h>
15 #include <dm.h>
16 #include <usb.h>
17 #include "ohci.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 ohci_sunxi_priv {
28         ohci_t ohci;
29         int ahb_gate_mask; /* Mask of ahb_gate0 clk gate bits for this hcd */
30         int usb_gate_mask; /* Mask of usb_clk_cfg clk gate bits for this hcd */
31         int phy_index;     /* Index of the usb-phy attached to this hcd */
32 };
33
34 static int ohci_usb_probe(struct udevice *dev)
35 {
36         struct sunxi_ccm_reg *ccm = (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
37         struct usb_bus_priv *bus_priv = dev_get_uclass_priv(dev);
38         struct ohci_sunxi_priv *priv = dev_get_priv(dev);
39         struct ohci_regs *regs = (struct ohci_regs *)devfdt_get_addr(dev);
40         int extra_ahb_gate_mask = 0;
41
42         bus_priv->companion = true;
43
44         /*
45          * This should go away once we've moved to the driver model for
46          * clocks resp. phys.
47          */
48         priv->ahb_gate_mask = 1 << AHB_GATE_OFFSET_USB_OHCI0;
49 #ifdef CONFIG_MACH_SUN8I_H3
50         extra_ahb_gate_mask = 1 << AHB_GATE_OFFSET_USB_EHCI0;
51 #endif
52         priv->usb_gate_mask = CCM_USB_CTRL_OHCI0_CLK;
53         priv->phy_index = ((uintptr_t)regs - (SUNXI_USB1_BASE + 0x400)) / BASE_DIST;
54         priv->ahb_gate_mask <<= priv->phy_index * AHB_CLK_DIST;
55         extra_ahb_gate_mask <<= priv->phy_index * AHB_CLK_DIST;
56         priv->usb_gate_mask <<= priv->phy_index;
57         priv->phy_index++; /* Non otg phys start at 1 */
58
59         setbits_le32(&ccm->ahb_gate0,
60                      priv->ahb_gate_mask | extra_ahb_gate_mask);
61         setbits_le32(&ccm->usb_clk_cfg, priv->usb_gate_mask);
62 #ifdef CONFIG_SUNXI_GEN_SUN6I
63         setbits_le32(&ccm->ahb_reset0_cfg,
64                      priv->ahb_gate_mask | extra_ahb_gate_mask);
65 #endif
66
67         sunxi_usb_phy_init(priv->phy_index);
68         sunxi_usb_phy_power_on(priv->phy_index);
69
70         return ohci_register(dev, regs);
71 }
72
73 static int ohci_usb_remove(struct udevice *dev)
74 {
75         struct sunxi_ccm_reg *ccm = (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
76         struct ohci_sunxi_priv *priv = dev_get_priv(dev);
77         int ret;
78
79         ret = ohci_deregister(dev);
80         if (ret)
81                 return ret;
82
83         sunxi_usb_phy_exit(priv->phy_index);
84
85 #ifdef CONFIG_SUNXI_GEN_SUN6I
86         clrbits_le32(&ccm->ahb_reset0_cfg, priv->ahb_gate_mask);
87 #endif
88         clrbits_le32(&ccm->usb_clk_cfg, priv->usb_gate_mask);
89         clrbits_le32(&ccm->ahb_gate0, priv->ahb_gate_mask);
90
91         return 0;
92 }
93
94 static const struct udevice_id ohci_usb_ids[] = {
95         { .compatible = "allwinner,sun4i-a10-ohci", },
96         { .compatible = "allwinner,sun5i-a13-ohci", },
97         { .compatible = "allwinner,sun6i-a31-ohci", },
98         { .compatible = "allwinner,sun7i-a20-ohci", },
99         { .compatible = "allwinner,sun8i-a23-ohci", },
100         { .compatible = "allwinner,sun8i-a83t-ohci", },
101         { .compatible = "allwinner,sun8i-h3-ohci",  },
102         { .compatible = "allwinner,sun9i-a80-ohci", },
103         { .compatible = "allwinner,sun50i-a64-ohci", },
104         { }
105 };
106
107 U_BOOT_DRIVER(usb_ohci) = {
108         .name   = "ohci_sunxi",
109         .id     = UCLASS_USB,
110         .of_match = ohci_usb_ids,
111         .probe = ohci_usb_probe,
112         .remove = ohci_usb_remove,
113         .ops    = &ohci_usb_ops,
114         .platdata_auto_alloc_size = sizeof(struct usb_platdata),
115         .priv_auto_alloc_size = sizeof(struct ohci_sunxi_priv),
116         .flags  = DM_FLAG_ALLOC_PRIV_DMA,
117 };