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