clock.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /*
  2. * Copyright (C) 2006 - 2008 Lemote Inc. & Insititute 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-loongson/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. return (unsigned long)clk->rate;
  73. }
  74. EXPORT_SYMBOL(clk_get_rate);
  75. void clk_put(struct clk *clk)
  76. {
  77. }
  78. EXPORT_SYMBOL(clk_put);
  79. int clk_set_rate(struct clk *clk, unsigned long rate)
  80. {
  81. unsigned int rate_khz = rate / 1000;
  82. struct cpufreq_frequency_table *pos;
  83. int ret = 0;
  84. int regval;
  85. if (likely(clk->ops && clk->ops->set_rate)) {
  86. unsigned long flags;
  87. spin_lock_irqsave(&clock_lock, flags);
  88. ret = clk->ops->set_rate(clk, rate, 0);
  89. spin_unlock_irqrestore(&clock_lock, flags);
  90. }
  91. if (unlikely(clk->flags & CLK_RATE_PROPAGATES))
  92. propagate_rate(clk);
  93. cpufreq_for_each_valid_entry(pos, loongson2_clockmod_table)
  94. if (rate_khz == pos->frequency)
  95. break;
  96. if (rate_khz != pos->frequency)
  97. return -ENOTSUPP;
  98. clk->rate = rate;
  99. regval = LOONGSON_CHIPCFG(0);
  100. regval = (regval & ~0x7) | (pos->driver_data - 1);
  101. LOONGSON_CHIPCFG(0) = regval;
  102. return ret;
  103. }
  104. EXPORT_SYMBOL_GPL(clk_set_rate);
  105. long clk_round_rate(struct clk *clk, unsigned long rate)
  106. {
  107. if (likely(clk->ops && clk->ops->round_rate)) {
  108. unsigned long flags, rounded;
  109. spin_lock_irqsave(&clock_lock, flags);
  110. rounded = clk->ops->round_rate(clk, rate);
  111. spin_unlock_irqrestore(&clock_lock, flags);
  112. return rounded;
  113. }
  114. return rate;
  115. }
  116. EXPORT_SYMBOL_GPL(clk_round_rate);