time-ts.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  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. #if defined(CONFIG_CYCLES_CLOCKSOURCE)
  24. static notrace cycle_t bfin_read_cycles(struct clocksource *cs)
  25. {
  26. #ifdef CONFIG_CPU_FREQ
  27. return __bfin_cycles_off + (get_cycles() << __bfin_cycles_mod);
  28. #else
  29. return get_cycles();
  30. #endif
  31. }
  32. static struct clocksource bfin_cs_cycles = {
  33. .name = "bfin_cs_cycles",
  34. .rating = 400,
  35. .read = bfin_read_cycles,
  36. .mask = CLOCKSOURCE_MASK(64),
  37. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  38. };
  39. static inline unsigned long long bfin_cs_cycles_sched_clock(void)
  40. {
  41. return clocksource_cyc2ns(bfin_read_cycles(&bfin_cs_cycles),
  42. bfin_cs_cycles.mult, bfin_cs_cycles.shift);
  43. }
  44. static int __init bfin_cs_cycles_init(void)
  45. {
  46. if (clocksource_register_hz(&bfin_cs_cycles, get_cclk()))
  47. panic("failed to register clocksource");
  48. return 0;
  49. }
  50. #else
  51. # define bfin_cs_cycles_init()
  52. #endif
  53. #ifdef CONFIG_GPTMR0_CLOCKSOURCE
  54. void __init setup_gptimer0(void)
  55. {
  56. disable_gptimers(TIMER0bit);
  57. #ifdef CONFIG_BF60x
  58. bfin_write16(TIMER_DATA_IMSK, 0);
  59. set_gptimer_config(TIMER0_id, TIMER_OUT_DIS
  60. | TIMER_MODE_PWM_CONT | TIMER_PULSE_HI | TIMER_IRQ_PER);
  61. #else
  62. set_gptimer_config(TIMER0_id, \
  63. TIMER_OUT_DIS | TIMER_PERIOD_CNT | TIMER_MODE_PWM);
  64. #endif
  65. set_gptimer_period(TIMER0_id, -1);
  66. set_gptimer_pwidth(TIMER0_id, -2);
  67. SSYNC();
  68. enable_gptimers(TIMER0bit);
  69. }
  70. static cycle_t bfin_read_gptimer0(struct clocksource *cs)
  71. {
  72. return bfin_read_TIMER0_COUNTER();
  73. }
  74. static struct clocksource bfin_cs_gptimer0 = {
  75. .name = "bfin_cs_gptimer0",
  76. .rating = 350,
  77. .read = bfin_read_gptimer0,
  78. .mask = CLOCKSOURCE_MASK(32),
  79. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  80. };
  81. static inline unsigned long long bfin_cs_gptimer0_sched_clock(void)
  82. {
  83. return clocksource_cyc2ns(bfin_read_TIMER0_COUNTER(),
  84. bfin_cs_gptimer0.mult, bfin_cs_gptimer0.shift);
  85. }
  86. static int __init bfin_cs_gptimer0_init(void)
  87. {
  88. setup_gptimer0();
  89. if (clocksource_register_hz(&bfin_cs_gptimer0, get_sclk()))
  90. panic("failed to register clocksource");
  91. return 0;
  92. }
  93. #else
  94. # define bfin_cs_gptimer0_init()
  95. #endif
  96. #if defined(CONFIG_GPTMR0_CLOCKSOURCE) || defined(CONFIG_CYCLES_CLOCKSOURCE)
  97. /* prefer to use cycles since it has higher rating */
  98. notrace unsigned long long sched_clock(void)
  99. {
  100. #if defined(CONFIG_CYCLES_CLOCKSOURCE)
  101. return bfin_cs_cycles_sched_clock();
  102. #else
  103. return bfin_cs_gptimer0_sched_clock();
  104. #endif
  105. }
  106. #endif
  107. #if defined(CONFIG_TICKSOURCE_GPTMR0)
  108. static int bfin_gptmr0_set_next_event(unsigned long cycles,
  109. struct clock_event_device *evt)
  110. {
  111. disable_gptimers(TIMER0bit);
  112. /* it starts counting three SCLK cycles after the TIMENx bit is set */
  113. set_gptimer_pwidth(TIMER0_id, cycles - 3);
  114. enable_gptimers(TIMER0bit);
  115. return 0;
  116. }
  117. static int bfin_gptmr0_set_periodic(struct clock_event_device *evt)
  118. {
  119. #ifndef CONFIG_BF60x
  120. set_gptimer_config(TIMER0_id,
  121. TIMER_OUT_DIS | TIMER_IRQ_ENA |
  122. TIMER_PERIOD_CNT | TIMER_MODE_PWM);
  123. #else
  124. set_gptimer_config(TIMER0_id,
  125. TIMER_OUT_DIS | TIMER_MODE_PWM_CONT |
  126. TIMER_PULSE_HI | TIMER_IRQ_PER);
  127. #endif
  128. set_gptimer_period(TIMER0_id, get_sclk() / HZ);
  129. set_gptimer_pwidth(TIMER0_id, get_sclk() / HZ - 1);
  130. enable_gptimers(TIMER0bit);
  131. return 0;
  132. }
  133. static int bfin_gptmr0_set_oneshot(struct clock_event_device *evt)
  134. {
  135. disable_gptimers(TIMER0bit);
  136. #ifndef CONFIG_BF60x
  137. set_gptimer_config(TIMER0_id,
  138. TIMER_OUT_DIS | TIMER_IRQ_ENA | TIMER_MODE_PWM);
  139. #else
  140. set_gptimer_config(TIMER0_id,
  141. TIMER_OUT_DIS | TIMER_MODE_PWM | TIMER_PULSE_HI |
  142. TIMER_IRQ_WID_DLY);
  143. #endif
  144. set_gptimer_period(TIMER0_id, 0);
  145. return 0;
  146. }
  147. static int bfin_gptmr0_shutdown(struct clock_event_device *evt)
  148. {
  149. disable_gptimers(TIMER0bit);
  150. return 0;
  151. }
  152. static void bfin_gptmr0_ack(void)
  153. {
  154. clear_gptimer_intr(TIMER0_id);
  155. }
  156. static void __init bfin_gptmr0_init(void)
  157. {
  158. disable_gptimers(TIMER0bit);
  159. }
  160. #ifdef CONFIG_CORE_TIMER_IRQ_L1
  161. __attribute__((l1_text))
  162. #endif
  163. irqreturn_t bfin_gptmr0_interrupt(int irq, void *dev_id)
  164. {
  165. struct clock_event_device *evt = dev_id;
  166. smp_mb();
  167. /*
  168. * We want to ACK before we handle so that we can handle smaller timer
  169. * intervals. This way if the timer expires again while we're handling
  170. * things, we're more likely to see that 2nd int rather than swallowing
  171. * it by ACKing the int at the end of this handler.
  172. */
  173. bfin_gptmr0_ack();
  174. evt->event_handler(evt);
  175. return IRQ_HANDLED;
  176. }
  177. static struct irqaction gptmr0_irq = {
  178. .name = "Blackfin GPTimer0",
  179. .flags = IRQF_TIMER | IRQF_IRQPOLL | IRQF_PERCPU,
  180. .handler = bfin_gptmr0_interrupt,
  181. };
  182. static struct clock_event_device clockevent_gptmr0 = {
  183. .name = "bfin_gptimer0",
  184. .rating = 300,
  185. .irq = IRQ_TIMER0,
  186. .shift = 32,
  187. .features = CLOCK_EVT_FEAT_PERIODIC |
  188. CLOCK_EVT_FEAT_ONESHOT,
  189. .set_next_event = bfin_gptmr0_set_next_event,
  190. .set_state_shutdown = bfin_gptmr0_shutdown,
  191. .set_state_periodic = bfin_gptmr0_set_periodic,
  192. .set_state_oneshot = bfin_gptmr0_set_oneshot,
  193. };
  194. static void __init bfin_gptmr0_clockevent_init(struct clock_event_device *evt)
  195. {
  196. unsigned long clock_tick;
  197. clock_tick = get_sclk();
  198. evt->mult = div_sc(clock_tick, NSEC_PER_SEC, evt->shift);
  199. evt->max_delta_ns = clockevent_delta2ns(-1, evt);
  200. evt->min_delta_ns = clockevent_delta2ns(100, evt);
  201. evt->cpumask = cpumask_of(0);
  202. clockevents_register_device(evt);
  203. }
  204. #endif /* CONFIG_TICKSOURCE_GPTMR0 */
  205. #if defined(CONFIG_TICKSOURCE_CORETMR)
  206. /* per-cpu local core timer */
  207. DEFINE_PER_CPU(struct clock_event_device, coretmr_events);
  208. static int bfin_coretmr_set_next_event(unsigned long cycles,
  209. struct clock_event_device *evt)
  210. {
  211. bfin_write_TCNTL(TMPWR);
  212. CSYNC();
  213. bfin_write_TCOUNT(cycles);
  214. CSYNC();
  215. bfin_write_TCNTL(TMPWR | TMREN);
  216. return 0;
  217. }
  218. static int bfin_coretmr_set_periodic(struct clock_event_device *evt)
  219. {
  220. unsigned long tcount = ((get_cclk() / (HZ * TIME_SCALE)) - 1);
  221. bfin_write_TCNTL(TMPWR);
  222. CSYNC();
  223. bfin_write_TSCALE(TIME_SCALE - 1);
  224. bfin_write_TPERIOD(tcount);
  225. bfin_write_TCOUNT(tcount);
  226. CSYNC();
  227. bfin_write_TCNTL(TMPWR | TMREN | TAUTORLD);
  228. return 0;
  229. }
  230. static int bfin_coretmr_set_oneshot(struct clock_event_device *evt)
  231. {
  232. bfin_write_TCNTL(TMPWR);
  233. CSYNC();
  234. bfin_write_TSCALE(TIME_SCALE - 1);
  235. bfin_write_TPERIOD(0);
  236. bfin_write_TCOUNT(0);
  237. return 0;
  238. }
  239. static int bfin_coretmr_shutdown(struct clock_event_device *evt)
  240. {
  241. bfin_write_TCNTL(0);
  242. CSYNC();
  243. return 0;
  244. }
  245. void bfin_coretmr_init(void)
  246. {
  247. /* power up the timer, but don't enable it just yet */
  248. bfin_write_TCNTL(TMPWR);
  249. CSYNC();
  250. /* the TSCALE prescaler counter. */
  251. bfin_write_TSCALE(TIME_SCALE - 1);
  252. bfin_write_TPERIOD(0);
  253. bfin_write_TCOUNT(0);
  254. CSYNC();
  255. }
  256. #ifdef CONFIG_CORE_TIMER_IRQ_L1
  257. __attribute__((l1_text))
  258. #endif
  259. irqreturn_t bfin_coretmr_interrupt(int irq, void *dev_id)
  260. {
  261. int cpu = smp_processor_id();
  262. struct clock_event_device *evt = &per_cpu(coretmr_events, cpu);
  263. smp_mb();
  264. evt->event_handler(evt);
  265. touch_nmi_watchdog();
  266. return IRQ_HANDLED;
  267. }
  268. static struct irqaction coretmr_irq = {
  269. .name = "Blackfin CoreTimer",
  270. .flags = IRQF_TIMER | IRQF_IRQPOLL | IRQF_PERCPU,
  271. .handler = bfin_coretmr_interrupt,
  272. };
  273. void bfin_coretmr_clockevent_init(void)
  274. {
  275. unsigned long clock_tick;
  276. unsigned int cpu = smp_processor_id();
  277. struct clock_event_device *evt = &per_cpu(coretmr_events, cpu);
  278. #ifdef CONFIG_SMP
  279. evt->broadcast = smp_timer_broadcast;
  280. #endif
  281. evt->name = "bfin_core_timer";
  282. evt->rating = 350;
  283. evt->irq = -1;
  284. evt->shift = 32;
  285. evt->features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT;
  286. evt->set_next_event = bfin_coretmr_set_next_event;
  287. evt->set_state_shutdown = bfin_coretmr_shutdown;
  288. evt->set_state_periodic = bfin_coretmr_set_periodic;
  289. evt->set_state_oneshot = bfin_coretmr_set_oneshot;
  290. clock_tick = get_cclk() / TIME_SCALE;
  291. evt->mult = div_sc(clock_tick, NSEC_PER_SEC, evt->shift);
  292. evt->max_delta_ns = clockevent_delta2ns(-1, evt);
  293. evt->min_delta_ns = clockevent_delta2ns(100, evt);
  294. evt->cpumask = cpumask_of(cpu);
  295. clockevents_register_device(evt);
  296. }
  297. #endif /* CONFIG_TICKSOURCE_CORETMR */
  298. void read_persistent_clock(struct timespec *ts)
  299. {
  300. time_t secs_since_1970 = (365 * 37 + 9) * 24 * 60 * 60; /* 1 Jan 2007 */
  301. ts->tv_sec = secs_since_1970;
  302. ts->tv_nsec = 0;
  303. }
  304. void __init time_init(void)
  305. {
  306. #ifdef CONFIG_RTC_DRV_BFIN
  307. /* [#2663] hack to filter junk RTC values that would cause
  308. * userspace to have to deal with time values greater than
  309. * 2^31 seconds (which uClibc cannot cope with yet)
  310. */
  311. if ((bfin_read_RTC_STAT() & 0xC0000000) == 0xC0000000) {
  312. printk(KERN_NOTICE "bfin-rtc: invalid date; resetting\n");
  313. bfin_write_RTC_STAT(0);
  314. }
  315. #endif
  316. bfin_cs_cycles_init();
  317. bfin_cs_gptimer0_init();
  318. #if defined(CONFIG_TICKSOURCE_CORETMR)
  319. bfin_coretmr_init();
  320. setup_irq(IRQ_CORETMR, &coretmr_irq);
  321. bfin_coretmr_clockevent_init();
  322. #endif
  323. #if defined(CONFIG_TICKSOURCE_GPTMR0)
  324. bfin_gptmr0_init();
  325. setup_irq(IRQ_TIMER0, &gptmr0_irq);
  326. gptmr0_irq.dev_id = &clockevent_gptmr0;
  327. bfin_gptmr0_clockevent_init(&clockevent_gptmr0);
  328. #endif
  329. #if !defined(CONFIG_TICKSOURCE_CORETMR) && !defined(CONFIG_TICKSOURCE_GPTMR0)
  330. # error at least one clock event device is required
  331. #endif
  332. }