timer.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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. #ifdef CONFIG_SELFMOD_TIMER
  31. #include <asm/selfmod.h>
  32. #define TIMER_BASE BARRIER_BASE_ADDR
  33. #else
  34. static unsigned int timer_baseaddr;
  35. #define TIMER_BASE timer_baseaddr
  36. #endif
  37. #define TCSR0 (0x00)
  38. #define TLR0 (0x04)
  39. #define TCR0 (0x08)
  40. #define TCSR1 (0x10)
  41. #define TLR1 (0x14)
  42. #define TCR1 (0x18)
  43. #define TCSR_MDT (1<<0)
  44. #define TCSR_UDT (1<<1)
  45. #define TCSR_GENT (1<<2)
  46. #define TCSR_CAPT (1<<3)
  47. #define TCSR_ARHT (1<<4)
  48. #define TCSR_LOAD (1<<5)
  49. #define TCSR_ENIT (1<<6)
  50. #define TCSR_ENT (1<<7)
  51. #define TCSR_TINT (1<<8)
  52. #define TCSR_PWMA (1<<9)
  53. #define TCSR_ENALL (1<<10)
  54. static inline void microblaze_timer0_stop(void)
  55. {
  56. out_be32(TIMER_BASE + TCSR0, in_be32(TIMER_BASE + TCSR0) & ~TCSR_ENT);
  57. }
  58. static inline void microblaze_timer0_start_periodic(unsigned long load_val)
  59. {
  60. if (!load_val)
  61. load_val = 1;
  62. out_be32(TIMER_BASE + TLR0, load_val); /* loading value to timer reg */
  63. /* load the initial value */
  64. out_be32(TIMER_BASE + TCSR0, TCSR_LOAD);
  65. /* see timer data sheet for detail
  66. * !ENALL - don't enable 'em all
  67. * !PWMA - disable pwm
  68. * TINT - clear interrupt status
  69. * ENT- enable timer itself
  70. * EINT - enable interrupt
  71. * !LOAD - clear the bit to let go
  72. * ARHT - auto reload
  73. * !CAPT - no external trigger
  74. * !GENT - no external signal
  75. * UDT - set the timer as down counter
  76. * !MDT0 - generate mode
  77. */
  78. out_be32(TIMER_BASE + TCSR0,
  79. TCSR_TINT|TCSR_ENIT|TCSR_ENT|TCSR_ARHT|TCSR_UDT);
  80. }
  81. static inline void microblaze_timer0_start_oneshot(unsigned long load_val)
  82. {
  83. if (!load_val)
  84. load_val = 1;
  85. out_be32(TIMER_BASE + TLR0, load_val); /* loading value to timer reg */
  86. /* load the initial value */
  87. out_be32(TIMER_BASE + TCSR0, TCSR_LOAD);
  88. out_be32(TIMER_BASE + 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. printk(KERN_INFO "%s: periodic\n", __func__);
  104. microblaze_timer0_start_periodic(cpuinfo.freq_div_hz);
  105. break;
  106. case CLOCK_EVT_MODE_ONESHOT:
  107. printk(KERN_INFO "%s: oneshot\n", __func__);
  108. break;
  109. case CLOCK_EVT_MODE_UNUSED:
  110. printk(KERN_INFO "%s: unused\n", __func__);
  111. break;
  112. case CLOCK_EVT_MODE_SHUTDOWN:
  113. printk(KERN_INFO "%s: shutdown\n", __func__);
  114. microblaze_timer0_stop();
  115. break;
  116. case CLOCK_EVT_MODE_RESUME:
  117. printk(KERN_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 = 24,
  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_BASE + TCSR0, in_be32(TIMER_BASE + 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(cpuinfo.cpu_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_BASE + TCR1));
  165. }
  166. static struct clocksource clocksource_microblaze = {
  167. .name = "microblaze_clocksource",
  168. .rating = 300,
  169. .read = microblaze_read,
  170. .mask = CLOCKSOURCE_MASK(32),
  171. .shift = 24, /* I can shift it */
  172. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  173. };
  174. static int __init microblaze_clocksource_init(void)
  175. {
  176. clocksource_microblaze.mult =
  177. clocksource_hz2mult(cpuinfo.cpu_clock_freq,
  178. clocksource_microblaze.shift);
  179. if (clocksource_register(&clocksource_microblaze))
  180. panic("failed to register clocksource");
  181. /* stop timer1 */
  182. out_be32(TIMER_BASE + TCSR1, in_be32(TIMER_BASE + TCSR1) & ~TCSR_ENT);
  183. /* start timer1 - up counting without interrupt */
  184. out_be32(TIMER_BASE + TCSR1, TCSR_TINT|TCSR_ENT|TCSR_ARHT);
  185. return 0;
  186. }
  187. void __init time_init(void)
  188. {
  189. u32 irq, i = 0;
  190. u32 timer_num = 1;
  191. struct device_node *timer = NULL;
  192. #ifdef CONFIG_SELFMOD_TIMER
  193. unsigned int timer_baseaddr = 0;
  194. int arr_func[] = {
  195. (int)&microblaze_read,
  196. (int)&timer_interrupt,
  197. (int)&microblaze_clocksource_init,
  198. (int)&microblaze_timer_set_mode,
  199. (int)&microblaze_timer_set_next_event,
  200. 0
  201. };
  202. #endif
  203. char *timer_list[] = {
  204. "xlnx,xps-timer-1.00.a",
  205. "xlnx,opb-timer-1.00.b",
  206. "xlnx,opb-timer-1.00.a",
  207. NULL
  208. };
  209. for (i = 0; timer_list[i] != NULL; i++) {
  210. timer = of_find_compatible_node(NULL, NULL, timer_list[i]);
  211. if (timer)
  212. break;
  213. }
  214. BUG_ON(!timer);
  215. timer_baseaddr = *(int *) of_get_property(timer, "reg", NULL);
  216. timer_baseaddr = (unsigned long) ioremap(timer_baseaddr, PAGE_SIZE);
  217. irq = *(int *) of_get_property(timer, "interrupts", NULL);
  218. timer_num =
  219. *(int *) of_get_property(timer, "xlnx,one-timer-only", NULL);
  220. if (timer_num) {
  221. printk(KERN_EMERG "Please enable two timers in HW\n");
  222. BUG();
  223. }
  224. #ifdef CONFIG_SELFMOD_TIMER
  225. selfmod_function((int *) arr_func, timer_baseaddr);
  226. #endif
  227. printk(KERN_INFO "%s #0 at 0x%08x, irq=%d\n",
  228. timer_list[i], timer_baseaddr, irq);
  229. cpuinfo.freq_div_hz = cpuinfo.cpu_clock_freq / HZ;
  230. setup_irq(irq, &timer_irqaction);
  231. #ifdef CONFIG_HEART_BEAT
  232. setup_heartbeat();
  233. #endif
  234. microblaze_clocksource_init();
  235. microblaze_clockevent_init();
  236. }