clk-xway.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /*
  2. * This program is free software; you can redistribute it and/or modify it
  3. * under the terms of the GNU General Public License version 2 as published
  4. * by the Free Software Foundation.
  5. *
  6. * Copyright (C) 2010 John Crispin <blogic@openwrt.org>
  7. */
  8. #include <linux/io.h>
  9. #include <linux/export.h>
  10. #include <linux/init.h>
  11. #include <linux/clk.h>
  12. #include <asm/time.h>
  13. #include <asm/irq.h>
  14. #include <asm/div64.h>
  15. #include <lantiq_soc.h>
  16. static unsigned int ltq_ram_clocks[] = {
  17. CLOCK_167M, CLOCK_133M, CLOCK_111M, CLOCK_83M };
  18. #define DDR_HZ ltq_ram_clocks[ltq_cgu_r32(LTQ_CGU_SYS) & 0x3]
  19. #define BASIC_FREQUENCY_1 35328000
  20. #define BASIC_FREQUENCY_2 36000000
  21. #define BASIS_REQUENCY_USB 12000000
  22. #define GET_BITS(x, msb, lsb) \
  23. (((x) & ((1 << ((msb) + 1)) - 1)) >> (lsb))
  24. #define LTQ_CGU_PLL0_CFG 0x0004
  25. #define LTQ_CGU_PLL1_CFG 0x0008
  26. #define LTQ_CGU_PLL2_CFG 0x000C
  27. #define LTQ_CGU_SYS 0x0010
  28. #define LTQ_CGU_UPDATE 0x0014
  29. #define LTQ_CGU_IF_CLK 0x0018
  30. #define LTQ_CGU_OSC_CON 0x001C
  31. #define LTQ_CGU_SMD 0x0020
  32. #define LTQ_CGU_CT1SR 0x0028
  33. #define LTQ_CGU_CT2SR 0x002C
  34. #define LTQ_CGU_PCMCR 0x0030
  35. #define LTQ_CGU_PCI_CR 0x0034
  36. #define LTQ_CGU_PD_PC 0x0038
  37. #define LTQ_CGU_FMR 0x003C
  38. #define CGU_PLL0_PHASE_DIVIDER_ENABLE \
  39. (ltq_cgu_r32(LTQ_CGU_PLL0_CFG) & (1 << 31))
  40. #define CGU_PLL0_BYPASS \
  41. (ltq_cgu_r32(LTQ_CGU_PLL0_CFG) & (1 << 30))
  42. #define CGU_PLL0_CFG_DSMSEL \
  43. (ltq_cgu_r32(LTQ_CGU_PLL0_CFG) & (1 << 28))
  44. #define CGU_PLL0_CFG_FRAC_EN \
  45. (ltq_cgu_r32(LTQ_CGU_PLL0_CFG) & (1 << 27))
  46. #define CGU_PLL1_SRC \
  47. (ltq_cgu_r32(LTQ_CGU_PLL1_CFG) & (1 << 31))
  48. #define CGU_PLL2_PHASE_DIVIDER_ENABLE \
  49. (ltq_cgu_r32(LTQ_CGU_PLL2_CFG) & (1 << 20))
  50. #define CGU_SYS_FPI_SEL (1 << 6)
  51. #define CGU_SYS_DDR_SEL 0x3
  52. #define CGU_PLL0_SRC (1 << 29)
  53. #define CGU_PLL0_CFG_PLLK GET_BITS(ltq_cgu_r32(LTQ_CGU_PLL0_CFG), 26, 17)
  54. #define CGU_PLL0_CFG_PLLN GET_BITS(ltq_cgu_r32(LTQ_CGU_PLL0_CFG), 12, 6)
  55. #define CGU_PLL0_CFG_PLLM GET_BITS(ltq_cgu_r32(LTQ_CGU_PLL0_CFG), 5, 2)
  56. #define CGU_PLL2_SRC GET_BITS(ltq_cgu_r32(LTQ_CGU_PLL2_CFG), 18, 17)
  57. #define CGU_PLL2_CFG_INPUT_DIV GET_BITS(ltq_cgu_r32(LTQ_CGU_PLL2_CFG), 16, 13)
  58. static unsigned int ltq_get_pll0_fdiv(void);
  59. static inline unsigned int get_input_clock(int pll)
  60. {
  61. switch (pll) {
  62. case 0:
  63. if (ltq_cgu_r32(LTQ_CGU_PLL0_CFG) & CGU_PLL0_SRC)
  64. return BASIS_REQUENCY_USB;
  65. else if (CGU_PLL0_PHASE_DIVIDER_ENABLE)
  66. return BASIC_FREQUENCY_1;
  67. else
  68. return BASIC_FREQUENCY_2;
  69. case 1:
  70. if (CGU_PLL1_SRC)
  71. return BASIS_REQUENCY_USB;
  72. else if (CGU_PLL0_PHASE_DIVIDER_ENABLE)
  73. return BASIC_FREQUENCY_1;
  74. else
  75. return BASIC_FREQUENCY_2;
  76. case 2:
  77. switch (CGU_PLL2_SRC) {
  78. case 0:
  79. return ltq_get_pll0_fdiv();
  80. case 1:
  81. return CGU_PLL2_PHASE_DIVIDER_ENABLE ?
  82. BASIC_FREQUENCY_1 :
  83. BASIC_FREQUENCY_2;
  84. case 2:
  85. return BASIS_REQUENCY_USB;
  86. }
  87. default:
  88. return 0;
  89. }
  90. }
  91. static inline unsigned int cal_dsm(int pll, unsigned int num, unsigned int den)
  92. {
  93. u64 res, clock = get_input_clock(pll);
  94. res = num * clock;
  95. do_div(res, den);
  96. return res;
  97. }
  98. static inline unsigned int mash_dsm(int pll, unsigned int M, unsigned int N,
  99. unsigned int K)
  100. {
  101. unsigned int num = ((N + 1) << 10) + K;
  102. unsigned int den = (M + 1) << 10;
  103. return cal_dsm(pll, num, den);
  104. }
  105. static inline unsigned int ssff_dsm_1(int pll, unsigned int M, unsigned int N,
  106. unsigned int K)
  107. {
  108. unsigned int num = ((N + 1) << 11) + K + 512;
  109. unsigned int den = (M + 1) << 11;
  110. return cal_dsm(pll, num, den);
  111. }
  112. static inline unsigned int ssff_dsm_2(int pll, unsigned int M, unsigned int N,
  113. unsigned int K)
  114. {
  115. unsigned int num = K >= 512 ?
  116. ((N + 1) << 12) + K - 512 : ((N + 1) << 12) + K + 3584;
  117. unsigned int den = (M + 1) << 12;
  118. return cal_dsm(pll, num, den);
  119. }
  120. static inline unsigned int dsm(int pll, unsigned int M, unsigned int N,
  121. unsigned int K, unsigned int dsmsel, unsigned int phase_div_en)
  122. {
  123. if (!dsmsel)
  124. return mash_dsm(pll, M, N, K);
  125. else if (!phase_div_en)
  126. return mash_dsm(pll, M, N, K);
  127. else
  128. return ssff_dsm_2(pll, M, N, K);
  129. }
  130. static inline unsigned int ltq_get_pll0_fosc(void)
  131. {
  132. if (CGU_PLL0_BYPASS)
  133. return get_input_clock(0);
  134. else
  135. return !CGU_PLL0_CFG_FRAC_EN
  136. ? dsm(0, CGU_PLL0_CFG_PLLM, CGU_PLL0_CFG_PLLN, 0,
  137. CGU_PLL0_CFG_DSMSEL,
  138. CGU_PLL0_PHASE_DIVIDER_ENABLE)
  139. : dsm(0, CGU_PLL0_CFG_PLLM, CGU_PLL0_CFG_PLLN,
  140. CGU_PLL0_CFG_PLLK, CGU_PLL0_CFG_DSMSEL,
  141. CGU_PLL0_PHASE_DIVIDER_ENABLE);
  142. }
  143. static unsigned int ltq_get_pll0_fdiv(void)
  144. {
  145. unsigned int div = CGU_PLL2_CFG_INPUT_DIV + 1;
  146. return (ltq_get_pll0_fosc() + (div >> 1)) / div;
  147. }
  148. unsigned int ltq_get_io_region_clock(void)
  149. {
  150. unsigned int ret = ltq_get_pll0_fosc();
  151. switch (ltq_cgu_r32(LTQ_CGU_PLL2_CFG) & CGU_SYS_DDR_SEL) {
  152. default:
  153. case 0:
  154. return (ret + 1) / 2;
  155. case 1:
  156. return (ret * 2 + 2) / 5;
  157. case 2:
  158. return (ret + 1) / 3;
  159. case 3:
  160. return (ret + 2) / 4;
  161. }
  162. }
  163. EXPORT_SYMBOL(ltq_get_io_region_clock);
  164. unsigned int ltq_get_fpi_bus_clock(int fpi)
  165. {
  166. unsigned int ret = ltq_get_io_region_clock();
  167. if ((fpi == 2) && (ltq_cgu_r32(LTQ_CGU_SYS) & CGU_SYS_FPI_SEL))
  168. ret >>= 1;
  169. return ret;
  170. }
  171. EXPORT_SYMBOL(ltq_get_fpi_bus_clock);
  172. unsigned int ltq_get_cpu_hz(void)
  173. {
  174. switch (ltq_cgu_r32(LTQ_CGU_SYS) & 0xc) {
  175. case 0:
  176. return CLOCK_333M;
  177. case 4:
  178. return DDR_HZ;
  179. case 8:
  180. return DDR_HZ << 1;
  181. default:
  182. return DDR_HZ >> 1;
  183. }
  184. }
  185. EXPORT_SYMBOL(ltq_get_cpu_hz);
  186. unsigned int ltq_get_fpi_hz(void)
  187. {
  188. unsigned int ddr_clock = DDR_HZ;
  189. if (ltq_cgu_r32(LTQ_CGU_SYS) & 0x40)
  190. return ddr_clock >> 1;
  191. return ddr_clock;
  192. }
  193. EXPORT_SYMBOL(ltq_get_fpi_hz);