blob: 523f9cfc8c488b04c5854e14a3f3342eeb18071b [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Vladimir Zapolskiy6b20ef82012-04-19 04:33:08 +00002/*
3 * Copyright (C) 2011 Vladimir Zapolskiy <vz@mleia.com>
Vladimir Zapolskiy6b20ef82012-04-19 04:33:08 +00004 */
5
Simon Glass97589732020-05-10 11:40:02 -06006#include <init.h>
Simon Glass495a5dc2019-11-14 12:57:30 -07007#include <time.h>
Vladimir Zapolskiy6b20ef82012-04-19 04:33:08 +00008#include <asm/arch/cpu.h>
9#include <asm/arch/clk.h>
10#include <asm/arch/timer.h>
11#include <asm/io.h>
Simon Glassdbd79542020-05-10 11:40:11 -060012#include <linux/delay.h>
Vladimir Zapolskiy6b20ef82012-04-19 04:33:08 +000013
14static struct timer_regs *timer0 = (struct timer_regs *)TIMER0_BASE;
15static struct timer_regs *timer1 = (struct timer_regs *)TIMER1_BASE;
16static struct clk_pm_regs *clk = (struct clk_pm_regs *)CLK_PM_BASE;
17
18static void lpc32xx_timer_clock(u32 bit, int enable)
19{
20 if (enable)
21 setbits_le32(&clk->timclk_ctrl1, bit);
22 else
23 clrbits_le32(&clk->timclk_ctrl1, bit);
24}
25
26static void lpc32xx_timer_reset(struct timer_regs *timer, u32 freq)
27{
28 writel(TIMER_TCR_COUNTER_RESET, &timer->tcr);
29 writel(TIMER_TCR_COUNTER_DISABLE, &timer->tcr);
30 writel(0, &timer->tc);
31 writel(0, &timer->pr);
32
33 /* Count mode is every rising PCLK edge */
34 writel(TIMER_CTCR_MODE_TIMER, &timer->ctcr);
35
36 /* Set prescale counter value */
37 writel((get_periph_clk_rate() / freq) - 1, &timer->pr);
Gregory CLEMENT67ebb1c2019-04-17 11:48:45 +020038
39 /* Ensure that the counter is not reset when matching TC */
40 writel(0, &timer->mcr);
Vladimir Zapolskiy6b20ef82012-04-19 04:33:08 +000041}
42
43static void lpc32xx_timer_count(struct timer_regs *timer, int enable)
44{
45 if (enable)
46 writel(TIMER_TCR_COUNTER_ENABLE, &timer->tcr);
47 else
48 writel(TIMER_TCR_COUNTER_DISABLE, &timer->tcr);
49}
50
51int timer_init(void)
52{
53 lpc32xx_timer_clock(CLK_TIMCLK_TIMER0, 1);
54 lpc32xx_timer_reset(timer0, CONFIG_SYS_HZ);
55 lpc32xx_timer_count(timer0, 1);
56
57 return 0;
58}
59
60ulong get_timer(ulong base)
61{
62 return readl(&timer0->tc) - base;
63}
64
65void __udelay(unsigned long usec)
66{
67 lpc32xx_timer_clock(CLK_TIMCLK_TIMER1, 1);
68 lpc32xx_timer_reset(timer1, CONFIG_SYS_HZ * 1000);
69 lpc32xx_timer_count(timer1, 1);
70
71 while (readl(&timer1->tc) < usec)
72 /* NOP */;
73
74 lpc32xx_timer_count(timer1, 0);
75 lpc32xx_timer_clock(CLK_TIMCLK_TIMER1, 0);
76}
77
78unsigned long long get_ticks(void)
79{
80 return get_timer(0);
81}
82
83ulong get_tbclk(void)
84{
85 return CONFIG_SYS_HZ;
86}