blob: d321d147c2f957e6d54053f7c869514529803c90 [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
Simon Glass9bc15642020-02-03 07:36:16 -07006#include <malloc.h>
Adam Ford5f364f52019-04-30 05:21:41 -05007#include <asm/io.h>
8#include <clk.h>
9#include <dm.h>
Simon Glass9bc15642020-02-03 07:36:16 -070010#include <dm/device_compat.h>
Simon Glassd66c5f72020-02-03 07:36:15 -070011#include <dm/devres.h>
Adam Ford5f364f52019-04-30 05:21:41 -050012#include <dm/ofnode.h>
13#include <generic-phy.h>
14#include <reset.h>
15#include "ohci.h"
Sughosh Ganu282e2af2012-08-09 10:45:20 +000016#include <asm/arch/da8xx-usb.h>
17
Adam Ford5f364f52019-04-30 05:21:41 -050018struct da8xx_ohci {
19 ohci_t ohci;
20 struct clk *clocks; /* clock list */
21 struct phy phy;
22 int clock_count; /* number of clock in clock list */
23};
24
25static int usb_phy_on(void)
26{
27 unsigned long timeout;
28
29 clrsetbits_le32(&davinci_syscfg_regs->cfgchip2,
30 (CFGCHIP2_RESET | CFGCHIP2_PHYPWRDN |
31 CFGCHIP2_OTGPWRDN | CFGCHIP2_OTGMODE |
32 CFGCHIP2_REFFREQ | CFGCHIP2_USB1PHYCLKMUX),
33 (CFGCHIP2_SESENDEN | CFGCHIP2_VBDTCTEN |
34 CFGCHIP2_PHY_PLLON | CFGCHIP2_REFFREQ_24MHZ |
35 CFGCHIP2_USB2PHYCLKMUX | CFGCHIP2_USB1SUSPENDM));
36
37 /* wait until the usb phy pll locks */
38 timeout = get_timer(0);
39 while (get_timer(timeout) < 10) {
40 if (readl(&davinci_syscfg_regs->cfgchip2) & CFGCHIP2_PHYCLKGD)
41 return 1;
42 }
43
44 /* USB phy was not turned on */
45 return 0;
46}
47
48static void usb_phy_off(void)
49{
50 /* Power down the on-chip PHY. */
51 clrsetbits_le32(&davinci_syscfg_regs->cfgchip2,
52 CFGCHIP2_PHY_PLLON | CFGCHIP2_USB1SUSPENDM,
53 CFGCHIP2_PHYPWRDN | CFGCHIP2_OTGPWRDN |
54 CFGCHIP2_RESET);
55}
56
Sughosh Ganu282e2af2012-08-09 10:45:20 +000057int usb_cpu_init(void)
58{
59 /* enable psc for usb2.0 */
60 lpsc_on(DAVINCI_LPSC_USB20);
61
62 /* enable psc for usb1.0 */
63 lpsc_on(DAVINCI_LPSC_USB11);
64
65 /* start the on-chip usb phy and its pll */
66 if (usb_phy_on())
67 return 0;
68
69 return 1;
70}
71
72int usb_cpu_stop(void)
73{
74 usb_phy_off();
75
76 /* turn off the usb clock and assert the module reset */
77 lpsc_disable(DAVINCI_LPSC_USB11);
78 lpsc_disable(DAVINCI_LPSC_USB20);
79
80 return 0;
81}
82
83int usb_cpu_init_fail(void)
84{
85 return usb_cpu_stop();
86}
Adam Ford5f364f52019-04-30 05:21:41 -050087
88#if CONFIG_IS_ENABLED(DM_USB)
89static int ohci_da8xx_probe(struct udevice *dev)
90{
Masahiro Yamada1096ae12020-07-17 14:36:46 +090091 struct ohci_regs *regs = dev_read_addr_ptr(dev);
Adam Ford5f364f52019-04-30 05:21:41 -050092 struct da8xx_ohci *priv = dev_get_priv(dev);
93 int i, err, ret, clock_nb;
94
95 err = 0;
96 priv->clock_count = 0;
Patrick Delaunayd776a842020-09-25 09:41:14 +020097 clock_nb = dev_count_phandle_with_args(dev, "clocks", "#clock-cells",
98 0);
Adam Ford331b30f2019-05-07 06:57:39 -050099
100 if (clock_nb < 0)
101 return clock_nb;
102
Adam Ford5f364f52019-04-30 05:21:41 -0500103 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);
Adam Ford5f364f52019-04-30 05:21:41 -0500117 goto clk_err;
118 }
119 priv->clock_count++;
120 }
Adam Ford5f364f52019-04-30 05:21:41 -0500121 }
122
123 err = usb_cpu_init();
124
125 if (err)
126 goto clk_err;
127
128 err = ohci_register(dev, regs);
129 if (err)
130 goto phy_err;
131
132 return 0;
133
134phy_err:
135 ret = usb_cpu_stop();
136 if (ret)
137 dev_err(dev, "failed to shutdown usb phy\n");
138
139clk_err:
140 ret = clk_release_all(priv->clocks, priv->clock_count);
141 if (ret)
142 dev_err(dev, "failed to disable all clocks\n");
143
144 return err;
145}
146
147static int ohci_da8xx_remove(struct udevice *dev)
148{
149 struct da8xx_ohci *priv = dev_get_priv(dev);
150 int ret;
151
152 ret = ohci_deregister(dev);
153 if (ret)
154 return ret;
155
156 ret = usb_cpu_stop();
157 if (ret)
158 return ret;
159
160 return clk_release_all(priv->clocks, priv->clock_count);
161}
162
163static const struct udevice_id da8xx_ohci_ids[] = {
164 { .compatible = "ti,da830-ohci" },
165 { }
166};
167
168U_BOOT_DRIVER(ohci_generic) = {
169 .name = "ohci-da8xx",
170 .id = UCLASS_USB,
171 .of_match = da8xx_ohci_ids,
172 .probe = ohci_da8xx_probe,
173 .remove = ohci_da8xx_remove,
174 .ops = &ohci_usb_ops,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700175 .priv_auto = sizeof(struct da8xx_ohci),
Adam Ford331b30f2019-05-07 06:57:39 -0500176 .flags = DM_FLAG_ALLOC_PRIV_DMA | DM_FLAG_OS_PREPARE,
Adam Ford5f364f52019-04-30 05:21:41 -0500177};
178#endif