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> |
Patrice Chotard | a0809dc | 2017-07-18 11:57:10 +0200 | [diff] [blame] | 8 | #include <dm/ofnode.h> |
Patrice Chotard | 1a62cde | 2017-07-18 11:57:11 +0200 | [diff] [blame] | 9 | #include <generic-phy.h> |
Masahiro Yamada | 8a5a180 | 2016-09-21 11:29:02 +0900 | [diff] [blame] | 10 | #include <reset.h> |
Marek Vasut | 851fecc | 2016-01-23 21:04:46 +0100 | [diff] [blame] | 11 | #include <asm/io.h> |
Alexey Brodkin | a6aff43 | 2015-12-02 12:32:02 +0300 | [diff] [blame] | 12 | #include <dm.h> |
| 13 | #include "ehci.h" |
| 14 | |
| 15 | /* |
| 16 | * Even though here we don't explicitly use "struct ehci_ctrl" |
| 17 | * ehci_register() expects it to be the first thing that resides in |
| 18 | * device's private data. |
| 19 | */ |
| 20 | struct generic_ehci { |
| 21 | struct ehci_ctrl ctrl; |
Patrice Chotard | a0809dc | 2017-07-18 11:57:10 +0200 | [diff] [blame] | 22 | struct clk *clocks; |
| 23 | struct reset_ctl *resets; |
Patrice Chotard | 1a62cde | 2017-07-18 11:57:11 +0200 | [diff] [blame] | 24 | struct phy phy; |
Patrice Chotard | a0809dc | 2017-07-18 11:57:10 +0200 | [diff] [blame] | 25 | int clock_count; |
| 26 | int reset_count; |
Alexey Brodkin | a6aff43 | 2015-12-02 12:32:02 +0300 | [diff] [blame] | 27 | }; |
| 28 | |
Patrice Chotard | 36afc03 | 2018-03-14 17:48:54 +0100 | [diff] [blame] | 29 | static int ehci_setup_phy(struct udevice *dev, int index) |
| 30 | { |
| 31 | struct generic_ehci *priv = dev_get_priv(dev); |
| 32 | int ret; |
| 33 | |
| 34 | ret = generic_phy_get_by_index(dev, index, &priv->phy); |
| 35 | if (ret) { |
| 36 | if (ret != -ENOENT) { |
| 37 | dev_err(dev, "failed to get usb phy\n"); |
| 38 | return ret; |
| 39 | } |
| 40 | } else { |
| 41 | ret = generic_phy_init(&priv->phy); |
| 42 | if (ret) { |
| 43 | dev_err(dev, "failed to init usb phy\n"); |
| 44 | return ret; |
| 45 | } |
| 46 | |
| 47 | ret = generic_phy_power_on(&priv->phy); |
| 48 | if (ret) { |
| 49 | dev_err(dev, "failed to power on usb phy\n"); |
| 50 | return generic_phy_exit(&priv->phy); |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | return 0; |
| 55 | } |
| 56 | |
| 57 | static int ehci_shutdown_phy(struct udevice *dev) |
| 58 | { |
| 59 | struct generic_ehci *priv = dev_get_priv(dev); |
| 60 | int ret = 0; |
| 61 | |
| 62 | if (generic_phy_valid(&priv->phy)) { |
| 63 | ret = generic_phy_power_off(&priv->phy); |
| 64 | if (ret) { |
| 65 | dev_err(dev, "failed to power off usb phy\n"); |
| 66 | return ret; |
| 67 | } |
| 68 | |
| 69 | ret = generic_phy_exit(&priv->phy); |
| 70 | if (ret) { |
| 71 | dev_err(dev, "failed to power off usb phy\n"); |
| 72 | return ret; |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | return 0; |
| 77 | } |
| 78 | |
Alexey Brodkin | a6aff43 | 2015-12-02 12:32:02 +0300 | [diff] [blame] | 79 | static int ehci_usb_probe(struct udevice *dev) |
| 80 | { |
Patrice Chotard | a0809dc | 2017-07-18 11:57:10 +0200 | [diff] [blame] | 81 | struct generic_ehci *priv = dev_get_priv(dev); |
Marek Vasut | 851fecc | 2016-01-23 21:04:46 +0100 | [diff] [blame] | 82 | struct ehci_hccr *hccr; |
Alexey Brodkin | a6aff43 | 2015-12-02 12:32:02 +0300 | [diff] [blame] | 83 | struct ehci_hcor *hcor; |
Patrice Chotard | a0809dc | 2017-07-18 11:57:10 +0200 | [diff] [blame] | 84 | int i, err, ret, clock_nb, reset_nb; |
Masahiro Yamada | fbb66bf | 2016-01-25 15:00:36 +0900 | [diff] [blame] | 85 | |
Patrice Chotard | a0809dc | 2017-07-18 11:57:10 +0200 | [diff] [blame] | 86 | err = 0; |
| 87 | priv->clock_count = 0; |
| 88 | clock_nb = ofnode_count_phandle_with_args(dev_ofnode(dev), "clocks", |
| 89 | "#clock-cells"); |
| 90 | if (clock_nb > 0) { |
| 91 | priv->clocks = devm_kcalloc(dev, clock_nb, sizeof(struct clk), |
| 92 | GFP_KERNEL); |
| 93 | if (!priv->clocks) |
| 94 | return -ENOMEM; |
Masahiro Yamada | fbb66bf | 2016-01-25 15:00:36 +0900 | [diff] [blame] | 95 | |
Patrice Chotard | a0809dc | 2017-07-18 11:57:10 +0200 | [diff] [blame] | 96 | for (i = 0; i < clock_nb; i++) { |
| 97 | err = clk_get_by_index(dev, i, &priv->clocks[i]); |
| 98 | |
| 99 | if (err < 0) |
| 100 | break; |
| 101 | err = clk_enable(&priv->clocks[i]); |
| 102 | if (err) { |
Patrice Chotard | f2cbba0 | 2018-03-14 17:48:55 +0100 | [diff] [blame] | 103 | dev_err(dev, "failed to enable clock %d\n", i); |
Patrice Chotard | a0809dc | 2017-07-18 11:57:10 +0200 | [diff] [blame] | 104 | clk_free(&priv->clocks[i]); |
| 105 | goto clk_err; |
| 106 | } |
| 107 | priv->clock_count++; |
| 108 | } |
| 109 | } else { |
| 110 | if (clock_nb != -ENOENT) { |
Patrice Chotard | f2cbba0 | 2018-03-14 17:48:55 +0100 | [diff] [blame] | 111 | dev_err(dev, "failed to get clock phandle(%d)\n", |
| 112 | clock_nb); |
Patrice Chotard | a0809dc | 2017-07-18 11:57:10 +0200 | [diff] [blame] | 113 | return clock_nb; |
| 114 | } |
Masahiro Yamada | fbb66bf | 2016-01-25 15:00:36 +0900 | [diff] [blame] | 115 | } |
Alexey Brodkin | a6aff43 | 2015-12-02 12:32:02 +0300 | [diff] [blame] | 116 | |
Patrice Chotard | a0809dc | 2017-07-18 11:57:10 +0200 | [diff] [blame] | 117 | priv->reset_count = 0; |
| 118 | reset_nb = ofnode_count_phandle_with_args(dev_ofnode(dev), "resets", |
| 119 | "#reset-cells"); |
| 120 | if (reset_nb > 0) { |
| 121 | priv->resets = devm_kcalloc(dev, reset_nb, |
| 122 | sizeof(struct reset_ctl), |
| 123 | GFP_KERNEL); |
| 124 | if (!priv->resets) |
| 125 | return -ENOMEM; |
Masahiro Yamada | 8a5a180 | 2016-09-21 11:29:02 +0900 | [diff] [blame] | 126 | |
Patrice Chotard | a0809dc | 2017-07-18 11:57:10 +0200 | [diff] [blame] | 127 | for (i = 0; i < reset_nb; i++) { |
| 128 | err = reset_get_by_index(dev, i, &priv->resets[i]); |
| 129 | if (err < 0) |
| 130 | break; |
| 131 | |
| 132 | if (reset_deassert(&priv->resets[i])) { |
Patrice Chotard | f2cbba0 | 2018-03-14 17:48:55 +0100 | [diff] [blame] | 133 | dev_err(dev, "failed to deassert reset %d\n", |
| 134 | i); |
Patrice Chotard | a0809dc | 2017-07-18 11:57:10 +0200 | [diff] [blame] | 135 | reset_free(&priv->resets[i]); |
| 136 | goto reset_err; |
| 137 | } |
| 138 | priv->reset_count++; |
| 139 | } |
| 140 | } else { |
| 141 | if (reset_nb != -ENOENT) { |
Patrice Chotard | f2cbba0 | 2018-03-14 17:48:55 +0100 | [diff] [blame] | 142 | dev_err(dev, "failed to get reset phandle(%d)\n", |
| 143 | reset_nb); |
Patrice Chotard | a0809dc | 2017-07-18 11:57:10 +0200 | [diff] [blame] | 144 | goto clk_err; |
| 145 | } |
Masahiro Yamada | 8a5a180 | 2016-09-21 11:29:02 +0900 | [diff] [blame] | 146 | } |
| 147 | |
Patrice Chotard | 36afc03 | 2018-03-14 17:48:54 +0100 | [diff] [blame] | 148 | err = ehci_setup_phy(dev, 0); |
| 149 | if (err) |
Patrice Chotard | 36afc03 | 2018-03-14 17:48:54 +0100 | [diff] [blame] | 150 | goto reset_err; |
Patrice Chotard | 1a62cde | 2017-07-18 11:57:11 +0200 | [diff] [blame] | 151 | |
Philipp Tomsich | 9f3f423 | 2017-09-12 17:32:28 +0200 | [diff] [blame] | 152 | hccr = map_physmem(dev_read_addr(dev), 0x100, MAP_NOCACHE); |
Alexey Brodkin | a6aff43 | 2015-12-02 12:32:02 +0300 | [diff] [blame] | 153 | hcor = (struct ehci_hcor *)((uintptr_t)hccr + |
| 154 | HC_LENGTH(ehci_readl(&hccr->cr_capbase))); |
| 155 | |
Patrice Chotard | a0809dc | 2017-07-18 11:57:10 +0200 | [diff] [blame] | 156 | err = ehci_register(dev, hccr, hcor, NULL, 0, USB_INIT_HOST); |
| 157 | if (err) |
Patrice Chotard | 1a62cde | 2017-07-18 11:57:11 +0200 | [diff] [blame] | 158 | goto phy_err; |
Patrice Chotard | a0809dc | 2017-07-18 11:57:10 +0200 | [diff] [blame] | 159 | |
| 160 | return 0; |
| 161 | |
Patrice Chotard | 1a62cde | 2017-07-18 11:57:11 +0200 | [diff] [blame] | 162 | phy_err: |
Patrice Chotard | 36afc03 | 2018-03-14 17:48:54 +0100 | [diff] [blame] | 163 | ret = ehci_shutdown_phy(dev); |
| 164 | if (ret) |
| 165 | dev_err(dev, "failed to shutdown usb phy\n"); |
Patrice Chotard | 1a62cde | 2017-07-18 11:57:11 +0200 | [diff] [blame] | 166 | |
Patrice Chotard | a0809dc | 2017-07-18 11:57:10 +0200 | [diff] [blame] | 167 | reset_err: |
| 168 | ret = reset_release_all(priv->resets, priv->reset_count); |
| 169 | if (ret) |
Patrice Chotard | f2cbba0 | 2018-03-14 17:48:55 +0100 | [diff] [blame] | 170 | dev_err(dev, "failed to assert all resets\n"); |
Patrice Chotard | a0809dc | 2017-07-18 11:57:10 +0200 | [diff] [blame] | 171 | clk_err: |
| 172 | ret = clk_release_all(priv->clocks, priv->clock_count); |
| 173 | if (ret) |
Patrice Chotard | f2cbba0 | 2018-03-14 17:48:55 +0100 | [diff] [blame] | 174 | dev_err(dev, "failed to disable all clocks\n"); |
Patrice Chotard | a0809dc | 2017-07-18 11:57:10 +0200 | [diff] [blame] | 175 | |
| 176 | return err; |
| 177 | } |
| 178 | |
| 179 | static int ehci_usb_remove(struct udevice *dev) |
| 180 | { |
| 181 | struct generic_ehci *priv = dev_get_priv(dev); |
| 182 | int ret; |
| 183 | |
| 184 | ret = ehci_deregister(dev); |
| 185 | if (ret) |
| 186 | return ret; |
| 187 | |
Patrice Chotard | 36afc03 | 2018-03-14 17:48:54 +0100 | [diff] [blame] | 188 | ret = ehci_shutdown_phy(dev); |
| 189 | if (ret) |
| 190 | return ret; |
Patrice Chotard | 1a62cde | 2017-07-18 11:57:11 +0200 | [diff] [blame] | 191 | |
Patrice Chotard | a0809dc | 2017-07-18 11:57:10 +0200 | [diff] [blame] | 192 | ret = reset_release_all(priv->resets, priv->reset_count); |
| 193 | if (ret) |
| 194 | return ret; |
| 195 | |
| 196 | return clk_release_all(priv->clocks, priv->clock_count); |
Alexey Brodkin | a6aff43 | 2015-12-02 12:32:02 +0300 | [diff] [blame] | 197 | } |
| 198 | |
Alexey Brodkin | a6aff43 | 2015-12-02 12:32:02 +0300 | [diff] [blame] | 199 | static const struct udevice_id ehci_usb_ids[] = { |
| 200 | { .compatible = "generic-ehci" }, |
| 201 | { } |
| 202 | }; |
| 203 | |
| 204 | U_BOOT_DRIVER(ehci_generic) = { |
| 205 | .name = "ehci_generic", |
| 206 | .id = UCLASS_USB, |
| 207 | .of_match = ehci_usb_ids, |
| 208 | .probe = ehci_usb_probe, |
Patrice Chotard | a0809dc | 2017-07-18 11:57:10 +0200 | [diff] [blame] | 209 | .remove = ehci_usb_remove, |
Alexey Brodkin | a6aff43 | 2015-12-02 12:32:02 +0300 | [diff] [blame] | 210 | .ops = &ehci_usb_ops, |
| 211 | .priv_auto_alloc_size = sizeof(struct generic_ehci), |
| 212 | .flags = DM_FLAG_ALLOC_PRIV_DMA, |
| 213 | }; |