timer.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. /*
  2. * arch/arm/mach-meson1/timer.c
  3. *
  4. * Copyright (c) 2007-2011, Amlogic, Inc.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19. *
  20. */
  21. #include <linux/kernel.h>
  22. #include <linux/init.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/irq.h>
  25. #include <linux/io.h>
  26. #include <linux/mm.h>
  27. #include <linux/clockchips.h>
  28. #include <linux/clocksource.h>
  29. #include <mach/hardware.h>
  30. #include <asm/memory.h>
  31. #include <asm/mach/map.h>
  32. #include <asm/mach/time.h>
  33. #include <asm/mach/irq.h>
  34. /* --------------------------------------------------------------------------*/
  35. /**
  36. * @brief cycle_read_timerE
  37. *
  38. * @param cs
  39. *
  40. * @return
  41. */
  42. /* --------------------------------------------------------------------------*/
  43. static cycle_t cycle_read_timerE(struct clocksource *cs)
  44. {
  45. return (cycles_t) READ_CBUS_REG(ISA_TIMERE);
  46. }
  47. static struct clocksource clocksource_timer_e = {
  48. .name = "Timer-E",
  49. .rating = 300,
  50. .read = cycle_read_timerE,
  51. .mask = CLOCKSOURCE_MASK(32),
  52. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  53. };
  54. /* --------------------------------------------------------------------------*/
  55. /**
  56. * @brief meson_clocksource_init
  57. *
  58. * @return
  59. */
  60. /* --------------------------------------------------------------------------*/
  61. static void __init meson_clocksource_init(void)
  62. {
  63. CLEAR_CBUS_REG_MASK(ISA_TIMER_MUX, TIMER_E_INPUT_MASK);
  64. SET_CBUS_REG_MASK(ISA_TIMER_MUX, TIMERE_UNIT_1ms << TIMER_E_INPUT_BIT);
  65. WRITE_CBUS_REG(ISA_TIMERE, 0);
  66. clocksource_timer_e.shift = clocksource_hz2shift(32, 1000);
  67. clocksource_timer_e.mult =
  68. clocksource_khz2mult(1, clocksource_timer_e.shift);
  69. clocksource_register(&clocksource_timer_e);
  70. }
  71. /* --------------------------------------------------------------------------*/
  72. /**
  73. * @brief sched_clock
  74. *
  75. * @return
  76. */
  77. /* --------------------------------------------------------------------------*/
  78. unsigned long long sched_clock(void)
  79. {
  80. cycle_t cyc = cycle_read_timerE(NULL);
  81. struct clocksource *cs = &clocksource_timer_e;
  82. return clocksource_cyc2ns(cyc, cs->mult, cs->shift);
  83. }
  84. /* --------------------------------------------------------------------------*/
  85. /**
  86. * @brief meson_clkevt_set_mode
  87. *
  88. * @param mode
  89. * @param dev
  90. */
  91. /* --------------------------------------------------------------------------*/
  92. static void meson_clkevt_set_mode(enum clock_event_mode mode,
  93. struct clock_event_device *dev)
  94. {
  95. switch (mode) {
  96. case CLOCK_EVT_MODE_RESUME:
  97. /* FIXME:
  98. * CLOCK_EVT_MODE_RESUME is always followed by
  99. * CLOCK_EVT_MODE_PERIODIC or CLOCK_EVT_MODE_ONESHOT.
  100. * do nothing here.
  101. */
  102. break;
  103. case CLOCK_EVT_MODE_PERIODIC:
  104. //Enable later
  105. //meson_mask_irq(INT_TIMER_C);
  106. //meson_unmask_irq(INT_TIMER_A);
  107. break;
  108. case CLOCK_EVT_MODE_ONESHOT:
  109. //Enable later
  110. //meson_mask_irq(INT_TIMER_A);
  111. break;
  112. case CLOCK_EVT_MODE_SHUTDOWN:
  113. case CLOCK_EVT_MODE_UNUSED:
  114. /* there is no way to actually pause or stop TIMERA/C,
  115. * so just disable TIMER interrupt.
  116. */
  117. //Enable later
  118. //meson_mask_irq(INT_TIMER_A);
  119. //meson_mask_irq(INT_TIMER_C);
  120. break;
  121. }
  122. }
  123. /* --------------------------------------------------------------------------*/
  124. /**
  125. * @brief meson_set_next_event
  126. *
  127. * @param evt
  128. * @param unused
  129. *
  130. * @return
  131. */
  132. /* --------------------------------------------------------------------------*/
  133. static int meson_set_next_event(unsigned long evt,
  134. struct clock_event_device *unused)
  135. {
  136. #if 0
  137. //Enable later
  138. meson_mask_irq(INT_TIMER_C);
  139. /* use a big number to clear previous trigger cleanly */
  140. SET_CBUS_REG_MASK(ISA_TIMERC, evt & 0xffff);
  141. //meson_ack_irq(INT_TIMER_C);
  142. /* then set next event */
  143. WRITE_CBUS_REG_BITS(ISA_TIMERC, evt, 0, 16);
  144. meson_unmask_irq(INT_TIMER_C);
  145. #endif
  146. return 0;
  147. }
  148. static struct clock_event_device clockevent_meson_1mhz = {
  149. .name = "TIMER-AC",
  150. .rating = 300, /* Reasonably fast and accurate clock event */
  151. .features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT,
  152. .shift = 20,
  153. .set_next_event = meson_set_next_event,
  154. .set_mode = meson_clkevt_set_mode,
  155. };
  156. /* --------------------------------------------------------------------------*/
  157. /**
  158. * @brief meson_timer_interrupt
  159. *
  160. * @param irq
  161. * @param dev_id
  162. *
  163. * @return
  164. */
  165. /* --------------------------------------------------------------------------*/
  166. static irqreturn_t meson_timer_interrupt(int irq, void *dev_id)
  167. {
  168. struct clock_event_device *evt = &clockevent_meson_1mhz;
  169. //Enable later
  170. //meson_ack_irq(irq);
  171. evt->event_handler(evt);
  172. return IRQ_HANDLED;
  173. }
  174. static struct irqaction meson_timer_irq = {
  175. .name = "Meson Timer Tick",
  176. .flags = IRQF_DISABLED | IRQF_TIMER | IRQF_IRQPOLL,
  177. .handler = meson_timer_interrupt,
  178. };
  179. /* --------------------------------------------------------------------------*/
  180. /**
  181. * @brief meson_clockevent_init
  182. *
  183. * @return
  184. */
  185. /* --------------------------------------------------------------------------*/
  186. static void __init meson_clockevent_init(void)
  187. {
  188. CLEAR_CBUS_REG_MASK(ISA_TIMER_MUX, TIMER_A_INPUT_MASK | TIMER_C_INPUT_MASK);
  189. SET_CBUS_REG_MASK(ISA_TIMER_MUX,
  190. (TIMER_UNIT_1us << TIMER_A_INPUT_BIT) |
  191. (TIMER_UNIT_1us << TIMER_C_INPUT_BIT));
  192. WRITE_CBUS_REG(ISA_TIMERA, 9999);
  193. clockevent_meson_1mhz.mult =
  194. div_sc(1000000, NSEC_PER_SEC, clockevent_meson_1mhz.shift);
  195. clockevent_meson_1mhz.max_delta_ns =
  196. clockevent_delta2ns(0xfffe, &clockevent_meson_1mhz);
  197. clockevent_meson_1mhz.min_delta_ns =
  198. clockevent_delta2ns(1, &clockevent_meson_1mhz);
  199. clockevent_meson_1mhz.cpumask = cpumask_of(0);
  200. clockevents_register_device(&clockevent_meson_1mhz);
  201. /* Set up the IRQ handler */
  202. setup_irq(INT_TIMER_A, &meson_timer_irq);
  203. setup_irq(INT_TIMER_C, &meson_timer_irq);
  204. }
  205. /* --------------------------------------------------------------------------*/
  206. /**
  207. * @brief meson_timer_init
  208. *
  209. * @return
  210. */
  211. /* --------------------------------------------------------------------------*/
  212. static void __init meson_timer_init(void)
  213. {
  214. meson_clocksource_init();
  215. meson_clockevent_init();
  216. }
  217. struct sys_timer meson_sys_timer = {
  218. .init = meson_timer_init,
  219. };