time.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. /*
  2. * arch/arm/plat-spear/time.c
  3. *
  4. * Copyright (C) 2010 ST Microelectronics
  5. * Shiraz Hashim<shiraz.hashim@st.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 <mach/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 void clockevent_set_mode(enum clock_event_mode mode,
  60. struct clock_event_device *clk_event_dev);
  61. static int clockevent_next_event(unsigned long evt,
  62. struct clock_event_device *clk_event_dev);
  63. static void spear_clocksource_init(void)
  64. {
  65. u32 tick_rate;
  66. u16 val;
  67. /* program the prescaler (/256)*/
  68. writew(CTRL_PRESCALER256, gpt_base + CR(CLKSRC));
  69. /* find out actual clock driving Timer */
  70. tick_rate = clk_get_rate(gpt_clk);
  71. tick_rate >>= CTRL_PRESCALER256;
  72. writew(0xFFFF, gpt_base + LOAD(CLKSRC));
  73. val = readw(gpt_base + CR(CLKSRC));
  74. val &= ~CTRL_ONE_SHOT; /* autoreload mode */
  75. val |= CTRL_ENABLE ;
  76. writew(val, gpt_base + CR(CLKSRC));
  77. /* register the clocksource */
  78. clocksource_mmio_init(gpt_base + COUNT(CLKSRC), "tmr1", tick_rate,
  79. 200, 16, clocksource_mmio_readw_up);
  80. }
  81. static struct clock_event_device clkevt = {
  82. .name = "tmr0",
  83. .features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT,
  84. .set_mode = clockevent_set_mode,
  85. .set_next_event = clockevent_next_event,
  86. .shift = 0, /* to be computed */
  87. };
  88. static void clockevent_set_mode(enum clock_event_mode mode,
  89. struct clock_event_device *clk_event_dev)
  90. {
  91. u32 period;
  92. u16 val;
  93. /* stop the timer */
  94. val = readw(gpt_base + CR(CLKEVT));
  95. val &= ~CTRL_ENABLE;
  96. writew(val, gpt_base + CR(CLKEVT));
  97. switch (mode) {
  98. case CLOCK_EVT_MODE_PERIODIC:
  99. period = clk_get_rate(gpt_clk) / HZ;
  100. period >>= CTRL_PRESCALER16;
  101. writew(period, gpt_base + LOAD(CLKEVT));
  102. val = readw(gpt_base + CR(CLKEVT));
  103. val &= ~CTRL_ONE_SHOT;
  104. val |= CTRL_ENABLE | CTRL_INT_ENABLE;
  105. writew(val, gpt_base + CR(CLKEVT));
  106. break;
  107. case CLOCK_EVT_MODE_ONESHOT:
  108. val = readw(gpt_base + CR(CLKEVT));
  109. val |= CTRL_ONE_SHOT;
  110. writew(val, gpt_base + CR(CLKEVT));
  111. break;
  112. case CLOCK_EVT_MODE_UNUSED:
  113. case CLOCK_EVT_MODE_SHUTDOWN:
  114. case CLOCK_EVT_MODE_RESUME:
  115. break;
  116. default:
  117. pr_err("Invalid mode requested\n");
  118. break;
  119. }
  120. }
  121. static int clockevent_next_event(unsigned long cycles,
  122. struct clock_event_device *clk_event_dev)
  123. {
  124. u16 val = readw(gpt_base + CR(CLKEVT));
  125. if (val & CTRL_ENABLE)
  126. writew(val & ~CTRL_ENABLE, gpt_base + CR(CLKEVT));
  127. writew(cycles, gpt_base + LOAD(CLKEVT));
  128. val |= CTRL_ENABLE | CTRL_INT_ENABLE;
  129. writew(val, gpt_base + CR(CLKEVT));
  130. return 0;
  131. }
  132. static irqreturn_t spear_timer_interrupt(int irq, void *dev_id)
  133. {
  134. struct clock_event_device *evt = &clkevt;
  135. writew(INT_STATUS, gpt_base + IR(CLKEVT));
  136. evt->event_handler(evt);
  137. return IRQ_HANDLED;
  138. }
  139. static struct irqaction spear_timer_irq = {
  140. .name = "timer",
  141. .flags = IRQF_DISABLED | IRQF_TIMER,
  142. .handler = spear_timer_interrupt
  143. };
  144. static void __init spear_clockevent_init(int irq)
  145. {
  146. u32 tick_rate;
  147. /* program the prescaler */
  148. writew(CTRL_PRESCALER16, gpt_base + CR(CLKEVT));
  149. tick_rate = clk_get_rate(gpt_clk);
  150. tick_rate >>= CTRL_PRESCALER16;
  151. clockevents_calc_mult_shift(&clkevt, tick_rate, SPEAR_MIN_RANGE);
  152. clkevt.max_delta_ns = clockevent_delta2ns(0xfff0,
  153. &clkevt);
  154. clkevt.min_delta_ns = clockevent_delta2ns(3, &clkevt);
  155. clkevt.cpumask = cpumask_of(0);
  156. clockevents_register_device(&clkevt);
  157. setup_irq(irq, &spear_timer_irq);
  158. }
  159. const static struct of_device_id timer_of_match[] __initconst = {
  160. { .compatible = "st,spear-timer", },
  161. { },
  162. };
  163. void __init spear_setup_of_timer(void)
  164. {
  165. struct device_node *np;
  166. int irq, ret;
  167. np = of_find_matching_node(NULL, timer_of_match);
  168. if (!np) {
  169. pr_err("%s: No timer passed via DT\n", __func__);
  170. return;
  171. }
  172. irq = irq_of_parse_and_map(np, 0);
  173. if (!irq) {
  174. pr_err("%s: No irq passed for timer via DT\n", __func__);
  175. return;
  176. }
  177. gpt_base = of_iomap(np, 0);
  178. if (!gpt_base) {
  179. pr_err("%s: of iomap failed\n", __func__);
  180. return;
  181. }
  182. gpt_clk = clk_get_sys("gpt0", NULL);
  183. if (!gpt_clk) {
  184. pr_err("%s:couldn't get clk for gpt\n", __func__);
  185. goto err_iomap;
  186. }
  187. ret = clk_prepare_enable(gpt_clk);
  188. if (ret < 0) {
  189. pr_err("%s:couldn't prepare-enable gpt clock\n", __func__);
  190. goto err_prepare_enable_clk;
  191. }
  192. spear_clockevent_init(irq);
  193. spear_clocksource_init();
  194. return;
  195. err_prepare_enable_clk:
  196. clk_put(gpt_clk);
  197. err_iomap:
  198. iounmap(gpt_base);
  199. }