blob: b42baa71506fa0b6ad9ad4c73f55215b3bcb2641 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Vipin KUMAR7cb16352010-01-15 19:15:43 +05302/*
3 * (C) Copyright 2009
4 * Vipin Kumar, ST Micoelectronics, vipin.kumar@st.com.
Vipin KUMAR7cb16352010-01-15 19:15:43 +05305 */
6
7#include <common.h>
Simon Glass97589732020-05-10 11:40:02 -06008#include <init.h>
Simon Glass495a5dc2019-11-14 12:57:30 -07009#include <time.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060010#include <asm/global_data.h>
Vipin KUMAR7cb16352010-01-15 19:15:43 +053011#include <asm/io.h>
12#include <asm/arch/hardware.h>
13#include <asm/arch/spr_gpt.h>
14#include <asm/arch/spr_misc.h>
Simon Glass6b9f0102020-05-10 11:40:06 -060015#include <asm/ptrace.h>
Simon Glassdbd79542020-05-10 11:40:11 -060016#include <linux/delay.h>
Vipin KUMAR7cb16352010-01-15 19:15:43 +053017
18#define GPT_RESOLUTION (CONFIG_SPEAR_HZ_CLOCK / CONFIG_SPEAR_HZ)
19#define READ_TIMER() (readl(&gpt_regs_p->count) & GPT_FREE_RUNNING)
20
21static struct gpt_regs *const gpt_regs_p =
22 (struct gpt_regs *)CONFIG_SPEAR_TIMERBASE;
23
24static struct misc_regs *const misc_regs_p =
25 (struct misc_regs *)CONFIG_SPEAR_MISCBASE;
26
Heiko Schocher5504dab2011-01-20 22:56:39 +000027DECLARE_GLOBAL_DATA_PTR;
28
Patrick Delaunay9858a602018-10-05 11:33:52 +020029static ulong get_timer_masked(void);
30
Simon Glass2655ee12012-12-13 20:48:34 +000031#define timestamp gd->arch.tbl
Simon Glassa848da52012-12-13 20:48:35 +000032#define lastdec gd->arch.lastinc
Vipin KUMAR7cb16352010-01-15 19:15:43 +053033
34int timer_init(void)
35{
36 u32 synth;
37
38 /* Prescaler setting */
39#if defined(CONFIG_SPEAR3XX)
40 writel(MISC_PRSC_CFG, &misc_regs_p->prsc2_clk_cfg);
41 synth = MISC_GPT4SYNTH;
42#elif defined(CONFIG_SPEAR600)
43 writel(MISC_PRSC_CFG, &misc_regs_p->prsc1_clk_cfg);
44 synth = MISC_GPT3SYNTH;
45#else
Simon Glass167ad902016-09-12 23:18:30 -060046# error Incorrect config. Can only be SPEAR{600|300|310|320}
Vipin KUMAR7cb16352010-01-15 19:15:43 +053047#endif
48
49 writel(readl(&misc_regs_p->periph_clk_cfg) | synth,
50 &misc_regs_p->periph_clk_cfg);
51
52 /* disable timers */
53 writel(GPT_PRESCALER_1 | GPT_MODE_AUTO_RELOAD, &gpt_regs_p->control);
54
55 /* load value for free running */
56 writel(GPT_FREE_RUNNING, &gpt_regs_p->compare);
57
58 /* auto reload, start timer */
59 writel(readl(&gpt_regs_p->control) | GPT_ENABLE, &gpt_regs_p->control);
60
Graeme Russ944a7fe2011-07-15 02:21:14 +000061 /* Reset the timer */
62 lastdec = READ_TIMER();
63 timestamp = 0;
Vipin KUMAR7cb16352010-01-15 19:15:43 +053064
65 return 0;
66}
67
68/*
69 * timer without interrupts
70 */
Vipin KUMAR7cb16352010-01-15 19:15:43 +053071ulong get_timer(ulong base)
72{
73 return (get_timer_masked() / GPT_RESOLUTION) - base;
74}
75
Vipin KUMAR7cb16352010-01-15 19:15:43 +053076void __udelay(unsigned long usec)
77{
78 ulong tmo;
79 ulong start = get_timer_masked();
80 ulong tenudelcnt = CONFIG_SPEAR_HZ_CLOCK / (1000 * 100);
81 ulong rndoff;
82
83 rndoff = (usec % 10) ? 1 : 0;
84
85 /* tenudelcnt timer tick gives 10 microsecconds delay */
86 tmo = ((usec / 10) + rndoff) * tenudelcnt;
87
88 while ((ulong) (get_timer_masked() - start) < tmo)
89 ;
90}
91
Patrick Delaunay9858a602018-10-05 11:33:52 +020092static ulong get_timer_masked(void)
Vipin KUMAR7cb16352010-01-15 19:15:43 +053093{
94 ulong now = READ_TIMER();
95
96 if (now >= lastdec) {
97 /* normal mode */
98 timestamp += now - lastdec;
99 } else {
100 /* we have an overflow ... */
101 timestamp += now + GPT_FREE_RUNNING - lastdec;
102 }
103 lastdec = now;
104
105 return timestamp;
106}
107
Vipin KUMAR7cb16352010-01-15 19:15:43 +0530108/*
109 * This function is derived from PowerPC code (read timebase as long long).
110 * On ARM it just returns the timer value.
111 */
112unsigned long long get_ticks(void)
113{
114 return get_timer(0);
115}
116
117/*
118 * This function is derived from PowerPC code (timebase clock frequency).
119 * On ARM it returns the number of timer ticks per second.
120 */
121ulong get_tbclk(void)
122{
123 return CONFIG_SPEAR_HZ;
124}