timer.c 6.9 KB

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