blob: 5cc6bae79c3fb28f2a4fc59efa6c728952b1a226 [file] [log] [blame]
Per Åstrand9f36f2e2021-09-30 09:57:34 +02001/*
2 * Copyright (c) 2021 Arm Limited. All rights reserved.
3 *
4 * This program is free software and is provided to you under the terms of the
5 * GNU General Public License version 2 as published by the Free Software
6 * Foundation, and any use by you of this program is subject to the terms
7 * of such GNU licence.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, you can access it online at
16 * http://www.gnu.org/licenses/gpl-2.0.html.
17 *
18 * SPDX-License-Identifier: GPL-2.0-only
19 */
20
21#include <linux/io.h>
22#include <linux/kernel.h>
23#include <linux/module.h>
24#include <linux/of.h>
25#include <linux/of_device.h>
26#include <linux/platform_device.h>
27#include <linux/reset-controller.h>
28
29#define JUNO_FPGA_RESET_DRIVER_VERSION "0.0.1"
30
31struct juno_fpga_reset {
32 struct reset_controller_dev rst;
33 struct device *dev;
34 void __iomem *base;
35};
36
37#define JUNO_FPGA_RESET_ID(base) (base)
38#define JUNO_FPGA_RESET_SOFT_RESET(base) ((base) + 0x140)
39#define JUNO_FPGA_RESET_CPU_WAIT(base) ((base) + 0x144)
40
41#define JUNO_FPGA_RESET_SET_RESET (0x1)
42#define JUNO_FPGA_RESET_UNSET_RESET (0x0)
43#define JUNO_FPGA_RESET_SET_CPUWAIT (0x1)
44#define JUNO_FPGA_RESET_UNSET_CPUWAIT (0x0)
45
46static void __iomem *verify_and_remap(struct device *dev,
47 struct resource *res)
48{
49 void __iomem *base = devm_ioremap_resource(dev, res);
50 u32 id;
51
52 if (IS_ERR(base))
53 return base;
54
55 id = readl(JUNO_FPGA_RESET_ID(base));
56
57 if (id != 0x2010f &&
58 id != 0x20110 &&
59 id != 0x20111) {
60 return IOMEM_ERR_PTR(-EINVAL);
61 }
62
63 return base;
64}
65
66int juno_fpga_reset_assert(struct reset_controller_dev *rcdev,
67 unsigned long id)
68{
69 struct juno_fpga_reset *reset = container_of(rcdev, struct juno_fpga_reset,
70 rst);
71
72 /* pull reset */
73 dev_dbg(reset->dev, "Asserting reset");
74
75 /* set wait and reset */
76 writel(JUNO_FPGA_RESET_SET_RESET,
77 JUNO_FPGA_RESET_SOFT_RESET(reset->base));
78 writel(JUNO_FPGA_RESET_SET_CPUWAIT,
79 JUNO_FPGA_RESET_CPU_WAIT(reset->base));
80
81 writel(JUNO_FPGA_RESET_UNSET_RESET,
82 JUNO_FPGA_RESET_SOFT_RESET(reset->base));
83 return 0;
84}
85
86int juno_fpga_reset_deassert(struct reset_controller_dev *rcdev,
87 unsigned long id)
88{
89 struct juno_fpga_reset *reset = container_of(rcdev, struct juno_fpga_reset,
90 rst);
91
92 /* release wait */
93 dev_dbg(reset->dev, "Deasserting reset");
94
95 writel(JUNO_FPGA_RESET_UNSET_CPUWAIT,
96 JUNO_FPGA_RESET_CPU_WAIT(reset->base));
97 return 0;
98}
99
100static struct reset_control_ops juno_fpga_reset_ops = {
101 .assert = juno_fpga_reset_assert,
102 .deassert = juno_fpga_reset_deassert,
103};
104
105static const struct of_device_id juno_fpga_reset_match[] = {
106 { .compatible = "arm,mali_fpga_sysctl", .data = 0 },
107 { /* sentinel */ },
108};
109
110static int juno_fpga_reset_probe(struct platform_device *pdev)
111{
112 struct device *dev = &pdev->dev;
113 struct juno_fpga_reset *reset;
114 struct resource *res;
115
116 reset = devm_kzalloc(&pdev->dev, sizeof(*reset), GFP_KERNEL);
117 if (!reset)
118 return -ENOMEM;
119
120 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
121
122 reset->base = verify_and_remap(dev, res);
123 reset->dev = dev;
124
125 if (IS_ERR(reset->base))
126 return PTR_ERR(reset->base);
127
128 platform_set_drvdata(pdev, reset);
129
130 reset->rst.owner = THIS_MODULE;
131 reset->rst.nr_resets = 1;
132 reset->rst.ops = &juno_fpga_reset_ops;
133 reset->rst.of_node = pdev->dev.of_node;
134
135 dev_dbg(dev, "registering to reset controller core");
136
137 return devm_reset_controller_register(dev, &reset->rst);
138}
139
140static int juno_fpga_reset_remove(struct platform_device *pdev)
141{
142 return 0;
143}
144
145static struct platform_driver juno_fpga_reset_driver = {
146 .probe = juno_fpga_reset_probe,
147 .remove = juno_fpga_reset_remove,
148 .driver = {
149 .name = "juno-fpga-reset",
150 .of_match_table = of_match_ptr(juno_fpga_reset_match),
151 },
152};
153
154module_platform_driver(juno_fpga_reset_driver);
155
156MODULE_LICENSE("GPL v2");
157MODULE_AUTHOR("Arm Ltd");
158MODULE_DESCRIPTION("Arm Juno FPGA Reset Driver");
159MODULE_VERSION(JUNO_FPGA_RESET_DRIVER_VERSION);