speed.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  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. DECLARE_GLOBAL_DATA_PTR;
  27. #if !defined(CONFIG_8xx_CPUCLK_DEFAULT) || defined(CONFIG_SYS_MEASURE_CPUCLK) || defined(DEBUG)
  28. #define PITC_SHIFT 16
  29. #define PITR_SHIFT 16
  30. /* pitc values to time for 58/8192 seconds (about 70.8 milliseconds) */
  31. #define SPEED_PIT_COUNTS 58
  32. #define SPEED_PITC ((SPEED_PIT_COUNTS - 1) << PITC_SHIFT)
  33. #define SPEED_PITC_INIT ((SPEED_PIT_COUNTS + 1) << PITC_SHIFT)
  34. /* Access functions for the Machine State Register */
  35. static __inline__ unsigned long get_msr(void)
  36. {
  37. unsigned long msr;
  38. asm volatile("mfmsr %0" : "=r" (msr) :);
  39. return msr;
  40. }
  41. static __inline__ void set_msr(unsigned long msr)
  42. {
  43. asm volatile("mtmsr %0" : : "r" (msr));
  44. }
  45. /* ------------------------------------------------------------------------- */
  46. /*
  47. * Measure CPU clock speed (core clock GCLK1, GCLK2),
  48. * also determine bus clock speed (checking bus divider factor)
  49. *
  50. * (Approx. GCLK frequency in Hz)
  51. *
  52. * Initializes timer 2 and PIT, but disables them before return.
  53. * [Use timer 2, because MPC823 CPUs mask 0.x do not have timers 3 and 4]
  54. *
  55. * When measuring the CPU clock against the PIT, we count cpu clocks
  56. * for 58/8192 seconds with a prescale divide by 177 for the cpu clock.
  57. * These strange values for the timing interval and prescaling are used
  58. * because the formula for the CPU clock is:
  59. *
  60. * CPU clock = count * (177 * (8192 / 58))
  61. *
  62. * = count * 24999.7241
  63. *
  64. * which is very close to
  65. *
  66. * = count * 25000
  67. *
  68. * Since the count gives the CPU clock divided by 25000, we can get
  69. * the CPU clock rounded to the nearest 0.1 MHz by
  70. *
  71. * CPU clock = ((count + 2) / 4) * 100000;
  72. *
  73. * The rounding is important since the measurement is sometimes going
  74. * to be high or low by 0.025 MHz, depending on exactly how the clocks
  75. * and counters interact. By rounding we get the exact answer for any
  76. * CPU clock that is an even multiple of 0.1 MHz.
  77. */
  78. unsigned long measure_gclk(void)
  79. {
  80. volatile immap_t *immr = (immap_t *) CONFIG_SYS_IMMR;
  81. volatile cpmtimer8xx_t *timerp = &immr->im_cpmtimer;
  82. ulong timer2_val;
  83. ulong msr_val;
  84. #ifdef CONFIG_SYS_8XX_XIN
  85. /* dont use OSCM, only use EXTCLK/512 */
  86. immr->im_clkrst.car_sccr |= SCCR_RTSEL | SCCR_RTDIV;
  87. #else
  88. immr->im_clkrst.car_sccr &= ~(SCCR_RTSEL | SCCR_RTDIV);
  89. #endif
  90. /* Reset + Stop Timer 2, no cascading
  91. */
  92. timerp->cpmt_tgcr &= ~(TGCR_CAS2 | TGCR_RST2);
  93. /* Keep stopped, halt in debug mode
  94. */
  95. timerp->cpmt_tgcr |= (TGCR_FRZ2 | TGCR_STP2);
  96. /* Timer 2 setup:
  97. * Output ref. interrupt disable, int. clock
  98. * Prescale by 177. Note that prescaler divides by value + 1
  99. * so we must subtract 1 here.
  100. */
  101. timerp->cpmt_tmr2 = ((177 - 1) << TMR_PS_SHIFT) | TMR_ICLK_IN_GEN;
  102. timerp->cpmt_tcn2 = 0; /* reset state */
  103. timerp->cpmt_tgcr |= TGCR_RST2; /* enable timer 2 */
  104. /*
  105. * PIT setup:
  106. *
  107. * We want to time for SPEED_PITC_COUNTS counts (of 8192 Hz),
  108. * so the count value would be SPEED_PITC_COUNTS - 1.
  109. * But there would be an uncertainty in the start time of 1/4
  110. * count since when we enable the PIT the count is not
  111. * synchronized to the 32768 Hz oscillator. The trick here is
  112. * to start the count higher and wait until the PIT count
  113. * changes to the required value before starting timer 2.
  114. *
  115. * One count high should be enough, but occasionally the start
  116. * is off by 1 or 2 counts of 32768 Hz. With the start value
  117. * set two counts high it seems very reliable.
  118. */
  119. immr->im_sitk.sitk_pitck = KAPWR_KEY; /* PIT initialization */
  120. immr->im_sit.sit_pitc = SPEED_PITC_INIT;
  121. immr->im_sitk.sitk_piscrk = KAPWR_KEY;
  122. immr->im_sit.sit_piscr = CONFIG_SYS_PISCR;
  123. /*
  124. * Start measurement - disable interrupts, just in case
  125. */
  126. msr_val = get_msr ();
  127. set_msr (msr_val & ~MSR_EE);
  128. immr->im_sit.sit_piscr |= PISCR_PTE;
  129. /* spin until get exact count when we want to start */
  130. while (immr->im_sit.sit_pitr > SPEED_PITC);
  131. timerp->cpmt_tgcr &= ~TGCR_STP2; /* Start Timer 2 */
  132. while ((immr->im_sit.sit_piscr & PISCR_PS) == 0);
  133. timerp->cpmt_tgcr |= TGCR_STP2; /* Stop Timer 2 */
  134. /* re-enable external interrupts if they were on */
  135. set_msr (msr_val);
  136. /* Disable timer and PIT
  137. */
  138. timer2_val = timerp->cpmt_tcn2; /* save before reset timer */
  139. timerp->cpmt_tgcr &= ~(TGCR_RST2 | TGCR_FRZ2 | TGCR_STP2);
  140. immr->im_sit.sit_piscr &= ~PISCR_PTE;
  141. #if defined(CONFIG_SYS_8XX_XIN)
  142. /* not using OSCM, using XIN, so scale appropriately */
  143. return (((timer2_val + 2) / 4) * (CONFIG_SYS_8XX_XIN/512))/8192 * 100000L;
  144. #else
  145. return ((timer2_val + 2) / 4) * 100000L; /* convert to Hz */
  146. #endif
  147. }
  148. #endif
  149. void get_brgclk(uint sccr)
  150. {
  151. uint divider = 0;
  152. switch((sccr&SCCR_DFBRG11)>>11){
  153. case 0:
  154. divider = 1;
  155. break;
  156. case 1:
  157. divider = 4;
  158. break;
  159. case 2:
  160. divider = 16;
  161. break;
  162. case 3:
  163. divider = 64;
  164. break;
  165. }
  166. gd->brg_clk = gd->cpu_clk/divider;
  167. }
  168. #if !defined(CONFIG_8xx_CPUCLK_DEFAULT)
  169. /*
  170. * get_clocks() fills in gd->cpu_clock depending on CONFIG_8xx_GCLK_FREQ
  171. * or (if it is not defined) measure_gclk() (which uses the ref clock)
  172. * from above.
  173. */
  174. int get_clocks (void)
  175. {
  176. uint immr = get_immr (0); /* Return full IMMR contents */
  177. volatile immap_t *immap = (immap_t *) (immr & 0xFFFF0000);
  178. uint sccr = immap->im_clkrst.car_sccr;
  179. /*
  180. * If for some reason measuring the gclk frequency won't
  181. * work, we return the hardwired value.
  182. * (For example, the cogent CMA286-60 CPU module has no
  183. * separate oscillator for PITRTCLK)
  184. */
  185. #if defined(CONFIG_8xx_GCLK_FREQ)
  186. gd->cpu_clk = CONFIG_8xx_GCLK_FREQ;
  187. #elif defined(CONFIG_8xx_OSCLK)
  188. #define PLPRCR_val(a) ((pll & PLPRCR_ ## a ## _MSK) >> PLPRCR_ ## a ## _SHIFT)
  189. uint pll = immap->im_clkrst.car_plprcr;
  190. uint clk;
  191. if ((immr & 0x0FFF) >= MPC8xx_NEW_CLK) { /* MPC866/87x/88x series */
  192. clk = ((CONFIG_8xx_OSCLK / (PLPRCR_val(PDF)+1)) *
  193. (PLPRCR_val(MFI) + PLPRCR_val(MFN) / (PLPRCR_val(MFD)+1))) /
  194. (1<<PLPRCR_val(S));
  195. } else {
  196. clk = CONFIG_8xx_OSCLK * (PLPRCR_val(MF)+1);
  197. }
  198. if (pll & PLPRCR_CSRC) { /* Low frequency division factor is used */
  199. gd->cpu_clk = clk / (2 << ((sccr >> 8) & 7));
  200. } else { /* High frequency division factor is used */
  201. gd->cpu_clk = clk / (1 << ((sccr >> 5) & 7));
  202. }
  203. #else
  204. gd->cpu_clk = measure_gclk();
  205. #endif /* CONFIG_8xx_GCLK_FREQ */
  206. if ((sccr & SCCR_EBDF11) == 0) {
  207. /* No Bus Divider active */
  208. gd->bus_clk = gd->cpu_clk;
  209. } else {
  210. /* The MPC8xx has only one BDF: half clock speed */
  211. gd->bus_clk = gd->cpu_clk / 2;
  212. }
  213. get_brgclk(sccr);
  214. return (0);
  215. }
  216. #else /* CONFIG_8xx_CPUCLK_DEFAULT defined, use dynamic clock setting */
  217. static long init_pll_866 (long clk);
  218. /* This function sets up PLL (init_pll_866() is called) and
  219. * fills gd->cpu_clk and gd->bus_clk according to the environment
  220. * variable 'cpuclk' or to CONFIG_8xx_CPUCLK_DEFAULT (if 'cpuclk'
  221. * contains invalid value).
  222. * This functions requires an MPC866 or newer series CPU.
  223. */
  224. int get_clocks_866 (void)
  225. {
  226. volatile immap_t *immr = (immap_t *) CONFIG_SYS_IMMR;
  227. char tmp[64];
  228. long cpuclk = 0;
  229. long sccr_reg;
  230. if (getenv_r ("cpuclk", tmp, sizeof (tmp)) > 0)
  231. cpuclk = simple_strtoul (tmp, NULL, 10) * 1000000;
  232. if ((CONFIG_SYS_8xx_CPUCLK_MIN > cpuclk) || (CONFIG_SYS_8xx_CPUCLK_MAX < cpuclk))
  233. cpuclk = CONFIG_8xx_CPUCLK_DEFAULT;
  234. gd->cpu_clk = init_pll_866 (cpuclk);
  235. #if defined(CONFIG_SYS_MEASURE_CPUCLK)
  236. gd->cpu_clk = measure_gclk ();
  237. #endif
  238. get_brgclk(immr->im_clkrst.car_sccr);
  239. /* if cpu clock <= 66 MHz then set bus division factor to 1,
  240. * otherwise set it to 2
  241. */
  242. sccr_reg = immr->im_clkrst.car_sccr;
  243. sccr_reg &= ~SCCR_EBDF11;
  244. if (gd->cpu_clk <= 66000000) {
  245. sccr_reg |= SCCR_EBDF00; /* bus division factor = 1 */
  246. gd->bus_clk = gd->cpu_clk;
  247. } else {
  248. sccr_reg |= SCCR_EBDF01; /* bus division factor = 2 */
  249. gd->bus_clk = gd->cpu_clk / 2;
  250. }
  251. immr->im_clkrst.car_sccr = sccr_reg;
  252. return (0);
  253. }
  254. /* Adjust sdram refresh rate to actual CPU clock.
  255. */
  256. int sdram_adjust_866 (void)
  257. {
  258. volatile immap_t *immr = (immap_t *) CONFIG_SYS_IMMR;
  259. long mamr;
  260. mamr = immr->im_memctl.memc_mamr;
  261. mamr &= ~MAMR_PTA_MSK;
  262. mamr |= ((gd->cpu_clk / CONFIG_SYS_PTA_PER_CLK) << MAMR_PTA_SHIFT);
  263. immr->im_memctl.memc_mamr = mamr;
  264. return (0);
  265. }
  266. /* Configure PLL for MPC866/859/885 CPU series
  267. * PLL multiplication factor is set to the value nearest to the desired clk,
  268. * assuming a oscclk of 10 MHz.
  269. */
  270. static long init_pll_866 (long clk)
  271. {
  272. extern void plprcr_write_866 (long);
  273. volatile immap_t *immr = (immap_t *) CONFIG_SYS_IMMR;
  274. long n, plprcr;
  275. char mfi, mfn, mfd, s, pdf;
  276. long step_mfi, step_mfn;
  277. if (clk < 20000000) {
  278. clk *= 2;
  279. pdf = 1;
  280. } else {
  281. pdf = 0;
  282. }
  283. if (clk < 40000000) {
  284. s = 2;
  285. step_mfi = CONFIG_8xx_OSCLK / 4;
  286. mfd = 7;
  287. step_mfn = CONFIG_8xx_OSCLK / 30;
  288. } else if (clk < 80000000) {
  289. s = 1;
  290. step_mfi = CONFIG_8xx_OSCLK / 2;
  291. mfd = 14;
  292. step_mfn = CONFIG_8xx_OSCLK / 30;
  293. } else {
  294. s = 0;
  295. step_mfi = CONFIG_8xx_OSCLK;
  296. mfd = 29;
  297. step_mfn = CONFIG_8xx_OSCLK / 30;
  298. }
  299. /* Calculate integer part of multiplication factor
  300. */
  301. n = clk / step_mfi;
  302. mfi = (char)n;
  303. /* Calculate numerator of fractional part of multiplication factor
  304. */
  305. n = clk - (n * step_mfi);
  306. mfn = (char)(n / step_mfn);
  307. /* Calculate effective clk
  308. */
  309. n = ((mfi * step_mfi) + (mfn * step_mfn)) / (pdf + 1);
  310. immr->im_clkrstk.cark_plprcrk = KAPWR_KEY;
  311. plprcr = (immr->im_clkrst.car_plprcr & ~(PLPRCR_MFN_MSK
  312. | PLPRCR_MFD_MSK | PLPRCR_S_MSK
  313. | PLPRCR_MFI_MSK | PLPRCR_DBRMO
  314. | PLPRCR_PDF_MSK))
  315. | (mfn << PLPRCR_MFN_SHIFT)
  316. | (mfd << PLPRCR_MFD_SHIFT)
  317. | (s << PLPRCR_S_SHIFT)
  318. | (mfi << PLPRCR_MFI_SHIFT)
  319. | (pdf << PLPRCR_PDF_SHIFT);
  320. if( (mfn > 0) && ((mfd / mfn) > 10) )
  321. plprcr |= PLPRCR_DBRMO;
  322. plprcr_write_866 (plprcr); /* set value using SIU4/9 workaround */
  323. immr->im_clkrstk.cark_plprcrk = 0x00000000;
  324. return (n);
  325. }
  326. #endif /* CONFIG_8xx_CPUCLK_DEFAULT */
  327. #if defined(CONFIG_TQM8xxL) && !defined(CONFIG_TQM866M) \
  328. && !defined(CONFIG_TQM885D)
  329. /*
  330. * Adjust sdram refresh rate to actual CPU clock
  331. * and set timebase source according to actual CPU clock
  332. */
  333. int adjust_sdram_tbs_8xx (void)
  334. {
  335. volatile immap_t *immr = (immap_t *) CONFIG_SYS_IMMR;
  336. long mamr;
  337. long sccr;
  338. mamr = immr->im_memctl.memc_mamr;
  339. mamr &= ~MAMR_PTA_MSK;
  340. mamr |= ((gd->cpu_clk / CONFIG_SYS_PTA_PER_CLK) << MAMR_PTA_SHIFT);
  341. immr->im_memctl.memc_mamr = mamr;
  342. if (gd->cpu_clk < 67000000) {
  343. sccr = immr->im_clkrst.car_sccr;
  344. sccr |= SCCR_TBS;
  345. immr->im_clkrst.car_sccr = sccr;
  346. }
  347. return (0);
  348. }
  349. #endif /* CONFIG_TQM8xxL/M, !TQM866M, !TQM885D */
  350. /* ------------------------------------------------------------------------- */