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