timer.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. /*
  2. * arch/arch/mach-tegra/timer.c
  3. *
  4. * Copyright (C) 2010 Google, Inc.
  5. *
  6. * Author:
  7. * Colin Cross <ccross@google.com>
  8. *
  9. * This software is licensed under the terms of the GNU General Public
  10. * License version 2, as published by the Free Software Foundation, and
  11. * may be copied, distributed, and modified under those terms.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. */
  19. #include <linux/init.h>
  20. #include <linux/err.h>
  21. #include <linux/sched.h>
  22. #include <linux/time.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/irq.h>
  25. #include <linux/clockchips.h>
  26. #include <linux/clocksource.h>
  27. #include <linux/clk.h>
  28. #include <linux/io.h>
  29. #include <asm/mach/time.h>
  30. #include <asm/localtimer.h>
  31. #include <asm/sched_clock.h>
  32. #include <mach/iomap.h>
  33. #include <mach/irqs.h>
  34. #include <mach/suspend.h>
  35. #include "board.h"
  36. #include "clock.h"
  37. #define RTC_SECONDS 0x08
  38. #define RTC_SHADOW_SECONDS 0x0c
  39. #define RTC_MILLISECONDS 0x10
  40. #define TIMERUS_CNTR_1US 0x10
  41. #define TIMERUS_USEC_CFG 0x14
  42. #define TIMERUS_CNTR_FREEZE 0x4c
  43. #define TIMER1_BASE 0x0
  44. #define TIMER2_BASE 0x8
  45. #define TIMER3_BASE 0x50
  46. #define TIMER4_BASE 0x58
  47. #define TIMER_PTV 0x0
  48. #define TIMER_PCR 0x4
  49. static void __iomem *timer_reg_base = IO_ADDRESS(TEGRA_TMR1_BASE);
  50. static void __iomem *rtc_base = IO_ADDRESS(TEGRA_RTC_BASE);
  51. static struct timespec persistent_ts;
  52. static u64 persistent_ms, last_persistent_ms;
  53. #define timer_writel(value, reg) \
  54. __raw_writel(value, (u32)timer_reg_base + (reg))
  55. #define timer_readl(reg) \
  56. __raw_readl((u32)timer_reg_base + (reg))
  57. static int tegra_timer_set_next_event(unsigned long cycles,
  58. struct clock_event_device *evt)
  59. {
  60. u32 reg;
  61. reg = 0x80000000 | ((cycles > 1) ? (cycles-1) : 0);
  62. timer_writel(reg, TIMER3_BASE + TIMER_PTV);
  63. return 0;
  64. }
  65. static void tegra_timer_set_mode(enum clock_event_mode mode,
  66. struct clock_event_device *evt)
  67. {
  68. u32 reg;
  69. timer_writel(0, TIMER3_BASE + TIMER_PTV);
  70. switch (mode) {
  71. case CLOCK_EVT_MODE_PERIODIC:
  72. reg = 0xC0000000 | ((1000000/HZ)-1);
  73. timer_writel(reg, TIMER3_BASE + TIMER_PTV);
  74. break;
  75. case CLOCK_EVT_MODE_ONESHOT:
  76. break;
  77. case CLOCK_EVT_MODE_UNUSED:
  78. case CLOCK_EVT_MODE_SHUTDOWN:
  79. case CLOCK_EVT_MODE_RESUME:
  80. break;
  81. }
  82. }
  83. static cycle_t tegra_clocksource_read(struct clocksource *cs)
  84. {
  85. return timer_readl(TIMERUS_CNTR_1US);
  86. }
  87. static struct clock_event_device tegra_clockevent = {
  88. .name = "timer0",
  89. .rating = 300,
  90. .features = CLOCK_EVT_FEAT_ONESHOT | CLOCK_EVT_FEAT_PERIODIC,
  91. .set_next_event = tegra_timer_set_next_event,
  92. .set_mode = tegra_timer_set_mode,
  93. };
  94. static struct clocksource tegra_clocksource = {
  95. .name = "timer_us",
  96. .rating = 300,
  97. .read = tegra_clocksource_read,
  98. .mask = CLOCKSOURCE_MASK(32),
  99. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  100. };
  101. static DEFINE_CLOCK_DATA(cd);
  102. /*
  103. * Constants generated by clocks_calc_mult_shift(m, s, 1MHz, NSEC_PER_SEC, 60).
  104. * This gives a resolution of about 1us and a wrap period of about 1h11min.
  105. */
  106. #define SC_MULT 4194304000u
  107. #define SC_SHIFT 22
  108. unsigned long long notrace sched_clock(void)
  109. {
  110. u32 cyc = timer_readl(TIMERUS_CNTR_1US);
  111. return cyc_to_fixed_sched_clock(&cd, cyc, (u32)~0, SC_MULT, SC_SHIFT);
  112. }
  113. static void notrace tegra_update_sched_clock(void)
  114. {
  115. u32 cyc = timer_readl(TIMERUS_CNTR_1US);
  116. update_sched_clock(&cd, cyc, (u32)~0);
  117. }
  118. /*
  119. * tegra_rtc_read - Reads the Tegra RTC registers
  120. * Care must be taken that this funciton is not called while the
  121. * tegra_rtc driver could be executing to avoid race conditions
  122. * on the RTC shadow register
  123. */
  124. u64 tegra_rtc_read_ms(void)
  125. {
  126. u32 ms = readl(rtc_base + RTC_MILLISECONDS);
  127. u32 s = readl(rtc_base + RTC_SHADOW_SECONDS);
  128. return (u64)s * MSEC_PER_SEC + ms;
  129. }
  130. /*
  131. * read_persistent_clock - Return time from a persistent clock.
  132. *
  133. * Reads the time from a source which isn't disabled during PM, the
  134. * 32k sync timer. Convert the cycles elapsed since last read into
  135. * nsecs and adds to a monotonically increasing timespec.
  136. * Care must be taken that this funciton is not called while the
  137. * tegra_rtc driver could be executing to avoid race conditions
  138. * on the RTC shadow register
  139. */
  140. void read_persistent_clock(struct timespec *ts)
  141. {
  142. u64 delta;
  143. struct timespec *tsp = &persistent_ts;
  144. last_persistent_ms = persistent_ms;
  145. persistent_ms = tegra_rtc_read_ms();
  146. delta = persistent_ms - last_persistent_ms;
  147. timespec_add_ns(tsp, delta * NSEC_PER_MSEC);
  148. *ts = *tsp;
  149. }
  150. static irqreturn_t tegra_timer_interrupt(int irq, void *dev_id)
  151. {
  152. struct clock_event_device *evt = (struct clock_event_device *)dev_id;
  153. timer_writel(1<<30, TIMER3_BASE + TIMER_PCR);
  154. evt->event_handler(evt);
  155. return IRQ_HANDLED;
  156. }
  157. static struct irqaction tegra_timer_irq = {
  158. .name = "timer0",
  159. .flags = IRQF_DISABLED | IRQF_TIMER | IRQF_TRIGGER_HIGH,
  160. .handler = tegra_timer_interrupt,
  161. .dev_id = &tegra_clockevent,
  162. .irq = INT_TMR3,
  163. };
  164. static void __init tegra_init_timer(void)
  165. {
  166. struct clk *clk;
  167. unsigned long rate = clk_measure_input_freq();
  168. int ret;
  169. clk = clk_get_sys("timer", NULL);
  170. BUG_ON(IS_ERR(clk));
  171. clk_enable(clk);
  172. /*
  173. * rtc registers are used by read_persistent_clock, keep the rtc clock
  174. * enabled
  175. */
  176. clk = clk_get_sys("rtc-tegra", NULL);
  177. BUG_ON(IS_ERR(clk));
  178. clk_enable(clk);
  179. #ifdef CONFIG_HAVE_ARM_TWD
  180. twd_base = IO_ADDRESS(TEGRA_ARM_PERIF_BASE + 0x600);
  181. #endif
  182. switch (rate) {
  183. case 12000000:
  184. timer_writel(0x000b, TIMERUS_USEC_CFG);
  185. break;
  186. case 13000000:
  187. timer_writel(0x000c, TIMERUS_USEC_CFG);
  188. break;
  189. case 19200000:
  190. timer_writel(0x045f, TIMERUS_USEC_CFG);
  191. break;
  192. case 26000000:
  193. timer_writel(0x0019, TIMERUS_USEC_CFG);
  194. break;
  195. default:
  196. WARN(1, "Unknown clock rate");
  197. }
  198. init_fixed_sched_clock(&cd, tegra_update_sched_clock, 32,
  199. 1000000, SC_MULT, SC_SHIFT);
  200. if (clocksource_register_hz(&tegra_clocksource, 1000000)) {
  201. printk(KERN_ERR "Failed to register clocksource\n");
  202. BUG();
  203. }
  204. ret = setup_irq(tegra_timer_irq.irq, &tegra_timer_irq);
  205. if (ret) {
  206. printk(KERN_ERR "Failed to register timer IRQ: %d\n", ret);
  207. BUG();
  208. }
  209. clockevents_calc_mult_shift(&tegra_clockevent, 1000000, 5);
  210. tegra_clockevent.max_delta_ns =
  211. clockevent_delta2ns(0x1fffffff, &tegra_clockevent);
  212. tegra_clockevent.min_delta_ns =
  213. clockevent_delta2ns(0x1, &tegra_clockevent);
  214. tegra_clockevent.cpumask = cpu_all_mask;
  215. tegra_clockevent.irq = tegra_timer_irq.irq;
  216. clockevents_register_device(&tegra_clockevent);
  217. }
  218. struct sys_timer tegra_timer = {
  219. .init = tegra_init_timer,
  220. };
  221. #ifdef CONFIG_PM
  222. static u32 usec_config;
  223. void tegra_timer_suspend(void)
  224. {
  225. usec_config = timer_readl(TIMERUS_USEC_CFG);
  226. }
  227. void tegra_timer_resume(void)
  228. {
  229. timer_writel(usec_config, TIMERUS_USEC_CFG);
  230. }
  231. #endif