time.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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. #include <linux/sched_clock.h>
  19. /*
  20. * Register definitions for the timers
  21. */
  22. #define TIMER1_BASE GEMINI_TIMER_BASE
  23. #define TIMER2_BASE (GEMINI_TIMER_BASE + 0x10)
  24. #define TIMER3_BASE (GEMINI_TIMER_BASE + 0x20)
  25. #define TIMER_COUNT(BASE) (IO_ADDRESS(BASE) + 0x00)
  26. #define TIMER_LOAD(BASE) (IO_ADDRESS(BASE) + 0x04)
  27. #define TIMER_MATCH1(BASE) (IO_ADDRESS(BASE) + 0x08)
  28. #define TIMER_MATCH2(BASE) (IO_ADDRESS(BASE) + 0x0C)
  29. #define TIMER_CR (IO_ADDRESS(GEMINI_TIMER_BASE) + 0x30)
  30. #define TIMER_INTR_STATE (IO_ADDRESS(GEMINI_TIMER_BASE) + 0x34)
  31. #define TIMER_INTR_MASK (IO_ADDRESS(GEMINI_TIMER_BASE) + 0x38)
  32. #define TIMER_1_CR_ENABLE (1 << 0)
  33. #define TIMER_1_CR_CLOCK (1 << 1)
  34. #define TIMER_1_CR_INT (1 << 2)
  35. #define TIMER_2_CR_ENABLE (1 << 3)
  36. #define TIMER_2_CR_CLOCK (1 << 4)
  37. #define TIMER_2_CR_INT (1 << 5)
  38. #define TIMER_3_CR_ENABLE (1 << 6)
  39. #define TIMER_3_CR_CLOCK (1 << 7)
  40. #define TIMER_3_CR_INT (1 << 8)
  41. #define TIMER_1_CR_UPDOWN (1 << 9)
  42. #define TIMER_2_CR_UPDOWN (1 << 10)
  43. #define TIMER_3_CR_UPDOWN (1 << 11)
  44. #define TIMER_DEFAULT_FLAGS (TIMER_1_CR_UPDOWN | \
  45. TIMER_3_CR_ENABLE | \
  46. TIMER_3_CR_UPDOWN)
  47. #define TIMER_1_INT_MATCH1 (1 << 0)
  48. #define TIMER_1_INT_MATCH2 (1 << 1)
  49. #define TIMER_1_INT_OVERFLOW (1 << 2)
  50. #define TIMER_2_INT_MATCH1 (1 << 3)
  51. #define TIMER_2_INT_MATCH2 (1 << 4)
  52. #define TIMER_2_INT_OVERFLOW (1 << 5)
  53. #define TIMER_3_INT_MATCH1 (1 << 6)
  54. #define TIMER_3_INT_MATCH2 (1 << 7)
  55. #define TIMER_3_INT_OVERFLOW (1 << 8)
  56. #define TIMER_INT_ALL_MASK 0x1ff
  57. static unsigned int tick_rate;
  58. static u64 notrace gemini_read_sched_clock(void)
  59. {
  60. return readl(TIMER_COUNT(TIMER3_BASE));
  61. }
  62. static int gemini_timer_set_next_event(unsigned long cycles,
  63. struct clock_event_device *evt)
  64. {
  65. u32 cr;
  66. /* Setup the match register */
  67. cr = readl(TIMER_COUNT(TIMER1_BASE));
  68. writel(cr + cycles, TIMER_MATCH1(TIMER1_BASE));
  69. if (readl(TIMER_COUNT(TIMER1_BASE)) - cr > cycles)
  70. return -ETIME;
  71. return 0;
  72. }
  73. static int gemini_timer_shutdown(struct clock_event_device *evt)
  74. {
  75. u32 cr;
  76. /*
  77. * Disable also for oneshot: the set_next() call will arm the timer
  78. * instead.
  79. */
  80. /* Stop timer and interrupt. */
  81. cr = readl(TIMER_CR);
  82. cr &= ~(TIMER_1_CR_ENABLE | TIMER_1_CR_INT);
  83. writel(cr, TIMER_CR);
  84. /* Setup counter start from 0 */
  85. writel(0, TIMER_COUNT(TIMER1_BASE));
  86. writel(0, TIMER_LOAD(TIMER1_BASE));
  87. /* enable interrupt */
  88. cr = readl(TIMER_INTR_MASK);
  89. cr &= ~(TIMER_1_INT_OVERFLOW | TIMER_1_INT_MATCH2);
  90. cr |= TIMER_1_INT_MATCH1;
  91. writel(cr, TIMER_INTR_MASK);
  92. /* start the timer */
  93. cr = readl(TIMER_CR);
  94. cr |= TIMER_1_CR_ENABLE;
  95. writel(cr, TIMER_CR);
  96. return 0;
  97. }
  98. static int gemini_timer_set_periodic(struct clock_event_device *evt)
  99. {
  100. u32 period = DIV_ROUND_CLOSEST(tick_rate, HZ);
  101. u32 cr;
  102. /* Stop timer and interrupt */
  103. cr = readl(TIMER_CR);
  104. cr &= ~(TIMER_1_CR_ENABLE | TIMER_1_CR_INT);
  105. writel(cr, TIMER_CR);
  106. /* Setup timer to fire at 1/HT intervals. */
  107. cr = 0xffffffff - (period - 1);
  108. writel(cr, TIMER_COUNT(TIMER1_BASE));
  109. writel(cr, TIMER_LOAD(TIMER1_BASE));
  110. /* enable interrupt on overflow */
  111. cr = readl(TIMER_INTR_MASK);
  112. cr &= ~(TIMER_1_INT_MATCH1 | TIMER_1_INT_MATCH2);
  113. cr |= TIMER_1_INT_OVERFLOW;
  114. writel(cr, TIMER_INTR_MASK);
  115. /* Start the timer */
  116. cr = readl(TIMER_CR);
  117. cr |= TIMER_1_CR_ENABLE;
  118. cr |= TIMER_1_CR_INT;
  119. writel(cr, TIMER_CR);
  120. return 0;
  121. }
  122. /* Use TIMER1 as clock event */
  123. static struct clock_event_device gemini_clockevent = {
  124. .name = "TIMER1",
  125. /* Reasonably fast and accurate clock event */
  126. .rating = 300,
  127. .shift = 32,
  128. .features = CLOCK_EVT_FEAT_PERIODIC |
  129. CLOCK_EVT_FEAT_ONESHOT,
  130. .set_next_event = gemini_timer_set_next_event,
  131. .set_state_shutdown = gemini_timer_shutdown,
  132. .set_state_periodic = gemini_timer_set_periodic,
  133. .set_state_oneshot = gemini_timer_shutdown,
  134. .tick_resume = gemini_timer_shutdown,
  135. };
  136. /*
  137. * IRQ handler for the timer
  138. */
  139. static irqreturn_t gemini_timer_interrupt(int irq, void *dev_id)
  140. {
  141. struct clock_event_device *evt = &gemini_clockevent;
  142. evt->event_handler(evt);
  143. return IRQ_HANDLED;
  144. }
  145. static struct irqaction gemini_timer_irq = {
  146. .name = "Gemini Timer Tick",
  147. .flags = IRQF_TIMER,
  148. .handler = gemini_timer_interrupt,
  149. };
  150. /*
  151. * Set up timer interrupt, and return the current time in seconds.
  152. */
  153. void __init gemini_timer_init(void)
  154. {
  155. u32 reg_v;
  156. reg_v = readl(IO_ADDRESS(GEMINI_GLOBAL_BASE + GLOBAL_STATUS));
  157. tick_rate = REG_TO_AHB_SPEED(reg_v) * 1000000;
  158. printk(KERN_INFO "Bus: %dMHz", tick_rate / 1000000);
  159. tick_rate /= 6; /* APB bus run AHB*(1/6) */
  160. switch(reg_v & CPU_AHB_RATIO_MASK) {
  161. case CPU_AHB_1_1:
  162. printk(KERN_CONT "(1/1)\n");
  163. break;
  164. case CPU_AHB_3_2:
  165. printk(KERN_CONT "(3/2)\n");
  166. break;
  167. case CPU_AHB_24_13:
  168. printk(KERN_CONT "(24/13)\n");
  169. break;
  170. case CPU_AHB_2_1:
  171. printk(KERN_CONT "(2/1)\n");
  172. break;
  173. }
  174. /*
  175. * Reset the interrupt mask and status
  176. */
  177. writel(TIMER_INT_ALL_MASK, TIMER_INTR_MASK);
  178. writel(0, TIMER_INTR_STATE);
  179. writel(TIMER_DEFAULT_FLAGS, TIMER_CR);
  180. /*
  181. * Setup free-running clocksource timer (interrupts
  182. * disabled.)
  183. */
  184. writel(0, TIMER_COUNT(TIMER3_BASE));
  185. writel(0, TIMER_LOAD(TIMER3_BASE));
  186. writel(0, TIMER_MATCH1(TIMER3_BASE));
  187. writel(0, TIMER_MATCH2(TIMER3_BASE));
  188. clocksource_mmio_init(TIMER_COUNT(TIMER3_BASE),
  189. "gemini_clocksource", tick_rate,
  190. 300, 32, clocksource_mmio_readl_up);
  191. sched_clock_register(gemini_read_sched_clock, 32, tick_rate);
  192. /*
  193. * Setup clockevent timer (interrupt-driven.)
  194. */
  195. writel(0, TIMER_COUNT(TIMER1_BASE));
  196. writel(0, TIMER_LOAD(TIMER1_BASE));
  197. writel(0, TIMER_MATCH1(TIMER1_BASE));
  198. writel(0, TIMER_MATCH2(TIMER1_BASE));
  199. setup_irq(IRQ_TIMER1, &gemini_timer_irq);
  200. gemini_clockevent.cpumask = cpumask_of(0);
  201. clockevents_config_and_register(&gemini_clockevent, tick_rate,
  202. 1, 0xffffffff);
  203. }