counter_32k.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. #if !(defined(CONFIG_ARCH_OMAP730) || defined(CONFIG_ARCH_OMAP15XX))
  33. #include <linux/clocksource.h>
  34. /*
  35. * offset_32k holds the init time counter value. It is then subtracted
  36. * from every counter read to achieve a counter that counts time from the
  37. * kernel boot (needed for sched_clock()).
  38. */
  39. static u32 offset_32k __read_mostly;
  40. #ifdef CONFIG_ARCH_OMAP16XX
  41. static cycle_t notrace omap16xx_32k_read(struct clocksource *cs)
  42. {
  43. return omap_readl(OMAP16XX_TIMER_32K_SYNCHRONIZED) - offset_32k;
  44. }
  45. #else
  46. #define omap16xx_32k_read NULL
  47. #endif
  48. #ifdef CONFIG_ARCH_OMAP2420
  49. static cycle_t notrace omap2420_32k_read(struct clocksource *cs)
  50. {
  51. return omap_readl(OMAP2420_32KSYNCT_BASE + 0x10) - offset_32k;
  52. }
  53. #else
  54. #define omap2420_32k_read NULL
  55. #endif
  56. #ifdef CONFIG_ARCH_OMAP2430
  57. static cycle_t notrace omap2430_32k_read(struct clocksource *cs)
  58. {
  59. return omap_readl(OMAP2430_32KSYNCT_BASE + 0x10) - offset_32k;
  60. }
  61. #else
  62. #define omap2430_32k_read NULL
  63. #endif
  64. #ifdef CONFIG_ARCH_OMAP3
  65. static cycle_t notrace omap34xx_32k_read(struct clocksource *cs)
  66. {
  67. return omap_readl(OMAP3430_32KSYNCT_BASE + 0x10) - offset_32k;
  68. }
  69. #else
  70. #define omap34xx_32k_read NULL
  71. #endif
  72. #ifdef CONFIG_ARCH_OMAP4
  73. static cycle_t notrace omap44xx_32k_read(struct clocksource *cs)
  74. {
  75. return omap_readl(OMAP4430_32KSYNCT_BASE + 0x10) - offset_32k;
  76. }
  77. #else
  78. #define omap44xx_32k_read NULL
  79. #endif
  80. /*
  81. * Kernel assumes that sched_clock can be called early but may not have
  82. * things ready yet.
  83. */
  84. static cycle_t notrace omap_32k_read_dummy(struct clocksource *cs)
  85. {
  86. return 0;
  87. }
  88. static struct clocksource clocksource_32k = {
  89. .name = "32k_counter",
  90. .rating = 250,
  91. .read = omap_32k_read_dummy,
  92. .mask = CLOCKSOURCE_MASK(32),
  93. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  94. };
  95. /*
  96. * Returns current time from boot in nsecs. It's OK for this to wrap
  97. * around for now, as it's just a relative time stamp.
  98. */
  99. static DEFINE_CLOCK_DATA(cd);
  100. /*
  101. * Constants generated by clocks_calc_mult_shift(m, s, 32768, NSEC_PER_SEC, 60).
  102. * This gives a resolution of about 30us and a wrap period of about 36hrs.
  103. */
  104. #define SC_MULT 4000000000u
  105. #define SC_SHIFT 17
  106. unsigned long long notrace sched_clock(void)
  107. {
  108. u32 cyc = clocksource_32k.read(&clocksource_32k);
  109. return cyc_to_fixed_sched_clock(&cd, cyc, (u32)~0, SC_MULT, SC_SHIFT);
  110. }
  111. static void notrace omap_update_sched_clock(void)
  112. {
  113. u32 cyc = clocksource_32k.read(&clocksource_32k);
  114. update_sched_clock(&cd, cyc, (u32)~0);
  115. }
  116. /**
  117. * read_persistent_clock - Return time from a persistent clock.
  118. *
  119. * Reads the time from a source which isn't disabled during PM, the
  120. * 32k sync timer. Convert the cycles elapsed since last read into
  121. * nsecs and adds to a monotonically increasing timespec.
  122. */
  123. static struct timespec persistent_ts;
  124. static cycles_t cycles, last_cycles;
  125. void read_persistent_clock(struct timespec *ts)
  126. {
  127. unsigned long long nsecs;
  128. cycles_t delta;
  129. struct timespec *tsp = &persistent_ts;
  130. last_cycles = cycles;
  131. cycles = clocksource_32k.read(&clocksource_32k);
  132. delta = cycles - last_cycles;
  133. nsecs = clocksource_cyc2ns(delta,
  134. clocksource_32k.mult, clocksource_32k.shift);
  135. timespec_add_ns(tsp, nsecs);
  136. *ts = *tsp;
  137. }
  138. static int __init omap_init_clocksource_32k(void)
  139. {
  140. static char err[] __initdata = KERN_ERR
  141. "%s: can't register clocksource!\n";
  142. if (cpu_is_omap16xx() || cpu_class_is_omap2()) {
  143. struct clk *sync_32k_ick;
  144. if (cpu_is_omap16xx())
  145. clocksource_32k.read = omap16xx_32k_read;
  146. else if (cpu_is_omap2420())
  147. clocksource_32k.read = omap2420_32k_read;
  148. else if (cpu_is_omap2430())
  149. clocksource_32k.read = omap2430_32k_read;
  150. else if (cpu_is_omap34xx())
  151. clocksource_32k.read = omap34xx_32k_read;
  152. else if (cpu_is_omap44xx())
  153. clocksource_32k.read = omap44xx_32k_read;
  154. else
  155. return -ENODEV;
  156. sync_32k_ick = clk_get(NULL, "omap_32ksync_ick");
  157. if (!IS_ERR(sync_32k_ick))
  158. clk_enable(sync_32k_ick);
  159. offset_32k = clocksource_32k.read(&clocksource_32k);
  160. if (clocksource_register_hz(&clocksource_32k, 32768))
  161. printk(err, clocksource_32k.name);
  162. init_fixed_sched_clock(&cd, omap_update_sched_clock, 32,
  163. 32768, SC_MULT, SC_SHIFT);
  164. }
  165. return 0;
  166. }
  167. arch_initcall(omap_init_clocksource_32k);
  168. #endif /* !(defined(CONFIG_ARCH_OMAP730) || defined(CONFIG_ARCH_OMAP15XX)) */