Merge branch 'master' of git://git.denx.de/u-boot-sunxi
[oweals/u-boot.git] / drivers / usb / host / ohci-da8xx.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (C) 2012 Sughosh Ganu <urwithsughosh@gmail.com>
4  */
5
6 #include <common.h>
7 #include <asm/io.h>
8 #include <clk.h>
9 #include <dm.h>
10 #include <dm/ofnode.h>
11 #include <generic-phy.h>
12 #include <reset.h>
13 #include "ohci.h"
14 #include <asm/arch/da8xx-usb.h>
15
16 struct da8xx_ohci {
17         ohci_t ohci;
18         struct clk *clocks;     /* clock list */
19         struct phy phy;
20         int clock_count;        /* number of clock in clock list */
21 };
22
23 static int usb_phy_on(void)
24 {
25         unsigned long timeout;
26
27         clrsetbits_le32(&davinci_syscfg_regs->cfgchip2,
28                         (CFGCHIP2_RESET | CFGCHIP2_PHYPWRDN |
29                         CFGCHIP2_OTGPWRDN | CFGCHIP2_OTGMODE |
30                         CFGCHIP2_REFFREQ | CFGCHIP2_USB1PHYCLKMUX),
31                         (CFGCHIP2_SESENDEN | CFGCHIP2_VBDTCTEN |
32                         CFGCHIP2_PHY_PLLON | CFGCHIP2_REFFREQ_24MHZ |
33                         CFGCHIP2_USB2PHYCLKMUX | CFGCHIP2_USB1SUSPENDM));
34
35         /* wait until the usb phy pll locks */
36         timeout = get_timer(0);
37         while (get_timer(timeout) < 10) {
38                 if (readl(&davinci_syscfg_regs->cfgchip2) & CFGCHIP2_PHYCLKGD)
39                         return 1;
40         }
41
42         /* USB phy was not turned on */
43         return 0;
44 }
45
46 static void usb_phy_off(void)
47 {
48         /* Power down the on-chip PHY. */
49         clrsetbits_le32(&davinci_syscfg_regs->cfgchip2,
50                         CFGCHIP2_PHY_PLLON | CFGCHIP2_USB1SUSPENDM,
51                         CFGCHIP2_PHYPWRDN | CFGCHIP2_OTGPWRDN |
52                         CFGCHIP2_RESET);
53 }
54
55 int usb_cpu_init(void)
56 {
57         /* enable psc for usb2.0 */
58         lpsc_on(DAVINCI_LPSC_USB20);
59
60         /* enable psc for usb1.0 */
61         lpsc_on(DAVINCI_LPSC_USB11);
62
63         /* start the on-chip usb phy and its pll */
64         if (usb_phy_on())
65                 return 0;
66
67         return 1;
68 }
69
70 int usb_cpu_stop(void)
71 {
72         usb_phy_off();
73
74         /* turn off the usb clock and assert the module reset */
75         lpsc_disable(DAVINCI_LPSC_USB11);
76         lpsc_disable(DAVINCI_LPSC_USB20);
77
78         return 0;
79 }
80
81 int usb_cpu_init_fail(void)
82 {
83         return usb_cpu_stop();
84 }
85
86 #if CONFIG_IS_ENABLED(DM_USB)
87 static int ohci_da8xx_probe(struct udevice *dev)
88 {
89         struct ohci_regs *regs = (struct ohci_regs *)devfdt_get_addr(dev);
90         struct da8xx_ohci *priv = dev_get_priv(dev);
91         int i, err, ret, clock_nb;
92
93         err = 0;
94         priv->clock_count = 0;
95         clock_nb = dev_count_phandle_with_args(dev, "clocks", "#clock-cells");
96         if (clock_nb > 0) {
97                 priv->clocks = devm_kcalloc(dev, clock_nb, sizeof(struct clk),
98                                             GFP_KERNEL);
99                 if (!priv->clocks)
100                         return -ENOMEM;
101
102                 for (i = 0; i < clock_nb; i++) {
103                         err = clk_get_by_index(dev, i, &priv->clocks[i]);
104                         if (err < 0)
105                                 break;
106
107                         err = clk_enable(&priv->clocks[i]);
108                         if (err) {
109                                 dev_err(dev, "failed to enable clock %d\n", i);
110                                 clk_free(&priv->clocks[i]);
111                                 goto clk_err;
112                         }
113                         priv->clock_count++;
114                 }
115         } else if (clock_nb != -ENOENT) {
116                 dev_err(dev, "failed to get clock phandle(%d)\n", clock_nb);
117                 return clock_nb;
118         }
119
120         err = usb_cpu_init();
121
122         if (err)
123                 goto clk_err;
124
125         err = ohci_register(dev, regs);
126         if (err)
127                 goto phy_err;
128
129         return 0;
130
131 phy_err:
132         ret = usb_cpu_stop();
133         if (ret)
134                 dev_err(dev, "failed to shutdown usb phy\n");
135
136 clk_err:
137         ret = clk_release_all(priv->clocks, priv->clock_count);
138         if (ret)
139                 dev_err(dev, "failed to disable all clocks\n");
140
141         return err;
142 }
143
144 static int ohci_da8xx_remove(struct udevice *dev)
145 {
146         struct da8xx_ohci *priv = dev_get_priv(dev);
147         int ret;
148
149         ret = ohci_deregister(dev);
150         if (ret)
151                 return ret;
152
153         ret = usb_cpu_stop();
154         if (ret)
155                 return ret;
156
157         return clk_release_all(priv->clocks, priv->clock_count);
158 }
159
160 static const struct udevice_id da8xx_ohci_ids[] = {
161         { .compatible = "ti,da830-ohci" },
162         { }
163 };
164
165 U_BOOT_DRIVER(ohci_generic) = {
166         .name   = "ohci-da8xx",
167         .id     = UCLASS_USB,
168         .of_match = da8xx_ohci_ids,
169         .probe = ohci_da8xx_probe,
170         .remove = ohci_da8xx_remove,
171         .ops    = &ohci_usb_ops,
172         .priv_auto_alloc_size = sizeof(struct da8xx_ohci),
173         .flags  = DM_FLAG_ALLOC_PRIV_DMA,
174 };
175 #endif