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