time.c 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /*
  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>
  19. #include <linux/irq.h>
  20. #include <linux/io.h>
  21. #include <mach/hardware.h>
  22. #include <asm/hardware/ioc.h>
  23. #include <asm/mach/time.h>
  24. #define RPC_CLOCK_FREQ 2000000
  25. #define RPC_LATCH DIV_ROUND_CLOSEST(RPC_CLOCK_FREQ, HZ)
  26. static u32 ioc_timer_gettimeoffset(void)
  27. {
  28. unsigned int count1, count2, status;
  29. long offset;
  30. ioc_writeb (0, IOC_T0LATCH);
  31. barrier ();
  32. count1 = ioc_readb(IOC_T0CNTL) | (ioc_readb(IOC_T0CNTH) << 8);
  33. barrier ();
  34. status = ioc_readb(IOC_IRQREQA);
  35. barrier ();
  36. ioc_writeb (0, IOC_T0LATCH);
  37. barrier ();
  38. count2 = ioc_readb(IOC_T0CNTL) | (ioc_readb(IOC_T0CNTH) << 8);
  39. offset = count2;
  40. if (count2 < count1) {
  41. /*
  42. * We have not had an interrupt between reading count1
  43. * and count2.
  44. */
  45. if (status & (1 << 5))
  46. offset -= RPC_LATCH;
  47. } else if (count2 > count1) {
  48. /*
  49. * We have just had another interrupt between reading
  50. * count1 and count2.
  51. */
  52. offset -= RPC_LATCH;
  53. }
  54. offset = (RPC_LATCH - offset) * (tick_nsec / 1000);
  55. return DIV_ROUND_CLOSEST(offset, RPC_LATCH) * 1000;
  56. }
  57. void __init ioctime_init(void)
  58. {
  59. ioc_writeb(RPC_LATCH & 255, IOC_T0LTCHL);
  60. ioc_writeb(RPC_LATCH >> 8, IOC_T0LTCHH);
  61. ioc_writeb(0, IOC_T0GO);
  62. }
  63. static irqreturn_t
  64. ioc_timer_interrupt(int irq, void *dev_id)
  65. {
  66. timer_tick();
  67. return IRQ_HANDLED;
  68. }
  69. static struct irqaction ioc_timer_irq = {
  70. .name = "timer",
  71. .handler = ioc_timer_interrupt
  72. };
  73. /*
  74. * Set up timer interrupt.
  75. */
  76. void __init ioc_timer_init(void)
  77. {
  78. arch_gettimeoffset = ioc_timer_gettimeoffset;
  79. ioctime_init();
  80. setup_irq(IRQ_TIMER0, &ioc_timer_irq);
  81. }