blob: 1d750152b160bec69194a923e59eda368e482037 [file] [log] [blame]
David Brazdil0f672f62019-12-10 10:32:29 +00001// SPDX-License-Identifier: GPL-2.0-only
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002/*
3 * linux/arch/arm/common/time-acorn.c
4 *
5 * Copyright (c) 1996-2000 Russell King.
6 *
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00007 * Changelog:
8 * 24-Sep-1996 RMK Created
9 * 10-Oct-1996 RMK Brought up to date with arch-sa110eval
10 * 04-Dec-1997 RMK Updated for new arch/arm/time.c
11 * 13=Jun-2004 DS Moved to arch/arm/common b/c shared w/CLPS7500
12 */
David Brazdil0f672f62019-12-10 10:32:29 +000013#include <linux/clocksource.h>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000014#include <linux/init.h>
15#include <linux/interrupt.h>
16#include <linux/irq.h>
17#include <linux/io.h>
18
19#include <mach/hardware.h>
20#include <asm/hardware/ioc.h>
21
22#include <asm/mach/time.h>
23
24#define RPC_CLOCK_FREQ 2000000
25#define RPC_LATCH DIV_ROUND_CLOSEST(RPC_CLOCK_FREQ, HZ)
26
David Brazdil0f672f62019-12-10 10:32:29 +000027static u32 ioc_time;
28
29static u64 ioc_timer_read(struct clocksource *cs)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000030{
31 unsigned int count1, count2, status;
David Brazdil0f672f62019-12-10 10:32:29 +000032 unsigned long flags;
33 u32 ticks;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000034
David Brazdil0f672f62019-12-10 10:32:29 +000035 local_irq_save(flags);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000036 ioc_writeb (0, IOC_T0LATCH);
37 barrier ();
38 count1 = ioc_readb(IOC_T0CNTL) | (ioc_readb(IOC_T0CNTH) << 8);
39 barrier ();
40 status = ioc_readb(IOC_IRQREQA);
41 barrier ();
42 ioc_writeb (0, IOC_T0LATCH);
43 barrier ();
44 count2 = ioc_readb(IOC_T0CNTL) | (ioc_readb(IOC_T0CNTH) << 8);
David Brazdil0f672f62019-12-10 10:32:29 +000045 ticks = ioc_time + RPC_LATCH - count2;
46 local_irq_restore(flags);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000047
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000048 if (count2 < count1) {
49 /*
David Brazdil0f672f62019-12-10 10:32:29 +000050 * The timer has not reloaded between reading count1 and
51 * count2, check whether an interrupt was actually pending.
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000052 */
53 if (status & (1 << 5))
David Brazdil0f672f62019-12-10 10:32:29 +000054 ticks += RPC_LATCH;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000055 } else if (count2 > count1) {
56 /*
David Brazdil0f672f62019-12-10 10:32:29 +000057 * The timer has reloaded, so count2 indicates the new
58 * count since the wrap. The interrupt would not have
59 * been processed, so add the missed ticks.
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000060 */
David Brazdil0f672f62019-12-10 10:32:29 +000061 ticks += RPC_LATCH;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000062 }
63
David Brazdil0f672f62019-12-10 10:32:29 +000064 return ticks;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000065}
66
David Brazdil0f672f62019-12-10 10:32:29 +000067static struct clocksource ioctime_clocksource = {
68 .read = ioc_timer_read,
69 .mask = CLOCKSOURCE_MASK(32),
70 .rating = 100,
71};
72
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000073void __init ioctime_init(void)
74{
75 ioc_writeb(RPC_LATCH & 255, IOC_T0LTCHL);
76 ioc_writeb(RPC_LATCH >> 8, IOC_T0LTCHH);
77 ioc_writeb(0, IOC_T0GO);
78}
79
80static irqreturn_t
81ioc_timer_interrupt(int irq, void *dev_id)
82{
David Brazdil0f672f62019-12-10 10:32:29 +000083 ioc_time += RPC_LATCH;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000084 timer_tick();
85 return IRQ_HANDLED;
86}
87
88static struct irqaction ioc_timer_irq = {
89 .name = "timer",
90 .handler = ioc_timer_interrupt
91};
92
93/*
94 * Set up timer interrupt.
95 */
96void __init ioc_timer_init(void)
97{
David Brazdil0f672f62019-12-10 10:32:29 +000098 WARN_ON(clocksource_register_hz(&ioctime_clocksource, RPC_CLOCK_FREQ));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000099 ioctime_init();
100 setup_irq(IRQ_TIMER0, &ioc_timer_irq);
101}