timer.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. switch (mode) {
  46. case CLOCK_EVT_MODE_PERIODIC:
  47. /* count current value? */
  48. writel(readl(mtu_base + MTU_IMSC) | 1, mtu_base + MTU_IMSC);
  49. break;
  50. case CLOCK_EVT_MODE_ONESHOT:
  51. BUG(); /* Not supported, yet */
  52. /* FALLTHROUGH */
  53. case CLOCK_EVT_MODE_SHUTDOWN:
  54. case CLOCK_EVT_MODE_UNUSED:
  55. writel(readl(mtu_base + MTU_IMSC) & ~1, mtu_base + MTU_IMSC);
  56. break;
  57. case CLOCK_EVT_MODE_RESUME:
  58. break;
  59. }
  60. }
  61. static struct clock_event_device nmdk_clkevt = {
  62. .name = "mtu_0",
  63. .features = CLOCK_EVT_FEAT_PERIODIC,
  64. .shift = 32,
  65. .rating = 100,
  66. .set_mode = nmdk_clkevt_mode,
  67. };
  68. /*
  69. * IRQ Handler for the timer 0 of the MTU block. The irq is not shared
  70. * as we are the only users of mtu0 by now.
  71. */
  72. static irqreturn_t nmdk_timer_interrupt(int irq, void *dev_id)
  73. {
  74. /* ack: "interrupt clear register" */
  75. writel(1 << 0, mtu_base + MTU_ICR);
  76. /* we can't count lost ticks, unfortunately */
  77. nmdk_count += nmdk_cycle;
  78. nmdk_clkevt.event_handler(&nmdk_clkevt);
  79. return IRQ_HANDLED;
  80. }
  81. /*
  82. * Set up timer interrupt, and return the current time in seconds.
  83. */
  84. static struct irqaction nmdk_timer_irq = {
  85. .name = "Nomadik Timer Tick",
  86. .flags = IRQF_DISABLED | IRQF_TIMER,
  87. .handler = nmdk_timer_interrupt,
  88. };
  89. static void nmdk_timer_reset(void)
  90. {
  91. u32 cr;
  92. writel(0, mtu_base + MTU_CR(0)); /* off */
  93. /* configure load and background-load, and fire it up */
  94. writel(nmdk_cycle, mtu_base + MTU_LR(0));
  95. writel(nmdk_cycle, mtu_base + MTU_BGLR(0));
  96. cr = MTU_CRn_PERIODIC | MTU_CRn_PRESCALE_1 | MTU_CRn_32BITS;
  97. writel(cr, mtu_base + MTU_CR(0));
  98. writel(cr | MTU_CRn_ENA, mtu_base + MTU_CR(0));
  99. }
  100. void __init nmdk_timer_init(void)
  101. {
  102. unsigned long rate;
  103. int bits;
  104. rate = CLOCK_TICK_RATE; /* 2.4MHz */
  105. nmdk_cycle = (rate + HZ/2) / HZ;
  106. /* Init the timer and register clocksource */
  107. nmdk_timer_reset();
  108. nmdk_clksrc.mult = clocksource_hz2mult(rate, nmdk_clksrc.shift);
  109. bits = 8*sizeof(nmdk_count);
  110. nmdk_clksrc.mask = CLOCKSOURCE_MASK(bits);
  111. if (clocksource_register(&nmdk_clksrc))
  112. printk(KERN_ERR "timer: failed to initialize clock "
  113. "source %s\n", nmdk_clksrc.name);
  114. /* Register irq and clockevents */
  115. setup_irq(IRQ_MTU0, &nmdk_timer_irq);
  116. nmdk_clkevt.mult = div_sc(rate, NSEC_PER_SEC, nmdk_clkevt.shift);
  117. nmdk_clkevt.cpumask = cpumask_of(0);
  118. clockevents_register_device(&nmdk_clkevt);
  119. }