blob: 163f0ef17b118f9d12587657f9a57c12dee7f2c5 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Alexey Brodkinfee331f2015-12-14 17:18:50 +03002/*
3 * Copyright (C) 2015 Alexey Brodkin <abrodkin@synopsys.com>
Alexey Brodkinfee331f2015-12-14 17:18:50 +03004 */
5
6#include <common.h>
Patrice Chotard155d9f62017-07-18 11:57:12 +02007#include <clk.h>
Alexey Brodkinfee331f2015-12-14 17:18:50 +03008#include <dm.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -06009#include <log.h>
Simon Glass336d4612020-02-03 07:36:16 -070010#include <dm/device_compat.h>
Simon Glass61b29b82020-02-03 07:36:15 -070011#include <dm/devres.h>
Patrice Chotard155d9f62017-07-18 11:57:12 +020012#include <dm/ofnode.h>
Patrice Chotard28df1cf2017-07-18 11:57:14 +020013#include <generic-phy.h>
Patrice Chotard8a51b4b2017-07-18 11:57:13 +020014#include <reset.h>
Alexey Brodkinfee331f2015-12-14 17:18:50 +030015#include "ohci.h"
16
17#if !defined(CONFIG_USB_OHCI_NEW)
18# error "Generic OHCI driver requires CONFIG_USB_OHCI_NEW"
19#endif
20
21struct generic_ohci {
22 ohci_t ohci;
Patrice Chotard155d9f62017-07-18 11:57:12 +020023 struct clk *clocks; /* clock list */
Patrice Chotard8a51b4b2017-07-18 11:57:13 +020024 struct reset_ctl *resets; /* reset list */
Patrice Chotard28df1cf2017-07-18 11:57:14 +020025 struct phy phy;
Patrice Chotard155d9f62017-07-18 11:57:12 +020026 int clock_count; /* number of clock in clock list */
Patrice Chotard8a51b4b2017-07-18 11:57:13 +020027 int reset_count; /* number of reset in reset list */
Alexey Brodkinfee331f2015-12-14 17:18:50 +030028};
29
Patrice Chotardcab4d482018-03-14 17:48:57 +010030static int ohci_setup_phy(struct udevice *dev, int index)
31{
32 struct generic_ohci *priv = dev_get_priv(dev);
33 int ret;
34
35 ret = generic_phy_get_by_index(dev, index, &priv->phy);
36 if (ret) {
37 if (ret != -ENOENT) {
38 dev_err(dev, "failed to get usb phy\n");
39 return ret;
40 }
41 } else {
42 ret = generic_phy_init(&priv->phy);
43 if (ret) {
Patrick Delaunay3b417a72020-07-03 17:36:42 +020044 dev_dbg(dev, "failed to init usb phy\n");
Patrice Chotardcab4d482018-03-14 17:48:57 +010045 return ret;
46 }
47
48 ret = generic_phy_power_on(&priv->phy);
49 if (ret) {
Patrick Delaunay3b417a72020-07-03 17:36:42 +020050 dev_dbg(dev, "failed to power on usb phy\n");
Patrice Chotardcab4d482018-03-14 17:48:57 +010051 return generic_phy_exit(&priv->phy);
52 }
53 }
54
55 return 0;
56}
57
58static int ohci_shutdown_phy(struct udevice *dev)
59{
60 struct generic_ohci *priv = dev_get_priv(dev);
61 int ret = 0;
62
63 if (generic_phy_valid(&priv->phy)) {
64 ret = generic_phy_power_off(&priv->phy);
65 if (ret) {
Patrick Delaunay3b417a72020-07-03 17:36:42 +020066 dev_dbg(dev, "failed to power off usb phy\n");
Patrice Chotardcab4d482018-03-14 17:48:57 +010067 return ret;
68 }
69
70 ret = generic_phy_exit(&priv->phy);
71 if (ret) {
Patrick Delaunay3b417a72020-07-03 17:36:42 +020072 dev_dbg(dev, "failed to power off usb phy\n");
Patrice Chotardcab4d482018-03-14 17:48:57 +010073 return ret;
74 }
75 }
76
77 return 0;
78}
79
Alexey Brodkinfee331f2015-12-14 17:18:50 +030080static int ohci_usb_probe(struct udevice *dev)
81{
Masahiro Yamada8613c8d2020-07-17 14:36:46 +090082 struct ohci_regs *regs = dev_read_addr_ptr(dev);
Patrice Chotard155d9f62017-07-18 11:57:12 +020083 struct generic_ohci *priv = dev_get_priv(dev);
Patrice Chotard8a51b4b2017-07-18 11:57:13 +020084 int i, err, ret, clock_nb, reset_nb;
Alexey Brodkinfee331f2015-12-14 17:18:50 +030085
Patrice Chotard155d9f62017-07-18 11:57:12 +020086 err = 0;
87 priv->clock_count = 0;
Patrick Delaunay89f68302020-09-25 09:41:14 +020088 clock_nb = dev_count_phandle_with_args(dev, "clocks", "#clock-cells",
89 0);
Patrice Chotard155d9f62017-07-18 11:57:12 +020090 if (clock_nb > 0) {
91 priv->clocks = devm_kcalloc(dev, clock_nb, sizeof(struct clk),
92 GFP_KERNEL);
93 if (!priv->clocks)
94 return -ENOMEM;
95
96 for (i = 0; i < clock_nb; i++) {
97 err = clk_get_by_index(dev, i, &priv->clocks[i]);
98 if (err < 0)
99 break;
100
101 err = clk_enable(&priv->clocks[i]);
Kever Yang6578db82019-08-28 16:23:47 +0800102 if (err && err != -ENOSYS) {
Patrice Chotard6048d422018-03-14 17:48:58 +0100103 dev_err(dev, "failed to enable clock %d\n", i);
Patrice Chotard155d9f62017-07-18 11:57:12 +0200104 clk_free(&priv->clocks[i]);
105 goto clk_err;
106 }
107 priv->clock_count++;
108 }
109 } else if (clock_nb != -ENOENT) {
Patrice Chotard6048d422018-03-14 17:48:58 +0100110 dev_err(dev, "failed to get clock phandle(%d)\n", clock_nb);
Patrice Chotard155d9f62017-07-18 11:57:12 +0200111 return clock_nb;
112 }
113
Patrice Chotard8a51b4b2017-07-18 11:57:13 +0200114 priv->reset_count = 0;
Patrick Delaunay89f68302020-09-25 09:41:14 +0200115 reset_nb = dev_count_phandle_with_args(dev, "resets", "#reset-cells",
116 0);
Patrice Chotard8a51b4b2017-07-18 11:57:13 +0200117 if (reset_nb > 0) {
118 priv->resets = devm_kcalloc(dev, reset_nb,
119 sizeof(struct reset_ctl),
120 GFP_KERNEL);
121 if (!priv->resets)
122 return -ENOMEM;
123
124 for (i = 0; i < reset_nb; i++) {
125 err = reset_get_by_index(dev, i, &priv->resets[i]);
126 if (err < 0)
127 break;
128
129 err = reset_deassert(&priv->resets[i]);
130 if (err) {
Patrice Chotard6048d422018-03-14 17:48:58 +0100131 dev_err(dev, "failed to deassert reset %d\n", i);
Patrice Chotard8a51b4b2017-07-18 11:57:13 +0200132 reset_free(&priv->resets[i]);
133 goto reset_err;
134 }
135 priv->reset_count++;
136 }
137 } else if (reset_nb != -ENOENT) {
Patrice Chotard6048d422018-03-14 17:48:58 +0100138 dev_err(dev, "failed to get reset phandle(%d)\n", reset_nb);
Patrice Chotard8a51b4b2017-07-18 11:57:13 +0200139 goto clk_err;
140 }
141
Patrice Chotardcab4d482018-03-14 17:48:57 +0100142 err = ohci_setup_phy(dev, 0);
143 if (err)
Patrice Chotardcab4d482018-03-14 17:48:57 +0100144 goto reset_err;
Patrice Chotard28df1cf2017-07-18 11:57:14 +0200145
Patrice Chotard155d9f62017-07-18 11:57:12 +0200146 err = ohci_register(dev, regs);
147 if (err)
Patrice Chotard28df1cf2017-07-18 11:57:14 +0200148 goto phy_err;
Patrice Chotard155d9f62017-07-18 11:57:12 +0200149
150 return 0;
151
Patrice Chotard28df1cf2017-07-18 11:57:14 +0200152phy_err:
Patrice Chotardcab4d482018-03-14 17:48:57 +0100153 ret = ohci_shutdown_phy(dev);
154 if (ret)
155 dev_err(dev, "failed to shutdown usb phy\n");
Patrice Chotard28df1cf2017-07-18 11:57:14 +0200156
Patrice Chotard8a51b4b2017-07-18 11:57:13 +0200157reset_err:
158 ret = reset_release_all(priv->resets, priv->reset_count);
159 if (ret)
Patrice Chotard6048d422018-03-14 17:48:58 +0100160 dev_err(dev, "failed to assert all resets\n");
Patrice Chotard155d9f62017-07-18 11:57:12 +0200161clk_err:
162 ret = clk_release_all(priv->clocks, priv->clock_count);
163 if (ret)
Patrice Chotard6048d422018-03-14 17:48:58 +0100164 dev_err(dev, "failed to disable all clocks\n");
Patrice Chotard155d9f62017-07-18 11:57:12 +0200165
166 return err;
Alexey Brodkinfee331f2015-12-14 17:18:50 +0300167}
168
169static int ohci_usb_remove(struct udevice *dev)
170{
Patrice Chotard155d9f62017-07-18 11:57:12 +0200171 struct generic_ohci *priv = dev_get_priv(dev);
172 int ret;
173
174 ret = ohci_deregister(dev);
175 if (ret)
176 return ret;
177
Patrice Chotardcab4d482018-03-14 17:48:57 +0100178 ret = ohci_shutdown_phy(dev);
179 if (ret)
180 return ret;
Patrice Chotard28df1cf2017-07-18 11:57:14 +0200181
Patrice Chotard8a51b4b2017-07-18 11:57:13 +0200182 ret = reset_release_all(priv->resets, priv->reset_count);
183 if (ret)
184 return ret;
185
Patrice Chotard155d9f62017-07-18 11:57:12 +0200186 return clk_release_all(priv->clocks, priv->clock_count);
Alexey Brodkinfee331f2015-12-14 17:18:50 +0300187}
188
189static const struct udevice_id ohci_usb_ids[] = {
190 { .compatible = "generic-ohci" },
191 { }
192};
193
194U_BOOT_DRIVER(ohci_generic) = {
195 .name = "ohci_generic",
196 .id = UCLASS_USB,
197 .of_match = ohci_usb_ids,
198 .probe = ohci_usb_probe,
199 .remove = ohci_usb_remove,
200 .ops = &ohci_usb_ops,
Simon Glass41575d82020-12-03 16:55:17 -0700201 .priv_auto = sizeof(struct generic_ohci),
Alexey Brodkinfee331f2015-12-14 17:18:50 +0300202 .flags = DM_FLAG_ALLOC_PRIV_DMA,
203};