]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - arch/arm/cpu/armv7/vf610/timer.c
Merge branch 'master' of git://git.denx.de/u-boot-arm
[karo-tx-uboot.git] / arch / arm / cpu / armv7 / vf610 / timer.c
1 /*
2  * Copyright 2013 Freescale Semiconductor, Inc.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License as
6  * published by the Free Software Foundation; either version 2 of
7  * the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
17  * MA 02111-1307 USA
18  */
19
20 #include <common.h>
21 #include <asm/io.h>
22 #include <div64.h>
23 #include <asm/arch/imx-regs.h>
24 #include <asm/arch/clock.h>
25
26 static struct pit_reg *cur_pit = (struct pit_reg *)PIT_BASE_ADDR;
27
28 DECLARE_GLOBAL_DATA_PTR;
29
30 #define TIMER_LOAD_VAL  0xffffffff
31
32 static inline unsigned long long tick_to_time(unsigned long long tick)
33 {
34         tick *= CONFIG_SYS_HZ;
35         do_div(tick, mxc_get_clock(MXC_IPG_CLK));
36
37         return tick;
38 }
39
40 static inline unsigned long long us_to_tick(unsigned long long usec)
41 {
42         usec = usec * mxc_get_clock(MXC_IPG_CLK)  + 999999;
43         do_div(usec, 1000000);
44
45         return usec;
46 }
47
48 int timer_init(void)
49 {
50         __raw_writel(0, &cur_pit->mcr);
51
52         __raw_writel(TIMER_LOAD_VAL, &cur_pit->ldval1);
53         __raw_writel(0, &cur_pit->tctrl1);
54         __raw_writel(1, &cur_pit->tctrl1);
55
56         gd->arch.tbl = 0;
57         gd->arch.tbu = 0;
58
59         return 0;
60 }
61
62 unsigned long long get_ticks(void)
63 {
64         ulong now = TIMER_LOAD_VAL - __raw_readl(&cur_pit->cval1);
65
66         /* increment tbu if tbl has rolled over */
67         if (now < gd->arch.tbl)
68                 gd->arch.tbu++;
69         gd->arch.tbl = now;
70
71         return (((unsigned long long)gd->arch.tbu) << 32) | gd->arch.tbl;
72 }
73
74 ulong get_timer_masked(void)
75 {
76         return tick_to_time(get_ticks());
77 }
78
79 ulong get_timer(ulong base)
80 {
81         return get_timer_masked() - base;
82 }
83
84 /* delay x useconds AND preserve advance timstamp value */
85 void __udelay(unsigned long usec)
86 {
87         unsigned long long start;
88         ulong tmo;
89
90         start = get_ticks();                    /* get current timestamp */
91         tmo = us_to_tick(usec);                 /* convert usecs to ticks */
92         while ((get_ticks() - start) < tmo)
93                 ;                               /* loop till time has passed */
94 }
95
96 /*
97  * This function is derived from PowerPC code (timebase clock frequency).
98  * On ARM it returns the number of timer ticks per second.
99  */
100 ulong get_tbclk(void)
101 {
102         return mxc_get_clock(MXC_IPG_CLK);
103 }