1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2015 Alexey Brodkin <abrodkin@synopsys.com>
9 #include <dm/device_compat.h>
10 #include <dm/devres.h>
11 #include <dm/ofnode.h>
12 #include <generic-phy.h>
17 #include <power/regulator.h>
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.
25 struct ehci_ctrl ctrl;
26 struct clk_bulk clocks;
27 struct reset_ctl_bulk resets;
29 struct udevice *vbus_supply;
32 static int ehci_enable_vbus_supply(struct udevice *dev)
34 struct generic_ehci *priv = dev_get_priv(dev);
37 ret = device_get_supply_regulator(dev, "vbus-supply",
39 if (ret && ret != -ENOENT)
42 if (priv->vbus_supply) {
43 ret = regulator_set_enable(priv->vbus_supply, true);
45 dev_err(dev, "Error enabling VBUS supply (ret=%d)\n", ret);
49 dev_dbg(dev, "No vbus supply\n");
55 static int ehci_disable_vbus_supply(struct generic_ehci *priv)
57 if (priv->vbus_supply)
58 return regulator_set_enable(priv->vbus_supply, false);
63 static int ehci_usb_probe(struct udevice *dev)
65 struct generic_ehci *priv = dev_get_priv(dev);
66 struct ehci_hccr *hccr;
67 struct ehci_hcor *hcor;
71 ret = clk_get_bulk(dev, &priv->clocks);
72 if (ret && ret != -ENOENT) {
73 dev_err(dev, "Failed to get clocks (ret=%d)\n", ret);
77 err = clk_enable_bulk(&priv->clocks);
79 dev_err(dev, "Failed to enable clocks (err=%d)\n", err);
83 err = reset_get_bulk(dev, &priv->resets);
84 if (ret && ret != -ENOENT) {
85 dev_err(dev, "Failed to get resets (err=%d)\n", err);
89 err = reset_deassert_bulk(&priv->resets);
91 dev_err(dev, "Failed to get deassert resets (err=%d)\n", err);
95 err = ehci_enable_vbus_supply(dev);
99 err = ehci_setup_phy(dev, &priv->phy, 0);
103 hccr = map_physmem(dev_read_addr(dev), 0x100, MAP_NOCACHE);
104 hcor = (struct ehci_hcor *)((uintptr_t)hccr +
105 HC_LENGTH(ehci_readl(&hccr->cr_capbase)));
107 err = ehci_register(dev, hccr, hcor, NULL, 0, USB_INIT_HOST);
114 ret = ehci_shutdown_phy(dev, &priv->phy);
116 dev_err(dev, "failed to shutdown usb phy (ret=%d)\n", ret);
119 ret = ehci_disable_vbus_supply(priv);
121 dev_err(dev, "failed to disable VBUS supply (ret=%d)\n", ret);
124 ret = reset_release_bulk(&priv->resets);
126 dev_err(dev, "failed to release resets (ret=%d)\n", ret);
128 ret = clk_release_bulk(&priv->clocks);
130 dev_err(dev, "failed to release clocks (ret=%d)\n", ret);
135 static int ehci_usb_remove(struct udevice *dev)
137 struct generic_ehci *priv = dev_get_priv(dev);
140 ret = ehci_deregister(dev);
144 ret = ehci_shutdown_phy(dev, &priv->phy);
148 ret = ehci_disable_vbus_supply(priv);
152 ret = reset_release_bulk(&priv->resets);
156 return clk_release_bulk(&priv->clocks);
159 static const struct udevice_id ehci_usb_ids[] = {
160 { .compatible = "generic-ehci" },
164 U_BOOT_DRIVER(ehci_generic) = {
165 .name = "ehci_generic",
167 .of_match = ehci_usb_ids,
168 .probe = ehci_usb_probe,
169 .remove = ehci_usb_remove,
170 .ops = &ehci_usb_ops,
171 .priv_auto = sizeof(struct generic_ehci),
172 .flags = DM_FLAG_ALLOC_PRIV_DMA,