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
14637cee5fSGabriel Fernandez #include "clk-stm32-core.h"
15637cee5fSGabriel Fernandez
16637cee5fSGabriel Fernandez #define STM32_RESET_ID_MASK GENMASK(15, 0)
17637cee5fSGabriel Fernandez
18637cee5fSGabriel Fernandez struct stm32_reset_data {
19637cee5fSGabriel Fernandez /* reset lock */
20637cee5fSGabriel Fernandez spinlock_t lock;
21637cee5fSGabriel Fernandez struct reset_controller_dev rcdev;
22637cee5fSGabriel Fernandez void __iomem *membase;
23637cee5fSGabriel Fernandez u32 clear_offset;
24637cee5fSGabriel Fernandez };
25637cee5fSGabriel Fernandez
26637cee5fSGabriel Fernandez static inline struct stm32_reset_data *
to_stm32_reset_data(struct reset_controller_dev * rcdev)27637cee5fSGabriel Fernandez to_stm32_reset_data(struct reset_controller_dev *rcdev)
28637cee5fSGabriel Fernandez {
29637cee5fSGabriel Fernandez return container_of(rcdev, struct stm32_reset_data, rcdev);
30637cee5fSGabriel Fernandez }
31637cee5fSGabriel Fernandez
stm32_reset_update(struct reset_controller_dev * rcdev,unsigned long id,bool assert)32637cee5fSGabriel Fernandez static int stm32_reset_update(struct reset_controller_dev *rcdev,
33637cee5fSGabriel Fernandez unsigned long id, bool assert)
34637cee5fSGabriel Fernandez {
35637cee5fSGabriel Fernandez struct stm32_reset_data *data = to_stm32_reset_data(rcdev);
36637cee5fSGabriel Fernandez int reg_width = sizeof(u32);
37637cee5fSGabriel Fernandez int bank = id / (reg_width * BITS_PER_BYTE);
38637cee5fSGabriel Fernandez int offset = id % (reg_width * BITS_PER_BYTE);
39637cee5fSGabriel Fernandez
40637cee5fSGabriel Fernandez if (data->clear_offset) {
41637cee5fSGabriel Fernandez void __iomem *addr;
42637cee5fSGabriel Fernandez
43637cee5fSGabriel Fernandez addr = data->membase + (bank * reg_width);
44637cee5fSGabriel Fernandez if (!assert)
45637cee5fSGabriel Fernandez addr += data->clear_offset;
46637cee5fSGabriel Fernandez
47637cee5fSGabriel Fernandez writel(BIT(offset), addr);
48637cee5fSGabriel Fernandez
49637cee5fSGabriel Fernandez } else {
50637cee5fSGabriel Fernandez unsigned long flags;
51637cee5fSGabriel Fernandez u32 reg;
52637cee5fSGabriel Fernandez
53637cee5fSGabriel Fernandez spin_lock_irqsave(&data->lock, flags);
54637cee5fSGabriel Fernandez
55637cee5fSGabriel Fernandez reg = readl(data->membase + (bank * reg_width));
56637cee5fSGabriel Fernandez
57637cee5fSGabriel Fernandez if (assert)
58637cee5fSGabriel Fernandez reg |= BIT(offset);
59637cee5fSGabriel Fernandez else
60637cee5fSGabriel Fernandez reg &= ~BIT(offset);
61637cee5fSGabriel Fernandez
62637cee5fSGabriel Fernandez writel(reg, data->membase + (bank * reg_width));
63637cee5fSGabriel Fernandez
64637cee5fSGabriel Fernandez spin_unlock_irqrestore(&data->lock, flags);
65637cee5fSGabriel Fernandez }
66637cee5fSGabriel Fernandez
67637cee5fSGabriel Fernandez return 0;
68637cee5fSGabriel Fernandez }
69637cee5fSGabriel Fernandez
stm32_reset_assert(struct reset_controller_dev * rcdev,unsigned long id)70637cee5fSGabriel Fernandez static int stm32_reset_assert(struct reset_controller_dev *rcdev,
71637cee5fSGabriel Fernandez unsigned long id)
72637cee5fSGabriel Fernandez {
73637cee5fSGabriel Fernandez return stm32_reset_update(rcdev, id, true);
74637cee5fSGabriel Fernandez }
75637cee5fSGabriel Fernandez
stm32_reset_deassert(struct reset_controller_dev * rcdev,unsigned long id)76637cee5fSGabriel Fernandez static int stm32_reset_deassert(struct reset_controller_dev *rcdev,
77637cee5fSGabriel Fernandez unsigned long id)
78637cee5fSGabriel Fernandez {
79637cee5fSGabriel Fernandez return stm32_reset_update(rcdev, id, false);
80637cee5fSGabriel Fernandez }
81637cee5fSGabriel Fernandez
stm32_reset_status(struct reset_controller_dev * rcdev,unsigned long id)82637cee5fSGabriel Fernandez static int stm32_reset_status(struct reset_controller_dev *rcdev,
83637cee5fSGabriel Fernandez unsigned long id)
84637cee5fSGabriel Fernandez {
85637cee5fSGabriel Fernandez struct stm32_reset_data *data = to_stm32_reset_data(rcdev);
86637cee5fSGabriel Fernandez int reg_width = sizeof(u32);
87637cee5fSGabriel Fernandez int bank = id / (reg_width * BITS_PER_BYTE);
88637cee5fSGabriel Fernandez int offset = id % (reg_width * BITS_PER_BYTE);
89637cee5fSGabriel Fernandez u32 reg;
90637cee5fSGabriel Fernandez
91637cee5fSGabriel Fernandez reg = readl(data->membase + (bank * reg_width));
92637cee5fSGabriel Fernandez
93637cee5fSGabriel Fernandez return !!(reg & BIT(offset));
94637cee5fSGabriel Fernandez }
95637cee5fSGabriel Fernandez
96637cee5fSGabriel Fernandez static const struct reset_control_ops stm32_reset_ops = {
97637cee5fSGabriel Fernandez .assert = stm32_reset_assert,
98637cee5fSGabriel Fernandez .deassert = stm32_reset_deassert,
99637cee5fSGabriel Fernandez .status = stm32_reset_status,
100637cee5fSGabriel Fernandez };
101637cee5fSGabriel Fernandez
stm32_rcc_reset_init(struct device * dev,const struct of_device_id * match,void __iomem * base)102637cee5fSGabriel Fernandez int stm32_rcc_reset_init(struct device *dev, const struct of_device_id *match,
103637cee5fSGabriel Fernandez void __iomem *base)
104637cee5fSGabriel Fernandez {
105637cee5fSGabriel Fernandez const struct stm32_rcc_match_data *data = match->data;
106637cee5fSGabriel Fernandez struct stm32_reset_data *reset_data = NULL;
107637cee5fSGabriel Fernandez
108637cee5fSGabriel Fernandez data = match->data;
109637cee5fSGabriel Fernandez
110637cee5fSGabriel Fernandez reset_data = kzalloc(sizeof(*reset_data), GFP_KERNEL);
111637cee5fSGabriel Fernandez if (!reset_data)
112637cee5fSGabriel Fernandez return -ENOMEM;
113637cee5fSGabriel Fernandez
114*a1ea0857SWei Yongjun spin_lock_init(&reset_data->lock);
115637cee5fSGabriel Fernandez reset_data->membase = base;
116637cee5fSGabriel Fernandez reset_data->rcdev.owner = THIS_MODULE;
117637cee5fSGabriel Fernandez reset_data->rcdev.ops = &stm32_reset_ops;
118637cee5fSGabriel Fernandez reset_data->rcdev.of_node = dev_of_node(dev);
119637cee5fSGabriel Fernandez reset_data->rcdev.nr_resets = STM32_RESET_ID_MASK;
120637cee5fSGabriel Fernandez reset_data->clear_offset = data->clear_offset;
121637cee5fSGabriel Fernandez
122637cee5fSGabriel Fernandez return reset_controller_register(&reset_data->rcdev);
123637cee5fSGabriel Fernandez }
124