time.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /*
  2. * Copyright (C) 2001-2006 Storlink, Corp.
  3. * Copyright (C) 2008-2009 Paulius Zaleckas <paulius.zaleckas@teltonika.lt>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. */
  10. #include <linux/interrupt.h>
  11. #include <linux/irq.h>
  12. #include <linux/io.h>
  13. #include <mach/hardware.h>
  14. #include <mach/global_reg.h>
  15. #include <asm/mach/time.h>
  16. #include <linux/clockchips.h>
  17. #include <linux/clocksource.h>
  18. /*
  19. * Register definitions for the timers
  20. */
  21. #define TIMER_COUNT(BASE_ADDR) (BASE_ADDR + 0x00)
  22. #define TIMER_LOAD(BASE_ADDR) (BASE_ADDR + 0x04)
  23. #define TIMER_MATCH1(BASE_ADDR) (BASE_ADDR + 0x08)
  24. #define TIMER_MATCH2(BASE_ADDR) (BASE_ADDR + 0x0C)
  25. #define TIMER_CR(BASE_ADDR) (BASE_ADDR + 0x30)
  26. #define TIMER_1_CR_ENABLE (1 << 0)
  27. #define TIMER_1_CR_CLOCK (1 << 1)
  28. #define TIMER_1_CR_INT (1 << 2)
  29. #define TIMER_2_CR_ENABLE (1 << 3)
  30. #define TIMER_2_CR_CLOCK (1 << 4)
  31. #define TIMER_2_CR_INT (1 << 5)
  32. #define TIMER_3_CR_ENABLE (1 << 6)
  33. #define TIMER_3_CR_CLOCK (1 << 7)
  34. #define TIMER_3_CR_INT (1 << 8)
  35. static unsigned int tick_rate;
  36. static int gemini_timer_set_next_event(unsigned long cycles,
  37. struct clock_event_device *evt)
  38. {
  39. u32 cr;
  40. cr = readl(TIMER_CR(IO_ADDRESS(GEMINI_TIMER_BASE)));
  41. /* This may be overdoing it, feel free to test without this */
  42. cr &= ~TIMER_2_CR_ENABLE;
  43. cr &= ~TIMER_2_CR_INT;
  44. writel(cr, TIMER_CR(IO_ADDRESS(GEMINI_TIMER_BASE)));
  45. /* Set next event */
  46. writel(cycles, TIMER_COUNT(IO_ADDRESS(GEMINI_TIMER2_BASE)));
  47. writel(cycles, TIMER_LOAD(IO_ADDRESS(GEMINI_TIMER2_BASE)));
  48. cr |= TIMER_2_CR_ENABLE;
  49. cr |= TIMER_2_CR_INT;
  50. writel(cr, TIMER_CR(IO_ADDRESS(GEMINI_TIMER_BASE)));
  51. return 0;
  52. }
  53. static void gemini_timer_set_mode(enum clock_event_mode mode,
  54. struct clock_event_device *evt)
  55. {
  56. u32 period = DIV_ROUND_CLOSEST(tick_rate, HZ);
  57. u32 cr;
  58. switch (mode) {
  59. case CLOCK_EVT_MODE_PERIODIC:
  60. /* Start the timer */
  61. writel(period,
  62. TIMER_COUNT(IO_ADDRESS(GEMINI_TIMER2_BASE)));
  63. writel(period,
  64. TIMER_LOAD(IO_ADDRESS(GEMINI_TIMER2_BASE)));
  65. cr = readl(TIMER_CR(IO_ADDRESS(GEMINI_TIMER_BASE)));
  66. cr |= TIMER_2_CR_ENABLE;
  67. cr |= TIMER_2_CR_INT;
  68. writel(cr, TIMER_CR(IO_ADDRESS(GEMINI_TIMER_BASE)));
  69. break;
  70. case CLOCK_EVT_MODE_ONESHOT:
  71. case CLOCK_EVT_MODE_UNUSED:
  72. case CLOCK_EVT_MODE_SHUTDOWN:
  73. case CLOCK_EVT_MODE_RESUME:
  74. /*
  75. * Disable also for oneshot: the set_next() call will
  76. * arm the timer instead.
  77. */
  78. cr = readl(TIMER_CR(IO_ADDRESS(GEMINI_TIMER_BASE)));
  79. cr &= ~TIMER_2_CR_ENABLE;
  80. cr &= ~TIMER_2_CR_INT;
  81. writel(cr, TIMER_CR(IO_ADDRESS(GEMINI_TIMER_BASE)));
  82. break;
  83. default:
  84. break;
  85. }
  86. }
  87. /* Use TIMER2 as clock event */
  88. static struct clock_event_device gemini_clockevent = {
  89. .name = "TIMER2",
  90. .rating = 300, /* Reasonably fast and accurate clock event */
  91. .features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT,
  92. .set_next_event = gemini_timer_set_next_event,
  93. .set_mode = gemini_timer_set_mode,
  94. };
  95. /*
  96. * IRQ handler for the timer
  97. */
  98. static irqreturn_t gemini_timer_interrupt(int irq, void *dev_id)
  99. {
  100. struct clock_event_device *evt = &gemini_clockevent;
  101. evt->event_handler(evt);
  102. return IRQ_HANDLED;
  103. }
  104. static struct irqaction gemini_timer_irq = {
  105. .name = "Gemini Timer Tick",
  106. .flags = IRQF_TIMER,
  107. .handler = gemini_timer_interrupt,
  108. };
  109. /*
  110. * Set up timer interrupt, and return the current time in seconds.
  111. */
  112. void __init gemini_timer_init(void)
  113. {
  114. u32 reg_v;
  115. reg_v = readl(IO_ADDRESS(GEMINI_GLOBAL_BASE + GLOBAL_STATUS));
  116. tick_rate = REG_TO_AHB_SPEED(reg_v) * 1000000;
  117. printk(KERN_INFO "Bus: %dMHz", tick_rate / 1000000);
  118. tick_rate /= 6; /* APB bus run AHB*(1/6) */
  119. switch(reg_v & CPU_AHB_RATIO_MASK) {
  120. case CPU_AHB_1_1:
  121. printk(KERN_CONT "(1/1)\n");
  122. break;
  123. case CPU_AHB_3_2:
  124. printk(KERN_CONT "(3/2)\n");
  125. break;
  126. case CPU_AHB_24_13:
  127. printk(KERN_CONT "(24/13)\n");
  128. break;
  129. case CPU_AHB_2_1:
  130. printk(KERN_CONT "(2/1)\n");
  131. break;
  132. }
  133. /*
  134. * Make irqs happen for the system timer
  135. */
  136. setup_irq(IRQ_TIMER2, &gemini_timer_irq);
  137. /* Enable and use TIMER1 as clock source */
  138. writel(0xffffffff, TIMER_COUNT(IO_ADDRESS(GEMINI_TIMER1_BASE)));
  139. writel(0xffffffff, TIMER_LOAD(IO_ADDRESS(GEMINI_TIMER1_BASE)));
  140. writel(TIMER_1_CR_ENABLE, TIMER_CR(IO_ADDRESS(GEMINI_TIMER_BASE)));
  141. if (clocksource_mmio_init(TIMER_COUNT(IO_ADDRESS(GEMINI_TIMER1_BASE)),
  142. "TIMER1", tick_rate, 300, 32,
  143. clocksource_mmio_readl_up))
  144. pr_err("timer: failed to initialize gemini clock source\n");
  145. /* Configure and register the clockevent */
  146. clockevents_config_and_register(&gemini_clockevent, tick_rate,
  147. 1, 0xffffffff);
  148. }