clock.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /*
  2. * Copyright (C) 2006 - 2008 Lemote Inc. & Institute of Computing Technology
  3. * Author: Yanhua, yanh@lemote.com
  4. *
  5. * This file is subject to the terms and conditions of the GNU General Public
  6. * License. See the file "COPYING" in the main directory of this archive
  7. * for more details.
  8. */
  9. #include <linux/clk.h>
  10. #include <linux/cpufreq.h>
  11. #include <linux/errno.h>
  12. #include <linux/export.h>
  13. #include <linux/list.h>
  14. #include <linux/mutex.h>
  15. #include <linux/spinlock.h>
  16. #include <asm/clock.h>
  17. #include <asm/mach-loongson64/loongson.h>
  18. static LIST_HEAD(clock_list);
  19. static DEFINE_SPINLOCK(clock_lock);
  20. static DEFINE_MUTEX(clock_list_sem);
  21. /* Minimum CLK support */
  22. enum {
  23. DC_ZERO, DC_25PT = 2, DC_37PT, DC_50PT, DC_62PT, DC_75PT,
  24. DC_87PT, DC_DISABLE, DC_RESV
  25. };
  26. struct cpufreq_frequency_table loongson2_clockmod_table[] = {
  27. {0, DC_RESV, CPUFREQ_ENTRY_INVALID},
  28. {0, DC_ZERO, CPUFREQ_ENTRY_INVALID},
  29. {0, DC_25PT, 0},
  30. {0, DC_37PT, 0},
  31. {0, DC_50PT, 0},
  32. {0, DC_62PT, 0},
  33. {0, DC_75PT, 0},
  34. {0, DC_87PT, 0},
  35. {0, DC_DISABLE, 0},
  36. {0, DC_RESV, CPUFREQ_TABLE_END},
  37. };
  38. EXPORT_SYMBOL_GPL(loongson2_clockmod_table);
  39. static struct clk cpu_clk = {
  40. .name = "cpu_clk",
  41. .flags = CLK_ALWAYS_ENABLED | CLK_RATE_PROPAGATES,
  42. .rate = 800000000,
  43. };
  44. struct clk *clk_get(struct device *dev, const char *id)
  45. {
  46. return &cpu_clk;
  47. }
  48. EXPORT_SYMBOL(clk_get);
  49. static void propagate_rate(struct clk *clk)
  50. {
  51. struct clk *clkp;
  52. list_for_each_entry(clkp, &clock_list, node) {
  53. if (likely(clkp->parent != clk))
  54. continue;
  55. if (likely(clkp->ops && clkp->ops->recalc))
  56. clkp->ops->recalc(clkp);
  57. if (unlikely(clkp->flags & CLK_RATE_PROPAGATES))
  58. propagate_rate(clkp);
  59. }
  60. }
  61. int clk_enable(struct clk *clk)
  62. {
  63. return 0;
  64. }
  65. EXPORT_SYMBOL(clk_enable);
  66. void clk_disable(struct clk *clk)
  67. {
  68. }
  69. EXPORT_SYMBOL(clk_disable);
  70. unsigned long clk_get_rate(struct clk *clk)
  71. {
  72. if (!clk)
  73. return 0;
  74. return (unsigned long)clk->rate;
  75. }
  76. EXPORT_SYMBOL(clk_get_rate);
  77. void clk_put(struct clk *clk)
  78. {
  79. }
  80. EXPORT_SYMBOL(clk_put);
  81. int clk_set_rate(struct clk *clk, unsigned long rate)
  82. {
  83. unsigned int rate_khz = rate / 1000;
  84. struct cpufreq_frequency_table *pos;
  85. int ret = 0;
  86. int regval;
  87. if (likely(clk->ops && clk->ops->set_rate)) {
  88. unsigned long flags;
  89. spin_lock_irqsave(&clock_lock, flags);
  90. ret = clk->ops->set_rate(clk, rate, 0);
  91. spin_unlock_irqrestore(&clock_lock, flags);
  92. }
  93. if (unlikely(clk->flags & CLK_RATE_PROPAGATES))
  94. propagate_rate(clk);
  95. cpufreq_for_each_valid_entry(pos, loongson2_clockmod_table)
  96. if (rate_khz == pos->frequency)
  97. break;
  98. if (rate_khz != pos->frequency)
  99. return -ENOTSUPP;
  100. clk->rate = rate;
  101. regval = LOONGSON_CHIPCFG(0);
  102. regval = (regval & ~0x7) | (pos->driver_data - 1);
  103. LOONGSON_CHIPCFG(0) = regval;
  104. return ret;
  105. }
  106. EXPORT_SYMBOL_GPL(clk_set_rate);
  107. long clk_round_rate(struct clk *clk, unsigned long rate)
  108. {
  109. if (likely(clk->ops && clk->ops->round_rate)) {
  110. unsigned long flags, rounded;
  111. spin_lock_irqsave(&clock_lock, flags);
  112. rounded = clk->ops->round_rate(clk, rate);
  113. spin_unlock_irqrestore(&clock_lock, flags);
  114. return rounded;
  115. }
  116. return rate;
  117. }
  118. EXPORT_SYMBOL_GPL(clk_round_rate);