tegra20_timer.c 6.7 KB

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