time.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * linux/arch/m32r/kernel/time.c
  4. *
  5. * Copyright (c) 2001, 2002 Hiroyuki Kondo, Hirokazu Takata,
  6. * Hitoshi Yamamoto
  7. * Taken from i386 version.
  8. * Copyright (C) 1991, 1992, 1995 Linus Torvalds
  9. * Copyright (C) 1996, 1997, 1998 Ralf Baechle
  10. *
  11. * This file contains the time handling details for PC-style clocks as
  12. * found in some MIPS systems.
  13. *
  14. * Some code taken from sh version.
  15. * Copyright (C) 1999 Tetsuya Okada & Niibe Yutaka
  16. * Copyright (C) 2000 Philipp Rumpf <prumpf@tux.org>
  17. */
  18. #undef DEBUG_TIMER
  19. #include <linux/errno.h>
  20. #include <linux/init.h>
  21. #include <linux/module.h>
  22. #include <linux/sched.h>
  23. #include <linux/kernel.h>
  24. #include <linux/param.h>
  25. #include <linux/string.h>
  26. #include <linux/mm.h>
  27. #include <linux/interrupt.h>
  28. #include <linux/profile.h>
  29. #include <asm/io.h>
  30. #include <asm/m32r.h>
  31. #include <asm/hw_irq.h>
  32. #if defined(CONFIG_RTC_DRV_CMOS) || defined(CONFIG_RTC_DRV_CMOS_MODULE)
  33. /* this needs a better home */
  34. DEFINE_SPINLOCK(rtc_lock);
  35. #ifdef CONFIG_RTC_DRV_CMOS_MODULE
  36. EXPORT_SYMBOL(rtc_lock);
  37. #endif
  38. #endif /* pc-style 'CMOS' RTC support */
  39. #ifdef CONFIG_SMP
  40. extern void smp_local_timer_interrupt(void);
  41. #endif
  42. #define TICK_SIZE (tick_nsec / 1000)
  43. /*
  44. * Change this if you have some constant time drift
  45. */
  46. /* This is for machines which generate the exact clock. */
  47. #define USECS_PER_JIFFY (1000000/HZ)
  48. static unsigned long latch;
  49. static u32 m32r_gettimeoffset(void)
  50. {
  51. unsigned long elapsed_time = 0; /* [us] */
  52. #if defined(CONFIG_CHIP_M32102) || defined(CONFIG_CHIP_XNUX2) \
  53. || defined(CONFIG_CHIP_VDEC2) || defined(CONFIG_CHIP_M32700) \
  54. || defined(CONFIG_CHIP_OPSP) || defined(CONFIG_CHIP_M32104)
  55. #ifndef CONFIG_SMP
  56. unsigned long count;
  57. /* timer count may underflow right here */
  58. count = inl(M32R_MFT2CUT_PORTL);
  59. if (inl(M32R_ICU_CR18_PORTL) & 0x00000100) /* underflow check */
  60. count = 0;
  61. count = (latch - count) * TICK_SIZE;
  62. elapsed_time = DIV_ROUND_CLOSEST(count, latch);
  63. /* NOTE: LATCH is equal to the "interval" value (= reload count). */
  64. #else /* CONFIG_SMP */
  65. unsigned long count;
  66. static unsigned long p_jiffies = -1;
  67. static unsigned long p_count = 0;
  68. /* timer count may underflow right here */
  69. count = inl(M32R_MFT2CUT_PORTL);
  70. if (jiffies == p_jiffies && count > p_count)
  71. count = 0;
  72. p_jiffies = jiffies;
  73. p_count = count;
  74. count = (latch - count) * TICK_SIZE;
  75. elapsed_time = DIV_ROUND_CLOSEST(count, latch);
  76. /* NOTE: LATCH is equal to the "interval" value (= reload count). */
  77. #endif /* CONFIG_SMP */
  78. #elif defined(CONFIG_CHIP_M32310)
  79. #warning do_gettimeoffse not implemented
  80. #else
  81. #error no chip configuration
  82. #endif
  83. return elapsed_time * 1000;
  84. }
  85. /*
  86. * timer_interrupt() needs to keep up the real-time clock,
  87. * as well as call the "xtime_update()" routine every clocktick
  88. */
  89. static irqreturn_t timer_interrupt(int irq, void *dev_id)
  90. {
  91. #ifndef CONFIG_SMP
  92. profile_tick(CPU_PROFILING);
  93. #endif
  94. xtime_update(1);
  95. #ifndef CONFIG_SMP
  96. update_process_times(user_mode(get_irq_regs()));
  97. #endif
  98. /* As we return to user mode fire off the other CPU schedulers..
  99. this is basically because we don't yet share IRQ's around.
  100. This message is rigged to be safe on the 386 - basically it's
  101. a hack, so don't look closely for now.. */
  102. #ifdef CONFIG_SMP
  103. smp_local_timer_interrupt();
  104. smp_send_timer();
  105. #endif
  106. return IRQ_HANDLED;
  107. }
  108. static struct irqaction irq0 = {
  109. .handler = timer_interrupt,
  110. .name = "MFT2",
  111. };
  112. void read_persistent_clock(struct timespec *ts)
  113. {
  114. unsigned int epoch, year, mon, day, hour, min, sec;
  115. sec = min = hour = day = mon = year = 0;
  116. epoch = 0;
  117. year = 23;
  118. mon = 4;
  119. day = 17;
  120. /* Attempt to guess the epoch. This is the same heuristic as in rtc.c
  121. so no stupid things will happen to timekeeping. Who knows, maybe
  122. Ultrix also uses 1952 as epoch ... */
  123. if (year > 10 && year < 44)
  124. epoch = 1980;
  125. else if (year < 96)
  126. epoch = 1952;
  127. year += epoch;
  128. ts->tv_sec = mktime(year, mon, day, hour, min, sec);
  129. ts->tv_nsec = (INITIAL_JIFFIES % HZ) * (NSEC_PER_SEC / HZ);
  130. }
  131. void __init time_init(void)
  132. {
  133. arch_gettimeoffset = m32r_gettimeoffset;
  134. #if defined(CONFIG_CHIP_M32102) || defined(CONFIG_CHIP_XNUX2) \
  135. || defined(CONFIG_CHIP_VDEC2) || defined(CONFIG_CHIP_M32700) \
  136. || defined(CONFIG_CHIP_OPSP) || defined(CONFIG_CHIP_M32104)
  137. /* M32102 MFT setup */
  138. setup_irq(M32R_IRQ_MFT2, &irq0);
  139. {
  140. unsigned long bus_clock;
  141. unsigned short divide;
  142. bus_clock = boot_cpu_data.bus_clock;
  143. divide = boot_cpu_data.timer_divide;
  144. latch = DIV_ROUND_CLOSEST(bus_clock/divide, HZ);
  145. printk("Timer start : latch = %ld\n", latch);
  146. outl((M32R_MFTMOD_CC_MASK | M32R_MFTMOD_TCCR \
  147. |M32R_MFTMOD_CSSEL011), M32R_MFT2MOD_PORTL);
  148. outl(latch, M32R_MFT2RLD_PORTL);
  149. outl(latch, M32R_MFT2CUT_PORTL);
  150. outl(0, M32R_MFT2CMPRLD_PORTL);
  151. outl((M32R_MFTCR_MFT2MSK|M32R_MFTCR_MFT2EN), M32R_MFTCR_PORTL);
  152. }
  153. #elif defined(CONFIG_CHIP_M32310)
  154. #warning time_init not implemented
  155. #else
  156. #error no chip configuration
  157. #endif
  158. }