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