clk_set.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. #include <linux/module.h>
  2. #include <linux/delay.h>
  3. #include <mach/am_regs.h>
  4. #include <mach/clk_set.h>
  5. #include <mach/clock.h>
  6. struct pll_reg_table {
  7. unsigned long xtal_clk;
  8. unsigned long out_clk;
  9. unsigned long settings;
  10. };
  11. unsigned long get_xtal_clock(void)
  12. {
  13. unsigned long clk;
  14. clk = READ_CBUS_REG_BITS(PREG_CTLREG0_ADDR, 4, 5);
  15. clk = clk * 1000 * 1000;
  16. return clk;
  17. }
  18. /*
  19. Get two number's max common divisor;
  20. */
  21. static int get_max_common_divisor(int a, int b)
  22. {
  23. while (b) {
  24. int temp = b;
  25. b = a % b;
  26. a = temp;
  27. }
  28. return a;
  29. }
  30. /*
  31. select clk:
  32. 7-SYS_PLL_DIV2_CLK
  33. 6-VID2_PLL_CLK
  34. 5-VID_PLL_CLK
  35. 4-AUDIO_PLL_CLK
  36. 3-DDR_PLL_CLK
  37. 2-MISC_PLL_CLK
  38. 1-SYS_PLL_CLK
  39. 0-XTAL (25Mhz)
  40. clk_freq:50M=50000000
  41. output_clk:50000000;
  42. aways,maybe changed for others?
  43. */
  44. int eth_clk_set(int selectclk, unsigned long clk_freq, unsigned long out_clk, unsigned int clk_invert)
  45. {
  46. int n;
  47. printk("select eth clk-%d,source=%ld,out=%ld\n", selectclk, clk_freq, out_clk);
  48. if (((clk_freq) % out_clk) != 0) {
  49. printk(KERN_ERR "ERROR:source clk must n times of out_clk=%ld ,source clk=%ld\n", out_clk, clk_freq);
  50. return -1;
  51. } else {
  52. n = (int)((clk_freq) / out_clk);
  53. }
  54. WRITE_CBUS_REG(HHI_ETH_CLK_CNTL,
  55. (n - 1) << 0 |
  56. selectclk << 9 |
  57. 1 << 8 //enable clk
  58. );
  59. udelay(100);
  60. return 0;
  61. }
  62. int auto_select_eth_clk(void)
  63. {
  64. return -1;
  65. }
  66. //0x065e11ff,0x0249a941, // min current for 750~1300
  67. //0x065e31ff,0xbe49a941, // max current for 500~750
  68. static unsigned pll_setting[17][3]={
  69. {0x20222,0x065e11ff,0x0249a941},
  70. {0x2022a,0x065e11ff,0x0249a941},
  71. {0x20232,0x065e11ff,0x0249a941},
  72. {0x2023a,0x065e31ff,0xbe49a941},
  73. {0x10221,0x065e11ff,0x0249a941},
  74. {0x10225,0x065e11ff,0x0249a941},
  75. {0x1022a,0x065e11ff,0x0249a941},
  76. {0x1022e,0x065e11ff,0x0249a941},
  77. {0x10232,0x065e11ff,0x0249a941},
  78. {0x10236,0x065e11ff,0x0249a941},
  79. {0x1023a,0x065e31ff,0xbe49a941},
  80. {0x1023e,0x065e31ff,0xbe49a941},
  81. {0x00220,0x065e11ff,0x0249a941},
  82. {0x00220,0x065e11ff,0x0249a941},
  83. {0x00221,0x065e11ff,0x0249a941},
  84. {0x00221,0x065e11ff,0x0249a941},
  85. {0x00222,0x065e11ff,0x0249a941},
  86. };
  87. unsigned get_sys_clkpll_setting(unsigned crystal_freq, unsigned out_freq)
  88. {
  89. unsigned long crys_M, out_M, i;
  90. if (!crystal_freq)
  91. crystal_freq = get_xtal_clock();
  92. crys_M = crystal_freq / 1000000;
  93. out_M = out_freq / 1000000;
  94. i = (out_M-200)/50;
  95. if (i>16) i=16;
  96. return pll_setting[i][0];
  97. }
  98. int sys_clkpll_setting(unsigned crystal_freq, unsigned out_freq)
  99. {
  100. int i, lock_flag;
  101. unsigned lock_time=0;
  102. unsigned long result_freq, target_freq;
  103. unsigned long crys_M, out_M;
  104. unsigned long freq_log[64];
  105. int log_index;
  106. unsigned target_pll_setting;
  107. if (!crystal_freq)
  108. crystal_freq = get_xtal_clock();
  109. crys_M = crystal_freq / 1000000;
  110. out_M = out_freq / 1000000;
  111. i = (out_M-200)/50;
  112. if (i>16) i=16;
  113. target_pll_setting = pll_setting[i][0];
  114. if (READ_MPEG_REG(HHI_SYS_PLL_CNTL)!=target_pll_setting){
  115. WRITE_MPEG_REG(HHI_SYS_PLL_CNTL, target_pll_setting);
  116. WRITE_MPEG_REG(HHI_SYS_PLL_CNTL2, pll_setting[i][1]);
  117. WRITE_MPEG_REG(HHI_SYS_PLL_CNTL3, pll_setting[i][2]);
  118. WRITE_MPEG_REG(RESET5_REGISTER, (1<<2)); // reset sys pll
  119. lock_flag = 0;
  120. log_index = 0;
  121. target_freq = ((target_pll_setting&0x1ff)*crys_M)>>(target_pll_setting>>16);
  122. for (i=0;i<64;i++){
  123. result_freq = clk_util_clk_msr(SYS_PLL_CLK);
  124. if ((result_freq <= target_freq+1)&&(result_freq >= target_freq-1)){
  125. lock_flag++;
  126. if (lock_flag>=1)
  127. break;
  128. }
  129. if (log_index<64)
  130. freq_log[log_index++]=result_freq;
  131. else
  132. break;
  133. lock_time+=64;
  134. }
  135. lock_time-=64;
  136. //printk("sys clk changed");
  137. //for (i=0;i<log_index;i++)
  138. // printk("-%d", freq_log[i]);
  139. //printk("\ncpu_clk_changed: out_freq=%ld,pll_setting=%x,locktime=%dus\n",out_M,target_pll_setting,lock_time);
  140. }
  141. return 0;
  142. }
  143. int misc_pll_setting(unsigned crystal_freq, unsigned out_freq)
  144. {
  145. int n, m, od;
  146. unsigned long crys_M, out_M, middle_freq;
  147. unsigned long flags;
  148. if (!crystal_freq) {
  149. crystal_freq = get_xtal_clock();
  150. }
  151. crys_M = crystal_freq / 1000000;
  152. out_M = out_freq / 1000000;
  153. if (out_M < 400) {
  154. /*if <400M, Od=1*/
  155. od = 1;/*out=pll_out/(1<<od)*/
  156. out_M = out_M << 1;
  157. } else {
  158. od = 0;
  159. }
  160. middle_freq = get_max_common_divisor(crys_M, out_M);
  161. n = crys_M / middle_freq;
  162. m = out_M / (middle_freq);
  163. if (n > (1 << 5) - 1) {
  164. printk(KERN_ERR "misc_pll_setting error, n is too bigger n=%d,crys_M=%ldM,out=%ldM\n",
  165. n, crys_M, out_M);
  166. return -1;
  167. }
  168. if (m > (1 << 9) - 1) {
  169. printk(KERN_ERR "misc_pll_setting error, m is too bigger m=%d,crys_M=%ldM,out=%ldM\n",
  170. m, crys_M, out_M);
  171. return -2;
  172. }
  173. local_irq_save(flags);
  174. WRITE_MPEG_REG(HHI_OTHER_PLL_CNTL,
  175. m |
  176. n << 9 |
  177. (od & 1) << 16
  178. ); // misc PLL
  179. WRITE_MPEG_REG(RESET5_REGISTER, (1<<1)); // reset misc pll
  180. WRITE_AOBUS_REG_BITS(AO_UART_CONTROL, (((out_freq/4) / (115200*4)) - 1) & 0xfff, 0, 12);
  181. local_irq_restore(flags);
  182. udelay(100);
  183. return 0;
  184. }
  185. int audio_pll_setting(unsigned crystal_freq, unsigned out_freq)
  186. {
  187. int n, m, od;
  188. unsigned long crys_M, out_M, middle_freq;
  189. /*
  190. FIXME:If we need can't exact setting this clock,Can used a pll table?
  191. */
  192. if (!crystal_freq) {
  193. crystal_freq = get_xtal_clock();
  194. }
  195. crys_M = crystal_freq / 1000000;
  196. out_M = out_freq / 1000000;
  197. if (out_M < 400) {
  198. /*if <400M, Od=1*/
  199. od = 1;/*out=pll_out/(1<<od)
  200. */
  201. out_M = out_M << 1;
  202. } else {
  203. od = 0;
  204. }
  205. middle_freq = get_max_common_divisor(crys_M, out_M);
  206. n = crys_M / middle_freq;
  207. m = out_M / (middle_freq);
  208. if (n > (1 << 5) - 1) {
  209. printk(KERN_ERR "audio_pll_setting error, n is too bigger n=%d,crys_M=%ldM,out=%ldM\n",
  210. n, crys_M, out_M);
  211. return -1;
  212. }
  213. if (m > (1 << 9) - 1) {
  214. printk(KERN_ERR "audio_pll_setting error, m is too bigger m=%d,crys_M=%ldM,out=%ldM\n",
  215. m, crys_M, out_M);
  216. return -2;
  217. }
  218. WRITE_MPEG_REG(HHI_AUD_PLL_CNTL,
  219. m |
  220. n << 9 |
  221. (od & 1) << 14
  222. ); // audio PLL
  223. printk(KERN_INFO "audio_pll_setting to crystal_req=%ld,out_freq=%ld,n=%d,m=%d,od=%d\n", crys_M, out_M / (od + 1), n, m, od);
  224. return 0;
  225. }
  226. int video_pll_setting(unsigned crystal_freq, unsigned out_freq, int powerdown, int flags)
  227. {
  228. int n, m, od;
  229. unsigned long crys_M, out_M, middle_freq;
  230. int ret = 0;
  231. /*
  232. flags can used for od1/xd settings
  233. FIXME:If we needn't exact setting this clock,Can used a pll table?
  234. */
  235. if (!crystal_freq) {
  236. crystal_freq = get_xtal_clock();
  237. }
  238. crys_M = crystal_freq / 1000000;
  239. out_M = out_freq / 1000000;
  240. if (out_M < 750) {
  241. /* if <750M, Od=1 */
  242. od = 1;/* out=pll_out/(1<<od) */
  243. out_M = out_M << 1;
  244. } else {
  245. od = 0;
  246. }
  247. middle_freq = get_max_common_divisor(crys_M, out_M);
  248. n = crys_M / middle_freq;
  249. m = out_M / (middle_freq);
  250. if (n > (1 << 5) - 1) {
  251. printk(KERN_ERR "video_pll_setting error, n is too bigger n=%d,crys_M=%ldM,out=%ldM\n",
  252. n, crys_M, out_M);
  253. ret = -1;
  254. }
  255. if (m > (1 << 9) - 1) {
  256. printk(KERN_ERR "video_pll_setting error, m is too bigger m=%d,crys_M=%ldM,out=%ldM\n",
  257. m, crys_M, out_M);
  258. ret = -2;
  259. }
  260. if (ret) {
  261. return ret;
  262. }
  263. /* There are some differents between M1 and M3*/
  264. WRITE_MPEG_REG(HHI_VID_PLL_CNTL,
  265. m |
  266. n << 10 |
  267. (od & 0x3) << 20 |
  268. (!!powerdown) << 30 /*is power down mode?*/
  269. ); // video PLL
  270. printk(KERN_INFO "video_pll_setting to crystal_req=%ld,out_freq=%ld,n=%d,m=%d,od=%d\n", crys_M, out_M / (od + 1), n, m, od);
  271. return 0;
  272. }