blob: 5d23058aaf6acdc65aa703cd1e7ddc2251d4ac58 [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
Alexey Brodkin83fd3122015-12-14 17:18:50 +030017struct generic_ohci {
18 ohci_t ohci;
Patrice Chotard37a197a2017-07-18 11:57:12 +020019 struct clk *clocks; /* clock list */
Patrice Chotard170229e2017-07-18 11:57:13 +020020 struct reset_ctl *resets; /* reset list */
Patrice Chotardc6e19d32017-07-18 11:57:14 +020021 struct phy phy;
Patrice Chotard37a197a2017-07-18 11:57:12 +020022 int clock_count; /* number of clock in clock list */
Patrice Chotard170229e2017-07-18 11:57:13 +020023 int reset_count; /* number of reset in reset list */
Alexey Brodkin83fd3122015-12-14 17:18:50 +030024};
25
Patrice Chotardab0b205e2018-03-14 17:48:57 +010026static int ohci_setup_phy(struct udevice *dev, int index)
27{
28 struct generic_ohci *priv = dev_get_priv(dev);
29 int ret;
30
31 ret = generic_phy_get_by_index(dev, index, &priv->phy);
32 if (ret) {
33 if (ret != -ENOENT) {
34 dev_err(dev, "failed to get usb phy\n");
35 return ret;
36 }
37 } else {
38 ret = generic_phy_init(&priv->phy);
39 if (ret) {
Patrick Delaunay01e05a92020-07-03 17:36:42 +020040 dev_dbg(dev, "failed to init usb phy\n");
Patrice Chotardab0b205e2018-03-14 17:48:57 +010041 return ret;
42 }
43
44 ret = generic_phy_power_on(&priv->phy);
45 if (ret) {
Patrick Delaunay01e05a92020-07-03 17:36:42 +020046 dev_dbg(dev, "failed to power on usb phy\n");
Patrice Chotardab0b205e2018-03-14 17:48:57 +010047 return generic_phy_exit(&priv->phy);
48 }
49 }
50
51 return 0;
52}
53
54static int ohci_shutdown_phy(struct udevice *dev)
55{
56 struct generic_ohci *priv = dev_get_priv(dev);
57 int ret = 0;
58
59 if (generic_phy_valid(&priv->phy)) {
60 ret = generic_phy_power_off(&priv->phy);
61 if (ret) {
Patrick Delaunay01e05a92020-07-03 17:36:42 +020062 dev_dbg(dev, "failed to power off usb phy\n");
Patrice Chotardab0b205e2018-03-14 17:48:57 +010063 return ret;
64 }
65
66 ret = generic_phy_exit(&priv->phy);
67 if (ret) {
Patrick Delaunay01e05a92020-07-03 17:36:42 +020068 dev_dbg(dev, "failed to power off usb phy\n");
Patrice Chotardab0b205e2018-03-14 17:48:57 +010069 return ret;
70 }
71 }
72
73 return 0;
74}
75
Alexey Brodkin83fd3122015-12-14 17:18:50 +030076static int ohci_usb_probe(struct udevice *dev)
77{
Masahiro Yamada1096ae12020-07-17 14:36:46 +090078 struct ohci_regs *regs = dev_read_addr_ptr(dev);
Patrice Chotard37a197a2017-07-18 11:57:12 +020079 struct generic_ohci *priv = dev_get_priv(dev);
Patrice Chotard170229e2017-07-18 11:57:13 +020080 int i, err, ret, clock_nb, reset_nb;
Alexey Brodkin83fd3122015-12-14 17:18:50 +030081
Patrice Chotard37a197a2017-07-18 11:57:12 +020082 err = 0;
83 priv->clock_count = 0;
Patrick Delaunayd776a842020-09-25 09:41:14 +020084 clock_nb = dev_count_phandle_with_args(dev, "clocks", "#clock-cells",
85 0);
Patrice Chotard37a197a2017-07-18 11:57:12 +020086 if (clock_nb > 0) {
87 priv->clocks = devm_kcalloc(dev, clock_nb, sizeof(struct clk),
88 GFP_KERNEL);
89 if (!priv->clocks)
90 return -ENOMEM;
91
92 for (i = 0; i < clock_nb; i++) {
93 err = clk_get_by_index(dev, i, &priv->clocks[i]);
94 if (err < 0)
95 break;
96
97 err = clk_enable(&priv->clocks[i]);
Kever Yang05b015b2019-08-28 16:23:47 +080098 if (err && err != -ENOSYS) {
Patrice Chotard7c11e352018-03-14 17:48:58 +010099 dev_err(dev, "failed to enable clock %d\n", i);
Patrice Chotard37a197a2017-07-18 11:57:12 +0200100 clk_free(&priv->clocks[i]);
101 goto clk_err;
102 }
103 priv->clock_count++;
104 }
105 } else if (clock_nb != -ENOENT) {
Patrice Chotard7c11e352018-03-14 17:48:58 +0100106 dev_err(dev, "failed to get clock phandle(%d)\n", clock_nb);
Patrice Chotard37a197a2017-07-18 11:57:12 +0200107 return clock_nb;
108 }
109
Patrice Chotard170229e2017-07-18 11:57:13 +0200110 priv->reset_count = 0;
Patrick Delaunayd776a842020-09-25 09:41:14 +0200111 reset_nb = dev_count_phandle_with_args(dev, "resets", "#reset-cells",
112 0);
Patrice Chotard170229e2017-07-18 11:57:13 +0200113 if (reset_nb > 0) {
114 priv->resets = devm_kcalloc(dev, reset_nb,
115 sizeof(struct reset_ctl),
116 GFP_KERNEL);
117 if (!priv->resets)
118 return -ENOMEM;
119
120 for (i = 0; i < reset_nb; i++) {
121 err = reset_get_by_index(dev, i, &priv->resets[i]);
122 if (err < 0)
123 break;
124
125 err = reset_deassert(&priv->resets[i]);
126 if (err) {
Patrice Chotard7c11e352018-03-14 17:48:58 +0100127 dev_err(dev, "failed to deassert reset %d\n", i);
Patrice Chotard170229e2017-07-18 11:57:13 +0200128 reset_free(&priv->resets[i]);
129 goto reset_err;
130 }
131 priv->reset_count++;
132 }
133 } else if (reset_nb != -ENOENT) {
Patrice Chotard7c11e352018-03-14 17:48:58 +0100134 dev_err(dev, "failed to get reset phandle(%d)\n", reset_nb);
Patrice Chotard170229e2017-07-18 11:57:13 +0200135 goto clk_err;
136 }
137
Patrice Chotardab0b205e2018-03-14 17:48:57 +0100138 err = ohci_setup_phy(dev, 0);
139 if (err)
Patrice Chotardab0b205e2018-03-14 17:48:57 +0100140 goto reset_err;
Patrice Chotardc6e19d32017-07-18 11:57:14 +0200141
Patrice Chotard37a197a2017-07-18 11:57:12 +0200142 err = ohci_register(dev, regs);
143 if (err)
Patrice Chotardc6e19d32017-07-18 11:57:14 +0200144 goto phy_err;
Patrice Chotard37a197a2017-07-18 11:57:12 +0200145
146 return 0;
147
Patrice Chotardc6e19d32017-07-18 11:57:14 +0200148phy_err:
Patrice Chotardab0b205e2018-03-14 17:48:57 +0100149 ret = ohci_shutdown_phy(dev);
150 if (ret)
151 dev_err(dev, "failed to shutdown usb phy\n");
Patrice Chotardc6e19d32017-07-18 11:57:14 +0200152
Patrice Chotard170229e2017-07-18 11:57:13 +0200153reset_err:
154 ret = reset_release_all(priv->resets, priv->reset_count);
155 if (ret)
Patrice Chotard7c11e352018-03-14 17:48:58 +0100156 dev_err(dev, "failed to assert all resets\n");
Patrice Chotard37a197a2017-07-18 11:57:12 +0200157clk_err:
158 ret = clk_release_all(priv->clocks, priv->clock_count);
159 if (ret)
Patrice Chotard7c11e352018-03-14 17:48:58 +0100160 dev_err(dev, "failed to disable all clocks\n");
Patrice Chotard37a197a2017-07-18 11:57:12 +0200161
162 return err;
Alexey Brodkin83fd3122015-12-14 17:18:50 +0300163}
164
165static int ohci_usb_remove(struct udevice *dev)
166{
Patrice Chotard37a197a2017-07-18 11:57:12 +0200167 struct generic_ohci *priv = dev_get_priv(dev);
168 int ret;
169
170 ret = ohci_deregister(dev);
171 if (ret)
172 return ret;
173
Patrice Chotardab0b205e2018-03-14 17:48:57 +0100174 ret = ohci_shutdown_phy(dev);
175 if (ret)
176 return ret;
Patrice Chotardc6e19d32017-07-18 11:57:14 +0200177
Patrice Chotard170229e2017-07-18 11:57:13 +0200178 ret = reset_release_all(priv->resets, priv->reset_count);
179 if (ret)
180 return ret;
181
Patrice Chotard37a197a2017-07-18 11:57:12 +0200182 return clk_release_all(priv->clocks, priv->clock_count);
Alexey Brodkin83fd3122015-12-14 17:18:50 +0300183}
184
185static const struct udevice_id ohci_usb_ids[] = {
186 { .compatible = "generic-ohci" },
187 { }
188};
189
190U_BOOT_DRIVER(ohci_generic) = {
191 .name = "ohci_generic",
192 .id = UCLASS_USB,
193 .of_match = ohci_usb_ids,
194 .probe = ohci_usb_probe,
195 .remove = ohci_usb_remove,
196 .ops = &ohci_usb_ops,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700197 .priv_auto = sizeof(struct generic_ohci),
Alexey Brodkin83fd3122015-12-14 17:18:50 +0300198 .flags = DM_FLAG_ALLOC_PRIV_DMA,
199};