time.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. /*
  2. * File: arch/blackfin/kernel/time.c
  3. * Based on: none - original work
  4. * Author:
  5. *
  6. * Created:
  7. * Description: This file contains the bfin-specific time handling details.
  8. * Most of the stuff is located in the machine specific files.
  9. *
  10. * Modified:
  11. * Copyright 2004-2006 Analog Devices Inc.
  12. *
  13. * Bugs: Enter bugs at http://blackfin.uclinux.org/
  14. *
  15. * This program is free software; you can redistribute it and/or modify
  16. * it under the terms of the GNU General Public License as published by
  17. * the Free Software Foundation; either version 2 of the License, or
  18. * (at your option) any later version.
  19. *
  20. * This program is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. * GNU General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU General Public License
  26. * along with this program; if not, see the file COPYING, or write
  27. * to the Free Software Foundation, Inc.,
  28. * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  29. */
  30. #include <linux/module.h>
  31. #include <linux/profile.h>
  32. #include <linux/interrupt.h>
  33. #include <linux/time.h>
  34. #include <linux/irq.h>
  35. #include <asm/blackfin.h>
  36. /* This is an NTP setting */
  37. #define TICK_SIZE (tick_nsec / 1000)
  38. static void time_sched_init(irqreturn_t(*timer_routine)
  39. (int, void *));
  40. static unsigned long gettimeoffset(void);
  41. static inline void do_leds(void);
  42. #if (defined(CONFIG_BFIN_ALIVE_LED) || defined(CONFIG_BFIN_IDLE_LED))
  43. void __init init_leds(void)
  44. {
  45. unsigned int tmp = 0;
  46. #if defined(CONFIG_BFIN_ALIVE_LED)
  47. /* config pins as output. */
  48. tmp = bfin_read_CONFIG_BFIN_ALIVE_LED_DPORT();
  49. SSYNC();
  50. bfin_write_CONFIG_BFIN_ALIVE_LED_DPORT(tmp | CONFIG_BFIN_ALIVE_LED_PIN);
  51. SSYNC();
  52. /* First set led be off */
  53. tmp = bfin_read_CONFIG_BFIN_ALIVE_LED_PORT();
  54. SSYNC();
  55. bfin_write_CONFIG_BFIN_ALIVE_LED_PORT(tmp | CONFIG_BFIN_ALIVE_LED_PIN); /* light off */
  56. SSYNC();
  57. #endif
  58. #if defined(CONFIG_BFIN_IDLE_LED)
  59. /* config pins as output. */
  60. tmp = bfin_read_CONFIG_BFIN_IDLE_LED_DPORT();
  61. SSYNC();
  62. bfin_write_CONFIG_BFIN_IDLE_LED_DPORT(tmp | CONFIG_BFIN_IDLE_LED_PIN);
  63. SSYNC();
  64. /* First set led be off */
  65. tmp = bfin_read_CONFIG_BFIN_IDLE_LED_PORT();
  66. SSYNC();
  67. bfin_write_CONFIG_BFIN_IDLE_LED_PORT(tmp | CONFIG_BFIN_IDLE_LED_PIN); /* light off */
  68. SSYNC();
  69. #endif
  70. }
  71. #else
  72. void __init init_leds(void)
  73. {
  74. }
  75. #endif
  76. #if defined(CONFIG_BFIN_ALIVE_LED)
  77. static inline void do_leds(void)
  78. {
  79. static unsigned int count = 50;
  80. static int flag;
  81. unsigned short tmp = 0;
  82. if (--count == 0) {
  83. count = 50;
  84. flag = ~flag;
  85. }
  86. tmp = bfin_read_CONFIG_BFIN_ALIVE_LED_PORT();
  87. SSYNC();
  88. if (flag)
  89. tmp &= ~CONFIG_BFIN_ALIVE_LED_PIN; /* light on */
  90. else
  91. tmp |= CONFIG_BFIN_ALIVE_LED_PIN; /* light off */
  92. bfin_write_CONFIG_BFIN_ALIVE_LED_PORT(tmp);
  93. SSYNC();
  94. }
  95. #else
  96. static inline void do_leds(void)
  97. {
  98. }
  99. #endif
  100. static struct irqaction bfin_timer_irq = {
  101. .name = "BFIN Timer Tick",
  102. .flags = IRQF_DISABLED
  103. };
  104. /*
  105. * The way that the Blackfin core timer works is:
  106. * - CCLK is divided by a programmable 8-bit pre-scaler (TSCALE)
  107. * - Every time TSCALE ticks, a 32bit is counted down (TCOUNT)
  108. *
  109. * If you take the fastest clock (1ns, or 1GHz to make the math work easier)
  110. * 10ms is 10,000,000 clock ticks, which fits easy into a 32-bit counter
  111. * (32 bit counter is 4,294,967,296ns or 4.2 seconds) so, we don't need
  112. * to use TSCALE, and program it to zero (which is pass CCLK through).
  113. * If you feel like using it, try to keep HZ * TIMESCALE to some
  114. * value that divides easy (like power of 2).
  115. */
  116. #define TIME_SCALE 1
  117. static void
  118. time_sched_init(irqreturn_t(*timer_routine) (int, void *))
  119. {
  120. u32 tcount;
  121. /* power up the timer, but don't enable it just yet */
  122. bfin_write_TCNTL(1);
  123. CSYNC();
  124. /*
  125. * the TSCALE prescaler counter.
  126. */
  127. bfin_write_TSCALE((TIME_SCALE - 1));
  128. tcount = ((get_cclk() / (HZ * TIME_SCALE)) - 1);
  129. bfin_write_TPERIOD(tcount);
  130. bfin_write_TCOUNT(tcount);
  131. /* now enable the timer */
  132. CSYNC();
  133. bfin_write_TCNTL(7);
  134. bfin_timer_irq.handler = (irq_handler_t)timer_routine;
  135. /* call setup_irq instead of request_irq because request_irq calls
  136. * kmalloc which has not been initialized yet
  137. */
  138. setup_irq(IRQ_CORETMR, &bfin_timer_irq);
  139. }
  140. /*
  141. * Should return useconds since last timer tick
  142. */
  143. static unsigned long gettimeoffset(void)
  144. {
  145. unsigned long offset;
  146. unsigned long clocks_per_jiffy;
  147. clocks_per_jiffy = bfin_read_TPERIOD();
  148. offset =
  149. (clocks_per_jiffy -
  150. bfin_read_TCOUNT()) / (((clocks_per_jiffy + 1) * HZ) /
  151. USEC_PER_SEC);
  152. /* Check if we just wrapped the counters and maybe missed a tick */
  153. if ((bfin_read_ILAT() & (1 << IRQ_CORETMR))
  154. && (offset < (100000 / HZ / 2)))
  155. offset += (USEC_PER_SEC / HZ);
  156. return offset;
  157. }
  158. static inline int set_rtc_mmss(unsigned long nowtime)
  159. {
  160. return 0;
  161. }
  162. /*
  163. * timer_interrupt() needs to keep up the real-time clock,
  164. * as well as call the "do_timer()" routine every clocktick
  165. */
  166. #ifdef CONFIG_CORE_TIMER_IRQ_L1
  167. irqreturn_t timer_interrupt(int irq, void *dummy)__attribute__((l1_text));
  168. #endif
  169. irqreturn_t timer_interrupt(int irq, void *dummy)
  170. {
  171. /* last time the cmos clock got updated */
  172. static long last_rtc_update;
  173. write_seqlock(&xtime_lock);
  174. do_timer(1);
  175. do_leds();
  176. #ifndef CONFIG_SMP
  177. update_process_times(user_mode(get_irq_regs()));
  178. #endif
  179. profile_tick(CPU_PROFILING);
  180. /*
  181. * If we have an externally synchronized Linux clock, then update
  182. * CMOS clock accordingly every ~11 minutes. Set_rtc_mmss() has to be
  183. * called as close as possible to 500 ms before the new second starts.
  184. */
  185. if (ntp_synced() &&
  186. xtime.tv_sec > last_rtc_update + 660 &&
  187. (xtime.tv_nsec / NSEC_PER_USEC) >=
  188. 500000 - ((unsigned)TICK_SIZE) / 2
  189. && (xtime.tv_nsec / NSEC_PER_USEC) <=
  190. 500000 + ((unsigned)TICK_SIZE) / 2) {
  191. if (set_rtc_mmss(xtime.tv_sec) == 0)
  192. last_rtc_update = xtime.tv_sec;
  193. else
  194. /* Do it again in 60s. */
  195. last_rtc_update = xtime.tv_sec - 600;
  196. }
  197. write_sequnlock(&xtime_lock);
  198. return IRQ_HANDLED;
  199. }
  200. void __init time_init(void)
  201. {
  202. time_t secs_since_1970 = (365 * 37 + 9) * 24 * 60 * 60; /* 1 Jan 2007 */
  203. #ifdef CONFIG_RTC_DRV_BFIN
  204. /* [#2663] hack to filter junk RTC values that would cause
  205. * userspace to have to deal with time values greater than
  206. * 2^31 seconds (which uClibc cannot cope with yet)
  207. */
  208. if ((bfin_read_RTC_STAT() & 0xC0000000) == 0xC0000000) {
  209. printk(KERN_NOTICE "bfin-rtc: invalid date; resetting\n");
  210. bfin_write_RTC_STAT(0);
  211. }
  212. #endif
  213. /* Initialize xtime. From now on, xtime is updated with timer interrupts */
  214. xtime.tv_sec = secs_since_1970;
  215. xtime.tv_nsec = 0;
  216. wall_to_monotonic.tv_sec = -xtime.tv_sec;
  217. time_sched_init(timer_interrupt);
  218. }
  219. #ifndef CONFIG_GENERIC_TIME
  220. void do_gettimeofday(struct timeval *tv)
  221. {
  222. unsigned long flags;
  223. unsigned long seq;
  224. unsigned long usec, sec;
  225. do {
  226. seq = read_seqbegin_irqsave(&xtime_lock, flags);
  227. usec = gettimeoffset();
  228. sec = xtime.tv_sec;
  229. usec += (xtime.tv_nsec / NSEC_PER_USEC);
  230. }
  231. while (read_seqretry_irqrestore(&xtime_lock, seq, flags));
  232. while (usec >= USEC_PER_SEC) {
  233. usec -= USEC_PER_SEC;
  234. sec++;
  235. }
  236. tv->tv_sec = sec;
  237. tv->tv_usec = usec;
  238. }
  239. EXPORT_SYMBOL(do_gettimeofday);
  240. int do_settimeofday(struct timespec *tv)
  241. {
  242. time_t wtm_sec, sec = tv->tv_sec;
  243. long wtm_nsec, nsec = tv->tv_nsec;
  244. if ((unsigned long)tv->tv_nsec >= NSEC_PER_SEC)
  245. return -EINVAL;
  246. write_seqlock_irq(&xtime_lock);
  247. /*
  248. * This is revolting. We need to set the xtime.tv_usec
  249. * correctly. However, the value in this location is
  250. * is value at the last tick.
  251. * Discover what correction gettimeofday
  252. * would have done, and then undo it!
  253. */
  254. nsec -= (gettimeoffset() * NSEC_PER_USEC);
  255. wtm_sec = wall_to_monotonic.tv_sec + (xtime.tv_sec - sec);
  256. wtm_nsec = wall_to_monotonic.tv_nsec + (xtime.tv_nsec - nsec);
  257. set_normalized_timespec(&xtime, sec, nsec);
  258. set_normalized_timespec(&wall_to_monotonic, wtm_sec, wtm_nsec);
  259. ntp_clear();
  260. write_sequnlock_irq(&xtime_lock);
  261. clock_was_set();
  262. return 0;
  263. }
  264. EXPORT_SYMBOL(do_settimeofday);
  265. #endif /* !CONFIG_GENERIC_TIME */
  266. /*
  267. * Scheduler clock - returns current time in nanosec units.
  268. */
  269. unsigned long long sched_clock(void)
  270. {
  271. return (unsigned long long)jiffies *(NSEC_PER_SEC / HZ);
  272. }