blob: 2a32b30999fec7d4e8bcf4c4c1b4d6099fff3175 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Wang Huan8ce6bec2014-09-05 13:52:34 +08002/*
3 * Copyright 2014 Freescale Semiconductor, Inc.
Wang Huan8ce6bec2014-09-05 13:52:34 +08004 */
5
6#include <common.h>
Simon Glass97589732020-05-10 11:40:02 -06007#include <init.h>
Simon Glass45c78902019-11-14 12:57:26 -07008#include <time.h>
Wang Huan8ce6bec2014-09-05 13:52:34 +08009#include <asm/io.h>
10#include <div64.h>
11#include <asm/arch/immap_ls102xa.h>
12#include <asm/arch/clock.h>
Simon Glassdbd79542020-05-10 11:40:11 -060013#include <linux/delay.h>
Wang Huan8ce6bec2014-09-05 13:52:34 +080014
15DECLARE_GLOBAL_DATA_PTR;
16
17/*
18 * This function is intended for SHORT delays only.
19 * It will overflow at around 10 seconds @ 400MHz,
20 * or 20 seconds @ 200MHz.
21 */
22unsigned long usec2ticks(unsigned long usec)
23{
24 ulong ticks;
25
26 if (usec < 1000)
27 ticks = ((usec * (get_tbclk()/1000)) + 500) / 1000;
28 else
29 ticks = ((usec / 10) * (get_tbclk() / 100000));
30
31 return ticks;
32}
33
34static inline unsigned long long tick_to_time(unsigned long long tick)
35{
36 unsigned long freq;
37
38 asm volatile("mrc p15, 0, %0, c14, c0, 0" : "=r" (freq));
39
40 tick *= CONFIG_SYS_HZ;
41 do_div(tick, freq);
42
43 return tick;
44}
45
46static inline unsigned long long us_to_tick(unsigned long long usec)
47{
48 unsigned long freq;
49
50 asm volatile("mrc p15, 0, %0, c14, c0, 0" : "=r" (freq));
51
52 usec = usec * freq + 999999;
53 do_div(usec, 1000000);
54
55 return usec;
56}
57
58int timer_init(void)
59{
60 struct sctr_regs *sctr = (struct sctr_regs *)SCTR_BASE_ADDR;
Alison Wang25c9dc52015-07-15 15:13:05 +080061 unsigned long ctrl, freq;
62 unsigned long long val;
Wang Huan8ce6bec2014-09-05 13:52:34 +080063
64 /* Enable System Counter */
65 writel(SYS_COUNTER_CTRL_ENABLE, &sctr->cntcr);
66
Andre Przywara9d095c32017-02-16 01:20:20 +000067 freq = COUNTER_FREQUENCY;
Wang Huan8ce6bec2014-09-05 13:52:34 +080068 asm("mcr p15, 0, %0, c14, c0, 0" : : "r" (freq));
69
70 /* Set PL1 Physical Timer Ctrl */
71 ctrl = ARCH_TIMER_CTRL_ENABLE;
72 asm("mcr p15, 0, %0, c14, c2, 1" : : "r" (ctrl));
73
74 /* Set PL1 Physical Comp Value */
75 val = TIMER_COMP_VAL;
76 asm("mcrr p15, 2, %Q0, %R0, c14" : : "r" (val));
77
78 gd->arch.tbl = 0;
79 gd->arch.tbu = 0;
80
81 return 0;
82}
83
84unsigned long long get_ticks(void)
85{
86 unsigned long long now;
87
88 asm("mrrc p15, 0, %Q0, %R0, c14" : "=r" (now));
89
90 gd->arch.tbl = (unsigned long)(now & 0xffffffff);
91 gd->arch.tbu = (unsigned long)(now >> 32);
92
93 return now;
94}
95
Wang Huan8ce6bec2014-09-05 13:52:34 +080096unsigned long get_timer(ulong base)
97{
Patrick Delaunay9858a602018-10-05 11:33:52 +020098 return tick_to_time(get_ticks()) - base;
Wang Huan8ce6bec2014-09-05 13:52:34 +080099}
100
101/* delay x useconds and preserve advance timstamp value */
102void __udelay(unsigned long usec)
103{
104 unsigned long long start;
105 unsigned long tmo;
106
107 start = get_ticks(); /* get current timestamp */
108 tmo = us_to_tick(usec); /* convert usecs to ticks */
109
110 while ((get_ticks() - start) < tmo)
111 ; /* loop till time has passed */
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 */
118unsigned long 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}