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/init.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 reg_width
= sizeof(u32
);
38 int bank
= id
/ (reg_width
* BITS_PER_BYTE
);
39 int offset
= id
% (reg_width
* BITS_PER_BYTE
);
43 spin_lock_irqsave(&data
->lock
, flags
);
45 reg
= readl(data
->membase
+ (bank
* reg_width
));
46 writel(reg
& ~BIT(offset
), data
->membase
+ (bank
* reg_width
));
48 spin_unlock_irqrestore(&data
->lock
, flags
);
53 static int sunxi_reset_deassert(struct reset_controller_dev
*rcdev
,
56 struct sunxi_reset_data
*data
= container_of(rcdev
,
57 struct sunxi_reset_data
,
59 int reg_width
= sizeof(u32
);
60 int bank
= id
/ (reg_width
* BITS_PER_BYTE
);
61 int offset
= id
% (reg_width
* BITS_PER_BYTE
);
65 spin_lock_irqsave(&data
->lock
, flags
);
67 reg
= readl(data
->membase
+ (bank
* reg_width
));
68 writel(reg
| BIT(offset
), data
->membase
+ (bank
* reg_width
));
70 spin_unlock_irqrestore(&data
->lock
, flags
);
75 static const struct reset_control_ops sunxi_reset_ops
= {
76 .assert = sunxi_reset_assert
,
77 .deassert
= sunxi_reset_deassert
,
80 static int sunxi_reset_init(struct device_node
*np
)
82 struct sunxi_reset_data
*data
;
87 data
= kzalloc(sizeof(*data
), GFP_KERNEL
);
91 ret
= of_address_to_resource(np
, 0, &res
);
95 size
= resource_size(&res
);
96 if (!request_mem_region(res
.start
, size
, np
->name
)) {
101 data
->membase
= ioremap(res
.start
, size
);
102 if (!data
->membase
) {
107 spin_lock_init(&data
->lock
);
109 data
->rcdev
.owner
= THIS_MODULE
;
110 data
->rcdev
.nr_resets
= size
* 8;
111 data
->rcdev
.ops
= &sunxi_reset_ops
;
112 data
->rcdev
.of_node
= np
;
114 return reset_controller_register(&data
->rcdev
);
122 * These are the reset controller we need to initialize early on in
123 * our system, before we can even think of using a regular device
126 static const struct of_device_id sunxi_early_reset_dt_ids
[] __initconst
= {
127 { .compatible
= "allwinner,sun6i-a31-ahb1-reset", },
131 void __init
sun6i_reset_init(void)
133 struct device_node
*np
;
135 for_each_matching_node(np
, sunxi_early_reset_dt_ids
)
136 sunxi_reset_init(np
);
140 * And these are the controllers we can register through the regular
143 static const struct of_device_id sunxi_reset_dt_ids
[] = {
144 { .compatible
= "allwinner,sun6i-a31-clock-reset", },
148 static int sunxi_reset_probe(struct platform_device
*pdev
)
150 struct sunxi_reset_data
*data
;
151 struct resource
*res
;
153 data
= devm_kzalloc(&pdev
->dev
, sizeof(*data
), GFP_KERNEL
);
157 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
158 data
->membase
= devm_ioremap_resource(&pdev
->dev
, res
);
159 if (IS_ERR(data
->membase
))
160 return PTR_ERR(data
->membase
);
162 spin_lock_init(&data
->lock
);
164 data
->rcdev
.owner
= THIS_MODULE
;
165 data
->rcdev
.nr_resets
= resource_size(res
) * 8;
166 data
->rcdev
.ops
= &sunxi_reset_ops
;
167 data
->rcdev
.of_node
= pdev
->dev
.of_node
;
169 return devm_reset_controller_register(&pdev
->dev
, &data
->rcdev
);
172 static struct platform_driver sunxi_reset_driver
= {
173 .probe
= sunxi_reset_probe
,
175 .name
= "sunxi-reset",
176 .of_match_table
= sunxi_reset_dt_ids
,
179 builtin_platform_driver(sunxi_reset_driver
);