timer.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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 <asm/cpuinfo.h>
  25. #include <asm/setup.h>
  26. #include <asm/prom.h>
  27. #include <asm/irq.h>
  28. #include <asm/system.h>
  29. #ifdef CONFIG_SELFMOD_TIMER
  30. #include <asm/selfmod.h>
  31. #define TIMER_BASE BARRIER_BASE_ADDR
  32. #else
  33. static unsigned int timer_baseaddr;
  34. #define TIMER_BASE timer_baseaddr
  35. #endif
  36. #define TCSR0 (0x00)
  37. #define TLR0 (0x04)
  38. #define TCR0 (0x08)
  39. #define TCSR1 (0x10)
  40. #define TLR1 (0x14)
  41. #define TCR1 (0x18)
  42. #define TCSR_MDT (1<<0)
  43. #define TCSR_UDT (1<<1)
  44. #define TCSR_GENT (1<<2)
  45. #define TCSR_CAPT (1<<3)
  46. #define TCSR_ARHT (1<<4)
  47. #define TCSR_LOAD (1<<5)
  48. #define TCSR_ENIT (1<<6)
  49. #define TCSR_ENT (1<<7)
  50. #define TCSR_TINT (1<<8)
  51. #define TCSR_PWMA (1<<9)
  52. #define TCSR_ENALL (1<<10)
  53. static inline void microblaze_timer0_stop(void)
  54. {
  55. out_be32(TIMER_BASE + TCSR0, in_be32(TIMER_BASE + TCSR0) & ~TCSR_ENT);
  56. }
  57. static inline void microblaze_timer0_start_periodic(unsigned long load_val)
  58. {
  59. if (!load_val)
  60. load_val = 1;
  61. out_be32(TIMER_BASE + TLR0, load_val); /* loading value to timer reg */
  62. /* load the initial value */
  63. out_be32(TIMER_BASE + 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. * EINT - 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_BASE + 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. out_be32(TIMER_BASE + TLR0, load_val); /* loading value to timer reg */
  85. /* load the initial value */
  86. out_be32(TIMER_BASE + TCSR0, TCSR_LOAD);
  87. out_be32(TIMER_BASE + 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. printk(KERN_INFO "%s: periodic\n", __func__);
  103. microblaze_timer0_start_periodic(cpuinfo.freq_div_hz);
  104. break;
  105. case CLOCK_EVT_MODE_ONESHOT:
  106. printk(KERN_INFO "%s: oneshot\n", __func__);
  107. break;
  108. case CLOCK_EVT_MODE_UNUSED:
  109. printk(KERN_INFO "%s: unused\n", __func__);
  110. break;
  111. case CLOCK_EVT_MODE_SHUTDOWN:
  112. printk(KERN_INFO "%s: shutdown\n", __func__);
  113. microblaze_timer0_stop();
  114. break;
  115. case CLOCK_EVT_MODE_RESUME:
  116. printk(KERN_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 = 24,
  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_BASE + TCSR0, in_be32(TIMER_BASE + 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(cpuinfo.cpu_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_BASE + TCR1));
  164. }
  165. static struct clocksource clocksource_microblaze = {
  166. .name = "microblaze_clocksource",
  167. .rating = 300,
  168. .read = microblaze_read,
  169. .mask = CLOCKSOURCE_MASK(32),
  170. .shift = 24, /* I can shift it */
  171. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  172. };
  173. static int __init microblaze_clocksource_init(void)
  174. {
  175. clocksource_microblaze.mult =
  176. clocksource_hz2mult(cpuinfo.cpu_clock_freq,
  177. clocksource_microblaze.shift);
  178. if (clocksource_register(&clocksource_microblaze))
  179. panic("failed to register clocksource");
  180. /* stop timer1 */
  181. out_be32(TIMER_BASE + TCSR1, in_be32(TIMER_BASE + TCSR1) & ~TCSR_ENT);
  182. /* start timer1 - up counting without interrupt */
  183. out_be32(TIMER_BASE + TCSR1, TCSR_TINT|TCSR_ENT|TCSR_ARHT);
  184. return 0;
  185. }
  186. void __init time_init(void)
  187. {
  188. u32 irq, i = 0;
  189. u32 timer_num = 1;
  190. struct device_node *timer = NULL;
  191. #ifdef CONFIG_SELFMOD_TIMER
  192. unsigned int timer_baseaddr = 0;
  193. int arr_func[] = {
  194. (int)&microblaze_read,
  195. (int)&timer_interrupt,
  196. (int)&microblaze_clocksource_init,
  197. (int)&microblaze_timer_set_mode,
  198. (int)&microblaze_timer_set_next_event,
  199. 0
  200. };
  201. #endif
  202. char *timer_list[] = {
  203. "xlnx,xps-timer-1.00.a",
  204. "xlnx,opb-timer-1.00.b",
  205. "xlnx,opb-timer-1.00.a",
  206. NULL
  207. };
  208. for (i = 0; timer_list[i] != NULL; i++) {
  209. timer = of_find_compatible_node(NULL, NULL, timer_list[i]);
  210. if (timer)
  211. break;
  212. }
  213. timer_baseaddr = *(int *) of_get_property(timer, "reg", NULL);
  214. timer_baseaddr = (unsigned long) ioremap(timer_baseaddr, PAGE_SIZE);
  215. irq = *(int *) of_get_property(timer, "interrupts", NULL);
  216. timer_num =
  217. *(int *) of_get_property(timer, "xlnx,one-timer-only", NULL);
  218. if (timer_num) {
  219. printk(KERN_EMERG "Please enable two timers in HW\n");
  220. BUG();
  221. }
  222. #ifdef CONFIG_SELFMOD_TIMER
  223. selfmod_function((int *) arr_func, timer_baseaddr);
  224. #endif
  225. printk(KERN_INFO "%s #0 at 0x%08x, irq=%d\n",
  226. timer_list[i], timer_baseaddr, irq);
  227. cpuinfo.freq_div_hz = cpuinfo.cpu_clock_freq / HZ;
  228. setup_irq(irq, &timer_irqaction);
  229. #ifdef CONFIG_HEART_BEAT
  230. setup_heartbeat();
  231. #endif
  232. microblaze_clocksource_init();
  233. microblaze_clockevent_init();
  234. }