time-ts.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  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 void bfin_gptmr0_set_mode(enum clock_event_mode mode,
  118. struct clock_event_device *evt)
  119. {
  120. switch (mode) {
  121. case CLOCK_EVT_MODE_PERIODIC: {
  122. #ifndef CONFIG_BF60x
  123. set_gptimer_config(TIMER0_id, \
  124. TIMER_OUT_DIS | TIMER_IRQ_ENA | \
  125. TIMER_PERIOD_CNT | TIMER_MODE_PWM);
  126. #else
  127. set_gptimer_config(TIMER0_id, TIMER_OUT_DIS
  128. | TIMER_MODE_PWM_CONT | TIMER_PULSE_HI | TIMER_IRQ_PER);
  129. #endif
  130. set_gptimer_period(TIMER0_id, get_sclk() / HZ);
  131. set_gptimer_pwidth(TIMER0_id, get_sclk() / HZ - 1);
  132. enable_gptimers(TIMER0bit);
  133. break;
  134. }
  135. case CLOCK_EVT_MODE_ONESHOT:
  136. disable_gptimers(TIMER0bit);
  137. #ifndef CONFIG_BF60x
  138. set_gptimer_config(TIMER0_id, \
  139. TIMER_OUT_DIS | TIMER_IRQ_ENA | TIMER_MODE_PWM);
  140. #else
  141. set_gptimer_config(TIMER0_id, TIMER_OUT_DIS | TIMER_MODE_PWM
  142. | TIMER_PULSE_HI | TIMER_IRQ_WID_DLY);
  143. #endif
  144. set_gptimer_period(TIMER0_id, 0);
  145. break;
  146. case CLOCK_EVT_MODE_UNUSED:
  147. case CLOCK_EVT_MODE_SHUTDOWN:
  148. disable_gptimers(TIMER0bit);
  149. break;
  150. case CLOCK_EVT_MODE_RESUME:
  151. break;
  152. }
  153. }
  154. static void bfin_gptmr0_ack(void)
  155. {
  156. clear_gptimer_intr(TIMER0_id);
  157. }
  158. static void __init bfin_gptmr0_init(void)
  159. {
  160. disable_gptimers(TIMER0bit);
  161. }
  162. #ifdef CONFIG_CORE_TIMER_IRQ_L1
  163. __attribute__((l1_text))
  164. #endif
  165. irqreturn_t bfin_gptmr0_interrupt(int irq, void *dev_id)
  166. {
  167. struct clock_event_device *evt = dev_id;
  168. smp_mb();
  169. /*
  170. * We want to ACK before we handle so that we can handle smaller timer
  171. * intervals. This way if the timer expires again while we're handling
  172. * things, we're more likely to see that 2nd int rather than swallowing
  173. * it by ACKing the int at the end of this handler.
  174. */
  175. bfin_gptmr0_ack();
  176. evt->event_handler(evt);
  177. return IRQ_HANDLED;
  178. }
  179. static struct irqaction gptmr0_irq = {
  180. .name = "Blackfin GPTimer0",
  181. .flags = IRQF_TIMER | IRQF_IRQPOLL | IRQF_PERCPU,
  182. .handler = bfin_gptmr0_interrupt,
  183. };
  184. static struct clock_event_device clockevent_gptmr0 = {
  185. .name = "bfin_gptimer0",
  186. .rating = 300,
  187. .irq = IRQ_TIMER0,
  188. .shift = 32,
  189. .features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT,
  190. .set_next_event = bfin_gptmr0_set_next_event,
  191. .set_mode = bfin_gptmr0_set_mode,
  192. };
  193. static void __init bfin_gptmr0_clockevent_init(struct clock_event_device *evt)
  194. {
  195. unsigned long clock_tick;
  196. clock_tick = get_sclk();
  197. evt->mult = div_sc(clock_tick, NSEC_PER_SEC, evt->shift);
  198. evt->max_delta_ns = clockevent_delta2ns(-1, evt);
  199. evt->min_delta_ns = clockevent_delta2ns(100, evt);
  200. evt->cpumask = cpumask_of(0);
  201. clockevents_register_device(evt);
  202. }
  203. #endif /* CONFIG_TICKSOURCE_GPTMR0 */
  204. #if defined(CONFIG_TICKSOURCE_CORETMR)
  205. /* per-cpu local core timer */
  206. DEFINE_PER_CPU(struct clock_event_device, coretmr_events);
  207. static int bfin_coretmr_set_next_event(unsigned long cycles,
  208. struct clock_event_device *evt)
  209. {
  210. bfin_write_TCNTL(TMPWR);
  211. CSYNC();
  212. bfin_write_TCOUNT(cycles);
  213. CSYNC();
  214. bfin_write_TCNTL(TMPWR | TMREN);
  215. return 0;
  216. }
  217. static void bfin_coretmr_set_mode(enum clock_event_mode mode,
  218. struct clock_event_device *evt)
  219. {
  220. switch (mode) {
  221. case CLOCK_EVT_MODE_PERIODIC: {
  222. unsigned long tcount = ((get_cclk() / (HZ * TIME_SCALE)) - 1);
  223. bfin_write_TCNTL(TMPWR);
  224. CSYNC();
  225. bfin_write_TSCALE(TIME_SCALE - 1);
  226. bfin_write_TPERIOD(tcount);
  227. bfin_write_TCOUNT(tcount);
  228. CSYNC();
  229. bfin_write_TCNTL(TMPWR | TMREN | TAUTORLD);
  230. break;
  231. }
  232. case CLOCK_EVT_MODE_ONESHOT:
  233. bfin_write_TCNTL(TMPWR);
  234. CSYNC();
  235. bfin_write_TSCALE(TIME_SCALE - 1);
  236. bfin_write_TPERIOD(0);
  237. bfin_write_TCOUNT(0);
  238. break;
  239. case CLOCK_EVT_MODE_UNUSED:
  240. case CLOCK_EVT_MODE_SHUTDOWN:
  241. bfin_write_TCNTL(0);
  242. CSYNC();
  243. break;
  244. case CLOCK_EVT_MODE_RESUME:
  245. break;
  246. }
  247. }
  248. void bfin_coretmr_init(void)
  249. {
  250. /* power up the timer, but don't enable it just yet */
  251. bfin_write_TCNTL(TMPWR);
  252. CSYNC();
  253. /* the TSCALE prescaler counter. */
  254. bfin_write_TSCALE(TIME_SCALE - 1);
  255. bfin_write_TPERIOD(0);
  256. bfin_write_TCOUNT(0);
  257. CSYNC();
  258. }
  259. #ifdef CONFIG_CORE_TIMER_IRQ_L1
  260. __attribute__((l1_text))
  261. #endif
  262. irqreturn_t bfin_coretmr_interrupt(int irq, void *dev_id)
  263. {
  264. int cpu = smp_processor_id();
  265. struct clock_event_device *evt = &per_cpu(coretmr_events, cpu);
  266. smp_mb();
  267. evt->event_handler(evt);
  268. touch_nmi_watchdog();
  269. return IRQ_HANDLED;
  270. }
  271. static struct irqaction coretmr_irq = {
  272. .name = "Blackfin CoreTimer",
  273. .flags = IRQF_TIMER | IRQF_IRQPOLL | IRQF_PERCPU,
  274. .handler = bfin_coretmr_interrupt,
  275. };
  276. void bfin_coretmr_clockevent_init(void)
  277. {
  278. unsigned long clock_tick;
  279. unsigned int cpu = smp_processor_id();
  280. struct clock_event_device *evt = &per_cpu(coretmr_events, cpu);
  281. #ifdef CONFIG_SMP
  282. evt->broadcast = smp_timer_broadcast;
  283. #endif
  284. evt->name = "bfin_core_timer";
  285. evt->rating = 350;
  286. evt->irq = -1;
  287. evt->shift = 32;
  288. evt->features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT;
  289. evt->set_next_event = bfin_coretmr_set_next_event;
  290. evt->set_mode = bfin_coretmr_set_mode;
  291. clock_tick = get_cclk() / TIME_SCALE;
  292. evt->mult = div_sc(clock_tick, NSEC_PER_SEC, evt->shift);
  293. evt->max_delta_ns = clockevent_delta2ns(-1, evt);
  294. evt->min_delta_ns = clockevent_delta2ns(100, evt);
  295. evt->cpumask = cpumask_of(cpu);
  296. clockevents_register_device(evt);
  297. }
  298. #endif /* CONFIG_TICKSOURCE_CORETMR */
  299. void read_persistent_clock(struct timespec *ts)
  300. {
  301. time_t secs_since_1970 = (365 * 37 + 9) * 24 * 60 * 60; /* 1 Jan 2007 */
  302. ts->tv_sec = secs_since_1970;
  303. ts->tv_nsec = 0;
  304. }
  305. void __init time_init(void)
  306. {
  307. #ifdef CONFIG_RTC_DRV_BFIN
  308. /* [#2663] hack to filter junk RTC values that would cause
  309. * userspace to have to deal with time values greater than
  310. * 2^31 seconds (which uClibc cannot cope with yet)
  311. */
  312. if ((bfin_read_RTC_STAT() & 0xC0000000) == 0xC0000000) {
  313. printk(KERN_NOTICE "bfin-rtc: invalid date; resetting\n");
  314. bfin_write_RTC_STAT(0);
  315. }
  316. #endif
  317. bfin_cs_cycles_init();
  318. bfin_cs_gptimer0_init();
  319. #if defined(CONFIG_TICKSOURCE_CORETMR)
  320. bfin_coretmr_init();
  321. setup_irq(IRQ_CORETMR, &coretmr_irq);
  322. bfin_coretmr_clockevent_init();
  323. #endif
  324. #if defined(CONFIG_TICKSOURCE_GPTMR0)
  325. bfin_gptmr0_init();
  326. setup_irq(IRQ_TIMER0, &gptmr0_irq);
  327. gptmr0_irq.dev_id = &clockevent_gptmr0;
  328. bfin_gptmr0_clockevent_init(&clockevent_gptmr0);
  329. #endif
  330. #if !defined(CONFIG_TICKSOURCE_CORETMR) && !defined(CONFIG_TICKSOURCE_GPTMR0)
  331. # error at least one clock event device is required
  332. #endif
  333. }