clocks.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /*
  2. * clocks.c - figure out sclk/cclk/vco and such
  3. *
  4. * Copyright (c) 2005-2008 Analog Devices Inc.
  5. *
  6. * Licensed under the GPL-2 or later.
  7. */
  8. #include <common.h>
  9. #include <asm/clock.h>
  10. /* Get the voltage input multiplier */
  11. u_long get_vco(void)
  12. {
  13. static u_long cached_vco_pll_ctl, cached_vco;
  14. u_long msel, pll_ctl;
  15. pll_ctl = bfin_read_PLL_CTL();
  16. if (pll_ctl == cached_vco_pll_ctl)
  17. return cached_vco;
  18. else
  19. cached_vco_pll_ctl = pll_ctl;
  20. msel = (pll_ctl & MSEL) >> MSEL_P;
  21. if (0 == msel)
  22. msel = (MSEL >> MSEL_P) + 1;
  23. cached_vco = CONFIG_CLKIN_HZ;
  24. cached_vco >>= (pll_ctl & DF);
  25. cached_vco *= msel;
  26. return cached_vco;
  27. }
  28. /* Get the Core clock */
  29. u_long get_cclk(void)
  30. {
  31. static u_long cached_cclk_pll_div, cached_cclk;
  32. u_long div, csel;
  33. #ifndef CGU_DIV
  34. u_long ssel;
  35. #endif
  36. if (pll_is_bypassed())
  37. return CONFIG_CLKIN_HZ;
  38. div = bfin_read_PLL_DIV();
  39. if (div == cached_cclk_pll_div)
  40. return cached_cclk;
  41. else
  42. cached_cclk_pll_div = div;
  43. csel = (div & CSEL) >> CSEL_P;
  44. #ifndef CGU_DIV
  45. ssel = (div & SSEL) >> SSEL_P;
  46. if (ssel && ssel < (1 << csel)) /* SCLK > CCLK */
  47. cached_cclk = get_vco() / ssel;
  48. else
  49. cached_cclk = get_vco() >> csel;
  50. #else
  51. cached_cclk = get_vco() / csel;
  52. #endif
  53. return cached_cclk;
  54. }
  55. /* Get the System clock */
  56. #ifdef CGU_DIV
  57. static u_long cached_sclk_pll_div, cached_sclk;
  58. static u_long cached_sclk0, cached_sclk1, cached_dclk;
  59. static u_long _get_sclk(u_long *cache)
  60. {
  61. u_long div, ssel;
  62. if (pll_is_bypassed())
  63. return CONFIG_CLKIN_HZ;
  64. div = bfin_read_PLL_DIV();
  65. if (div == cached_sclk_pll_div)
  66. return *cache;
  67. else
  68. cached_sclk_pll_div = div;
  69. ssel = (div & SYSSEL) >> SYSSEL_P;
  70. cached_sclk = get_vco() / ssel;
  71. ssel = (div & S0SEL) >> S0SEL_P;
  72. cached_sclk0 = cached_sclk / ssel;
  73. ssel = (div & S1SEL) >> S1SEL_P;
  74. cached_sclk1 = cached_sclk / ssel;
  75. ssel = (div & DSEL) >> DSEL_P;
  76. cached_dclk = get_vco() / ssel;
  77. return *cache;
  78. }
  79. u_long get_sclk(void)
  80. {
  81. return _get_sclk(&cached_sclk);
  82. }
  83. u_long get_sclk0(void)
  84. {
  85. return _get_sclk(&cached_sclk0);
  86. }
  87. u_long get_sclk1(void)
  88. {
  89. return _get_sclk(&cached_sclk1);
  90. }
  91. u_long get_dclk(void)
  92. {
  93. return _get_sclk(&cached_dclk);
  94. }
  95. #else
  96. u_long get_sclk(void)
  97. {
  98. static u_long cached_sclk_pll_div, cached_sclk;
  99. u_long div, ssel;
  100. if (pll_is_bypassed())
  101. return CONFIG_CLKIN_HZ;
  102. div = bfin_read_PLL_DIV();
  103. if (div == cached_sclk_pll_div)
  104. return cached_sclk;
  105. else
  106. cached_sclk_pll_div = div;
  107. ssel = (div & SSEL) >> SSEL_P;
  108. cached_sclk = get_vco() / ssel;
  109. return cached_sclk;
  110. }
  111. #endif