blob: c93a7051a795b6add03699b5c0a314578e646a57 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Alexey Brodkina6aff432015-12-02 12:32:02 +03002/*
3 * Copyright (C) 2015 Alexey Brodkin <abrodkin@synopsys.com>
Alexey Brodkina6aff432015-12-02 12:32:02 +03004 */
5
6#include <common.h>
Masahiro Yamadafbb66bf2016-01-25 15:00:36 +09007#include <clk.h>
Simon Glass0f2af882020-05-10 11:40:05 -06008#include <log.h>
Simon Glass9bc15642020-02-03 07:36:16 -07009#include <dm/device_compat.h>
Simon Glassd66c5f72020-02-03 07:36:15 -070010#include <dm/devres.h>
Patrice Chotarda0809dc2017-07-18 11:57:10 +020011#include <dm/ofnode.h>
Patrice Chotard1a62cde2017-07-18 11:57:11 +020012#include <generic-phy.h>
Masahiro Yamada8a5a1802016-09-21 11:29:02 +090013#include <reset.h>
Marek Vasut851fecc2016-01-23 21:04:46 +010014#include <asm/io.h>
Alexey Brodkina6aff432015-12-02 12:32:02 +030015#include <dm.h>
16#include "ehci.h"
Patrice Chotardfc424682018-09-04 11:37:25 +020017#include <power/regulator.h>
Alexey Brodkina6aff432015-12-02 12:32:02 +030018
19/*
20 * Even though here we don't explicitly use "struct ehci_ctrl"
21 * ehci_register() expects it to be the first thing that resides in
22 * device's private data.
23 */
24struct generic_ehci {
25 struct ehci_ctrl ctrl;
Patrice Chotarda0809dc2017-07-18 11:57:10 +020026 struct clk *clocks;
27 struct reset_ctl *resets;
Patrice Chotard1a62cde2017-07-18 11:57:11 +020028 struct phy phy;
Patrice Chotardfc424682018-09-04 11:37:25 +020029#ifdef CONFIG_DM_REGULATOR
30 struct udevice *vbus_supply;
31#endif
Patrice Chotarda0809dc2017-07-18 11:57:10 +020032 int clock_count;
33 int reset_count;
Alexey Brodkina6aff432015-12-02 12:32:02 +030034};
35
Patrice Chotardfc424682018-09-04 11:37:25 +020036#ifdef CONFIG_DM_REGULATOR
37static int ehci_enable_vbus_supply(struct udevice *dev)
38{
39 struct generic_ehci *priv = dev_get_priv(dev);
40 int ret;
41
42 ret = device_get_supply_regulator(dev, "vbus-supply",
43 &priv->vbus_supply);
44 if (ret && ret != -ENOENT)
45 return ret;
46
47 if (priv->vbus_supply) {
48 ret = regulator_set_enable(priv->vbus_supply, true);
49 if (ret) {
50 dev_err(dev, "Error enabling VBUS supply\n");
51 return ret;
52 }
53 } else {
54 dev_dbg(dev, "No vbus supply\n");
55 }
56
57 return 0;
58}
59
60static int ehci_disable_vbus_supply(struct generic_ehci *priv)
61{
62 if (priv->vbus_supply)
63 return regulator_set_enable(priv->vbus_supply, false);
64 else
65 return 0;
66}
67#else
68static int ehci_enable_vbus_supply(struct udevice *dev)
69{
70 return 0;
71}
72
73static int ehci_disable_vbus_supply(struct generic_ehci *priv)
74{
75 return 0;
76}
77#endif
78
Alexey Brodkina6aff432015-12-02 12:32:02 +030079static int ehci_usb_probe(struct udevice *dev)
80{
Patrice Chotarda0809dc2017-07-18 11:57:10 +020081 struct generic_ehci *priv = dev_get_priv(dev);
Marek Vasut851fecc2016-01-23 21:04:46 +010082 struct ehci_hccr *hccr;
Alexey Brodkina6aff432015-12-02 12:32:02 +030083 struct ehci_hcor *hcor;
Patrice Chotarda0809dc2017-07-18 11:57:10 +020084 int i, err, ret, clock_nb, reset_nb;
Masahiro Yamadafbb66bf2016-01-25 15:00:36 +090085
Patrice Chotarda0809dc2017-07-18 11:57:10 +020086 err = 0;
87 priv->clock_count = 0;
88 clock_nb = ofnode_count_phandle_with_args(dev_ofnode(dev), "clocks",
Patrick Delaunayd776a842020-09-25 09:41:14 +020089 "#clock-cells", 0);
Patrice Chotarda0809dc2017-07-18 11:57:10 +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;
Masahiro Yamadafbb66bf2016-01-25 15:00:36 +090095
Patrice Chotarda0809dc2017-07-18 11:57:10 +020096 for (i = 0; i < clock_nb; i++) {
97 err = clk_get_by_index(dev, i, &priv->clocks[i]);
98
99 if (err < 0)
100 break;
101 err = clk_enable(&priv->clocks[i]);
Kever Yang481869e2019-08-28 16:23:46 +0800102 if (err && err != -ENOSYS) {
Patrice Chotardf2cbba02018-03-14 17:48:55 +0100103 dev_err(dev, "failed to enable clock %d\n", i);
Patrice Chotarda0809dc2017-07-18 11:57:10 +0200104 clk_free(&priv->clocks[i]);
105 goto clk_err;
106 }
107 priv->clock_count++;
108 }
109 } else {
110 if (clock_nb != -ENOENT) {
Patrice Chotardf2cbba02018-03-14 17:48:55 +0100111 dev_err(dev, "failed to get clock phandle(%d)\n",
112 clock_nb);
Patrice Chotarda0809dc2017-07-18 11:57:10 +0200113 return clock_nb;
114 }
Masahiro Yamadafbb66bf2016-01-25 15:00:36 +0900115 }
Alexey Brodkina6aff432015-12-02 12:32:02 +0300116
Patrice Chotarda0809dc2017-07-18 11:57:10 +0200117 priv->reset_count = 0;
118 reset_nb = ofnode_count_phandle_with_args(dev_ofnode(dev), "resets",
Patrick Delaunayd776a842020-09-25 09:41:14 +0200119 "#reset-cells", 0);
Patrice Chotarda0809dc2017-07-18 11:57:10 +0200120 if (reset_nb > 0) {
121 priv->resets = devm_kcalloc(dev, reset_nb,
122 sizeof(struct reset_ctl),
123 GFP_KERNEL);
124 if (!priv->resets)
125 return -ENOMEM;
Masahiro Yamada8a5a1802016-09-21 11:29:02 +0900126
Patrice Chotarda0809dc2017-07-18 11:57:10 +0200127 for (i = 0; i < reset_nb; i++) {
128 err = reset_get_by_index(dev, i, &priv->resets[i]);
129 if (err < 0)
130 break;
131
132 if (reset_deassert(&priv->resets[i])) {
Patrice Chotardf2cbba02018-03-14 17:48:55 +0100133 dev_err(dev, "failed to deassert reset %d\n",
134 i);
Patrice Chotarda0809dc2017-07-18 11:57:10 +0200135 reset_free(&priv->resets[i]);
136 goto reset_err;
137 }
138 priv->reset_count++;
139 }
140 } else {
141 if (reset_nb != -ENOENT) {
Patrice Chotardf2cbba02018-03-14 17:48:55 +0100142 dev_err(dev, "failed to get reset phandle(%d)\n",
143 reset_nb);
Patrice Chotarda0809dc2017-07-18 11:57:10 +0200144 goto clk_err;
145 }
Masahiro Yamada8a5a1802016-09-21 11:29:02 +0900146 }
147
Patrice Chotardfc424682018-09-04 11:37:25 +0200148 err = ehci_enable_vbus_supply(dev);
Patrice Chotard36afc032018-03-14 17:48:54 +0100149 if (err)
Patrice Chotard36afc032018-03-14 17:48:54 +0100150 goto reset_err;
Patrice Chotard1a62cde2017-07-18 11:57:11 +0200151
Patrice Chotardfc424682018-09-04 11:37:25 +0200152 err = ehci_setup_phy(dev, &priv->phy, 0);
153 if (err)
154 goto regulator_err;
155
Philipp Tomsich9f3f4232017-09-12 17:32:28 +0200156 hccr = map_physmem(dev_read_addr(dev), 0x100, MAP_NOCACHE);
Alexey Brodkina6aff432015-12-02 12:32:02 +0300157 hcor = (struct ehci_hcor *)((uintptr_t)hccr +
158 HC_LENGTH(ehci_readl(&hccr->cr_capbase)));
159
Patrice Chotarda0809dc2017-07-18 11:57:10 +0200160 err = ehci_register(dev, hccr, hcor, NULL, 0, USB_INIT_HOST);
161 if (err)
Patrice Chotard1a62cde2017-07-18 11:57:11 +0200162 goto phy_err;
Patrice Chotarda0809dc2017-07-18 11:57:10 +0200163
164 return 0;
165
Patrice Chotard1a62cde2017-07-18 11:57:11 +0200166phy_err:
Marek Vasutd9af6cd2018-08-08 14:29:55 +0200167 ret = ehci_shutdown_phy(dev, &priv->phy);
Patrice Chotard36afc032018-03-14 17:48:54 +0100168 if (ret)
169 dev_err(dev, "failed to shutdown usb phy\n");
Patrice Chotard1a62cde2017-07-18 11:57:11 +0200170
Patrice Chotardfc424682018-09-04 11:37:25 +0200171regulator_err:
172 ret = ehci_disable_vbus_supply(priv);
173 if (ret)
174 dev_err(dev, "failed to disable VBUS supply\n");
175
Patrice Chotarda0809dc2017-07-18 11:57:10 +0200176reset_err:
177 ret = reset_release_all(priv->resets, priv->reset_count);
178 if (ret)
Patrice Chotardf2cbba02018-03-14 17:48:55 +0100179 dev_err(dev, "failed to assert all resets\n");
Patrice Chotarda0809dc2017-07-18 11:57:10 +0200180clk_err:
181 ret = clk_release_all(priv->clocks, priv->clock_count);
182 if (ret)
Patrice Chotardf2cbba02018-03-14 17:48:55 +0100183 dev_err(dev, "failed to disable all clocks\n");
Patrice Chotarda0809dc2017-07-18 11:57:10 +0200184
185 return err;
186}
187
188static int ehci_usb_remove(struct udevice *dev)
189{
190 struct generic_ehci *priv = dev_get_priv(dev);
191 int ret;
192
193 ret = ehci_deregister(dev);
194 if (ret)
195 return ret;
196
Marek Vasutd9af6cd2018-08-08 14:29:55 +0200197 ret = ehci_shutdown_phy(dev, &priv->phy);
Patrice Chotard36afc032018-03-14 17:48:54 +0100198 if (ret)
199 return ret;
Patrice Chotard1a62cde2017-07-18 11:57:11 +0200200
Patrice Chotardfc424682018-09-04 11:37:25 +0200201 ret = ehci_disable_vbus_supply(priv);
202 if (ret)
203 return ret;
204
Patrice Chotarda0809dc2017-07-18 11:57:10 +0200205 ret = reset_release_all(priv->resets, priv->reset_count);
206 if (ret)
207 return ret;
208
209 return clk_release_all(priv->clocks, priv->clock_count);
Alexey Brodkina6aff432015-12-02 12:32:02 +0300210}
211
Alexey Brodkina6aff432015-12-02 12:32:02 +0300212static const struct udevice_id ehci_usb_ids[] = {
213 { .compatible = "generic-ehci" },
214 { }
215};
216
217U_BOOT_DRIVER(ehci_generic) = {
218 .name = "ehci_generic",
219 .id = UCLASS_USB,
220 .of_match = ehci_usb_ids,
221 .probe = ehci_usb_probe,
Patrice Chotarda0809dc2017-07-18 11:57:10 +0200222 .remove = ehci_usb_remove,
Alexey Brodkina6aff432015-12-02 12:32:02 +0300223 .ops = &ehci_usb_ops,
224 .priv_auto_alloc_size = sizeof(struct generic_ehci),
225 .flags = DM_FLAG_ALLOC_PRIV_DMA,
226};