pm.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /*
  2. * Copyright (C) 2006 Atmel Corporation
  3. *
  4. * See file CREDITS for list of people who contributed to this
  5. * project.
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License as
  9. * published by the Free Software Foundation; either version 2 of
  10. * the License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  20. * MA 02111-1307 USA
  21. */
  22. #include <common.h>
  23. #ifdef CFG_POWER_MANAGER
  24. #include <asm/errno.h>
  25. #include <asm/io.h>
  26. #include <asm/arch/memory-map.h>
  27. #include <asm/arch/platform.h>
  28. #include "sm.h"
  29. /* Sanity checks */
  30. #if (CFG_CLKDIV_CPU > CFG_CLKDIV_HSB) \
  31. || (CFG_CLKDIV_HSB > CFG_CLKDIV_PBA) \
  32. || (CFG_CLKDIV_HSB > CFG_CLKDIV_PBB)
  33. # error Constraint fCPU >= fHSB >= fPB{A,B} violated
  34. #endif
  35. #if defined(CONFIG_PLL) && ((CFG_PLL0_MUL < 1) || (CFG_PLL0_DIV < 1))
  36. # error Invalid PLL multiplier and/or divider
  37. #endif
  38. DECLARE_GLOBAL_DATA_PTR;
  39. struct clock_domain_state {
  40. const struct device *bridge;
  41. unsigned long freq;
  42. u32 mask;
  43. };
  44. static struct clock_domain_state ckd_state[NR_CLOCK_DOMAINS];
  45. int pm_enable_clock(enum clock_domain_id id, unsigned int index)
  46. {
  47. const struct clock_domain *ckd = &chip_clock[id];
  48. struct clock_domain_state *state = &ckd_state[id];
  49. if (ckd->bridge != NO_DEVICE) {
  50. state->bridge = get_device(ckd->bridge);
  51. if (!state->bridge)
  52. return -EBUSY;
  53. }
  54. state->mask |= 1 << index;
  55. if (gd->sm)
  56. writel(state->mask, gd->sm->regs + ckd->reg);
  57. return 0;
  58. }
  59. void pm_disable_clock(enum clock_domain_id id, unsigned int index)
  60. {
  61. const struct clock_domain *ckd = &chip_clock[id];
  62. struct clock_domain_state *state = &ckd_state[id];
  63. state->mask &= ~(1 << index);
  64. if (gd->sm)
  65. writel(state->mask, gd->sm->regs + ckd->reg);
  66. if (ckd->bridge)
  67. put_device(state->bridge);
  68. }
  69. unsigned long pm_get_clock_freq(enum clock_domain_id domain)
  70. {
  71. return ckd_state[domain].freq;
  72. }
  73. void pm_init(void)
  74. {
  75. uint32_t cksel = 0;
  76. unsigned long main_clock;
  77. /* Make sure we don't disable any device we're already using */
  78. get_device(DEVICE_HRAMC);
  79. get_device(DEVICE_HEBI);
  80. /* Enable the PICO as well */
  81. ckd_state[CLOCK_CPU].mask |= 1;
  82. gd->sm = get_device(DEVICE_SM);
  83. if (!gd->sm)
  84. panic("Unable to claim system manager device!\n");
  85. /* Disable any devices that haven't been explicitly claimed */
  86. sm_writel(gd->sm, PM_PBB_MASK, ckd_state[CLOCK_PBB].mask);
  87. sm_writel(gd->sm, PM_PBA_MASK, ckd_state[CLOCK_PBA].mask);
  88. sm_writel(gd->sm, PM_HSB_MASK, ckd_state[CLOCK_HSB].mask);
  89. sm_writel(gd->sm, PM_CPU_MASK, ckd_state[CLOCK_CPU].mask);
  90. #ifdef CONFIG_PLL
  91. /* Initialize the PLL */
  92. main_clock = (CFG_OSC0_HZ / CFG_PLL0_DIV) * CFG_PLL0_MUL;
  93. sm_writel(gd->sm, PM_PLL0, (SM_BF(PLLCOUNT, CFG_PLL0_SUPPRESS_CYCLES)
  94. | SM_BF(PLLMUL, CFG_PLL0_MUL - 1)
  95. | SM_BF(PLLDIV, CFG_PLL0_DIV - 1)
  96. | SM_BF(PLLOPT, CFG_PLL0_OPT)
  97. | SM_BF(PLLOSC, 0)
  98. | SM_BIT(PLLEN)));
  99. /* Wait for lock */
  100. while (!(sm_readl(gd->sm, PM_ISR) & SM_BIT(LOCK0))) ;
  101. #else
  102. main_clock = CFG_OSC0_HZ;
  103. #endif
  104. /* Set up clocks for the CPU and all peripheral buses */
  105. if (CFG_CLKDIV_CPU) {
  106. cksel |= SM_BIT(CPUDIV) | SM_BF(CPUSEL, CFG_CLKDIV_CPU - 1);
  107. ckd_state[CLOCK_CPU].freq = main_clock / (1 << CFG_CLKDIV_CPU);
  108. } else {
  109. ckd_state[CLOCK_CPU].freq = main_clock;
  110. }
  111. if (CFG_CLKDIV_HSB) {
  112. cksel |= SM_BIT(HSBDIV) | SM_BF(HSBSEL, CFG_CLKDIV_HSB - 1);
  113. ckd_state[CLOCK_HSB].freq = main_clock / (1 << CFG_CLKDIV_HSB);
  114. } else {
  115. ckd_state[CLOCK_HSB].freq = main_clock;
  116. }
  117. if (CFG_CLKDIV_PBA) {
  118. cksel |= SM_BIT(PBADIV) | SM_BF(PBASEL, CFG_CLKDIV_PBA - 1);
  119. ckd_state[CLOCK_PBA].freq = main_clock / (1 << CFG_CLKDIV_PBA);
  120. } else {
  121. ckd_state[CLOCK_PBA].freq = main_clock;
  122. }
  123. if (CFG_CLKDIV_PBB) {
  124. cksel |= SM_BIT(PBBDIV) | SM_BF(PBBSEL, CFG_CLKDIV_PBB - 1);
  125. ckd_state[CLOCK_PBB].freq = main_clock / (1 << CFG_CLKDIV_PBB);
  126. } else {
  127. ckd_state[CLOCK_PBB].freq = main_clock;
  128. }
  129. sm_writel(gd->sm, PM_CKSEL, cksel);
  130. /* CFG_HZ currently depends on cpu_hz */
  131. gd->cpu_hz = ckd_state[CLOCK_CPU].freq;
  132. #ifdef CONFIG_PLL
  133. /* Use PLL0 as main clock */
  134. sm_writel(gd->sm, PM_MCCTRL, SM_BIT(PLLSEL));
  135. #endif
  136. }
  137. #endif /* CFG_POWER_MANAGER */