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