clock-sh4-202.c 3.8 KB

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