time.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  1. /*
  2. * Copyright 2001 MontaVista Software Inc.
  3. * Author: Jun Sun, jsun@mvista.com or jsun@junsun.net
  4. * Copyright (c) 2003, 2004 Maciej W. Rozycki
  5. *
  6. * Common time service routines for MIPS machines. See
  7. * Documentation/mips/time.README.
  8. *
  9. * This program is free software; you can redistribute it and/or modify it
  10. * under the terms of the GNU General Public License as published by the
  11. * Free Software Foundation; either version 2 of the License, or (at your
  12. * option) any later version.
  13. */
  14. #include <linux/types.h>
  15. #include <linux/kernel.h>
  16. #include <linux/init.h>
  17. #include <linux/sched.h>
  18. #include <linux/param.h>
  19. #include <linux/time.h>
  20. #include <linux/timex.h>
  21. #include <linux/smp.h>
  22. #include <linux/kernel_stat.h>
  23. #include <linux/spinlock.h>
  24. #include <linux/interrupt.h>
  25. #include <linux/module.h>
  26. #include <asm/bootinfo.h>
  27. #include <asm/cache.h>
  28. #include <asm/compiler.h>
  29. #include <asm/cpu.h>
  30. #include <asm/cpu-features.h>
  31. #include <asm/div64.h>
  32. #include <asm/sections.h>
  33. #include <asm/time.h>
  34. /*
  35. * The integer part of the number of usecs per jiffy is taken from tick,
  36. * but the fractional part is not recorded, so we calculate it using the
  37. * initial value of HZ. This aids systems where tick isn't really an
  38. * integer (e.g. for HZ = 128).
  39. */
  40. #define USECS_PER_JIFFY TICK_SIZE
  41. #define USECS_PER_JIFFY_FRAC ((unsigned long)(u32)((1000000ULL << 32) / HZ))
  42. #define TICK_SIZE (tick_nsec / 1000)
  43. /*
  44. * forward reference
  45. */
  46. DEFINE_SPINLOCK(rtc_lock);
  47. /*
  48. * By default we provide the null RTC ops
  49. */
  50. static unsigned long null_rtc_get_time(void)
  51. {
  52. return mktime(2000, 1, 1, 0, 0, 0);
  53. }
  54. static int null_rtc_set_time(unsigned long sec)
  55. {
  56. return 0;
  57. }
  58. unsigned long (*rtc_mips_get_time)(void) = null_rtc_get_time;
  59. int (*rtc_mips_set_time)(unsigned long) = null_rtc_set_time;
  60. int (*rtc_mips_set_mmss)(unsigned long);
  61. /* how many counter cycles in a jiffy */
  62. static unsigned long cycles_per_jiffy __read_mostly;
  63. /* expirelo is the count value for next CPU timer interrupt */
  64. static unsigned int expirelo;
  65. /*
  66. * Null timer ack for systems not needing one (e.g. i8254).
  67. */
  68. static void null_timer_ack(void) { /* nothing */ }
  69. /*
  70. * Null high precision timer functions for systems lacking one.
  71. */
  72. static cycle_t null_hpt_read(void)
  73. {
  74. return 0;
  75. }
  76. /*
  77. * Timer ack for an R4k-compatible timer of a known frequency.
  78. */
  79. static void c0_timer_ack(void)
  80. {
  81. unsigned int count;
  82. /* Ack this timer interrupt and set the next one. */
  83. expirelo += cycles_per_jiffy;
  84. write_c0_compare(expirelo);
  85. /* Check to see if we have missed any timer interrupts. */
  86. while (((count = read_c0_count()) - expirelo) < 0x7fffffff) {
  87. /* missed_timer_count++; */
  88. expirelo = count + cycles_per_jiffy;
  89. write_c0_compare(expirelo);
  90. }
  91. }
  92. /*
  93. * High precision timer functions for a R4k-compatible timer.
  94. */
  95. static cycle_t c0_hpt_read(void)
  96. {
  97. return read_c0_count();
  98. }
  99. /* For use both as a high precision timer and an interrupt source. */
  100. static void __init c0_hpt_timer_init(void)
  101. {
  102. expirelo = read_c0_count() + cycles_per_jiffy;
  103. write_c0_compare(expirelo);
  104. }
  105. int (*mips_timer_state)(void);
  106. void (*mips_timer_ack)(void);
  107. /* last time when xtime and rtc are sync'ed up */
  108. static long last_rtc_update;
  109. /*
  110. * local_timer_interrupt() does profiling and process accounting
  111. * on a per-CPU basis.
  112. *
  113. * In UP mode, it is invoked from the (global) timer_interrupt.
  114. *
  115. * In SMP mode, it might invoked by per-CPU timer interrupt, or
  116. * a broadcasted inter-processor interrupt which itself is triggered
  117. * by the global timer interrupt.
  118. */
  119. void local_timer_interrupt(int irq, void *dev_id)
  120. {
  121. profile_tick(CPU_PROFILING);
  122. update_process_times(user_mode(get_irq_regs()));
  123. }
  124. /*
  125. * High-level timer interrupt service routines. This function
  126. * is set as irqaction->handler and is invoked through do_IRQ.
  127. */
  128. irqreturn_t timer_interrupt(int irq, void *dev_id)
  129. {
  130. write_seqlock(&xtime_lock);
  131. mips_timer_ack();
  132. /*
  133. * call the generic timer interrupt handling
  134. */
  135. do_timer(1);
  136. /*
  137. * If we have an externally synchronized Linux clock, then update
  138. * CMOS clock accordingly every ~11 minutes. rtc_mips_set_time() has to be
  139. * called as close as possible to 500 ms before the new second starts.
  140. */
  141. if (ntp_synced() &&
  142. xtime.tv_sec > last_rtc_update + 660 &&
  143. (xtime.tv_nsec / 1000) >= 500000 - ((unsigned) TICK_SIZE) / 2 &&
  144. (xtime.tv_nsec / 1000) <= 500000 + ((unsigned) TICK_SIZE) / 2) {
  145. if (rtc_mips_set_mmss(xtime.tv_sec) == 0) {
  146. last_rtc_update = xtime.tv_sec;
  147. } else {
  148. /* do it again in 60 s */
  149. last_rtc_update = xtime.tv_sec - 600;
  150. }
  151. }
  152. write_sequnlock(&xtime_lock);
  153. /*
  154. * In UP mode, we call local_timer_interrupt() to do profiling
  155. * and process accouting.
  156. *
  157. * In SMP mode, local_timer_interrupt() is invoked by appropriate
  158. * low-level local timer interrupt handler.
  159. */
  160. local_timer_interrupt(irq, dev_id);
  161. return IRQ_HANDLED;
  162. }
  163. int null_perf_irq(void)
  164. {
  165. return 0;
  166. }
  167. int (*perf_irq)(void) = null_perf_irq;
  168. EXPORT_SYMBOL(null_perf_irq);
  169. EXPORT_SYMBOL(perf_irq);
  170. /*
  171. * Performance counter IRQ or -1 if shared with timer
  172. */
  173. int mipsxx_perfcount_irq;
  174. EXPORT_SYMBOL(mipsxx_perfcount_irq);
  175. /*
  176. * Possibly handle a performance counter interrupt.
  177. * Return true if the timer interrupt should not be checked
  178. */
  179. static inline int handle_perf_irq (int r2)
  180. {
  181. /*
  182. * The performance counter overflow interrupt may be shared with the
  183. * timer interrupt (mipsxx_perfcount_irq < 0). If it is and a
  184. * performance counter has overflowed (perf_irq() == IRQ_HANDLED)
  185. * and we can't reliably determine if a counter interrupt has also
  186. * happened (!r2) then don't check for a timer interrupt.
  187. */
  188. return (mipsxx_perfcount_irq < 0) &&
  189. perf_irq() == IRQ_HANDLED &&
  190. !r2;
  191. }
  192. asmlinkage void ll_timer_interrupt(int irq)
  193. {
  194. int r2 = cpu_has_mips_r2;
  195. irq_enter();
  196. kstat_this_cpu.irqs[irq]++;
  197. if (handle_perf_irq(r2))
  198. goto out;
  199. if (r2 && ((read_c0_cause() & (1 << 30)) == 0))
  200. goto out;
  201. timer_interrupt(irq, NULL);
  202. out:
  203. irq_exit();
  204. }
  205. asmlinkage void ll_local_timer_interrupt(int irq)
  206. {
  207. irq_enter();
  208. if (smp_processor_id() != 0)
  209. kstat_this_cpu.irqs[irq]++;
  210. /* we keep interrupt disabled all the time */
  211. local_timer_interrupt(irq, NULL);
  212. irq_exit();
  213. }
  214. /*
  215. * time_init() - it does the following things.
  216. *
  217. * 1) board_time_init() -
  218. * a) (optional) set up RTC routines,
  219. * b) (optional) calibrate and set the mips_hpt_frequency
  220. * (only needed if you intended to use cpu counter as timer interrupt
  221. * source)
  222. * 2) setup xtime based on rtc_mips_get_time().
  223. * 3) calculate a couple of cached variables for later usage
  224. * 4) plat_timer_setup() -
  225. * a) (optional) over-write any choices made above by time_init().
  226. * b) machine specific code should setup the timer irqaction.
  227. * c) enable the timer interrupt
  228. */
  229. void (*board_time_init)(void);
  230. unsigned int mips_hpt_frequency;
  231. static struct irqaction timer_irqaction = {
  232. .handler = timer_interrupt,
  233. .flags = IRQF_DISABLED | IRQF_PERCPU,
  234. .name = "timer",
  235. };
  236. static unsigned int __init calibrate_hpt(void)
  237. {
  238. cycle_t frequency, hpt_start, hpt_end, hpt_count, hz;
  239. const int loops = HZ / 10;
  240. int log_2_loops = 0;
  241. int i;
  242. /*
  243. * We want to calibrate for 0.1s, but to avoid a 64-bit
  244. * division we round the number of loops up to the nearest
  245. * power of 2.
  246. */
  247. while (loops > 1 << log_2_loops)
  248. log_2_loops++;
  249. i = 1 << log_2_loops;
  250. /*
  251. * Wait for a rising edge of the timer interrupt.
  252. */
  253. while (mips_timer_state());
  254. while (!mips_timer_state());
  255. /*
  256. * Now see how many high precision timer ticks happen
  257. * during the calculated number of periods between timer
  258. * interrupts.
  259. */
  260. hpt_start = clocksource_mips.read();
  261. do {
  262. while (mips_timer_state());
  263. while (!mips_timer_state());
  264. } while (--i);
  265. hpt_end = clocksource_mips.read();
  266. hpt_count = (hpt_end - hpt_start) & clocksource_mips.mask;
  267. hz = HZ;
  268. frequency = hpt_count * hz;
  269. return frequency >> log_2_loops;
  270. }
  271. struct clocksource clocksource_mips = {
  272. .name = "MIPS",
  273. .mask = CLOCKSOURCE_MASK(32),
  274. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  275. };
  276. static void __init init_mips_clocksource(void)
  277. {
  278. u64 temp;
  279. u32 shift;
  280. if (!mips_hpt_frequency || clocksource_mips.read == null_hpt_read)
  281. return;
  282. /* Calclate a somewhat reasonable rating value */
  283. clocksource_mips.rating = 200 + mips_hpt_frequency / 10000000;
  284. /* Find a shift value */
  285. for (shift = 32; shift > 0; shift--) {
  286. temp = (u64) NSEC_PER_SEC << shift;
  287. do_div(temp, mips_hpt_frequency);
  288. if ((temp >> 32) == 0)
  289. break;
  290. }
  291. clocksource_mips.shift = shift;
  292. clocksource_mips.mult = (u32)temp;
  293. clocksource_register(&clocksource_mips);
  294. }
  295. void __init time_init(void)
  296. {
  297. if (board_time_init)
  298. board_time_init();
  299. if (!rtc_mips_set_mmss)
  300. rtc_mips_set_mmss = rtc_mips_set_time;
  301. xtime.tv_sec = rtc_mips_get_time();
  302. xtime.tv_nsec = 0;
  303. set_normalized_timespec(&wall_to_monotonic,
  304. -xtime.tv_sec, -xtime.tv_nsec);
  305. /* Choose appropriate high precision timer routines. */
  306. if (!cpu_has_counter && !clocksource_mips.read)
  307. /* No high precision timer -- sorry. */
  308. clocksource_mips.read = null_hpt_read;
  309. else if (!mips_hpt_frequency && !mips_timer_state) {
  310. /* A high precision timer of unknown frequency. */
  311. if (!clocksource_mips.read)
  312. /* No external high precision timer -- use R4k. */
  313. clocksource_mips.read = c0_hpt_read;
  314. } else {
  315. /* We know counter frequency. Or we can get it. */
  316. if (!clocksource_mips.read) {
  317. /* No external high precision timer -- use R4k. */
  318. clocksource_mips.read = c0_hpt_read;
  319. if (!mips_timer_state) {
  320. /* No external timer interrupt -- use R4k. */
  321. mips_timer_ack = c0_timer_ack;
  322. /* Calculate cache parameters. */
  323. cycles_per_jiffy =
  324. (mips_hpt_frequency + HZ / 2) / HZ;
  325. /*
  326. * This sets up the high precision
  327. * timer for the first interrupt.
  328. */
  329. c0_hpt_timer_init();
  330. }
  331. }
  332. if (!mips_hpt_frequency)
  333. mips_hpt_frequency = calibrate_hpt();
  334. /* Report the high precision timer rate for a reference. */
  335. printk("Using %u.%03u MHz high precision timer.\n",
  336. ((mips_hpt_frequency + 500) / 1000) / 1000,
  337. ((mips_hpt_frequency + 500) / 1000) % 1000);
  338. }
  339. if (!mips_timer_ack)
  340. /* No timer interrupt ack (e.g. i8254). */
  341. mips_timer_ack = null_timer_ack;
  342. /*
  343. * Call board specific timer interrupt setup.
  344. *
  345. * this pointer must be setup in machine setup routine.
  346. *
  347. * Even if a machine chooses to use a low-level timer interrupt,
  348. * it still needs to setup the timer_irqaction.
  349. * In that case, it might be better to set timer_irqaction.handler
  350. * to be NULL function so that we are sure the high-level code
  351. * is not invoked accidentally.
  352. */
  353. plat_timer_setup(&timer_irqaction);
  354. init_mips_clocksource();
  355. }
  356. #define FEBRUARY 2
  357. #define STARTOFTIME 1970
  358. #define SECDAY 86400L
  359. #define SECYR (SECDAY * 365)
  360. #define leapyear(y) ((!((y) % 4) && ((y) % 100)) || !((y) % 400))
  361. #define days_in_year(y) (leapyear(y) ? 366 : 365)
  362. #define days_in_month(m) (month_days[(m) - 1])
  363. static int month_days[12] = {
  364. 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
  365. };
  366. void to_tm(unsigned long tim, struct rtc_time *tm)
  367. {
  368. long hms, day, gday;
  369. int i;
  370. gday = day = tim / SECDAY;
  371. hms = tim % SECDAY;
  372. /* Hours, minutes, seconds are easy */
  373. tm->tm_hour = hms / 3600;
  374. tm->tm_min = (hms % 3600) / 60;
  375. tm->tm_sec = (hms % 3600) % 60;
  376. /* Number of years in days */
  377. for (i = STARTOFTIME; day >= days_in_year(i); i++)
  378. day -= days_in_year(i);
  379. tm->tm_year = i;
  380. /* Number of months in days left */
  381. if (leapyear(tm->tm_year))
  382. days_in_month(FEBRUARY) = 29;
  383. for (i = 1; day >= days_in_month(i); i++)
  384. day -= days_in_month(i);
  385. days_in_month(FEBRUARY) = 28;
  386. tm->tm_mon = i - 1; /* tm_mon starts from 0 to 11 */
  387. /* Days are what is left over (+1) from all that. */
  388. tm->tm_mday = day + 1;
  389. /*
  390. * Determine the day of week
  391. */
  392. tm->tm_wday = (gday + 4) % 7; /* 1970/1/1 was Thursday */
  393. }
  394. EXPORT_SYMBOL(rtc_lock);
  395. EXPORT_SYMBOL(to_tm);
  396. EXPORT_SYMBOL(rtc_mips_set_time);
  397. EXPORT_SYMBOL(rtc_mips_get_time);