Linux 4.19.133
[linux/fpc-iii.git] / drivers / reset / reset-zynq.c
blob87a4e355578f2cde0784d19a85fc73a28e415e9b
1 /*
2 * Copyright (c) 2015, National Instruments Corp.
4 * Xilinx Zynq Reset controller driver
6 * Author: Moritz Fischer <moritz.fischer@ettus.com>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; version 2 of the License.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
18 #include <linux/err.h>
19 #include <linux/io.h>
20 #include <linux/init.h>
21 #include <linux/mfd/syscon.h>
22 #include <linux/of.h>
23 #include <linux/platform_device.h>
24 #include <linux/reset-controller.h>
25 #include <linux/regmap.h>
26 #include <linux/types.h>
28 struct zynq_reset_data {
29 struct regmap *slcr;
30 struct reset_controller_dev rcdev;
31 u32 offset;
34 #define to_zynq_reset_data(p) \
35 container_of((p), struct zynq_reset_data, rcdev)
37 static int zynq_reset_assert(struct reset_controller_dev *rcdev,
38 unsigned long id)
40 struct zynq_reset_data *priv = to_zynq_reset_data(rcdev);
42 int bank = id / BITS_PER_LONG;
43 int offset = id % BITS_PER_LONG;
45 pr_debug("%s: %s reset bank %u offset %u\n", KBUILD_MODNAME, __func__,
46 bank, offset);
48 return regmap_update_bits(priv->slcr,
49 priv->offset + (bank * 4),
50 BIT(offset),
51 BIT(offset));
54 static int zynq_reset_deassert(struct reset_controller_dev *rcdev,
55 unsigned long id)
57 struct zynq_reset_data *priv = to_zynq_reset_data(rcdev);
59 int bank = id / BITS_PER_LONG;
60 int offset = id % BITS_PER_LONG;
62 pr_debug("%s: %s reset bank %u offset %u\n", KBUILD_MODNAME, __func__,
63 bank, offset);
65 return regmap_update_bits(priv->slcr,
66 priv->offset + (bank * 4),
67 BIT(offset),
68 ~BIT(offset));
71 static int zynq_reset_status(struct reset_controller_dev *rcdev,
72 unsigned long id)
74 struct zynq_reset_data *priv = to_zynq_reset_data(rcdev);
76 int bank = id / BITS_PER_LONG;
77 int offset = id % BITS_PER_LONG;
78 int ret;
79 u32 reg;
81 pr_debug("%s: %s reset bank %u offset %u\n", KBUILD_MODNAME, __func__,
82 bank, offset);
84 ret = regmap_read(priv->slcr, priv->offset + (bank * 4), &reg);
85 if (ret)
86 return ret;
88 return !!(reg & BIT(offset));
91 static const struct reset_control_ops zynq_reset_ops = {
92 .assert = zynq_reset_assert,
93 .deassert = zynq_reset_deassert,
94 .status = zynq_reset_status,
97 static int zynq_reset_probe(struct platform_device *pdev)
99 struct resource *res;
100 struct zynq_reset_data *priv;
102 priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
103 if (!priv)
104 return -ENOMEM;
105 platform_set_drvdata(pdev, priv);
107 priv->slcr = syscon_regmap_lookup_by_phandle(pdev->dev.of_node,
108 "syscon");
109 if (IS_ERR(priv->slcr)) {
110 dev_err(&pdev->dev, "unable to get zynq-slcr regmap");
111 return PTR_ERR(priv->slcr);
114 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
115 if (!res) {
116 dev_err(&pdev->dev, "missing IO resource\n");
117 return -ENODEV;
120 priv->offset = res->start;
122 priv->rcdev.owner = THIS_MODULE;
123 priv->rcdev.nr_resets = resource_size(res) / 4 * BITS_PER_LONG;
124 priv->rcdev.ops = &zynq_reset_ops;
125 priv->rcdev.of_node = pdev->dev.of_node;
127 return devm_reset_controller_register(&pdev->dev, &priv->rcdev);
130 static const struct of_device_id zynq_reset_dt_ids[] = {
131 { .compatible = "xlnx,zynq-reset", },
132 { /* sentinel */ },
135 static struct platform_driver zynq_reset_driver = {
136 .probe = zynq_reset_probe,
137 .driver = {
138 .name = KBUILD_MODNAME,
139 .of_match_table = zynq_reset_dt_ids,
142 builtin_platform_driver(zynq_reset_driver);