2 * Copyright (C) Maxime Coquelin 2015
3 * Author: Maxime Coquelin <mcoquelin.stm32@gmail.com>
4 * License terms: GNU General Public License (GPL), version 2
6 * Heavily based on sunxi driver from Maxime Ripard.
12 #include <linux/of_address.h>
13 #include <linux/platform_device.h>
14 #include <linux/reset-controller.h>
15 #include <linux/slab.h>
16 #include <linux/spinlock.h>
17 #include <linux/types.h>
19 struct stm32_reset_data
{
21 void __iomem
*membase
;
22 struct reset_controller_dev rcdev
;
25 static int stm32_reset_assert(struct reset_controller_dev
*rcdev
,
28 struct stm32_reset_data
*data
= container_of(rcdev
,
29 struct stm32_reset_data
,
31 int bank
= id
/ BITS_PER_LONG
;
32 int offset
= id
% BITS_PER_LONG
;
36 spin_lock_irqsave(&data
->lock
, flags
);
38 reg
= readl(data
->membase
+ (bank
* 4));
39 writel(reg
| BIT(offset
), data
->membase
+ (bank
* 4));
41 spin_unlock_irqrestore(&data
->lock
, flags
);
46 static int stm32_reset_deassert(struct reset_controller_dev
*rcdev
,
49 struct stm32_reset_data
*data
= container_of(rcdev
,
50 struct stm32_reset_data
,
52 int bank
= id
/ BITS_PER_LONG
;
53 int offset
= id
% BITS_PER_LONG
;
57 spin_lock_irqsave(&data
->lock
, flags
);
59 reg
= readl(data
->membase
+ (bank
* 4));
60 writel(reg
& ~BIT(offset
), data
->membase
+ (bank
* 4));
62 spin_unlock_irqrestore(&data
->lock
, flags
);
67 static const struct reset_control_ops stm32_reset_ops
= {
68 .assert = stm32_reset_assert
,
69 .deassert
= stm32_reset_deassert
,
72 static const struct of_device_id stm32_reset_dt_ids
[] = {
73 { .compatible
= "st,stm32-rcc", },
77 static int stm32_reset_probe(struct platform_device
*pdev
)
79 struct stm32_reset_data
*data
;
82 data
= devm_kzalloc(&pdev
->dev
, sizeof(*data
), GFP_KERNEL
);
86 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
87 data
->membase
= devm_ioremap_resource(&pdev
->dev
, res
);
88 if (IS_ERR(data
->membase
))
89 return PTR_ERR(data
->membase
);
91 spin_lock_init(&data
->lock
);
93 data
->rcdev
.owner
= THIS_MODULE
;
94 data
->rcdev
.nr_resets
= resource_size(res
) * 8;
95 data
->rcdev
.ops
= &stm32_reset_ops
;
96 data
->rcdev
.of_node
= pdev
->dev
.of_node
;
98 return devm_reset_controller_register(&pdev
->dev
, &data
->rcdev
);
101 static struct platform_driver stm32_reset_driver
= {
102 .probe
= stm32_reset_probe
,
104 .name
= "stm32-rcc-reset",
105 .of_match_table
= stm32_reset_dt_ids
,
108 builtin_platform_driver(stm32_reset_driver
);