3d48a5fa1f2b7b101ae45e1c85bbec1361c600c0
[openwrt/openwrt.git] / target / linux / sunxi / patches-3.12 / 140-add-a31-reset-driver.patch
1 From d8d7a5805579db687fdfdfda4125b43d6a50e616 Mon Sep 17 00:00:00 2001
2 From: Maxime Ripard <maxime.ripard@free-electrons.com>
3 Date: Tue, 24 Sep 2013 11:07:43 +0300
4 Subject: [PATCH] reset: Add Allwinner A31 Reset Controller Driver
5
6 The Allwinner A31 has an IP maintaining a few other IPs in the SoC in
7 reset by default. Among these IPs are the High Speed Timers, hence why
8 we can't use the regular driver construct, and need to call the
9 registering function directly during machine initialisation.
10
11 Signed-off-by: Maxime Ripard <maxime.ripard@free-electrons.com>
12 ---
13 drivers/reset/Makefile | 1 +
14 drivers/reset/reset-sunxi.c | 156 ++++++++++++++++++++++++++++++++++++++++++++
15 2 files changed, 157 insertions(+)
16 create mode 100644 drivers/reset/reset-sunxi.c
17
18 diff --git a/drivers/reset/Makefile b/drivers/reset/Makefile
19 index 1e2d83f..f216b74 100644
20 --- a/drivers/reset/Makefile
21 +++ b/drivers/reset/Makefile
22 @@ -1 +1,2 @@
23 obj-$(CONFIG_RESET_CONTROLLER) += core.o
24 +obj-$(CONFIG_ARCH_SUNXI) += reset-sun6i.o
25 diff --git a/drivers/reset/reset-sunxi.c b/drivers/reset/reset-sunxi.c
26 new file mode 100644
27 index 0000000..5bbd59a
28 --- /dev/null
29 +++ b/drivers/reset/reset-sunxi.c
30 @@ -0,0 +1,156 @@
31 +/*
32 + * Allwinner SoCs Reset Controller driver
33 + *
34 + * Copyright 2013 Maxime Ripard
35 + *
36 + * Maxime Ripard <maxime.ripard@free-electrons.com>
37 + *
38 + * This program is free software; you can redistribute it and/or modify
39 + * it under the terms of the GNU General Public License as published by
40 + * the Free Software Foundation; either version 2 of the License, or
41 + * (at your option) any later version.
42 + */
43 +
44 +#include <linux/err.h>
45 +#include <linux/io.h>
46 +#include <linux/module.h>
47 +#include <linux/of.h>
48 +#include <linux/of_address.h>
49 +#include <linux/platform_device.h>
50 +#include <linux/reset-controller.h>
51 +#include <linux/slab.h>
52 +#include <linux/types.h>
53 +
54 +struct sunxi_reset_data {
55 + void __iomem *membase;
56 + struct reset_controller_dev rcdev;
57 +};
58 +
59 +static int sunxi_reset_assert(struct reset_controller_dev *rcdev,
60 + unsigned long id)
61 +{
62 + struct sunxi_reset_data *data = container_of(rcdev,
63 + struct sunxi_reset_data,
64 + rcdev);
65 + int bank = id / BITS_PER_LONG;
66 + int offset = id % BITS_PER_LONG;
67 + u32 reg = readl(data->membase + (bank * 4));
68 +
69 + writel(reg & ~BIT(offset), data->membase + (bank * 4));
70 +
71 + return 0;
72 +}
73 +
74 +static int sunxi_reset_deassert(struct reset_controller_dev *rcdev,
75 + unsigned long id)
76 +{
77 + struct sunxi_reset_data *data = container_of(rcdev,
78 + struct sunxi_reset_data,
79 + rcdev);
80 + int bank = id / BITS_PER_LONG;
81 + int offset = id % BITS_PER_LONG;
82 + u32 reg = readl(data->membase + (bank * 4));
83 +
84 + writel(reg | BIT(offset), data->membase + (bank * 4));
85 +
86 + return 0;
87 +}
88 +
89 +static struct reset_control_ops sunxi_reset_ops = {
90 + .assert = sunxi_reset_assert,
91 + .deassert = sunxi_reset_deassert,
92 +};
93 +
94 +static int sunxi_reset_init(struct device_node *np)
95 +{
96 + struct sunxi_reset_data *data;
97 + struct resource res;
98 + resource_size_t size;
99 + int ret;
100 +
101 + data = kzalloc(sizeof(*data), GFP_KERNEL);
102 + if (!data)
103 + return -ENOMEM;
104 +
105 + ret = of_address_to_resource(np, 0, &res);
106 + if (ret)
107 + goto err_alloc;
108 +
109 + size = resource_size(&res);
110 + data->membase = ioremap(res.start, size);
111 + if (!data->membase) {
112 + ret = -ENOMEM;
113 + goto err_alloc;
114 + }
115 +
116 + data->rcdev.owner = THIS_MODULE;
117 + data->rcdev.nr_resets = size * 32;
118 + data->rcdev.ops = &sunxi_reset_ops;
119 + data->rcdev.of_node = np;
120 + reset_controller_register(&data->rcdev);
121 +
122 + return 0;
123 +
124 +err_alloc:
125 + kfree(data);
126 + return ret;
127 +};
128 +
129 +/*
130 + * These are the reset controller we need to initialize early on in
131 + * our system, before we can even think of using a regular device
132 + * driver for it.
133 + */
134 +static const struct of_device_id sunxi_early_reset_dt_ids[] __initdata = {
135 + { .compatible = "allwinner,sun6i-a31-ahb1-reset", },
136 + { /* sentinel */ },
137 +};
138 +
139 +void __init sun6i_reset_init(void)
140 +{
141 + struct device_node *np;
142 +
143 + for_each_matching_node(np, sunxi_early_reset_dt_ids)
144 + sunxi_reset_init(np);
145 +}
146 +
147 +/*
148 + * And these are the controllers we can register through the regular
149 + * device model.
150 + */
151 +static const struct of_device_id sunxi_reset_dt_ids[] = {
152 + { .compatible = "allwinner,sun4i-clock-reset", },
153 + { /* sentinel */ },
154 +};
155 +MODULE_DEVICE_TABLE(of, sunxi_reset_dt_ids);
156 +
157 +static int sunxi_reset_probe(struct platform_device *pdev)
158 +{
159 + return sunxi_reset_init(pdev->dev.of_node);
160 +}
161 +
162 +static int sunxi_reset_remove(struct platform_device *pdev)
163 +{
164 + struct sunxi_reset_data *data = platform_get_drvdata(pdev);
165 +
166 + reset_controller_unregister(&data->rcdev);
167 + iounmap(data->membase);
168 + kfree(data);
169 +
170 + return 0;
171 +}
172 +
173 +static struct platform_driver sunxi_reset_driver = {
174 + .probe = sunxi_reset_probe,
175 + .remove = sunxi_reset_remove,
176 + .driver = {
177 + .name = "sunxi-reset",
178 + .owner = THIS_MODULE,
179 + .of_match_table = sunxi_reset_dt_ids,
180 + },
181 +};
182 +module_platform_driver(sunxi_reset_driver);
183 +
184 +MODULE_AUTHOR("Maxime Ripard <maxime.ripard@free-electrons.com");
185 +MODULE_DESCRIPTION("Allwinner SoCs Reset Controller Driver");
186 +MODULE_LICENSE("GPL");
187 --
188 1.8.4
189