timer.c 6.1 KB

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