blob: 0643681846139f461bf4abc144421b8e2d43c92c [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Alexey Brodkin90fbb282015-12-02 12:32:02 +03002/*
3 * Copyright (C) 2015 Alexey Brodkin <abrodkin@synopsys.com>
Alexey Brodkin90fbb282015-12-02 12:32:02 +03004 */
5
6#include <common.h>
Masahiro Yamada4feefdc2016-01-25 15:00:36 +09007#include <clk.h>
Simon Glass336d4612020-02-03 07:36:16 -07008#include <dm/device_compat.h>
Simon Glass61b29b82020-02-03 07:36:15 -07009#include <dm/devres.h>
Patrice Chotarda1cee8e2017-07-18 11:57:10 +020010#include <dm/ofnode.h>
Patrice Chotard0d0ba1a2017-07-18 11:57:11 +020011#include <generic-phy.h>
Masahiro Yamada8824cfc2016-09-21 11:29:02 +090012#include <reset.h>
Marek Vasut643cacb2016-01-23 21:04:46 +010013#include <asm/io.h>
Alexey Brodkin90fbb282015-12-02 12:32:02 +030014#include <dm.h>
15#include "ehci.h"
Patrice Chotard5c349e12018-09-04 11:37:25 +020016#include <power/regulator.h>
Alexey Brodkin90fbb282015-12-02 12:32:02 +030017
18/*
19 * Even though here we don't explicitly use "struct ehci_ctrl"
20 * ehci_register() expects it to be the first thing that resides in
21 * device's private data.
22 */
23struct generic_ehci {
24 struct ehci_ctrl ctrl;
Patrice Chotarda1cee8e2017-07-18 11:57:10 +020025 struct clk *clocks;
26 struct reset_ctl *resets;
Patrice Chotard0d0ba1a2017-07-18 11:57:11 +020027 struct phy phy;
Patrice Chotard5c349e12018-09-04 11:37:25 +020028#ifdef CONFIG_DM_REGULATOR
29 struct udevice *vbus_supply;
30#endif
Patrice Chotarda1cee8e2017-07-18 11:57:10 +020031 int clock_count;
32 int reset_count;
Alexey Brodkin90fbb282015-12-02 12:32:02 +030033};
34
Patrice Chotard5c349e12018-09-04 11:37:25 +020035#ifdef CONFIG_DM_REGULATOR
36static int ehci_enable_vbus_supply(struct udevice *dev)
37{
38 struct generic_ehci *priv = dev_get_priv(dev);
39 int ret;
40
41 ret = device_get_supply_regulator(dev, "vbus-supply",
42 &priv->vbus_supply);
43 if (ret && ret != -ENOENT)
44 return ret;
45
46 if (priv->vbus_supply) {
47 ret = regulator_set_enable(priv->vbus_supply, true);
48 if (ret) {
49 dev_err(dev, "Error enabling VBUS supply\n");
50 return ret;
51 }
52 } else {
53 dev_dbg(dev, "No vbus supply\n");
54 }
55
56 return 0;
57}
58
59static int ehci_disable_vbus_supply(struct generic_ehci *priv)
60{
61 if (priv->vbus_supply)
62 return regulator_set_enable(priv->vbus_supply, false);
63 else
64 return 0;
65}
66#else
67static int ehci_enable_vbus_supply(struct udevice *dev)
68{
69 return 0;
70}
71
72static int ehci_disable_vbus_supply(struct generic_ehci *priv)
73{
74 return 0;
75}
76#endif
77
Alexey Brodkin90fbb282015-12-02 12:32:02 +030078static int ehci_usb_probe(struct udevice *dev)
79{
Patrice Chotarda1cee8e2017-07-18 11:57:10 +020080 struct generic_ehci *priv = dev_get_priv(dev);
Marek Vasut643cacb2016-01-23 21:04:46 +010081 struct ehci_hccr *hccr;
Alexey Brodkin90fbb282015-12-02 12:32:02 +030082 struct ehci_hcor *hcor;
Patrice Chotarda1cee8e2017-07-18 11:57:10 +020083 int i, err, ret, clock_nb, reset_nb;
Masahiro Yamada4feefdc2016-01-25 15:00:36 +090084
Patrice Chotarda1cee8e2017-07-18 11:57:10 +020085 err = 0;
86 priv->clock_count = 0;
87 clock_nb = ofnode_count_phandle_with_args(dev_ofnode(dev), "clocks",
88 "#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;
Masahiro Yamada4feefdc2016-01-25 15:00:36 +090094
Patrice Chotarda1cee8e2017-07-18 11:57:10 +020095 for (i = 0; i < clock_nb; i++) {
96 err = clk_get_by_index(dev, i, &priv->clocks[i]);
97
98 if (err < 0)
99 break;
100 err = clk_enable(&priv->clocks[i]);
Kever Yang54a0c7b2019-08-28 16:23:46 +0800101 if (err && err != -ENOSYS) {
Patrice Chotarddf7777a2018-03-14 17:48:55 +0100102 dev_err(dev, "failed to enable clock %d\n", i);
Patrice Chotarda1cee8e2017-07-18 11:57:10 +0200103 clk_free(&priv->clocks[i]);
104 goto clk_err;
105 }
106 priv->clock_count++;
107 }
108 } else {
109 if (clock_nb != -ENOENT) {
Patrice Chotarddf7777a2018-03-14 17:48:55 +0100110 dev_err(dev, "failed to get clock phandle(%d)\n",
111 clock_nb);
Patrice Chotarda1cee8e2017-07-18 11:57:10 +0200112 return clock_nb;
113 }
Masahiro Yamada4feefdc2016-01-25 15:00:36 +0900114 }
Alexey Brodkin90fbb282015-12-02 12:32:02 +0300115
Patrice Chotarda1cee8e2017-07-18 11:57:10 +0200116 priv->reset_count = 0;
117 reset_nb = ofnode_count_phandle_with_args(dev_ofnode(dev), "resets",
118 "#reset-cells");
119 if (reset_nb > 0) {
120 priv->resets = devm_kcalloc(dev, reset_nb,
121 sizeof(struct reset_ctl),
122 GFP_KERNEL);
123 if (!priv->resets)
124 return -ENOMEM;
Masahiro Yamada8824cfc2016-09-21 11:29:02 +0900125
Patrice Chotarda1cee8e2017-07-18 11:57:10 +0200126 for (i = 0; i < reset_nb; i++) {
127 err = reset_get_by_index(dev, i, &priv->resets[i]);
128 if (err < 0)
129 break;
130
131 if (reset_deassert(&priv->resets[i])) {
Patrice Chotarddf7777a2018-03-14 17:48:55 +0100132 dev_err(dev, "failed to deassert reset %d\n",
133 i);
Patrice Chotarda1cee8e2017-07-18 11:57:10 +0200134 reset_free(&priv->resets[i]);
135 goto reset_err;
136 }
137 priv->reset_count++;
138 }
139 } else {
140 if (reset_nb != -ENOENT) {
Patrice Chotarddf7777a2018-03-14 17:48:55 +0100141 dev_err(dev, "failed to get reset phandle(%d)\n",
142 reset_nb);
Patrice Chotarda1cee8e2017-07-18 11:57:10 +0200143 goto clk_err;
144 }
Masahiro Yamada8824cfc2016-09-21 11:29:02 +0900145 }
146
Patrice Chotard5c349e12018-09-04 11:37:25 +0200147 err = ehci_enable_vbus_supply(dev);
Patrice Chotard20f06a42018-03-14 17:48:54 +0100148 if (err)
Patrice Chotard20f06a42018-03-14 17:48:54 +0100149 goto reset_err;
Patrice Chotard0d0ba1a2017-07-18 11:57:11 +0200150
Patrice Chotard5c349e12018-09-04 11:37:25 +0200151 err = ehci_setup_phy(dev, &priv->phy, 0);
152 if (err)
153 goto regulator_err;
154
Philipp Tomsich6e652e32017-09-12 17:32:28 +0200155 hccr = map_physmem(dev_read_addr(dev), 0x100, MAP_NOCACHE);
Alexey Brodkin90fbb282015-12-02 12:32:02 +0300156 hcor = (struct ehci_hcor *)((uintptr_t)hccr +
157 HC_LENGTH(ehci_readl(&hccr->cr_capbase)));
158
Patrice Chotarda1cee8e2017-07-18 11:57:10 +0200159 err = ehci_register(dev, hccr, hcor, NULL, 0, USB_INIT_HOST);
160 if (err)
Patrice Chotard0d0ba1a2017-07-18 11:57:11 +0200161 goto phy_err;
Patrice Chotarda1cee8e2017-07-18 11:57:10 +0200162
163 return 0;
164
Patrice Chotard0d0ba1a2017-07-18 11:57:11 +0200165phy_err:
Marek Vasutb43cdf92018-08-08 14:29:55 +0200166 ret = ehci_shutdown_phy(dev, &priv->phy);
Patrice Chotard20f06a42018-03-14 17:48:54 +0100167 if (ret)
168 dev_err(dev, "failed to shutdown usb phy\n");
Patrice Chotard0d0ba1a2017-07-18 11:57:11 +0200169
Patrice Chotard5c349e12018-09-04 11:37:25 +0200170regulator_err:
171 ret = ehci_disable_vbus_supply(priv);
172 if (ret)
173 dev_err(dev, "failed to disable VBUS supply\n");
174
Patrice Chotarda1cee8e2017-07-18 11:57:10 +0200175reset_err:
176 ret = reset_release_all(priv->resets, priv->reset_count);
177 if (ret)
Patrice Chotarddf7777a2018-03-14 17:48:55 +0100178 dev_err(dev, "failed to assert all resets\n");
Patrice Chotarda1cee8e2017-07-18 11:57:10 +0200179clk_err:
180 ret = clk_release_all(priv->clocks, priv->clock_count);
181 if (ret)
Patrice Chotarddf7777a2018-03-14 17:48:55 +0100182 dev_err(dev, "failed to disable all clocks\n");
Patrice Chotarda1cee8e2017-07-18 11:57:10 +0200183
184 return err;
185}
186
187static int ehci_usb_remove(struct udevice *dev)
188{
189 struct generic_ehci *priv = dev_get_priv(dev);
190 int ret;
191
192 ret = ehci_deregister(dev);
193 if (ret)
194 return ret;
195
Marek Vasutb43cdf92018-08-08 14:29:55 +0200196 ret = ehci_shutdown_phy(dev, &priv->phy);
Patrice Chotard20f06a42018-03-14 17:48:54 +0100197 if (ret)
198 return ret;
Patrice Chotard0d0ba1a2017-07-18 11:57:11 +0200199
Patrice Chotard5c349e12018-09-04 11:37:25 +0200200 ret = ehci_disable_vbus_supply(priv);
201 if (ret)
202 return ret;
203
Patrice Chotarda1cee8e2017-07-18 11:57:10 +0200204 ret = reset_release_all(priv->resets, priv->reset_count);
205 if (ret)
206 return ret;
207
208 return clk_release_all(priv->clocks, priv->clock_count);
Alexey Brodkin90fbb282015-12-02 12:32:02 +0300209}
210
Alexey Brodkin90fbb282015-12-02 12:32:02 +0300211static const struct udevice_id ehci_usb_ids[] = {
212 { .compatible = "generic-ehci" },
213 { }
214};
215
216U_BOOT_DRIVER(ehci_generic) = {
217 .name = "ehci_generic",
218 .id = UCLASS_USB,
219 .of_match = ehci_usb_ids,
220 .probe = ehci_usb_probe,
Patrice Chotarda1cee8e2017-07-18 11:57:10 +0200221 .remove = ehci_usb_remove,
Alexey Brodkin90fbb282015-12-02 12:32:02 +0300222 .ops = &ehci_usb_ops,
223 .priv_auto_alloc_size = sizeof(struct generic_ehci),
224 .flags = DM_FLAG_ALLOC_PRIV_DMA,
225};