timer.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /*
  2. * arch/arch/mach-tegra/timer.c
  3. *
  4. * Copyright (C) 2010 Google, Inc.
  5. *
  6. * Author:
  7. * Colin Cross <ccross@google.com>
  8. *
  9. * This software is licensed under the terms of the GNU General Public
  10. * License version 2, as published by the Free Software Foundation, and
  11. * may be copied, distributed, and modified under those terms.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. */
  19. #include <linux/init.h>
  20. #include <linux/sched.h>
  21. #include <linux/time.h>
  22. #include <linux/interrupt.h>
  23. #include <linux/irq.h>
  24. #include <linux/clockchips.h>
  25. #include <linux/clocksource.h>
  26. #include <linux/clk.h>
  27. #include <linux/io.h>
  28. #include <asm/mach/time.h>
  29. #include <asm/localtimer.h>
  30. #include <asm/sched_clock.h>
  31. #include <mach/iomap.h>
  32. #include <mach/irqs.h>
  33. #include <mach/suspend.h>
  34. #include "board.h"
  35. #include "clock.h"
  36. #define TIMERUS_CNTR_1US 0x10
  37. #define TIMERUS_USEC_CFG 0x14
  38. #define TIMERUS_CNTR_FREEZE 0x4c
  39. #define TIMER1_BASE 0x0
  40. #define TIMER2_BASE 0x8
  41. #define TIMER3_BASE 0x50
  42. #define TIMER4_BASE 0x58
  43. #define TIMER_PTV 0x0
  44. #define TIMER_PCR 0x4
  45. struct tegra_timer;
  46. static void __iomem *timer_reg_base = IO_ADDRESS(TEGRA_TMR1_BASE);
  47. #define timer_writel(value, reg) \
  48. __raw_writel(value, (u32)timer_reg_base + (reg))
  49. #define timer_readl(reg) \
  50. __raw_readl((u32)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 cycle_t tegra_clocksource_read(struct clocksource *cs)
  78. {
  79. return timer_readl(TIMERUS_CNTR_1US);
  80. }
  81. static struct clock_event_device tegra_clockevent = {
  82. .name = "timer0",
  83. .rating = 300,
  84. .features = CLOCK_EVT_FEAT_ONESHOT | CLOCK_EVT_FEAT_PERIODIC,
  85. .set_next_event = tegra_timer_set_next_event,
  86. .set_mode = tegra_timer_set_mode,
  87. };
  88. static struct clocksource tegra_clocksource = {
  89. .name = "timer_us",
  90. .rating = 300,
  91. .read = tegra_clocksource_read,
  92. .mask = CLOCKSOURCE_MASK(32),
  93. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  94. };
  95. static DEFINE_CLOCK_DATA(cd);
  96. /*
  97. * Constants generated by clocks_calc_mult_shift(m, s, 1MHz, NSEC_PER_SEC, 60).
  98. * This gives a resolution of about 1us and a wrap period of about 1h11min.
  99. */
  100. #define SC_MULT 4194304000u
  101. #define SC_SHIFT 22
  102. unsigned long long notrace sched_clock(void)
  103. {
  104. u32 cyc = timer_readl(TIMERUS_CNTR_1US);
  105. return cyc_to_fixed_sched_clock(&cd, cyc, (u32)~0, SC_MULT, SC_SHIFT);
  106. }
  107. static void notrace tegra_update_sched_clock(void)
  108. {
  109. u32 cyc = timer_readl(TIMERUS_CNTR_1US);
  110. update_sched_clock(&cd, cyc, (u32)~0);
  111. }
  112. static irqreturn_t tegra_timer_interrupt(int irq, void *dev_id)
  113. {
  114. struct clock_event_device *evt = (struct clock_event_device *)dev_id;
  115. timer_writel(1<<30, TIMER3_BASE + TIMER_PCR);
  116. evt->event_handler(evt);
  117. return IRQ_HANDLED;
  118. }
  119. static struct irqaction tegra_timer_irq = {
  120. .name = "timer0",
  121. .flags = IRQF_DISABLED | IRQF_TIMER | IRQF_TRIGGER_HIGH,
  122. .handler = tegra_timer_interrupt,
  123. .dev_id = &tegra_clockevent,
  124. .irq = INT_TMR3,
  125. };
  126. static void __init tegra_init_timer(void)
  127. {
  128. unsigned long rate = clk_measure_input_freq();
  129. int ret;
  130. #ifdef CONFIG_HAVE_ARM_TWD
  131. twd_base = IO_ADDRESS(TEGRA_ARM_PERIF_BASE + 0x600);
  132. #endif
  133. switch (rate) {
  134. case 12000000:
  135. timer_writel(0x000b, TIMERUS_USEC_CFG);
  136. break;
  137. case 13000000:
  138. timer_writel(0x000c, TIMERUS_USEC_CFG);
  139. break;
  140. case 19200000:
  141. timer_writel(0x045f, TIMERUS_USEC_CFG);
  142. break;
  143. case 26000000:
  144. timer_writel(0x0019, TIMERUS_USEC_CFG);
  145. break;
  146. default:
  147. WARN(1, "Unknown clock rate");
  148. }
  149. init_fixed_sched_clock(&cd, tegra_update_sched_clock, 32,
  150. 1000000, SC_MULT, SC_SHIFT);
  151. if (clocksource_register_hz(&tegra_clocksource, 1000000)) {
  152. printk(KERN_ERR "Failed to register clocksource\n");
  153. BUG();
  154. }
  155. ret = setup_irq(tegra_timer_irq.irq, &tegra_timer_irq);
  156. if (ret) {
  157. printk(KERN_ERR "Failed to register timer IRQ: %d\n", ret);
  158. BUG();
  159. }
  160. clockevents_calc_mult_shift(&tegra_clockevent, 1000000, 5);
  161. tegra_clockevent.max_delta_ns =
  162. clockevent_delta2ns(0x1fffffff, &tegra_clockevent);
  163. tegra_clockevent.min_delta_ns =
  164. clockevent_delta2ns(0x1, &tegra_clockevent);
  165. tegra_clockevent.cpumask = cpu_all_mask;
  166. tegra_clockevent.irq = tegra_timer_irq.irq;
  167. clockevents_register_device(&tegra_clockevent);
  168. return;
  169. }
  170. struct sys_timer tegra_timer = {
  171. .init = tegra_init_timer,
  172. };