blob: 381553755732531e58056fa8e8209d412036791b [file] [log] [blame]
wdenk4a9cbbe2002-08-27 09:48:53 +00001/*
wdenkd4ca31c2004-01-02 14:00:00 +00002 * (C) Copyright 2000-2004
wdenk4a9cbbe2002-08-27 09:48:53 +00003 * 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
24#include <common.h>
25#include <mpc8xx.h>
26#include <asm/processor.h>
27
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
wdenk4a9cbbe2002-08-27 09:48:53 +000035/* Access functions for the Machine State Register */
36static __inline__ unsigned long get_msr(void)
37{
38 unsigned long msr;
39
40 asm volatile("mfmsr %0" : "=r" (msr) :);
41 return msr;
42}
43
44static __inline__ void set_msr(unsigned long msr)
45{
46 asm volatile("mtmsr %0" : : "r" (msr));
47}
wdenk4a9cbbe2002-08-27 09:48:53 +000048
49/* ------------------------------------------------------------------------- */
50
51/*
52 * Measure CPU clock speed (core clock GCLK1, GCLK2),
53 * also determine bus clock speed (checking bus divider factor)
54 *
55 * (Approx. GCLK frequency in Hz)
56 *
57 * Initializes timer 2 and PIT, but disables them before return.
58 * [Use timer 2, because MPC823 CPUs mask 0.x do not have timers 3 and 4]
59 *
60 * When measuring the CPU clock against the PIT, we count cpu clocks
61 * for 58/8192 seconds with a prescale divide by 177 for the cpu clock.
62 * These strange values for the timing interval and prescaling are used
63 * because the formula for the CPU clock is:
64 *
65 * CPU clock = count * (177 * (8192 / 58))
66 *
67 * = count * 24999.7241
68 *
69 * which is very close to
70 *
71 * = count * 25000
72 *
73 * Since the count gives the CPU clock divided by 25000, we can get
74 * the CPU clock rounded to the nearest 0.1 MHz by
75 *
76 * CPU clock = ((count + 2) / 4) * 100000;
77 *
78 * The rounding is important since the measurement is sometimes going
79 * to be high or low by 0.025 MHz, depending on exactly how the clocks
80 * and counters interact. By rounding we get the exact answer for any
81 * CPU clock that is an even multiple of 0.1 MHz.
82 */
83
wdenk2535d602003-07-17 23:16:40 +000084unsigned long measure_gclk(void)
wdenk4a9cbbe2002-08-27 09:48:53 +000085{
wdenk4a9cbbe2002-08-27 09:48:53 +000086 volatile immap_t *immr = (immap_t *) CFG_IMMR;
wdenk4a9cbbe2002-08-27 09:48:53 +000087 volatile cpmtimer8xx_t *timerp = &immr->im_cpmtimer;
88 ulong timer2_val;
89 ulong msr_val;
90
wdenk2535d602003-07-17 23:16:40 +000091#ifdef CONFIG_MPC866_et_al
wdenk42d1f032003-10-15 23:53:47 +000092 /* dont use OSCM, only use EXTCLK/512 */
93 immr->im_clkrst.car_sccr |= SCCR_RTSEL | SCCR_RTDIV;
wdenk2535d602003-07-17 23:16:40 +000094#else
wdenk42d1f032003-10-15 23:53:47 +000095 immr->im_clkrst.car_sccr &= ~(SCCR_RTSEL | SCCR_RTDIV);
wdenk2535d602003-07-17 23:16:40 +000096#endif
97
wdenk4a9cbbe2002-08-27 09:48:53 +000098 /* Reset + Stop Timer 2, no cascading
99 */
100 timerp->cpmt_tgcr &= ~(TGCR_CAS2 | TGCR_RST2);
101
102 /* Keep stopped, halt in debug mode
103 */
104 timerp->cpmt_tgcr |= (TGCR_FRZ2 | TGCR_STP2);
105
106 /* Timer 2 setup:
107 * Output ref. interrupt disable, int. clock
108 * Prescale by 177. Note that prescaler divides by value + 1
109 * so we must subtract 1 here.
110 */
111 timerp->cpmt_tmr2 = ((177 - 1) << TMR_PS_SHIFT) | TMR_ICLK_IN_GEN;
112
113 timerp->cpmt_tcn2 = 0; /* reset state */
114 timerp->cpmt_tgcr |= TGCR_RST2; /* enable timer 2 */
115
116 /*
117 * PIT setup:
118 *
wdenk8bde7f72003-06-27 21:31:46 +0000119 * We want to time for SPEED_PITC_COUNTS counts (of 8192 Hz),
120 * so the count value would be SPEED_PITC_COUNTS - 1.
121 * But there would be an uncertainty in the start time of 1/4
122 * count since when we enable the PIT the count is not
123 * synchronized to the 32768 Hz oscillator. The trick here is
124 * to start the count higher and wait until the PIT count
125 * changes to the required value before starting timer 2.
wdenk4a9cbbe2002-08-27 09:48:53 +0000126 *
wdenk8bde7f72003-06-27 21:31:46 +0000127 * One count high should be enough, but occasionally the start
128 * is off by 1 or 2 counts of 32768 Hz. With the start value
129 * set two counts high it seems very reliable.
130 */
wdenk4a9cbbe2002-08-27 09:48:53 +0000131
132 immr->im_sitk.sitk_pitck = KAPWR_KEY; /* PIT initialization */
133 immr->im_sit.sit_pitc = SPEED_PITC_INIT;
134
135 immr->im_sitk.sitk_piscrk = KAPWR_KEY;
136 immr->im_sit.sit_piscr = CFG_PISCR;
137
138 /*
139 * Start measurement - disable interrupts, just in case
140 */
141 msr_val = get_msr ();
142 set_msr (msr_val & ~MSR_EE);
143
144 immr->im_sit.sit_piscr |= PISCR_PTE;
145
146 /* spin until get exact count when we want to start */
147 while (immr->im_sit.sit_pitr > SPEED_PITC);
148
149 timerp->cpmt_tgcr &= ~TGCR_STP2; /* Start Timer 2 */
150 while ((immr->im_sit.sit_piscr & PISCR_PS) == 0);
151 timerp->cpmt_tgcr |= TGCR_STP2; /* Stop Timer 2 */
152
153 /* re-enable external interrupts if they were on */
154 set_msr (msr_val);
155
156 /* Disable timer and PIT
157 */
158 timer2_val = timerp->cpmt_tcn2; /* save before reset timer */
159
160 timerp->cpmt_tgcr &= ~(TGCR_RST2 | TGCR_FRZ2 | TGCR_STP2);
161 immr->im_sit.sit_piscr &= ~PISCR_PTE;
162
wdenkd4ca31c2004-01-02 14:00:00 +0000163#if defined(CONFIG_MPC866_et_al)
wdenk42d1f032003-10-15 23:53:47 +0000164 /* not using OSCM, using XIN, so scale appropriately */
wdenk2535d602003-07-17 23:16:40 +0000165 return (((timer2_val + 2) / 4) * (CFG_8XX_XIN/512))/8192 * 100000L;
166#else
167 return ((timer2_val + 2) / 4) * 100000L; /* convert to Hz */
168#endif
169}
wdenk4a9cbbe2002-08-27 09:48:53 +0000170
wdenk2535d602003-07-17 23:16:40 +0000171/*
172 * get_clocks() fills in gd->cpu_clock depending on CONFIG_8xx_GCLK_FREQ
173 * or (if it is not defined) measure_gclk() (which uses the ref clock)
174 * from above.
175 */
176int get_clocks (void)
177{
178 DECLARE_GLOBAL_DATA_PTR;
179
180 volatile immap_t *immr = (immap_t *) CFG_IMMR;
181#ifndef CONFIG_8xx_GCLK_FREQ
182 gd->cpu_clk = measure_gclk();
wdenk4a9cbbe2002-08-27 09:48:53 +0000183#else /* CONFIG_8xx_GCLK_FREQ */
wdenk4a9cbbe2002-08-27 09:48:53 +0000184 /*
wdenk8bde7f72003-06-27 21:31:46 +0000185 * If for some reason measuring the gclk frequency won't
186 * work, we return the hardwired value.
187 * (For example, the cogent CMA286-60 CPU module has no
188 * separate oscillator for PITRTCLK)
wdenk4a9cbbe2002-08-27 09:48:53 +0000189 */
190
191 gd->cpu_clk = CONFIG_8xx_GCLK_FREQ;
192
193#endif /* CONFIG_8xx_GCLK_FREQ */
194
195 if ((immr->im_clkrst.car_sccr & SCCR_EBDF11) == 0) {
196 /* No Bus Divider active */
197 gd->bus_clk = gd->cpu_clk;
198 } else {
199 /* The MPC8xx has only one BDF: half clock speed */
200 gd->bus_clk = gd->cpu_clk / 2;
201 }
202
203 return (0);
204}
205
206/* ------------------------------------------------------------------------- */