1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright 2013 Freescale Semiconductor, Inc.
11 #include <asm/arch/imx-regs.h>
12 #include <asm/arch/clock.h>
13 #include <linux/delay.h>
15 static struct pit_reg *cur_pit = (struct pit_reg *)PIT_BASE_ADDR;
17 DECLARE_GLOBAL_DATA_PTR;
19 #define TIMER_LOAD_VAL 0xffffffff
21 static inline unsigned long long tick_to_time(unsigned long long tick)
23 tick *= CONFIG_SYS_HZ;
24 do_div(tick, mxc_get_clock(MXC_IPG_CLK));
29 static inline unsigned long long us_to_tick(unsigned long long usec)
31 usec = usec * mxc_get_clock(MXC_IPG_CLK) + 999999;
32 do_div(usec, 1000000);
39 __raw_writel(0, &cur_pit->mcr);
41 __raw_writel(TIMER_LOAD_VAL, &cur_pit->ldval1);
42 __raw_writel(0, &cur_pit->tctrl1);
43 __raw_writel(1, &cur_pit->tctrl1);
51 unsigned long long get_ticks(void)
53 ulong now = TIMER_LOAD_VAL - __raw_readl(&cur_pit->cval1);
55 /* increment tbu if tbl has rolled over */
56 if (now < gd->arch.tbl)
60 return (((unsigned long long)gd->arch.tbu) << 32) | gd->arch.tbl;
63 ulong get_timer(ulong base)
65 return tick_to_time(get_ticks()) - base;
68 /* delay x useconds AND preserve advance timstamp value */
69 void __udelay(unsigned long usec)
71 unsigned long long start;
74 start = get_ticks(); /* get current timestamp */
75 tmo = us_to_tick(usec); /* convert usecs to ticks */
76 while ((get_ticks() - start) < tmo)
77 ; /* loop till time has passed */
81 * This function is derived from PowerPC code (timebase clock frequency).
82 * On ARM it returns the number of timer ticks per second.
86 return mxc_get_clock(MXC_IPG_CLK);