time.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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/uaccess.h>
  27. #include <asm/mach/irq.h>
  28. #include <asm/mach/time.h>
  29. #include <mach/time.h>
  30. /*
  31. * Minimum clocksource/clockevent timer range in seconds
  32. */
  33. #define IOP_MIN_RANGE 4
  34. /*
  35. * IOP clocksource (free-running timer 1).
  36. */
  37. static cycle_t notrace iop_clocksource_read(struct clocksource *unused)
  38. {
  39. return 0xffffffffu - read_tcr1();
  40. }
  41. static struct clocksource iop_clocksource = {
  42. .name = "iop_timer1",
  43. .rating = 300,
  44. .read = iop_clocksource_read,
  45. .mask = CLOCKSOURCE_MASK(32),
  46. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  47. };
  48. /*
  49. * IOP sched_clock() implementation via its clocksource.
  50. */
  51. unsigned long long sched_clock(void)
  52. {
  53. cycle_t cyc = iop_clocksource_read(NULL);
  54. struct clocksource *cs = &iop_clocksource;
  55. return clocksource_cyc2ns(cyc, cs->mult, cs->shift);
  56. }
  57. /*
  58. * IOP clockevents (interrupting timer 0).
  59. */
  60. static int iop_set_next_event(unsigned long delta,
  61. struct clock_event_device *unused)
  62. {
  63. u32 tmr = IOP_TMR_PRIVILEGED | IOP_TMR_RATIO_1_1;
  64. BUG_ON(delta == 0);
  65. write_tmr0(tmr & ~(IOP_TMR_EN | IOP_TMR_RELOAD));
  66. write_tcr0(delta);
  67. write_tmr0((tmr & ~IOP_TMR_RELOAD) | IOP_TMR_EN);
  68. return 0;
  69. }
  70. static unsigned long ticks_per_jiffy;
  71. static void iop_set_mode(enum clock_event_mode mode,
  72. struct clock_event_device *unused)
  73. {
  74. u32 tmr = read_tmr0();
  75. switch (mode) {
  76. case CLOCK_EVT_MODE_PERIODIC:
  77. write_tmr0(tmr & ~IOP_TMR_EN);
  78. write_tcr0(ticks_per_jiffy - 1);
  79. tmr |= (IOP_TMR_RELOAD | IOP_TMR_EN);
  80. break;
  81. case CLOCK_EVT_MODE_ONESHOT:
  82. /* ->set_next_event sets period and enables timer */
  83. tmr &= ~(IOP_TMR_RELOAD | IOP_TMR_EN);
  84. break;
  85. case CLOCK_EVT_MODE_RESUME:
  86. tmr |= IOP_TMR_EN;
  87. break;
  88. case CLOCK_EVT_MODE_SHUTDOWN:
  89. case CLOCK_EVT_MODE_UNUSED:
  90. default:
  91. tmr &= ~IOP_TMR_EN;
  92. break;
  93. }
  94. write_tmr0(tmr);
  95. }
  96. static struct clock_event_device iop_clockevent = {
  97. .name = "iop_timer0",
  98. .features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT,
  99. .rating = 300,
  100. .set_next_event = iop_set_next_event,
  101. .set_mode = iop_set_mode,
  102. };
  103. static irqreturn_t
  104. iop_timer_interrupt(int irq, void *dev_id)
  105. {
  106. struct clock_event_device *evt = dev_id;
  107. write_tisr(1);
  108. evt->event_handler(evt);
  109. return IRQ_HANDLED;
  110. }
  111. static struct irqaction iop_timer_irq = {
  112. .name = "IOP Timer Tick",
  113. .handler = iop_timer_interrupt,
  114. .flags = IRQF_DISABLED | IRQF_TIMER | IRQF_IRQPOLL,
  115. .dev_id = &iop_clockevent,
  116. };
  117. static unsigned long iop_tick_rate;
  118. unsigned long get_iop_tick_rate(void)
  119. {
  120. return iop_tick_rate;
  121. }
  122. EXPORT_SYMBOL(get_iop_tick_rate);
  123. void __init iop_init_time(unsigned long tick_rate)
  124. {
  125. u32 timer_ctl;
  126. ticks_per_jiffy = DIV_ROUND_CLOSEST(tick_rate, HZ);
  127. iop_tick_rate = tick_rate;
  128. timer_ctl = IOP_TMR_EN | IOP_TMR_PRIVILEGED |
  129. IOP_TMR_RELOAD | IOP_TMR_RATIO_1_1;
  130. /*
  131. * Set up interrupting clockevent timer 0.
  132. */
  133. write_tmr0(timer_ctl & ~IOP_TMR_EN);
  134. setup_irq(IRQ_IOP_TIMER0, &iop_timer_irq);
  135. clockevents_calc_mult_shift(&iop_clockevent,
  136. tick_rate, IOP_MIN_RANGE);
  137. iop_clockevent.max_delta_ns =
  138. clockevent_delta2ns(0xfffffffe, &iop_clockevent);
  139. iop_clockevent.min_delta_ns =
  140. clockevent_delta2ns(0xf, &iop_clockevent);
  141. iop_clockevent.cpumask = cpumask_of(0);
  142. clockevents_register_device(&iop_clockevent);
  143. write_trr0(ticks_per_jiffy - 1);
  144. write_tcr0(ticks_per_jiffy - 1);
  145. write_tmr0(timer_ctl);
  146. /*
  147. * Set up free-running clocksource timer 1.
  148. */
  149. write_trr1(0xffffffff);
  150. write_tcr1(0xffffffff);
  151. write_tmr1(timer_ctl);
  152. clocksource_calc_mult_shift(&iop_clocksource, tick_rate,
  153. IOP_MIN_RANGE);
  154. clocksource_register(&iop_clocksource);
  155. }