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