time-ts.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. /*
  2. * Based on arm clockevents implementation and old bfin time tick.
  3. *
  4. * Copyright 2008-2009 Analog Devics Inc.
  5. * 2008 GeoTechnologies
  6. * Vitja Makarov
  7. *
  8. * Licensed under the GPL-2
  9. */
  10. #include <linux/module.h>
  11. #include <linux/profile.h>
  12. #include <linux/interrupt.h>
  13. #include <linux/time.h>
  14. #include <linux/timex.h>
  15. #include <linux/irq.h>
  16. #include <linux/clocksource.h>
  17. #include <linux/clockchips.h>
  18. #include <linux/cpufreq.h>
  19. #include <asm/blackfin.h>
  20. #include <asm/time.h>
  21. #include <asm/gptimers.h>
  22. #include <asm/nmi.h>
  23. /* Accelerators for sched_clock()
  24. * convert from cycles(64bits) => nanoseconds (64bits)
  25. * basic equation:
  26. * ns = cycles / (freq / ns_per_sec)
  27. * ns = cycles * (ns_per_sec / freq)
  28. * ns = cycles * (10^9 / (cpu_khz * 10^3))
  29. * ns = cycles * (10^6 / cpu_khz)
  30. *
  31. * Then we use scaling math (suggested by george@mvista.com) to get:
  32. * ns = cycles * (10^6 * SC / cpu_khz) / SC
  33. * ns = cycles * cyc2ns_scale / SC
  34. *
  35. * And since SC is a constant power of two, we can convert the div
  36. * into a shift.
  37. *
  38. * We can use khz divisor instead of mhz to keep a better precision, since
  39. * cyc2ns_scale is limited to 10^6 * 2^10, which fits in 32 bits.
  40. * (mathieu.desnoyers@polymtl.ca)
  41. *
  42. * -johnstul@us.ibm.com "math is hard, lets go shopping!"
  43. */
  44. #define CYC2NS_SCALE_FACTOR 10 /* 2^10, carefully chosen */
  45. #if defined(CONFIG_CYCLES_CLOCKSOURCE)
  46. static notrace cycle_t bfin_read_cycles(struct clocksource *cs)
  47. {
  48. #ifdef CONFIG_CPU_FREQ
  49. return __bfin_cycles_off + (get_cycles() << __bfin_cycles_mod);
  50. #else
  51. return get_cycles();
  52. #endif
  53. }
  54. static struct clocksource bfin_cs_cycles = {
  55. .name = "bfin_cs_cycles",
  56. .rating = 400,
  57. .read = bfin_read_cycles,
  58. .mask = CLOCKSOURCE_MASK(64),
  59. .shift = CYC2NS_SCALE_FACTOR,
  60. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  61. };
  62. static inline unsigned long long bfin_cs_cycles_sched_clock(void)
  63. {
  64. return clocksource_cyc2ns(bfin_read_cycles(&bfin_cs_cycles),
  65. bfin_cs_cycles.mult, bfin_cs_cycles.shift);
  66. }
  67. static int __init bfin_cs_cycles_init(void)
  68. {
  69. bfin_cs_cycles.mult = \
  70. clocksource_hz2mult(get_cclk(), bfin_cs_cycles.shift);
  71. if (clocksource_register(&bfin_cs_cycles))
  72. panic("failed to register clocksource");
  73. return 0;
  74. }
  75. #else
  76. # define bfin_cs_cycles_init()
  77. #endif
  78. #ifdef CONFIG_GPTMR0_CLOCKSOURCE
  79. void __init setup_gptimer0(void)
  80. {
  81. disable_gptimers(TIMER0bit);
  82. set_gptimer_config(TIMER0_id, \
  83. TIMER_OUT_DIS | TIMER_PERIOD_CNT | TIMER_MODE_PWM);
  84. set_gptimer_period(TIMER0_id, -1);
  85. set_gptimer_pwidth(TIMER0_id, -2);
  86. SSYNC();
  87. enable_gptimers(TIMER0bit);
  88. }
  89. static cycle_t bfin_read_gptimer0(struct clocksource *cs)
  90. {
  91. return bfin_read_TIMER0_COUNTER();
  92. }
  93. static struct clocksource bfin_cs_gptimer0 = {
  94. .name = "bfin_cs_gptimer0",
  95. .rating = 350,
  96. .read = bfin_read_gptimer0,
  97. .mask = CLOCKSOURCE_MASK(32),
  98. .shift = CYC2NS_SCALE_FACTOR,
  99. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  100. };
  101. static inline unsigned long long bfin_cs_gptimer0_sched_clock(void)
  102. {
  103. return clocksource_cyc2ns(bfin_read_TIMER0_COUNTER(),
  104. bfin_cs_gptimer0.mult, bfin_cs_gptimer0.shift);
  105. }
  106. static int __init bfin_cs_gptimer0_init(void)
  107. {
  108. setup_gptimer0();
  109. bfin_cs_gptimer0.mult = \
  110. clocksource_hz2mult(get_sclk(), bfin_cs_gptimer0.shift);
  111. if (clocksource_register(&bfin_cs_gptimer0))
  112. panic("failed to register clocksource");
  113. return 0;
  114. }
  115. #else
  116. # define bfin_cs_gptimer0_init()
  117. #endif
  118. #if defined(CONFIG_GPTMR0_CLOCKSOURCE) || defined(CONFIG_CYCLES_CLOCKSOURCE)
  119. /* prefer to use cycles since it has higher rating */
  120. notrace unsigned long long sched_clock(void)
  121. {
  122. #if defined(CONFIG_CYCLES_CLOCKSOURCE)
  123. return bfin_cs_cycles_sched_clock();
  124. #else
  125. return bfin_cs_gptimer0_sched_clock();
  126. #endif
  127. }
  128. #endif
  129. #if defined(CONFIG_TICKSOURCE_GPTMR0)
  130. static int bfin_gptmr0_set_next_event(unsigned long cycles,
  131. struct clock_event_device *evt)
  132. {
  133. disable_gptimers(TIMER0bit);
  134. /* it starts counting three SCLK cycles after the TIMENx bit is set */
  135. set_gptimer_pwidth(TIMER0_id, cycles - 3);
  136. enable_gptimers(TIMER0bit);
  137. return 0;
  138. }
  139. static void bfin_gptmr0_set_mode(enum clock_event_mode mode,
  140. struct clock_event_device *evt)
  141. {
  142. switch (mode) {
  143. case CLOCK_EVT_MODE_PERIODIC: {
  144. set_gptimer_config(TIMER0_id, \
  145. TIMER_OUT_DIS | TIMER_IRQ_ENA | \
  146. TIMER_PERIOD_CNT | TIMER_MODE_PWM);
  147. set_gptimer_period(TIMER0_id, get_sclk() / HZ);
  148. set_gptimer_pwidth(TIMER0_id, get_sclk() / HZ - 1);
  149. enable_gptimers(TIMER0bit);
  150. break;
  151. }
  152. case CLOCK_EVT_MODE_ONESHOT:
  153. disable_gptimers(TIMER0bit);
  154. set_gptimer_config(TIMER0_id, \
  155. TIMER_OUT_DIS | TIMER_IRQ_ENA | TIMER_MODE_PWM);
  156. set_gptimer_period(TIMER0_id, 0);
  157. break;
  158. case CLOCK_EVT_MODE_UNUSED:
  159. case CLOCK_EVT_MODE_SHUTDOWN:
  160. disable_gptimers(TIMER0bit);
  161. break;
  162. case CLOCK_EVT_MODE_RESUME:
  163. break;
  164. }
  165. }
  166. static void bfin_gptmr0_ack(void)
  167. {
  168. set_gptimer_status(TIMER_GROUP1, TIMER_STATUS_TIMIL0);
  169. }
  170. static void __init bfin_gptmr0_init(void)
  171. {
  172. disable_gptimers(TIMER0bit);
  173. }
  174. #ifdef CONFIG_CORE_TIMER_IRQ_L1
  175. __attribute__((l1_text))
  176. #endif
  177. irqreturn_t bfin_gptmr0_interrupt(int irq, void *dev_id)
  178. {
  179. struct clock_event_device *evt = dev_id;
  180. smp_mb();
  181. /*
  182. * We want to ACK before we handle so that we can handle smaller timer
  183. * intervals. This way if the timer expires again while we're handling
  184. * things, we're more likely to see that 2nd int rather than swallowing
  185. * it by ACKing the int at the end of this handler.
  186. */
  187. bfin_gptmr0_ack();
  188. evt->event_handler(evt);
  189. return IRQ_HANDLED;
  190. }
  191. static struct irqaction gptmr0_irq = {
  192. .name = "Blackfin GPTimer0",
  193. .flags = IRQF_DISABLED | IRQF_TIMER | \
  194. IRQF_IRQPOLL | IRQF_PERCPU,
  195. .handler = bfin_gptmr0_interrupt,
  196. };
  197. static struct clock_event_device clockevent_gptmr0 = {
  198. .name = "bfin_gptimer0",
  199. .rating = 300,
  200. .irq = IRQ_TIMER0,
  201. .shift = 32,
  202. .features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT,
  203. .set_next_event = bfin_gptmr0_set_next_event,
  204. .set_mode = bfin_gptmr0_set_mode,
  205. };
  206. static void __init bfin_gptmr0_clockevent_init(struct clock_event_device *evt)
  207. {
  208. unsigned long clock_tick;
  209. clock_tick = get_sclk();
  210. evt->mult = div_sc(clock_tick, NSEC_PER_SEC, evt->shift);
  211. evt->max_delta_ns = clockevent_delta2ns(-1, evt);
  212. evt->min_delta_ns = clockevent_delta2ns(100, evt);
  213. evt->cpumask = cpumask_of(0);
  214. clockevents_register_device(evt);
  215. }
  216. #endif /* CONFIG_TICKSOURCE_GPTMR0 */
  217. #if defined(CONFIG_TICKSOURCE_CORETMR)
  218. /* per-cpu local core timer */
  219. static DEFINE_PER_CPU(struct clock_event_device, coretmr_events);
  220. static int bfin_coretmr_set_next_event(unsigned long cycles,
  221. struct clock_event_device *evt)
  222. {
  223. bfin_write_TCNTL(TMPWR);
  224. CSYNC();
  225. bfin_write_TCOUNT(cycles);
  226. CSYNC();
  227. bfin_write_TCNTL(TMPWR | TMREN);
  228. return 0;
  229. }
  230. static void bfin_coretmr_set_mode(enum clock_event_mode mode,
  231. struct clock_event_device *evt)
  232. {
  233. switch (mode) {
  234. case CLOCK_EVT_MODE_PERIODIC: {
  235. unsigned long tcount = ((get_cclk() / (HZ * TIME_SCALE)) - 1);
  236. bfin_write_TCNTL(TMPWR);
  237. CSYNC();
  238. bfin_write_TSCALE(TIME_SCALE - 1);
  239. bfin_write_TPERIOD(tcount);
  240. bfin_write_TCOUNT(tcount);
  241. CSYNC();
  242. bfin_write_TCNTL(TMPWR | TMREN | TAUTORLD);
  243. break;
  244. }
  245. case CLOCK_EVT_MODE_ONESHOT:
  246. bfin_write_TCNTL(TMPWR);
  247. CSYNC();
  248. bfin_write_TSCALE(TIME_SCALE - 1);
  249. bfin_write_TPERIOD(0);
  250. bfin_write_TCOUNT(0);
  251. break;
  252. case CLOCK_EVT_MODE_UNUSED:
  253. case CLOCK_EVT_MODE_SHUTDOWN:
  254. bfin_write_TCNTL(0);
  255. CSYNC();
  256. break;
  257. case CLOCK_EVT_MODE_RESUME:
  258. break;
  259. }
  260. }
  261. void bfin_coretmr_init(void)
  262. {
  263. /* power up the timer, but don't enable it just yet */
  264. bfin_write_TCNTL(TMPWR);
  265. CSYNC();
  266. /* the TSCALE prescaler counter. */
  267. bfin_write_TSCALE(TIME_SCALE - 1);
  268. bfin_write_TPERIOD(0);
  269. bfin_write_TCOUNT(0);
  270. CSYNC();
  271. }
  272. #ifdef CONFIG_CORE_TIMER_IRQ_L1
  273. __attribute__((l1_text))
  274. #endif
  275. irqreturn_t bfin_coretmr_interrupt(int irq, void *dev_id)
  276. {
  277. int cpu = smp_processor_id();
  278. struct clock_event_device *evt = &per_cpu(coretmr_events, cpu);
  279. smp_mb();
  280. evt->event_handler(evt);
  281. touch_nmi_watchdog();
  282. return IRQ_HANDLED;
  283. }
  284. static struct irqaction coretmr_irq = {
  285. .name = "Blackfin CoreTimer",
  286. .flags = IRQF_DISABLED | IRQF_TIMER | \
  287. IRQF_IRQPOLL | IRQF_PERCPU,
  288. .handler = bfin_coretmr_interrupt,
  289. };
  290. void bfin_coretmr_clockevent_init(void)
  291. {
  292. unsigned long clock_tick;
  293. unsigned int cpu = smp_processor_id();
  294. struct clock_event_device *evt = &per_cpu(coretmr_events, cpu);
  295. evt->name = "bfin_core_timer";
  296. evt->rating = 350;
  297. evt->irq = -1;
  298. evt->shift = 32;
  299. evt->features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT;
  300. evt->set_next_event = bfin_coretmr_set_next_event;
  301. evt->set_mode = bfin_coretmr_set_mode;
  302. clock_tick = get_cclk() / TIME_SCALE;
  303. evt->mult = div_sc(clock_tick, NSEC_PER_SEC, evt->shift);
  304. evt->max_delta_ns = clockevent_delta2ns(-1, evt);
  305. evt->min_delta_ns = clockevent_delta2ns(100, evt);
  306. evt->cpumask = cpumask_of(cpu);
  307. clockevents_register_device(evt);
  308. }
  309. #endif /* CONFIG_TICKSOURCE_CORETMR */
  310. void read_persistent_clock(struct timespec *ts)
  311. {
  312. time_t secs_since_1970 = (365 * 37 + 9) * 24 * 60 * 60; /* 1 Jan 2007 */
  313. ts->tv_sec = secs_since_1970;
  314. ts->tv_nsec = 0;
  315. }
  316. void __init time_init(void)
  317. {
  318. #ifdef CONFIG_RTC_DRV_BFIN
  319. /* [#2663] hack to filter junk RTC values that would cause
  320. * userspace to have to deal with time values greater than
  321. * 2^31 seconds (which uClibc cannot cope with yet)
  322. */
  323. if ((bfin_read_RTC_STAT() & 0xC0000000) == 0xC0000000) {
  324. printk(KERN_NOTICE "bfin-rtc: invalid date; resetting\n");
  325. bfin_write_RTC_STAT(0);
  326. }
  327. #endif
  328. bfin_cs_cycles_init();
  329. bfin_cs_gptimer0_init();
  330. #if defined(CONFIG_TICKSOURCE_CORETMR)
  331. bfin_coretmr_init();
  332. setup_irq(IRQ_CORETMR, &coretmr_irq);
  333. bfin_coretmr_clockevent_init();
  334. #endif
  335. #if defined(CONFIG_TICKSOURCE_GPTMR0)
  336. bfin_gptmr0_init();
  337. setup_irq(IRQ_TIMER0, &gptmr0_irq);
  338. gptmr0_irq.dev_id = &clockevent_gptmr0;
  339. bfin_gptmr0_clockevent_init(&clockevent_gptmr0);
  340. #endif
  341. #if !defined(CONFIG_TICKSOURCE_CORETMR) && !defined(CONFIG_TICKSOURCE_GPTMR0)
  342. # error at least one clock event device is required
  343. #endif
  344. }