time.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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. static __iomem void *gpt_base;
  54. static struct clk *gpt_clk;
  55. static void clockevent_set_mode(enum clock_event_mode mode,
  56. struct clock_event_device *clk_event_dev);
  57. static int clockevent_next_event(unsigned long evt,
  58. struct clock_event_device *clk_event_dev);
  59. /*
  60. * Following clocksource_set_clock and clockevent_set_clock picked
  61. * from arch/mips/kernel/time.c
  62. */
  63. void __init clocksource_set_clock(struct clocksource *cs, unsigned int clock)
  64. {
  65. u64 temp;
  66. u32 shift;
  67. /* Find a shift value */
  68. for (shift = 32; shift > 0; shift--) {
  69. temp = (u64) NSEC_PER_SEC << shift;
  70. do_div(temp, clock);
  71. if ((temp >> 32) == 0)
  72. break;
  73. }
  74. cs->shift = shift;
  75. cs->mult = (u32) temp;
  76. }
  77. void __init clockevent_set_clock(struct clock_event_device *cd,
  78. unsigned int clock)
  79. {
  80. u64 temp;
  81. u32 shift;
  82. /* Find a shift value */
  83. for (shift = 32; shift > 0; shift--) {
  84. temp = (u64) clock << shift;
  85. do_div(temp, NSEC_PER_SEC);
  86. if ((temp >> 32) == 0)
  87. break;
  88. }
  89. cd->shift = shift;
  90. cd->mult = (u32) temp;
  91. }
  92. static cycle_t clocksource_read_cycles(struct clocksource *cs)
  93. {
  94. return (cycle_t) readw(gpt_base + COUNT(CLKSRC));
  95. }
  96. static struct clocksource clksrc = {
  97. .name = "tmr1",
  98. .rating = 200, /* its a pretty decent clock */
  99. .read = clocksource_read_cycles,
  100. .mask = 0xFFFF, /* 16 bits */
  101. .mult = 0, /* to be computed */
  102. .shift = 0, /* to be computed */
  103. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  104. };
  105. static void spear_clocksource_init(void)
  106. {
  107. u32 tick_rate;
  108. u16 val;
  109. /* program the prescaler (/256)*/
  110. writew(CTRL_PRESCALER256, gpt_base + CR(CLKSRC));
  111. /* find out actual clock driving Timer */
  112. tick_rate = clk_get_rate(gpt_clk);
  113. tick_rate >>= CTRL_PRESCALER256;
  114. writew(0xFFFF, gpt_base + LOAD(CLKSRC));
  115. val = readw(gpt_base + CR(CLKSRC));
  116. val &= ~CTRL_ONE_SHOT; /* autoreload mode */
  117. val |= CTRL_ENABLE ;
  118. writew(val, gpt_base + CR(CLKSRC));
  119. clocksource_set_clock(&clksrc, tick_rate);
  120. /* register the clocksource */
  121. clocksource_register(&clksrc);
  122. }
  123. static struct clock_event_device clkevt = {
  124. .name = "tmr0",
  125. .features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT,
  126. .set_mode = clockevent_set_mode,
  127. .set_next_event = clockevent_next_event,
  128. .shift = 0, /* to be computed */
  129. };
  130. static void clockevent_set_mode(enum clock_event_mode mode,
  131. struct clock_event_device *clk_event_dev)
  132. {
  133. u32 period;
  134. u16 val;
  135. /* stop the timer */
  136. val = readw(gpt_base + CR(CLKEVT));
  137. val &= ~CTRL_ENABLE;
  138. writew(val, gpt_base + CR(CLKEVT));
  139. switch (mode) {
  140. case CLOCK_EVT_MODE_PERIODIC:
  141. period = clk_get_rate(gpt_clk) / HZ;
  142. period >>= CTRL_PRESCALER16;
  143. writew(period, gpt_base + LOAD(CLKEVT));
  144. val = readw(gpt_base + CR(CLKEVT));
  145. val &= ~CTRL_ONE_SHOT;
  146. val |= CTRL_ENABLE | CTRL_INT_ENABLE;
  147. writew(val, gpt_base + CR(CLKEVT));
  148. break;
  149. case CLOCK_EVT_MODE_ONESHOT:
  150. val = readw(gpt_base + CR(CLKEVT));
  151. val |= CTRL_ONE_SHOT;
  152. writew(val, gpt_base + CR(CLKEVT));
  153. break;
  154. case CLOCK_EVT_MODE_UNUSED:
  155. case CLOCK_EVT_MODE_SHUTDOWN:
  156. case CLOCK_EVT_MODE_RESUME:
  157. break;
  158. default:
  159. pr_err("Invalid mode requested\n");
  160. break;
  161. }
  162. }
  163. static int clockevent_next_event(unsigned long cycles,
  164. struct clock_event_device *clk_event_dev)
  165. {
  166. u16 val;
  167. writew(cycles, gpt_base + LOAD(CLKEVT));
  168. val = readw(gpt_base + CR(CLKEVT));
  169. val |= CTRL_ENABLE | CTRL_INT_ENABLE;
  170. writew(val, gpt_base + CR(CLKEVT));
  171. return 0;
  172. }
  173. static irqreturn_t spear_timer_interrupt(int irq, void *dev_id)
  174. {
  175. struct clock_event_device *evt = &clkevt;
  176. writew(INT_STATUS, gpt_base + IR(CLKEVT));
  177. evt->event_handler(evt);
  178. return IRQ_HANDLED;
  179. }
  180. static struct irqaction spear_timer_irq = {
  181. .name = "timer",
  182. .flags = IRQF_DISABLED | IRQF_TIMER,
  183. .handler = spear_timer_interrupt
  184. };
  185. static void __init spear_clockevent_init(void)
  186. {
  187. u32 tick_rate;
  188. /* program the prescaler */
  189. writew(CTRL_PRESCALER16, gpt_base + CR(CLKEVT));
  190. tick_rate = clk_get_rate(gpt_clk);
  191. tick_rate >>= CTRL_PRESCALER16;
  192. clockevent_set_clock(&clkevt, tick_rate);
  193. clkevt.max_delta_ns = clockevent_delta2ns(0xfff0,
  194. &clkevt);
  195. clkevt.min_delta_ns = clockevent_delta2ns(3, &clkevt);
  196. clkevt.cpumask = cpumask_of(0);
  197. clockevents_register_device(&clkevt);
  198. setup_irq(SPEAR_GPT0_CHAN0_IRQ, &spear_timer_irq);
  199. }
  200. void __init spear_setup_timer(void)
  201. {
  202. struct clk *pll3_clk;
  203. if (!request_mem_region(SPEAR_GPT0_BASE, SZ_1K, "gpt0")) {
  204. pr_err("%s:cannot get IO addr\n", __func__);
  205. return;
  206. }
  207. gpt_base = (void __iomem *)ioremap(SPEAR_GPT0_BASE, SZ_1K);
  208. if (!gpt_base) {
  209. pr_err("%s:ioremap failed for gpt\n", __func__);
  210. goto err_mem;
  211. }
  212. gpt_clk = clk_get_sys("gpt0", NULL);
  213. if (!gpt_clk) {
  214. pr_err("%s:couldn't get clk for gpt\n", __func__);
  215. goto err_iomap;
  216. }
  217. pll3_clk = clk_get(NULL, "pll3_48m_clk");
  218. if (!pll3_clk) {
  219. pr_err("%s:couldn't get PLL3 as parent for gpt\n", __func__);
  220. goto err_iomap;
  221. }
  222. clk_set_parent(gpt_clk, pll3_clk);
  223. spear_clockevent_init();
  224. spear_clocksource_init();
  225. return;
  226. err_iomap:
  227. iounmap(gpt_base);
  228. err_mem:
  229. release_mem_region(SPEAR_GPT0_BASE, SZ_1K);
  230. }
  231. struct sys_timer spear_sys_timer = {
  232. .init = spear_setup_timer,
  233. };