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