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