blob: 33c4a911a090be1fa26d54217c192588ff8ebe53 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Sughosh Ganu282e2af2012-08-09 10:45:20 +00002/*
3 * Copyright (C) 2012 Sughosh Ganu <urwithsughosh@gmail.com>
Sughosh Ganu282e2af2012-08-09 10:45:20 +00004 */
5
6#include <common.h>
Simon Glass9bc15642020-02-03 07:36:16 -07007#include <malloc.h>
Adam Ford5f364f52019-04-30 05:21:41 -05008#include <asm/io.h>
9#include <clk.h>
10#include <dm.h>
Simon Glass9bc15642020-02-03 07:36:16 -070011#include <dm/device_compat.h>
Simon Glassd66c5f72020-02-03 07:36:15 -070012#include <dm/devres.h>
Adam Ford5f364f52019-04-30 05:21:41 -050013#include <dm/ofnode.h>
14#include <generic-phy.h>
15#include <reset.h>
16#include "ohci.h"
Sughosh Ganu282e2af2012-08-09 10:45:20 +000017#include <asm/arch/da8xx-usb.h>
18
Adam Ford5f364f52019-04-30 05:21:41 -050019struct 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
26static 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
49static 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
Sughosh Ganu282e2af2012-08-09 10:45:20 +000058int 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
73int 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
84int usb_cpu_init_fail(void)
85{
86 return usb_cpu_stop();
87}
Adam Ford5f364f52019-04-30 05:21:41 -050088
89#if CONFIG_IS_ENABLED(DM_USB)
90static int ohci_da8xx_probe(struct udevice *dev)
91{
Masahiro Yamada1096ae12020-07-17 14:36:46 +090092 struct ohci_regs *regs = dev_read_addr_ptr(dev);
Adam Ford5f364f52019-04-30 05:21:41 -050093 struct da8xx_ohci *priv = dev_get_priv(dev);
94 int i, err, ret, clock_nb;
95
96 err = 0;
97 priv->clock_count = 0;
Patrick Delaunayd776a842020-09-25 09:41:14 +020098 clock_nb = dev_count_phandle_with_args(dev, "clocks", "#clock-cells",
99 0);
Adam Ford331b30f2019-05-07 06:57:39 -0500100
101 if (clock_nb < 0)
102 return clock_nb;
103
Adam Ford5f364f52019-04-30 05:21:41 -0500104 if (clock_nb > 0) {
105 priv->clocks = devm_kcalloc(dev, clock_nb, sizeof(struct clk),
106 GFP_KERNEL);
107 if (!priv->clocks)
108 return -ENOMEM;
109
110 for (i = 0; i < clock_nb; i++) {
111 err = clk_get_by_index(dev, i, &priv->clocks[i]);
112 if (err < 0)
113 break;
114
115 err = clk_enable(&priv->clocks[i]);
116 if (err) {
117 dev_err(dev, "failed to enable clock %d\n", i);
118 clk_free(&priv->clocks[i]);
119 goto clk_err;
120 }
121 priv->clock_count++;
122 }
Adam Ford5f364f52019-04-30 05:21:41 -0500123 }
124
125 err = usb_cpu_init();
126
127 if (err)
128 goto clk_err;
129
130 err = ohci_register(dev, regs);
131 if (err)
132 goto phy_err;
133
134 return 0;
135
136phy_err:
137 ret = usb_cpu_stop();
138 if (ret)
139 dev_err(dev, "failed to shutdown usb phy\n");
140
141clk_err:
142 ret = clk_release_all(priv->clocks, priv->clock_count);
143 if (ret)
144 dev_err(dev, "failed to disable all clocks\n");
145
146 return err;
147}
148
149static int ohci_da8xx_remove(struct udevice *dev)
150{
151 struct da8xx_ohci *priv = dev_get_priv(dev);
152 int ret;
153
154 ret = ohci_deregister(dev);
155 if (ret)
156 return ret;
157
158 ret = usb_cpu_stop();
159 if (ret)
160 return ret;
161
162 return clk_release_all(priv->clocks, priv->clock_count);
163}
164
165static const struct udevice_id da8xx_ohci_ids[] = {
166 { .compatible = "ti,da830-ohci" },
167 { }
168};
169
170U_BOOT_DRIVER(ohci_generic) = {
171 .name = "ohci-da8xx",
172 .id = UCLASS_USB,
173 .of_match = da8xx_ohci_ids,
174 .probe = ohci_da8xx_probe,
175 .remove = ohci_da8xx_remove,
176 .ops = &ohci_usb_ops,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700177 .priv_auto = sizeof(struct da8xx_ohci),
Adam Ford331b30f2019-05-07 06:57:39 -0500178 .flags = DM_FLAG_ALLOC_PRIV_DMA | DM_FLAG_OS_PREPARE,
Adam Ford5f364f52019-04-30 05:21:41 -0500179};
180#endif