timer.c 9.1 KB

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