1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24#include <common.h>
25#include <commproc.h>
26#include <mpc8xx_irq.h>
27#include <exports.h>
28
29DECLARE_GLOBAL_DATA_PTR;
30
31#undef DEBUG
32
33#define TIMER_PERIOD 1000000
34
35static void timer_handler (void *arg);
36
37
38
39static __inline__ unsigned long get_msr(void)
40{
41 unsigned long msr;
42
43 asm volatile("mfmsr %0" : "=r" (msr) :);
44 return msr;
45}
46
47static __inline__ void set_msr(unsigned long msr)
48{
49 asm volatile("mtmsr %0" : : "r" (msr));
50}
51
52
53
54
55
56
57
58typedef struct tid_8xx_cpmtimer_s {
59 int cpm_vec;
60 ushort *tgcrp;
61 ushort *tmrp;
62 ushort *trrp;
63 ushort *tcrp;
64 ushort *tcnp;
65 ushort *terp;
66} tid_8xx_cpmtimer_t;
67
68#ifndef CLOCKRATE
69# define CLOCKRATE 64
70#endif
71
72#define CPMT_CLOCK_DIV 16
73#define CPMT_MAX_PRESCALER 256
74#define CPMT_MAX_REFERENCE 65535
75
76#define CPMT_MAX_TICKS (CPMT_MAX_REFERENCE * CPMT_MAX_PRESCALER)
77#define CPMT_MAX_TICKS_WITH_DIV (CPMT_MAX_REFERENCE * CPMT_MAX_PRESCALER * CPMT_CLOCK_DIV)
78#define CPMT_MAX_INTERVAL (CPMT_MAX_TICKS_WITH_DIV / CLOCKRATE)
79
80
81#define CPMT_PRESCALER (CPMT_MAX_PRESCALER)
82
83
84#define CPMT_EVENT_CAP 0x0001
85#define CPMT_EVENT_REF 0x0002
86
87
88#define CPMT_GCR_RST 0x0001
89#define CPMT_GCR_STP 0x0002
90#define CPMT_GCR_FRZ 0x0004
91#define CPMT_GCR_GM_CAS 0x0008
92#define CPMT_GCR_MASK (CPMT_GCR_RST|CPMT_GCR_STP|CPMT_GCR_FRZ|CPMT_GCR_GM_CAS)
93
94
95#define CPMT_MR_GE 0x0001
96#define CPMT_MR_ICLK_CASC 0x0000
97#define CPMT_MR_ICLK_CLK 0x0002
98#define CPMT_MR_ICLK_CLKDIV 0x0004
99#define CPMT_MR_ICLK_TIN 0x0006
100#define CPMT_MR_FRR 0x0008
101#define CPMT_MR_ORI 0x0010
102#define CPMT_MR_OM 0x0020
103#define CPMT_MR_CE_DIS 0x0000
104#define CPMT_MR_CE_RISE 0x0040
105#define CPMT_MR_CE_FALL 0x0080
106#define CPMT_MR_CE_ANY 0x00C0
107
108
109
110
111
112#define TID_TIMER_ID 0
113
114void setPeriod (tid_8xx_cpmtimer_t *hwp, ulong interval);
115
116static char *usage = "\n[q, b, e, ?] ";
117
118int timer (int argc, char * const argv[])
119{
120 cpmtimer8xx_t *cpmtimerp;
121 tid_8xx_cpmtimer_t hw;
122 tid_8xx_cpmtimer_t *hwp = &hw;
123 int c;
124 int running;
125
126 app_startup(argv);
127
128
129 cpmtimerp = &((immap_t *) gd->bd->bi_immr_base)->im_cpmtimer;
130
131 printf ("TIMERS=0x%x\n", (unsigned) cpmtimerp);
132
133
134 switch (TID_TIMER_ID) {
135 case 0:
136 hwp->tmrp = &(cpmtimerp->cpmt_tmr1);
137 hwp->trrp = &(cpmtimerp->cpmt_trr1);
138 hwp->tcrp = &(cpmtimerp->cpmt_tcr1);
139 hwp->tcnp = &(cpmtimerp->cpmt_tcn1);
140 hwp->terp = &(cpmtimerp->cpmt_ter1);
141 hwp->cpm_vec = CPMVEC_TIMER1;
142 break;
143 case 1:
144 hwp->tmrp = &(cpmtimerp->cpmt_tmr2);
145 hwp->trrp = &(cpmtimerp->cpmt_trr2);
146 hwp->tcrp = &(cpmtimerp->cpmt_tcr2);
147 hwp->tcnp = &(cpmtimerp->cpmt_tcn2);
148 hwp->terp = &(cpmtimerp->cpmt_ter2);
149 hwp->cpm_vec = CPMVEC_TIMER2;
150 break;
151 case 2:
152 hwp->tmrp = &(cpmtimerp->cpmt_tmr3);
153 hwp->trrp = &(cpmtimerp->cpmt_trr3);
154 hwp->tcrp = &(cpmtimerp->cpmt_tcr3);
155 hwp->tcnp = &(cpmtimerp->cpmt_tcn3);
156 hwp->terp = &(cpmtimerp->cpmt_ter3);
157 hwp->cpm_vec = CPMVEC_TIMER3;
158 break;
159 case 3:
160 hwp->tmrp = &(cpmtimerp->cpmt_tmr4);
161 hwp->trrp = &(cpmtimerp->cpmt_trr4);
162 hwp->tcrp = &(cpmtimerp->cpmt_tcr4);
163 hwp->tcnp = &(cpmtimerp->cpmt_tcn4);
164 hwp->terp = &(cpmtimerp->cpmt_ter4);
165 hwp->cpm_vec = CPMVEC_TIMER4;
166 break;
167 }
168
169 hwp->tgcrp = &cpmtimerp->cpmt_tgcr;
170
171 printf ("Using timer %d\n"
172 "tgcr @ 0x%x, tmr @ 0x%x, trr @ 0x%x,"
173 " tcr @ 0x%x, tcn @ 0x%x, ter @ 0x%x\n",
174 TID_TIMER_ID + 1,
175 (unsigned) hwp->tgcrp,
176 (unsigned) hwp->tmrp,
177 (unsigned) hwp->trrp,
178 (unsigned) hwp->tcrp,
179 (unsigned) hwp->tcnp,
180 (unsigned) hwp->terp
181 );
182
183
184 *hwp->tgcrp &= ~(CPMT_GCR_MASK << TID_TIMER_ID);
185
186
187 *hwp->terp = (CPMT_EVENT_CAP | CPMT_EVENT_REF);
188
189 printf (usage);
190 running = 0;
191 while ((c = getc()) != 'q') {
192 if (c == 'b') {
193
194 setPeriod (hwp, TIMER_PERIOD);
195
196
197 install_hdlr (hwp->cpm_vec, timer_handler, hwp);
198
199 printf ("Enabling timer\n");
200
201
202 *hwp->tgcrp |= (CPMT_GCR_RST << TID_TIMER_ID);
203 running = 1;
204
205#ifdef DEBUG
206 printf ("tgcr=0x%x, tmr=0x%x, trr=0x%x,"
207 " tcr=0x%x, tcn=0x%x, ter=0x%x\n",
208 *hwp->tgcrp, *hwp->tmrp, *hwp->trrp,
209 *hwp->tcrp, *hwp->tcnp, *hwp->terp
210 );
211#endif
212 } else if (c == 'e') {
213
214 printf ("Stopping timer\n");
215
216 *hwp->tgcrp &= ~(CPMT_GCR_MASK << TID_TIMER_ID);
217 running = 0;
218
219#ifdef DEBUG
220 printf ("tgcr=0x%x, tmr=0x%x, trr=0x%x,"
221 " tcr=0x%x, tcn=0x%x, ter=0x%x\n",
222 *hwp->tgcrp, *hwp->tmrp, *hwp->trrp,
223 *hwp->tcrp, *hwp->tcnp, *hwp->terp
224 );
225#endif
226
227 free_hdlr (hwp->cpm_vec);
228
229 } else if (c == '?') {
230#ifdef DEBUG
231 cpic8xx_t *cpm_icp = &((immap_t *) gd->bd->bi_immr_base)->im_cpic;
232 sysconf8xx_t *siup = &((immap_t *) gd->bd->bi_immr_base)->im_siu_conf;
233#endif
234
235 printf ("\ntgcr=0x%x, tmr=0x%x, trr=0x%x,"
236 " tcr=0x%x, tcn=0x%x, ter=0x%x\n",
237 *hwp->tgcrp, *hwp->tmrp, *hwp->trrp,
238 *hwp->tcrp, *hwp->tcnp, *hwp->terp
239 );
240#ifdef DEBUG
241 printf ("SIUMCR=0x%08lx, SYPCR=0x%08lx,"
242 " SIMASK=0x%08lx, SIPEND=0x%08lx\n",
243 siup->sc_siumcr,
244 siup->sc_sypcr,
245 siup->sc_simask,
246 siup->sc_sipend
247 );
248
249 printf ("CIMR=0x%08lx, CICR=0x%08lx, CIPR=0x%08lx\n",
250 cpm_icp->cpic_cimr,
251 cpm_icp->cpic_cicr,
252 cpm_icp->cpic_cipr
253 );
254#endif
255 } else {
256 printf ("\nEnter: q - quit, b - start timer, e - stop timer, ? - get status\n");
257 }
258 printf (usage);
259 }
260 if (running) {
261 printf ("Stopping timer\n");
262 *hwp->tgcrp &= ~(CPMT_GCR_MASK << TID_TIMER_ID);
263 free_hdlr (hwp->cpm_vec);
264 }
265
266 return (0);
267}
268
269
270
271
272
273
274void setPeriod (tid_8xx_cpmtimer_t *hwp, ulong interval)
275{
276 unsigned short prescaler;
277 unsigned long ticks;
278
279 printf ("Set interval %ld us\n", interval);
280
281
282 if (interval > CPMT_MAX_INTERVAL) {
283 printf ("Truncate interval %ld to maximum (%d)\n",
284 interval, CPMT_MAX_INTERVAL);
285 interval = CPMT_MAX_INTERVAL;
286 }
287
288
289
290
291
292
293
294 prescaler = (ushort) ((CPMT_PRESCALER - 1) << 8) |
295 CPMT_MR_ORI |
296 CPMT_MR_FRR;
297
298 ticks = ((ulong) CLOCKRATE * interval);
299
300 if (ticks > CPMT_MAX_TICKS) {
301 ticks /= CPMT_CLOCK_DIV;
302 prescaler |= CPMT_MR_ICLK_CLKDIV;
303 } else {
304 prescaler |= CPMT_MR_ICLK_CLK;
305 }
306
307#ifdef DEBUG
308 printf ("clock/%d, prescale factor %d, reference %ld, ticks %ld\n",
309 (ticks > CPMT_MAX_TICKS) ? CPMT_CLOCK_DIV : 1,
310 CPMT_PRESCALER,
311 (ticks / CPMT_PRESCALER),
312 ticks
313 );
314#endif
315
316
317 *hwp->tmrp = prescaler;
318
319
320 *hwp->tcnp = 0;
321
322
323 *hwp->trrp = (unsigned short) (ticks / CPMT_PRESCALER);
324
325#ifdef DEBUG
326 printf ("tgcr=0x%x, tmr=0x%x, trr=0x%x,"
327 " tcr=0x%x, tcn=0x%x, ter=0x%x\n",
328 *hwp->tgcrp, *hwp->tmrp, *hwp->trrp,
329 *hwp->tcrp, *hwp->tcnp, *hwp->terp
330 );
331#endif
332}
333
334
335
336
337static
338void timer_handler (void *arg)
339{
340 tid_8xx_cpmtimer_t *hwp = (tid_8xx_cpmtimer_t *)arg;
341
342
343
344
345 printf (".");
346
347
348 *hwp->terp = (CPMT_EVENT_CAP | CPMT_EVENT_REF);
349}
350