506-cpufreq-Add-DVFS-support-for-Armada-37xx.patch 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. From 92ce45fb875d7c3e021cc454482fe0687ff54f29 Mon Sep 17 00:00:00 2001
  2. From: Gregory CLEMENT <gregory.clement@free-electrons.com>
  3. Date: Thu, 14 Dec 2017 16:00:05 +0100
  4. Subject: cpufreq: Add DVFS support for Armada 37xx
  5. This patch adds DVFS support for the Armada 37xx SoCs
  6. There are up to four CPU frequency loads for Armada 37xx controlled by
  7. the hardware.
  8. This driver associates the CPU load level to a frequency, then the
  9. hardware will switch while selecting a load level.
  10. The hardware also can associate a voltage for each level (AVS support)
  11. but it is not yet supported
  12. Tested-by: Andre Heider <a.heider@gmail.com>
  13. Acked-by: Viresh Kumar <viresh.kumar@linaro.org>
  14. Signed-off-by: Gregory CLEMENT <gregory.clement@free-electrons.com>
  15. Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
  16. ---
  17. drivers/cpufreq/Kconfig.arm | 7 +
  18. drivers/cpufreq/Makefile | 1 +
  19. drivers/cpufreq/armada-37xx-cpufreq.c | 241 ++++++++++++++++++++++++++++++++++
  20. 3 files changed, 249 insertions(+)
  21. create mode 100644 drivers/cpufreq/armada-37xx-cpufreq.c
  22. --- a/drivers/cpufreq/Kconfig.arm
  23. +++ b/drivers/cpufreq/Kconfig.arm
  24. @@ -2,6 +2,13 @@
  25. # ARM CPU Frequency scaling drivers
  26. #
  27. +config ARM_ARMADA_37XX_CPUFREQ
  28. + tristate "Armada 37xx CPUFreq support"
  29. + depends on ARCH_MVEBU
  30. + help
  31. + This adds the CPUFreq driver support for Marvell Armada 37xx SoCs.
  32. + The Armada 37xx PMU supports 4 frequency and VDD levels.
  33. +
  34. # big LITTLE core layer and glue drivers
  35. config ARM_BIG_LITTLE_CPUFREQ
  36. tristate "Generic ARM big LITTLE CPUfreq driver"
  37. --- a/drivers/cpufreq/Makefile
  38. +++ b/drivers/cpufreq/Makefile
  39. @@ -52,6 +52,7 @@ obj-$(CONFIG_ARM_BIG_LITTLE_CPUFREQ) +=
  40. # LITTLE drivers, so that it is probed last.
  41. obj-$(CONFIG_ARM_DT_BL_CPUFREQ) += arm_big_little_dt.o
  42. +obj-$(CONFIG_ARM_ARMADA_37XX_CPUFREQ) += armada-37xx-cpufreq.o
  43. obj-$(CONFIG_ARM_BRCMSTB_AVS_CPUFREQ) += brcmstb-avs-cpufreq.o
  44. obj-$(CONFIG_ARCH_DAVINCI) += davinci-cpufreq.o
  45. obj-$(CONFIG_ARM_EXYNOS5440_CPUFREQ) += exynos5440-cpufreq.o
  46. --- /dev/null
  47. +++ b/drivers/cpufreq/armada-37xx-cpufreq.c
  48. @@ -0,0 +1,241 @@
  49. +// SPDX-License-Identifier: GPL-2.0+
  50. +/*
  51. + * CPU frequency scaling support for Armada 37xx platform.
  52. + *
  53. + * Copyright (C) 2017 Marvell
  54. + *
  55. + * Gregory CLEMENT <gregory.clement@free-electrons.com>
  56. + */
  57. +
  58. +#include <linux/clk.h>
  59. +#include <linux/cpu.h>
  60. +#include <linux/cpufreq.h>
  61. +#include <linux/err.h>
  62. +#include <linux/interrupt.h>
  63. +#include <linux/io.h>
  64. +#include <linux/mfd/syscon.h>
  65. +#include <linux/module.h>
  66. +#include <linux/of_address.h>
  67. +#include <linux/of_device.h>
  68. +#include <linux/of_irq.h>
  69. +#include <linux/platform_device.h>
  70. +#include <linux/pm_opp.h>
  71. +#include <linux/regmap.h>
  72. +#include <linux/slab.h>
  73. +
  74. +/* Power management in North Bridge register set */
  75. +#define ARMADA_37XX_NB_L0L1 0x18
  76. +#define ARMADA_37XX_NB_L2L3 0x1C
  77. +#define ARMADA_37XX_NB_TBG_DIV_OFF 13
  78. +#define ARMADA_37XX_NB_TBG_DIV_MASK 0x7
  79. +#define ARMADA_37XX_NB_CLK_SEL_OFF 11
  80. +#define ARMADA_37XX_NB_CLK_SEL_MASK 0x1
  81. +#define ARMADA_37XX_NB_CLK_SEL_TBG 0x1
  82. +#define ARMADA_37XX_NB_TBG_SEL_OFF 9
  83. +#define ARMADA_37XX_NB_TBG_SEL_MASK 0x3
  84. +#define ARMADA_37XX_NB_VDD_SEL_OFF 6
  85. +#define ARMADA_37XX_NB_VDD_SEL_MASK 0x3
  86. +#define ARMADA_37XX_NB_CONFIG_SHIFT 16
  87. +#define ARMADA_37XX_NB_DYN_MOD 0x24
  88. +#define ARMADA_37XX_NB_CLK_SEL_EN BIT(26)
  89. +#define ARMADA_37XX_NB_TBG_EN BIT(28)
  90. +#define ARMADA_37XX_NB_DIV_EN BIT(29)
  91. +#define ARMADA_37XX_NB_VDD_EN BIT(30)
  92. +#define ARMADA_37XX_NB_DFS_EN BIT(31)
  93. +#define ARMADA_37XX_NB_CPU_LOAD 0x30
  94. +#define ARMADA_37XX_NB_CPU_LOAD_MASK 0x3
  95. +#define ARMADA_37XX_DVFS_LOAD_0 0
  96. +#define ARMADA_37XX_DVFS_LOAD_1 1
  97. +#define ARMADA_37XX_DVFS_LOAD_2 2
  98. +#define ARMADA_37XX_DVFS_LOAD_3 3
  99. +
  100. +/*
  101. + * On Armada 37xx the Power management manages 4 level of CPU load,
  102. + * each level can be associated with a CPU clock source, a CPU
  103. + * divider, a VDD level, etc...
  104. + */
  105. +#define LOAD_LEVEL_NR 4
  106. +
  107. +struct armada_37xx_dvfs {
  108. + u32 cpu_freq_max;
  109. + u8 divider[LOAD_LEVEL_NR];
  110. +};
  111. +
  112. +static struct armada_37xx_dvfs armada_37xx_dvfs[] = {
  113. + {.cpu_freq_max = 1200*1000*1000, .divider = {1, 2, 4, 6} },
  114. + {.cpu_freq_max = 1000*1000*1000, .divider = {1, 2, 4, 5} },
  115. + {.cpu_freq_max = 800*1000*1000, .divider = {1, 2, 3, 4} },
  116. + {.cpu_freq_max = 600*1000*1000, .divider = {2, 4, 5, 6} },
  117. +};
  118. +
  119. +static struct armada_37xx_dvfs *armada_37xx_cpu_freq_info_get(u32 freq)
  120. +{
  121. + int i;
  122. +
  123. + for (i = 0; i < ARRAY_SIZE(armada_37xx_dvfs); i++) {
  124. + if (freq == armada_37xx_dvfs[i].cpu_freq_max)
  125. + return &armada_37xx_dvfs[i];
  126. + }
  127. +
  128. + pr_err("Unsupported CPU frequency %d MHz\n", freq/1000000);
  129. + return NULL;
  130. +}
  131. +
  132. +/*
  133. + * Setup the four level managed by the hardware. Once the four level
  134. + * will be configured then the DVFS will be enabled.
  135. + */
  136. +static void __init armada37xx_cpufreq_dvfs_setup(struct regmap *base,
  137. + struct clk *clk, u8 *divider)
  138. +{
  139. + int load_lvl;
  140. + struct clk *parent;
  141. +
  142. + for (load_lvl = 0; load_lvl < LOAD_LEVEL_NR; load_lvl++) {
  143. + unsigned int reg, mask, val, offset = 0;
  144. +
  145. + if (load_lvl <= ARMADA_37XX_DVFS_LOAD_1)
  146. + reg = ARMADA_37XX_NB_L0L1;
  147. + else
  148. + reg = ARMADA_37XX_NB_L2L3;
  149. +
  150. + if (load_lvl == ARMADA_37XX_DVFS_LOAD_0 ||
  151. + load_lvl == ARMADA_37XX_DVFS_LOAD_2)
  152. + offset += ARMADA_37XX_NB_CONFIG_SHIFT;
  153. +
  154. + /* Set cpu clock source, for all the level we use TBG */
  155. + val = ARMADA_37XX_NB_CLK_SEL_TBG << ARMADA_37XX_NB_CLK_SEL_OFF;
  156. + mask = (ARMADA_37XX_NB_CLK_SEL_MASK
  157. + << ARMADA_37XX_NB_CLK_SEL_OFF);
  158. +
  159. + /*
  160. + * Set cpu divider based on the pre-computed array in
  161. + * order to have balanced step.
  162. + */
  163. + val |= divider[load_lvl] << ARMADA_37XX_NB_TBG_DIV_OFF;
  164. + mask |= (ARMADA_37XX_NB_TBG_DIV_MASK
  165. + << ARMADA_37XX_NB_TBG_DIV_OFF);
  166. +
  167. + /* Set VDD divider which is actually the load level. */
  168. + val |= load_lvl << ARMADA_37XX_NB_VDD_SEL_OFF;
  169. + mask |= (ARMADA_37XX_NB_VDD_SEL_MASK
  170. + << ARMADA_37XX_NB_VDD_SEL_OFF);
  171. +
  172. + val <<= offset;
  173. + mask <<= offset;
  174. +
  175. + regmap_update_bits(base, reg, mask, val);
  176. + }
  177. +
  178. + /*
  179. + * Set cpu clock source, for all the level we keep the same
  180. + * clock source that the one already configured. For this one
  181. + * we need to use the clock framework
  182. + */
  183. + parent = clk_get_parent(clk);
  184. + clk_set_parent(clk, parent);
  185. +}
  186. +
  187. +static void __init armada37xx_cpufreq_disable_dvfs(struct regmap *base)
  188. +{
  189. + unsigned int reg = ARMADA_37XX_NB_DYN_MOD,
  190. + mask = ARMADA_37XX_NB_DFS_EN;
  191. +
  192. + regmap_update_bits(base, reg, mask, 0);
  193. +}
  194. +
  195. +static void __init armada37xx_cpufreq_enable_dvfs(struct regmap *base)
  196. +{
  197. + unsigned int val, reg = ARMADA_37XX_NB_CPU_LOAD,
  198. + mask = ARMADA_37XX_NB_CPU_LOAD_MASK;
  199. +
  200. + /* Start with the highest load (0) */
  201. + val = ARMADA_37XX_DVFS_LOAD_0;
  202. + regmap_update_bits(base, reg, mask, val);
  203. +
  204. + /* Now enable DVFS for the CPUs */
  205. + reg = ARMADA_37XX_NB_DYN_MOD;
  206. + mask = ARMADA_37XX_NB_CLK_SEL_EN | ARMADA_37XX_NB_TBG_EN |
  207. + ARMADA_37XX_NB_DIV_EN | ARMADA_37XX_NB_VDD_EN |
  208. + ARMADA_37XX_NB_DFS_EN;
  209. +
  210. + regmap_update_bits(base, reg, mask, mask);
  211. +}
  212. +
  213. +static int __init armada37xx_cpufreq_driver_init(void)
  214. +{
  215. + struct armada_37xx_dvfs *dvfs;
  216. + struct platform_device *pdev;
  217. + unsigned int cur_frequency;
  218. + struct regmap *nb_pm_base;
  219. + struct device *cpu_dev;
  220. + int load_lvl, ret;
  221. + struct clk *clk;
  222. +
  223. + nb_pm_base =
  224. + syscon_regmap_lookup_by_compatible("marvell,armada-3700-nb-pm");
  225. +
  226. + if (IS_ERR(nb_pm_base))
  227. + return -ENODEV;
  228. +
  229. + /* Before doing any configuration on the DVFS first, disable it */
  230. + armada37xx_cpufreq_disable_dvfs(nb_pm_base);
  231. +
  232. + /*
  233. + * On CPU 0 register the operating points supported (which are
  234. + * the nominal CPU frequency and full integer divisions of
  235. + * it).
  236. + */
  237. + cpu_dev = get_cpu_device(0);
  238. + if (!cpu_dev) {
  239. + dev_err(cpu_dev, "Cannot get CPU\n");
  240. + return -ENODEV;
  241. + }
  242. +
  243. + clk = clk_get(cpu_dev, 0);
  244. + if (IS_ERR(clk)) {
  245. + dev_err(cpu_dev, "Cannot get clock for CPU0\n");
  246. + return PTR_ERR(clk);
  247. + }
  248. +
  249. + /* Get nominal (current) CPU frequency */
  250. + cur_frequency = clk_get_rate(clk);
  251. + if (!cur_frequency) {
  252. + dev_err(cpu_dev, "Failed to get clock rate for CPU\n");
  253. + return -EINVAL;
  254. + }
  255. +
  256. + dvfs = armada_37xx_cpu_freq_info_get(cur_frequency);
  257. + if (!dvfs)
  258. + return -EINVAL;
  259. +
  260. + armada37xx_cpufreq_dvfs_setup(nb_pm_base, clk, dvfs->divider);
  261. +
  262. + for (load_lvl = ARMADA_37XX_DVFS_LOAD_0; load_lvl < LOAD_LEVEL_NR;
  263. + load_lvl++) {
  264. + unsigned long freq = cur_frequency / dvfs->divider[load_lvl];
  265. +
  266. + ret = dev_pm_opp_add(cpu_dev, freq, 0);
  267. + if (ret) {
  268. + /* clean-up the already added opp before leaving */
  269. + while (load_lvl-- > ARMADA_37XX_DVFS_LOAD_0) {
  270. + freq = cur_frequency / dvfs->divider[load_lvl];
  271. + dev_pm_opp_remove(cpu_dev, freq);
  272. + }
  273. + return ret;
  274. + }
  275. + }
  276. +
  277. + /* Now that everything is setup, enable the DVFS at hardware level */
  278. + armada37xx_cpufreq_enable_dvfs(nb_pm_base);
  279. +
  280. + pdev = platform_device_register_simple("cpufreq-dt", -1, NULL, 0);
  281. +
  282. + return PTR_ERR_OR_ZERO(pdev);
  283. +}
  284. +/* late_initcall, to guarantee the driver is loaded after A37xx clock driver */
  285. +late_initcall(armada37xx_cpufreq_driver_init);
  286. +
  287. +MODULE_AUTHOR("Gregory CLEMENT <gregory.clement@free-electrons.com>");
  288. +MODULE_DESCRIPTION("Armada 37xx cpufreq driver");
  289. +MODULE_LICENSE("GPL");