xref: /linux/arch/arm/mach-rpc/time.c (revision cdd38c5f1ce4398ec58fec95904b75824daab7b5)
1d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2a1be5d64SRussell King /*
3a1be5d64SRussell King  *  linux/arch/arm/common/time-acorn.c
4a1be5d64SRussell King  *
5a1be5d64SRussell King  *  Copyright (c) 1996-2000 Russell King.
6a1be5d64SRussell King  *
7a1be5d64SRussell King  *  Changelog:
8a1be5d64SRussell King  *   24-Sep-1996	RMK	Created
9a1be5d64SRussell King  *   10-Oct-1996	RMK	Brought up to date with arch-sa110eval
10a1be5d64SRussell King  *   04-Dec-1997	RMK	Updated for new arch/arm/time.c
11a1be5d64SRussell King  *   13=Jun-2004	DS	Moved to arch/arm/common b/c shared w/CLPS7500
12a1be5d64SRussell King  */
13a44c1d70SRussell King #include <linux/clocksource.h>
14a1be5d64SRussell King #include <linux/init.h>
15a1be5d64SRussell King #include <linux/interrupt.h>
16a1be5d64SRussell King #include <linux/irq.h>
17a1be5d64SRussell King #include <linux/io.h>
18a1be5d64SRussell King 
19a1be5d64SRussell King #include <mach/hardware.h>
20a1be5d64SRussell King #include <asm/hardware/ioc.h>
21a1be5d64SRussell King 
22a1be5d64SRussell King #include <asm/mach/time.h>
23a1be5d64SRussell King 
2453985371SUwe Kleine-König #define RPC_CLOCK_FREQ 2000000
2553985371SUwe Kleine-König #define RPC_LATCH DIV_ROUND_CLOSEST(RPC_CLOCK_FREQ, HZ)
2653985371SUwe Kleine-König 
27a44c1d70SRussell King static u32 ioc_time;
28a44c1d70SRussell King 
ioc_timer_read(struct clocksource * cs)29a44c1d70SRussell King static u64 ioc_timer_read(struct clocksource *cs)
30a1be5d64SRussell King {
31a1be5d64SRussell King 	unsigned int count1, count2, status;
32a44c1d70SRussell King 	unsigned long flags;
33a44c1d70SRussell King 	u32 ticks;
34a1be5d64SRussell King 
35a44c1d70SRussell King 	local_irq_save(flags);
36a1be5d64SRussell King 	ioc_writeb (0, IOC_T0LATCH);
37a1be5d64SRussell King 	barrier ();
38a1be5d64SRussell King 	count1 = ioc_readb(IOC_T0CNTL) | (ioc_readb(IOC_T0CNTH) << 8);
39a1be5d64SRussell King 	barrier ();
40a1be5d64SRussell King 	status = ioc_readb(IOC_IRQREQA);
41a1be5d64SRussell King 	barrier ();
42a1be5d64SRussell King 	ioc_writeb (0, IOC_T0LATCH);
43a1be5d64SRussell King 	barrier ();
44a1be5d64SRussell King 	count2 = ioc_readb(IOC_T0CNTL) | (ioc_readb(IOC_T0CNTH) << 8);
45a44c1d70SRussell King 	ticks = ioc_time + RPC_LATCH - count2;
46a44c1d70SRussell King 	local_irq_restore(flags);
47a1be5d64SRussell King 
48a1be5d64SRussell King 	if (count2 < count1) {
49a1be5d64SRussell King 		/*
50a44c1d70SRussell King 		 * The timer has not reloaded between reading count1 and
51a44c1d70SRussell King 		 * count2, check whether an interrupt was actually pending.
52a1be5d64SRussell King 		 */
53a1be5d64SRussell King 		if (status & (1 << 5))
54a44c1d70SRussell King 			ticks += RPC_LATCH;
55a1be5d64SRussell King 	} else if (count2 > count1) {
56a1be5d64SRussell King 		/*
57a44c1d70SRussell King 		 * The timer has reloaded, so count2 indicates the new
58a44c1d70SRussell King 		 * count since the wrap.  The interrupt would not have
59a44c1d70SRussell King 		 * been processed, so add the missed ticks.
60a1be5d64SRussell King 		 */
61a44c1d70SRussell King 		ticks += RPC_LATCH;
62a1be5d64SRussell King 	}
63a1be5d64SRussell King 
64a44c1d70SRussell King 	return ticks;
65a1be5d64SRussell King }
66a1be5d64SRussell King 
67a44c1d70SRussell King static struct clocksource ioctime_clocksource = {
68a44c1d70SRussell King 	.read = ioc_timer_read,
69a44c1d70SRussell King 	.mask = CLOCKSOURCE_MASK(32),
70a44c1d70SRussell King 	.rating = 100,
71a44c1d70SRussell King };
72a44c1d70SRussell King 
ioctime_init(void)73a1be5d64SRussell King void __init ioctime_init(void)
74a1be5d64SRussell King {
7553985371SUwe Kleine-König 	ioc_writeb(RPC_LATCH & 255, IOC_T0LTCHL);
7653985371SUwe Kleine-König 	ioc_writeb(RPC_LATCH >> 8, IOC_T0LTCHH);
77a1be5d64SRussell King 	ioc_writeb(0, IOC_T0GO);
78a1be5d64SRussell King }
79a1be5d64SRussell King 
80a1be5d64SRussell King static irqreturn_t
ioc_timer_interrupt(int irq,void * dev_id)81a1be5d64SRussell King ioc_timer_interrupt(int irq, void *dev_id)
82a1be5d64SRussell King {
83a44c1d70SRussell King 	ioc_time += RPC_LATCH;
84*6239da29SArnd Bergmann 	legacy_timer_tick(1);
85a1be5d64SRussell King 	return IRQ_HANDLED;
86a1be5d64SRussell King }
87a1be5d64SRussell King 
88a1be5d64SRussell King /*
89a1be5d64SRussell King  * Set up timer interrupt.
90a1be5d64SRussell King  */
ioc_timer_init(void)916bb27d73SStephen Warren void __init ioc_timer_init(void)
92a1be5d64SRussell King {
93a44c1d70SRussell King 	WARN_ON(clocksource_register_hz(&ioctime_clocksource, RPC_CLOCK_FREQ));
94a1be5d64SRussell King 	ioctime_init();
95575fb69eSafzal mohammed 	if (request_irq(IRQ_TIMER0, ioc_timer_interrupt, 0, "timer", NULL))
96575fb69eSafzal mohammed 		pr_err("Failed to request irq %d (timer)\n", IRQ_TIMER0);
97a1be5d64SRussell King }
98