time.c 12 KB

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