time.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. .features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT,
  68. .set_next_event = netx_set_next_event,
  69. .set_mode = netx_set_mode,
  70. };
  71. /*
  72. * IRQ handler for the timer
  73. */
  74. static irqreturn_t
  75. netx_timer_interrupt(int irq, void *dev_id)
  76. {
  77. struct clock_event_device *evt = &netx_clockevent;
  78. /* acknowledge interrupt */
  79. writel(COUNTER_BIT(0), NETX_GPIO_IRQ);
  80. evt->event_handler(evt);
  81. return IRQ_HANDLED;
  82. }
  83. static struct irqaction netx_timer_irq = {
  84. .name = "NetX Timer Tick",
  85. .flags = IRQF_DISABLED | IRQF_TIMER | IRQF_IRQPOLL,
  86. .handler = netx_timer_interrupt,
  87. };
  88. /*
  89. * Set up timer interrupt
  90. */
  91. void __init netx_timer_init(void)
  92. {
  93. /* disable timer initially */
  94. writel(0, NETX_GPIO_COUNTER_CTRL(0));
  95. /* Reset the timer value to zero */
  96. writel(0, NETX_GPIO_COUNTER_CURRENT(0));
  97. writel(LATCH, NETX_GPIO_COUNTER_MAX(0));
  98. /* acknowledge interrupt */
  99. writel(COUNTER_BIT(0), NETX_GPIO_IRQ);
  100. /* Enable the interrupt in the specific timer
  101. * register and start timer
  102. */
  103. writel(COUNTER_BIT(0), NETX_GPIO_IRQ_ENABLE);
  104. writel(NETX_GPIO_COUNTER_CTRL_IRQ_EN | NETX_GPIO_COUNTER_CTRL_RUN,
  105. NETX_GPIO_COUNTER_CTRL(0));
  106. setup_irq(NETX_IRQ_TIMER0, &netx_timer_irq);
  107. /* Setup timer one for clocksource */
  108. writel(0, NETX_GPIO_COUNTER_CTRL(TIMER_CLOCKSOURCE));
  109. writel(0, NETX_GPIO_COUNTER_CURRENT(TIMER_CLOCKSOURCE));
  110. writel(0xffffffff, NETX_GPIO_COUNTER_MAX(TIMER_CLOCKSOURCE));
  111. writel(NETX_GPIO_COUNTER_CTRL_RUN,
  112. NETX_GPIO_COUNTER_CTRL(TIMER_CLOCKSOURCE));
  113. clocksource_mmio_init(NETX_GPIO_COUNTER_CURRENT(TIMER_CLOCKSOURCE),
  114. "netx_timer", CLOCK_TICK_RATE, 200, 32, clocksource_mmio_readl_up);
  115. /* with max_delta_ns >= delta2ns(0x800) the system currently runs fine.
  116. * Adding some safety ... */
  117. netx_clockevent.cpumask = cpumask_of(0);
  118. clockevents_config_and_register(&netx_clockevent, CLOCK_TICK_RATE,
  119. 0xa00, 0xfffffffe);
  120. }