time.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. /* linux/arch/arm/mach-exynos4/time.c
  2. *
  3. * Copyright (c) 2010-2011 Samsung Electronics Co., Ltd.
  4. * http://www.samsung.com
  5. *
  6. * EXYNOS4 (and compatible) HRT support
  7. * PWM 2/4 is used for this feature
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2 as
  11. * published by the Free Software Foundation.
  12. */
  13. #include <linux/sched.h>
  14. #include <linux/interrupt.h>
  15. #include <linux/irq.h>
  16. #include <linux/err.h>
  17. #include <linux/clk.h>
  18. #include <linux/clockchips.h>
  19. #include <linux/platform_device.h>
  20. #include <asm/smp_twd.h>
  21. #include <mach/map.h>
  22. #include <plat/regs-timer.h>
  23. #include <asm/mach/time.h>
  24. static unsigned long clock_count_per_tick;
  25. static struct clk *tin2;
  26. static struct clk *tin4;
  27. static struct clk *tdiv2;
  28. static struct clk *tdiv4;
  29. static struct clk *timerclk;
  30. static void exynos4_pwm_stop(unsigned int pwm_id)
  31. {
  32. unsigned long tcon;
  33. tcon = __raw_readl(S3C2410_TCON);
  34. switch (pwm_id) {
  35. case 2:
  36. tcon &= ~S3C2410_TCON_T2START;
  37. break;
  38. case 4:
  39. tcon &= ~S3C2410_TCON_T4START;
  40. break;
  41. default:
  42. break;
  43. }
  44. __raw_writel(tcon, S3C2410_TCON);
  45. }
  46. static void exynos4_pwm_init(unsigned int pwm_id, unsigned long tcnt)
  47. {
  48. unsigned long tcon;
  49. tcon = __raw_readl(S3C2410_TCON);
  50. /* timers reload after counting zero, so reduce the count by 1 */
  51. tcnt--;
  52. /* ensure timer is stopped... */
  53. switch (pwm_id) {
  54. case 2:
  55. tcon &= ~(0xf<<12);
  56. tcon |= S3C2410_TCON_T2MANUALUPD;
  57. __raw_writel(tcnt, S3C2410_TCNTB(2));
  58. __raw_writel(tcnt, S3C2410_TCMPB(2));
  59. __raw_writel(tcon, S3C2410_TCON);
  60. break;
  61. case 4:
  62. tcon &= ~(7<<20);
  63. tcon |= S3C2410_TCON_T4MANUALUPD;
  64. __raw_writel(tcnt, S3C2410_TCNTB(4));
  65. __raw_writel(tcnt, S3C2410_TCMPB(4));
  66. __raw_writel(tcon, S3C2410_TCON);
  67. break;
  68. default:
  69. break;
  70. }
  71. }
  72. static inline void exynos4_pwm_start(unsigned int pwm_id, bool periodic)
  73. {
  74. unsigned long tcon;
  75. tcon = __raw_readl(S3C2410_TCON);
  76. switch (pwm_id) {
  77. case 2:
  78. tcon |= S3C2410_TCON_T2START;
  79. tcon &= ~S3C2410_TCON_T2MANUALUPD;
  80. if (periodic)
  81. tcon |= S3C2410_TCON_T2RELOAD;
  82. else
  83. tcon &= ~S3C2410_TCON_T2RELOAD;
  84. break;
  85. case 4:
  86. tcon |= S3C2410_TCON_T4START;
  87. tcon &= ~S3C2410_TCON_T4MANUALUPD;
  88. if (periodic)
  89. tcon |= S3C2410_TCON_T4RELOAD;
  90. else
  91. tcon &= ~S3C2410_TCON_T4RELOAD;
  92. break;
  93. default:
  94. break;
  95. }
  96. __raw_writel(tcon, S3C2410_TCON);
  97. }
  98. static int exynos4_pwm_set_next_event(unsigned long cycles,
  99. struct clock_event_device *evt)
  100. {
  101. exynos4_pwm_init(2, cycles);
  102. exynos4_pwm_start(2, 0);
  103. return 0;
  104. }
  105. static void exynos4_pwm_set_mode(enum clock_event_mode mode,
  106. struct clock_event_device *evt)
  107. {
  108. exynos4_pwm_stop(2);
  109. switch (mode) {
  110. case CLOCK_EVT_MODE_PERIODIC:
  111. exynos4_pwm_init(2, clock_count_per_tick);
  112. exynos4_pwm_start(2, 1);
  113. break;
  114. case CLOCK_EVT_MODE_ONESHOT:
  115. break;
  116. case CLOCK_EVT_MODE_UNUSED:
  117. case CLOCK_EVT_MODE_SHUTDOWN:
  118. case CLOCK_EVT_MODE_RESUME:
  119. break;
  120. }
  121. }
  122. static struct clock_event_device pwm_event_device = {
  123. .name = "pwm_timer2",
  124. .features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT,
  125. .rating = 200,
  126. .shift = 32,
  127. .set_next_event = exynos4_pwm_set_next_event,
  128. .set_mode = exynos4_pwm_set_mode,
  129. };
  130. irqreturn_t exynos4_clock_event_isr(int irq, void *dev_id)
  131. {
  132. struct clock_event_device *evt = &pwm_event_device;
  133. evt->event_handler(evt);
  134. return IRQ_HANDLED;
  135. }
  136. static struct irqaction exynos4_clock_event_irq = {
  137. .name = "pwm_timer2_irq",
  138. .flags = IRQF_DISABLED | IRQF_TIMER | IRQF_IRQPOLL,
  139. .handler = exynos4_clock_event_isr,
  140. };
  141. static void __init exynos4_clockevent_init(void)
  142. {
  143. unsigned long pclk;
  144. unsigned long clock_rate;
  145. struct clk *tscaler;
  146. pclk = clk_get_rate(timerclk);
  147. /* configure clock tick */
  148. tscaler = clk_get_parent(tdiv2);
  149. clk_set_rate(tscaler, pclk / 2);
  150. clk_set_rate(tdiv2, pclk / 2);
  151. clk_set_parent(tin2, tdiv2);
  152. clock_rate = clk_get_rate(tin2);
  153. clock_count_per_tick = clock_rate / HZ;
  154. pwm_event_device.mult =
  155. div_sc(clock_rate, NSEC_PER_SEC, pwm_event_device.shift);
  156. pwm_event_device.max_delta_ns =
  157. clockevent_delta2ns(-1, &pwm_event_device);
  158. pwm_event_device.min_delta_ns =
  159. clockevent_delta2ns(1, &pwm_event_device);
  160. pwm_event_device.cpumask = cpumask_of(0);
  161. clockevents_register_device(&pwm_event_device);
  162. setup_irq(IRQ_TIMER2, &exynos4_clock_event_irq);
  163. }
  164. static cycle_t exynos4_pwm4_read(struct clocksource *cs)
  165. {
  166. return (cycle_t) ~__raw_readl(S3C_TIMERREG(0x40));
  167. }
  168. #ifdef CONFIG_PM
  169. static void exynos4_pwm4_resume(struct clocksource *cs)
  170. {
  171. unsigned long pclk;
  172. pclk = clk_get_rate(timerclk);
  173. clk_set_rate(tdiv4, pclk / 2);
  174. clk_set_parent(tin4, tdiv4);
  175. exynos4_pwm_init(4, ~0);
  176. exynos4_pwm_start(4, 1);
  177. }
  178. #endif
  179. struct clocksource pwm_clocksource = {
  180. .name = "pwm_timer4",
  181. .rating = 250,
  182. .read = exynos4_pwm4_read,
  183. .mask = CLOCKSOURCE_MASK(32),
  184. .flags = CLOCK_SOURCE_IS_CONTINUOUS ,
  185. #ifdef CONFIG_PM
  186. .resume = exynos4_pwm4_resume,
  187. #endif
  188. };
  189. static void __init exynos4_clocksource_init(void)
  190. {
  191. unsigned long pclk;
  192. unsigned long clock_rate;
  193. pclk = clk_get_rate(timerclk);
  194. clk_set_rate(tdiv4, pclk / 2);
  195. clk_set_parent(tin4, tdiv4);
  196. clock_rate = clk_get_rate(tin4);
  197. exynos4_pwm_init(4, ~0);
  198. exynos4_pwm_start(4, 1);
  199. if (clocksource_register_hz(&pwm_clocksource, clock_rate))
  200. panic("%s: can't register clocksource\n", pwm_clocksource.name);
  201. }
  202. static void __init exynos4_timer_resources(void)
  203. {
  204. struct platform_device tmpdev;
  205. tmpdev.dev.bus = &platform_bus_type;
  206. timerclk = clk_get(NULL, "timers");
  207. if (IS_ERR(timerclk))
  208. panic("failed to get timers clock for system timer");
  209. clk_enable(timerclk);
  210. tmpdev.id = 2;
  211. tin2 = clk_get(&tmpdev.dev, "pwm-tin");
  212. if (IS_ERR(tin2))
  213. panic("failed to get pwm-tin2 clock for system timer");
  214. tdiv2 = clk_get(&tmpdev.dev, "pwm-tdiv");
  215. if (IS_ERR(tdiv2))
  216. panic("failed to get pwm-tdiv2 clock for system timer");
  217. clk_enable(tin2);
  218. tmpdev.id = 4;
  219. tin4 = clk_get(&tmpdev.dev, "pwm-tin");
  220. if (IS_ERR(tin4))
  221. panic("failed to get pwm-tin4 clock for system timer");
  222. tdiv4 = clk_get(&tmpdev.dev, "pwm-tdiv");
  223. if (IS_ERR(tdiv4))
  224. panic("failed to get pwm-tdiv4 clock for system timer");
  225. clk_enable(tin4);
  226. }
  227. static void __init exynos4_timer_init(void)
  228. {
  229. #ifdef CONFIG_LOCAL_TIMERS
  230. twd_base = S5P_VA_TWD;
  231. #endif
  232. exynos4_timer_resources();
  233. exynos4_clockevent_init();
  234. exynos4_clocksource_init();
  235. }
  236. struct sys_timer exynos4_timer = {
  237. .init = exynos4_timer_init,
  238. };