speed.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. /*
  2. *
  3. * (C) Copyright 2000-2003
  4. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  5. *
  6. * Copyright (C) 2004-2008 Freescale Semiconductor, Inc.
  7. * TsiChung Liew (Tsi-Chung.Liew@freescale.com)
  8. *
  9. * See file CREDITS for list of people who contributed to this
  10. * project.
  11. *
  12. * This program is free software; you can redistribute it and/or
  13. * modify it under the terms of the GNU General Public License as
  14. * published by the Free Software Foundation; either version 2 of
  15. * the License, or (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program; if not, write to the Free Software
  24. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  25. * MA 02111-1307 USA
  26. */
  27. #include <common.h>
  28. #include <asm/processor.h>
  29. #include <asm/immap.h>
  30. DECLARE_GLOBAL_DATA_PTR;
  31. /* PLL min/max specifications */
  32. #define MAX_FVCO 500000 /* KHz */
  33. #define MAX_FSYS 80000 /* KHz */
  34. #define MIN_FSYS 58333 /* KHz */
  35. #ifdef CONFIG_MCF5301x
  36. #define FREF 20000 /* KHz */
  37. #define MAX_MFD 63 /* Multiplier */
  38. #define MIN_MFD 0 /* Multiplier */
  39. #define USBDIV 8
  40. /* Low Power Divider specifications */
  41. #define MIN_LPD (0) /* Divider (not encoded) */
  42. #define MAX_LPD (15) /* Divider (not encoded) */
  43. #define DEFAULT_LPD (0) /* Divider (not encoded) */
  44. #endif
  45. #ifdef CONFIG_MCF532x
  46. #define FREF 16000 /* KHz */
  47. #define MAX_MFD 135 /* Multiplier */
  48. #define MIN_MFD 88 /* Multiplier */
  49. /* Low Power Divider specifications */
  50. #define MIN_LPD (1 << 0) /* Divider (not encoded) */
  51. #define MAX_LPD (1 << 15) /* Divider (not encoded) */
  52. #define DEFAULT_LPD (1 << 1) /* Divider (not encoded) */
  53. #endif
  54. #define BUSDIV 6 /* Divider */
  55. /* Get the value of the current system clock */
  56. int get_sys_clock(void)
  57. {
  58. volatile ccm_t *ccm = (volatile ccm_t *)(MMAP_CCM);
  59. volatile pll_t *pll = (volatile pll_t *)(MMAP_PLL);
  60. int divider;
  61. /* Test to see if device is in LIMP mode */
  62. if (ccm->misccr & CCM_MISCCR_LIMP) {
  63. divider = ccm->cdr & CCM_CDR_LPDIV(0xF);
  64. #ifdef CONFIG_MCF5301x
  65. return (FREF / (3 * (1 << divider)));
  66. #endif
  67. #ifdef CONFIG_MCF532x
  68. return (FREF / (2 << divider));
  69. #endif
  70. } else {
  71. #ifdef CONFIG_MCF5301x
  72. u32 pfdr = (pll->pcr & 0x3F) + 1;
  73. u32 refdiv = (1 << ((pll->pcr & PLL_PCR_REFDIV(7)) >> 8));
  74. u32 busdiv = ((pll->pdr & 0x00F0) >> 4) + 1;
  75. return (((FREF * pfdr) / refdiv) / busdiv);
  76. #endif
  77. #ifdef CONFIG_MCF532x
  78. return ((FREF * pll->pfdr) / (BUSDIV * 4));
  79. #endif
  80. }
  81. }
  82. /*
  83. * Initialize the Low Power Divider circuit
  84. *
  85. * Parameters:
  86. * div Desired system frequency divider
  87. *
  88. * Return Value:
  89. * The resulting output system frequency
  90. */
  91. int clock_limp(int div)
  92. {
  93. volatile ccm_t *ccm = (volatile ccm_t *)(MMAP_CCM);
  94. u32 temp;
  95. /* Check bounds of divider */
  96. if (div < MIN_LPD)
  97. div = MIN_LPD;
  98. if (div > MAX_LPD)
  99. div = MAX_LPD;
  100. /* Save of the current value of the SSIDIV so we don't overwrite the value */
  101. temp = (ccm->cdr & CCM_CDR_SSIDIV(0xFF));
  102. /* Apply the divider to the system clock */
  103. ccm->cdr = (CCM_CDR_LPDIV(div) | CCM_CDR_SSIDIV(temp));
  104. ccm->misccr |= CCM_MISCCR_LIMP;
  105. return (FREF / (3 * (1 << div)));
  106. }
  107. /* Exit low power LIMP mode */
  108. int clock_exit_limp(void)
  109. {
  110. volatile ccm_t *ccm = (volatile ccm_t *)(MMAP_CCM);
  111. int fout;
  112. /* Exit LIMP mode */
  113. ccm->misccr &= (~CCM_MISCCR_LIMP);
  114. /* Wait for PLL to lock */
  115. while (!(ccm->misccr & CCM_MISCCR_PLL_LOCK)) ;
  116. fout = get_sys_clock();
  117. return fout;
  118. }
  119. /* Initialize the PLL
  120. *
  121. * Parameters:
  122. * fref PLL reference clock frequency in KHz
  123. * fsys Desired PLL output frequency in KHz
  124. * flags Operating parameters
  125. *
  126. * Return Value:
  127. * The resulting output system frequency
  128. */
  129. int clock_pll(int fsys, int flags)
  130. {
  131. #ifdef CONFIG_MCF532x
  132. volatile u32 *sdram_workaround = (volatile u32 *)(MMAP_SDRAM + 0x80);
  133. #endif
  134. volatile sdram_t *sdram = (volatile sdram_t *)(MMAP_SDRAM);
  135. volatile pll_t *pll = (volatile pll_t *)(MMAP_PLL);
  136. int fref, temp, fout, mfd;
  137. u32 i;
  138. fref = FREF;
  139. if (fsys == 0) {
  140. /* Return current PLL output */
  141. #ifdef CONFIG_MCF5301x
  142. u32 busdiv = ((pll->pdr >> 4) & 0x0F) + 1;
  143. mfd = (pll->pcr & 0x3F) + 1;
  144. return (fref * mfd) / busdiv;
  145. #endif
  146. #ifdef CONFIG_MCF532x
  147. mfd = pll->pfdr;
  148. return (fref * mfd / (BUSDIV * 4));
  149. #endif
  150. }
  151. /* Check bounds of requested system clock */
  152. if (fsys > MAX_FSYS)
  153. fsys = MAX_FSYS;
  154. if (fsys < MIN_FSYS)
  155. fsys = MIN_FSYS;
  156. /*
  157. * Multiplying by 100 when calculating the temp value,
  158. * and then dividing by 100 to calculate the mfd allows
  159. * for exact values without needing to include floating
  160. * point libraries.
  161. */
  162. temp = (100 * fsys) / fref;
  163. #ifdef CONFIG_MCF5301x
  164. mfd = (BUSDIV * temp) / 100;
  165. /* Determine the output frequency for selected values */
  166. fout = ((fref * mfd) / BUSDIV);
  167. #endif
  168. #ifdef CONFIG_MCF532x
  169. mfd = (4 * BUSDIV * temp) / 100;
  170. /* Determine the output frequency for selected values */
  171. fout = ((fref * mfd) / (BUSDIV * 4));
  172. #endif
  173. /*
  174. * Check to see if the SDRAM has already been initialized.
  175. * If it has then the SDRAM needs to be put into self refresh
  176. * mode before reprogramming the PLL.
  177. */
  178. if (sdram->ctrl & SDRAMC_SDCR_REF)
  179. sdram->ctrl &= ~SDRAMC_SDCR_CKE;
  180. /*
  181. * Initialize the PLL to generate the new system clock frequency.
  182. * The device must be put into LIMP mode to reprogram the PLL.
  183. */
  184. /* Enter LIMP mode */
  185. clock_limp(DEFAULT_LPD);
  186. #ifdef CONFIG_MCF5301x
  187. pll->pdr =
  188. PLL_PDR_OUTDIV1((BUSDIV / 3) - 1) |
  189. PLL_PDR_OUTDIV2(BUSDIV - 1) |
  190. PLL_PDR_OUTDIV3((BUSDIV / 2) - 1) |
  191. PLL_PDR_OUTDIV4(USBDIV - 1);
  192. pll->pcr &= PLL_PCR_FBDIV_MASK;
  193. pll->pcr |= PLL_PCR_FBDIV(mfd - 1);
  194. #endif
  195. #ifdef CONFIG_MCF532x
  196. /* Reprogram PLL for desired fsys */
  197. pll->podr = (PLL_PODR_CPUDIV(BUSDIV / 3) | PLL_PODR_BUSDIV(BUSDIV));
  198. pll->pfdr = mfd;
  199. #endif
  200. /* Exit LIMP mode */
  201. clock_exit_limp();
  202. /* Return the SDRAM to normal operation if it is in use. */
  203. if (sdram->ctrl & SDRAMC_SDCR_REF)
  204. sdram->ctrl |= SDRAMC_SDCR_CKE;
  205. #ifdef CONFIG_MCF532x
  206. /*
  207. * software workaround for SDRAM opeartion after exiting LIMP
  208. * mode errata
  209. */
  210. *sdram_workaround = CONFIG_SYS_SDRAM_BASE;
  211. #endif
  212. /* wait for DQS logic to relock */
  213. for (i = 0; i < 0x200; i++) ;
  214. return fout;
  215. }
  216. /* get_clocks() fills in gd->cpu_clock and gd->bus_clk */
  217. int get_clocks(void)
  218. {
  219. gd->bus_clk = clock_pll(CONFIG_SYS_CLK / 1000, 0) * 1000;
  220. gd->cpu_clk = (gd->bus_clk * 3);
  221. #ifdef CONFIG_FSL_I2C
  222. gd->i2c1_clk = gd->bus_clk;
  223. #endif
  224. return (0);
  225. }