Stephen Warren | 185ad87 | 2016-06-17 09:43:58 -0600 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2016, NVIDIA CORPORATION. |
| 3 | * |
| 4 | * SPDX-License-Identifier: GPL-2.0 |
| 5 | */ |
| 6 | |
| 7 | #include <common.h> |
| 8 | #include <dm.h> |
| 9 | #include <fdtdec.h> |
| 10 | #include <reset.h> |
| 11 | #include <reset-uclass.h> |
| 12 | |
| 13 | DECLARE_GLOBAL_DATA_PTR; |
| 14 | |
| 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 | |
| 35 | int reset_get_by_index(struct udevice *dev, int index, |
| 36 | struct reset_ctl *reset_ctl) |
| 37 | { |
Simon Glass | aec8cc0 | 2017-05-18 20:09:50 -0600 | [diff] [blame] | 38 | struct ofnode_phandle_args args; |
Stephen Warren | 185ad87 | 2016-06-17 09:43:58 -0600 | [diff] [blame] | 39 | int ret; |
| 40 | struct udevice *dev_reset; |
| 41 | struct reset_ops *ops; |
| 42 | |
| 43 | debug("%s(dev=%p, index=%d, reset_ctl=%p)\n", __func__, dev, index, |
| 44 | reset_ctl); |
| 45 | |
Simon Glass | aec8cc0 | 2017-05-18 20:09:50 -0600 | [diff] [blame] | 46 | ret = dev_read_phandle_with_args(dev, "resets", "#reset-cells", 0, |
| 47 | index, &args); |
Stephen Warren | 185ad87 | 2016-06-17 09:43:58 -0600 | [diff] [blame] | 48 | if (ret) { |
Simon Glass | aec8cc0 | 2017-05-18 20:09:50 -0600 | [diff] [blame] | 49 | debug("%s: fdtdec_parse_phandle_with_args() failed: %d\n", |
Stephen Warren | 185ad87 | 2016-06-17 09:43:58 -0600 | [diff] [blame] | 50 | __func__, ret); |
| 51 | return ret; |
| 52 | } |
| 53 | |
Simon Glass | aec8cc0 | 2017-05-18 20:09:50 -0600 | [diff] [blame] | 54 | ret = uclass_get_device_by_ofnode(UCLASS_RESET, args.node, |
| 55 | &dev_reset); |
Stephen Warren | 185ad87 | 2016-06-17 09:43:58 -0600 | [diff] [blame] | 56 | if (ret) { |
Simon Glass | aec8cc0 | 2017-05-18 20:09:50 -0600 | [diff] [blame] | 57 | debug("%s: uclass_get_device_by_ofnode() failed: %d\n", |
Stephen Warren | 185ad87 | 2016-06-17 09:43:58 -0600 | [diff] [blame] | 58 | __func__, ret); |
Simon Glass | aec8cc0 | 2017-05-18 20:09:50 -0600 | [diff] [blame] | 59 | debug("%s %d\n", ofnode_get_name(args.node), args.args[0]); |
Stephen Warren | 185ad87 | 2016-06-17 09:43:58 -0600 | [diff] [blame] | 60 | return ret; |
| 61 | } |
| 62 | ops = reset_dev_ops(dev_reset); |
| 63 | |
| 64 | reset_ctl->dev = dev_reset; |
| 65 | if (ops->of_xlate) |
| 66 | ret = ops->of_xlate(reset_ctl, &args); |
| 67 | else |
| 68 | ret = reset_of_xlate_default(reset_ctl, &args); |
| 69 | if (ret) { |
| 70 | debug("of_xlate() failed: %d\n", ret); |
| 71 | return ret; |
| 72 | } |
| 73 | |
| 74 | ret = ops->request(reset_ctl); |
| 75 | if (ret) { |
| 76 | debug("ops->request() failed: %d\n", ret); |
| 77 | return ret; |
| 78 | } |
| 79 | |
| 80 | return 0; |
| 81 | } |
| 82 | |
| 83 | int reset_get_by_name(struct udevice *dev, const char *name, |
| 84 | struct reset_ctl *reset_ctl) |
| 85 | { |
| 86 | int index; |
| 87 | |
| 88 | debug("%s(dev=%p, name=%s, reset_ctl=%p)\n", __func__, dev, name, |
| 89 | reset_ctl); |
| 90 | |
Simon Glass | aec8cc0 | 2017-05-18 20:09:50 -0600 | [diff] [blame] | 91 | index = dev_read_stringlist_search(dev, "reset-names", name); |
Stephen Warren | 185ad87 | 2016-06-17 09:43:58 -0600 | [diff] [blame] | 92 | if (index < 0) { |
Simon Glass | b0ea740 | 2016-10-02 17:59:28 -0600 | [diff] [blame] | 93 | debug("fdt_stringlist_search() failed: %d\n", index); |
Stephen Warren | 185ad87 | 2016-06-17 09:43:58 -0600 | [diff] [blame] | 94 | return index; |
| 95 | } |
| 96 | |
| 97 | return reset_get_by_index(dev, index, reset_ctl); |
| 98 | } |
| 99 | |
Patrice Chotard | 76290e3 | 2017-07-18 11:57:05 +0200 | [diff] [blame^] | 100 | int reset_request(struct reset_ctl *reset_ctl) |
| 101 | { |
| 102 | struct reset_ops *ops = reset_dev_ops(reset_ctl->dev); |
| 103 | |
| 104 | debug("%s(reset_ctl=%p)\n", __func__, reset_ctl); |
| 105 | |
| 106 | return ops->request(reset_ctl); |
| 107 | } |
| 108 | |
Stephen Warren | 185ad87 | 2016-06-17 09:43:58 -0600 | [diff] [blame] | 109 | int reset_free(struct reset_ctl *reset_ctl) |
| 110 | { |
| 111 | struct reset_ops *ops = reset_dev_ops(reset_ctl->dev); |
| 112 | |
| 113 | debug("%s(reset_ctl=%p)\n", __func__, reset_ctl); |
| 114 | |
| 115 | return ops->free(reset_ctl); |
| 116 | } |
| 117 | |
| 118 | int reset_assert(struct reset_ctl *reset_ctl) |
| 119 | { |
| 120 | struct reset_ops *ops = reset_dev_ops(reset_ctl->dev); |
| 121 | |
| 122 | debug("%s(reset_ctl=%p)\n", __func__, reset_ctl); |
| 123 | |
| 124 | return ops->rst_assert(reset_ctl); |
| 125 | } |
| 126 | |
| 127 | int reset_deassert(struct reset_ctl *reset_ctl) |
| 128 | { |
| 129 | struct reset_ops *ops = reset_dev_ops(reset_ctl->dev); |
| 130 | |
| 131 | debug("%s(reset_ctl=%p)\n", __func__, reset_ctl); |
| 132 | |
| 133 | return ops->rst_deassert(reset_ctl); |
| 134 | } |
| 135 | |
| 136 | UCLASS_DRIVER(reset) = { |
| 137 | .id = UCLASS_RESET, |
| 138 | .name = "reset", |
| 139 | }; |