timer.c 7.8 KB

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