kirkwood-cpufreq.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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");
  106. return PTR_ERR(priv.cpu_clk);
  107. }
  108. clk_prepare_enable(priv.cpu_clk);
  109. kirkwood_freq_table[0].frequency = clk_get_rate(priv.cpu_clk) / 1000;
  110. priv.ddr_clk = of_clk_get_by_name(np, "ddrclk");
  111. if (IS_ERR(priv.ddr_clk)) {
  112. dev_err(priv.dev, "Unable to get ddrclk");
  113. err = PTR_ERR(priv.ddr_clk);
  114. goto out_cpu;
  115. }
  116. clk_prepare_enable(priv.ddr_clk);
  117. kirkwood_freq_table[1].frequency = clk_get_rate(priv.ddr_clk) / 1000;
  118. priv.powersave_clk = of_clk_get_by_name(np, "powersave");
  119. if (IS_ERR(priv.powersave_clk)) {
  120. dev_err(priv.dev, "Unable to get powersave");
  121. err = PTR_ERR(priv.powersave_clk);
  122. goto out_ddr;
  123. }
  124. clk_prepare_enable(priv.powersave_clk);
  125. of_node_put(np);
  126. np = NULL;
  127. err = cpufreq_register_driver(&kirkwood_cpufreq_driver);
  128. if (!err)
  129. return 0;
  130. dev_err(priv.dev, "Failed to register cpufreq driver");
  131. clk_disable_unprepare(priv.powersave_clk);
  132. out_ddr:
  133. clk_disable_unprepare(priv.ddr_clk);
  134. out_cpu:
  135. clk_disable_unprepare(priv.cpu_clk);
  136. of_node_put(np);
  137. return err;
  138. }
  139. static int kirkwood_cpufreq_remove(struct platform_device *pdev)
  140. {
  141. cpufreq_unregister_driver(&kirkwood_cpufreq_driver);
  142. clk_disable_unprepare(priv.powersave_clk);
  143. clk_disable_unprepare(priv.ddr_clk);
  144. clk_disable_unprepare(priv.cpu_clk);
  145. return 0;
  146. }
  147. static struct platform_driver kirkwood_cpufreq_platform_driver = {
  148. .probe = kirkwood_cpufreq_probe,
  149. .remove = kirkwood_cpufreq_remove,
  150. .driver = {
  151. .name = "kirkwood-cpufreq",
  152. },
  153. };
  154. module_platform_driver(kirkwood_cpufreq_platform_driver);
  155. MODULE_LICENSE("GPL v2");
  156. MODULE_AUTHOR("Andrew Lunn <andrew@lunn.ch");
  157. MODULE_DESCRIPTION("cpufreq driver for Marvell's kirkwood CPU");
  158. MODULE_ALIAS("platform:kirkwood-cpufreq");