cadence_ttc_timer.c 14 KB

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