kirkwood-cpufreq.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /*
  2. * kirkwood_freq.c: cpufreq driver for the Marvell kirkwood
  3. *
  4. * Copyright (C) 2013 Andrew Lunn <andrew@lunn.ch>
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version
  9. * 2 of the License, or (at your option) any later version.
  10. */
  11. #include <linux/kernel.h>
  12. #include <linux/module.h>
  13. #include <linux/clk.h>
  14. #include <linux/cpufreq.h>
  15. #include <linux/of_device.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/io.h>
  18. #include <asm/proc-fns.h>
  19. #define CPU_SW_INT_BLK BIT(28)
  20. static struct priv
  21. {
  22. struct clk *cpu_clk;
  23. struct clk *ddr_clk;
  24. struct clk *powersave_clk;
  25. struct device *dev;
  26. void __iomem *base;
  27. } priv;
  28. #define STATE_CPU_FREQ 0x01
  29. #define STATE_DDR_FREQ 0x02
  30. /*
  31. * Kirkwood can swap the clock to the CPU between two clocks:
  32. *
  33. * - cpu clk
  34. * - ddr clk
  35. *
  36. * The frequencies are set at runtime before registering this table.
  37. */
  38. static struct cpufreq_frequency_table kirkwood_freq_table[] = {
  39. {0, STATE_CPU_FREQ, 0}, /* CPU uses cpuclk */
  40. {0, STATE_DDR_FREQ, 0}, /* CPU uses ddrclk */
  41. {0, 0, CPUFREQ_TABLE_END},
  42. };
  43. static unsigned int kirkwood_cpufreq_get_cpu_frequency(unsigned int cpu)
  44. {
  45. return clk_get_rate(priv.powersave_clk) / 1000;
  46. }
  47. static int kirkwood_cpufreq_target(struct cpufreq_policy *policy,
  48. unsigned int index)
  49. {
  50. unsigned int state = kirkwood_freq_table[index].driver_data;
  51. unsigned long reg;
  52. local_irq_disable();
  53. /* Disable interrupts to the CPU */
  54. reg = readl_relaxed(priv.base);
  55. reg |= CPU_SW_INT_BLK;
  56. writel_relaxed(reg, priv.base);
  57. switch (state) {
  58. case STATE_CPU_FREQ:
  59. clk_set_parent(priv.powersave_clk, priv.cpu_clk);
  60. break;
  61. case STATE_DDR_FREQ:
  62. clk_set_parent(priv.powersave_clk, priv.ddr_clk);
  63. break;
  64. }
  65. /* Wait-for-Interrupt, while the hardware changes frequency */
  66. cpu_do_idle();
  67. /* Enable interrupts to the CPU */
  68. reg = readl_relaxed(priv.base);
  69. reg &= ~CPU_SW_INT_BLK;
  70. writel_relaxed(reg, priv.base);
  71. local_irq_enable();
  72. return 0;
  73. }
  74. /* Module init and exit code */
  75. static int kirkwood_cpufreq_cpu_init(struct cpufreq_policy *policy)
  76. {
  77. return cpufreq_generic_init(policy, kirkwood_freq_table, 5000);
  78. }
  79. static struct cpufreq_driver kirkwood_cpufreq_driver = {
  80. .flags = CPUFREQ_NEED_INITIAL_FREQ_CHECK,
  81. .get = kirkwood_cpufreq_get_cpu_frequency,
  82. .verify = cpufreq_generic_frequency_table_verify,
  83. .target_index = kirkwood_cpufreq_target,
  84. .init = kirkwood_cpufreq_cpu_init,
  85. .name = "kirkwood-cpufreq",
  86. .attr = cpufreq_generic_attr,
  87. };
  88. static int kirkwood_cpufreq_probe(struct platform_device *pdev)
  89. {
  90. struct device_node *np;
  91. struct resource *res;
  92. int err;
  93. priv.dev = &pdev->dev;
  94. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  95. priv.base = devm_ioremap_resource(&pdev->dev, res);
  96. if (IS_ERR(priv.base))
  97. return PTR_ERR(priv.base);
  98. np = of_cpu_device_node_get(0);
  99. if (!np) {
  100. dev_err(&pdev->dev, "failed to get cpu device node\n");
  101. return -ENODEV;
  102. }
  103. priv.cpu_clk = of_clk_get_by_name(np, "cpu_clk");
  104. if (IS_ERR(priv.cpu_clk)) {
  105. dev_err(priv.dev, "Unable to get cpuclk\n");
  106. err = PTR_ERR(priv.cpu_clk);
  107. goto out_node;
  108. }
  109. err = clk_prepare_enable(priv.cpu_clk);
  110. if (err) {
  111. dev_err(priv.dev, "Unable to prepare cpuclk\n");
  112. goto out_node;
  113. }
  114. kirkwood_freq_table[0].frequency = clk_get_rate(priv.cpu_clk) / 1000;
  115. priv.ddr_clk = of_clk_get_by_name(np, "ddrclk");
  116. if (IS_ERR(priv.ddr_clk)) {
  117. dev_err(priv.dev, "Unable to get ddrclk\n");
  118. err = PTR_ERR(priv.ddr_clk);
  119. goto out_cpu;
  120. }
  121. err = clk_prepare_enable(priv.ddr_clk);
  122. if (err) {
  123. dev_err(priv.dev, "Unable to prepare ddrclk\n");
  124. goto out_cpu;
  125. }
  126. kirkwood_freq_table[1].frequency = clk_get_rate(priv.ddr_clk) / 1000;
  127. priv.powersave_clk = of_clk_get_by_name(np, "powersave");
  128. if (IS_ERR(priv.powersave_clk)) {
  129. dev_err(priv.dev, "Unable to get powersave\n");
  130. err = PTR_ERR(priv.powersave_clk);
  131. goto out_ddr;
  132. }
  133. err = clk_prepare_enable(priv.powersave_clk);
  134. if (err) {
  135. dev_err(priv.dev, "Unable to prepare powersave clk\n");
  136. goto out_ddr;
  137. }
  138. err = cpufreq_register_driver(&kirkwood_cpufreq_driver);
  139. if (err) {
  140. dev_err(priv.dev, "Failed to register cpufreq driver\n");
  141. goto out_powersave;
  142. }
  143. of_node_put(np);
  144. return 0;
  145. out_powersave:
  146. clk_disable_unprepare(priv.powersave_clk);
  147. out_ddr:
  148. clk_disable_unprepare(priv.ddr_clk);
  149. out_cpu:
  150. clk_disable_unprepare(priv.cpu_clk);
  151. out_node:
  152. of_node_put(np);
  153. return err;
  154. }
  155. static int kirkwood_cpufreq_remove(struct platform_device *pdev)
  156. {
  157. cpufreq_unregister_driver(&kirkwood_cpufreq_driver);
  158. clk_disable_unprepare(priv.powersave_clk);
  159. clk_disable_unprepare(priv.ddr_clk);
  160. clk_disable_unprepare(priv.cpu_clk);
  161. return 0;
  162. }
  163. static struct platform_driver kirkwood_cpufreq_platform_driver = {
  164. .probe = kirkwood_cpufreq_probe,
  165. .remove = kirkwood_cpufreq_remove,
  166. .driver = {
  167. .name = "kirkwood-cpufreq",
  168. },
  169. };
  170. module_platform_driver(kirkwood_cpufreq_platform_driver);
  171. MODULE_LICENSE("GPL v2");
  172. MODULE_AUTHOR("Andrew Lunn <andrew@lunn.ch");
  173. MODULE_DESCRIPTION("cpufreq driver for Marvell's kirkwood CPU");
  174. MODULE_ALIAS("platform:kirkwood-cpufreq");