timer.c 7.5 KB

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