time.c 5.9 KB

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