46ade4ae86db19e39bf78ca865c8eb4767f7e594
[oweals/u-boot.git] / arch / arm / cpu / armv7 / ls102xa / timer.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright 2014 Freescale Semiconductor, Inc.
4  */
5
6 #include <common.h>
7 #include <init.h>
8 #include <time.h>
9 #include <asm/io.h>
10 #include <div64.h>
11 #include <asm/arch/immap_ls102xa.h>
12 #include <asm/arch/clock.h>
13
14 DECLARE_GLOBAL_DATA_PTR;
15
16 /*
17  * This function is intended for SHORT delays only.
18  * It will overflow at around 10 seconds @ 400MHz,
19  * or 20 seconds @ 200MHz.
20  */
21 unsigned long usec2ticks(unsigned long usec)
22 {
23         ulong ticks;
24
25         if (usec < 1000)
26                 ticks = ((usec * (get_tbclk()/1000)) + 500) / 1000;
27         else
28                 ticks = ((usec / 10) * (get_tbclk() / 100000));
29
30         return ticks;
31 }
32
33 static inline unsigned long long tick_to_time(unsigned long long tick)
34 {
35         unsigned long freq;
36
37         asm volatile("mrc p15, 0, %0, c14, c0, 0" : "=r" (freq));
38
39         tick *= CONFIG_SYS_HZ;
40         do_div(tick, freq);
41
42         return tick;
43 }
44
45 static inline unsigned long long us_to_tick(unsigned long long usec)
46 {
47         unsigned long freq;
48
49         asm volatile("mrc p15, 0, %0, c14, c0, 0" : "=r" (freq));
50
51         usec = usec * freq  + 999999;
52         do_div(usec, 1000000);
53
54         return usec;
55 }
56
57 int timer_init(void)
58 {
59         struct sctr_regs *sctr = (struct sctr_regs *)SCTR_BASE_ADDR;
60         unsigned long ctrl, freq;
61         unsigned long long val;
62
63         /* Enable System Counter */
64         writel(SYS_COUNTER_CTRL_ENABLE, &sctr->cntcr);
65
66         freq = COUNTER_FREQUENCY;
67         asm("mcr p15, 0, %0, c14, c0, 0" : : "r" (freq));
68
69         /* Set PL1 Physical Timer Ctrl */
70         ctrl = ARCH_TIMER_CTRL_ENABLE;
71         asm("mcr p15, 0, %0, c14, c2, 1" : : "r" (ctrl));
72
73         /* Set PL1 Physical Comp Value */
74         val = TIMER_COMP_VAL;
75         asm("mcrr p15, 2, %Q0, %R0, c14" : : "r" (val));
76
77         gd->arch.tbl = 0;
78         gd->arch.tbu = 0;
79
80         return 0;
81 }
82
83 unsigned long long get_ticks(void)
84 {
85         unsigned long long now;
86
87         asm("mrrc p15, 0, %Q0, %R0, c14" : "=r" (now));
88
89         gd->arch.tbl = (unsigned long)(now & 0xffffffff);
90         gd->arch.tbu = (unsigned long)(now >> 32);
91
92         return now;
93 }
94
95 unsigned long get_timer(ulong base)
96 {
97         return tick_to_time(get_ticks()) - base;
98 }
99
100 /* delay x useconds and preserve advance timstamp value */
101 void __udelay(unsigned long usec)
102 {
103         unsigned long long start;
104         unsigned long tmo;
105
106         start = get_ticks();                    /* get current timestamp */
107         tmo = us_to_tick(usec);                 /* convert usecs to ticks */
108
109         while ((get_ticks() - start) < tmo)
110                 ;                               /* loop till time has passed */
111 }
112
113 /*
114  * This function is derived from PowerPC code (timebase clock frequency).
115  * On ARM it returns the number of timer ticks per second.
116  */
117 unsigned long get_tbclk(void)
118 {
119         unsigned long freq;
120
121         asm volatile("mrc p15, 0, %0, c14, c0, 0" : "=r" (freq));
122
123         return freq;
124 }