time.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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 u64 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 (!clockevent_state_periodic(cd))
  50. jz4740_timer_disable(TIMER_CLOCKEVENT);
  51. cd->event_handler(cd);
  52. return IRQ_HANDLED;
  53. }
  54. static int jz4740_clockevent_set_periodic(struct clock_event_device *evt)
  55. {
  56. jz4740_timer_set_count(TIMER_CLOCKEVENT, 0);
  57. jz4740_timer_set_period(TIMER_CLOCKEVENT, jz4740_jiffies_per_tick);
  58. jz4740_timer_irq_full_enable(TIMER_CLOCKEVENT);
  59. jz4740_timer_enable(TIMER_CLOCKEVENT);
  60. return 0;
  61. }
  62. static int jz4740_clockevent_resume(struct clock_event_device *evt)
  63. {
  64. jz4740_timer_irq_full_enable(TIMER_CLOCKEVENT);
  65. jz4740_timer_enable(TIMER_CLOCKEVENT);
  66. return 0;
  67. }
  68. static int jz4740_clockevent_shutdown(struct clock_event_device *evt)
  69. {
  70. jz4740_timer_disable(TIMER_CLOCKEVENT);
  71. return 0;
  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_state_shutdown = jz4740_clockevent_shutdown,
  86. .set_state_periodic = jz4740_clockevent_set_periodic,
  87. .set_state_oneshot = jz4740_clockevent_shutdown,
  88. .tick_resume = jz4740_clockevent_resume,
  89. .rating = 200,
  90. #ifdef CONFIG_MACH_JZ4740
  91. .irq = JZ4740_IRQ_TCU0,
  92. #endif
  93. #if defined(CONFIG_MACH_JZ4770) || defined(CONFIG_MACH_JZ4780)
  94. .irq = JZ4780_IRQ_TCU2,
  95. #endif
  96. };
  97. static struct irqaction timer_irqaction = {
  98. .handler = jz4740_clockevent_irq,
  99. .flags = IRQF_PERCPU | IRQF_TIMER,
  100. .name = "jz4740-timerirq",
  101. .dev_id = &jz4740_clockevent,
  102. };
  103. void __init plat_time_init(void)
  104. {
  105. int ret;
  106. uint32_t clk_rate;
  107. uint16_t ctrl;
  108. struct clk *ext_clk;
  109. of_clk_init(NULL);
  110. jz4740_timer_init();
  111. ext_clk = clk_get(NULL, "ext");
  112. if (IS_ERR(ext_clk))
  113. panic("unable to get ext clock");
  114. clk_rate = clk_get_rate(ext_clk) >> 4;
  115. clk_put(ext_clk);
  116. jz4740_jiffies_per_tick = DIV_ROUND_CLOSEST(clk_rate, HZ);
  117. clockevent_set_clock(&jz4740_clockevent, clk_rate);
  118. jz4740_clockevent.min_delta_ns = clockevent_delta2ns(100, &jz4740_clockevent);
  119. jz4740_clockevent.min_delta_ticks = 100;
  120. jz4740_clockevent.max_delta_ns = clockevent_delta2ns(0xffff, &jz4740_clockevent);
  121. jz4740_clockevent.max_delta_ticks = 0xffff;
  122. jz4740_clockevent.cpumask = cpumask_of(0);
  123. clockevents_register_device(&jz4740_clockevent);
  124. ret = clocksource_register_hz(&jz4740_clocksource, clk_rate);
  125. if (ret)
  126. printk(KERN_ERR "Failed to register clocksource: %d\n", ret);
  127. sched_clock_register(jz4740_read_sched_clock, 16, clk_rate);
  128. setup_irq(jz4740_clockevent.irq, &timer_irqaction);
  129. ctrl = JZ_TIMER_CTRL_PRESCALE_16 | JZ_TIMER_CTRL_SRC_EXT;
  130. jz4740_timer_set_ctrl(TIMER_CLOCKEVENT, ctrl);
  131. jz4740_timer_set_ctrl(TIMER_CLOCKSOURCE, ctrl);
  132. jz4740_timer_set_period(TIMER_CLOCKEVENT, jz4740_jiffies_per_tick);
  133. jz4740_timer_irq_full_enable(TIMER_CLOCKEVENT);
  134. jz4740_timer_set_period(TIMER_CLOCKSOURCE, 0xffff);
  135. jz4740_timer_enable(TIMER_CLOCKEVENT);
  136. jz4740_timer_enable(TIMER_CLOCKSOURCE);
  137. }