timer.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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/time.h>
  21. #include <linux/interrupt.h>
  22. #include <linux/irq.h>
  23. #include <linux/clockchips.h>
  24. #include <linux/clocksource.h>
  25. #include <linux/clk.h>
  26. #include <linux/io.h>
  27. #include <linux/cnt32_to_63.h>
  28. #include <asm/mach/time.h>
  29. #include <asm/localtimer.h>
  30. #include <mach/iomap.h>
  31. #include <mach/irqs.h>
  32. #include "board.h"
  33. #include "clock.h"
  34. #define TIMERUS_CNTR_1US 0x10
  35. #define TIMERUS_USEC_CFG 0x14
  36. #define TIMERUS_CNTR_FREEZE 0x4c
  37. #define TIMER1_BASE 0x0
  38. #define TIMER2_BASE 0x8
  39. #define TIMER3_BASE 0x50
  40. #define TIMER4_BASE 0x58
  41. #define TIMER_PTV 0x0
  42. #define TIMER_PCR 0x4
  43. struct tegra_timer;
  44. static void __iomem *timer_reg_base = IO_ADDRESS(TEGRA_TMR1_BASE);
  45. #define timer_writel(value, reg) \
  46. __raw_writel(value, (u32)timer_reg_base + (reg))
  47. #define timer_readl(reg) \
  48. __raw_readl((u32)timer_reg_base + (reg))
  49. static int tegra_timer_set_next_event(unsigned long cycles,
  50. struct clock_event_device *evt)
  51. {
  52. u32 reg;
  53. reg = 0x80000000 | ((cycles > 1) ? (cycles-1) : 0);
  54. timer_writel(reg, TIMER3_BASE + TIMER_PTV);
  55. return 0;
  56. }
  57. static void tegra_timer_set_mode(enum clock_event_mode mode,
  58. struct clock_event_device *evt)
  59. {
  60. u32 reg;
  61. timer_writel(0, TIMER3_BASE + TIMER_PTV);
  62. switch (mode) {
  63. case CLOCK_EVT_MODE_PERIODIC:
  64. reg = 0xC0000000 | ((1000000/HZ)-1);
  65. timer_writel(reg, TIMER3_BASE + TIMER_PTV);
  66. break;
  67. case CLOCK_EVT_MODE_ONESHOT:
  68. break;
  69. case CLOCK_EVT_MODE_UNUSED:
  70. case CLOCK_EVT_MODE_SHUTDOWN:
  71. case CLOCK_EVT_MODE_RESUME:
  72. break;
  73. }
  74. }
  75. static cycle_t tegra_clocksource_read(struct clocksource *cs)
  76. {
  77. return cnt32_to_63(timer_readl(TIMERUS_CNTR_1US));
  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 struct clocksource tegra_clocksource = {
  87. .name = "timer_us",
  88. .rating = 300,
  89. .read = tegra_clocksource_read,
  90. .mask = 0x7FFFFFFFFFFFFFFFULL,
  91. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  92. };
  93. unsigned long long sched_clock(void)
  94. {
  95. return clocksource_cyc2ns(tegra_clocksource.read(&tegra_clocksource),
  96. tegra_clocksource.mult, tegra_clocksource.shift);
  97. }
  98. static irqreturn_t tegra_timer_interrupt(int irq, void *dev_id)
  99. {
  100. struct clock_event_device *evt = (struct clock_event_device *)dev_id;
  101. timer_writel(1<<30, TIMER3_BASE + TIMER_PCR);
  102. evt->event_handler(evt);
  103. return IRQ_HANDLED;
  104. }
  105. static struct irqaction tegra_timer_irq = {
  106. .name = "timer0",
  107. .flags = IRQF_DISABLED | IRQF_TIMER | IRQF_TRIGGER_HIGH,
  108. .handler = tegra_timer_interrupt,
  109. .dev_id = &tegra_clockevent,
  110. .irq = INT_TMR3,
  111. };
  112. static void __init tegra_init_timer(void)
  113. {
  114. unsigned long rate = clk_measure_input_freq();
  115. int ret;
  116. #ifdef CONFIG_HAVE_ARM_TWD
  117. twd_base = IO_ADDRESS(TEGRA_ARM_PERIF_BASE + 0x600);
  118. #endif
  119. switch (rate) {
  120. case 12000000:
  121. timer_writel(0x000b, TIMERUS_USEC_CFG);
  122. break;
  123. case 13000000:
  124. timer_writel(0x000c, TIMERUS_USEC_CFG);
  125. break;
  126. case 19200000:
  127. timer_writel(0x045f, TIMERUS_USEC_CFG);
  128. break;
  129. case 26000000:
  130. timer_writel(0x0019, TIMERUS_USEC_CFG);
  131. break;
  132. default:
  133. WARN(1, "Unknown clock rate");
  134. }
  135. if (clocksource_register_hz(&tegra_clocksource, 1000000)) {
  136. printk(KERN_ERR "Failed to register clocksource\n");
  137. BUG();
  138. }
  139. ret = setup_irq(tegra_timer_irq.irq, &tegra_timer_irq);
  140. if (ret) {
  141. printk(KERN_ERR "Failed to register timer IRQ: %d\n", ret);
  142. BUG();
  143. }
  144. clockevents_calc_mult_shift(&tegra_clockevent, 1000000, 5);
  145. tegra_clockevent.max_delta_ns =
  146. clockevent_delta2ns(0x1fffffff, &tegra_clockevent);
  147. tegra_clockevent.min_delta_ns =
  148. clockevent_delta2ns(0x1, &tegra_clockevent);
  149. tegra_clockevent.cpumask = cpu_all_mask;
  150. tegra_clockevent.irq = tegra_timer_irq.irq;
  151. clockevents_register_device(&tegra_clockevent);
  152. return;
  153. }
  154. struct sys_timer tegra_timer = {
  155. .init = tegra_init_timer,
  156. };