Skip to content

Commit 1c35282

Browse files
gclementrafaeljw
authored andcommitted
cpufreq: armada-37xx: Add AVS support
Armada 37xx supports Adaptive Voltage Scaling and thanks to this patch a voltage is associated to each load level. Signed-off-by: Gregory CLEMENT <gregory.clement@bootlin.com> Acked-by: Viresh Kumar <viresh.kumar@linaro.org> Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
1 parent 2851b90 commit 1c35282

1 file changed

Lines changed: 160 additions & 3 deletions

File tree

‎drivers/cpufreq/armada-37xx-cpufreq.c‎

Lines changed: 160 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,34 @@
5151
#define ARMADA_37XX_DVFS_LOAD_2 2
5252
#define ARMADA_37XX_DVFS_LOAD_3 3
5353

54+
/* AVS register set */
55+
#define ARMADA_37XX_AVS_CTL0 0x0
56+
#define ARMADA_37XX_AVS_ENABLE BIT(30)
57+
#define ARMADA_37XX_AVS_HIGH_VDD_LIMIT 16
58+
#define ARMADA_37XX_AVS_LOW_VDD_LIMIT 22
59+
#define ARMADA_37XX_AVS_VDD_MASK 0x3F
60+
#define ARMADA_37XX_AVS_CTL2 0x8
61+
#define ARMADA_37XX_AVS_LOW_VDD_EN BIT(6)
62+
#define ARMADA_37XX_AVS_VSET(x) (0x1C + 4 * (x))
63+
5464
/*
5565
* On Armada 37xx the Power management manages 4 level of CPU load,
5666
* each level can be associated with a CPU clock source, a CPU
5767
* divider, a VDD level, etc...
5868
*/
5969
#define LOAD_LEVEL_NR 4
6070

