time.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. /*
  2. * Copyright (C) 2004-2006 Atmel Corporation
  3. *
  4. * Based on MIPS implementation arch/mips/kernel/time.c
  5. * Copyright 2001 MontaVista Software Inc.
  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/clk.h>
  12. #include <linux/clocksource.h>
  13. #include <linux/time.h>
  14. #include <linux/module.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/irq.h>
  17. #include <linux/kernel_stat.h>
  18. #include <linux/errno.h>
  19. #include <linux/init.h>
  20. #include <linux/profile.h>
  21. #include <linux/sysdev.h>
  22. #include <asm/div64.h>
  23. #include <asm/sysreg.h>
  24. #include <asm/io.h>
  25. #include <asm/sections.h>
  26. static cycle_t read_cycle_count(void)
  27. {
  28. return (cycle_t)sysreg_read(COUNT);
  29. }
  30. static struct clocksource clocksource_avr32 = {
  31. .name = "avr32",
  32. .rating = 350,
  33. .read = read_cycle_count,
  34. .mask = CLOCKSOURCE_MASK(32),
  35. .shift = 16,
  36. .is_continuous = 1,
  37. };
  38. /*
  39. * By default we provide the null RTC ops
  40. */
  41. static unsigned long null_rtc_get_time(void)
  42. {
  43. return mktime(2004, 1, 1, 0, 0, 0);
  44. }
  45. static int null_rtc_set_time(unsigned long sec)
  46. {
  47. return 0;
  48. }
  49. static unsigned long (*rtc_get_time)(void) = null_rtc_get_time;
  50. static int (*rtc_set_time)(unsigned long) = null_rtc_set_time;
  51. /* how many counter cycles in a jiffy? */
  52. static unsigned long cycles_per_jiffy;
  53. /* cycle counter value at the previous timer interrupt */
  54. static unsigned int timerhi, timerlo;
  55. /* the count value for the next timer interrupt */
  56. static unsigned int expirelo;
  57. static void avr32_timer_ack(void)
  58. {
  59. unsigned int count;
  60. /* Ack this timer interrupt and set the next one */
  61. expirelo += cycles_per_jiffy;
  62. if (expirelo == 0) {
  63. printk(KERN_DEBUG "expirelo == 0\n");
  64. sysreg_write(COMPARE, expirelo + 1);
  65. } else {
  66. sysreg_write(COMPARE, expirelo);
  67. }
  68. /* Check to see if we have missed any timer interrupts */
  69. count = sysreg_read(COUNT);
  70. if ((count - expirelo) < 0x7fffffff) {
  71. expirelo = count + cycles_per_jiffy;
  72. sysreg_write(COMPARE, expirelo);
  73. }
  74. }
  75. static unsigned int avr32_hpt_read(void)
  76. {
  77. return sysreg_read(COUNT);
  78. }
  79. /*
  80. * Taken from MIPS c0_hpt_timer_init().
  81. *
  82. * Why is it so complicated, and what is "count"? My assumption is
  83. * that `count' specifies the "reference cycle", i.e. the cycle since
  84. * reset that should mean "zero". The reason COUNT is written twice is
  85. * probably to make sure we don't get any timer interrupts while we
  86. * are messing with the counter.
  87. */
  88. static void avr32_hpt_init(unsigned int count)
  89. {
  90. count = sysreg_read(COUNT) - count;
  91. expirelo = (count / cycles_per_jiffy + 1) * cycles_per_jiffy;
  92. sysreg_write(COUNT, expirelo - cycles_per_jiffy);
  93. sysreg_write(COMPARE, expirelo);
  94. sysreg_write(COUNT, count);
  95. }
  96. /*
  97. * local_timer_interrupt() does profiling and process accounting on a
  98. * per-CPU basis.
  99. *
  100. * In UP mode, it is invoked from the (global) timer_interrupt.
  101. */
  102. static void local_timer_interrupt(int irq, void *dev_id)
  103. {
  104. if (current->pid)
  105. profile_tick(CPU_PROFILING);
  106. update_process_times(user_mode(get_irq_regs()));
  107. }
  108. static irqreturn_t
  109. timer_interrupt(int irq, void *dev_id)
  110. {
  111. unsigned int count;
  112. /* ack timer interrupt and try to set next interrupt */
  113. count = avr32_hpt_read();
  114. avr32_timer_ack();
  115. /* Update timerhi/timerlo for intra-jiffy calibration */
  116. timerhi += count < timerlo; /* Wrap around */
  117. timerlo = count;
  118. /*
  119. * Call the generic timer interrupt handler
  120. */
  121. write_seqlock(&xtime_lock);
  122. do_timer(1);
  123. write_sequnlock(&xtime_lock);
  124. /*
  125. * In UP mode, we call local_timer_interrupt() to do profiling
  126. * and process accounting.
  127. *
  128. * SMP is not supported yet.
  129. */
  130. local_timer_interrupt(irq, dev_id);
  131. return IRQ_HANDLED;
  132. }
  133. static struct irqaction timer_irqaction = {
  134. .handler = timer_interrupt,
  135. .flags = IRQF_DISABLED,
  136. .name = "timer",
  137. };
  138. void __init time_init(void)
  139. {
  140. unsigned long mult, shift, count_hz;
  141. int ret;
  142. xtime.tv_sec = rtc_get_time();
  143. xtime.tv_nsec = 0;
  144. set_normalized_timespec(&wall_to_monotonic,
  145. -xtime.tv_sec, -xtime.tv_nsec);
  146. printk("Before time_init: count=%08lx, compare=%08lx\n",
  147. (unsigned long)sysreg_read(COUNT),
  148. (unsigned long)sysreg_read(COMPARE));
  149. count_hz = clk_get_rate(boot_cpu_data.clk);
  150. shift = clocksource_avr32.shift;
  151. mult = clocksource_hz2mult(count_hz, shift);
  152. clocksource_avr32.mult = mult;
  153. printk("Cycle counter: mult=%lu, shift=%lu\n", mult, shift);
  154. {
  155. u64 tmp;
  156. tmp = TICK_NSEC;
  157. tmp <<= shift;
  158. tmp += mult / 2;
  159. do_div(tmp, mult);
  160. cycles_per_jiffy = tmp;
  161. }
  162. /* This sets up the high precision timer for the first interrupt. */
  163. avr32_hpt_init(avr32_hpt_read());
  164. printk("After time_init: count=%08lx, compare=%08lx\n",
  165. (unsigned long)sysreg_read(COUNT),
  166. (unsigned long)sysreg_read(COMPARE));
  167. ret = clocksource_register(&clocksource_avr32);
  168. if (ret)
  169. printk(KERN_ERR
  170. "timer: could not register clocksource: %d\n", ret);
  171. ret = setup_irq(0, &timer_irqaction);
  172. if (ret)
  173. printk("timer: could not request IRQ 0: %d\n", ret);
  174. }
  175. static struct sysdev_class timer_class = {
  176. set_kset_name("timer"),
  177. };
  178. static struct sys_device timer_device = {
  179. .id = 0,
  180. .cls = &timer_class,
  181. };
  182. static int __init init_timer_sysfs(void)
  183. {
  184. int err = sysdev_class_register(&timer_class);
  185. if (!err)
  186. err = sysdev_register(&timer_device);
  187. return err;
  188. }
  189. device_initcall(init_timer_sysfs);