blob: 163f0ef17b118f9d12587657f9a57c12dee7f2c5 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Alexey Brodkin83fd3122015-12-14 17:18:50 +03002/*
3 * Copyright (C) 2015 Alexey Brodkin <abrodkin@synopsys.com>
Alexey Brodkin83fd3122015-12-14 17:18:50 +03004 */
5
6#include <common.h>
Patrice Chotard37a197a2017-07-18 11:57:12 +02007#include <clk.h>
Alexey Brodkin83fd3122015-12-14 17:18:50 +03008#include <dm.h>
Simon Glass0f2af882020-05-10 11:40:05 -06009#include <log.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>
Patrice Chotard37a197a2017-07-18 11:57:12 +020012#include <dm/ofnode.h>
Patrice Chotardc6e19d32017-07-18 11:57:14 +020013#include <generic-phy.h>
Patrice Chotard170229e2017-07-18 11:57:13 +020014#include <reset.h>
Alexey Brodkin83fd3122015-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 Chotard37a197a2017-07-18 11:57:12 +020023 struct clk *clocks; /* clock list */
Patrice Chotard170229e2017-07-18 11:57:13 +020024 struct reset_ctl *resets; /* reset list */
Patrice Chotardc6e19d32017-07-18 11:57:14 +020025 struct phy phy;
Patrice Chotard37a197a2017-07-18 11:57:12 +020026 int clock_count; /* number of clock in clock list */
Patrice Chotard170229e2017-07-18 11:57:13 +020027 int reset_count; /* number of reset in reset list */
Alexey Brodkin83fd3122015-12-14 17:18:50 +030028};
29
Patrice Chotardab0b205e2018-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 Delaunay01e05a92020-07-03 17:36:42 +020044 dev_dbg(dev, "failed to init usb phy\n");
Patrice Chotardab0b205e2018-03-14 17:48:57 +010045 return ret;
46 }
47
48 ret = generic_phy_power_on(&priv->phy);
49 if (ret) {
Patrick Delaunay01e05a92020-07-03 17:36:42 +020050 dev_dbg(dev, "failed to power on usb phy\n");
Patrice Chotardab0b205e2018-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 Delaunay01e05a92020-07-03 17:36:42 +020066 dev_dbg(dev, "failed to power off usb phy\n");
Patrice Chotardab0b205e2018-03-14 17:48:57 +010067 return ret;
68 }
69
70 ret = generic_phy_exit(&priv->phy);
71 if (ret) {
Patrick Delaunay01e05a92020-07-03 17:36:42 +020072 dev_dbg(dev, "failed to power off usb phy\n");
Patrice Chotardab0b205e2018-03-14 17:48:57 +010073 return ret;
74 }
75 }
76
77 return 0;
78}
79
Alexey Brodkin83fd3122015-12-14 17:18:50 +030080static int ohci_usb_probe(struct udevice *dev)
81{
Masahiro Yamada1096ae12020-07-17 14:36:46 +090082 struct ohci_regs *regs = dev_read_addr_ptr(dev);
Patrice Chotard37a197a2017-07-18 11:57:12 +020083 struct generic_ohci *priv = dev_get_priv(dev);
Patrice Chotard170229e2017-07-18 11:57:13 +020084 int i, err, ret, clock_nb, reset_nb;
Alexey Brodkin83fd3122015-12-14 17:18:50 +030085
Patrice Chotard37a197a2017-07-18 11:57:12 +020086 err = 0;
87 priv->clock_count = 0;
Patrick Delaunayd776a842020-09-25 09:41:14 +020088 clock_nb = dev_count_phandle_with_args(dev, "clocks", "#clock-cells",
89 0);
Patrice Chotard37a197a2017-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 Yang05b015b2019-08-28 16:23:47 +0800102 if (err && err != -ENOSYS) {
Patrice Chotard7c11e352018-03-14 17:48:58 +0100103 dev_err(dev, "failed to enable clock %d\n", i);
Patrice Chotard37a197a2017-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 Chotard7c11e352018-03-14 17:48:58 +0100110 dev_err(dev, "failed to get clock phandle(%d)\n", clock_nb);
Patrice Chotard37a197a2017-07-18 11:57:12 +0200111 return clock_nb;
112 }
113
Patrice Chotard170229e2017-07-18 11:57:13 +0200114 priv->reset_count = 0;
Patrick Delaunayd776a842020-09-25 09:41:14 +0200115 reset_nb = dev_count_phandle_with_args(dev, "resets", "#reset-cells",
116 0);
Patrice Chotard170229e2017-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 Chotard7c11e352018-03-14 17:48:58 +0100131 dev_err(dev, "failed to deassert reset %d\n", i);
Patrice Chotard170229e2017-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 Chotard7c11e352018-03-14 17:48:58 +0100138 dev_err(dev, "failed to get reset phandle(%d)\n", reset_nb);
Patrice Chotard170229e2017-07-18 11:57:13 +0200139 goto clk_err;
140 }
141
Patrice Chotardab0b205e2018-03-14 17:48:57 +0100142 err = ohci_setup_phy(dev, 0);
143 if (err)
Patrice Chotardab0b205e2018-03-14 17:48:57 +0100144 goto reset_err;
Patrice Chotardc6e19d32017-07-18 11:57:14 +0200145
Patrice Chotard37a197a2017-07-18 11:57:12 +0200146 err = ohci_register(dev, regs);
147 if (err)
Patrice Chotardc6e19d32017-07-18 11:57:14 +0200148 goto phy_err;
Patrice Chotard37a197a2017-07-18 11:57:12 +0200149
150 return 0;
151
Patrice Chotardc6e19d32017-07-18 11:57:14 +0200152phy_err:
Patrice Chotardab0b205e2018-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 Chotardc6e19d32017-07-18 11:57:14 +0200156
Patrice Chotard170229e2017-07-18 11:57:13 +0200157reset_err:
158 ret = reset_release_all(priv->resets, priv->reset_count);
159 if (ret)
Patrice Chotard7c11e352018-03-14 17:48:58 +0100160 dev_err(dev, "failed to assert all resets\n");
Patrice Chotard37a197a2017-07-18 11:57:12 +0200161clk_err:
162 ret = clk_release_all(priv->clocks, priv->clock_count);
163 if (ret)
Patrice Chotard7c11e352018-03-14 17:48:58 +0100164 dev_err(dev, "failed to disable all clocks\n");
Patrice Chotard37a197a2017-07-18 11:57:12 +0200165
166 return err;
Alexey Brodkin83fd3122015-12-14 17:18:50 +0300167}
168
169static int ohci_usb_remove(struct udevice *dev)
170{
Patrice Chotard37a197a2017-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 Chotardab0b205e2018-03-14 17:48:57 +0100178 ret = ohci_shutdown_phy(dev);
179 if (ret)
180 return ret;
Patrice Chotardc6e19d32017-07-18 11:57:14 +0200181
Patrice Chotard170229e2017-07-18 11:57:13 +0200182 ret = reset_release_all(priv->resets, priv->reset_count);
183 if (ret)
184 return ret;
185
Patrice Chotard37a197a2017-07-18 11:57:12 +0200186 return clk_release_all(priv->clocks, priv->clock_count);
Alexey Brodkin83fd3122015-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 Glass8a2b47f2020-12-03 16:55:17 -0700201 .priv_auto = sizeof(struct generic_ohci),
Alexey Brodkin83fd3122015-12-14 17:18:50 +0300202 .flags = DM_FLAG_ALLOC_PRIV_DMA,
203};