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