tegra20_timer.c 6.6 KB

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