time.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  1. /*
  2. * linux/arch/arm/mach-omap1/time.c
  3. *
  4. * OMAP Timers
  5. *
  6. * Copyright (C) 2004 Nokia Corporation
  7. * Partial timer rewrite and additional dynamic tick timer support by
  8. * Tony Lindgen <tony@atomide.com> and
  9. * Tuukka Tikkanen <tuukka.tikkanen@elektrobit.com>
  10. *
  11. * MPU timer code based on the older MPU timer code for OMAP
  12. * Copyright (C) 2000 RidgeRun, Inc.
  13. * Author: Greg Lonnon <glonnon@ridgerun.com>
  14. *
  15. * This program is free software; you can redistribute it and/or modify it
  16. * under the terms of the GNU General Public License as published by the
  17. * Free Software Foundation; either version 2 of the License, or (at your
  18. * option) any later version.
  19. *
  20. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
  21. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  22. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
  23. * NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  24. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  25. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  26. * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  27. * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  28. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  29. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. *
  31. * You should have received a copy of the GNU General Public License along
  32. * with this program; if not, write to the Free Software Foundation, Inc.,
  33. * 675 Mass Ave, Cambridge, MA 02139, USA.
  34. */
  35. #include <linux/config.h>
  36. #include <linux/kernel.h>
  37. #include <linux/init.h>
  38. #include <linux/delay.h>
  39. #include <linux/interrupt.h>
  40. #include <linux/sched.h>
  41. #include <linux/spinlock.h>
  42. #include <asm/system.h>
  43. #include <asm/hardware.h>
  44. #include <asm/io.h>
  45. #include <asm/leds.h>
  46. #include <asm/irq.h>
  47. #include <asm/mach/irq.h>
  48. #include <asm/mach/time.h>
  49. struct sys_timer omap_timer;
  50. #ifdef CONFIG_OMAP_MPU_TIMER
  51. /*
  52. * ---------------------------------------------------------------------------
  53. * MPU timer
  54. * ---------------------------------------------------------------------------
  55. */
  56. #define OMAP_MPU_TIMER_BASE OMAP_MPU_TIMER1_BASE
  57. #define OMAP_MPU_TIMER_OFFSET 0x100
  58. /* cycles to nsec conversions taken from arch/i386/kernel/timers/timer_tsc.c,
  59. * converted to use kHz by Kevin Hilman */
  60. /* convert from cycles(64bits) => nanoseconds (64bits)
  61. * basic equation:
  62. * ns = cycles / (freq / ns_per_sec)
  63. * ns = cycles * (ns_per_sec / freq)
  64. * ns = cycles * (10^9 / (cpu_khz * 10^3))
  65. * ns = cycles * (10^6 / cpu_khz)
  66. *
  67. * Then we use scaling math (suggested by george at mvista.com) to get:
  68. * ns = cycles * (10^6 * SC / cpu_khz / SC
  69. * ns = cycles * cyc2ns_scale / SC
  70. *
  71. * And since SC is a constant power of two, we can convert the div
  72. * into a shift.
  73. * -johnstul at us.ibm.com "math is hard, lets go shopping!"
  74. */
  75. static unsigned long cyc2ns_scale;
  76. #define CYC2NS_SCALE_FACTOR 10 /* 2^10, carefully chosen */
  77. static inline void set_cyc2ns_scale(unsigned long cpu_khz)
  78. {
  79. cyc2ns_scale = (1000000 << CYC2NS_SCALE_FACTOR)/cpu_khz;
  80. }
  81. static inline unsigned long long cycles_2_ns(unsigned long long cyc)
  82. {
  83. return (cyc * cyc2ns_scale) >> CYC2NS_SCALE_FACTOR;
  84. }
  85. /*
  86. * MPU_TICKS_PER_SEC must be an even number, otherwise machinecycles_to_usecs
  87. * will break. On P2, the timer count rate is 6.5 MHz after programming PTV
  88. * with 0. This divides the 13MHz input by 2, and is undocumented.
  89. */
  90. #ifdef CONFIG_MACH_OMAP_PERSEUS2
  91. /* REVISIT: This ifdef construct should be replaced by a query to clock
  92. * framework to see if timer base frequency is 12.0, 13.0 or 19.2 MHz.
  93. */
  94. #define MPU_TICKS_PER_SEC (13000000 / 2)
  95. #else
  96. #define MPU_TICKS_PER_SEC (12000000 / 2)
  97. #endif
  98. #define MPU_TIMER_TICK_PERIOD ((MPU_TICKS_PER_SEC / HZ) - 1)
  99. typedef struct {
  100. u32 cntl; /* CNTL_TIMER, R/W */
  101. u32 load_tim; /* LOAD_TIM, W */
  102. u32 read_tim; /* READ_TIM, R */
  103. } omap_mpu_timer_regs_t;
  104. #define omap_mpu_timer_base(n) \
  105. ((volatile omap_mpu_timer_regs_t*)IO_ADDRESS(OMAP_MPU_TIMER_BASE + \
  106. (n)*OMAP_MPU_TIMER_OFFSET))
  107. static inline unsigned long omap_mpu_timer_read(int nr)
  108. {
  109. volatile omap_mpu_timer_regs_t* timer = omap_mpu_timer_base(nr);
  110. return timer->read_tim;
  111. }
  112. static inline void omap_mpu_timer_start(int nr, unsigned long load_val)
  113. {
  114. volatile omap_mpu_timer_regs_t* timer = omap_mpu_timer_base(nr);
  115. timer->cntl = MPU_TIMER_CLOCK_ENABLE;
  116. udelay(1);
  117. timer->load_tim = load_val;
  118. udelay(1);
  119. timer->cntl = (MPU_TIMER_CLOCK_ENABLE | MPU_TIMER_AR | MPU_TIMER_ST);
  120. }
  121. unsigned long omap_mpu_timer_ticks_to_usecs(unsigned long nr_ticks)
  122. {
  123. unsigned long long nsec;
  124. nsec = cycles_2_ns((unsigned long long)nr_ticks);
  125. return (unsigned long)nsec / 1000;
  126. }
  127. /*
  128. * Last processed system timer interrupt
  129. */
  130. static unsigned long omap_mpu_timer_last = 0;
  131. /*
  132. * Returns elapsed usecs since last system timer interrupt
  133. */
  134. static unsigned long omap_mpu_timer_gettimeoffset(void)
  135. {
  136. unsigned long now = 0 - omap_mpu_timer_read(0);
  137. unsigned long elapsed = now - omap_mpu_timer_last;
  138. return omap_mpu_timer_ticks_to_usecs(elapsed);
  139. }
  140. /*
  141. * Elapsed time between interrupts is calculated using timer0.
  142. * Latency during the interrupt is calculated using timer1.
  143. * Both timer0 and timer1 are counting at 6MHz (P2 6.5MHz).
  144. */
  145. static irqreturn_t omap_mpu_timer_interrupt(int irq, void *dev_id,
  146. struct pt_regs *regs)
  147. {
  148. unsigned long now, latency;
  149. write_seqlock(&xtime_lock);
  150. now = 0 - omap_mpu_timer_read(0);
  151. latency = MPU_TICKS_PER_SEC / HZ - omap_mpu_timer_read(1);
  152. omap_mpu_timer_last = now - latency;
  153. timer_tick(regs);
  154. write_sequnlock(&xtime_lock);
  155. return IRQ_HANDLED;
  156. }
  157. static struct irqaction omap_mpu_timer_irq = {
  158. .name = "mpu timer",
  159. .flags = SA_INTERRUPT | SA_TIMER,
  160. .handler = omap_mpu_timer_interrupt,
  161. };
  162. static unsigned long omap_mpu_timer1_overflows;
  163. static irqreturn_t omap_mpu_timer1_interrupt(int irq, void *dev_id,
  164. struct pt_regs *regs)
  165. {
  166. omap_mpu_timer1_overflows++;
  167. return IRQ_HANDLED;
  168. }
  169. static struct irqaction omap_mpu_timer1_irq = {
  170. .name = "mpu timer1 overflow",
  171. .flags = SA_INTERRUPT,
  172. .handler = omap_mpu_timer1_interrupt,
  173. };
  174. static __init void omap_init_mpu_timer(void)
  175. {
  176. set_cyc2ns_scale(MPU_TICKS_PER_SEC / 1000);
  177. omap_timer.offset = omap_mpu_timer_gettimeoffset;
  178. setup_irq(INT_TIMER1, &omap_mpu_timer1_irq);
  179. setup_irq(INT_TIMER2, &omap_mpu_timer_irq);
  180. omap_mpu_timer_start(0, 0xffffffff);
  181. omap_mpu_timer_start(1, MPU_TIMER_TICK_PERIOD);
  182. }
  183. /*
  184. * Scheduler clock - returns current time in nanosec units.
  185. */
  186. unsigned long long sched_clock(void)
  187. {
  188. unsigned long ticks = 0 - omap_mpu_timer_read(0);
  189. unsigned long long ticks64;
  190. ticks64 = omap_mpu_timer1_overflows;
  191. ticks64 <<= 32;
  192. ticks64 |= ticks;
  193. return cycles_2_ns(ticks64);
  194. }
  195. #endif /* CONFIG_OMAP_MPU_TIMER */
  196. #ifdef CONFIG_OMAP_32K_TIMER
  197. #ifdef CONFIG_ARCH_OMAP1510
  198. #error OMAP 32KHz timer does not currently work on 1510!
  199. #endif
  200. /*
  201. * ---------------------------------------------------------------------------
  202. * 32KHz OS timer
  203. *
  204. * This currently works only on 16xx, as 1510 does not have the continuous
  205. * 32KHz synchronous timer. The 32KHz synchronous timer is used to keep track
  206. * of time in addition to the 32KHz OS timer. Using only the 32KHz OS timer
  207. * on 1510 would be possible, but the timer would not be as accurate as
  208. * with the 32KHz synchronized timer.
  209. * ---------------------------------------------------------------------------
  210. */
  211. #define OMAP_32K_TIMER_BASE 0xfffb9000
  212. #define OMAP_32K_TIMER_CR 0x08
  213. #define OMAP_32K_TIMER_TVR 0x00
  214. #define OMAP_32K_TIMER_TCR 0x04
  215. #define OMAP_32K_TICKS_PER_HZ (32768 / HZ)
  216. /*
  217. * TRM says 1 / HZ = ( TVR + 1) / 32768, so TRV = (32768 / HZ) - 1
  218. * so with HZ = 100, TVR = 327.68.
  219. */
  220. #define OMAP_32K_TIMER_TICK_PERIOD ((32768 / HZ) - 1)
  221. #define TIMER_32K_SYNCHRONIZED 0xfffbc410
  222. #define JIFFIES_TO_HW_TICKS(nr_jiffies, clock_rate) \
  223. (((nr_jiffies) * (clock_rate)) / HZ)
  224. static inline void omap_32k_timer_write(int val, int reg)
  225. {
  226. omap_writew(val, reg + OMAP_32K_TIMER_BASE);
  227. }
  228. static inline unsigned long omap_32k_timer_read(int reg)
  229. {
  230. return omap_readl(reg + OMAP_32K_TIMER_BASE) & 0xffffff;
  231. }
  232. /*
  233. * The 32KHz synchronized timer is an additional timer on 16xx.
  234. * It is always running.
  235. */
  236. static inline unsigned long omap_32k_sync_timer_read(void)
  237. {
  238. return omap_readl(TIMER_32K_SYNCHRONIZED);
  239. }
  240. static inline void omap_32k_timer_start(unsigned long load_val)
  241. {
  242. omap_32k_timer_write(load_val, OMAP_32K_TIMER_TVR);
  243. omap_32k_timer_write(0x0f, OMAP_32K_TIMER_CR);
  244. }
  245. static inline void omap_32k_timer_stop(void)
  246. {
  247. omap_32k_timer_write(0x0, OMAP_32K_TIMER_CR);
  248. }
  249. /*
  250. * Rounds down to nearest usec. Note that this will overflow for larger values.
  251. */
  252. static inline unsigned long omap_32k_ticks_to_usecs(unsigned long ticks_32k)
  253. {
  254. return (ticks_32k * 5*5*5*5*5*5) >> 9;
  255. }
  256. /*
  257. * Rounds down to nearest nsec.
  258. */
  259. static inline unsigned long long
  260. omap_32k_ticks_to_nsecs(unsigned long ticks_32k)
  261. {
  262. return (unsigned long long) ticks_32k * 1000 * 5*5*5*5*5*5 >> 9;
  263. }
  264. static unsigned long omap_32k_last_tick = 0;
  265. /*
  266. * Returns elapsed usecs since last 32k timer interrupt
  267. */
  268. static unsigned long omap_32k_timer_gettimeoffset(void)
  269. {
  270. unsigned long now = omap_32k_sync_timer_read();
  271. return omap_32k_ticks_to_usecs(now - omap_32k_last_tick);
  272. }
  273. /*
  274. * Returns current time from boot in nsecs. It's OK for this to wrap
  275. * around for now, as it's just a relative time stamp.
  276. */
  277. unsigned long long sched_clock(void)
  278. {
  279. return omap_32k_ticks_to_nsecs(omap_32k_sync_timer_read());
  280. }
  281. /*
  282. * Timer interrupt for 32KHz timer. When dynamic tick is enabled, this
  283. * function is also called from other interrupts to remove latency
  284. * issues with dynamic tick. In the dynamic tick case, we need to lock
  285. * with irqsave.
  286. */
  287. static irqreturn_t omap_32k_timer_interrupt(int irq, void *dev_id,
  288. struct pt_regs *regs)
  289. {
  290. unsigned long flags;
  291. unsigned long now;
  292. write_seqlock_irqsave(&xtime_lock, flags);
  293. now = omap_32k_sync_timer_read();
  294. while (now - omap_32k_last_tick >= OMAP_32K_TICKS_PER_HZ) {
  295. omap_32k_last_tick += OMAP_32K_TICKS_PER_HZ;
  296. timer_tick(regs);
  297. }
  298. /* Restart timer so we don't drift off due to modulo or dynamic tick.
  299. * By default we program the next timer to be continuous to avoid
  300. * latencies during high system load. During dynamic tick operation the
  301. * continuous timer can be overridden from pm_idle to be longer.
  302. */
  303. omap_32k_timer_start(omap_32k_last_tick + OMAP_32K_TICKS_PER_HZ - now);
  304. write_sequnlock_irqrestore(&xtime_lock, flags);
  305. return IRQ_HANDLED;
  306. }
  307. #ifdef CONFIG_NO_IDLE_HZ
  308. /*
  309. * Programs the next timer interrupt needed. Called when dynamic tick is
  310. * enabled, and to reprogram the ticks to skip from pm_idle. Note that
  311. * we can keep the timer continuous, and don't need to set it to run in
  312. * one-shot mode. This is because the timer will get reprogrammed again
  313. * after next interrupt.
  314. */
  315. void omap_32k_timer_reprogram(unsigned long next_tick)
  316. {
  317. omap_32k_timer_start(JIFFIES_TO_HW_TICKS(next_tick, 32768) + 1);
  318. }
  319. static struct irqaction omap_32k_timer_irq;
  320. extern struct timer_update_handler timer_update;
  321. static int omap_32k_timer_enable_dyn_tick(void)
  322. {
  323. /* No need to reprogram timer, just use the next interrupt */
  324. return 0;
  325. }
  326. static int omap_32k_timer_disable_dyn_tick(void)
  327. {
  328. omap_32k_timer_start(OMAP_32K_TIMER_TICK_PERIOD);
  329. return 0;
  330. }
  331. static struct dyn_tick_timer omap_dyn_tick_timer = {
  332. .enable = omap_32k_timer_enable_dyn_tick,
  333. .disable = omap_32k_timer_disable_dyn_tick,
  334. .reprogram = omap_32k_timer_reprogram,
  335. .handler = omap_32k_timer_interrupt,
  336. };
  337. #endif /* CONFIG_NO_IDLE_HZ */
  338. static struct irqaction omap_32k_timer_irq = {
  339. .name = "32KHz timer",
  340. .flags = SA_INTERRUPT | SA_TIMER,
  341. .handler = omap_32k_timer_interrupt,
  342. };
  343. static __init void omap_init_32k_timer(void)
  344. {
  345. #ifdef CONFIG_NO_IDLE_HZ
  346. omap_timer.dyn_tick = &omap_dyn_tick_timer;
  347. #endif
  348. setup_irq(INT_OS_TIMER, &omap_32k_timer_irq);
  349. omap_timer.offset = omap_32k_timer_gettimeoffset;
  350. omap_32k_last_tick = omap_32k_sync_timer_read();
  351. omap_32k_timer_start(OMAP_32K_TIMER_TICK_PERIOD);
  352. }
  353. #endif /* CONFIG_OMAP_32K_TIMER */
  354. /*
  355. * ---------------------------------------------------------------------------
  356. * Timer initialization
  357. * ---------------------------------------------------------------------------
  358. */
  359. static void __init omap_timer_init(void)
  360. {
  361. #if defined(CONFIG_OMAP_MPU_TIMER)
  362. omap_init_mpu_timer();
  363. #elif defined(CONFIG_OMAP_32K_TIMER)
  364. omap_init_32k_timer();
  365. #else
  366. #error No system timer selected in Kconfig!
  367. #endif
  368. }
  369. struct sys_timer omap_timer = {
  370. .init = omap_timer_init,
  371. .offset = NULL, /* Initialized later */
  372. };