Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Stephen Warren | 185ad87 | 2016-06-17 09:43:58 -0600 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2016, NVIDIA CORPORATION. |
Stephen Warren | 185ad87 | 2016-06-17 09:43:58 -0600 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | #include <common.h> |
| 7 | #include <dm.h> |
| 8 | #include <fdtdec.h> |
| 9 | #include <reset.h> |
| 10 | #include <reset-uclass.h> |
Simon Glass | d66c5f7 | 2020-02-03 07:36:15 -0700 | [diff] [blame^] | 11 | #include <dm/devres.h> |
Stephen Warren | 185ad87 | 2016-06-17 09:43:58 -0600 | [diff] [blame] | 12 | |
Stephen Warren | 185ad87 | 2016-06-17 09:43:58 -0600 | [diff] [blame] | 13 | static inline struct reset_ops *reset_dev_ops(struct udevice *dev) |
| 14 | { |
| 15 | return (struct reset_ops *)dev->driver->ops; |
| 16 | } |
| 17 | |
| 18 | static int reset_of_xlate_default(struct reset_ctl *reset_ctl, |
Simon Glass | aec8cc0 | 2017-05-18 20:09:50 -0600 | [diff] [blame] | 19 | struct ofnode_phandle_args *args) |
Stephen Warren | 185ad87 | 2016-06-17 09:43:58 -0600 | [diff] [blame] | 20 | { |
| 21 | debug("%s(reset_ctl=%p)\n", __func__, reset_ctl); |
| 22 | |
| 23 | if (args->args_count != 1) { |
| 24 | debug("Invaild args_count: %d\n", args->args_count); |
| 25 | return -EINVAL; |
| 26 | } |
| 27 | |
| 28 | reset_ctl->id = args->args[0]; |
| 29 | |
| 30 | return 0; |
| 31 | } |
| 32 | |
Jagan Teki | 01af115 | 2019-02-28 00:26:55 +0530 | [diff] [blame] | 33 | static int reset_get_by_index_tail(int ret, ofnode node, |
| 34 | struct ofnode_phandle_args *args, |
| 35 | const char *list_name, int index, |
| 36 | struct reset_ctl *reset_ctl) |
Stephen Warren | 185ad87 | 2016-06-17 09:43:58 -0600 | [diff] [blame] | 37 | { |
Stephen Warren | 185ad87 | 2016-06-17 09:43:58 -0600 | [diff] [blame] | 38 | struct udevice *dev_reset; |
| 39 | struct reset_ops *ops; |
| 40 | |
Jagan Teki | 01af115 | 2019-02-28 00:26:55 +0530 | [diff] [blame] | 41 | assert(reset_ctl); |
Patrice Chotard | e4d368e | 2017-07-18 11:57:06 +0200 | [diff] [blame] | 42 | reset_ctl->dev = NULL; |
Jagan Teki | 01af115 | 2019-02-28 00:26:55 +0530 | [diff] [blame] | 43 | if (ret) |
Stephen Warren | 185ad87 | 2016-06-17 09:43:58 -0600 | [diff] [blame] | 44 | return ret; |
Stephen Warren | 185ad87 | 2016-06-17 09:43:58 -0600 | [diff] [blame] | 45 | |
Jagan Teki | 01af115 | 2019-02-28 00:26:55 +0530 | [diff] [blame] | 46 | ret = uclass_get_device_by_ofnode(UCLASS_RESET, args->node, |
Simon Glass | aec8cc0 | 2017-05-18 20:09:50 -0600 | [diff] [blame] | 47 | &dev_reset); |
Stephen Warren | 185ad87 | 2016-06-17 09:43:58 -0600 | [diff] [blame] | 48 | if (ret) { |
Simon Glass | aec8cc0 | 2017-05-18 20:09:50 -0600 | [diff] [blame] | 49 | debug("%s: uclass_get_device_by_ofnode() failed: %d\n", |
Stephen Warren | 185ad87 | 2016-06-17 09:43:58 -0600 | [diff] [blame] | 50 | __func__, ret); |
Jagan Teki | 01af115 | 2019-02-28 00:26:55 +0530 | [diff] [blame] | 51 | debug("%s %d\n", ofnode_get_name(args->node), args->args[0]); |
Stephen Warren | 185ad87 | 2016-06-17 09:43:58 -0600 | [diff] [blame] | 52 | return ret; |
| 53 | } |
| 54 | ops = reset_dev_ops(dev_reset); |
| 55 | |
| 56 | reset_ctl->dev = dev_reset; |
| 57 | if (ops->of_xlate) |
Jagan Teki | 01af115 | 2019-02-28 00:26:55 +0530 | [diff] [blame] | 58 | ret = ops->of_xlate(reset_ctl, args); |
Stephen Warren | 185ad87 | 2016-06-17 09:43:58 -0600 | [diff] [blame] | 59 | else |
Jagan Teki | 01af115 | 2019-02-28 00:26:55 +0530 | [diff] [blame] | 60 | ret = reset_of_xlate_default(reset_ctl, args); |
Stephen Warren | 185ad87 | 2016-06-17 09:43:58 -0600 | [diff] [blame] | 61 | if (ret) { |
| 62 | debug("of_xlate() failed: %d\n", ret); |
| 63 | return ret; |
| 64 | } |
| 65 | |
| 66 | ret = ops->request(reset_ctl); |
| 67 | if (ret) { |
| 68 | debug("ops->request() failed: %d\n", ret); |
| 69 | return ret; |
| 70 | } |
| 71 | |
| 72 | return 0; |
| 73 | } |
| 74 | |
Jagan Teki | 01af115 | 2019-02-28 00:26:55 +0530 | [diff] [blame] | 75 | int reset_get_by_index(struct udevice *dev, int index, |
| 76 | struct reset_ctl *reset_ctl) |
| 77 | { |
| 78 | struct ofnode_phandle_args args; |
| 79 | int ret; |
| 80 | |
| 81 | ret = dev_read_phandle_with_args(dev, "resets", "#reset-cells", 0, |
| 82 | index, &args); |
| 83 | |
| 84 | return reset_get_by_index_tail(ret, dev_ofnode(dev), &args, "resets", |
| 85 | index > 0, reset_ctl); |
| 86 | } |
| 87 | |
| 88 | int reset_get_by_index_nodev(ofnode node, int index, |
| 89 | struct reset_ctl *reset_ctl) |
| 90 | { |
| 91 | struct ofnode_phandle_args args; |
| 92 | int ret; |
| 93 | |
| 94 | ret = ofnode_parse_phandle_with_args(node, "resets", "#reset-cells", 0, |
| 95 | index > 0, &args); |
| 96 | |
| 97 | return reset_get_by_index_tail(ret, node, &args, "resets", |
| 98 | index > 0, reset_ctl); |
| 99 | } |
| 100 | |
Neil Armstrong | dbb26d3 | 2018-04-03 11:40:50 +0200 | [diff] [blame] | 101 | int reset_get_bulk(struct udevice *dev, struct reset_ctl_bulk *bulk) |
| 102 | { |
| 103 | int i, ret, err, count; |
| 104 | |
| 105 | bulk->count = 0; |
| 106 | |
| 107 | count = dev_count_phandle_with_args(dev, "resets", "#reset-cells"); |
Neil Armstrong | 6acc49a | 2018-04-17 11:30:22 +0200 | [diff] [blame] | 108 | if (count < 1) |
| 109 | return count; |
Neil Armstrong | dbb26d3 | 2018-04-03 11:40:50 +0200 | [diff] [blame] | 110 | |
| 111 | bulk->resets = devm_kcalloc(dev, count, sizeof(struct reset_ctl), |
| 112 | GFP_KERNEL); |
| 113 | if (!bulk->resets) |
| 114 | return -ENOMEM; |
| 115 | |
| 116 | for (i = 0; i < count; i++) { |
| 117 | ret = reset_get_by_index(dev, i, &bulk->resets[i]); |
| 118 | if (ret < 0) |
| 119 | goto bulk_get_err; |
| 120 | |
| 121 | ++bulk->count; |
| 122 | } |
| 123 | |
| 124 | return 0; |
| 125 | |
| 126 | bulk_get_err: |
| 127 | err = reset_release_all(bulk->resets, bulk->count); |
| 128 | if (err) |
| 129 | debug("%s: could release all resets for %p\n", |
| 130 | __func__, dev); |
| 131 | |
| 132 | return ret; |
| 133 | } |
| 134 | |
Stephen Warren | 185ad87 | 2016-06-17 09:43:58 -0600 | [diff] [blame] | 135 | int reset_get_by_name(struct udevice *dev, const char *name, |
| 136 | struct reset_ctl *reset_ctl) |
| 137 | { |
| 138 | int index; |
| 139 | |
| 140 | debug("%s(dev=%p, name=%s, reset_ctl=%p)\n", __func__, dev, name, |
| 141 | reset_ctl); |
Patrice Chotard | e4d368e | 2017-07-18 11:57:06 +0200 | [diff] [blame] | 142 | reset_ctl->dev = NULL; |
Stephen Warren | 185ad87 | 2016-06-17 09:43:58 -0600 | [diff] [blame] | 143 | |
Simon Glass | aec8cc0 | 2017-05-18 20:09:50 -0600 | [diff] [blame] | 144 | index = dev_read_stringlist_search(dev, "reset-names", name); |
Stephen Warren | 185ad87 | 2016-06-17 09:43:58 -0600 | [diff] [blame] | 145 | if (index < 0) { |
Simon Glass | b0ea740 | 2016-10-02 17:59:28 -0600 | [diff] [blame] | 146 | debug("fdt_stringlist_search() failed: %d\n", index); |
Stephen Warren | 185ad87 | 2016-06-17 09:43:58 -0600 | [diff] [blame] | 147 | return index; |
| 148 | } |
| 149 | |
| 150 | return reset_get_by_index(dev, index, reset_ctl); |
| 151 | } |
| 152 | |
Patrice Chotard | 76290e3 | 2017-07-18 11:57:05 +0200 | [diff] [blame] | 153 | int reset_request(struct reset_ctl *reset_ctl) |
| 154 | { |
| 155 | struct reset_ops *ops = reset_dev_ops(reset_ctl->dev); |
| 156 | |
| 157 | debug("%s(reset_ctl=%p)\n", __func__, reset_ctl); |
| 158 | |
| 159 | return ops->request(reset_ctl); |
| 160 | } |
| 161 | |
Stephen Warren | 185ad87 | 2016-06-17 09:43:58 -0600 | [diff] [blame] | 162 | int reset_free(struct reset_ctl *reset_ctl) |
| 163 | { |
| 164 | struct reset_ops *ops = reset_dev_ops(reset_ctl->dev); |
| 165 | |
| 166 | debug("%s(reset_ctl=%p)\n", __func__, reset_ctl); |
| 167 | |
Simon Glass | 1928cd4 | 2020-02-03 07:35:52 -0700 | [diff] [blame] | 168 | return ops->rfree(reset_ctl); |
Stephen Warren | 185ad87 | 2016-06-17 09:43:58 -0600 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | int reset_assert(struct reset_ctl *reset_ctl) |
| 172 | { |
| 173 | struct reset_ops *ops = reset_dev_ops(reset_ctl->dev); |
| 174 | |
| 175 | debug("%s(reset_ctl=%p)\n", __func__, reset_ctl); |
| 176 | |
| 177 | return ops->rst_assert(reset_ctl); |
| 178 | } |
| 179 | |
Neil Armstrong | dbb26d3 | 2018-04-03 11:40:50 +0200 | [diff] [blame] | 180 | int reset_assert_bulk(struct reset_ctl_bulk *bulk) |
| 181 | { |
| 182 | int i, ret; |
| 183 | |
| 184 | for (i = 0; i < bulk->count; i++) { |
| 185 | ret = reset_assert(&bulk->resets[i]); |
| 186 | if (ret < 0) |
| 187 | return ret; |
| 188 | } |
| 189 | |
| 190 | return 0; |
| 191 | } |
| 192 | |
Stephen Warren | 185ad87 | 2016-06-17 09:43:58 -0600 | [diff] [blame] | 193 | int reset_deassert(struct reset_ctl *reset_ctl) |
| 194 | { |
| 195 | struct reset_ops *ops = reset_dev_ops(reset_ctl->dev); |
| 196 | |
| 197 | debug("%s(reset_ctl=%p)\n", __func__, reset_ctl); |
| 198 | |
| 199 | return ops->rst_deassert(reset_ctl); |
| 200 | } |
| 201 | |
Neil Armstrong | dbb26d3 | 2018-04-03 11:40:50 +0200 | [diff] [blame] | 202 | int reset_deassert_bulk(struct reset_ctl_bulk *bulk) |
| 203 | { |
| 204 | int i, ret; |
| 205 | |
| 206 | for (i = 0; i < bulk->count; i++) { |
| 207 | ret = reset_deassert(&bulk->resets[i]); |
| 208 | if (ret < 0) |
| 209 | return ret; |
| 210 | } |
| 211 | |
| 212 | return 0; |
| 213 | } |
| 214 | |
Andreas Dannenberg | e2bc740 | 2018-08-27 15:57:39 +0530 | [diff] [blame] | 215 | int reset_status(struct reset_ctl *reset_ctl) |
| 216 | { |
| 217 | struct reset_ops *ops = reset_dev_ops(reset_ctl->dev); |
| 218 | |
| 219 | debug("%s(reset_ctl=%p)\n", __func__, reset_ctl); |
| 220 | |
| 221 | return ops->rst_status(reset_ctl); |
| 222 | } |
| 223 | |
Patrice Chotard | e4d368e | 2017-07-18 11:57:06 +0200 | [diff] [blame] | 224 | int reset_release_all(struct reset_ctl *reset_ctl, int count) |
| 225 | { |
| 226 | int i, ret; |
| 227 | |
| 228 | for (i = 0; i < count; i++) { |
| 229 | debug("%s(reset_ctl[%d]=%p)\n", __func__, i, &reset_ctl[i]); |
| 230 | |
| 231 | /* check if reset has been previously requested */ |
| 232 | if (!reset_ctl[i].dev) |
| 233 | continue; |
| 234 | |
| 235 | ret = reset_assert(&reset_ctl[i]); |
| 236 | if (ret) |
| 237 | return ret; |
| 238 | |
| 239 | ret = reset_free(&reset_ctl[i]); |
| 240 | if (ret) |
| 241 | return ret; |
| 242 | } |
| 243 | |
| 244 | return 0; |
| 245 | } |
| 246 | |
Stephen Warren | 185ad87 | 2016-06-17 09:43:58 -0600 | [diff] [blame] | 247 | UCLASS_DRIVER(reset) = { |
| 248 | .id = UCLASS_RESET, |
| 249 | .name = "reset", |
| 250 | }; |