]> git.sur5r.net Git - u-boot/blob - drivers/usb/host/ohci-sunxi.c
usb: sunxi: Switch to use generic-phy
[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 struct ohci_sunxi_priv {
26         struct sunxi_ccm_reg *ccm;
27         ohci_t ohci;
28         int ahb_gate_mask; /* Mask of ahb_gate0 clk gate bits for this hcd */
29         int usb_gate_mask; /* Mask of usb_clk_cfg clk gate bits for this hcd */
30         struct phy phy;
31 };
32
33 static int ohci_usb_probe(struct udevice *dev)
34 {
35         struct usb_bus_priv *bus_priv = dev_get_uclass_priv(dev);
36         struct ohci_sunxi_priv *priv = dev_get_priv(dev);
37         struct ohci_regs *regs = (struct ohci_regs *)devfdt_get_addr(dev);
38         int extra_ahb_gate_mask = 0;
39         int phys, ret;
40
41         priv->ccm = (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
42         if (IS_ERR(priv->ccm))
43                 return PTR_ERR(priv->ccm);
44
45         phys = dev_count_phandle_with_args(dev, "phys", "#phy-cells");
46         if (phys < 0) {
47                 phys = 0;
48                 goto no_phy;
49         }
50
51         ret = generic_phy_get_by_name(dev, "usb", &priv->phy);
52         if (ret) {
53                 pr_err("failed to get %s usb PHY\n", dev->name);
54                 return ret;
55         }
56
57         ret = generic_phy_init(&priv->phy);
58         if (ret) {
59                 pr_err("failed to init %s USB PHY\n", dev->name);
60                 return ret;
61         }
62
63         ret = generic_phy_power_on(&priv->phy);
64         if (ret) {
65                 pr_err("failed to power on %s USB PHY\n", dev->name);
66                 return ret;
67         }
68
69 no_phy:
70         bus_priv->companion = true;
71
72         /*
73          * This should go away once we've moved to the driver model for
74          * clocks resp. phys.
75          */
76         priv->ahb_gate_mask = 1 << AHB_GATE_OFFSET_USB_OHCI0;
77 #ifdef CONFIG_MACH_SUN8I_H3
78         extra_ahb_gate_mask = 1 << AHB_GATE_OFFSET_USB_EHCI0;
79 #endif
80         priv->usb_gate_mask = CCM_USB_CTRL_OHCI0_CLK;
81         priv->ahb_gate_mask <<= phys * AHB_CLK_DIST;
82         extra_ahb_gate_mask <<= phys * AHB_CLK_DIST;
83         priv->usb_gate_mask <<= phys;
84
85         setbits_le32(&priv->ccm->ahb_gate0,
86                      priv->ahb_gate_mask | extra_ahb_gate_mask);
87         setbits_le32(&priv->ccm->usb_clk_cfg, priv->usb_gate_mask);
88 #ifdef CONFIG_SUNXI_GEN_SUN6I
89         setbits_le32(&priv->ccm->ahb_reset0_cfg,
90                      priv->ahb_gate_mask | extra_ahb_gate_mask);
91 #endif
92
93         return ohci_register(dev, regs);
94 }
95
96 static int ohci_usb_remove(struct udevice *dev)
97 {
98         struct ohci_sunxi_priv *priv = dev_get_priv(dev);
99         int ret;
100
101         if (generic_phy_valid(&priv->phy)) {
102                 ret = generic_phy_exit(&priv->phy);
103                 if (ret) {
104                         pr_err("failed to exit %s USB PHY\n", dev->name);
105                         return ret;
106                 }
107         }
108
109         ret = ohci_deregister(dev);
110         if (ret)
111                 return ret;
112
113 #ifdef CONFIG_SUNXI_GEN_SUN6I
114         clrbits_le32(&priv->ccm->ahb_reset0_cfg, priv->ahb_gate_mask);
115 #endif
116         clrbits_le32(&priv->ccm->usb_clk_cfg, priv->usb_gate_mask);
117         clrbits_le32(&priv->ccm->ahb_gate0, priv->ahb_gate_mask);
118
119         return 0;
120 }
121
122 static const struct udevice_id ohci_usb_ids[] = {
123         { .compatible = "allwinner,sun4i-a10-ohci", },
124         { .compatible = "allwinner,sun5i-a13-ohci", },
125         { .compatible = "allwinner,sun6i-a31-ohci", },
126         { .compatible = "allwinner,sun7i-a20-ohci", },
127         { .compatible = "allwinner,sun8i-a23-ohci", },
128         { .compatible = "allwinner,sun8i-a83t-ohci", },
129         { .compatible = "allwinner,sun8i-h3-ohci",  },
130         { .compatible = "allwinner,sun9i-a80-ohci", },
131         { .compatible = "allwinner,sun50i-a64-ohci", },
132         { }
133 };
134
135 U_BOOT_DRIVER(usb_ohci) = {
136         .name   = "ohci_sunxi",
137         .id     = UCLASS_USB,
138         .of_match = ohci_usb_ids,
139         .probe = ohci_usb_probe,
140         .remove = ohci_usb_remove,
141         .ops    = &ohci_usb_ops,
142         .platdata_auto_alloc_size = sizeof(struct usb_platdata),
143         .priv_auto_alloc_size = sizeof(struct ohci_sunxi_priv),
144         .flags  = DM_FLAG_ALLOC_PRIV_DMA,
145 };