cadence_ttc_timer.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  1. /*
  2. * This file contains driver for the Cadence Triple Timer Counter Rev 06
  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/sched_clock.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 TTC_CLK_CNTRL_OFFSET 0x00 /* Clock Control Reg, RW */
  42. #define TTC_CNT_CNTRL_OFFSET 0x0C /* Counter Control Reg, RW */
  43. #define TTC_COUNT_VAL_OFFSET 0x18 /* Counter Value Reg, RO */
  44. #define TTC_INTR_VAL_OFFSET 0x24 /* Interval Count Reg, RW */
  45. #define TTC_ISR_OFFSET 0x54 /* Interrupt Status Reg, RO */
  46. #define TTC_IER_OFFSET 0x60 /* Interrupt Enable Reg, RW */
  47. #define TTC_CNT_CNTRL_DISABLE_MASK 0x1
  48. #define TTC_CLK_CNTRL_CSRC_MASK (1 << 5) /* clock source */
  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 ttc_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 ttc_timer {
  66. void __iomem *base_addr;
  67. struct clk *clk;
  68. struct notifier_block clk_rate_change_nb;
  69. };
  70. #define to_ttc_timer(x) \
  71. container_of(x, struct ttc_timer, clk_rate_change_nb)
  72. struct ttc_timer_clocksource {
  73. struct ttc_timer ttc;
  74. struct clocksource cs;
  75. };
  76. #define to_ttc_timer_clksrc(x) \
  77. container_of(x, struct ttc_timer_clocksource, cs)
  78. struct ttc_timer_clockevent {
  79. struct ttc_timer ttc;
  80. struct clock_event_device ce;
  81. };
  82. #define to_ttc_timer_clkevent(x) \
  83. container_of(x, struct ttc_timer_clockevent, ce)
  84. static void __iomem *ttc_sched_clock_val_reg;
  85. /**
  86. * ttc_set_interval - Set the timer interval value
  87. *
  88. * @timer: Pointer to the timer instance
  89. * @cycles: Timer interval ticks
  90. **/
  91. static void ttc_set_interval(struct ttc_timer *timer,
  92. unsigned long cycles)
  93. {
  94. u32 ctrl_reg;
  95. /* Disable the counter, set the counter value and re-enable counter */
  96. ctrl_reg = __raw_readl(timer->base_addr + TTC_CNT_CNTRL_OFFSET);
  97. ctrl_reg |= TTC_CNT_CNTRL_DISABLE_MASK;
  98. __raw_writel(ctrl_reg, timer->base_addr + TTC_CNT_CNTRL_OFFSET);
  99. __raw_writel(cycles, timer->base_addr + TTC_INTR_VAL_OFFSET);
  100. /*
  101. * Reset the counter (0x10) so that it starts from 0, one-shot
  102. * mode makes this needed for timing to be right.
  103. */
  104. ctrl_reg |= CNT_CNTRL_RESET;
  105. ctrl_reg &= ~TTC_CNT_CNTRL_DISABLE_MASK;
  106. __raw_writel(ctrl_reg, timer->base_addr + TTC_CNT_CNTRL_OFFSET);
  107. }
  108. /**
  109. * ttc_clock_event_interrupt - Clock event timer interrupt handler
  110. *
  111. * @irq: IRQ number of the Timer
  112. * @dev_id: void pointer to the ttc_timer instance
  113. *
  114. * returns: Always IRQ_HANDLED - success
  115. **/
  116. static irqreturn_t ttc_clock_event_interrupt(int irq, void *dev_id)
  117. {
  118. struct ttc_timer_clockevent *ttce = dev_id;
  119. struct ttc_timer *timer = &ttce->ttc;
  120. /* Acknowledge the interrupt and call event handler */
  121. __raw_readl(timer->base_addr + TTC_ISR_OFFSET);
  122. ttce->ce.event_handler(&ttce->ce);
  123. return IRQ_HANDLED;
  124. }
  125. /**
  126. * __ttc_clocksource_read - Reads the timer counter register
  127. *
  128. * returns: Current timer counter register value
  129. **/
  130. static cycle_t __ttc_clocksource_read(struct clocksource *cs)
  131. {
  132. struct ttc_timer *timer = &to_ttc_timer_clksrc(cs)->ttc;
  133. return (cycle_t)__raw_readl(timer->base_addr +
  134. TTC_COUNT_VAL_OFFSET);
  135. }
  136. static u32 notrace ttc_sched_clock_read(void)
  137. {
  138. return __raw_readl(ttc_sched_clock_val_reg);
  139. }
  140. /**
  141. * ttc_set_next_event - Sets the time interval for next event
  142. *
  143. * @cycles: Timer interval ticks
  144. * @evt: Address of clock event instance
  145. *
  146. * returns: Always 0 - success
  147. **/
  148. static int ttc_set_next_event(unsigned long cycles,
  149. struct clock_event_device *evt)
  150. {
  151. struct ttc_timer_clockevent *ttce = to_ttc_timer_clkevent(evt);
  152. struct ttc_timer *timer = &ttce->ttc;
  153. ttc_set_interval(timer, cycles);
  154. return 0;
  155. }
  156. /**
  157. * ttc_set_mode - Sets the mode of timer
  158. *
  159. * @mode: Mode to be set
  160. * @evt: Address of clock event instance
  161. **/
  162. static void ttc_set_mode(enum clock_event_mode mode,
  163. struct clock_event_device *evt)
  164. {
  165. struct ttc_timer_clockevent *ttce = to_ttc_timer_clkevent(evt);
  166. struct ttc_timer *timer = &ttce->ttc;
  167. u32 ctrl_reg;
  168. switch (mode) {
  169. case CLOCK_EVT_MODE_PERIODIC:
  170. ttc_set_interval(timer,
  171. DIV_ROUND_CLOSEST(clk_get_rate(ttce->ttc.clk),
  172. PRESCALE * HZ));
  173. break;
  174. case CLOCK_EVT_MODE_ONESHOT:
  175. case CLOCK_EVT_MODE_UNUSED:
  176. case CLOCK_EVT_MODE_SHUTDOWN:
  177. ctrl_reg = __raw_readl(timer->base_addr +
  178. TTC_CNT_CNTRL_OFFSET);
  179. ctrl_reg |= TTC_CNT_CNTRL_DISABLE_MASK;
  180. __raw_writel(ctrl_reg,
  181. timer->base_addr + TTC_CNT_CNTRL_OFFSET);
  182. break;
  183. case CLOCK_EVT_MODE_RESUME:
  184. ctrl_reg = __raw_readl(timer->base_addr +
  185. TTC_CNT_CNTRL_OFFSET);
  186. ctrl_reg &= ~TTC_CNT_CNTRL_DISABLE_MASK;
  187. __raw_writel(ctrl_reg,
  188. timer->base_addr + TTC_CNT_CNTRL_OFFSET);
  189. break;
  190. }
  191. }
  192. static int ttc_rate_change_clocksource_cb(struct notifier_block *nb,
  193. unsigned long event, void *data)
  194. {
  195. struct clk_notifier_data *ndata = data;
  196. struct ttc_timer *ttc = to_ttc_timer(nb);
  197. struct ttc_timer_clocksource *ttccs = container_of(ttc,
  198. struct ttc_timer_clocksource, ttc);
  199. switch (event) {
  200. case POST_RATE_CHANGE:
  201. /*
  202. * Do whatever is necessary to maintain a proper time base
  203. *
  204. * I cannot find a way to adjust the currently used clocksource
  205. * to the new frequency. __clocksource_updatefreq_hz() sounds
  206. * good, but does not work. Not sure what's that missing.
  207. *
  208. * This approach works, but triggers two clocksource switches.
  209. * The first after unregister to clocksource jiffies. And
  210. * another one after the register to the newly registered timer.
  211. *
  212. * Alternatively we could 'waste' another HW timer to ping pong
  213. * between clock sources. That would also use one register and
  214. * one unregister call, but only trigger one clocksource switch
  215. * for the cost of another HW timer used by the OS.
  216. */
  217. clocksource_unregister(&ttccs->cs);
  218. clocksource_register_hz(&ttccs->cs,
  219. ndata->new_rate / PRESCALE);
  220. /* fall through */
  221. case PRE_RATE_CHANGE:
  222. case ABORT_RATE_CHANGE:
  223. default:
  224. return NOTIFY_DONE;
  225. }
  226. }
  227. static void __init ttc_setup_clocksource(struct clk *clk, void __iomem *base)
  228. {
  229. struct ttc_timer_clocksource *ttccs;
  230. int err;
  231. ttccs = kzalloc(sizeof(*ttccs), GFP_KERNEL);
  232. if (WARN_ON(!ttccs))
  233. return;
  234. ttccs->ttc.clk = clk;
  235. err = clk_prepare_enable(ttccs->ttc.clk);
  236. if (WARN_ON(err)) {
  237. kfree(ttccs);
  238. return;
  239. }
  240. ttccs->ttc.clk_rate_change_nb.notifier_call =
  241. ttc_rate_change_clocksource_cb;
  242. ttccs->ttc.clk_rate_change_nb.next = NULL;
  243. if (clk_notifier_register(ttccs->ttc.clk,
  244. &ttccs->ttc.clk_rate_change_nb))
  245. pr_warn("Unable to register clock notifier.\n");
  246. ttccs->ttc.base_addr = base;
  247. ttccs->cs.name = "ttc_clocksource";
  248. ttccs->cs.rating = 200;
  249. ttccs->cs.read = __ttc_clocksource_read;
  250. ttccs->cs.mask = CLOCKSOURCE_MASK(16);
  251. ttccs->cs.flags = CLOCK_SOURCE_IS_CONTINUOUS;
  252. /*
  253. * Setup the clock source counter to be an incrementing counter
  254. * with no interrupt and it rolls over at 0xFFFF. Pre-scale
  255. * it by 32 also. Let it start running now.
  256. */
  257. __raw_writel(0x0, ttccs->ttc.base_addr + TTC_IER_OFFSET);
  258. __raw_writel(CLK_CNTRL_PRESCALE | CLK_CNTRL_PRESCALE_EN,
  259. ttccs->ttc.base_addr + TTC_CLK_CNTRL_OFFSET);
  260. __raw_writel(CNT_CNTRL_RESET,
  261. ttccs->ttc.base_addr + TTC_CNT_CNTRL_OFFSET);
  262. err = clocksource_register_hz(&ttccs->cs,
  263. clk_get_rate(ttccs->ttc.clk) / PRESCALE);
  264. if (WARN_ON(err)) {
  265. kfree(ttccs);
  266. return;
  267. }
  268. ttc_sched_clock_val_reg = base + TTC_COUNT_VAL_OFFSET;
  269. setup_sched_clock(ttc_sched_clock_read, 16,
  270. clk_get_rate(ttccs->ttc.clk) / PRESCALE);
  271. }
  272. static int ttc_rate_change_clockevent_cb(struct notifier_block *nb,
  273. unsigned long event, void *data)
  274. {
  275. struct clk_notifier_data *ndata = data;
  276. struct ttc_timer *ttc = to_ttc_timer(nb);
  277. struct ttc_timer_clockevent *ttcce = container_of(ttc,
  278. struct ttc_timer_clockevent, ttc);
  279. switch (event) {
  280. case POST_RATE_CHANGE:
  281. {
  282. unsigned long flags;
  283. /*
  284. * clockevents_update_freq should be called with IRQ disabled on
  285. * the CPU the timer provides events for. The timer we use is
  286. * common to both CPUs, not sure if we need to run on both
  287. * cores.
  288. */
  289. local_irq_save(flags);
  290. clockevents_update_freq(&ttcce->ce,
  291. ndata->new_rate / PRESCALE);
  292. local_irq_restore(flags);
  293. /* fall through */
  294. }
  295. case PRE_RATE_CHANGE:
  296. case ABORT_RATE_CHANGE:
  297. default:
  298. return NOTIFY_DONE;
  299. }
  300. }
  301. static void __init ttc_setup_clockevent(struct clk *clk,
  302. void __iomem *base, u32 irq)
  303. {
  304. struct ttc_timer_clockevent *ttcce;
  305. int err;
  306. ttcce = kzalloc(sizeof(*ttcce), GFP_KERNEL);
  307. if (WARN_ON(!ttcce))
  308. return;
  309. ttcce->ttc.clk = clk;
  310. err = clk_prepare_enable(ttcce->ttc.clk);
  311. if (WARN_ON(err)) {
  312. kfree(ttcce);
  313. return;
  314. }
  315. ttcce->ttc.clk_rate_change_nb.notifier_call =
  316. ttc_rate_change_clockevent_cb;
  317. ttcce->ttc.clk_rate_change_nb.next = NULL;
  318. if (clk_notifier_register(ttcce->ttc.clk,
  319. &ttcce->ttc.clk_rate_change_nb))
  320. pr_warn("Unable to register clock notifier.\n");
  321. ttcce->ttc.base_addr = base;
  322. ttcce->ce.name = "ttc_clockevent";
  323. ttcce->ce.features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT;
  324. ttcce->ce.set_next_event = ttc_set_next_event;
  325. ttcce->ce.set_mode = ttc_set_mode;
  326. ttcce->ce.rating = 200;
  327. ttcce->ce.irq = irq;
  328. ttcce->ce.cpumask = cpu_possible_mask;
  329. /*
  330. * Setup the clock event timer to be an interval timer which
  331. * is prescaled by 32 using the interval interrupt. Leave it
  332. * disabled for now.
  333. */
  334. __raw_writel(0x23, ttcce->ttc.base_addr + TTC_CNT_CNTRL_OFFSET);
  335. __raw_writel(CLK_CNTRL_PRESCALE | CLK_CNTRL_PRESCALE_EN,
  336. ttcce->ttc.base_addr + TTC_CLK_CNTRL_OFFSET);
  337. __raw_writel(0x1, ttcce->ttc.base_addr + TTC_IER_OFFSET);
  338. err = request_irq(irq, ttc_clock_event_interrupt,
  339. IRQF_DISABLED | IRQF_TIMER,
  340. ttcce->ce.name, ttcce);
  341. if (WARN_ON(err)) {
  342. kfree(ttcce);
  343. return;
  344. }
  345. clockevents_config_and_register(&ttcce->ce,
  346. clk_get_rate(ttcce->ttc.clk) / PRESCALE, 1, 0xfffe);
  347. }
  348. /**
  349. * ttc_timer_init - Initialize the timer
  350. *
  351. * Initializes the timer hardware and register the clock source and clock event
  352. * timers with Linux kernal timer framework
  353. */
  354. static void __init ttc_timer_init(struct device_node *timer)
  355. {
  356. unsigned int irq;
  357. void __iomem *timer_baseaddr;
  358. struct clk *clk_cs, *clk_ce;
  359. static int initialized;
  360. int clksel;
  361. if (initialized)
  362. return;
  363. initialized = 1;
  364. /*
  365. * Get the 1st Triple Timer Counter (TTC) block from the device tree
  366. * and use it. Note that the event timer uses the interrupt and it's the
  367. * 2nd TTC hence the irq_of_parse_and_map(,1)
  368. */
  369. timer_baseaddr = of_iomap(timer, 0);
  370. if (!timer_baseaddr) {
  371. pr_err("ERROR: invalid timer base address\n");
  372. BUG();
  373. }
  374. irq = irq_of_parse_and_map(timer, 1);
  375. if (irq <= 0) {
  376. pr_err("ERROR: invalid interrupt number\n");
  377. BUG();
  378. }
  379. clksel = __raw_readl(timer_baseaddr + TTC_CLK_CNTRL_OFFSET);
  380. clksel = !!(clksel & TTC_CLK_CNTRL_CSRC_MASK);
  381. clk_cs = of_clk_get(timer, clksel);
  382. if (IS_ERR(clk_cs)) {
  383. pr_err("ERROR: timer input clock not found\n");
  384. BUG();
  385. }
  386. clksel = __raw_readl(timer_baseaddr + 4 + TTC_CLK_CNTRL_OFFSET);
  387. clksel = !!(clksel & TTC_CLK_CNTRL_CSRC_MASK);
  388. clk_ce = of_clk_get(timer, clksel);
  389. if (IS_ERR(clk_ce)) {
  390. pr_err("ERROR: timer input clock not found\n");
  391. BUG();
  392. }
  393. ttc_setup_clocksource(clk_cs, timer_baseaddr);
  394. ttc_setup_clockevent(clk_ce, timer_baseaddr + 4, irq);
  395. pr_info("%s #0 at %p, irq=%d\n", timer->name, timer_baseaddr, irq);
  396. }
  397. CLOCKSOURCE_OF_DECLARE(ttc, "cdns,ttc", ttc_timer_init);