timer.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. /*
  2. * Copyright (C) 2007-2013 Michal Simek <monstr@monstr.eu>
  3. * Copyright (C) 2012-2013 Xilinx, Inc.
  4. * Copyright (C) 2007-2009 PetaLogix
  5. * Copyright (C) 2006 Atmark Techno, Inc.
  6. *
  7. * This file is subject to the terms and conditions of the GNU General Public
  8. * License. See the file "COPYING" in the main directory of this archive
  9. * for more details.
  10. */
  11. #include <linux/interrupt.h>
  12. #include <linux/delay.h>
  13. #include <linux/sched.h>
  14. #include <linux/clk.h>
  15. #include <linux/clockchips.h>
  16. #include <linux/of_address.h>
  17. #include <asm/cpuinfo.h>
  18. #include <linux/cnt32_to_63.h>
  19. static void __iomem *timer_baseaddr;
  20. static unsigned int freq_div_hz;
  21. static unsigned int timer_clock_freq;
  22. #define TCSR0 (0x00)
  23. #define TLR0 (0x04)
  24. #define TCR0 (0x08)
  25. #define TCSR1 (0x10)
  26. #define TLR1 (0x14)
  27. #define TCR1 (0x18)
  28. #define TCSR_MDT (1<<0)
  29. #define TCSR_UDT (1<<1)
  30. #define TCSR_GENT (1<<2)
  31. #define TCSR_CAPT (1<<3)
  32. #define TCSR_ARHT (1<<4)
  33. #define TCSR_LOAD (1<<5)
  34. #define TCSR_ENIT (1<<6)
  35. #define TCSR_ENT (1<<7)
  36. #define TCSR_TINT (1<<8)
  37. #define TCSR_PWMA (1<<9)
  38. #define TCSR_ENALL (1<<10)
  39. static inline void xilinx_timer0_stop(void)
  40. {
  41. out_be32(timer_baseaddr + TCSR0,
  42. in_be32(timer_baseaddr + TCSR0) & ~TCSR_ENT);
  43. }
  44. static inline void xilinx_timer0_start_periodic(unsigned long load_val)
  45. {
  46. if (!load_val)
  47. load_val = 1;
  48. /* loading value to timer reg */
  49. out_be32(timer_baseaddr + TLR0, load_val);
  50. /* load the initial value */
  51. out_be32(timer_baseaddr + TCSR0, TCSR_LOAD);
  52. /* see timer data sheet for detail
  53. * !ENALL - don't enable 'em all
  54. * !PWMA - disable pwm
  55. * TINT - clear interrupt status
  56. * ENT- enable timer itself
  57. * ENIT - enable interrupt
  58. * !LOAD - clear the bit to let go
  59. * ARHT - auto reload
  60. * !CAPT - no external trigger
  61. * !GENT - no external signal
  62. * UDT - set the timer as down counter
  63. * !MDT0 - generate mode
  64. */
  65. out_be32(timer_baseaddr + TCSR0,
  66. TCSR_TINT|TCSR_ENIT|TCSR_ENT|TCSR_ARHT|TCSR_UDT);
  67. }
  68. static inline void xilinx_timer0_start_oneshot(unsigned long load_val)
  69. {
  70. if (!load_val)
  71. load_val = 1;
  72. /* loading value to timer reg */
  73. out_be32(timer_baseaddr + TLR0, load_val);
  74. /* load the initial value */
  75. out_be32(timer_baseaddr + TCSR0, TCSR_LOAD);
  76. out_be32(timer_baseaddr + TCSR0,
  77. TCSR_TINT|TCSR_ENIT|TCSR_ENT|TCSR_ARHT|TCSR_UDT);
  78. }
  79. static int xilinx_timer_set_next_event(unsigned long delta,
  80. struct clock_event_device *dev)
  81. {
  82. pr_debug("%s: next event, delta %x\n", __func__, (u32)delta);
  83. xilinx_timer0_start_oneshot(delta);
  84. return 0;
  85. }
  86. static void xilinx_timer_set_mode(enum clock_event_mode mode,
  87. struct clock_event_device *evt)
  88. {
  89. switch (mode) {
  90. case CLOCK_EVT_MODE_PERIODIC:
  91. pr_info("%s: periodic\n", __func__);
  92. xilinx_timer0_start_periodic(freq_div_hz);
  93. break;
  94. case CLOCK_EVT_MODE_ONESHOT:
  95. pr_info("%s: oneshot\n", __func__);
  96. break;
  97. case CLOCK_EVT_MODE_UNUSED:
  98. pr_info("%s: unused\n", __func__);
  99. break;
  100. case CLOCK_EVT_MODE_SHUTDOWN:
  101. pr_info("%s: shutdown\n", __func__);
  102. xilinx_timer0_stop();
  103. break;
  104. case CLOCK_EVT_MODE_RESUME:
  105. pr_info("%s: resume\n", __func__);
  106. break;
  107. }
  108. }
  109. static struct clock_event_device clockevent_xilinx_timer = {
  110. .name = "xilinx_clockevent",
  111. .features = CLOCK_EVT_FEAT_ONESHOT | CLOCK_EVT_FEAT_PERIODIC,
  112. .shift = 8,
  113. .rating = 300,
  114. .set_next_event = xilinx_timer_set_next_event,
  115. .set_mode = xilinx_timer_set_mode,
  116. };
  117. static inline void timer_ack(void)
  118. {
  119. out_be32(timer_baseaddr + TCSR0, in_be32(timer_baseaddr + TCSR0));
  120. }
  121. static irqreturn_t timer_interrupt(int irq, void *dev_id)
  122. {
  123. struct clock_event_device *evt = &clockevent_xilinx_timer;
  124. #ifdef CONFIG_HEART_BEAT
  125. heartbeat();
  126. #endif
  127. timer_ack();
  128. evt->event_handler(evt);
  129. return IRQ_HANDLED;
  130. }
  131. static struct irqaction timer_irqaction = {
  132. .handler = timer_interrupt,
  133. .flags = IRQF_DISABLED | IRQF_TIMER,
  134. .name = "timer",
  135. .dev_id = &clockevent_xilinx_timer,
  136. };
  137. static __init void xilinx_clockevent_init(void)
  138. {
  139. clockevent_xilinx_timer.mult =
  140. div_sc(timer_clock_freq, NSEC_PER_SEC,
  141. clockevent_xilinx_timer.shift);
  142. clockevent_xilinx_timer.max_delta_ns =
  143. clockevent_delta2ns((u32)~0, &clockevent_xilinx_timer);
  144. clockevent_xilinx_timer.min_delta_ns =
  145. clockevent_delta2ns(1, &clockevent_xilinx_timer);
  146. clockevent_xilinx_timer.cpumask = cpumask_of(0);
  147. clockevents_register_device(&clockevent_xilinx_timer);
  148. }
  149. static cycle_t xilinx_read(struct clocksource *cs)
  150. {
  151. /* reading actual value of timer 1 */
  152. return (cycle_t) (in_be32(timer_baseaddr + TCR1));
  153. }
  154. static struct timecounter xilinx_tc = {
  155. .cc = NULL,
  156. };
  157. static cycle_t xilinx_cc_read(const struct cyclecounter *cc)
  158. {
  159. return xilinx_read(NULL);
  160. }
  161. static struct cyclecounter xilinx_cc = {
  162. .read = xilinx_cc_read,
  163. .mask = CLOCKSOURCE_MASK(32),
  164. .shift = 8,
  165. };
  166. static int __init init_xilinx_timecounter(void)
  167. {
  168. xilinx_cc.mult = div_sc(timer_clock_freq, NSEC_PER_SEC,
  169. xilinx_cc.shift);
  170. timecounter_init(&xilinx_tc, &xilinx_cc, sched_clock());
  171. return 0;
  172. }
  173. static struct clocksource clocksource_microblaze = {
  174. .name = "xilinx_clocksource",
  175. .rating = 300,
  176. .read = xilinx_read,
  177. .mask = CLOCKSOURCE_MASK(32),
  178. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  179. };
  180. static int __init xilinx_clocksource_init(void)
  181. {
  182. if (clocksource_register_hz(&clocksource_microblaze, timer_clock_freq))
  183. panic("failed to register clocksource");
  184. /* stop timer1 */
  185. out_be32(timer_baseaddr + TCSR1,
  186. in_be32(timer_baseaddr + TCSR1) & ~TCSR_ENT);
  187. /* start timer1 - up counting without interrupt */
  188. out_be32(timer_baseaddr + TCSR1, TCSR_TINT|TCSR_ENT|TCSR_ARHT);
  189. /* register timecounter - for ftrace support */
  190. init_xilinx_timecounter();
  191. return 0;
  192. }
  193. /*
  194. * We have to protect accesses before timer initialization
  195. * and return 0 for sched_clock function below.
  196. */
  197. static int timer_initialized;
  198. static void __init xilinx_timer_init(struct device_node *timer)
  199. {
  200. u32 irq;
  201. u32 timer_num = 1;
  202. int ret;
  203. timer_baseaddr = of_iomap(timer, 0);
  204. if (!timer_baseaddr) {
  205. pr_err("ERROR: invalid timer base address\n");
  206. BUG();
  207. }
  208. irq = irq_of_parse_and_map(timer, 0);
  209. of_property_read_u32(timer, "xlnx,one-timer-only", &timer_num);
  210. if (timer_num) {
  211. pr_emerg("Please enable two timers in HW\n");
  212. BUG();
  213. }
  214. pr_info("%s: irq=%d\n", timer->full_name, irq);
  215. /* If there is clock-frequency property than use it */
  216. ret = of_property_read_u32(timer, "clock-frequency", &timer_clock_freq);
  217. if (ret < 0)
  218. timer_clock_freq = cpuinfo.cpu_clock_freq;
  219. freq_div_hz = timer_clock_freq / HZ;
  220. setup_irq(irq, &timer_irqaction);
  221. #ifdef CONFIG_HEART_BEAT
  222. setup_heartbeat();
  223. #endif
  224. xilinx_clocksource_init();
  225. xilinx_clockevent_init();
  226. timer_initialized = 1;
  227. }
  228. unsigned long long notrace sched_clock(void)
  229. {
  230. if (timer_initialized) {
  231. struct clocksource *cs = &clocksource_microblaze;
  232. cycle_t cyc = cnt32_to_63(cs->read(NULL)) & LLONG_MAX;
  233. return clocksource_cyc2ns(cyc, cs->mult, cs->shift);
  234. }
  235. return 0;
  236. }
  237. CLOCKSOURCE_OF_DECLARE(xilinx_timer, "xlnx,xps-timer-1.00.a",
  238. xilinx_timer_init);