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