blob: 2689771c1d38f351d00630d82a45a00838ea1b33 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/arch/arm/common/time-acorn.c
3 *
4 * Copyright (c) 1996-2000 Russell King.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 *
10 * Changelog:
11 * 24-Sep-1996 RMK Created
12 * 10-Oct-1996 RMK Brought up to date with arch-sa110eval
13 * 04-Dec-1997 RMK Updated for new arch/arm/time.c
14 * 13=Jun-2004 DS Moved to arch/arm/common b/c shared w/CLPS7500
15 */
16#include <linux/timex.h>
17#include <linux/init.h>
18#include <linux/interrupt.h>
Thomas Gleixnerc4bfa282006-07-01 22:32:14 +010019#include <linux/irq.h>
Russell Kingfced80c2008-09-06 12:10:45 +010020#include <linux/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021
Russell Kinga09e64f2008-08-05 16:14:15 +010022#include <mach/hardware.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <asm/hardware/ioc.h>
24
25#include <asm/mach/time.h>
26
Uwe Kleine-König53985372013-12-19 22:22:23 +010027#define RPC_CLOCK_FREQ 2000000
28#define RPC_LATCH DIV_ROUND_CLOSEST(RPC_CLOCK_FREQ, HZ)
29
Stephen Warren23c197b2012-11-08 11:51:58 -070030static u32 ioc_timer_gettimeoffset(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070031{
32 unsigned int count1, count2, status;
33 long offset;
34
35 ioc_writeb (0, IOC_T0LATCH);
36 barrier ();
37 count1 = ioc_readb(IOC_T0CNTL) | (ioc_readb(IOC_T0CNTH) << 8);
38 barrier ();
39 status = ioc_readb(IOC_IRQREQA);
40 barrier ();
41 ioc_writeb (0, IOC_T0LATCH);
42 barrier ();
43 count2 = ioc_readb(IOC_T0CNTL) | (ioc_readb(IOC_T0CNTH) << 8);
44
45 offset = count2;
46 if (count2 < count1) {
47 /*
48 * We have not had an interrupt between reading count1
49 * and count2.
50 */
51 if (status & (1 << 5))
Uwe Kleine-König53985372013-12-19 22:22:23 +010052 offset -= RPC_LATCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 } else if (count2 > count1) {
54 /*
55 * We have just had another interrupt between reading
56 * count1 and count2.
57 */
Uwe Kleine-König53985372013-12-19 22:22:23 +010058 offset -= RPC_LATCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 }
60
Uwe Kleine-König53985372013-12-19 22:22:23 +010061 offset = (RPC_LATCH - offset) * (tick_nsec / 1000);
62 return DIV_ROUND_CLOSEST(offset, RPC_LATCH) * 1000;
Linus Torvalds1da177e2005-04-16 15:20:36 -070063}
64
65void __init ioctime_init(void)
66{
Uwe Kleine-König53985372013-12-19 22:22:23 +010067 ioc_writeb(RPC_LATCH & 255, IOC_T0LTCHL);
68 ioc_writeb(RPC_LATCH >> 8, IOC_T0LTCHH);
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 ioc_writeb(0, IOC_T0GO);
70}
71
72static irqreturn_t
Linus Torvalds0cd61b62006-10-06 10:53:39 -070073ioc_timer_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -070074{
Linus Torvalds0cd61b62006-10-06 10:53:39 -070075 timer_tick();
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 return IRQ_HANDLED;
77}
78
79static struct irqaction ioc_timer_irq = {
80 .name = "timer",
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 .handler = ioc_timer_interrupt
82};
83
84/*
85 * Set up timer interrupt.
86 */
Stephen Warren6bb27d72012-11-08 12:40:59 -070087void __init ioc_timer_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070088{
Stephen Warren23c197b2012-11-08 11:51:58 -070089 arch_gettimeoffset = ioc_timer_gettimeoffset;
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 ioctime_init();
Russell King927b6c42012-03-01 16:55:44 +000091 setup_irq(IRQ_TIMER0, &ioc_timer_irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -070092}