timer.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  1. /*
  2. * This file contains driver for the Xilinx PS Timer Counter IP.
  3. *
  4. * Copyright (C) 2011-2013 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/clk.h>
  18. #include <linux/interrupt.h>
  19. #include <linux/clockchips.h>
  20. #include <linux/of_address.h>
  21. #include <linux/of_irq.h>
  22. #include <linux/slab.h>
  23. #include <linux/clk-provider.h>
  24. #include "common.h"
  25. /*
  26. * This driver configures the 2 16-bit count-up timers as follows:
  27. *
  28. * T1: Timer 1, clocksource for generic timekeeping
  29. * T2: Timer 2, clockevent source for hrtimers
  30. * T3: Timer 3, <unused>
  31. *
  32. * The input frequency to the timer module for emulation is 2.5MHz which is
  33. * common to all the timer channels (T1, T2, and T3). With a pre-scaler of 32,
  34. * the timers are clocked at 78.125KHz (12.8 us resolution).
  35. * The input frequency to the timer module in silicon is configurable and
  36. * obtained from device tree. The pre-scaler of 32 is used.
  37. */
  38. /*
  39. * Timer Register Offset Definitions of Timer 1, Increment base address by 4
  40. * and use same offsets for Timer 2
  41. */
  42. #define XTTCPS_CLK_CNTRL_OFFSET 0x00 /* Clock Control Reg, RW */
  43. #define XTTCPS_CNT_CNTRL_OFFSET 0x0C /* Counter Control Reg, RW */
  44. #define XTTCPS_COUNT_VAL_OFFSET 0x18 /* Counter Value Reg, RO */
  45. #define XTTCPS_INTR_VAL_OFFSET 0x24 /* Interval Count Reg, RW */
  46. #define XTTCPS_ISR_OFFSET 0x54 /* Interrupt Status Reg, RO */
  47. #define XTTCPS_IER_OFFSET 0x60 /* Interrupt Enable Reg, RW */
  48. #define XTTCPS_CNT_CNTRL_DISABLE_MASK 0x1
  49. /*
  50. * Setup the timers to use pre-scaling, using a fixed value for now that will
  51. * work across most input frequency, but it may need to be more dynamic
  52. */
  53. #define PRESCALE_EXPONENT 11 /* 2 ^ PRESCALE_EXPONENT = PRESCALE */
  54. #define PRESCALE 2048 /* The exponent must match this */
  55. #define CLK_CNTRL_PRESCALE ((PRESCALE_EXPONENT - 1) << 1)
  56. #define CLK_CNTRL_PRESCALE_EN 1
  57. #define CNT_CNTRL_RESET (1 << 4)
  58. /**
  59. * struct xttcps_timer - This definition defines local timer structure
  60. *
  61. * @base_addr: Base address of timer
  62. * @clk: Associated clock source
  63. * @clk_rate_change_nb Notifier block for clock rate changes
  64. */
  65. struct xttcps_timer {
  66. void __iomem *base_addr;
  67. struct clk *clk;
  68. struct notifier_block clk_rate_change_nb;
  69. };
  70. #define to_xttcps_timer(x) \
  71. container_of(x, struct xttcps_timer, clk_rate_change_nb)
  72. struct xttcps_timer_clocksource {
  73. struct xttcps_timer xttc;
  74. struct clocksource cs;
  75. };
  76. #define to_xttcps_timer_clksrc(x) \
  77. container_of(x, struct xttcps_timer_clocksource, cs)
  78. struct xttcps_timer_clockevent {
  79. struct xttcps_timer xttc;
  80. struct clock_event_device ce;
  81. };
  82. #define to_xttcps_timer_clkevent(x) \
  83. container_of(x, struct xttcps_timer_clockevent, ce)
  84. /**
  85. * xttcps_set_interval - Set the timer interval value
  86. *
  87. * @timer: Pointer to the timer instance
  88. * @cycles: Timer interval ticks
  89. **/
  90. static void xttcps_set_interval(struct xttcps_timer *timer,
  91. unsigned long cycles)
  92. {
  93. u32 ctrl_reg;
  94. /* Disable the counter, set the counter value and re-enable counter */
  95. ctrl_reg = __raw_readl(timer->base_addr + XTTCPS_CNT_CNTRL_OFFSET);
  96. ctrl_reg |= XTTCPS_CNT_CNTRL_DISABLE_MASK;
  97. __raw_writel(ctrl_reg, timer->base_addr + XTTCPS_CNT_CNTRL_OFFSET);
  98. __raw_writel(cycles, timer->base_addr + XTTCPS_INTR_VAL_OFFSET);
  99. /*
  100. * Reset the counter (0x10) so that it starts from 0, one-shot
  101. * mode makes this needed for timing to be right.
  102. */
  103. ctrl_reg |= CNT_CNTRL_RESET;
  104. ctrl_reg &= ~XTTCPS_CNT_CNTRL_DISABLE_MASK;
  105. __raw_writel(ctrl_reg, timer->base_addr + XTTCPS_CNT_CNTRL_OFFSET);
  106. }
  107. /**
  108. * xttcps_clock_event_interrupt - Clock event timer interrupt handler
  109. *
  110. * @irq: IRQ number of the Timer
  111. * @dev_id: void pointer to the xttcps_timer instance
  112. *
  113. * returns: Always IRQ_HANDLED - success
  114. **/
  115. static irqreturn_t xttcps_clock_event_interrupt(int irq, void *dev_id)
  116. {
  117. struct xttcps_timer_clockevent *xttce = dev_id;
  118. struct xttcps_timer *timer = &xttce->xttc;
  119. /* Acknowledge the interrupt and call event handler */
  120. __raw_readl(timer->base_addr + XTTCPS_ISR_OFFSET);
  121. xttce->ce.event_handler(&xttce->ce);
  122. return IRQ_HANDLED;
  123. }
  124. /**
  125. * __xttc_clocksource_read - Reads the timer counter register
  126. *
  127. * returns: Current timer counter register value
  128. **/
  129. static cycle_t __xttc_clocksource_read(struct clocksource *cs)
  130. {
  131. struct xttcps_timer *timer = &to_xttcps_timer_clksrc(cs)->xttc;
  132. return (cycle_t)__raw_readl(timer->base_addr +
  133. XTTCPS_COUNT_VAL_OFFSET);
  134. }
  135. /**
  136. * xttcps_set_next_event - Sets the time interval for next event
  137. *
  138. * @cycles: Timer interval ticks
  139. * @evt: Address of clock event instance
  140. *
  141. * returns: Always 0 - success
  142. **/
  143. static int xttcps_set_next_event(unsigned long cycles,
  144. struct clock_event_device *evt)
  145. {
  146. struct xttcps_timer_clockevent *xttce = to_xttcps_timer_clkevent(evt);
  147. struct xttcps_timer *timer = &xttce->xttc;
  148. xttcps_set_interval(timer, cycles);
  149. return 0;
  150. }
  151. /**
  152. * xttcps_set_mode - Sets the mode of timer
  153. *
  154. * @mode: Mode to be set
  155. * @evt: Address of clock event instance
  156. **/
  157. static void xttcps_set_mode(enum clock_event_mode mode,
  158. struct clock_event_device *evt)
  159. {
  160. struct xttcps_timer_clockevent *xttce = to_xttcps_timer_clkevent(evt);
  161. struct xttcps_timer *timer = &xttce->xttc;
  162. u32 ctrl_reg;
  163. switch (mode) {
  164. case CLOCK_EVT_MODE_PERIODIC:
  165. xttcps_set_interval(timer,
  166. DIV_ROUND_CLOSEST(clk_get_rate(xttce->xttc.clk),
  167. PRESCALE * HZ));
  168. break;
  169. case CLOCK_EVT_MODE_ONESHOT:
  170. case CLOCK_EVT_MODE_UNUSED:
  171. case CLOCK_EVT_MODE_SHUTDOWN:
  172. ctrl_reg = __raw_readl(timer->base_addr +
  173. XTTCPS_CNT_CNTRL_OFFSET);
  174. ctrl_reg |= XTTCPS_CNT_CNTRL_DISABLE_MASK;
  175. __raw_writel(ctrl_reg,
  176. timer->base_addr + XTTCPS_CNT_CNTRL_OFFSET);
  177. break;
  178. case CLOCK_EVT_MODE_RESUME:
  179. ctrl_reg = __raw_readl(timer->base_addr +
  180. XTTCPS_CNT_CNTRL_OFFSET);
  181. ctrl_reg &= ~XTTCPS_CNT_CNTRL_DISABLE_MASK;
  182. __raw_writel(ctrl_reg,
  183. timer->base_addr + XTTCPS_CNT_CNTRL_OFFSET);
  184. break;
  185. }
  186. }
  187. static int xttcps_rate_change_clocksource_cb(struct notifier_block *nb,
  188. unsigned long event, void *data)
  189. {
  190. struct clk_notifier_data *ndata = data;
  191. struct xttcps_timer *xttcps = to_xttcps_timer(nb);
  192. struct xttcps_timer_clocksource *xttccs = container_of(xttcps,
  193. struct xttcps_timer_clocksource, xttc);
  194. switch (event) {
  195. case POST_RATE_CHANGE:
  196. /*
  197. * Do whatever is necessary to maintain a proper time base
  198. *
  199. * I cannot find a way to adjust the currently used clocksource
  200. * to the new frequency. __clocksource_updatefreq_hz() sounds
  201. * good, but does not work. Not sure what's that missing.
  202. *
  203. * This approach works, but triggers two clocksource switches.
  204. * The first after unregister to clocksource jiffies. And
  205. * another one after the register to the newly registered timer.
  206. *
  207. * Alternatively we could 'waste' another HW timer to ping pong
  208. * between clock sources. That would also use one register and
  209. * one unregister call, but only trigger one clocksource switch
  210. * for the cost of another HW timer used by the OS.
  211. */
  212. clocksource_unregister(&xttccs->cs);
  213. clocksource_register_hz(&xttccs->cs,
  214. ndata->new_rate / PRESCALE);
  215. /* fall through */
  216. case PRE_RATE_CHANGE:
  217. case ABORT_RATE_CHANGE:
  218. default:
  219. return NOTIFY_DONE;
  220. }
  221. }
  222. static void __init xttc_setup_clocksource(struct clk *clk, void __iomem *base)
  223. {
  224. struct xttcps_timer_clocksource *ttccs;
  225. int err;
  226. ttccs = kzalloc(sizeof(*ttccs), GFP_KERNEL);
  227. if (WARN_ON(!ttccs))
  228. return;
  229. ttccs->xttc.clk = clk;
  230. err = clk_prepare_enable(ttccs->xttc.clk);
  231. if (WARN_ON(err))
  232. return;
  233. ttccs->xttc.clk_rate_change_nb.notifier_call =
  234. xttcps_rate_change_clocksource_cb;
  235. ttccs->xttc.clk_rate_change_nb.next = NULL;
  236. if (clk_notifier_register(ttccs->xttc.clk,
  237. &ttccs->xttc.clk_rate_change_nb))
  238. pr_warn("Unable to register clock notifier.\n");
  239. ttccs->xttc.base_addr = base;
  240. ttccs->cs.name = "xttcps_clocksource";
  241. ttccs->cs.rating = 200;
  242. ttccs->cs.read = __xttc_clocksource_read;
  243. ttccs->cs.mask = CLOCKSOURCE_MASK(16);
  244. ttccs->cs.flags = CLOCK_SOURCE_IS_CONTINUOUS;
  245. /*
  246. * Setup the clock source counter to be an incrementing counter
  247. * with no interrupt and it rolls over at 0xFFFF. Pre-scale
  248. * it by 32 also. Let it start running now.
  249. */
  250. __raw_writel(0x0, ttccs->xttc.base_addr + XTTCPS_IER_OFFSET);
  251. __raw_writel(CLK_CNTRL_PRESCALE | CLK_CNTRL_PRESCALE_EN,
  252. ttccs->xttc.base_addr + XTTCPS_CLK_CNTRL_OFFSET);
  253. __raw_writel(CNT_CNTRL_RESET,
  254. ttccs->xttc.base_addr + XTTCPS_CNT_CNTRL_OFFSET);
  255. err = clocksource_register_hz(&ttccs->cs,
  256. clk_get_rate(ttccs->xttc.clk) / PRESCALE);
  257. if (WARN_ON(err))
  258. return;
  259. }
  260. static int xttcps_rate_change_clockevent_cb(struct notifier_block *nb,
  261. unsigned long event, void *data)
  262. {
  263. struct clk_notifier_data *ndata = data;
  264. struct xttcps_timer *xttcps = to_xttcps_timer(nb);
  265. struct xttcps_timer_clockevent *xttcce = container_of(xttcps,
  266. struct xttcps_timer_clockevent, xttc);
  267. switch (event) {
  268. case POST_RATE_CHANGE:
  269. {
  270. unsigned long flags;
  271. /*
  272. * clockevents_update_freq should be called with IRQ disabled on
  273. * the CPU the timer provides events for. The timer we use is
  274. * common to both CPUs, not sure if we need to run on both
  275. * cores.
  276. */
  277. local_irq_save(flags);
  278. clockevents_update_freq(&xttcce->ce,
  279. ndata->new_rate / PRESCALE);
  280. local_irq_restore(flags);
  281. /* fall through */
  282. }
  283. case PRE_RATE_CHANGE:
  284. case ABORT_RATE_CHANGE:
  285. default:
  286. return NOTIFY_DONE;
  287. }
  288. }
  289. static void __init xttc_setup_clockevent(struct clk *clk,
  290. void __iomem *base, u32 irq)
  291. {
  292. struct xttcps_timer_clockevent *ttcce;
  293. int err;
  294. ttcce = kzalloc(sizeof(*ttcce), GFP_KERNEL);
  295. if (WARN_ON(!ttcce))
  296. return;
  297. ttcce->xttc.clk = clk;
  298. err = clk_prepare_enable(ttcce->xttc.clk);
  299. if (WARN_ON(err))
  300. return;
  301. ttcce->xttc.clk_rate_change_nb.notifier_call =
  302. xttcps_rate_change_clockevent_cb;
  303. ttcce->xttc.clk_rate_change_nb.next = NULL;
  304. if (clk_notifier_register(ttcce->xttc.clk,
  305. &ttcce->xttc.clk_rate_change_nb))
  306. pr_warn("Unable to register clock notifier.\n");
  307. ttcce->xttc.base_addr = base;
  308. ttcce->ce.name = "xttcps_clockevent";
  309. ttcce->ce.features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT;
  310. ttcce->ce.set_next_event = xttcps_set_next_event;
  311. ttcce->ce.set_mode = xttcps_set_mode;
  312. ttcce->ce.rating = 200;
  313. ttcce->ce.irq = irq;
  314. ttcce->ce.cpumask = cpu_possible_mask;
  315. /*
  316. * Setup the clock event timer to be an interval timer which
  317. * is prescaled by 32 using the interval interrupt. Leave it
  318. * disabled for now.
  319. */
  320. __raw_writel(0x23, ttcce->xttc.base_addr + XTTCPS_CNT_CNTRL_OFFSET);
  321. __raw_writel(CLK_CNTRL_PRESCALE | CLK_CNTRL_PRESCALE_EN,
  322. ttcce->xttc.base_addr + XTTCPS_CLK_CNTRL_OFFSET);
  323. __raw_writel(0x1, ttcce->xttc.base_addr + XTTCPS_IER_OFFSET);
  324. err = request_irq(irq, xttcps_clock_event_interrupt,
  325. IRQF_DISABLED | IRQF_TIMER,
  326. ttcce->ce.name, ttcce);
  327. if (WARN_ON(err))
  328. return;
  329. clockevents_config_and_register(&ttcce->ce,
  330. clk_get_rate(ttcce->xttc.clk) / PRESCALE, 1, 0xfffe);
  331. }
  332. /**
  333. * xttcps_timer_init - Initialize the timer
  334. *
  335. * Initializes the timer hardware and register the clock source and clock event
  336. * timers with Linux kernal timer framework
  337. */
  338. static void __init xttcps_timer_init_of(struct device_node *timer)
  339. {
  340. unsigned int irq;
  341. void __iomem *timer_baseaddr;
  342. struct clk *clk;
  343. /*
  344. * Get the 1st Triple Timer Counter (TTC) block from the device tree
  345. * and use it. Note that the event timer uses the interrupt and it's the
  346. * 2nd TTC hence the irq_of_parse_and_map(,1)
  347. */
  348. timer_baseaddr = of_iomap(timer, 0);
  349. if (!timer_baseaddr) {
  350. pr_err("ERROR: invalid timer base address\n");
  351. BUG();
  352. }
  353. irq = irq_of_parse_and_map(timer, 1);
  354. if (irq <= 0) {
  355. pr_err("ERROR: invalid interrupt number\n");
  356. BUG();
  357. }
  358. clk = of_clk_get_by_name(timer, "cpu_1x");
  359. if (IS_ERR(clk)) {
  360. pr_err("ERROR: timer input clock not found\n");
  361. BUG();
  362. }
  363. xttc_setup_clocksource(clk, timer_baseaddr);
  364. xttc_setup_clockevent(clk, timer_baseaddr + 4, irq);
  365. pr_info("%s #0 at %p, irq=%d\n", timer->name, timer_baseaddr, irq);
  366. }
  367. void __init xttcps_timer_init(void)
  368. {
  369. const char * const timer_list[] = {
  370. "cdns,ttc",
  371. NULL
  372. };
  373. struct device_node *timer;
  374. timer = of_find_compatible_node(NULL, NULL, timer_list[0]);
  375. if (!timer) {
  376. pr_err("ERROR: no compatible timer found\n");
  377. BUG();
  378. }
  379. xttcps_timer_init_of(timer);
  380. }