counter_32k.c 4.7 KB

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