clocks.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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/blackfin.h>
  10. #ifdef PLL_CTL
  11. # include <asm/mach-common/bits/pll.h>
  12. # define pll_is_bypassed() (bfin_read_PLL_STAT() & DF)
  13. #else
  14. # include <asm/mach-common/bits/cgu.h>
  15. # define pll_is_bypassed() (bfin_read_CGU_STAT() & PLLBP)
  16. # define bfin_read_PLL_CTL() bfin_read_CGU_CTL()
  17. # define bfin_read_PLL_DIV() bfin_read_CGU_DIV()
  18. #endif
  19. /* Get the voltage input multiplier */
  20. u_long get_vco(void)
  21. {
  22. static u_long cached_vco_pll_ctl, cached_vco;
  23. u_long msel, pll_ctl;
  24. pll_ctl = bfin_read_PLL_CTL();
  25. if (pll_ctl == cached_vco_pll_ctl)
  26. return cached_vco;
  27. else
  28. cached_vco_pll_ctl = pll_ctl;
  29. msel = (pll_ctl & MSEL) >> MSEL_P;
  30. if (0 == msel)
  31. msel = (MSEL >> MSEL_P) + 1;
  32. cached_vco = CONFIG_CLKIN_HZ;
  33. cached_vco >>= (pll_ctl & DF);
  34. cached_vco *= msel;
  35. return cached_vco;
  36. }
  37. /* Get the Core clock */
  38. u_long get_cclk(void)
  39. {
  40. static u_long cached_cclk_pll_div, cached_cclk;
  41. u_long div, csel, ssel;
  42. if (pll_is_bypassed())
  43. return CONFIG_CLKIN_HZ;
  44. div = bfin_read_PLL_DIV();
  45. if (div == cached_cclk_pll_div)
  46. return cached_cclk;
  47. else
  48. cached_cclk_pll_div = div;
  49. csel = (div & CSEL) >> CSEL_P;
  50. #ifndef CGU_DIV
  51. ssel = (div & SSEL) >> SSEL_P;
  52. if (ssel && ssel < (1 << csel)) /* SCLK > CCLK */
  53. cached_cclk = get_vco() / ssel;
  54. else
  55. cached_cclk = get_vco() >> csel;
  56. #else
  57. cached_cclk = get_vco() / csel;
  58. #endif
  59. return cached_cclk;
  60. }
  61. /* Get the System clock */
  62. #ifdef CGU_DIV
  63. static u_long cached_sclk_pll_div, cached_sclk;
  64. static u_long cached_sclk0, cached_sclk1, cached_dclk;
  65. static u_long _get_sclk(u_long *cache)
  66. {
  67. u_long div, ssel;
  68. if (pll_is_bypassed())
  69. return CONFIG_CLKIN_HZ;
  70. div = bfin_read_PLL_DIV();
  71. if (div == cached_sclk_pll_div)
  72. return *cache;
  73. else
  74. cached_sclk_pll_div = div;
  75. ssel = (div & SYSSEL) >> SYSSEL_P;
  76. cached_sclk = get_vco() / ssel;
  77. ssel = (div & S0SEL) >> S0SEL_P;
  78. cached_sclk0 = cached_sclk / ssel;
  79. ssel = (div & S1SEL) >> S1SEL_P;
  80. cached_sclk1 = cached_sclk / ssel;
  81. ssel = (div & DSEL) >> DSEL_P;
  82. cached_dclk = get_vco() / ssel;
  83. return *cache;
  84. }
  85. u_long get_sclk(void)
  86. {
  87. return _get_sclk(&cached_sclk);
  88. }
  89. u_long get_sclk0(void)
  90. {
  91. return _get_sclk(&cached_sclk0);
  92. }
  93. u_long get_sclk1(void)
  94. {
  95. return _get_sclk(&cached_sclk1);
  96. }
  97. u_long get_dclk(void)
  98. {
  99. return _get_sclk(&cached_dclk);
  100. }
  101. #else
  102. u_long get_sclk(void)
  103. {
  104. static u_long cached_sclk_pll_div, cached_sclk;
  105. u_long div, ssel;
  106. if (pll_is_bypassed())
  107. return CONFIG_CLKIN_HZ;
  108. div = bfin_read_PLL_DIV();
  109. if (div == cached_sclk_pll_div)
  110. return cached_sclk;
  111. else
  112. cached_sclk_pll_div = div;
  113. ssel = (div & SSEL) >> SSEL_P;
  114. cached_sclk = get_vco() / ssel;
  115. return cached_sclk;
  116. }
  117. #endif