• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright 2013 Freescale Semiconductor, Inc.
4  */
5 
6 #include <common.h>
7 #include <time.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 
tick_to_time(unsigned long long tick)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 
us_to_tick(unsigned long long usec)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 
timer_init(void)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 
get_ticks(void)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 
get_timer(ulong base)61 ulong get_timer(ulong base)
62 {
63 	return tick_to_time(get_ticks()) - base;
64 }
65 
66 /* delay x useconds AND preserve advance timstamp value */
__udelay(unsigned long usec)67 void __udelay(unsigned long usec)
68 {
69 	unsigned long long start;
70 	ulong tmo;
71 
72 	start = get_ticks();			/* get current timestamp */
73 	tmo = us_to_tick(usec);			/* convert usecs to ticks */
74 	while ((get_ticks() - start) < tmo)
75 		;				/* loop till time has passed */
76 }
77 
78 /*
79  * This function is derived from PowerPC code (timebase clock frequency).
80  * On ARM it returns the number of timer ticks per second.
81  */
get_tbclk(void)82 ulong get_tbclk(void)
83 {
84 	return mxc_get_clock(MXC_IPG_CLK);
85 }
86