timer.c 6.8 KB

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