cevt-ds1287.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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 void ds1287_set_mode(enum clock_event_mode mode,
  55. struct clock_event_device *evt)
  56. {
  57. u8 val;
  58. spin_lock(&rtc_lock);
  59. val = CMOS_READ(RTC_REG_B);
  60. switch (mode) {
  61. case CLOCK_EVT_MODE_PERIODIC:
  62. val |= RTC_PIE;
  63. break;
  64. default:
  65. val &= ~RTC_PIE;
  66. break;
  67. }
  68. CMOS_WRITE(val, RTC_REG_B);
  69. spin_unlock(&rtc_lock);
  70. }
  71. static void ds1287_event_handler(struct clock_event_device *dev)
  72. {
  73. }
  74. static struct clock_event_device ds1287_clockevent = {
  75. .name = "ds1287",
  76. .features = CLOCK_EVT_FEAT_PERIODIC,
  77. .set_next_event = ds1287_set_next_event,
  78. .set_mode = ds1287_set_mode,
  79. .event_handler = ds1287_event_handler,
  80. };
  81. static irqreturn_t ds1287_interrupt(int irq, void *dev_id)
  82. {
  83. struct clock_event_device *cd = &ds1287_clockevent;
  84. /* Ack the RTC interrupt. */
  85. CMOS_READ(RTC_REG_C);
  86. cd->event_handler(cd);
  87. return IRQ_HANDLED;
  88. }
  89. static struct irqaction ds1287_irqaction = {
  90. .handler = ds1287_interrupt,
  91. .flags = IRQF_PERCPU | IRQF_TIMER,
  92. .name = "ds1287",
  93. };
  94. int __init ds1287_clockevent_init(int irq)
  95. {
  96. struct clock_event_device *cd;
  97. cd = &ds1287_clockevent;
  98. cd->rating = 100;
  99. cd->irq = irq;
  100. clockevent_set_clock(cd, 32768);
  101. cd->max_delta_ns = clockevent_delta2ns(0x7fffffff, cd);
  102. cd->min_delta_ns = clockevent_delta2ns(0x300, cd);
  103. cd->cpumask = cpumask_of(0);
  104. clockevents_register_device(&ds1287_clockevent);
  105. return setup_irq(irq, &ds1287_irqaction);
  106. }