1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
|
/*
* Copyright (c) 2006-2022, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2022-3-08 GuEe-GUI the first version
*/
#include "clk-rk-cpu.h"
static rt_ubase_t rockchip_cpu_clk_recalc_rate(struct rt_clk_cell *cell, rt_ubase_t parent_rate)
{
void *base;
rt_uint32_t clksel0;
struct rockchip_cpu_clk_cell *cpu_clk_cell = cell_to_rockchip_cpu_cell(cell);
const struct rockchip_cpu_clk_reg_data *reg_data = cpu_clk_cell->reg_data;
base = cpu_clk_cell->rk_cell.provider->reg_base;
clksel0 = HWREG32(base + reg_data->core_reg[0]);
clksel0 >>= reg_data->div_core_shift[0];
clksel0 &= reg_data->div_core_mask[0];
return parent_rate / (clksel0 + 1);
}
const struct rt_clk_ops rockchip_cpu_clk_ops =
{
.recalc_rate = rockchip_cpu_clk_recalc_rate,
};
static const struct rockchip_cpu_clk_rate_table *rockchip_get_cpu_clk_settings(
struct rockchip_cpu_clk_cell *cpu_clk_cell, rt_ubase_t rate)
{
const struct rockchip_cpu_clk_rate_table *rate_table = cpu_clk_cell->rate_table;
for (int i = 0; i < cpu_clk_cell->rate_count; ++i)
{
if (rate == rate_table[i].prate)
{
return &rate_table[i];
}
}
return RT_NULL;
}
static void rockchip_cpu_clk_set_dividers(struct rockchip_cpu_clk_cell *cpu_clk_cell,
const struct rockchip_cpu_clk_rate_table *rate)
{
void *base = cpu_clk_cell->rk_cell.provider->reg_base;
/* Alternate parent is active now. set the dividers */
for (int i = 0; i < RT_ARRAY_SIZE(rate->divs); ++i)
{
const struct rockchip_cpu_clk_clksel *clksel = &rate->divs[i];
if (!clksel->reg)
{
continue;
}
HWREG32(base + clksel->reg) = clksel->val;
}
}
static void rockchip_cpu_clk_set_pre_muxs(struct rockchip_cpu_clk_cell *cpu_clk_cell,
const struct rockchip_cpu_clk_rate_table *rate)
{
void *base = cpu_clk_cell->rk_cell.provider->reg_base;
/* Alternate parent is active now. set the pre_muxs */
for (int i = 0; i < RT_ARRAY_SIZE(rate->pre_muxs); ++i)
{
const struct rockchip_cpu_clk_clksel *clksel = &rate->pre_muxs[i];
if (!clksel->reg)
{
break;
}
HWREG32(base + clksel->reg) = clksel->val;
}
}
static void rockchip_cpu_clk_set_post_muxs(struct rockchip_cpu_clk_cell *cpu_clk_cell,
const struct rockchip_cpu_clk_rate_table *rate)
{
void *base = cpu_clk_cell->rk_cell.provider->reg_base;
/* Alternate parent is active now. set the muxs */
for (int i = 0; i < RT_ARRAY_SIZE(rate->post_muxs); ++i)
{
const struct rockchip_cpu_clk_clksel *clksel = &rate->post_muxs[i];
if (!clksel->reg)
{
break;
}
HWREG32(base + clksel->reg) = clksel->val;
}
}
static int rockchip_cpu_clk_pre_rate_change(struct rockchip_cpu_clk_cell *cpu_clk_cell,
rt_ubase_t old_rate, rt_ubase_t new_rate)
{
rt_ubase_t alt_prate, alt_div;
void *base = cpu_clk_cell->rk_cell.provider->reg_base;
const struct rockchip_cpu_clk_reg_data *reg_data = cpu_clk_cell->reg_data;
const struct rockchip_cpu_clk_rate_table *rate;
/* Check validity of the new rate */
if (!(rate = rockchip_get_cpu_clk_settings(cpu_clk_cell, new_rate)))
{
return -RT_EINVAL;
}
alt_prate = rt_clk_cell_get_rate(&cpu_clk_cell->rk_cell_alt_parent->cell);
/*
* If the old parent clock speed is less than the clock speed
* of the alternate parent, then it should be ensured that at no point
* the armclk speed is more than the old_rate until the dividers are
* set.
*/
if (alt_prate > old_rate)
{
/* Calculate dividers */
alt_div = RT_DIV_ROUND_UP(alt_prate, old_rate) - 1;
if (alt_div > reg_data->div_core_mask[0])
{
alt_div = reg_data->div_core_mask[0];
}
/*
* Change parents and add dividers in a single transaction.
*
* NOTE: we do this in a single transaction so we're never
* dividing the primary parent by the extra dividers that were
* needed for the alt.
*/
for (int i = 0; i < reg_data->num_cores; ++i)
{
HWREG32(base + reg_data->core_reg[i]) = HIWORD_UPDATE(
alt_div,
reg_data->div_core_mask[i],
reg_data->div_core_shift[i]);
}
}
rockchip_cpu_clk_set_pre_muxs(cpu_clk_cell, rate);
/* select alternate parent */
if (reg_data->mux_core_reg)
{
HWREG32(base + reg_data->mux_core_reg) = HIWORD_UPDATE(
reg_data->mux_core_alt,
reg_data->mux_core_mask,
reg_data->mux_core_shift);
}
else
{
HWREG32(base + reg_data->core_reg[0]) = HIWORD_UPDATE(
reg_data->mux_core_alt,
reg_data->mux_core_mask,
reg_data->mux_core_shift);
}
return RT_EOK;
}
static rt_err_t rockchip_cpu_clk_post_rate_change(struct rockchip_cpu_clk_cell *cpu_clk_cell,
rt_ubase_t old_rate, rt_ubase_t new_rate)
{
void *base = cpu_clk_cell->rk_cell.provider->reg_base;
const struct rockchip_cpu_clk_rate_table *rate;
const struct rockchip_cpu_clk_reg_data *reg_data = cpu_clk_cell->reg_data;
if (!(rate = rockchip_get_cpu_clk_settings(cpu_clk_cell, new_rate)))
{
return -RT_EINVAL;
}
if (old_rate < new_rate)
{
rockchip_cpu_clk_set_dividers(cpu_clk_cell, rate);
}
/*
* post-rate change event, re-mux to primary parent and remove dividers.
*
* NOTE: we do this in a single transaction so we're never dividing the
* primary parent by the extra dividers that were needed for the alt.
*/
if (reg_data->mux_core_reg)
{
HWREG32(base + reg_data->mux_core_reg) = HIWORD_UPDATE(
reg_data->mux_core_main,
reg_data->mux_core_mask,
reg_data->mux_core_shift);
}
else
{
HWREG32(base + reg_data->core_reg[0]) = HIWORD_UPDATE(
reg_data->mux_core_main,
reg_data->mux_core_mask,
reg_data->mux_core_shift);
}
rockchip_cpu_clk_set_post_muxs(cpu_clk_cell, rate);
/* Remove dividers */
for (int i = 0; i < reg_data->num_cores; ++i)
{
HWREG32(base + reg_data->core_reg[i]) = HIWORD_UPDATE(
0,
reg_data->div_core_mask[i],
reg_data->div_core_shift[i]);
}
if (old_rate > new_rate)
{
rockchip_cpu_clk_set_dividers(cpu_clk_cell, rate);
}
return RT_EOK;
}
static rt_err_t rockchip_cpu_clk_notify(struct rt_clk_notifier *notifier,
rt_ubase_t msg, rt_ubase_t old_rate, rt_ubase_t new_rate)
{
rt_err_t err = RT_EOK;
struct rockchip_cpu_clk_cell *cpu_cell;
cpu_cell = rt_container_of(notifier, struct rockchip_cpu_clk_cell, notifier);
if (msg == RT_CLK_MSG_PRE_RATE_CHANGE)
{
err = rockchip_cpu_clk_pre_rate_change(cpu_cell, old_rate, new_rate);
}
else if (msg == RT_CLK_MSG_POST_RATE_CHANGE)
{
err = rockchip_cpu_clk_post_rate_change(cpu_cell, old_rate, new_rate);
}
return err;
}
void rockchip_cpu_clk_cell_init(struct rockchip_clk_cell *rk_cell)
{
struct rockchip_cpu_clk_cell *cpu_clk_cell = cell_to_rockchip_cpu_cell(&rk_cell->cell);
rk_cell->cell.parents_nr = 1;
rk_cell->cell.parent_name = cpu_clk_cell->rk_cell_parent->cell.name;
if (cpu_clk_cell->rate_count > 0)
{
rk_cell->cell.flags |= RT_CLK_F_SET_RATE_PARENT;
}
}
void rockchip_cpu_clk_cell_setup(struct rockchip_clk_cell *rk_cell)
{
struct rockchip_cpu_clk_cell *cpu_clk_cell = cell_to_rockchip_cpu_cell(&rk_cell->cell);
rt_clk_prepare_enable(rt_clk_cell_get_clk(&cpu_clk_cell->rk_cell_alt_parent->cell, RT_NULL));
cpu_clk_cell->notifier.callback = rockchip_cpu_clk_notify;
rt_clk_notifier_register(rt_clk_cell_get_clk(&cpu_clk_cell->rk_cell_parent->cell, RT_NULL),
&cpu_clk_cell->notifier);
}
|