cadence_ttc_timer.c 13 KB

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