speed.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /*
  2. *
  3. * Copyright (C) 2004-2007, 2012 Freescale Semiconductor, Inc.
  4. * TsiChung Liew (Tsi-Chung.Liew@freescale.com)
  5. *
  6. * See file CREDITS for list of people who contributed to this
  7. * project.
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License as
  11. * published by the Free Software Foundation; either version 2 of
  12. * the License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  22. * MA 02111-1307 USA
  23. */
  24. #include <common.h>
  25. #include <asm/processor.h>
  26. #include <asm/immap.h>
  27. #include <asm/io.h>
  28. DECLARE_GLOBAL_DATA_PTR;
  29. /*
  30. * Low Power Divider specifications
  31. */
  32. #define CLOCK_LPD_MIN (1 << 0) /* Divider (decoded) */
  33. #define CLOCK_LPD_MAX (1 << 15) /* Divider (decoded) */
  34. #define CLOCK_PLL_FVCO_MAX 540000000
  35. #define CLOCK_PLL_FVCO_MIN 300000000
  36. #define CLOCK_PLL_FSYS_MAX 266666666
  37. #define CLOCK_PLL_FSYS_MIN 100000000
  38. #define MHZ 1000000
  39. void clock_enter_limp(int lpdiv)
  40. {
  41. ccm_t *ccm = (ccm_t *)MMAP_CCM;
  42. int i, j;
  43. /* Check bounds of divider */
  44. if (lpdiv < CLOCK_LPD_MIN)
  45. lpdiv = CLOCK_LPD_MIN;
  46. if (lpdiv > CLOCK_LPD_MAX)
  47. lpdiv = CLOCK_LPD_MAX;
  48. /* Round divider down to nearest power of two */
  49. for (i = 0, j = lpdiv; j != 1; j >>= 1, i++) ;
  50. /* Apply the divider to the system clock */
  51. clrsetbits_be16(&ccm->cdr, 0x0f00, CCM_CDR_LPDIV(i));
  52. /* Enable Limp Mode */
  53. setbits_be16(&ccm->misccr, CCM_MISCCR_LIMP);
  54. }
  55. /*
  56. * brief Exit Limp mode
  57. * warning The PLL should be set and locked prior to exiting Limp mode
  58. */
  59. void clock_exit_limp(void)
  60. {
  61. ccm_t *ccm = (ccm_t *)MMAP_CCM;
  62. pll_t *pll = (pll_t *)MMAP_PLL;
  63. /* Exit Limp mode */
  64. clrbits_be16(&ccm->misccr, CCM_MISCCR_LIMP);
  65. /* Wait for the PLL to lock */
  66. while (!(in_be32(&pll->psr) & PLL_PSR_LOCK))
  67. ;
  68. }
  69. /*
  70. * get_clocks() fills in gd->cpu_clock and gd->bus_clk
  71. */
  72. int get_clocks(void)
  73. {
  74. ccm_t *ccm = (ccm_t *)MMAP_CCM;
  75. pll_t *pll = (pll_t *)MMAP_PLL;
  76. int pllmult_nopci[] = { 20, 10, 24, 18, 12, 6, 16, 8 };
  77. int pllmult_pci[] = { 12, 6, 16, 8 };
  78. int vco = 0, temp, fbtemp, pcrvalue;
  79. int *pPllmult = NULL;
  80. u16 fbpll_mask;
  81. #ifdef CONFIG_PCI
  82. int bPci;
  83. #endif
  84. #ifdef CONFIG_M54455EVB
  85. u8 *cpld = (u8 *)(CONFIG_SYS_CS2_BASE + 3);
  86. #endif
  87. u8 bootmode;
  88. /* To determine PCI is present or not */
  89. if (((in_be16(&ccm->ccr) & CCM_CCR_360_FBCONFIG_MASK) == 0x00e0) ||
  90. ((in_be16(&ccm->ccr) & CCM_CCR_360_FBCONFIG_MASK) == 0x0060)) {
  91. pPllmult = &pllmult_pci[0];
  92. fbpll_mask = 3; /* 11b */
  93. #ifdef CONFIG_PCI
  94. bPci = 1;
  95. #endif
  96. } else {
  97. pPllmult = &pllmult_nopci[0];
  98. fbpll_mask = 7; /* 111b */
  99. #ifdef CONFIG_PCI
  100. gd->pci_clk = 0;
  101. bPci = 0;
  102. #endif
  103. }
  104. #ifdef CONFIG_M54455EVB
  105. bootmode = (in_8(cpld) & 0x03);
  106. if (bootmode != 3) {
  107. /* Temporary read from CCR- fixed fb issue, must be the same clock
  108. as pci or input clock, causing cpld/fpga read inconsistancy */
  109. fbtemp = pPllmult[ccm->ccr & fbpll_mask];
  110. /* Break down into small pieces, code still in flex bus */
  111. pcrvalue = in_be32(&pll->pcr) & 0xFFFFF0FF;
  112. temp = fbtemp - 1;
  113. pcrvalue |= PLL_PCR_OUTDIV3(temp);
  114. out_be32(&pll->pcr, pcrvalue);
  115. }
  116. #endif
  117. #ifdef CONFIG_M54451EVB
  118. /* No external logic to read the bootmode, hard coded from built */
  119. #ifdef CONFIG_CF_SBF
  120. bootmode = 3;
  121. #else
  122. bootmode = 2;
  123. /* default value is 16 mul, set to 20 mul */
  124. pcrvalue = (in_be32(&pll->pcr) & 0x00FFFFFF) | 0x14000000;
  125. out_be32(&pll->pcr, pcrvalue);
  126. while ((in_be32(&pll->psr) & PLL_PSR_LOCK) != PLL_PSR_LOCK)
  127. ;
  128. #endif
  129. #endif
  130. if (bootmode == 0) {
  131. /* RCON mode */
  132. vco = pPllmult[ccm->rcon & fbpll_mask] * CONFIG_SYS_INPUT_CLKSRC;
  133. if ((vco < CLOCK_PLL_FVCO_MIN) || (vco > CLOCK_PLL_FVCO_MAX)) {
  134. /* invaild range, re-set in PCR */
  135. int temp = ((in_be32(&pll->pcr) & PLL_PCR_OUTDIV2_MASK) >> 4) + 1;
  136. int i, j, bus;
  137. j = (in_be32(&pll->pcr) & 0xFF000000) >> 24;
  138. for (i = j; i < 0xFF; i++) {
  139. vco = i * CONFIG_SYS_INPUT_CLKSRC;
  140. if (vco >= CLOCK_PLL_FVCO_MIN) {
  141. bus = vco / temp;
  142. if (bus <= CLOCK_PLL_FSYS_MIN - MHZ)
  143. continue;
  144. else
  145. break;
  146. }
  147. }
  148. pcrvalue = in_be32(&pll->pcr) & 0x00FF00FF;
  149. fbtemp = ((i - 1) << 8) | ((i - 1) << 12);
  150. pcrvalue |= ((i << 24) | fbtemp);
  151. out_be32(&pll->pcr, pcrvalue);
  152. }
  153. gd->vco_clk = vco; /* Vco clock */
  154. } else if (bootmode == 2) {
  155. /* Normal mode */
  156. vco = ((in_be32(&pll->pcr) & 0xFF000000) >> 24) * CONFIG_SYS_INPUT_CLKSRC;
  157. if ((vco < CLOCK_PLL_FVCO_MIN) || (vco > CLOCK_PLL_FVCO_MAX)) {
  158. /* Default value */
  159. pcrvalue = (in_be32(&pll->pcr) & 0x00FFFFFF);
  160. pcrvalue |= pPllmult[in_be16(&ccm->ccr) & fbpll_mask] << 24;
  161. out_be32(&pll->pcr, pcrvalue);
  162. vco = ((in_be32(&pll->pcr) & 0xFF000000) >> 24) * CONFIG_SYS_INPUT_CLKSRC;
  163. }
  164. gd->vco_clk = vco; /* Vco clock */
  165. } else if (bootmode == 3) {
  166. /* serial mode */
  167. vco = ((in_be32(&pll->pcr) & 0xFF000000) >> 24) * CONFIG_SYS_INPUT_CLKSRC;
  168. gd->vco_clk = vco; /* Vco clock */
  169. }
  170. if ((in_be16(&ccm->ccr) & CCM_MISCCR_LIMP) == CCM_MISCCR_LIMP) {
  171. /* Limp mode */
  172. } else {
  173. gd->inp_clk = CONFIG_SYS_INPUT_CLKSRC; /* Input clock */
  174. temp = (in_be32(&pll->pcr) & PLL_PCR_OUTDIV1_MASK) + 1;
  175. gd->cpu_clk = vco / temp; /* cpu clock */
  176. temp = ((in_be32(&pll->pcr) & PLL_PCR_OUTDIV2_MASK) >> 4) + 1;
  177. gd->bus_clk = vco / temp; /* bus clock */
  178. temp = ((in_be32(&pll->pcr) & PLL_PCR_OUTDIV3_MASK) >> 8) + 1;
  179. gd->flb_clk = vco / temp; /* FlexBus clock */
  180. #ifdef CONFIG_PCI
  181. if (bPci) {
  182. temp = ((in_be32(&pll->pcr) & PLL_PCR_OUTDIV4_MASK) >> 12) + 1;
  183. gd->pci_clk = vco / temp; /* PCI clock */
  184. }
  185. #endif
  186. }
  187. #ifdef CONFIG_FSL_I2C
  188. gd->i2c1_clk = gd->bus_clk;
  189. #endif
  190. return (0);
  191. }