time.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*
  2. * linux/arch/unicore32/kernel/time.c
  3. *
  4. * Code specific to PKUnity SoC and UniCore ISA
  5. *
  6. * Maintained by GUAN Xue-tao <gxt@mprc.pku.edu.cn>
  7. * Copyright (C) 2001-2010 Guan Xuetao
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2 as
  11. * published by the Free Software Foundation.
  12. */
  13. #include <linux/init.h>
  14. #include <linux/errno.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/irq.h>
  17. #include <linux/timex.h>
  18. #include <linux/clockchips.h>
  19. #include <mach/hardware.h>
  20. #define MIN_OSCR_DELTA 2
  21. static irqreturn_t puv3_ost0_interrupt(int irq, void *dev_id)
  22. {
  23. struct clock_event_device *c = dev_id;
  24. /* Disarm the compare/match, signal the event. */
  25. writel(readl(OST_OIER) & ~OST_OIER_E0, OST_OIER);
  26. writel(readl(OST_OSSR) & ~OST_OSSR_M0, OST_OSSR);
  27. c->event_handler(c);
  28. return IRQ_HANDLED;
  29. }
  30. static int
  31. puv3_osmr0_set_next_event(unsigned long delta, struct clock_event_device *c)
  32. {
  33. unsigned long next, oscr;
  34. writel(readl(OST_OIER) | OST_OIER_E0, OST_OIER);
  35. next = readl(OST_OSCR) + delta;
  36. writel(next, OST_OSMR0);
  37. oscr = readl(OST_OSCR);
  38. return (signed)(next - oscr) <= MIN_OSCR_DELTA ? -ETIME : 0;
  39. }
  40. static void
  41. puv3_osmr0_set_mode(enum clock_event_mode mode, struct clock_event_device *c)
  42. {
  43. switch (mode) {
  44. case CLOCK_EVT_MODE_ONESHOT:
  45. case CLOCK_EVT_MODE_UNUSED:
  46. case CLOCK_EVT_MODE_SHUTDOWN:
  47. writel(readl(OST_OIER) & ~OST_OIER_E0, OST_OIER);
  48. writel(readl(OST_OSSR) & ~OST_OSSR_M0, OST_OSSR);
  49. break;
  50. case CLOCK_EVT_MODE_RESUME:
  51. case CLOCK_EVT_MODE_PERIODIC:
  52. break;
  53. }
  54. }
  55. static struct clock_event_device ckevt_puv3_osmr0 = {
  56. .name = "osmr0",
  57. .features = CLOCK_EVT_FEAT_ONESHOT,
  58. .rating = 200,
  59. .set_next_event = puv3_osmr0_set_next_event,
  60. .set_mode = puv3_osmr0_set_mode,
  61. };
  62. static cycle_t puv3_read_oscr(struct clocksource *cs)
  63. {
  64. return readl(OST_OSCR);
  65. }
  66. static struct clocksource cksrc_puv3_oscr = {
  67. .name = "oscr",
  68. .rating = 200,
  69. .read = puv3_read_oscr,
  70. .mask = CLOCKSOURCE_MASK(32),
  71. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  72. };
  73. static struct irqaction puv3_timer_irq = {
  74. .name = "ost0",
  75. .flags = IRQF_DISABLED | IRQF_TIMER | IRQF_IRQPOLL,
  76. .handler = puv3_ost0_interrupt,
  77. .dev_id = &ckevt_puv3_osmr0,
  78. };
  79. void __init time_init(void)
  80. {
  81. writel(0, OST_OIER); /* disable any timer interrupts */
  82. writel(0, OST_OSSR); /* clear status on all timers */
  83. clockevents_calc_mult_shift(&ckevt_puv3_osmr0, CLOCK_TICK_RATE, 5);
  84. ckevt_puv3_osmr0.max_delta_ns =
  85. clockevent_delta2ns(0x7fffffff, &ckevt_puv3_osmr0);
  86. ckevt_puv3_osmr0.min_delta_ns =
  87. clockevent_delta2ns(MIN_OSCR_DELTA * 2, &ckevt_puv3_osmr0) + 1;
  88. ckevt_puv3_osmr0.cpumask = cpumask_of(0);
  89. setup_irq(IRQ_TIMER0, &puv3_timer_irq);
  90. clocksource_register_hz(&cksrc_puv3_oscr, CLOCK_TICK_RATE);
  91. clockevents_register_device(&ckevt_puv3_osmr0);
  92. }
  93. #ifdef CONFIG_PM
  94. unsigned long osmr[4], oier;
  95. void puv3_timer_suspend(void)
  96. {
  97. osmr[0] = readl(OST_OSMR0);
  98. osmr[1] = readl(OST_OSMR1);
  99. osmr[2] = readl(OST_OSMR2);
  100. osmr[3] = readl(OST_OSMR3);
  101. oier = readl(OST_OIER);
  102. }
  103. void puv3_timer_resume(void)
  104. {
  105. writel(0, OST_OSSR);
  106. writel(osmr[0], OST_OSMR0);
  107. writel(osmr[1], OST_OSMR1);
  108. writel(osmr[2], OST_OSMR2);
  109. writel(osmr[3], OST_OSMR3);
  110. writel(oier, OST_OIER);
  111. /*
  112. * OSMR0 is the system timer: make sure OSCR is sufficiently behind
  113. */
  114. writel(readl(OST_OSMR0) - LATCH, OST_OSCR);
  115. }
  116. #else
  117. void puv3_timer_suspend(void) { };
  118. void puv3_timer_resume(void) { };
  119. #endif