timer.c 8.8 KB

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