time.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /*
  2. * arch/arm/plat-iop/time.c
  3. *
  4. * Timer code for IOP32x and IOP33x based systems
  5. *
  6. * Author: Deepak Saxena <dsaxena@mvista.com>
  7. *
  8. * Copyright 2002-2003 MontaVista Software Inc.
  9. *
  10. * This program is free software; you can redistribute it and/or modify it
  11. * under the terms of the GNU General Public License as published by the
  12. * Free Software Foundation; either version 2 of the License, or (at your
  13. * option) any later version.
  14. */
  15. #include <linux/kernel.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/time.h>
  18. #include <linux/init.h>
  19. #include <linux/timex.h>
  20. #include <linux/sched.h>
  21. #include <linux/io.h>
  22. #include <linux/clocksource.h>
  23. #include <linux/clockchips.h>
  24. #include <mach/hardware.h>
  25. #include <asm/irq.h>
  26. #include <asm/sched_clock.h>
  27. #include <asm/uaccess.h>
  28. #include <asm/mach/irq.h>
  29. #include <asm/mach/time.h>
  30. #include <mach/time.h>
  31. /*
  32. * Minimum clocksource/clockevent timer range in seconds
  33. */
  34. #define IOP_MIN_RANGE 4
  35. /*
  36. * IOP clocksource (free-running timer 1).
  37. */
  38. static cycle_t notrace iop_clocksource_read(struct clocksource *unused)
  39. {
  40. return 0xffffffffu - read_tcr1();
  41. }
  42. static struct clocksource iop_clocksource = {
  43. .name = "iop_timer1",
  44. .rating = 300,
  45. .read = iop_clocksource_read,
  46. .mask = CLOCKSOURCE_MASK(32),
  47. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  48. };
  49. static DEFINE_CLOCK_DATA(cd);
  50. /*
  51. * IOP sched_clock() implementation via its clocksource.
  52. */
  53. unsigned long long notrace sched_clock(void)
  54. {
  55. u32 cyc = 0xffffffffu - read_tcr1();
  56. return cyc_to_sched_clock(&cd, cyc, (u32)~0);
  57. }
  58. static void notrace iop_update_sched_clock(void)
  59. {
  60. u32 cyc = 0xffffffffu - read_tcr1();
  61. update_sched_clock(&cd, cyc, (u32)~0);
  62. }
  63. /*
  64. * IOP clockevents (interrupting timer 0).
  65. */
  66. static int iop_set_next_event(unsigned long delta,
  67. struct clock_event_device *unused)
  68. {
  69. u32 tmr = IOP_TMR_PRIVILEGED | IOP_TMR_RATIO_1_1;
  70. BUG_ON(delta == 0);
  71. write_tmr0(tmr & ~(IOP_TMR_EN | IOP_TMR_RELOAD));
  72. write_tcr0(delta);
  73. write_tmr0((tmr & ~IOP_TMR_RELOAD) | IOP_TMR_EN);
  74. return 0;
  75. }
  76. static unsigned long ticks_per_jiffy;
  77. static void iop_set_mode(enum clock_event_mode mode,
  78. struct clock_event_device *unused)
  79. {
  80. u32 tmr = read_tmr0();
  81. switch (mode) {
  82. case CLOCK_EVT_MODE_PERIODIC:
  83. write_tmr0(tmr & ~IOP_TMR_EN);
  84. write_tcr0(ticks_per_jiffy - 1);
  85. write_trr0(ticks_per_jiffy - 1);
  86. tmr |= (IOP_TMR_RELOAD | IOP_TMR_EN);
  87. break;
  88. case CLOCK_EVT_MODE_ONESHOT:
  89. /* ->set_next_event sets period and enables timer */
  90. tmr &= ~(IOP_TMR_RELOAD | IOP_TMR_EN);
  91. break;
  92. case CLOCK_EVT_MODE_RESUME:
  93. tmr |= IOP_TMR_EN;
  94. break;
  95. case CLOCK_EVT_MODE_SHUTDOWN:
  96. case CLOCK_EVT_MODE_UNUSED:
  97. default:
  98. tmr &= ~IOP_TMR_EN;
  99. break;
  100. }
  101. write_tmr0(tmr);
  102. }
  103. static struct clock_event_device iop_clockevent = {
  104. .name = "iop_timer0",
  105. .features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT,
  106. .rating = 300,
  107. .set_next_event = iop_set_next_event,
  108. .set_mode = iop_set_mode,
  109. };
  110. static irqreturn_t
  111. iop_timer_interrupt(int irq, void *dev_id)
  112. {
  113. struct clock_event_device *evt = dev_id;
  114. write_tisr(1);
  115. evt->event_handler(evt);
  116. return IRQ_HANDLED;
  117. }
  118. static struct irqaction iop_timer_irq = {
  119. .name = "IOP Timer Tick",
  120. .handler = iop_timer_interrupt,
  121. .flags = IRQF_DISABLED | IRQF_TIMER | IRQF_IRQPOLL,
  122. .dev_id = &iop_clockevent,
  123. };
  124. static unsigned long iop_tick_rate;
  125. unsigned long get_iop_tick_rate(void)
  126. {
  127. return iop_tick_rate;
  128. }
  129. EXPORT_SYMBOL(get_iop_tick_rate);
  130. void __init iop_init_time(unsigned long tick_rate)
  131. {
  132. u32 timer_ctl;
  133. init_sched_clock(&cd, iop_update_sched_clock, 32, tick_rate);
  134. ticks_per_jiffy = DIV_ROUND_CLOSEST(tick_rate, HZ);
  135. iop_tick_rate = tick_rate;
  136. timer_ctl = IOP_TMR_EN | IOP_TMR_PRIVILEGED |
  137. IOP_TMR_RELOAD | IOP_TMR_RATIO_1_1;
  138. /*
  139. * Set up interrupting clockevent timer 0.
  140. */
  141. write_tmr0(timer_ctl & ~IOP_TMR_EN);
  142. write_tisr(1);
  143. setup_irq(IRQ_IOP_TIMER0, &iop_timer_irq);
  144. clockevents_calc_mult_shift(&iop_clockevent,
  145. tick_rate, IOP_MIN_RANGE);
  146. iop_clockevent.max_delta_ns =
  147. clockevent_delta2ns(0xfffffffe, &iop_clockevent);
  148. iop_clockevent.min_delta_ns =
  149. clockevent_delta2ns(0xf, &iop_clockevent);
  150. iop_clockevent.cpumask = cpumask_of(0);
  151. clockevents_register_device(&iop_clockevent);
  152. /*
  153. * Set up free-running clocksource timer 1.
  154. */
  155. write_trr1(0xffffffff);
  156. write_tcr1(0xffffffff);
  157. write_tmr1(timer_ctl);
  158. clocksource_register_hz(&iop_clocksource, tick_rate);
  159. }