blob: 307a29705f154640273521ca39fc7d70f8cc65c4 [file] [log] [blame]
Stephen Warren185ad872016-06-17 09:43:58 -06001/*
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
13DECLARE_GLOBAL_DATA_PTR;
14
15static inline struct reset_ops *reset_dev_ops(struct udevice *dev)
16{
17 return (struct reset_ops *)dev->driver->ops;
18}
19
20static int reset_of_xlate_default(struct reset_ctl *reset_ctl,
Simon Glassaec8cc02017-05-18 20:09:50 -060021 struct ofnode_phandle_args *args)
Stephen Warren185ad872016-06-17 09:43:58 -060022{
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
35int reset_get_by_index(struct udevice *dev, int index,
36 struct reset_ctl *reset_ctl)
37{
Simon Glassaec8cc02017-05-18 20:09:50 -060038 struct ofnode_phandle_args args;
Stephen Warren185ad872016-06-17 09:43:58 -060039 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);
Patrice Chotarde4d368e2017-07-18 11:57:06 +020045 reset_ctl->dev = NULL;
Stephen Warren185ad872016-06-17 09:43:58 -060046
Simon Glassaec8cc02017-05-18 20:09:50 -060047 ret = dev_read_phandle_with_args(dev, "resets", "#reset-cells", 0,
48 index, &args);
Stephen Warren185ad872016-06-17 09:43:58 -060049 if (ret) {
Simon Glassaec8cc02017-05-18 20:09:50 -060050 debug("%s: fdtdec_parse_phandle_with_args() failed: %d\n",
Stephen Warren185ad872016-06-17 09:43:58 -060051 __func__, ret);
52 return ret;
53 }
54
Simon Glassaec8cc02017-05-18 20:09:50 -060055 ret = uclass_get_device_by_ofnode(UCLASS_RESET, args.node,
56 &dev_reset);
Stephen Warren185ad872016-06-17 09:43:58 -060057 if (ret) {
Simon Glassaec8cc02017-05-18 20:09:50 -060058 debug("%s: uclass_get_device_by_ofnode() failed: %d\n",
Stephen Warren185ad872016-06-17 09:43:58 -060059 __func__, ret);
Simon Glassaec8cc02017-05-18 20:09:50 -060060 debug("%s %d\n", ofnode_get_name(args.node), args.args[0]);
Stephen Warren185ad872016-06-17 09:43:58 -060061 return ret;
62 }
63 ops = reset_dev_ops(dev_reset);
64
65 reset_ctl->dev = dev_reset;
66 if (ops->of_xlate)
67 ret = ops->of_xlate(reset_ctl, &args);
68 else
69 ret = reset_of_xlate_default(reset_ctl, &args);
70 if (ret) {
71 debug("of_xlate() failed: %d\n", ret);
72 return ret;
73 }
74
75 ret = ops->request(reset_ctl);
76 if (ret) {
77 debug("ops->request() failed: %d\n", ret);
78 return ret;
79 }
80
81 return 0;
82}
83
84int reset_get_by_name(struct udevice *dev, const char *name,
85 struct reset_ctl *reset_ctl)
86{
87 int index;
88
89 debug("%s(dev=%p, name=%s, reset_ctl=%p)\n", __func__, dev, name,
90 reset_ctl);
Patrice Chotarde4d368e2017-07-18 11:57:06 +020091 reset_ctl->dev = NULL;
Stephen Warren185ad872016-06-17 09:43:58 -060092
Simon Glassaec8cc02017-05-18 20:09:50 -060093 index = dev_read_stringlist_search(dev, "reset-names", name);
Stephen Warren185ad872016-06-17 09:43:58 -060094 if (index < 0) {
Simon Glassb0ea7402016-10-02 17:59:28 -060095 debug("fdt_stringlist_search() failed: %d\n", index);
Stephen Warren185ad872016-06-17 09:43:58 -060096 return index;
97 }
98
99 return reset_get_by_index(dev, index, reset_ctl);
100}
101
Patrice Chotard76290e32017-07-18 11:57:05 +0200102int reset_request(struct reset_ctl *reset_ctl)
103{
104 struct reset_ops *ops = reset_dev_ops(reset_ctl->dev);
105
106 debug("%s(reset_ctl=%p)\n", __func__, reset_ctl);
107
108 return ops->request(reset_ctl);
109}
110
Stephen Warren185ad872016-06-17 09:43:58 -0600111int reset_free(struct reset_ctl *reset_ctl)
112{
113 struct reset_ops *ops = reset_dev_ops(reset_ctl->dev);
114
115 debug("%s(reset_ctl=%p)\n", __func__, reset_ctl);
116
117 return ops->free(reset_ctl);
118}
119
120int reset_assert(struct reset_ctl *reset_ctl)
121{
122 struct reset_ops *ops = reset_dev_ops(reset_ctl->dev);
123
124 debug("%s(reset_ctl=%p)\n", __func__, reset_ctl);
125
126 return ops->rst_assert(reset_ctl);
127}
128
129int reset_deassert(struct reset_ctl *reset_ctl)
130{
131 struct reset_ops *ops = reset_dev_ops(reset_ctl->dev);
132
133 debug("%s(reset_ctl=%p)\n", __func__, reset_ctl);
134
135 return ops->rst_deassert(reset_ctl);
136}
137
Patrice Chotarde4d368e2017-07-18 11:57:06 +0200138int reset_release_all(struct reset_ctl *reset_ctl, int count)
139{
140 int i, ret;
141
142 for (i = 0; i < count; i++) {
143 debug("%s(reset_ctl[%d]=%p)\n", __func__, i, &reset_ctl[i]);
144
145 /* check if reset has been previously requested */
146 if (!reset_ctl[i].dev)
147 continue;
148
149 ret = reset_assert(&reset_ctl[i]);
150 if (ret)
151 return ret;
152
153 ret = reset_free(&reset_ctl[i]);
154 if (ret)
155 return ret;
156 }
157
158 return 0;
159}
160
Stephen Warren185ad872016-06-17 09:43:58 -0600161UCLASS_DRIVER(reset) = {
162 .id = UCLASS_RESET,
163 .name = "reset",
164};