xref: /linux/drivers/reset/reset-ti-sci.c (revision a1ff5a7d78a036d6c2178ee5acd6ba4946243800)
15a729246SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
228df169bSAndrew F. Davis /*
328df169bSAndrew F. Davis  * Texas Instrument's System Control Interface (TI-SCI) reset driver
428df169bSAndrew F. Davis  *
5a6df49f4SAlexander A. Klimov  * Copyright (C) 2015-2017 Texas Instruments Incorporated - https://www.ti.com/
628df169bSAndrew F. Davis  *	Andrew F. Davis <afd@ti.com>
728df169bSAndrew F. Davis  */
828df169bSAndrew F. Davis 
928df169bSAndrew F. Davis #include <linux/idr.h>
1028df169bSAndrew F. Davis #include <linux/module.h>
1128df169bSAndrew F. Davis #include <linux/mutex.h>
1228df169bSAndrew F. Davis #include <linux/of.h>
1328df169bSAndrew F. Davis #include <linux/platform_device.h>
1428df169bSAndrew F. Davis #include <linux/reset-controller.h>
1528df169bSAndrew F. Davis #include <linux/soc/ti/ti_sci_protocol.h>
1628df169bSAndrew F. Davis 
1728df169bSAndrew F. Davis /**
1828df169bSAndrew F. Davis  * struct ti_sci_reset_control - reset control structure
1928df169bSAndrew F. Davis  * @dev_id: SoC-specific device identifier
2028df169bSAndrew F. Davis  * @reset_mask: reset mask to use for toggling reset
2128df169bSAndrew F. Davis  * @lock: synchronize reset_mask read-modify-writes
2228df169bSAndrew F. Davis  */
2328df169bSAndrew F. Davis struct ti_sci_reset_control {
2428df169bSAndrew F. Davis 	u32 dev_id;
2528df169bSAndrew F. Davis 	u32 reset_mask;
2628df169bSAndrew F. Davis 	struct mutex lock;
2728df169bSAndrew F. Davis };
2828df169bSAndrew F. Davis 
2928df169bSAndrew F. Davis /**
3028df169bSAndrew F. Davis  * struct ti_sci_reset_data - reset controller information structure
3128df169bSAndrew F. Davis  * @rcdev: reset controller entity
3228df169bSAndrew F. Davis  * @dev: reset controller device pointer
3328df169bSAndrew F. Davis  * @sci: TI SCI handle used for communication with system controller
3428df169bSAndrew F. Davis  * @idr: idr structure for mapping ids to reset control structures
3528df169bSAndrew F. Davis  */
3628df169bSAndrew F. Davis struct ti_sci_reset_data {
3728df169bSAndrew F. Davis 	struct reset_controller_dev rcdev;
3828df169bSAndrew F. Davis 	struct device *dev;
3928df169bSAndrew F. Davis 	const struct ti_sci_handle *sci;
4028df169bSAndrew F. Davis 	struct idr idr;
4128df169bSAndrew F. Davis };
4228df169bSAndrew F. Davis 
4328df169bSAndrew F. Davis #define to_ti_sci_reset_data(p)	\
4428df169bSAndrew F. Davis 	container_of((p), struct ti_sci_reset_data, rcdev)
4528df169bSAndrew F. Davis 
4628df169bSAndrew F. Davis /**
4728df169bSAndrew F. Davis  * ti_sci_reset_set() - program a device's reset
4828df169bSAndrew F. Davis  * @rcdev: reset controller entity
4928df169bSAndrew F. Davis  * @id: ID of the reset to toggle
5028df169bSAndrew F. Davis  * @assert: boolean flag to indicate assert or deassert
5128df169bSAndrew F. Davis  *
5228df169bSAndrew F. Davis  * This is a common internal function used to assert or deassert a device's
5328df169bSAndrew F. Davis  * reset using the TI SCI protocol. The device's reset is asserted if the
5428df169bSAndrew F. Davis  * @assert argument is true, or deasserted if @assert argument is false.
5528df169bSAndrew F. Davis  * The mechanism itself is a read-modify-write procedure, the current device
5628df169bSAndrew F. Davis  * reset register is read using a TI SCI device operation, the new value is
5728df169bSAndrew F. Davis  * set or un-set using the reset's mask, and the new reset value written by
5828df169bSAndrew F. Davis  * using another TI SCI device operation.
5928df169bSAndrew F. Davis  *
6028df169bSAndrew F. Davis  * Return: 0 for successful request, else a corresponding error value
6128df169bSAndrew F. Davis  */
ti_sci_reset_set(struct reset_controller_dev * rcdev,unsigned long id,bool assert)6228df169bSAndrew F. Davis static int ti_sci_reset_set(struct reset_controller_dev *rcdev,
6328df169bSAndrew F. Davis 			    unsigned long id, bool assert)
6428df169bSAndrew F. Davis {
6528df169bSAndrew F. Davis 	struct ti_sci_reset_data *data = to_ti_sci_reset_data(rcdev);
6628df169bSAndrew F. Davis 	const struct ti_sci_handle *sci = data->sci;
6728df169bSAndrew F. Davis 	const struct ti_sci_dev_ops *dev_ops = &sci->ops.dev_ops;
6828df169bSAndrew F. Davis 	struct ti_sci_reset_control *control;
6928df169bSAndrew F. Davis 	u32 reset_state;
7028df169bSAndrew F. Davis 	int ret;
7128df169bSAndrew F. Davis 
7228df169bSAndrew F. Davis 	control = idr_find(&data->idr, id);
7328df169bSAndrew F. Davis 	if (!control)
7428df169bSAndrew F. Davis 		return -EINVAL;
7528df169bSAndrew F. Davis 
7628df169bSAndrew F. Davis 	mutex_lock(&control->lock);
7728df169bSAndrew F. Davis 
7828df169bSAndrew F. Davis 	ret = dev_ops->get_device_resets(sci, control->dev_id, &reset_state);
7928df169bSAndrew F. Davis 	if (ret)
8028df169bSAndrew F. Davis 		goto out;
8128df169bSAndrew F. Davis 
8228df169bSAndrew F. Davis 	if (assert)
8328df169bSAndrew F. Davis 		reset_state |= control->reset_mask;
8428df169bSAndrew F. Davis 	else
8528df169bSAndrew F. Davis 		reset_state &= ~control->reset_mask;
8628df169bSAndrew F. Davis 
8728df169bSAndrew F. Davis 	ret = dev_ops->set_device_resets(sci, control->dev_id, reset_state);
8828df169bSAndrew F. Davis out:
8928df169bSAndrew F. Davis 	mutex_unlock(&control->lock);
9028df169bSAndrew F. Davis 
9128df169bSAndrew F. Davis 	return ret;
9228df169bSAndrew F. Davis }
9328df169bSAndrew F. Davis 
9428df169bSAndrew F. Davis /**
9528df169bSAndrew F. Davis  * ti_sci_reset_assert() - assert device reset
9628df169bSAndrew F. Davis  * @rcdev: reset controller entity
9728df169bSAndrew F. Davis  * @id: ID of the reset to be asserted
9828df169bSAndrew F. Davis  *
9928df169bSAndrew F. Davis  * This function implements the reset driver op to assert a device's reset
10028df169bSAndrew F. Davis  * using the TI SCI protocol. This invokes the function ti_sci_reset_set()
10128df169bSAndrew F. Davis  * with the corresponding parameters as passed in, but with the @assert
10228df169bSAndrew F. Davis  * argument set to true for asserting the reset.
10328df169bSAndrew F. Davis  *
10428df169bSAndrew F. Davis  * Return: 0 for successful request, else a corresponding error value
10528df169bSAndrew F. Davis  */
ti_sci_reset_assert(struct reset_controller_dev * rcdev,unsigned long id)10628df169bSAndrew F. Davis static int ti_sci_reset_assert(struct reset_controller_dev *rcdev,
10728df169bSAndrew F. Davis 			       unsigned long id)
10828df169bSAndrew F. Davis {
10928df169bSAndrew F. Davis 	return ti_sci_reset_set(rcdev, id, true);
11028df169bSAndrew F. Davis }
11128df169bSAndrew F. Davis 
11228df169bSAndrew F. Davis /**
11328df169bSAndrew F. Davis  * ti_sci_reset_deassert() - deassert device reset
11428df169bSAndrew F. Davis  * @rcdev: reset controller entity
11528df169bSAndrew F. Davis  * @id: ID of the reset to be deasserted
11628df169bSAndrew F. Davis  *
11728df169bSAndrew F. Davis  * This function implements the reset driver op to deassert a device's reset
11828df169bSAndrew F. Davis  * using the TI SCI protocol. This invokes the function ti_sci_reset_set()
11928df169bSAndrew F. Davis  * with the corresponding parameters as passed in, but with the @assert
12028df169bSAndrew F. Davis  * argument set to false for deasserting the reset.
12128df169bSAndrew F. Davis  *
12228df169bSAndrew F. Davis  * Return: 0 for successful request, else a corresponding error value
12328df169bSAndrew F. Davis  */
ti_sci_reset_deassert(struct reset_controller_dev * rcdev,unsigned long id)12428df169bSAndrew F. Davis static int ti_sci_reset_deassert(struct reset_controller_dev *rcdev,
12528df169bSAndrew F. Davis 				 unsigned long id)
12628df169bSAndrew F. Davis {
12728df169bSAndrew F. Davis 	return ti_sci_reset_set(rcdev, id, false);
12828df169bSAndrew F. Davis }
12928df169bSAndrew F. Davis 
13028df169bSAndrew F. Davis /**
13128df169bSAndrew F. Davis  * ti_sci_reset_status() - check device reset status
13228df169bSAndrew F. Davis  * @rcdev: reset controller entity
13328df169bSAndrew F. Davis  * @id: ID of reset to be checked
13428df169bSAndrew F. Davis  *
13528df169bSAndrew F. Davis  * This function implements the reset driver op to return the status of a
13628df169bSAndrew F. Davis  * device's reset using the TI SCI protocol. The reset register value is read
13728df169bSAndrew F. Davis  * by invoking the TI SCI device operation .get_device_resets(), and the
13828df169bSAndrew F. Davis  * status of the specific reset is extracted and returned using this reset's
13928df169bSAndrew F. Davis  * reset mask.
14028df169bSAndrew F. Davis  *
14128df169bSAndrew F. Davis  * Return: 0 if reset is deasserted, or a non-zero value if reset is asserted
14228df169bSAndrew F. Davis  */
ti_sci_reset_status(struct reset_controller_dev * rcdev,unsigned long id)14328df169bSAndrew F. Davis static int ti_sci_reset_status(struct reset_controller_dev *rcdev,
14428df169bSAndrew F. Davis 			       unsigned long id)
14528df169bSAndrew F. Davis {
14628df169bSAndrew F. Davis 	struct ti_sci_reset_data *data = to_ti_sci_reset_data(rcdev);
14728df169bSAndrew F. Davis 	const struct ti_sci_handle *sci = data->sci;
14828df169bSAndrew F. Davis 	const struct ti_sci_dev_ops *dev_ops = &sci->ops.dev_ops;
14928df169bSAndrew F. Davis 	struct ti_sci_reset_control *control;
15028df169bSAndrew F. Davis 	u32 reset_state;
15128df169bSAndrew F. Davis 	int ret;
15228df169bSAndrew F. Davis 
15328df169bSAndrew F. Davis 	control = idr_find(&data->idr, id);
15428df169bSAndrew F. Davis 	if (!control)
15528df169bSAndrew F. Davis 		return -EINVAL;
15628df169bSAndrew F. Davis 
15728df169bSAndrew F. Davis 	ret = dev_ops->get_device_resets(sci, control->dev_id, &reset_state);
15828df169bSAndrew F. Davis 	if (ret)
15928df169bSAndrew F. Davis 		return ret;
16028df169bSAndrew F. Davis 
16128df169bSAndrew F. Davis 	return reset_state & control->reset_mask;
16228df169bSAndrew F. Davis }
16328df169bSAndrew F. Davis 
16428df169bSAndrew F. Davis static const struct reset_control_ops ti_sci_reset_ops = {
16528df169bSAndrew F. Davis 	.assert		= ti_sci_reset_assert,
16628df169bSAndrew F. Davis 	.deassert	= ti_sci_reset_deassert,
16728df169bSAndrew F. Davis 	.status		= ti_sci_reset_status,
16828df169bSAndrew F. Davis };
16928df169bSAndrew F. Davis 
17028df169bSAndrew F. Davis /**
17128df169bSAndrew F. Davis  * ti_sci_reset_of_xlate() - translate a set of OF arguments to a reset ID
17228df169bSAndrew F. Davis  * @rcdev: reset controller entity
17328df169bSAndrew F. Davis  * @reset_spec: OF reset argument specifier
17428df169bSAndrew F. Davis  *
17528df169bSAndrew F. Davis  * This function performs the translation of the reset argument specifier
17628df169bSAndrew F. Davis  * values defined in a reset consumer device node. The function allocates a
17728df169bSAndrew F. Davis  * reset control structure for that device reset, and will be used by the
17828df169bSAndrew F. Davis  * driver for performing any reset functions on that reset. An idr structure
17928df169bSAndrew F. Davis  * is allocated and used to map to the reset control structure. This idr
18028df169bSAndrew F. Davis  * is used by the driver to do reset lookups.
18128df169bSAndrew F. Davis  *
18228df169bSAndrew F. Davis  * Return: 0 for successful request, else a corresponding error value
18328df169bSAndrew F. Davis  */
ti_sci_reset_of_xlate(struct reset_controller_dev * rcdev,const struct of_phandle_args * reset_spec)18428df169bSAndrew F. Davis static int ti_sci_reset_of_xlate(struct reset_controller_dev *rcdev,
18528df169bSAndrew F. Davis 				 const struct of_phandle_args *reset_spec)
18628df169bSAndrew F. Davis {
18728df169bSAndrew F. Davis 	struct ti_sci_reset_data *data = to_ti_sci_reset_data(rcdev);
18828df169bSAndrew F. Davis 	struct ti_sci_reset_control *control;
18928df169bSAndrew F. Davis 
19028df169bSAndrew F. Davis 	if (WARN_ON(reset_spec->args_count != rcdev->of_reset_n_cells))
19128df169bSAndrew F. Davis 		return -EINVAL;
19228df169bSAndrew F. Davis 
19328df169bSAndrew F. Davis 	control = devm_kzalloc(data->dev, sizeof(*control), GFP_KERNEL);
19428df169bSAndrew F. Davis 	if (!control)
19528df169bSAndrew F. Davis 		return -ENOMEM;
19628df169bSAndrew F. Davis 
19728df169bSAndrew F. Davis 	control->dev_id = reset_spec->args[0];
19828df169bSAndrew F. Davis 	control->reset_mask = reset_spec->args[1];
19928df169bSAndrew F. Davis 	mutex_init(&control->lock);
20028df169bSAndrew F. Davis 
20128df169bSAndrew F. Davis 	return idr_alloc(&data->idr, control, 0, 0, GFP_KERNEL);
20228df169bSAndrew F. Davis }
20328df169bSAndrew F. Davis 
20428df169bSAndrew F. Davis static const struct of_device_id ti_sci_reset_of_match[] = {
20528df169bSAndrew F. Davis 	{ .compatible = "ti,sci-reset", },
20628df169bSAndrew F. Davis 	{ /* sentinel */ },
20728df169bSAndrew F. Davis };
20828df169bSAndrew F. Davis MODULE_DEVICE_TABLE(of, ti_sci_reset_of_match);
20928df169bSAndrew F. Davis 
ti_sci_reset_probe(struct platform_device * pdev)21028df169bSAndrew F. Davis static int ti_sci_reset_probe(struct platform_device *pdev)
21128df169bSAndrew F. Davis {
21228df169bSAndrew F. Davis 	struct ti_sci_reset_data *data;
21328df169bSAndrew F. Davis 
21428df169bSAndrew F. Davis 	if (!pdev->dev.of_node)
21528df169bSAndrew F. Davis 		return -ENODEV;
21628df169bSAndrew F. Davis 
21728df169bSAndrew F. Davis 	data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
21828df169bSAndrew F. Davis 	if (!data)
21928df169bSAndrew F. Davis 		return -ENOMEM;
22028df169bSAndrew F. Davis 
22128df169bSAndrew F. Davis 	data->sci = devm_ti_sci_get_handle(&pdev->dev);
22228df169bSAndrew F. Davis 	if (IS_ERR(data->sci))
22328df169bSAndrew F. Davis 		return PTR_ERR(data->sci);
22428df169bSAndrew F. Davis 
22528df169bSAndrew F. Davis 	data->rcdev.ops = &ti_sci_reset_ops;
22628df169bSAndrew F. Davis 	data->rcdev.owner = THIS_MODULE;
22728df169bSAndrew F. Davis 	data->rcdev.of_node = pdev->dev.of_node;
22828df169bSAndrew F. Davis 	data->rcdev.of_reset_n_cells = 2;
22928df169bSAndrew F. Davis 	data->rcdev.of_xlate = ti_sci_reset_of_xlate;
23028df169bSAndrew F. Davis 	data->dev = &pdev->dev;
23128df169bSAndrew F. Davis 	idr_init(&data->idr);
23228df169bSAndrew F. Davis 
23328df169bSAndrew F. Davis 	platform_set_drvdata(pdev, data);
23428df169bSAndrew F. Davis 
23528df169bSAndrew F. Davis 	return reset_controller_register(&data->rcdev);
23628df169bSAndrew F. Davis }
23728df169bSAndrew F. Davis 
ti_sci_reset_remove(struct platform_device * pdev)238*6d89df61SUwe Kleine-König static void ti_sci_reset_remove(struct platform_device *pdev)
23928df169bSAndrew F. Davis {
24028df169bSAndrew F. Davis 	struct ti_sci_reset_data *data = platform_get_drvdata(pdev);
24128df169bSAndrew F. Davis 
24228df169bSAndrew F. Davis 	reset_controller_unregister(&data->rcdev);
24328df169bSAndrew F. Davis 
24428df169bSAndrew F. Davis 	idr_destroy(&data->idr);
24528df169bSAndrew F. Davis }
24628df169bSAndrew F. Davis 
24728df169bSAndrew F. Davis static struct platform_driver ti_sci_reset_driver = {
24828df169bSAndrew F. Davis 	.probe = ti_sci_reset_probe,
249*6d89df61SUwe Kleine-König 	.remove_new = ti_sci_reset_remove,
25028df169bSAndrew F. Davis 	.driver = {
25128df169bSAndrew F. Davis 		.name = "ti-sci-reset",
25228df169bSAndrew F. Davis 		.of_match_table = ti_sci_reset_of_match,
25328df169bSAndrew F. Davis 	},
25428df169bSAndrew F. Davis };
25528df169bSAndrew F. Davis module_platform_driver(ti_sci_reset_driver);
25628df169bSAndrew F. Davis 
25728df169bSAndrew F. Davis MODULE_AUTHOR("Andrew F. Davis <afd@ti.com>");
25828df169bSAndrew F. Davis MODULE_DESCRIPTION("TI System Control Interface (TI SCI) Reset driver");
25928df169bSAndrew F. Davis MODULE_LICENSE("GPL v2");
260