Skip to content

Commit 2089dc3

Browse files
gclementbebarino
authored andcommitted
clk: mvebu: armada-37xx-periph: add DVFS support for cpu clocks
When DVFS is enabled the CPU clock setting is done using an other set of registers. These Power Management registers are exposed through a syscon as they will also be used by other drivers such as the cpufreq. This patch add the possibility to modify the CPU frequency using the associate load level matching the target frequency. Then all the frequency switch is handle by the hardware. Signed-off-by: Gregory CLEMENT <gregory.clement@free-electrons.com> [sboyd@codeaurora.org: Grow a local variable for regmap pointer to keep lines shorter] Signed-off-by: Stephen Boyd <sboyd@codeaurora.org>
1 parent 9818a7a commit 2089dc3

1 file changed

Lines changed: 217 additions & 4 deletions

File tree

‎drivers/clk/mvebu/armada-37xx-periph.c‎

Lines changed: 217 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,11 @@
2121
*/
2222

2323
#include <linux/clk-provider.h>
24+
#include <linux/mfd/syscon.h>
2425
#include <linux/of.h>
2526
#include <linux/of_device.h>
2627
#include <linux/platform_device.h>
28+
#include <linux/regmap.h>
2729
#include <linux/slab.h>
2830

2931
#define TBG_SEL 0x0
@@ -33,6 +35,26 @@
3335
#define CLK_SEL 0x10
3436
#define CLK_DIS 0x14
3537

