blob: 7da43c32e0db14543f9ffc50e8309efc149b7fa0 [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 Glass691d7192020-05-10 11:40:02 -06009#include <init.h>
Simon Glass6887c5b2019-11-14 12:57:26 -070010#include <time.h>
Adrian Alonso69535742015-09-02 13:54:16 -050011#include <asm/io.h>
12#include <div64.h>
13#include <asm/arch/imx-regs.h>
14#include <asm/arch/sys_proto.h>
Stefano Babic552a8482017-06-29 10:16:06 +020015#include <asm/mach-imx/syscounter.h>
Simon Glassc05ed002020-05-10 11:40:11 -060016#include <linux/delay.h>
Adrian Alonso69535742015-09-02 13:54:16 -050017
18DECLARE_GLOBAL_DATA_PTR;
19
20/*
21 * This function is intended for SHORT delays only.
22 * It will overflow at around 10 seconds @ 400MHz,
23 * or 20 seconds @ 200MHz.
24 */
25unsigned long usec2ticks(unsigned long usec)
26{
27 ulong ticks;
28
29 if (usec < 1000)
30 ticks = ((usec * (get_tbclk()/1000)) + 500) / 1000;
31 else
32 ticks = ((usec / 10) * (get_tbclk() / 100000));
33
34 return ticks;
35}
36
37static inline unsigned long long tick_to_time(unsigned long long tick)
38{
39 unsigned long freq;
40
41 asm volatile("mrc p15, 0, %0, c14, c0, 0" : "=r" (freq));
42
43 tick *= CONFIG_SYS_HZ;
44 do_div(tick, freq);
45
46 return tick;
47}
48
49static inline unsigned long long us_to_tick(unsigned long long usec)
50{
51 unsigned long freq;
52
53 asm volatile("mrc p15, 0, %0, c14, c0, 0" : "=r" (freq));
54
55 usec = usec * freq + 999999;
56 do_div(usec, 1000000);
57
58 return usec;
59}
60
Rui Miguel Silvabe277c32018-09-05 11:56:05 +010061#ifndef CONFIG_SKIP_LOWLEVEL_INIT
Adrian Alonso69535742015-09-02 13:54:16 -050062int timer_init(void)
63{
64 struct sctr_regs *sctr = (struct sctr_regs *)SCTR_BASE_ADDR;
65 unsigned long val, freq;
66
67 freq = CONFIG_SC_TIMER_CLK;
Yasushi SHOJI314d9f72018-03-08 13:21:10 +090068 asm volatile("mcr p15, 0, %0, c14, c0, 0" : : "r" (freq));
Adrian Alonso69535742015-09-02 13:54:16 -050069
70 writel(freq, &sctr->cntfid0);
71
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);
77
78 gd->arch.tbl = 0;
79 gd->arch.tbu = 0;
80
81 return 0;
82}
Rui Miguel Silvabe277c32018-09-05 11:56:05 +010083#endif
Adrian Alonso69535742015-09-02 13:54:16 -050084
85unsigned long long get_ticks(void)
86{
87 unsigned long long now;
88
Yasushi SHOJI314d9f72018-03-08 13:21:10 +090089 asm volatile("mrrc p15, 0, %Q0, %R0, c14" : "=r" (now));
Adrian Alonso69535742015-09-02 13:54:16 -050090
91 gd->arch.tbl = (unsigned long)(now & 0xffffffff);
92 gd->arch.tbu = (unsigned long)(now >> 32);
93
94 return now;
95}
96
Adrian Alonso69535742015-09-02 13:54:16 -050097ulong get_timer(ulong base)
98{
Patrick Delaunay6180ea72018-10-05 11:33:52 +020099 return tick_to_time(get_ticks()) - base;
Adrian Alonso69535742015-09-02 13:54:16 -0500100}
101
102void __udelay(unsigned long usec)
103{
104 unsigned long long tmp;
105 ulong tmo;
106
107 tmo = us_to_tick(usec);
108 tmp = get_ticks() + tmo; /* get current timestamp */
109
110 while (get_ticks() < tmp) /* loop till event */
111 /*NOP*/;
112}
113
114/*
115 * This function is derived from PowerPC code (timebase clock frequency).
116 * On ARM it returns the number of timer ticks per second.
117 */
118ulong get_tbclk(void)
119{
120 unsigned long freq;
121
122 asm volatile("mrc p15, 0, %0, c14, c0, 0" : "=r" (freq));
123
124 return freq;
125}