speed.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /*
  2. * Cirrus Logic EP93xx PLL support.
  3. *
  4. * Copyright (C) 2009 Matthias Kaehlcke <matthias@kaehlcke.net>
  5. *
  6. * See file CREDITS for list of people who contributed to this project.
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  15. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  16. * for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License along
  19. * with this program; if not, write to the Free Software Foundation, Inc.,
  20. * 675 Mass Ave, Cambridge, MA 02139, USA.
  21. */
  22. #include <common.h>
  23. #include <asm/arch/ep93xx.h>
  24. #include <asm/io.h>
  25. #include <div64.h>
  26. /*
  27. * CONFIG_SYS_CLK_FREQ should be defined as the input frequency of the PLL.
  28. *
  29. * get_FCLK(), get_HCLK(), get_PCLK() and get_UCLK() return the clock of
  30. * the specified bus in HZ.
  31. */
  32. /*
  33. * return the PLL output frequency
  34. *
  35. * PLL rate = CONFIG_SYS_CLK_FREQ * (X1FBD + 1) * (X2FBD + 1)
  36. * / (X2IPD + 1) / 2^PS
  37. */
  38. static ulong get_PLLCLK(uint32_t *pllreg)
  39. {
  40. uint8_t i;
  41. const uint32_t clkset = readl(pllreg);
  42. uint64_t rate = CONFIG_SYS_CLK_FREQ;
  43. rate *= ((clkset >> SYSCON_CLKSET_PLL_X1FBD1_SHIFT) & 0x1f) + 1;
  44. rate *= ((clkset >> SYSCON_CLKSET_PLL_X2FBD2_SHIFT) & 0x3f) + 1;
  45. do_div(rate, (clkset & 0x1f) + 1); /* X2IPD */
  46. for (i = 0; i < ((clkset >> SYSCON_CLKSET_PLL_PS_SHIFT) & 3); i++)
  47. rate >>= 1;
  48. return (ulong)rate;
  49. }
  50. /* return FCLK frequency */
  51. ulong get_FCLK()
  52. {
  53. const uint8_t fclk_divisors[] = { 1, 2, 4, 8, 16, 1, 1, 1 };
  54. struct syscon_regs *syscon = (struct syscon_regs *)SYSCON_BASE;
  55. const uint32_t clkset1 = readl(&syscon->clkset1);
  56. const uint8_t fclk_div =
  57. fclk_divisors[(clkset1 >> SYSCON_CLKSET1_FCLK_DIV_SHIFT) & 7];
  58. const ulong fclk_rate = get_PLLCLK(&syscon->clkset1) / fclk_div;
  59. return fclk_rate;
  60. }
  61. /* return HCLK frequency */
  62. ulong get_HCLK(void)
  63. {
  64. const uint8_t hclk_divisors[] = { 1, 2, 4, 5, 6, 8, 16, 32 };
  65. struct syscon_regs *syscon = (struct syscon_regs *)SYSCON_BASE;
  66. const uint32_t clkset1 = readl(&syscon->clkset1);
  67. const uint8_t hclk_div =
  68. hclk_divisors[(clkset1 >> SYSCON_CLKSET1_HCLK_DIV_SHIFT) & 7];
  69. const ulong hclk_rate = get_PLLCLK(&syscon->clkset1) / hclk_div;
  70. return hclk_rate;
  71. }
  72. /* return PCLK frequency */
  73. ulong get_PCLK(void)
  74. {
  75. const uint8_t pclk_divisors[] = { 1, 2, 4, 8 };
  76. struct syscon_regs *syscon = (struct syscon_regs *)SYSCON_BASE;
  77. const uint32_t clkset1 = readl(&syscon->clkset1);
  78. const uint8_t pclk_div =
  79. pclk_divisors[(clkset1 >> SYSCON_CLKSET1_PCLK_DIV_SHIFT) & 3];
  80. const ulong pclk_rate = get_HCLK() / pclk_div;
  81. return pclk_rate;
  82. }
  83. /* return UCLK frequency */
  84. ulong get_UCLK(void)
  85. {
  86. struct syscon_regs *syscon = (struct syscon_regs *)SYSCON_BASE;
  87. ulong uclk_rate;
  88. const uint32_t value = readl(&syscon->pwrcnt);
  89. if (value & SYSCON_PWRCNT_UART_BAUD)
  90. uclk_rate = CONFIG_SYS_CLK_FREQ;
  91. else
  92. uclk_rate = CONFIG_SYS_CLK_FREQ / 2;
  93. return uclk_rate;
  94. }