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 | |
| 6 | #include <common.h> |
| 7 | #include <dm.h> |
| 8 | #include <fdtdec.h> |
| 9 | #include <reset.h> |
| 10 | #include <reset-uclass.h> |
| 11 | |
Stephen Warren | 89c1e2d | 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 | 40a475e | 2017-05-18 20:09:50 -0600 | [diff] [blame] | 18 | struct ofnode_phandle_args *args) |
Stephen Warren | 89c1e2d | 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 | |
| 32 | int reset_get_by_index(struct udevice *dev, int index, |
| 33 | struct reset_ctl *reset_ctl) |
| 34 | { |
Simon Glass | 40a475e | 2017-05-18 20:09:50 -0600 | [diff] [blame] | 35 | struct ofnode_phandle_args args; |
Stephen Warren | 89c1e2d | 2016-06-17 09:43:58 -0600 | [diff] [blame] | 36 | int ret; |
| 37 | struct udevice *dev_reset; |
| 38 | struct reset_ops *ops; |
| 39 | |
| 40 | debug("%s(dev=%p, index=%d, reset_ctl=%p)\n", __func__, dev, index, |
| 41 | reset_ctl); |
Patrice Chotard | 3b9d1bd | 2017-07-18 11:57:06 +0200 | [diff] [blame] | 42 | reset_ctl->dev = NULL; |
Stephen Warren | 89c1e2d | 2016-06-17 09:43:58 -0600 | [diff] [blame] | 43 | |
Simon Glass | 40a475e | 2017-05-18 20:09:50 -0600 | [diff] [blame] | 44 | ret = dev_read_phandle_with_args(dev, "resets", "#reset-cells", 0, |
| 45 | index, &args); |
Stephen Warren | 89c1e2d | 2016-06-17 09:43:58 -0600 | [diff] [blame] | 46 | if (ret) { |
Simon Glass | 40a475e | 2017-05-18 20:09:50 -0600 | [diff] [blame] | 47 | debug("%s: fdtdec_parse_phandle_with_args() failed: %d\n", |
Stephen Warren | 89c1e2d | 2016-06-17 09:43:58 -0600 | [diff] [blame] | 48 | __func__, ret); |
| 49 | return ret; |
| 50 | } |
| 51 | |
Simon Glass | 40a475e | 2017-05-18 20:09:50 -0600 | [diff] [blame] | 52 | ret = uclass_get_device_by_ofnode(UCLASS_RESET, args.node, |
| 53 | &dev_reset); |
Stephen Warren | 89c1e2d | 2016-06-17 09:43:58 -0600 | [diff] [blame] | 54 | if (ret) { |
Simon Glass | 40a475e | 2017-05-18 20:09:50 -0600 | [diff] [blame] | 55 | debug("%s: uclass_get_device_by_ofnode() failed: %d\n", |
Stephen Warren | 89c1e2d | 2016-06-17 09:43:58 -0600 | [diff] [blame] | 56 | __func__, ret); |
Simon Glass | 40a475e | 2017-05-18 20:09:50 -0600 | [diff] [blame] | 57 | debug("%s %d\n", ofnode_get_name(args.node), args.args[0]); |
Stephen Warren | 89c1e2d | 2016-06-17 09:43:58 -0600 | [diff] [blame] | 58 | return ret; |
| 59 | } |
| 60 | ops = reset_dev_ops(dev_reset); |
| 61 | |
| 62 | reset_ctl->dev = dev_reset; |
| 63 | if (ops->of_xlate) |
| 64 | ret = ops->of_xlate(reset_ctl, &args); |
| 65 | else |
| 66 | ret = reset_of_xlate_default(reset_ctl, &args); |
| 67 | if (ret) { |
| 68 | debug("of_xlate() failed: %d\n", ret); |
| 69 | return ret; |
| 70 | } |
| 71 | |
| 72 | ret = ops->request(reset_ctl); |
| 73 | if (ret) { |
| 74 | debug("ops->request() failed: %d\n", ret); |
| 75 | return ret; |
| 76 | } |
| 77 | |
| 78 | return 0; |
| 79 | } |
| 80 | |
Neil Armstrong | 0c28233 | 2018-04-03 11:40:50 +0200 | [diff] [blame] | 81 | int reset_get_bulk(struct udevice *dev, struct reset_ctl_bulk *bulk) |
| 82 | { |
| 83 | int i, ret, err, count; |
| 84 | |
| 85 | bulk->count = 0; |
| 86 | |
| 87 | count = dev_count_phandle_with_args(dev, "resets", "#reset-cells"); |
Neil Armstrong | 895a82c | 2018-04-17 11:30:22 +0200 | [diff] [blame] | 88 | if (count < 1) |
| 89 | return count; |
Neil Armstrong | 0c28233 | 2018-04-03 11:40:50 +0200 | [diff] [blame] | 90 | |
| 91 | bulk->resets = devm_kcalloc(dev, count, sizeof(struct reset_ctl), |
| 92 | GFP_KERNEL); |
| 93 | if (!bulk->resets) |
| 94 | return -ENOMEM; |
| 95 | |
| 96 | for (i = 0; i < count; i++) { |
| 97 | ret = reset_get_by_index(dev, i, &bulk->resets[i]); |
| 98 | if (ret < 0) |
| 99 | goto bulk_get_err; |
| 100 | |
| 101 | ++bulk->count; |
| 102 | } |
| 103 | |
| 104 | return 0; |
| 105 | |
| 106 | bulk_get_err: |
| 107 | err = reset_release_all(bulk->resets, bulk->count); |
| 108 | if (err) |
| 109 | debug("%s: could release all resets for %p\n", |
| 110 | __func__, dev); |
| 111 | |
| 112 | return ret; |
| 113 | } |
| 114 | |
Stephen Warren | 89c1e2d | 2016-06-17 09:43:58 -0600 | [diff] [blame] | 115 | int reset_get_by_name(struct udevice *dev, const char *name, |
| 116 | struct reset_ctl *reset_ctl) |
| 117 | { |
| 118 | int index; |
| 119 | |
| 120 | debug("%s(dev=%p, name=%s, reset_ctl=%p)\n", __func__, dev, name, |
| 121 | reset_ctl); |
Patrice Chotard | 3b9d1bd | 2017-07-18 11:57:06 +0200 | [diff] [blame] | 122 | reset_ctl->dev = NULL; |
Stephen Warren | 89c1e2d | 2016-06-17 09:43:58 -0600 | [diff] [blame] | 123 | |
Simon Glass | 40a475e | 2017-05-18 20:09:50 -0600 | [diff] [blame] | 124 | index = dev_read_stringlist_search(dev, "reset-names", name); |
Stephen Warren | 89c1e2d | 2016-06-17 09:43:58 -0600 | [diff] [blame] | 125 | if (index < 0) { |
Simon Glass | b02e404 | 2016-10-02 17:59:28 -0600 | [diff] [blame] | 126 | debug("fdt_stringlist_search() failed: %d\n", index); |
Stephen Warren | 89c1e2d | 2016-06-17 09:43:58 -0600 | [diff] [blame] | 127 | return index; |
| 128 | } |
| 129 | |
| 130 | return reset_get_by_index(dev, index, reset_ctl); |
| 131 | } |
| 132 | |
Patrice Chotard | 9bd5cdf | 2017-07-18 11:57:05 +0200 | [diff] [blame] | 133 | int reset_request(struct reset_ctl *reset_ctl) |
| 134 | { |
| 135 | struct reset_ops *ops = reset_dev_ops(reset_ctl->dev); |
| 136 | |
| 137 | debug("%s(reset_ctl=%p)\n", __func__, reset_ctl); |
| 138 | |
| 139 | return ops->request(reset_ctl); |
| 140 | } |
| 141 | |
Stephen Warren | 89c1e2d | 2016-06-17 09:43:58 -0600 | [diff] [blame] | 142 | int reset_free(struct reset_ctl *reset_ctl) |
| 143 | { |
| 144 | struct reset_ops *ops = reset_dev_ops(reset_ctl->dev); |
| 145 | |
| 146 | debug("%s(reset_ctl=%p)\n", __func__, reset_ctl); |
| 147 | |
| 148 | return ops->free(reset_ctl); |
| 149 | } |
| 150 | |
| 151 | int reset_assert(struct reset_ctl *reset_ctl) |
| 152 | { |
| 153 | struct reset_ops *ops = reset_dev_ops(reset_ctl->dev); |
| 154 | |
| 155 | debug("%s(reset_ctl=%p)\n", __func__, reset_ctl); |
| 156 | |
| 157 | return ops->rst_assert(reset_ctl); |
| 158 | } |
| 159 | |
Neil Armstrong | 0c28233 | 2018-04-03 11:40:50 +0200 | [diff] [blame] | 160 | int reset_assert_bulk(struct reset_ctl_bulk *bulk) |
| 161 | { |
| 162 | int i, ret; |
| 163 | |
| 164 | for (i = 0; i < bulk->count; i++) { |
| 165 | ret = reset_assert(&bulk->resets[i]); |
| 166 | if (ret < 0) |
| 167 | return ret; |
| 168 | } |
| 169 | |
| 170 | return 0; |
| 171 | } |
| 172 | |
Stephen Warren | 89c1e2d | 2016-06-17 09:43:58 -0600 | [diff] [blame] | 173 | int reset_deassert(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_deassert(reset_ctl); |
| 180 | } |
| 181 | |
Neil Armstrong | 0c28233 | 2018-04-03 11:40:50 +0200 | [diff] [blame] | 182 | int reset_deassert_bulk(struct reset_ctl_bulk *bulk) |
| 183 | { |
| 184 | int i, ret; |
| 185 | |
| 186 | for (i = 0; i < bulk->count; i++) { |
| 187 | ret = reset_deassert(&bulk->resets[i]); |
| 188 | if (ret < 0) |
| 189 | return ret; |
| 190 | } |
| 191 | |
| 192 | return 0; |
| 193 | } |
| 194 | |
Patrice Chotard | 3b9d1bd | 2017-07-18 11:57:06 +0200 | [diff] [blame] | 195 | int reset_release_all(struct reset_ctl *reset_ctl, int count) |
| 196 | { |
| 197 | int i, ret; |
| 198 | |
| 199 | for (i = 0; i < count; i++) { |
| 200 | debug("%s(reset_ctl[%d]=%p)\n", __func__, i, &reset_ctl[i]); |
| 201 | |
| 202 | /* check if reset has been previously requested */ |
| 203 | if (!reset_ctl[i].dev) |
| 204 | continue; |
| 205 | |
| 206 | ret = reset_assert(&reset_ctl[i]); |
| 207 | if (ret) |
| 208 | return ret; |
| 209 | |
| 210 | ret = reset_free(&reset_ctl[i]); |
| 211 | if (ret) |
| 212 | return ret; |
| 213 | } |
| 214 | |
| 215 | return 0; |
| 216 | } |
| 217 | |
Stephen Warren | 89c1e2d | 2016-06-17 09:43:58 -0600 | [diff] [blame] | 218 | UCLASS_DRIVER(reset) = { |
| 219 | .id = UCLASS_RESET, |
| 220 | .name = "reset", |
| 221 | }; |