timer.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /*
  2. * linux/arch/arm/mach-omap2/timer.c
  3. *
  4. * OMAP2 GP timer support.
  5. *
  6. * Copyright (C) 2009 Nokia Corporation
  7. *
  8. * Update to use new clocksource/clockevent layers
  9. * Author: Kevin Hilman, MontaVista Software, Inc. <[email protected]>
  10. * Copyright (C) 2007 MontaVista Software, Inc.
  11. *
  12. * Original driver:
  13. * Copyright (C) 2005 Nokia Corporation
  14. * Author: Paul Mundt <[email protected]>
  15. * Juha Yrjölä <[email protected]>
  16. * OMAP Dual-mode timer framework support by Timo Teras
  17. *
  18. * Some parts based off of TI's 24xx code:
  19. *
  20. * Copyright (C) 2004-2009 Texas Instruments, Inc.
  21. *
  22. * Roughly modelled after the OMAP1 MPU timer code.
  23. * Added OMAP4 support - Santosh Shilimkar <[email protected]>
  24. *
  25. * This file is subject to the terms and conditions of the GNU General Public
  26. * License. See the file "COPYING" in the main directory of this archive
  27. * for more details.
  28. */
  29. #include <linux/clk.h>
  30. #include <linux/clocksource.h>
  31. #include "soc.h"
  32. #include "common.h"
  33. #include "control.h"
  34. #include "omap-secure.h"
  35. #define REALTIME_COUNTER_BASE 0x48243200
  36. #define INCREMENTER_NUMERATOR_OFFSET 0x10
  37. #define INCREMENTER_DENUMERATOR_RELOAD_OFFSET 0x14
  38. #define NUMERATOR_DENUMERATOR_MASK 0xfffff000
  39. static unsigned long arch_timer_freq;
  40. void set_cntfreq(void)
  41. {
  42. omap_smc1(OMAP5_DRA7_MON_SET_CNTFRQ_INDEX, arch_timer_freq);
  43. }
  44. /*
  45. * The realtime counter also called master counter, is a free-running
  46. * counter, which is related to real time. It produces the count used
  47. * by the CPU local timer peripherals in the MPU cluster. The timer counts
  48. * at a rate of 6.144 MHz. Because the device operates on different clocks
  49. * in different power modes, the master counter shifts operation between
  50. * clocks, adjusting the increment per clock in hardware accordingly to
  51. * maintain a constant count rate.
  52. */
  53. static void __init realtime_counter_init(void)
  54. {
  55. void __iomem *base;
  56. static struct clk *sys_clk;
  57. unsigned long rate;
  58. unsigned int reg;
  59. unsigned long long num, den;
  60. base = ioremap(REALTIME_COUNTER_BASE, SZ_32);
  61. if (!base) {
  62. pr_err("%s: ioremap failed\n", __func__);
  63. return;
  64. }
  65. sys_clk = clk_get(NULL, "sys_clkin");
  66. if (IS_ERR(sys_clk)) {
  67. pr_err("%s: failed to get system clock handle\n", __func__);
  68. iounmap(base);
  69. return;
  70. }
  71. rate = clk_get_rate(sys_clk);
  72. clk_put(sys_clk);
  73. if (soc_is_dra7xx()) {
  74. /*
  75. * Errata i856 says the 32.768KHz crystal does not start at
  76. * power on, so the CPU falls back to an emulated 32KHz clock
  77. * based on sysclk / 610 instead. This causes the master counter
  78. * frequency to not be 6.144MHz but at sysclk / 610 * 375 / 2
  79. * (OR sysclk * 75 / 244)
  80. *
  81. * This affects at least the DRA7/AM572x 1.0, 1.1 revisions.
  82. * Of course any board built without a populated 32.768KHz
  83. * crystal would also need this fix even if the CPU is fixed
  84. * later.
  85. *
  86. * Either case can be detected by using the two speedselect bits
  87. * If they are not 0, then the 32.768KHz clock driving the
  88. * coarse counter that corrects the fine counter every time it
  89. * ticks is actually rate/610 rather than 32.768KHz and we
  90. * should compensate to avoid the 570ppm (at 20MHz, much worse
  91. * at other rates) too fast system time.
  92. */
  93. reg = omap_ctrl_readl(DRA7_CTRL_CORE_BOOTSTRAP);
  94. if (reg & DRA7_SPEEDSELECT_MASK) {
  95. num = 75;
  96. den = 244;
  97. goto sysclk1_based;
  98. }
  99. }
  100. /* Numerator/denumerator values refer TRM Realtime Counter section */
  101. switch (rate) {
  102. case 12000000:
  103. num = 64;
  104. den = 125;
  105. break;
  106. case 13000000:
  107. num = 768;
  108. den = 1625;
  109. break;
  110. case 19200000:
  111. num = 8;
  112. den = 25;
  113. break;
  114. case 20000000:
  115. num = 192;
  116. den = 625;
  117. break;
  118. case 26000000:
  119. num = 384;
  120. den = 1625;
  121. break;
  122. case 27000000:
  123. num = 256;
  124. den = 1125;
  125. break;
  126. case 38400000:
  127. default:
  128. /* Program it for 38.4 MHz */
  129. num = 4;
  130. den = 25;
  131. break;
  132. }
  133. sysclk1_based:
  134. /* Program numerator and denumerator registers */
  135. reg = readl_relaxed(base + INCREMENTER_NUMERATOR_OFFSET) &
  136. NUMERATOR_DENUMERATOR_MASK;
  137. reg |= num;
  138. writel_relaxed(reg, base + INCREMENTER_NUMERATOR_OFFSET);
  139. reg = readl_relaxed(base + INCREMENTER_DENUMERATOR_RELOAD_OFFSET) &
  140. NUMERATOR_DENUMERATOR_MASK;
  141. reg |= den;
  142. writel_relaxed(reg, base + INCREMENTER_DENUMERATOR_RELOAD_OFFSET);
  143. arch_timer_freq = DIV_ROUND_UP_ULL(rate * num, den);
  144. set_cntfreq();
  145. iounmap(base);
  146. }
  147. void __init omap5_realtime_timer_init(void)
  148. {
  149. omap_clk_init();
  150. realtime_counter_init();
  151. timer_probe();
  152. }