xref: /openbmc/u-boot/arch/arm/cpu/armv7/vf610/timer.c (revision 23ff8633)
1 /*
2  * Copyright 2013 Freescale Semiconductor, Inc.
3  *
4  * SPDX-License-Identifier:	GPL-2.0+
5  */
6 
7 #include <common.h>
8 #include <asm/io.h>
9 #include <div64.h>
10 #include <asm/arch/imx-regs.h>
11 #include <asm/arch/clock.h>
12 
13 static struct pit_reg *cur_pit = (struct pit_reg *)PIT_BASE_ADDR;
14 
15 DECLARE_GLOBAL_DATA_PTR;
16 
17 #define TIMER_LOAD_VAL	0xffffffff
18 
19 static inline unsigned long long tick_to_time(unsigned long long tick)
20 {
21 	tick *= CONFIG_SYS_HZ;
22 	do_div(tick, mxc_get_clock(MXC_IPG_CLK));
23 
24 	return tick;
25 }
26 
27 static inline unsigned long long us_to_tick(unsigned long long usec)
28 {
29 	usec = usec * mxc_get_clock(MXC_IPG_CLK)  + 999999;
30 	do_div(usec, 1000000);
31 
32 	return usec;
33 }
34 
35 int timer_init(void)
36 {
37 	__raw_writel(0, &cur_pit->mcr);
38 
39 	__raw_writel(TIMER_LOAD_VAL, &cur_pit->ldval1);
40 	__raw_writel(0, &cur_pit->tctrl1);
41 	__raw_writel(1, &cur_pit->tctrl1);
42 
43 	gd->arch.tbl = 0;
44 	gd->arch.tbu = 0;
45 
46 	return 0;
47 }
48 
49 unsigned long long get_ticks(void)
50 {
51 	ulong now = TIMER_LOAD_VAL - __raw_readl(&cur_pit->cval1);
52 
53 	/* increment tbu if tbl has rolled over */
54 	if (now < gd->arch.tbl)
55 		gd->arch.tbu++;
56 	gd->arch.tbl = now;
57 
58 	return (((unsigned long long)gd->arch.tbu) << 32) | gd->arch.tbl;
59 }
60 
61 ulong get_timer_masked(void)
62 {
63 	return tick_to_time(get_ticks());
64 }
65 
66 ulong get_timer(ulong base)
67 {
68 	return get_timer_masked() - base;
69 }
70 
71 /* delay x useconds AND preserve advance timstamp value */
72 void __udelay(unsigned long usec)
73 {
74 	unsigned long long start;
75 	ulong tmo;
76 
77 	start = get_ticks();			/* get current timestamp */
78 	tmo = us_to_tick(usec);			/* convert usecs to ticks */
79 	while ((get_ticks() - start) < tmo)
80 		;				/* loop till time has passed */
81 }
82 
83 /*
84  * This function is derived from PowerPC code (timebase clock frequency).
85  * On ARM it returns the number of timer ticks per second.
86  */
87 ulong get_tbclk(void)
88 {
89 	return mxc_get_clock(MXC_IPG_CLK);
90 }
91