cadence_ttc_timer.c 12 KB

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