clk.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*
  2. * Copyright (C) 2011 by Vladimir Zapolskiy <vz@mleia.com>
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation; either version 2
  7. * of the License, or (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  17. * MA 02110-1301, USA.
  18. */
  19. #include <common.h>
  20. #include <div64.h>
  21. #include <asm/arch/cpu.h>
  22. #include <asm/arch/clk.h>
  23. #include <asm/io.h>
  24. static struct clk_pm_regs *clk = (struct clk_pm_regs *)CLK_PM_BASE;
  25. unsigned int get_sys_clk_rate(void)
  26. {
  27. if (readl(&clk->sysclk_ctrl) & CLK_SYSCLK_PLL397)
  28. return RTC_CLK_FREQUENCY * 397;
  29. else
  30. return OSC_CLK_FREQUENCY;
  31. }
  32. unsigned int get_hclk_pll_rate(void)
  33. {
  34. unsigned long long fin, fref, fcco, fout;
  35. u32 val, m_div, n_div, p_div;
  36. /*
  37. * Valid frequency ranges:
  38. * 1 * 10^6 <= Fin <= 20 * 10^6
  39. * 1 * 10^6 <= Fref <= 27 * 10^6
  40. * 156 * 10^6 <= Fcco <= 320 * 10^6
  41. */
  42. fref = fin = get_sys_clk_rate();
  43. if (fin > 20000000ULL || fin < 1000000ULL)
  44. return 0;
  45. val = readl(&clk->hclkpll_ctrl);
  46. m_div = ((val & CLK_HCLK_PLL_FEEDBACK_DIV_MASK) >> 1) + 1;
  47. n_div = ((val & CLK_HCLK_PLL_PREDIV_MASK) >> 9) + 1;
  48. if (val & CLK_HCLK_PLL_DIRECT)
  49. p_div = 0;
  50. else
  51. p_div = ((val & CLK_HCLK_PLL_POSTDIV_MASK) >> 11) + 1;
  52. p_div = 1 << p_div;
  53. if (val & CLK_HCLK_PLL_BYPASS) {
  54. do_div(fin, p_div);
  55. return fin;
  56. }
  57. do_div(fref, n_div);
  58. if (fref > 27000000ULL || fref < 1000000ULL)
  59. return 0;
  60. fout = fref * m_div;
  61. if (val & CLK_HCLK_PLL_FEEDBACK) {
  62. fcco = fout;
  63. do_div(fout, p_div);
  64. } else
  65. fcco = fout * p_div;
  66. if (fcco > 320000000ULL || fcco < 156000000ULL)
  67. return 0;
  68. return fout;
  69. }
  70. unsigned int get_hclk_clk_div(void)
  71. {
  72. u32 val;
  73. val = readl(&clk->hclkdiv_ctrl) & CLK_HCLK_ARM_PLL_DIV_MASK;
  74. return 1 << val;
  75. }
  76. unsigned int get_hclk_clk_rate(void)
  77. {
  78. return get_hclk_pll_rate() / get_hclk_clk_div();
  79. }
  80. unsigned int get_periph_clk_div(void)
  81. {
  82. u32 val;
  83. val = readl(&clk->hclkdiv_ctrl) & CLK_HCLK_PERIPH_DIV_MASK;
  84. return (val >> 2) + 1;
  85. }
  86. unsigned int get_periph_clk_rate(void)
  87. {
  88. if (!(readl(&clk->pwr_ctrl) & CLK_PWR_NORMAL_RUN))
  89. return get_sys_clk_rate();
  90. return get_hclk_pll_rate() / get_periph_clk_div();
  91. }
  92. int get_serial_clock(void)
  93. {
  94. return get_periph_clk_rate();
  95. }