blob: f4962ec45f984de32ee5733e1efba8393a18adc1 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Prafulla Wadaskarc0c7a112010-10-12 16:31:40 +05302/*
3 * (C) Copyright 2010
4 * Marvell Semiconductor <www.marvell.com>
5 * Written-by: Prafulla Wadaskar <prafulla@marvell.com>
6 * Contributor: Mahavir Jain <mjain@marvell.com>
Prafulla Wadaskarc0c7a112010-10-12 16:31:40 +05307 */
8
9#include <common.h>
Simon Glassafb02152019-12-28 10:45:01 -070010#include <cpu_func.h>
Simon Glass495a5dc2019-11-14 12:57:30 -070011#include <time.h>
Lei Wen35b130c2011-10-18 19:50:48 +053012#include <asm/arch/cpu.h>
Prafulla Wadaskarc0c7a112010-10-12 16:31:40 +053013#include <asm/arch/armada100.h>
14
15/*
16 * Timer registers
17 * Refer Section A.6 in Datasheet
18 */
19struct armd1tmr_registers {
20 u32 clk_ctrl; /* Timer clk control reg */
21 u32 match[9]; /* Timer match registers */
22 u32 count[3]; /* Timer count registers */
23 u32 status[3];
24 u32 ie[3];
25 u32 preload[3]; /* Timer preload value */
26 u32 preload_ctrl[3];
27 u32 wdt_match_en;
28 u32 wdt_match_r;
29 u32 wdt_val;
30 u32 wdt_sts;
31 u32 icr[3];
32 u32 wdt_icr;
33 u32 cer; /* Timer count enable reg */
34 u32 cmr;
35 u32 ilr[3];
36 u32 wcr;
37 u32 wfar;
38 u32 wsar;
39 u32 cvwr;
40};
41
42#define TIMER 0 /* Use TIMER 0 */
43/* Each timer has 3 match registers */
44#define MATCH_CMP(x) ((3 * TIMER) + x)
45#define TIMER_LOAD_VAL 0xffffffff
46#define COUNT_RD_REQ 0x1
47
48DECLARE_GLOBAL_DATA_PTR;
Simon Glass2655ee12012-12-13 20:48:34 +000049/* Using gd->arch.tbu from timestamp and gd->arch.tbl for lastdec */
Prafulla Wadaskarc0c7a112010-10-12 16:31:40 +053050
51/* For preventing risk of instability in reading counter value,
52 * first set read request to register cvwr and then read same
53 * register after it captures counter value.
54 */
55ulong read_timer(void)
56{
57 struct armd1tmr_registers *armd1timers =
58 (struct armd1tmr_registers *) ARMD1_TIMER_BASE;
59 volatile int loop=100;
60
61 writel(COUNT_RD_REQ, &armd1timers->cvwr);
62 while (loop--);
63 return(readl(&armd1timers->cvwr));
64}
65
Patrick Delaunay9858a602018-10-05 11:33:52 +020066static ulong get_timer_masked(void)
Prafulla Wadaskarc0c7a112010-10-12 16:31:40 +053067{
68 ulong now = read_timer();
69
Simon Glass2655ee12012-12-13 20:48:34 +000070 if (now >= gd->arch.tbl) {
Prafulla Wadaskarc0c7a112010-10-12 16:31:40 +053071 /* normal mode */
Simon Glass2655ee12012-12-13 20:48:34 +000072 gd->arch.tbu += now - gd->arch.tbl;
Prafulla Wadaskarc0c7a112010-10-12 16:31:40 +053073 } else {
74 /* we have an overflow ... */
Simon Glass2655ee12012-12-13 20:48:34 +000075 gd->arch.tbu += now + TIMER_LOAD_VAL - gd->arch.tbl;
Prafulla Wadaskarc0c7a112010-10-12 16:31:40 +053076 }
Simon Glass2655ee12012-12-13 20:48:34 +000077 gd->arch.tbl = now;
Prafulla Wadaskarc0c7a112010-10-12 16:31:40 +053078
Simon Glass8ca15202012-12-13 20:48:33 +000079 return gd->arch.tbu;
Prafulla Wadaskarc0c7a112010-10-12 16:31:40 +053080}
81
Prafulla Wadaskarc0c7a112010-10-12 16:31:40 +053082ulong get_timer(ulong base)
83{
84 return ((get_timer_masked() / (CONFIG_SYS_HZ_CLOCK / 1000)) -
85 base);
86}
87
Prafulla Wadaskarc0c7a112010-10-12 16:31:40 +053088void __udelay(unsigned long usec)
89{
90 ulong delayticks;
91 ulong endtime;
92
93 delayticks = (usec * (CONFIG_SYS_HZ_CLOCK / 1000000));
94 endtime = get_timer_masked() + delayticks;
95
96 while (get_timer_masked() < endtime);
97}
98
99/*
100 * init the Timer
101 */
102int timer_init(void)
103{
104 struct armd1apb1_registers *apb1clkres =
105 (struct armd1apb1_registers *) ARMD1_APBC1_BASE;
106 struct armd1tmr_registers *armd1timers =
107 (struct armd1tmr_registers *) ARMD1_TIMER_BASE;
108
109 /* Enable Timer clock at 3.25 MHZ */
110 writel(APBC_APBCLK | APBC_FNCLK | APBC_FNCLKSEL(3), &apb1clkres->timers);
111
112 /* load value into timer */
113 writel(0x0, &armd1timers->clk_ctrl);
114 /* Use Timer 0 Match Resiger 0 */
115 writel(TIMER_LOAD_VAL, &armd1timers->match[MATCH_CMP(0)]);
116 /* Preload value is 0 */
117 writel(0x0, &armd1timers->preload[TIMER]);
118 /* Enable match comparator 0 for Timer 0 */
119 writel(0x1, &armd1timers->preload_ctrl[TIMER]);
120
121 /* Enable timer 0 */
122 writel(0x1, &armd1timers->cer);
Simon Glass2655ee12012-12-13 20:48:34 +0000123 /* init the gd->arch.tbu and gd->arch.tbl value */
124 gd->arch.tbl = read_timer();
Simon Glass8ca15202012-12-13 20:48:33 +0000125 gd->arch.tbu = 0;
Prafulla Wadaskarc0c7a112010-10-12 16:31:40 +0530126
127 return 0;
128}
129
130#define MPMU_APRR_WDTR (1<<4)
131#define TMR_WFAR 0xbaba /* WDT Register First key */
132#define TMP_WSAR 0xeb10 /* WDT Register Second key */
133
134/*
135 * This function uses internal Watchdog Timer
136 * based reset mechanism.
137 * Steps to write watchdog registers (protected access)
138 * 1. Write key value to TMR_WFAR reg.
139 * 2. Write key value to TMP_WSAR reg.
140 * 3. Perform write operation.
141 */
Simon Glassafb02152019-12-28 10:45:01 -0700142void reset_cpu(unsigned long ignored)
Prafulla Wadaskarc0c7a112010-10-12 16:31:40 +0530143{
144 struct armd1mpmu_registers *mpmu =
145 (struct armd1mpmu_registers *) ARMD1_MPMU_BASE;
146 struct armd1tmr_registers *armd1timers =
147 (struct armd1tmr_registers *) ARMD1_TIMER_BASE;
148 u32 val;
149
150 /* negate hardware reset to the WDT after system reset */
151 val = readl(&mpmu->aprr);
152 val = val | MPMU_APRR_WDTR;
153 writel(val, &mpmu->aprr);
154
155 /* reset/enable WDT clock */
156 writel(APBC_APBCLK | APBC_FNCLK | APBC_RST, &mpmu->wdtpcr);
157 readl(&mpmu->wdtpcr);
158 writel(APBC_APBCLK | APBC_FNCLK, &mpmu->wdtpcr);
159 readl(&mpmu->wdtpcr);
160
161 /* clear previous WDT status */
162 writel(TMR_WFAR, &armd1timers->wfar);
163 writel(TMP_WSAR, &armd1timers->wsar);
164 writel(0, &armd1timers->wdt_sts);
165
166 /* set match counter */
167 writel(TMR_WFAR, &armd1timers->wfar);
168 writel(TMP_WSAR, &armd1timers->wsar);
169 writel(0xf, &armd1timers->wdt_match_r);
170
171 /* enable WDT reset */
172 writel(TMR_WFAR, &armd1timers->wfar);
173 writel(TMP_WSAR, &armd1timers->wsar);
174 writel(0x3, &armd1timers->wdt_match_en);
175
176 while(1);
177}
Prafulla Wadaskarf4ce6ad2012-02-08 14:15:53 +0530178
179/*
180 * This function is derived from PowerPC code (read timebase as long long).
181 * On ARM it just returns the timer value.
182 */
183unsigned long long get_ticks(void)
184{
185 return get_timer(0);
186}
187
188/*
189 * This function is derived from PowerPC code (timebase clock frequency).
190 * On ARM it returns the number of timer ticks per second.
191 */
Simon Glassa9dc0682019-12-28 10:44:59 -0700192ulong get_tbclk(void)
Prafulla Wadaskarf4ce6ad2012-02-08 14:15:53 +0530193{
194 return (ulong)CONFIG_SYS_HZ;
195}