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 | |
Patrick Delaunay | 8131335 | 2021-04-27 11:02:19 +0200 | [diff] [blame] | 6 | #define LOG_CATEGORY UCLASS_RESET |
| 7 | |
Stephen Warren | 185ad87 | 2016-06-17 09:43:58 -0600 | [diff] [blame] | 8 | #include <dm.h> |
| 9 | #include <fdtdec.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 10 | #include <log.h> |
Simon Glass | 9bc1564 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 11 | #include <malloc.h> |
Stephen Warren | 185ad87 | 2016-06-17 09:43:58 -0600 | [diff] [blame] | 12 | #include <reset.h> |
| 13 | #include <reset-uclass.h> |
Simon Glass | d66c5f7 | 2020-02-03 07:36:15 -0700 | [diff] [blame] | 14 | #include <dm/devres.h> |
Jean-Jacques Hiblot | 5be97a3 | 2020-09-09 15:37:03 +0530 | [diff] [blame] | 15 | #include <dm/lists.h> |
Stephen Warren | 185ad87 | 2016-06-17 09:43:58 -0600 | [diff] [blame] | 16 | |
Stephen Warren | 185ad87 | 2016-06-17 09:43:58 -0600 | [diff] [blame] | 17 | static inline struct reset_ops *reset_dev_ops(struct udevice *dev) |
| 18 | { |
| 19 | return (struct reset_ops *)dev->driver->ops; |
| 20 | } |
| 21 | |
| 22 | static int reset_of_xlate_default(struct reset_ctl *reset_ctl, |
Simon Glass | aec8cc0 | 2017-05-18 20:09:50 -0600 | [diff] [blame] | 23 | struct ofnode_phandle_args *args) |
Stephen Warren | 185ad87 | 2016-06-17 09:43:58 -0600 | [diff] [blame] | 24 | { |
| 25 | debug("%s(reset_ctl=%p)\n", __func__, reset_ctl); |
| 26 | |
| 27 | if (args->args_count != 1) { |
Sean Anderson | a1b654b | 2021-12-01 14:26:53 -0500 | [diff] [blame] | 28 | debug("Invalid args_count: %d\n", args->args_count); |
Stephen Warren | 185ad87 | 2016-06-17 09:43:58 -0600 | [diff] [blame] | 29 | return -EINVAL; |
| 30 | } |
| 31 | |
| 32 | reset_ctl->id = args->args[0]; |
| 33 | |
| 34 | return 0; |
| 35 | } |
| 36 | |
Jagan Teki | 01af115 | 2019-02-28 00:26:55 +0530 | [diff] [blame] | 37 | static int reset_get_by_index_tail(int ret, ofnode node, |
| 38 | struct ofnode_phandle_args *args, |
| 39 | const char *list_name, int index, |
| 40 | struct reset_ctl *reset_ctl) |
Stephen Warren | 185ad87 | 2016-06-17 09:43:58 -0600 | [diff] [blame] | 41 | { |
Stephen Warren | 185ad87 | 2016-06-17 09:43:58 -0600 | [diff] [blame] | 42 | struct udevice *dev_reset; |
| 43 | struct reset_ops *ops; |
| 44 | |
Jagan Teki | 01af115 | 2019-02-28 00:26:55 +0530 | [diff] [blame] | 45 | assert(reset_ctl); |
Patrice Chotard | e4d368e | 2017-07-18 11:57:06 +0200 | [diff] [blame] | 46 | reset_ctl->dev = NULL; |
Jagan Teki | 01af115 | 2019-02-28 00:26:55 +0530 | [diff] [blame] | 47 | if (ret) |
Stephen Warren | 185ad87 | 2016-06-17 09:43:58 -0600 | [diff] [blame] | 48 | return ret; |
Stephen Warren | 185ad87 | 2016-06-17 09:43:58 -0600 | [diff] [blame] | 49 | |
Jagan Teki | 01af115 | 2019-02-28 00:26:55 +0530 | [diff] [blame] | 50 | ret = uclass_get_device_by_ofnode(UCLASS_RESET, args->node, |
Simon Glass | aec8cc0 | 2017-05-18 20:09:50 -0600 | [diff] [blame] | 51 | &dev_reset); |
Stephen Warren | 185ad87 | 2016-06-17 09:43:58 -0600 | [diff] [blame] | 52 | if (ret) { |
Simon Glass | aec8cc0 | 2017-05-18 20:09:50 -0600 | [diff] [blame] | 53 | debug("%s: uclass_get_device_by_ofnode() failed: %d\n", |
Stephen Warren | 185ad87 | 2016-06-17 09:43:58 -0600 | [diff] [blame] | 54 | __func__, ret); |
Jagan Teki | 01af115 | 2019-02-28 00:26:55 +0530 | [diff] [blame] | 55 | debug("%s %d\n", ofnode_get_name(args->node), args->args[0]); |
Stephen Warren | 185ad87 | 2016-06-17 09:43:58 -0600 | [diff] [blame] | 56 | return ret; |
| 57 | } |
| 58 | ops = reset_dev_ops(dev_reset); |
| 59 | |
| 60 | reset_ctl->dev = dev_reset; |
| 61 | if (ops->of_xlate) |
Jagan Teki | 01af115 | 2019-02-28 00:26:55 +0530 | [diff] [blame] | 62 | ret = ops->of_xlate(reset_ctl, args); |
Stephen Warren | 185ad87 | 2016-06-17 09:43:58 -0600 | [diff] [blame] | 63 | else |
Jagan Teki | 01af115 | 2019-02-28 00:26:55 +0530 | [diff] [blame] | 64 | ret = reset_of_xlate_default(reset_ctl, args); |
Stephen Warren | 185ad87 | 2016-06-17 09:43:58 -0600 | [diff] [blame] | 65 | if (ret) { |
| 66 | debug("of_xlate() failed: %d\n", ret); |
| 67 | return ret; |
| 68 | } |
| 69 | |
Marek Vasut | f817d17 | 2022-04-26 23:43:30 +0200 | [diff] [blame] | 70 | ret = ops->request ? ops->request(reset_ctl) : 0; |
Stephen Warren | 185ad87 | 2016-06-17 09:43:58 -0600 | [diff] [blame] | 71 | if (ret) { |
| 72 | debug("ops->request() failed: %d\n", ret); |
| 73 | return ret; |
| 74 | } |
| 75 | |
| 76 | return 0; |
| 77 | } |
| 78 | |
Jagan Teki | 01af115 | 2019-02-28 00:26:55 +0530 | [diff] [blame] | 79 | int reset_get_by_index(struct udevice *dev, int index, |
| 80 | struct reset_ctl *reset_ctl) |
| 81 | { |
| 82 | struct ofnode_phandle_args args; |
| 83 | int ret; |
| 84 | |
| 85 | ret = dev_read_phandle_with_args(dev, "resets", "#reset-cells", 0, |
| 86 | index, &args); |
| 87 | |
| 88 | return reset_get_by_index_tail(ret, dev_ofnode(dev), &args, "resets", |
| 89 | index > 0, reset_ctl); |
| 90 | } |
| 91 | |
| 92 | int reset_get_by_index_nodev(ofnode node, int index, |
| 93 | struct reset_ctl *reset_ctl) |
| 94 | { |
| 95 | struct ofnode_phandle_args args; |
| 96 | int ret; |
| 97 | |
| 98 | ret = ofnode_parse_phandle_with_args(node, "resets", "#reset-cells", 0, |
Neil Armstrong | 6198db6 | 2021-04-20 10:42:26 +0200 | [diff] [blame] | 99 | index, &args); |
Jagan Teki | 01af115 | 2019-02-28 00:26:55 +0530 | [diff] [blame] | 100 | |
| 101 | return reset_get_by_index_tail(ret, node, &args, "resets", |
| 102 | index > 0, reset_ctl); |
| 103 | } |
| 104 | |
Jean-Jacques Hiblot | 5be97a3 | 2020-09-09 15:37:03 +0530 | [diff] [blame] | 105 | static int __reset_get_bulk(struct udevice *dev, ofnode node, |
| 106 | struct reset_ctl_bulk *bulk) |
Neil Armstrong | dbb26d3 | 2018-04-03 11:40:50 +0200 | [diff] [blame] | 107 | { |
| 108 | int i, ret, err, count; |
Jean-Jacques Hiblot | 5be97a3 | 2020-09-09 15:37:03 +0530 | [diff] [blame] | 109 | |
Neil Armstrong | dbb26d3 | 2018-04-03 11:40:50 +0200 | [diff] [blame] | 110 | bulk->count = 0; |
| 111 | |
Patrick Delaunay | d776a84 | 2020-09-25 09:41:14 +0200 | [diff] [blame] | 112 | count = ofnode_count_phandle_with_args(node, "resets", "#reset-cells", |
| 113 | 0); |
Neil Armstrong | 6acc49a | 2018-04-17 11:30:22 +0200 | [diff] [blame] | 114 | if (count < 1) |
| 115 | return count; |
Neil Armstrong | dbb26d3 | 2018-04-03 11:40:50 +0200 | [diff] [blame] | 116 | |
| 117 | bulk->resets = devm_kcalloc(dev, count, sizeof(struct reset_ctl), |
| 118 | GFP_KERNEL); |
| 119 | if (!bulk->resets) |
| 120 | return -ENOMEM; |
| 121 | |
| 122 | for (i = 0; i < count; i++) { |
Jean-Jacques Hiblot | 5be97a3 | 2020-09-09 15:37:03 +0530 | [diff] [blame] | 123 | ret = reset_get_by_index_nodev(node, i, &bulk->resets[i]); |
Neil Armstrong | dbb26d3 | 2018-04-03 11:40:50 +0200 | [diff] [blame] | 124 | if (ret < 0) |
| 125 | goto bulk_get_err; |
| 126 | |
| 127 | ++bulk->count; |
| 128 | } |
| 129 | |
| 130 | return 0; |
| 131 | |
| 132 | bulk_get_err: |
| 133 | err = reset_release_all(bulk->resets, bulk->count); |
| 134 | if (err) |
| 135 | debug("%s: could release all resets for %p\n", |
| 136 | __func__, dev); |
| 137 | |
| 138 | return ret; |
| 139 | } |
| 140 | |
Jean-Jacques Hiblot | 5be97a3 | 2020-09-09 15:37:03 +0530 | [diff] [blame] | 141 | int reset_get_bulk(struct udevice *dev, struct reset_ctl_bulk *bulk) |
| 142 | { |
| 143 | return __reset_get_bulk(dev, dev_ofnode(dev), bulk); |
| 144 | } |
| 145 | |
Stephen Warren | 185ad87 | 2016-06-17 09:43:58 -0600 | [diff] [blame] | 146 | int reset_get_by_name(struct udevice *dev, const char *name, |
| 147 | struct reset_ctl *reset_ctl) |
| 148 | { |
Samuel Holland | 6078bff | 2023-01-21 18:02:52 -0600 | [diff] [blame] | 149 | int index = 0; |
Stephen Warren | 185ad87 | 2016-06-17 09:43:58 -0600 | [diff] [blame] | 150 | |
| 151 | debug("%s(dev=%p, name=%s, reset_ctl=%p)\n", __func__, dev, name, |
| 152 | reset_ctl); |
Patrice Chotard | e4d368e | 2017-07-18 11:57:06 +0200 | [diff] [blame] | 153 | reset_ctl->dev = NULL; |
Stephen Warren | 185ad87 | 2016-06-17 09:43:58 -0600 | [diff] [blame] | 154 | |
Samuel Holland | 6078bff | 2023-01-21 18:02:52 -0600 | [diff] [blame] | 155 | if (name) { |
| 156 | index = dev_read_stringlist_search(dev, "reset-names", name); |
| 157 | if (index < 0) { |
| 158 | debug("fdt_stringlist_search() failed: %d\n", index); |
| 159 | return index; |
| 160 | } |
Stephen Warren | 185ad87 | 2016-06-17 09:43:58 -0600 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | return reset_get_by_index(dev, index, reset_ctl); |
| 164 | } |
| 165 | |
Patrice Chotard | 76290e3 | 2017-07-18 11:57:05 +0200 | [diff] [blame] | 166 | int reset_request(struct reset_ctl *reset_ctl) |
| 167 | { |
| 168 | struct reset_ops *ops = reset_dev_ops(reset_ctl->dev); |
| 169 | |
| 170 | debug("%s(reset_ctl=%p)\n", __func__, reset_ctl); |
| 171 | |
Marek Vasut | f817d17 | 2022-04-26 23:43:30 +0200 | [diff] [blame] | 172 | return ops->request ? ops->request(reset_ctl) : 0; |
Patrice Chotard | 76290e3 | 2017-07-18 11:57:05 +0200 | [diff] [blame] | 173 | } |
| 174 | |
Stephen Warren | 185ad87 | 2016-06-17 09:43:58 -0600 | [diff] [blame] | 175 | int reset_free(struct reset_ctl *reset_ctl) |
| 176 | { |
| 177 | struct reset_ops *ops = reset_dev_ops(reset_ctl->dev); |
| 178 | |
| 179 | debug("%s(reset_ctl=%p)\n", __func__, reset_ctl); |
| 180 | |
Marek Vasut | f817d17 | 2022-04-26 23:43:30 +0200 | [diff] [blame] | 181 | return ops->rfree ? ops->rfree(reset_ctl) : 0; |
Stephen Warren | 185ad87 | 2016-06-17 09:43:58 -0600 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | int reset_assert(struct reset_ctl *reset_ctl) |
| 185 | { |
| 186 | struct reset_ops *ops = reset_dev_ops(reset_ctl->dev); |
| 187 | |
| 188 | debug("%s(reset_ctl=%p)\n", __func__, reset_ctl); |
| 189 | |
Marek Vasut | f817d17 | 2022-04-26 23:43:30 +0200 | [diff] [blame] | 190 | return ops->rst_assert ? ops->rst_assert(reset_ctl) : 0; |
Stephen Warren | 185ad87 | 2016-06-17 09:43:58 -0600 | [diff] [blame] | 191 | } |
| 192 | |
Neil Armstrong | dbb26d3 | 2018-04-03 11:40:50 +0200 | [diff] [blame] | 193 | int reset_assert_bulk(struct reset_ctl_bulk *bulk) |
| 194 | { |
| 195 | int i, ret; |
| 196 | |
| 197 | for (i = 0; i < bulk->count; i++) { |
| 198 | ret = reset_assert(&bulk->resets[i]); |
| 199 | if (ret < 0) |
| 200 | return ret; |
| 201 | } |
| 202 | |
| 203 | return 0; |
| 204 | } |
| 205 | |
Stephen Warren | 185ad87 | 2016-06-17 09:43:58 -0600 | [diff] [blame] | 206 | int reset_deassert(struct reset_ctl *reset_ctl) |
| 207 | { |
| 208 | struct reset_ops *ops = reset_dev_ops(reset_ctl->dev); |
| 209 | |
| 210 | debug("%s(reset_ctl=%p)\n", __func__, reset_ctl); |
| 211 | |
Marek Vasut | f817d17 | 2022-04-26 23:43:30 +0200 | [diff] [blame] | 212 | return ops->rst_deassert ? ops->rst_deassert(reset_ctl) : 0; |
Stephen Warren | 185ad87 | 2016-06-17 09:43:58 -0600 | [diff] [blame] | 213 | } |
| 214 | |
Neil Armstrong | dbb26d3 | 2018-04-03 11:40:50 +0200 | [diff] [blame] | 215 | int reset_deassert_bulk(struct reset_ctl_bulk *bulk) |
| 216 | { |
| 217 | int i, ret; |
| 218 | |
| 219 | for (i = 0; i < bulk->count; i++) { |
| 220 | ret = reset_deassert(&bulk->resets[i]); |
| 221 | if (ret < 0) |
| 222 | return ret; |
| 223 | } |
| 224 | |
| 225 | return 0; |
| 226 | } |
| 227 | |
Andreas Dannenberg | e2bc740 | 2018-08-27 15:57:39 +0530 | [diff] [blame] | 228 | int reset_status(struct reset_ctl *reset_ctl) |
| 229 | { |
| 230 | struct reset_ops *ops = reset_dev_ops(reset_ctl->dev); |
| 231 | |
| 232 | debug("%s(reset_ctl=%p)\n", __func__, reset_ctl); |
| 233 | |
Marek Vasut | f817d17 | 2022-04-26 23:43:30 +0200 | [diff] [blame] | 234 | return ops->rst_status ? ops->rst_status(reset_ctl) : 0; |
Andreas Dannenberg | e2bc740 | 2018-08-27 15:57:39 +0530 | [diff] [blame] | 235 | } |
| 236 | |
Patrice Chotard | e4d368e | 2017-07-18 11:57:06 +0200 | [diff] [blame] | 237 | int reset_release_all(struct reset_ctl *reset_ctl, int count) |
| 238 | { |
| 239 | int i, ret; |
| 240 | |
| 241 | for (i = 0; i < count; i++) { |
| 242 | debug("%s(reset_ctl[%d]=%p)\n", __func__, i, &reset_ctl[i]); |
| 243 | |
| 244 | /* check if reset has been previously requested */ |
| 245 | if (!reset_ctl[i].dev) |
| 246 | continue; |
| 247 | |
| 248 | ret = reset_assert(&reset_ctl[i]); |
| 249 | if (ret) |
| 250 | return ret; |
| 251 | |
| 252 | ret = reset_free(&reset_ctl[i]); |
| 253 | if (ret) |
| 254 | return ret; |
| 255 | } |
| 256 | |
| 257 | return 0; |
| 258 | } |
| 259 | |
Jean-Jacques Hiblot | 5be97a3 | 2020-09-09 15:37:03 +0530 | [diff] [blame] | 260 | static void devm_reset_release(struct udevice *dev, void *res) |
| 261 | { |
| 262 | reset_free(res); |
| 263 | } |
| 264 | |
| 265 | struct reset_ctl *devm_reset_control_get_by_index(struct udevice *dev, |
| 266 | int index) |
| 267 | { |
| 268 | int rc; |
| 269 | struct reset_ctl *reset_ctl; |
| 270 | |
| 271 | reset_ctl = devres_alloc(devm_reset_release, sizeof(struct reset_ctl), |
| 272 | __GFP_ZERO); |
| 273 | if (unlikely(!reset_ctl)) |
| 274 | return ERR_PTR(-ENOMEM); |
| 275 | |
| 276 | rc = reset_get_by_index(dev, index, reset_ctl); |
| 277 | if (rc) |
| 278 | return ERR_PTR(rc); |
| 279 | |
| 280 | devres_add(dev, reset_ctl); |
| 281 | return reset_ctl; |
| 282 | } |
| 283 | |
| 284 | struct reset_ctl *devm_reset_control_get(struct udevice *dev, const char *id) |
| 285 | { |
| 286 | int rc; |
| 287 | struct reset_ctl *reset_ctl; |
| 288 | |
| 289 | reset_ctl = devres_alloc(devm_reset_release, sizeof(struct reset_ctl), |
| 290 | __GFP_ZERO); |
| 291 | if (unlikely(!reset_ctl)) |
| 292 | return ERR_PTR(-ENOMEM); |
| 293 | |
| 294 | rc = reset_get_by_name(dev, id, reset_ctl); |
| 295 | if (rc) |
| 296 | return ERR_PTR(rc); |
| 297 | |
| 298 | devres_add(dev, reset_ctl); |
| 299 | return reset_ctl; |
| 300 | } |
| 301 | |
| 302 | struct reset_ctl *devm_reset_control_get_optional(struct udevice *dev, |
| 303 | const char *id) |
| 304 | { |
| 305 | struct reset_ctl *r = devm_reset_control_get(dev, id); |
| 306 | |
| 307 | if (IS_ERR(r)) |
| 308 | return NULL; |
| 309 | |
| 310 | return r; |
| 311 | } |
| 312 | |
| 313 | static void devm_reset_bulk_release(struct udevice *dev, void *res) |
| 314 | { |
| 315 | struct reset_ctl_bulk *bulk = res; |
| 316 | |
| 317 | reset_release_all(bulk->resets, bulk->count); |
| 318 | } |
| 319 | |
| 320 | struct reset_ctl_bulk *devm_reset_bulk_get_by_node(struct udevice *dev, |
| 321 | ofnode node) |
| 322 | { |
| 323 | int rc; |
| 324 | struct reset_ctl_bulk *bulk; |
| 325 | |
| 326 | bulk = devres_alloc(devm_reset_bulk_release, |
| 327 | sizeof(struct reset_ctl_bulk), |
| 328 | __GFP_ZERO); |
Simon Glass | 956cd30 | 2021-05-13 19:39:21 -0600 | [diff] [blame] | 329 | |
| 330 | /* this looks like a leak, but devres takes care of it */ |
Jean-Jacques Hiblot | 5be97a3 | 2020-09-09 15:37:03 +0530 | [diff] [blame] | 331 | if (unlikely(!bulk)) |
| 332 | return ERR_PTR(-ENOMEM); |
| 333 | |
| 334 | rc = __reset_get_bulk(dev, node, bulk); |
| 335 | if (rc) |
| 336 | return ERR_PTR(rc); |
| 337 | |
| 338 | devres_add(dev, bulk); |
| 339 | return bulk; |
| 340 | } |
| 341 | |
| 342 | struct reset_ctl_bulk *devm_reset_bulk_get_optional_by_node(struct udevice *dev, |
| 343 | ofnode node) |
| 344 | { |
| 345 | struct reset_ctl_bulk *bulk; |
| 346 | |
| 347 | bulk = devm_reset_bulk_get_by_node(dev, node); |
| 348 | |
| 349 | if (IS_ERR(bulk)) |
| 350 | return NULL; |
| 351 | |
| 352 | return bulk; |
| 353 | } |
| 354 | |
| 355 | struct reset_ctl_bulk *devm_reset_bulk_get(struct udevice *dev) |
| 356 | { |
| 357 | return devm_reset_bulk_get_by_node(dev, dev_ofnode(dev)); |
| 358 | } |
| 359 | |
| 360 | struct reset_ctl_bulk *devm_reset_bulk_get_optional(struct udevice *dev) |
| 361 | { |
| 362 | return devm_reset_bulk_get_optional_by_node(dev, dev_ofnode(dev)); |
| 363 | } |
| 364 | |
Stephen Warren | 185ad87 | 2016-06-17 09:43:58 -0600 | [diff] [blame] | 365 | UCLASS_DRIVER(reset) = { |
| 366 | .id = UCLASS_RESET, |
| 367 | .name = "reset", |
| 368 | }; |