time.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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/io.h>
  21. #include <linux/clocksource.h>
  22. #include <linux/clockchips.h>
  23. #include <mach/hardware.h>
  24. #include <asm/irq.h>
  25. #include <asm/uaccess.h>
  26. #include <asm/mach/irq.h>
  27. #include <asm/mach/time.h>
  28. #include <mach/time.h>
  29. /*
  30. * IOP clocksource (free-running timer 1).
  31. */
  32. static cycle_t iop_clocksource_read(struct clocksource *unused)
  33. {
  34. return 0xffffffffu - read_tcr1();
  35. }
  36. static struct clocksource iop_clocksource = {
  37. .name = "iop_timer1",
  38. .rating = 300,
  39. .read = iop_clocksource_read,
  40. .mask = CLOCKSOURCE_MASK(32),
  41. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  42. };
  43. static void __init iop_clocksource_set_hz(struct clocksource *cs, unsigned int hz)
  44. {
  45. u64 temp;
  46. u32 shift;
  47. /* Find shift and mult values for hz. */
  48. shift = 32;
  49. do {
  50. temp = (u64) NSEC_PER_SEC << shift;
  51. do_div(temp, hz);
  52. if ((temp >> 32) == 0)
  53. break;
  54. } while (--shift != 0);
  55. cs->shift = shift;
  56. cs->mult = (u32) temp;
  57. printk(KERN_INFO "clocksource: %s uses shift %u mult %#x\n",
  58. cs->name, cs->shift, cs->mult);
  59. }
  60. /*
  61. * IOP clockevents (interrupting timer 0).
  62. */
  63. static int iop_set_next_event(unsigned long delta,
  64. struct clock_event_device *unused)
  65. {
  66. u32 tmr = IOP_TMR_PRIVILEGED | IOP_TMR_RATIO_1_1;
  67. BUG_ON(delta == 0);
  68. write_tmr0(tmr & ~(IOP_TMR_EN | IOP_TMR_RELOAD));
  69. write_tcr0(delta);
  70. write_tmr0((tmr & ~IOP_TMR_RELOAD) | IOP_TMR_EN);
  71. return 0;
  72. }
  73. static unsigned long ticks_per_jiffy;
  74. static void iop_set_mode(enum clock_event_mode mode,
  75. struct clock_event_device *unused)
  76. {
  77. u32 tmr = read_tmr0();
  78. switch (mode) {
  79. case CLOCK_EVT_MODE_PERIODIC:
  80. write_tmr0(tmr & ~IOP_TMR_EN);
  81. write_tcr0(ticks_per_jiffy - 1);
  82. tmr |= (IOP_TMR_RELOAD | IOP_TMR_EN);
  83. break;
  84. case CLOCK_EVT_MODE_ONESHOT:
  85. /* ->set_next_event sets period and enables timer */
  86. tmr &= ~(IOP_TMR_RELOAD | IOP_TMR_EN);
  87. break;
  88. case CLOCK_EVT_MODE_RESUME:
  89. tmr |= IOP_TMR_EN;
  90. break;
  91. case CLOCK_EVT_MODE_SHUTDOWN:
  92. case CLOCK_EVT_MODE_UNUSED:
  93. default:
  94. tmr &= ~IOP_TMR_EN;
  95. break;
  96. }
  97. write_tmr0(tmr);
  98. }
  99. static struct clock_event_device iop_clockevent = {
  100. .name = "iop_timer0",
  101. .features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT,
  102. .rating = 300,
  103. .set_next_event = iop_set_next_event,
  104. .set_mode = iop_set_mode,
  105. };
  106. static void __init iop_clockevent_set_hz(struct clock_event_device *ce, unsigned int hz)
  107. {
  108. u64 temp;
  109. u32 shift;
  110. /* Find shift and mult values for hz. */
  111. shift = 32;
  112. do {
  113. temp = (u64) hz << shift;
  114. do_div(temp, NSEC_PER_SEC);
  115. if ((temp >> 32) == 0)
  116. break;
  117. } while (--shift != 0);
  118. ce->shift = shift;
  119. ce->mult = (u32) temp;
  120. printk(KERN_INFO "clockevent: %s uses shift %u mult %#lx\n",
  121. ce->name, ce->shift, ce->mult);
  122. }
  123. static irqreturn_t
  124. iop_timer_interrupt(int irq, void *dev_id)
  125. {
  126. struct clock_event_device *evt = dev_id;
  127. write_tisr(1);
  128. evt->event_handler(evt);
  129. return IRQ_HANDLED;
  130. }
  131. static struct irqaction iop_timer_irq = {
  132. .name = "IOP Timer Tick",
  133. .handler = iop_timer_interrupt,
  134. .flags = IRQF_DISABLED | IRQF_TIMER | IRQF_IRQPOLL,
  135. .dev_id = &iop_clockevent,
  136. };
  137. static unsigned long iop_tick_rate;
  138. unsigned long get_iop_tick_rate(void)
  139. {
  140. return iop_tick_rate;
  141. }
  142. EXPORT_SYMBOL(get_iop_tick_rate);
  143. void __init iop_init_time(unsigned long tick_rate)
  144. {
  145. u32 timer_ctl;
  146. ticks_per_jiffy = DIV_ROUND_CLOSEST(tick_rate, HZ);
  147. iop_tick_rate = tick_rate;
  148. timer_ctl = IOP_TMR_EN | IOP_TMR_PRIVILEGED |
  149. IOP_TMR_RELOAD | IOP_TMR_RATIO_1_1;
  150. /*
  151. * Set up interrupting clockevent timer 0.
  152. */
  153. write_tmr0(timer_ctl & ~IOP_TMR_EN);
  154. setup_irq(IRQ_IOP_TIMER0, &iop_timer_irq);
  155. iop_clockevent_set_hz(&iop_clockevent, tick_rate);
  156. iop_clockevent.max_delta_ns =
  157. clockevent_delta2ns(0xfffffffe, &iop_clockevent);
  158. iop_clockevent.min_delta_ns =
  159. clockevent_delta2ns(0xf, &iop_clockevent);
  160. iop_clockevent.cpumask = cpumask_of(0);
  161. clockevents_register_device(&iop_clockevent);
  162. write_trr0(ticks_per_jiffy - 1);
  163. write_tcr0(ticks_per_jiffy - 1);
  164. write_tmr0(timer_ctl);
  165. /*
  166. * Set up free-running clocksource timer 1.
  167. */
  168. write_trr1(0xffffffff);
  169. write_tcr1(0xffffffff);
  170. write_tmr1(timer_ctl);
  171. iop_clocksource_set_hz(&iop_clocksource, tick_rate);
  172. clocksource_register(&iop_clocksource);
  173. }