time-ts.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /*
  2. * linux/arch/kernel/time-ts.c
  3. *
  4. * Based on arm clockevents implementation and old bfin time tick.
  5. *
  6. * Copyright(C) 2008, GeoTechnologies, Vitja Makarov
  7. *
  8. * This code is licenced under the GPL version 2. For details see
  9. * kernel-base/COPYING.
  10. */
  11. #include <linux/module.h>
  12. #include <linux/profile.h>
  13. #include <linux/interrupt.h>
  14. #include <linux/time.h>
  15. #include <linux/irq.h>
  16. #include <linux/clocksource.h>
  17. #include <linux/clockchips.h>
  18. #include <asm/blackfin.h>
  19. #ifdef CONFIG_CYCLES_CLOCKSOURCE
  20. static unsigned long cyc2ns_scale;
  21. #define CYC2NS_SCALE_FACTOR 10 /* 2^10, carefully chosen */
  22. static inline void set_cyc2ns_scale(unsigned long cpu_khz)
  23. {
  24. cyc2ns_scale = (1000000 << CYC2NS_SCALE_FACTOR) / cpu_khz;
  25. }
  26. static inline unsigned long long cycles_2_ns(cycle_t cyc)
  27. {
  28. return (cyc * cyc2ns_scale) >> CYC2NS_SCALE_FACTOR;
  29. }
  30. static cycle_t read_cycles(void)
  31. {
  32. unsigned long tmp, tmp2;
  33. asm("%0 = cycles; %1 = cycles2;" : "=d"(tmp), "=d"(tmp2));
  34. return tmp | ((cycle_t)tmp2 << 32);
  35. }
  36. unsigned long long sched_clock(void)
  37. {
  38. return cycles_2_ns(read_cycles());
  39. }
  40. static struct clocksource clocksource_bfin = {
  41. .name = "bfin_cycles",
  42. .rating = 350,
  43. .read = read_cycles,
  44. .mask = CLOCKSOURCE_MASK(64),
  45. .shift = 22,
  46. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  47. };
  48. static int __init bfin_clocksource_init(void)
  49. {
  50. set_cyc2ns_scale(get_cclk() / 1000);
  51. clocksource_bfin.mult = clocksource_hz2mult(get_cclk(), clocksource_bfin.shift);
  52. if (clocksource_register(&clocksource_bfin))
  53. panic("failed to register clocksource");
  54. return 0;
  55. }
  56. #else
  57. # define bfin_clocksource_init()
  58. #endif
  59. static int bfin_timer_set_next_event(unsigned long cycles,
  60. struct clock_event_device *evt)
  61. {
  62. bfin_write_TCOUNT(cycles);
  63. CSYNC();
  64. return 0;
  65. }
  66. static void bfin_timer_set_mode(enum clock_event_mode mode,
  67. struct clock_event_device *evt)
  68. {
  69. switch (mode) {
  70. case CLOCK_EVT_MODE_PERIODIC: {
  71. unsigned long tcount = ((get_cclk() / (HZ * 1)) - 1);
  72. bfin_write_TCNTL(TMPWR);
  73. CSYNC();
  74. bfin_write_TPERIOD(tcount);
  75. bfin_write_TCOUNT(tcount);
  76. bfin_write_TCNTL(TMPWR | TMREN | TAUTORLD);
  77. CSYNC();
  78. break;
  79. }
  80. case CLOCK_EVT_MODE_ONESHOT:
  81. bfin_write_TCOUNT(0);
  82. bfin_write_TCNTL(TMPWR | TMREN);
  83. CSYNC();
  84. break;
  85. case CLOCK_EVT_MODE_UNUSED:
  86. case CLOCK_EVT_MODE_SHUTDOWN:
  87. bfin_write_TCNTL(0);
  88. CSYNC();
  89. break;
  90. case CLOCK_EVT_MODE_RESUME:
  91. break;
  92. }
  93. }
  94. static void __init bfin_timer_init(void)
  95. {
  96. /* power up the timer, but don't enable it just yet */
  97. bfin_write_TCNTL(TMPWR);
  98. CSYNC();
  99. /*
  100. * the TSCALE prescaler counter.
  101. */
  102. bfin_write_TSCALE(0);
  103. bfin_write_TPERIOD(0);
  104. bfin_write_TCOUNT(0);
  105. /* now enable the timer */
  106. CSYNC();
  107. }
  108. /*
  109. * timer_interrupt() needs to keep up the real-time clock,
  110. * as well as call the "do_timer()" routine every clocktick
  111. */
  112. #ifdef CONFIG_CORE_TIMER_IRQ_L1
  113. __attribute__((l1_text))
  114. #endif
  115. irqreturn_t timer_interrupt(int irq, void *dev_id);
  116. static struct clock_event_device clockevent_bfin = {
  117. .name = "bfin_core_timer",
  118. .features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT,
  119. .shift = 32,
  120. .cpumask = CPU_MASK_CPU0,
  121. .set_next_event = bfin_timer_set_next_event,
  122. .set_mode = bfin_timer_set_mode,
  123. };
  124. static struct irqaction bfin_timer_irq = {
  125. .name = "Blackfin Core Timer",
  126. .flags = IRQF_DISABLED | IRQF_TIMER | IRQF_IRQPOLL,
  127. .handler = timer_interrupt,
  128. .dev_id = &clockevent_bfin,
  129. };
  130. irqreturn_t timer_interrupt(int irq, void *dev_id)
  131. {
  132. struct clock_event_device *evt = dev_id;
  133. evt->event_handler(evt);
  134. return IRQ_HANDLED;
  135. }
  136. static int __init bfin_clockevent_init(void)
  137. {
  138. setup_irq(IRQ_CORETMR, &bfin_timer_irq);
  139. bfin_timer_init();
  140. clockevent_bfin.mult = div_sc(get_cclk(), NSEC_PER_SEC, clockevent_bfin.shift);
  141. clockevent_bfin.max_delta_ns = clockevent_delta2ns(-1, &clockevent_bfin);
  142. clockevent_bfin.min_delta_ns = clockevent_delta2ns(100, &clockevent_bfin);
  143. clockevents_register_device(&clockevent_bfin);
  144. return 0;
  145. }
  146. void __init time_init(void)
  147. {
  148. time_t secs_since_1970 = (365 * 37 + 9) * 24 * 60 * 60; /* 1 Jan 2007 */
  149. #ifdef CONFIG_RTC_DRV_BFIN
  150. /* [#2663] hack to filter junk RTC values that would cause
  151. * userspace to have to deal with time values greater than
  152. * 2^31 seconds (which uClibc cannot cope with yet)
  153. */
  154. if ((bfin_read_RTC_STAT() & 0xC0000000) == 0xC0000000) {
  155. printk(KERN_NOTICE "bfin-rtc: invalid date; resetting\n");
  156. bfin_write_RTC_STAT(0);
  157. }
  158. #endif
  159. /* Initialize xtime. From now on, xtime is updated with timer interrupts */
  160. xtime.tv_sec = secs_since_1970;
  161. xtime.tv_nsec = 0;
  162. set_normalized_timespec(&wall_to_monotonic, -xtime.tv_sec, -xtime.tv_nsec);
  163. bfin_clocksource_init();
  164. bfin_clockevent_init();
  165. }