loongson1-cpufreq.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /*
  2. * CPU Frequency Scaling for Loongson 1 SoC
  3. *
  4. * Copyright (C) 2014-2016 Zhang, Keguang <keguang.zhang@gmail.com>
  5. *
  6. * This file is licensed under the terms of the GNU General Public
  7. * License version 2. This program is licensed "as is" without any
  8. * warranty of any kind, whether express or implied.
  9. */
  10. #include <linux/clk.h>
  11. #include <linux/clk-provider.h>
  12. #include <linux/cpu.h>
  13. #include <linux/cpufreq.h>
  14. #include <linux/delay.h>
  15. #include <linux/module.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/slab.h>
  18. #include <cpufreq.h>
  19. #include <loongson1.h>
  20. struct ls1x_cpufreq {
  21. struct device *dev;
  22. struct clk *clk; /* CPU clk */
  23. struct clk *mux_clk; /* MUX of CPU clk */
  24. struct clk *pll_clk; /* PLL clk */
  25. struct clk *osc_clk; /* OSC clk */
  26. unsigned int max_freq;
  27. unsigned int min_freq;
  28. };
  29. static struct ls1x_cpufreq *cpufreq;
  30. static int ls1x_cpufreq_notifier(struct notifier_block *nb,
  31. unsigned long val, void *data)
  32. {
  33. if (val == CPUFREQ_POSTCHANGE)
  34. current_cpu_data.udelay_val = loops_per_jiffy;
  35. return NOTIFY_OK;
  36. }
  37. static struct notifier_block ls1x_cpufreq_notifier_block = {
  38. .notifier_call = ls1x_cpufreq_notifier
  39. };
  40. static int ls1x_cpufreq_target(struct cpufreq_policy *policy,
  41. unsigned int index)
  42. {
  43. struct device *cpu_dev = get_cpu_device(policy->cpu);
  44. unsigned int old_freq, new_freq;
  45. old_freq = policy->cur;
  46. new_freq = policy->freq_table[index].frequency;
  47. /*
  48. * The procedure of reconfiguring CPU clk is as below.
  49. *
  50. * - Reparent CPU clk to OSC clk
  51. * - Reset CPU clock (very important)
  52. * - Reconfigure CPU DIV
  53. * - Reparent CPU clk back to CPU DIV clk
  54. */
  55. clk_set_parent(policy->clk, cpufreq->osc_clk);
  56. __raw_writel(__raw_readl(LS1X_CLK_PLL_DIV) | RST_CPU_EN | RST_CPU,
  57. LS1X_CLK_PLL_DIV);
  58. __raw_writel(__raw_readl(LS1X_CLK_PLL_DIV) & ~(RST_CPU_EN | RST_CPU),
  59. LS1X_CLK_PLL_DIV);
  60. clk_set_rate(cpufreq->mux_clk, new_freq * 1000);
  61. clk_set_parent(policy->clk, cpufreq->mux_clk);
  62. dev_dbg(cpu_dev, "%u KHz --> %u KHz\n", old_freq, new_freq);
  63. return 0;
  64. }
  65. static int ls1x_cpufreq_init(struct cpufreq_policy *policy)
  66. {
  67. struct device *cpu_dev = get_cpu_device(policy->cpu);
  68. struct cpufreq_frequency_table *freq_tbl;
  69. unsigned int pll_freq, freq;
  70. int steps, i, ret;
  71. pll_freq = clk_get_rate(cpufreq->pll_clk) / 1000;
  72. steps = 1 << DIV_CPU_WIDTH;
  73. freq_tbl = kcalloc(steps, sizeof(*freq_tbl), GFP_KERNEL);
  74. if (!freq_tbl)
  75. return -ENOMEM;
  76. for (i = 0; i < (steps - 1); i++) {
  77. freq = pll_freq / (i + 1);
  78. if ((freq < cpufreq->min_freq) || (freq > cpufreq->max_freq))
  79. freq_tbl[i].frequency = CPUFREQ_ENTRY_INVALID;
  80. else
  81. freq_tbl[i].frequency = freq;
  82. dev_dbg(cpu_dev,
  83. "cpufreq table: index %d: frequency %d\n", i,
  84. freq_tbl[i].frequency);
  85. }
  86. freq_tbl[i].frequency = CPUFREQ_TABLE_END;
  87. policy->clk = cpufreq->clk;
  88. ret = cpufreq_generic_init(policy, freq_tbl, 0);
  89. if (ret)
  90. kfree(freq_tbl);
  91. return ret;
  92. }
  93. static int ls1x_cpufreq_exit(struct cpufreq_policy *policy)
  94. {
  95. kfree(policy->freq_table);
  96. return 0;
  97. }
  98. static struct cpufreq_driver ls1x_cpufreq_driver = {
  99. .name = "cpufreq-ls1x",
  100. .flags = CPUFREQ_STICKY | CPUFREQ_NEED_INITIAL_FREQ_CHECK,
  101. .verify = cpufreq_generic_frequency_table_verify,
  102. .target_index = ls1x_cpufreq_target,
  103. .get = cpufreq_generic_get,
  104. .init = ls1x_cpufreq_init,
  105. .exit = ls1x_cpufreq_exit,
  106. .attr = cpufreq_generic_attr,
  107. };
  108. static int ls1x_cpufreq_remove(struct platform_device *pdev)
  109. {
  110. cpufreq_unregister_notifier(&ls1x_cpufreq_notifier_block,
  111. CPUFREQ_TRANSITION_NOTIFIER);
  112. cpufreq_unregister_driver(&ls1x_cpufreq_driver);
  113. return 0;
  114. }
  115. static int ls1x_cpufreq_probe(struct platform_device *pdev)
  116. {
  117. struct plat_ls1x_cpufreq *pdata = dev_get_platdata(&pdev->dev);
  118. struct clk *clk;
  119. int ret;
  120. if (!pdata || !pdata->clk_name || !pdata->osc_clk_name) {
  121. dev_err(&pdev->dev, "platform data missing\n");
  122. return -EINVAL;
  123. }
  124. cpufreq =
  125. devm_kzalloc(&pdev->dev, sizeof(struct ls1x_cpufreq), GFP_KERNEL);
  126. if (!cpufreq)
  127. return -ENOMEM;
  128. cpufreq->dev = &pdev->dev;
  129. clk = devm_clk_get(&pdev->dev, pdata->clk_name);
  130. if (IS_ERR(clk)) {
  131. dev_err(&pdev->dev, "unable to get %s clock\n",
  132. pdata->clk_name);
  133. return PTR_ERR(clk);
  134. }
  135. cpufreq->clk = clk;
  136. clk = clk_get_parent(clk);
  137. if (IS_ERR(clk)) {
  138. dev_err(&pdev->dev, "unable to get parent of %s clock\n",
  139. __clk_get_name(cpufreq->clk));
  140. return PTR_ERR(clk);
  141. }
  142. cpufreq->mux_clk = clk;
  143. clk = clk_get_parent(clk);
  144. if (IS_ERR(clk)) {
  145. dev_err(&pdev->dev, "unable to get parent of %s clock\n",
  146. __clk_get_name(cpufreq->mux_clk));
  147. return PTR_ERR(clk);
  148. }
  149. cpufreq->pll_clk = clk;
  150. clk = devm_clk_get(&pdev->dev, pdata->osc_clk_name);
  151. if (IS_ERR(clk)) {
  152. dev_err(&pdev->dev, "unable to get %s clock\n",
  153. pdata->osc_clk_name);
  154. return PTR_ERR(clk);
  155. }
  156. cpufreq->osc_clk = clk;
  157. cpufreq->max_freq = pdata->max_freq;
  158. cpufreq->min_freq = pdata->min_freq;
  159. ret = cpufreq_register_driver(&ls1x_cpufreq_driver);
  160. if (ret) {
  161. dev_err(&pdev->dev,
  162. "failed to register CPUFreq driver: %d\n", ret);
  163. return ret;
  164. }
  165. ret = cpufreq_register_notifier(&ls1x_cpufreq_notifier_block,
  166. CPUFREQ_TRANSITION_NOTIFIER);
  167. if (ret) {
  168. dev_err(&pdev->dev,
  169. "failed to register CPUFreq notifier: %d\n",ret);
  170. cpufreq_unregister_driver(&ls1x_cpufreq_driver);
  171. }
  172. return ret;
  173. }
  174. static struct platform_driver ls1x_cpufreq_platdrv = {
  175. .probe = ls1x_cpufreq_probe,
  176. .remove = ls1x_cpufreq_remove,
  177. .driver = {
  178. .name = "ls1x-cpufreq",
  179. },
  180. };
  181. module_platform_driver(ls1x_cpufreq_platdrv);
  182. MODULE_AUTHOR("Kelvin Cheung <keguang.zhang@gmail.com>");
  183. MODULE_DESCRIPTION("Loongson1 CPUFreq driver");
  184. MODULE_LICENSE("GPL");