time.c 12 KB

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