1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2015 Freescale Semiconductor, Inc.
5 * The file use ls102xa/timer.c as a reference.
13 #include <asm/arch/imx-regs.h>
14 #include <asm/arch/sys_proto.h>
15 #include <asm/mach-imx/syscounter.h>
16 #include <linux/delay.h>
18 DECLARE_GLOBAL_DATA_PTR;
21 * This function is intended for SHORT delays only.
22 * It will overflow at around 10 seconds @ 400MHz,
23 * or 20 seconds @ 200MHz.
25 unsigned long usec2ticks(unsigned long usec)
30 ticks = ((usec * (get_tbclk()/1000)) + 500) / 1000;
32 ticks = ((usec / 10) * (get_tbclk() / 100000));
37 static inline unsigned long long tick_to_time(unsigned long long tick)
41 asm volatile("mrc p15, 0, %0, c14, c0, 0" : "=r" (freq));
43 tick *= CONFIG_SYS_HZ;
49 static inline unsigned long long us_to_tick(unsigned long long usec)
53 asm volatile("mrc p15, 0, %0, c14, c0, 0" : "=r" (freq));
55 usec = usec * freq + 999999;
56 do_div(usec, 1000000);
61 #ifndef CONFIG_SKIP_LOWLEVEL_INIT
64 struct sctr_regs *sctr = (struct sctr_regs *)SCTR_BASE_ADDR;
65 unsigned long val, freq;
67 freq = CONFIG_SC_TIMER_CLK;
68 asm volatile("mcr p15, 0, %0, c14, c0, 0" : : "r" (freq));
70 writel(freq, &sctr->cntfid0);
72 /* Enable system counter */
73 val = readl(&sctr->cntcr);
74 val &= ~(SC_CNTCR_FREQ0 | SC_CNTCR_FREQ1);
75 val |= SC_CNTCR_FREQ0 | SC_CNTCR_ENABLE | SC_CNTCR_HDBG;
76 writel(val, &sctr->cntcr);
85 unsigned long long get_ticks(void)
87 unsigned long long now;
89 asm volatile("mrrc p15, 0, %Q0, %R0, c14" : "=r" (now));
91 gd->arch.tbl = (unsigned long)(now & 0xffffffff);
92 gd->arch.tbu = (unsigned long)(now >> 32);
97 ulong get_timer(ulong base)
99 return tick_to_time(get_ticks()) - base;
102 void __udelay(unsigned long usec)
104 unsigned long long tmp;
107 tmo = us_to_tick(usec);
108 tmp = get_ticks() + tmo; /* get current timestamp */
110 while (get_ticks() < tmp) /* loop till event */
115 * This function is derived from PowerPC code (timebase clock frequency).
116 * On ARM it returns the number of timer ticks per second.
118 ulong get_tbclk(void)
122 asm volatile("mrc p15, 0, %0, c14, c0, 0" : "=r" (freq));