time.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /*
  2. * Copyright (C) 2004-2007 Atmel Corporation
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 as
  6. * published by the Free Software Foundation.
  7. */
  8. #include <linux/clk.h>
  9. #include <linux/clockchips.h>
  10. #include <linux/init.h>
  11. #include <linux/interrupt.h>
  12. #include <linux/irq.h>
  13. #include <linux/kernel.h>
  14. #include <linux/time.h>
  15. #include <asm/sysreg.h>
  16. #include <mach/pm.h>
  17. static cycle_t read_cycle_count(struct clocksource *cs)
  18. {
  19. return (cycle_t)sysreg_read(COUNT);
  20. }
  21. /*
  22. * The architectural cycle count registers are a fine clocksource unless
  23. * the system idle loop use sleep states like "idle": the CPU cycles
  24. * measured by COUNT (and COMPARE) don't happen during sleep states.
  25. * Their duration also changes if cpufreq changes the CPU clock rate.
  26. * So we rate the clocksource using COUNT as very low quality.
  27. */
  28. static struct clocksource counter = {
  29. .name = "avr32_counter",
  30. .rating = 50,
  31. .read = read_cycle_count,
  32. .mask = CLOCKSOURCE_MASK(32),
  33. .shift = 16,
  34. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  35. };
  36. static irqreturn_t timer_interrupt(int irq, void *dev_id)
  37. {
  38. struct clock_event_device *evdev = dev_id;
  39. if (unlikely(!(intc_get_pending(0) & 1)))
  40. return IRQ_NONE;
  41. /*
  42. * Disable the interrupt until the clockevent subsystem
  43. * reprograms it.
  44. */
  45. sysreg_write(COMPARE, 0);
  46. evdev->event_handler(evdev);
  47. return IRQ_HANDLED;
  48. }
  49. static struct irqaction timer_irqaction = {
  50. .handler = timer_interrupt,
  51. /* Oprofile uses the same irq as the timer, so allow it to be shared */
  52. .flags = IRQF_TIMER | IRQF_DISABLED | IRQF_SHARED,
  53. .name = "avr32_comparator",
  54. };
  55. static int comparator_next_event(unsigned long delta,
  56. struct clock_event_device *evdev)
  57. {
  58. unsigned long flags;
  59. raw_local_irq_save(flags);
  60. /* The time to read COUNT then update COMPARE must be less
  61. * than the min_delta_ns value for this clockevent source.
  62. */
  63. sysreg_write(COMPARE, (sysreg_read(COUNT) + delta) ? : 1);
  64. raw_local_irq_restore(flags);
  65. return 0;
  66. }
  67. static void comparator_mode(enum clock_event_mode mode,
  68. struct clock_event_device *evdev)
  69. {
  70. switch (mode) {
  71. case CLOCK_EVT_MODE_ONESHOT:
  72. pr_debug("%s: start\n", evdev->name);
  73. /* FALLTHROUGH */
  74. case CLOCK_EVT_MODE_RESUME:
  75. cpu_disable_idle_sleep();
  76. break;
  77. case CLOCK_EVT_MODE_UNUSED:
  78. case CLOCK_EVT_MODE_SHUTDOWN:
  79. sysreg_write(COMPARE, 0);
  80. pr_debug("%s: stop\n", evdev->name);
  81. cpu_enable_idle_sleep();
  82. break;
  83. default:
  84. BUG();
  85. }
  86. }
  87. static struct clock_event_device comparator = {
  88. .name = "avr32_comparator",
  89. .features = CLOCK_EVT_FEAT_ONESHOT,
  90. .shift = 16,
  91. .rating = 50,
  92. .set_next_event = comparator_next_event,
  93. .set_mode = comparator_mode,
  94. };
  95. void __init time_init(void)
  96. {
  97. unsigned long counter_hz;
  98. int ret;
  99. xtime.tv_sec = mktime(2007, 1, 1, 0, 0, 0);
  100. xtime.tv_nsec = 0;
  101. set_normalized_timespec(&wall_to_monotonic,
  102. -xtime.tv_sec, -xtime.tv_nsec);
  103. /* figure rate for counter */
  104. counter_hz = clk_get_rate(boot_cpu_data.clk);
  105. counter.mult = clocksource_hz2mult(counter_hz, counter.shift);
  106. ret = clocksource_register(&counter);
  107. if (ret)
  108. pr_debug("timer: could not register clocksource: %d\n", ret);
  109. /* setup COMPARE clockevent */
  110. comparator.mult = div_sc(counter_hz, NSEC_PER_SEC, comparator.shift);
  111. comparator.max_delta_ns = clockevent_delta2ns((u32)~0, &comparator);
  112. comparator.min_delta_ns = clockevent_delta2ns(50, &comparator) + 1;
  113. comparator.cpumask = cpumask_of(0);
  114. sysreg_write(COMPARE, 0);
  115. timer_irqaction.dev_id = &comparator;
  116. ret = setup_irq(0, &timer_irqaction);
  117. if (ret)
  118. pr_debug("timer: could not request IRQ 0: %d\n", ret);
  119. else {
  120. clockevents_register_device(&comparator);
  121. pr_info("%s: irq 0, %lu.%03lu MHz\n", comparator.name,
  122. ((counter_hz + 500) / 1000) / 1000,
  123. ((counter_hz + 500) / 1000) % 1000);
  124. }
  125. }