blob: 5a292c396433308ffc98155429afd2ffa50eb633 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Adrian Alonso69535742015-09-02 13:54:16 -05002/*
3 * Copyright (C) 2015 Freescale Semiconductor, Inc.
4 *
Adrian Alonso69535742015-09-02 13:54:16 -05005 * The file use ls102xa/timer.c as a reference.
6 */
7
8#include <common.h>
Simon Glass6887c5b2019-11-14 12:57:26 -07009#include <time.h>
Adrian Alonso69535742015-09-02 13:54:16 -050010#include <asm/io.h>
11#include <div64.h>
12#include <asm/arch/imx-regs.h>
13#include <asm/arch/sys_proto.h>
Stefano Babic552a8482017-06-29 10:16:06 +020014#include <asm/mach-imx/syscounter.h>
Adrian Alonso69535742015-09-02 13:54:16 -050015
16DECLARE_GLOBAL_DATA_PTR;
17
18/*
19 * This function is intended for SHORT delays only.
20 * It will overflow at around 10 seconds @ 400MHz,
21 * or 20 seconds @ 200MHz.
22 */
23unsigned long usec2ticks(unsigned long usec)
24{
25 ulong ticks;
26
27 if (usec < 1000)
28 ticks = ((usec * (get_tbclk()/1000)) + 500) / 1000;
29 else
30 ticks = ((usec / 10) * (get_tbclk() / 100000));
31
32 return ticks;
33}
34
35static inline unsigned long long tick_to_time(unsigned long long tick)
36{
37 unsigned long freq;
38
39 asm volatile("mrc p15, 0, %0, c14, c0, 0" : "=r" (freq));
40
41 tick *= CONFIG_SYS_HZ;
42 do_div(tick, freq);
43
44 return tick;
45}
46
47static inline unsigned long long us_to_tick(unsigned long long usec)
48{
49 unsigned long freq;
50
51 asm volatile("mrc p15, 0, %0, c14, c0, 0" : "=r" (freq));
52
53 usec = usec * freq + 999999;
54 do_div(usec, 1000000);
55
56 return usec;
57}
58
Rui Miguel Silvabe277c32018-09-05 11:56:05 +010059#ifndef CONFIG_SKIP_LOWLEVEL_INIT
Adrian Alonso69535742015-09-02 13:54:16 -050060int timer_init(void)
61{
62 struct sctr_regs *sctr = (struct sctr_regs *)SCTR_BASE_ADDR;
63 unsigned long val, freq;
64
65 freq = CONFIG_SC_TIMER_CLK;
Yasushi SHOJI314d9f72018-03-08 13:21:10 +090066 asm volatile("mcr p15, 0, %0, c14, c0, 0" : : "r" (freq));
Adrian Alonso69535742015-09-02 13:54:16 -050067
68 writel(freq, &sctr->cntfid0);
69
70 /* Enable system counter */
71 val = readl(&sctr->cntcr);
72 val &= ~(SC_CNTCR_FREQ0 | SC_CNTCR_FREQ1);
73 val |= SC_CNTCR_FREQ0 | SC_CNTCR_ENABLE | SC_CNTCR_HDBG;
74 writel(val, &sctr->cntcr);
75
76 gd->arch.tbl = 0;
77 gd->arch.tbu = 0;
78
79 return 0;
80}
Rui Miguel Silvabe277c32018-09-05 11:56:05 +010081#endif
Adrian Alonso69535742015-09-02 13:54:16 -050082
83unsigned long long get_ticks(void)
84{
85 unsigned long long now;
86
Yasushi SHOJI314d9f72018-03-08 13:21:10 +090087 asm volatile("mrrc p15, 0, %Q0, %R0, c14" : "=r" (now));
Adrian Alonso69535742015-09-02 13:54:16 -050088
89 gd->arch.tbl = (unsigned long)(now & 0xffffffff);
90 gd->arch.tbu = (unsigned long)(now >> 32);
91
92 return now;
93}
94
Adrian Alonso69535742015-09-02 13:54:16 -050095ulong get_timer(ulong base)
96{
Patrick Delaunay6180ea72018-10-05 11:33:52 +020097 return tick_to_time(get_ticks()) - base;
Adrian Alonso69535742015-09-02 13:54:16 -050098}
99
100void __udelay(unsigned long usec)
101{
102 unsigned long long tmp;
103 ulong tmo;
104
105 tmo = us_to_tick(usec);
106 tmp = get_ticks() + tmo; /* get current timestamp */
107
108 while (get_ticks() < tmp) /* loop till event */
109 /*NOP*/;
110}
111
112/*
113 * This function is derived from PowerPC code (timebase clock frequency).
114 * On ARM it returns the number of timer ticks per second.
115 */
116ulong get_tbclk(void)
117{
118 unsigned long freq;
119
120 asm volatile("mrc p15, 0, %0, c14, c0, 0" : "=r" (freq));
121
122 return freq;
123}