Path: blob/master/drivers/cpufreq/armada-37xx-cpufreq.c
26278 views
// SPDX-License-Identifier: GPL-2.0+1/*2* CPU frequency scaling support for Armada 37xx platform.3*4* Copyright (C) 2017 Marvell5*6* Gregory CLEMENT <[email protected]>7*/89#include <linux/clk.h>10#include <linux/cpu.h>11#include <linux/cpufreq.h>12#include <linux/err.h>13#include <linux/interrupt.h>14#include <linux/io.h>15#include <linux/mfd/syscon.h>16#include <linux/mod_devicetable.h>17#include <linux/module.h>18#include <linux/platform_device.h>19#include <linux/pm_opp.h>20#include <linux/regmap.h>21#include <linux/slab.h>2223#include "cpufreq-dt.h"2425/* Clk register set */26#define ARMADA_37XX_CLK_TBG_SEL 027#define ARMADA_37XX_CLK_TBG_SEL_CPU_OFF 222829/* Power management in North Bridge register set */30#define ARMADA_37XX_NB_L0L1 0x1831#define ARMADA_37XX_NB_L2L3 0x1C32#define ARMADA_37XX_NB_TBG_DIV_OFF 1333#define ARMADA_37XX_NB_TBG_DIV_MASK 0x734#define ARMADA_37XX_NB_CLK_SEL_OFF 1135#define ARMADA_37XX_NB_CLK_SEL_MASK 0x136#define ARMADA_37XX_NB_CLK_SEL_TBG 0x137#define ARMADA_37XX_NB_TBG_SEL_OFF 938#define ARMADA_37XX_NB_TBG_SEL_MASK 0x339#define ARMADA_37XX_NB_VDD_SEL_OFF 640#define ARMADA_37XX_NB_VDD_SEL_MASK 0x341#define ARMADA_37XX_NB_CONFIG_SHIFT 1642#define ARMADA_37XX_NB_DYN_MOD 0x2443#define ARMADA_37XX_NB_CLK_SEL_EN BIT(26)44#define ARMADA_37XX_NB_TBG_EN BIT(28)45#define ARMADA_37XX_NB_DIV_EN BIT(29)46#define ARMADA_37XX_NB_VDD_EN BIT(30)47#define ARMADA_37XX_NB_DFS_EN BIT(31)48#define ARMADA_37XX_NB_CPU_LOAD 0x3049#define ARMADA_37XX_NB_CPU_LOAD_MASK 0x350#define ARMADA_37XX_DVFS_LOAD_0 051#define ARMADA_37XX_DVFS_LOAD_1 152#define ARMADA_37XX_DVFS_LOAD_2 253#define ARMADA_37XX_DVFS_LOAD_3 35455/* AVS register set */56#define ARMADA_37XX_AVS_CTL0 0x057#define ARMADA_37XX_AVS_ENABLE BIT(30)58#define ARMADA_37XX_AVS_HIGH_VDD_LIMIT 1659#define ARMADA_37XX_AVS_LOW_VDD_LIMIT 2260#define ARMADA_37XX_AVS_VDD_MASK 0x3F61#define ARMADA_37XX_AVS_CTL2 0x862#define ARMADA_37XX_AVS_LOW_VDD_EN BIT(6)63#define ARMADA_37XX_AVS_VSET(x) (0x1C + 4 * (x))6465/*66* On Armada 37xx the Power management manages 4 level of CPU load,67* each level can be associated with a CPU clock source, a CPU68* divider, a VDD level, etc...69*/70#define LOAD_LEVEL_NR 47172#define MIN_VOLT_MV 100073#define MIN_VOLT_MV_FOR_L1_1000MHZ 110874#define MIN_VOLT_MV_FOR_L1_1200MHZ 11557576/* AVS value for the corresponding voltage (in mV) */77static int avs_map[] = {78747, 758, 770, 782, 793, 805, 817, 828, 840, 852, 863, 875, 887, 898,79910, 922, 933, 945, 957, 968, 980, 992, 1003, 1015, 1027, 1038, 1050,801062, 1073, 1085, 1097, 1108, 1120, 1132, 1143, 1155, 1167, 1178, 1190,811202, 1213, 1225, 1237, 1248, 1260, 1272, 1283, 1295, 1307, 1318, 1330,82134283};8485struct armada37xx_cpufreq_state {86struct platform_device *pdev;87struct device *cpu_dev;88struct regmap *regmap;89u32 nb_l0l1;90u32 nb_l2l3;91u32 nb_dyn_mod;92u32 nb_cpu_load;93};9495static struct armada37xx_cpufreq_state *armada37xx_cpufreq_state;9697struct armada_37xx_dvfs {98u32 cpu_freq_max;99u8 divider[LOAD_LEVEL_NR];100u32 avs[LOAD_LEVEL_NR];101};102103static struct armada_37xx_dvfs armada_37xx_dvfs[] = {104{.cpu_freq_max = 1200*1000*1000, .divider = {1, 2, 4, 6} },105{.cpu_freq_max = 1000*1000*1000, .divider = {1, 2, 4, 5} },106{.cpu_freq_max = 800*1000*1000, .divider = {1, 2, 3, 4} },107{.cpu_freq_max = 600*1000*1000, .divider = {2, 4, 5, 6} },108};109110static struct armada_37xx_dvfs *armada_37xx_cpu_freq_info_get(u32 freq)111{112int i;113114for (i = 0; i < ARRAY_SIZE(armada_37xx_dvfs); i++) {115if (freq == armada_37xx_dvfs[i].cpu_freq_max)116return &armada_37xx_dvfs[i];117}118119pr_err("Unsupported CPU frequency %d MHz\n", freq/1000000);120return NULL;121}122123/*124* Setup the four level managed by the hardware. Once the four level125* will be configured then the DVFS will be enabled.126*/127static void __init armada37xx_cpufreq_dvfs_setup(struct regmap *base,128struct regmap *clk_base, u8 *divider)129{130u32 cpu_tbg_sel;131int load_lvl;132133/* Determine to which TBG clock is CPU connected */134regmap_read(clk_base, ARMADA_37XX_CLK_TBG_SEL, &cpu_tbg_sel);135cpu_tbg_sel >>= ARMADA_37XX_CLK_TBG_SEL_CPU_OFF;136cpu_tbg_sel &= ARMADA_37XX_NB_TBG_SEL_MASK;137138for (load_lvl = 0; load_lvl < LOAD_LEVEL_NR; load_lvl++) {139unsigned int reg, mask, val, offset = 0;140141if (load_lvl <= ARMADA_37XX_DVFS_LOAD_1)142reg = ARMADA_37XX_NB_L0L1;143else144reg = ARMADA_37XX_NB_L2L3;145146if (load_lvl == ARMADA_37XX_DVFS_LOAD_0 ||147load_lvl == ARMADA_37XX_DVFS_LOAD_2)148offset += ARMADA_37XX_NB_CONFIG_SHIFT;149150/* Set cpu clock source, for all the level we use TBG */151val = ARMADA_37XX_NB_CLK_SEL_TBG << ARMADA_37XX_NB_CLK_SEL_OFF;152mask = (ARMADA_37XX_NB_CLK_SEL_MASK153<< ARMADA_37XX_NB_CLK_SEL_OFF);154155/* Set TBG index, for all levels we use the same TBG */156val = cpu_tbg_sel << ARMADA_37XX_NB_TBG_SEL_OFF;157mask = (ARMADA_37XX_NB_TBG_SEL_MASK158<< ARMADA_37XX_NB_TBG_SEL_OFF);159160/*161* Set cpu divider based on the pre-computed array in162* order to have balanced step.163*/164val |= divider[load_lvl] << ARMADA_37XX_NB_TBG_DIV_OFF;165mask |= (ARMADA_37XX_NB_TBG_DIV_MASK166<< ARMADA_37XX_NB_TBG_DIV_OFF);167168/* Set VDD divider which is actually the load level. */169val |= load_lvl << ARMADA_37XX_NB_VDD_SEL_OFF;170mask |= (ARMADA_37XX_NB_VDD_SEL_MASK171<< ARMADA_37XX_NB_VDD_SEL_OFF);172173val <<= offset;174mask <<= offset;175176regmap_update_bits(base, reg, mask, val);177}178}179180/*181* Find out the armada 37x supported AVS value whose voltage value is182* the round-up closest to the target voltage value.183*/184static u32 armada_37xx_avs_val_match(int target_vm)185{186u32 avs;187188/* Find out the round-up closest supported voltage value */189for (avs = 0; avs < ARRAY_SIZE(avs_map); avs++)190if (avs_map[avs] >= target_vm)191break;192193/*194* If all supported voltages are smaller than target one,195* choose the largest supported voltage196*/197if (avs == ARRAY_SIZE(avs_map))198avs = ARRAY_SIZE(avs_map) - 1;199200return avs;201}202203/*204* For Armada 37xx soc, L0(VSET0) VDD AVS value is set to SVC revision205* value or a default value when SVC is not supported.206* - L0 can be read out from the register of AVS_CTRL_0 and L0 voltage207* can be got from the mapping table of avs_map.208* - L1 voltage should be about 100mv smaller than L0 voltage209* - L2 & L3 voltage should be about 150mv smaller than L0 voltage.210* This function calculates L1 & L2 & L3 AVS values dynamically based211* on L0 voltage and fill all AVS values to the AVS value table.212* When base CPU frequency is 1000 or 1200 MHz then there is additional213* minimal avs value for load L1.214*/215static void __init armada37xx_cpufreq_avs_configure(struct regmap *base,216struct armada_37xx_dvfs *dvfs)217{218unsigned int target_vm;219int load_level = 0;220u32 l0_vdd_min;221222if (base == NULL)223return;224225/* Get L0 VDD min value */226regmap_read(base, ARMADA_37XX_AVS_CTL0, &l0_vdd_min);227l0_vdd_min = (l0_vdd_min >> ARMADA_37XX_AVS_LOW_VDD_LIMIT) &228ARMADA_37XX_AVS_VDD_MASK;229if (l0_vdd_min >= ARRAY_SIZE(avs_map)) {230pr_err("L0 VDD MIN %d is not correct.\n", l0_vdd_min);231return;232}233dvfs->avs[0] = l0_vdd_min;234235if (avs_map[l0_vdd_min] <= MIN_VOLT_MV) {236/*237* If L0 voltage is smaller than 1000mv, then all VDD sets238* use L0 voltage;239*/240u32 avs_min = armada_37xx_avs_val_match(MIN_VOLT_MV);241242for (load_level = 1; load_level < LOAD_LEVEL_NR; load_level++)243dvfs->avs[load_level] = avs_min;244245/*246* Set the avs values for load L0 and L1 when base CPU frequency247* is 1000/1200 MHz to its typical initial values according to248* the Armada 3700 Hardware Specifications.249*/250if (dvfs->cpu_freq_max >= 1000*1000*1000) {251if (dvfs->cpu_freq_max >= 1200*1000*1000)252avs_min = armada_37xx_avs_val_match(MIN_VOLT_MV_FOR_L1_1200MHZ);253else254avs_min = armada_37xx_avs_val_match(MIN_VOLT_MV_FOR_L1_1000MHZ);255dvfs->avs[0] = dvfs->avs[1] = avs_min;256}257258return;259}260261/*262* L1 voltage is equal to L0 voltage - 100mv and it must be263* larger than 1000mv264*/265266target_vm = avs_map[l0_vdd_min] - 100;267target_vm = target_vm > MIN_VOLT_MV ? target_vm : MIN_VOLT_MV;268dvfs->avs[1] = armada_37xx_avs_val_match(target_vm);269270/*271* L2 & L3 voltage is equal to L0 voltage - 150mv and it must272* be larger than 1000mv273*/274target_vm = avs_map[l0_vdd_min] - 150;275target_vm = target_vm > MIN_VOLT_MV ? target_vm : MIN_VOLT_MV;276dvfs->avs[2] = dvfs->avs[3] = armada_37xx_avs_val_match(target_vm);277278/*279* Fix the avs value for load L1 when base CPU frequency is 1000/1200 MHz,280* otherwise the CPU gets stuck when switching from load L1 to load L0.281* Also ensure that avs value for load L1 is not higher than for L0.282*/283if (dvfs->cpu_freq_max >= 1000*1000*1000) {284u32 avs_min_l1;285286if (dvfs->cpu_freq_max >= 1200*1000*1000)287avs_min_l1 = armada_37xx_avs_val_match(MIN_VOLT_MV_FOR_L1_1200MHZ);288else289avs_min_l1 = armada_37xx_avs_val_match(MIN_VOLT_MV_FOR_L1_1000MHZ);290291if (avs_min_l1 > dvfs->avs[0])292avs_min_l1 = dvfs->avs[0];293294if (dvfs->avs[1] < avs_min_l1)295dvfs->avs[1] = avs_min_l1;296}297}298299static void __init armada37xx_cpufreq_avs_setup(struct regmap *base,300struct armada_37xx_dvfs *dvfs)301{302unsigned int avs_val = 0;303int load_level = 0;304305if (base == NULL)306return;307308/* Disable AVS before the configuration */309regmap_update_bits(base, ARMADA_37XX_AVS_CTL0,310ARMADA_37XX_AVS_ENABLE, 0);311312313/* Enable low voltage mode */314regmap_update_bits(base, ARMADA_37XX_AVS_CTL2,315ARMADA_37XX_AVS_LOW_VDD_EN,316ARMADA_37XX_AVS_LOW_VDD_EN);317318319for (load_level = 1; load_level < LOAD_LEVEL_NR; load_level++) {320avs_val = dvfs->avs[load_level];321regmap_update_bits(base, ARMADA_37XX_AVS_VSET(load_level-1),322ARMADA_37XX_AVS_VDD_MASK << ARMADA_37XX_AVS_HIGH_VDD_LIMIT |323ARMADA_37XX_AVS_VDD_MASK << ARMADA_37XX_AVS_LOW_VDD_LIMIT,324avs_val << ARMADA_37XX_AVS_HIGH_VDD_LIMIT |325avs_val << ARMADA_37XX_AVS_LOW_VDD_LIMIT);326}327328/* Enable AVS after the configuration */329regmap_update_bits(base, ARMADA_37XX_AVS_CTL0,330ARMADA_37XX_AVS_ENABLE,331ARMADA_37XX_AVS_ENABLE);332333}334335static void armada37xx_cpufreq_disable_dvfs(struct regmap *base)336{337unsigned int reg = ARMADA_37XX_NB_DYN_MOD,338mask = ARMADA_37XX_NB_DFS_EN;339340regmap_update_bits(base, reg, mask, 0);341}342343static void __init armada37xx_cpufreq_enable_dvfs(struct regmap *base)344{345unsigned int val, reg = ARMADA_37XX_NB_CPU_LOAD,346mask = ARMADA_37XX_NB_CPU_LOAD_MASK;347348/* Start with the highest load (0) */349val = ARMADA_37XX_DVFS_LOAD_0;350regmap_update_bits(base, reg, mask, val);351352/* Now enable DVFS for the CPUs */353reg = ARMADA_37XX_NB_DYN_MOD;354mask = ARMADA_37XX_NB_CLK_SEL_EN | ARMADA_37XX_NB_TBG_EN |355ARMADA_37XX_NB_DIV_EN | ARMADA_37XX_NB_VDD_EN |356ARMADA_37XX_NB_DFS_EN;357358regmap_update_bits(base, reg, mask, mask);359}360361static int armada37xx_cpufreq_suspend(struct cpufreq_policy *policy)362{363struct armada37xx_cpufreq_state *state = armada37xx_cpufreq_state;364365regmap_read(state->regmap, ARMADA_37XX_NB_L0L1, &state->nb_l0l1);366regmap_read(state->regmap, ARMADA_37XX_NB_L2L3, &state->nb_l2l3);367regmap_read(state->regmap, ARMADA_37XX_NB_CPU_LOAD,368&state->nb_cpu_load);369regmap_read(state->regmap, ARMADA_37XX_NB_DYN_MOD, &state->nb_dyn_mod);370371return 0;372}373374static int armada37xx_cpufreq_resume(struct cpufreq_policy *policy)375{376struct armada37xx_cpufreq_state *state = armada37xx_cpufreq_state;377378/* Ensure DVFS is disabled otherwise the following registers are RO */379armada37xx_cpufreq_disable_dvfs(state->regmap);380381regmap_write(state->regmap, ARMADA_37XX_NB_L0L1, state->nb_l0l1);382regmap_write(state->regmap, ARMADA_37XX_NB_L2L3, state->nb_l2l3);383regmap_write(state->regmap, ARMADA_37XX_NB_CPU_LOAD,384state->nb_cpu_load);385386/*387* NB_DYN_MOD register is the one that actually enable back DVFS if it388* was enabled before the suspend operation. This must be done last389* otherwise other registers are not writable.390*/391regmap_write(state->regmap, ARMADA_37XX_NB_DYN_MOD, state->nb_dyn_mod);392393return 0;394}395396static int __init armada37xx_cpufreq_driver_init(void)397{398struct cpufreq_dt_platform_data pdata;399struct armada_37xx_dvfs *dvfs;400struct platform_device *pdev;401unsigned long freq;402unsigned int base_frequency;403struct regmap *nb_clk_base, *nb_pm_base, *avs_base;404struct device *cpu_dev;405int load_lvl, ret;406struct clk *clk, *parent;407408nb_clk_base =409syscon_regmap_lookup_by_compatible("marvell,armada-3700-periph-clock-nb");410if (IS_ERR(nb_clk_base))411return -ENODEV;412413nb_pm_base =414syscon_regmap_lookup_by_compatible("marvell,armada-3700-nb-pm");415416if (IS_ERR(nb_pm_base))417return -ENODEV;418419avs_base =420syscon_regmap_lookup_by_compatible("marvell,armada-3700-avs");421422/* if AVS is not present don't use it but still try to setup dvfs */423if (IS_ERR(avs_base)) {424pr_info("Syscon failed for Adapting Voltage Scaling: skip it\n");425avs_base = NULL;426}427/* Before doing any configuration on the DVFS first, disable it */428armada37xx_cpufreq_disable_dvfs(nb_pm_base);429430/*431* On CPU 0 register the operating points supported (which are432* the nominal CPU frequency and full integer divisions of433* it).434*/435cpu_dev = get_cpu_device(0);436if (!cpu_dev) {437dev_err(cpu_dev, "Cannot get CPU\n");438return -ENODEV;439}440441clk = clk_get(cpu_dev, NULL);442if (IS_ERR(clk)) {443dev_err(cpu_dev, "Cannot get clock for CPU0\n");444return PTR_ERR(clk);445}446447parent = clk_get_parent(clk);448if (IS_ERR(parent)) {449dev_err(cpu_dev, "Cannot get parent clock for CPU0\n");450clk_put(clk);451return PTR_ERR(parent);452}453454/* Get parent CPU frequency */455base_frequency = clk_get_rate(parent);456457if (!base_frequency) {458dev_err(cpu_dev, "Failed to get parent clock rate for CPU\n");459clk_put(clk);460return -EINVAL;461}462463dvfs = armada_37xx_cpu_freq_info_get(base_frequency);464if (!dvfs) {465clk_put(clk);466return -EINVAL;467}468469armada37xx_cpufreq_state = kmalloc(sizeof(*armada37xx_cpufreq_state),470GFP_KERNEL);471if (!armada37xx_cpufreq_state) {472clk_put(clk);473return -ENOMEM;474}475476armada37xx_cpufreq_state->regmap = nb_pm_base;477478armada37xx_cpufreq_avs_configure(avs_base, dvfs);479armada37xx_cpufreq_avs_setup(avs_base, dvfs);480481armada37xx_cpufreq_dvfs_setup(nb_pm_base, nb_clk_base, dvfs->divider);482clk_put(clk);483484for (load_lvl = ARMADA_37XX_DVFS_LOAD_0; load_lvl < LOAD_LEVEL_NR;485load_lvl++) {486unsigned long u_volt = avs_map[dvfs->avs[load_lvl]] * 1000;487freq = base_frequency / dvfs->divider[load_lvl];488ret = dev_pm_opp_add(cpu_dev, freq, u_volt);489if (ret)490goto remove_opp;491492493}494495/* Now that everything is setup, enable the DVFS at hardware level */496armada37xx_cpufreq_enable_dvfs(nb_pm_base);497498memset(&pdata, 0, sizeof(pdata));499pdata.suspend = armada37xx_cpufreq_suspend;500pdata.resume = armada37xx_cpufreq_resume;501502pdev = platform_device_register_data(NULL, "cpufreq-dt", -1, &pdata,503sizeof(pdata));504ret = PTR_ERR_OR_ZERO(pdev);505if (ret)506goto disable_dvfs;507508armada37xx_cpufreq_state->cpu_dev = cpu_dev;509armada37xx_cpufreq_state->pdev = pdev;510platform_set_drvdata(pdev, dvfs);511return 0;512513disable_dvfs:514armada37xx_cpufreq_disable_dvfs(nb_pm_base);515remove_opp:516/* clean-up the already added opp before leaving */517while (load_lvl-- > ARMADA_37XX_DVFS_LOAD_0) {518freq = base_frequency / dvfs->divider[load_lvl];519dev_pm_opp_remove(cpu_dev, freq);520}521522kfree(armada37xx_cpufreq_state);523524return ret;525}526/* late_initcall, to guarantee the driver is loaded after A37xx clock driver */527late_initcall(armada37xx_cpufreq_driver_init);528529static void __exit armada37xx_cpufreq_driver_exit(void)530{531struct platform_device *pdev = armada37xx_cpufreq_state->pdev;532struct armada_37xx_dvfs *dvfs = platform_get_drvdata(pdev);533unsigned long freq;534int load_lvl;535536platform_device_unregister(pdev);537538armada37xx_cpufreq_disable_dvfs(armada37xx_cpufreq_state->regmap);539540for (load_lvl = ARMADA_37XX_DVFS_LOAD_0; load_lvl < LOAD_LEVEL_NR; load_lvl++) {541freq = dvfs->cpu_freq_max / dvfs->divider[load_lvl];542dev_pm_opp_remove(armada37xx_cpufreq_state->cpu_dev, freq);543}544545kfree(armada37xx_cpufreq_state);546}547module_exit(armada37xx_cpufreq_driver_exit);548549static const struct of_device_id __maybe_unused armada37xx_cpufreq_of_match[] = {550{ .compatible = "marvell,armada-3700-nb-pm" },551{ },552};553MODULE_DEVICE_TABLE(of, armada37xx_cpufreq_of_match);554555MODULE_AUTHOR("Gregory CLEMENT <[email protected]>");556MODULE_DESCRIPTION("Armada 37xx cpufreq driver");557MODULE_LICENSE("GPL");558559560