timer.c 7.9 KB

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