cadence_ttc_timer.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544
  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/clocksource.h>
  21. #include <linux/of_address.h>
  22. #include <linux/of_irq.h>
  23. #include <linux/slab.h>
  24. #include <linux/sched_clock.h>
  25. /*
  26. * This driver configures the 2 16/32-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 TTC_CLK_CNTRL_OFFSET 0x00 /* Clock Control Reg, RW */
  43. #define TTC_CNT_CNTRL_OFFSET 0x0C /* Counter Control Reg, RW */
  44. #define TTC_COUNT_VAL_OFFSET 0x18 /* Counter Value Reg, RO */
  45. #define TTC_INTR_VAL_OFFSET 0x24 /* Interval Count Reg, RW */
  46. #define TTC_ISR_OFFSET 0x54 /* Interrupt Status Reg, RO */
  47. #define TTC_IER_OFFSET 0x60 /* Interrupt Enable Reg, RW */
  48. #define TTC_CNT_CNTRL_DISABLE_MASK 0x1
  49. #define TTC_CLK_CNTRL_CSRC_MASK (1 << 5) /* clock source */
  50. #define TTC_CLK_CNTRL_PSV_MASK 0x1e
  51. #define TTC_CLK_CNTRL_PSV_SHIFT 1
  52. /*
  53. * Setup the timers to use pre-scaling, using a fixed value for now that will
  54. * work across most input frequency, but it may need to be more dynamic
  55. */
  56. #define PRESCALE_EXPONENT 11 /* 2 ^ PRESCALE_EXPONENT = PRESCALE */
  57. #define PRESCALE 2048 /* The exponent must match this */
  58. #define CLK_CNTRL_PRESCALE ((PRESCALE_EXPONENT - 1) << 1)
  59. #define CLK_CNTRL_PRESCALE_EN 1
  60. #define CNT_CNTRL_RESET (1 << 4)
  61. #define MAX_F_ERR 50
  62. /**
  63. * struct ttc_timer - This definition defines local timer structure
  64. *
  65. * @base_addr: Base address of timer
  66. * @freq: Timer input clock frequency
  67. * @clk: Associated clock source
  68. * @clk_rate_change_nb Notifier block for clock rate changes
  69. */
  70. struct ttc_timer {
  71. void __iomem *base_addr;
  72. unsigned long freq;
  73. struct clk *clk;
  74. struct notifier_block clk_rate_change_nb;
  75. };
  76. #define to_ttc_timer(x) \
  77. container_of(x, struct ttc_timer, clk_rate_change_nb)
  78. struct ttc_timer_clocksource {
  79. u32 scale_clk_ctrl_reg_old;
  80. u32 scale_clk_ctrl_reg_new;
  81. struct ttc_timer ttc;
  82. struct clocksource cs;
  83. };
  84. #define to_ttc_timer_clksrc(x) \
  85. container_of(x, struct ttc_timer_clocksource, cs)
  86. struct ttc_timer_clockevent {
  87. struct ttc_timer ttc;
  88. struct clock_event_device ce;
  89. };
  90. #define to_ttc_timer_clkevent(x) \
  91. container_of(x, struct ttc_timer_clockevent, ce)
  92. static void __iomem *ttc_sched_clock_val_reg;
  93. /**
  94. * ttc_set_interval - Set the timer interval value
  95. *
  96. * @timer: Pointer to the timer instance
  97. * @cycles: Timer interval ticks
  98. **/
  99. static void ttc_set_interval(struct ttc_timer *timer,
  100. unsigned long cycles)
  101. {
  102. u32 ctrl_reg;
  103. /* Disable the counter, set the counter value and re-enable counter */
  104. ctrl_reg = readl_relaxed(timer->base_addr + TTC_CNT_CNTRL_OFFSET);
  105. ctrl_reg |= TTC_CNT_CNTRL_DISABLE_MASK;
  106. writel_relaxed(ctrl_reg, timer->base_addr + TTC_CNT_CNTRL_OFFSET);
  107. writel_relaxed(cycles, timer->base_addr + TTC_INTR_VAL_OFFSET);
  108. /*
  109. * Reset the counter (0x10) so that it starts from 0, one-shot
  110. * mode makes this needed for timing to be right.
  111. */
  112. ctrl_reg |= CNT_CNTRL_RESET;
  113. ctrl_reg &= ~TTC_CNT_CNTRL_DISABLE_MASK;
  114. writel_relaxed(ctrl_reg, timer->base_addr + TTC_CNT_CNTRL_OFFSET);
  115. }
  116. /**
  117. * ttc_clock_event_interrupt - Clock event timer interrupt handler
  118. *
  119. * @irq: IRQ number of the Timer
  120. * @dev_id: void pointer to the ttc_timer instance
  121. *
  122. * returns: Always IRQ_HANDLED - success
  123. **/
  124. static irqreturn_t ttc_clock_event_interrupt(int irq, void *dev_id)
  125. {
  126. struct ttc_timer_clockevent *ttce = dev_id;
  127. struct ttc_timer *timer = &ttce->ttc;
  128. /* Acknowledge the interrupt and call event handler */
  129. readl_relaxed(timer->base_addr + TTC_ISR_OFFSET);
  130. ttce->ce.event_handler(&ttce->ce);
  131. return IRQ_HANDLED;
  132. }
  133. /**
  134. * __ttc_clocksource_read - Reads the timer counter register
  135. *
  136. * returns: Current timer counter register value
  137. **/
  138. static u64 __ttc_clocksource_read(struct clocksource *cs)
  139. {
  140. struct ttc_timer *timer = &to_ttc_timer_clksrc(cs)->ttc;
  141. return (u64)readl_relaxed(timer->base_addr +
  142. TTC_COUNT_VAL_OFFSET);
  143. }
  144. static u64 notrace ttc_sched_clock_read(void)
  145. {
  146. return readl_relaxed(ttc_sched_clock_val_reg);
  147. }
  148. /**
  149. * ttc_set_next_event - Sets the time interval for next event
  150. *
  151. * @cycles: Timer interval ticks
  152. * @evt: Address of clock event instance
  153. *
  154. * returns: Always 0 - success
  155. **/
  156. static int ttc_set_next_event(unsigned long cycles,
  157. struct clock_event_device *evt)
  158. {
  159. struct ttc_timer_clockevent *ttce = to_ttc_timer_clkevent(evt);
  160. struct ttc_timer *timer = &ttce->ttc;
  161. ttc_set_interval(timer, cycles);
  162. return 0;
  163. }
  164. /**
  165. * ttc_set_{shutdown|oneshot|periodic} - Sets the state of timer
  166. *
  167. * @evt: Address of clock event instance
  168. **/
  169. static int ttc_shutdown(struct clock_event_device *evt)
  170. {
  171. struct ttc_timer_clockevent *ttce = to_ttc_timer_clkevent(evt);
  172. struct ttc_timer *timer = &ttce->ttc;
  173. u32 ctrl_reg;
  174. ctrl_reg = readl_relaxed(timer->base_addr + TTC_CNT_CNTRL_OFFSET);
  175. ctrl_reg |= TTC_CNT_CNTRL_DISABLE_MASK;
  176. writel_relaxed(ctrl_reg, timer->base_addr + TTC_CNT_CNTRL_OFFSET);
  177. return 0;
  178. }
  179. static int ttc_set_periodic(struct clock_event_device *evt)
  180. {
  181. struct ttc_timer_clockevent *ttce = to_ttc_timer_clkevent(evt);
  182. struct ttc_timer *timer = &ttce->ttc;
  183. ttc_set_interval(timer,
  184. DIV_ROUND_CLOSEST(ttce->ttc.freq, PRESCALE * HZ));
  185. return 0;
  186. }
  187. static int ttc_resume(struct clock_event_device *evt)
  188. {
  189. struct ttc_timer_clockevent *ttce = to_ttc_timer_clkevent(evt);
  190. struct ttc_timer *timer = &ttce->ttc;
  191. u32 ctrl_reg;
  192. ctrl_reg = readl_relaxed(timer->base_addr + TTC_CNT_CNTRL_OFFSET);
  193. ctrl_reg &= ~TTC_CNT_CNTRL_DISABLE_MASK;
  194. writel_relaxed(ctrl_reg, timer->base_addr + TTC_CNT_CNTRL_OFFSET);
  195. return 0;
  196. }
  197. static int ttc_rate_change_clocksource_cb(struct notifier_block *nb,
  198. unsigned long event, void *data)
  199. {
  200. struct clk_notifier_data *ndata = data;
  201. struct ttc_timer *ttc = to_ttc_timer(nb);
  202. struct ttc_timer_clocksource *ttccs = container_of(ttc,
  203. struct ttc_timer_clocksource, ttc);
  204. switch (event) {
  205. case PRE_RATE_CHANGE:
  206. {
  207. u32 psv;
  208. unsigned long factor, rate_low, rate_high;
  209. if (ndata->new_rate > ndata->old_rate) {
  210. factor = DIV_ROUND_CLOSEST(ndata->new_rate,
  211. ndata->old_rate);
  212. rate_low = ndata->old_rate;
  213. rate_high = ndata->new_rate;
  214. } else {
  215. factor = DIV_ROUND_CLOSEST(ndata->old_rate,
  216. ndata->new_rate);
  217. rate_low = ndata->new_rate;
  218. rate_high = ndata->old_rate;
  219. }
  220. if (!is_power_of_2(factor))
  221. return NOTIFY_BAD;
  222. if (abs(rate_high - (factor * rate_low)) > MAX_F_ERR)
  223. return NOTIFY_BAD;
  224. factor = __ilog2_u32(factor);
  225. /*
  226. * store timer clock ctrl register so we can restore it in case
  227. * of an abort.
  228. */
  229. ttccs->scale_clk_ctrl_reg_old =
  230. readl_relaxed(ttccs->ttc.base_addr +
  231. TTC_CLK_CNTRL_OFFSET);
  232. psv = (ttccs->scale_clk_ctrl_reg_old &
  233. TTC_CLK_CNTRL_PSV_MASK) >>
  234. TTC_CLK_CNTRL_PSV_SHIFT;
  235. if (ndata->new_rate < ndata->old_rate)
  236. psv -= factor;
  237. else
  238. psv += factor;
  239. /* prescaler within legal range? */
  240. if (psv & ~(TTC_CLK_CNTRL_PSV_MASK >> TTC_CLK_CNTRL_PSV_SHIFT))
  241. return NOTIFY_BAD;
  242. ttccs->scale_clk_ctrl_reg_new = ttccs->scale_clk_ctrl_reg_old &
  243. ~TTC_CLK_CNTRL_PSV_MASK;
  244. ttccs->scale_clk_ctrl_reg_new |= psv << TTC_CLK_CNTRL_PSV_SHIFT;
  245. /* scale down: adjust divider in post-change notification */
  246. if (ndata->new_rate < ndata->old_rate)
  247. return NOTIFY_DONE;
  248. /* scale up: adjust divider now - before frequency change */
  249. writel_relaxed(ttccs->scale_clk_ctrl_reg_new,
  250. ttccs->ttc.base_addr + TTC_CLK_CNTRL_OFFSET);
  251. break;
  252. }
  253. case POST_RATE_CHANGE:
  254. /* scale up: pre-change notification did the adjustment */
  255. if (ndata->new_rate > ndata->old_rate)
  256. return NOTIFY_OK;
  257. /* scale down: adjust divider now - after frequency change */
  258. writel_relaxed(ttccs->scale_clk_ctrl_reg_new,
  259. ttccs->ttc.base_addr + TTC_CLK_CNTRL_OFFSET);
  260. break;
  261. case ABORT_RATE_CHANGE:
  262. /* we have to undo the adjustment in case we scale up */
  263. if (ndata->new_rate < ndata->old_rate)
  264. return NOTIFY_OK;
  265. /* restore original register value */
  266. writel_relaxed(ttccs->scale_clk_ctrl_reg_old,
  267. ttccs->ttc.base_addr + TTC_CLK_CNTRL_OFFSET);
  268. /* fall through */
  269. default:
  270. return NOTIFY_DONE;
  271. }
  272. return NOTIFY_DONE;
  273. }
  274. static int __init ttc_setup_clocksource(struct clk *clk, void __iomem *base,
  275. u32 timer_width)
  276. {
  277. struct ttc_timer_clocksource *ttccs;
  278. int err;
  279. ttccs = kzalloc(sizeof(*ttccs), GFP_KERNEL);
  280. if (!ttccs)
  281. return -ENOMEM;
  282. ttccs->ttc.clk = clk;
  283. err = clk_prepare_enable(ttccs->ttc.clk);
  284. if (err) {
  285. kfree(ttccs);
  286. return err;
  287. }
  288. ttccs->ttc.freq = clk_get_rate(ttccs->ttc.clk);
  289. ttccs->ttc.clk_rate_change_nb.notifier_call =
  290. ttc_rate_change_clocksource_cb;
  291. ttccs->ttc.clk_rate_change_nb.next = NULL;
  292. err = clk_notifier_register(ttccs->ttc.clk,
  293. &ttccs->ttc.clk_rate_change_nb);
  294. if (err)
  295. pr_warn("Unable to register clock notifier.\n");
  296. ttccs->ttc.base_addr = base;
  297. ttccs->cs.name = "ttc_clocksource";
  298. ttccs->cs.rating = 200;
  299. ttccs->cs.read = __ttc_clocksource_read;
  300. ttccs->cs.mask = CLOCKSOURCE_MASK(timer_width);
  301. ttccs->cs.flags = CLOCK_SOURCE_IS_CONTINUOUS;
  302. /*
  303. * Setup the clock source counter to be an incrementing counter
  304. * with no interrupt and it rolls over at 0xFFFF. Pre-scale
  305. * it by 32 also. Let it start running now.
  306. */
  307. writel_relaxed(0x0, ttccs->ttc.base_addr + TTC_IER_OFFSET);
  308. writel_relaxed(CLK_CNTRL_PRESCALE | CLK_CNTRL_PRESCALE_EN,
  309. ttccs->ttc.base_addr + TTC_CLK_CNTRL_OFFSET);
  310. writel_relaxed(CNT_CNTRL_RESET,
  311. ttccs->ttc.base_addr + TTC_CNT_CNTRL_OFFSET);
  312. err = clocksource_register_hz(&ttccs->cs, ttccs->ttc.freq / PRESCALE);
  313. if (err) {
  314. kfree(ttccs);
  315. return err;
  316. }
  317. ttc_sched_clock_val_reg = base + TTC_COUNT_VAL_OFFSET;
  318. sched_clock_register(ttc_sched_clock_read, timer_width,
  319. ttccs->ttc.freq / PRESCALE);
  320. return 0;
  321. }
  322. static int ttc_rate_change_clockevent_cb(struct notifier_block *nb,
  323. unsigned long event, void *data)
  324. {
  325. struct clk_notifier_data *ndata = data;
  326. struct ttc_timer *ttc = to_ttc_timer(nb);
  327. struct ttc_timer_clockevent *ttcce = container_of(ttc,
  328. struct ttc_timer_clockevent, ttc);
  329. switch (event) {
  330. case POST_RATE_CHANGE:
  331. /* update cached frequency */
  332. ttc->freq = ndata->new_rate;
  333. clockevents_update_freq(&ttcce->ce, ndata->new_rate / PRESCALE);
  334. /* fall through */
  335. case PRE_RATE_CHANGE:
  336. case ABORT_RATE_CHANGE:
  337. default:
  338. return NOTIFY_DONE;
  339. }
  340. }
  341. static int __init ttc_setup_clockevent(struct clk *clk,
  342. void __iomem *base, u32 irq)
  343. {
  344. struct ttc_timer_clockevent *ttcce;
  345. int err;
  346. ttcce = kzalloc(sizeof(*ttcce), GFP_KERNEL);
  347. if (!ttcce)
  348. return -ENOMEM;
  349. ttcce->ttc.clk = clk;
  350. err = clk_prepare_enable(ttcce->ttc.clk);
  351. if (err)
  352. goto out_kfree;
  353. ttcce->ttc.clk_rate_change_nb.notifier_call =
  354. ttc_rate_change_clockevent_cb;
  355. ttcce->ttc.clk_rate_change_nb.next = NULL;
  356. err = clk_notifier_register(ttcce->ttc.clk,
  357. &ttcce->ttc.clk_rate_change_nb);
  358. if (err) {
  359. pr_warn("Unable to register clock notifier.\n");
  360. goto out_kfree;
  361. }
  362. ttcce->ttc.freq = clk_get_rate(ttcce->ttc.clk);
  363. ttcce->ttc.base_addr = base;
  364. ttcce->ce.name = "ttc_clockevent";
  365. ttcce->ce.features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT;
  366. ttcce->ce.set_next_event = ttc_set_next_event;
  367. ttcce->ce.set_state_shutdown = ttc_shutdown;
  368. ttcce->ce.set_state_periodic = ttc_set_periodic;
  369. ttcce->ce.set_state_oneshot = ttc_shutdown;
  370. ttcce->ce.tick_resume = ttc_resume;
  371. ttcce->ce.rating = 200;
  372. ttcce->ce.irq = irq;
  373. ttcce->ce.cpumask = cpu_possible_mask;
  374. /*
  375. * Setup the clock event timer to be an interval timer which
  376. * is prescaled by 32 using the interval interrupt. Leave it
  377. * disabled for now.
  378. */
  379. writel_relaxed(0x23, ttcce->ttc.base_addr + TTC_CNT_CNTRL_OFFSET);
  380. writel_relaxed(CLK_CNTRL_PRESCALE | CLK_CNTRL_PRESCALE_EN,
  381. ttcce->ttc.base_addr + TTC_CLK_CNTRL_OFFSET);
  382. writel_relaxed(0x1, ttcce->ttc.base_addr + TTC_IER_OFFSET);
  383. err = request_irq(irq, ttc_clock_event_interrupt,
  384. IRQF_TIMER, ttcce->ce.name, ttcce);
  385. if (err)
  386. goto out_kfree;
  387. clockevents_config_and_register(&ttcce->ce,
  388. ttcce->ttc.freq / PRESCALE, 1, 0xfffe);
  389. return 0;
  390. out_kfree:
  391. kfree(ttcce);
  392. return err;
  393. }
  394. /**
  395. * ttc_timer_init - Initialize the timer
  396. *
  397. * Initializes the timer hardware and register the clock source and clock event
  398. * timers with Linux kernal timer framework
  399. */
  400. static int __init ttc_timer_init(struct device_node *timer)
  401. {
  402. unsigned int irq;
  403. void __iomem *timer_baseaddr;
  404. struct clk *clk_cs, *clk_ce;
  405. static int initialized;
  406. int clksel, ret;
  407. u32 timer_width = 16;
  408. if (initialized)
  409. return 0;
  410. initialized = 1;
  411. /*
  412. * Get the 1st Triple Timer Counter (TTC) block from the device tree
  413. * and use it. Note that the event timer uses the interrupt and it's the
  414. * 2nd TTC hence the irq_of_parse_and_map(,1)
  415. */
  416. timer_baseaddr = of_iomap(timer, 0);
  417. if (!timer_baseaddr) {
  418. pr_err("ERROR: invalid timer base address\n");
  419. return -ENXIO;
  420. }
  421. irq = irq_of_parse_and_map(timer, 1);
  422. if (irq <= 0) {
  423. pr_err("ERROR: invalid interrupt number\n");
  424. return -EINVAL;
  425. }
  426. of_property_read_u32(timer, "timer-width", &timer_width);
  427. clksel = readl_relaxed(timer_baseaddr + TTC_CLK_CNTRL_OFFSET);
  428. clksel = !!(clksel & TTC_CLK_CNTRL_CSRC_MASK);
  429. clk_cs = of_clk_get(timer, clksel);
  430. if (IS_ERR(clk_cs)) {
  431. pr_err("ERROR: timer input clock not found\n");
  432. return PTR_ERR(clk_cs);
  433. }
  434. clksel = readl_relaxed(timer_baseaddr + 4 + TTC_CLK_CNTRL_OFFSET);
  435. clksel = !!(clksel & TTC_CLK_CNTRL_CSRC_MASK);
  436. clk_ce = of_clk_get(timer, clksel);
  437. if (IS_ERR(clk_ce)) {
  438. pr_err("ERROR: timer input clock not found\n");
  439. return PTR_ERR(clk_ce);
  440. }
  441. ret = ttc_setup_clocksource(clk_cs, timer_baseaddr, timer_width);
  442. if (ret)
  443. return ret;
  444. ret = ttc_setup_clockevent(clk_ce, timer_baseaddr + 4, irq);
  445. if (ret)
  446. return ret;
  447. pr_info("%s #0 at %p, irq=%d\n", timer->name, timer_baseaddr, irq);
  448. return 0;
  449. }
  450. TIMER_OF_DECLARE(ttc, "cdns,ttc", ttc_timer_init);