clock.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. void __init ath79_clocks_init(void)
  85. {
  86. if (soc_is_ar71xx())
  87. ar71xx_clocks_init();
  88. else if (soc_is_ar724x())
  89. ar724x_clocks_init();
  90. else if (soc_is_ar913x())
  91. ar913x_clocks_init();
  92. else
  93. BUG();
  94. pr_info("Clocks: CPU:%lu.%03luMHz, DDR:%lu.%03luMHz, AHB:%lu.%03luMHz, "
  95. "Ref:%lu.%03luMHz",
  96. ath79_cpu_clk.rate / 1000000,
  97. (ath79_cpu_clk.rate / 1000) % 1000,
  98. ath79_ddr_clk.rate / 1000000,
  99. (ath79_ddr_clk.rate / 1000) % 1000,
  100. ath79_ahb_clk.rate / 1000000,
  101. (ath79_ahb_clk.rate / 1000) % 1000,
  102. ath79_ref_clk.rate / 1000000,
  103. (ath79_ref_clk.rate / 1000) % 1000);
  104. }
  105. /*
  106. * Linux clock API
  107. */
  108. struct clk *clk_get(struct device *dev, const char *id)
  109. {
  110. if (!strcmp(id, "ref"))
  111. return &ath79_ref_clk;
  112. if (!strcmp(id, "cpu"))
  113. return &ath79_cpu_clk;
  114. if (!strcmp(id, "ddr"))
  115. return &ath79_ddr_clk;
  116. if (!strcmp(id, "ahb"))
  117. return &ath79_ahb_clk;
  118. if (!strcmp(id, "wdt"))
  119. return &ath79_wdt_clk;
  120. if (!strcmp(id, "uart"))
  121. return &ath79_uart_clk;
  122. return ERR_PTR(-ENOENT);
  123. }
  124. EXPORT_SYMBOL(clk_get);
  125. int clk_enable(struct clk *clk)
  126. {
  127. return 0;
  128. }
  129. EXPORT_SYMBOL(clk_enable);
  130. void clk_disable(struct clk *clk)
  131. {
  132. }
  133. EXPORT_SYMBOL(clk_disable);
  134. unsigned long clk_get_rate(struct clk *clk)
  135. {
  136. return clk->rate;
  137. }
  138. EXPORT_SYMBOL(clk_get_rate);
  139. void clk_put(struct clk *clk)
  140. {
  141. }
  142. EXPORT_SYMBOL(clk_put);