armada-37xx-cpufreq.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  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. /* AVS register set */
  51. #define ARMADA_37XX_AVS_CTL0 0x0
  52. #define ARMADA_37XX_AVS_ENABLE BIT(30)
  53. #define ARMADA_37XX_AVS_HIGH_VDD_LIMIT 16
  54. #define ARMADA_37XX_AVS_LOW_VDD_LIMIT 22
  55. #define ARMADA_37XX_AVS_VDD_MASK 0x3F
  56. #define ARMADA_37XX_AVS_CTL2 0x8
  57. #define ARMADA_37XX_AVS_LOW_VDD_EN BIT(6)
  58. #define ARMADA_37XX_AVS_VSET(x) (0x1C + 4 * (x))
  59. /*
  60. * On Armada 37xx the Power management manages 4 level of CPU load,
  61. * each level can be associated with a CPU clock source, a CPU
  62. * divider, a VDD level, etc...
  63. */
  64. #define LOAD_LEVEL_NR 4
  65. #define MIN_VOLT_MV 1000
  66. /* AVS value for the corresponding voltage (in mV) */
  67. static int avs_map[] = {
  68. 747, 758, 770, 782, 793, 805, 817, 828, 840, 852, 863, 875, 887, 898,
  69. 910, 922, 933, 945, 957, 968, 980, 992, 1003, 1015, 1027, 1038, 1050,
  70. 1062, 1073, 1085, 1097, 1108, 1120, 1132, 1143, 1155, 1167, 1178, 1190,
  71. 1202, 1213, 1225, 1237, 1248, 1260, 1272, 1283, 1295, 1307, 1318, 1330,
  72. 1342
  73. };
  74. struct armada37xx_cpufreq_state {
  75. struct regmap *regmap;
  76. u32 nb_l0l1;
  77. u32 nb_l2l3;
  78. u32 nb_dyn_mod;
  79. u32 nb_cpu_load;
  80. };
  81. static struct armada37xx_cpufreq_state *armada37xx_cpufreq_state;
  82. struct armada_37xx_dvfs {
  83. u32 cpu_freq_max;
  84. u8 divider[LOAD_LEVEL_NR];
  85. u32 avs[LOAD_LEVEL_NR];
  86. };
  87. static struct armada_37xx_dvfs armada_37xx_dvfs[] = {
  88. {.cpu_freq_max = 1200*1000*1000, .divider = {1, 2, 4, 6} },
  89. {.cpu_freq_max = 1000*1000*1000, .divider = {1, 2, 4, 5} },
  90. {.cpu_freq_max = 800*1000*1000, .divider = {1, 2, 3, 4} },
  91. {.cpu_freq_max = 600*1000*1000, .divider = {2, 4, 5, 6} },
  92. };
  93. static struct armada_37xx_dvfs *armada_37xx_cpu_freq_info_get(u32 freq)
  94. {
  95. int i;
  96. for (i = 0; i < ARRAY_SIZE(armada_37xx_dvfs); i++) {
  97. if (freq == armada_37xx_dvfs[i].cpu_freq_max)
  98. return &armada_37xx_dvfs[i];
  99. }
  100. pr_err("Unsupported CPU frequency %d MHz\n", freq/1000000);
  101. return NULL;
  102. }
  103. /*
  104. * Setup the four level managed by the hardware. Once the four level
  105. * will be configured then the DVFS will be enabled.
  106. */
  107. static void __init armada37xx_cpufreq_dvfs_setup(struct regmap *base,
  108. struct clk *clk, u8 *divider)
  109. {
  110. int load_lvl;
  111. struct clk *parent;
  112. for (load_lvl = 0; load_lvl < LOAD_LEVEL_NR; load_lvl++) {
  113. unsigned int reg, mask, val, offset = 0;
  114. if (load_lvl <= ARMADA_37XX_DVFS_LOAD_1)
  115. reg = ARMADA_37XX_NB_L0L1;
  116. else
  117. reg = ARMADA_37XX_NB_L2L3;
  118. if (load_lvl == ARMADA_37XX_DVFS_LOAD_0 ||
  119. load_lvl == ARMADA_37XX_DVFS_LOAD_2)
  120. offset += ARMADA_37XX_NB_CONFIG_SHIFT;
  121. /* Set cpu clock source, for all the level we use TBG */
  122. val = ARMADA_37XX_NB_CLK_SEL_TBG << ARMADA_37XX_NB_CLK_SEL_OFF;
  123. mask = (ARMADA_37XX_NB_CLK_SEL_MASK
  124. << ARMADA_37XX_NB_CLK_SEL_OFF);
  125. /*
  126. * Set cpu divider based on the pre-computed array in
  127. * order to have balanced step.
  128. */
  129. val |= divider[load_lvl] << ARMADA_37XX_NB_TBG_DIV_OFF;
  130. mask |= (ARMADA_37XX_NB_TBG_DIV_MASK
  131. << ARMADA_37XX_NB_TBG_DIV_OFF);
  132. /* Set VDD divider which is actually the load level. */
  133. val |= load_lvl << ARMADA_37XX_NB_VDD_SEL_OFF;
  134. mask |= (ARMADA_37XX_NB_VDD_SEL_MASK
  135. << ARMADA_37XX_NB_VDD_SEL_OFF);
  136. val <<= offset;
  137. mask <<= offset;
  138. regmap_update_bits(base, reg, mask, val);
  139. }
  140. /*
  141. * Set cpu clock source, for all the level we keep the same
  142. * clock source that the one already configured. For this one
  143. * we need to use the clock framework
  144. */
  145. parent = clk_get_parent(clk);
  146. clk_set_parent(clk, parent);
  147. }
  148. /*
  149. * Find out the armada 37x supported AVS value whose voltage value is
  150. * the round-up closest to the target voltage value.
  151. */
  152. static u32 armada_37xx_avs_val_match(int target_vm)
  153. {
  154. u32 avs;
  155. /* Find out the round-up closest supported voltage value */
  156. for (avs = 0; avs < ARRAY_SIZE(avs_map); avs++)
  157. if (avs_map[avs] >= target_vm)
  158. break;
  159. /*
  160. * If all supported voltages are smaller than target one,
  161. * choose the largest supported voltage
  162. */
  163. if (avs == ARRAY_SIZE(avs_map))
  164. avs = ARRAY_SIZE(avs_map) - 1;
  165. return avs;
  166. }
  167. /*
  168. * For Armada 37xx soc, L0(VSET0) VDD AVS value is set to SVC revision
  169. * value or a default value when SVC is not supported.
  170. * - L0 can be read out from the register of AVS_CTRL_0 and L0 voltage
  171. * can be got from the mapping table of avs_map.
  172. * - L1 voltage should be about 100mv smaller than L0 voltage
  173. * - L2 & L3 voltage should be about 150mv smaller than L0 voltage.
  174. * This function calculates L1 & L2 & L3 AVS values dynamically based
  175. * on L0 voltage and fill all AVS values to the AVS value table.
  176. */
  177. static void __init armada37xx_cpufreq_avs_configure(struct regmap *base,
  178. struct armada_37xx_dvfs *dvfs)
  179. {
  180. unsigned int target_vm;
  181. int load_level = 0;
  182. u32 l0_vdd_min;
  183. if (base == NULL)
  184. return;
  185. /* Get L0 VDD min value */
  186. regmap_read(base, ARMADA_37XX_AVS_CTL0, &l0_vdd_min);
  187. l0_vdd_min = (l0_vdd_min >> ARMADA_37XX_AVS_LOW_VDD_LIMIT) &
  188. ARMADA_37XX_AVS_VDD_MASK;
  189. if (l0_vdd_min >= ARRAY_SIZE(avs_map)) {
  190. pr_err("L0 VDD MIN %d is not correct.\n", l0_vdd_min);
  191. return;
  192. }
  193. dvfs->avs[0] = l0_vdd_min;
  194. if (avs_map[l0_vdd_min] <= MIN_VOLT_MV) {
  195. /*
  196. * If L0 voltage is smaller than 1000mv, then all VDD sets
  197. * use L0 voltage;
  198. */
  199. u32 avs_min = armada_37xx_avs_val_match(MIN_VOLT_MV);
  200. for (load_level = 1; load_level < LOAD_LEVEL_NR; load_level++)
  201. dvfs->avs[load_level] = avs_min;
  202. return;
  203. }
  204. /*
  205. * L1 voltage is equal to L0 voltage - 100mv and it must be
  206. * larger than 1000mv
  207. */
  208. target_vm = avs_map[l0_vdd_min] - 100;
  209. target_vm = target_vm > MIN_VOLT_MV ? target_vm : MIN_VOLT_MV;
  210. dvfs->avs[1] = armada_37xx_avs_val_match(target_vm);
  211. /*
  212. * L2 & L3 voltage is equal to L0 voltage - 150mv and it must
  213. * be larger than 1000mv
  214. */
  215. target_vm = avs_map[l0_vdd_min] - 150;
  216. target_vm = target_vm > MIN_VOLT_MV ? target_vm : MIN_VOLT_MV;
  217. dvfs->avs[2] = dvfs->avs[3] = armada_37xx_avs_val_match(target_vm);
  218. }
  219. static void __init armada37xx_cpufreq_avs_setup(struct regmap *base,
  220. struct armada_37xx_dvfs *dvfs)
  221. {
  222. unsigned int avs_val = 0, freq;
  223. int load_level = 0;
  224. if (base == NULL)
  225. return;
  226. /* Disable AVS before the configuration */
  227. regmap_update_bits(base, ARMADA_37XX_AVS_CTL0,
  228. ARMADA_37XX_AVS_ENABLE, 0);
  229. /* Enable low voltage mode */
  230. regmap_update_bits(base, ARMADA_37XX_AVS_CTL2,
  231. ARMADA_37XX_AVS_LOW_VDD_EN,
  232. ARMADA_37XX_AVS_LOW_VDD_EN);
  233. for (load_level = 1; load_level < LOAD_LEVEL_NR; load_level++) {
  234. freq = dvfs->cpu_freq_max / dvfs->divider[load_level];
  235. avs_val = dvfs->avs[load_level];
  236. regmap_update_bits(base, ARMADA_37XX_AVS_VSET(load_level-1),
  237. ARMADA_37XX_AVS_VDD_MASK << ARMADA_37XX_AVS_HIGH_VDD_LIMIT |
  238. ARMADA_37XX_AVS_VDD_MASK << ARMADA_37XX_AVS_LOW_VDD_LIMIT,
  239. avs_val << ARMADA_37XX_AVS_HIGH_VDD_LIMIT |
  240. avs_val << ARMADA_37XX_AVS_LOW_VDD_LIMIT);
  241. }
  242. /* Enable AVS after the configuration */
  243. regmap_update_bits(base, ARMADA_37XX_AVS_CTL0,
  244. ARMADA_37XX_AVS_ENABLE,
  245. ARMADA_37XX_AVS_ENABLE);
  246. }
  247. static void armada37xx_cpufreq_disable_dvfs(struct regmap *base)
  248. {
  249. unsigned int reg = ARMADA_37XX_NB_DYN_MOD,
  250. mask = ARMADA_37XX_NB_DFS_EN;
  251. regmap_update_bits(base, reg, mask, 0);
  252. }
  253. static void __init armada37xx_cpufreq_enable_dvfs(struct regmap *base)
  254. {
  255. unsigned int val, reg = ARMADA_37XX_NB_CPU_LOAD,
  256. mask = ARMADA_37XX_NB_CPU_LOAD_MASK;
  257. /* Start with the highest load (0) */
  258. val = ARMADA_37XX_DVFS_LOAD_0;
  259. regmap_update_bits(base, reg, mask, val);
  260. /* Now enable DVFS for the CPUs */
  261. reg = ARMADA_37XX_NB_DYN_MOD;
  262. mask = ARMADA_37XX_NB_CLK_SEL_EN | ARMADA_37XX_NB_TBG_EN |
  263. ARMADA_37XX_NB_DIV_EN | ARMADA_37XX_NB_VDD_EN |
  264. ARMADA_37XX_NB_DFS_EN;
  265. regmap_update_bits(base, reg, mask, mask);
  266. }
  267. static int armada37xx_cpufreq_suspend(struct cpufreq_policy *policy)
  268. {
  269. struct armada37xx_cpufreq_state *state = armada37xx_cpufreq_state;
  270. regmap_read(state->regmap, ARMADA_37XX_NB_L0L1, &state->nb_l0l1);
  271. regmap_read(state->regmap, ARMADA_37XX_NB_L2L3, &state->nb_l2l3);
  272. regmap_read(state->regmap, ARMADA_37XX_NB_CPU_LOAD,
  273. &state->nb_cpu_load);
  274. regmap_read(state->regmap, ARMADA_37XX_NB_DYN_MOD, &state->nb_dyn_mod);
  275. return 0;
  276. }
  277. static int armada37xx_cpufreq_resume(struct cpufreq_policy *policy)
  278. {
  279. struct armada37xx_cpufreq_state *state = armada37xx_cpufreq_state;
  280. /* Ensure DVFS is disabled otherwise the following registers are RO */
  281. armada37xx_cpufreq_disable_dvfs(state->regmap);
  282. regmap_write(state->regmap, ARMADA_37XX_NB_L0L1, state->nb_l0l1);
  283. regmap_write(state->regmap, ARMADA_37XX_NB_L2L3, state->nb_l2l3);
  284. regmap_write(state->regmap, ARMADA_37XX_NB_CPU_LOAD,
  285. state->nb_cpu_load);
  286. /*
  287. * NB_DYN_MOD register is the one that actually enable back DVFS if it
  288. * was enabled before the suspend operation. This must be done last
  289. * otherwise other registers are not writable.
  290. */
  291. regmap_write(state->regmap, ARMADA_37XX_NB_DYN_MOD, state->nb_dyn_mod);
  292. return 0;
  293. }
  294. static int __init armada37xx_cpufreq_driver_init(void)
  295. {
  296. struct cpufreq_dt_platform_data pdata;
  297. struct armada_37xx_dvfs *dvfs;
  298. struct platform_device *pdev;
  299. unsigned long freq;
  300. unsigned int cur_frequency, base_frequency;
  301. struct regmap *nb_pm_base, *avs_base;
  302. struct device *cpu_dev;
  303. int load_lvl, ret;
  304. struct clk *clk, *parent;
  305. nb_pm_base =
  306. syscon_regmap_lookup_by_compatible("marvell,armada-3700-nb-pm");
  307. if (IS_ERR(nb_pm_base))
  308. return -ENODEV;
  309. avs_base =
  310. syscon_regmap_lookup_by_compatible("marvell,armada-3700-avs");
  311. /* if AVS is not present don't use it but still try to setup dvfs */
  312. if (IS_ERR(avs_base)) {
  313. pr_info("Syscon failed for Adapting Voltage Scaling: skip it\n");
  314. avs_base = NULL;
  315. }
  316. /* Before doing any configuration on the DVFS first, disable it */
  317. armada37xx_cpufreq_disable_dvfs(nb_pm_base);
  318. /*
  319. * On CPU 0 register the operating points supported (which are
  320. * the nominal CPU frequency and full integer divisions of
  321. * it).
  322. */
  323. cpu_dev = get_cpu_device(0);
  324. if (!cpu_dev) {
  325. dev_err(cpu_dev, "Cannot get CPU\n");
  326. return -ENODEV;
  327. }
  328. clk = clk_get(cpu_dev, 0);
  329. if (IS_ERR(clk)) {
  330. dev_err(cpu_dev, "Cannot get clock for CPU0\n");
  331. return PTR_ERR(clk);
  332. }
  333. parent = clk_get_parent(clk);
  334. if (IS_ERR(parent)) {
  335. dev_err(cpu_dev, "Cannot get parent clock for CPU0\n");
  336. clk_put(clk);
  337. return PTR_ERR(parent);
  338. }
  339. /* Get parent CPU frequency */
  340. base_frequency = clk_get_rate(parent);
  341. if (!base_frequency) {
  342. dev_err(cpu_dev, "Failed to get parent clock rate for CPU\n");
  343. clk_put(clk);
  344. return -EINVAL;
  345. }
  346. /* Get nominal (current) CPU frequency */
  347. cur_frequency = clk_get_rate(clk);
  348. if (!cur_frequency) {
  349. dev_err(cpu_dev, "Failed to get clock rate for CPU\n");
  350. clk_put(clk);
  351. return -EINVAL;
  352. }
  353. dvfs = armada_37xx_cpu_freq_info_get(cur_frequency);
  354. if (!dvfs) {
  355. clk_put(clk);
  356. return -EINVAL;
  357. }
  358. armada37xx_cpufreq_state = kmalloc(sizeof(*armada37xx_cpufreq_state),
  359. GFP_KERNEL);
  360. if (!armada37xx_cpufreq_state) {
  361. clk_put(clk);
  362. return -ENOMEM;
  363. }
  364. armada37xx_cpufreq_state->regmap = nb_pm_base;
  365. armada37xx_cpufreq_avs_configure(avs_base, dvfs);
  366. armada37xx_cpufreq_avs_setup(avs_base, dvfs);
  367. armada37xx_cpufreq_dvfs_setup(nb_pm_base, clk, dvfs->divider);
  368. clk_put(clk);
  369. for (load_lvl = ARMADA_37XX_DVFS_LOAD_0; load_lvl < LOAD_LEVEL_NR;
  370. load_lvl++) {
  371. unsigned long u_volt = avs_map[dvfs->avs[load_lvl]] * 1000;
  372. freq = base_frequency / dvfs->divider[load_lvl];
  373. ret = dev_pm_opp_add(cpu_dev, freq, u_volt);
  374. if (ret)
  375. goto remove_opp;
  376. }
  377. /* Now that everything is setup, enable the DVFS at hardware level */
  378. armada37xx_cpufreq_enable_dvfs(nb_pm_base);
  379. pdata.suspend = armada37xx_cpufreq_suspend;
  380. pdata.resume = armada37xx_cpufreq_resume;
  381. pdev = platform_device_register_data(NULL, "cpufreq-dt", -1, &pdata,
  382. sizeof(pdata));
  383. ret = PTR_ERR_OR_ZERO(pdev);
  384. if (ret)
  385. goto disable_dvfs;
  386. return 0;
  387. disable_dvfs:
  388. armada37xx_cpufreq_disable_dvfs(nb_pm_base);
  389. remove_opp:
  390. /* clean-up the already added opp before leaving */
  391. while (load_lvl-- > ARMADA_37XX_DVFS_LOAD_0) {
  392. freq = cur_frequency / dvfs->divider[load_lvl];
  393. dev_pm_opp_remove(cpu_dev, freq);
  394. }
  395. kfree(armada37xx_cpufreq_state);
  396. return ret;
  397. }
  398. /* late_initcall, to guarantee the driver is loaded after A37xx clock driver */
  399. late_initcall(armada37xx_cpufreq_driver_init);
  400. MODULE_AUTHOR("Gregory CLEMENT <gregory.clement@free-electrons.com>");
  401. MODULE_DESCRIPTION("Armada 37xx cpufreq driver");
  402. MODULE_LICENSE("GPL");