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