armada-37xx-cpufreq.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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. /* Power management in North Bridge register set */
  25. #define ARMADA_37XX_NB_L0L1 0x18
  26. #define ARMADA_37XX_NB_L2L3 0x1C
  27. #define ARMADA_37XX_NB_TBG_DIV_OFF 13
  28. #define ARMADA_37XX_NB_TBG_DIV_MASK 0x7
  29. #define ARMADA_37XX_NB_CLK_SEL_OFF 11
  30. #define ARMADA_37XX_NB_CLK_SEL_MASK 0x1
  31. #define ARMADA_37XX_NB_CLK_SEL_TBG 0x1
  32. #define ARMADA_37XX_NB_TBG_SEL_OFF 9
  33. #define ARMADA_37XX_NB_TBG_SEL_MASK 0x3
  34. #define ARMADA_37XX_NB_VDD_SEL_OFF 6
  35. #define ARMADA_37XX_NB_VDD_SEL_MASK 0x3
  36. #define ARMADA_37XX_NB_CONFIG_SHIFT 16
  37. #define ARMADA_37XX_NB_DYN_MOD 0x24
  38. #define ARMADA_37XX_NB_CLK_SEL_EN BIT(26)
  39. #define ARMADA_37XX_NB_TBG_EN BIT(28)
  40. #define ARMADA_37XX_NB_DIV_EN BIT(29)
  41. #define ARMADA_37XX_NB_VDD_EN BIT(30)
  42. #define ARMADA_37XX_NB_DFS_EN BIT(31)
  43. #define ARMADA_37XX_NB_CPU_LOAD 0x30
  44. #define ARMADA_37XX_NB_CPU_LOAD_MASK 0x3
  45. #define ARMADA_37XX_DVFS_LOAD_0 0
  46. #define ARMADA_37XX_DVFS_LOAD_1 1
  47. #define ARMADA_37XX_DVFS_LOAD_2 2
  48. #define ARMADA_37XX_DVFS_LOAD_3 3
  49. /*
  50. * On Armada 37xx the Power management manages 4 level of CPU load,
  51. * each level can be associated with a CPU clock source, a CPU
  52. * divider, a VDD level, etc...
  53. */
  54. #define LOAD_LEVEL_NR 4
  55. struct armada_37xx_dvfs {
  56. u32 cpu_freq_max;
  57. u8 divider[LOAD_LEVEL_NR];
  58. };
  59. static struct armada_37xx_dvfs armada_37xx_dvfs[] = {
  60. {.cpu_freq_max = 1200*1000*1000, .divider = {1, 2, 4, 6} },
  61. {.cpu_freq_max = 1000*1000*1000, .divider = {1, 2, 4, 5} },
  62. {.cpu_freq_max = 800*1000*1000, .divider = {1, 2, 3, 4} },
  63. {.cpu_freq_max = 600*1000*1000, .divider = {2, 4, 5, 6} },
  64. };
  65. static struct armada_37xx_dvfs *armada_37xx_cpu_freq_info_get(u32 freq)
  66. {
  67. int i;
  68. for (i = 0; i < ARRAY_SIZE(armada_37xx_dvfs); i++) {
  69. if (freq == armada_37xx_dvfs[i].cpu_freq_max)
  70. return &armada_37xx_dvfs[i];
  71. }
  72. pr_err("Unsupported CPU frequency %d MHz\n", freq/1000000);
  73. return NULL;
  74. }
  75. /*
  76. * Setup the four level managed by the hardware. Once the four level
  77. * will be configured then the DVFS will be enabled.
  78. */
  79. static void __init armada37xx_cpufreq_dvfs_setup(struct regmap *base,
  80. struct clk *clk, u8 *divider)
  81. {
  82. int load_lvl;
  83. struct clk *parent;
  84. for (load_lvl = 0; load_lvl < LOAD_LEVEL_NR; load_lvl++) {
  85. unsigned int reg, mask, val, offset = 0;
  86. if (load_lvl <= ARMADA_37XX_DVFS_LOAD_1)
  87. reg = ARMADA_37XX_NB_L0L1;
  88. else
  89. reg = ARMADA_37XX_NB_L2L3;
  90. if (load_lvl == ARMADA_37XX_DVFS_LOAD_0 ||
  91. load_lvl == ARMADA_37XX_DVFS_LOAD_2)
  92. offset += ARMADA_37XX_NB_CONFIG_SHIFT;
  93. /* Set cpu clock source, for all the level we use TBG */
  94. val = ARMADA_37XX_NB_CLK_SEL_TBG << ARMADA_37XX_NB_CLK_SEL_OFF;
  95. mask = (ARMADA_37XX_NB_CLK_SEL_MASK
  96. << ARMADA_37XX_NB_CLK_SEL_OFF);
  97. /*
  98. * Set cpu divider based on the pre-computed array in
  99. * order to have balanced step.
  100. */
  101. val |= divider[load_lvl] << ARMADA_37XX_NB_TBG_DIV_OFF;
  102. mask |= (ARMADA_37XX_NB_TBG_DIV_MASK
  103. << ARMADA_37XX_NB_TBG_DIV_OFF);
  104. /* Set VDD divider which is actually the load level. */
  105. val |= load_lvl << ARMADA_37XX_NB_VDD_SEL_OFF;
  106. mask |= (ARMADA_37XX_NB_VDD_SEL_MASK
  107. << ARMADA_37XX_NB_VDD_SEL_OFF);
  108. val <<= offset;
  109. mask <<= offset;
  110. regmap_update_bits(base, reg, mask, val);
  111. }
  112. /*
  113. * Set cpu clock source, for all the level we keep the same
  114. * clock source that the one already configured. For this one
  115. * we need to use the clock framework
  116. */
  117. parent = clk_get_parent(clk);
  118. clk_set_parent(clk, parent);
  119. }
  120. static void __init armada37xx_cpufreq_disable_dvfs(struct regmap *base)
  121. {
  122. unsigned int reg = ARMADA_37XX_NB_DYN_MOD,
  123. mask = ARMADA_37XX_NB_DFS_EN;
  124. regmap_update_bits(base, reg, mask, 0);
  125. }
  126. static void __init armada37xx_cpufreq_enable_dvfs(struct regmap *base)
  127. {
  128. unsigned int val, reg = ARMADA_37XX_NB_CPU_LOAD,
  129. mask = ARMADA_37XX_NB_CPU_LOAD_MASK;
  130. /* Start with the highest load (0) */
  131. val = ARMADA_37XX_DVFS_LOAD_0;
  132. regmap_update_bits(base, reg, mask, val);
  133. /* Now enable DVFS for the CPUs */
  134. reg = ARMADA_37XX_NB_DYN_MOD;
  135. mask = ARMADA_37XX_NB_CLK_SEL_EN | ARMADA_37XX_NB_TBG_EN |
  136. ARMADA_37XX_NB_DIV_EN | ARMADA_37XX_NB_VDD_EN |
  137. ARMADA_37XX_NB_DFS_EN;
  138. regmap_update_bits(base, reg, mask, mask);
  139. }
  140. static int __init armada37xx_cpufreq_driver_init(void)
  141. {
  142. struct armada_37xx_dvfs *dvfs;
  143. struct platform_device *pdev;
  144. unsigned int cur_frequency;
  145. struct regmap *nb_pm_base;
  146. struct device *cpu_dev;
  147. int load_lvl, ret;
  148. struct clk *clk;
  149. nb_pm_base =
  150. syscon_regmap_lookup_by_compatible("marvell,armada-3700-nb-pm");
  151. if (IS_ERR(nb_pm_base))
  152. return -ENODEV;
  153. /* Before doing any configuration on the DVFS first, disable it */
  154. armada37xx_cpufreq_disable_dvfs(nb_pm_base);
  155. /*
  156. * On CPU 0 register the operating points supported (which are
  157. * the nominal CPU frequency and full integer divisions of
  158. * it).
  159. */
  160. cpu_dev = get_cpu_device(0);
  161. if (!cpu_dev) {
  162. dev_err(cpu_dev, "Cannot get CPU\n");
  163. return -ENODEV;
  164. }
  165. clk = clk_get(cpu_dev, 0);
  166. if (IS_ERR(clk)) {
  167. dev_err(cpu_dev, "Cannot get clock for CPU0\n");
  168. return PTR_ERR(clk);
  169. }
  170. /* Get nominal (current) CPU frequency */
  171. cur_frequency = clk_get_rate(clk);
  172. if (!cur_frequency) {
  173. dev_err(cpu_dev, "Failed to get clock rate for CPU\n");
  174. clk_put(clk);
  175. return -EINVAL;
  176. }
  177. dvfs = armada_37xx_cpu_freq_info_get(cur_frequency);
  178. if (!dvfs)
  179. return -EINVAL;
  180. armada37xx_cpufreq_dvfs_setup(nb_pm_base, clk, dvfs->divider);
  181. clk_put(clk);
  182. for (load_lvl = ARMADA_37XX_DVFS_LOAD_0; load_lvl < LOAD_LEVEL_NR;
  183. load_lvl++) {
  184. unsigned long freq = cur_frequency / dvfs->divider[load_lvl];
  185. ret = dev_pm_opp_add(cpu_dev, freq, 0);
  186. if (ret) {
  187. /* clean-up the already added opp before leaving */
  188. while (load_lvl-- > ARMADA_37XX_DVFS_LOAD_0) {
  189. freq = cur_frequency / dvfs->divider[load_lvl];
  190. dev_pm_opp_remove(cpu_dev, freq);
  191. }
  192. return ret;
  193. }
  194. }
  195. /* Now that everything is setup, enable the DVFS at hardware level */
  196. armada37xx_cpufreq_enable_dvfs(nb_pm_base);
  197. pdev = platform_device_register_simple("cpufreq-dt", -1, NULL, 0);
  198. return PTR_ERR_OR_ZERO(pdev);
  199. }
  200. /* late_initcall, to guarantee the driver is loaded after A37xx clock driver */
  201. late_initcall(armada37xx_cpufreq_driver_init);
  202. MODULE_AUTHOR("Gregory CLEMENT <gregory.clement@free-electrons.com>");
  203. MODULE_DESCRIPTION("Armada 37xx cpufreq driver");
  204. MODULE_LICENSE("GPL");