time.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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/clk.h>
  16. #include <linux/clk-provider.h>
  17. #include <linux/interrupt.h>
  18. #include <linux/kernel.h>
  19. #include <linux/time.h>
  20. #include <linux/clockchips.h>
  21. #include <linux/sched_clock.h>
  22. #include <asm/mach-jz4740/clock.h>
  23. #include <asm/mach-jz4740/irq.h>
  24. #include <asm/mach-jz4740/timer.h>
  25. #include <asm/time.h>
  26. #include "clock.h"
  27. #define TIMER_CLOCKEVENT 0
  28. #define TIMER_CLOCKSOURCE 1
  29. static uint16_t jz4740_jiffies_per_tick;
  30. static cycle_t jz4740_clocksource_read(struct clocksource *cs)
  31. {
  32. return jz4740_timer_get_count(TIMER_CLOCKSOURCE);
  33. }
  34. static struct clocksource jz4740_clocksource = {
  35. .name = "jz4740-timer",
  36. .rating = 200,
  37. .read = jz4740_clocksource_read,
  38. .mask = CLOCKSOURCE_MASK(16),
  39. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  40. };
  41. static u64 notrace jz4740_read_sched_clock(void)
  42. {
  43. return jz4740_timer_get_count(TIMER_CLOCKSOURCE);
  44. }
  45. static irqreturn_t jz4740_clockevent_irq(int irq, void *devid)
  46. {
  47. struct clock_event_device *cd = devid;
  48. jz4740_timer_ack_full(TIMER_CLOCKEVENT);
  49. if (cd->mode != CLOCK_EVT_MODE_PERIODIC)
  50. jz4740_timer_disable(TIMER_CLOCKEVENT);
  51. cd->event_handler(cd);
  52. return IRQ_HANDLED;
  53. }
  54. static void jz4740_clockevent_set_mode(enum clock_event_mode mode,
  55. struct clock_event_device *cd)
  56. {
  57. switch (mode) {
  58. case CLOCK_EVT_MODE_PERIODIC:
  59. jz4740_timer_set_count(TIMER_CLOCKEVENT, 0);
  60. jz4740_timer_set_period(TIMER_CLOCKEVENT, jz4740_jiffies_per_tick);
  61. case CLOCK_EVT_MODE_RESUME:
  62. jz4740_timer_irq_full_enable(TIMER_CLOCKEVENT);
  63. jz4740_timer_enable(TIMER_CLOCKEVENT);
  64. break;
  65. case CLOCK_EVT_MODE_ONESHOT:
  66. case CLOCK_EVT_MODE_SHUTDOWN:
  67. jz4740_timer_disable(TIMER_CLOCKEVENT);
  68. break;
  69. default:
  70. break;
  71. }
  72. }
  73. static int jz4740_clockevent_set_next(unsigned long evt,
  74. struct clock_event_device *cd)
  75. {
  76. jz4740_timer_set_count(TIMER_CLOCKEVENT, 0);
  77. jz4740_timer_set_period(TIMER_CLOCKEVENT, evt);
  78. jz4740_timer_enable(TIMER_CLOCKEVENT);
  79. return 0;
  80. }
  81. static struct clock_event_device jz4740_clockevent = {
  82. .name = "jz4740-timer",
  83. .features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT,
  84. .set_next_event = jz4740_clockevent_set_next,
  85. .set_mode = jz4740_clockevent_set_mode,
  86. .rating = 200,
  87. #ifdef CONFIG_MACH_JZ4740
  88. .irq = JZ4740_IRQ_TCU0,
  89. #endif
  90. #ifdef CONFIG_MACH_JZ4780
  91. .irq = JZ4780_IRQ_TCU2,
  92. #endif
  93. };
  94. static struct irqaction timer_irqaction = {
  95. .handler = jz4740_clockevent_irq,
  96. .flags = IRQF_PERCPU | IRQF_TIMER,
  97. .name = "jz4740-timerirq",
  98. .dev_id = &jz4740_clockevent,
  99. };
  100. void __init plat_time_init(void)
  101. {
  102. int ret;
  103. uint32_t clk_rate;
  104. uint16_t ctrl;
  105. struct clk *ext_clk;
  106. of_clk_init(NULL);
  107. jz4740_timer_init();
  108. ext_clk = clk_get(NULL, "ext");
  109. if (IS_ERR(ext_clk))
  110. panic("unable to get ext clock");
  111. clk_rate = clk_get_rate(ext_clk) >> 4;
  112. clk_put(ext_clk);
  113. jz4740_jiffies_per_tick = DIV_ROUND_CLOSEST(clk_rate, HZ);
  114. clockevent_set_clock(&jz4740_clockevent, clk_rate);
  115. jz4740_clockevent.min_delta_ns = clockevent_delta2ns(100, &jz4740_clockevent);
  116. jz4740_clockevent.max_delta_ns = clockevent_delta2ns(0xffff, &jz4740_clockevent);
  117. jz4740_clockevent.cpumask = cpumask_of(0);
  118. clockevents_register_device(&jz4740_clockevent);
  119. ret = clocksource_register_hz(&jz4740_clocksource, clk_rate);
  120. if (ret)
  121. printk(KERN_ERR "Failed to register clocksource: %d\n", ret);
  122. sched_clock_register(jz4740_read_sched_clock, 16, clk_rate);
  123. setup_irq(jz4740_clockevent.irq, &timer_irqaction);
  124. ctrl = JZ_TIMER_CTRL_PRESCALE_16 | JZ_TIMER_CTRL_SRC_EXT;
  125. jz4740_timer_set_ctrl(TIMER_CLOCKEVENT, ctrl);
  126. jz4740_timer_set_ctrl(TIMER_CLOCKSOURCE, ctrl);
  127. jz4740_timer_set_period(TIMER_CLOCKEVENT, jz4740_jiffies_per_tick);
  128. jz4740_timer_irq_full_enable(TIMER_CLOCKEVENT);
  129. jz4740_timer_set_period(TIMER_CLOCKSOURCE, 0xffff);
  130. jz4740_timer_enable(TIMER_CLOCKEVENT);
  131. jz4740_timer_enable(TIMER_CLOCKSOURCE);
  132. }