timer.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. /*
  2. * This file contains driver for the Xilinx PS Timer Counter IP.
  3. *
  4. * Copyright (C) 2011 Xilinx
  5. *
  6. * based on arch/mips/kernel/time.c timer driver
  7. *
  8. * This software is licensed under the terms of the GNU General Public
  9. * License version 2, as published by the Free Software Foundation, and
  10. * may be copied, distributed, and modified under those terms.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. */
  17. #include <linux/interrupt.h>
  18. #include <linux/clockchips.h>
  19. #include <linux/of_address.h>
  20. #include <linux/of_irq.h>
  21. #include <linux/slab.h>
  22. #include <linux/clk-provider.h>
  23. #include "common.h"
  24. /*
  25. * Timer Register Offset Definitions of Timer 1, Increment base address by 4
  26. * and use same offsets for Timer 2
  27. */
  28. #define XTTCPS_CLK_CNTRL_OFFSET 0x00 /* Clock Control Reg, RW */
  29. #define XTTCPS_CNT_CNTRL_OFFSET 0x0C /* Counter Control Reg, RW */
  30. #define XTTCPS_COUNT_VAL_OFFSET 0x18 /* Counter Value Reg, RO */
  31. #define XTTCPS_INTR_VAL_OFFSET 0x24 /* Interval Count Reg, RW */
  32. #define XTTCPS_ISR_OFFSET 0x54 /* Interrupt Status Reg, RO */
  33. #define XTTCPS_IER_OFFSET 0x60 /* Interrupt Enable Reg, RW */
  34. #define XTTCPS_CNT_CNTRL_DISABLE_MASK 0x1
  35. /*
  36. * Setup the timers to use pre-scaling, using a fixed value for now that will
  37. * work across most input frequency, but it may need to be more dynamic
  38. */
  39. #define PRESCALE_EXPONENT 11 /* 2 ^ PRESCALE_EXPONENT = PRESCALE */
  40. #define PRESCALE 2048 /* The exponent must match this */
  41. #define CLK_CNTRL_PRESCALE ((PRESCALE_EXPONENT - 1) << 1)
  42. #define CLK_CNTRL_PRESCALE_EN 1
  43. #define CNT_CNTRL_RESET (1<<4)
  44. /**
  45. * struct xttcps_timer - This definition defines local timer structure
  46. *
  47. * @base_addr: Base address of timer
  48. **/
  49. struct xttcps_timer {
  50. void __iomem *base_addr;
  51. };
  52. struct xttcps_timer_clocksource {
  53. struct xttcps_timer xttc;
  54. struct clocksource cs;
  55. };
  56. #define to_xttcps_timer_clksrc(x) \
  57. container_of(x, struct xttcps_timer_clocksource, cs)
  58. struct xttcps_timer_clockevent {
  59. struct xttcps_timer xttc;
  60. struct clock_event_device ce;
  61. struct clk *clk;
  62. };
  63. #define to_xttcps_timer_clkevent(x) \
  64. container_of(x, struct xttcps_timer_clockevent, ce)
  65. /**
  66. * xttcps_set_interval - Set the timer interval value
  67. *
  68. * @timer: Pointer to the timer instance
  69. * @cycles: Timer interval ticks
  70. **/
  71. static void xttcps_set_interval(struct xttcps_timer *timer,
  72. unsigned long cycles)
  73. {
  74. u32 ctrl_reg;
  75. /* Disable the counter, set the counter value and re-enable counter */
  76. ctrl_reg = __raw_readl(timer->base_addr + XTTCPS_CNT_CNTRL_OFFSET);
  77. ctrl_reg |= XTTCPS_CNT_CNTRL_DISABLE_MASK;
  78. __raw_writel(ctrl_reg, timer->base_addr + XTTCPS_CNT_CNTRL_OFFSET);
  79. __raw_writel(cycles, timer->base_addr + XTTCPS_INTR_VAL_OFFSET);
  80. /*
  81. * Reset the counter (0x10) so that it starts from 0, one-shot
  82. * mode makes this needed for timing to be right.
  83. */
  84. ctrl_reg |= CNT_CNTRL_RESET;
  85. ctrl_reg &= ~XTTCPS_CNT_CNTRL_DISABLE_MASK;
  86. __raw_writel(ctrl_reg, timer->base_addr + XTTCPS_CNT_CNTRL_OFFSET);
  87. }
  88. /**
  89. * xttcps_clock_event_interrupt - Clock event timer interrupt handler
  90. *
  91. * @irq: IRQ number of the Timer
  92. * @dev_id: void pointer to the xttcps_timer instance
  93. *
  94. * returns: Always IRQ_HANDLED - success
  95. **/
  96. static irqreturn_t xttcps_clock_event_interrupt(int irq, void *dev_id)
  97. {
  98. struct xttcps_timer_clockevent *xttce = dev_id;
  99. struct xttcps_timer *timer = &xttce->xttc;
  100. /* Acknowledge the interrupt and call event handler */
  101. __raw_readl(timer->base_addr + XTTCPS_ISR_OFFSET);
  102. xttce->ce.event_handler(&xttce->ce);
  103. return IRQ_HANDLED;
  104. }
  105. /**
  106. * __xttc_clocksource_read - Reads the timer counter register
  107. *
  108. * returns: Current timer counter register value
  109. **/
  110. static cycle_t __xttc_clocksource_read(struct clocksource *cs)
  111. {
  112. struct xttcps_timer *timer = &to_xttcps_timer_clksrc(cs)->xttc;
  113. return (cycle_t)__raw_readl(timer->base_addr +
  114. XTTCPS_COUNT_VAL_OFFSET);
  115. }
  116. /**
  117. * xttcps_set_next_event - Sets the time interval for next event
  118. *
  119. * @cycles: Timer interval ticks
  120. * @evt: Address of clock event instance
  121. *
  122. * returns: Always 0 - success
  123. **/
  124. static int xttcps_set_next_event(unsigned long cycles,
  125. struct clock_event_device *evt)
  126. {
  127. struct xttcps_timer_clockevent *xttce = to_xttcps_timer_clkevent(evt);
  128. struct xttcps_timer *timer = &xttce->xttc;
  129. xttcps_set_interval(timer, cycles);
  130. return 0;
  131. }
  132. /**
  133. * xttcps_set_mode - Sets the mode of timer
  134. *
  135. * @mode: Mode to be set
  136. * @evt: Address of clock event instance
  137. **/
  138. static void xttcps_set_mode(enum clock_event_mode mode,
  139. struct clock_event_device *evt)
  140. {
  141. struct xttcps_timer_clockevent *xttce = to_xttcps_timer_clkevent(evt);
  142. struct xttcps_timer *timer = &xttce->xttc;
  143. u32 ctrl_reg;
  144. switch (mode) {
  145. case CLOCK_EVT_MODE_PERIODIC:
  146. xttcps_set_interval(timer,
  147. DIV_ROUND_CLOSEST(clk_get_rate(xttce->clk),
  148. PRESCALE * HZ));
  149. break;
  150. case CLOCK_EVT_MODE_ONESHOT:
  151. case CLOCK_EVT_MODE_UNUSED:
  152. case CLOCK_EVT_MODE_SHUTDOWN:
  153. ctrl_reg = __raw_readl(timer->base_addr +
  154. XTTCPS_CNT_CNTRL_OFFSET);
  155. ctrl_reg |= XTTCPS_CNT_CNTRL_DISABLE_MASK;
  156. __raw_writel(ctrl_reg,
  157. timer->base_addr + XTTCPS_CNT_CNTRL_OFFSET);
  158. break;
  159. case CLOCK_EVT_MODE_RESUME:
  160. ctrl_reg = __raw_readl(timer->base_addr +
  161. XTTCPS_CNT_CNTRL_OFFSET);
  162. ctrl_reg &= ~XTTCPS_CNT_CNTRL_DISABLE_MASK;
  163. __raw_writel(ctrl_reg,
  164. timer->base_addr + XTTCPS_CNT_CNTRL_OFFSET);
  165. break;
  166. }
  167. }
  168. static void __init zynq_ttc_setup_clocksource(struct device_node *np,
  169. void __iomem *base)
  170. {
  171. struct xttcps_timer_clocksource *ttccs;
  172. struct clk *clk;
  173. int err;
  174. u32 reg;
  175. ttccs = kzalloc(sizeof(*ttccs), GFP_KERNEL);
  176. if (WARN_ON(!ttccs))
  177. return;
  178. err = of_property_read_u32(np, "reg", &reg);
  179. if (WARN_ON(err))
  180. return;
  181. clk = of_clk_get_by_name(np, "cpu_1x");
  182. if (WARN_ON(IS_ERR(clk)))
  183. return;
  184. err = clk_prepare_enable(clk);
  185. if (WARN_ON(err))
  186. return;
  187. ttccs->xttc.base_addr = base + reg * 4;
  188. ttccs->cs.name = np->name;
  189. ttccs->cs.rating = 200;
  190. ttccs->cs.read = __xttc_clocksource_read;
  191. ttccs->cs.mask = CLOCKSOURCE_MASK(16);
  192. ttccs->cs.flags = CLOCK_SOURCE_IS_CONTINUOUS;
  193. __raw_writel(0x0, ttccs->xttc.base_addr + XTTCPS_IER_OFFSET);
  194. __raw_writel(CLK_CNTRL_PRESCALE | CLK_CNTRL_PRESCALE_EN,
  195. ttccs->xttc.base_addr + XTTCPS_CLK_CNTRL_OFFSET);
  196. __raw_writel(CNT_CNTRL_RESET,
  197. ttccs->xttc.base_addr + XTTCPS_CNT_CNTRL_OFFSET);
  198. err = clocksource_register_hz(&ttccs->cs, clk_get_rate(clk) / PRESCALE);
  199. if (WARN_ON(err))
  200. return;
  201. }
  202. static void __init zynq_ttc_setup_clockevent(struct device_node *np,
  203. void __iomem *base)
  204. {
  205. struct xttcps_timer_clockevent *ttcce;
  206. int err, irq;
  207. u32 reg;
  208. ttcce = kzalloc(sizeof(*ttcce), GFP_KERNEL);
  209. if (WARN_ON(!ttcce))
  210. return;
  211. err = of_property_read_u32(np, "reg", &reg);
  212. if (WARN_ON(err))
  213. return;
  214. ttcce->xttc.base_addr = base + reg * 4;
  215. ttcce->clk = of_clk_get_by_name(np, "cpu_1x");
  216. if (WARN_ON(IS_ERR(ttcce->clk)))
  217. return;
  218. err = clk_prepare_enable(ttcce->clk);
  219. if (WARN_ON(err))
  220. return;
  221. irq = irq_of_parse_and_map(np, 0);
  222. if (WARN_ON(!irq))
  223. return;
  224. ttcce->ce.name = np->name;
  225. ttcce->ce.features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT;
  226. ttcce->ce.set_next_event = xttcps_set_next_event;
  227. ttcce->ce.set_mode = xttcps_set_mode;
  228. ttcce->ce.rating = 200;
  229. ttcce->ce.irq = irq;
  230. ttcce->ce.cpumask = cpu_possible_mask;
  231. __raw_writel(0x23, ttcce->xttc.base_addr + XTTCPS_CNT_CNTRL_OFFSET);
  232. __raw_writel(CLK_CNTRL_PRESCALE | CLK_CNTRL_PRESCALE_EN,
  233. ttcce->xttc.base_addr + XTTCPS_CLK_CNTRL_OFFSET);
  234. __raw_writel(0x1, ttcce->xttc.base_addr + XTTCPS_IER_OFFSET);
  235. err = request_irq(irq, xttcps_clock_event_interrupt, IRQF_TIMER,
  236. np->name, ttcce);
  237. if (WARN_ON(err))
  238. return;
  239. clockevents_config_and_register(&ttcce->ce,
  240. clk_get_rate(ttcce->clk) / PRESCALE,
  241. 1, 0xfffe);
  242. }
  243. static const __initconst struct of_device_id zynq_ttc_match[] = {
  244. { .compatible = "xlnx,ttc-counter-clocksource",
  245. .data = zynq_ttc_setup_clocksource, },
  246. { .compatible = "xlnx,ttc-counter-clockevent",
  247. .data = zynq_ttc_setup_clockevent, },
  248. {}
  249. };
  250. /**
  251. * xttcps_timer_init - Initialize the timer
  252. *
  253. * Initializes the timer hardware and register the clock source and clock event
  254. * timers with Linux kernal timer framework
  255. **/
  256. void __init xttcps_timer_init(void)
  257. {
  258. struct device_node *np;
  259. for_each_compatible_node(np, NULL, "xlnx,ttc") {
  260. struct device_node *np_chld;
  261. void __iomem *base;
  262. base = of_iomap(np, 0);
  263. if (WARN_ON(!base))
  264. return;
  265. for_each_available_child_of_node(np, np_chld) {
  266. int (*cb)(struct device_node *np, void __iomem *base);
  267. const struct of_device_id *match;
  268. match = of_match_node(zynq_ttc_match, np_chld);
  269. if (match) {
  270. cb = match->data;
  271. cb(np_chld, base);
  272. }
  273. }
  274. }
  275. }