timer.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*
  2. * linux/arch/arm/mach-nomadik/timer.c
  3. *
  4. * Copyright (C) 2008 STMicroelectronics
  5. * Copyright (C) 2009 Alessandro Rubini, somewhat based on at91sam926x
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2, as
  9. * published by the Free Software Foundation.
  10. */
  11. #include <linux/init.h>
  12. #include <linux/interrupt.h>
  13. #include <linux/irq.h>
  14. #include <linux/io.h>
  15. #include <linux/clockchips.h>
  16. #include <linux/jiffies.h>
  17. #include <asm/mach/time.h>
  18. #include <plat/mtu.h>
  19. static u32 nmdk_count; /* accumulated count */
  20. static u32 nmdk_cycle; /* write-once */
  21. /* setup by the platform code */
  22. void __iomem *mtu_base;
  23. /*
  24. * clocksource: the MTU device is a decrementing counters, so we negate
  25. * the value being read.
  26. */
  27. static cycle_t nmdk_read_timer(struct clocksource *cs)
  28. {
  29. u32 count = readl(mtu_base + MTU_VAL(0));
  30. return nmdk_count + nmdk_cycle - count;
  31. }
  32. static struct clocksource nmdk_clksrc = {
  33. .name = "mtu_0",
  34. .rating = 120,
  35. .read = nmdk_read_timer,
  36. .shift = 20,
  37. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  38. };
  39. /*
  40. * Clockevent device: currently only periodic mode is supported
  41. */
  42. static void nmdk_clkevt_mode(enum clock_event_mode mode,
  43. struct clock_event_device *dev)
  44. {
  45. unsigned long flags;
  46. switch (mode) {
  47. case CLOCK_EVT_MODE_PERIODIC:
  48. /* enable interrupts -- and count current value? */
  49. raw_local_irq_save(flags);
  50. writel(readl(mtu_base + MTU_IMSC) | 1, mtu_base + MTU_IMSC);
  51. raw_local_irq_restore(flags);
  52. break;
  53. case CLOCK_EVT_MODE_ONESHOT:
  54. BUG(); /* Not supported, yet */
  55. /* FALLTHROUGH */
  56. case CLOCK_EVT_MODE_SHUTDOWN:
  57. case CLOCK_EVT_MODE_UNUSED:
  58. /* disable irq */
  59. raw_local_irq_save(flags);
  60. writel(readl(mtu_base + MTU_IMSC) & ~1, mtu_base + MTU_IMSC);
  61. raw_local_irq_restore(flags);
  62. break;
  63. case CLOCK_EVT_MODE_RESUME:
  64. break;
  65. }
  66. }
  67. static struct clock_event_device nmdk_clkevt = {
  68. .name = "mtu_0",
  69. .features = CLOCK_EVT_FEAT_PERIODIC,
  70. .shift = 32,
  71. .rating = 100,
  72. .set_mode = nmdk_clkevt_mode,
  73. };
  74. /*
  75. * IRQ Handler for the timer 0 of the MTU block. The irq is not shared
  76. * as we are the only users of mtu0 by now.
  77. */
  78. static irqreturn_t nmdk_timer_interrupt(int irq, void *dev_id)
  79. {
  80. /* ack: "interrupt clear register" */
  81. writel(1 << 0, mtu_base + MTU_ICR);
  82. /* we can't count lost ticks, unfortunately */
  83. nmdk_count += nmdk_cycle;
  84. nmdk_clkevt.event_handler(&nmdk_clkevt);
  85. return IRQ_HANDLED;
  86. }
  87. /*
  88. * Set up timer interrupt, and return the current time in seconds.
  89. */
  90. static struct irqaction nmdk_timer_irq = {
  91. .name = "Nomadik Timer Tick",
  92. .flags = IRQF_DISABLED | IRQF_TIMER,
  93. .handler = nmdk_timer_interrupt,
  94. };
  95. static void nmdk_timer_reset(void)
  96. {
  97. u32 cr;
  98. writel(0, mtu_base + MTU_CR(0)); /* off */
  99. /* configure load and background-load, and fire it up */
  100. writel(nmdk_cycle, mtu_base + MTU_LR(0));
  101. writel(nmdk_cycle, mtu_base + MTU_BGLR(0));
  102. cr = MTU_CRn_PERIODIC | MTU_CRn_PRESCALE_1 | MTU_CRn_32BITS;
  103. writel(cr, mtu_base + MTU_CR(0));
  104. writel(cr | MTU_CRn_ENA, mtu_base + MTU_CR(0));
  105. }
  106. void __init nmdk_timer_init(void)
  107. {
  108. unsigned long rate;
  109. int bits;
  110. rate = CLOCK_TICK_RATE; /* 2.4MHz */
  111. nmdk_cycle = (rate + HZ/2) / HZ;
  112. /* Init the timer and register clocksource */
  113. nmdk_timer_reset();
  114. nmdk_clksrc.mult = clocksource_hz2mult(rate, nmdk_clksrc.shift);
  115. bits = 8*sizeof(nmdk_count);
  116. nmdk_clksrc.mask = CLOCKSOURCE_MASK(bits);
  117. if (clocksource_register(&nmdk_clksrc))
  118. printk(KERN_ERR "timer: failed to initialize clock "
  119. "source %s\n", nmdk_clksrc.name);
  120. /* Register irq and clockevents */
  121. setup_irq(IRQ_MTU0, &nmdk_timer_irq);
  122. nmdk_clkevt.mult = div_sc(rate, NSEC_PER_SEC, nmdk_clkevt.shift);
  123. nmdk_clkevt.cpumask = cpumask_of(0);
  124. clockevents_register_device(&nmdk_clkevt);
  125. }