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