speed.c 6.1 KB

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