38+
#define LOAD_LEVEL_NR 4
39+
40+
#define ARMADA_37XX_NB_L0L1 0x18
41+
#define ARMADA_37XX_NB_L2L3 0x1C
42+
#define ARMADA_37XX_NB_TBG_DIV_OFF 13
43+
#define ARMADA_37XX_NB_TBG_DIV_MASK 0x7
44+
#define ARMADA_37XX_NB_CLK_SEL_OFF 11
45+
#define ARMADA_37XX_NB_CLK_SEL_MASK 0x1
46+
#define ARMADA_37XX_NB_TBG_SEL_OFF 9
47+
#define ARMADA_37XX_NB_TBG_SEL_MASK 0x3
48+
#define ARMADA_37XX_NB_CONFIG_SHIFT 16
49+
#define ARMADA_37XX_NB_DYN_MOD 0x24
50+
#define ARMADA_37XX_NB_DFS_EN 31
51+
#define ARMADA_37XX_NB_CPU_LOAD 0x30
52+
#define ARMADA_37XX_NB_CPU_LOAD_MASK 0x3
53+
#define ARMADA_37XX_DVFS_LOAD_0 0
54+
#define ARMADA_37XX_DVFS_LOAD_1 1
55+
#define ARMADA_37XX_DVFS_LOAD_2 2
56+
#define ARMADA_37XX_DVFS_LOAD_3 3
57+
3658
struct clk_periph_driver_data {
3759
struct clk_hw_onecell_data *hw_data;
3860
spinlock_t lock;
@@ -53,6 +75,7 @@ struct clk_pm_cpu {
5375
u32 mask_mux;
5476
void __iomem *reg_div;
5577
u8 shift_div;
78+
struct regmap *nb_pm_base;
5679
};
5780

5881
#define to_clk_double_div(_hw) container_of(_hw, struct clk_double_div, hw)
@@ -316,34 +339,219 @@ static const struct clk_ops clk_double_div_ops = {
316339
.recalc_rate = clk_double_div_recalc_rate,
317340
};
318341

342+
static void armada_3700_pm_dvfs_update_regs(unsigned int load_level,
343+
unsigned int *reg,
344+
unsigned int *offset)
345+
{
346+
if (load_level <= ARMADA_37XX_DVFS_LOAD_1)
347+
*reg = ARMADA_37XX_NB_L0L1;
348+
else
349+
*reg = ARMADA_37XX_NB_L2L3;
350+
351+
if (load_level == ARMADA_37XX_DVFS_LOAD_0 ||
352+
load_level == ARMADA_37XX_DVFS_LOAD_2)
353+
*offset += ARMADA_37XX_NB_CONFIG_SHIFT;
354+
}
355+
356+
static bool armada_3700_pm_dvfs_is_enabled(struct regmap *base)
357+
{
358+
unsigned int val, reg = ARMADA_37XX_NB_DYN_MOD;
359+
360+
if (IS_ERR(base))
361+
return false;
362+
363+
regmap_read(base, reg, &val);
364+
365+
return !!(val & BIT(ARMADA_37XX_NB_DFS_EN));
366+
}
367+
368+
static unsigned int armada_3700_pm_dvfs_get_cpu_div(struct regmap *base)
369+
{
370+
unsigned int reg = ARMADA_37XX_NB_CPU_LOAD;
371+
unsigned int offset = ARMADA_37XX_NB_TBG_DIV_OFF;
372+
unsigned int load_level, div;
373+
374+
/*
375+
* This function is always called after the function
376+
* armada_3700_pm_dvfs_is_enabled, so no need to check again
377+
* if the base is valid.
378+
*/
379+
regmap_read(base, reg, &load_level);
380+
381+
/*
382+
* The register and the offset inside this register accessed to
383+
* read the current divider depend on the load level
384+
*/
385+
load_level &= ARMADA_37XX_NB_CPU_LOAD_MASK;
386+
armada_3700_pm_dvfs_update_regs(load_level, &reg, &offset);
387+
388+
regmap_read(base, reg, &div);
389+
390+
return (div >> offset) & ARMADA_37XX_NB_TBG_DIV_MASK;
391+
}
392+
393+
static unsigned int armada_3700_pm_dvfs_get_cpu_parent(struct regmap *base)
394+
{
395+
unsigned int reg = ARMADA_37XX_NB_CPU_LOAD;
396+
unsigned int offset = ARMADA_37XX_NB_TBG_SEL_OFF;
397+
unsigned int load_level, sel;
398+
399+
/*
400+
* This function is always called after the function
401+
* armada_3700_pm_dvfs_is_enabled, so no need to check again
402+
* if the base is valid
403+
*/
404+
regmap_read(base, reg, &load_level);
405+
406+
/*
407+
* The register and the offset inside this register accessed to
408+
* read the current divider depend on the load level
409+
*/
410+
load_level &= ARMADA_37XX_NB_CPU_LOAD_MASK;
411+
armada_3700_pm_dvfs_update_regs(load_level, &reg, &offset);
412+
413+
regmap_read(base, reg, &sel);
414+
415+
return (sel >> offset) & ARMADA_37XX_NB_TBG_SEL_MASK;
416+
}
417+
319418
static u8 clk_pm_cpu_get_parent(struct clk_hw *hw)
320419
{
321420
struct clk_pm_cpu *pm_cpu = to_clk_pm_cpu(hw);
322421
int num_parents = clk_hw_get_num_parents(hw);
323422
u32 val;
324423

325-
val = readl(pm_cpu->reg_mux) >> pm_cpu->shift_mux;
326-
val &= pm_cpu->mask_mux;
424+
if (armada_3700_pm_dvfs_is_enabled(pm_cpu->nb_pm_base)) {
425+
val = armada_3700_pm_dvfs_get_cpu_parent(pm_cpu->nb_pm_base);
426+
} else {
427+
val = readl(pm_cpu->reg_mux) >> pm_cpu->shift_mux;
428+
val &= pm_cpu->mask_mux;
429+
}
327430

328431
if (val >= num_parents)
329432
return -EINVAL;
330433

331434
return val;
332435
}
333436

437+
static int clk_pm_cpu_set_parent(struct clk_hw *hw, u8 index)
438+
{
439+
struct clk_pm_cpu *pm_cpu = to_clk_pm_cpu(hw);
440+
struct regmap *base = pm_cpu->nb_pm_base;
441+
int load_level;
442+
443+
/*
444+
* We set the clock parent only if the DVFS is available but
445+
* not enabled.
446+
*/
447+
if (IS_ERR(base) || armada_3700_pm_dvfs_is_enabled(base))
448+
return -EINVAL;
449+
450+
/* Set the parent clock for all the load level */
451+
for (load_level = 0; load_level < LOAD_LEVEL_NR; load_level++) {
452+
unsigned int reg, mask, val,
453+
offset = ARMADA_37XX_NB_TBG_SEL_OFF;
454+
455+
armada_3700_pm_dvfs_update_regs(load_level, &reg, &offset);
456+
457+
val = index << offset;
458+
mask = ARMADA_37XX_NB_TBG_SEL_MASK << offset;
459+
regmap_update_bits(base, reg, mask, val);
460+
}
461+
return 0;
462+
}
463+
334464
static unsigned long clk_pm_cpu_recalc_rate(struct clk_hw *hw,
335465
unsigned long parent_rate)
336466
{
337467
struct clk_pm_cpu *pm_cpu = to_clk_pm_cpu(hw);
338468
unsigned int div;
339469

340-
div = get_div(pm_cpu->reg_div, pm_cpu->shift_div);
341-
470+
if (armada_3700_pm_dvfs_is_enabled(pm_cpu->nb_pm_base))
471+
div = armada_3700_pm_dvfs_get_cpu_div(pm_cpu->nb_pm_base);
472+
else
473+
div = get_div(pm_cpu->reg_div, pm_cpu->shift_div);
342474
return DIV_ROUND_UP_ULL((u64)parent_rate, div);
343475
}
344476

477+
static long clk_pm_cpu_round_rate(struct clk_hw *hw, unsigned long rate,
478+
unsigned long *parent_rate)
479+
{
480+
struct clk_pm_cpu *pm_cpu = to_clk_pm_cpu(hw);
481+
struct regmap *base = pm_cpu->nb_pm_base;
482+
unsigned int div = *parent_rate / rate;
483+
unsigned int load_level;
484+
/* only available when DVFS is enabled */
485+
if (!armada_3700_pm_dvfs_is_enabled(base))
486+
return -EINVAL;
487+
488+
for (load_level = 0; load_level < LOAD_LEVEL_NR; load_level++) {
489+
unsigned int reg, val, offset = ARMADA_37XX_NB_TBG_DIV_OFF;
490+
491+
armada_3700_pm_dvfs_update_regs(load_level, &reg, &offset);
492+
493+
regmap_read(base, reg, &val);
494+
495+
val >>= offset;
496+
val &= ARMADA_37XX_NB_TBG_DIV_MASK;
497+
if (val == div)
498+
/*
499+
* We found a load level matching the target
500+
* divider, switch to this load level and
501+
* return.
502+
*/
503+
return *parent_rate / div;
504+
}
505+
506+
/* We didn't find any valid divider */
507+
return -EINVAL;
508+
}
509+
510+
static int clk_pm_cpu_set_rate(struct clk_hw *hw, unsigned long rate,
511+
unsigned long parent_rate)
512+
{
513+
struct clk_pm_cpu *pm_cpu = to_clk_pm_cpu(hw);
514+
struct regmap *base = pm_cpu->nb_pm_base;
515+
unsigned int div = parent_rate / rate;
516+
unsigned int load_level;
517+
518+
/* only available when DVFS is enabled */
519+
if (!armada_3700_pm_dvfs_is_enabled(base))
520+
return -EINVAL;
521+
522+
for (load_level = 0; load_level < LOAD_LEVEL_NR; load_level++) {
523+
unsigned int reg, mask, val,
524+
offset = ARMADA_37XX_NB_TBG_DIV_OFF;
525+
526+
armada_3700_pm_dvfs_update_regs(load_level, &reg, &offset);
527+
528+
regmap_read(base, reg, &val);
529+
val >>= offset;
530+
val &= ARMADA_37XX_NB_TBG_DIV_MASK;
531+
532+
if (val == div) {
533+
/*
534+
* We found a load level matching the target
535+
* divider, switch to this load level and
536+
* return.
537+
*/
538+
reg = ARMADA_37XX_NB_CPU_LOAD;
539+
mask = ARMADA_37XX_NB_CPU_LOAD_MASK;
540+
regmap_update_bits(base, reg, mask, load_level);
541+
542+
return rate;
543+
}
544+
}
545+
546+
/* We didn't find any valid divider */
547+
return -EINVAL;
548+
}
549+
345550
static const struct clk_ops clk_pm_cpu_ops = {
346551
.get_parent = clk_pm_cpu_get_parent,
552+
.set_parent = clk_pm_cpu_set_parent,
553+
.round_rate = clk_pm_cpu_round_rate,
554+
.set_rate = clk_pm_cpu_set_rate,
347555
.recalc_rate = clk_pm_cpu_recalc_rate,
348556
};
349557

@@ -409,6 +617,7 @@ static int armada_3700_add_composite_clk(const struct clk_periph_data *data,
409617
if (data->muxrate_hw) {
410618
struct clk_pm_cpu *pmcpu_clk;
411619
struct clk_hw *muxrate_hw = data->muxrate_hw;
620+
struct regmap *map;
412621

413622
pmcpu_clk = to_clk_pm_cpu(muxrate_hw);
414623
pmcpu_clk->reg_mux = reg + (u64)pmcpu_clk->reg_mux;
@@ -418,6 +627,10 @@ static int armada_3700_add_composite_clk(const struct clk_periph_data *data,
418627
rate_hw = muxrate_hw;
419628
mux_ops = muxrate_hw->init->ops;
420629
rate_ops = muxrate_hw->init->ops;
630+
631+
map = syscon_regmap_lookup_by_compatible(
632+
"marvell,armada-3700-nb-pm");
633+
pmcpu_clk->nb_pm_base = map;
421634
}
422635

423636
*hw = clk_hw_register_composite(dev, data->name, data->parent_names,

0 commit comments

Comments
 (0)