speed.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /*
  2. * (C) Copyright 2000-2002
  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. #define PITC_SHIFT 16
  27. #define PITR_SHIFT 16
  28. /* pitc values to time for 58/8192 seconds (about 70.8 milliseconds) */
  29. #define SPEED_PIT_COUNTS 58
  30. #define SPEED_PITC ((SPEED_PIT_COUNTS - 1) << PITC_SHIFT)
  31. #define SPEED_PITC_INIT ((SPEED_PIT_COUNTS + 1) << PITC_SHIFT)
  32. /* Access functions for the Machine State Register */
  33. static __inline__ unsigned long get_msr(void)
  34. {
  35. unsigned long msr;
  36. asm volatile("mfmsr %0" : "=r" (msr) :);
  37. return msr;
  38. }
  39. static __inline__ void set_msr(unsigned long msr)
  40. {
  41. asm volatile("mtmsr %0" : : "r" (msr));
  42. }
  43. /* ------------------------------------------------------------------------- */
  44. /*
  45. * Measure CPU clock speed (core clock GCLK1, GCLK2),
  46. * also determine bus clock speed (checking bus divider factor)
  47. *
  48. * (Approx. GCLK frequency in Hz)
  49. *
  50. * Initializes timer 2 and PIT, but disables them before return.
  51. * [Use timer 2, because MPC823 CPUs mask 0.x do not have timers 3 and 4]
  52. *
  53. * When measuring the CPU clock against the PIT, we count cpu clocks
  54. * for 58/8192 seconds with a prescale divide by 177 for the cpu clock.
  55. * These strange values for the timing interval and prescaling are used
  56. * because the formula for the CPU clock is:
  57. *
  58. * CPU clock = count * (177 * (8192 / 58))
  59. *
  60. * = count * 24999.7241
  61. *
  62. * which is very close to
  63. *
  64. * = count * 25000
  65. *
  66. * Since the count gives the CPU clock divided by 25000, we can get
  67. * the CPU clock rounded to the nearest 0.1 MHz by
  68. *
  69. * CPU clock = ((count + 2) / 4) * 100000;
  70. *
  71. * The rounding is important since the measurement is sometimes going
  72. * to be high or low by 0.025 MHz, depending on exactly how the clocks
  73. * and counters interact. By rounding we get the exact answer for any
  74. * CPU clock that is an even multiple of 0.1 MHz.
  75. */
  76. unsigned long measure_gclk(void)
  77. {
  78. volatile immap_t *immr = (immap_t *) CFG_IMMR;
  79. volatile cpmtimer8xx_t *timerp = &immr->im_cpmtimer;
  80. ulong timer2_val;
  81. ulong msr_val;
  82. #ifdef CONFIG_MPC866_et_al
  83. /* dont use OSCM, only use EXTCLK/512 */
  84. immr->im_clkrst.car_sccr |= SCCR_RTSEL | SCCR_RTDIV;
  85. #else
  86. immr->im_clkrst.car_sccr &= ~(SCCR_RTSEL | SCCR_RTDIV);
  87. #endif
  88. /* Reset + Stop Timer 2, no cascading
  89. */
  90. timerp->cpmt_tgcr &= ~(TGCR_CAS2 | TGCR_RST2);
  91. /* Keep stopped, halt in debug mode
  92. */
  93. timerp->cpmt_tgcr |= (TGCR_FRZ2 | TGCR_STP2);
  94. /* Timer 2 setup:
  95. * Output ref. interrupt disable, int. clock
  96. * Prescale by 177. Note that prescaler divides by value + 1
  97. * so we must subtract 1 here.
  98. */
  99. timerp->cpmt_tmr2 = ((177 - 1) << TMR_PS_SHIFT) | TMR_ICLK_IN_GEN;
  100. timerp->cpmt_tcn2 = 0; /* reset state */
  101. timerp->cpmt_tgcr |= TGCR_RST2; /* enable timer 2 */
  102. /*
  103. * PIT setup:
  104. *
  105. * We want to time for SPEED_PITC_COUNTS counts (of 8192 Hz),
  106. * so the count value would be SPEED_PITC_COUNTS - 1.
  107. * But there would be an uncertainty in the start time of 1/4
  108. * count since when we enable the PIT the count is not
  109. * synchronized to the 32768 Hz oscillator. The trick here is
  110. * to start the count higher and wait until the PIT count
  111. * changes to the required value before starting timer 2.
  112. *
  113. * One count high should be enough, but occasionally the start
  114. * is off by 1 or 2 counts of 32768 Hz. With the start value
  115. * set two counts high it seems very reliable.
  116. */
  117. immr->im_sitk.sitk_pitck = KAPWR_KEY; /* PIT initialization */
  118. immr->im_sit.sit_pitc = SPEED_PITC_INIT;
  119. immr->im_sitk.sitk_piscrk = KAPWR_KEY;
  120. immr->im_sit.sit_piscr = CFG_PISCR;
  121. /*
  122. * Start measurement - disable interrupts, just in case
  123. */
  124. msr_val = get_msr ();
  125. set_msr (msr_val & ~MSR_EE);
  126. immr->im_sit.sit_piscr |= PISCR_PTE;
  127. /* spin until get exact count when we want to start */
  128. while (immr->im_sit.sit_pitr > SPEED_PITC);
  129. timerp->cpmt_tgcr &= ~TGCR_STP2; /* Start Timer 2 */
  130. while ((immr->im_sit.sit_piscr & PISCR_PS) == 0);
  131. timerp->cpmt_tgcr |= TGCR_STP2; /* Stop Timer 2 */
  132. /* re-enable external interrupts if they were on */
  133. set_msr (msr_val);
  134. /* Disable timer and PIT
  135. */
  136. timer2_val = timerp->cpmt_tcn2; /* save before reset timer */
  137. timerp->cpmt_tgcr &= ~(TGCR_RST2 | TGCR_FRZ2 | TGCR_STP2);
  138. immr->im_sit.sit_piscr &= ~PISCR_PTE;
  139. #ifdef CONFIG_MPC866_et_al
  140. /* not using OSCM, using XIN, so scale appropriately */
  141. return (((timer2_val + 2) / 4) * (CFG_8XX_XIN/512))/8192 * 100000L;
  142. #else
  143. return ((timer2_val + 2) / 4) * 100000L; /* convert to Hz */
  144. #endif
  145. }
  146. /*
  147. * get_clocks() fills in gd->cpu_clock depending on CONFIG_8xx_GCLK_FREQ
  148. * or (if it is not defined) measure_gclk() (which uses the ref clock)
  149. * from above.
  150. */
  151. int get_clocks (void)
  152. {
  153. DECLARE_GLOBAL_DATA_PTR;
  154. volatile immap_t *immr = (immap_t *) CFG_IMMR;
  155. #ifndef CONFIG_8xx_GCLK_FREQ
  156. gd->cpu_clk = measure_gclk();
  157. #else /* CONFIG_8xx_GCLK_FREQ */
  158. /*
  159. * If for some reason measuring the gclk frequency won't
  160. * work, we return the hardwired value.
  161. * (For example, the cogent CMA286-60 CPU module has no
  162. * separate oscillator for PITRTCLK)
  163. */
  164. gd->cpu_clk = CONFIG_8xx_GCLK_FREQ;
  165. #endif /* CONFIG_8xx_GCLK_FREQ */
  166. if ((immr->im_clkrst.car_sccr & SCCR_EBDF11) == 0) {
  167. /* No Bus Divider active */
  168. gd->bus_clk = gd->cpu_clk;
  169. } else {
  170. /* The MPC8xx has only one BDF: half clock speed */
  171. gd->bus_clk = gd->cpu_clk / 2;
  172. }
  173. return (0);
  174. }
  175. /* ------------------------------------------------------------------------- */