xref: /OK3568_Linux_fs/kernel/drivers/reset/reset-ti-sci.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun  * Texas Instrument's System Control Interface (TI-SCI) reset driver
3*4882a593Smuzhiyun  *
4*4882a593Smuzhiyun  * Copyright (C) 2015-2017 Texas Instruments Incorporated - https://www.ti.com/
5*4882a593Smuzhiyun  *	Andrew F. Davis <afd@ti.com>
6*4882a593Smuzhiyun  *
7*4882a593Smuzhiyun  * This program is free software; you can redistribute it and/or modify
8*4882a593Smuzhiyun  * it under the terms of the GNU General Public License version 2 as
9*4882a593Smuzhiyun  * published by the Free Software Foundation.
10*4882a593Smuzhiyun  *
11*4882a593Smuzhiyun  * This program is distributed "as is" WITHOUT ANY WARRANTY of any
12*4882a593Smuzhiyun  * kind, whether express or implied; without even the implied warranty
13*4882a593Smuzhiyun  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14*4882a593Smuzhiyun  * GNU General Public License for more details.
15*4882a593Smuzhiyun  */
16*4882a593Smuzhiyun 
17*4882a593Smuzhiyun #include <linux/idr.h>
18*4882a593Smuzhiyun #include <linux/module.h>
19*4882a593Smuzhiyun #include <linux/mutex.h>
20*4882a593Smuzhiyun #include <linux/of.h>
21*4882a593Smuzhiyun #include <linux/platform_device.h>
22*4882a593Smuzhiyun #include <linux/reset-controller.h>
23*4882a593Smuzhiyun #include <linux/soc/ti/ti_sci_protocol.h>
24*4882a593Smuzhiyun 
25*4882a593Smuzhiyun /**
26*4882a593Smuzhiyun  * struct ti_sci_reset_control - reset control structure
27*4882a593Smuzhiyun  * @dev_id: SoC-specific device identifier
28*4882a593Smuzhiyun  * @reset_mask: reset mask to use for toggling reset
29*4882a593Smuzhiyun  * @lock: synchronize reset_mask read-modify-writes
30*4882a593Smuzhiyun  */
31*4882a593Smuzhiyun struct ti_sci_reset_control {
32*4882a593Smuzhiyun 	u32 dev_id;
33*4882a593Smuzhiyun 	u32 reset_mask;
34*4882a593Smuzhiyun 	struct mutex lock;
35*4882a593Smuzhiyun };
36*4882a593Smuzhiyun 
37*4882a593Smuzhiyun /**
38*4882a593Smuzhiyun  * struct ti_sci_reset_data - reset controller information structure
39*4882a593Smuzhiyun  * @rcdev: reset controller entity
40*4882a593Smuzhiyun  * @dev: reset controller device pointer
41*4882a593Smuzhiyun  * @sci: TI SCI handle used for communication with system controller
42*4882a593Smuzhiyun  * @idr: idr structure for mapping ids to reset control structures
43*4882a593Smuzhiyun  */
44*4882a593Smuzhiyun struct ti_sci_reset_data {
45*4882a593Smuzhiyun 	struct reset_controller_dev rcdev;
46*4882a593Smuzhiyun 	struct device *dev;
47*4882a593Smuzhiyun 	const struct ti_sci_handle *sci;
48*4882a593Smuzhiyun 	struct idr idr;
49*4882a593Smuzhiyun };
50*4882a593Smuzhiyun 
51*4882a593Smuzhiyun #define to_ti_sci_reset_data(p)	\
52*4882a593Smuzhiyun 	container_of((p), struct ti_sci_reset_data, rcdev)
53*4882a593Smuzhiyun 
54*4882a593Smuzhiyun /**
55*4882a593Smuzhiyun  * ti_sci_reset_set() - program a device's reset
56*4882a593Smuzhiyun  * @rcdev: reset controller entity
57*4882a593Smuzhiyun  * @id: ID of the reset to toggle
58*4882a593Smuzhiyun  * @assert: boolean flag to indicate assert or deassert
59*4882a593Smuzhiyun  *
60*4882a593Smuzhiyun  * This is a common internal function used to assert or deassert a device's
61*4882a593Smuzhiyun  * reset using the TI SCI protocol. The device's reset is asserted if the
62*4882a593Smuzhiyun  * @assert argument is true, or deasserted if @assert argument is false.
63*4882a593Smuzhiyun  * The mechanism itself is a read-modify-write procedure, the current device
64*4882a593Smuzhiyun  * reset register is read using a TI SCI device operation, the new value is
65*4882a593Smuzhiyun  * set or un-set using the reset's mask, and the new reset value written by
66*4882a593Smuzhiyun  * using another TI SCI device operation.
67*4882a593Smuzhiyun  *
68*4882a593Smuzhiyun  * Return: 0 for successful request, else a corresponding error value
69*4882a593Smuzhiyun  */
ti_sci_reset_set(struct reset_controller_dev * rcdev,unsigned long id,bool assert)70*4882a593Smuzhiyun static int ti_sci_reset_set(struct reset_controller_dev *rcdev,
71*4882a593Smuzhiyun 			    unsigned long id, bool assert)
72*4882a593Smuzhiyun {
73*4882a593Smuzhiyun 	struct ti_sci_reset_data *data = to_ti_sci_reset_data(rcdev);
74*4882a593Smuzhiyun 	const struct ti_sci_handle *sci = data->sci;
75*4882a593Smuzhiyun 	const struct ti_sci_dev_ops *dev_ops = &sci->ops.dev_ops;
76*4882a593Smuzhiyun 	struct ti_sci_reset_control *control;
77*4882a593Smuzhiyun 	u32 reset_state;
78*4882a593Smuzhiyun 	int ret;
79*4882a593Smuzhiyun 
80*4882a593Smuzhiyun 	control = idr_find(&data->idr, id);
81*4882a593Smuzhiyun 	if (!control)
82*4882a593Smuzhiyun 		return -EINVAL;
83*4882a593Smuzhiyun 
84*4882a593Smuzhiyun 	mutex_lock(&control->lock);
85*4882a593Smuzhiyun 
86*4882a593Smuzhiyun 	ret = dev_ops->get_device_resets(sci, control->dev_id, &reset_state);
87*4882a593Smuzhiyun 	if (ret)
88*4882a593Smuzhiyun 		goto out;
89*4882a593Smuzhiyun 
90*4882a593Smuzhiyun 	if (assert)
91*4882a593Smuzhiyun 		reset_state |= control->reset_mask;
92*4882a593Smuzhiyun 	else
93*4882a593Smuzhiyun 		reset_state &= ~control->reset_mask;
94*4882a593Smuzhiyun 
95*4882a593Smuzhiyun 	ret = dev_ops->set_device_resets(sci, control->dev_id, reset_state);
96*4882a593Smuzhiyun out:
97*4882a593Smuzhiyun 	mutex_unlock(&control->lock);
98*4882a593Smuzhiyun 
99*4882a593Smuzhiyun 	return ret;
100*4882a593Smuzhiyun }
101*4882a593Smuzhiyun 
102*4882a593Smuzhiyun /**
103*4882a593Smuzhiyun  * ti_sci_reset_assert() - assert device reset
104*4882a593Smuzhiyun  * @rcdev: reset controller entity
105*4882a593Smuzhiyun  * @id: ID of the reset to be asserted
106*4882a593Smuzhiyun  *
107*4882a593Smuzhiyun  * This function implements the reset driver op to assert a device's reset
108*4882a593Smuzhiyun  * using the TI SCI protocol. This invokes the function ti_sci_reset_set()
109*4882a593Smuzhiyun  * with the corresponding parameters as passed in, but with the @assert
110*4882a593Smuzhiyun  * argument set to true for asserting the reset.
111*4882a593Smuzhiyun  *
112*4882a593Smuzhiyun  * Return: 0 for successful request, else a corresponding error value
113*4882a593Smuzhiyun  */
ti_sci_reset_assert(struct reset_controller_dev * rcdev,unsigned long id)114*4882a593Smuzhiyun static int ti_sci_reset_assert(struct reset_controller_dev *rcdev,
115*4882a593Smuzhiyun 			       unsigned long id)
116*4882a593Smuzhiyun {
117*4882a593Smuzhiyun 	return ti_sci_reset_set(rcdev, id, true);
118*4882a593Smuzhiyun }
119*4882a593Smuzhiyun 
120*4882a593Smuzhiyun /**
121*4882a593Smuzhiyun  * ti_sci_reset_deassert() - deassert device reset
122*4882a593Smuzhiyun  * @rcdev: reset controller entity
123*4882a593Smuzhiyun  * @id: ID of the reset to be deasserted
124*4882a593Smuzhiyun  *
125*4882a593Smuzhiyun  * This function implements the reset driver op to deassert a device's reset
126*4882a593Smuzhiyun  * using the TI SCI protocol. This invokes the function ti_sci_reset_set()
127*4882a593Smuzhiyun  * with the corresponding parameters as passed in, but with the @assert
128*4882a593Smuzhiyun  * argument set to false for deasserting the reset.
129*4882a593Smuzhiyun  *
130*4882a593Smuzhiyun  * Return: 0 for successful request, else a corresponding error value
131*4882a593Smuzhiyun  */
ti_sci_reset_deassert(struct reset_controller_dev * rcdev,unsigned long id)132*4882a593Smuzhiyun static int ti_sci_reset_deassert(struct reset_controller_dev *rcdev,
133*4882a593Smuzhiyun 				 unsigned long id)
134*4882a593Smuzhiyun {
135*4882a593Smuzhiyun 	return ti_sci_reset_set(rcdev, id, false);
136*4882a593Smuzhiyun }
137*4882a593Smuzhiyun 
138*4882a593Smuzhiyun /**
139*4882a593Smuzhiyun  * ti_sci_reset_status() - check device reset status
140*4882a593Smuzhiyun  * @rcdev: reset controller entity
141*4882a593Smuzhiyun  * @id: ID of reset to be checked
142*4882a593Smuzhiyun  *
143*4882a593Smuzhiyun  * This function implements the reset driver op to return the status of a
144*4882a593Smuzhiyun  * device's reset using the TI SCI protocol. The reset register value is read
145*4882a593Smuzhiyun  * by invoking the TI SCI device operation .get_device_resets(), and the
146*4882a593Smuzhiyun  * status of the specific reset is extracted and returned using this reset's
147*4882a593Smuzhiyun  * reset mask.
148*4882a593Smuzhiyun  *
149*4882a593Smuzhiyun  * Return: 0 if reset is deasserted, or a non-zero value if reset is asserted
150*4882a593Smuzhiyun  */
ti_sci_reset_status(struct reset_controller_dev * rcdev,unsigned long id)151*4882a593Smuzhiyun static int ti_sci_reset_status(struct reset_controller_dev *rcdev,
152*4882a593Smuzhiyun 			       unsigned long id)
153*4882a593Smuzhiyun {
154*4882a593Smuzhiyun 	struct ti_sci_reset_data *data = to_ti_sci_reset_data(rcdev);
155*4882a593Smuzhiyun 	const struct ti_sci_handle *sci = data->sci;
156*4882a593Smuzhiyun 	const struct ti_sci_dev_ops *dev_ops = &sci->ops.dev_ops;
157*4882a593Smuzhiyun 	struct ti_sci_reset_control *control;
158*4882a593Smuzhiyun 	u32 reset_state;
159*4882a593Smuzhiyun 	int ret;
160*4882a593Smuzhiyun 
161*4882a593Smuzhiyun 	control = idr_find(&data->idr, id);
162*4882a593Smuzhiyun 	if (!control)
163*4882a593Smuzhiyun 		return -EINVAL;
164*4882a593Smuzhiyun 
165*4882a593Smuzhiyun 	ret = dev_ops->get_device_resets(sci, control->dev_id, &reset_state);
166*4882a593Smuzhiyun 	if (ret)
167*4882a593Smuzhiyun 		return ret;
168*4882a593Smuzhiyun 
169*4882a593Smuzhiyun 	return reset_state & control->reset_mask;
170*4882a593Smuzhiyun }
171*4882a593Smuzhiyun 
172*4882a593Smuzhiyun static const struct reset_control_ops ti_sci_reset_ops = {
173*4882a593Smuzhiyun 	.assert		= ti_sci_reset_assert,
174*4882a593Smuzhiyun 	.deassert	= ti_sci_reset_deassert,
175*4882a593Smuzhiyun 	.status		= ti_sci_reset_status,
176*4882a593Smuzhiyun };
177*4882a593Smuzhiyun 
178*4882a593Smuzhiyun /**
179*4882a593Smuzhiyun  * ti_sci_reset_of_xlate() - translate a set of OF arguments to a reset ID
180*4882a593Smuzhiyun  * @rcdev: reset controller entity
181*4882a593Smuzhiyun  * @reset_spec: OF reset argument specifier
182*4882a593Smuzhiyun  *
183*4882a593Smuzhiyun  * This function performs the translation of the reset argument specifier
184*4882a593Smuzhiyun  * values defined in a reset consumer device node. The function allocates a
185*4882a593Smuzhiyun  * reset control structure for that device reset, and will be used by the
186*4882a593Smuzhiyun  * driver for performing any reset functions on that reset. An idr structure
187*4882a593Smuzhiyun  * is allocated and used to map to the reset control structure. This idr
188*4882a593Smuzhiyun  * is used by the driver to do reset lookups.
189*4882a593Smuzhiyun  *
190*4882a593Smuzhiyun  * Return: 0 for successful request, else a corresponding error value
191*4882a593Smuzhiyun  */
ti_sci_reset_of_xlate(struct reset_controller_dev * rcdev,const struct of_phandle_args * reset_spec)192*4882a593Smuzhiyun static int ti_sci_reset_of_xlate(struct reset_controller_dev *rcdev,
193*4882a593Smuzhiyun 				 const struct of_phandle_args *reset_spec)
194*4882a593Smuzhiyun {
195*4882a593Smuzhiyun 	struct ti_sci_reset_data *data = to_ti_sci_reset_data(rcdev);
196*4882a593Smuzhiyun 	struct ti_sci_reset_control *control;
197*4882a593Smuzhiyun 
198*4882a593Smuzhiyun 	if (WARN_ON(reset_spec->args_count != rcdev->of_reset_n_cells))
199*4882a593Smuzhiyun 		return -EINVAL;
200*4882a593Smuzhiyun 
201*4882a593Smuzhiyun 	control = devm_kzalloc(data->dev, sizeof(*control), GFP_KERNEL);
202*4882a593Smuzhiyun 	if (!control)
203*4882a593Smuzhiyun 		return -ENOMEM;
204*4882a593Smuzhiyun 
205*4882a593Smuzhiyun 	control->dev_id = reset_spec->args[0];
206*4882a593Smuzhiyun 	control->reset_mask = reset_spec->args[1];
207*4882a593Smuzhiyun 	mutex_init(&control->lock);
208*4882a593Smuzhiyun 
209*4882a593Smuzhiyun 	return idr_alloc(&data->idr, control, 0, 0, GFP_KERNEL);
210*4882a593Smuzhiyun }
211*4882a593Smuzhiyun 
212*4882a593Smuzhiyun static const struct of_device_id ti_sci_reset_of_match[] = {
213*4882a593Smuzhiyun 	{ .compatible = "ti,sci-reset", },
214*4882a593Smuzhiyun 	{ /* sentinel */ },
215*4882a593Smuzhiyun };
216*4882a593Smuzhiyun MODULE_DEVICE_TABLE(of, ti_sci_reset_of_match);
217*4882a593Smuzhiyun 
ti_sci_reset_probe(struct platform_device * pdev)218*4882a593Smuzhiyun static int ti_sci_reset_probe(struct platform_device *pdev)
219*4882a593Smuzhiyun {
220*4882a593Smuzhiyun 	struct ti_sci_reset_data *data;
221*4882a593Smuzhiyun 
222*4882a593Smuzhiyun 	if (!pdev->dev.of_node)
223*4882a593Smuzhiyun 		return -ENODEV;
224*4882a593Smuzhiyun 
225*4882a593Smuzhiyun 	data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
226*4882a593Smuzhiyun 	if (!data)
227*4882a593Smuzhiyun 		return -ENOMEM;
228*4882a593Smuzhiyun 
229*4882a593Smuzhiyun 	data->sci = devm_ti_sci_get_handle(&pdev->dev);
230*4882a593Smuzhiyun 	if (IS_ERR(data->sci))
231*4882a593Smuzhiyun 		return PTR_ERR(data->sci);
232*4882a593Smuzhiyun 
233*4882a593Smuzhiyun 	data->rcdev.ops = &ti_sci_reset_ops;
234*4882a593Smuzhiyun 	data->rcdev.owner = THIS_MODULE;
235*4882a593Smuzhiyun 	data->rcdev.of_node = pdev->dev.of_node;
236*4882a593Smuzhiyun 	data->rcdev.of_reset_n_cells = 2;
237*4882a593Smuzhiyun 	data->rcdev.of_xlate = ti_sci_reset_of_xlate;
238*4882a593Smuzhiyun 	data->dev = &pdev->dev;
239*4882a593Smuzhiyun 	idr_init(&data->idr);
240*4882a593Smuzhiyun 
241*4882a593Smuzhiyun 	platform_set_drvdata(pdev, data);
242*4882a593Smuzhiyun 
243*4882a593Smuzhiyun 	return reset_controller_register(&data->rcdev);
244*4882a593Smuzhiyun }
245*4882a593Smuzhiyun 
ti_sci_reset_remove(struct platform_device * pdev)246*4882a593Smuzhiyun static int ti_sci_reset_remove(struct platform_device *pdev)
247*4882a593Smuzhiyun {
248*4882a593Smuzhiyun 	struct ti_sci_reset_data *data = platform_get_drvdata(pdev);
249*4882a593Smuzhiyun 
250*4882a593Smuzhiyun 	reset_controller_unregister(&data->rcdev);
251*4882a593Smuzhiyun 
252*4882a593Smuzhiyun 	idr_destroy(&data->idr);
253*4882a593Smuzhiyun 
254*4882a593Smuzhiyun 	return 0;
255*4882a593Smuzhiyun }
256*4882a593Smuzhiyun 
257*4882a593Smuzhiyun static struct platform_driver ti_sci_reset_driver = {
258*4882a593Smuzhiyun 	.probe = ti_sci_reset_probe,
259*4882a593Smuzhiyun 	.remove = ti_sci_reset_remove,
260*4882a593Smuzhiyun 	.driver = {
261*4882a593Smuzhiyun 		.name = "ti-sci-reset",
262*4882a593Smuzhiyun 		.of_match_table = ti_sci_reset_of_match,
263*4882a593Smuzhiyun 	},
264*4882a593Smuzhiyun };
265*4882a593Smuzhiyun module_platform_driver(ti_sci_reset_driver);
266*4882a593Smuzhiyun 
267*4882a593Smuzhiyun MODULE_AUTHOR("Andrew F. Davis <afd@ti.com>");
268*4882a593Smuzhiyun MODULE_DESCRIPTION("TI System Control Interface (TI SCI) Reset driver");
269*4882a593Smuzhiyun MODULE_LICENSE("GPL v2");
270