timer.c 12 KB

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