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 | |
Jean-Jacques Hiblot | 5be97a3 | 2020-09-09 15:37:03 +0530 | [diff] [blame^] | 111 | count = ofnode_count_phandle_with_args(node, "resets", "#reset-cells"); |
Neil Armstrong | 6acc49a | 2018-04-17 11:30:22 +0200 | [diff] [blame] | 112 | if (count < 1) |
| 113 | return count; |
Neil Armstrong | dbb26d3 | 2018-04-03 11:40:50 +0200 | [diff] [blame] | 114 | |
| 115 | bulk->resets = devm_kcalloc(dev, count, sizeof(struct reset_ctl), |
| 116 | GFP_KERNEL); |
| 117 | if (!bulk->resets) |
| 118 | return -ENOMEM; |
| 119 | |
| 120 | for (i = 0; i < count; i++) { |
Jean-Jacques Hiblot | 5be97a3 | 2020-09-09 15:37:03 +0530 | [diff] [blame^] | 121 | ret = reset_get_by_index_nodev(node, i, &bulk->resets[i]); |
Neil Armstrong | dbb26d3 | 2018-04-03 11:40:50 +0200 | [diff] [blame] | 122 | if (ret < 0) |
| 123 | goto bulk_get_err; |
| 124 | |
| 125 | ++bulk->count; |
| 126 | } |
| 127 | |
| 128 | return 0; |
| 129 | |
| 130 | bulk_get_err: |
| 131 | err = reset_release_all(bulk->resets, bulk->count); |
| 132 | if (err) |
| 133 | debug("%s: could release all resets for %p\n", |
| 134 | __func__, dev); |
| 135 | |
| 136 | return ret; |
| 137 | } |
| 138 | |
Jean-Jacques Hiblot | 5be97a3 | 2020-09-09 15:37:03 +0530 | [diff] [blame^] | 139 | int reset_get_bulk(struct udevice *dev, struct reset_ctl_bulk *bulk) |
| 140 | { |
| 141 | return __reset_get_bulk(dev, dev_ofnode(dev), bulk); |
| 142 | } |
| 143 | |
Stephen Warren | 185ad87 | 2016-06-17 09:43:58 -0600 | [diff] [blame] | 144 | int reset_get_by_name(struct udevice *dev, const char *name, |
| 145 | struct reset_ctl *reset_ctl) |
| 146 | { |
| 147 | int index; |
| 148 | |
| 149 | debug("%s(dev=%p, name=%s, reset_ctl=%p)\n", __func__, dev, name, |
| 150 | reset_ctl); |
Patrice Chotard | e4d368e | 2017-07-18 11:57:06 +0200 | [diff] [blame] | 151 | reset_ctl->dev = NULL; |
Stephen Warren | 185ad87 | 2016-06-17 09:43:58 -0600 | [diff] [blame] | 152 | |
Simon Glass | aec8cc0 | 2017-05-18 20:09:50 -0600 | [diff] [blame] | 153 | index = dev_read_stringlist_search(dev, "reset-names", name); |
Stephen Warren | 185ad87 | 2016-06-17 09:43:58 -0600 | [diff] [blame] | 154 | if (index < 0) { |
Simon Glass | b0ea740 | 2016-10-02 17:59:28 -0600 | [diff] [blame] | 155 | debug("fdt_stringlist_search() failed: %d\n", index); |
Stephen Warren | 185ad87 | 2016-06-17 09:43:58 -0600 | [diff] [blame] | 156 | return index; |
| 157 | } |
| 158 | |
| 159 | return reset_get_by_index(dev, index, reset_ctl); |
| 160 | } |
| 161 | |
Patrice Chotard | 76290e3 | 2017-07-18 11:57:05 +0200 | [diff] [blame] | 162 | int reset_request(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 | |
| 168 | return ops->request(reset_ctl); |
| 169 | } |
| 170 | |
Stephen Warren | 185ad87 | 2016-06-17 09:43:58 -0600 | [diff] [blame] | 171 | int reset_free(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 | |
Simon Glass | 1928cd4 | 2020-02-03 07:35:52 -0700 | [diff] [blame] | 177 | return ops->rfree(reset_ctl); |
Stephen Warren | 185ad87 | 2016-06-17 09:43:58 -0600 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | int reset_assert(struct reset_ctl *reset_ctl) |
| 181 | { |
| 182 | struct reset_ops *ops = reset_dev_ops(reset_ctl->dev); |
| 183 | |
| 184 | debug("%s(reset_ctl=%p)\n", __func__, reset_ctl); |
| 185 | |
| 186 | return ops->rst_assert(reset_ctl); |
| 187 | } |
| 188 | |
Neil Armstrong | dbb26d3 | 2018-04-03 11:40:50 +0200 | [diff] [blame] | 189 | int reset_assert_bulk(struct reset_ctl_bulk *bulk) |
| 190 | { |
| 191 | int i, ret; |
| 192 | |
| 193 | for (i = 0; i < bulk->count; i++) { |
| 194 | ret = reset_assert(&bulk->resets[i]); |
| 195 | if (ret < 0) |
| 196 | return ret; |
| 197 | } |
| 198 | |
| 199 | return 0; |
| 200 | } |
| 201 | |
Stephen Warren | 185ad87 | 2016-06-17 09:43:58 -0600 | [diff] [blame] | 202 | int reset_deassert(struct reset_ctl *reset_ctl) |
| 203 | { |
| 204 | struct reset_ops *ops = reset_dev_ops(reset_ctl->dev); |
| 205 | |
| 206 | debug("%s(reset_ctl=%p)\n", __func__, reset_ctl); |
| 207 | |
| 208 | return ops->rst_deassert(reset_ctl); |
| 209 | } |
| 210 | |
Neil Armstrong | dbb26d3 | 2018-04-03 11:40:50 +0200 | [diff] [blame] | 211 | int reset_deassert_bulk(struct reset_ctl_bulk *bulk) |
| 212 | { |
| 213 | int i, ret; |
| 214 | |
| 215 | for (i = 0; i < bulk->count; i++) { |
| 216 | ret = reset_deassert(&bulk->resets[i]); |
| 217 | if (ret < 0) |
| 218 | return ret; |
| 219 | } |
| 220 | |
| 221 | return 0; |
| 222 | } |
| 223 | |
Andreas Dannenberg | e2bc740 | 2018-08-27 15:57:39 +0530 | [diff] [blame] | 224 | int reset_status(struct reset_ctl *reset_ctl) |
| 225 | { |
| 226 | struct reset_ops *ops = reset_dev_ops(reset_ctl->dev); |
| 227 | |
| 228 | debug("%s(reset_ctl=%p)\n", __func__, reset_ctl); |
| 229 | |
| 230 | return ops->rst_status(reset_ctl); |
| 231 | } |
| 232 | |
Patrice Chotard | e4d368e | 2017-07-18 11:57:06 +0200 | [diff] [blame] | 233 | int reset_release_all(struct reset_ctl *reset_ctl, int count) |
| 234 | { |
| 235 | int i, ret; |
| 236 | |
| 237 | for (i = 0; i < count; i++) { |
| 238 | debug("%s(reset_ctl[%d]=%p)\n", __func__, i, &reset_ctl[i]); |
| 239 | |
| 240 | /* check if reset has been previously requested */ |
| 241 | if (!reset_ctl[i].dev) |
| 242 | continue; |
| 243 | |
| 244 | ret = reset_assert(&reset_ctl[i]); |
| 245 | if (ret) |
| 246 | return ret; |
| 247 | |
| 248 | ret = reset_free(&reset_ctl[i]); |
| 249 | if (ret) |
| 250 | return ret; |
| 251 | } |
| 252 | |
| 253 | return 0; |
| 254 | } |
| 255 | |
Jean-Jacques Hiblot | 5be97a3 | 2020-09-09 15:37:03 +0530 | [diff] [blame^] | 256 | static void devm_reset_release(struct udevice *dev, void *res) |
| 257 | { |
| 258 | reset_free(res); |
| 259 | } |
| 260 | |
| 261 | struct reset_ctl *devm_reset_control_get_by_index(struct udevice *dev, |
| 262 | int index) |
| 263 | { |
| 264 | int rc; |
| 265 | struct reset_ctl *reset_ctl; |
| 266 | |
| 267 | reset_ctl = devres_alloc(devm_reset_release, sizeof(struct reset_ctl), |
| 268 | __GFP_ZERO); |
| 269 | if (unlikely(!reset_ctl)) |
| 270 | return ERR_PTR(-ENOMEM); |
| 271 | |
| 272 | rc = reset_get_by_index(dev, index, reset_ctl); |
| 273 | if (rc) |
| 274 | return ERR_PTR(rc); |
| 275 | |
| 276 | devres_add(dev, reset_ctl); |
| 277 | return reset_ctl; |
| 278 | } |
| 279 | |
| 280 | struct reset_ctl *devm_reset_control_get(struct udevice *dev, const char *id) |
| 281 | { |
| 282 | int rc; |
| 283 | struct reset_ctl *reset_ctl; |
| 284 | |
| 285 | reset_ctl = devres_alloc(devm_reset_release, sizeof(struct reset_ctl), |
| 286 | __GFP_ZERO); |
| 287 | if (unlikely(!reset_ctl)) |
| 288 | return ERR_PTR(-ENOMEM); |
| 289 | |
| 290 | rc = reset_get_by_name(dev, id, reset_ctl); |
| 291 | if (rc) |
| 292 | return ERR_PTR(rc); |
| 293 | |
| 294 | devres_add(dev, reset_ctl); |
| 295 | return reset_ctl; |
| 296 | } |
| 297 | |
| 298 | struct reset_ctl *devm_reset_control_get_optional(struct udevice *dev, |
| 299 | const char *id) |
| 300 | { |
| 301 | struct reset_ctl *r = devm_reset_control_get(dev, id); |
| 302 | |
| 303 | if (IS_ERR(r)) |
| 304 | return NULL; |
| 305 | |
| 306 | return r; |
| 307 | } |
| 308 | |
| 309 | static void devm_reset_bulk_release(struct udevice *dev, void *res) |
| 310 | { |
| 311 | struct reset_ctl_bulk *bulk = res; |
| 312 | |
| 313 | reset_release_all(bulk->resets, bulk->count); |
| 314 | } |
| 315 | |
| 316 | struct reset_ctl_bulk *devm_reset_bulk_get_by_node(struct udevice *dev, |
| 317 | ofnode node) |
| 318 | { |
| 319 | int rc; |
| 320 | struct reset_ctl_bulk *bulk; |
| 321 | |
| 322 | bulk = devres_alloc(devm_reset_bulk_release, |
| 323 | sizeof(struct reset_ctl_bulk), |
| 324 | __GFP_ZERO); |
| 325 | if (unlikely(!bulk)) |
| 326 | return ERR_PTR(-ENOMEM); |
| 327 | |
| 328 | rc = __reset_get_bulk(dev, node, bulk); |
| 329 | if (rc) |
| 330 | return ERR_PTR(rc); |
| 331 | |
| 332 | devres_add(dev, bulk); |
| 333 | return bulk; |
| 334 | } |
| 335 | |
| 336 | struct reset_ctl_bulk *devm_reset_bulk_get_optional_by_node(struct udevice *dev, |
| 337 | ofnode node) |
| 338 | { |
| 339 | struct reset_ctl_bulk *bulk; |
| 340 | |
| 341 | bulk = devm_reset_bulk_get_by_node(dev, node); |
| 342 | |
| 343 | if (IS_ERR(bulk)) |
| 344 | return NULL; |
| 345 | |
| 346 | return bulk; |
| 347 | } |
| 348 | |
| 349 | struct reset_ctl_bulk *devm_reset_bulk_get(struct udevice *dev) |
| 350 | { |
| 351 | return devm_reset_bulk_get_by_node(dev, dev_ofnode(dev)); |
| 352 | } |
| 353 | |
| 354 | struct reset_ctl_bulk *devm_reset_bulk_get_optional(struct udevice *dev) |
| 355 | { |
| 356 | return devm_reset_bulk_get_optional_by_node(dev, dev_ofnode(dev)); |
| 357 | } |
| 358 | |
Stephen Warren | 185ad87 | 2016-06-17 09:43:58 -0600 | [diff] [blame] | 359 | UCLASS_DRIVER(reset) = { |
| 360 | .id = UCLASS_RESET, |
| 361 | .name = "reset", |
| 362 | }; |