clock-sh4-202.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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 /
  72. divisor, 0) == 0)
  73. break;
  74. }
  75. WARN_ON(i == ARRAY_SIZE(frqcr3_divisors)); /* Undefined clock */
  76. }
  77. static void shoc_clk_recalc(struct clk *clk)
  78. {
  79. int idx = (ctrl_inl(CPG2_FRQCR3) >> 6) & 0x0007;
  80. clk->rate = clk->parent->rate / frqcr3_divisors[idx];
  81. }
  82. static int shoc_clk_verify_rate(struct clk *clk, unsigned long rate)
  83. {
  84. struct clk *bclk = clk_get(NULL, "bus_clk");
  85. unsigned long bclk_rate = clk_get_rate(bclk);
  86. clk_put(bclk);
  87. if (rate > bclk_rate)
  88. return 1;
  89. if (rate > 66000000)
  90. return 1;
  91. return 0;
  92. }
  93. static int shoc_clk_set_rate(struct clk *clk, unsigned long rate, int algo_id)
  94. {
  95. unsigned long frqcr3;
  96. unsigned int tmp;
  97. /* Make sure we have something sensible to switch to */
  98. if (shoc_clk_verify_rate(clk, rate) != 0)
  99. return -EINVAL;
  100. tmp = frqcr3_lookup(clk, rate);
  101. frqcr3 = ctrl_inl(CPG2_FRQCR3);
  102. frqcr3 &= ~(0x0007 << 6);
  103. frqcr3 |= tmp << 6;
  104. ctrl_outl(frqcr3, CPG2_FRQCR3);
  105. clk->rate = clk->parent->rate / frqcr3_divisors[tmp];
  106. return 0;
  107. }
  108. static struct clk_ops sh4202_shoc_clk_ops = {
  109. .init = shoc_clk_init,
  110. .recalc = shoc_clk_recalc,
  111. .set_rate = shoc_clk_set_rate,
  112. };
  113. static struct clk sh4202_shoc_clk = {
  114. .name = "shoc_clk",
  115. .flags = CLK_ALWAYS_ENABLED,
  116. .ops = &sh4202_shoc_clk_ops,
  117. };
  118. static struct clk *sh4202_onchip_clocks[] = {
  119. &sh4202_emi_clk,
  120. &sh4202_femi_clk,
  121. &sh4202_shoc_clk,
  122. };
  123. static int __init sh4202_clk_init(void)
  124. {
  125. struct clk *clk = clk_get(NULL, "master_clk");
  126. int i;
  127. for (i = 0; i < ARRAY_SIZE(sh4202_onchip_clocks); i++) {
  128. struct clk *clkp = sh4202_onchip_clocks[i];
  129. clkp->parent = clk;
  130. clk_register(clkp);
  131. clk_enable(clkp);
  132. }
  133. /*
  134. * Now that we have the rest of the clocks registered, we need to
  135. * force the parent clock to propagate so that these clocks will
  136. * automatically figure out their rate. We cheat by handing the
  137. * parent clock its current rate and forcing child propagation.
  138. */
  139. clk_set_rate(clk, clk_get_rate(clk));
  140. clk_put(clk);
  141. return 0;
  142. }
  143. arch_initcall(sh4202_clk_init);