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