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 <linux/io.h>
  16. #include <asm/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 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 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 /
  71. divisor, 0) == 0)
  72. break;
  73. }
  74. WARN_ON(i == ARRAY_SIZE(frqcr3_divisors)); /* Undefined clock */
  75. }
  76. static unsigned long shoc_clk_recalc(struct clk *clk)
  77. {
  78. int idx = (__raw_readl(CPG2_FRQCR3) >> 6) & 0x0007;
  79. return 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, int algo_id)
  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 = __raw_readl(CPG2_FRQCR3);
  101. frqcr3 &= ~(0x0007 << 6);
  102. frqcr3 |= tmp << 6;
  103. __raw_writel(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. .flags = CLK_ENABLE_ON_INIT,
  114. .ops = &sh4202_shoc_clk_ops,
  115. };
  116. static struct clk *sh4202_onchip_clocks[] = {
  117. &sh4202_emi_clk,
  118. &sh4202_femi_clk,
  119. &sh4202_shoc_clk,
  120. };
  121. #define CLKDEV_CON_ID(_id, _clk) { .con_id = _id, .clk = _clk }
  122. static struct clk_lookup lookups[] = {
  123. /* main clocks */
  124. CLKDEV_CON_ID("emi_clk", &sh4202_emi_clk),
  125. CLKDEV_CON_ID("femi_clk", &sh4202_femi_clk),
  126. CLKDEV_CON_ID("shoc_clk", &sh4202_shoc_clk),
  127. };
  128. int __init arch_clk_init(void)
  129. {
  130. struct clk *clk;
  131. int i, ret = 0;
  132. cpg_clk_init();
  133. clk = clk_get(NULL, "master_clk");
  134. for (i = 0; i < ARRAY_SIZE(sh4202_onchip_clocks); i++) {
  135. struct clk *clkp = sh4202_onchip_clocks[i];
  136. clkp->parent = clk;
  137. ret |= clk_register(clkp);
  138. }
  139. clk_put(clk);
  140. clkdev_add_table(lookups, ARRAY_SIZE(lookups));
  141. return ret;
  142. }