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