timer.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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/time.h>
  22. #include <linux/interrupt.h>
  23. #include <linux/irq.h>
  24. #include <linux/clockchips.h>
  25. #include <linux/clocksource.h>
  26. #include <linux/clk.h>
  27. #include <linux/io.h>
  28. #include <asm/mach/time.h>
  29. #include <asm/smp_twd.h>
  30. #include <asm/sched_clock.h>
  31. #include <mach/iomap.h>
  32. #include <mach/irqs.h>
  33. #include "board.h"
  34. #include "clock.h"
  35. #define RTC_SECONDS 0x08
  36. #define RTC_SHADOW_SECONDS 0x0c
  37. #define RTC_MILLISECONDS 0x10
  38. #define TIMERUS_CNTR_1US 0x10
  39. #define TIMERUS_USEC_CFG 0x14
  40. #define TIMERUS_CNTR_FREEZE 0x4c
  41. #define TIMER1_BASE 0x0
  42. #define TIMER2_BASE 0x8
  43. #define TIMER3_BASE 0x50
  44. #define TIMER4_BASE 0x58
  45. #define TIMER_PTV 0x0
  46. #define TIMER_PCR 0x4
  47. static void __iomem *timer_reg_base = IO_ADDRESS(TEGRA_TMR1_BASE);
  48. static void __iomem *rtc_base = IO_ADDRESS(TEGRA_RTC_BASE);
  49. static struct timespec persistent_ts;
  50. static u64 persistent_ms, last_persistent_ms;
  51. #define timer_writel(value, reg) \
  52. __raw_writel(value, timer_reg_base + (reg))
  53. #define timer_readl(reg) \
  54. __raw_readl(timer_reg_base + (reg))
  55. static int tegra_timer_set_next_event(unsigned long cycles,
  56. struct clock_event_device *evt)
  57. {
  58. u32 reg;
  59. reg = 0x80000000 | ((cycles > 1) ? (cycles-1) : 0);
  60. timer_writel(reg, TIMER3_BASE + TIMER_PTV);
  61. return 0;
  62. }
  63. static void tegra_timer_set_mode(enum clock_event_mode mode,
  64. struct clock_event_device *evt)
  65. {
  66. u32 reg;
  67. timer_writel(0, TIMER3_BASE + TIMER_PTV);
  68. switch (mode) {
  69. case CLOCK_EVT_MODE_PERIODIC:
  70. reg = 0xC0000000 | ((1000000/HZ)-1);
  71. timer_writel(reg, TIMER3_BASE + TIMER_PTV);
  72. break;
  73. case CLOCK_EVT_MODE_ONESHOT:
  74. break;
  75. case CLOCK_EVT_MODE_UNUSED:
  76. case CLOCK_EVT_MODE_SHUTDOWN:
  77. case CLOCK_EVT_MODE_RESUME:
  78. break;
  79. }
  80. }
  81. static struct clock_event_device tegra_clockevent = {
  82. .name = "timer0",
  83. .rating = 300,
  84. .features = CLOCK_EVT_FEAT_ONESHOT | CLOCK_EVT_FEAT_PERIODIC,
  85. .set_next_event = tegra_timer_set_next_event,
  86. .set_mode = tegra_timer_set_mode,
  87. };
  88. static u32 notrace tegra_read_sched_clock(void)
  89. {
  90. return timer_readl(TIMERUS_CNTR_1US);
  91. }
  92. /*
  93. * tegra_rtc_read - Reads the Tegra RTC registers
  94. * Care must be taken that this funciton is not called while the
  95. * tegra_rtc driver could be executing to avoid race conditions
  96. * on the RTC shadow register
  97. */
  98. static u64 tegra_rtc_read_ms(void)
  99. {
  100. u32 ms = readl(rtc_base + RTC_MILLISECONDS);
  101. u32 s = readl(rtc_base + RTC_SHADOW_SECONDS);
  102. return (u64)s * MSEC_PER_SEC + ms;
  103. }
  104. /*
  105. * tegra_read_persistent_clock - Return time from a persistent clock.
  106. *
  107. * Reads the time from a source which isn't disabled during PM, the
  108. * 32k sync timer. Convert the cycles elapsed since last read into
  109. * nsecs and adds to a monotonically increasing timespec.
  110. * Care must be taken that this funciton is not called while the
  111. * tegra_rtc driver could be executing to avoid race conditions
  112. * on the RTC shadow register
  113. */
  114. static void tegra_read_persistent_clock(struct timespec *ts)
  115. {
  116. u64 delta;
  117. struct timespec *tsp = &persistent_ts;
  118. last_persistent_ms = persistent_ms;
  119. persistent_ms = tegra_rtc_read_ms();
  120. delta = persistent_ms - last_persistent_ms;
  121. timespec_add_ns(tsp, delta * NSEC_PER_MSEC);
  122. *ts = *tsp;
  123. }
  124. static irqreturn_t tegra_timer_interrupt(int irq, void *dev_id)
  125. {
  126. struct clock_event_device *evt = (struct clock_event_device *)dev_id;
  127. timer_writel(1<<30, TIMER3_BASE + TIMER_PCR);
  128. evt->event_handler(evt);
  129. return IRQ_HANDLED;
  130. }
  131. static struct irqaction tegra_timer_irq = {
  132. .name = "timer0",
  133. .flags = IRQF_DISABLED | IRQF_TIMER | IRQF_TRIGGER_HIGH,
  134. .handler = tegra_timer_interrupt,
  135. .dev_id = &tegra_clockevent,
  136. .irq = INT_TMR3,
  137. };
  138. #ifdef CONFIG_HAVE_ARM_TWD
  139. static DEFINE_TWD_LOCAL_TIMER(twd_local_timer,
  140. TEGRA_ARM_PERIF_BASE + 0x600,
  141. IRQ_LOCALTIMER);
  142. static void __init tegra_twd_init(void)
  143. {
  144. int err = twd_local_timer_register(&twd_local_timer);
  145. if (err)
  146. pr_err("twd_local_timer_register failed %d\n", err);
  147. }
  148. #else
  149. #define tegra_twd_init() do {} while(0)
  150. #endif
  151. static void __init tegra_init_timer(void)
  152. {
  153. struct clk *clk;
  154. unsigned long rate;
  155. int ret;
  156. clk = clk_get_sys("timer", NULL);
  157. if (IS_ERR(clk)) {
  158. pr_warn("Unable to get timer clock."
  159. " Assuming 12Mhz input clock.\n");
  160. rate = 12000000;
  161. } else {
  162. clk_prepare_enable(clk);
  163. rate = clk_get_rate(clk);
  164. }
  165. /*
  166. * rtc registers are used by read_persistent_clock, keep the rtc clock
  167. * enabled
  168. */
  169. clk = clk_get_sys("rtc-tegra", NULL);
  170. if (IS_ERR(clk))
  171. pr_warn("Unable to get rtc-tegra clock\n");
  172. else
  173. clk_prepare_enable(clk);
  174. switch (rate) {
  175. case 12000000:
  176. timer_writel(0x000b, TIMERUS_USEC_CFG);
  177. break;
  178. case 13000000:
  179. timer_writel(0x000c, TIMERUS_USEC_CFG);
  180. break;
  181. case 19200000:
  182. timer_writel(0x045f, TIMERUS_USEC_CFG);
  183. break;
  184. case 26000000:
  185. timer_writel(0x0019, TIMERUS_USEC_CFG);
  186. break;
  187. default:
  188. WARN(1, "Unknown clock rate");
  189. }
  190. setup_sched_clock(tegra_read_sched_clock, 32, 1000000);
  191. if (clocksource_mmio_init(timer_reg_base + TIMERUS_CNTR_1US,
  192. "timer_us", 1000000, 300, 32, clocksource_mmio_readl_up)) {
  193. printk(KERN_ERR "Failed to register clocksource\n");
  194. BUG();
  195. }
  196. ret = setup_irq(tegra_timer_irq.irq, &tegra_timer_irq);
  197. if (ret) {
  198. printk(KERN_ERR "Failed to register timer IRQ: %d\n", ret);
  199. BUG();
  200. }
  201. clockevents_calc_mult_shift(&tegra_clockevent, 1000000, 5);
  202. tegra_clockevent.max_delta_ns =
  203. clockevent_delta2ns(0x1fffffff, &tegra_clockevent);
  204. tegra_clockevent.min_delta_ns =
  205. clockevent_delta2ns(0x1, &tegra_clockevent);
  206. tegra_clockevent.cpumask = cpu_all_mask;
  207. tegra_clockevent.irq = tegra_timer_irq.irq;
  208. clockevents_register_device(&tegra_clockevent);
  209. tegra_twd_init();
  210. register_persistent_clock(NULL, tegra_read_persistent_clock);
  211. }
  212. struct sys_timer tegra_timer = {
  213. .init = tegra_init_timer,
  214. };
  215. #ifdef CONFIG_PM
  216. static u32 usec_config;
  217. void tegra_timer_suspend(void)
  218. {
  219. usec_config = timer_readl(TIMERUS_USEC_CFG);
  220. }
  221. void tegra_timer_resume(void)
  222. {
  223. timer_writel(usec_config, TIMERUS_USEC_CFG);
  224. }
  225. #endif