samsung_pwm_timer.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  1. /*
  2. * Copyright (c) 2011 Samsung Electronics Co., Ltd.
  3. * http://www.samsung.com/
  4. *
  5. * samsung - Common hr-timer support (s3c and s5p)
  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/list.h>
  17. #include <linux/module.h>
  18. #include <linux/of.h>
  19. #include <linux/of_address.h>
  20. #include <linux/of_irq.h>
  21. #include <linux/platform_device.h>
  22. #include <linux/slab.h>
  23. #include <linux/sched_clock.h>
  24. #include <clocksource/samsung_pwm.h>
  25. /*
  26. * Clocksource driver
  27. */
  28. #define REG_TCFG0 0x00
  29. #define REG_TCFG1 0x04
  30. #define REG_TCON 0x08
  31. #define REG_TINT_CSTAT 0x44
  32. #define REG_TCNTB(chan) (0x0c + 12 * (chan))
  33. #define REG_TCMPB(chan) (0x10 + 12 * (chan))
  34. #define TCFG0_PRESCALER_MASK 0xff
  35. #define TCFG0_PRESCALER1_SHIFT 8
  36. #define TCFG1_SHIFT(x) ((x) * 4)
  37. #define TCFG1_MUX_MASK 0xf
  38. /*
  39. * Each channel occupies 4 bits in TCON register, but there is a gap of 4
  40. * bits (one channel) after channel 0, so channels have different numbering
  41. * when accessing TCON register.
  42. *
  43. * In addition, the location of autoreload bit for channel 4 (TCON channel 5)
  44. * in its set of bits is 2 as opposed to 3 for other channels.
  45. */
  46. #define TCON_START(chan) (1 << (4 * (chan) + 0))
  47. #define TCON_MANUALUPDATE(chan) (1 << (4 * (chan) + 1))
  48. #define TCON_INVERT(chan) (1 << (4 * (chan) + 2))
  49. #define _TCON_AUTORELOAD(chan) (1 << (4 * (chan) + 3))
  50. #define _TCON_AUTORELOAD4(chan) (1 << (4 * (chan) + 2))
  51. #define TCON_AUTORELOAD(chan) \
  52. ((chan < 5) ? _TCON_AUTORELOAD(chan) : _TCON_AUTORELOAD4(chan))
  53. DEFINE_SPINLOCK(samsung_pwm_lock);
  54. EXPORT_SYMBOL(samsung_pwm_lock);
  55. struct samsung_pwm_clocksource {
  56. void __iomem *base;
  57. void __iomem *source_reg;
  58. unsigned int irq[SAMSUNG_PWM_NUM];
  59. struct samsung_pwm_variant variant;
  60. struct clk *timerclk;
  61. unsigned int event_id;
  62. unsigned int source_id;
  63. unsigned int tcnt_max;
  64. unsigned int tscaler_div;
  65. unsigned int tdiv;
  66. unsigned long clock_count_per_tick;
  67. };
  68. static struct samsung_pwm_clocksource pwm;
  69. static void samsung_timer_set_prescale(unsigned int channel, u16 prescale)
  70. {
  71. unsigned long flags;
  72. u8 shift = 0;
  73. u32 reg;
  74. if (channel >= 2)
  75. shift = TCFG0_PRESCALER1_SHIFT;
  76. spin_lock_irqsave(&samsung_pwm_lock, flags);
  77. reg = readl(pwm.base + REG_TCFG0);
  78. reg &= ~(TCFG0_PRESCALER_MASK << shift);
  79. reg |= (prescale - 1) << shift;
  80. writel(reg, pwm.base + REG_TCFG0);
  81. spin_unlock_irqrestore(&samsung_pwm_lock, flags);
  82. }
  83. static void samsung_timer_set_divisor(unsigned int channel, u8 divisor)
  84. {
  85. u8 shift = TCFG1_SHIFT(channel);
  86. unsigned long flags;
  87. u32 reg;
  88. u8 bits;
  89. bits = (fls(divisor) - 1) - pwm.variant.div_base;
  90. spin_lock_irqsave(&samsung_pwm_lock, flags);
  91. reg = readl(pwm.base + REG_TCFG1);
  92. reg &= ~(TCFG1_MUX_MASK << shift);
  93. reg |= bits << shift;
  94. writel(reg, pwm.base + REG_TCFG1);
  95. spin_unlock_irqrestore(&samsung_pwm_lock, flags);
  96. }
  97. static void samsung_time_stop(unsigned int channel)
  98. {
  99. unsigned long tcon;
  100. unsigned long flags;
  101. if (channel > 0)
  102. ++channel;
  103. spin_lock_irqsave(&samsung_pwm_lock, flags);
  104. tcon = readl_relaxed(pwm.base + REG_TCON);
  105. tcon &= ~TCON_START(channel);
  106. writel_relaxed(tcon, pwm.base + REG_TCON);
  107. spin_unlock_irqrestore(&samsung_pwm_lock, flags);
  108. }
  109. static void samsung_time_setup(unsigned int channel, unsigned long tcnt)
  110. {
  111. unsigned long tcon;
  112. unsigned long flags;
  113. unsigned int tcon_chan = channel;
  114. if (tcon_chan > 0)
  115. ++tcon_chan;
  116. spin_lock_irqsave(&samsung_pwm_lock, flags);
  117. tcon = readl_relaxed(pwm.base + REG_TCON);
  118. tcon &= ~(TCON_START(tcon_chan) | TCON_AUTORELOAD(tcon_chan));
  119. tcon |= TCON_MANUALUPDATE(tcon_chan);
  120. writel_relaxed(tcnt, pwm.base + REG_TCNTB(channel));
  121. writel_relaxed(tcnt, pwm.base + REG_TCMPB(channel));
  122. writel_relaxed(tcon, pwm.base + REG_TCON);
  123. spin_unlock_irqrestore(&samsung_pwm_lock, flags);
  124. }
  125. static void samsung_time_start(unsigned int channel, bool periodic)
  126. {
  127. unsigned long tcon;
  128. unsigned long flags;
  129. if (channel > 0)
  130. ++channel;
  131. spin_lock_irqsave(&samsung_pwm_lock, flags);
  132. tcon = readl_relaxed(pwm.base + REG_TCON);
  133. tcon &= ~TCON_MANUALUPDATE(channel);
  134. tcon |= TCON_START(channel);
  135. if (periodic)
  136. tcon |= TCON_AUTORELOAD(channel);
  137. else
  138. tcon &= ~TCON_AUTORELOAD(channel);
  139. writel_relaxed(tcon, pwm.base + REG_TCON);
  140. spin_unlock_irqrestore(&samsung_pwm_lock, flags);
  141. }
  142. static int samsung_set_next_event(unsigned long cycles,
  143. struct clock_event_device *evt)
  144. {
  145. /*
  146. * This check is needed to account for internal rounding
  147. * errors inside clockevents core, which might result in
  148. * passing cycles = 0, which in turn would not generate any
  149. * timer interrupt and hang the system.
  150. *
  151. * Another solution would be to set up the clockevent device
  152. * with min_delta = 2, but this would unnecessarily increase
  153. * the minimum sleep period.
  154. */
  155. if (!cycles)
  156. cycles = 1;
  157. samsung_time_setup(pwm.event_id, cycles);
  158. samsung_time_start(pwm.event_id, false);
  159. return 0;
  160. }
  161. static int samsung_shutdown(struct clock_event_device *evt)
  162. {
  163. samsung_time_stop(pwm.event_id);
  164. return 0;
  165. }
  166. static int samsung_set_periodic(struct clock_event_device *evt)
  167. {
  168. samsung_time_stop(pwm.event_id);
  169. samsung_time_setup(pwm.event_id, pwm.clock_count_per_tick - 1);
  170. samsung_time_start(pwm.event_id, true);
  171. return 0;
  172. }
  173. static void samsung_clockevent_resume(struct clock_event_device *cev)
  174. {
  175. samsung_timer_set_prescale(pwm.event_id, pwm.tscaler_div);
  176. samsung_timer_set_divisor(pwm.event_id, pwm.tdiv);
  177. if (pwm.variant.has_tint_cstat) {
  178. u32 mask = (1 << pwm.event_id);
  179. writel(mask | (mask << 5), pwm.base + REG_TINT_CSTAT);
  180. }
  181. }
  182. static struct clock_event_device time_event_device = {
  183. .name = "samsung_event_timer",
  184. .features = CLOCK_EVT_FEAT_PERIODIC |
  185. CLOCK_EVT_FEAT_ONESHOT,
  186. .rating = 200,
  187. .set_next_event = samsung_set_next_event,
  188. .set_state_shutdown = samsung_shutdown,
  189. .set_state_periodic = samsung_set_periodic,
  190. .set_state_oneshot = samsung_shutdown,
  191. .tick_resume = samsung_shutdown,
  192. .resume = samsung_clockevent_resume,
  193. };
  194. static irqreturn_t samsung_clock_event_isr(int irq, void *dev_id)
  195. {
  196. struct clock_event_device *evt = dev_id;
  197. if (pwm.variant.has_tint_cstat) {
  198. u32 mask = (1 << pwm.event_id);
  199. writel(mask | (mask << 5), pwm.base + REG_TINT_CSTAT);
  200. }
  201. evt->event_handler(evt);
  202. return IRQ_HANDLED;
  203. }
  204. static struct irqaction samsung_clock_event_irq = {
  205. .name = "samsung_time_irq",
  206. .flags = IRQF_TIMER | IRQF_IRQPOLL,
  207. .handler = samsung_clock_event_isr,
  208. .dev_id = &time_event_device,
  209. };
  210. static void __init samsung_clockevent_init(void)
  211. {
  212. unsigned long pclk;
  213. unsigned long clock_rate;
  214. unsigned int irq_number;
  215. pclk = clk_get_rate(pwm.timerclk);
  216. samsung_timer_set_prescale(pwm.event_id, pwm.tscaler_div);
  217. samsung_timer_set_divisor(pwm.event_id, pwm.tdiv);
  218. clock_rate = pclk / (pwm.tscaler_div * pwm.tdiv);
  219. pwm.clock_count_per_tick = clock_rate / HZ;
  220. time_event_device.cpumask = cpumask_of(0);
  221. clockevents_config_and_register(&time_event_device,
  222. clock_rate, 1, pwm.tcnt_max);
  223. irq_number = pwm.irq[pwm.event_id];
  224. setup_irq(irq_number, &samsung_clock_event_irq);
  225. if (pwm.variant.has_tint_cstat) {
  226. u32 mask = (1 << pwm.event_id);
  227. writel(mask | (mask << 5), pwm.base + REG_TINT_CSTAT);
  228. }
  229. }
  230. static void samsung_clocksource_suspend(struct clocksource *cs)
  231. {
  232. samsung_time_stop(pwm.source_id);
  233. }
  234. static void samsung_clocksource_resume(struct clocksource *cs)
  235. {
  236. samsung_timer_set_prescale(pwm.source_id, pwm.tscaler_div);
  237. samsung_timer_set_divisor(pwm.source_id, pwm.tdiv);
  238. samsung_time_setup(pwm.source_id, pwm.tcnt_max);
  239. samsung_time_start(pwm.source_id, true);
  240. }
  241. static cycle_t notrace samsung_clocksource_read(struct clocksource *c)
  242. {
  243. return ~readl_relaxed(pwm.source_reg);
  244. }
  245. static struct clocksource samsung_clocksource = {
  246. .name = "samsung_clocksource_timer",
  247. .rating = 250,
  248. .read = samsung_clocksource_read,
  249. .suspend = samsung_clocksource_suspend,
  250. .resume = samsung_clocksource_resume,
  251. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  252. };
  253. /*
  254. * Override the global weak sched_clock symbol with this
  255. * local implementation which uses the clocksource to get some
  256. * better resolution when scheduling the kernel. We accept that
  257. * this wraps around for now, since it is just a relative time
  258. * stamp. (Inspired by U300 implementation.)
  259. */
  260. static u64 notrace samsung_read_sched_clock(void)
  261. {
  262. return samsung_clocksource_read(NULL);
  263. }
  264. static int __init samsung_clocksource_init(void)
  265. {
  266. unsigned long pclk;
  267. unsigned long clock_rate;
  268. pclk = clk_get_rate(pwm.timerclk);
  269. samsung_timer_set_prescale(pwm.source_id, pwm.tscaler_div);
  270. samsung_timer_set_divisor(pwm.source_id, pwm.tdiv);
  271. clock_rate = pclk / (pwm.tscaler_div * pwm.tdiv);
  272. samsung_time_setup(pwm.source_id, pwm.tcnt_max);
  273. samsung_time_start(pwm.source_id, true);
  274. if (pwm.source_id == 4)
  275. pwm.source_reg = pwm.base + 0x40;
  276. else
  277. pwm.source_reg = pwm.base + pwm.source_id * 0x0c + 0x14;
  278. sched_clock_register(samsung_read_sched_clock,
  279. pwm.variant.bits, clock_rate);
  280. samsung_clocksource.mask = CLOCKSOURCE_MASK(pwm.variant.bits);
  281. return clocksource_register_hz(&samsung_clocksource, clock_rate);
  282. }
  283. static void __init samsung_timer_resources(void)
  284. {
  285. clk_prepare_enable(pwm.timerclk);
  286. pwm.tcnt_max = (1UL << pwm.variant.bits) - 1;
  287. if (pwm.variant.bits == 16) {
  288. pwm.tscaler_div = 25;
  289. pwm.tdiv = 2;
  290. } else {
  291. pwm.tscaler_div = 2;
  292. pwm.tdiv = 1;
  293. }
  294. }
  295. /*
  296. * PWM master driver
  297. */
  298. static int __init _samsung_pwm_clocksource_init(void)
  299. {
  300. u8 mask;
  301. int channel;
  302. mask = ~pwm.variant.output_mask & ((1 << SAMSUNG_PWM_NUM) - 1);
  303. channel = fls(mask) - 1;
  304. if (channel < 0) {
  305. pr_crit("failed to find PWM channel for clocksource");
  306. return -EINVAL;
  307. }
  308. pwm.source_id = channel;
  309. mask &= ~(1 << channel);
  310. channel = fls(mask) - 1;
  311. if (channel < 0) {
  312. pr_crit("failed to find PWM channel for clock event");
  313. return -EINVAL;
  314. }
  315. pwm.event_id = channel;
  316. samsung_timer_resources();
  317. samsung_clockevent_init();
  318. return samsung_clocksource_init();
  319. }
  320. void __init samsung_pwm_clocksource_init(void __iomem *base,
  321. unsigned int *irqs, struct samsung_pwm_variant *variant)
  322. {
  323. pwm.base = base;
  324. memcpy(&pwm.variant, variant, sizeof(pwm.variant));
  325. memcpy(pwm.irq, irqs, SAMSUNG_PWM_NUM * sizeof(*irqs));
  326. pwm.timerclk = clk_get(NULL, "timers");
  327. if (IS_ERR(pwm.timerclk))
  328. panic("failed to get timers clock for timer");
  329. _samsung_pwm_clocksource_init();
  330. }
  331. #ifdef CONFIG_CLKSRC_OF
  332. static int __init samsung_pwm_alloc(struct device_node *np,
  333. const struct samsung_pwm_variant *variant)
  334. {
  335. struct property *prop;
  336. const __be32 *cur;
  337. u32 val;
  338. int i;
  339. memcpy(&pwm.variant, variant, sizeof(pwm.variant));
  340. for (i = 0; i < SAMSUNG_PWM_NUM; ++i)
  341. pwm.irq[i] = irq_of_parse_and_map(np, i);
  342. of_property_for_each_u32(np, "samsung,pwm-outputs", prop, cur, val) {
  343. if (val >= SAMSUNG_PWM_NUM) {
  344. pr_warning("%s: invalid channel index in samsung,pwm-outputs property\n",
  345. __func__);
  346. continue;
  347. }
  348. pwm.variant.output_mask |= 1 << val;
  349. }
  350. pwm.base = of_iomap(np, 0);
  351. if (!pwm.base) {
  352. pr_err("%s: failed to map PWM registers\n", __func__);
  353. return -ENXIO;
  354. }
  355. pwm.timerclk = of_clk_get_by_name(np, "timers");
  356. if (IS_ERR(pwm.timerclk)) {
  357. pr_crit("failed to get timers clock for timer");
  358. return PTR_ERR(pwm.timerclk);
  359. }
  360. return _samsung_pwm_clocksource_init();
  361. }
  362. static const struct samsung_pwm_variant s3c24xx_variant = {
  363. .bits = 16,
  364. .div_base = 1,
  365. .has_tint_cstat = false,
  366. .tclk_mask = (1 << 4),
  367. };
  368. static int __init s3c2410_pwm_clocksource_init(struct device_node *np)
  369. {
  370. return samsung_pwm_alloc(np, &s3c24xx_variant);
  371. }
  372. CLOCKSOURCE_OF_DECLARE(s3c2410_pwm, "samsung,s3c2410-pwm", s3c2410_pwm_clocksource_init);
  373. static const struct samsung_pwm_variant s3c64xx_variant = {
  374. .bits = 32,
  375. .div_base = 0,
  376. .has_tint_cstat = true,
  377. .tclk_mask = (1 << 7) | (1 << 6) | (1 << 5),
  378. };
  379. static int __init s3c64xx_pwm_clocksource_init(struct device_node *np)
  380. {
  381. return samsung_pwm_alloc(np, &s3c64xx_variant);
  382. }
  383. CLOCKSOURCE_OF_DECLARE(s3c6400_pwm, "samsung,s3c6400-pwm", s3c64xx_pwm_clocksource_init);
  384. static const struct samsung_pwm_variant s5p64x0_variant = {
  385. .bits = 32,
  386. .div_base = 0,
  387. .has_tint_cstat = true,
  388. .tclk_mask = 0,
  389. };
  390. static int __init s5p64x0_pwm_clocksource_init(struct device_node *np)
  391. {
  392. return samsung_pwm_alloc(np, &s5p64x0_variant);
  393. }
  394. CLOCKSOURCE_OF_DECLARE(s5p6440_pwm, "samsung,s5p6440-pwm", s5p64x0_pwm_clocksource_init);
  395. static const struct samsung_pwm_variant s5p_variant = {
  396. .bits = 32,
  397. .div_base = 0,
  398. .has_tint_cstat = true,
  399. .tclk_mask = (1 << 5),
  400. };
  401. static int __init s5p_pwm_clocksource_init(struct device_node *np)
  402. {
  403. return samsung_pwm_alloc(np, &s5p_variant);
  404. }
  405. CLOCKSOURCE_OF_DECLARE(s5pc100_pwm, "samsung,s5pc100-pwm", s5p_pwm_clocksource_init);
  406. #endif