Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Alexey Brodkin | a6aff43 | 2015-12-02 12:32:02 +0300 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2015 Alexey Brodkin <abrodkin@synopsys.com> |
Alexey Brodkin | a6aff43 | 2015-12-02 12:32:02 +0300 | [diff] [blame] | 4 | */ |
| 5 | |
Masahiro Yamada | fbb66bf | 2016-01-25 15:00:36 +0900 | [diff] [blame] | 6 | #include <clk.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 7 | #include <log.h> |
Simon Glass | 9bc1564 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 8 | #include <dm/device_compat.h> |
Simon Glass | d66c5f7 | 2020-02-03 07:36:15 -0700 | [diff] [blame] | 9 | #include <dm/devres.h> |
Patrice Chotard | a0809dc | 2017-07-18 11:57:10 +0200 | [diff] [blame] | 10 | #include <dm/ofnode.h> |
Patrice Chotard | 1a62cde | 2017-07-18 11:57:11 +0200 | [diff] [blame] | 11 | #include <generic-phy.h> |
Masahiro Yamada | 8a5a180 | 2016-09-21 11:29:02 +0900 | [diff] [blame] | 12 | #include <reset.h> |
Marek Vasut | 851fecc | 2016-01-23 21:04:46 +0100 | [diff] [blame] | 13 | #include <asm/io.h> |
Alexey Brodkin | a6aff43 | 2015-12-02 12:32:02 +0300 | [diff] [blame] | 14 | #include <dm.h> |
| 15 | #include "ehci.h" |
Patrice Chotard | fc42468 | 2018-09-04 11:37:25 +0200 | [diff] [blame] | 16 | #include <power/regulator.h> |
Alexey Brodkin | a6aff43 | 2015-12-02 12:32:02 +0300 | [diff] [blame] | 17 | |
| 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 | */ |
| 23 | struct generic_ehci { |
| 24 | struct ehci_ctrl ctrl; |
Patrice Chotard | 0ef4571 | 2022-05-06 08:22:34 +0200 | [diff] [blame] | 25 | struct clk_bulk clocks; |
| 26 | struct reset_ctl_bulk resets; |
Patrice Chotard | 1a62cde | 2017-07-18 11:57:11 +0200 | [diff] [blame] | 27 | struct phy phy; |
Patrice Chotard | fc42468 | 2018-09-04 11:37:25 +0200 | [diff] [blame] | 28 | struct udevice *vbus_supply; |
Alexey Brodkin | a6aff43 | 2015-12-02 12:32:02 +0300 | [diff] [blame] | 29 | }; |
| 30 | |
Patrice Chotard | fc42468 | 2018-09-04 11:37:25 +0200 | [diff] [blame] | 31 | static int ehci_enable_vbus_supply(struct udevice *dev) |
| 32 | { |
| 33 | struct generic_ehci *priv = dev_get_priv(dev); |
| 34 | int ret; |
| 35 | |
| 36 | ret = device_get_supply_regulator(dev, "vbus-supply", |
| 37 | &priv->vbus_supply); |
| 38 | if (ret && ret != -ENOENT) |
| 39 | return ret; |
| 40 | |
Jonas Karlman | 2b9e248 | 2023-07-19 21:20:57 +0000 | [diff] [blame] | 41 | ret = regulator_set_enable_if_allowed(priv->vbus_supply, true); |
| 42 | if (ret && ret != -ENOSYS) { |
| 43 | dev_err(dev, "Error enabling VBUS supply (ret=%d)\n", ret); |
| 44 | return ret; |
Patrice Chotard | fc42468 | 2018-09-04 11:37:25 +0200 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | return 0; |
| 48 | } |
| 49 | |
| 50 | static int ehci_disable_vbus_supply(struct generic_ehci *priv) |
| 51 | { |
Jonas Karlman | 2b9e248 | 2023-07-19 21:20:57 +0000 | [diff] [blame] | 52 | int ret; |
| 53 | |
| 54 | ret = regulator_set_enable_if_allowed(priv->vbus_supply, false); |
| 55 | if (ret && ret != -ENOSYS) |
| 56 | return ret; |
| 57 | |
| 58 | return 0; |
Patrice Chotard | fc42468 | 2018-09-04 11:37:25 +0200 | [diff] [blame] | 59 | } |
Patrice Chotard | fc42468 | 2018-09-04 11:37:25 +0200 | [diff] [blame] | 60 | |
Alexey Brodkin | a6aff43 | 2015-12-02 12:32:02 +0300 | [diff] [blame] | 61 | static int ehci_usb_probe(struct udevice *dev) |
| 62 | { |
Patrice Chotard | a0809dc | 2017-07-18 11:57:10 +0200 | [diff] [blame] | 63 | struct generic_ehci *priv = dev_get_priv(dev); |
Marek Vasut | 851fecc | 2016-01-23 21:04:46 +0100 | [diff] [blame] | 64 | struct ehci_hccr *hccr; |
Alexey Brodkin | a6aff43 | 2015-12-02 12:32:02 +0300 | [diff] [blame] | 65 | struct ehci_hcor *hcor; |
Patrice Chotard | 0ef4571 | 2022-05-06 08:22:34 +0200 | [diff] [blame] | 66 | int err, ret; |
Masahiro Yamada | fbb66bf | 2016-01-25 15:00:36 +0900 | [diff] [blame] | 67 | |
Patrice Chotard | a0809dc | 2017-07-18 11:57:10 +0200 | [diff] [blame] | 68 | err = 0; |
Patrice Chotard | 0ef4571 | 2022-05-06 08:22:34 +0200 | [diff] [blame] | 69 | ret = clk_get_bulk(dev, &priv->clocks); |
Andre Przywara | fbcb8f0 | 2022-06-08 00:42:22 +0100 | [diff] [blame] | 70 | if (ret && ret != -ENOENT) { |
Patrice Chotard | 0ef4571 | 2022-05-06 08:22:34 +0200 | [diff] [blame] | 71 | dev_err(dev, "Failed to get clocks (ret=%d)\n", ret); |
| 72 | return ret; |
Masahiro Yamada | fbb66bf | 2016-01-25 15:00:36 +0900 | [diff] [blame] | 73 | } |
Alexey Brodkin | a6aff43 | 2015-12-02 12:32:02 +0300 | [diff] [blame] | 74 | |
Patrice Chotard | 0ef4571 | 2022-05-06 08:22:34 +0200 | [diff] [blame] | 75 | err = clk_enable_bulk(&priv->clocks); |
| 76 | if (err) { |
| 77 | dev_err(dev, "Failed to enable clocks (err=%d)\n", err); |
| 78 | goto clk_err; |
| 79 | } |
Masahiro Yamada | 8a5a180 | 2016-09-21 11:29:02 +0900 | [diff] [blame] | 80 | |
Patrice Chotard | 0ef4571 | 2022-05-06 08:22:34 +0200 | [diff] [blame] | 81 | err = reset_get_bulk(dev, &priv->resets); |
Andre Przywara | 47bb0dc | 2022-07-02 01:45:10 +0100 | [diff] [blame] | 82 | if (err && err != -ENOENT) { |
Patrice Chotard | 0ef4571 | 2022-05-06 08:22:34 +0200 | [diff] [blame] | 83 | dev_err(dev, "Failed to get resets (err=%d)\n", err); |
| 84 | goto clk_err; |
| 85 | } |
Patrice Chotard | a0809dc | 2017-07-18 11:57:10 +0200 | [diff] [blame] | 86 | |
Patrice Chotard | 0ef4571 | 2022-05-06 08:22:34 +0200 | [diff] [blame] | 87 | err = reset_deassert_bulk(&priv->resets); |
| 88 | if (err) { |
| 89 | dev_err(dev, "Failed to get deassert resets (err=%d)\n", err); |
| 90 | goto reset_err; |
Masahiro Yamada | 8a5a180 | 2016-09-21 11:29:02 +0900 | [diff] [blame] | 91 | } |
| 92 | |
Patrice Chotard | fc42468 | 2018-09-04 11:37:25 +0200 | [diff] [blame] | 93 | err = ehci_enable_vbus_supply(dev); |
Patrice Chotard | 36afc03 | 2018-03-14 17:48:54 +0100 | [diff] [blame] | 94 | if (err) |
Patrice Chotard | 36afc03 | 2018-03-14 17:48:54 +0100 | [diff] [blame] | 95 | goto reset_err; |
Patrice Chotard | 1a62cde | 2017-07-18 11:57:11 +0200 | [diff] [blame] | 96 | |
Patrice Chotard | 343ff75 | 2022-09-06 08:15:28 +0200 | [diff] [blame] | 97 | err = generic_setup_phy(dev, &priv->phy, 0); |
Patrice Chotard | fc42468 | 2018-09-04 11:37:25 +0200 | [diff] [blame] | 98 | if (err) |
| 99 | goto regulator_err; |
| 100 | |
Philipp Tomsich | 9f3f423 | 2017-09-12 17:32:28 +0200 | [diff] [blame] | 101 | hccr = map_physmem(dev_read_addr(dev), 0x100, MAP_NOCACHE); |
Alexey Brodkin | a6aff43 | 2015-12-02 12:32:02 +0300 | [diff] [blame] | 102 | hcor = (struct ehci_hcor *)((uintptr_t)hccr + |
| 103 | HC_LENGTH(ehci_readl(&hccr->cr_capbase))); |
| 104 | |
Patrice Chotard | a0809dc | 2017-07-18 11:57:10 +0200 | [diff] [blame] | 105 | err = ehci_register(dev, hccr, hcor, NULL, 0, USB_INIT_HOST); |
| 106 | if (err) |
Patrice Chotard | 1a62cde | 2017-07-18 11:57:11 +0200 | [diff] [blame] | 107 | goto phy_err; |
Patrice Chotard | a0809dc | 2017-07-18 11:57:10 +0200 | [diff] [blame] | 108 | |
| 109 | return 0; |
| 110 | |
Patrice Chotard | 1a62cde | 2017-07-18 11:57:11 +0200 | [diff] [blame] | 111 | phy_err: |
Patrice Chotard | 343ff75 | 2022-09-06 08:15:28 +0200 | [diff] [blame] | 112 | ret = generic_shutdown_phy(&priv->phy); |
Patrice Chotard | 36afc03 | 2018-03-14 17:48:54 +0100 | [diff] [blame] | 113 | if (ret) |
Patrice Chotard | 0ef4571 | 2022-05-06 08:22:34 +0200 | [diff] [blame] | 114 | dev_err(dev, "failed to shutdown usb phy (ret=%d)\n", ret); |
Patrice Chotard | 1a62cde | 2017-07-18 11:57:11 +0200 | [diff] [blame] | 115 | |
Patrice Chotard | fc42468 | 2018-09-04 11:37:25 +0200 | [diff] [blame] | 116 | regulator_err: |
| 117 | ret = ehci_disable_vbus_supply(priv); |
| 118 | if (ret) |
Patrice Chotard | 0ef4571 | 2022-05-06 08:22:34 +0200 | [diff] [blame] | 119 | dev_err(dev, "failed to disable VBUS supply (ret=%d)\n", ret); |
Patrice Chotard | fc42468 | 2018-09-04 11:37:25 +0200 | [diff] [blame] | 120 | |
Patrice Chotard | a0809dc | 2017-07-18 11:57:10 +0200 | [diff] [blame] | 121 | reset_err: |
Patrice Chotard | 0ef4571 | 2022-05-06 08:22:34 +0200 | [diff] [blame] | 122 | ret = reset_release_bulk(&priv->resets); |
Patrice Chotard | a0809dc | 2017-07-18 11:57:10 +0200 | [diff] [blame] | 123 | if (ret) |
Patrice Chotard | 0ef4571 | 2022-05-06 08:22:34 +0200 | [diff] [blame] | 124 | dev_err(dev, "failed to release resets (ret=%d)\n", ret); |
Patrice Chotard | a0809dc | 2017-07-18 11:57:10 +0200 | [diff] [blame] | 125 | clk_err: |
Patrice Chotard | 0ef4571 | 2022-05-06 08:22:34 +0200 | [diff] [blame] | 126 | ret = clk_release_bulk(&priv->clocks); |
Patrice Chotard | a0809dc | 2017-07-18 11:57:10 +0200 | [diff] [blame] | 127 | if (ret) |
Patrice Chotard | 0ef4571 | 2022-05-06 08:22:34 +0200 | [diff] [blame] | 128 | dev_err(dev, "failed to release clocks (ret=%d)\n", ret); |
Patrice Chotard | a0809dc | 2017-07-18 11:57:10 +0200 | [diff] [blame] | 129 | |
| 130 | return err; |
| 131 | } |
| 132 | |
| 133 | static int ehci_usb_remove(struct udevice *dev) |
| 134 | { |
| 135 | struct generic_ehci *priv = dev_get_priv(dev); |
| 136 | int ret; |
| 137 | |
| 138 | ret = ehci_deregister(dev); |
| 139 | if (ret) |
| 140 | return ret; |
| 141 | |
Patrice Chotard | 343ff75 | 2022-09-06 08:15:28 +0200 | [diff] [blame] | 142 | ret = generic_shutdown_phy(&priv->phy); |
Patrice Chotard | 36afc03 | 2018-03-14 17:48:54 +0100 | [diff] [blame] | 143 | if (ret) |
| 144 | return ret; |
Patrice Chotard | 1a62cde | 2017-07-18 11:57:11 +0200 | [diff] [blame] | 145 | |
Patrice Chotard | fc42468 | 2018-09-04 11:37:25 +0200 | [diff] [blame] | 146 | ret = ehci_disable_vbus_supply(priv); |
| 147 | if (ret) |
| 148 | return ret; |
| 149 | |
Patrice Chotard | 0ef4571 | 2022-05-06 08:22:34 +0200 | [diff] [blame] | 150 | ret = reset_release_bulk(&priv->resets); |
Patrice Chotard | a0809dc | 2017-07-18 11:57:10 +0200 | [diff] [blame] | 151 | if (ret) |
| 152 | return ret; |
| 153 | |
Patrice Chotard | 0ef4571 | 2022-05-06 08:22:34 +0200 | [diff] [blame] | 154 | return clk_release_bulk(&priv->clocks); |
Alexey Brodkin | a6aff43 | 2015-12-02 12:32:02 +0300 | [diff] [blame] | 155 | } |
| 156 | |
Alexey Brodkin | a6aff43 | 2015-12-02 12:32:02 +0300 | [diff] [blame] | 157 | static const struct udevice_id ehci_usb_ids[] = { |
| 158 | { .compatible = "generic-ehci" }, |
| 159 | { } |
| 160 | }; |
| 161 | |
| 162 | U_BOOT_DRIVER(ehci_generic) = { |
| 163 | .name = "ehci_generic", |
| 164 | .id = UCLASS_USB, |
| 165 | .of_match = ehci_usb_ids, |
| 166 | .probe = ehci_usb_probe, |
Patrice Chotard | a0809dc | 2017-07-18 11:57:10 +0200 | [diff] [blame] | 167 | .remove = ehci_usb_remove, |
Alexey Brodkin | a6aff43 | 2015-12-02 12:32:02 +0300 | [diff] [blame] | 168 | .ops = &ehci_usb_ops, |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 169 | .priv_auto = sizeof(struct generic_ehci), |
Alexey Brodkin | a6aff43 | 2015-12-02 12:32:02 +0300 | [diff] [blame] | 170 | .flags = DM_FLAG_ALLOC_PRIV_DMA, |
| 171 | }; |