time.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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 NETX_CLOCK_FREQ 100000000
  29. #define NETX_LATCH DIV_ROUND_CLOSEST(NETX_CLOCK_FREQ, HZ)
  30. #define TIMER_CLOCKEVENT 0
  31. #define TIMER_CLOCKSOURCE 1
  32. static void netx_set_mode(enum clock_event_mode mode,
  33. struct clock_event_device *clk)
  34. {
  35. u32 tmode;
  36. /* disable timer */
  37. writel(0, NETX_GPIO_COUNTER_CTRL(TIMER_CLOCKEVENT));
  38. switch (mode) {
  39. case CLOCK_EVT_MODE_PERIODIC:
  40. writel(NETX_LATCH, NETX_GPIO_COUNTER_MAX(TIMER_CLOCKEVENT));
  41. tmode = NETX_GPIO_COUNTER_CTRL_RST_EN |
  42. NETX_GPIO_COUNTER_CTRL_IRQ_EN |
  43. NETX_GPIO_COUNTER_CTRL_RUN;
  44. break;
  45. case CLOCK_EVT_MODE_ONESHOT:
  46. writel(0, NETX_GPIO_COUNTER_MAX(TIMER_CLOCKEVENT));
  47. tmode = NETX_GPIO_COUNTER_CTRL_IRQ_EN |
  48. NETX_GPIO_COUNTER_CTRL_RUN;
  49. break;
  50. default:
  51. WARN(1, "%s: unhandled mode %d\n", __func__, mode);
  52. /* fall through */
  53. case CLOCK_EVT_MODE_SHUTDOWN:
  54. case CLOCK_EVT_MODE_UNUSED:
  55. case CLOCK_EVT_MODE_RESUME:
  56. tmode = 0;
  57. break;
  58. }
  59. writel(tmode, NETX_GPIO_COUNTER_CTRL(TIMER_CLOCKEVENT));
  60. }
  61. static int netx_set_next_event(unsigned long evt,
  62. struct clock_event_device *clk)
  63. {
  64. writel(0 - evt, NETX_GPIO_COUNTER_CURRENT(TIMER_CLOCKEVENT));
  65. return 0;
  66. }
  67. static struct clock_event_device netx_clockevent = {
  68. .name = "netx-timer" __stringify(TIMER_CLOCKEVENT),
  69. .features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT,
  70. .set_next_event = netx_set_next_event,
  71. .set_mode = netx_set_mode,
  72. };
  73. /*
  74. * IRQ handler for the timer
  75. */
  76. static irqreturn_t
  77. netx_timer_interrupt(int irq, void *dev_id)
  78. {
  79. struct clock_event_device *evt = &netx_clockevent;
  80. /* acknowledge interrupt */
  81. writel(COUNTER_BIT(0), NETX_GPIO_IRQ);
  82. evt->event_handler(evt);
  83. return IRQ_HANDLED;
  84. }
  85. static struct irqaction netx_timer_irq = {
  86. .name = "NetX Timer Tick",
  87. .flags = IRQF_TIMER | IRQF_IRQPOLL,
  88. .handler = netx_timer_interrupt,
  89. };
  90. /*
  91. * Set up timer interrupt
  92. */
  93. void __init netx_timer_init(void)
  94. {
  95. /* disable timer initially */
  96. writel(0, NETX_GPIO_COUNTER_CTRL(0));
  97. /* Reset the timer value to zero */
  98. writel(0, NETX_GPIO_COUNTER_CURRENT(0));
  99. writel(NETX_LATCH, NETX_GPIO_COUNTER_MAX(0));
  100. /* acknowledge interrupt */
  101. writel(COUNTER_BIT(0), NETX_GPIO_IRQ);
  102. /* Enable the interrupt in the specific timer
  103. * register and start timer
  104. */
  105. writel(COUNTER_BIT(0), NETX_GPIO_IRQ_ENABLE);
  106. writel(NETX_GPIO_COUNTER_CTRL_IRQ_EN | NETX_GPIO_COUNTER_CTRL_RUN,
  107. NETX_GPIO_COUNTER_CTRL(0));
  108. setup_irq(NETX_IRQ_TIMER0, &netx_timer_irq);
  109. /* Setup timer one for clocksource */
  110. writel(0, NETX_GPIO_COUNTER_CTRL(TIMER_CLOCKSOURCE));
  111. writel(0, NETX_GPIO_COUNTER_CURRENT(TIMER_CLOCKSOURCE));
  112. writel(0xffffffff, NETX_GPIO_COUNTER_MAX(TIMER_CLOCKSOURCE));
  113. writel(NETX_GPIO_COUNTER_CTRL_RUN,
  114. NETX_GPIO_COUNTER_CTRL(TIMER_CLOCKSOURCE));
  115. clocksource_mmio_init(NETX_GPIO_COUNTER_CURRENT(TIMER_CLOCKSOURCE),
  116. "netx_timer", NETX_CLOCK_FREQ, 200, 32, clocksource_mmio_readl_up);
  117. /* with max_delta_ns >= delta2ns(0x800) the system currently runs fine.
  118. * Adding some safety ... */
  119. netx_clockevent.cpumask = cpumask_of(0);
  120. clockevents_config_and_register(&netx_clockevent, NETX_CLOCK_FREQ,
  121. 0xa00, 0xfffffffe);
  122. }