time.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /* MN10300 Low level time management
  2. *
  3. * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
  4. * Written by David Howells (dhowells@redhat.com)
  5. * - Derived from arch/i386/kernel/time.c
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public Licence
  9. * as published by the Free Software Foundation; either version
  10. * 2 of the Licence, or (at your option) any later version.
  11. */
  12. #include <linux/sched.h>
  13. #include <linux/kernel.h>
  14. #include <linux/interrupt.h>
  15. #include <linux/time.h>
  16. #include <linux/init.h>
  17. #include <linux/smp.h>
  18. #include <linux/profile.h>
  19. #include <asm/irq.h>
  20. #include <asm/div64.h>
  21. #include <asm/processor.h>
  22. #include <asm/intctl-regs.h>
  23. #include <asm/rtc.h>
  24. #ifdef CONFIG_MN10300_RTC
  25. unsigned long mn10300_ioclk; /* system I/O clock frequency */
  26. unsigned long mn10300_iobclk; /* system I/O clock frequency */
  27. unsigned long mn10300_tsc_per_HZ; /* number of ioclks per jiffy */
  28. #endif /* CONFIG_MN10300_RTC */
  29. static unsigned long mn10300_last_tsc; /* time-stamp counter at last time
  30. * interrupt occurred */
  31. static irqreturn_t timer_interrupt(int irq, void *dev_id);
  32. static struct irqaction timer_irq = {
  33. .handler = timer_interrupt,
  34. .flags = IRQF_DISABLED | IRQF_SHARED | IRQF_TIMER,
  35. .mask = CPU_MASK_NONE,
  36. .name = "timer",
  37. };
  38. /*
  39. * scheduler clock - returns current time in nanosec units.
  40. */
  41. unsigned long long sched_clock(void)
  42. {
  43. union {
  44. unsigned long long l;
  45. u32 w[2];
  46. } quot;
  47. quot.w[0] = mn10300_last_tsc - get_cycles();
  48. quot.w[1] = 1000000000;
  49. asm("mulu %2,%3,%0,%1"
  50. : "=r"(quot.w[1]), "=r"(quot.w[0])
  51. : "0"(quot.w[1]), "1"(quot.w[0])
  52. : "cc");
  53. do_div(quot.l, MN10300_TSCCLK);
  54. return quot.l;
  55. }
  56. /*
  57. * advance the kernel's time keeping clocks (xtime and jiffies)
  58. * - we use Timer 0 & 1 cascaded as a clock to nudge us the next time
  59. * there's a need to update
  60. */
  61. static irqreturn_t timer_interrupt(int irq, void *dev_id)
  62. {
  63. unsigned tsc, elapse;
  64. write_seqlock(&xtime_lock);
  65. while (tsc = get_cycles(),
  66. elapse = mn10300_last_tsc - tsc, /* time elapsed since last
  67. * tick */
  68. elapse > MN10300_TSC_PER_HZ
  69. ) {
  70. mn10300_last_tsc -= MN10300_TSC_PER_HZ;
  71. /* advance the kernel's time tracking system */
  72. profile_tick(CPU_PROFILING);
  73. do_timer(1);
  74. check_rtc_time();
  75. }
  76. write_sequnlock(&xtime_lock);
  77. update_process_times(user_mode(get_irq_regs()));
  78. return IRQ_HANDLED;
  79. }
  80. /*
  81. * initialise the various timers used by the main part of the kernel
  82. */
  83. void __init time_init(void)
  84. {
  85. /* we need the prescalar running to be able to use IOCLK/8
  86. * - IOCLK runs at 1/4 (ST5 open) or 1/8 (ST5 closed) internal CPU clock
  87. * - IOCLK runs at Fosc rate (crystal speed)
  88. */
  89. TMPSCNT |= TMPSCNT_ENABLE;
  90. startup_timestamp_counter();
  91. printk(KERN_INFO
  92. "timestamp counter I/O clock running at %lu.%02lu"
  93. " (calibrated against RTC)\n",
  94. MN10300_TSCCLK / 1000000, (MN10300_TSCCLK / 10000) % 100);
  95. xtime.tv_sec = get_initial_rtc_time();
  96. xtime.tv_nsec = 0;
  97. mn10300_last_tsc = TMTSCBC;
  98. /* use timer 0 & 1 cascaded to tick at as close to HZ as possible */
  99. setup_irq(TMJCIRQ, &timer_irq);
  100. set_intr_level(TMJCIRQ, TMJCICR_LEVEL);
  101. startup_jiffies_counter();
  102. #ifdef CONFIG_MN10300_WD_TIMER
  103. /* start the watchdog timer */
  104. watchdog_go();
  105. #endif
  106. }