0001
0002
0003
0004
0005
0006
0007
0008 #include <linux/kernel.h>
0009 #include <linux/platform_device.h>
0010 #include <linux/module.h>
0011 #include <linux/err.h>
0012 #include <linux/types.h>
0013 #include <linux/of_device.h>
0014 #include <linux/regmap.h>
0015 #include <linux/mfd/syscon.h>
0016
0017 #include "reset-syscfg.h"
0018
0019
0020
0021
0022
0023
0024
0025 struct syscfg_reset_channel {
0026 struct regmap_field *reset;
0027 struct regmap_field *ack;
0028 };
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040 struct syscfg_reset_controller {
0041 struct reset_controller_dev rst;
0042 bool active_low;
0043 struct syscfg_reset_channel *channels;
0044 };
0045
0046 #define to_syscfg_reset_controller(_rst) \
0047 container_of(_rst, struct syscfg_reset_controller, rst)
0048
0049 static int syscfg_reset_program_hw(struct reset_controller_dev *rcdev,
0050 unsigned long idx, int assert)
0051 {
0052 struct syscfg_reset_controller *rst = to_syscfg_reset_controller(rcdev);
0053 const struct syscfg_reset_channel *ch;
0054 u32 ctrl_val = rst->active_low ? !assert : !!assert;
0055 int err;
0056
0057 if (idx >= rcdev->nr_resets)
0058 return -EINVAL;
0059
0060 ch = &rst->channels[idx];
0061
0062 err = regmap_field_write(ch->reset, ctrl_val);
0063 if (err)
0064 return err;
0065
0066 if (ch->ack) {
0067 unsigned long timeout = jiffies + msecs_to_jiffies(1000);
0068 u32 ack_val;
0069
0070 while (true) {
0071 err = regmap_field_read(ch->ack, &ack_val);
0072 if (err)
0073 return err;
0074
0075 if (ack_val == ctrl_val)
0076 break;
0077
0078 if (time_after(jiffies, timeout))
0079 return -ETIME;
0080
0081 cpu_relax();
0082 }
0083 }
0084
0085 return 0;
0086 }
0087
0088 static int syscfg_reset_assert(struct reset_controller_dev *rcdev,
0089 unsigned long idx)
0090 {
0091 return syscfg_reset_program_hw(rcdev, idx, true);
0092 }
0093
0094 static int syscfg_reset_deassert(struct reset_controller_dev *rcdev,
0095 unsigned long idx)
0096 {
0097 return syscfg_reset_program_hw(rcdev, idx, false);
0098 }
0099
0100 static int syscfg_reset_dev(struct reset_controller_dev *rcdev,
0101 unsigned long idx)
0102 {
0103 int err;
0104
0105 err = syscfg_reset_assert(rcdev, idx);
0106 if (err)
0107 return err;
0108
0109 return syscfg_reset_deassert(rcdev, idx);
0110 }
0111
0112 static int syscfg_reset_status(struct reset_controller_dev *rcdev,
0113 unsigned long idx)
0114 {
0115 struct syscfg_reset_controller *rst = to_syscfg_reset_controller(rcdev);
0116 const struct syscfg_reset_channel *ch;
0117 u32 ret_val = 0;
0118 int err;
0119
0120 if (idx >= rcdev->nr_resets)
0121 return -EINVAL;
0122
0123 ch = &rst->channels[idx];
0124 if (ch->ack)
0125 err = regmap_field_read(ch->ack, &ret_val);
0126 else
0127 err = regmap_field_read(ch->reset, &ret_val);
0128 if (err)
0129 return err;
0130
0131 return rst->active_low ? !ret_val : !!ret_val;
0132 }
0133
0134 static const struct reset_control_ops syscfg_reset_ops = {
0135 .reset = syscfg_reset_dev,
0136 .assert = syscfg_reset_assert,
0137 .deassert = syscfg_reset_deassert,
0138 .status = syscfg_reset_status,
0139 };
0140
0141 static int syscfg_reset_controller_register(struct device *dev,
0142 const struct syscfg_reset_controller_data *data)
0143 {
0144 struct syscfg_reset_controller *rc;
0145 int i, err;
0146
0147 rc = devm_kzalloc(dev, sizeof(*rc), GFP_KERNEL);
0148 if (!rc)
0149 return -ENOMEM;
0150
0151 rc->channels = devm_kcalloc(dev, data->nr_channels,
0152 sizeof(*rc->channels), GFP_KERNEL);
0153 if (!rc->channels)
0154 return -ENOMEM;
0155
0156 rc->rst.ops = &syscfg_reset_ops;
0157 rc->rst.of_node = dev->of_node;
0158 rc->rst.nr_resets = data->nr_channels;
0159 rc->active_low = data->active_low;
0160
0161 for (i = 0; i < data->nr_channels; i++) {
0162 struct regmap *map;
0163 struct regmap_field *f;
0164 const char *compatible = data->channels[i].compatible;
0165
0166 map = syscon_regmap_lookup_by_compatible(compatible);
0167 if (IS_ERR(map))
0168 return PTR_ERR(map);
0169
0170 f = devm_regmap_field_alloc(dev, map, data->channels[i].reset);
0171 if (IS_ERR(f))
0172 return PTR_ERR(f);
0173
0174 rc->channels[i].reset = f;
0175
0176 if (!data->wait_for_ack)
0177 continue;
0178
0179 f = devm_regmap_field_alloc(dev, map, data->channels[i].ack);
0180 if (IS_ERR(f))
0181 return PTR_ERR(f);
0182
0183 rc->channels[i].ack = f;
0184 }
0185
0186 err = reset_controller_register(&rc->rst);
0187 if (!err)
0188 dev_info(dev, "registered\n");
0189
0190 return err;
0191 }
0192
0193 int syscfg_reset_probe(struct platform_device *pdev)
0194 {
0195 struct device *dev = pdev ? &pdev->dev : NULL;
0196 const struct of_device_id *match;
0197
0198 if (!dev || !dev->driver)
0199 return -ENODEV;
0200
0201 match = of_match_device(dev->driver->of_match_table, dev);
0202 if (!match || !match->data)
0203 return -EINVAL;
0204
0205 return syscfg_reset_controller_register(dev, match->data);
0206 }