timer.c 8.0 KB

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