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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
|
// SPDX-License-Identifier: GPL-2.0
/*
* StarFive JH7110 Clock Generator Driver
*
* Copyright (C) 2022 StarFive Technology Co., Ltd.
* Author: Xingyu Wu <xingyu.wu@starfivetech.com>
*/
#include <linux/bits.h>
#include <linux/clk.h>
#include <linux/clk-provider.h>
#include <linux/device.h>
#include <linux/init.h>
#include <linux/io.h>
#include <linux/kernel.h>
#include <linux/mod_devicetable.h>
#include <linux/module.h>
#include <linux/platform_device.h>
#include <linux/of_device.h>
#include <linux/pm_runtime.h>
#include <dt-bindings/clock/starfive-jh7110-clkgen.h>
#include "clk-starfive-jh7110.h"
#include "clk-starfive-jh7110-pll.h"
static struct jh7110_clk * __init jh7110_clk_from(struct clk_hw *hw)
{
return container_of(hw, struct jh7110_clk, hw);
}
static struct jh7110_clk_priv *jh7110_priv_from(struct jh7110_clk *clk)
{
return container_of(clk, struct jh7110_clk_priv, reg[clk->idx]);
}
void __iomem *jh7110_clk_reg_addr_get(struct jh7110_clk *clk)
{
void __iomem *reg;
struct jh7110_clk_priv *priv = jh7110_priv_from(clk);
if (clk->reg_flags == JH7110_CLK_SYS_FLAG)
reg = priv->sys_base + 4 * clk->idx;
else if (clk->reg_flags == JH7110_CLK_STG_FLAG)
reg = priv->stg_base + 4 * (clk->idx - JH7110_CLK_SYS_REG_END);
else if (clk->reg_flags == JH7110_CLK_AON_FLAG)
reg = priv->aon_base + 4 * (clk->idx - JH7110_CLK_STG_REG_END);
else if (clk->reg_flags == JH7110_CLK_VOUT_FLAG)
reg = priv->vout_base + 4 * clk->idx;
else if (clk->reg_flags == JH7110_CLK_ISP_FLAG)
reg = priv->isp_base + 4 * clk->idx;
return reg;
}
static u32 jh7110_clk_reg_get(struct jh7110_clk *clk)
{
void __iomem *reg = jh7110_clk_reg_addr_get(clk);
if ((clk->reg_flags == JH7110_CLK_ISP_FLAG) || (clk->reg_flags == JH7110_CLK_VOUT_FLAG)) {
int ret;
struct jh7110_clk_priv *priv = jh7110_priv_from(clk);
if (pm_runtime_suspended(priv->dev)) {
ret = pm_runtime_get_sync(priv->dev);
if (ret < 0) {
dev_err(priv->dev, "cannot resume device :%d.\n", ret);
return 0;
}
pm_runtime_put(priv->dev);
}
}
return readl_relaxed(reg);
}
static void jh7110_clk_reg_rmw(struct jh7110_clk *clk, u32 mask, u32 value)
{
struct jh7110_clk_priv *priv = jh7110_priv_from(clk);
void __iomem *reg = jh7110_clk_reg_addr_get(clk);
unsigned long flags;
spin_lock_irqsave(&priv->rmw_lock, flags);
value |= jh7110_clk_reg_get(clk) & ~mask;
writel_relaxed(value, reg);
spin_unlock_irqrestore(&priv->rmw_lock, flags);
}
static int jh7110_clk_enable(struct clk_hw *hw)
{
struct jh7110_clk *clk = jh7110_clk_from(hw);
jh7110_clk_reg_rmw(clk, JH7110_CLK_ENABLE, JH7110_CLK_ENABLE);
return 0;
}
static void jh7110_clk_disable(struct clk_hw *hw)
{
struct jh7110_clk *clk = jh7110_clk_from(hw);
jh7110_clk_reg_rmw(clk, JH7110_CLK_ENABLE, 0);
}
static int jh7110_clk_is_enabled(struct clk_hw *hw)
{
struct jh7110_clk *clk = jh7110_clk_from(hw);
return !!(jh7110_clk_reg_get(clk) & JH7110_CLK_ENABLE);
}
static unsigned long jh7110_clk_recalc_rate(struct clk_hw *hw,
unsigned long parent_rate)
{
struct jh7110_clk *clk = jh7110_clk_from(hw);
u32 div = jh7110_clk_reg_get(clk) & JH7110_CLK_DIV_MASK;
if (clk->idx == JH7110_UART3_CLK_CORE
|| clk->idx == JH7110_UART4_CLK_CORE
|| clk->idx == JH7110_UART5_CLK_CORE)
div = div >> 8;
return div ? parent_rate / div : 0;
}
static int jh7110_clk_determine_rate(struct clk_hw *hw,
struct clk_rate_request *req)
{
struct jh7110_clk *clk = jh7110_clk_from(hw);
unsigned long parent = req->best_parent_rate;
unsigned long rate = clamp(req->rate, req->min_rate, req->max_rate);
unsigned long div = min_t(unsigned long,
DIV_ROUND_UP(parent, rate), clk->max_div);
unsigned long result = parent / div;
/*
* we want the result clamped by min_rate and max_rate if possible:
* case 1: div hits the max divider value, which means it's less than
* parent / rate, so the result is greater than rate and min_rate in
* particular. we can't do anything about result > max_rate because the
* divider doesn't go any further.
* case 2: div = DIV_ROUND_UP(parent, rate) which means the result is
* always lower or equal to rate and max_rate. however the result may
* turn out lower than min_rate, but then the next higher rate is fine:
* div - 1 = ceil(parent / rate) - 1 < parent / rate
* and thus
* min_rate <= rate < parent / (div - 1)
*/
if (result < req->min_rate && div > 1)
result = parent / (div - 1);
req->rate = result;
return 0;
}
static int jh7110_clk_set_rate(struct clk_hw *hw,
unsigned long rate,
unsigned long parent_rate)
{
struct jh7110_clk *clk = jh7110_clk_from(hw);
unsigned long div = clamp(DIV_ROUND_CLOSEST(parent_rate, rate),
1UL, (unsigned long)clk->max_div);
/* UART3-5: [15:8]: integer part of the divisor. [7:0] fraction part of the divisor */
if (clk->idx == JH7110_UART3_CLK_CORE ||
clk->idx == JH7110_UART4_CLK_CORE ||
clk->idx == JH7110_UART5_CLK_CORE)
div <<= 8;
jh7110_clk_reg_rmw(clk, JH7110_CLK_DIV_MASK, div);
return 0;
}
static u8 jh7110_clk_get_parent(struct clk_hw *hw)
{
struct jh7110_clk *clk = jh7110_clk_from(hw);
u32 value = jh7110_clk_reg_get(clk);
return (value & JH7110_CLK_MUX_MASK) >> JH7110_CLK_MUX_SHIFT;
}
static int jh7110_clk_set_parent(struct clk_hw *hw, u8 index)
{
struct jh7110_clk *clk = jh7110_clk_from(hw);
u32 value = (u32)index << JH7110_CLK_MUX_SHIFT;
jh7110_clk_reg_rmw(clk, JH7110_CLK_MUX_MASK, value);
return 0;
}
static int jh7110_clk_mux_determine_rate(struct clk_hw *hw,
struct clk_rate_request *req)
{
return clk_mux_determine_rate_flags(hw, req, 0);
}
static int jh7110_clk_get_phase(struct clk_hw *hw)
{
struct jh7110_clk *clk = jh7110_clk_from(hw);
u32 value = jh7110_clk_reg_get(clk);
return (value & JH7110_CLK_INVERT) ? 180 : 0;
}
static int jh7110_clk_set_phase(struct clk_hw *hw, int degrees)
{
struct jh7110_clk *clk = jh7110_clk_from(hw);
u32 value;
if (degrees == 0)
value = 0;
else if (degrees == 180)
value = JH7110_CLK_INVERT;
else
return -EINVAL;
jh7110_clk_reg_rmw(clk, JH7110_CLK_INVERT, value);
return 0;
}
#ifdef CONFIG_DEBUG_FS
static void jh7110_clk_debug_init(struct clk_hw *hw, struct dentry *dentry)
{
static const struct debugfs_reg32 jh7110_clk_reg = {
.name = "CTRL",
.offset = 0,
};
struct jh7110_clk *clk = jh7110_clk_from(hw);
struct jh7110_clk_priv *priv = jh7110_priv_from(clk);
struct debugfs_regset32 *regset;
regset = devm_kzalloc(priv->dev, sizeof(*regset), GFP_KERNEL);
if (!regset)
return;
regset->regs = &jh7110_clk_reg;
regset->nregs = 1;
regset->base = jh7110_clk_reg_addr_get(clk);
debugfs_create_regset32("registers", 0400, dentry, regset);
}
#else
#define jh7110_clk_debug_init NULL
#endif
#ifdef CONFIG_PM_SLEEP
static int jh7110_clk_save_context(struct clk_hw *hw)
{
struct jh7110_clk *clk = jh7110_clk_from(hw);
void __iomem *reg = jh7110_clk_reg_addr_get(clk);
struct jh7110_clk_priv *priv = jh7110_priv_from(clk);
if (!clk || !priv)
return 0;
if ((clk->reg_flags == JH7110_CLK_ISP_FLAG) || (clk->reg_flags == JH7110_CLK_VOUT_FLAG))
return 0;
if (clk->idx >= JH7110_CLK_REG_END)
return 0;
spin_lock(&priv->rmw_lock);
clk->saved_reg_value = readl_relaxed(reg);
spin_unlock(&priv->rmw_lock);
return 0;
}
static void jh7110_clk_gate_restore_context(struct clk_hw *hw)
{
struct jh7110_clk *clk = jh7110_clk_from(hw);
if (!clk)
return;
if ((clk->reg_flags == JH7110_CLK_ISP_FLAG) || (clk->reg_flags == JH7110_CLK_VOUT_FLAG))
return;
if (clk->idx >= JH7110_CLK_REG_END)
return;
jh7110_clk_reg_rmw(clk, JH7110_CLK_ENABLE, clk->saved_reg_value);
return;
}
static void jh7110_clk_div_restore_context(struct clk_hw *hw)
{
struct jh7110_clk *clk = jh7110_clk_from(hw);
if (!clk)
return;
if ((clk->reg_flags == JH7110_CLK_ISP_FLAG) || (clk->reg_flags == JH7110_CLK_VOUT_FLAG))
return;
if (clk->idx >= JH7110_CLK_REG_END)
return;
jh7110_clk_reg_rmw(clk, JH7110_CLK_DIV_MASK, clk->saved_reg_value);
return;
}
static void jh7110_clk_mux_restore_context(struct clk_hw *hw)
{
struct jh7110_clk *clk = jh7110_clk_from(hw);
if (!clk)
return;
if ((clk->reg_flags == JH7110_CLK_ISP_FLAG) || (clk->reg_flags == JH7110_CLK_VOUT_FLAG))
return;
if (clk->idx >= JH7110_CLK_REG_END)
return;
jh7110_clk_reg_rmw(clk, JH7110_CLK_MUX_MASK, clk->saved_reg_value);
return;
}
static void jh7110_clk_inv_restore_context(struct clk_hw *hw)
{
struct jh7110_clk *clk = jh7110_clk_from(hw);
if (!clk)
return;
if ((clk->reg_flags == JH7110_CLK_ISP_FLAG) || (clk->reg_flags == JH7110_CLK_VOUT_FLAG))
return;
if (clk->idx >= JH7110_CLK_REG_END)
return;
jh7110_clk_reg_rmw(clk, JH7110_CLK_INVERT, clk->saved_reg_value);
return;
}
static void jh7110_clk_gdiv_restore_context(struct clk_hw *hw)
{
jh7110_clk_div_restore_context(hw);
jh7110_clk_gate_restore_context(hw);
return;
}
static void jh7110_clk_gmux_restore_context(struct clk_hw *hw)
{
jh7110_clk_mux_restore_context(hw);
jh7110_clk_gate_restore_context(hw);
return;
}
static void jh7110_clk_mdiv_restore_context(struct clk_hw *hw)
{
jh7110_clk_mux_restore_context(hw);
jh7110_clk_div_restore_context(hw);
return;
}
static void jh7110_clk_gmd_restore_context(struct clk_hw *hw)
{
jh7110_clk_mux_restore_context(hw);
jh7110_clk_div_restore_context(hw);
jh7110_clk_gate_restore_context(hw);
return;
}
#endif
static const struct clk_ops jh7110_clk_gate_ops = {
.enable = jh7110_clk_enable,
.disable = jh7110_clk_disable,
.is_enabled = jh7110_clk_is_enabled,
.debug_init = jh7110_clk_debug_init,
#ifdef CONFIG_PM_SLEEP
.save_context = jh7110_clk_save_context,
.restore_context = jh7110_clk_gate_restore_context,
#endif
};
static const struct clk_ops jh7110_clk_div_ops = {
.recalc_rate = jh7110_clk_recalc_rate,
.determine_rate = jh7110_clk_determine_rate,
.set_rate = jh7110_clk_set_rate,
.debug_init = jh7110_clk_debug_init,
#ifdef CONFIG_PM_SLEEP
.save_context = jh7110_clk_save_context,
.restore_context = jh7110_clk_div_restore_context,
#endif
};
static const struct clk_ops jh7110_clk_gdiv_ops = {
.enable = jh7110_clk_enable,
.disable = jh7110_clk_disable,
.is_enabled = jh7110_clk_is_enabled,
.recalc_rate = jh7110_clk_recalc_rate,
.determine_rate = jh7110_clk_determine_rate,
.set_rate = jh7110_clk_set_rate,
.debug_init = jh7110_clk_debug_init,
#ifdef CONFIG_PM_SLEEP
.save_context = jh7110_clk_save_context,
.restore_context = jh7110_clk_gdiv_restore_context,
#endif
};
static const struct clk_ops jh7110_clk_mux_ops = {
.determine_rate = jh7110_clk_mux_determine_rate,
.set_parent = jh7110_clk_set_parent,
.get_parent = jh7110_clk_get_parent,
.debug_init = jh7110_clk_debug_init,
#ifdef CONFIG_PM_SLEEP
.save_context = jh7110_clk_save_context,
.restore_context = jh7110_clk_mux_restore_context,
#endif
};
static const struct clk_ops jh7110_clk_gmux_ops = {
.enable = jh7110_clk_enable,
.disable = jh7110_clk_disable,
.is_enabled = jh7110_clk_is_enabled,
.determine_rate = jh7110_clk_mux_determine_rate,
.set_parent = jh7110_clk_set_parent,
.get_parent = jh7110_clk_get_parent,
.debug_init = jh7110_clk_debug_init,
#ifdef CONFIG_PM_SLEEP
.save_context = jh7110_clk_save_context,
.restore_context = jh7110_clk_gmux_restore_context,
#endif
};
static const struct clk_ops jh7110_clk_mdiv_ops = {
.recalc_rate = jh7110_clk_recalc_rate,
.determine_rate = jh7110_clk_determine_rate,
.get_parent = jh7110_clk_get_parent,
.set_parent = jh7110_clk_set_parent,
.set_rate = jh7110_clk_set_rate,
.debug_init = jh7110_clk_debug_init,
#ifdef CONFIG_PM_SLEEP
.save_context = jh7110_clk_save_context,
.restore_context = jh7110_clk_mdiv_restore_context,
#endif
};
static const struct clk_ops jh7110_clk_gmd_ops = {
.enable = jh7110_clk_enable,
.disable = jh7110_clk_disable,
.is_enabled = jh7110_clk_is_enabled,
.recalc_rate = jh7110_clk_recalc_rate,
.determine_rate = jh7110_clk_determine_rate,
.get_parent = jh7110_clk_get_parent,
.set_parent = jh7110_clk_set_parent,
.set_rate = jh7110_clk_set_rate,
.debug_init = jh7110_clk_debug_init,
#ifdef CONFIG_PM_SLEEP
.save_context = jh7110_clk_save_context,
.restore_context = jh7110_clk_gmd_restore_context,
#endif
};
static const struct clk_ops jh7110_clk_inv_ops = {
.get_phase = jh7110_clk_get_phase,
.set_phase = jh7110_clk_set_phase,
.debug_init = jh7110_clk_debug_init,
#ifdef CONFIG_PM_SLEEP
.save_context = jh7110_clk_save_context,
.restore_context = jh7110_clk_inv_restore_context,
#endif
};
const struct clk_ops *starfive_jh7110_clk_ops(u32 max)
{
const struct clk_ops *ops;
if (max & JH7110_CLK_DIV_MASK) {
if (max & JH7110_CLK_MUX_MASK) {
if (max & JH7110_CLK_ENABLE)
ops = &jh7110_clk_gmd_ops;
else
ops = &jh7110_clk_mdiv_ops;
} else if (max & JH7110_CLK_ENABLE)
ops = &jh7110_clk_gdiv_ops;
else
ops = &jh7110_clk_div_ops;
} else if (max & JH7110_CLK_MUX_MASK) {
if (max & JH7110_CLK_ENABLE)
ops = &jh7110_clk_gmux_ops;
else
ops = &jh7110_clk_mux_ops;
} else if (max & JH7110_CLK_ENABLE)
ops = &jh7110_clk_gate_ops;
else
ops = &jh7110_clk_inv_ops;
return ops;
}
EXPORT_SYMBOL_GPL(starfive_jh7110_clk_ops);
#ifdef CONFIG_PM_SLEEP
static int clk_starfive_jh7110_gen_system_suspend(struct device *dev)
{
return clk_save_context();
}
static int clk_starfive_jh7110_gen_system_resume(struct device *dev)
{
clk_restore_context();
return 0;
}
#endif
static const struct dev_pm_ops clk_starfive_jh7110_gen_pm_ops = {
SET_LATE_SYSTEM_SLEEP_PM_OPS(clk_starfive_jh7110_gen_system_suspend,
clk_starfive_jh7110_gen_system_resume)
};
static struct clk_hw *jh7110_clk_get(struct of_phandle_args *clkspec,
void *data)
{
struct jh7110_clk_priv *priv = data;
unsigned int idx = clkspec->args[0];
if (idx < JH7110_PLL0_OUT)
return &priv->reg[idx].hw;
if (idx < JH7110_CLK_END) {
#ifdef CONFIG_CLK_STARFIVE_JH7110_PLL
if ((idx == JH7110_PLL0_OUT) || (idx == JH7110_PLL2_OUT))
return &priv->pll_priv[PLL_OF(idx)].hw;
#endif
return priv->pll[PLL_OF(idx)];
}
return ERR_PTR(-EINVAL);
}
static int __init clk_starfive_jh7110_probe(struct platform_device *pdev)
{
struct jh7110_clk_priv *priv;
int ret = 0;
priv = devm_kzalloc(&pdev->dev, struct_size(priv, reg, JH7110_PLL0_OUT),
GFP_KERNEL);
if (!priv)
return -ENOMEM;
spin_lock_init(&priv->rmw_lock);
priv->dev = &pdev->dev;
pm_runtime_enable(priv->dev);
#ifdef CONFIG_CLK_STARFIVE_JH7110_PLL
ret = clk_starfive_jh7110_pll_init(pdev, priv->pll_priv);
if (ret)
return ret;
#endif
ret = clk_starfive_jh7110_sys_init(pdev, priv);
if (ret)
return ret;
/* set PLL0 default rate */
#ifdef CONFIG_CLK_STARFIVE_JH7110_PLL
if (PLL0_DEFAULT_FREQ) {
struct clk *pll0_clk = priv->pll_priv[PLL0_INDEX].hw.clk;
struct clk *cpu_root = priv->reg[JH7110_CPU_ROOT].hw.clk;
struct clk *osc_clk = clk_get(&pdev->dev, "osc");
if (IS_ERR(osc_clk))
dev_err(&pdev->dev, "get osc_clk failed\n");
if (PLL0_DEFAULT_FREQ >= PLL0_FREQ_1500_VALUE) {
struct clk *cpu_core = priv->reg[JH7110_CPU_CORE].hw.clk;
if (clk_set_rate(cpu_core, clk_get_rate(pll0_clk) / 2)) {
dev_err(&pdev->dev, "set cpu_core rate failed\n");
goto failed_set;
}
}
if (clk_set_parent(cpu_root, osc_clk)) {
dev_err(&pdev->dev, "set parent to osc_clk failed\n");
goto failed_set;
}
if (clk_set_rate(pll0_clk, PLL0_DEFAULT_FREQ))
dev_err(&pdev->dev, "set pll0 rate failed\n");
if (clk_set_parent(cpu_root, pll0_clk))
dev_err(&pdev->dev, "set parent to pll0_clk failed\n");
failed_set:
clk_put(osc_clk);
}
#endif
ret = clk_starfive_jh7110_stg_init(pdev, priv);
if (ret)
return ret;
ret = clk_starfive_jh7110_aon_init(pdev, priv);
if (ret)
return ret;
ret = devm_of_clk_add_hw_provider(priv->dev, jh7110_clk_get, priv);
if (ret)
return ret;
dev_info(&pdev->dev, "starfive JH7110 clkgen init successfully.");
return 0;
}
static const struct of_device_id clk_starfive_jh7110_match[] = {
{.compatible = "starfive,jh7110-clkgen"},
{ /* sentinel */ }
};
static struct platform_driver clk_starfive_jh7110_driver = {
.driver = {
.name = "clk-starfive-jh7110",
.of_match_table = clk_starfive_jh7110_match,
.pm = &clk_starfive_jh7110_gen_pm_ops,
},
};
builtin_platform_driver_probe(clk_starfive_jh7110_driver,
clk_starfive_jh7110_probe);
MODULE_AUTHOR("Xingyu Wu <xingyu.wu@starfivetech.com>");
MODULE_DESCRIPTION("StarFive JH7110 sysgen clock driver");
MODULE_LICENSE("GPL");
|