system_clock.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /*
  2. * File : clock.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2006, RT-Thread Development Team
  5. *
  6. * The license and distribution terms for this file may be
  7. * found in the file LICENSE in this distribution or at
  8. * http://openlab.rt-thread.com/license/LICENSE
  9. *
  10. * Change Logs:
  11. * Date Author Notes
  12. * 2008-04-25 Yi.qiu first version
  13. */
  14. #include <rtthread.h>
  15. #include "s3c24x0.h"
  16. #define CONFIG_SYS_CLK_FREQ 12000000 // Fin = 12.00MHz
  17. #if CONFIG_SYS_CLK_FREQ == 12000000
  18. /* MPLL=2*12*100/6=400MHz */
  19. #define MPL_MIDV 92 /* m=MPL_MDIV+8=100 */
  20. #define MPL_PDIV 4 /* p=MPL_PDIV+2=6 */
  21. #define MPL_SDIV 0 /* s=MPL_SDIV=0 */
  22. /* UPLL=12*64/8=96MHz */
  23. #define UPL_MDIV 56 /* m=UPL_MDIV+8=64 */
  24. #define UPL_PDIV 2 /* p=UPL_PDIV+2=4 */
  25. #define UPL_SDIV 1 /* s=UPL_SDIV=1 */
  26. /* System clock divider FCLK:HCLK:PCLK=1:4:8 */
  27. #define DIVN_UPLL 0x1 /* UCLK = UPLL clock / 2 */
  28. #define HDIVN 0x2 /* HCLK = FCLK / 4 */
  29. #define PDIVN 0x1 /* PCLK = HCLK / 2 */
  30. #endif
  31. rt_uint32_t PCLK;
  32. rt_uint32_t FCLK;
  33. rt_uint32_t HCLK;
  34. rt_uint32_t UCLK;
  35. void rt_hw_get_clock(void)
  36. {
  37. rt_uint32_t val;
  38. rt_uint8_t m, p, s;
  39. val = MPLLCON;
  40. m = (val>>12)&0xff;
  41. p = (val>>4)&0x3f;
  42. s = val&3;
  43. FCLK = ((m+8)*(CONFIG_SYS_CLK_FREQ/100)*2)/((p+2)*(1<<s))*100;
  44. val = CLKDIVN;
  45. m = (val>>1)&3;
  46. p = val&1;
  47. switch (m) {
  48. case 0:
  49. HCLK = FCLK;
  50. break;
  51. case 1:
  52. HCLK = FCLK>>1;
  53. break;
  54. case 2:
  55. if(s&2)
  56. HCLK = FCLK>>3;
  57. else
  58. HCLK = FCLK>>2;
  59. break;
  60. case 3:
  61. if(s&1)
  62. HCLK = FCLK/6;
  63. else
  64. HCLK = FCLK/3;
  65. break;
  66. }
  67. if(p)
  68. PCLK = HCLK>>1;
  69. else
  70. PCLK = HCLK;
  71. }
  72. void rt_hw_set_mpll_clock(rt_uint8_t sdiv, rt_uint8_t pdiv, rt_uint8_t mdiv)
  73. {
  74. MPLLCON = sdiv | (pdiv<<4) | (mdiv<<12);
  75. }
  76. void rt_hw_set_upll_clock(rt_uint8_t sdiv, rt_uint8_t pdiv, rt_uint8_t mdiv)
  77. {
  78. UPLLCON = (mdiv<<12) | (pdiv<<4) | sdiv;
  79. }
  80. void rt_hw_set_divider(rt_uint8_t hdivn, rt_uint8_t pdivn)
  81. {
  82. CLKDIVN = (hdivn<<1) | pdivn;
  83. }
  84. /**
  85. * @brief System Clock Configuration
  86. */
  87. void rt_hw_clock_init(void)
  88. {
  89. LOCKTIME = 0xFFFFFFFF;
  90. rt_hw_set_mpll_clock(MPL_SDIV, MPL_PDIV, MPL_MIDV);
  91. rt_hw_set_upll_clock(UPL_SDIV, UPL_PDIV, UPL_MDIV);
  92. rt_hw_set_divider(HDIVN, PDIVN);
  93. }