time.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*
  2. * Copyright (C) 2010, Lars-Peter Clausen <lars@metafoo.de>
  3. * JZ4740 platform time support
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License as published by the
  7. * Free Software Foundation; either version 2 of the License, or (at your
  8. * option) any later version.
  9. *
  10. * You should have received a copy of the GNU General Public License along
  11. * with this program; if not, write to the Free Software Foundation, Inc.,
  12. * 675 Mass Ave, Cambridge, MA 02139, USA.
  13. *
  14. */
  15. #include <linux/interrupt.h>
  16. #include <linux/kernel.h>
  17. #include <linux/time.h>
  18. #include <linux/clockchips.h>
  19. #include <asm/mach-jz4740/irq.h>
  20. #include <asm/time.h>
  21. #include "clock.h"
  22. #include "timer.h"
  23. #define TIMER_CLOCKEVENT 0
  24. #define TIMER_CLOCKSOURCE 1
  25. static uint16_t jz4740_jiffies_per_tick;
  26. static cycle_t jz4740_clocksource_read(struct clocksource *cs)
  27. {
  28. return jz4740_timer_get_count(TIMER_CLOCKSOURCE);
  29. }
  30. static struct clocksource jz4740_clocksource = {
  31. .name = "jz4740-timer",
  32. .rating = 200,
  33. .read = jz4740_clocksource_read,
  34. .mask = CLOCKSOURCE_MASK(16),
  35. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  36. };
  37. static irqreturn_t jz4740_clockevent_irq(int irq, void *devid)
  38. {
  39. struct clock_event_device *cd = devid;
  40. jz4740_timer_ack_full(TIMER_CLOCKEVENT);
  41. if (cd->mode != CLOCK_EVT_MODE_PERIODIC)
  42. jz4740_timer_disable(TIMER_CLOCKEVENT);
  43. cd->event_handler(cd);
  44. return IRQ_HANDLED;
  45. }
  46. static void jz4740_clockevent_set_mode(enum clock_event_mode mode,
  47. struct clock_event_device *cd)
  48. {
  49. switch (mode) {
  50. case CLOCK_EVT_MODE_PERIODIC:
  51. jz4740_timer_set_count(TIMER_CLOCKEVENT, 0);
  52. jz4740_timer_set_period(TIMER_CLOCKEVENT, jz4740_jiffies_per_tick);
  53. case CLOCK_EVT_MODE_RESUME:
  54. jz4740_timer_irq_full_enable(TIMER_CLOCKEVENT);
  55. jz4740_timer_enable(TIMER_CLOCKEVENT);
  56. break;
  57. case CLOCK_EVT_MODE_ONESHOT:
  58. case CLOCK_EVT_MODE_SHUTDOWN:
  59. jz4740_timer_disable(TIMER_CLOCKEVENT);
  60. break;
  61. default:
  62. break;
  63. }
  64. }
  65. static int jz4740_clockevent_set_next(unsigned long evt,
  66. struct clock_event_device *cd)
  67. {
  68. jz4740_timer_set_count(TIMER_CLOCKEVENT, 0);
  69. jz4740_timer_set_period(TIMER_CLOCKEVENT, evt);
  70. jz4740_timer_enable(TIMER_CLOCKEVENT);
  71. return 0;
  72. }
  73. static struct clock_event_device jz4740_clockevent = {
  74. .name = "jz4740-timer",
  75. .features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT,
  76. .set_next_event = jz4740_clockevent_set_next,
  77. .set_mode = jz4740_clockevent_set_mode,
  78. .rating = 200,
  79. .irq = JZ4740_IRQ_TCU0,
  80. };
  81. static struct irqaction timer_irqaction = {
  82. .handler = jz4740_clockevent_irq,
  83. .flags = IRQF_PERCPU | IRQF_TIMER,
  84. .name = "jz4740-timerirq",
  85. .dev_id = &jz4740_clockevent,
  86. };
  87. void __init plat_time_init(void)
  88. {
  89. int ret;
  90. uint32_t clk_rate;
  91. uint16_t ctrl;
  92. jz4740_timer_init();
  93. clk_rate = jz4740_clock_bdata.ext_rate >> 4;
  94. jz4740_jiffies_per_tick = DIV_ROUND_CLOSEST(clk_rate, HZ);
  95. clockevent_set_clock(&jz4740_clockevent, clk_rate);
  96. jz4740_clockevent.min_delta_ns = clockevent_delta2ns(100, &jz4740_clockevent);
  97. jz4740_clockevent.max_delta_ns = clockevent_delta2ns(0xffff, &jz4740_clockevent);
  98. jz4740_clockevent.cpumask = cpumask_of(0);
  99. clockevents_register_device(&jz4740_clockevent);
  100. ret = clocksource_register_hz(&jz4740_clocksource, clk_rate);
  101. if (ret)
  102. printk(KERN_ERR "Failed to register clocksource: %d\n", ret);
  103. setup_irq(JZ4740_IRQ_TCU0, &timer_irqaction);
  104. ctrl = JZ_TIMER_CTRL_PRESCALE_16 | JZ_TIMER_CTRL_SRC_EXT;
  105. jz4740_timer_set_ctrl(TIMER_CLOCKEVENT, ctrl);
  106. jz4740_timer_set_ctrl(TIMER_CLOCKSOURCE, ctrl);
  107. jz4740_timer_set_period(TIMER_CLOCKEVENT, jz4740_jiffies_per_tick);
  108. jz4740_timer_irq_full_enable(TIMER_CLOCKEVENT);
  109. jz4740_timer_set_period(TIMER_CLOCKSOURCE, 0xffff);
  110. jz4740_timer_enable(TIMER_CLOCKEVENT);
  111. jz4740_timer_enable(TIMER_CLOCKSOURCE);
  112. }