armada-37xx-cpufreq.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * CPU frequency scaling support for Armada 37xx platform.
  4. *
  5. * Copyright (C) 2017 Marvell
  6. *
  7. * Gregory CLEMENT <gregory.clement@free-electrons.com>
  8. */
  9. #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/module.h>
  17. #include <linux/of_address.h>
  18. #include <linux/of_device.h>
  19. #include <linux/of_irq.h>
  20. #include <linux/platform_device.h>
  21. #include <linux/pm_opp.h>
  22. #include <linux/regmap.h>
  23. #include <linux/slab.h>
  24. #include "cpufreq-dt.h"
  25. /* Power management in North Bridge register set */
  26. #define ARMADA_37XX_NB_L0L1 0x18
  27. #define ARMADA_37XX_NB_L2L3 0x1C
  28. #define ARMADA_37XX_NB_TBG_DIV_OFF 13
  29. #define ARMADA_37XX_NB_TBG_DIV_MASK 0x7
  30. #define ARMADA_37XX_NB_CLK_SEL_OFF 11
  31. #define ARMADA_37XX_NB_CLK_SEL_MASK 0x1
  32. #define ARMADA_37XX_NB_CLK_SEL_TBG 0x1
  33. #define ARMADA_37XX_NB_TBG_SEL_OFF 9
  34. #define ARMADA_37XX_NB_TBG_SEL_MASK 0x3
  35. #define ARMADA_37XX_NB_VDD_SEL_OFF 6
  36. #define ARMADA_37XX_NB_VDD_SEL_MASK 0x3
  37. #define ARMADA_37XX_NB_CONFIG_SHIFT 16
  38. #define ARMADA_37XX_NB_DYN_MOD 0x24
  39. #define ARMADA_37XX_NB_CLK_SEL_EN BIT(26)
  40. #define ARMADA_37XX_NB_TBG_EN BIT(28)
  41. #define ARMADA_37XX_NB_DIV_EN BIT(29)
  42. #define ARMADA_37XX_NB_VDD_EN BIT(30)
  43. #define ARMADA_37XX_NB_DFS_EN BIT(31)
  44. #define ARMADA_37XX_NB_CPU_LOAD 0x30
  45. #define ARMADA_37XX_NB_CPU_LOAD_MASK 0x3
  46. #define ARMADA_37XX_DVFS_LOAD_0 0
  47. #define ARMADA_37XX_DVFS_LOAD_1 1
  48. #define ARMADA_37XX_DVFS_LOAD_2 2
  49. #define ARMADA_37XX_DVFS_LOAD_3 3
  50. /*
  51. * On Armada 37xx the Power management manages 4 level of CPU load,
  52. * each level can be associated with a CPU clock source, a CPU
  53. * divider, a VDD level, etc...
  54. */
  55. #define LOAD_LEVEL_NR 4
  56. struct armada37xx_cpufreq_state {
  57. struct regmap *regmap;
  58. u32 nb_l0l1;
  59. u32 nb_l2l3;
  60. u32 nb_dyn_mod;
  61. u32 nb_cpu_load;
  62. };
  63. static struct armada37xx_cpufreq_state *armada37xx_cpufreq_state;
  64. struct armada_37xx_dvfs {
  65. u32 cpu_freq_max;
  66. u8 divider[LOAD_LEVEL_NR];
  67. };
  68. static struct armada_37xx_dvfs armada_37xx_dvfs[] = {
  69. {.cpu_freq_max = 1200*1000*1000, .divider = {1, 2, 4, 6} },
  70. {.cpu_freq_max = 1000*1000*1000, .divider = {1, 2, 4, 5} },
  71. {.cpu_freq_max = 800*1000*1000, .divider = {1, 2, 3, 4} },
  72. {.cpu_freq_max = 600*1000*1000, .divider = {2, 4, 5, 6} },
  73. };
  74. static struct armada_37xx_dvfs *armada_37xx_cpu_freq_info_get(u32 freq)
  75. {
  76. int i;
  77. for (i = 0; i < ARRAY_SIZE(armada_37xx_dvfs); i++) {
  78. if (freq == armada_37xx_dvfs[i].cpu_freq_max)
  79. return &armada_37xx_dvfs[i];
  80. }
  81. pr_err("Unsupported CPU frequency %d MHz\n", freq/1000000);
  82. return NULL;
  83. }
  84. /*
  85. * Setup the four level managed by the hardware. Once the four level
  86. * will be configured then the DVFS will be enabled.
  87. */
  88. static void __init armada37xx_cpufreq_dvfs_setup(struct regmap *base,
  89. struct clk *clk, u8 *divider)
  90. {
  91. int load_lvl;
  92. struct clk *parent;
  93. for (load_lvl = 0; load_lvl < LOAD_LEVEL_NR; load_lvl++) {
  94. unsigned int reg, mask, val, offset = 0;
  95. if (load_lvl <= ARMADA_37XX_DVFS_LOAD_1)
  96. reg = ARMADA_37XX_NB_L0L1;
  97. else
  98. reg = ARMADA_37XX_NB_L2L3;
  99. if (load_lvl == ARMADA_37XX_DVFS_LOAD_0 ||
  100. load_lvl == ARMADA_37XX_DVFS_LOAD_2)
  101. offset += ARMADA_37XX_NB_CONFIG_SHIFT;
  102. /* Set cpu clock source, for all the level we use TBG */
  103. val = ARMADA_37XX_NB_CLK_SEL_TBG << ARMADA_37XX_NB_CLK_SEL_OFF;
  104. mask = (ARMADA_37XX_NB_CLK_SEL_MASK
  105. << ARMADA_37XX_NB_CLK_SEL_OFF);
  106. /*
  107. * Set cpu divider based on the pre-computed array in
  108. * order to have balanced step.
  109. */
  110. val |= divider[load_lvl] << ARMADA_37XX_NB_TBG_DIV_OFF;
  111. mask |= (ARMADA_37XX_NB_TBG_DIV_MASK
  112. << ARMADA_37XX_NB_TBG_DIV_OFF);
  113. /* Set VDD divider which is actually the load level. */
  114. val |= load_lvl << ARMADA_37XX_NB_VDD_SEL_OFF;
  115. mask |= (ARMADA_37XX_NB_VDD_SEL_MASK
  116. << ARMADA_37XX_NB_VDD_SEL_OFF);
  117. val <<= offset;
  118. mask <<= offset;
  119. regmap_update_bits(base, reg, mask, val);
  120. }
  121. /*
  122. * Set cpu clock source, for all the level we keep the same
  123. * clock source that the one already configured. For this one
  124. * we need to use the clock framework
  125. */
  126. parent = clk_get_parent(clk);
  127. clk_set_parent(clk, parent);
  128. }
  129. static void armada37xx_cpufreq_disable_dvfs(struct regmap *base)
  130. {
  131. unsigned int reg = ARMADA_37XX_NB_DYN_MOD,
  132. mask = ARMADA_37XX_NB_DFS_EN;
  133. regmap_update_bits(base, reg, mask, 0);
  134. }
  135. static void __init armada37xx_cpufreq_enable_dvfs(struct regmap *base)
  136. {
  137. unsigned int val, reg = ARMADA_37XX_NB_CPU_LOAD,
  138. mask = ARMADA_37XX_NB_CPU_LOAD_MASK;
  139. /* Start with the highest load (0) */
  140. val = ARMADA_37XX_DVFS_LOAD_0;
  141. regmap_update_bits(base, reg, mask, val);
  142. /* Now enable DVFS for the CPUs */
  143. reg = ARMADA_37XX_NB_DYN_MOD;
  144. mask = ARMADA_37XX_NB_CLK_SEL_EN | ARMADA_37XX_NB_TBG_EN |
  145. ARMADA_37XX_NB_DIV_EN | ARMADA_37XX_NB_VDD_EN |
  146. ARMADA_37XX_NB_DFS_EN;
  147. regmap_update_bits(base, reg, mask, mask);
  148. }
  149. static int armada37xx_cpufreq_suspend(struct cpufreq_policy *policy)
  150. {
  151. struct armada37xx_cpufreq_state *state = armada37xx_cpufreq_state;
  152. regmap_read(state->regmap, ARMADA_37XX_NB_L0L1, &state->nb_l0l1);
  153. regmap_read(state->regmap, ARMADA_37XX_NB_L2L3, &state->nb_l2l3);
  154. regmap_read(state->regmap, ARMADA_37XX_NB_CPU_LOAD,
  155. &state->nb_cpu_load);
  156. regmap_read(state->regmap, ARMADA_37XX_NB_DYN_MOD, &state->nb_dyn_mod);
  157. return 0;
  158. }
  159. static int armada37xx_cpufreq_resume(struct cpufreq_policy *policy)
  160. {
  161. struct armada37xx_cpufreq_state *state = armada37xx_cpufreq_state;
  162. /* Ensure DVFS is disabled otherwise the following registers are RO */
  163. armada37xx_cpufreq_disable_dvfs(state->regmap);
  164. regmap_write(state->regmap, ARMADA_37XX_NB_L0L1, state->nb_l0l1);
  165. regmap_write(state->regmap, ARMADA_37XX_NB_L2L3, state->nb_l2l3);
  166. regmap_write(state->regmap, ARMADA_37XX_NB_CPU_LOAD,
  167. state->nb_cpu_load);
  168. /*
  169. * NB_DYN_MOD register is the one that actually enable back DVFS if it
  170. * was enabled before the suspend operation. This must be done last
  171. * otherwise other registers are not writable.
  172. */
  173. regmap_write(state->regmap, ARMADA_37XX_NB_DYN_MOD, state->nb_dyn_mod);
  174. return 0;
  175. }
  176. static int __init armada37xx_cpufreq_driver_init(void)
  177. {
  178. struct cpufreq_dt_platform_data pdata;
  179. struct armada_37xx_dvfs *dvfs;
  180. struct platform_device *pdev;
  181. unsigned long freq;
  182. unsigned int cur_frequency;
  183. struct regmap *nb_pm_base;
  184. struct device *cpu_dev;
  185. int load_lvl, ret;
  186. struct clk *clk;
  187. nb_pm_base =
  188. syscon_regmap_lookup_by_compatible("marvell,armada-3700-nb-pm");
  189. if (IS_ERR(nb_pm_base))
  190. return -ENODEV;
  191. /* Before doing any configuration on the DVFS first, disable it */
  192. armada37xx_cpufreq_disable_dvfs(nb_pm_base);
  193. /*
  194. * On CPU 0 register the operating points supported (which are
  195. * the nominal CPU frequency and full integer divisions of
  196. * it).
  197. */
  198. cpu_dev = get_cpu_device(0);
  199. if (!cpu_dev) {
  200. dev_err(cpu_dev, "Cannot get CPU\n");
  201. return -ENODEV;
  202. }
  203. clk = clk_get(cpu_dev, 0);
  204. if (IS_ERR(clk)) {
  205. dev_err(cpu_dev, "Cannot get clock for CPU0\n");
  206. return PTR_ERR(clk);
  207. }
  208. /* Get nominal (current) CPU frequency */
  209. cur_frequency = clk_get_rate(clk);
  210. if (!cur_frequency) {
  211. dev_err(cpu_dev, "Failed to get clock rate for CPU\n");
  212. clk_put(clk);
  213. return -EINVAL;
  214. }
  215. dvfs = armada_37xx_cpu_freq_info_get(cur_frequency);
  216. if (!dvfs) {
  217. clk_put(clk);
  218. return -EINVAL;
  219. }
  220. armada37xx_cpufreq_state = kmalloc(sizeof(*armada37xx_cpufreq_state),
  221. GFP_KERNEL);
  222. if (!armada37xx_cpufreq_state) {
  223. clk_put(clk);
  224. return -ENOMEM;
  225. }
  226. armada37xx_cpufreq_state->regmap = nb_pm_base;
  227. armada37xx_cpufreq_dvfs_setup(nb_pm_base, clk, dvfs->divider);
  228. clk_put(clk);
  229. for (load_lvl = ARMADA_37XX_DVFS_LOAD_0; load_lvl < LOAD_LEVEL_NR;
  230. load_lvl++) {
  231. freq = cur_frequency / dvfs->divider[load_lvl];
  232. ret = dev_pm_opp_add(cpu_dev, freq, 0);
  233. if (ret)
  234. goto remove_opp;
  235. }
  236. /* Now that everything is setup, enable the DVFS at hardware level */
  237. armada37xx_cpufreq_enable_dvfs(nb_pm_base);
  238. pdata.suspend = armada37xx_cpufreq_suspend;
  239. pdata.resume = armada37xx_cpufreq_resume;
  240. pdev = platform_device_register_data(NULL, "cpufreq-dt", -1, &pdata,
  241. sizeof(pdata));
  242. ret = PTR_ERR_OR_ZERO(pdev);
  243. if (ret)
  244. goto disable_dvfs;
  245. return 0;
  246. disable_dvfs:
  247. armada37xx_cpufreq_disable_dvfs(nb_pm_base);
  248. remove_opp:
  249. /* clean-up the already added opp before leaving */
  250. while (load_lvl-- > ARMADA_37XX_DVFS_LOAD_0) {
  251. freq = cur_frequency / dvfs->divider[load_lvl];
  252. dev_pm_opp_remove(cpu_dev, freq);
  253. }
  254. kfree(armada37xx_cpufreq_state);
  255. return ret;
  256. }
  257. /* late_initcall, to guarantee the driver is loaded after A37xx clock driver */
  258. late_initcall(armada37xx_cpufreq_driver_init);
  259. MODULE_AUTHOR("Gregory CLEMENT <gregory.clement@free-electrons.com>");
  260. MODULE_DESCRIPTION("Armada 37xx cpufreq driver");
  261. MODULE_LICENSE("GPL");