blob: b84bf8ac0f7c22cc6c9f10dac9228461dd276eb2 [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) {
44 dev_err(dev, "failed to init usb phy\n");
45 return ret;
46 }
47
48 ret = generic_phy_power_on(&priv->phy);
49 if (ret) {
50 dev_err(dev, "failed to power on usb phy\n");
51 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) {
66 dev_err(dev, "failed to power off usb phy\n");
67 return ret;
68 }
69
70 ret = generic_phy_exit(&priv->phy);
71 if (ret) {
72 dev_err(dev, "failed to power off usb phy\n");
73 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;
88 clock_nb = dev_count_phandle_with_args(dev, "clocks", "#clock-cells");
89 if (clock_nb > 0) {
90 priv->clocks = devm_kcalloc(dev, clock_nb, sizeof(struct clk),
91 GFP_KERNEL);
92 if (!priv->clocks)
93 return -ENOMEM;
94
95 for (i = 0; i < clock_nb; i++) {
96 err = clk_get_by_index(dev, i, &priv->clocks[i]);
97 if (err < 0)
98 break;
99
100 err = clk_enable(&priv->clocks[i]);
Kever Yang6578db82019-08-28 16:23:47 +0800101 if (err && err != -ENOSYS) {
Patrice Chotard6048d422018-03-14 17:48:58 +0100102 dev_err(dev, "failed to enable clock %d\n", i);
Patrice Chotard155d9f62017-07-18 11:57:12 +0200103 clk_free(&priv->clocks[i]);
104 goto clk_err;
105 }
106 priv->clock_count++;
107 }
108 } else if (clock_nb != -ENOENT) {
Patrice Chotard6048d422018-03-14 17:48:58 +0100109 dev_err(dev, "failed to get clock phandle(%d)\n", clock_nb);
Patrice Chotard155d9f62017-07-18 11:57:12 +0200110 return clock_nb;
111 }
112
Patrice Chotard8a51b4b2017-07-18 11:57:13 +0200113 priv->reset_count = 0;
114 reset_nb = dev_count_phandle_with_args(dev, "resets", "#reset-cells");
115 if (reset_nb > 0) {
116 priv->resets = devm_kcalloc(dev, reset_nb,
117 sizeof(struct reset_ctl),
118 GFP_KERNEL);
119 if (!priv->resets)
120 return -ENOMEM;
121
122 for (i = 0; i < reset_nb; i++) {
123 err = reset_get_by_index(dev, i, &priv->resets[i]);
124 if (err < 0)
125 break;
126
127 err = reset_deassert(&priv->resets[i]);
128 if (err) {
Patrice Chotard6048d422018-03-14 17:48:58 +0100129 dev_err(dev, "failed to deassert reset %d\n", i);
Patrice Chotard8a51b4b2017-07-18 11:57:13 +0200130 reset_free(&priv->resets[i]);
131 goto reset_err;
132 }
133 priv->reset_count++;
134 }
135 } else if (reset_nb != -ENOENT) {
Patrice Chotard6048d422018-03-14 17:48:58 +0100136 dev_err(dev, "failed to get reset phandle(%d)\n", reset_nb);
Patrice Chotard8a51b4b2017-07-18 11:57:13 +0200137 goto clk_err;
138 }
139
Patrice Chotardcab4d482018-03-14 17:48:57 +0100140 err = ohci_setup_phy(dev, 0);
141 if (err)
Patrice Chotardcab4d482018-03-14 17:48:57 +0100142 goto reset_err;
Patrice Chotard28df1cf2017-07-18 11:57:14 +0200143
Patrice Chotard155d9f62017-07-18 11:57:12 +0200144 err = ohci_register(dev, regs);
145 if (err)
Patrice Chotard28df1cf2017-07-18 11:57:14 +0200146 goto phy_err;
Patrice Chotard155d9f62017-07-18 11:57:12 +0200147
148 return 0;
149
Patrice Chotard28df1cf2017-07-18 11:57:14 +0200150phy_err:
Patrice Chotardcab4d482018-03-14 17:48:57 +0100151 ret = ohci_shutdown_phy(dev);
152 if (ret)
153 dev_err(dev, "failed to shutdown usb phy\n");
Patrice Chotard28df1cf2017-07-18 11:57:14 +0200154
Patrice Chotard8a51b4b2017-07-18 11:57:13 +0200155reset_err:
156 ret = reset_release_all(priv->resets, priv->reset_count);
157 if (ret)
Patrice Chotard6048d422018-03-14 17:48:58 +0100158 dev_err(dev, "failed to assert all resets\n");
Patrice Chotard155d9f62017-07-18 11:57:12 +0200159clk_err:
160 ret = clk_release_all(priv->clocks, priv->clock_count);
161 if (ret)
Patrice Chotard6048d422018-03-14 17:48:58 +0100162 dev_err(dev, "failed to disable all clocks\n");
Patrice Chotard155d9f62017-07-18 11:57:12 +0200163
164 return err;
Alexey Brodkinfee331f2015-12-14 17:18:50 +0300165}
166
167static int ohci_usb_remove(struct udevice *dev)
168{
Patrice Chotard155d9f62017-07-18 11:57:12 +0200169 struct generic_ohci *priv = dev_get_priv(dev);
170 int ret;
171
172 ret = ohci_deregister(dev);
173 if (ret)
174 return ret;
175
Patrice Chotardcab4d482018-03-14 17:48:57 +0100176 ret = ohci_shutdown_phy(dev);
177 if (ret)
178 return ret;
Patrice Chotard28df1cf2017-07-18 11:57:14 +0200179
Patrice Chotard8a51b4b2017-07-18 11:57:13 +0200180 ret = reset_release_all(priv->resets, priv->reset_count);
181 if (ret)
182 return ret;
183
Patrice Chotard155d9f62017-07-18 11:57:12 +0200184 return clk_release_all(priv->clocks, priv->clock_count);
Alexey Brodkinfee331f2015-12-14 17:18:50 +0300185}
186
187static const struct udevice_id ohci_usb_ids[] = {
188 { .compatible = "generic-ohci" },
189 { }
190};
191
192U_BOOT_DRIVER(ohci_generic) = {
193 .name = "ohci_generic",
194 .id = UCLASS_USB,
195 .of_match = ohci_usb_ids,
196 .probe = ohci_usb_probe,
197 .remove = ohci_usb_remove,
198 .ops = &ohci_usb_ops,
199 .priv_auto_alloc_size = sizeof(struct generic_ohci),
200 .flags = DM_FLAG_ALLOC_PRIV_DMA,
201};