]> git.sur5r.net Git - u-boot/blob - drivers/usb/host/ehci-sunxi.c
usb: sunxi: Switch to use generic-phy
[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_priv {
26         struct ehci_ctrl ehci;
27         struct sunxi_ccm_reg *ccm;
28         int ahb_gate_mask; /* Mask of ahb_gate0 clk gate bits for this hcd */
29         struct phy phy;
30 };
31
32 static int ehci_usb_probe(struct udevice *dev)
33 {
34         struct usb_platdata *plat = dev_get_platdata(dev);
35         struct ehci_sunxi_priv *priv = dev_get_priv(dev);
36         struct ehci_hccr *hccr = (struct ehci_hccr *)devfdt_get_addr(dev);
37         struct ehci_hcor *hcor;
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         /*
71          * This should go away once we've moved to the driver model for
72          * clocks resp. phys.
73          */
74         priv->ahb_gate_mask = 1 << AHB_GATE_OFFSET_USB_EHCI0;
75 #if defined(CONFIG_MACH_SUNXI_H3_H5) || defined(CONFIG_MACH_SUN50I)
76         extra_ahb_gate_mask = 1 << AHB_GATE_OFFSET_USB_OHCI0;
77 #endif
78         priv->ahb_gate_mask <<= phys * AHB_CLK_DIST;
79         extra_ahb_gate_mask <<= phys * AHB_CLK_DIST;
80
81         setbits_le32(&priv->ccm->ahb_gate0,
82                      priv->ahb_gate_mask | extra_ahb_gate_mask);
83 #ifdef CONFIG_SUNXI_GEN_SUN6I
84         setbits_le32(&priv->ccm->ahb_reset0_cfg,
85                      priv->ahb_gate_mask | extra_ahb_gate_mask);
86 #endif
87
88         hcor = (struct ehci_hcor *)((uintptr_t)hccr +
89                                     HC_LENGTH(ehci_readl(&hccr->cr_capbase)));
90
91         return ehci_register(dev, hccr, hcor, NULL, 0, plat->init_type);
92 }
93
94 static int ehci_usb_remove(struct udevice *dev)
95 {
96         struct ehci_sunxi_priv *priv = dev_get_priv(dev);
97         int ret;
98
99         if (generic_phy_valid(&priv->phy)) {
100                 ret = generic_phy_exit(&priv->phy);
101                 if (ret) {
102                         pr_err("failed to exit %s USB PHY\n", dev->name);
103                         return ret;
104                 }
105         }
106
107         ret = ehci_deregister(dev);
108         if (ret)
109                 return ret;
110
111 #ifdef CONFIG_SUNXI_GEN_SUN6I
112         clrbits_le32(&priv->ccm->ahb_reset0_cfg, priv->ahb_gate_mask);
113 #endif
114         clrbits_le32(&priv->ccm->ahb_gate0, priv->ahb_gate_mask);
115
116         return 0;
117 }
118
119 static const struct udevice_id ehci_usb_ids[] = {
120         { .compatible = "allwinner,sun4i-a10-ehci", },
121         { .compatible = "allwinner,sun5i-a13-ehci", },
122         { .compatible = "allwinner,sun6i-a31-ehci", },
123         { .compatible = "allwinner,sun7i-a20-ehci", },
124         { .compatible = "allwinner,sun8i-a23-ehci", },
125         { .compatible = "allwinner,sun8i-a83t-ehci", },
126         { .compatible = "allwinner,sun8i-h3-ehci",  },
127         { .compatible = "allwinner,sun9i-a80-ehci", },
128         { .compatible = "allwinner,sun50i-a64-ehci", },
129         { }
130 };
131
132 U_BOOT_DRIVER(ehci_sunxi) = {
133         .name   = "ehci_sunxi",
134         .id     = UCLASS_USB,
135         .of_match = ehci_usb_ids,
136         .probe = ehci_usb_probe,
137         .remove = ehci_usb_remove,
138         .ops    = &ehci_usb_ops,
139         .platdata_auto_alloc_size = sizeof(struct usb_platdata),
140         .priv_auto_alloc_size = sizeof(struct ehci_sunxi_priv),
141         .flags  = DM_FLAG_ALLOC_PRIV_DMA,
142 };