time.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. /*
  2. * arch/arm/plat-spear/time.c
  3. *
  4. * Copyright (C) 2010 ST Microelectronics
  5. * Shiraz Hashim<shiraz.linux.kernel@gmail.com>
  6. *
  7. * This file is licensed under the terms of the GNU General Public
  8. * License version 2. This program is licensed "as is" without any
  9. * warranty of any kind, whether express or implied.
  10. */
  11. #include <linux/clk.h>
  12. #include <linux/clockchips.h>
  13. #include <linux/clocksource.h>
  14. #include <linux/err.h>
  15. #include <linux/init.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/ioport.h>
  18. #include <linux/io.h>
  19. #include <linux/kernel.h>
  20. #include <linux/of_irq.h>
  21. #include <linux/of_address.h>
  22. #include <linux/time.h>
  23. #include <linux/irq.h>
  24. #include <asm/mach/time.h>
  25. #include "generic.h"
  26. /*
  27. * We would use TIMER0 and TIMER1 as clockevent and clocksource.
  28. * Timer0 and Timer1 both belong to same gpt block in cpu subbsystem. Further
  29. * they share same functional clock. Any change in one's functional clock will
  30. * also affect other timer.
  31. */
  32. #define CLKEVT 0 /* gpt0, channel0 as clockevent */
  33. #define CLKSRC 1 /* gpt0, channel1 as clocksource */
  34. /* Register offsets, x is channel number */
  35. #define CR(x) ((x) * 0x80 + 0x80)
  36. #define IR(x) ((x) * 0x80 + 0x84)
  37. #define LOAD(x) ((x) * 0x80 + 0x88)
  38. #define COUNT(x) ((x) * 0x80 + 0x8C)
  39. /* Reg bit definitions */
  40. #define CTRL_INT_ENABLE 0x0100
  41. #define CTRL_ENABLE 0x0020
  42. #define CTRL_ONE_SHOT 0x0010
  43. #define CTRL_PRESCALER1 0x0
  44. #define CTRL_PRESCALER2 0x1
  45. #define CTRL_PRESCALER4 0x2
  46. #define CTRL_PRESCALER8 0x3
  47. #define CTRL_PRESCALER16 0x4
  48. #define CTRL_PRESCALER32 0x5
  49. #define CTRL_PRESCALER64 0x6
  50. #define CTRL_PRESCALER128 0x7
  51. #define CTRL_PRESCALER256 0x8
  52. #define INT_STATUS 0x1
  53. /*
  54. * Minimum clocksource/clockevent timer range in seconds
  55. */
  56. #define SPEAR_MIN_RANGE 4
  57. static __iomem void *gpt_base;
  58. static struct clk *gpt_clk;
  59. static int clockevent_next_event(unsigned long evt,
  60. struct clock_event_device *clk_event_dev);
  61. static void __init spear_clocksource_init(void)
  62. {
  63. u32 tick_rate;
  64. u16 val;
  65. /* program the prescaler (/256)*/
  66. writew(CTRL_PRESCALER256, gpt_base + CR(CLKSRC));
  67. /* find out actual clock driving Timer */
  68. tick_rate = clk_get_rate(gpt_clk);
  69. tick_rate >>= CTRL_PRESCALER256;
  70. writew(0xFFFF, gpt_base + LOAD(CLKSRC));
  71. val = readw(gpt_base + CR(CLKSRC));
  72. val &= ~CTRL_ONE_SHOT; /* autoreload mode */
  73. val |= CTRL_ENABLE ;
  74. writew(val, gpt_base + CR(CLKSRC));
  75. /* register the clocksource */
  76. clocksource_mmio_init(gpt_base + COUNT(CLKSRC), "tmr1", tick_rate,
  77. 200, 16, clocksource_mmio_readw_up);
  78. }
  79. static inline void timer_shutdown(struct clock_event_device *evt)
  80. {
  81. u16 val = readw(gpt_base + CR(CLKEVT));
  82. /* stop the timer */
  83. val &= ~CTRL_ENABLE;
  84. writew(val, gpt_base + CR(CLKEVT));
  85. }
  86. static int spear_shutdown(struct clock_event_device *evt)
  87. {
  88. timer_shutdown(evt);
  89. return 0;
  90. }
  91. static int spear_set_oneshot(struct clock_event_device *evt)
  92. {
  93. u16 val;
  94. /* stop the timer */
  95. timer_shutdown(evt);
  96. val = readw(gpt_base + CR(CLKEVT));
  97. val |= CTRL_ONE_SHOT;
  98. writew(val, gpt_base + CR(CLKEVT));
  99. return 0;
  100. }
  101. static int spear_set_periodic(struct clock_event_device *evt)
  102. {
  103. u32 period;
  104. u16 val;
  105. /* stop the timer */
  106. timer_shutdown(evt);
  107. period = clk_get_rate(gpt_clk) / HZ;
  108. period >>= CTRL_PRESCALER16;
  109. writew(period, gpt_base + LOAD(CLKEVT));
  110. val = readw(gpt_base + CR(CLKEVT));
  111. val &= ~CTRL_ONE_SHOT;
  112. val |= CTRL_ENABLE | CTRL_INT_ENABLE;
  113. writew(val, gpt_base + CR(CLKEVT));
  114. return 0;
  115. }
  116. static struct clock_event_device clkevt = {
  117. .name = "tmr0",
  118. .features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT,
  119. .set_state_shutdown = spear_shutdown,
  120. .set_state_periodic = spear_set_periodic,
  121. .set_state_oneshot = spear_set_oneshot,
  122. .tick_resume = spear_shutdown,
  123. .set_next_event = clockevent_next_event,
  124. .shift = 0, /* to be computed */
  125. };
  126. static int clockevent_next_event(unsigned long cycles,
  127. struct clock_event_device *clk_event_dev)
  128. {
  129. u16 val = readw(gpt_base + CR(CLKEVT));
  130. if (val & CTRL_ENABLE)
  131. writew(val & ~CTRL_ENABLE, gpt_base + CR(CLKEVT));
  132. writew(cycles, gpt_base + LOAD(CLKEVT));
  133. val |= CTRL_ENABLE | CTRL_INT_ENABLE;
  134. writew(val, gpt_base + CR(CLKEVT));
  135. return 0;
  136. }
  137. static irqreturn_t spear_timer_interrupt(int irq, void *dev_id)
  138. {
  139. struct clock_event_device *evt = &clkevt;
  140. writew(INT_STATUS, gpt_base + IR(CLKEVT));
  141. evt->event_handler(evt);
  142. return IRQ_HANDLED;
  143. }
  144. static struct irqaction spear_timer_irq = {
  145. .name = "timer",
  146. .flags = IRQF_TIMER,
  147. .handler = spear_timer_interrupt
  148. };
  149. static void __init spear_clockevent_init(int irq)
  150. {
  151. u32 tick_rate;
  152. /* program the prescaler */
  153. writew(CTRL_PRESCALER16, gpt_base + CR(CLKEVT));
  154. tick_rate = clk_get_rate(gpt_clk);
  155. tick_rate >>= CTRL_PRESCALER16;
  156. clkevt.cpumask = cpumask_of(0);
  157. clockevents_config_and_register(&clkevt, tick_rate, 3, 0xfff0);
  158. setup_irq(irq, &spear_timer_irq);
  159. }
  160. static const struct of_device_id timer_of_match[] __initconst = {
  161. { .compatible = "st,spear-timer", },
  162. { },
  163. };
  164. void __init spear_setup_of_timer(void)
  165. {
  166. struct device_node *np;
  167. int irq, ret;
  168. np = of_find_matching_node(NULL, timer_of_match);
  169. if (!np) {
  170. pr_err("%s: No timer passed via DT\n", __func__);
  171. return;
  172. }
  173. irq = irq_of_parse_and_map(np, 0);
  174. if (!irq) {
  175. pr_err("%s: No irq passed for timer via DT\n", __func__);
  176. return;
  177. }
  178. gpt_base = of_iomap(np, 0);
  179. if (!gpt_base) {
  180. pr_err("%s: of iomap failed\n", __func__);
  181. return;
  182. }
  183. gpt_clk = clk_get_sys("gpt0", NULL);
  184. if (!gpt_clk) {
  185. pr_err("%s:couldn't get clk for gpt\n", __func__);
  186. goto err_iomap;
  187. }
  188. ret = clk_prepare_enable(gpt_clk);
  189. if (ret < 0) {
  190. pr_err("%s:couldn't prepare-enable gpt clock\n", __func__);
  191. goto err_prepare_enable_clk;
  192. }
  193. spear_clockevent_init(irq);
  194. spear_clocksource_init();
  195. return;
  196. err_prepare_enable_clk:
  197. clk_put(gpt_clk);
  198. err_iomap:
  199. iounmap(gpt_base);
  200. }