2 * Allwinner SoCs Reset Controller driver
4 * Copyright 2013 Maxime Ripard
6 * Maxime Ripard <maxime.ripard@free-electrons.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; either version 2 of the License, or
11 * (at your option) any later version.
14 #include <linux/err.h>
16 #include <linux/module.h>
18 #include <linux/of_address.h>
19 #include <linux/platform_device.h>
20 #include <linux/reset-controller.h>
21 #include <linux/slab.h>
22 #include <linux/spinlock.h>
23 #include <linux/types.h>
25 struct sunxi_reset_data {
27 void __iomem *membase;
28 struct reset_controller_dev rcdev;
31 static int sunxi_reset_assert(struct reset_controller_dev *rcdev,
34 struct sunxi_reset_data *data = container_of(rcdev,
35 struct sunxi_reset_data,
37 int bank = id / BITS_PER_LONG;
38 int offset = id % BITS_PER_LONG;
42 spin_lock_irqsave(&data->lock, flags);
44 reg = readl(data->membase + (bank * 4));
45 writel(reg & ~BIT(offset), data->membase + (bank * 4));
47 spin_unlock_irqrestore(&data->lock, flags);
52 static int sunxi_reset_deassert(struct reset_controller_dev *rcdev,
55 struct sunxi_reset_data *data = container_of(rcdev,
56 struct sunxi_reset_data,
58 int bank = id / BITS_PER_LONG;
59 int offset = id % BITS_PER_LONG;
63 spin_lock_irqsave(&data->lock, flags);
65 reg = readl(data->membase + (bank * 4));
66 writel(reg | BIT(offset), data->membase + (bank * 4));
68 spin_unlock_irqrestore(&data->lock, flags);
73 static struct reset_control_ops sunxi_reset_ops = {
74 .assert = sunxi_reset_assert,
75 .deassert = sunxi_reset_deassert,
78 static int sunxi_reset_init(struct device_node *np)
80 struct sunxi_reset_data *data;
85 data = kzalloc(sizeof(*data), GFP_KERNEL);
89 ret = of_address_to_resource(np, 0, &res);
93 size = resource_size(&res);
94 if (!request_mem_region(res.start, size, np->name)) {
99 data->membase = ioremap(res.start, size);
100 if (!data->membase) {
105 data->rcdev.owner = THIS_MODULE;
106 data->rcdev.nr_resets = size * 32;
107 data->rcdev.ops = &sunxi_reset_ops;
108 data->rcdev.of_node = np;
109 reset_controller_register(&data->rcdev);
119 * These are the reset controller we need to initialize early on in
120 * our system, before we can even think of using a regular device
123 static const struct of_device_id sunxi_early_reset_dt_ids[] __initdata = {
124 { .compatible = "allwinner,sun6i-a31-ahb1-reset", },
128 void __init sun6i_reset_init(void)
130 struct device_node *np;
132 for_each_matching_node(np, sunxi_early_reset_dt_ids)
133 sunxi_reset_init(np);
137 * And these are the controllers we can register through the regular
140 static const struct of_device_id sunxi_reset_dt_ids[] = {
141 { .compatible = "allwinner,sun6i-a31-clock-reset", },
144 MODULE_DEVICE_TABLE(of, sunxi_reset_dt_ids);
146 static int sunxi_reset_probe(struct platform_device *pdev)
148 return sunxi_reset_init(pdev->dev.of_node);
151 static int sunxi_reset_remove(struct platform_device *pdev)
153 struct sunxi_reset_data *data = platform_get_drvdata(pdev);
155 reset_controller_unregister(&data->rcdev);
156 iounmap(data->membase);
162 static struct platform_driver sunxi_reset_driver = {
163 .probe = sunxi_reset_probe,
164 .remove = sunxi_reset_remove,
166 .name = "sunxi-reset",
167 .owner = THIS_MODULE,
168 .of_match_table = sunxi_reset_dt_ids,
171 module_platform_driver(sunxi_reset_driver);
173 MODULE_AUTHOR("Maxime Ripard <maxime.ripard@free-electrons.com");
174 MODULE_DESCRIPTION("Allwinner SoCs Reset Controller Driver");
175 MODULE_LICENSE("GPL");