s5p-time.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. /* linux/arch/arm/plat-s5p/s5p-time.c
  2. *
  3. * Copyright (c) 2011 Samsung Electronics Co., Ltd.
  4. * http://www.samsung.com/
  5. *
  6. * S5P - Common hr-timer support
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. */
  12. #include <linux/interrupt.h>
  13. #include <linux/irq.h>
  14. #include <linux/err.h>
  15. #include <linux/clk.h>
  16. #include <linux/clockchips.h>
  17. #include <linux/platform_device.h>
  18. #include <asm/smp_twd.h>
  19. #include <asm/mach/time.h>
  20. #include <asm/mach/arch.h>
  21. #include <asm/mach/map.h>
  22. #include <asm/sched_clock.h>
  23. #include <mach/map.h>
  24. #include <plat/devs.h>
  25. #include <plat/regs-timer.h>
  26. #include <plat/s5p-time.h>
  27. static struct clk *tin_event;
  28. static struct clk *tin_source;
  29. static struct clk *tdiv_event;
  30. static struct clk *tdiv_source;
  31. static struct clk *timerclk;
  32. static struct s5p_timer_source timer_source;
  33. static unsigned long clock_count_per_tick;
  34. static void s5p_timer_resume(void);
  35. static void s5p_time_stop(enum s5p_timer_mode mode)
  36. {
  37. unsigned long tcon;
  38. tcon = __raw_readl(S3C2410_TCON);
  39. switch (mode) {
  40. case S5P_PWM0:
  41. tcon &= ~S3C2410_TCON_T0START;
  42. break;
  43. case S5P_PWM1:
  44. tcon &= ~S3C2410_TCON_T1START;
  45. break;
  46. case S5P_PWM2:
  47. tcon &= ~S3C2410_TCON_T2START;
  48. break;
  49. case S5P_PWM3:
  50. tcon &= ~S3C2410_TCON_T3START;
  51. break;
  52. case S5P_PWM4:
  53. tcon &= ~S3C2410_TCON_T4START;
  54. break;
  55. default:
  56. printk(KERN_ERR "Invalid Timer %d\n", mode);
  57. break;
  58. }
  59. __raw_writel(tcon, S3C2410_TCON);
  60. }
  61. static void s5p_time_setup(enum s5p_timer_mode mode, unsigned long tcnt)
  62. {
  63. unsigned long tcon;
  64. tcon = __raw_readl(S3C2410_TCON);
  65. tcnt--;
  66. switch (mode) {
  67. case S5P_PWM0:
  68. tcon &= ~(0x0f << 0);
  69. tcon |= S3C2410_TCON_T0MANUALUPD;
  70. break;
  71. case S5P_PWM1:
  72. tcon &= ~(0x0f << 8);
  73. tcon |= S3C2410_TCON_T1MANUALUPD;
  74. break;
  75. case S5P_PWM2:
  76. tcon &= ~(0x0f << 12);
  77. tcon |= S3C2410_TCON_T2MANUALUPD;
  78. break;
  79. case S5P_PWM3:
  80. tcon &= ~(0x0f << 16);
  81. tcon |= S3C2410_TCON_T3MANUALUPD;
  82. break;
  83. case S5P_PWM4:
  84. tcon &= ~(0x07 << 20);
  85. tcon |= S3C2410_TCON_T4MANUALUPD;
  86. break;
  87. default:
  88. printk(KERN_ERR "Invalid Timer %d\n", mode);
  89. break;
  90. }
  91. __raw_writel(tcnt, S3C2410_TCNTB(mode));
  92. __raw_writel(tcnt, S3C2410_TCMPB(mode));
  93. __raw_writel(tcon, S3C2410_TCON);
  94. }
  95. static void s5p_time_start(enum s5p_timer_mode mode, bool periodic)
  96. {
  97. unsigned long tcon;
  98. tcon = __raw_readl(S3C2410_TCON);
  99. switch (mode) {
  100. case S5P_PWM0:
  101. tcon |= S3C2410_TCON_T0START;
  102. tcon &= ~S3C2410_TCON_T0MANUALUPD;
  103. if (periodic)
  104. tcon |= S3C2410_TCON_T0RELOAD;
  105. else
  106. tcon &= ~S3C2410_TCON_T0RELOAD;
  107. break;
  108. case S5P_PWM1:
  109. tcon |= S3C2410_TCON_T1START;
  110. tcon &= ~S3C2410_TCON_T1MANUALUPD;
  111. if (periodic)
  112. tcon |= S3C2410_TCON_T1RELOAD;
  113. else
  114. tcon &= ~S3C2410_TCON_T1RELOAD;
  115. break;
  116. case S5P_PWM2:
  117. tcon |= S3C2410_TCON_T2START;
  118. tcon &= ~S3C2410_TCON_T2MANUALUPD;
  119. if (periodic)
  120. tcon |= S3C2410_TCON_T2RELOAD;
  121. else
  122. tcon &= ~S3C2410_TCON_T2RELOAD;
  123. break;
  124. case S5P_PWM3:
  125. tcon |= S3C2410_TCON_T3START;
  126. tcon &= ~S3C2410_TCON_T3MANUALUPD;
  127. if (periodic)
  128. tcon |= S3C2410_TCON_T3RELOAD;
  129. else
  130. tcon &= ~S3C2410_TCON_T3RELOAD;
  131. break;
  132. case S5P_PWM4:
  133. tcon |= S3C2410_TCON_T4START;
  134. tcon &= ~S3C2410_TCON_T4MANUALUPD;
  135. if (periodic)
  136. tcon |= S3C2410_TCON_T4RELOAD;
  137. else
  138. tcon &= ~S3C2410_TCON_T4RELOAD;
  139. break;
  140. default:
  141. printk(KERN_ERR "Invalid Timer %d\n", mode);
  142. break;
  143. }
  144. __raw_writel(tcon, S3C2410_TCON);
  145. }
  146. static int s5p_set_next_event(unsigned long cycles,
  147. struct clock_event_device *evt)
  148. {
  149. s5p_time_setup(timer_source.event_id, cycles);
  150. s5p_time_start(timer_source.event_id, NON_PERIODIC);
  151. return 0;
  152. }
  153. static void s5p_set_mode(enum clock_event_mode mode,
  154. struct clock_event_device *evt)
  155. {
  156. s5p_time_stop(timer_source.event_id);
  157. switch (mode) {
  158. case CLOCK_EVT_MODE_PERIODIC:
  159. s5p_time_setup(timer_source.event_id, clock_count_per_tick);
  160. s5p_time_start(timer_source.event_id, PERIODIC);
  161. break;
  162. case CLOCK_EVT_MODE_ONESHOT:
  163. break;
  164. case CLOCK_EVT_MODE_UNUSED:
  165. case CLOCK_EVT_MODE_SHUTDOWN:
  166. break;
  167. case CLOCK_EVT_MODE_RESUME:
  168. s5p_timer_resume();
  169. break;
  170. }
  171. }
  172. static void s5p_timer_resume(void)
  173. {
  174. /* event timer restart */
  175. s5p_time_setup(timer_source.event_id, clock_count_per_tick);
  176. s5p_time_start(timer_source.event_id, PERIODIC);
  177. /* source timer restart */
  178. s5p_time_setup(timer_source.source_id, TCNT_MAX);
  179. s5p_time_start(timer_source.source_id, PERIODIC);
  180. }
  181. void __init s5p_set_timer_source(enum s5p_timer_mode event,
  182. enum s5p_timer_mode source)
  183. {
  184. s3c_device_timer[event].dev.bus = &platform_bus_type;
  185. s3c_device_timer[source].dev.bus = &platform_bus_type;
  186. timer_source.event_id = event;
  187. timer_source.source_id = source;
  188. }
  189. static struct clock_event_device time_event_device = {
  190. .name = "s5p_event_timer",
  191. .features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT,
  192. .rating = 200,
  193. .set_next_event = s5p_set_next_event,
  194. .set_mode = s5p_set_mode,
  195. };
  196. static irqreturn_t s5p_clock_event_isr(int irq, void *dev_id)
  197. {
  198. struct clock_event_device *evt = dev_id;
  199. evt->event_handler(evt);
  200. return IRQ_HANDLED;
  201. }
  202. static struct irqaction s5p_clock_event_irq = {
  203. .name = "s5p_time_irq",
  204. .flags = IRQF_DISABLED | IRQF_TIMER | IRQF_IRQPOLL,
  205. .handler = s5p_clock_event_isr,
  206. .dev_id = &time_event_device,
  207. };
  208. static void __init s5p_clockevent_init(void)
  209. {
  210. unsigned long pclk;
  211. unsigned long clock_rate;
  212. unsigned int irq_number;
  213. struct clk *tscaler;
  214. pclk = clk_get_rate(timerclk);
  215. tscaler = clk_get_parent(tdiv_event);
  216. clk_set_rate(tscaler, pclk / 2);
  217. clk_set_rate(tdiv_event, pclk / 2);
  218. clk_set_parent(tin_event, tdiv_event);
  219. clock_rate = clk_get_rate(tin_event);
  220. clock_count_per_tick = clock_rate / HZ;
  221. clockevents_calc_mult_shift(&time_event_device,
  222. clock_rate, S5PTIMER_MIN_RANGE);
  223. time_event_device.max_delta_ns =
  224. clockevent_delta2ns(-1, &time_event_device);
  225. time_event_device.min_delta_ns =
  226. clockevent_delta2ns(1, &time_event_device);
  227. time_event_device.cpumask = cpumask_of(0);
  228. clockevents_register_device(&time_event_device);
  229. irq_number = timer_source.event_id + IRQ_TIMER0;
  230. setup_irq(irq_number, &s5p_clock_event_irq);
  231. }
  232. static void __iomem *s5p_timer_reg(void)
  233. {
  234. unsigned long offset = 0;
  235. switch (timer_source.source_id) {
  236. case S5P_PWM0:
  237. case S5P_PWM1:
  238. case S5P_PWM2:
  239. case S5P_PWM3:
  240. offset = (timer_source.source_id * 0x0c) + 0x14;
  241. break;
  242. case S5P_PWM4:
  243. offset = 0x40;
  244. break;
  245. default:
  246. printk(KERN_ERR "Invalid Timer %d\n", timer_source.source_id);
  247. return NULL;
  248. }
  249. return S3C_TIMERREG(offset);
  250. }
  251. /*
  252. * Override the global weak sched_clock symbol with this
  253. * local implementation which uses the clocksource to get some
  254. * better resolution when scheduling the kernel. We accept that
  255. * this wraps around for now, since it is just a relative time
  256. * stamp. (Inspired by U300 implementation.)
  257. */
  258. static u32 notrace s5p_read_sched_clock(void)
  259. {
  260. void __iomem *reg = s5p_timer_reg();
  261. if (!reg)
  262. return 0;
  263. return ~__raw_readl(reg);
  264. }
  265. static void __init s5p_clocksource_init(void)
  266. {
  267. unsigned long pclk;
  268. unsigned long clock_rate;
  269. pclk = clk_get_rate(timerclk);
  270. clk_set_rate(tdiv_source, pclk / 2);
  271. clk_set_parent(tin_source, tdiv_source);
  272. clock_rate = clk_get_rate(tin_source);
  273. s5p_time_setup(timer_source.source_id, TCNT_MAX);
  274. s5p_time_start(timer_source.source_id, PERIODIC);
  275. setup_sched_clock(s5p_read_sched_clock, 32, clock_rate);
  276. if (clocksource_mmio_init(s5p_timer_reg(), "s5p_clocksource_timer",
  277. clock_rate, 250, 32, clocksource_mmio_readl_down))
  278. panic("s5p_clocksource_timer: can't register clocksource\n");
  279. }
  280. static void __init s5p_timer_resources(void)
  281. {
  282. unsigned long event_id = timer_source.event_id;
  283. unsigned long source_id = timer_source.source_id;
  284. char devname[15];
  285. timerclk = clk_get(NULL, "timers");
  286. if (IS_ERR(timerclk))
  287. panic("failed to get timers clock for timer");
  288. clk_enable(timerclk);
  289. sprintf(devname, "s3c24xx-pwm.%lu", event_id);
  290. s3c_device_timer[event_id].id = event_id;
  291. s3c_device_timer[event_id].dev.init_name = devname;
  292. tin_event = clk_get(&s3c_device_timer[event_id].dev, "pwm-tin");
  293. if (IS_ERR(tin_event))
  294. panic("failed to get pwm-tin clock for event timer");
  295. tdiv_event = clk_get(&s3c_device_timer[event_id].dev, "pwm-tdiv");
  296. if (IS_ERR(tdiv_event))
  297. panic("failed to get pwm-tdiv clock for event timer");
  298. clk_enable(tin_event);
  299. sprintf(devname, "s3c24xx-pwm.%lu", source_id);
  300. s3c_device_timer[source_id].id = source_id;
  301. s3c_device_timer[source_id].dev.init_name = devname;
  302. tin_source = clk_get(&s3c_device_timer[source_id].dev, "pwm-tin");
  303. if (IS_ERR(tin_source))
  304. panic("failed to get pwm-tin clock for source timer");
  305. tdiv_source = clk_get(&s3c_device_timer[source_id].dev, "pwm-tdiv");
  306. if (IS_ERR(tdiv_source))
  307. panic("failed to get pwm-tdiv clock for source timer");
  308. clk_enable(tin_source);
  309. }
  310. static void __init s5p_timer_init(void)
  311. {
  312. s5p_timer_resources();
  313. s5p_clockevent_init();
  314. s5p_clocksource_init();
  315. }
  316. struct sys_timer s5p_timer = {
  317. .init = s5p_timer_init,
  318. };