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