Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Alexey Brodkin | fee331f | 2015-12-14 17:18:50 +0300 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2015 Alexey Brodkin <abrodkin@synopsys.com> |
Alexey Brodkin | fee331f | 2015-12-14 17:18:50 +0300 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | #include <common.h> |
Patrice Chotard | 155d9f6 | 2017-07-18 11:57:12 +0200 | [diff] [blame] | 7 | #include <clk.h> |
Alexey Brodkin | fee331f | 2015-12-14 17:18:50 +0300 | [diff] [blame] | 8 | #include <dm.h> |
Simon Glass | f7ae49f | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 9 | #include <log.h> |
Simon Glass | 336d461 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 10 | #include <dm/device_compat.h> |
Simon Glass | 61b29b8 | 2020-02-03 07:36:15 -0700 | [diff] [blame] | 11 | #include <dm/devres.h> |
Patrice Chotard | 155d9f6 | 2017-07-18 11:57:12 +0200 | [diff] [blame] | 12 | #include <dm/ofnode.h> |
Patrice Chotard | 28df1cf | 2017-07-18 11:57:14 +0200 | [diff] [blame] | 13 | #include <generic-phy.h> |
Patrice Chotard | 8a51b4b | 2017-07-18 11:57:13 +0200 | [diff] [blame] | 14 | #include <reset.h> |
Alexey Brodkin | fee331f | 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 | 155d9f6 | 2017-07-18 11:57:12 +0200 | [diff] [blame] | 23 | struct clk *clocks; /* clock list */ |
Patrice Chotard | 8a51b4b | 2017-07-18 11:57:13 +0200 | [diff] [blame] | 24 | struct reset_ctl *resets; /* reset list */ |
Patrice Chotard | 28df1cf | 2017-07-18 11:57:14 +0200 | [diff] [blame] | 25 | struct phy phy; |
Patrice Chotard | 155d9f6 | 2017-07-18 11:57:12 +0200 | [diff] [blame] | 26 | int clock_count; /* number of clock in clock list */ |
Patrice Chotard | 8a51b4b | 2017-07-18 11:57:13 +0200 | [diff] [blame] | 27 | int reset_count; /* number of reset in reset list */ |
Alexey Brodkin | fee331f | 2015-12-14 17:18:50 +0300 | [diff] [blame] | 28 | }; |
| 29 | |
Patrice Chotard | cab4d48 | 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) { |
| 44 | dev_err(dev, "failed to init usb phy\n"); |
| 45 | return ret; |
| 46 | } |
| 47 | |
| 48 | ret = generic_phy_power_on(&priv->phy); |
| 49 | if (ret) { |
| 50 | dev_err(dev, "failed to power on usb phy\n"); |
| 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) { |
| 66 | dev_err(dev, "failed to power off usb phy\n"); |
| 67 | return ret; |
| 68 | } |
| 69 | |
| 70 | ret = generic_phy_exit(&priv->phy); |
| 71 | if (ret) { |
| 72 | dev_err(dev, "failed to power off usb phy\n"); |
| 73 | return ret; |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | return 0; |
| 78 | } |
| 79 | |
Alexey Brodkin | fee331f | 2015-12-14 17:18:50 +0300 | [diff] [blame] | 80 | static int ohci_usb_probe(struct udevice *dev) |
| 81 | { |
Simon Glass | a821c4a | 2017-05-17 17:18:05 -0600 | [diff] [blame] | 82 | struct ohci_regs *regs = (struct ohci_regs *)devfdt_get_addr(dev); |
Patrice Chotard | 155d9f6 | 2017-07-18 11:57:12 +0200 | [diff] [blame] | 83 | struct generic_ohci *priv = dev_get_priv(dev); |
Patrice Chotard | 8a51b4b | 2017-07-18 11:57:13 +0200 | [diff] [blame] | 84 | int i, err, ret, clock_nb, reset_nb; |
Alexey Brodkin | fee331f | 2015-12-14 17:18:50 +0300 | [diff] [blame] | 85 | |
Patrice Chotard | 155d9f6 | 2017-07-18 11:57:12 +0200 | [diff] [blame] | 86 | err = 0; |
| 87 | priv->clock_count = 0; |
| 88 | clock_nb = dev_count_phandle_with_args(dev, "clocks", "#clock-cells"); |
| 89 | if (clock_nb > 0) { |
| 90 | priv->clocks = devm_kcalloc(dev, clock_nb, sizeof(struct clk), |
| 91 | GFP_KERNEL); |
| 92 | if (!priv->clocks) |
| 93 | return -ENOMEM; |
| 94 | |
| 95 | for (i = 0; i < clock_nb; i++) { |
| 96 | err = clk_get_by_index(dev, i, &priv->clocks[i]); |
| 97 | if (err < 0) |
| 98 | break; |
| 99 | |
| 100 | err = clk_enable(&priv->clocks[i]); |
Kever Yang | 6578db8 | 2019-08-28 16:23:47 +0800 | [diff] [blame] | 101 | if (err && err != -ENOSYS) { |
Patrice Chotard | 6048d42 | 2018-03-14 17:48:58 +0100 | [diff] [blame] | 102 | dev_err(dev, "failed to enable clock %d\n", i); |
Patrice Chotard | 155d9f6 | 2017-07-18 11:57:12 +0200 | [diff] [blame] | 103 | clk_free(&priv->clocks[i]); |
| 104 | goto clk_err; |
| 105 | } |
| 106 | priv->clock_count++; |
| 107 | } |
| 108 | } else if (clock_nb != -ENOENT) { |
Patrice Chotard | 6048d42 | 2018-03-14 17:48:58 +0100 | [diff] [blame] | 109 | dev_err(dev, "failed to get clock phandle(%d)\n", clock_nb); |
Patrice Chotard | 155d9f6 | 2017-07-18 11:57:12 +0200 | [diff] [blame] | 110 | return clock_nb; |
| 111 | } |
| 112 | |
Patrice Chotard | 8a51b4b | 2017-07-18 11:57:13 +0200 | [diff] [blame] | 113 | priv->reset_count = 0; |
| 114 | reset_nb = dev_count_phandle_with_args(dev, "resets", "#reset-cells"); |
| 115 | if (reset_nb > 0) { |
| 116 | priv->resets = devm_kcalloc(dev, reset_nb, |
| 117 | sizeof(struct reset_ctl), |
| 118 | GFP_KERNEL); |
| 119 | if (!priv->resets) |
| 120 | return -ENOMEM; |
| 121 | |
| 122 | for (i = 0; i < reset_nb; i++) { |
| 123 | err = reset_get_by_index(dev, i, &priv->resets[i]); |
| 124 | if (err < 0) |
| 125 | break; |
| 126 | |
| 127 | err = reset_deassert(&priv->resets[i]); |
| 128 | if (err) { |
Patrice Chotard | 6048d42 | 2018-03-14 17:48:58 +0100 | [diff] [blame] | 129 | dev_err(dev, "failed to deassert reset %d\n", i); |
Patrice Chotard | 8a51b4b | 2017-07-18 11:57:13 +0200 | [diff] [blame] | 130 | reset_free(&priv->resets[i]); |
| 131 | goto reset_err; |
| 132 | } |
| 133 | priv->reset_count++; |
| 134 | } |
| 135 | } else if (reset_nb != -ENOENT) { |
Patrice Chotard | 6048d42 | 2018-03-14 17:48:58 +0100 | [diff] [blame] | 136 | dev_err(dev, "failed to get reset phandle(%d)\n", reset_nb); |
Patrice Chotard | 8a51b4b | 2017-07-18 11:57:13 +0200 | [diff] [blame] | 137 | goto clk_err; |
| 138 | } |
| 139 | |
Patrice Chotard | cab4d48 | 2018-03-14 17:48:57 +0100 | [diff] [blame] | 140 | err = ohci_setup_phy(dev, 0); |
| 141 | if (err) |
Patrice Chotard | cab4d48 | 2018-03-14 17:48:57 +0100 | [diff] [blame] | 142 | goto reset_err; |
Patrice Chotard | 28df1cf | 2017-07-18 11:57:14 +0200 | [diff] [blame] | 143 | |
Patrice Chotard | 155d9f6 | 2017-07-18 11:57:12 +0200 | [diff] [blame] | 144 | err = ohci_register(dev, regs); |
| 145 | if (err) |
Patrice Chotard | 28df1cf | 2017-07-18 11:57:14 +0200 | [diff] [blame] | 146 | goto phy_err; |
Patrice Chotard | 155d9f6 | 2017-07-18 11:57:12 +0200 | [diff] [blame] | 147 | |
| 148 | return 0; |
| 149 | |
Patrice Chotard | 28df1cf | 2017-07-18 11:57:14 +0200 | [diff] [blame] | 150 | phy_err: |
Patrice Chotard | cab4d48 | 2018-03-14 17:48:57 +0100 | [diff] [blame] | 151 | ret = ohci_shutdown_phy(dev); |
| 152 | if (ret) |
| 153 | dev_err(dev, "failed to shutdown usb phy\n"); |
Patrice Chotard | 28df1cf | 2017-07-18 11:57:14 +0200 | [diff] [blame] | 154 | |
Patrice Chotard | 8a51b4b | 2017-07-18 11:57:13 +0200 | [diff] [blame] | 155 | reset_err: |
| 156 | ret = reset_release_all(priv->resets, priv->reset_count); |
| 157 | if (ret) |
Patrice Chotard | 6048d42 | 2018-03-14 17:48:58 +0100 | [diff] [blame] | 158 | dev_err(dev, "failed to assert all resets\n"); |
Patrice Chotard | 155d9f6 | 2017-07-18 11:57:12 +0200 | [diff] [blame] | 159 | clk_err: |
| 160 | ret = clk_release_all(priv->clocks, priv->clock_count); |
| 161 | if (ret) |
Patrice Chotard | 6048d42 | 2018-03-14 17:48:58 +0100 | [diff] [blame] | 162 | dev_err(dev, "failed to disable all clocks\n"); |
Patrice Chotard | 155d9f6 | 2017-07-18 11:57:12 +0200 | [diff] [blame] | 163 | |
| 164 | return err; |
Alexey Brodkin | fee331f | 2015-12-14 17:18:50 +0300 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | static int ohci_usb_remove(struct udevice *dev) |
| 168 | { |
Patrice Chotard | 155d9f6 | 2017-07-18 11:57:12 +0200 | [diff] [blame] | 169 | struct generic_ohci *priv = dev_get_priv(dev); |
| 170 | int ret; |
| 171 | |
| 172 | ret = ohci_deregister(dev); |
| 173 | if (ret) |
| 174 | return ret; |
| 175 | |
Patrice Chotard | cab4d48 | 2018-03-14 17:48:57 +0100 | [diff] [blame] | 176 | ret = ohci_shutdown_phy(dev); |
| 177 | if (ret) |
| 178 | return ret; |
Patrice Chotard | 28df1cf | 2017-07-18 11:57:14 +0200 | [diff] [blame] | 179 | |
Patrice Chotard | 8a51b4b | 2017-07-18 11:57:13 +0200 | [diff] [blame] | 180 | ret = reset_release_all(priv->resets, priv->reset_count); |
| 181 | if (ret) |
| 182 | return ret; |
| 183 | |
Patrice Chotard | 155d9f6 | 2017-07-18 11:57:12 +0200 | [diff] [blame] | 184 | return clk_release_all(priv->clocks, priv->clock_count); |
Alexey Brodkin | fee331f | 2015-12-14 17:18:50 +0300 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | static const struct udevice_id ohci_usb_ids[] = { |
| 188 | { .compatible = "generic-ohci" }, |
| 189 | { } |
| 190 | }; |
| 191 | |
| 192 | U_BOOT_DRIVER(ohci_generic) = { |
| 193 | .name = "ohci_generic", |
| 194 | .id = UCLASS_USB, |
| 195 | .of_match = ohci_usb_ids, |
| 196 | .probe = ohci_usb_probe, |
| 197 | .remove = ohci_usb_remove, |
| 198 | .ops = &ohci_usb_ops, |
| 199 | .priv_auto_alloc_size = sizeof(struct generic_ohci), |
| 200 | .flags = DM_FLAG_ALLOC_PRIV_DMA, |
| 201 | }; |