blob: 0f1e68c8355f64c8303f2858562a3fdaf2cedf6c [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 Glass97589732020-05-10 11:40:02 -060011#include <init.h>
Simon Glass495a5dc2019-11-14 12:57:30 -070012#include <time.h>
Lei Wen35b130c2011-10-18 19:50:48 +053013#include <asm/arch/cpu.h>
Prafulla Wadaskarc0c7a112010-10-12 16:31:40 +053014#include <asm/arch/armada100.h>
Simon Glassdbd79542020-05-10 11:40:11 -060015#include <linux/delay.h>
Prafulla Wadaskarc0c7a112010-10-12 16:31:40 +053016
17/*
18 * Timer registers
19 * Refer Section A.6 in Datasheet
20 */
21struct armd1tmr_registers {
22 u32 clk_ctrl; /* Timer clk control reg */
23 u32 match[9]; /* Timer match registers */
24 u32 count[3]; /* Timer count registers */
25 u32 status[3];
26 u32 ie[3];
27 u32 preload[3]; /* Timer preload value */
28 u32 preload_ctrl[3];
29 u32 wdt_match_en;
30 u32 wdt_match_r;
31 u32 wdt_val;
32 u32 wdt_sts;
33 u32 icr[3];
34 u32 wdt_icr;
35 u32 cer; /* Timer count enable reg */
36 u32 cmr;
37 u32 ilr[3];
38 u32 wcr;
39 u32 wfar;
40 u32 wsar;
41 u32 cvwr;
42};
43
44#define TIMER 0 /* Use TIMER 0 */
45/* Each timer has 3 match registers */
46#define MATCH_CMP(x) ((3 * TIMER) + x)
47#define TIMER_LOAD_VAL 0xffffffff
48#define COUNT_RD_REQ 0x1
49
50DECLARE_GLOBAL_DATA_PTR;
Simon Glass2655ee12012-12-13 20:48:34 +000051/* Using gd->arch.tbu from timestamp and gd->arch.tbl for lastdec */
Prafulla Wadaskarc0c7a112010-10-12 16:31:40 +053052
53/* For preventing risk of instability in reading counter value,
54 * first set read request to register cvwr and then read same
55 * register after it captures counter value.
56 */
57ulong read_timer(void)
58{
59 struct armd1tmr_registers *armd1timers =
60 (struct armd1tmr_registers *) ARMD1_TIMER_BASE;
61 volatile int loop=100;
62
63 writel(COUNT_RD_REQ, &armd1timers->cvwr);
64 while (loop--);
65 return(readl(&armd1timers->cvwr));
66}
67
Patrick Delaunay9858a602018-10-05 11:33:52 +020068static ulong get_timer_masked(void)
Prafulla Wadaskarc0c7a112010-10-12 16:31:40 +053069{
70 ulong now = read_timer();
71
Simon Glass2655ee12012-12-13 20:48:34 +000072 if (now >= gd->arch.tbl) {
Prafulla Wadaskarc0c7a112010-10-12 16:31:40 +053073 /* normal mode */
Simon Glass2655ee12012-12-13 20:48:34 +000074 gd->arch.tbu += now - gd->arch.tbl;
Prafulla Wadaskarc0c7a112010-10-12 16:31:40 +053075 } else {
76 /* we have an overflow ... */
Simon Glass2655ee12012-12-13 20:48:34 +000077 gd->arch.tbu += now + TIMER_LOAD_VAL - gd->arch.tbl;
Prafulla Wadaskarc0c7a112010-10-12 16:31:40 +053078 }
Simon Glass2655ee12012-12-13 20:48:34 +000079 gd->arch.tbl = now;
Prafulla Wadaskarc0c7a112010-10-12 16:31:40 +053080
Simon Glass8ca15202012-12-13 20:48:33 +000081 return gd->arch.tbu;
Prafulla Wadaskarc0c7a112010-10-12 16:31:40 +053082}
83
Prafulla Wadaskarc0c7a112010-10-12 16:31:40 +053084ulong get_timer(ulong base)
85{
86 return ((get_timer_masked() / (CONFIG_SYS_HZ_CLOCK / 1000)) -
87 base);
88}
89
Prafulla Wadaskarc0c7a112010-10-12 16:31:40 +053090void __udelay(unsigned long usec)
91{
92 ulong delayticks;
93 ulong endtime;
94
95 delayticks = (usec * (CONFIG_SYS_HZ_CLOCK / 1000000));
96 endtime = get_timer_masked() + delayticks;
97
98 while (get_timer_masked() < endtime);
99}
100
101/*
102 * init the Timer
103 */
104int timer_init(void)
105{
106 struct armd1apb1_registers *apb1clkres =
107 (struct armd1apb1_registers *) ARMD1_APBC1_BASE;
108 struct armd1tmr_registers *armd1timers =
109 (struct armd1tmr_registers *) ARMD1_TIMER_BASE;
110
111 /* Enable Timer clock at 3.25 MHZ */
112 writel(APBC_APBCLK | APBC_FNCLK | APBC_FNCLKSEL(3), &apb1clkres->timers);
113
114 /* load value into timer */
115 writel(0x0, &armd1timers->clk_ctrl);
116 /* Use Timer 0 Match Resiger 0 */
117 writel(TIMER_LOAD_VAL, &armd1timers->match[MATCH_CMP(0)]);
118 /* Preload value is 0 */
119 writel(0x0, &armd1timers->preload[TIMER]);
120 /* Enable match comparator 0 for Timer 0 */
121 writel(0x1, &armd1timers->preload_ctrl[TIMER]);
122
123 /* Enable timer 0 */
124 writel(0x1, &armd1timers->cer);
Simon Glass2655ee12012-12-13 20:48:34 +0000125 /* init the gd->arch.tbu and gd->arch.tbl value */
126 gd->arch.tbl = read_timer();
Simon Glass8ca15202012-12-13 20:48:33 +0000127 gd->arch.tbu = 0;
Prafulla Wadaskarc0c7a112010-10-12 16:31:40 +0530128
129 return 0;
130}
131
132#define MPMU_APRR_WDTR (1<<4)
133#define TMR_WFAR 0xbaba /* WDT Register First key */
134#define TMP_WSAR 0xeb10 /* WDT Register Second key */
135
136/*
137 * This function uses internal Watchdog Timer
138 * based reset mechanism.
139 * Steps to write watchdog registers (protected access)
140 * 1. Write key value to TMR_WFAR reg.
141 * 2. Write key value to TMP_WSAR reg.
142 * 3. Perform write operation.
143 */
Simon Glassafb02152019-12-28 10:45:01 -0700144void reset_cpu(unsigned long ignored)
Prafulla Wadaskarc0c7a112010-10-12 16:31:40 +0530145{
146 struct armd1mpmu_registers *mpmu =
147 (struct armd1mpmu_registers *) ARMD1_MPMU_BASE;
148 struct armd1tmr_registers *armd1timers =
149 (struct armd1tmr_registers *) ARMD1_TIMER_BASE;
150 u32 val;
151
152 /* negate hardware reset to the WDT after system reset */
153 val = readl(&mpmu->aprr);
154 val = val | MPMU_APRR_WDTR;
155 writel(val, &mpmu->aprr);
156
157 /* reset/enable WDT clock */
158 writel(APBC_APBCLK | APBC_FNCLK | APBC_RST, &mpmu->wdtpcr);
159 readl(&mpmu->wdtpcr);
160 writel(APBC_APBCLK | APBC_FNCLK, &mpmu->wdtpcr);
161 readl(&mpmu->wdtpcr);
162
163 /* clear previous WDT status */
164 writel(TMR_WFAR, &armd1timers->wfar);
165 writel(TMP_WSAR, &armd1timers->wsar);
166 writel(0, &armd1timers->wdt_sts);
167
168 /* set match counter */
169 writel(TMR_WFAR, &armd1timers->wfar);
170 writel(TMP_WSAR, &armd1timers->wsar);
171 writel(0xf, &armd1timers->wdt_match_r);
172
173 /* enable WDT reset */
174 writel(TMR_WFAR, &armd1timers->wfar);
175 writel(TMP_WSAR, &armd1timers->wsar);
176 writel(0x3, &armd1timers->wdt_match_en);
177
178 while(1);
179}
Prafulla Wadaskarf4ce6ad2012-02-08 14:15:53 +0530180
181/*
182 * This function is derived from PowerPC code (read timebase as long long).
183 * On ARM it just returns the timer value.
184 */
185unsigned long long get_ticks(void)
186{
187 return get_timer(0);
188}
189
190/*
191 * This function is derived from PowerPC code (timebase clock frequency).
192 * On ARM it returns the number of timer ticks per second.
193 */
Simon Glassa9dc0682019-12-28 10:44:59 -0700194ulong get_tbclk(void)
Prafulla Wadaskarf4ce6ad2012-02-08 14:15:53 +0530195{
196 return (ulong)CONFIG_SYS_HZ;
197}