time.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /*
  2. * arch/arm/mach-netx/time.c
  3. *
  4. * Copyright (c) 2005 Sascha Hauer <s.hauer@pengutronix.de>, Pengutronix
  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 version 2
  8. * as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. #include <linux/init.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/irq.h>
  22. #include <linux/clocksource.h>
  23. #include <linux/clockchips.h>
  24. #include <linux/io.h>
  25. #include <mach/hardware.h>
  26. #include <asm/mach/time.h>
  27. #include <mach/netx-regs.h>
  28. #define TIMER_CLOCKEVENT 0
  29. #define TIMER_CLOCKSOURCE 1
  30. static void netx_set_mode(enum clock_event_mode mode,
  31. struct clock_event_device *clk)
  32. {
  33. u32 tmode;
  34. /* disable timer */
  35. writel(0, NETX_GPIO_COUNTER_CTRL(TIMER_CLOCKEVENT));
  36. switch (mode) {
  37. case CLOCK_EVT_MODE_PERIODIC:
  38. writel(LATCH, NETX_GPIO_COUNTER_MAX(TIMER_CLOCKEVENT));
  39. tmode = NETX_GPIO_COUNTER_CTRL_RST_EN |
  40. NETX_GPIO_COUNTER_CTRL_IRQ_EN |
  41. NETX_GPIO_COUNTER_CTRL_RUN;
  42. break;
  43. case CLOCK_EVT_MODE_ONESHOT:
  44. writel(0, NETX_GPIO_COUNTER_MAX(TIMER_CLOCKEVENT));
  45. tmode = NETX_GPIO_COUNTER_CTRL_IRQ_EN |
  46. NETX_GPIO_COUNTER_CTRL_RUN;
  47. break;
  48. default:
  49. WARN(1, "%s: unhandled mode %d\n", __func__, mode);
  50. /* fall through */
  51. case CLOCK_EVT_MODE_SHUTDOWN:
  52. case CLOCK_EVT_MODE_UNUSED:
  53. case CLOCK_EVT_MODE_RESUME:
  54. tmode = 0;
  55. break;
  56. }
  57. writel(tmode, NETX_GPIO_COUNTER_CTRL(TIMER_CLOCKEVENT));
  58. }
  59. static int netx_set_next_event(unsigned long evt,
  60. struct clock_event_device *clk)
  61. {
  62. writel(0 - evt, NETX_GPIO_COUNTER_CURRENT(TIMER_CLOCKEVENT));
  63. return 0;
  64. }
  65. static struct clock_event_device netx_clockevent = {
  66. .name = "netx-timer" __stringify(TIMER_CLOCKEVENT),
  67. .shift = 32,
  68. .features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT,
  69. .set_next_event = netx_set_next_event,
  70. .set_mode = netx_set_mode,
  71. };
  72. /*
  73. * IRQ handler for the timer
  74. */
  75. static irqreturn_t
  76. netx_timer_interrupt(int irq, void *dev_id)
  77. {
  78. struct clock_event_device *evt = &netx_clockevent;
  79. /* acknowledge interrupt */
  80. writel(COUNTER_BIT(0), NETX_GPIO_IRQ);
  81. evt->event_handler(evt);
  82. return IRQ_HANDLED;
  83. }
  84. static struct irqaction netx_timer_irq = {
  85. .name = "NetX Timer Tick",
  86. .flags = IRQF_DISABLED | IRQF_TIMER | IRQF_IRQPOLL,
  87. .handler = netx_timer_interrupt,
  88. };
  89. cycle_t netx_get_cycles(struct clocksource *cs)
  90. {
  91. return readl(NETX_GPIO_COUNTER_CURRENT(TIMER_CLOCKSOURCE));
  92. }
  93. static struct clocksource clocksource_netx = {
  94. .name = "netx_timer",
  95. .rating = 200,
  96. .read = netx_get_cycles,
  97. .mask = CLOCKSOURCE_MASK(32),
  98. .shift = 20,
  99. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  100. };
  101. /*
  102. * Set up timer interrupt
  103. */
  104. static void __init netx_timer_init(void)
  105. {
  106. /* disable timer initially */
  107. writel(0, NETX_GPIO_COUNTER_CTRL(0));
  108. /* Reset the timer value to zero */
  109. writel(0, NETX_GPIO_COUNTER_CURRENT(0));
  110. writel(LATCH, NETX_GPIO_COUNTER_MAX(0));
  111. /* acknowledge interrupt */
  112. writel(COUNTER_BIT(0), NETX_GPIO_IRQ);
  113. /* Enable the interrupt in the specific timer
  114. * register and start timer
  115. */
  116. writel(COUNTER_BIT(0), NETX_GPIO_IRQ_ENABLE);
  117. writel(NETX_GPIO_COUNTER_CTRL_IRQ_EN | NETX_GPIO_COUNTER_CTRL_RUN,
  118. NETX_GPIO_COUNTER_CTRL(0));
  119. setup_irq(NETX_IRQ_TIMER0, &netx_timer_irq);
  120. /* Setup timer one for clocksource */
  121. writel(0, NETX_GPIO_COUNTER_CTRL(TIMER_CLOCKSOURCE));
  122. writel(0, NETX_GPIO_COUNTER_CURRENT(TIMER_CLOCKSOURCE));
  123. writel(0xffffffff, NETX_GPIO_COUNTER_MAX(TIMER_CLOCKSOURCE));
  124. writel(NETX_GPIO_COUNTER_CTRL_RUN,
  125. NETX_GPIO_COUNTER_CTRL(TIMER_CLOCKSOURCE));
  126. clocksource_netx.mult =
  127. clocksource_hz2mult(CLOCK_TICK_RATE, clocksource_netx.shift);
  128. clocksource_register(&clocksource_netx);
  129. netx_clockevent.mult = div_sc(CLOCK_TICK_RATE, NSEC_PER_SEC,
  130. netx_clockevent.shift);
  131. netx_clockevent.max_delta_ns =
  132. clockevent_delta2ns(0xfffffffe, &netx_clockevent);
  133. /* with max_delta_ns >= delta2ns(0x800) the system currently runs fine.
  134. * Adding some safety ... */
  135. netx_clockevent.min_delta_ns =
  136. clockevent_delta2ns(0xa00, &netx_clockevent);
  137. netx_clockevent.cpumask = cpumask_of(0);
  138. clockevents_register_device(&netx_clockevent);
  139. }
  140. struct sys_timer netx_timer = {
  141. .init = netx_timer_init,
  142. };