]>
git.ipfire.org Git - people/ms/u-boot.git/blob - examples/standalone/timer.c
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5 * SPDX-License-Identifier: GPL-2.0+
10 #include <mpc8xx_irq.h>
13 DECLARE_GLOBAL_DATA_PTR
;
17 #define TIMER_PERIOD 1000000 /* 1 second clock */
19 static void timer_handler (void *arg
);
22 /* Access functions for the Machine State Register */
23 static __inline__
unsigned long get_msr(void)
27 asm volatile("mfmsr %0" : "=r" (msr
) :);
31 static __inline__
void set_msr(unsigned long msr
)
33 asm volatile("mtmsr %0" : : "r" (msr
));
37 * Definitions to access the CPM Timer registers
38 * See 8xx_immap.h for Internal Memory Map layout,
39 * and commproc.h for CPM Interrupt vectors (aka "IRQ"s)
42 typedef struct tid_8xx_cpmtimer_s
{
43 int cpm_vec
; /* CPM Interrupt Vector for this timer */
44 ushort
*tgcrp
; /* Pointer to Timer Global Config Reg. */
45 ushort
*tmrp
; /* Pointer to Timer Mode Register */
46 ushort
*trrp
; /* Pointer to Timer Reference Register */
47 ushort
*tcrp
; /* Pointer to Timer Capture Register */
48 ushort
*tcnp
; /* Pointer to Timer Counter Register */
49 ushort
*terp
; /* Pointer to Timer Event Register */
56 #define CPMT_CLOCK_DIV 16
57 #define CPMT_MAX_PRESCALER 256
58 #define CPMT_MAX_REFERENCE 65535 /* max. unsigned short */
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)
64 /* For now: always use max. prescaler value */
65 #define CPMT_PRESCALER (CPMT_MAX_PRESCALER)
67 /* CPM Timer Event Register Bits */
68 #define CPMT_EVENT_CAP 0x0001 /* Capture Event */
69 #define CPMT_EVENT_REF 0x0002 /* Reference Counter Event */
71 /* CPM Timer Global Config Register */
72 #define CPMT_GCR_RST 0x0001 /* Reset Timer */
73 #define CPMT_GCR_STP 0x0002 /* Stop Timer */
74 #define CPMT_GCR_FRZ 0x0004 /* Freeze Timer */
75 #define CPMT_GCR_GM_CAS 0x0008 /* Gate Mode / Cascade Timers */
76 #define CPMT_GCR_MASK (CPMT_GCR_RST|CPMT_GCR_STP|CPMT_GCR_FRZ|CPMT_GCR_GM_CAS)
78 /* CPM Timer Mode register */
79 #define CPMT_MR_GE 0x0001 /* Gate Enable */
80 #define CPMT_MR_ICLK_CASC 0x0000 /* Clock internally cascaded */
81 #define CPMT_MR_ICLK_CLK 0x0002 /* Clock = system clock */
82 #define CPMT_MR_ICLK_CLKDIV 0x0004 /* Clock = system clock / 16 */
83 #define CPMT_MR_ICLK_TIN 0x0006 /* Clock = TINx signal */
84 #define CPMT_MR_FRR 0x0008 /* Free Run / Restart */
85 #define CPMT_MR_ORI 0x0010 /* Out. Reference Interrupt En. */
86 #define CPMT_MR_OM 0x0020 /* Output Mode */
87 #define CPMT_MR_CE_DIS 0x0000 /* Capture/Interrupt disabled */
88 #define CPMT_MR_CE_RISE 0x0040 /* Capt./Interr. on rising TIN */
89 #define CPMT_MR_CE_FALL 0x0080 /* Capt./Interr. on falling TIN */
90 #define CPMT_MR_CE_ANY 0x00C0 /* Capt./Interr. on any TIN edge*/
94 * which CPM timer to use - index starts at 0 (= timer 1)
96 #define TID_TIMER_ID 0 /* use CPM timer 1 */
98 void setPeriod (tid_8xx_cpmtimer_t
*hwp
, ulong interval
);
100 static const char usage
[] = "\n[q, b, e, ?] ";
102 int timer (int argc
, char * const argv
[])
104 cpmtimer8xx_t
*cpmtimerp
; /* Pointer to the CPM Timer structure */
105 tid_8xx_cpmtimer_t hw
;
106 tid_8xx_cpmtimer_t
*hwp
= &hw
;
112 /* Pointer to CPM Timer structure */
113 cpmtimerp
= &((immap_t
*) gd
->bd
->bi_immr_base
)->im_cpmtimer
;
115 printf ("TIMERS=0x%x\n", (unsigned) cpmtimerp
);
117 /* Initialize pointers depending on which timer we use */
118 switch (TID_TIMER_ID
) {
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
;
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
;
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
;
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
;
153 hwp
->tgcrp
= &cpmtimerp
->cpmt_tgcr
;
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",
159 (unsigned) hwp
->tgcrp
,
160 (unsigned) hwp
->tmrp
,
161 (unsigned) hwp
->trrp
,
162 (unsigned) hwp
->tcrp
,
163 (unsigned) hwp
->tcnp
,
168 *hwp
->tgcrp
&= ~(CPMT_GCR_MASK
<< TID_TIMER_ID
);
170 /* clear all events */
171 *hwp
->terp
= (CPMT_EVENT_CAP
| CPMT_EVENT_REF
);
175 while ((c
= getc()) != 'q') {
178 setPeriod (hwp
, TIMER_PERIOD
); /* Set period and start ticking */
180 /* Install interrupt handler (enable timer in CIMR) */
181 install_hdlr (hwp
->cpm_vec
, timer_handler
, hwp
);
183 printf ("Enabling timer\n");
186 *hwp
->tgcrp
|= (CPMT_GCR_RST
<< TID_TIMER_ID
);
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
196 } else if (c
== 'e') {
198 printf ("Stopping timer\n");
200 *hwp
->tgcrp
&= ~(CPMT_GCR_MASK
<< TID_TIMER_ID
);
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
210 /* Uninstall interrupt handler */
211 free_hdlr (hwp
->cpm_vec
);
213 } else if (c
== '?') {
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
;
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
225 printf ("SIUMCR=0x%08lx, SYPCR=0x%08lx,"
226 " SIMASK=0x%08lx, SIPEND=0x%08lx\n",
233 printf ("CIMR=0x%08lx, CICR=0x%08lx, CIPR=0x%08lx\n",
240 printf ("\nEnter: q - quit, b - start timer, e - stop timer, ? - get status\n");
245 printf ("Stopping timer\n");
246 *hwp
->tgcrp
&= ~(CPMT_GCR_MASK
<< TID_TIMER_ID
);
247 free_hdlr (hwp
->cpm_vec
);
254 /* Set period in microseconds and start.
255 * Truncate to maximum period if more than this is requested - but warn about it.
258 void setPeriod (tid_8xx_cpmtimer_t
*hwp
, ulong interval
)
260 unsigned short prescaler
;
263 printf ("Set interval %ld us\n", interval
);
265 /* Warn if requesting longer period than possible */
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
;
272 * Check if we want to use clock divider:
273 * Since the reference counter can be incremented only in integer steps,
274 * we try to keep it as big as possible to allow the resulting period to be
275 * as precise as possible.
277 /* prescaler, enable interrupt, restart after ref count is reached */
278 prescaler
= (ushort
) ((CPMT_PRESCALER
- 1) << 8) |
282 ticks
= ((ulong
) CLOCKRATE
* interval
);
284 if (ticks
> CPMT_MAX_TICKS
) {
285 ticks
/= CPMT_CLOCK_DIV
;
286 prescaler
|= CPMT_MR_ICLK_CLKDIV
; /* use system clock divided by 16 */
288 prescaler
|= CPMT_MR_ICLK_CLK
; /* use system clock without divider */
292 printf ("clock/%d, prescale factor %d, reference %ld, ticks %ld\n",
293 (ticks
> CPMT_MAX_TICKS
) ? CPMT_CLOCK_DIV
: 1,
295 (ticks
/ CPMT_PRESCALER
),
300 /* set prescaler register */
301 *hwp
->tmrp
= prescaler
;
303 /* clear timer counter */
306 /* set reference register */
307 *hwp
->trrp
= (unsigned short) (ticks
/ CPMT_PRESCALER
);
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
319 * Handler for CPMVEC_TIMER1 interrupt
322 void timer_handler (void *arg
)
324 tid_8xx_cpmtimer_t
*hwp
= (tid_8xx_cpmtimer_t
*)arg
;
326 /* printf ("** TER1=%04x ** ", *hwp->terp); */
328 /* just for demonstration */
331 /* clear all possible events: Ref. and Cap. */
332 *hwp
->terp
= (CPMT_EVENT_CAP
| CPMT_EVENT_REF
);