cevt-gt641xx.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /*
  2. * GT641xx clockevent routines.
  3. *
  4. * Copyright (C) 2007 Yoichi Yuasa <yoichi_yuasa@tripeaks.co.jp>
  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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #include <linux/clockchips.h>
  21. #include <linux/init.h>
  22. #include <linux/interrupt.h>
  23. #include <linux/spinlock.h>
  24. #include <asm/gt64120.h>
  25. #include <asm/time.h>
  26. static DEFINE_SPINLOCK(gt641xx_timer_lock);
  27. static unsigned int gt641xx_base_clock;
  28. void gt641xx_set_base_clock(unsigned int clock)
  29. {
  30. gt641xx_base_clock = clock;
  31. }
  32. int gt641xx_timer0_state(void)
  33. {
  34. if (GT_READ(GT_TC0_OFS))
  35. return 0;
  36. GT_WRITE(GT_TC0_OFS, gt641xx_base_clock / HZ);
  37. GT_WRITE(GT_TC_CONTROL_OFS, GT_TC_CONTROL_ENTC0_MSK);
  38. return 1;
  39. }
  40. static int gt641xx_timer0_set_next_event(unsigned long delta,
  41. struct clock_event_device *evt)
  42. {
  43. u32 ctrl;
  44. spin_lock(&gt641xx_timer_lock);
  45. ctrl = GT_READ(GT_TC_CONTROL_OFS);
  46. ctrl &= ~(GT_TC_CONTROL_ENTC0_MSK | GT_TC_CONTROL_SELTC0_MSK);
  47. ctrl |= GT_TC_CONTROL_ENTC0_MSK;
  48. GT_WRITE(GT_TC0_OFS, delta);
  49. GT_WRITE(GT_TC_CONTROL_OFS, ctrl);
  50. spin_unlock(&gt641xx_timer_lock);
  51. return 0;
  52. }
  53. static void gt641xx_timer0_set_mode(enum clock_event_mode mode,
  54. struct clock_event_device *evt)
  55. {
  56. u32 ctrl;
  57. spin_lock(&gt641xx_timer_lock);
  58. ctrl = GT_READ(GT_TC_CONTROL_OFS);
  59. ctrl &= ~(GT_TC_CONTROL_ENTC0_MSK | GT_TC_CONTROL_SELTC0_MSK);
  60. switch (mode) {
  61. case CLOCK_EVT_MODE_PERIODIC:
  62. ctrl |= GT_TC_CONTROL_ENTC0_MSK | GT_TC_CONTROL_SELTC0_MSK;
  63. break;
  64. case CLOCK_EVT_MODE_ONESHOT:
  65. ctrl |= GT_TC_CONTROL_ENTC0_MSK;
  66. break;
  67. default:
  68. break;
  69. }
  70. GT_WRITE(GT_TC_CONTROL_OFS, ctrl);
  71. spin_unlock(&gt641xx_timer_lock);
  72. }
  73. static void gt641xx_timer0_event_handler(struct clock_event_device *dev)
  74. {
  75. }
  76. static struct clock_event_device gt641xx_timer0_clockevent = {
  77. .name = "gt641xx-timer0",
  78. .features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT,
  79. .cpumask = CPU_MASK_CPU0,
  80. .irq = GT641XX_TIMER0_IRQ,
  81. .set_next_event = gt641xx_timer0_set_next_event,
  82. .set_mode = gt641xx_timer0_set_mode,
  83. .event_handler = gt641xx_timer0_event_handler,
  84. };
  85. static irqreturn_t gt641xx_timer0_interrupt(int irq, void *dev_id)
  86. {
  87. struct clock_event_device *cd = &gt641xx_timer0_clockevent;
  88. cd->event_handler(cd);
  89. return IRQ_HANDLED;
  90. }
  91. static struct irqaction gt641xx_timer0_irqaction = {
  92. .handler = gt641xx_timer0_interrupt,
  93. .flags = IRQF_DISABLED | IRQF_PERCPU,
  94. .name = "gt641xx_timer0",
  95. };
  96. static int __init gt641xx_timer0_clockevent_init(void)
  97. {
  98. struct clock_event_device *cd;
  99. if (!gt641xx_base_clock)
  100. return 0;
  101. GT_WRITE(GT_TC0_OFS, gt641xx_base_clock / HZ);
  102. cd = &gt641xx_timer0_clockevent;
  103. cd->rating = 200 + gt641xx_base_clock / 10000000;
  104. clockevent_set_clock(cd, gt641xx_base_clock);
  105. cd->max_delta_ns = clockevent_delta2ns(0x7fffffff, cd);
  106. cd->min_delta_ns = clockevent_delta2ns(0x300, cd);
  107. clockevents_register_device(&gt641xx_timer0_clockevent);
  108. return setup_irq(GT641XX_TIMER0_IRQ, &gt641xx_timer0_irqaction);
  109. }
  110. arch_initcall(gt641xx_timer0_clockevent_init);