1d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
249cbe786SEric Miao /*
349cbe786SEric Miao * linux/arch/arm/mach-mmp/time.c
449cbe786SEric Miao *
549cbe786SEric Miao * Support for clocksource and clockevents
649cbe786SEric Miao *
749cbe786SEric Miao * Copyright (C) 2008 Marvell International Ltd.
849cbe786SEric Miao * All rights reserved.
949cbe786SEric Miao *
1049cbe786SEric Miao * 2008-04-11: Jason Chagas <Jason.chagas@marvell.com>
1149cbe786SEric Miao * 2008-10-08: Bin Yang <bin.yang@marvell.com>
1249cbe786SEric Miao *
1349cbe786SEric Miao * The timers module actually includes three timers, each timer with up to
1449cbe786SEric Miao * three match comparators. Timer #0 is used here in free-running mode as
1549cbe786SEric Miao * the clock source, and match comparator #1 used as clock event device.
1649cbe786SEric Miao */
1749cbe786SEric Miao
1849cbe786SEric Miao #include <linux/init.h>
1949cbe786SEric Miao #include <linux/kernel.h>
2049cbe786SEric Miao #include <linux/interrupt.h>
2149cbe786SEric Miao #include <linux/clockchips.h>
22f36797eeSLubomir Rintel #include <linux/clk.h>
2349cbe786SEric Miao
2449cbe786SEric Miao #include <linux/io.h>
2549cbe786SEric Miao #include <linux/irq.h>
26c68ef2b5SHaojian Zhuang #include <linux/of.h>
27c68ef2b5SHaojian Zhuang #include <linux/of_address.h>
28c68ef2b5SHaojian Zhuang #include <linux/of_irq.h>
2938ff87f7SStephen Boyd #include <linux/sched_clock.h>
302f7e8faeSHaojian Zhuang #include <asm/mach/time.h>
3149cbe786SEric Miao
32b501fd7bSArnd Bergmann #include "regs-timers.h"
3332adcaa0SLubomir Rintel #include <linux/soc/mmp/cputype.h>
3449cbe786SEric Miao
3549cbe786SEric Miao #define MAX_DELTA (0xfffffffe)
3649cbe786SEric Miao #define MIN_DELTA (16)
3749cbe786SEric Miao
38*77acc85cSArnd Bergmann static void __iomem *mmp_timer_base;
39c68ef2b5SHaojian Zhuang
4049cbe786SEric Miao /*
41e348b401SDoug Brown * Read the timer through the CVWR register. Delay is required after requesting
42e348b401SDoug Brown * a read. The CR register cannot be directly read due to metastability issues
43e348b401SDoug Brown * documented in the PXA168 software manual.
4449cbe786SEric Miao */
timer_read(void)4549cbe786SEric Miao static inline uint32_t timer_read(void)
4649cbe786SEric Miao {
47e348b401SDoug Brown uint32_t val;
48e348b401SDoug Brown int delay = 3;
4949cbe786SEric Miao
50c68ef2b5SHaojian Zhuang __raw_writel(1, mmp_timer_base + TMR_CVWR(1));
5149cbe786SEric Miao
5249cbe786SEric Miao while (delay--)
53e348b401SDoug Brown val = __raw_readl(mmp_timer_base + TMR_CVWR(1));
5449cbe786SEric Miao
55e348b401SDoug Brown return val;
5649cbe786SEric Miao }
5749cbe786SEric Miao
mmp_read_sched_clock(void)58e5c0228dSStephen Boyd static u64 notrace mmp_read_sched_clock(void)
5949cbe786SEric Miao {
602f0778afSMarc Zyngier return timer_read();
6149cbe786SEric Miao }
6249cbe786SEric Miao
timer_interrupt(int irq,void * dev_id)6349cbe786SEric Miao static irqreturn_t timer_interrupt(int irq, void *dev_id)
6449cbe786SEric Miao {
6549cbe786SEric Miao struct clock_event_device *c = dev_id;
6649cbe786SEric Miao
67af9dafb1SLennert Buytenhek /*
68af9dafb1SLennert Buytenhek * Clear pending interrupt status.
69af9dafb1SLennert Buytenhek */
70c68ef2b5SHaojian Zhuang __raw_writel(0x01, mmp_timer_base + TMR_ICR(0));
71af9dafb1SLennert Buytenhek
72af9dafb1SLennert Buytenhek /*
73af9dafb1SLennert Buytenhek * Disable timer 0.
74af9dafb1SLennert Buytenhek */
75c68ef2b5SHaojian Zhuang __raw_writel(0x02, mmp_timer_base + TMR_CER);
76af9dafb1SLennert Buytenhek
7749cbe786SEric Miao c->event_handler(c);
78af9dafb1SLennert Buytenhek
7949cbe786SEric Miao return IRQ_HANDLED;
8049cbe786SEric Miao }
8149cbe786SEric Miao
timer_set_next_event(unsigned long delta,struct clock_event_device * dev)8249cbe786SEric Miao static int timer_set_next_event(unsigned long delta,
8349cbe786SEric Miao struct clock_event_device *dev)
8449cbe786SEric Miao {
85af9dafb1SLennert Buytenhek unsigned long flags;
8649cbe786SEric Miao
8749cbe786SEric Miao local_irq_save(flags);
8849cbe786SEric Miao
89af9dafb1SLennert Buytenhek /*
90af9dafb1SLennert Buytenhek * Disable timer 0.
91af9dafb1SLennert Buytenhek */
92c68ef2b5SHaojian Zhuang __raw_writel(0x02, mmp_timer_base + TMR_CER);
93af9dafb1SLennert Buytenhek
94af9dafb1SLennert Buytenhek /*
95af9dafb1SLennert Buytenhek * Clear and enable timer match 0 interrupt.
96af9dafb1SLennert Buytenhek */
97c68ef2b5SHaojian Zhuang __raw_writel(0x01, mmp_timer_base + TMR_ICR(0));
98c68ef2b5SHaojian Zhuang __raw_writel(0x01, mmp_timer_base + TMR_IER(0));
9949cbe786SEric Miao
100af9dafb1SLennert Buytenhek /*
101af9dafb1SLennert Buytenhek * Setup new clockevent timer value.
102af9dafb1SLennert Buytenhek */
103c68ef2b5SHaojian Zhuang __raw_writel(delta - 1, mmp_timer_base + TMR_TN_MM(0, 0));
104af9dafb1SLennert Buytenhek
105af9dafb1SLennert Buytenhek /*
106af9dafb1SLennert Buytenhek * Enable timer 0.
107af9dafb1SLennert Buytenhek */
108c68ef2b5SHaojian Zhuang __raw_writel(0x03, mmp_timer_base + TMR_CER);
10949cbe786SEric Miao
11049cbe786SEric Miao local_irq_restore(flags);
111af9dafb1SLennert Buytenhek
11249cbe786SEric Miao return 0;
11349cbe786SEric Miao }
11449cbe786SEric Miao
timer_set_shutdown(struct clock_event_device * evt)115a785fb39SViresh Kumar static int timer_set_shutdown(struct clock_event_device *evt)
11649cbe786SEric Miao {
11749cbe786SEric Miao unsigned long flags;
11849cbe786SEric Miao
11949cbe786SEric Miao local_irq_save(flags);
12049cbe786SEric Miao /* disable the matching interrupt */
121c68ef2b5SHaojian Zhuang __raw_writel(0x00, mmp_timer_base + TMR_IER(0));
12249cbe786SEric Miao local_irq_restore(flags);
123a785fb39SViresh Kumar
124a785fb39SViresh Kumar return 0;
12549cbe786SEric Miao }
12649cbe786SEric Miao
12749cbe786SEric Miao static struct clock_event_device ckevt = {
12849cbe786SEric Miao .name = "clockevent",
12949cbe786SEric Miao .features = CLOCK_EVT_FEAT_ONESHOT,
13049cbe786SEric Miao .rating = 200,
13149cbe786SEric Miao .set_next_event = timer_set_next_event,
132a785fb39SViresh Kumar .set_state_shutdown = timer_set_shutdown,
133a785fb39SViresh Kumar .set_state_oneshot = timer_set_shutdown,
13449cbe786SEric Miao };
13549cbe786SEric Miao
clksrc_read(struct clocksource * cs)136a5a1d1c2SThomas Gleixner static u64 clksrc_read(struct clocksource *cs)
13749cbe786SEric Miao {
13849cbe786SEric Miao return timer_read();
13949cbe786SEric Miao }
14049cbe786SEric Miao
14149cbe786SEric Miao static struct clocksource cksrc = {
14249cbe786SEric Miao .name = "clocksource",
14349cbe786SEric Miao .rating = 200,
14449cbe786SEric Miao .read = clksrc_read,
14549cbe786SEric Miao .mask = CLOCKSOURCE_MASK(32),
14649cbe786SEric Miao .flags = CLOCK_SOURCE_IS_CONTINUOUS,
14749cbe786SEric Miao };
14849cbe786SEric Miao
timer_config(void)14949cbe786SEric Miao static void __init timer_config(void)
15049cbe786SEric Miao {
151c68ef2b5SHaojian Zhuang uint32_t ccr = __raw_readl(mmp_timer_base + TMR_CCR);
15249cbe786SEric Miao
153c68ef2b5SHaojian Zhuang __raw_writel(0x0, mmp_timer_base + TMR_CER); /* disable */
15449cbe786SEric Miao
155a9372a5fSLubomir Rintel ccr &= (cpu_is_mmp2() || cpu_is_mmp3()) ?
156a9372a5fSLubomir Rintel (TMR_CCR_CS_0(0) | TMR_CCR_CS_1(0)) :
1577ce5ae39SLennert Buytenhek (TMR_CCR_CS_0(3) | TMR_CCR_CS_1(3));
158c68ef2b5SHaojian Zhuang __raw_writel(ccr, mmp_timer_base + TMR_CCR);
15949cbe786SEric Miao
160af9dafb1SLennert Buytenhek /* set timer 0 to periodic mode, and timer 1 to free-running mode */
161c68ef2b5SHaojian Zhuang __raw_writel(0x2, mmp_timer_base + TMR_CMR);
16249cbe786SEric Miao
163c68ef2b5SHaojian Zhuang __raw_writel(0x1, mmp_timer_base + TMR_PLCR(0)); /* periodic */
164c68ef2b5SHaojian Zhuang __raw_writel(0x7, mmp_timer_base + TMR_ICR(0)); /* clear status */
165c68ef2b5SHaojian Zhuang __raw_writel(0x0, mmp_timer_base + TMR_IER(0));
16649cbe786SEric Miao
167c68ef2b5SHaojian Zhuang __raw_writel(0x0, mmp_timer_base + TMR_PLCR(1)); /* free-running */
168c68ef2b5SHaojian Zhuang __raw_writel(0x7, mmp_timer_base + TMR_ICR(1)); /* clear status */
169c68ef2b5SHaojian Zhuang __raw_writel(0x0, mmp_timer_base + TMR_IER(1));
1707ce5ae39SLennert Buytenhek
171af9dafb1SLennert Buytenhek /* enable timer 1 counter */
172c68ef2b5SHaojian Zhuang __raw_writel(0x2, mmp_timer_base + TMR_CER);
17349cbe786SEric Miao }
17449cbe786SEric Miao
mmp_timer_init(int irq,unsigned long rate)175*77acc85cSArnd Bergmann static void __init mmp_timer_init(int irq, unsigned long rate)
17649cbe786SEric Miao {
17749cbe786SEric Miao timer_config();
17849cbe786SEric Miao
179f36797eeSLubomir Rintel sched_clock_register(mmp_read_sched_clock, 32, rate);
18049cbe786SEric Miao
18149cbe786SEric Miao ckevt.cpumask = cpumask_of(0);
18249cbe786SEric Miao
1832fcf5335Safzal mohammed if (request_irq(irq, timer_interrupt, IRQF_TIMER | IRQF_IRQPOLL,
1842fcf5335Safzal mohammed "timer", &ckevt))
1852fcf5335Safzal mohammed pr_err("Failed to request irq %d (timer)\n", irq);
18649cbe786SEric Miao
187f36797eeSLubomir Rintel clocksource_register_hz(&cksrc, rate);
188f36797eeSLubomir Rintel clockevents_config_and_register(&ckevt, rate, MIN_DELTA, MAX_DELTA);
18949cbe786SEric Miao }
190c68ef2b5SHaojian Zhuang
mmp_dt_init_timer(struct device_node * np)1911732050fSLubomir Rintel static int __init mmp_dt_init_timer(struct device_node *np)
192c68ef2b5SHaojian Zhuang {
193f36797eeSLubomir Rintel struct clk *clk;
194c68ef2b5SHaojian Zhuang int irq, ret;
195f36797eeSLubomir Rintel unsigned long rate;
196c68ef2b5SHaojian Zhuang
197f36797eeSLubomir Rintel clk = of_clk_get(np, 0);
198f36797eeSLubomir Rintel if (!IS_ERR(clk)) {
199f36797eeSLubomir Rintel ret = clk_prepare_enable(clk);
200f36797eeSLubomir Rintel if (ret)
2011732050fSLubomir Rintel return ret;
2020bd0f30bSLubomir Rintel rate = clk_get_rate(clk);
203f36797eeSLubomir Rintel } else if (cpu_is_pj4()) {
204f36797eeSLubomir Rintel rate = 6500000;
205f36797eeSLubomir Rintel } else {
206f36797eeSLubomir Rintel rate = 3250000;
207f36797eeSLubomir Rintel }
208f36797eeSLubomir Rintel
209c68ef2b5SHaojian Zhuang irq = irq_of_parse_and_map(np, 0);
2101732050fSLubomir Rintel if (!irq)
2111732050fSLubomir Rintel return -EINVAL;
2121732050fSLubomir Rintel
213c68ef2b5SHaojian Zhuang mmp_timer_base = of_iomap(np, 0);
2141732050fSLubomir Rintel if (!mmp_timer_base)
2151732050fSLubomir Rintel return -ENOMEM;
2161732050fSLubomir Rintel
21712d3a30dSArnd Bergmann mmp_timer_init(irq, rate);
2181732050fSLubomir Rintel return 0;
219c68ef2b5SHaojian Zhuang }
2201732050fSLubomir Rintel
2211732050fSLubomir Rintel TIMER_OF_DECLARE(mmp_timer, "mrvl,mmp-timer", mmp_dt_init_timer);
222