timer.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. /*
  2. * Copyright (C) 2007-2013 Michal Simek <monstr@monstr.eu>
  3. * Copyright (C) 2012-2013 Xilinx, Inc.
  4. * Copyright (C) 2007-2009 PetaLogix
  5. * Copyright (C) 2006 Atmark Techno, Inc.
  6. *
  7. * This file is subject to the terms and conditions of the GNU General Public
  8. * License. See the file "COPYING" in the main directory of this archive
  9. * for more details.
  10. */
  11. #include <linux/interrupt.h>
  12. #include <linux/delay.h>
  13. #include <linux/sched.h>
  14. #include <linux/sched_clock.h>
  15. #include <linux/clk.h>
  16. #include <linux/clockchips.h>
  17. #include <linux/of_address.h>
  18. #include <linux/of_irq.h>
  19. #include <linux/timecounter.h>
  20. #include <asm/cpuinfo.h>
  21. static void __iomem *timer_baseaddr;
  22. static unsigned int freq_div_hz;
  23. static unsigned int timer_clock_freq;
  24. #define TCSR0 (0x00)
  25. #define TLR0 (0x04)
  26. #define TCR0 (0x08)
  27. #define TCSR1 (0x10)
  28. #define TLR1 (0x14)
  29. #define TCR1 (0x18)
  30. #define TCSR_MDT (1<<0)
  31. #define TCSR_UDT (1<<1)
  32. #define TCSR_GENT (1<<2)
  33. #define TCSR_CAPT (1<<3)
  34. #define TCSR_ARHT (1<<4)
  35. #define TCSR_LOAD (1<<5)
  36. #define TCSR_ENIT (1<<6)
  37. #define TCSR_ENT (1<<7)
  38. #define TCSR_TINT (1<<8)
  39. #define TCSR_PWMA (1<<9)
  40. #define TCSR_ENALL (1<<10)
  41. static unsigned int (*read_fn)(void __iomem *);
  42. static void (*write_fn)(u32, void __iomem *);
  43. static void timer_write32(u32 val, void __iomem *addr)
  44. {
  45. iowrite32(val, addr);
  46. }
  47. static unsigned int timer_read32(void __iomem *addr)
  48. {
  49. return ioread32(addr);
  50. }
  51. static void timer_write32_be(u32 val, void __iomem *addr)
  52. {
  53. iowrite32be(val, addr);
  54. }
  55. static unsigned int timer_read32_be(void __iomem *addr)
  56. {
  57. return ioread32be(addr);
  58. }
  59. static inline void xilinx_timer0_stop(void)
  60. {
  61. write_fn(read_fn(timer_baseaddr + TCSR0) & ~TCSR_ENT,
  62. timer_baseaddr + TCSR0);
  63. }
  64. static inline void xilinx_timer0_start_periodic(unsigned long load_val)
  65. {
  66. if (!load_val)
  67. load_val = 1;
  68. /* loading value to timer reg */
  69. write_fn(load_val, timer_baseaddr + TLR0);
  70. /* load the initial value */
  71. write_fn(TCSR_LOAD, timer_baseaddr + TCSR0);
  72. /* see timer data sheet for detail
  73. * !ENALL - don't enable 'em all
  74. * !PWMA - disable pwm
  75. * TINT - clear interrupt status
  76. * ENT- enable timer itself
  77. * ENIT - enable interrupt
  78. * !LOAD - clear the bit to let go
  79. * ARHT - auto reload
  80. * !CAPT - no external trigger
  81. * !GENT - no external signal
  82. * UDT - set the timer as down counter
  83. * !MDT0 - generate mode
  84. */
  85. write_fn(TCSR_TINT|TCSR_ENIT|TCSR_ENT|TCSR_ARHT|TCSR_UDT,
  86. timer_baseaddr + TCSR0);
  87. }
  88. static inline void xilinx_timer0_start_oneshot(unsigned long load_val)
  89. {
  90. if (!load_val)
  91. load_val = 1;
  92. /* loading value to timer reg */
  93. write_fn(load_val, timer_baseaddr + TLR0);
  94. /* load the initial value */
  95. write_fn(TCSR_LOAD, timer_baseaddr + TCSR0);
  96. write_fn(TCSR_TINT|TCSR_ENIT|TCSR_ENT|TCSR_ARHT|TCSR_UDT,
  97. timer_baseaddr + TCSR0);
  98. }
  99. static int xilinx_timer_set_next_event(unsigned long delta,
  100. struct clock_event_device *dev)
  101. {
  102. pr_debug("%s: next event, delta %x\n", __func__, (u32)delta);
  103. xilinx_timer0_start_oneshot(delta);
  104. return 0;
  105. }
  106. static void xilinx_timer_set_mode(enum clock_event_mode mode,
  107. struct clock_event_device *evt)
  108. {
  109. switch (mode) {
  110. case CLOCK_EVT_MODE_PERIODIC:
  111. pr_info("%s: periodic\n", __func__);
  112. xilinx_timer0_start_periodic(freq_div_hz);
  113. break;
  114. case CLOCK_EVT_MODE_ONESHOT:
  115. pr_info("%s: oneshot\n", __func__);
  116. break;
  117. case CLOCK_EVT_MODE_UNUSED:
  118. pr_info("%s: unused\n", __func__);
  119. break;
  120. case CLOCK_EVT_MODE_SHUTDOWN:
  121. pr_info("%s: shutdown\n", __func__);
  122. xilinx_timer0_stop();
  123. break;
  124. case CLOCK_EVT_MODE_RESUME:
  125. pr_info("%s: resume\n", __func__);
  126. break;
  127. }
  128. }
  129. static struct clock_event_device clockevent_xilinx_timer = {
  130. .name = "xilinx_clockevent",
  131. .features = CLOCK_EVT_FEAT_ONESHOT | CLOCK_EVT_FEAT_PERIODIC,
  132. .shift = 8,
  133. .rating = 300,
  134. .set_next_event = xilinx_timer_set_next_event,
  135. .set_mode = xilinx_timer_set_mode,
  136. };
  137. static inline void timer_ack(void)
  138. {
  139. write_fn(read_fn(timer_baseaddr + TCSR0), timer_baseaddr + TCSR0);
  140. }
  141. static irqreturn_t timer_interrupt(int irq, void *dev_id)
  142. {
  143. struct clock_event_device *evt = &clockevent_xilinx_timer;
  144. #ifdef CONFIG_HEART_BEAT
  145. microblaze_heartbeat();
  146. #endif
  147. timer_ack();
  148. evt->event_handler(evt);
  149. return IRQ_HANDLED;
  150. }
  151. static struct irqaction timer_irqaction = {
  152. .handler = timer_interrupt,
  153. .flags = IRQF_TIMER,
  154. .name = "timer",
  155. .dev_id = &clockevent_xilinx_timer,
  156. };
  157. static __init void xilinx_clockevent_init(void)
  158. {
  159. clockevent_xilinx_timer.mult =
  160. div_sc(timer_clock_freq, NSEC_PER_SEC,
  161. clockevent_xilinx_timer.shift);
  162. clockevent_xilinx_timer.max_delta_ns =
  163. clockevent_delta2ns((u32)~0, &clockevent_xilinx_timer);
  164. clockevent_xilinx_timer.min_delta_ns =
  165. clockevent_delta2ns(1, &clockevent_xilinx_timer);
  166. clockevent_xilinx_timer.cpumask = cpumask_of(0);
  167. clockevents_register_device(&clockevent_xilinx_timer);
  168. }
  169. static u64 xilinx_clock_read(void)
  170. {
  171. return read_fn(timer_baseaddr + TCR1);
  172. }
  173. static cycle_t xilinx_read(struct clocksource *cs)
  174. {
  175. /* reading actual value of timer 1 */
  176. return (cycle_t)xilinx_clock_read();
  177. }
  178. static struct timecounter xilinx_tc = {
  179. .cc = NULL,
  180. };
  181. static cycle_t xilinx_cc_read(const struct cyclecounter *cc)
  182. {
  183. return xilinx_read(NULL);
  184. }
  185. static struct cyclecounter xilinx_cc = {
  186. .read = xilinx_cc_read,
  187. .mask = CLOCKSOURCE_MASK(32),
  188. .shift = 8,
  189. };
  190. static int __init init_xilinx_timecounter(void)
  191. {
  192. xilinx_cc.mult = div_sc(timer_clock_freq, NSEC_PER_SEC,
  193. xilinx_cc.shift);
  194. timecounter_init(&xilinx_tc, &xilinx_cc, sched_clock());
  195. return 0;
  196. }
  197. static struct clocksource clocksource_microblaze = {
  198. .name = "xilinx_clocksource",
  199. .rating = 300,
  200. .read = xilinx_read,
  201. .mask = CLOCKSOURCE_MASK(32),
  202. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  203. };
  204. static int __init xilinx_clocksource_init(void)
  205. {
  206. if (clocksource_register_hz(&clocksource_microblaze, timer_clock_freq))
  207. panic("failed to register clocksource");
  208. /* stop timer1 */
  209. write_fn(read_fn(timer_baseaddr + TCSR1) & ~TCSR_ENT,
  210. timer_baseaddr + TCSR1);
  211. /* start timer1 - up counting without interrupt */
  212. write_fn(TCSR_TINT|TCSR_ENT|TCSR_ARHT, timer_baseaddr + TCSR1);
  213. /* register timecounter - for ftrace support */
  214. init_xilinx_timecounter();
  215. return 0;
  216. }
  217. static void __init xilinx_timer_init(struct device_node *timer)
  218. {
  219. struct clk *clk;
  220. static int initialized;
  221. u32 irq;
  222. u32 timer_num = 1;
  223. if (initialized)
  224. return;
  225. initialized = 1;
  226. timer_baseaddr = of_iomap(timer, 0);
  227. if (!timer_baseaddr) {
  228. pr_err("ERROR: invalid timer base address\n");
  229. BUG();
  230. }
  231. write_fn = timer_write32;
  232. read_fn = timer_read32;
  233. write_fn(TCSR_MDT, timer_baseaddr + TCSR0);
  234. if (!(read_fn(timer_baseaddr + TCSR0) & TCSR_MDT)) {
  235. write_fn = timer_write32_be;
  236. read_fn = timer_read32_be;
  237. }
  238. irq = irq_of_parse_and_map(timer, 0);
  239. of_property_read_u32(timer, "xlnx,one-timer-only", &timer_num);
  240. if (timer_num) {
  241. pr_emerg("Please enable two timers in HW\n");
  242. BUG();
  243. }
  244. pr_info("%s: irq=%d\n", timer->full_name, irq);
  245. clk = of_clk_get(timer, 0);
  246. if (IS_ERR(clk)) {
  247. pr_err("ERROR: timer CCF input clock not found\n");
  248. /* If there is clock-frequency property than use it */
  249. of_property_read_u32(timer, "clock-frequency",
  250. &timer_clock_freq);
  251. } else {
  252. timer_clock_freq = clk_get_rate(clk);
  253. }
  254. if (!timer_clock_freq) {
  255. pr_err("ERROR: Using CPU clock frequency\n");
  256. timer_clock_freq = cpuinfo.cpu_clock_freq;
  257. }
  258. freq_div_hz = timer_clock_freq / HZ;
  259. setup_irq(irq, &timer_irqaction);
  260. #ifdef CONFIG_HEART_BEAT
  261. microblaze_setup_heartbeat();
  262. #endif
  263. xilinx_clocksource_init();
  264. xilinx_clockevent_init();
  265. sched_clock_register(xilinx_clock_read, 32, timer_clock_freq);
  266. }
  267. CLOCKSOURCE_OF_DECLARE(xilinx_timer, "xlnx,xps-timer-1.00.a",
  268. xilinx_timer_init);