time.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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 cycle_t clocksource_read_cycles(struct clocksource *cs)
  63. {
  64. return (cycle_t) readw(gpt_base + COUNT(CLKSRC));
  65. }
  66. static struct clocksource clksrc = {
  67. .name = "tmr1",
  68. .rating = 200, /* its a pretty decent clock */
  69. .read = clocksource_read_cycles,
  70. .mask = 0xFFFF, /* 16 bits */
  71. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  72. };
  73. static void spear_clocksource_init(void)
  74. {
  75. u32 tick_rate;
  76. u16 val;
  77. /* program the prescaler (/256)*/
  78. writew(CTRL_PRESCALER256, gpt_base + CR(CLKSRC));
  79. /* find out actual clock driving Timer */
  80. tick_rate = clk_get_rate(gpt_clk);
  81. tick_rate >>= CTRL_PRESCALER256;
  82. writew(0xFFFF, gpt_base + LOAD(CLKSRC));
  83. val = readw(gpt_base + CR(CLKSRC));
  84. val &= ~CTRL_ONE_SHOT; /* autoreload mode */
  85. val |= CTRL_ENABLE ;
  86. writew(val, gpt_base + CR(CLKSRC));
  87. /* register the clocksource */
  88. clocksource_register_hz(&clksrc, tick_rate);
  89. }
  90. static struct clock_event_device clkevt = {
  91. .name = "tmr0",
  92. .features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT,
  93. .set_mode = clockevent_set_mode,
  94. .set_next_event = clockevent_next_event,
  95. .shift = 0, /* to be computed */
  96. };
  97. static void clockevent_set_mode(enum clock_event_mode mode,
  98. struct clock_event_device *clk_event_dev)
  99. {
  100. u32 period;
  101. u16 val;
  102. /* stop the timer */
  103. val = readw(gpt_base + CR(CLKEVT));
  104. val &= ~CTRL_ENABLE;
  105. writew(val, gpt_base + CR(CLKEVT));
  106. switch (mode) {
  107. case CLOCK_EVT_MODE_PERIODIC:
  108. period = clk_get_rate(gpt_clk) / HZ;
  109. period >>= CTRL_PRESCALER16;
  110. writew(period, gpt_base + LOAD(CLKEVT));
  111. val = readw(gpt_base + CR(CLKEVT));
  112. val &= ~CTRL_ONE_SHOT;
  113. val |= CTRL_ENABLE | CTRL_INT_ENABLE;
  114. writew(val, gpt_base + CR(CLKEVT));
  115. break;
  116. case CLOCK_EVT_MODE_ONESHOT:
  117. val = readw(gpt_base + CR(CLKEVT));
  118. val |= CTRL_ONE_SHOT;
  119. writew(val, gpt_base + CR(CLKEVT));
  120. break;
  121. case CLOCK_EVT_MODE_UNUSED:
  122. case CLOCK_EVT_MODE_SHUTDOWN:
  123. case CLOCK_EVT_MODE_RESUME:
  124. break;
  125. default:
  126. pr_err("Invalid mode requested\n");
  127. break;
  128. }
  129. }
  130. static int clockevent_next_event(unsigned long cycles,
  131. struct clock_event_device *clk_event_dev)
  132. {
  133. u16 val;
  134. writew(cycles, gpt_base + LOAD(CLKEVT));
  135. val = readw(gpt_base + CR(CLKEVT));
  136. val |= CTRL_ENABLE | CTRL_INT_ENABLE;
  137. writew(val, gpt_base + CR(CLKEVT));
  138. return 0;
  139. }
  140. static irqreturn_t spear_timer_interrupt(int irq, void *dev_id)
  141. {
  142. struct clock_event_device *evt = &clkevt;
  143. writew(INT_STATUS, gpt_base + IR(CLKEVT));
  144. evt->event_handler(evt);
  145. return IRQ_HANDLED;
  146. }
  147. static struct irqaction spear_timer_irq = {
  148. .name = "timer",
  149. .flags = IRQF_DISABLED | IRQF_TIMER,
  150. .handler = spear_timer_interrupt
  151. };
  152. static void __init spear_clockevent_init(void)
  153. {
  154. u32 tick_rate;
  155. /* program the prescaler */
  156. writew(CTRL_PRESCALER16, gpt_base + CR(CLKEVT));
  157. tick_rate = clk_get_rate(gpt_clk);
  158. tick_rate >>= CTRL_PRESCALER16;
  159. clockevents_calc_mult_shift(&clkevt, tick_rate, SPEAR_MIN_RANGE);
  160. clkevt.max_delta_ns = clockevent_delta2ns(0xfff0,
  161. &clkevt);
  162. clkevt.min_delta_ns = clockevent_delta2ns(3, &clkevt);
  163. clkevt.cpumask = cpumask_of(0);
  164. clockevents_register_device(&clkevt);
  165. setup_irq(SPEAR_GPT0_CHAN0_IRQ, &spear_timer_irq);
  166. }
  167. void __init spear_setup_timer(void)
  168. {
  169. int ret;
  170. if (!request_mem_region(SPEAR_GPT0_BASE, SZ_1K, "gpt0")) {
  171. pr_err("%s:cannot get IO addr\n", __func__);
  172. return;
  173. }
  174. gpt_base = (void __iomem *)ioremap(SPEAR_GPT0_BASE, SZ_1K);
  175. if (!gpt_base) {
  176. pr_err("%s:ioremap failed for gpt\n", __func__);
  177. goto err_mem;
  178. }
  179. gpt_clk = clk_get_sys("gpt0", NULL);
  180. if (!gpt_clk) {
  181. pr_err("%s:couldn't get clk for gpt\n", __func__);
  182. goto err_iomap;
  183. }
  184. ret = clk_enable(gpt_clk);
  185. if (ret < 0) {
  186. pr_err("%s:couldn't enable gpt clock\n", __func__);
  187. goto err_clk;
  188. }
  189. spear_clockevent_init();
  190. spear_clocksource_init();
  191. return;
  192. err_clk:
  193. clk_put(gpt_clk);
  194. err_iomap:
  195. iounmap(gpt_base);
  196. err_mem:
  197. release_mem_region(SPEAR_GPT0_BASE, SZ_1K);
  198. }