]> git.ipfire.org Git - thirdparty/u-boot.git/blame - arch/arm/mach-imx/syscounter.c
Convert CONFIG_SKIP_LOWLEVEL_INIT et al to Kconfig
[thirdparty/u-boot.git] / arch / arm / mach-imx / syscounter.c
CommitLineData
83d290c5 1// SPDX-License-Identifier: GPL-2.0+
69535741
AA
2/*
3 * Copyright (C) 2015 Freescale Semiconductor, Inc.
4 *
69535741
AA
5 * The file use ls102xa/timer.c as a reference.
6 */
7
8#include <common.h>
691d719d 9#include <init.h>
6887c5be 10#include <time.h>
401d1c4f 11#include <asm/global_data.h>
69535741
AA
12#include <asm/io.h>
13#include <div64.h>
14#include <asm/arch/imx-regs.h>
15#include <asm/arch/sys_proto.h>
552a848e 16#include <asm/mach-imx/syscounter.h>
c05ed00a 17#include <linux/delay.h>
69535741
AA
18
19DECLARE_GLOBAL_DATA_PTR;
20
21/*
22 * This function is intended for SHORT delays only.
23 * It will overflow at around 10 seconds @ 400MHz,
24 * or 20 seconds @ 200MHz.
25 */
26unsigned long usec2ticks(unsigned long usec)
27{
28 ulong ticks;
29
30 if (usec < 1000)
31 ticks = ((usec * (get_tbclk()/1000)) + 500) / 1000;
32 else
33 ticks = ((usec / 10) * (get_tbclk() / 100000));
34
35 return ticks;
36}
37
38static inline unsigned long long tick_to_time(unsigned long long tick)
39{
40 unsigned long freq;
41
42 asm volatile("mrc p15, 0, %0, c14, c0, 0" : "=r" (freq));
43
44 tick *= CONFIG_SYS_HZ;
45 do_div(tick, freq);
46
47 return tick;
48}
49
50static inline unsigned long long us_to_tick(unsigned long long usec)
51{
52 unsigned long freq;
53
54 asm volatile("mrc p15, 0, %0, c14, c0, 0" : "=r" (freq));
55
56 usec = usec * freq + 999999;
57 do_div(usec, 1000000);
58
59 return usec;
60}
61
a2ac2b96 62#if !CONFIG_IS_ENABLED(SKIP_LOWLEVEL_INIT)
69535741
AA
63int timer_init(void)
64{
65 struct sctr_regs *sctr = (struct sctr_regs *)SCTR_BASE_ADDR;
66 unsigned long val, freq;
67
68 freq = CONFIG_SC_TIMER_CLK;
314d9f7e 69 asm volatile("mcr p15, 0, %0, c14, c0, 0" : : "r" (freq));
69535741
AA
70
71 writel(freq, &sctr->cntfid0);
72
73 /* Enable system counter */
74 val = readl(&sctr->cntcr);
75 val &= ~(SC_CNTCR_FREQ0 | SC_CNTCR_FREQ1);
76 val |= SC_CNTCR_FREQ0 | SC_CNTCR_ENABLE | SC_CNTCR_HDBG;
77 writel(val, &sctr->cntcr);
78
79 gd->arch.tbl = 0;
80 gd->arch.tbu = 0;
81
82 return 0;
83}
be277c3a 84#endif
69535741
AA
85
86unsigned long long get_ticks(void)
87{
88 unsigned long long now;
89
314d9f7e 90 asm volatile("mrrc p15, 0, %Q0, %R0, c14" : "=r" (now));
69535741
AA
91
92 gd->arch.tbl = (unsigned long)(now & 0xffffffff);
93 gd->arch.tbu = (unsigned long)(now >> 32);
94
95 return now;
96}
97
69535741
AA
98ulong get_timer(ulong base)
99{
6180ea7e 100 return tick_to_time(get_ticks()) - base;
69535741
AA
101}
102
103void __udelay(unsigned long usec)
104{
105 unsigned long long tmp;
106 ulong tmo;
107
108 tmo = us_to_tick(usec);
109 tmp = get_ticks() + tmo; /* get current timestamp */
110
111 while (get_ticks() < tmp) /* loop till event */
112 /*NOP*/;
113}
114
115/*
116 * This function is derived from PowerPC code (timebase clock frequency).
117 * On ARM it returns the number of timer ticks per second.
118 */
119ulong get_tbclk(void)
120{
121 unsigned long freq;
122
123 asm volatile("mrc p15, 0, %0, c14, c0, 0" : "=r" (freq));
124
125 return freq;
126}