Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Alexey Brodkin | 83fd312 | 2015-12-14 17:18:50 +0300 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2015 Alexey Brodkin <abrodkin@synopsys.com> |
Alexey Brodkin | 83fd312 | 2015-12-14 17:18:50 +0300 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | #include <common.h> |
Patrice Chotard | 37a197a | 2017-07-18 11:57:12 +0200 | [diff] [blame] | 7 | #include <clk.h> |
Alexey Brodkin | 83fd312 | 2015-12-14 17:18:50 +0300 | [diff] [blame] | 8 | #include <dm.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 9 | #include <log.h> |
Simon Glass | 9bc1564 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 10 | #include <dm/device_compat.h> |
Simon Glass | d66c5f7 | 2020-02-03 07:36:15 -0700 | [diff] [blame] | 11 | #include <dm/devres.h> |
Patrice Chotard | 37a197a | 2017-07-18 11:57:12 +0200 | [diff] [blame] | 12 | #include <dm/ofnode.h> |
Patrice Chotard | c6e19d3 | 2017-07-18 11:57:14 +0200 | [diff] [blame] | 13 | #include <generic-phy.h> |
Patrice Chotard | 170229e | 2017-07-18 11:57:13 +0200 | [diff] [blame] | 14 | #include <reset.h> |
Alexey Brodkin | 83fd312 | 2015-12-14 17:18:50 +0300 | [diff] [blame] | 15 | #include "ohci.h" |
| 16 | |
| 17 | #if !defined(CONFIG_USB_OHCI_NEW) |
| 18 | # error "Generic OHCI driver requires CONFIG_USB_OHCI_NEW" |
| 19 | #endif |
| 20 | |
| 21 | struct generic_ohci { |
| 22 | ohci_t ohci; |
Patrice Chotard | 37a197a | 2017-07-18 11:57:12 +0200 | [diff] [blame] | 23 | struct clk *clocks; /* clock list */ |
Patrice Chotard | 170229e | 2017-07-18 11:57:13 +0200 | [diff] [blame] | 24 | struct reset_ctl *resets; /* reset list */ |
Patrice Chotard | c6e19d3 | 2017-07-18 11:57:14 +0200 | [diff] [blame] | 25 | struct phy phy; |
Patrice Chotard | 37a197a | 2017-07-18 11:57:12 +0200 | [diff] [blame] | 26 | int clock_count; /* number of clock in clock list */ |
Patrice Chotard | 170229e | 2017-07-18 11:57:13 +0200 | [diff] [blame] | 27 | int reset_count; /* number of reset in reset list */ |
Alexey Brodkin | 83fd312 | 2015-12-14 17:18:50 +0300 | [diff] [blame] | 28 | }; |
| 29 | |
Patrice Chotard | ab0b205e | 2018-03-14 17:48:57 +0100 | [diff] [blame] | 30 | static int ohci_setup_phy(struct udevice *dev, int index) |
| 31 | { |
| 32 | struct generic_ohci *priv = dev_get_priv(dev); |
| 33 | int ret; |
| 34 | |
| 35 | ret = generic_phy_get_by_index(dev, index, &priv->phy); |
| 36 | if (ret) { |
| 37 | if (ret != -ENOENT) { |
| 38 | dev_err(dev, "failed to get usb phy\n"); |
| 39 | return ret; |
| 40 | } |
| 41 | } else { |
| 42 | ret = generic_phy_init(&priv->phy); |
| 43 | if (ret) { |
Patrick Delaunay | 01e05a9 | 2020-07-03 17:36:42 +0200 | [diff] [blame] | 44 | dev_dbg(dev, "failed to init usb phy\n"); |
Patrice Chotard | ab0b205e | 2018-03-14 17:48:57 +0100 | [diff] [blame] | 45 | return ret; |
| 46 | } |
| 47 | |
| 48 | ret = generic_phy_power_on(&priv->phy); |
| 49 | if (ret) { |
Patrick Delaunay | 01e05a9 | 2020-07-03 17:36:42 +0200 | [diff] [blame] | 50 | dev_dbg(dev, "failed to power on usb phy\n"); |
Patrice Chotard | ab0b205e | 2018-03-14 17:48:57 +0100 | [diff] [blame] | 51 | return generic_phy_exit(&priv->phy); |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | return 0; |
| 56 | } |
| 57 | |
| 58 | static int ohci_shutdown_phy(struct udevice *dev) |
| 59 | { |
| 60 | struct generic_ohci *priv = dev_get_priv(dev); |
| 61 | int ret = 0; |
| 62 | |
| 63 | if (generic_phy_valid(&priv->phy)) { |
| 64 | ret = generic_phy_power_off(&priv->phy); |
| 65 | if (ret) { |
Patrick Delaunay | 01e05a9 | 2020-07-03 17:36:42 +0200 | [diff] [blame] | 66 | dev_dbg(dev, "failed to power off usb phy\n"); |
Patrice Chotard | ab0b205e | 2018-03-14 17:48:57 +0100 | [diff] [blame] | 67 | return ret; |
| 68 | } |
| 69 | |
| 70 | ret = generic_phy_exit(&priv->phy); |
| 71 | if (ret) { |
Patrick Delaunay | 01e05a9 | 2020-07-03 17:36:42 +0200 | [diff] [blame] | 72 | dev_dbg(dev, "failed to power off usb phy\n"); |
Patrice Chotard | ab0b205e | 2018-03-14 17:48:57 +0100 | [diff] [blame] | 73 | return ret; |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | return 0; |
| 78 | } |
| 79 | |
Alexey Brodkin | 83fd312 | 2015-12-14 17:18:50 +0300 | [diff] [blame] | 80 | static int ohci_usb_probe(struct udevice *dev) |
| 81 | { |
Masahiro Yamada | 1096ae1 | 2020-07-17 14:36:46 +0900 | [diff] [blame] | 82 | struct ohci_regs *regs = dev_read_addr_ptr(dev); |
Patrice Chotard | 37a197a | 2017-07-18 11:57:12 +0200 | [diff] [blame] | 83 | struct generic_ohci *priv = dev_get_priv(dev); |
Patrice Chotard | 170229e | 2017-07-18 11:57:13 +0200 | [diff] [blame] | 84 | int i, err, ret, clock_nb, reset_nb; |
Alexey Brodkin | 83fd312 | 2015-12-14 17:18:50 +0300 | [diff] [blame] | 85 | |
Patrice Chotard | 37a197a | 2017-07-18 11:57:12 +0200 | [diff] [blame] | 86 | err = 0; |
| 87 | priv->clock_count = 0; |
Patrick Delaunay | d776a84 | 2020-09-25 09:41:14 +0200 | [diff] [blame] | 88 | clock_nb = dev_count_phandle_with_args(dev, "clocks", "#clock-cells", |
| 89 | 0); |
Patrice Chotard | 37a197a | 2017-07-18 11:57:12 +0200 | [diff] [blame] | 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; |
| 95 | |
| 96 | for (i = 0; i < clock_nb; i++) { |
| 97 | err = clk_get_by_index(dev, i, &priv->clocks[i]); |
| 98 | if (err < 0) |
| 99 | break; |
| 100 | |
| 101 | err = clk_enable(&priv->clocks[i]); |
Kever Yang | 05b015b | 2019-08-28 16:23:47 +0800 | [diff] [blame] | 102 | if (err && err != -ENOSYS) { |
Patrice Chotard | 7c11e35 | 2018-03-14 17:48:58 +0100 | [diff] [blame] | 103 | dev_err(dev, "failed to enable clock %d\n", i); |
Patrice Chotard | 37a197a | 2017-07-18 11:57:12 +0200 | [diff] [blame] | 104 | clk_free(&priv->clocks[i]); |
| 105 | goto clk_err; |
| 106 | } |
| 107 | priv->clock_count++; |
| 108 | } |
| 109 | } else if (clock_nb != -ENOENT) { |
Patrice Chotard | 7c11e35 | 2018-03-14 17:48:58 +0100 | [diff] [blame] | 110 | dev_err(dev, "failed to get clock phandle(%d)\n", clock_nb); |
Patrice Chotard | 37a197a | 2017-07-18 11:57:12 +0200 | [diff] [blame] | 111 | return clock_nb; |
| 112 | } |
| 113 | |
Patrice Chotard | 170229e | 2017-07-18 11:57:13 +0200 | [diff] [blame] | 114 | priv->reset_count = 0; |
Patrick Delaunay | d776a84 | 2020-09-25 09:41:14 +0200 | [diff] [blame] | 115 | reset_nb = dev_count_phandle_with_args(dev, "resets", "#reset-cells", |
| 116 | 0); |
Patrice Chotard | 170229e | 2017-07-18 11:57:13 +0200 | [diff] [blame] | 117 | if (reset_nb > 0) { |
| 118 | priv->resets = devm_kcalloc(dev, reset_nb, |
| 119 | sizeof(struct reset_ctl), |
| 120 | GFP_KERNEL); |
| 121 | if (!priv->resets) |
| 122 | return -ENOMEM; |
| 123 | |
| 124 | for (i = 0; i < reset_nb; i++) { |
| 125 | err = reset_get_by_index(dev, i, &priv->resets[i]); |
| 126 | if (err < 0) |
| 127 | break; |
| 128 | |
| 129 | err = reset_deassert(&priv->resets[i]); |
| 130 | if (err) { |
Patrice Chotard | 7c11e35 | 2018-03-14 17:48:58 +0100 | [diff] [blame] | 131 | dev_err(dev, "failed to deassert reset %d\n", i); |
Patrice Chotard | 170229e | 2017-07-18 11:57:13 +0200 | [diff] [blame] | 132 | reset_free(&priv->resets[i]); |
| 133 | goto reset_err; |
| 134 | } |
| 135 | priv->reset_count++; |
| 136 | } |
| 137 | } else if (reset_nb != -ENOENT) { |
Patrice Chotard | 7c11e35 | 2018-03-14 17:48:58 +0100 | [diff] [blame] | 138 | dev_err(dev, "failed to get reset phandle(%d)\n", reset_nb); |
Patrice Chotard | 170229e | 2017-07-18 11:57:13 +0200 | [diff] [blame] | 139 | goto clk_err; |
| 140 | } |
| 141 | |
Patrice Chotard | ab0b205e | 2018-03-14 17:48:57 +0100 | [diff] [blame] | 142 | err = ohci_setup_phy(dev, 0); |
| 143 | if (err) |
Patrice Chotard | ab0b205e | 2018-03-14 17:48:57 +0100 | [diff] [blame] | 144 | goto reset_err; |
Patrice Chotard | c6e19d3 | 2017-07-18 11:57:14 +0200 | [diff] [blame] | 145 | |
Patrice Chotard | 37a197a | 2017-07-18 11:57:12 +0200 | [diff] [blame] | 146 | err = ohci_register(dev, regs); |
| 147 | if (err) |
Patrice Chotard | c6e19d3 | 2017-07-18 11:57:14 +0200 | [diff] [blame] | 148 | goto phy_err; |
Patrice Chotard | 37a197a | 2017-07-18 11:57:12 +0200 | [diff] [blame] | 149 | |
| 150 | return 0; |
| 151 | |
Patrice Chotard | c6e19d3 | 2017-07-18 11:57:14 +0200 | [diff] [blame] | 152 | phy_err: |
Patrice Chotard | ab0b205e | 2018-03-14 17:48:57 +0100 | [diff] [blame] | 153 | ret = ohci_shutdown_phy(dev); |
| 154 | if (ret) |
| 155 | dev_err(dev, "failed to shutdown usb phy\n"); |
Patrice Chotard | c6e19d3 | 2017-07-18 11:57:14 +0200 | [diff] [blame] | 156 | |
Patrice Chotard | 170229e | 2017-07-18 11:57:13 +0200 | [diff] [blame] | 157 | reset_err: |
| 158 | ret = reset_release_all(priv->resets, priv->reset_count); |
| 159 | if (ret) |
Patrice Chotard | 7c11e35 | 2018-03-14 17:48:58 +0100 | [diff] [blame] | 160 | dev_err(dev, "failed to assert all resets\n"); |
Patrice Chotard | 37a197a | 2017-07-18 11:57:12 +0200 | [diff] [blame] | 161 | clk_err: |
| 162 | ret = clk_release_all(priv->clocks, priv->clock_count); |
| 163 | if (ret) |
Patrice Chotard | 7c11e35 | 2018-03-14 17:48:58 +0100 | [diff] [blame] | 164 | dev_err(dev, "failed to disable all clocks\n"); |
Patrice Chotard | 37a197a | 2017-07-18 11:57:12 +0200 | [diff] [blame] | 165 | |
| 166 | return err; |
Alexey Brodkin | 83fd312 | 2015-12-14 17:18:50 +0300 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | static int ohci_usb_remove(struct udevice *dev) |
| 170 | { |
Patrice Chotard | 37a197a | 2017-07-18 11:57:12 +0200 | [diff] [blame] | 171 | struct generic_ohci *priv = dev_get_priv(dev); |
| 172 | int ret; |
| 173 | |
| 174 | ret = ohci_deregister(dev); |
| 175 | if (ret) |
| 176 | return ret; |
| 177 | |
Patrice Chotard | ab0b205e | 2018-03-14 17:48:57 +0100 | [diff] [blame] | 178 | ret = ohci_shutdown_phy(dev); |
| 179 | if (ret) |
| 180 | return ret; |
Patrice Chotard | c6e19d3 | 2017-07-18 11:57:14 +0200 | [diff] [blame] | 181 | |
Patrice Chotard | 170229e | 2017-07-18 11:57:13 +0200 | [diff] [blame] | 182 | ret = reset_release_all(priv->resets, priv->reset_count); |
| 183 | if (ret) |
| 184 | return ret; |
| 185 | |
Patrice Chotard | 37a197a | 2017-07-18 11:57:12 +0200 | [diff] [blame] | 186 | return clk_release_all(priv->clocks, priv->clock_count); |
Alexey Brodkin | 83fd312 | 2015-12-14 17:18:50 +0300 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | static const struct udevice_id ohci_usb_ids[] = { |
| 190 | { .compatible = "generic-ohci" }, |
| 191 | { } |
| 192 | }; |
| 193 | |
| 194 | U_BOOT_DRIVER(ohci_generic) = { |
| 195 | .name = "ohci_generic", |
| 196 | .id = UCLASS_USB, |
| 197 | .of_match = ohci_usb_ids, |
| 198 | .probe = ohci_usb_probe, |
| 199 | .remove = ohci_usb_remove, |
| 200 | .ops = &ohci_usb_ops, |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 201 | .priv_auto = sizeof(struct generic_ohci), |
Alexey Brodkin | 83fd312 | 2015-12-14 17:18:50 +0300 | [diff] [blame] | 202 | .flags = DM_FLAG_ALLOC_PRIV_DMA, |
| 203 | }; |