clock-sh4-202.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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 <linux/io.h>
  16. #include <linux/clkdev.h>
  17. #include <asm/clock.h>
  18. #include <asm/freq.h>
  19. #define CPG2_FRQCR3 0xfe0a0018
  20. static int frqcr3_divisors[] = { 1, 2, 3, 4, 6, 8, 16 };
  21. static int frqcr3_values[] = { 0, 1, 2, 3, 4, 5, 6 };
  22. static unsigned long emi_clk_recalc(struct clk *clk)
  23. {
  24. int idx = __raw_readl(CPG2_FRQCR3) & 0x0007;
  25. return clk->parent->rate / frqcr3_divisors[idx];
  26. }
  27. static inline int frqcr3_lookup(struct clk *clk, unsigned long rate)
  28. {
  29. int divisor = clk->parent->rate / rate;
  30. int i;
  31. for (i = 0; i < ARRAY_SIZE(frqcr3_divisors); i++)
  32. if (frqcr3_divisors[i] == divisor)
  33. return frqcr3_values[i];
  34. /* Safe fallback */
  35. return 5;
  36. }
  37. static struct sh_clk_ops sh4202_emi_clk_ops = {
  38. .recalc = emi_clk_recalc,
  39. };
  40. static struct clk sh4202_emi_clk = {
  41. .flags = CLK_ENABLE_ON_INIT,
  42. .ops = &sh4202_emi_clk_ops,
  43. };
  44. static unsigned long femi_clk_recalc(struct clk *clk)
  45. {
  46. int idx = (__raw_readl(CPG2_FRQCR3) >> 3) & 0x0007;
  47. return clk->parent->rate / frqcr3_divisors[idx];
  48. }
  49. static struct sh_clk_ops sh4202_femi_clk_ops = {
  50. .recalc = femi_clk_recalc,
  51. };
  52. static struct clk sh4202_femi_clk = {
  53. .flags = CLK_ENABLE_ON_INIT,
  54. .ops = &sh4202_femi_clk_ops,
  55. };
  56. static void shoc_clk_init(struct clk *clk)
  57. {
  58. int i;
  59. /*
  60. * For some reason, the shoc_clk seems to be set to some really
  61. * insane value at boot (values outside of the allowable frequency
  62. * range for instance). We deal with this by scaling it back down
  63. * to something sensible just in case.
  64. *
  65. * Start scaling from the high end down until we find something
  66. * that passes rate verification..
  67. */
  68. for (i = 0; i < ARRAY_SIZE(frqcr3_divisors); i++) {
  69. int divisor = frqcr3_divisors[i];
  70. if (clk->ops->set_rate(clk, clk->parent->rate / divisor) == 0)
  71. break;
  72. }
  73. WARN_ON(i == ARRAY_SIZE(frqcr3_divisors)); /* Undefined clock */
  74. }
  75. static unsigned long shoc_clk_recalc(struct clk *clk)
  76. {
  77. int idx = (__raw_readl(CPG2_FRQCR3) >> 6) & 0x0007;
  78. return clk->parent->rate / frqcr3_divisors[idx];
  79. }
  80. static int shoc_clk_verify_rate(struct clk *clk, unsigned long rate)
  81. {
  82. struct clk *bclk = clk_get(NULL, "bus_clk");
  83. unsigned long bclk_rate = clk_get_rate(bclk);
  84. clk_put(bclk);
  85. if (rate > bclk_rate)
  86. return 1;
  87. if (rate > 66000000)
  88. return 1;
  89. return 0;
  90. }
  91. static int shoc_clk_set_rate(struct clk *clk, unsigned long rate)
  92. {
  93. unsigned long frqcr3;
  94. unsigned int tmp;
  95. /* Make sure we have something sensible to switch to */
  96. if (shoc_clk_verify_rate(clk, rate) != 0)
  97. return -EINVAL;
  98. tmp = frqcr3_lookup(clk, rate);
  99. frqcr3 = __raw_readl(CPG2_FRQCR3);
  100. frqcr3 &= ~(0x0007 << 6);
  101. frqcr3 |= tmp << 6;
  102. __raw_writel(frqcr3, CPG2_FRQCR3);
  103. clk->rate = clk->parent->rate / frqcr3_divisors[tmp];
  104. return 0;
  105. }
  106. static struct sh_clk_ops sh4202_shoc_clk_ops = {
  107. .init = shoc_clk_init,
  108. .recalc = shoc_clk_recalc,
  109. .set_rate = shoc_clk_set_rate,
  110. };
  111. static struct clk sh4202_shoc_clk = {
  112. .flags = CLK_ENABLE_ON_INIT,
  113. .ops = &sh4202_shoc_clk_ops,
  114. };
  115. static struct clk *sh4202_onchip_clocks[] = {
  116. &sh4202_emi_clk,
  117. &sh4202_femi_clk,
  118. &sh4202_shoc_clk,
  119. };
  120. static struct clk_lookup lookups[] = {
  121. /* main clocks */
  122. CLKDEV_CON_ID("emi_clk", &sh4202_emi_clk),
  123. CLKDEV_CON_ID("femi_clk", &sh4202_femi_clk),
  124. CLKDEV_CON_ID("shoc_clk", &sh4202_shoc_clk),
  125. };
  126. int __init arch_clk_init(void)
  127. {
  128. struct clk *clk;
  129. int i, ret = 0;
  130. cpg_clk_init();
  131. clk = clk_get(NULL, "master_clk");
  132. for (i = 0; i < ARRAY_SIZE(sh4202_onchip_clocks); i++) {
  133. struct clk *clkp = sh4202_onchip_clocks[i];
  134. clkp->parent = clk;
  135. ret |= clk_register(clkp);
  136. }
  137. clk_put(clk);
  138. clkdev_add_table(lookups, ARRAY_SIZE(lookups));
  139. return ret;
  140. }