samsung_pwm_timer.c 13 KB

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