xref: /linux/drivers/clk/stm32/reset-stm32.c (revision 30500c2ad9c440d1a81e7a5dac3bcef62e21d910)
1637cee5fSGabriel Fernandez // SPDX-License-Identifier: GPL-2.0
2637cee5fSGabriel Fernandez /*
3637cee5fSGabriel Fernandez  * Copyright (C) STMicroelectronics 2022 - All Rights Reserved
4637cee5fSGabriel Fernandez  * Author: Gabriel Fernandez <gabriel.fernandez@foss.st.com> for STMicroelectronics.
5637cee5fSGabriel Fernandez  */
6637cee5fSGabriel Fernandez 
7637cee5fSGabriel Fernandez #include <linux/of.h>
8637cee5fSGabriel Fernandez #include <linux/platform_device.h>
9637cee5fSGabriel Fernandez #include <linux/regmap.h>
10637cee5fSGabriel Fernandez #include <linux/reset-controller.h>
11637cee5fSGabriel Fernandez #include <linux/slab.h>
12637cee5fSGabriel Fernandez #include <linux/spinlock.h>
13637cee5fSGabriel Fernandez 
14*30500c2aSGabriel Fernandez #include "reset-stm32.h"
15637cee5fSGabriel Fernandez 
16637cee5fSGabriel Fernandez struct stm32_reset_data {
17637cee5fSGabriel Fernandez 	/* reset lock */
18637cee5fSGabriel Fernandez 	spinlock_t			lock;
19637cee5fSGabriel Fernandez 	struct reset_controller_dev	rcdev;
20637cee5fSGabriel Fernandez 	void __iomem			*membase;
21637cee5fSGabriel Fernandez 	u32				clear_offset;
22637cee5fSGabriel Fernandez };
23637cee5fSGabriel Fernandez 
24637cee5fSGabriel Fernandez static inline struct stm32_reset_data *
25637cee5fSGabriel Fernandez to_stm32_reset_data(struct reset_controller_dev *rcdev)
26637cee5fSGabriel Fernandez {
27637cee5fSGabriel Fernandez 	return container_of(rcdev, struct stm32_reset_data, rcdev);
28637cee5fSGabriel Fernandez }
29637cee5fSGabriel Fernandez 
30637cee5fSGabriel Fernandez static int stm32_reset_update(struct reset_controller_dev *rcdev,
31637cee5fSGabriel Fernandez 			      unsigned long id, bool assert)
32637cee5fSGabriel Fernandez {
33637cee5fSGabriel Fernandez 	struct stm32_reset_data *data = to_stm32_reset_data(rcdev);
34637cee5fSGabriel Fernandez 	int reg_width = sizeof(u32);
35637cee5fSGabriel Fernandez 	int bank = id / (reg_width * BITS_PER_BYTE);
36637cee5fSGabriel Fernandez 	int offset = id % (reg_width * BITS_PER_BYTE);
37637cee5fSGabriel Fernandez 
38637cee5fSGabriel Fernandez 	if (data->clear_offset) {
39637cee5fSGabriel Fernandez 		void __iomem *addr;
40637cee5fSGabriel Fernandez 
41637cee5fSGabriel Fernandez 		addr = data->membase + (bank * reg_width);
42637cee5fSGabriel Fernandez 		if (!assert)
43637cee5fSGabriel Fernandez 			addr += data->clear_offset;
44637cee5fSGabriel Fernandez 
45637cee5fSGabriel Fernandez 		writel(BIT(offset), addr);
46637cee5fSGabriel Fernandez 
47637cee5fSGabriel Fernandez 	} else {
48637cee5fSGabriel Fernandez 		unsigned long flags;
49637cee5fSGabriel Fernandez 		u32 reg;
50637cee5fSGabriel Fernandez 
51637cee5fSGabriel Fernandez 		spin_lock_irqsave(&data->lock, flags);
52637cee5fSGabriel Fernandez 
53637cee5fSGabriel Fernandez 		reg = readl(data->membase + (bank * reg_width));
54637cee5fSGabriel Fernandez 
55637cee5fSGabriel Fernandez 		if (assert)
56637cee5fSGabriel Fernandez 			reg |= BIT(offset);
57637cee5fSGabriel Fernandez 		else
58637cee5fSGabriel Fernandez 			reg &= ~BIT(offset);
59637cee5fSGabriel Fernandez 
60637cee5fSGabriel Fernandez 		writel(reg, data->membase + (bank * reg_width));
61637cee5fSGabriel Fernandez 
62637cee5fSGabriel Fernandez 		spin_unlock_irqrestore(&data->lock, flags);
63637cee5fSGabriel Fernandez 	}
64637cee5fSGabriel Fernandez 
65637cee5fSGabriel Fernandez 	return 0;
66637cee5fSGabriel Fernandez }
67637cee5fSGabriel Fernandez 
68637cee5fSGabriel Fernandez static int stm32_reset_assert(struct reset_controller_dev *rcdev,
69637cee5fSGabriel Fernandez 			      unsigned long id)
70637cee5fSGabriel Fernandez {
71637cee5fSGabriel Fernandez 	return stm32_reset_update(rcdev, id, true);
72637cee5fSGabriel Fernandez }
73637cee5fSGabriel Fernandez 
74637cee5fSGabriel Fernandez static int stm32_reset_deassert(struct reset_controller_dev *rcdev,
75637cee5fSGabriel Fernandez 				unsigned long id)
76637cee5fSGabriel Fernandez {
77637cee5fSGabriel Fernandez 	return stm32_reset_update(rcdev, id, false);
78637cee5fSGabriel Fernandez }
79637cee5fSGabriel Fernandez 
80637cee5fSGabriel Fernandez static int stm32_reset_status(struct reset_controller_dev *rcdev,
81637cee5fSGabriel Fernandez 			      unsigned long id)
82637cee5fSGabriel Fernandez {
83637cee5fSGabriel Fernandez 	struct stm32_reset_data *data = to_stm32_reset_data(rcdev);
84637cee5fSGabriel Fernandez 	int reg_width = sizeof(u32);
85637cee5fSGabriel Fernandez 	int bank = id / (reg_width * BITS_PER_BYTE);
86637cee5fSGabriel Fernandez 	int offset = id % (reg_width * BITS_PER_BYTE);
87637cee5fSGabriel Fernandez 	u32 reg;
88637cee5fSGabriel Fernandez 
89637cee5fSGabriel Fernandez 	reg = readl(data->membase + (bank * reg_width));
90637cee5fSGabriel Fernandez 
91637cee5fSGabriel Fernandez 	return !!(reg & BIT(offset));
92637cee5fSGabriel Fernandez }
93637cee5fSGabriel Fernandez 
94637cee5fSGabriel Fernandez static const struct reset_control_ops stm32_reset_ops = {
95637cee5fSGabriel Fernandez 	.assert		= stm32_reset_assert,
96637cee5fSGabriel Fernandez 	.deassert	= stm32_reset_deassert,
97637cee5fSGabriel Fernandez 	.status		= stm32_reset_status,
98637cee5fSGabriel Fernandez };
99637cee5fSGabriel Fernandez 
100*30500c2aSGabriel Fernandez int stm32_rcc_reset_init(struct device *dev, struct clk_stm32_reset_data *data,
101637cee5fSGabriel Fernandez 			 void __iomem *base)
102637cee5fSGabriel Fernandez {
103*30500c2aSGabriel Fernandez 	struct stm32_reset_data *reset_data;
104637cee5fSGabriel Fernandez 
105637cee5fSGabriel Fernandez 	reset_data = kzalloc(sizeof(*reset_data), GFP_KERNEL);
106637cee5fSGabriel Fernandez 	if (!reset_data)
107637cee5fSGabriel Fernandez 		return -ENOMEM;
108637cee5fSGabriel Fernandez 
109a1ea0857SWei Yongjun 	spin_lock_init(&reset_data->lock);
110*30500c2aSGabriel Fernandez 
111637cee5fSGabriel Fernandez 	reset_data->membase = base;
112637cee5fSGabriel Fernandez 	reset_data->rcdev.owner = THIS_MODULE;
113637cee5fSGabriel Fernandez 	reset_data->rcdev.ops = &stm32_reset_ops;
114637cee5fSGabriel Fernandez 	reset_data->rcdev.of_node = dev_of_node(dev);
115*30500c2aSGabriel Fernandez 	reset_data->rcdev.nr_resets = data->nr_lines;
116637cee5fSGabriel Fernandez 	reset_data->clear_offset = data->clear_offset;
117637cee5fSGabriel Fernandez 
118637cee5fSGabriel Fernandez 	return reset_controller_register(&reset_data->rcdev);
119637cee5fSGabriel Fernandez }
120