xref: /linux/drivers/clk/stm32/reset-stm32.c (revision 637cee5ffc71698eecf014a794e8d24a213b3c07)
1*637cee5fSGabriel Fernandez // SPDX-License-Identifier: GPL-2.0
2*637cee5fSGabriel Fernandez /*
3*637cee5fSGabriel Fernandez  * Copyright (C) STMicroelectronics 2022 - All Rights Reserved
4*637cee5fSGabriel Fernandez  * Author: Gabriel Fernandez <gabriel.fernandez@foss.st.com> for STMicroelectronics.
5*637cee5fSGabriel Fernandez  */
6*637cee5fSGabriel Fernandez 
7*637cee5fSGabriel Fernandez #include <linux/of.h>
8*637cee5fSGabriel Fernandez #include <linux/platform_device.h>
9*637cee5fSGabriel Fernandez #include <linux/regmap.h>
10*637cee5fSGabriel Fernandez #include <linux/reset-controller.h>
11*637cee5fSGabriel Fernandez #include <linux/slab.h>
12*637cee5fSGabriel Fernandez #include <linux/spinlock.h>
13*637cee5fSGabriel Fernandez 
14*637cee5fSGabriel Fernandez #include "clk-stm32-core.h"
15*637cee5fSGabriel Fernandez 
16*637cee5fSGabriel Fernandez #define STM32_RESET_ID_MASK GENMASK(15, 0)
17*637cee5fSGabriel Fernandez 
18*637cee5fSGabriel Fernandez struct stm32_reset_data {
19*637cee5fSGabriel Fernandez 	/* reset lock */
20*637cee5fSGabriel Fernandez 	spinlock_t			lock;
21*637cee5fSGabriel Fernandez 	struct reset_controller_dev	rcdev;
22*637cee5fSGabriel Fernandez 	void __iomem			*membase;
23*637cee5fSGabriel Fernandez 	u32				clear_offset;
24*637cee5fSGabriel Fernandez };
25*637cee5fSGabriel Fernandez 
26*637cee5fSGabriel Fernandez static inline struct stm32_reset_data *
27*637cee5fSGabriel Fernandez to_stm32_reset_data(struct reset_controller_dev *rcdev)
28*637cee5fSGabriel Fernandez {
29*637cee5fSGabriel Fernandez 	return container_of(rcdev, struct stm32_reset_data, rcdev);
30*637cee5fSGabriel Fernandez }
31*637cee5fSGabriel Fernandez 
32*637cee5fSGabriel Fernandez static int stm32_reset_update(struct reset_controller_dev *rcdev,
33*637cee5fSGabriel Fernandez 			      unsigned long id, bool assert)
34*637cee5fSGabriel Fernandez {
35*637cee5fSGabriel Fernandez 	struct stm32_reset_data *data = to_stm32_reset_data(rcdev);
36*637cee5fSGabriel Fernandez 	int reg_width = sizeof(u32);
37*637cee5fSGabriel Fernandez 	int bank = id / (reg_width * BITS_PER_BYTE);
38*637cee5fSGabriel Fernandez 	int offset = id % (reg_width * BITS_PER_BYTE);
39*637cee5fSGabriel Fernandez 
40*637cee5fSGabriel Fernandez 	if (data->clear_offset) {
41*637cee5fSGabriel Fernandez 		void __iomem *addr;
42*637cee5fSGabriel Fernandez 
43*637cee5fSGabriel Fernandez 		addr = data->membase + (bank * reg_width);
44*637cee5fSGabriel Fernandez 		if (!assert)
45*637cee5fSGabriel Fernandez 			addr += data->clear_offset;
46*637cee5fSGabriel Fernandez 
47*637cee5fSGabriel Fernandez 		writel(BIT(offset), addr);
48*637cee5fSGabriel Fernandez 
49*637cee5fSGabriel Fernandez 	} else {
50*637cee5fSGabriel Fernandez 		unsigned long flags;
51*637cee5fSGabriel Fernandez 		u32 reg;
52*637cee5fSGabriel Fernandez 
53*637cee5fSGabriel Fernandez 		spin_lock_irqsave(&data->lock, flags);
54*637cee5fSGabriel Fernandez 
55*637cee5fSGabriel Fernandez 		reg = readl(data->membase + (bank * reg_width));
56*637cee5fSGabriel Fernandez 
57*637cee5fSGabriel Fernandez 		if (assert)
58*637cee5fSGabriel Fernandez 			reg |= BIT(offset);
59*637cee5fSGabriel Fernandez 		else
60*637cee5fSGabriel Fernandez 			reg &= ~BIT(offset);
61*637cee5fSGabriel Fernandez 
62*637cee5fSGabriel Fernandez 		writel(reg, data->membase + (bank * reg_width));
63*637cee5fSGabriel Fernandez 
64*637cee5fSGabriel Fernandez 		spin_unlock_irqrestore(&data->lock, flags);
65*637cee5fSGabriel Fernandez 	}
66*637cee5fSGabriel Fernandez 
67*637cee5fSGabriel Fernandez 	return 0;
68*637cee5fSGabriel Fernandez }
69*637cee5fSGabriel Fernandez 
70*637cee5fSGabriel Fernandez static int stm32_reset_assert(struct reset_controller_dev *rcdev,
71*637cee5fSGabriel Fernandez 			      unsigned long id)
72*637cee5fSGabriel Fernandez {
73*637cee5fSGabriel Fernandez 	return stm32_reset_update(rcdev, id, true);
74*637cee5fSGabriel Fernandez }
75*637cee5fSGabriel Fernandez 
76*637cee5fSGabriel Fernandez static int stm32_reset_deassert(struct reset_controller_dev *rcdev,
77*637cee5fSGabriel Fernandez 				unsigned long id)
78*637cee5fSGabriel Fernandez {
79*637cee5fSGabriel Fernandez 	return stm32_reset_update(rcdev, id, false);
80*637cee5fSGabriel Fernandez }
81*637cee5fSGabriel Fernandez 
82*637cee5fSGabriel Fernandez static int stm32_reset_status(struct reset_controller_dev *rcdev,
83*637cee5fSGabriel Fernandez 			      unsigned long id)
84*637cee5fSGabriel Fernandez {
85*637cee5fSGabriel Fernandez 	struct stm32_reset_data *data = to_stm32_reset_data(rcdev);
86*637cee5fSGabriel Fernandez 	int reg_width = sizeof(u32);
87*637cee5fSGabriel Fernandez 	int bank = id / (reg_width * BITS_PER_BYTE);
88*637cee5fSGabriel Fernandez 	int offset = id % (reg_width * BITS_PER_BYTE);
89*637cee5fSGabriel Fernandez 	u32 reg;
90*637cee5fSGabriel Fernandez 
91*637cee5fSGabriel Fernandez 	reg = readl(data->membase + (bank * reg_width));
92*637cee5fSGabriel Fernandez 
93*637cee5fSGabriel Fernandez 	return !!(reg & BIT(offset));
94*637cee5fSGabriel Fernandez }
95*637cee5fSGabriel Fernandez 
96*637cee5fSGabriel Fernandez static const struct reset_control_ops stm32_reset_ops = {
97*637cee5fSGabriel Fernandez 	.assert		= stm32_reset_assert,
98*637cee5fSGabriel Fernandez 	.deassert	= stm32_reset_deassert,
99*637cee5fSGabriel Fernandez 	.status		= stm32_reset_status,
100*637cee5fSGabriel Fernandez };
101*637cee5fSGabriel Fernandez 
102*637cee5fSGabriel Fernandez int stm32_rcc_reset_init(struct device *dev, const struct of_device_id *match,
103*637cee5fSGabriel Fernandez 			 void __iomem *base)
104*637cee5fSGabriel Fernandez {
105*637cee5fSGabriel Fernandez 	const struct stm32_rcc_match_data *data = match->data;
106*637cee5fSGabriel Fernandez 	struct stm32_reset_data *reset_data = NULL;
107*637cee5fSGabriel Fernandez 
108*637cee5fSGabriel Fernandez 	data = match->data;
109*637cee5fSGabriel Fernandez 
110*637cee5fSGabriel Fernandez 	reset_data = kzalloc(sizeof(*reset_data), GFP_KERNEL);
111*637cee5fSGabriel Fernandez 	if (!reset_data)
112*637cee5fSGabriel Fernandez 		return -ENOMEM;
113*637cee5fSGabriel Fernandez 
114*637cee5fSGabriel Fernandez 	reset_data->membase = base;
115*637cee5fSGabriel Fernandez 	reset_data->rcdev.owner = THIS_MODULE;
116*637cee5fSGabriel Fernandez 	reset_data->rcdev.ops = &stm32_reset_ops;
117*637cee5fSGabriel Fernandez 	reset_data->rcdev.of_node = dev_of_node(dev);
118*637cee5fSGabriel Fernandez 	reset_data->rcdev.nr_resets = STM32_RESET_ID_MASK;
119*637cee5fSGabriel Fernandez 	reset_data->clear_offset = data->clear_offset;
120*637cee5fSGabriel Fernandez 
121*637cee5fSGabriel Fernandez 	return reset_controller_register(&reset_data->rcdev);
122*637cee5fSGabriel Fernandez }
123