timer32k.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. /*
  2. * linux/arch/arm/plat-omap/timer32k.c
  3. *
  4. * OMAP 32K Timer
  5. *
  6. * Copyright (C) 2004 - 2005 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 <linux/err.h>
  43. #include <linux/clk.h>
  44. #include <asm/system.h>
  45. #include <asm/hardware.h>
  46. #include <asm/io.h>
  47. #include <asm/leds.h>
  48. #include <asm/irq.h>
  49. #include <asm/mach/irq.h>
  50. #include <asm/mach/time.h>
  51. struct sys_timer omap_timer;
  52. /*
  53. * ---------------------------------------------------------------------------
  54. * 32KHz OS timer
  55. *
  56. * This currently works only on 16xx, as 1510 does not have the continuous
  57. * 32KHz synchronous timer. The 32KHz synchronous timer is used to keep track
  58. * of time in addition to the 32KHz OS timer. Using only the 32KHz OS timer
  59. * on 1510 would be possible, but the timer would not be as accurate as
  60. * with the 32KHz synchronized timer.
  61. * ---------------------------------------------------------------------------
  62. */
  63. #if defined(CONFIG_ARCH_OMAP16XX)
  64. #define TIMER_32K_SYNCHRONIZED 0xfffbc410
  65. #elif defined(CONFIG_ARCH_OMAP24XX)
  66. #define TIMER_32K_SYNCHRONIZED 0x48004010
  67. #else
  68. #error OMAP 32KHz timer does not currently work on 15XX!
  69. #endif
  70. /* 16xx specific defines */
  71. #define OMAP1_32K_TIMER_BASE 0xfffb9000
  72. #define OMAP1_32K_TIMER_CR 0x08
  73. #define OMAP1_32K_TIMER_TVR 0x00
  74. #define OMAP1_32K_TIMER_TCR 0x04
  75. /* 24xx specific defines */
  76. #define OMAP2_GP_TIMER_BASE 0x48028000
  77. #define CM_CLKSEL_WKUP 0x48008440
  78. #define GP_TIMER_TIDR 0x00
  79. #define GP_TIMER_TISR 0x18
  80. #define GP_TIMER_TIER 0x1c
  81. #define GP_TIMER_TCLR 0x24
  82. #define GP_TIMER_TCRR 0x28
  83. #define GP_TIMER_TLDR 0x2c
  84. #define GP_TIMER_TTGR 0x30
  85. #define GP_TIMER_TSICR 0x40
  86. #define OMAP_32K_TICKS_PER_HZ (32768 / HZ)
  87. /*
  88. * TRM says 1 / HZ = ( TVR + 1) / 32768, so TRV = (32768 / HZ) - 1
  89. * so with HZ = 128, TVR = 255.
  90. */
  91. #define OMAP_32K_TIMER_TICK_PERIOD ((32768 / HZ) - 1)
  92. #define JIFFIES_TO_HW_TICKS(nr_jiffies, clock_rate) \
  93. (((nr_jiffies) * (clock_rate)) / HZ)
  94. static inline void omap_32k_timer_write(int val, int reg)
  95. {
  96. if (cpu_class_is_omap1())
  97. omap_writew(val, OMAP1_32K_TIMER_BASE + reg);
  98. if (cpu_is_omap24xx())
  99. omap_writel(val, OMAP2_GP_TIMER_BASE + reg);
  100. }
  101. static inline unsigned long omap_32k_timer_read(int reg)
  102. {
  103. if (cpu_class_is_omap1())
  104. return omap_readl(OMAP1_32K_TIMER_BASE + reg) & 0xffffff;
  105. if (cpu_is_omap24xx())
  106. return omap_readl(OMAP2_GP_TIMER_BASE + reg);
  107. }
  108. /*
  109. * The 32KHz synchronized timer is an additional timer on 16xx.
  110. * It is always running.
  111. */
  112. static inline unsigned long omap_32k_sync_timer_read(void)
  113. {
  114. return omap_readl(TIMER_32K_SYNCHRONIZED);
  115. }
  116. static inline void omap_32k_timer_start(unsigned long load_val)
  117. {
  118. if (cpu_class_is_omap1()) {
  119. omap_32k_timer_write(load_val, OMAP1_32K_TIMER_TVR);
  120. omap_32k_timer_write(0x0f, OMAP1_32K_TIMER_CR);
  121. }
  122. if (cpu_is_omap24xx()) {
  123. omap_32k_timer_write(0xffffffff - load_val, GP_TIMER_TCRR);
  124. omap_32k_timer_write((1 << 1), GP_TIMER_TIER);
  125. omap_32k_timer_write((1 << 1) | 1, GP_TIMER_TCLR);
  126. }
  127. }
  128. static inline void omap_32k_timer_stop(void)
  129. {
  130. if (cpu_class_is_omap1())
  131. omap_32k_timer_write(0x0, OMAP1_32K_TIMER_CR);
  132. if (cpu_is_omap24xx())
  133. omap_32k_timer_write(0x0, GP_TIMER_TCLR);
  134. }
  135. /*
  136. * Rounds down to nearest usec. Note that this will overflow for larger values.
  137. */
  138. static inline unsigned long omap_32k_ticks_to_usecs(unsigned long ticks_32k)
  139. {
  140. return (ticks_32k * 5*5*5*5*5*5) >> 9;
  141. }
  142. /*
  143. * Rounds down to nearest nsec.
  144. */
  145. static inline unsigned long long
  146. omap_32k_ticks_to_nsecs(unsigned long ticks_32k)
  147. {
  148. return (unsigned long long) ticks_32k * 1000 * 5*5*5*5*5*5 >> 9;
  149. }
  150. static unsigned long omap_32k_last_tick = 0;
  151. /*
  152. * Returns elapsed usecs since last 32k timer interrupt
  153. */
  154. static unsigned long omap_32k_timer_gettimeoffset(void)
  155. {
  156. unsigned long now = omap_32k_sync_timer_read();
  157. return omap_32k_ticks_to_usecs(now - omap_32k_last_tick);
  158. }
  159. /*
  160. * Returns current time from boot in nsecs. It's OK for this to wrap
  161. * around for now, as it's just a relative time stamp.
  162. */
  163. unsigned long long sched_clock(void)
  164. {
  165. return omap_32k_ticks_to_nsecs(omap_32k_sync_timer_read());
  166. }
  167. /*
  168. * Timer interrupt for 32KHz timer. When dynamic tick is enabled, this
  169. * function is also called from other interrupts to remove latency
  170. * issues with dynamic tick. In the dynamic tick case, we need to lock
  171. * with irqsave.
  172. */
  173. static irqreturn_t omap_32k_timer_interrupt(int irq, void *dev_id,
  174. struct pt_regs *regs)
  175. {
  176. unsigned long flags;
  177. unsigned long now;
  178. write_seqlock_irqsave(&xtime_lock, flags);
  179. if (cpu_is_omap24xx()) {
  180. u32 status = omap_32k_timer_read(GP_TIMER_TISR);
  181. omap_32k_timer_write(status, GP_TIMER_TISR);
  182. }
  183. now = omap_32k_sync_timer_read();
  184. while (now - omap_32k_last_tick >= OMAP_32K_TICKS_PER_HZ) {
  185. omap_32k_last_tick += OMAP_32K_TICKS_PER_HZ;
  186. timer_tick(regs);
  187. }
  188. /* Restart timer so we don't drift off due to modulo or dynamic tick.
  189. * By default we program the next timer to be continuous to avoid
  190. * latencies during high system load. During dynamic tick operation the
  191. * continuous timer can be overridden from pm_idle to be longer.
  192. */
  193. omap_32k_timer_start(omap_32k_last_tick + OMAP_32K_TICKS_PER_HZ - now);
  194. write_sequnlock_irqrestore(&xtime_lock, flags);
  195. return IRQ_HANDLED;
  196. }
  197. #ifdef CONFIG_NO_IDLE_HZ
  198. /*
  199. * Programs the next timer interrupt needed. Called when dynamic tick is
  200. * enabled, and to reprogram the ticks to skip from pm_idle. Note that
  201. * we can keep the timer continuous, and don't need to set it to run in
  202. * one-shot mode. This is because the timer will get reprogrammed again
  203. * after next interrupt.
  204. */
  205. void omap_32k_timer_reprogram(unsigned long next_tick)
  206. {
  207. omap_32k_timer_start(JIFFIES_TO_HW_TICKS(next_tick, 32768) + 1);
  208. }
  209. static struct irqaction omap_32k_timer_irq;
  210. extern struct timer_update_handler timer_update;
  211. static int omap_32k_timer_enable_dyn_tick(void)
  212. {
  213. /* No need to reprogram timer, just use the next interrupt */
  214. return 0;
  215. }
  216. static int omap_32k_timer_disable_dyn_tick(void)
  217. {
  218. omap_32k_timer_start(OMAP_32K_TIMER_TICK_PERIOD);
  219. return 0;
  220. }
  221. static struct dyn_tick_timer omap_dyn_tick_timer = {
  222. .enable = omap_32k_timer_enable_dyn_tick,
  223. .disable = omap_32k_timer_disable_dyn_tick,
  224. .reprogram = omap_32k_timer_reprogram,
  225. .handler = omap_32k_timer_interrupt,
  226. };
  227. #endif /* CONFIG_NO_IDLE_HZ */
  228. static struct irqaction omap_32k_timer_irq = {
  229. .name = "32KHz timer",
  230. .flags = SA_INTERRUPT | SA_TIMER,
  231. .handler = omap_32k_timer_interrupt,
  232. };
  233. static struct clk * gpt1_ick;
  234. static struct clk * gpt1_fck;
  235. static __init void omap_init_32k_timer(void)
  236. {
  237. #ifdef CONFIG_NO_IDLE_HZ
  238. omap_timer.dyn_tick = &omap_dyn_tick_timer;
  239. #endif
  240. if (cpu_class_is_omap1())
  241. setup_irq(INT_OS_TIMER, &omap_32k_timer_irq);
  242. if (cpu_is_omap24xx())
  243. setup_irq(37, &omap_32k_timer_irq);
  244. omap_timer.offset = omap_32k_timer_gettimeoffset;
  245. omap_32k_last_tick = omap_32k_sync_timer_read();
  246. /* REVISIT: Check 24xx TIOCP_CFG settings after idle works */
  247. if (cpu_is_omap24xx()) {
  248. omap_32k_timer_write(0, GP_TIMER_TCLR);
  249. omap_writel(0, CM_CLKSEL_WKUP); /* 32KHz clock source */
  250. gpt1_ick = clk_get(NULL, "gpt1_ick");
  251. if (IS_ERR(gpt1_ick))
  252. printk(KERN_ERR "Could not get gpt1_ick\n");
  253. else
  254. clk_enable(gpt1_ick);
  255. gpt1_fck = clk_get(NULL, "gpt1_fck");
  256. if (IS_ERR(gpt1_fck))
  257. printk(KERN_ERR "Could not get gpt1_fck\n");
  258. else
  259. clk_enable(gpt1_fck);
  260. mdelay(100); /* Wait for clocks to stabilize */
  261. omap_32k_timer_write(0x7, GP_TIMER_TISR);
  262. }
  263. omap_32k_timer_start(OMAP_32K_TIMER_TICK_PERIOD);
  264. }
  265. /*
  266. * ---------------------------------------------------------------------------
  267. * Timer initialization
  268. * ---------------------------------------------------------------------------
  269. */
  270. static void __init omap_timer_init(void)
  271. {
  272. omap_init_32k_timer();
  273. }
  274. struct sys_timer omap_timer = {
  275. .init = omap_timer_init,
  276. .offset = NULL, /* Initialized later */
  277. };