timer.c 7.2 KB

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