speed.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. /*
  2. *
  3. * (C) Copyright 2000-2003
  4. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  5. *
  6. * Copyright (C) 2004-2008, 2012 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. #include <asm/io.h>
  31. DECLARE_GLOBAL_DATA_PTR;
  32. /* PLL min/max specifications */
  33. #define MAX_FVCO 500000 /* KHz */
  34. #define MAX_FSYS 80000 /* KHz */
  35. #define MIN_FSYS 58333 /* KHz */
  36. #ifdef CONFIG_MCF5301x
  37. #define FREF 20000 /* KHz */
  38. #define MAX_MFD 63 /* Multiplier */
  39. #define MIN_MFD 0 /* Multiplier */
  40. #define USBDIV 8
  41. /* Low Power Divider specifications */
  42. #define MIN_LPD (0) /* Divider (not encoded) */
  43. #define MAX_LPD (15) /* Divider (not encoded) */
  44. #define DEFAULT_LPD (0) /* Divider (not encoded) */
  45. #endif
  46. #ifdef CONFIG_MCF532x
  47. #define FREF 16000 /* KHz */
  48. #define MAX_MFD 135 /* Multiplier */
  49. #define MIN_MFD 88 /* Multiplier */
  50. /* Low Power Divider specifications */
  51. #define MIN_LPD (1 << 0) /* Divider (not encoded) */
  52. #define MAX_LPD (1 << 15) /* Divider (not encoded) */
  53. #define DEFAULT_LPD (1 << 1) /* Divider (not encoded) */
  54. #endif
  55. #define BUSDIV 6 /* Divider */
  56. /* Get the value of the current system clock */
  57. int get_sys_clock(void)
  58. {
  59. ccm_t *ccm = (ccm_t *)(MMAP_CCM);
  60. pll_t *pll = (pll_t *)(MMAP_PLL);
  61. int divider;
  62. /* Test to see if device is in LIMP mode */
  63. if (in_be16(&ccm->misccr) & CCM_MISCCR_LIMP) {
  64. divider = in_be16(&ccm->cdr) & CCM_CDR_LPDIV(0xF);
  65. #ifdef CONFIG_MCF5301x
  66. return (FREF / (3 * (1 << divider)));
  67. #endif
  68. #ifdef CONFIG_MCF532x
  69. return (FREF / (2 << divider));
  70. #endif
  71. } else {
  72. #ifdef CONFIG_MCF5301x
  73. u32 pfdr = (in_be32(&pll->pcr) & 0x3F) + 1;
  74. u32 refdiv = (1 << ((in_be32(&pll->pcr) & PLL_PCR_REFDIV(7)) >> 8));
  75. u32 busdiv = ((in_be32(&pll->pdr) & 0x00F0) >> 4) + 1;
  76. return (((FREF * pfdr) / refdiv) / busdiv);
  77. #endif
  78. #ifdef CONFIG_MCF532x
  79. return (FREF * in_8(&pll->pfdr)) / (BUSDIV * 4);
  80. #endif
  81. }
  82. }
  83. /*
  84. * Initialize the Low Power Divider circuit
  85. *
  86. * Parameters:
  87. * div Desired system frequency divider
  88. *
  89. * Return Value:
  90. * The resulting output system frequency
  91. */
  92. int clock_limp(int div)
  93. {
  94. ccm_t *ccm = (ccm_t *)(MMAP_CCM);
  95. u32 temp;
  96. /* Check bounds of divider */
  97. if (div < MIN_LPD)
  98. div = MIN_LPD;
  99. if (div > MAX_LPD)
  100. div = MAX_LPD;
  101. /* Save of the current value of the SSIDIV so we don't overwrite the value */
  102. temp = (in_be16(&ccm->cdr) & CCM_CDR_SSIDIV(0xFF));
  103. /* Apply the divider to the system clock */
  104. out_be16(&ccm->cdr, CCM_CDR_LPDIV(div) | CCM_CDR_SSIDIV(temp));
  105. setbits_be16(&ccm->misccr, CCM_MISCCR_LIMP);
  106. return (FREF / (3 * (1 << div)));
  107. }
  108. /* Exit low power LIMP mode */
  109. int clock_exit_limp(void)
  110. {
  111. ccm_t *ccm = (ccm_t *)(MMAP_CCM);
  112. int fout;
  113. /* Exit LIMP mode */
  114. clrbits_be16(&ccm->misccr, CCM_MISCCR_LIMP);
  115. /* Wait for PLL to lock */
  116. while (!(in_be16(&ccm->misccr) & CCM_MISCCR_PLL_LOCK))
  117. ;
  118. fout = get_sys_clock();
  119. return fout;
  120. }
  121. /* Initialize the PLL
  122. *
  123. * Parameters:
  124. * fref PLL reference clock frequency in KHz
  125. * fsys Desired PLL output frequency in KHz
  126. * flags Operating parameters
  127. *
  128. * Return Value:
  129. * The resulting output system frequency
  130. */
  131. int clock_pll(int fsys, int flags)
  132. {
  133. #ifdef CONFIG_MCF532x
  134. u32 *sdram_workaround = (u32 *)(MMAP_SDRAM + 0x80);
  135. #endif
  136. sdram_t *sdram = (sdram_t *)(MMAP_SDRAM);
  137. pll_t *pll = (pll_t *)(MMAP_PLL);
  138. int fref, temp, fout, mfd;
  139. u32 i;
  140. fref = FREF;
  141. if (fsys == 0) {
  142. /* Return current PLL output */
  143. #ifdef CONFIG_MCF5301x
  144. u32 busdiv = ((in_be32(&pll->pdr) >> 4) & 0x0F) + 1;
  145. mfd = (in_be32(&pll->pcr) & 0x3F) + 1;
  146. return (fref * mfd) / busdiv;
  147. #endif
  148. #ifdef CONFIG_MCF532x
  149. mfd = in_8(&pll->pfdr);
  150. return (fref * mfd / (BUSDIV * 4));
  151. #endif
  152. }
  153. /* Check bounds of requested system clock */
  154. if (fsys > MAX_FSYS)
  155. fsys = MAX_FSYS;
  156. if (fsys < MIN_FSYS)
  157. fsys = MIN_FSYS;
  158. /*
  159. * Multiplying by 100 when calculating the temp value,
  160. * and then dividing by 100 to calculate the mfd allows
  161. * for exact values without needing to include floating
  162. * point libraries.
  163. */
  164. temp = (100 * fsys) / fref;
  165. #ifdef CONFIG_MCF5301x
  166. mfd = (BUSDIV * temp) / 100;
  167. /* Determine the output frequency for selected values */
  168. fout = ((fref * mfd) / BUSDIV);
  169. #endif
  170. #ifdef CONFIG_MCF532x
  171. mfd = (4 * BUSDIV * temp) / 100;
  172. /* Determine the output frequency for selected values */
  173. fout = ((fref * mfd) / (BUSDIV * 4));
  174. #endif
  175. /* must not tamper with SDRAMC if running from SDRAM */
  176. #if !defined(CONFIG_MONITOR_IS_IN_RAM)
  177. /*
  178. * Check to see if the SDRAM has already been initialized.
  179. * If it has then the SDRAM needs to be put into self refresh
  180. * mode before reprogramming the PLL.
  181. */
  182. if (in_be32(&sdram->ctrl) & SDRAMC_SDCR_REF)
  183. clrbits_be32(&sdram->ctrl, SDRAMC_SDCR_CKE);
  184. /*
  185. * Initialize the PLL to generate the new system clock frequency.
  186. * The device must be put into LIMP mode to reprogram the PLL.
  187. */
  188. /* Enter LIMP mode */
  189. clock_limp(DEFAULT_LPD);
  190. #ifdef CONFIG_MCF5301x
  191. out_be32(&pll->pdr,
  192. PLL_PDR_OUTDIV1((BUSDIV / 3) - 1) |
  193. PLL_PDR_OUTDIV2(BUSDIV - 1) |
  194. PLL_PDR_OUTDIV3((BUSDIV / 2) - 1) |
  195. PLL_PDR_OUTDIV4(USBDIV - 1));
  196. clrbits_be32(&pll->pcr, ~PLL_PCR_FBDIV_UNMASK);
  197. setbits_be32(&pll->pcr, PLL_PCR_FBDIV(mfd - 1));
  198. #endif
  199. #ifdef CONFIG_MCF532x
  200. /* Reprogram PLL for desired fsys */
  201. out_8(&pll->podr,
  202. PLL_PODR_CPUDIV(BUSDIV / 3) | PLL_PODR_BUSDIV(BUSDIV));
  203. out_8(&pll->pfdr, mfd);
  204. #endif
  205. /* Exit LIMP mode */
  206. clock_exit_limp();
  207. /* Return the SDRAM to normal operation if it is in use. */
  208. if (in_be32(&sdram->ctrl) & SDRAMC_SDCR_REF)
  209. setbits_be32(&sdram->ctrl, SDRAMC_SDCR_CKE);
  210. #ifdef CONFIG_MCF532x
  211. /*
  212. * software workaround for SDRAM opeartion after exiting LIMP
  213. * mode errata
  214. */
  215. out_be32(sdram_workaround, CONFIG_SYS_SDRAM_BASE);
  216. #endif
  217. /* wait for DQS logic to relock */
  218. for (i = 0; i < 0x200; i++) ;
  219. #endif /* !defined(CONFIG_MONITOR_IS_IN_RAM) */
  220. return fout;
  221. }
  222. /* get_clocks() fills in gd->cpu_clock and gd->bus_clk */
  223. int get_clocks(void)
  224. {
  225. gd->bus_clk = clock_pll(CONFIG_SYS_CLK / 1000, 0) * 1000;
  226. gd->cpu_clk = (gd->bus_clk * 3);
  227. #ifdef CONFIG_FSL_I2C
  228. gd->i2c1_clk = gd->bus_clk;
  229. #endif
  230. return (0);
  231. }