time.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. /* linux/arch/arm/plat-samsung/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/kernel.h>
  21. #include <linux/sched.h>
  22. #include <linux/init.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/irq.h>
  25. #include <linux/err.h>
  26. #include <linux/clk.h>
  27. #include <linux/io.h>
  28. #include <linux/platform_device.h>
  29. #include <asm/leds.h>
  30. #include <asm/mach-types.h>
  31. #include <asm/irq.h>
  32. #include <mach/map.h>
  33. #include <plat/regs-timer.h>
  34. #include <mach/regs-irq.h>
  35. #include <asm/mach/time.h>
  36. #include <mach/tick.h>
  37. #include <plat/clock.h>
  38. #include <plat/cpu.h>
  39. static unsigned long timer_startval;
  40. static unsigned long timer_usec_ticks;
  41. #ifndef TICK_MAX
  42. #define TICK_MAX (0xffff)
  43. #endif
  44. #define TIMER_USEC_SHIFT 16
  45. /* we use the shifted arithmetic to work out the ratio of timer ticks
  46. * to usecs, as often the peripheral clock is not a nice even multiple
  47. * of 1MHz.
  48. *
  49. * shift of 14 and 15 are too low for the 12MHz, 16 seems to be ok
  50. * for the current HZ value of 200 without producing overflows.
  51. *
  52. * Original patch by Dimitry Andric, updated by Ben Dooks
  53. */
  54. /* timer_mask_usec_ticks
  55. *
  56. * given a clock and divisor, make the value to pass into timer_ticks_to_usec
  57. * to scale the ticks into usecs
  58. */
  59. static inline unsigned long
  60. timer_mask_usec_ticks(unsigned long scaler, unsigned long pclk)
  61. {
  62. unsigned long den = pclk / 1000;
  63. return ((1000 << TIMER_USEC_SHIFT) * scaler + (den >> 1)) / den;
  64. }
  65. /* timer_ticks_to_usec
  66. *
  67. * convert timer ticks to usec.
  68. */
  69. static inline unsigned long timer_ticks_to_usec(unsigned long ticks)
  70. {
  71. unsigned long res;
  72. res = ticks * timer_usec_ticks;
  73. res += 1 << (TIMER_USEC_SHIFT - 4); /* round up slightly */
  74. return res >> TIMER_USEC_SHIFT;
  75. }
  76. /***
  77. * Returns microsecond since last clock interrupt. Note that interrupts
  78. * will have been disabled by do_gettimeoffset()
  79. * IRQs are disabled before entering here from do_gettimeofday()
  80. */
  81. static unsigned long s3c2410_gettimeoffset (void)
  82. {
  83. unsigned long tdone;
  84. unsigned long tval;
  85. /* work out how many ticks have gone since last timer interrupt */
  86. tval = __raw_readl(S3C2410_TCNTO(4));
  87. tdone = timer_startval - tval;
  88. /* check to see if there is an interrupt pending */
  89. if (s3c24xx_ostimer_pending()) {
  90. /* re-read the timer, and try and fix up for the missed
  91. * interrupt. Note, the interrupt may go off before the
  92. * timer has re-loaded from wrapping.
  93. */
  94. tval = __raw_readl(S3C2410_TCNTO(4));
  95. tdone = timer_startval - tval;
  96. if (tval != 0)
  97. tdone += timer_startval;
  98. }
  99. return timer_ticks_to_usec(tdone);
  100. }
  101. /*
  102. * IRQ handler for the timer
  103. */
  104. static irqreturn_t
  105. s3c2410_timer_interrupt(int irq, void *dev_id)
  106. {
  107. timer_tick();
  108. return IRQ_HANDLED;
  109. }
  110. static struct irqaction s3c2410_timer_irq = {
  111. .name = "S3C2410 Timer Tick",
  112. .flags = IRQF_DISABLED | IRQF_TIMER | IRQF_IRQPOLL,
  113. .handler = s3c2410_timer_interrupt,
  114. };
  115. #define use_tclk1_12() ( \
  116. machine_is_bast() || \
  117. machine_is_vr1000() || \
  118. machine_is_anubis() || \
  119. machine_is_osiris())
  120. static struct clk *tin;
  121. static struct clk *tdiv;
  122. static struct clk *timerclk;
  123. /*
  124. * Set up timer interrupt, and return the current time in seconds.
  125. *
  126. * Currently we only use timer4, as it is the only timer which has no
  127. * other function that can be exploited externally
  128. */
  129. static void s3c2410_timer_setup (void)
  130. {
  131. unsigned long tcon;
  132. unsigned long tcnt;
  133. unsigned long tcfg1;
  134. unsigned long tcfg0;
  135. tcnt = TICK_MAX; /* default value for tcnt */
  136. /* configure the system for whichever machine is in use */
  137. if (use_tclk1_12()) {
  138. /* timer is at 12MHz, scaler is 1 */
  139. timer_usec_ticks = timer_mask_usec_ticks(1, 12000000);
  140. tcnt = 12000000 / HZ;
  141. tcfg1 = __raw_readl(S3C2410_TCFG1);
  142. tcfg1 &= ~S3C2410_TCFG1_MUX4_MASK;
  143. tcfg1 |= S3C2410_TCFG1_MUX4_TCLK1;
  144. __raw_writel(tcfg1, S3C2410_TCFG1);
  145. } else {
  146. unsigned long pclk;
  147. struct clk *tscaler;
  148. /* for the h1940 (and others), we use the pclk from the core
  149. * to generate the timer values. since values around 50 to
  150. * 70MHz are not values we can directly generate the timer
  151. * value from, we need to pre-scale and divide before using it.
  152. *
  153. * for instance, using 50.7MHz and dividing by 6 gives 8.45MHz
  154. * (8.45 ticks per usec)
  155. */
  156. pclk = clk_get_rate(timerclk);
  157. /* configure clock tick */
  158. timer_usec_ticks = timer_mask_usec_ticks(6, pclk);
  159. tscaler = clk_get_parent(tdiv);
  160. clk_set_rate(tscaler, pclk / 3);
  161. clk_set_rate(tdiv, pclk / 6);
  162. clk_set_parent(tin, tdiv);
  163. tcnt = clk_get_rate(tin) / HZ;
  164. }
  165. tcon = __raw_readl(S3C2410_TCON);
  166. tcfg0 = __raw_readl(S3C2410_TCFG0);
  167. tcfg1 = __raw_readl(S3C2410_TCFG1);
  168. /* timers reload after counting zero, so reduce the count by 1 */
  169. tcnt--;
  170. printk(KERN_DEBUG "timer tcon=%08lx, tcnt %04lx, tcfg %08lx,%08lx, usec %08lx\n",
  171. tcon, tcnt, tcfg0, tcfg1, timer_usec_ticks);
  172. /* check to see if timer is within 16bit range... */
  173. if (tcnt > TICK_MAX) {
  174. panic("setup_timer: HZ is too small, cannot configure timer!");
  175. return;
  176. }
  177. __raw_writel(tcfg1, S3C2410_TCFG1);
  178. __raw_writel(tcfg0, S3C2410_TCFG0);
  179. timer_startval = tcnt;
  180. __raw_writel(tcnt, S3C2410_TCNTB(4));
  181. /* ensure timer is stopped... */
  182. tcon &= ~(7<<20);
  183. tcon |= S3C2410_TCON_T4RELOAD;
  184. tcon |= S3C2410_TCON_T4MANUALUPD;
  185. __raw_writel(tcon, S3C2410_TCON);
  186. __raw_writel(tcnt, S3C2410_TCNTB(4));
  187. __raw_writel(tcnt, S3C2410_TCMPB(4));
  188. /* start the timer running */
  189. tcon |= S3C2410_TCON_T4START;
  190. tcon &= ~S3C2410_TCON_T4MANUALUPD;
  191. __raw_writel(tcon, S3C2410_TCON);
  192. }
  193. static void __init s3c2410_timer_resources(void)
  194. {
  195. struct platform_device tmpdev;
  196. tmpdev.dev.bus = &platform_bus_type;
  197. tmpdev.id = 4;
  198. timerclk = clk_get(NULL, "timers");
  199. if (IS_ERR(timerclk))
  200. panic("failed to get clock for system timer");
  201. clk_enable(timerclk);
  202. if (!use_tclk1_12()) {
  203. tmpdev.id = 4;
  204. tmpdev.dev.init_name = "s3c24xx-pwm.4";
  205. tin = clk_get(&tmpdev.dev, "pwm-tin");
  206. if (IS_ERR(tin))
  207. panic("failed to get pwm-tin clock for system timer");
  208. tdiv = clk_get(&tmpdev.dev, "pwm-tdiv");
  209. if (IS_ERR(tdiv))
  210. panic("failed to get pwm-tdiv clock for system timer");
  211. }
  212. clk_enable(tin);
  213. }
  214. static void __init s3c2410_timer_init(void)
  215. {
  216. s3c2410_timer_resources();
  217. s3c2410_timer_setup();
  218. setup_irq(IRQ_TIMER4, &s3c2410_timer_irq);
  219. }
  220. struct sys_timer s3c24xx_timer = {
  221. .init = s3c2410_timer_init,
  222. .offset = s3c2410_gettimeoffset,
  223. .resume = s3c2410_timer_setup
  224. };