samsung_pwm_timer.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  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 <clocksource/samsung_pwm.h>
  24. #include <asm/sched_clock.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. #define TCON_START(chan) (1 << (4 * (chan) + 0))
  39. #define TCON_MANUALUPDATE(chan) (1 << (4 * (chan) + 1))
  40. #define TCON_INVERT(chan) (1 << (4 * (chan) + 2))
  41. #define TCON_AUTORELOAD(chan) (1 << (4 * (chan) + 3))
  42. DEFINE_SPINLOCK(samsung_pwm_lock);
  43. EXPORT_SYMBOL(samsung_pwm_lock);
  44. struct samsung_timer_source {
  45. unsigned int event_id;
  46. unsigned int source_id;
  47. unsigned int tcnt_max;
  48. unsigned int tscaler_div;
  49. unsigned int tdiv;
  50. };
  51. static struct samsung_pwm *pwm;
  52. static struct clk *timerclk;
  53. static struct samsung_timer_source timer_source;
  54. static unsigned long clock_count_per_tick;
  55. static void samsung_timer_set_prescale(struct samsung_pwm *pwm,
  56. unsigned int channel, u16 prescale)
  57. {
  58. unsigned long flags;
  59. u8 shift = 0;
  60. u32 reg;
  61. if (channel >= 2)
  62. shift = TCFG0_PRESCALER1_SHIFT;
  63. spin_lock_irqsave(&samsung_pwm_lock, flags);
  64. reg = readl(pwm->base + REG_TCFG0);
  65. reg &= ~(TCFG0_PRESCALER_MASK << shift);
  66. reg |= (prescale - 1) << shift;
  67. writel(reg, pwm->base + REG_TCFG0);
  68. spin_unlock_irqrestore(&samsung_pwm_lock, flags);
  69. }
  70. static void samsung_timer_set_divisor(struct samsung_pwm *pwm,
  71. unsigned int channel, u8 divisor)
  72. {
  73. u8 shift = TCFG1_SHIFT(channel);
  74. unsigned long flags;
  75. u32 reg;
  76. u8 bits;
  77. bits = (fls(divisor) - 1) - pwm->variant.div_base;
  78. spin_lock_irqsave(&samsung_pwm_lock, flags);
  79. reg = readl(pwm->base + REG_TCFG1);
  80. reg &= ~(TCFG1_MUX_MASK << shift);
  81. reg |= bits << shift;
  82. writel(reg, pwm->base + REG_TCFG1);
  83. spin_unlock_irqrestore(&samsung_pwm_lock, flags);
  84. }
  85. static void samsung_time_stop(unsigned int channel)
  86. {
  87. unsigned long tcon;
  88. unsigned long flags;
  89. if (channel > 0)
  90. ++channel;
  91. spin_lock_irqsave(&samsung_pwm_lock, flags);
  92. tcon = __raw_readl(pwm->base + REG_TCON);
  93. tcon &= ~TCON_START(channel);
  94. __raw_writel(tcon, pwm->base + REG_TCON);
  95. spin_unlock_irqrestore(&samsung_pwm_lock, flags);
  96. }
  97. static void samsung_time_setup(unsigned int channel, unsigned long tcnt)
  98. {
  99. unsigned long tcon;
  100. unsigned long flags;
  101. unsigned int tcon_chan = channel;
  102. if (tcon_chan > 0)
  103. ++tcon_chan;
  104. spin_lock_irqsave(&samsung_pwm_lock, flags);
  105. tcon = __raw_readl(pwm->base + REG_TCON);
  106. tcnt--;
  107. tcon &= ~(TCON_START(tcon_chan) | TCON_AUTORELOAD(tcon_chan));
  108. tcon |= TCON_MANUALUPDATE(tcon_chan);
  109. __raw_writel(tcnt, pwm->base + REG_TCNTB(channel));
  110. __raw_writel(tcnt, pwm->base + REG_TCMPB(channel));
  111. __raw_writel(tcon, pwm->base + REG_TCON);
  112. spin_unlock_irqrestore(&samsung_pwm_lock, flags);
  113. }
  114. static void samsung_time_start(unsigned int channel, bool periodic)
  115. {
  116. unsigned long tcon;
  117. unsigned long flags;
  118. if (channel > 0)
  119. ++channel;
  120. spin_lock_irqsave(&samsung_pwm_lock, flags);
  121. tcon = __raw_readl(pwm->base + REG_TCON);
  122. tcon &= ~TCON_MANUALUPDATE(channel);
  123. tcon |= TCON_START(channel);
  124. if (periodic)
  125. tcon |= TCON_AUTORELOAD(channel);
  126. else
  127. tcon &= ~TCON_AUTORELOAD(channel);
  128. __raw_writel(tcon, pwm->base + REG_TCON);
  129. spin_unlock_irqrestore(&samsung_pwm_lock, flags);
  130. }
  131. static int samsung_set_next_event(unsigned long cycles,
  132. struct clock_event_device *evt)
  133. {
  134. samsung_time_setup(timer_source.event_id, cycles);
  135. samsung_time_start(timer_source.event_id, false);
  136. return 0;
  137. }
  138. static void samsung_timer_resume(void)
  139. {
  140. /* event timer restart */
  141. samsung_time_setup(timer_source.event_id, clock_count_per_tick);
  142. samsung_time_start(timer_source.event_id, true);
  143. /* source timer restart */
  144. samsung_time_setup(timer_source.source_id, timer_source.tcnt_max);
  145. samsung_time_start(timer_source.source_id, true);
  146. }
  147. static void samsung_set_mode(enum clock_event_mode mode,
  148. struct clock_event_device *evt)
  149. {
  150. samsung_time_stop(timer_source.event_id);
  151. switch (mode) {
  152. case CLOCK_EVT_MODE_PERIODIC:
  153. samsung_time_setup(timer_source.event_id, clock_count_per_tick);
  154. samsung_time_start(timer_source.event_id, true);
  155. break;
  156. case CLOCK_EVT_MODE_ONESHOT:
  157. break;
  158. case CLOCK_EVT_MODE_UNUSED:
  159. case CLOCK_EVT_MODE_SHUTDOWN:
  160. break;
  161. case CLOCK_EVT_MODE_RESUME:
  162. samsung_timer_resume();
  163. break;
  164. }
  165. }
  166. static struct clock_event_device time_event_device = {
  167. .name = "samsung_event_timer",
  168. .features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT,
  169. .rating = 200,
  170. .set_next_event = samsung_set_next_event,
  171. .set_mode = samsung_set_mode,
  172. };
  173. static irqreturn_t samsung_clock_event_isr(int irq, void *dev_id)
  174. {
  175. struct clock_event_device *evt = dev_id;
  176. if (pwm->variant.has_tint_cstat) {
  177. u32 mask = (1 << timer_source.event_id);
  178. writel(mask | (mask << 5), pwm->base + REG_TINT_CSTAT);
  179. }
  180. evt->event_handler(evt);
  181. return IRQ_HANDLED;
  182. }
  183. static struct irqaction samsung_clock_event_irq = {
  184. .name = "samsung_time_irq",
  185. .flags = IRQF_DISABLED | IRQF_TIMER | IRQF_IRQPOLL,
  186. .handler = samsung_clock_event_isr,
  187. .dev_id = &time_event_device,
  188. };
  189. static void __init samsung_clockevent_init(void)
  190. {
  191. unsigned long pclk;
  192. unsigned long clock_rate;
  193. unsigned int irq_number;
  194. pclk = clk_get_rate(timerclk);
  195. samsung_timer_set_prescale(pwm, timer_source.event_id,
  196. timer_source.tscaler_div);
  197. samsung_timer_set_divisor(pwm, timer_source.event_id,
  198. timer_source.tdiv);
  199. clock_rate = pclk / (timer_source.tscaler_div * timer_source.tdiv);
  200. clock_count_per_tick = clock_rate / HZ;
  201. time_event_device.cpumask = cpumask_of(0);
  202. clockevents_config_and_register(&time_event_device, clock_rate, 1, -1);
  203. irq_number = pwm->irq[timer_source.event_id];
  204. setup_irq(irq_number, &samsung_clock_event_irq);
  205. if (pwm->variant.has_tint_cstat) {
  206. u32 mask = (1 << timer_source.event_id);
  207. writel(mask | (mask << 5), pwm->base + REG_TINT_CSTAT);
  208. }
  209. }
  210. static void __iomem *samsung_timer_reg(void)
  211. {
  212. switch (timer_source.source_id) {
  213. case 0:
  214. case 1:
  215. case 2:
  216. case 3:
  217. return pwm->base + timer_source.source_id * 0x0c + 0x14;
  218. case 4:
  219. return pwm->base + 0x40;
  220. default:
  221. BUG();
  222. }
  223. }
  224. /*
  225. * Override the global weak sched_clock symbol with this
  226. * local implementation which uses the clocksource to get some
  227. * better resolution when scheduling the kernel. We accept that
  228. * this wraps around for now, since it is just a relative time
  229. * stamp. (Inspired by U300 implementation.)
  230. */
  231. static u32 notrace samsung_read_sched_clock(void)
  232. {
  233. void __iomem *reg = samsung_timer_reg();
  234. if (!reg)
  235. return 0;
  236. return ~__raw_readl(reg);
  237. }
  238. static void __init samsung_clocksource_init(void)
  239. {
  240. void __iomem *reg = samsung_timer_reg();
  241. unsigned long pclk;
  242. unsigned long clock_rate;
  243. int ret;
  244. pclk = clk_get_rate(timerclk);
  245. samsung_timer_set_prescale(pwm, timer_source.source_id,
  246. timer_source.tscaler_div);
  247. samsung_timer_set_divisor(pwm, timer_source.source_id,
  248. timer_source.tdiv);
  249. clock_rate = pclk / (timer_source.tscaler_div * timer_source.tdiv);
  250. samsung_time_setup(timer_source.source_id, timer_source.tcnt_max);
  251. samsung_time_start(timer_source.source_id, true);
  252. setup_sched_clock(samsung_read_sched_clock,
  253. pwm->variant.bits, clock_rate);
  254. ret = clocksource_mmio_init(reg, "samsung_clocksource_timer",
  255. clock_rate, 250, pwm->variant.bits,
  256. clocksource_mmio_readl_down);
  257. if (ret)
  258. panic("samsung_clocksource_timer: can't register clocksource\n");
  259. }
  260. static void __init samsung_timer_resources(void)
  261. {
  262. timerclk = clk_get(NULL, "timers");
  263. if (IS_ERR(timerclk))
  264. panic("failed to get timers clock for timer");
  265. clk_prepare_enable(timerclk);
  266. timer_source.tcnt_max = (1UL << pwm->variant.bits) - 1;
  267. if (pwm->variant.bits == 16) {
  268. timer_source.tscaler_div = 25;
  269. timer_source.tdiv = 2;
  270. } else {
  271. timer_source.tscaler_div = 2;
  272. timer_source.tdiv = 1;
  273. }
  274. }
  275. /*
  276. * PWM master driver
  277. */
  278. static void __init samsung_pwm_clocksource_init(void)
  279. {
  280. u8 mask;
  281. int channel;
  282. if (!pwm)
  283. panic("no pwm clocksource device found");
  284. mask = ~pwm->variant.output_mask & ((1 << SAMSUNG_PWM_NUM) - 1);
  285. channel = fls(mask) - 1;
  286. if (channel < 0)
  287. panic("failed to find PWM channel for clocksource");
  288. timer_source.source_id = channel;
  289. mask &= ~(1 << channel);
  290. channel = fls(mask) - 1;
  291. if (channel < 0)
  292. panic("failed to find PWM channel for clock event");
  293. timer_source.event_id = channel;
  294. samsung_timer_resources();
  295. samsung_clockevent_init();
  296. samsung_clocksource_init();
  297. }
  298. static void __init samsung_pwm_alloc(struct device_node *np,
  299. const struct samsung_pwm_variant *variant)
  300. {
  301. struct resource res;
  302. struct property *prop;
  303. const __be32 *cur;
  304. u32 val;
  305. int i;
  306. pwm = kzalloc(sizeof(*pwm), GFP_KERNEL);
  307. if (!pwm) {
  308. pr_err("%s: could not allocate PWM device struct\n", __func__);
  309. return;
  310. }
  311. memcpy(&pwm->variant, variant, sizeof(pwm->variant));
  312. for (i = 0; i < SAMSUNG_PWM_NUM; ++i)
  313. pwm->irq[i] = irq_of_parse_and_map(np, i);
  314. of_property_for_each_u32(np, "samsung,pwm-outputs", prop, cur, val) {
  315. if (val >= SAMSUNG_PWM_NUM) {
  316. pr_warning("%s: invalid channel index in samsung,pwm-outputs property\n",
  317. __func__);
  318. continue;
  319. }
  320. pwm->variant.output_mask |= 1 << val;
  321. }
  322. of_address_to_resource(np, 0, &res);
  323. if (!request_mem_region(res.start,
  324. resource_size(&res), "samsung-pwm")) {
  325. pr_err("%s: failed to request IO mem region\n", __func__);
  326. return;
  327. }
  328. pwm->base = ioremap(res.start, resource_size(&res));
  329. if (!pwm->base) {
  330. pr_err("%s: failed to map PWM registers\n", __func__);
  331. release_mem_region(res.start, resource_size(&res));
  332. return;
  333. }
  334. samsung_pwm_clocksource_init();
  335. }
  336. static const struct samsung_pwm_variant s3c24xx_variant = {
  337. .bits = 16,
  338. .div_base = 1,
  339. .has_tint_cstat = false,
  340. .tclk_mask = (1 << 4),
  341. };
  342. static void __init s3c2410_pwm_clocksource_init(struct device_node *np)
  343. {
  344. samsung_pwm_alloc(np, &s3c24xx_variant);
  345. }
  346. CLOCKSOURCE_OF_DECLARE(s3c2410_pwm, "samsung,s3c2410-pwm", s3c2410_pwm_clocksource_init);
  347. static const struct samsung_pwm_variant s3c64xx_variant = {
  348. .bits = 32,
  349. .div_base = 0,
  350. .has_tint_cstat = true,
  351. .tclk_mask = (1 << 7) | (1 << 6) | (1 << 5),
  352. };
  353. static void __init s3c64xx_pwm_clocksource_init(struct device_node *np)
  354. {
  355. samsung_pwm_alloc(np, &s3c64xx_variant);
  356. }
  357. CLOCKSOURCE_OF_DECLARE(s3c6400_pwm, "samsung,s3c6400-pwm", s3c64xx_pwm_clocksource_init);
  358. static const struct samsung_pwm_variant s5p64x0_variant = {
  359. .bits = 32,
  360. .div_base = 0,
  361. .has_tint_cstat = true,
  362. .tclk_mask = 0,
  363. };
  364. static void __init s5p64x0_pwm_clocksource_init(struct device_node *np)
  365. {
  366. samsung_pwm_alloc(np, &s5p64x0_variant);
  367. }
  368. CLOCKSOURCE_OF_DECLARE(s5p6440_pwm, "samsung,s5p6440-pwm", s5p64x0_pwm_clocksource_init);
  369. static const struct samsung_pwm_variant s5p_variant = {
  370. .bits = 32,
  371. .div_base = 0,
  372. .has_tint_cstat = true,
  373. .tclk_mask = (1 << 5),
  374. };
  375. static void __init s5p_pwm_clocksource_init(struct device_node *np)
  376. {
  377. samsung_pwm_alloc(np, &s5p_variant);
  378. }
  379. CLOCKSOURCE_OF_DECLARE(s5pc100_pwm, "samsung,s5pc100-pwm", s5p_pwm_clocksource_init);