Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  *  linux/arch/arm/common/time-acorn.c
0004  *
0005  *  Copyright (c) 1996-2000 Russell King.
0006  *
0007  *  Changelog:
0008  *   24-Sep-1996    RMK Created
0009  *   10-Oct-1996    RMK Brought up to date with arch-sa110eval
0010  *   04-Dec-1997    RMK Updated for new arch/arm/time.c
0011  *   13=Jun-2004    DS  Moved to arch/arm/common b/c shared w/CLPS7500
0012  */
0013 #include <linux/clocksource.h>
0014 #include <linux/init.h>
0015 #include <linux/interrupt.h>
0016 #include <linux/irq.h>
0017 #include <linux/io.h>
0018 
0019 #include <mach/hardware.h>
0020 #include <asm/hardware/ioc.h>
0021 
0022 #include <asm/mach/time.h>
0023 
0024 #define RPC_CLOCK_FREQ 2000000
0025 #define RPC_LATCH DIV_ROUND_CLOSEST(RPC_CLOCK_FREQ, HZ)
0026 
0027 static u32 ioc_time;
0028 
0029 static u64 ioc_timer_read(struct clocksource *cs)
0030 {
0031     unsigned int count1, count2, status;
0032     unsigned long flags;
0033     u32 ticks;
0034 
0035     local_irq_save(flags);
0036     ioc_writeb (0, IOC_T0LATCH);
0037     barrier ();
0038     count1 = ioc_readb(IOC_T0CNTL) | (ioc_readb(IOC_T0CNTH) << 8);
0039     barrier ();
0040     status = ioc_readb(IOC_IRQREQA);
0041     barrier ();
0042     ioc_writeb (0, IOC_T0LATCH);
0043     barrier ();
0044     count2 = ioc_readb(IOC_T0CNTL) | (ioc_readb(IOC_T0CNTH) << 8);
0045     ticks = ioc_time + RPC_LATCH - count2;
0046     local_irq_restore(flags);
0047 
0048     if (count2 < count1) {
0049         /*
0050          * The timer has not reloaded between reading count1 and
0051          * count2, check whether an interrupt was actually pending.
0052          */
0053         if (status & (1 << 5))
0054             ticks += RPC_LATCH;
0055     } else if (count2 > count1) {
0056         /*
0057          * The timer has reloaded, so count2 indicates the new
0058          * count since the wrap.  The interrupt would not have
0059          * been processed, so add the missed ticks.
0060          */
0061         ticks += RPC_LATCH;
0062     }
0063 
0064     return ticks;
0065 }
0066 
0067 static struct clocksource ioctime_clocksource = {
0068     .read = ioc_timer_read,
0069     .mask = CLOCKSOURCE_MASK(32),
0070     .rating = 100,
0071 };
0072 
0073 void __init ioctime_init(void)
0074 {
0075     ioc_writeb(RPC_LATCH & 255, IOC_T0LTCHL);
0076     ioc_writeb(RPC_LATCH >> 8, IOC_T0LTCHH);
0077     ioc_writeb(0, IOC_T0GO);
0078 }
0079 
0080 static irqreturn_t
0081 ioc_timer_interrupt(int irq, void *dev_id)
0082 {
0083     ioc_time += RPC_LATCH;
0084     legacy_timer_tick(1);
0085     return IRQ_HANDLED;
0086 }
0087 
0088 /*
0089  * Set up timer interrupt.
0090  */
0091 void __init ioc_timer_init(void)
0092 {
0093     WARN_ON(clocksource_register_hz(&ioctime_clocksource, RPC_CLOCK_FREQ));
0094     ioctime_init();
0095     if (request_irq(IRQ_TIMER0, ioc_timer_interrupt, 0, "timer", NULL))
0096         pr_err("Failed to request irq %d (timer)\n", IRQ_TIMER0);
0097 }