tegra20_timer.c 6.6 KB

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