tegra20_timer.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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 inline void timer_shutdown(struct clock_event_device *evt)
  62. {
  63. timer_writel(0, TIMER3_BASE + TIMER_PTV);
  64. }
  65. static int tegra_timer_shutdown(struct clock_event_device *evt)
  66. {
  67. timer_shutdown(evt);
  68. return 0;
  69. }
  70. static int tegra_timer_set_periodic(struct clock_event_device *evt)
  71. {
  72. u32 reg = 0xC0000000 | ((1000000 / HZ) - 1);
  73. timer_shutdown(evt);
  74. timer_writel(reg, TIMER3_BASE + TIMER_PTV);
  75. return 0;
  76. }
  77. static struct clock_event_device tegra_clockevent = {
  78. .name = "timer0",
  79. .rating = 300,
  80. .features = CLOCK_EVT_FEAT_ONESHOT |
  81. CLOCK_EVT_FEAT_PERIODIC,
  82. .set_next_event = tegra_timer_set_next_event,
  83. .set_state_shutdown = tegra_timer_shutdown,
  84. .set_state_periodic = tegra_timer_set_periodic,
  85. .set_state_oneshot = tegra_timer_shutdown,
  86. .tick_resume = tegra_timer_shutdown,
  87. };
  88. static u64 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_clock64 - 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 timespec64.
  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_clock64(struct timespec64 *ts)
  115. {
  116. u64 delta;
  117. last_persistent_ms = persistent_ms;
  118. persistent_ms = tegra_rtc_read_ms();
  119. delta = persistent_ms - last_persistent_ms;
  120. timespec64_add_ns(&persistent_ts, delta * NSEC_PER_MSEC);
  121. *ts = persistent_ts;
  122. }
  123. static unsigned long tegra_delay_timer_read_counter_long(void)
  124. {
  125. return readl(timer_reg_base + TIMERUS_CNTR_1US);
  126. }
  127. static irqreturn_t tegra_timer_interrupt(int irq, void *dev_id)
  128. {
  129. struct clock_event_device *evt = (struct clock_event_device *)dev_id;
  130. timer_writel(1<<30, TIMER3_BASE + TIMER_PCR);
  131. evt->event_handler(evt);
  132. return IRQ_HANDLED;
  133. }
  134. static struct irqaction tegra_timer_irq = {
  135. .name = "timer0",
  136. .flags = IRQF_TIMER | IRQF_TRIGGER_HIGH,
  137. .handler = tegra_timer_interrupt,
  138. .dev_id = &tegra_clockevent,
  139. };
  140. static void __init tegra20_init_timer(struct device_node *np)
  141. {
  142. struct clk *clk;
  143. unsigned long rate;
  144. int ret;
  145. timer_reg_base = of_iomap(np, 0);
  146. if (!timer_reg_base) {
  147. pr_err("Can't map timer registers\n");
  148. BUG();
  149. }
  150. tegra_timer_irq.irq = irq_of_parse_and_map(np, 2);
  151. if (tegra_timer_irq.irq <= 0) {
  152. pr_err("Failed to map timer IRQ\n");
  153. BUG();
  154. }
  155. clk = of_clk_get(np, 0);
  156. if (IS_ERR(clk)) {
  157. pr_warn("Unable to get timer clock. Assuming 12Mhz input clock.\n");
  158. rate = 12000000;
  159. } else {
  160. clk_prepare_enable(clk);
  161. rate = clk_get_rate(clk);
  162. }
  163. switch (rate) {
  164. case 12000000:
  165. timer_writel(0x000b, TIMERUS_USEC_CFG);
  166. break;
  167. case 13000000:
  168. timer_writel(0x000c, TIMERUS_USEC_CFG);
  169. break;
  170. case 19200000:
  171. timer_writel(0x045f, TIMERUS_USEC_CFG);
  172. break;
  173. case 26000000:
  174. timer_writel(0x0019, TIMERUS_USEC_CFG);
  175. break;
  176. default:
  177. WARN(1, "Unknown clock rate");
  178. }
  179. sched_clock_register(tegra_read_sched_clock, 32, 1000000);
  180. if (clocksource_mmio_init(timer_reg_base + TIMERUS_CNTR_1US,
  181. "timer_us", 1000000, 300, 32, clocksource_mmio_readl_up)) {
  182. pr_err("Failed to register clocksource\n");
  183. BUG();
  184. }
  185. tegra_delay_timer.read_current_timer =
  186. tegra_delay_timer_read_counter_long;
  187. tegra_delay_timer.freq = 1000000;
  188. register_current_timer_delay(&tegra_delay_timer);
  189. ret = setup_irq(tegra_timer_irq.irq, &tegra_timer_irq);
  190. if (ret) {
  191. pr_err("Failed to register timer IRQ: %d\n", ret);
  192. BUG();
  193. }
  194. tegra_clockevent.cpumask = cpu_all_mask;
  195. tegra_clockevent.irq = tegra_timer_irq.irq;
  196. clockevents_config_and_register(&tegra_clockevent, 1000000,
  197. 0x1, 0x1fffffff);
  198. }
  199. CLOCKSOURCE_OF_DECLARE(tegra20_timer, "nvidia,tegra20-timer", tegra20_init_timer);
  200. static void __init tegra20_init_rtc(struct device_node *np)
  201. {
  202. struct clk *clk;
  203. rtc_base = of_iomap(np, 0);
  204. if (!rtc_base) {
  205. pr_err("Can't map RTC registers");
  206. BUG();
  207. }
  208. /*
  209. * rtc registers are used by read_persistent_clock, keep the rtc clock
  210. * enabled
  211. */
  212. clk = of_clk_get(np, 0);
  213. if (IS_ERR(clk))
  214. pr_warn("Unable to get rtc-tegra clock\n");
  215. else
  216. clk_prepare_enable(clk);
  217. register_persistent_clock(NULL, tegra_read_persistent_clock64);
  218. }
  219. CLOCKSOURCE_OF_DECLARE(tegra20_rtc, "nvidia,tegra20-rtc", tegra20_init_rtc);
  220. #ifdef CONFIG_PM
  221. static u32 usec_config;
  222. void tegra_timer_suspend(void)
  223. {
  224. usec_config = timer_readl(TIMERUS_USEC_CFG);
  225. }
  226. void tegra_timer_resume(void)
  227. {
  228. timer_writel(usec_config, TIMERUS_USEC_CFG);
  229. }
  230. #endif