time.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. /* linux/arch/arm/mach-s3c2410/time.c
  2. *
  3. * Copyright (C) 2003-2005 Simtec Electronics
  4. * Ben Dooks, <ben@simtec.co.uk>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. */
  20. #include <linux/config.h>
  21. #include <linux/kernel.h>
  22. #include <linux/sched.h>
  23. #include <linux/init.h>
  24. #include <linux/interrupt.h>
  25. #include <linux/err.h>
  26. #include <asm/system.h>
  27. #include <asm/leds.h>
  28. #include <asm/mach-types.h>
  29. #include <asm/io.h>
  30. #include <asm/irq.h>
  31. #include <asm/arch/map.h>
  32. #include <asm/arch/regs-timer.h>
  33. #include <asm/arch/regs-irq.h>
  34. #include <asm/mach/time.h>
  35. #include <asm/hardware/clock.h>
  36. #include "clock.h"
  37. static unsigned long timer_startval;
  38. static unsigned long timer_usec_ticks;
  39. #define TIMER_USEC_SHIFT 16
  40. /* we use the shifted arithmetic to work out the ratio of timer ticks
  41. * to usecs, as often the peripheral clock is not a nice even multiple
  42. * of 1MHz.
  43. *
  44. * shift of 14 and 15 are too low for the 12MHz, 16 seems to be ok
  45. * for the current HZ value of 200 without producing overflows.
  46. *
  47. * Original patch by Dimitry Andric, updated by Ben Dooks
  48. */
  49. /* timer_mask_usec_ticks
  50. *
  51. * given a clock and divisor, make the value to pass into timer_ticks_to_usec
  52. * to scale the ticks into usecs
  53. */
  54. static inline unsigned long
  55. timer_mask_usec_ticks(unsigned long scaler, unsigned long pclk)
  56. {
  57. unsigned long den = pclk / 1000;
  58. return ((1000 << TIMER_USEC_SHIFT) * scaler + (den >> 1)) / den;
  59. }
  60. /* timer_ticks_to_usec
  61. *
  62. * convert timer ticks to usec.
  63. */
  64. static inline unsigned long timer_ticks_to_usec(unsigned long ticks)
  65. {
  66. unsigned long res;
  67. res = ticks * timer_usec_ticks;
  68. res += 1 << (TIMER_USEC_SHIFT - 4); /* round up slightly */
  69. return res >> TIMER_USEC_SHIFT;
  70. }
  71. /***
  72. * Returns microsecond since last clock interrupt. Note that interrupts
  73. * will have been disabled by do_gettimeoffset()
  74. * IRQs are disabled before entering here from do_gettimeofday()
  75. */
  76. #define SRCPND_TIMER4 (1<<(IRQ_TIMER4 - IRQ_EINT0))
  77. static unsigned long s3c2410_gettimeoffset (void)
  78. {
  79. unsigned long tdone;
  80. unsigned long irqpend;
  81. unsigned long tval;
  82. /* work out how many ticks have gone since last timer interrupt */
  83. tval = __raw_readl(S3C2410_TCNTO(4));
  84. tdone = timer_startval - tval;
  85. /* check to see if there is an interrupt pending */
  86. irqpend = __raw_readl(S3C2410_SRCPND);
  87. if (irqpend & SRCPND_TIMER4) {
  88. /* re-read the timer, and try and fix up for the missed
  89. * interrupt. Note, the interrupt may go off before the
  90. * timer has re-loaded from wrapping.
  91. */
  92. tval = __raw_readl(S3C2410_TCNTO(4));
  93. tdone = timer_startval - tval;
  94. if (tval != 0)
  95. tdone += timer_startval;
  96. }
  97. return timer_ticks_to_usec(tdone);
  98. }
  99. /*
  100. * IRQ handler for the timer
  101. */
  102. static irqreturn_t
  103. s3c2410_timer_interrupt(int irq, void *dev_id, struct pt_regs *regs)
  104. {
  105. write_seqlock(&xtime_lock);
  106. timer_tick(regs);
  107. write_sequnlock(&xtime_lock);
  108. return IRQ_HANDLED;
  109. }
  110. static struct irqaction s3c2410_timer_irq = {
  111. .name = "S3C2410 Timer Tick",
  112. .flags = SA_INTERRUPT | SA_TIMER,
  113. .handler = s3c2410_timer_interrupt,
  114. };
  115. /*
  116. * Set up timer interrupt, and return the current time in seconds.
  117. *
  118. * Currently we only use timer4, as it is the only timer which has no
  119. * other function that can be exploited externally
  120. */
  121. static void s3c2410_timer_setup (void)
  122. {
  123. unsigned long tcon;
  124. unsigned long tcnt;
  125. unsigned long tcfg1;
  126. unsigned long tcfg0;
  127. tcnt = 0xffff; /* default value for tcnt */
  128. /* read the current timer configuration bits */
  129. tcon = __raw_readl(S3C2410_TCON);
  130. tcfg1 = __raw_readl(S3C2410_TCFG1);
  131. tcfg0 = __raw_readl(S3C2410_TCFG0);
  132. /* configure the system for whichever machine is in use */
  133. if (machine_is_bast() || machine_is_vr1000() || machine_is_anubis()) {
  134. /* timer is at 12MHz, scaler is 1 */
  135. timer_usec_ticks = timer_mask_usec_ticks(1, 12000000);
  136. tcnt = 12000000 / HZ;
  137. tcfg1 &= ~S3C2410_TCFG1_MUX4_MASK;
  138. tcfg1 |= S3C2410_TCFG1_MUX4_TCLK1;
  139. } else {
  140. unsigned long pclk;
  141. struct clk *clk;
  142. /* for the h1940 (and others), we use the pclk from the core
  143. * to generate the timer values. since values around 50 to
  144. * 70MHz are not values we can directly generate the timer
  145. * value from, we need to pre-scale and divide before using it.
  146. *
  147. * for instance, using 50.7MHz and dividing by 6 gives 8.45MHz
  148. * (8.45 ticks per usec)
  149. */
  150. /* this is used as default if no other timer can be found */
  151. clk = clk_get(NULL, "timers");
  152. if (IS_ERR(clk))
  153. panic("failed to get clock for system timer");
  154. clk_use(clk);
  155. clk_enable(clk);
  156. pclk = clk_get_rate(clk);
  157. /* configure clock tick */
  158. timer_usec_ticks = timer_mask_usec_ticks(6, pclk);
  159. tcfg1 &= ~S3C2410_TCFG1_MUX4_MASK;
  160. tcfg1 |= S3C2410_TCFG1_MUX4_DIV2;
  161. tcfg0 &= ~S3C2410_TCFG_PRESCALER1_MASK;
  162. tcfg0 |= ((6 - 1) / 2) << S3C2410_TCFG_PRESCALER1_SHIFT;
  163. tcnt = (pclk / 6) / HZ;
  164. }
  165. /* timers reload after counting zero, so reduce the count by 1 */
  166. tcnt--;
  167. printk("timer tcon=%08lx, tcnt %04lx, tcfg %08lx,%08lx, usec %08lx\n",
  168. tcon, tcnt, tcfg0, tcfg1, timer_usec_ticks);
  169. /* check to see if timer is within 16bit range... */
  170. if (tcnt > 0xffff) {
  171. panic("setup_timer: HZ is too small, cannot configure timer!");
  172. return;
  173. }
  174. __raw_writel(tcfg1, S3C2410_TCFG1);
  175. __raw_writel(tcfg0, S3C2410_TCFG0);
  176. timer_startval = tcnt;
  177. __raw_writel(tcnt, S3C2410_TCNTB(4));
  178. /* ensure timer is stopped... */
  179. tcon &= ~(7<<20);
  180. tcon |= S3C2410_TCON_T4RELOAD;
  181. tcon |= S3C2410_TCON_T4MANUALUPD;
  182. __raw_writel(tcon, S3C2410_TCON);
  183. __raw_writel(tcnt, S3C2410_TCNTB(4));
  184. __raw_writel(tcnt, S3C2410_TCMPB(4));
  185. /* start the timer running */
  186. tcon |= S3C2410_TCON_T4START;
  187. tcon &= ~S3C2410_TCON_T4MANUALUPD;
  188. __raw_writel(tcon, S3C2410_TCON);
  189. }
  190. static void __init s3c2410_timer_init (void)
  191. {
  192. s3c2410_timer_setup();
  193. setup_irq(IRQ_TIMER4, &s3c2410_timer_irq);
  194. }
  195. struct sys_timer s3c24xx_timer = {
  196. .init = s3c2410_timer_init,
  197. .offset = s3c2410_gettimeoffset,
  198. .resume = s3c2410_timer_setup
  199. };