time.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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 int puv3_osmr0_shutdown(struct clock_event_device *evt)
  41. {
  42. writel(readl(OST_OIER) & ~OST_OIER_E0, OST_OIER);
  43. writel(readl(OST_OSSR) & ~OST_OSSR_M0, OST_OSSR);
  44. return 0;
  45. }
  46. static struct clock_event_device ckevt_puv3_osmr0 = {
  47. .name = "osmr0",
  48. .features = CLOCK_EVT_FEAT_ONESHOT,
  49. .rating = 200,
  50. .set_next_event = puv3_osmr0_set_next_event,
  51. .set_state_shutdown = puv3_osmr0_shutdown,
  52. .set_state_oneshot = puv3_osmr0_shutdown,
  53. };
  54. static cycle_t puv3_read_oscr(struct clocksource *cs)
  55. {
  56. return readl(OST_OSCR);
  57. }
  58. static struct clocksource cksrc_puv3_oscr = {
  59. .name = "oscr",
  60. .rating = 200,
  61. .read = puv3_read_oscr,
  62. .mask = CLOCKSOURCE_MASK(32),
  63. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  64. };
  65. static struct irqaction puv3_timer_irq = {
  66. .name = "ost0",
  67. .flags = IRQF_TIMER | IRQF_IRQPOLL,
  68. .handler = puv3_ost0_interrupt,
  69. .dev_id = &ckevt_puv3_osmr0,
  70. };
  71. void __init time_init(void)
  72. {
  73. writel(0, OST_OIER); /* disable any timer interrupts */
  74. writel(0, OST_OSSR); /* clear status on all timers */
  75. clockevents_calc_mult_shift(&ckevt_puv3_osmr0, CLOCK_TICK_RATE, 5);
  76. ckevt_puv3_osmr0.max_delta_ns =
  77. clockevent_delta2ns(0x7fffffff, &ckevt_puv3_osmr0);
  78. ckevt_puv3_osmr0.min_delta_ns =
  79. clockevent_delta2ns(MIN_OSCR_DELTA * 2, &ckevt_puv3_osmr0) + 1;
  80. ckevt_puv3_osmr0.cpumask = cpumask_of(0);
  81. setup_irq(IRQ_TIMER0, &puv3_timer_irq);
  82. clocksource_register_hz(&cksrc_puv3_oscr, CLOCK_TICK_RATE);
  83. clockevents_register_device(&ckevt_puv3_osmr0);
  84. }
  85. #ifdef CONFIG_PM
  86. unsigned long osmr[4], oier;
  87. void puv3_timer_suspend(void)
  88. {
  89. osmr[0] = readl(OST_OSMR0);
  90. osmr[1] = readl(OST_OSMR1);
  91. osmr[2] = readl(OST_OSMR2);
  92. osmr[3] = readl(OST_OSMR3);
  93. oier = readl(OST_OIER);
  94. }
  95. void puv3_timer_resume(void)
  96. {
  97. writel(0, OST_OSSR);
  98. writel(osmr[0], OST_OSMR0);
  99. writel(osmr[1], OST_OSMR1);
  100. writel(osmr[2], OST_OSMR2);
  101. writel(osmr[3], OST_OSMR3);
  102. writel(oier, OST_OIER);
  103. /*
  104. * OSMR0 is the system timer: make sure OSCR is sufficiently behind
  105. */
  106. writel(readl(OST_OSMR0) - LATCH, OST_OSCR);
  107. }
  108. #else
  109. void puv3_timer_suspend(void) { };
  110. void puv3_timer_resume(void) { };
  111. #endif