cevt-ds1287.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /*
  2. * DS1287 clockevent driver
  3. *
  4. * Copyright (C) 2008 Yoichi Yuasa <yuasa@linux-mips.org>
  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/mc146818rtc.h>
  24. #include <linux/irq.h>
  25. #include <asm/time.h>
  26. int ds1287_timer_state(void)
  27. {
  28. return (CMOS_READ(RTC_REG_C) & RTC_PF) != 0;
  29. }
  30. int ds1287_set_base_clock(unsigned int hz)
  31. {
  32. u8 rate;
  33. switch (hz) {
  34. case 128:
  35. rate = 0x9;
  36. break;
  37. case 256:
  38. rate = 0x8;
  39. break;
  40. case 1024:
  41. rate = 0x6;
  42. break;
  43. default:
  44. return -EINVAL;
  45. }
  46. CMOS_WRITE(RTC_REF_CLCK_32KHZ | rate, RTC_REG_A);
  47. return 0;
  48. }
  49. static int ds1287_set_next_event(unsigned long delta,
  50. struct clock_event_device *evt)
  51. {
  52. return -EINVAL;
  53. }
  54. static int ds1287_shutdown(struct clock_event_device *evt)
  55. {
  56. u8 val;
  57. spin_lock(&rtc_lock);
  58. val = CMOS_READ(RTC_REG_B);
  59. val &= ~RTC_PIE;
  60. CMOS_WRITE(val, RTC_REG_B);
  61. spin_unlock(&rtc_lock);
  62. return 0;
  63. }
  64. static int ds1287_set_periodic(struct clock_event_device *evt)
  65. {
  66. u8 val;
  67. spin_lock(&rtc_lock);
  68. val = CMOS_READ(RTC_REG_B);
  69. val |= RTC_PIE;
  70. CMOS_WRITE(val, RTC_REG_B);
  71. spin_unlock(&rtc_lock);
  72. return 0;
  73. }
  74. static void ds1287_event_handler(struct clock_event_device *dev)
  75. {
  76. }
  77. static struct clock_event_device ds1287_clockevent = {
  78. .name = "ds1287",
  79. .features = CLOCK_EVT_FEAT_PERIODIC,
  80. .set_next_event = ds1287_set_next_event,
  81. .set_state_shutdown = ds1287_shutdown,
  82. .set_state_periodic = ds1287_set_periodic,
  83. .tick_resume = ds1287_shutdown,
  84. .event_handler = ds1287_event_handler,
  85. };
  86. static irqreturn_t ds1287_interrupt(int irq, void *dev_id)
  87. {
  88. struct clock_event_device *cd = &ds1287_clockevent;
  89. /* Ack the RTC interrupt. */
  90. CMOS_READ(RTC_REG_C);
  91. cd->event_handler(cd);
  92. return IRQ_HANDLED;
  93. }
  94. static struct irqaction ds1287_irqaction = {
  95. .handler = ds1287_interrupt,
  96. .flags = IRQF_PERCPU | IRQF_TIMER,
  97. .name = "ds1287",
  98. };
  99. int __init ds1287_clockevent_init(int irq)
  100. {
  101. struct clock_event_device *cd;
  102. cd = &ds1287_clockevent;
  103. cd->rating = 100;
  104. cd->irq = irq;
  105. clockevent_set_clock(cd, 32768);
  106. cd->max_delta_ns = clockevent_delta2ns(0x7fffffff, cd);
  107. cd->min_delta_ns = clockevent_delta2ns(0x300, cd);
  108. cd->cpumask = cpumask_of(0);
  109. clockevents_register_device(&ds1287_clockevent);
  110. return setup_irq(irq, &ds1287_irqaction);
  111. }