speed.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. /*
  2. * (C) Copyright 2000-2004
  3. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  4. *
  5. * See file CREDITS for list of people who contributed to this
  6. * project.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation; either version 2 of
  11. * the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  21. * MA 02111-1307 USA
  22. */
  23. #include <common.h>
  24. #include <mpc8xx.h>
  25. #include <asm/processor.h>
  26. #if !defined(CONFIG_TQM866M) || defined(CFG_MEASURE_CPUCLK)
  27. #define PITC_SHIFT 16
  28. #define PITR_SHIFT 16
  29. /* pitc values to time for 58/8192 seconds (about 70.8 milliseconds) */
  30. #define SPEED_PIT_COUNTS 58
  31. #define SPEED_PITC ((SPEED_PIT_COUNTS - 1) << PITC_SHIFT)
  32. #define SPEED_PITC_INIT ((SPEED_PIT_COUNTS + 1) << PITC_SHIFT)
  33. /* Access functions for the Machine State Register */
  34. static __inline__ unsigned long get_msr(void)
  35. {
  36. unsigned long msr;
  37. asm volatile("mfmsr %0" : "=r" (msr) :);
  38. return msr;
  39. }
  40. static __inline__ void set_msr(unsigned long msr)
  41. {
  42. asm volatile("mtmsr %0" : : "r" (msr));
  43. }
  44. /* ------------------------------------------------------------------------- */
  45. /*
  46. * Measure CPU clock speed (core clock GCLK1, GCLK2),
  47. * also determine bus clock speed (checking bus divider factor)
  48. *
  49. * (Approx. GCLK frequency in Hz)
  50. *
  51. * Initializes timer 2 and PIT, but disables them before return.
  52. * [Use timer 2, because MPC823 CPUs mask 0.x do not have timers 3 and 4]
  53. *
  54. * When measuring the CPU clock against the PIT, we count cpu clocks
  55. * for 58/8192 seconds with a prescale divide by 177 for the cpu clock.
  56. * These strange values for the timing interval and prescaling are used
  57. * because the formula for the CPU clock is:
  58. *
  59. * CPU clock = count * (177 * (8192 / 58))
  60. *
  61. * = count * 24999.7241
  62. *
  63. * which is very close to
  64. *
  65. * = count * 25000
  66. *
  67. * Since the count gives the CPU clock divided by 25000, we can get
  68. * the CPU clock rounded to the nearest 0.1 MHz by
  69. *
  70. * CPU clock = ((count + 2) / 4) * 100000;
  71. *
  72. * The rounding is important since the measurement is sometimes going
  73. * to be high or low by 0.025 MHz, depending on exactly how the clocks
  74. * and counters interact. By rounding we get the exact answer for any
  75. * CPU clock that is an even multiple of 0.1 MHz.
  76. */
  77. unsigned long measure_gclk(void)
  78. {
  79. volatile immap_t *immr = (immap_t *) CFG_IMMR;
  80. volatile cpmtimer8xx_t *timerp = &immr->im_cpmtimer;
  81. ulong timer2_val;
  82. ulong msr_val;
  83. #ifdef CONFIG_MPC866_et_al
  84. /* dont use OSCM, only use EXTCLK/512 */
  85. immr->im_clkrst.car_sccr |= SCCR_RTSEL | SCCR_RTDIV;
  86. #else
  87. immr->im_clkrst.car_sccr &= ~(SCCR_RTSEL | SCCR_RTDIV);
  88. #endif
  89. /* Reset + Stop Timer 2, no cascading
  90. */
  91. timerp->cpmt_tgcr &= ~(TGCR_CAS2 | TGCR_RST2);
  92. /* Keep stopped, halt in debug mode
  93. */
  94. timerp->cpmt_tgcr |= (TGCR_FRZ2 | TGCR_STP2);
  95. /* Timer 2 setup:
  96. * Output ref. interrupt disable, int. clock
  97. * Prescale by 177. Note that prescaler divides by value + 1
  98. * so we must subtract 1 here.
  99. */
  100. timerp->cpmt_tmr2 = ((177 - 1) << TMR_PS_SHIFT) | TMR_ICLK_IN_GEN;
  101. timerp->cpmt_tcn2 = 0; /* reset state */
  102. timerp->cpmt_tgcr |= TGCR_RST2; /* enable timer 2 */
  103. /*
  104. * PIT setup:
  105. *
  106. * We want to time for SPEED_PITC_COUNTS counts (of 8192 Hz),
  107. * so the count value would be SPEED_PITC_COUNTS - 1.
  108. * But there would be an uncertainty in the start time of 1/4
  109. * count since when we enable the PIT the count is not
  110. * synchronized to the 32768 Hz oscillator. The trick here is
  111. * to start the count higher and wait until the PIT count
  112. * changes to the required value before starting timer 2.
  113. *
  114. * One count high should be enough, but occasionally the start
  115. * is off by 1 or 2 counts of 32768 Hz. With the start value
  116. * set two counts high it seems very reliable.
  117. */
  118. immr->im_sitk.sitk_pitck = KAPWR_KEY; /* PIT initialization */
  119. immr->im_sit.sit_pitc = SPEED_PITC_INIT;
  120. immr->im_sitk.sitk_piscrk = KAPWR_KEY;
  121. immr->im_sit.sit_piscr = CFG_PISCR;
  122. /*
  123. * Start measurement - disable interrupts, just in case
  124. */
  125. msr_val = get_msr ();
  126. set_msr (msr_val & ~MSR_EE);
  127. immr->im_sit.sit_piscr |= PISCR_PTE;
  128. /* spin until get exact count when we want to start */
  129. while (immr->im_sit.sit_pitr > SPEED_PITC);
  130. timerp->cpmt_tgcr &= ~TGCR_STP2; /* Start Timer 2 */
  131. while ((immr->im_sit.sit_piscr & PISCR_PS) == 0);
  132. timerp->cpmt_tgcr |= TGCR_STP2; /* Stop Timer 2 */
  133. /* re-enable external interrupts if they were on */
  134. set_msr (msr_val);
  135. /* Disable timer and PIT
  136. */
  137. timer2_val = timerp->cpmt_tcn2; /* save before reset timer */
  138. timerp->cpmt_tgcr &= ~(TGCR_RST2 | TGCR_FRZ2 | TGCR_STP2);
  139. immr->im_sit.sit_piscr &= ~PISCR_PTE;
  140. #if defined(CONFIG_MPC866_et_al)
  141. /* not using OSCM, using XIN, so scale appropriately */
  142. return (((timer2_val + 2) / 4) * (CFG_8XX_XIN/512))/8192 * 100000L;
  143. #else
  144. return ((timer2_val + 2) / 4) * 100000L; /* convert to Hz */
  145. #endif
  146. }
  147. #endif
  148. #if !defined(CONFIG_TQM866M)
  149. /*
  150. * get_clocks() fills in gd->cpu_clock depending on CONFIG_8xx_GCLK_FREQ
  151. * or (if it is not defined) measure_gclk() (which uses the ref clock)
  152. * from above.
  153. */
  154. int get_clocks (void)
  155. {
  156. DECLARE_GLOBAL_DATA_PTR;
  157. volatile immap_t *immr = (immap_t *) CFG_IMMR;
  158. #ifndef CONFIG_8xx_GCLK_FREQ
  159. gd->cpu_clk = measure_gclk();
  160. #else /* CONFIG_8xx_GCLK_FREQ */
  161. /*
  162. * If for some reason measuring the gclk frequency won't
  163. * work, we return the hardwired value.
  164. * (For example, the cogent CMA286-60 CPU module has no
  165. * separate oscillator for PITRTCLK)
  166. */
  167. gd->cpu_clk = CONFIG_8xx_GCLK_FREQ;
  168. #endif /* CONFIG_8xx_GCLK_FREQ */
  169. if ((immr->im_clkrst.car_sccr & SCCR_EBDF11) == 0) {
  170. /* No Bus Divider active */
  171. gd->bus_clk = gd->cpu_clk;
  172. } else {
  173. /* The MPC8xx has only one BDF: half clock speed */
  174. gd->bus_clk = gd->cpu_clk / 2;
  175. }
  176. return (0);
  177. }
  178. #else /* CONFIG_MPC866_et_al */
  179. static long init_pll_866 (long clk);
  180. /* This function sets up PLL (init_pll_866() is called) and
  181. * fills gd->cpu_clk and gd->bus_clk according to the environment
  182. * variable 'cpuclk' or to CFG_866_CPUCLK_DEFAULT (if 'cpuclk'
  183. * contains invalid value).
  184. * This functions requires an MPC866 series CPU.
  185. */
  186. int get_clocks_866 (void)
  187. {
  188. DECLARE_GLOBAL_DATA_PTR;
  189. volatile immap_t *immr = (immap_t *) CFG_IMMR;
  190. char tmp[64];
  191. long cpuclk = 0;
  192. long sccr_reg;
  193. if (getenv_r ("cpuclk", tmp, sizeof (tmp)) > 0)
  194. cpuclk = simple_strtoul (tmp, NULL, 10) * 1000000;
  195. if ((CFG_866_CPUCLK_MIN > cpuclk) || (CFG_866_CPUCLK_MAX < cpuclk))
  196. cpuclk = CFG_866_CPUCLK_DEFAULT;
  197. gd->cpu_clk = init_pll_866 (cpuclk);
  198. #if defined(CFG_MEASURE_CPUCLK)
  199. gd->cpu_clk = measure_gclk ();
  200. #endif
  201. /* if cpu clock <= 66 MHz then set bus division factor to 1,
  202. * otherwise set it to 2
  203. */
  204. sccr_reg = immr->im_clkrst.car_sccr;
  205. sccr_reg &= ~SCCR_EBDF11;
  206. if (gd->cpu_clk <= 66000000) {
  207. sccr_reg |= SCCR_EBDF00; /* bus division factor = 1 */
  208. gd->bus_clk = gd->cpu_clk;
  209. } else {
  210. sccr_reg |= SCCR_EBDF01; /* bus division factor = 2 */
  211. gd->bus_clk = gd->cpu_clk / 2;
  212. }
  213. immr->im_clkrst.car_sccr = sccr_reg;
  214. return (0);
  215. }
  216. /* Adjust sdram refresh rate to actual CPU clock.
  217. */
  218. int sdram_adjust_866 (void)
  219. {
  220. DECLARE_GLOBAL_DATA_PTR;
  221. volatile immap_t *immr = (immap_t *) CFG_IMMR;
  222. long mamr;
  223. mamr = immr->im_memctl.memc_mamr;
  224. mamr &= ~MAMR_PTA_MSK;
  225. mamr |= ((gd->cpu_clk / CFG_866_PTA_PER_CLK) << MAMR_PTA_SHIFT);
  226. immr->im_memctl.memc_mamr = mamr;
  227. return (0);
  228. }
  229. /* Configure PLL for MPC866/859 CPU series
  230. * PLL multiplication factor is set to the value nearest to the desired clk,
  231. * assuming a oscclk of 10 MHz.
  232. */
  233. static long init_pll_866 (long clk)
  234. {
  235. extern void plprcr_write_866 (long);
  236. volatile immap_t *immr = (immap_t *) CFG_IMMR;
  237. long n, plprcr;
  238. char mfi, mfn, mfd, s, pdf;
  239. long step_mfi, step_mfn;
  240. if (clk < 20000000) {
  241. clk *= 2;
  242. pdf = 1;
  243. } else {
  244. pdf = 0;
  245. }
  246. if (clk < 40000000) {
  247. s = 2;
  248. step_mfi = CFG_866_OSCCLK / 4;
  249. mfd = 7;
  250. step_mfn = CFG_866_OSCCLK / 30;
  251. } else if (clk < 80000000) {
  252. s = 1;
  253. step_mfi = CFG_866_OSCCLK / 2;
  254. mfd = 14;
  255. step_mfn = CFG_866_OSCCLK / 30;
  256. } else {
  257. s = 0;
  258. step_mfi = CFG_866_OSCCLK;
  259. mfd = 29;
  260. step_mfn = CFG_866_OSCCLK / 30;
  261. }
  262. /* Calculate integer part of multiplication factor
  263. */
  264. n = clk / step_mfi;
  265. mfi = (char)n;
  266. /* Calculate numerator of fractional part of multiplication factor
  267. */
  268. n = clk - (n * step_mfi);
  269. mfn = (char)(n / step_mfn);
  270. /* Calculate effective clk
  271. */
  272. n = ((mfi * step_mfi) + (mfn * step_mfn)) / (pdf + 1);
  273. immr->im_clkrstk.cark_plprcrk = KAPWR_KEY;
  274. plprcr = (immr->im_clkrst.car_plprcr & ~(PLPRCR_MFN_MSK
  275. | PLPRCR_MFD_MSK | PLPRCR_S_MSK
  276. | PLPRCR_MFI_MSK | PLPRCR_DBRMO
  277. | PLPRCR_PDF_MSK))
  278. | (mfn << PLPRCR_MFN_SHIFT)
  279. | (mfd << PLPRCR_MFD_SHIFT)
  280. | (s << PLPRCR_S_SHIFT)
  281. | (mfi << PLPRCR_MFI_SHIFT)
  282. | (pdf << PLPRCR_PDF_SHIFT);
  283. if( (mfn > 0) && ((mfd / mfn) > 10) )
  284. plprcr |= PLPRCR_DBRMO;
  285. plprcr_write_866 (plprcr); /* set value using SIU4/9 workaround */
  286. immr->im_clkrstk.cark_plprcrk = 0x00000000;
  287. return (n);
  288. }
  289. #endif /* CONFIG_MPC866_et_al */
  290. /* ------------------------------------------------------------------------- */