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 | |
Alexey Brodkin | 83fd312 | 2015-12-14 17:18:50 +0300 | [diff] [blame] | 17 | struct generic_ohci { |
| 18 | ohci_t ohci; |
Patrice Chotard | 37a197a | 2017-07-18 11:57:12 +0200 | [diff] [blame] | 19 | struct clk *clocks; /* clock list */ |
Patrice Chotard | 170229e | 2017-07-18 11:57:13 +0200 | [diff] [blame] | 20 | struct reset_ctl *resets; /* reset list */ |
Patrice Chotard | c6e19d3 | 2017-07-18 11:57:14 +0200 | [diff] [blame] | 21 | struct phy phy; |
Patrice Chotard | 37a197a | 2017-07-18 11:57:12 +0200 | [diff] [blame] | 22 | int clock_count; /* number of clock in clock list */ |
Patrice Chotard | 170229e | 2017-07-18 11:57:13 +0200 | [diff] [blame] | 23 | int reset_count; /* number of reset in reset list */ |
Alexey Brodkin | 83fd312 | 2015-12-14 17:18:50 +0300 | [diff] [blame] | 24 | }; |
| 25 | |
| 26 | static int ohci_usb_probe(struct udevice *dev) |
| 27 | { |
Masahiro Yamada | 1096ae1 | 2020-07-17 14:36:46 +0900 | [diff] [blame] | 28 | struct ohci_regs *regs = dev_read_addr_ptr(dev); |
Patrice Chotard | 37a197a | 2017-07-18 11:57:12 +0200 | [diff] [blame] | 29 | struct generic_ohci *priv = dev_get_priv(dev); |
Patrice Chotard | 170229e | 2017-07-18 11:57:13 +0200 | [diff] [blame] | 30 | int i, err, ret, clock_nb, reset_nb; |
Alexey Brodkin | 83fd312 | 2015-12-14 17:18:50 +0300 | [diff] [blame] | 31 | |
Patrice Chotard | 37a197a | 2017-07-18 11:57:12 +0200 | [diff] [blame] | 32 | err = 0; |
| 33 | priv->clock_count = 0; |
Patrick Delaunay | d776a84 | 2020-09-25 09:41:14 +0200 | [diff] [blame] | 34 | clock_nb = dev_count_phandle_with_args(dev, "clocks", "#clock-cells", |
| 35 | 0); |
Patrice Chotard | 37a197a | 2017-07-18 11:57:12 +0200 | [diff] [blame] | 36 | if (clock_nb > 0) { |
| 37 | priv->clocks = devm_kcalloc(dev, clock_nb, sizeof(struct clk), |
| 38 | GFP_KERNEL); |
| 39 | if (!priv->clocks) |
| 40 | return -ENOMEM; |
| 41 | |
| 42 | for (i = 0; i < clock_nb; i++) { |
| 43 | err = clk_get_by_index(dev, i, &priv->clocks[i]); |
| 44 | if (err < 0) |
| 45 | break; |
| 46 | |
| 47 | err = clk_enable(&priv->clocks[i]); |
Kever Yang | 05b015b | 2019-08-28 16:23:47 +0800 | [diff] [blame] | 48 | if (err && err != -ENOSYS) { |
Patrice Chotard | 7c11e35 | 2018-03-14 17:48:58 +0100 | [diff] [blame] | 49 | dev_err(dev, "failed to enable clock %d\n", i); |
Patrice Chotard | 37a197a | 2017-07-18 11:57:12 +0200 | [diff] [blame] | 50 | clk_free(&priv->clocks[i]); |
| 51 | goto clk_err; |
| 52 | } |
| 53 | priv->clock_count++; |
| 54 | } |
| 55 | } else if (clock_nb != -ENOENT) { |
Patrice Chotard | 7c11e35 | 2018-03-14 17:48:58 +0100 | [diff] [blame] | 56 | dev_err(dev, "failed to get clock phandle(%d)\n", clock_nb); |
Patrice Chotard | 37a197a | 2017-07-18 11:57:12 +0200 | [diff] [blame] | 57 | return clock_nb; |
| 58 | } |
| 59 | |
Patrice Chotard | 170229e | 2017-07-18 11:57:13 +0200 | [diff] [blame] | 60 | priv->reset_count = 0; |
Patrick Delaunay | d776a84 | 2020-09-25 09:41:14 +0200 | [diff] [blame] | 61 | reset_nb = dev_count_phandle_with_args(dev, "resets", "#reset-cells", |
| 62 | 0); |
Patrice Chotard | 170229e | 2017-07-18 11:57:13 +0200 | [diff] [blame] | 63 | if (reset_nb > 0) { |
| 64 | priv->resets = devm_kcalloc(dev, reset_nb, |
| 65 | sizeof(struct reset_ctl), |
| 66 | GFP_KERNEL); |
| 67 | if (!priv->resets) |
| 68 | return -ENOMEM; |
| 69 | |
| 70 | for (i = 0; i < reset_nb; i++) { |
| 71 | err = reset_get_by_index(dev, i, &priv->resets[i]); |
| 72 | if (err < 0) |
| 73 | break; |
| 74 | |
| 75 | err = reset_deassert(&priv->resets[i]); |
| 76 | if (err) { |
Patrice Chotard | 7c11e35 | 2018-03-14 17:48:58 +0100 | [diff] [blame] | 77 | dev_err(dev, "failed to deassert reset %d\n", i); |
Patrice Chotard | 170229e | 2017-07-18 11:57:13 +0200 | [diff] [blame] | 78 | reset_free(&priv->resets[i]); |
| 79 | goto reset_err; |
| 80 | } |
| 81 | priv->reset_count++; |
| 82 | } |
| 83 | } else if (reset_nb != -ENOENT) { |
Patrice Chotard | 7c11e35 | 2018-03-14 17:48:58 +0100 | [diff] [blame] | 84 | dev_err(dev, "failed to get reset phandle(%d)\n", reset_nb); |
Patrice Chotard | 170229e | 2017-07-18 11:57:13 +0200 | [diff] [blame] | 85 | goto clk_err; |
| 86 | } |
| 87 | |
Patrice Chotard | cb9f1e4 | 2022-09-06 08:15:27 +0200 | [diff] [blame] | 88 | err = generic_setup_phy(dev, &priv->phy, 0); |
Patrice Chotard | ab0b205e | 2018-03-14 17:48:57 +0100 | [diff] [blame] | 89 | if (err) |
Patrice Chotard | ab0b205e | 2018-03-14 17:48:57 +0100 | [diff] [blame] | 90 | goto reset_err; |
Patrice Chotard | c6e19d3 | 2017-07-18 11:57:14 +0200 | [diff] [blame] | 91 | |
Patrice Chotard | 37a197a | 2017-07-18 11:57:12 +0200 | [diff] [blame] | 92 | err = ohci_register(dev, regs); |
| 93 | if (err) |
Patrice Chotard | c6e19d3 | 2017-07-18 11:57:14 +0200 | [diff] [blame] | 94 | goto phy_err; |
Patrice Chotard | 37a197a | 2017-07-18 11:57:12 +0200 | [diff] [blame] | 95 | |
| 96 | return 0; |
| 97 | |
Patrice Chotard | c6e19d3 | 2017-07-18 11:57:14 +0200 | [diff] [blame] | 98 | phy_err: |
Patrice Chotard | cb9f1e4 | 2022-09-06 08:15:27 +0200 | [diff] [blame] | 99 | ret = generic_shutdown_phy(&priv->phy); |
Patrice Chotard | ab0b205e | 2018-03-14 17:48:57 +0100 | [diff] [blame] | 100 | if (ret) |
| 101 | dev_err(dev, "failed to shutdown usb phy\n"); |
Patrice Chotard | c6e19d3 | 2017-07-18 11:57:14 +0200 | [diff] [blame] | 102 | |
Patrice Chotard | 170229e | 2017-07-18 11:57:13 +0200 | [diff] [blame] | 103 | reset_err: |
| 104 | ret = reset_release_all(priv->resets, priv->reset_count); |
| 105 | if (ret) |
Patrice Chotard | 7c11e35 | 2018-03-14 17:48:58 +0100 | [diff] [blame] | 106 | dev_err(dev, "failed to assert all resets\n"); |
Patrice Chotard | 37a197a | 2017-07-18 11:57:12 +0200 | [diff] [blame] | 107 | clk_err: |
| 108 | ret = clk_release_all(priv->clocks, priv->clock_count); |
| 109 | if (ret) |
Patrice Chotard | 7c11e35 | 2018-03-14 17:48:58 +0100 | [diff] [blame] | 110 | dev_err(dev, "failed to disable all clocks\n"); |
Patrice Chotard | 37a197a | 2017-07-18 11:57:12 +0200 | [diff] [blame] | 111 | |
| 112 | return err; |
Alexey Brodkin | 83fd312 | 2015-12-14 17:18:50 +0300 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | static int ohci_usb_remove(struct udevice *dev) |
| 116 | { |
Patrice Chotard | 37a197a | 2017-07-18 11:57:12 +0200 | [diff] [blame] | 117 | struct generic_ohci *priv = dev_get_priv(dev); |
| 118 | int ret; |
| 119 | |
| 120 | ret = ohci_deregister(dev); |
| 121 | if (ret) |
| 122 | return ret; |
| 123 | |
Patrice Chotard | cb9f1e4 | 2022-09-06 08:15:27 +0200 | [diff] [blame] | 124 | ret = generic_shutdown_phy(&priv->phy); |
Patrice Chotard | ab0b205e | 2018-03-14 17:48:57 +0100 | [diff] [blame] | 125 | if (ret) |
| 126 | return ret; |
Patrice Chotard | c6e19d3 | 2017-07-18 11:57:14 +0200 | [diff] [blame] | 127 | |
Patrice Chotard | 170229e | 2017-07-18 11:57:13 +0200 | [diff] [blame] | 128 | ret = reset_release_all(priv->resets, priv->reset_count); |
| 129 | if (ret) |
| 130 | return ret; |
| 131 | |
Patrice Chotard | 37a197a | 2017-07-18 11:57:12 +0200 | [diff] [blame] | 132 | return clk_release_all(priv->clocks, priv->clock_count); |
Alexey Brodkin | 83fd312 | 2015-12-14 17:18:50 +0300 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | static const struct udevice_id ohci_usb_ids[] = { |
| 136 | { .compatible = "generic-ohci" }, |
| 137 | { } |
| 138 | }; |
| 139 | |
| 140 | U_BOOT_DRIVER(ohci_generic) = { |
| 141 | .name = "ohci_generic", |
| 142 | .id = UCLASS_USB, |
| 143 | .of_match = ohci_usb_ids, |
| 144 | .probe = ohci_usb_probe, |
| 145 | .remove = ohci_usb_remove, |
| 146 | .ops = &ohci_usb_ops, |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 147 | .priv_auto = sizeof(struct generic_ohci), |
Alexey Brodkin | 83fd312 | 2015-12-14 17:18:50 +0300 | [diff] [blame] | 148 | .flags = DM_FLAG_ALLOC_PRIV_DMA, |
| 149 | }; |