timer.c 6.9 KB

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