time.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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 <asm/arch/pm.h>
  17. static cycle_t read_cycle_count(void)
  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. /*
  40. * Disable the interrupt until the clockevent subsystem
  41. * reprograms it.
  42. */
  43. sysreg_write(COMPARE, 0);
  44. evdev->event_handler(evdev);
  45. return IRQ_HANDLED;
  46. }
  47. static struct irqaction timer_irqaction = {
  48. .handler = timer_interrupt,
  49. .flags = IRQF_TIMER | IRQF_DISABLED,
  50. .name = "avr32_comparator",
  51. };
  52. static int comparator_next_event(unsigned long delta,
  53. struct clock_event_device *evdev)
  54. {
  55. unsigned long flags;
  56. raw_local_irq_save(flags);
  57. /* The time to read COUNT then update COMPARE must be less
  58. * than the min_delta_ns value for this clockevent source.
  59. */
  60. sysreg_write(COMPARE, (sysreg_read(COUNT) + delta) ? : 1);
  61. raw_local_irq_restore(flags);
  62. return 0;
  63. }
  64. static void comparator_mode(enum clock_event_mode mode,
  65. struct clock_event_device *evdev)
  66. {
  67. switch (mode) {
  68. case CLOCK_EVT_MODE_ONESHOT:
  69. pr_debug("%s: start\n", evdev->name);
  70. /* FALLTHROUGH */
  71. case CLOCK_EVT_MODE_RESUME:
  72. cpu_disable_idle_sleep();
  73. break;
  74. case CLOCK_EVT_MODE_UNUSED:
  75. case CLOCK_EVT_MODE_SHUTDOWN:
  76. sysreg_write(COMPARE, 0);
  77. pr_debug("%s: stop\n", evdev->name);
  78. cpu_enable_idle_sleep();
  79. break;
  80. default:
  81. BUG();
  82. }
  83. }
  84. static struct clock_event_device comparator = {
  85. .name = "avr32_comparator",
  86. .features = CLOCK_EVT_FEAT_ONESHOT,
  87. .shift = 16,
  88. .rating = 50,
  89. .cpumask = CPU_MASK_CPU0,
  90. .set_next_event = comparator_next_event,
  91. .set_mode = comparator_mode,
  92. };
  93. void __init time_init(void)
  94. {
  95. unsigned long counter_hz;
  96. int ret;
  97. xtime.tv_sec = mktime(2007, 1, 1, 0, 0, 0);
  98. xtime.tv_nsec = 0;
  99. set_normalized_timespec(&wall_to_monotonic,
  100. -xtime.tv_sec, -xtime.tv_nsec);
  101. /* figure rate for counter */
  102. counter_hz = clk_get_rate(boot_cpu_data.clk);
  103. counter.mult = clocksource_hz2mult(counter_hz, counter.shift);
  104. ret = clocksource_register(&counter);
  105. if (ret)
  106. pr_debug("timer: could not register clocksource: %d\n", ret);
  107. /* setup COMPARE clockevent */
  108. comparator.mult = div_sc(counter_hz, NSEC_PER_SEC, comparator.shift);
  109. comparator.max_delta_ns = clockevent_delta2ns((u32)~0, &comparator);
  110. comparator.min_delta_ns = clockevent_delta2ns(50, &comparator) + 1;
  111. sysreg_write(COMPARE, 0);
  112. timer_irqaction.dev_id = &comparator;
  113. ret = setup_irq(0, &timer_irqaction);
  114. if (ret)
  115. pr_debug("timer: could not request IRQ 0: %d\n", ret);
  116. else {
  117. clockevents_register_device(&comparator);
  118. pr_info("%s: irq 0, %lu.%03lu MHz\n", comparator.name,
  119. ((counter_hz + 500) / 1000) / 1000,
  120. ((counter_hz + 500) / 1000) % 1000);
  121. }
  122. }