clock.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. /*
  2. * Atheros AR71XX/AR724X/AR913X common routines
  3. *
  4. * Copyright (C) 2011 Gabor Juhos <juhosg@openwrt.org>
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License version 2 as published
  8. * by the Free Software Foundation.
  9. */
  10. #include <linux/kernel.h>
  11. #include <linux/module.h>
  12. #include <linux/init.h>
  13. #include <linux/err.h>
  14. #include <linux/clk.h>
  15. #include <asm/mach-ath79/ath79.h>
  16. #include <asm/mach-ath79/ar71xx_regs.h>
  17. #include "common.h"
  18. #define AR71XX_BASE_FREQ 40000000
  19. #define AR724X_BASE_FREQ 5000000
  20. #define AR913X_BASE_FREQ 5000000
  21. struct clk {
  22. unsigned long rate;
  23. };
  24. static struct clk ath79_ref_clk;
  25. static struct clk ath79_cpu_clk;
  26. static struct clk ath79_ddr_clk;
  27. static struct clk ath79_ahb_clk;
  28. static struct clk ath79_wdt_clk;
  29. static struct clk ath79_uart_clk;
  30. static void __init ar71xx_clocks_init(void)
  31. {
  32. u32 pll;
  33. u32 freq;
  34. u32 div;
  35. ath79_ref_clk.rate = AR71XX_BASE_FREQ;
  36. pll = ath79_pll_rr(AR71XX_PLL_REG_CPU_CONFIG);
  37. div = ((pll >> AR71XX_PLL_DIV_SHIFT) & AR71XX_PLL_DIV_MASK) + 1;
  38. freq = div * ath79_ref_clk.rate;
  39. div = ((pll >> AR71XX_CPU_DIV_SHIFT) & AR71XX_CPU_DIV_MASK) + 1;
  40. ath79_cpu_clk.rate = freq / div;
  41. div = ((pll >> AR71XX_DDR_DIV_SHIFT) & AR71XX_DDR_DIV_MASK) + 1;
  42. ath79_ddr_clk.rate = freq / div;
  43. div = (((pll >> AR71XX_AHB_DIV_SHIFT) & AR71XX_AHB_DIV_MASK) + 1) * 2;
  44. ath79_ahb_clk.rate = ath79_cpu_clk.rate / div;
  45. ath79_wdt_clk.rate = ath79_ahb_clk.rate;
  46. ath79_uart_clk.rate = ath79_ahb_clk.rate;
  47. }
  48. static void __init ar724x_clocks_init(void)
  49. {
  50. u32 pll;
  51. u32 freq;
  52. u32 div;
  53. ath79_ref_clk.rate = AR724X_BASE_FREQ;
  54. pll = ath79_pll_rr(AR724X_PLL_REG_CPU_CONFIG);
  55. div = ((pll >> AR724X_PLL_DIV_SHIFT) & AR724X_PLL_DIV_MASK);
  56. freq = div * ath79_ref_clk.rate;
  57. div = ((pll >> AR724X_PLL_REF_DIV_SHIFT) & AR724X_PLL_REF_DIV_MASK);
  58. freq *= div;
  59. ath79_cpu_clk.rate = freq;
  60. div = ((pll >> AR724X_DDR_DIV_SHIFT) & AR724X_DDR_DIV_MASK) + 1;
  61. ath79_ddr_clk.rate = freq / div;
  62. div = (((pll >> AR724X_AHB_DIV_SHIFT) & AR724X_AHB_DIV_MASK) + 1) * 2;
  63. ath79_ahb_clk.rate = ath79_cpu_clk.rate / div;
  64. ath79_wdt_clk.rate = ath79_ahb_clk.rate;
  65. ath79_uart_clk.rate = ath79_ahb_clk.rate;
  66. }
  67. static void __init ar913x_clocks_init(void)
  68. {
  69. u32 pll;
  70. u32 freq;
  71. u32 div;
  72. ath79_ref_clk.rate = AR913X_BASE_FREQ;
  73. pll = ath79_pll_rr(AR913X_PLL_REG_CPU_CONFIG);
  74. div = ((pll >> AR913X_PLL_DIV_SHIFT) & AR913X_PLL_DIV_MASK);
  75. freq = div * ath79_ref_clk.rate;
  76. ath79_cpu_clk.rate = freq;
  77. div = ((pll >> AR913X_DDR_DIV_SHIFT) & AR913X_DDR_DIV_MASK) + 1;
  78. ath79_ddr_clk.rate = freq / div;
  79. div = (((pll >> AR913X_AHB_DIV_SHIFT) & AR913X_AHB_DIV_MASK) + 1) * 2;
  80. ath79_ahb_clk.rate = ath79_cpu_clk.rate / div;
  81. ath79_wdt_clk.rate = ath79_ahb_clk.rate;
  82. ath79_uart_clk.rate = ath79_ahb_clk.rate;
  83. }
  84. static void __init ar933x_clocks_init(void)
  85. {
  86. u32 clock_ctrl;
  87. u32 cpu_config;
  88. u32 freq;
  89. u32 t;
  90. t = ath79_reset_rr(AR933X_RESET_REG_BOOTSTRAP);
  91. if (t & AR933X_BOOTSTRAP_REF_CLK_40)
  92. ath79_ref_clk.rate = (40 * 1000 * 1000);
  93. else
  94. ath79_ref_clk.rate = (25 * 1000 * 1000);
  95. clock_ctrl = ath79_pll_rr(AR933X_PLL_CLOCK_CTRL_REG);
  96. if (clock_ctrl & AR933X_PLL_CLOCK_CTRL_BYPASS) {
  97. ath79_cpu_clk.rate = ath79_ref_clk.rate;
  98. ath79_ahb_clk.rate = ath79_ref_clk.rate;
  99. ath79_ddr_clk.rate = ath79_ref_clk.rate;
  100. } else {
  101. cpu_config = ath79_pll_rr(AR933X_PLL_CPU_CONFIG_REG);
  102. t = (cpu_config >> AR933X_PLL_CPU_CONFIG_REFDIV_SHIFT) &
  103. AR933X_PLL_CPU_CONFIG_REFDIV_MASK;
  104. freq = ath79_ref_clk.rate / t;
  105. t = (cpu_config >> AR933X_PLL_CPU_CONFIG_NINT_SHIFT) &
  106. AR933X_PLL_CPU_CONFIG_NINT_MASK;
  107. freq *= t;
  108. t = (cpu_config >> AR933X_PLL_CPU_CONFIG_OUTDIV_SHIFT) &
  109. AR933X_PLL_CPU_CONFIG_OUTDIV_MASK;
  110. if (t == 0)
  111. t = 1;
  112. freq >>= t;
  113. t = ((clock_ctrl >> AR933X_PLL_CLOCK_CTRL_CPU_DIV_SHIFT) &
  114. AR933X_PLL_CLOCK_CTRL_CPU_DIV_MASK) + 1;
  115. ath79_cpu_clk.rate = freq / t;
  116. t = ((clock_ctrl >> AR933X_PLL_CLOCK_CTRL_DDR_DIV_SHIFT) &
  117. AR933X_PLL_CLOCK_CTRL_DDR_DIV_MASK) + 1;
  118. ath79_ddr_clk.rate = freq / t;
  119. t = ((clock_ctrl >> AR933X_PLL_CLOCK_CTRL_AHB_DIV_SHIFT) &
  120. AR933X_PLL_CLOCK_CTRL_AHB_DIV_MASK) + 1;
  121. ath79_ahb_clk.rate = freq / t;
  122. }
  123. ath79_wdt_clk.rate = ath79_ahb_clk.rate;
  124. ath79_uart_clk.rate = ath79_ref_clk.rate;
  125. }
  126. void __init ath79_clocks_init(void)
  127. {
  128. if (soc_is_ar71xx())
  129. ar71xx_clocks_init();
  130. else if (soc_is_ar724x())
  131. ar724x_clocks_init();
  132. else if (soc_is_ar913x())
  133. ar913x_clocks_init();
  134. else if (soc_is_ar933x())
  135. ar933x_clocks_init();
  136. else
  137. BUG();
  138. pr_info("Clocks: CPU:%lu.%03luMHz, DDR:%lu.%03luMHz, AHB:%lu.%03luMHz, "
  139. "Ref:%lu.%03luMHz",
  140. ath79_cpu_clk.rate / 1000000,
  141. (ath79_cpu_clk.rate / 1000) % 1000,
  142. ath79_ddr_clk.rate / 1000000,
  143. (ath79_ddr_clk.rate / 1000) % 1000,
  144. ath79_ahb_clk.rate / 1000000,
  145. (ath79_ahb_clk.rate / 1000) % 1000,
  146. ath79_ref_clk.rate / 1000000,
  147. (ath79_ref_clk.rate / 1000) % 1000);
  148. }
  149. /*
  150. * Linux clock API
  151. */
  152. struct clk *clk_get(struct device *dev, const char *id)
  153. {
  154. if (!strcmp(id, "ref"))
  155. return &ath79_ref_clk;
  156. if (!strcmp(id, "cpu"))
  157. return &ath79_cpu_clk;
  158. if (!strcmp(id, "ddr"))
  159. return &ath79_ddr_clk;
  160. if (!strcmp(id, "ahb"))
  161. return &ath79_ahb_clk;
  162. if (!strcmp(id, "wdt"))
  163. return &ath79_wdt_clk;
  164. if (!strcmp(id, "uart"))
  165. return &ath79_uart_clk;
  166. return ERR_PTR(-ENOENT);
  167. }
  168. EXPORT_SYMBOL(clk_get);
  169. int clk_enable(struct clk *clk)
  170. {
  171. return 0;
  172. }
  173. EXPORT_SYMBOL(clk_enable);
  174. void clk_disable(struct clk *clk)
  175. {
  176. }
  177. EXPORT_SYMBOL(clk_disable);
  178. unsigned long clk_get_rate(struct clk *clk)
  179. {
  180. return clk->rate;
  181. }
  182. EXPORT_SYMBOL(clk_get_rate);
  183. void clk_put(struct clk *clk)
  184. {
  185. }
  186. EXPORT_SYMBOL(clk_put);