counter_32k.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /*
  2. * OMAP 32ksynctimer/counter_32k-related code
  3. *
  4. * Copyright (C) 2009 Texas Instruments
  5. * Copyright (C) 2010 Nokia Corporation
  6. * Tony Lindgren <tony@atomide.com>
  7. * Added OMAP4 support - Santosh Shilimkar <santosh.shilimkar@ti.com>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2 as
  11. * published by the Free Software Foundation.
  12. *
  13. * NOTE: This timer is not the same timer as the old OMAP1 MPU timer.
  14. */
  15. #include <linux/kernel.h>
  16. #include <linux/init.h>
  17. #include <linux/clk.h>
  18. #include <linux/err.h>
  19. #include <linux/io.h>
  20. #include <linux/sched.h>
  21. #include <asm/sched_clock.h>
  22. #include <plat/common.h>
  23. #include <plat/board.h>
  24. #include <plat/clock.h>
  25. /*
  26. * 32KHz clocksource ... always available, on pretty most chips except
  27. * OMAP 730 and 1510. Other timers could be used as clocksources, with
  28. * higher resolution in free-running counter modes (e.g. 12 MHz xtal),
  29. * but systems won't necessarily want to spend resources that way.
  30. */
  31. #define OMAP16XX_TIMER_32K_SYNCHRONIZED 0xfffbc410
  32. #include <linux/clocksource.h>
  33. /*
  34. * offset_32k holds the init time counter value. It is then subtracted
  35. * from every counter read to achieve a counter that counts time from the
  36. * kernel boot (needed for sched_clock()).
  37. */
  38. static u32 offset_32k __read_mostly;
  39. #ifdef CONFIG_ARCH_OMAP16XX
  40. static cycle_t notrace omap16xx_32k_read(struct clocksource *cs)
  41. {
  42. return omap_readl(OMAP16XX_TIMER_32K_SYNCHRONIZED) - offset_32k;
  43. }
  44. #else
  45. #define omap16xx_32k_read NULL
  46. #endif
  47. #ifdef CONFIG_SOC_OMAP2420
  48. static cycle_t notrace omap2420_32k_read(struct clocksource *cs)
  49. {
  50. return omap_readl(OMAP2420_32KSYNCT_BASE + 0x10) - offset_32k;
  51. }
  52. #else
  53. #define omap2420_32k_read NULL
  54. #endif
  55. #ifdef CONFIG_SOC_OMAP2430
  56. static cycle_t notrace omap2430_32k_read(struct clocksource *cs)
  57. {
  58. return omap_readl(OMAP2430_32KSYNCT_BASE + 0x10) - offset_32k;
  59. }
  60. #else
  61. #define omap2430_32k_read NULL
  62. #endif
  63. #ifdef CONFIG_ARCH_OMAP3
  64. static cycle_t notrace omap34xx_32k_read(struct clocksource *cs)
  65. {
  66. return omap_readl(OMAP3430_32KSYNCT_BASE + 0x10) - offset_32k;
  67. }
  68. #else
  69. #define omap34xx_32k_read NULL
  70. #endif
  71. #ifdef CONFIG_ARCH_OMAP4
  72. static cycle_t notrace omap44xx_32k_read(struct clocksource *cs)
  73. {
  74. return omap_readl(OMAP4430_32KSYNCT_BASE + 0x10) - offset_32k;
  75. }
  76. #else
  77. #define omap44xx_32k_read NULL
  78. #endif
  79. /*
  80. * Kernel assumes that sched_clock can be called early but may not have
  81. * things ready yet.
  82. */
  83. static cycle_t notrace omap_32k_read_dummy(struct clocksource *cs)
  84. {
  85. return 0;
  86. }
  87. static struct clocksource clocksource_32k = {
  88. .name = "32k_counter",
  89. .rating = 250,
  90. .read = omap_32k_read_dummy,
  91. .mask = CLOCKSOURCE_MASK(32),
  92. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  93. };
  94. /*
  95. * Returns current time from boot in nsecs. It's OK for this to wrap
  96. * around for now, as it's just a relative time stamp.
  97. */
  98. static DEFINE_CLOCK_DATA(cd);
  99. /*
  100. * Constants generated by clocks_calc_mult_shift(m, s, 32768, NSEC_PER_SEC, 60).
  101. * This gives a resolution of about 30us and a wrap period of about 36hrs.
  102. */
  103. #define SC_MULT 4000000000u
  104. #define SC_SHIFT 17
  105. static inline unsigned long long notrace _omap_32k_sched_clock(void)
  106. {
  107. u32 cyc = clocksource_32k.read(&clocksource_32k);
  108. return cyc_to_fixed_sched_clock(&cd, cyc, (u32)~0, SC_MULT, SC_SHIFT);
  109. }
  110. #ifndef CONFIG_OMAP_MPU_TIMER
  111. unsigned long long notrace sched_clock(void)
  112. {
  113. return _omap_32k_sched_clock();
  114. }
  115. #else
  116. unsigned long long notrace omap_32k_sched_clock(void)
  117. {
  118. return _omap_32k_sched_clock();
  119. }
  120. #endif
  121. static void notrace omap_update_sched_clock(void)
  122. {
  123. u32 cyc = clocksource_32k.read(&clocksource_32k);
  124. update_sched_clock(&cd, cyc, (u32)~0);
  125. }
  126. /**
  127. * read_persistent_clock - Return time from a persistent clock.
  128. *
  129. * Reads the time from a source which isn't disabled during PM, the
  130. * 32k sync timer. Convert the cycles elapsed since last read into
  131. * nsecs and adds to a monotonically increasing timespec.
  132. */
  133. static struct timespec persistent_ts;
  134. static cycles_t cycles, last_cycles;
  135. void read_persistent_clock(struct timespec *ts)
  136. {
  137. unsigned long long nsecs;
  138. cycles_t delta;
  139. struct timespec *tsp = &persistent_ts;
  140. last_cycles = cycles;
  141. cycles = clocksource_32k.read(&clocksource_32k);
  142. delta = cycles - last_cycles;
  143. nsecs = clocksource_cyc2ns(delta,
  144. clocksource_32k.mult, clocksource_32k.shift);
  145. timespec_add_ns(tsp, nsecs);
  146. *ts = *tsp;
  147. }
  148. int __init omap_init_clocksource_32k(void)
  149. {
  150. static char err[] __initdata = KERN_ERR
  151. "%s: can't register clocksource!\n";
  152. if (cpu_is_omap16xx() || cpu_class_is_omap2()) {
  153. struct clk *sync_32k_ick;
  154. if (cpu_is_omap16xx())
  155. clocksource_32k.read = omap16xx_32k_read;
  156. else if (cpu_is_omap2420())
  157. clocksource_32k.read = omap2420_32k_read;
  158. else if (cpu_is_omap2430())
  159. clocksource_32k.read = omap2430_32k_read;
  160. else if (cpu_is_omap34xx())
  161. clocksource_32k.read = omap34xx_32k_read;
  162. else if (cpu_is_omap44xx())
  163. clocksource_32k.read = omap44xx_32k_read;
  164. else
  165. return -ENODEV;
  166. sync_32k_ick = clk_get(NULL, "omap_32ksync_ick");
  167. if (!IS_ERR(sync_32k_ick))
  168. clk_enable(sync_32k_ick);
  169. offset_32k = clocksource_32k.read(&clocksource_32k);
  170. if (clocksource_register_hz(&clocksource_32k, 32768))
  171. printk(err, clocksource_32k.name);
  172. init_fixed_sched_clock(&cd, omap_update_sched_clock, 32,
  173. 32768, SC_MULT, SC_SHIFT);
  174. }
  175. return 0;
  176. }