s5p-time.c 8.9 KB

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