timer-keystone.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. /*
  2. * Keystone broadcast clock-event
  3. *
  4. * Copyright 2013 Texas Instruments, Inc.
  5. *
  6. * Author: Ivan Khoronzhuk <ivan.khoronzhuk@ti.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. *
  12. */
  13. #include <linux/clk.h>
  14. #include <linux/clockchips.h>
  15. #include <linux/clocksource.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/of_address.h>
  18. #include <linux/of_irq.h>
  19. #define TIMER_NAME "timer-keystone"
  20. /* Timer register offsets */
  21. #define TIM12 0x10
  22. #define TIM34 0x14
  23. #define PRD12 0x18
  24. #define PRD34 0x1c
  25. #define TCR 0x20
  26. #define TGCR 0x24
  27. #define INTCTLSTAT 0x44
  28. /* Timer register bitfields */
  29. #define TCR_ENAMODE_MASK 0xC0
  30. #define TCR_ENAMODE_ONESHOT_MASK 0x40
  31. #define TCR_ENAMODE_PERIODIC_MASK 0x80
  32. #define TGCR_TIM_UNRESET_MASK 0x03
  33. #define INTCTLSTAT_ENINT_MASK 0x01
  34. /**
  35. * struct keystone_timer: holds timer's data
  36. * @base: timer memory base address
  37. * @hz_period: cycles per HZ period
  38. * @event_dev: event device based on timer
  39. */
  40. static struct keystone_timer {
  41. void __iomem *base;
  42. unsigned long hz_period;
  43. struct clock_event_device event_dev;
  44. } timer;
  45. static inline u32 keystone_timer_readl(unsigned long rg)
  46. {
  47. return readl_relaxed(timer.base + rg);
  48. }
  49. static inline void keystone_timer_writel(u32 val, unsigned long rg)
  50. {
  51. writel_relaxed(val, timer.base + rg);
  52. }
  53. /**
  54. * keystone_timer_barrier: write memory barrier
  55. * use explicit barrier to avoid using readl/writel non relaxed function
  56. * variants, because in our case non relaxed variants hide the true places
  57. * where barrier is needed.
  58. */
  59. static inline void keystone_timer_barrier(void)
  60. {
  61. __iowmb();
  62. }
  63. /**
  64. * keystone_timer_config: configures timer to work in oneshot/periodic modes.
  65. * @ mask: mask of the mode to configure
  66. * @ period: cycles number to configure for
  67. */
  68. static int keystone_timer_config(u64 period, int mask)
  69. {
  70. u32 tcr;
  71. u32 off;
  72. tcr = keystone_timer_readl(TCR);
  73. off = tcr & ~(TCR_ENAMODE_MASK);
  74. /* set enable mode */
  75. tcr |= mask;
  76. /* disable timer */
  77. keystone_timer_writel(off, TCR);
  78. /* here we have to be sure the timer has been disabled */
  79. keystone_timer_barrier();
  80. /* reset counter to zero, set new period */
  81. keystone_timer_writel(0, TIM12);
  82. keystone_timer_writel(0, TIM34);
  83. keystone_timer_writel(period & 0xffffffff, PRD12);
  84. keystone_timer_writel(period >> 32, PRD34);
  85. /*
  86. * enable timer
  87. * here we have to be sure that CNTLO, CNTHI, PRDLO, PRDHI registers
  88. * have been written.
  89. */
  90. keystone_timer_barrier();
  91. keystone_timer_writel(tcr, TCR);
  92. return 0;
  93. }
  94. static void keystone_timer_disable(void)
  95. {
  96. u32 tcr;
  97. tcr = keystone_timer_readl(TCR);
  98. /* disable timer */
  99. tcr &= ~(TCR_ENAMODE_MASK);
  100. keystone_timer_writel(tcr, TCR);
  101. }
  102. static irqreturn_t keystone_timer_interrupt(int irq, void *dev_id)
  103. {
  104. struct clock_event_device *evt = dev_id;
  105. evt->event_handler(evt);
  106. return IRQ_HANDLED;
  107. }
  108. static int keystone_set_next_event(unsigned long cycles,
  109. struct clock_event_device *evt)
  110. {
  111. return keystone_timer_config(cycles, TCR_ENAMODE_ONESHOT_MASK);
  112. }
  113. static int keystone_shutdown(struct clock_event_device *evt)
  114. {
  115. keystone_timer_disable();
  116. return 0;
  117. }
  118. static int keystone_set_periodic(struct clock_event_device *evt)
  119. {
  120. keystone_timer_config(timer.hz_period, TCR_ENAMODE_PERIODIC_MASK);
  121. return 0;
  122. }
  123. static int __init keystone_timer_init(struct device_node *np)
  124. {
  125. struct clock_event_device *event_dev = &timer.event_dev;
  126. unsigned long rate;
  127. struct clk *clk;
  128. int irq, error;
  129. irq = irq_of_parse_and_map(np, 0);
  130. if (!irq) {
  131. pr_err("%s: failed to map interrupts\n", __func__);
  132. return -EINVAL;
  133. }
  134. timer.base = of_iomap(np, 0);
  135. if (!timer.base) {
  136. pr_err("%s: failed to map registers\n", __func__);
  137. return -ENXIO;
  138. }
  139. clk = of_clk_get(np, 0);
  140. if (IS_ERR(clk)) {
  141. pr_err("%s: failed to get clock\n", __func__);
  142. iounmap(timer.base);
  143. return PTR_ERR(clk);
  144. }
  145. error = clk_prepare_enable(clk);
  146. if (error) {
  147. pr_err("%s: failed to enable clock\n", __func__);
  148. goto err;
  149. }
  150. rate = clk_get_rate(clk);
  151. /* disable, use internal clock source */
  152. keystone_timer_writel(0, TCR);
  153. /* here we have to be sure the timer has been disabled */
  154. keystone_timer_barrier();
  155. /* reset timer as 64-bit, no pre-scaler, plus features are disabled */
  156. keystone_timer_writel(0, TGCR);
  157. /* unreset timer */
  158. keystone_timer_writel(TGCR_TIM_UNRESET_MASK, TGCR);
  159. /* init counter to zero */
  160. keystone_timer_writel(0, TIM12);
  161. keystone_timer_writel(0, TIM34);
  162. timer.hz_period = DIV_ROUND_UP(rate, HZ);
  163. /* enable timer interrupts */
  164. keystone_timer_writel(INTCTLSTAT_ENINT_MASK, INTCTLSTAT);
  165. error = request_irq(irq, keystone_timer_interrupt, IRQF_TIMER,
  166. TIMER_NAME, event_dev);
  167. if (error) {
  168. pr_err("%s: failed to setup irq\n", __func__);
  169. goto err;
  170. }
  171. /* setup clockevent */
  172. event_dev->features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT;
  173. event_dev->set_next_event = keystone_set_next_event;
  174. event_dev->set_state_shutdown = keystone_shutdown;
  175. event_dev->set_state_periodic = keystone_set_periodic;
  176. event_dev->set_state_oneshot = keystone_shutdown;
  177. event_dev->cpumask = cpu_all_mask;
  178. event_dev->owner = THIS_MODULE;
  179. event_dev->name = TIMER_NAME;
  180. event_dev->irq = irq;
  181. clockevents_config_and_register(event_dev, rate, 1, ULONG_MAX);
  182. pr_info("keystone timer clock @%lu Hz\n", rate);
  183. return 0;
  184. err:
  185. clk_put(clk);
  186. iounmap(timer.base);
  187. return error;
  188. }
  189. CLOCKSOURCE_OF_DECLARE(keystone_timer, "ti,keystone-timer",
  190. keystone_timer_init);