blob: 682a0703060f41d733d01f8ecab2473bd31abb5c [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>
Patrice Chotarda0809dc2017-07-18 11:57:10 +02008#include <dm/ofnode.h>
Patrice Chotard1a62cde2017-07-18 11:57:11 +02009#include <generic-phy.h>
Masahiro Yamada8a5a1802016-09-21 11:29:02 +090010#include <reset.h>
Marek Vasut851fecc2016-01-23 21:04:46 +010011#include <asm/io.h>
Alexey Brodkina6aff432015-12-02 12:32:02 +030012#include <dm.h>
13#include "ehci.h"
Patrice Chotardfc424682018-09-04 11:37:25 +020014#include <power/regulator.h>
Alexey Brodkina6aff432015-12-02 12:32:02 +030015
16/*
17 * Even though here we don't explicitly use "struct ehci_ctrl"
18 * ehci_register() expects it to be the first thing that resides in
19 * device's private data.
20 */
21struct generic_ehci {
22 struct ehci_ctrl ctrl;
Patrice Chotarda0809dc2017-07-18 11:57:10 +020023 struct clk *clocks;
24 struct reset_ctl *resets;
Patrice Chotard1a62cde2017-07-18 11:57:11 +020025 struct phy phy;
Patrice Chotardfc424682018-09-04 11:37:25 +020026#ifdef CONFIG_DM_REGULATOR
27 struct udevice *vbus_supply;
28#endif
Patrice Chotarda0809dc2017-07-18 11:57:10 +020029 int clock_count;
30 int reset_count;
Alexey Brodkina6aff432015-12-02 12:32:02 +030031};
32
Patrice Chotardfc424682018-09-04 11:37:25 +020033#ifdef CONFIG_DM_REGULATOR
34static int ehci_enable_vbus_supply(struct udevice *dev)
35{
36 struct generic_ehci *priv = dev_get_priv(dev);
37 int ret;
38
39 ret = device_get_supply_regulator(dev, "vbus-supply",
40 &priv->vbus_supply);
41 if (ret && ret != -ENOENT)
42 return ret;
43
44 if (priv->vbus_supply) {
45 ret = regulator_set_enable(priv->vbus_supply, true);
46 if (ret) {
47 dev_err(dev, "Error enabling VBUS supply\n");
48 return ret;
49 }
50 } else {
51 dev_dbg(dev, "No vbus supply\n");
52 }
53
54 return 0;
55}
56
57static int ehci_disable_vbus_supply(struct generic_ehci *priv)
58{
59 if (priv->vbus_supply)
60 return regulator_set_enable(priv->vbus_supply, false);
61 else
62 return 0;
63}
64#else
65static int ehci_enable_vbus_supply(struct udevice *dev)
66{
67 return 0;
68}
69
70static int ehci_disable_vbus_supply(struct generic_ehci *priv)
71{
72 return 0;
73}
74#endif
75
Alexey Brodkina6aff432015-12-02 12:32:02 +030076static int ehci_usb_probe(struct udevice *dev)
77{
Patrice Chotarda0809dc2017-07-18 11:57:10 +020078 struct generic_ehci *priv = dev_get_priv(dev);
Marek Vasut851fecc2016-01-23 21:04:46 +010079 struct ehci_hccr *hccr;
Alexey Brodkina6aff432015-12-02 12:32:02 +030080 struct ehci_hcor *hcor;
Patrice Chotarda0809dc2017-07-18 11:57:10 +020081 int i, err, ret, clock_nb, reset_nb;
Masahiro Yamadafbb66bf2016-01-25 15:00:36 +090082
Patrice Chotarda0809dc2017-07-18 11:57:10 +020083 err = 0;
84 priv->clock_count = 0;
85 clock_nb = ofnode_count_phandle_with_args(dev_ofnode(dev), "clocks",
86 "#clock-cells");
87 if (clock_nb > 0) {
88 priv->clocks = devm_kcalloc(dev, clock_nb, sizeof(struct clk),
89 GFP_KERNEL);
90 if (!priv->clocks)
91 return -ENOMEM;
Masahiro Yamadafbb66bf2016-01-25 15:00:36 +090092
Patrice Chotarda0809dc2017-07-18 11:57:10 +020093 for (i = 0; i < clock_nb; i++) {
94 err = clk_get_by_index(dev, i, &priv->clocks[i]);
95
96 if (err < 0)
97 break;
98 err = clk_enable(&priv->clocks[i]);
Kever Yang481869e2019-08-28 16:23:46 +080099 if (err && err != -ENOSYS) {
Patrice Chotardf2cbba02018-03-14 17:48:55 +0100100 dev_err(dev, "failed to enable clock %d\n", i);
Patrice Chotarda0809dc2017-07-18 11:57:10 +0200101 clk_free(&priv->clocks[i]);
102 goto clk_err;
103 }
104 priv->clock_count++;
105 }
106 } else {
107 if (clock_nb != -ENOENT) {
Patrice Chotardf2cbba02018-03-14 17:48:55 +0100108 dev_err(dev, "failed to get clock phandle(%d)\n",
109 clock_nb);
Patrice Chotarda0809dc2017-07-18 11:57:10 +0200110 return clock_nb;
111 }
Masahiro Yamadafbb66bf2016-01-25 15:00:36 +0900112 }
Alexey Brodkina6aff432015-12-02 12:32:02 +0300113
Patrice Chotarda0809dc2017-07-18 11:57:10 +0200114 priv->reset_count = 0;
115 reset_nb = ofnode_count_phandle_with_args(dev_ofnode(dev), "resets",
116 "#reset-cells");
117 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;
Masahiro Yamada8a5a1802016-09-21 11:29:02 +0900123
Patrice Chotarda0809dc2017-07-18 11:57:10 +0200124 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 if (reset_deassert(&priv->resets[i])) {
Patrice Chotardf2cbba02018-03-14 17:48:55 +0100130 dev_err(dev, "failed to deassert reset %d\n",
131 i);
Patrice Chotarda0809dc2017-07-18 11:57:10 +0200132 reset_free(&priv->resets[i]);
133 goto reset_err;
134 }
135 priv->reset_count++;
136 }
137 } else {
138 if (reset_nb != -ENOENT) {
Patrice Chotardf2cbba02018-03-14 17:48:55 +0100139 dev_err(dev, "failed to get reset phandle(%d)\n",
140 reset_nb);
Patrice Chotarda0809dc2017-07-18 11:57:10 +0200141 goto clk_err;
142 }
Masahiro Yamada8a5a1802016-09-21 11:29:02 +0900143 }
144
Patrice Chotardfc424682018-09-04 11:37:25 +0200145 err = ehci_enable_vbus_supply(dev);
Patrice Chotard36afc032018-03-14 17:48:54 +0100146 if (err)
Patrice Chotard36afc032018-03-14 17:48:54 +0100147 goto reset_err;
Patrice Chotard1a62cde2017-07-18 11:57:11 +0200148
Patrice Chotardfc424682018-09-04 11:37:25 +0200149 err = ehci_setup_phy(dev, &priv->phy, 0);
150 if (err)
151 goto regulator_err;
152
Philipp Tomsich9f3f4232017-09-12 17:32:28 +0200153 hccr = map_physmem(dev_read_addr(dev), 0x100, MAP_NOCACHE);
Alexey Brodkina6aff432015-12-02 12:32:02 +0300154 hcor = (struct ehci_hcor *)((uintptr_t)hccr +
155 HC_LENGTH(ehci_readl(&hccr->cr_capbase)));
156
Patrice Chotarda0809dc2017-07-18 11:57:10 +0200157 err = ehci_register(dev, hccr, hcor, NULL, 0, USB_INIT_HOST);
158 if (err)
Patrice Chotard1a62cde2017-07-18 11:57:11 +0200159 goto phy_err;
Patrice Chotarda0809dc2017-07-18 11:57:10 +0200160
161 return 0;
162
Patrice Chotard1a62cde2017-07-18 11:57:11 +0200163phy_err:
Marek Vasutd9af6cd2018-08-08 14:29:55 +0200164 ret = ehci_shutdown_phy(dev, &priv->phy);
Patrice Chotard36afc032018-03-14 17:48:54 +0100165 if (ret)
166 dev_err(dev, "failed to shutdown usb phy\n");
Patrice Chotard1a62cde2017-07-18 11:57:11 +0200167
Patrice Chotardfc424682018-09-04 11:37:25 +0200168regulator_err:
169 ret = ehci_disable_vbus_supply(priv);
170 if (ret)
171 dev_err(dev, "failed to disable VBUS supply\n");
172
Patrice Chotarda0809dc2017-07-18 11:57:10 +0200173reset_err:
174 ret = reset_release_all(priv->resets, priv->reset_count);
175 if (ret)
Patrice Chotardf2cbba02018-03-14 17:48:55 +0100176 dev_err(dev, "failed to assert all resets\n");
Patrice Chotarda0809dc2017-07-18 11:57:10 +0200177clk_err:
178 ret = clk_release_all(priv->clocks, priv->clock_count);
179 if (ret)
Patrice Chotardf2cbba02018-03-14 17:48:55 +0100180 dev_err(dev, "failed to disable all clocks\n");
Patrice Chotarda0809dc2017-07-18 11:57:10 +0200181
182 return err;
183}
184
185static int ehci_usb_remove(struct udevice *dev)
186{
187 struct generic_ehci *priv = dev_get_priv(dev);
188 int ret;
189
190 ret = ehci_deregister(dev);
191 if (ret)
192 return ret;
193
Marek Vasutd9af6cd2018-08-08 14:29:55 +0200194 ret = ehci_shutdown_phy(dev, &priv->phy);
Patrice Chotard36afc032018-03-14 17:48:54 +0100195 if (ret)
196 return ret;
Patrice Chotard1a62cde2017-07-18 11:57:11 +0200197
Patrice Chotardfc424682018-09-04 11:37:25 +0200198 ret = ehci_disable_vbus_supply(priv);
199 if (ret)
200 return ret;
201
Patrice Chotarda0809dc2017-07-18 11:57:10 +0200202 ret = reset_release_all(priv->resets, priv->reset_count);
203 if (ret)
204 return ret;
205
206 return clk_release_all(priv->clocks, priv->clock_count);
Alexey Brodkina6aff432015-12-02 12:32:02 +0300207}
208
Alexey Brodkina6aff432015-12-02 12:32:02 +0300209static const struct udevice_id ehci_usb_ids[] = {
210 { .compatible = "generic-ehci" },
211 { }
212};
213
214U_BOOT_DRIVER(ehci_generic) = {
215 .name = "ehci_generic",
216 .id = UCLASS_USB,
217 .of_match = ehci_usb_ids,
218 .probe = ehci_usb_probe,
Patrice Chotarda0809dc2017-07-18 11:57:10 +0200219 .remove = ehci_usb_remove,
Alexey Brodkina6aff432015-12-02 12:32:02 +0300220 .ops = &ehci_usb_ops,
221 .priv_auto_alloc_size = sizeof(struct generic_ehci),
222 .flags = DM_FLAG_ALLOC_PRIV_DMA,
223};