time.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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/io.h>
  18. #include <linux/kernel.h>
  19. #include <linux/time.h>
  20. #include <linux/irq.h>
  21. #include <asm/mach/time.h>
  22. #include <mach/generic.h>
  23. #include <mach/hardware.h>
  24. #include <mach/irqs.h>
  25. /*
  26. * We would use TIMER0 and TIMER1 as clockevent and clocksource.
  27. * Timer0 and Timer1 both belong to same gpt block in cpu subbsystem. Further
  28. * they share same functional clock. Any change in one's functional clock will
  29. * also affect other timer.
  30. */
  31. #define CLKEVT 0 /* gpt0, channel0 as clockevent */
  32. #define CLKSRC 1 /* gpt0, channel1 as clocksource */
  33. /* Register offsets, x is channel number */
  34. #define CR(x) ((x) * 0x80 + 0x80)
  35. #define IR(x) ((x) * 0x80 + 0x84)
  36. #define LOAD(x) ((x) * 0x80 + 0x88)
  37. #define COUNT(x) ((x) * 0x80 + 0x8C)
  38. /* Reg bit definitions */
  39. #define CTRL_INT_ENABLE 0x0100
  40. #define CTRL_ENABLE 0x0020
  41. #define CTRL_ONE_SHOT 0x0010
  42. #define CTRL_PRESCALER1 0x0
  43. #define CTRL_PRESCALER2 0x1
  44. #define CTRL_PRESCALER4 0x2
  45. #define CTRL_PRESCALER8 0x3
  46. #define CTRL_PRESCALER16 0x4
  47. #define CTRL_PRESCALER32 0x5
  48. #define CTRL_PRESCALER64 0x6
  49. #define CTRL_PRESCALER128 0x7
  50. #define CTRL_PRESCALER256 0x8
  51. #define INT_STATUS 0x1
  52. /*
  53. * Minimum clocksource/clockevent timer range in seconds
  54. */
  55. #define SPEAR_MIN_RANGE 4
  56. static __iomem void *gpt_base;
  57. static struct clk *gpt_clk;
  58. static void clockevent_set_mode(enum clock_event_mode mode,
  59. struct clock_event_device *clk_event_dev);
  60. static int clockevent_next_event(unsigned long evt,
  61. struct clock_event_device *clk_event_dev);
  62. static void spear_clocksource_init(void)
  63. {
  64. u32 tick_rate;
  65. u16 val;
  66. /* program the prescaler (/256)*/
  67. writew(CTRL_PRESCALER256, gpt_base + CR(CLKSRC));
  68. /* find out actual clock driving Timer */
  69. tick_rate = clk_get_rate(gpt_clk);
  70. tick_rate >>= CTRL_PRESCALER256;
  71. writew(0xFFFF, gpt_base + LOAD(CLKSRC));
  72. val = readw(gpt_base + CR(CLKSRC));
  73. val &= ~CTRL_ONE_SHOT; /* autoreload mode */
  74. val |= CTRL_ENABLE ;
  75. writew(val, gpt_base + CR(CLKSRC));
  76. /* register the clocksource */
  77. clocksource_mmio_init(gpt_base + COUNT(CLKSRC), "tmr1", tick_rate,
  78. 200, 16, clocksource_mmio_readw_up);
  79. }
  80. static struct clock_event_device clkevt = {
  81. .name = "tmr0",
  82. .features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT,
  83. .set_mode = clockevent_set_mode,
  84. .set_next_event = clockevent_next_event,
  85. .shift = 0, /* to be computed */
  86. };
  87. static void clockevent_set_mode(enum clock_event_mode mode,
  88. struct clock_event_device *clk_event_dev)
  89. {
  90. u32 period;
  91. u16 val;
  92. /* stop the timer */
  93. val = readw(gpt_base + CR(CLKEVT));
  94. val &= ~CTRL_ENABLE;
  95. writew(val, gpt_base + CR(CLKEVT));
  96. switch (mode) {
  97. case CLOCK_EVT_MODE_PERIODIC:
  98. period = clk_get_rate(gpt_clk) / HZ;
  99. period >>= CTRL_PRESCALER16;
  100. writew(period, gpt_base + LOAD(CLKEVT));
  101. val = readw(gpt_base + CR(CLKEVT));
  102. val &= ~CTRL_ONE_SHOT;
  103. val |= CTRL_ENABLE | CTRL_INT_ENABLE;
  104. writew(val, gpt_base + CR(CLKEVT));
  105. break;
  106. case CLOCK_EVT_MODE_ONESHOT:
  107. val = readw(gpt_base + CR(CLKEVT));
  108. val |= CTRL_ONE_SHOT;
  109. writew(val, gpt_base + CR(CLKEVT));
  110. break;
  111. case CLOCK_EVT_MODE_UNUSED:
  112. case CLOCK_EVT_MODE_SHUTDOWN:
  113. case CLOCK_EVT_MODE_RESUME:
  114. break;
  115. default:
  116. pr_err("Invalid mode requested\n");
  117. break;
  118. }
  119. }
  120. static int clockevent_next_event(unsigned long cycles,
  121. struct clock_event_device *clk_event_dev)
  122. {
  123. u16 val;
  124. writew(cycles, gpt_base + LOAD(CLKEVT));
  125. val = readw(gpt_base + CR(CLKEVT));
  126. val |= CTRL_ENABLE | CTRL_INT_ENABLE;
  127. writew(val, gpt_base + CR(CLKEVT));
  128. return 0;
  129. }
  130. static irqreturn_t spear_timer_interrupt(int irq, void *dev_id)
  131. {
  132. struct clock_event_device *evt = &clkevt;
  133. writew(INT_STATUS, gpt_base + IR(CLKEVT));
  134. evt->event_handler(evt);
  135. return IRQ_HANDLED;
  136. }
  137. static struct irqaction spear_timer_irq = {
  138. .name = "timer",
  139. .flags = IRQF_DISABLED | IRQF_TIMER,
  140. .handler = spear_timer_interrupt
  141. };
  142. static void __init spear_clockevent_init(void)
  143. {
  144. u32 tick_rate;
  145. /* program the prescaler */
  146. writew(CTRL_PRESCALER16, gpt_base + CR(CLKEVT));
  147. tick_rate = clk_get_rate(gpt_clk);
  148. tick_rate >>= CTRL_PRESCALER16;
  149. clockevents_calc_mult_shift(&clkevt, tick_rate, SPEAR_MIN_RANGE);
  150. clkevt.max_delta_ns = clockevent_delta2ns(0xfff0,
  151. &clkevt);
  152. clkevt.min_delta_ns = clockevent_delta2ns(3, &clkevt);
  153. clkevt.cpumask = cpumask_of(0);
  154. clockevents_register_device(&clkevt);
  155. setup_irq(SPEAR_GPT0_CHAN0_IRQ, &spear_timer_irq);
  156. }
  157. void __init spear_setup_timer(void)
  158. {
  159. int ret;
  160. if (!request_mem_region(SPEAR_GPT0_BASE, SZ_1K, "gpt0")) {
  161. pr_err("%s:cannot get IO addr\n", __func__);
  162. return;
  163. }
  164. gpt_base = (void __iomem *)ioremap(SPEAR_GPT0_BASE, SZ_1K);
  165. if (!gpt_base) {
  166. pr_err("%s:ioremap failed for gpt\n", __func__);
  167. goto err_mem;
  168. }
  169. gpt_clk = clk_get_sys("gpt0", NULL);
  170. if (!gpt_clk) {
  171. pr_err("%s:couldn't get clk for gpt\n", __func__);
  172. goto err_iomap;
  173. }
  174. ret = clk_enable(gpt_clk);
  175. if (ret < 0) {
  176. pr_err("%s:couldn't enable gpt clock\n", __func__);
  177. goto err_clk;
  178. }
  179. spear_clockevent_init();
  180. spear_clocksource_init();
  181. return;
  182. err_clk:
  183. clk_put(gpt_clk);
  184. err_iomap:
  185. iounmap(gpt_base);
  186. err_mem:
  187. release_mem_region(SPEAR_GPT0_BASE, SZ_1K);
  188. }