cevt-ds1287.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * DS1287 clockevent driver
  4. *
  5. * Copyright (C) 2008 Yoichi Yuasa <yuasa@linux-mips.org>
  6. */
  7. #include <linux/clockchips.h>
  8. #include <linux/init.h>
  9. #include <linux/interrupt.h>
  10. #include <linux/mc146818rtc.h>
  11. #include <linux/irq.h>
  12. #include <asm/ds1287.h>
  13. #include <asm/time.h>
  14. int ds1287_timer_state(void)
  15. {
  16. return (CMOS_READ(RTC_REG_C) & RTC_PF) != 0;
  17. }
  18. int ds1287_set_base_clock(unsigned int hz)
  19. {
  20. u8 rate;
  21. switch (hz) {
  22. case 128:
  23. rate = 0x9;
  24. break;
  25. case 256:
  26. rate = 0x8;
  27. break;
  28. case 1024:
  29. rate = 0x6;
  30. break;
  31. default:
  32. return -EINVAL;
  33. }
  34. CMOS_WRITE(RTC_REF_CLCK_32KHZ | rate, RTC_REG_A);
  35. return 0;
  36. }
  37. static int ds1287_set_next_event(unsigned long delta,
  38. struct clock_event_device *evt)
  39. {
  40. return -EINVAL;
  41. }
  42. static int ds1287_shutdown(struct clock_event_device *evt)
  43. {
  44. u8 val;
  45. spin_lock(&rtc_lock);
  46. val = CMOS_READ(RTC_REG_B);
  47. val &= ~RTC_PIE;
  48. CMOS_WRITE(val, RTC_REG_B);
  49. spin_unlock(&rtc_lock);
  50. return 0;
  51. }
  52. static int ds1287_set_periodic(struct clock_event_device *evt)
  53. {
  54. u8 val;
  55. spin_lock(&rtc_lock);
  56. val = CMOS_READ(RTC_REG_B);
  57. val |= RTC_PIE;
  58. CMOS_WRITE(val, RTC_REG_B);
  59. spin_unlock(&rtc_lock);
  60. return 0;
  61. }
  62. static void ds1287_event_handler(struct clock_event_device *dev)
  63. {
  64. }
  65. static struct clock_event_device ds1287_clockevent = {
  66. .name = "ds1287",
  67. .features = CLOCK_EVT_FEAT_PERIODIC,
  68. .set_next_event = ds1287_set_next_event,
  69. .set_state_shutdown = ds1287_shutdown,
  70. .set_state_periodic = ds1287_set_periodic,
  71. .tick_resume = ds1287_shutdown,
  72. .event_handler = ds1287_event_handler,
  73. };
  74. static irqreturn_t ds1287_interrupt(int irq, void *dev_id)
  75. {
  76. struct clock_event_device *cd = &ds1287_clockevent;
  77. /* Ack the RTC interrupt. */
  78. CMOS_READ(RTC_REG_C);
  79. cd->event_handler(cd);
  80. return IRQ_HANDLED;
  81. }
  82. int __init ds1287_clockevent_init(int irq)
  83. {
  84. unsigned long flags = IRQF_PERCPU | IRQF_TIMER;
  85. struct clock_event_device *cd;
  86. cd = &ds1287_clockevent;
  87. cd->rating = 100;
  88. cd->irq = irq;
  89. clockevent_set_clock(cd, 32768);
  90. cd->max_delta_ns = clockevent_delta2ns(0x7fffffff, cd);
  91. cd->max_delta_ticks = 0x7fffffff;
  92. cd->min_delta_ns = clockevent_delta2ns(0x300, cd);
  93. cd->min_delta_ticks = 0x300;
  94. cd->cpumask = cpumask_of(0);
  95. clockevents_register_device(&ds1287_clockevent);
  96. return request_irq(irq, ds1287_interrupt, flags, "ds1287", NULL);
  97. }