blob: 936e30438d9ff32e1d95fa04fbc861c31ebc94fe [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 Glassf7ae49f2020-05-10 11:40:05 -06008#include <log.h>
Simon Glass336d4612020-02-03 07:36:16 -07009#include <dm/device_compat.h>
Simon Glass61b29b82020-02-03 07:36:15 -070010#include <dm/devres.h>
Patrice Chotarda1cee8e2017-07-18 11:57:10 +020011#include <dm/ofnode.h>
Patrice Chotard0d0ba1a2017-07-18 11:57:11 +020012#include <generic-phy.h>
Masahiro Yamada8824cfc2016-09-21 11:29:02 +090013#include <reset.h>
Marek Vasut643cacb2016-01-23 21:04:46 +010014#include <asm/io.h>
Alexey Brodkin90fbb282015-12-02 12:32:02 +030015#include <dm.h>
16#include "ehci.h"
Patrice Chotard5c349e12018-09-04 11:37:25 +020017#include <power/regulator.h>
Alexey Brodkin90fbb282015-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 Chotardba961762022-05-06 08:22:34 +020026 struct clk_bulk clocks;
27 struct reset_ctl_bulk resets;
Patrice Chotard0d0ba1a2017-07-18 11:57:11 +020028 struct phy phy;
Patrice Chotard5c349e12018-09-04 11:37:25 +020029 struct udevice *vbus_supply;
Alexey Brodkin90fbb282015-12-02 12:32:02 +030030};
31
Patrice Chotard5c349e12018-09-04 11:37:25 +020032static int ehci_enable_vbus_supply(struct udevice *dev)
33{
34 struct generic_ehci *priv = dev_get_priv(dev);
35 int ret;
36
37 ret = device_get_supply_regulator(dev, "vbus-supply",
38 &priv->vbus_supply);
39 if (ret && ret != -ENOENT)
40 return ret;
41
Jonas Karlman08303332023-07-19 21:20:57 +000042 ret = regulator_set_enable_if_allowed(priv->vbus_supply, true);
43 if (ret && ret != -ENOSYS) {
44 dev_err(dev, "Error enabling VBUS supply (ret=%d)\n", ret);
45 return ret;
Patrice Chotard5c349e12018-09-04 11:37:25 +020046 }
47
48 return 0;
49}
50
51static int ehci_disable_vbus_supply(struct generic_ehci *priv)
52{
Jonas Karlman08303332023-07-19 21:20:57 +000053 int ret;
54
55 ret = regulator_set_enable_if_allowed(priv->vbus_supply, false);
56 if (ret && ret != -ENOSYS)
57 return ret;
58
59 return 0;
Patrice Chotard5c349e12018-09-04 11:37:25 +020060}
Patrice Chotard5c349e12018-09-04 11:37:25 +020061
Alexey Brodkin90fbb282015-12-02 12:32:02 +030062static int ehci_usb_probe(struct udevice *dev)
63{
Patrice Chotarda1cee8e2017-07-18 11:57:10 +020064 struct generic_ehci *priv = dev_get_priv(dev);
Marek Vasut643cacb2016-01-23 21:04:46 +010065 struct ehci_hccr *hccr;
Alexey Brodkin90fbb282015-12-02 12:32:02 +030066 struct ehci_hcor *hcor;
Patrice Chotardba961762022-05-06 08:22:34 +020067 int err, ret;
Masahiro Yamada4feefdc2016-01-25 15:00:36 +090068
Patrice Chotarda1cee8e2017-07-18 11:57:10 +020069 err = 0;
Patrice Chotardba961762022-05-06 08:22:34 +020070 ret = clk_get_bulk(dev, &priv->clocks);
Andre Przywara81755b82022-06-08 00:42:22 +010071 if (ret && ret != -ENOENT) {
Patrice Chotardba961762022-05-06 08:22:34 +020072 dev_err(dev, "Failed to get clocks (ret=%d)\n", ret);
73 return ret;
Masahiro Yamada4feefdc2016-01-25 15:00:36 +090074 }
Alexey Brodkin90fbb282015-12-02 12:32:02 +030075
Patrice Chotardba961762022-05-06 08:22:34 +020076 err = clk_enable_bulk(&priv->clocks);
77 if (err) {
78 dev_err(dev, "Failed to enable clocks (err=%d)\n", err);
79 goto clk_err;
80 }
Masahiro Yamada8824cfc2016-09-21 11:29:02 +090081
Patrice Chotardba961762022-05-06 08:22:34 +020082 err = reset_get_bulk(dev, &priv->resets);
Andre Przywara9125b4b2022-07-02 01:45:10 +010083 if (err && err != -ENOENT) {
Patrice Chotardba961762022-05-06 08:22:34 +020084 dev_err(dev, "Failed to get resets (err=%d)\n", err);
85 goto clk_err;
86 }
Patrice Chotarda1cee8e2017-07-18 11:57:10 +020087
Patrice Chotardba961762022-05-06 08:22:34 +020088 err = reset_deassert_bulk(&priv->resets);
89 if (err) {
90 dev_err(dev, "Failed to get deassert resets (err=%d)\n", err);
91 goto reset_err;
Masahiro Yamada8824cfc2016-09-21 11:29:02 +090092 }
93
Patrice Chotard5c349e12018-09-04 11:37:25 +020094 err = ehci_enable_vbus_supply(dev);
Patrice Chotard20f06a42018-03-14 17:48:54 +010095 if (err)
Patrice Chotard20f06a42018-03-14 17:48:54 +010096 goto reset_err;
Patrice Chotard0d0ba1a2017-07-18 11:57:11 +020097
Patrice Chotard083f8aa2022-09-06 08:15:28 +020098 err = generic_setup_phy(dev, &priv->phy, 0);
Patrice Chotard5c349e12018-09-04 11:37:25 +020099 if (err)
100 goto regulator_err;
101
Philipp Tomsich6e652e32017-09-12 17:32:28 +0200102 hccr = map_physmem(dev_read_addr(dev), 0x100, MAP_NOCACHE);
Alexey Brodkin90fbb282015-12-02 12:32:02 +0300103 hcor = (struct ehci_hcor *)((uintptr_t)hccr +
104 HC_LENGTH(ehci_readl(&hccr->cr_capbase)));
105
Patrice Chotarda1cee8e2017-07-18 11:57:10 +0200106 err = ehci_register(dev, hccr, hcor, NULL, 0, USB_INIT_HOST);
107 if (err)
Patrice Chotard0d0ba1a2017-07-18 11:57:11 +0200108 goto phy_err;
Patrice Chotarda1cee8e2017-07-18 11:57:10 +0200109
110 return 0;
111
Patrice Chotard0d0ba1a2017-07-18 11:57:11 +0200112phy_err:
Patrice Chotard083f8aa2022-09-06 08:15:28 +0200113 ret = generic_shutdown_phy(&priv->phy);
Patrice Chotard20f06a42018-03-14 17:48:54 +0100114 if (ret)
Patrice Chotardba961762022-05-06 08:22:34 +0200115 dev_err(dev, "failed to shutdown usb phy (ret=%d)\n", ret);
Patrice Chotard0d0ba1a2017-07-18 11:57:11 +0200116
Patrice Chotard5c349e12018-09-04 11:37:25 +0200117regulator_err:
118 ret = ehci_disable_vbus_supply(priv);
119 if (ret)
Patrice Chotardba961762022-05-06 08:22:34 +0200120 dev_err(dev, "failed to disable VBUS supply (ret=%d)\n", ret);
Patrice Chotard5c349e12018-09-04 11:37:25 +0200121
Patrice Chotarda1cee8e2017-07-18 11:57:10 +0200122reset_err:
Patrice Chotardba961762022-05-06 08:22:34 +0200123 ret = reset_release_bulk(&priv->resets);
Patrice Chotarda1cee8e2017-07-18 11:57:10 +0200124 if (ret)
Patrice Chotardba961762022-05-06 08:22:34 +0200125 dev_err(dev, "failed to release resets (ret=%d)\n", ret);
Patrice Chotarda1cee8e2017-07-18 11:57:10 +0200126clk_err:
Patrice Chotardba961762022-05-06 08:22:34 +0200127 ret = clk_release_bulk(&priv->clocks);
Patrice Chotarda1cee8e2017-07-18 11:57:10 +0200128 if (ret)
Patrice Chotardba961762022-05-06 08:22:34 +0200129 dev_err(dev, "failed to release clocks (ret=%d)\n", ret);
Patrice Chotarda1cee8e2017-07-18 11:57:10 +0200130
131 return err;
132}
133
134static int ehci_usb_remove(struct udevice *dev)
135{
136 struct generic_ehci *priv = dev_get_priv(dev);
137 int ret;
138
139 ret = ehci_deregister(dev);
140 if (ret)
141 return ret;
142
Patrice Chotard083f8aa2022-09-06 08:15:28 +0200143 ret = generic_shutdown_phy(&priv->phy);
Patrice Chotard20f06a42018-03-14 17:48:54 +0100144 if (ret)
145 return ret;
Patrice Chotard0d0ba1a2017-07-18 11:57:11 +0200146
Patrice Chotard5c349e12018-09-04 11:37:25 +0200147 ret = ehci_disable_vbus_supply(priv);
148 if (ret)
149 return ret;
150
Patrice Chotardba961762022-05-06 08:22:34 +0200151 ret = reset_release_bulk(&priv->resets);
Patrice Chotarda1cee8e2017-07-18 11:57:10 +0200152 if (ret)
153 return ret;
154
Patrice Chotardba961762022-05-06 08:22:34 +0200155 return clk_release_bulk(&priv->clocks);
Alexey Brodkin90fbb282015-12-02 12:32:02 +0300156}
157
Alexey Brodkin90fbb282015-12-02 12:32:02 +0300158static const struct udevice_id ehci_usb_ids[] = {
159 { .compatible = "generic-ehci" },
160 { }
161};
162
163U_BOOT_DRIVER(ehci_generic) = {
164 .name = "ehci_generic",
165 .id = UCLASS_USB,
166 .of_match = ehci_usb_ids,
167 .probe = ehci_usb_probe,
Patrice Chotarda1cee8e2017-07-18 11:57:10 +0200168 .remove = ehci_usb_remove,
Alexey Brodkin90fbb282015-12-02 12:32:02 +0300169 .ops = &ehci_usb_ops,
Simon Glass41575d82020-12-03 16:55:17 -0700170 .priv_auto = sizeof(struct generic_ehci),
Alexey Brodkin90fbb282015-12-02 12:32:02 +0300171 .flags = DM_FLAG_ALLOC_PRIV_DMA,
172};