1 // SPDX-License-Identifier: GPL-2.0-only
3 * Texas Instrument's System Control Interface (TI-SCI) reset driver
5 * Copyright (C) 2015-2017 Texas Instruments Incorporated - https://www.ti.com/
6 * Andrew F. Davis <afd@ti.com>
10 #include <linux/module.h>
11 #include <linux/mutex.h>
13 #include <linux/platform_device.h>
14 #include <linux/reset-controller.h>
15 #include <linux/soc/ti/ti_sci_protocol.h>
18 * struct ti_sci_reset_control - reset control structure
19 * @dev_id: SoC-specific device identifier
20 * @reset_mask: reset mask to use for toggling reset
21 * @lock: synchronize reset_mask read-modify-writes
23 struct ti_sci_reset_control {
30 * struct ti_sci_reset_data - reset controller information structure
31 * @rcdev: reset controller entity
32 * @dev: reset controller device pointer
33 * @sci: TI SCI handle used for communication with system controller
34 * @idr: idr structure for mapping ids to reset control structures
36 struct ti_sci_reset_data {
37 struct reset_controller_dev rcdev;
39 const struct ti_sci_handle *sci;
43 #define to_ti_sci_reset_data(p) \
44 container_of((p), struct ti_sci_reset_data, rcdev)
47 * ti_sci_reset_set() - program a device's reset
48 * @rcdev: reset controller entity
49 * @id: ID of the reset to toggle
50 * @assert: boolean flag to indicate assert or deassert
52 * This is a common internal function used to assert or deassert a device's
53 * reset using the TI SCI protocol. The device's reset is asserted if the
54 * @assert argument is true, or deasserted if @assert argument is false.
55 * The mechanism itself is a read-modify-write procedure, the current device
56 * reset register is read using a TI SCI device operation, the new value is
57 * set or un-set using the reset's mask, and the new reset value written by
58 * using another TI SCI device operation.
60 * Return: 0 for successful request, else a corresponding error value
62 static int ti_sci_reset_set(struct reset_controller_dev *rcdev,
63 unsigned long id, bool assert)
65 struct ti_sci_reset_data *data = to_ti_sci_reset_data(rcdev);
66 const struct ti_sci_handle *sci = data->sci;
67 const struct ti_sci_dev_ops *dev_ops = &sci->ops.dev_ops;
68 struct ti_sci_reset_control *control;
72 control = idr_find(&data->idr, id);
76 mutex_lock(&control->lock);
78 ret = dev_ops->get_device_resets(sci, control->dev_id, &reset_state);
83 reset_state |= control->reset_mask;
85 reset_state &= ~control->reset_mask;
87 ret = dev_ops->set_device_resets(sci, control->dev_id, reset_state);
89 mutex_unlock(&control->lock);
95 * ti_sci_reset_assert() - assert device reset
96 * @rcdev: reset controller entity
97 * @id: ID of the reset to be asserted
99 * This function implements the reset driver op to assert a device's reset
100 * using the TI SCI protocol. This invokes the function ti_sci_reset_set()
101 * with the corresponding parameters as passed in, but with the @assert
102 * argument set to true for asserting the reset.
104 * Return: 0 for successful request, else a corresponding error value
106 static int ti_sci_reset_assert(struct reset_controller_dev *rcdev,
109 return ti_sci_reset_set(rcdev, id, true);
113 * ti_sci_reset_deassert() - deassert device reset
114 * @rcdev: reset controller entity
115 * @id: ID of the reset to be deasserted
117 * This function implements the reset driver op to deassert a device's reset
118 * using the TI SCI protocol. This invokes the function ti_sci_reset_set()
119 * with the corresponding parameters as passed in, but with the @assert
120 * argument set to false for deasserting the reset.
122 * Return: 0 for successful request, else a corresponding error value
124 static int ti_sci_reset_deassert(struct reset_controller_dev *rcdev,
127 return ti_sci_reset_set(rcdev, id, false);
131 * ti_sci_reset_status() - check device reset status
132 * @rcdev: reset controller entity
133 * @id: ID of reset to be checked
135 * This function implements the reset driver op to return the status of a
136 * device's reset using the TI SCI protocol. The reset register value is read
137 * by invoking the TI SCI device operation .get_device_resets(), and the
138 * status of the specific reset is extracted and returned using this reset's
141 * Return: 0 if reset is deasserted, or a non-zero value if reset is asserted
143 static int ti_sci_reset_status(struct reset_controller_dev *rcdev,
146 struct ti_sci_reset_data *data = to_ti_sci_reset_data(rcdev);
147 const struct ti_sci_handle *sci = data->sci;
148 const struct ti_sci_dev_ops *dev_ops = &sci->ops.dev_ops;
149 struct ti_sci_reset_control *control;
153 control = idr_find(&data->idr, id);
157 ret = dev_ops->get_device_resets(sci, control->dev_id, &reset_state);
161 return reset_state & control->reset_mask;
164 static const struct reset_control_ops ti_sci_reset_ops = {
165 .assert = ti_sci_reset_assert,
166 .deassert = ti_sci_reset_deassert,
167 .status = ti_sci_reset_status,
171 * ti_sci_reset_of_xlate() - translate a set of OF arguments to a reset ID
172 * @rcdev: reset controller entity
173 * @reset_spec: OF reset argument specifier
175 * This function performs the translation of the reset argument specifier
176 * values defined in a reset consumer device node. The function allocates a
177 * reset control structure for that device reset, and will be used by the
178 * driver for performing any reset functions on that reset. An idr structure
179 * is allocated and used to map to the reset control structure. This idr
180 * is used by the driver to do reset lookups.
182 * Return: 0 for successful request, else a corresponding error value
184 static int ti_sci_reset_of_xlate(struct reset_controller_dev *rcdev,
185 const struct of_phandle_args *reset_spec)
187 struct ti_sci_reset_data *data = to_ti_sci_reset_data(rcdev);
188 struct ti_sci_reset_control *control;
190 if (WARN_ON(reset_spec->args_count != rcdev->of_reset_n_cells))
193 control = devm_kzalloc(data->dev, sizeof(*control), GFP_KERNEL);
197 control->dev_id = reset_spec->args[0];
198 control->reset_mask = reset_spec->args[1];
199 mutex_init(&control->lock);
201 return idr_alloc(&data->idr, control, 0, 0, GFP_KERNEL);
204 static const struct of_device_id ti_sci_reset_of_match[] = {
205 { .compatible = "ti,sci-reset", },
208 MODULE_DEVICE_TABLE(of, ti_sci_reset_of_match);
210 static int ti_sci_reset_probe(struct platform_device *pdev)
212 struct ti_sci_reset_data *data;
214 if (!pdev->dev.of_node)
217 data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
221 data->sci = devm_ti_sci_get_handle(&pdev->dev);
222 if (IS_ERR(data->sci))
223 return PTR_ERR(data->sci);
225 data->rcdev.ops = &ti_sci_reset_ops;
226 data->rcdev.owner = THIS_MODULE;
227 data->rcdev.of_node = pdev->dev.of_node;
228 data->rcdev.of_reset_n_cells = 2;
229 data->rcdev.of_xlate = ti_sci_reset_of_xlate;
230 data->dev = &pdev->dev;
231 idr_init(&data->idr);
233 platform_set_drvdata(pdev, data);
235 return reset_controller_register(&data->rcdev);
238 static int ti_sci_reset_remove(struct platform_device *pdev)
240 struct ti_sci_reset_data *data = platform_get_drvdata(pdev);
242 reset_controller_unregister(&data->rcdev);
244 idr_destroy(&data->idr);
249 static struct platform_driver ti_sci_reset_driver = {
250 .probe = ti_sci_reset_probe,
251 .remove = ti_sci_reset_remove,
253 .name = "ti-sci-reset",
254 .of_match_table = ti_sci_reset_of_match,
257 module_platform_driver(ti_sci_reset_driver);
259 MODULE_AUTHOR("Andrew F. Davis <afd@ti.com>");
260 MODULE_DESCRIPTION("TI System Control Interface (TI SCI) Reset driver");
261 MODULE_LICENSE("GPL v2");