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
usb_phy_on(void)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
usb_phy_off(void)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
usb_cpu_init(void)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
usb_cpu_stop(void)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
usb_cpu_init_fail(void)81 int usb_cpu_init_fail(void)
82 {
83 return usb_cpu_stop();
84 }
85
86 #if CONFIG_IS_ENABLED(DM_USB)
ohci_da8xx_probe(struct udevice * dev)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
97 if (clock_nb < 0)
98 return clock_nb;
99
100 if (clock_nb > 0) {
101 priv->clocks = devm_kcalloc(dev, clock_nb, sizeof(struct clk),
102 GFP_KERNEL);
103 if (!priv->clocks)
104 return -ENOMEM;
105
106 for (i = 0; i < clock_nb; i++) {
107 err = clk_get_by_index(dev, i, &priv->clocks[i]);
108 if (err < 0)
109 break;
110
111 err = clk_enable(&priv->clocks[i]);
112 if (err) {
113 dev_err(dev, "failed to enable clock %d\n", i);
114 clk_free(&priv->clocks[i]);
115 goto clk_err;
116 }
117 priv->clock_count++;
118 }
119 }
120
121 err = usb_cpu_init();
122
123 if (err)
124 goto clk_err;
125
126 err = ohci_register(dev, regs);
127 if (err)
128 goto phy_err;
129
130 return 0;
131
132 phy_err:
133 ret = usb_cpu_stop();
134 if (ret)
135 dev_err(dev, "failed to shutdown usb phy\n");
136
137 clk_err:
138 ret = clk_release_all(priv->clocks, priv->clock_count);
139 if (ret)
140 dev_err(dev, "failed to disable all clocks\n");
141
142 return err;
143 }
144
ohci_da8xx_remove(struct udevice * dev)145 static int ohci_da8xx_remove(struct udevice *dev)
146 {
147 struct da8xx_ohci *priv = dev_get_priv(dev);
148 int ret;
149
150 ret = ohci_deregister(dev);
151 if (ret)
152 return ret;
153
154 ret = usb_cpu_stop();
155 if (ret)
156 return ret;
157
158 return clk_release_all(priv->clocks, priv->clock_count);
159 }
160
161 static const struct udevice_id da8xx_ohci_ids[] = {
162 { .compatible = "ti,da830-ohci" },
163 { }
164 };
165
166 U_BOOT_DRIVER(ohci_generic) = {
167 .name = "ohci-da8xx",
168 .id = UCLASS_USB,
169 .of_match = da8xx_ohci_ids,
170 .probe = ohci_da8xx_probe,
171 .remove = ohci_da8xx_remove,
172 .ops = &ohci_usb_ops,
173 .priv_auto_alloc_size = sizeof(struct da8xx_ohci),
174 .flags = DM_FLAG_ALLOC_PRIV_DMA | DM_FLAG_OS_PREPARE,
175 };
176 #endif
177