timer.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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/mach/time.h>
  30. #include <asm/localtimer.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 cnt32_to_63(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 = 0x7FFFFFFFFFFFFFFFULL,
  92. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  93. };
  94. unsigned long long sched_clock(void)
  95. {
  96. return clocksource_cyc2ns(tegra_clocksource.read(&tegra_clocksource),
  97. tegra_clocksource.mult, tegra_clocksource.shift);
  98. }
  99. static irqreturn_t tegra_timer_interrupt(int irq, void *dev_id)
  100. {
  101. struct clock_event_device *evt = (struct clock_event_device *)dev_id;
  102. timer_writel(1<<30, TIMER3_BASE + TIMER_PCR);
  103. evt->event_handler(evt);
  104. return IRQ_HANDLED;
  105. }
  106. static struct irqaction tegra_timer_irq = {
  107. .name = "timer0",
  108. .flags = IRQF_DISABLED | IRQF_TIMER | IRQF_TRIGGER_HIGH,
  109. .handler = tegra_timer_interrupt,
  110. .dev_id = &tegra_clockevent,
  111. .irq = INT_TMR3,
  112. };
  113. static void __init tegra_init_timer(void)
  114. {
  115. unsigned long rate = clk_measure_input_freq();
  116. int ret;
  117. #ifdef CONFIG_HAVE_ARM_TWD
  118. twd_base = IO_ADDRESS(TEGRA_ARM_PERIF_BASE + 0x600);
  119. #endif
  120. switch (rate) {
  121. case 12000000:
  122. timer_writel(0x000b, TIMERUS_USEC_CFG);
  123. break;
  124. case 13000000:
  125. timer_writel(0x000c, TIMERUS_USEC_CFG);
  126. break;
  127. case 19200000:
  128. timer_writel(0x045f, TIMERUS_USEC_CFG);
  129. break;
  130. case 26000000:
  131. timer_writel(0x0019, TIMERUS_USEC_CFG);
  132. break;
  133. default:
  134. WARN(1, "Unknown clock rate");
  135. }
  136. if (clocksource_register_hz(&tegra_clocksource, 1000000)) {
  137. printk(KERN_ERR "Failed to register clocksource\n");
  138. BUG();
  139. }
  140. ret = setup_irq(tegra_timer_irq.irq, &tegra_timer_irq);
  141. if (ret) {
  142. printk(KERN_ERR "Failed to register timer IRQ: %d\n", ret);
  143. BUG();
  144. }
  145. clockevents_calc_mult_shift(&tegra_clockevent, 1000000, 5);
  146. tegra_clockevent.max_delta_ns =
  147. clockevent_delta2ns(0x1fffffff, &tegra_clockevent);
  148. tegra_clockevent.min_delta_ns =
  149. clockevent_delta2ns(0x1, &tegra_clockevent);
  150. tegra_clockevent.cpumask = cpu_all_mask;
  151. tegra_clockevent.irq = tegra_timer_irq.irq;
  152. clockevents_register_device(&tegra_clockevent);
  153. return;
  154. }
  155. struct sys_timer tegra_timer = {
  156. .init = tegra_init_timer,
  157. };