clock-sh4-202.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /*
  2. * arch/sh/kernel/cpu/sh4/clock-sh4-202.c
  3. *
  4. * Additional SH4-202 support for the clock framework
  5. *
  6. * Copyright (C) 2005 Paul Mundt
  7. *
  8. * This file is subject to the terms and conditions of the GNU General Public
  9. * License. See the file "COPYING" in the main directory of this archive
  10. * for more details.
  11. */
  12. #include <linux/init.h>
  13. #include <linux/kernel.h>
  14. #include <linux/err.h>
  15. #include <asm/clock.h>
  16. #include <asm/freq.h>
  17. #include <asm/io.h>
  18. #define CPG2_FRQCR3 0xfe0a0018
  19. static int frqcr3_divisors[] = { 1, 2, 3, 4, 6, 8, 16 };
  20. static int frqcr3_values[] = { 0, 1, 2, 3, 4, 5, 6 };
  21. static void emi_clk_recalc(struct clk *clk)
  22. {
  23. int idx = ctrl_inl(CPG2_FRQCR3) & 0x0007;
  24. clk->rate = clk->parent->rate / frqcr3_divisors[idx];
  25. }
  26. static inline int frqcr3_lookup(struct clk *clk, unsigned long rate)
  27. {
  28. int divisor = clk->parent->rate / rate;
  29. int i;
  30. for (i = 0; i < ARRAY_SIZE(frqcr3_divisors); i++)
  31. if (frqcr3_divisors[i] == divisor)
  32. return frqcr3_values[i];
  33. /* Safe fallback */
  34. return 5;
  35. }
  36. static struct clk_ops sh4202_emi_clk_ops = {
  37. .recalc = emi_clk_recalc,
  38. };
  39. static struct clk sh4202_emi_clk = {
  40. .name = "emi_clk",
  41. .flags = CLK_ALWAYS_ENABLED,
  42. .ops = &sh4202_emi_clk_ops,
  43. };
  44. static void femi_clk_recalc(struct clk *clk)
  45. {
  46. int idx = (ctrl_inl(CPG2_FRQCR3) >> 3) & 0x0007;
  47. clk->rate = clk->parent->rate / frqcr3_divisors[idx];
  48. }
  49. static struct clk_ops sh4202_femi_clk_ops = {
  50. .recalc = femi_clk_recalc,
  51. };
  52. static struct clk sh4202_femi_clk = {
  53. .name = "femi_clk",
  54. .flags = CLK_ALWAYS_ENABLED,
  55. .ops = &sh4202_femi_clk_ops,
  56. };
  57. static void shoc_clk_init(struct clk *clk)
  58. {
  59. int i;
  60. /*
  61. * For some reason, the shoc_clk seems to be set to some really
  62. * insane value at boot (values outside of the allowable frequency
  63. * range for instance). We deal with this by scaling it back down
  64. * to something sensible just in case.
  65. *
  66. * Start scaling from the high end down until we find something
  67. * that passes rate verification..
  68. */
  69. for (i = 0; i < ARRAY_SIZE(frqcr3_divisors); i++) {
  70. int divisor = frqcr3_divisors[i];
  71. if (clk->ops->set_rate(clk, clk->parent->rate / divisor) == 0)
  72. break;
  73. }
  74. WARN_ON(i == ARRAY_SIZE(frqcr3_divisors)); /* Undefined clock */
  75. }
  76. static void shoc_clk_recalc(struct clk *clk)
  77. {
  78. int idx = (ctrl_inl(CPG2_FRQCR3) >> 6) & 0x0007;
  79. clk->rate = clk->parent->rate / frqcr3_divisors[idx];
  80. }
  81. static int shoc_clk_verify_rate(struct clk *clk, unsigned long rate)
  82. {
  83. struct clk *bclk = clk_get(NULL, "bus_clk");
  84. unsigned long bclk_rate = clk_get_rate(bclk);
  85. clk_put(bclk);
  86. if (rate > bclk_rate)
  87. return 1;
  88. if (rate > 66000000)
  89. return 1;
  90. return 0;
  91. }
  92. static int shoc_clk_set_rate(struct clk *clk, unsigned long rate)
  93. {
  94. unsigned long frqcr3;
  95. unsigned int tmp;
  96. /* Make sure we have something sensible to switch to */
  97. if (shoc_clk_verify_rate(clk, rate) != 0)
  98. return -EINVAL;
  99. tmp = frqcr3_lookup(clk, rate);
  100. frqcr3 = ctrl_inl(CPG2_FRQCR3);
  101. frqcr3 &= ~(0x0007 << 6);
  102. frqcr3 |= tmp << 6;
  103. ctrl_outl(frqcr3, CPG2_FRQCR3);
  104. clk->rate = clk->parent->rate / frqcr3_divisors[tmp];
  105. return 0;
  106. }
  107. static struct clk_ops sh4202_shoc_clk_ops = {
  108. .init = shoc_clk_init,
  109. .recalc = shoc_clk_recalc,
  110. .set_rate = shoc_clk_set_rate,
  111. };
  112. static struct clk sh4202_shoc_clk = {
  113. .name = "shoc_clk",
  114. .flags = CLK_ALWAYS_ENABLED,
  115. .ops = &sh4202_shoc_clk_ops,
  116. };
  117. static struct clk *sh4202_onchip_clocks[] = {
  118. &sh4202_emi_clk,
  119. &sh4202_femi_clk,
  120. &sh4202_shoc_clk,
  121. };
  122. static int __init sh4202_clk_init(void)
  123. {
  124. struct clk *clk = clk_get(NULL, "master_clk");
  125. int i;
  126. for (i = 0; i < ARRAY_SIZE(sh4202_onchip_clocks); i++) {
  127. struct clk *clkp = sh4202_onchip_clocks[i];
  128. clkp->parent = clk;
  129. clk_register(clkp);
  130. clk_enable(clkp);
  131. }
  132. /*
  133. * Now that we have the rest of the clocks registered, we need to
  134. * force the parent clock to propagate so that these clocks will
  135. * automatically figure out their rate. We cheat by handing the
  136. * parent clock its current rate and forcing child propagation.
  137. */
  138. clk_set_rate(clk, clk_get_rate(clk));
  139. clk_put(clk);
  140. return 0;
  141. }
  142. arch_initcall(sh4202_clk_init);