timer.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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 int xilinx_timer_shutdown(struct clock_event_device *evt)
  107. {
  108. pr_info("%s\n", __func__);
  109. xilinx_timer0_stop();
  110. return 0;
  111. }
  112. static int xilinx_timer_set_periodic(struct clock_event_device *evt)
  113. {
  114. pr_info("%s\n", __func__);
  115. xilinx_timer0_start_periodic(freq_div_hz);
  116. return 0;
  117. }
  118. static struct clock_event_device clockevent_xilinx_timer = {
  119. .name = "xilinx_clockevent",
  120. .features = CLOCK_EVT_FEAT_ONESHOT |
  121. CLOCK_EVT_FEAT_PERIODIC,
  122. .shift = 8,
  123. .rating = 300,
  124. .set_next_event = xilinx_timer_set_next_event,
  125. .set_state_shutdown = xilinx_timer_shutdown,
  126. .set_state_periodic = xilinx_timer_set_periodic,
  127. };
  128. static inline void timer_ack(void)
  129. {
  130. write_fn(read_fn(timer_baseaddr + TCSR0), timer_baseaddr + TCSR0);
  131. }
  132. static irqreturn_t timer_interrupt(int irq, void *dev_id)
  133. {
  134. struct clock_event_device *evt = &clockevent_xilinx_timer;
  135. #ifdef CONFIG_HEART_BEAT
  136. microblaze_heartbeat();
  137. #endif
  138. timer_ack();
  139. evt->event_handler(evt);
  140. return IRQ_HANDLED;
  141. }
  142. static struct irqaction timer_irqaction = {
  143. .handler = timer_interrupt,
  144. .flags = IRQF_TIMER,
  145. .name = "timer",
  146. .dev_id = &clockevent_xilinx_timer,
  147. };
  148. static __init int xilinx_clockevent_init(void)
  149. {
  150. clockevent_xilinx_timer.mult =
  151. div_sc(timer_clock_freq, NSEC_PER_SEC,
  152. clockevent_xilinx_timer.shift);
  153. clockevent_xilinx_timer.max_delta_ns =
  154. clockevent_delta2ns((u32)~0, &clockevent_xilinx_timer);
  155. clockevent_xilinx_timer.min_delta_ns =
  156. clockevent_delta2ns(1, &clockevent_xilinx_timer);
  157. clockevent_xilinx_timer.cpumask = cpumask_of(0);
  158. clockevents_register_device(&clockevent_xilinx_timer);
  159. return 0;
  160. }
  161. static u64 xilinx_clock_read(void)
  162. {
  163. return read_fn(timer_baseaddr + TCR1);
  164. }
  165. static cycle_t xilinx_read(struct clocksource *cs)
  166. {
  167. /* reading actual value of timer 1 */
  168. return (cycle_t)xilinx_clock_read();
  169. }
  170. static struct timecounter xilinx_tc = {
  171. .cc = NULL,
  172. };
  173. static cycle_t xilinx_cc_read(const struct cyclecounter *cc)
  174. {
  175. return xilinx_read(NULL);
  176. }
  177. static struct cyclecounter xilinx_cc = {
  178. .read = xilinx_cc_read,
  179. .mask = CLOCKSOURCE_MASK(32),
  180. .shift = 8,
  181. };
  182. static int __init init_xilinx_timecounter(void)
  183. {
  184. xilinx_cc.mult = div_sc(timer_clock_freq, NSEC_PER_SEC,
  185. xilinx_cc.shift);
  186. timecounter_init(&xilinx_tc, &xilinx_cc, sched_clock());
  187. return 0;
  188. }
  189. static struct clocksource clocksource_microblaze = {
  190. .name = "xilinx_clocksource",
  191. .rating = 300,
  192. .read = xilinx_read,
  193. .mask = CLOCKSOURCE_MASK(32),
  194. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  195. };
  196. static int __init xilinx_clocksource_init(void)
  197. {
  198. int ret;
  199. ret = clocksource_register_hz(&clocksource_microblaze,
  200. timer_clock_freq);
  201. if (ret) {
  202. pr_err("failed to register clocksource");
  203. return ret;
  204. }
  205. /* stop timer1 */
  206. write_fn(read_fn(timer_baseaddr + TCSR1) & ~TCSR_ENT,
  207. timer_baseaddr + TCSR1);
  208. /* start timer1 - up counting without interrupt */
  209. write_fn(TCSR_TINT|TCSR_ENT|TCSR_ARHT, timer_baseaddr + TCSR1);
  210. /* register timecounter - for ftrace support */
  211. return init_xilinx_timecounter();
  212. }
  213. static int __init xilinx_timer_init(struct device_node *timer)
  214. {
  215. struct clk *clk;
  216. static int initialized;
  217. u32 irq;
  218. u32 timer_num = 1;
  219. int ret;
  220. if (initialized)
  221. return;
  222. initialized = 1;
  223. timer_baseaddr = of_iomap(timer, 0);
  224. if (!timer_baseaddr) {
  225. pr_err("ERROR: invalid timer base address\n");
  226. return -ENXIO;
  227. }
  228. write_fn = timer_write32;
  229. read_fn = timer_read32;
  230. write_fn(TCSR_MDT, timer_baseaddr + TCSR0);
  231. if (!(read_fn(timer_baseaddr + TCSR0) & TCSR_MDT)) {
  232. write_fn = timer_write32_be;
  233. read_fn = timer_read32_be;
  234. }
  235. irq = irq_of_parse_and_map(timer, 0);
  236. if (irq <= 0) {
  237. pr_err("Failed to parse and map irq");
  238. return -EINVAL;
  239. }
  240. of_property_read_u32(timer, "xlnx,one-timer-only", &timer_num);
  241. if (timer_num) {
  242. pr_err("Please enable two timers in HW\n");
  243. return -EINVAL;
  244. }
  245. pr_info("%s: irq=%d\n", timer->full_name, irq);
  246. clk = of_clk_get(timer, 0);
  247. if (IS_ERR(clk)) {
  248. pr_err("ERROR: timer CCF input clock not found\n");
  249. /* If there is clock-frequency property than use it */
  250. of_property_read_u32(timer, "clock-frequency",
  251. &timer_clock_freq);
  252. } else {
  253. timer_clock_freq = clk_get_rate(clk);
  254. }
  255. if (!timer_clock_freq) {
  256. pr_err("ERROR: Using CPU clock frequency\n");
  257. timer_clock_freq = cpuinfo.cpu_clock_freq;
  258. }
  259. freq_div_hz = timer_clock_freq / HZ;
  260. ret = setup_irq(irq, &timer_irqaction);
  261. if (ret) {
  262. pr_err("Failed to setup IRQ");
  263. return ret;
  264. }
  265. #ifdef CONFIG_HEART_BEAT
  266. microblaze_setup_heartbeat();
  267. #endif
  268. ret = xilinx_clocksource_init();
  269. if (ret)
  270. return ret;
  271. ret = xilinx_clockevent_init();
  272. if (ret)
  273. return ret;
  274. sched_clock_register(xilinx_clock_read, 32, timer_clock_freq);
  275. return 0;
  276. }
  277. CLOCKSOURCE_OF_DECLARE(xilinx_timer, "xlnx,xps-timer-1.00.a",
  278. xilinx_timer_init);