counter_32k.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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/io.h>
  19. #include <plat/common.h>
  20. #include <plat/board.h>
  21. #include <plat/clock.h>
  22. /*
  23. * 32KHz clocksource ... always available, on pretty most chips except
  24. * OMAP 730 and 1510. Other timers could be used as clocksources, with
  25. * higher resolution in free-running counter modes (e.g. 12 MHz xtal),
  26. * but systems won't necessarily want to spend resources that way.
  27. */
  28. #define OMAP16XX_TIMER_32K_SYNCHRONIZED 0xfffbc410
  29. #if !(defined(CONFIG_ARCH_OMAP730) || defined(CONFIG_ARCH_OMAP15XX))
  30. #include <linux/clocksource.h>
  31. /*
  32. * offset_32k holds the init time counter value. It is then subtracted
  33. * from every counter read to achieve a counter that counts time from the
  34. * kernel boot (needed for sched_clock()).
  35. */
  36. static u32 offset_32k __read_mostly;
  37. #ifdef CONFIG_ARCH_OMAP16XX
  38. static cycle_t omap16xx_32k_read(struct clocksource *cs)
  39. {
  40. return omap_readl(OMAP16XX_TIMER_32K_SYNCHRONIZED) - offset_32k;
  41. }
  42. #else
  43. #define omap16xx_32k_read NULL
  44. #endif
  45. #ifdef CONFIG_ARCH_OMAP2420
  46. static cycle_t omap2420_32k_read(struct clocksource *cs)
  47. {
  48. return omap_readl(OMAP2420_32KSYNCT_BASE + 0x10) - offset_32k;
  49. }
  50. #else
  51. #define omap2420_32k_read NULL
  52. #endif
  53. #ifdef CONFIG_ARCH_OMAP2430
  54. static cycle_t omap2430_32k_read(struct clocksource *cs)
  55. {
  56. return omap_readl(OMAP2430_32KSYNCT_BASE + 0x10) - offset_32k;
  57. }
  58. #else
  59. #define omap2430_32k_read NULL
  60. #endif
  61. #ifdef CONFIG_ARCH_OMAP3
  62. static cycle_t omap34xx_32k_read(struct clocksource *cs)
  63. {
  64. return omap_readl(OMAP3430_32KSYNCT_BASE + 0x10) - offset_32k;
  65. }
  66. #else
  67. #define omap34xx_32k_read NULL
  68. #endif
  69. #ifdef CONFIG_ARCH_OMAP4
  70. static cycle_t omap44xx_32k_read(struct clocksource *cs)
  71. {
  72. return omap_readl(OMAP4430_32KSYNCT_BASE + 0x10) - offset_32k;
  73. }
  74. #else
  75. #define omap44xx_32k_read NULL
  76. #endif
  77. /*
  78. * Kernel assumes that sched_clock can be called early but may not have
  79. * things ready yet.
  80. */
  81. static cycle_t omap_32k_read_dummy(struct clocksource *cs)
  82. {
  83. return 0;
  84. }
  85. static struct clocksource clocksource_32k = {
  86. .name = "32k_counter",
  87. .rating = 250,
  88. .read = omap_32k_read_dummy,
  89. .mask = CLOCKSOURCE_MASK(32),
  90. .shift = 10,
  91. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  92. };
  93. /*
  94. * Returns current time from boot in nsecs. It's OK for this to wrap
  95. * around for now, as it's just a relative time stamp.
  96. */
  97. unsigned long long sched_clock(void)
  98. {
  99. return clocksource_cyc2ns(clocksource_32k.read(&clocksource_32k),
  100. clocksource_32k.mult, clocksource_32k.shift);
  101. }
  102. /**
  103. * read_persistent_clock - Return time from a persistent clock.
  104. *
  105. * Reads the time from a source which isn't disabled during PM, the
  106. * 32k sync timer. Convert the cycles elapsed since last read into
  107. * nsecs and adds to a monotonically increasing timespec.
  108. */
  109. static struct timespec persistent_ts;
  110. static cycles_t cycles, last_cycles;
  111. void read_persistent_clock(struct timespec *ts)
  112. {
  113. unsigned long long nsecs;
  114. cycles_t delta;
  115. struct timespec *tsp = &persistent_ts;
  116. last_cycles = cycles;
  117. cycles = clocksource_32k.read(&clocksource_32k);
  118. delta = cycles - last_cycles;
  119. nsecs = clocksource_cyc2ns(delta,
  120. clocksource_32k.mult, clocksource_32k.shift);
  121. timespec_add_ns(tsp, nsecs);
  122. *ts = *tsp;
  123. }
  124. static int __init omap_init_clocksource_32k(void)
  125. {
  126. static char err[] __initdata = KERN_ERR
  127. "%s: can't register clocksource!\n";
  128. if (cpu_is_omap16xx() || cpu_class_is_omap2()) {
  129. struct clk *sync_32k_ick;
  130. if (cpu_is_omap16xx())
  131. clocksource_32k.read = omap16xx_32k_read;
  132. else if (cpu_is_omap2420())
  133. clocksource_32k.read = omap2420_32k_read;
  134. else if (cpu_is_omap2430())
  135. clocksource_32k.read = omap2430_32k_read;
  136. else if (cpu_is_omap34xx())
  137. clocksource_32k.read = omap34xx_32k_read;
  138. else if (cpu_is_omap44xx())
  139. clocksource_32k.read = omap44xx_32k_read;
  140. else
  141. return -ENODEV;
  142. sync_32k_ick = clk_get(NULL, "omap_32ksync_ick");
  143. if (sync_32k_ick)
  144. clk_enable(sync_32k_ick);
  145. clocksource_32k.mult = clocksource_hz2mult(32768,
  146. clocksource_32k.shift);
  147. offset_32k = clocksource_32k.read(&clocksource_32k);
  148. if (clocksource_register(&clocksource_32k))
  149. printk(err, clocksource_32k.name);
  150. }
  151. return 0;
  152. }
  153. arch_initcall(omap_init_clocksource_32k);
  154. #endif /* !(defined(CONFIG_ARCH_OMAP730) || defined(CONFIG_ARCH_OMAP15XX)) */