timer.c 7.4 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/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 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 = 24,
  177. };
  178. int __init init_microblaze_timecounter(void)
  179. {
  180. microblaze_cc.mult = div_sc(cpuinfo.cpu_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. .shift = 24, /* I can shift it */
  191. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  192. };
  193. static int __init microblaze_clocksource_init(void)
  194. {
  195. clocksource_microblaze.mult =
  196. clocksource_hz2mult(cpuinfo.cpu_clock_freq,
  197. clocksource_microblaze.shift);
  198. if (clocksource_register(&clocksource_microblaze))
  199. panic("failed to register clocksource");
  200. /* stop timer1 */
  201. out_be32(TIMER_BASE + TCSR1, in_be32(TIMER_BASE + TCSR1) & ~TCSR_ENT);
  202. /* start timer1 - up counting without interrupt */
  203. out_be32(TIMER_BASE + TCSR1, TCSR_TINT|TCSR_ENT|TCSR_ARHT);
  204. /* register timecounter - for ftrace support */
  205. init_microblaze_timecounter();
  206. return 0;
  207. }
  208. void __init time_init(void)
  209. {
  210. u32 irq, i = 0;
  211. u32 timer_num = 1;
  212. struct device_node *timer = NULL;
  213. #ifdef CONFIG_SELFMOD_TIMER
  214. unsigned int timer_baseaddr = 0;
  215. int arr_func[] = {
  216. (int)&microblaze_read,
  217. (int)&timer_interrupt,
  218. (int)&microblaze_clocksource_init,
  219. (int)&microblaze_timer_set_mode,
  220. (int)&microblaze_timer_set_next_event,
  221. 0
  222. };
  223. #endif
  224. char *timer_list[] = {
  225. "xlnx,xps-timer-1.00.a",
  226. "xlnx,opb-timer-1.00.b",
  227. "xlnx,opb-timer-1.00.a",
  228. NULL
  229. };
  230. for (i = 0; timer_list[i] != NULL; i++) {
  231. timer = of_find_compatible_node(NULL, NULL, timer_list[i]);
  232. if (timer)
  233. break;
  234. }
  235. BUG_ON(!timer);
  236. timer_baseaddr = *(int *) of_get_property(timer, "reg", NULL);
  237. timer_baseaddr = (unsigned long) ioremap(timer_baseaddr, PAGE_SIZE);
  238. irq = *(int *) of_get_property(timer, "interrupts", NULL);
  239. timer_num =
  240. *(int *) of_get_property(timer, "xlnx,one-timer-only", NULL);
  241. if (timer_num) {
  242. printk(KERN_EMERG "Please enable two timers in HW\n");
  243. BUG();
  244. }
  245. #ifdef CONFIG_SELFMOD_TIMER
  246. selfmod_function((int *) arr_func, timer_baseaddr);
  247. #endif
  248. printk(KERN_INFO "%s #0 at 0x%08x, irq=%d\n",
  249. timer_list[i], timer_baseaddr, irq);
  250. cpuinfo.freq_div_hz = cpuinfo.cpu_clock_freq / HZ;
  251. setup_irq(irq, &timer_irqaction);
  252. #ifdef CONFIG_HEART_BEAT
  253. setup_heartbeat();
  254. #endif
  255. microblaze_clocksource_init();
  256. microblaze_clockevent_init();
  257. }