timer.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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 "board.h"
  34. #include "clock.h"
  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. struct tegra_timer;
  45. static void __iomem *timer_reg_base = IO_ADDRESS(TEGRA_TMR1_BASE);
  46. #define timer_writel(value, reg) \
  47. __raw_writel(value, (u32)timer_reg_base + (reg))
  48. #define timer_readl(reg) \
  49. __raw_readl((u32)timer_reg_base + (reg))
  50. static int tegra_timer_set_next_event(unsigned long cycles,
  51. struct clock_event_device *evt)
  52. {
  53. u32 reg;
  54. reg = 0x80000000 | ((cycles > 1) ? (cycles-1) : 0);
  55. timer_writel(reg, TIMER3_BASE + TIMER_PTV);
  56. return 0;
  57. }
  58. static void tegra_timer_set_mode(enum clock_event_mode mode,
  59. struct clock_event_device *evt)
  60. {
  61. u32 reg;
  62. timer_writel(0, TIMER3_BASE + TIMER_PTV);
  63. switch (mode) {
  64. case CLOCK_EVT_MODE_PERIODIC:
  65. reg = 0xC0000000 | ((1000000/HZ)-1);
  66. timer_writel(reg, TIMER3_BASE + TIMER_PTV);
  67. break;
  68. case CLOCK_EVT_MODE_ONESHOT:
  69. break;
  70. case CLOCK_EVT_MODE_UNUSED:
  71. case CLOCK_EVT_MODE_SHUTDOWN:
  72. case CLOCK_EVT_MODE_RESUME:
  73. break;
  74. }
  75. }
  76. static cycle_t tegra_clocksource_read(struct clocksource *cs)
  77. {
  78. return timer_readl(TIMERUS_CNTR_1US);
  79. }
  80. static struct clock_event_device tegra_clockevent = {
  81. .name = "timer0",
  82. .rating = 300,
  83. .features = CLOCK_EVT_FEAT_ONESHOT | CLOCK_EVT_FEAT_PERIODIC,
  84. .set_next_event = tegra_timer_set_next_event,
  85. .set_mode = tegra_timer_set_mode,
  86. };
  87. static struct clocksource tegra_clocksource = {
  88. .name = "timer_us",
  89. .rating = 300,
  90. .read = tegra_clocksource_read,
  91. .mask = CLOCKSOURCE_MASK(32),
  92. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  93. };
  94. static DEFINE_CLOCK_DATA(cd);
  95. /*
  96. * Constants generated by clocks_calc_mult_shift(m, s, 1MHz, NSEC_PER_SEC, 60).
  97. * This gives a resolution of about 1us and a wrap period of about 1h11min.
  98. */
  99. #define SC_MULT 4194304000u
  100. #define SC_SHIFT 22
  101. unsigned long long notrace sched_clock(void)
  102. {
  103. u32 cyc = timer_readl(TIMERUS_CNTR_1US);
  104. return cyc_to_fixed_sched_clock(&cd, cyc, (u32)~0, SC_MULT, SC_SHIFT);
  105. }
  106. static void notrace tegra_update_sched_clock(void)
  107. {
  108. u32 cyc = timer_readl(TIMERUS_CNTR_1US);
  109. update_sched_clock(&cd, cyc, (u32)~0);
  110. }
  111. static irqreturn_t tegra_timer_interrupt(int irq, void *dev_id)
  112. {
  113. struct clock_event_device *evt = (struct clock_event_device *)dev_id;
  114. timer_writel(1<<30, TIMER3_BASE + TIMER_PCR);
  115. evt->event_handler(evt);
  116. return IRQ_HANDLED;
  117. }
  118. static struct irqaction tegra_timer_irq = {
  119. .name = "timer0",
  120. .flags = IRQF_DISABLED | IRQF_TIMER | IRQF_TRIGGER_HIGH,
  121. .handler = tegra_timer_interrupt,
  122. .dev_id = &tegra_clockevent,
  123. .irq = INT_TMR3,
  124. };
  125. static void __init tegra_init_timer(void)
  126. {
  127. unsigned long rate = clk_measure_input_freq();
  128. int ret;
  129. #ifdef CONFIG_HAVE_ARM_TWD
  130. twd_base = IO_ADDRESS(TEGRA_ARM_PERIF_BASE + 0x600);
  131. #endif
  132. switch (rate) {
  133. case 12000000:
  134. timer_writel(0x000b, TIMERUS_USEC_CFG);
  135. break;
  136. case 13000000:
  137. timer_writel(0x000c, TIMERUS_USEC_CFG);
  138. break;
  139. case 19200000:
  140. timer_writel(0x045f, TIMERUS_USEC_CFG);
  141. break;
  142. case 26000000:
  143. timer_writel(0x0019, TIMERUS_USEC_CFG);
  144. break;
  145. default:
  146. WARN(1, "Unknown clock rate");
  147. }
  148. init_fixed_sched_clock(&cd, tegra_update_sched_clock, 32,
  149. 1000000, SC_MULT, SC_SHIFT);
  150. if (clocksource_register_hz(&tegra_clocksource, 1000000)) {
  151. printk(KERN_ERR "Failed to register clocksource\n");
  152. BUG();
  153. }
  154. ret = setup_irq(tegra_timer_irq.irq, &tegra_timer_irq);
  155. if (ret) {
  156. printk(KERN_ERR "Failed to register timer IRQ: %d\n", ret);
  157. BUG();
  158. }
  159. clockevents_calc_mult_shift(&tegra_clockevent, 1000000, 5);
  160. tegra_clockevent.max_delta_ns =
  161. clockevent_delta2ns(0x1fffffff, &tegra_clockevent);
  162. tegra_clockevent.min_delta_ns =
  163. clockevent_delta2ns(0x1, &tegra_clockevent);
  164. tegra_clockevent.cpumask = cpu_all_mask;
  165. tegra_clockevent.irq = tegra_timer_irq.irq;
  166. clockevents_register_device(&tegra_clockevent);
  167. return;
  168. }
  169. struct sys_timer tegra_timer = {
  170. .init = tegra_init_timer,
  171. };