71+
#define MIN_VOLT_MV 1000
72+
73+
/* AVS value for the corresponding voltage (in mV) */
74+
static int avs_map[] = {
75+
747, 758, 770, 782, 793, 805, 817, 828, 840, 852, 863, 875, 887, 898,
76+
910, 922, 933, 945, 957, 968, 980, 992, 1003, 1015, 1027, 1038, 1050,
77+
1062, 1073, 1085, 1097, 1108, 1120, 1132, 1143, 1155, 1167, 1178, 1190,
78+
1202, 1213, 1225, 1237, 1248, 1260, 1272, 1283, 1295, 1307, 1318, 1330,
79+
1342
80+
};
81+
6182
struct armada37xx_cpufreq_state {
6283
struct regmap *regmap;
6384
u32 nb_l0l1;
@@ -71,6 +92,7 @@ static struct armada37xx_cpufreq_state *armada37xx_cpufreq_state;
7192
struct armada_37xx_dvfs {
7293
u32 cpu_freq_max;
7394
u8 divider[LOAD_LEVEL_NR];
95+
u32 avs[LOAD_LEVEL_NR];
7496
};
7597

7698
static struct armada_37xx_dvfs armada_37xx_dvfs[] = {
@@ -148,6 +170,128 @@ static void __init armada37xx_cpufreq_dvfs_setup(struct regmap *base,
148170
clk_set_parent(clk, parent);
149171
}
150172

173+
/*
174+
* Find out the armada 37x supported AVS value whose voltage value is
175+
* the round-up closest to the target voltage value.
176+
*/
177+
static u32 armada_37xx_avs_val_match(int target_vm)
178+
{
179+
u32 avs;
180+
181+
/* Find out the round-up closest supported voltage value */
182+
for (avs = 0; avs < ARRAY_SIZE(avs_map); avs++)
183+
if (avs_map[avs] >= target_vm)
184+
break;
185+
186+
/*
187+
* If all supported voltages are smaller than target one,
188+
* choose the largest supported voltage
189+
*/
190+
if (avs == ARRAY_SIZE(avs_map))
191+
avs = ARRAY_SIZE(avs_map) - 1;
192+
193+
return avs;
194+
}
195+
196+
/*
197+
* For Armada 37xx soc, L0(VSET0) VDD AVS value is set to SVC revision
198+
* value or a default value when SVC is not supported.
199+
* - L0 can be read out from the register of AVS_CTRL_0 and L0 voltage
200+
* can be got from the mapping table of avs_map.
201+
* - L1 voltage should be about 100mv smaller than L0 voltage
202+
* - L2 & L3 voltage should be about 150mv smaller than L0 voltage.
203+
* This function calculates L1 & L2 & L3 AVS values dynamically based
204+
* on L0 voltage and fill all AVS values to the AVS value table.
205+
*/
206+
static void __init armada37xx_cpufreq_avs_configure(struct regmap *base,
207+
struct armada_37xx_dvfs *dvfs)
208+
{
209+
unsigned int target_vm;
210+
int load_level = 0;
211+
u32 l0_vdd_min;
212+
213+
if (base == NULL)
214+
return;
215+
216+
/* Get L0 VDD min value */
217+
regmap_read(base, ARMADA_37XX_AVS_CTL0, &l0_vdd_min);
218+
l0_vdd_min = (l0_vdd_min >> ARMADA_37XX_AVS_LOW_VDD_LIMIT) &
219+
ARMADA_37XX_AVS_VDD_MASK;
220+
if (l0_vdd_min >= ARRAY_SIZE(avs_map)) {
221+
pr_err("L0 VDD MIN %d is not correct.\n", l0_vdd_min);
222+
return;
223+
}
224+
dvfs->avs[0] = l0_vdd_min;
225+
226+
if (avs_map[l0_vdd_min] <= MIN_VOLT_MV) {
227+
/*
228+
* If L0 voltage is smaller than 1000mv, then all VDD sets
229+
* use L0 voltage;
230+
*/
231+
u32 avs_min = armada_37xx_avs_val_match(MIN_VOLT_MV);
232+
233+
for (load_level = 1; load_level < LOAD_LEVEL_NR; load_level++)
234+
dvfs->avs[load_level] = avs_min;
235+
236+
return;
237+
}
238+
239+
/*
240+
* L1 voltage is equal to L0 voltage - 100mv and it must be
241+
* larger than 1000mv
242+
*/
243+
244+
target_vm = avs_map[l0_vdd_min] - 100;
245+
target_vm = target_vm > MIN_VOLT_MV ? target_vm : MIN_VOLT_MV;
246+
dvfs->avs[1] = armada_37xx_avs_val_match(target_vm);
247+
248+
/*
249+
* L2 & L3 voltage is equal to L0 voltage - 150mv and it must
250+
* be larger than 1000mv
251+
*/
252+
target_vm = avs_map[l0_vdd_min] - 150;
253+
target_vm = target_vm > MIN_VOLT_MV ? target_vm : MIN_VOLT_MV;
254+
dvfs->avs[2] = dvfs->avs[3] = armada_37xx_avs_val_match(target_vm);
255+
}
256+
257+
static void __init armada37xx_cpufreq_avs_setup(struct regmap *base,
258+
struct armada_37xx_dvfs *dvfs)
259+
{
260+
unsigned int avs_val = 0, freq;
261+
int load_level = 0;
262+
263+
if (base == NULL)
264+
return;
265+
266+
/* Disable AVS before the configuration */
267+
regmap_update_bits(base, ARMADA_37XX_AVS_CTL0,
268+
ARMADA_37XX_AVS_ENABLE, 0);
269+
270+
271+
/* Enable low voltage mode */
272+
regmap_update_bits(base, ARMADA_37XX_AVS_CTL2,
273+
ARMADA_37XX_AVS_LOW_VDD_EN,
274+
ARMADA_37XX_AVS_LOW_VDD_EN);
275+
276+
277+
for (load_level = 1; load_level < LOAD_LEVEL_NR; load_level++) {
278+
freq = dvfs->cpu_freq_max / dvfs->divider[load_level];
279+
280+
avs_val = dvfs->avs[load_level];
281+
regmap_update_bits(base, ARMADA_37XX_AVS_VSET(load_level-1),
282+
ARMADA_37XX_AVS_VDD_MASK << ARMADA_37XX_AVS_HIGH_VDD_LIMIT |
283+
ARMADA_37XX_AVS_VDD_MASK << ARMADA_37XX_AVS_LOW_VDD_LIMIT,
284+
avs_val << ARMADA_37XX_AVS_HIGH_VDD_LIMIT |
285+
avs_val << ARMADA_37XX_AVS_LOW_VDD_LIMIT);
286+
}
287+
288+
/* Enable AVS after the configuration */
289+
regmap_update_bits(base, ARMADA_37XX_AVS_CTL0,
290+
ARMADA_37XX_AVS_ENABLE,
291+
ARMADA_37XX_AVS_ENABLE);
292+
293+
}
294+
151295
static void armada37xx_cpufreq_disable_dvfs(struct regmap *base)
152296
{
153297
unsigned int reg = ARMADA_37XX_NB_DYN_MOD,
@@ -216,7 +360,7 @@ static int __init armada37xx_cpufreq_driver_init(void)
216360
struct platform_device *pdev;
217361
unsigned long freq;
218362
unsigned int cur_frequency;
219-
struct regmap *nb_pm_base;
363+
struct regmap *nb_pm_base, *avs_base;
220364
struct device *cpu_dev;
221365
int load_lvl, ret;
222366
struct clk *clk;
@@ -227,6 +371,14 @@ static int __init armada37xx_cpufreq_driver_init(void)
227371
if (IS_ERR(nb_pm_base))
228372
return -ENODEV;
229373

374+
avs_base =
375+
syscon_regmap_lookup_by_compatible("marvell,armada-3700-avs");
376+
377+
/* if AVS is not present don't use it but still try to setup dvfs */
378+
if (IS_ERR(avs_base)) {
379+
pr_info("Syscon failed for Adapting Voltage Scaling: skip it\n");
380+
avs_base = NULL;
381+
}
230382
/* Before doing any configuration on the DVFS first, disable it */
231383
armada37xx_cpufreq_disable_dvfs(nb_pm_base);
232384

@@ -270,16 +422,21 @@ static int __init armada37xx_cpufreq_driver_init(void)
270422

271423
armada37xx_cpufreq_state->regmap = nb_pm_base;
272424

425+
armada37xx_cpufreq_avs_configure(avs_base, dvfs);
426+
armada37xx_cpufreq_avs_setup(avs_base, dvfs);
427+
273428
armada37xx_cpufreq_dvfs_setup(nb_pm_base, clk, dvfs->divider);
274429
clk_put(clk);
275430

276431
for (load_lvl = ARMADA_37XX_DVFS_LOAD_0; load_lvl < LOAD_LEVEL_NR;
277432
load_lvl++) {
433+
unsigned long u_volt = avs_map[dvfs->avs[load_lvl]] * 1000;
278434
freq = cur_frequency / dvfs->divider[load_lvl];
279-
280-
ret = dev_pm_opp_add(cpu_dev, freq, 0);
435+
ret = dev_pm_opp_add(cpu_dev, freq, u_volt);
281436
if (ret)
282437
goto remove_opp;
438+
439+
283440
}
284441

285442
/* Now that everything is setup, enable the DVFS at hardware level */

0 commit comments

Comments
 (0)