1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2015 Alexey Brodkin <abrodkin@synopsys.com>
8 #include <dm/device_compat.h>
10 #include <dm/ofnode.h>
11 #include <generic-phy.h>
16 #include <power/regulator.h>
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.
24 struct ehci_ctrl ctrl;
26 struct reset_ctl *resets;
28 #ifdef CONFIG_DM_REGULATOR
29 struct udevice *vbus_supply;
35 #ifdef CONFIG_DM_REGULATOR
36 static int ehci_enable_vbus_supply(struct udevice *dev)
38 struct generic_ehci *priv = dev_get_priv(dev);
41 ret = device_get_supply_regulator(dev, "vbus-supply",
43 if (ret && ret != -ENOENT)
46 if (priv->vbus_supply) {
47 ret = regulator_set_enable(priv->vbus_supply, true);
49 dev_err(dev, "Error enabling VBUS supply\n");
53 dev_dbg(dev, "No vbus supply\n");
59 static int ehci_disable_vbus_supply(struct generic_ehci *priv)
61 if (priv->vbus_supply)
62 return regulator_set_enable(priv->vbus_supply, false);
67 static int ehci_enable_vbus_supply(struct udevice *dev)
72 static int ehci_disable_vbus_supply(struct generic_ehci *priv)
78 static int ehci_usb_probe(struct udevice *dev)
80 struct generic_ehci *priv = dev_get_priv(dev);
81 struct ehci_hccr *hccr;
82 struct ehci_hcor *hcor;
83 int i, err, ret, clock_nb, reset_nb;
86 priv->clock_count = 0;
87 clock_nb = ofnode_count_phandle_with_args(dev_ofnode(dev), "clocks",
90 priv->clocks = devm_kcalloc(dev, clock_nb, sizeof(struct clk),
95 for (i = 0; i < clock_nb; i++) {
96 err = clk_get_by_index(dev, i, &priv->clocks[i]);
100 err = clk_enable(&priv->clocks[i]);
101 if (err && err != -ENOSYS) {
102 dev_err(dev, "failed to enable clock %d\n", i);
103 clk_free(&priv->clocks[i]);
109 if (clock_nb != -ENOENT) {
110 dev_err(dev, "failed to get clock phandle(%d)\n",
116 priv->reset_count = 0;
117 reset_nb = ofnode_count_phandle_with_args(dev_ofnode(dev), "resets",
120 priv->resets = devm_kcalloc(dev, reset_nb,
121 sizeof(struct reset_ctl),
126 for (i = 0; i < reset_nb; i++) {
127 err = reset_get_by_index(dev, i, &priv->resets[i]);
131 if (reset_deassert(&priv->resets[i])) {
132 dev_err(dev, "failed to deassert reset %d\n",
134 reset_free(&priv->resets[i]);
140 if (reset_nb != -ENOENT) {
141 dev_err(dev, "failed to get reset phandle(%d)\n",
147 err = ehci_enable_vbus_supply(dev);
151 err = ehci_setup_phy(dev, &priv->phy, 0);
155 hccr = map_physmem(dev_read_addr(dev), 0x100, MAP_NOCACHE);
156 hcor = (struct ehci_hcor *)((uintptr_t)hccr +
157 HC_LENGTH(ehci_readl(&hccr->cr_capbase)));
159 err = ehci_register(dev, hccr, hcor, NULL, 0, USB_INIT_HOST);
166 ret = ehci_shutdown_phy(dev, &priv->phy);
168 dev_err(dev, "failed to shutdown usb phy\n");
171 ret = ehci_disable_vbus_supply(priv);
173 dev_err(dev, "failed to disable VBUS supply\n");
176 ret = reset_release_all(priv->resets, priv->reset_count);
178 dev_err(dev, "failed to assert all resets\n");
180 ret = clk_release_all(priv->clocks, priv->clock_count);
182 dev_err(dev, "failed to disable all clocks\n");
187 static int ehci_usb_remove(struct udevice *dev)
189 struct generic_ehci *priv = dev_get_priv(dev);
192 ret = ehci_deregister(dev);
196 ret = ehci_shutdown_phy(dev, &priv->phy);
200 ret = ehci_disable_vbus_supply(priv);
204 ret = reset_release_all(priv->resets, priv->reset_count);
208 return clk_release_all(priv->clocks, priv->clock_count);
211 static const struct udevice_id ehci_usb_ids[] = {
212 { .compatible = "generic-ehci" },
216 U_BOOT_DRIVER(ehci_generic) = {
217 .name = "ehci_generic",
219 .of_match = ehci_usb_ids,
220 .probe = ehci_usb_probe,
221 .remove = ehci_usb_remove,
222 .ops = &ehci_usb_ops,
223 .priv_auto_alloc_size = sizeof(struct generic_ehci),
224 .flags = DM_FLAG_ALLOC_PRIV_DMA,