1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright 2014 Freescale Semiconductor, Inc.
11 #include <asm/arch/immap_ls102xa.h>
12 #include <asm/arch/clock.h>
13 #include <linux/delay.h>
15 DECLARE_GLOBAL_DATA_PTR;
18 * This function is intended for SHORT delays only.
19 * It will overflow at around 10 seconds @ 400MHz,
20 * or 20 seconds @ 200MHz.
22 unsigned long usec2ticks(unsigned long usec)
27 ticks = ((usec * (get_tbclk()/1000)) + 500) / 1000;
29 ticks = ((usec / 10) * (get_tbclk() / 100000));
34 static inline unsigned long long tick_to_time(unsigned long long tick)
38 asm volatile("mrc p15, 0, %0, c14, c0, 0" : "=r" (freq));
40 tick *= CONFIG_SYS_HZ;
46 static inline unsigned long long us_to_tick(unsigned long long usec)
50 asm volatile("mrc p15, 0, %0, c14, c0, 0" : "=r" (freq));
52 usec = usec * freq + 999999;
53 do_div(usec, 1000000);
60 struct sctr_regs *sctr = (struct sctr_regs *)SCTR_BASE_ADDR;
61 unsigned long ctrl, freq;
62 unsigned long long val;
64 /* Enable System Counter */
65 writel(SYS_COUNTER_CTRL_ENABLE, &sctr->cntcr);
67 freq = COUNTER_FREQUENCY;
68 asm("mcr p15, 0, %0, c14, c0, 0" : : "r" (freq));
70 /* Set PL1 Physical Timer Ctrl */
71 ctrl = ARCH_TIMER_CTRL_ENABLE;
72 asm("mcr p15, 0, %0, c14, c2, 1" : : "r" (ctrl));
74 /* Set PL1 Physical Comp Value */
76 asm("mcrr p15, 2, %Q0, %R0, c14" : : "r" (val));
84 unsigned long long get_ticks(void)
86 unsigned long long now;
88 asm("mrrc p15, 0, %Q0, %R0, c14" : "=r" (now));
90 gd->arch.tbl = (unsigned long)(now & 0xffffffff);
91 gd->arch.tbu = (unsigned long)(now >> 32);
96 unsigned long get_timer(ulong base)
98 return tick_to_time(get_ticks()) - base;
101 /* delay x useconds and preserve advance timstamp value */
102 void __udelay(unsigned long usec)
104 unsigned long long start;
107 start = get_ticks(); /* get current timestamp */
108 tmo = us_to_tick(usec); /* convert usecs to ticks */
110 while ((get_ticks() - start) < tmo)
111 ; /* loop till time has passed */
115 * This function is derived from PowerPC code (timebase clock frequency).
116 * On ARM it returns the number of timer ticks per second.
118 unsigned long get_tbclk(void)
122 asm volatile("mrc p15, 0, %0, c14, c0, 0" : "=r" (freq));