time.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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 inline void timer_shutdown(struct clock_event_device *evt)
  33. {
  34. /* disable timer */
  35. writel(0, NETX_GPIO_COUNTER_CTRL(TIMER_CLOCKEVENT));
  36. }
  37. static int netx_shutdown(struct clock_event_device *evt)
  38. {
  39. timer_shutdown(evt);
  40. return 0;
  41. }
  42. static int netx_set_oneshot(struct clock_event_device *evt)
  43. {
  44. u32 tmode = NETX_GPIO_COUNTER_CTRL_IRQ_EN | NETX_GPIO_COUNTER_CTRL_RUN;
  45. timer_shutdown(evt);
  46. writel(0, NETX_GPIO_COUNTER_MAX(TIMER_CLOCKEVENT));
  47. writel(tmode, NETX_GPIO_COUNTER_CTRL(TIMER_CLOCKEVENT));
  48. return 0;
  49. }
  50. static int netx_set_periodic(struct clock_event_device *evt)
  51. {
  52. u32 tmode = NETX_GPIO_COUNTER_CTRL_RST_EN |
  53. NETX_GPIO_COUNTER_CTRL_IRQ_EN | NETX_GPIO_COUNTER_CTRL_RUN;
  54. timer_shutdown(evt);
  55. writel(NETX_LATCH, NETX_GPIO_COUNTER_MAX(TIMER_CLOCKEVENT));
  56. writel(tmode, NETX_GPIO_COUNTER_CTRL(TIMER_CLOCKEVENT));
  57. return 0;
  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_state_shutdown = netx_shutdown,
  70. .set_state_periodic = netx_set_periodic,
  71. .set_state_oneshot = netx_set_oneshot,
  72. .tick_resume = netx_shutdown,
  73. };
  74. /*
  75. * IRQ handler for the timer
  76. */
  77. static irqreturn_t
  78. netx_timer_interrupt(int irq, void *dev_id)
  79. {
  80. struct clock_event_device *evt = &netx_clockevent;
  81. /* acknowledge interrupt */
  82. writel(COUNTER_BIT(0), NETX_GPIO_IRQ);
  83. evt->event_handler(evt);
  84. return IRQ_HANDLED;
  85. }
  86. static struct irqaction netx_timer_irq = {
  87. .name = "NetX Timer Tick",
  88. .flags = IRQF_TIMER | IRQF_IRQPOLL,
  89. .handler = netx_timer_interrupt,
  90. };
  91. /*
  92. * Set up timer interrupt
  93. */
  94. void __init netx_timer_init(void)
  95. {
  96. /* disable timer initially */
  97. writel(0, NETX_GPIO_COUNTER_CTRL(0));
  98. /* Reset the timer value to zero */
  99. writel(0, NETX_GPIO_COUNTER_CURRENT(0));
  100. writel(NETX_LATCH, NETX_GPIO_COUNTER_MAX(0));
  101. /* acknowledge interrupt */
  102. writel(COUNTER_BIT(0), NETX_GPIO_IRQ);
  103. /* Enable the interrupt in the specific timer
  104. * register and start timer
  105. */
  106. writel(COUNTER_BIT(0), NETX_GPIO_IRQ_ENABLE);
  107. writel(NETX_GPIO_COUNTER_CTRL_IRQ_EN | NETX_GPIO_COUNTER_CTRL_RUN,
  108. NETX_GPIO_COUNTER_CTRL(0));
  109. setup_irq(NETX_IRQ_TIMER0, &netx_timer_irq);
  110. /* Setup timer one for clocksource */
  111. writel(0, NETX_GPIO_COUNTER_CTRL(TIMER_CLOCKSOURCE));
  112. writel(0, NETX_GPIO_COUNTER_CURRENT(TIMER_CLOCKSOURCE));
  113. writel(0xffffffff, NETX_GPIO_COUNTER_MAX(TIMER_CLOCKSOURCE));
  114. writel(NETX_GPIO_COUNTER_CTRL_RUN,
  115. NETX_GPIO_COUNTER_CTRL(TIMER_CLOCKSOURCE));
  116. clocksource_mmio_init(NETX_GPIO_COUNTER_CURRENT(TIMER_CLOCKSOURCE),
  117. "netx_timer", NETX_CLOCK_FREQ, 200, 32, clocksource_mmio_readl_up);
  118. /* with max_delta_ns >= delta2ns(0x800) the system currently runs fine.
  119. * Adding some safety ... */
  120. netx_clockevent.cpumask = cpumask_of(0);
  121. clockevents_config_and_register(&netx_clockevent, NETX_CLOCK_FREQ,
  122. 0xa00, 0xfffffffe);
  123. }