counter_32k.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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/clocksource.h>
  21. #include <linux/sched_clock.h>
  22. #include <asm/mach/time.h>
  23. #include <plat/counter-32k.h>
  24. /* OMAP2_32KSYNCNT_CR_OFF: offset of 32ksync counter register */
  25. #define OMAP2_32KSYNCNT_REV_OFF 0x0
  26. #define OMAP2_32KSYNCNT_REV_SCHEME (0x3 << 30)
  27. #define OMAP2_32KSYNCNT_CR_OFF_LOW 0x10
  28. #define OMAP2_32KSYNCNT_CR_OFF_HIGH 0x30
  29. /*
  30. * 32KHz clocksource ... always available, on pretty most chips except
  31. * OMAP 730 and 1510. Other timers could be used as clocksources, with
  32. * higher resolution in free-running counter modes (e.g. 12 MHz xtal),
  33. * but systems won't necessarily want to spend resources that way.
  34. */
  35. static void __iomem *sync32k_cnt_reg;
  36. static u64 notrace omap_32k_read_sched_clock(void)
  37. {
  38. return sync32k_cnt_reg ? readl_relaxed(sync32k_cnt_reg) : 0;
  39. }
  40. /**
  41. * omap_read_persistent_clock64 - Return time from a persistent clock.
  42. *
  43. * Reads the time from a source which isn't disabled during PM, the
  44. * 32k sync timer. Convert the cycles elapsed since last read into
  45. * nsecs and adds to a monotonically increasing timespec64.
  46. */
  47. static struct timespec64 persistent_ts;
  48. static cycles_t cycles;
  49. static unsigned int persistent_mult, persistent_shift;
  50. static void omap_read_persistent_clock64(struct timespec64 *ts)
  51. {
  52. unsigned long long nsecs;
  53. cycles_t last_cycles;
  54. last_cycles = cycles;
  55. cycles = sync32k_cnt_reg ? readl_relaxed(sync32k_cnt_reg) : 0;
  56. nsecs = clocksource_cyc2ns(cycles - last_cycles,
  57. persistent_mult, persistent_shift);
  58. timespec64_add_ns(&persistent_ts, nsecs);
  59. *ts = persistent_ts;
  60. }
  61. /**
  62. * omap_init_clocksource_32k - setup and register counter 32k as a
  63. * kernel clocksource
  64. * @pbase: base addr of counter_32k module
  65. * @size: size of counter_32k to map
  66. *
  67. * Returns 0 upon success or negative error code upon failure.
  68. *
  69. */
  70. int __init omap_init_clocksource_32k(void __iomem *vbase)
  71. {
  72. int ret;
  73. /*
  74. * 32k sync Counter IP register offsets vary between the
  75. * highlander version and the legacy ones.
  76. * The 'SCHEME' bits(30-31) of the revision register is used
  77. * to identify the version.
  78. */
  79. if (readl_relaxed(vbase + OMAP2_32KSYNCNT_REV_OFF) &
  80. OMAP2_32KSYNCNT_REV_SCHEME)
  81. sync32k_cnt_reg = vbase + OMAP2_32KSYNCNT_CR_OFF_HIGH;
  82. else
  83. sync32k_cnt_reg = vbase + OMAP2_32KSYNCNT_CR_OFF_LOW;
  84. /*
  85. * 120000 rough estimate from the calculations in
  86. * __clocksource_update_freq_scale.
  87. */
  88. clocks_calc_mult_shift(&persistent_mult, &persistent_shift,
  89. 32768, NSEC_PER_SEC, 120000);
  90. ret = clocksource_mmio_init(sync32k_cnt_reg, "32k_counter", 32768,
  91. 250, 32, clocksource_mmio_readl_up);
  92. if (ret) {
  93. pr_err("32k_counter: can't register clocksource\n");
  94. return ret;
  95. }
  96. sched_clock_register(omap_32k_read_sched_clock, 32, 32768);
  97. register_persistent_clock(omap_read_persistent_clock64);
  98. pr_info("OMAP clocksource: 32k_counter at 32768 Hz\n");
  99. return 0;
  100. }