clk.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. *
  4. * Copyright (C) 2010 Thomas Langer <[email protected]>
  5. * Copyright (C) 2010 John Crispin <[email protected]>
  6. */
  7. #include <linux/io.h>
  8. #include <linux/export.h>
  9. #include <linux/init.h>
  10. #include <linux/kernel.h>
  11. #include <linux/types.h>
  12. #include <linux/clk.h>
  13. #include <linux/clkdev.h>
  14. #include <linux/err.h>
  15. #include <linux/list.h>
  16. #include <asm/time.h>
  17. #include <asm/irq.h>
  18. #include <asm/div64.h>
  19. #include <lantiq_soc.h>
  20. #include "clk.h"
  21. #include "prom.h"
  22. /* lantiq socs have 3 static clocks */
  23. static struct clk cpu_clk_generic[4];
  24. void clkdev_add_static(unsigned long cpu, unsigned long fpi,
  25. unsigned long io, unsigned long ppe)
  26. {
  27. cpu_clk_generic[0].rate = cpu;
  28. cpu_clk_generic[1].rate = fpi;
  29. cpu_clk_generic[2].rate = io;
  30. cpu_clk_generic[3].rate = ppe;
  31. }
  32. struct clk *clk_get_cpu(void)
  33. {
  34. return &cpu_clk_generic[0];
  35. }
  36. struct clk *clk_get_fpi(void)
  37. {
  38. return &cpu_clk_generic[1];
  39. }
  40. EXPORT_SYMBOL_GPL(clk_get_fpi);
  41. struct clk *clk_get_io(void)
  42. {
  43. return &cpu_clk_generic[2];
  44. }
  45. EXPORT_SYMBOL_GPL(clk_get_io);
  46. struct clk *clk_get_ppe(void)
  47. {
  48. return &cpu_clk_generic[3];
  49. }
  50. EXPORT_SYMBOL_GPL(clk_get_ppe);
  51. static inline int clk_good(struct clk *clk)
  52. {
  53. return clk && !IS_ERR(clk);
  54. }
  55. unsigned long clk_get_rate(struct clk *clk)
  56. {
  57. if (unlikely(!clk_good(clk)))
  58. return 0;
  59. if (clk->rate != 0)
  60. return clk->rate;
  61. if (clk->get_rate != NULL)
  62. return clk->get_rate();
  63. return 0;
  64. }
  65. EXPORT_SYMBOL(clk_get_rate);
  66. int clk_set_rate(struct clk *clk, unsigned long rate)
  67. {
  68. if (unlikely(!clk_good(clk)))
  69. return 0;
  70. if (clk->rates && *clk->rates) {
  71. unsigned long *r = clk->rates;
  72. while (*r && (*r != rate))
  73. r++;
  74. if (!*r) {
  75. pr_err("clk %s.%s: trying to set invalid rate %ld\n",
  76. clk->cl.dev_id, clk->cl.con_id, rate);
  77. return -1;
  78. }
  79. }
  80. clk->rate = rate;
  81. return 0;
  82. }
  83. EXPORT_SYMBOL(clk_set_rate);
  84. long clk_round_rate(struct clk *clk, unsigned long rate)
  85. {
  86. if (unlikely(!clk_good(clk)))
  87. return 0;
  88. if (clk->rates && *clk->rates) {
  89. unsigned long *r = clk->rates;
  90. while (*r && (*r != rate))
  91. r++;
  92. if (!*r) {
  93. return clk->rate;
  94. }
  95. }
  96. return rate;
  97. }
  98. EXPORT_SYMBOL(clk_round_rate);
  99. int clk_enable(struct clk *clk)
  100. {
  101. if (unlikely(!clk_good(clk)))
  102. return -1;
  103. if (clk->enable)
  104. return clk->enable(clk);
  105. return -1;
  106. }
  107. EXPORT_SYMBOL(clk_enable);
  108. void clk_disable(struct clk *clk)
  109. {
  110. if (unlikely(!clk_good(clk)))
  111. return;
  112. if (clk->disable)
  113. clk->disable(clk);
  114. }
  115. EXPORT_SYMBOL(clk_disable);
  116. int clk_activate(struct clk *clk)
  117. {
  118. if (unlikely(!clk_good(clk)))
  119. return -1;
  120. if (clk->activate)
  121. return clk->activate(clk);
  122. return -1;
  123. }
  124. EXPORT_SYMBOL(clk_activate);
  125. void clk_deactivate(struct clk *clk)
  126. {
  127. if (unlikely(!clk_good(clk)))
  128. return;
  129. if (clk->deactivate)
  130. clk->deactivate(clk);
  131. }
  132. EXPORT_SYMBOL(clk_deactivate);
  133. struct clk *clk_get_parent(struct clk *clk)
  134. {
  135. return NULL;
  136. }
  137. EXPORT_SYMBOL(clk_get_parent);
  138. int clk_set_parent(struct clk *clk, struct clk *parent)
  139. {
  140. return 0;
  141. }
  142. EXPORT_SYMBOL(clk_set_parent);
  143. static inline u32 get_counter_resolution(void)
  144. {
  145. u32 res;
  146. __asm__ __volatile__(
  147. ".set push\n"
  148. ".set mips32r2\n"
  149. "rdhwr %0, $3\n"
  150. ".set pop\n"
  151. : "=&r" (res)
  152. : /* no input */
  153. : "memory");
  154. return res;
  155. }
  156. void __init plat_time_init(void)
  157. {
  158. struct clk *clk;
  159. ltq_soc_init();
  160. clk = clk_get_cpu();
  161. mips_hpt_frequency = clk_get_rate(clk) / get_counter_resolution();
  162. write_c0_compare(read_c0_count());
  163. pr_info("CPU Clock: %ldMHz\n", clk_get_rate(clk) / 1000000);
  164. clk_put(clk);
  165. }