1*cd71b1d5SPaul Burton // SPDX-License-Identifier: GPL-2.0+
2*cd71b1d5SPaul Burton /*
3*cd71b1d5SPaul Burton * JZ4780 timer
4*cd71b1d5SPaul Burton *
5*cd71b1d5SPaul Burton * Copyright (c) 2013 Imagination Technologies
6*cd71b1d5SPaul Burton * Author: Paul Burton <paul.burton@imgtec.com>
7*cd71b1d5SPaul Burton */
8*cd71b1d5SPaul Burton
9*cd71b1d5SPaul Burton #include <config.h>
10*cd71b1d5SPaul Burton #include <common.h>
11*cd71b1d5SPaul Burton #include <div64.h>
12*cd71b1d5SPaul Burton #include <asm/io.h>
13*cd71b1d5SPaul Burton #include <asm/mipsregs.h>
14*cd71b1d5SPaul Burton #include <mach/jz4780.h>
15*cd71b1d5SPaul Burton
16*cd71b1d5SPaul Burton #define TCU_TSR 0x1C /* Timer Stop Register */
17*cd71b1d5SPaul Burton #define TCU_TSSR 0x2C /* Timer Stop Set Register */
18*cd71b1d5SPaul Burton #define TCU_TSCR 0x3C /* Timer Stop Clear Register */
19*cd71b1d5SPaul Burton #define TCU_TER 0x10 /* Timer Counter Enable Register */
20*cd71b1d5SPaul Burton #define TCU_TESR 0x14 /* Timer Counter Enable Set Register */
21*cd71b1d5SPaul Burton #define TCU_TECR 0x18 /* Timer Counter Enable Clear Register */
22*cd71b1d5SPaul Burton #define TCU_TFR 0x20 /* Timer Flag Register */
23*cd71b1d5SPaul Burton #define TCU_TFSR 0x24 /* Timer Flag Set Register */
24*cd71b1d5SPaul Burton #define TCU_TFCR 0x28 /* Timer Flag Clear Register */
25*cd71b1d5SPaul Burton #define TCU_TMR 0x30 /* Timer Mask Register */
26*cd71b1d5SPaul Burton #define TCU_TMSR 0x34 /* Timer Mask Set Register */
27*cd71b1d5SPaul Burton #define TCU_TMCR 0x38 /* Timer Mask Clear Register */
28*cd71b1d5SPaul Burton /* n = 0,1,2,3,4,5 */
29*cd71b1d5SPaul Burton #define TCU_TDFR(n) (0x40 + (n) * 0x10) /* Timer Data Full Reg */
30*cd71b1d5SPaul Burton #define TCU_TDHR(n) (0x44 + (n) * 0x10) /* Timer Data Half Reg */
31*cd71b1d5SPaul Burton #define TCU_TCNT(n) (0x48 + (n) * 0x10) /* Timer Counter Reg */
32*cd71b1d5SPaul Burton #define TCU_TCSR(n) (0x4C + (n) * 0x10) /* Timer Control Reg */
33*cd71b1d5SPaul Burton
34*cd71b1d5SPaul Burton #define TCU_OSTCNTL 0xe4
35*cd71b1d5SPaul Burton #define TCU_OSTCNTH 0xe8
36*cd71b1d5SPaul Burton #define TCU_OSTCSR 0xec
37*cd71b1d5SPaul Burton #define TCU_OSTCNTHBUF 0xfc
38*cd71b1d5SPaul Burton
39*cd71b1d5SPaul Burton /* Register definitions */
40*cd71b1d5SPaul Burton #define TCU_TCSR_PWM_SD BIT(9)
41*cd71b1d5SPaul Burton #define TCU_TCSR_PWM_INITL_HIGH BIT(8)
42*cd71b1d5SPaul Burton #define TCU_TCSR_PWM_EN BIT(7)
43*cd71b1d5SPaul Burton #define TCU_TCSR_PRESCALE_BIT 3
44*cd71b1d5SPaul Burton #define TCU_TCSR_PRESCALE_MASK (0x7 << TCU_TCSR_PRESCALE_BIT)
45*cd71b1d5SPaul Burton #define TCU_TCSR_PRESCALE1 (0x0 << TCU_TCSR_PRESCALE_BIT)
46*cd71b1d5SPaul Burton #define TCU_TCSR_PRESCALE4 (0x1 << TCU_TCSR_PRESCALE_BIT)
47*cd71b1d5SPaul Burton #define TCU_TCSR_PRESCALE16 (0x2 << TCU_TCSR_PRESCALE_BIT)
48*cd71b1d5SPaul Burton #define TCU_TCSR_PRESCALE64 (0x3 << TCU_TCSR_PRESCALE_BIT)
49*cd71b1d5SPaul Burton #define TCU_TCSR_PRESCALE256 (0x4 << TCU_TCSR_PRESCALE_BIT)
50*cd71b1d5SPaul Burton #define TCU_TCSR_PRESCALE1024 (0x5 << TCU_TCSR_PRESCALE_BIT)
51*cd71b1d5SPaul Burton #define TCU_TCSR_EXT_EN BIT(2)
52*cd71b1d5SPaul Burton #define TCU_TCSR_RTC_EN BIT(1)
53*cd71b1d5SPaul Burton #define TCU_TCSR_PCK_EN BIT(0)
54*cd71b1d5SPaul Burton
55*cd71b1d5SPaul Burton #define TCU_TER_TCEN5 BIT(5)
56*cd71b1d5SPaul Burton #define TCU_TER_TCEN4 BIT(4)
57*cd71b1d5SPaul Burton #define TCU_TER_TCEN3 BIT(3)
58*cd71b1d5SPaul Burton #define TCU_TER_TCEN2 BIT(2)
59*cd71b1d5SPaul Burton #define TCU_TER_TCEN1 BIT(1)
60*cd71b1d5SPaul Burton #define TCU_TER_TCEN0 BIT(0)
61*cd71b1d5SPaul Burton
62*cd71b1d5SPaul Burton #define TCU_TESR_TCST5 BIT(5)
63*cd71b1d5SPaul Burton #define TCU_TESR_TCST4 BIT(4)
64*cd71b1d5SPaul Burton #define TCU_TESR_TCST3 BIT(3)
65*cd71b1d5SPaul Burton #define TCU_TESR_TCST2 BIT(2)
66*cd71b1d5SPaul Burton #define TCU_TESR_TCST1 BIT(1)
67*cd71b1d5SPaul Burton #define TCU_TESR_TCST0 BIT(0)
68*cd71b1d5SPaul Burton
69*cd71b1d5SPaul Burton #define TCU_TECR_TCCL5 BIT(5)
70*cd71b1d5SPaul Burton #define TCU_TECR_TCCL4 BIT(4)
71*cd71b1d5SPaul Burton #define TCU_TECR_TCCL3 BIT(3)
72*cd71b1d5SPaul Burton #define TCU_TECR_TCCL2 BIT(2)
73*cd71b1d5SPaul Burton #define TCU_TECR_TCCL1 BIT(1)
74*cd71b1d5SPaul Burton #define TCU_TECR_TCCL0 BIT(0)
75*cd71b1d5SPaul Burton
76*cd71b1d5SPaul Burton #define TCU_TFR_HFLAG5 BIT(21)
77*cd71b1d5SPaul Burton #define TCU_TFR_HFLAG4 BIT(20)
78*cd71b1d5SPaul Burton #define TCU_TFR_HFLAG3 BIT(19)
79*cd71b1d5SPaul Burton #define TCU_TFR_HFLAG2 BIT(18)
80*cd71b1d5SPaul Burton #define TCU_TFR_HFLAG1 BIT(17)
81*cd71b1d5SPaul Burton #define TCU_TFR_HFLAG0 BIT(16)
82*cd71b1d5SPaul Burton #define TCU_TFR_FFLAG5 BIT(5)
83*cd71b1d5SPaul Burton #define TCU_TFR_FFLAG4 BIT(4)
84*cd71b1d5SPaul Burton #define TCU_TFR_FFLAG3 BIT(3)
85*cd71b1d5SPaul Burton #define TCU_TFR_FFLAG2 BIT(2)
86*cd71b1d5SPaul Burton #define TCU_TFR_FFLAG1 BIT(1)
87*cd71b1d5SPaul Burton #define TCU_TFR_FFLAG0 BIT(0)
88*cd71b1d5SPaul Burton
89*cd71b1d5SPaul Burton #define TCU_TFSR_HFLAG5 BIT(21)
90*cd71b1d5SPaul Burton #define TCU_TFSR_HFLAG4 BIT(20)
91*cd71b1d5SPaul Burton #define TCU_TFSR_HFLAG3 BIT(19)
92*cd71b1d5SPaul Burton #define TCU_TFSR_HFLAG2 BIT(18)
93*cd71b1d5SPaul Burton #define TCU_TFSR_HFLAG1 BIT(17)
94*cd71b1d5SPaul Burton #define TCU_TFSR_HFLAG0 BIT(16)
95*cd71b1d5SPaul Burton #define TCU_TFSR_FFLAG5 BIT(5)
96*cd71b1d5SPaul Burton #define TCU_TFSR_FFLAG4 BIT(4)
97*cd71b1d5SPaul Burton #define TCU_TFSR_FFLAG3 BIT(3)
98*cd71b1d5SPaul Burton #define TCU_TFSR_FFLAG2 BIT(2)
99*cd71b1d5SPaul Burton #define TCU_TFSR_FFLAG1 BIT(1)
100*cd71b1d5SPaul Burton #define TCU_TFSR_FFLAG0 BIT(0)
101*cd71b1d5SPaul Burton
102*cd71b1d5SPaul Burton #define TCU_TFCR_HFLAG5 BIT(21)
103*cd71b1d5SPaul Burton #define TCU_TFCR_HFLAG4 BIT(20)
104*cd71b1d5SPaul Burton #define TCU_TFCR_HFLAG3 BIT(19)
105*cd71b1d5SPaul Burton #define TCU_TFCR_HFLAG2 BIT(18)
106*cd71b1d5SPaul Burton #define TCU_TFCR_HFLAG1 BIT(17)
107*cd71b1d5SPaul Burton #define TCU_TFCR_HFLAG0 BIT(16)
108*cd71b1d5SPaul Burton #define TCU_TFCR_FFLAG5 BIT(5)
109*cd71b1d5SPaul Burton #define TCU_TFCR_FFLAG4 BIT(4)
110*cd71b1d5SPaul Burton #define TCU_TFCR_FFLAG3 BIT(3)
111*cd71b1d5SPaul Burton #define TCU_TFCR_FFLAG2 BIT(2)
112*cd71b1d5SPaul Burton #define TCU_TFCR_FFLAG1 BIT(1)
113*cd71b1d5SPaul Burton #define TCU_TFCR_FFLAG0 BIT(0)
114*cd71b1d5SPaul Burton
115*cd71b1d5SPaul Burton #define TCU_TMR_HMASK5 BIT(21)
116*cd71b1d5SPaul Burton #define TCU_TMR_HMASK4 BIT(20)
117*cd71b1d5SPaul Burton #define TCU_TMR_HMASK3 BIT(19)
118*cd71b1d5SPaul Burton #define TCU_TMR_HMASK2 BIT(18)
119*cd71b1d5SPaul Burton #define TCU_TMR_HMASK1 BIT(17)
120*cd71b1d5SPaul Burton #define TCU_TMR_HMASK0 BIT(16)
121*cd71b1d5SPaul Burton #define TCU_TMR_FMASK5 BIT(5)
122*cd71b1d5SPaul Burton #define TCU_TMR_FMASK4 BIT(4)
123*cd71b1d5SPaul Burton #define TCU_TMR_FMASK3 BIT(3)
124*cd71b1d5SPaul Burton #define TCU_TMR_FMASK2 BIT(2)
125*cd71b1d5SPaul Burton #define TCU_TMR_FMASK1 BIT(1)
126*cd71b1d5SPaul Burton #define TCU_TMR_FMASK0 BIT(0)
127*cd71b1d5SPaul Burton
128*cd71b1d5SPaul Burton #define TCU_TMSR_HMST5 BIT(21)
129*cd71b1d5SPaul Burton #define TCU_TMSR_HMST4 BIT(20)
130*cd71b1d5SPaul Burton #define TCU_TMSR_HMST3 BIT(19)
131*cd71b1d5SPaul Burton #define TCU_TMSR_HMST2 BIT(18)
132*cd71b1d5SPaul Burton #define TCU_TMSR_HMST1 BIT(17)
133*cd71b1d5SPaul Burton #define TCU_TMSR_HMST0 BIT(16)
134*cd71b1d5SPaul Burton #define TCU_TMSR_FMST5 BIT(5)
135*cd71b1d5SPaul Burton #define TCU_TMSR_FMST4 BIT(4)
136*cd71b1d5SPaul Burton #define TCU_TMSR_FMST3 BIT(3)
137*cd71b1d5SPaul Burton #define TCU_TMSR_FMST2 BIT(2)
138*cd71b1d5SPaul Burton #define TCU_TMSR_FMST1 BIT(1)
139*cd71b1d5SPaul Burton #define TCU_TMSR_FMST0 BIT(0)
140*cd71b1d5SPaul Burton
141*cd71b1d5SPaul Burton #define TCU_TMCR_HMCL5 BIT(21)
142*cd71b1d5SPaul Burton #define TCU_TMCR_HMCL4 BIT(20)
143*cd71b1d5SPaul Burton #define TCU_TMCR_HMCL3 BIT(19)
144*cd71b1d5SPaul Burton #define TCU_TMCR_HMCL2 BIT(18)
145*cd71b1d5SPaul Burton #define TCU_TMCR_HMCL1 BIT(17)
146*cd71b1d5SPaul Burton #define TCU_TMCR_HMCL0 BIT(16)
147*cd71b1d5SPaul Burton #define TCU_TMCR_FMCL5 BIT(5)
148*cd71b1d5SPaul Burton #define TCU_TMCR_FMCL4 BIT(4)
149*cd71b1d5SPaul Burton #define TCU_TMCR_FMCL3 BIT(3)
150*cd71b1d5SPaul Burton #define TCU_TMCR_FMCL2 BIT(2)
151*cd71b1d5SPaul Burton #define TCU_TMCR_FMCL1 BIT(1)
152*cd71b1d5SPaul Burton #define TCU_TMCR_FMCL0 BIT(0)
153*cd71b1d5SPaul Burton
154*cd71b1d5SPaul Burton #define TCU_TSR_WDTS BIT(16)
155*cd71b1d5SPaul Burton #define TCU_TSR_STOP5 BIT(5)
156*cd71b1d5SPaul Burton #define TCU_TSR_STOP4 BIT(4)
157*cd71b1d5SPaul Burton #define TCU_TSR_STOP3 BIT(3)
158*cd71b1d5SPaul Burton #define TCU_TSR_STOP2 BIT(2)
159*cd71b1d5SPaul Burton #define TCU_TSR_STOP1 BIT(1)
160*cd71b1d5SPaul Burton #define TCU_TSR_STOP0 BIT(0)
161*cd71b1d5SPaul Burton
162*cd71b1d5SPaul Burton #define TCU_TSSR_WDTSS BIT(16)
163*cd71b1d5SPaul Burton #define TCU_TSSR_STPS5 BIT(5)
164*cd71b1d5SPaul Burton #define TCU_TSSR_STPS4 BIT(4)
165*cd71b1d5SPaul Burton #define TCU_TSSR_STPS3 BIT(3)
166*cd71b1d5SPaul Burton #define TCU_TSSR_STPS2 BIT(2)
167*cd71b1d5SPaul Burton #define TCU_TSSR_STPS1 BIT(1)
168*cd71b1d5SPaul Burton #define TCU_TSSR_STPS0 BIT(0)
169*cd71b1d5SPaul Burton
170*cd71b1d5SPaul Burton #define TCU_TSSR_WDTSC BIT(16)
171*cd71b1d5SPaul Burton #define TCU_TSSR_STPC5 BIT(5)
172*cd71b1d5SPaul Burton #define TCU_TSSR_STPC4 BIT(4)
173*cd71b1d5SPaul Burton #define TCU_TSSR_STPC3 BIT(3)
174*cd71b1d5SPaul Burton #define TCU_TSSR_STPC2 BIT(2)
175*cd71b1d5SPaul Burton #define TCU_TSSR_STPC1 BIT(1)
176*cd71b1d5SPaul Burton #define TCU_TSSR_STPC0 BIT(0)
177*cd71b1d5SPaul Burton
178*cd71b1d5SPaul Burton #define TER_OSTEN BIT(15)
179*cd71b1d5SPaul Burton
180*cd71b1d5SPaul Burton #define OSTCSR_CNT_MD BIT(15)
181*cd71b1d5SPaul Burton #define OSTCSR_SD BIT(9)
182*cd71b1d5SPaul Burton #define OSTCSR_PRESCALE_16 (0x2 << 3)
183*cd71b1d5SPaul Burton #define OSTCSR_EXT_EN BIT(2)
184*cd71b1d5SPaul Burton
timer_init(void)185*cd71b1d5SPaul Burton int timer_init(void)
186*cd71b1d5SPaul Burton {
187*cd71b1d5SPaul Burton void __iomem *regs = (void __iomem *)TCU_BASE;
188*cd71b1d5SPaul Burton
189*cd71b1d5SPaul Burton writel(OSTCSR_SD, regs + TCU_OSTCSR);
190*cd71b1d5SPaul Burton reset_timer();
191*cd71b1d5SPaul Burton writel(OSTCSR_CNT_MD | OSTCSR_EXT_EN | OSTCSR_PRESCALE_16,
192*cd71b1d5SPaul Burton regs + TCU_OSTCSR);
193*cd71b1d5SPaul Burton writew(TER_OSTEN, regs + TCU_TESR);
194*cd71b1d5SPaul Burton return 0;
195*cd71b1d5SPaul Burton }
196*cd71b1d5SPaul Burton
reset_timer(void)197*cd71b1d5SPaul Burton void reset_timer(void)
198*cd71b1d5SPaul Burton {
199*cd71b1d5SPaul Burton void __iomem *regs = (void __iomem *)TCU_BASE;
200*cd71b1d5SPaul Burton
201*cd71b1d5SPaul Burton writel(0, regs + TCU_OSTCNTH);
202*cd71b1d5SPaul Burton writel(0, regs + TCU_OSTCNTL);
203*cd71b1d5SPaul Burton }
204*cd71b1d5SPaul Burton
get_timer64(void)205*cd71b1d5SPaul Burton static u64 get_timer64(void)
206*cd71b1d5SPaul Burton {
207*cd71b1d5SPaul Burton void __iomem *regs = (void __iomem *)TCU_BASE;
208*cd71b1d5SPaul Burton u32 low = readl(regs + TCU_OSTCNTL);
209*cd71b1d5SPaul Burton u32 high = readl(regs + TCU_OSTCNTHBUF);
210*cd71b1d5SPaul Burton
211*cd71b1d5SPaul Burton return ((u64)high << 32) | low;
212*cd71b1d5SPaul Burton }
213*cd71b1d5SPaul Burton
get_timer(ulong base)214*cd71b1d5SPaul Burton ulong get_timer(ulong base)
215*cd71b1d5SPaul Burton {
216*cd71b1d5SPaul Burton return lldiv(get_timer64(), 3000) - base;
217*cd71b1d5SPaul Burton }
218*cd71b1d5SPaul Burton
__udelay(unsigned long usec)219*cd71b1d5SPaul Burton void __udelay(unsigned long usec)
220*cd71b1d5SPaul Burton {
221*cd71b1d5SPaul Burton /* OST count increments at 3MHz */
222*cd71b1d5SPaul Burton u64 end = get_timer64() + ((u64)usec * 3);
223*cd71b1d5SPaul Burton
224*cd71b1d5SPaul Burton while (get_timer64() < end)
225*cd71b1d5SPaul Burton ;
226*cd71b1d5SPaul Burton }
227*cd71b1d5SPaul Burton
get_ticks(void)228*cd71b1d5SPaul Burton unsigned long long get_ticks(void)
229*cd71b1d5SPaul Burton {
230*cd71b1d5SPaul Burton return get_timer64();
231*cd71b1d5SPaul Burton }
232*cd71b1d5SPaul Burton
jz4780_tcu_wdt_start(void)233*cd71b1d5SPaul Burton void jz4780_tcu_wdt_start(void)
234*cd71b1d5SPaul Burton {
235*cd71b1d5SPaul Burton void __iomem *tcu_regs = (void __iomem *)TCU_BASE;
236*cd71b1d5SPaul Burton
237*cd71b1d5SPaul Burton /* Enable WDT clock */
238*cd71b1d5SPaul Burton writel(TCU_TSSR_WDTSC, tcu_regs + TCU_TSCR);
239*cd71b1d5SPaul Burton }
240