sh_mtu2.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * SuperH Timer Support - MTU2
  4. *
  5. * Copyright (C) 2009 Magnus Damm
  6. */
  7. #include <linux/clk.h>
  8. #include <linux/clockchips.h>
  9. #include <linux/delay.h>
  10. #include <linux/err.h>
  11. #include <linux/init.h>
  12. #include <linux/interrupt.h>
  13. #include <linux/io.h>
  14. #include <linux/ioport.h>
  15. #include <linux/irq.h>
  16. #include <linux/module.h>
  17. #include <linux/of.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/pm_domain.h>
  20. #include <linux/pm_runtime.h>
  21. #include <linux/sh_timer.h>
  22. #include <linux/slab.h>
  23. #include <linux/spinlock.h>
  24. #ifdef CONFIG_SUPERH
  25. #include <asm/platform_early.h>
  26. #endif
  27. struct sh_mtu2_device;
  28. struct sh_mtu2_channel {
  29. struct sh_mtu2_device *mtu;
  30. unsigned int index;
  31. void __iomem *base;
  32. struct clock_event_device ced;
  33. };
  34. struct sh_mtu2_device {
  35. struct platform_device *pdev;
  36. void __iomem *mapbase;
  37. struct clk *clk;
  38. raw_spinlock_t lock; /* Protect the shared registers */
  39. struct sh_mtu2_channel *channels;
  40. unsigned int num_channels;
  41. bool has_clockevent;
  42. };
  43. #define TSTR -1 /* shared register */
  44. #define TCR 0 /* channel register */
  45. #define TMDR 1 /* channel register */
  46. #define TIOR 2 /* channel register */
  47. #define TIER 3 /* channel register */
  48. #define TSR 4 /* channel register */
  49. #define TCNT 5 /* channel register */
  50. #define TGR 6 /* channel register */
  51. #define TCR_CCLR_NONE (0 << 5)
  52. #define TCR_CCLR_TGRA (1 << 5)
  53. #define TCR_CCLR_TGRB (2 << 5)
  54. #define TCR_CCLR_SYNC (3 << 5)
  55. #define TCR_CCLR_TGRC (5 << 5)
  56. #define TCR_CCLR_TGRD (6 << 5)
  57. #define TCR_CCLR_MASK (7 << 5)
  58. #define TCR_CKEG_RISING (0 << 3)
  59. #define TCR_CKEG_FALLING (1 << 3)
  60. #define TCR_CKEG_BOTH (2 << 3)
  61. #define TCR_CKEG_MASK (3 << 3)
  62. /* Values 4 to 7 are channel-dependent */
  63. #define TCR_TPSC_P1 (0 << 0)
  64. #define TCR_TPSC_P4 (1 << 0)
  65. #define TCR_TPSC_P16 (2 << 0)
  66. #define TCR_TPSC_P64 (3 << 0)
  67. #define TCR_TPSC_CH0_TCLKA (4 << 0)
  68. #define TCR_TPSC_CH0_TCLKB (5 << 0)
  69. #define TCR_TPSC_CH0_TCLKC (6 << 0)
  70. #define TCR_TPSC_CH0_TCLKD (7 << 0)
  71. #define TCR_TPSC_CH1_TCLKA (4 << 0)
  72. #define TCR_TPSC_CH1_TCLKB (5 << 0)
  73. #define TCR_TPSC_CH1_P256 (6 << 0)
  74. #define TCR_TPSC_CH1_TCNT2 (7 << 0)
  75. #define TCR_TPSC_CH2_TCLKA (4 << 0)
  76. #define TCR_TPSC_CH2_TCLKB (5 << 0)
  77. #define TCR_TPSC_CH2_TCLKC (6 << 0)
  78. #define TCR_TPSC_CH2_P1024 (7 << 0)
  79. #define TCR_TPSC_CH34_P256 (4 << 0)
  80. #define TCR_TPSC_CH34_P1024 (5 << 0)
  81. #define TCR_TPSC_CH34_TCLKA (6 << 0)
  82. #define TCR_TPSC_CH34_TCLKB (7 << 0)
  83. #define TCR_TPSC_MASK (7 << 0)
  84. #define TMDR_BFE (1 << 6)
  85. #define TMDR_BFB (1 << 5)
  86. #define TMDR_BFA (1 << 4)
  87. #define TMDR_MD_NORMAL (0 << 0)
  88. #define TMDR_MD_PWM_1 (2 << 0)
  89. #define TMDR_MD_PWM_2 (3 << 0)
  90. #define TMDR_MD_PHASE_1 (4 << 0)
  91. #define TMDR_MD_PHASE_2 (5 << 0)
  92. #define TMDR_MD_PHASE_3 (6 << 0)
  93. #define TMDR_MD_PHASE_4 (7 << 0)
  94. #define TMDR_MD_PWM_SYNC (8 << 0)
  95. #define TMDR_MD_PWM_COMP_CREST (13 << 0)
  96. #define TMDR_MD_PWM_COMP_TROUGH (14 << 0)
  97. #define TMDR_MD_PWM_COMP_BOTH (15 << 0)
  98. #define TMDR_MD_MASK (15 << 0)
  99. #define TIOC_IOCH(n) ((n) << 4)
  100. #define TIOC_IOCL(n) ((n) << 0)
  101. #define TIOR_OC_RETAIN (0 << 0)
  102. #define TIOR_OC_0_CLEAR (1 << 0)
  103. #define TIOR_OC_0_SET (2 << 0)
  104. #define TIOR_OC_0_TOGGLE (3 << 0)
  105. #define TIOR_OC_1_CLEAR (5 << 0)
  106. #define TIOR_OC_1_SET (6 << 0)
  107. #define TIOR_OC_1_TOGGLE (7 << 0)
  108. #define TIOR_IC_RISING (8 << 0)
  109. #define TIOR_IC_FALLING (9 << 0)
  110. #define TIOR_IC_BOTH (10 << 0)
  111. #define TIOR_IC_TCNT (12 << 0)
  112. #define TIOR_MASK (15 << 0)
  113. #define TIER_TTGE (1 << 7)
  114. #define TIER_TTGE2 (1 << 6)
  115. #define TIER_TCIEU (1 << 5)
  116. #define TIER_TCIEV (1 << 4)
  117. #define TIER_TGIED (1 << 3)
  118. #define TIER_TGIEC (1 << 2)
  119. #define TIER_TGIEB (1 << 1)
  120. #define TIER_TGIEA (1 << 0)
  121. #define TSR_TCFD (1 << 7)
  122. #define TSR_TCFU (1 << 5)
  123. #define TSR_TCFV (1 << 4)
  124. #define TSR_TGFD (1 << 3)
  125. #define TSR_TGFC (1 << 2)
  126. #define TSR_TGFB (1 << 1)
  127. #define TSR_TGFA (1 << 0)
  128. static unsigned long mtu2_reg_offs[] = {
  129. [TCR] = 0,
  130. [TMDR] = 1,
  131. [TIOR] = 2,
  132. [TIER] = 4,
  133. [TSR] = 5,
  134. [TCNT] = 6,
  135. [TGR] = 8,
  136. };
  137. static inline unsigned long sh_mtu2_read(struct sh_mtu2_channel *ch, int reg_nr)
  138. {
  139. unsigned long offs;
  140. if (reg_nr == TSTR)
  141. return ioread8(ch->mtu->mapbase + 0x280);
  142. offs = mtu2_reg_offs[reg_nr];
  143. if ((reg_nr == TCNT) || (reg_nr == TGR))
  144. return ioread16(ch->base + offs);
  145. else
  146. return ioread8(ch->base + offs);
  147. }
  148. static inline void sh_mtu2_write(struct sh_mtu2_channel *ch, int reg_nr,
  149. unsigned long value)
  150. {
  151. unsigned long offs;
  152. if (reg_nr == TSTR)
  153. return iowrite8(value, ch->mtu->mapbase + 0x280);
  154. offs = mtu2_reg_offs[reg_nr];
  155. if ((reg_nr == TCNT) || (reg_nr == TGR))
  156. iowrite16(value, ch->base + offs);
  157. else
  158. iowrite8(value, ch->base + offs);
  159. }
  160. static void sh_mtu2_start_stop_ch(struct sh_mtu2_channel *ch, int start)
  161. {
  162. unsigned long flags, value;
  163. /* start stop register shared by multiple timer channels */
  164. raw_spin_lock_irqsave(&ch->mtu->lock, flags);
  165. value = sh_mtu2_read(ch, TSTR);
  166. if (start)
  167. value |= 1 << ch->index;
  168. else
  169. value &= ~(1 << ch->index);
  170. sh_mtu2_write(ch, TSTR, value);
  171. raw_spin_unlock_irqrestore(&ch->mtu->lock, flags);
  172. }
  173. static int sh_mtu2_enable(struct sh_mtu2_channel *ch)
  174. {
  175. unsigned long periodic;
  176. unsigned long rate;
  177. int ret;
  178. pm_runtime_get_sync(&ch->mtu->pdev->dev);
  179. dev_pm_syscore_device(&ch->mtu->pdev->dev, true);
  180. /* enable clock */
  181. ret = clk_enable(ch->mtu->clk);
  182. if (ret) {
  183. dev_err(&ch->mtu->pdev->dev, "ch%u: cannot enable clock\n",
  184. ch->index);
  185. return ret;
  186. }
  187. /* make sure channel is disabled */
  188. sh_mtu2_start_stop_ch(ch, 0);
  189. rate = clk_get_rate(ch->mtu->clk) / 64;
  190. periodic = (rate + HZ/2) / HZ;
  191. /*
  192. * "Periodic Counter Operation"
  193. * Clear on TGRA compare match, divide clock by 64.
  194. */
  195. sh_mtu2_write(ch, TCR, TCR_CCLR_TGRA | TCR_TPSC_P64);
  196. sh_mtu2_write(ch, TIOR, TIOC_IOCH(TIOR_OC_0_CLEAR) |
  197. TIOC_IOCL(TIOR_OC_0_CLEAR));
  198. sh_mtu2_write(ch, TGR, periodic);
  199. sh_mtu2_write(ch, TCNT, 0);
  200. sh_mtu2_write(ch, TMDR, TMDR_MD_NORMAL);
  201. sh_mtu2_write(ch, TIER, TIER_TGIEA);
  202. /* enable channel */
  203. sh_mtu2_start_stop_ch(ch, 1);
  204. return 0;
  205. }
  206. static void sh_mtu2_disable(struct sh_mtu2_channel *ch)
  207. {
  208. /* disable channel */
  209. sh_mtu2_start_stop_ch(ch, 0);
  210. /* stop clock */
  211. clk_disable(ch->mtu->clk);
  212. dev_pm_syscore_device(&ch->mtu->pdev->dev, false);
  213. pm_runtime_put(&ch->mtu->pdev->dev);
  214. }
  215. static irqreturn_t sh_mtu2_interrupt(int irq, void *dev_id)
  216. {
  217. struct sh_mtu2_channel *ch = dev_id;
  218. /* acknowledge interrupt */
  219. sh_mtu2_read(ch, TSR);
  220. sh_mtu2_write(ch, TSR, ~TSR_TGFA);
  221. /* notify clockevent layer */
  222. ch->ced.event_handler(&ch->ced);
  223. return IRQ_HANDLED;
  224. }
  225. static struct sh_mtu2_channel *ced_to_sh_mtu2(struct clock_event_device *ced)
  226. {
  227. return container_of(ced, struct sh_mtu2_channel, ced);
  228. }
  229. static int sh_mtu2_clock_event_shutdown(struct clock_event_device *ced)
  230. {
  231. struct sh_mtu2_channel *ch = ced_to_sh_mtu2(ced);
  232. if (clockevent_state_periodic(ced))
  233. sh_mtu2_disable(ch);
  234. return 0;
  235. }
  236. static int sh_mtu2_clock_event_set_periodic(struct clock_event_device *ced)
  237. {
  238. struct sh_mtu2_channel *ch = ced_to_sh_mtu2(ced);
  239. if (clockevent_state_periodic(ced))
  240. sh_mtu2_disable(ch);
  241. dev_info(&ch->mtu->pdev->dev, "ch%u: used for periodic clock events\n",
  242. ch->index);
  243. sh_mtu2_enable(ch);
  244. return 0;
  245. }
  246. static void sh_mtu2_clock_event_suspend(struct clock_event_device *ced)
  247. {
  248. dev_pm_genpd_suspend(&ced_to_sh_mtu2(ced)->mtu->pdev->dev);
  249. }
  250. static void sh_mtu2_clock_event_resume(struct clock_event_device *ced)
  251. {
  252. dev_pm_genpd_resume(&ced_to_sh_mtu2(ced)->mtu->pdev->dev);
  253. }
  254. static void sh_mtu2_register_clockevent(struct sh_mtu2_channel *ch,
  255. const char *name)
  256. {
  257. struct clock_event_device *ced = &ch->ced;
  258. ced->name = name;
  259. ced->features = CLOCK_EVT_FEAT_PERIODIC;
  260. ced->rating = 200;
  261. ced->cpumask = cpu_possible_mask;
  262. ced->set_state_shutdown = sh_mtu2_clock_event_shutdown;
  263. ced->set_state_periodic = sh_mtu2_clock_event_set_periodic;
  264. ced->suspend = sh_mtu2_clock_event_suspend;
  265. ced->resume = sh_mtu2_clock_event_resume;
  266. dev_info(&ch->mtu->pdev->dev, "ch%u: used for clock events\n",
  267. ch->index);
  268. clockevents_register_device(ced);
  269. }
  270. static int sh_mtu2_register(struct sh_mtu2_channel *ch, const char *name)
  271. {
  272. ch->mtu->has_clockevent = true;
  273. sh_mtu2_register_clockevent(ch, name);
  274. return 0;
  275. }
  276. static const unsigned int sh_mtu2_channel_offsets[] = {
  277. 0x300, 0x380, 0x000,
  278. };
  279. static int sh_mtu2_setup_channel(struct sh_mtu2_channel *ch, unsigned int index,
  280. struct sh_mtu2_device *mtu)
  281. {
  282. char name[6];
  283. int irq;
  284. int ret;
  285. ch->mtu = mtu;
  286. sprintf(name, "tgi%ua", index);
  287. irq = platform_get_irq_byname(mtu->pdev, name);
  288. if (irq < 0) {
  289. /* Skip channels with no declared interrupt. */
  290. return 0;
  291. }
  292. ret = request_irq(irq, sh_mtu2_interrupt,
  293. IRQF_TIMER | IRQF_IRQPOLL | IRQF_NOBALANCING,
  294. dev_name(&ch->mtu->pdev->dev), ch);
  295. if (ret) {
  296. dev_err(&ch->mtu->pdev->dev, "ch%u: failed to request irq %d\n",
  297. index, irq);
  298. return ret;
  299. }
  300. ch->base = mtu->mapbase + sh_mtu2_channel_offsets[index];
  301. ch->index = index;
  302. return sh_mtu2_register(ch, dev_name(&mtu->pdev->dev));
  303. }
  304. static int sh_mtu2_map_memory(struct sh_mtu2_device *mtu)
  305. {
  306. struct resource *res;
  307. res = platform_get_resource(mtu->pdev, IORESOURCE_MEM, 0);
  308. if (!res) {
  309. dev_err(&mtu->pdev->dev, "failed to get I/O memory\n");
  310. return -ENXIO;
  311. }
  312. mtu->mapbase = ioremap(res->start, resource_size(res));
  313. if (mtu->mapbase == NULL)
  314. return -ENXIO;
  315. return 0;
  316. }
  317. static int sh_mtu2_setup(struct sh_mtu2_device *mtu,
  318. struct platform_device *pdev)
  319. {
  320. unsigned int i;
  321. int ret;
  322. mtu->pdev = pdev;
  323. raw_spin_lock_init(&mtu->lock);
  324. /* Get hold of clock. */
  325. mtu->clk = clk_get(&mtu->pdev->dev, "fck");
  326. if (IS_ERR(mtu->clk)) {
  327. dev_err(&mtu->pdev->dev, "cannot get clock\n");
  328. return PTR_ERR(mtu->clk);
  329. }
  330. ret = clk_prepare(mtu->clk);
  331. if (ret < 0)
  332. goto err_clk_put;
  333. /* Map the memory resource. */
  334. ret = sh_mtu2_map_memory(mtu);
  335. if (ret < 0) {
  336. dev_err(&mtu->pdev->dev, "failed to remap I/O memory\n");
  337. goto err_clk_unprepare;
  338. }
  339. /* Allocate and setup the channels. */
  340. ret = platform_irq_count(pdev);
  341. if (ret < 0)
  342. goto err_unmap;
  343. mtu->num_channels = min_t(unsigned int, ret,
  344. ARRAY_SIZE(sh_mtu2_channel_offsets));
  345. mtu->channels = kcalloc(mtu->num_channels, sizeof(*mtu->channels),
  346. GFP_KERNEL);
  347. if (mtu->channels == NULL) {
  348. ret = -ENOMEM;
  349. goto err_unmap;
  350. }
  351. for (i = 0; i < mtu->num_channels; ++i) {
  352. ret = sh_mtu2_setup_channel(&mtu->channels[i], i, mtu);
  353. if (ret < 0)
  354. goto err_unmap;
  355. }
  356. platform_set_drvdata(pdev, mtu);
  357. return 0;
  358. err_unmap:
  359. kfree(mtu->channels);
  360. iounmap(mtu->mapbase);
  361. err_clk_unprepare:
  362. clk_unprepare(mtu->clk);
  363. err_clk_put:
  364. clk_put(mtu->clk);
  365. return ret;
  366. }
  367. static int sh_mtu2_probe(struct platform_device *pdev)
  368. {
  369. struct sh_mtu2_device *mtu = platform_get_drvdata(pdev);
  370. int ret;
  371. if (!is_sh_early_platform_device(pdev)) {
  372. pm_runtime_set_active(&pdev->dev);
  373. pm_runtime_enable(&pdev->dev);
  374. }
  375. if (mtu) {
  376. dev_info(&pdev->dev, "kept as earlytimer\n");
  377. goto out;
  378. }
  379. mtu = kzalloc(sizeof(*mtu), GFP_KERNEL);
  380. if (mtu == NULL)
  381. return -ENOMEM;
  382. ret = sh_mtu2_setup(mtu, pdev);
  383. if (ret) {
  384. kfree(mtu);
  385. pm_runtime_idle(&pdev->dev);
  386. return ret;
  387. }
  388. if (is_sh_early_platform_device(pdev))
  389. return 0;
  390. out:
  391. if (mtu->has_clockevent)
  392. pm_runtime_irq_safe(&pdev->dev);
  393. else
  394. pm_runtime_idle(&pdev->dev);
  395. return 0;
  396. }
  397. static int sh_mtu2_remove(struct platform_device *pdev)
  398. {
  399. return -EBUSY; /* cannot unregister clockevent */
  400. }
  401. static const struct platform_device_id sh_mtu2_id_table[] = {
  402. { "sh-mtu2", 0 },
  403. { },
  404. };
  405. MODULE_DEVICE_TABLE(platform, sh_mtu2_id_table);
  406. static const struct of_device_id sh_mtu2_of_table[] __maybe_unused = {
  407. { .compatible = "renesas,mtu2" },
  408. { }
  409. };
  410. MODULE_DEVICE_TABLE(of, sh_mtu2_of_table);
  411. static struct platform_driver sh_mtu2_device_driver = {
  412. .probe = sh_mtu2_probe,
  413. .remove = sh_mtu2_remove,
  414. .driver = {
  415. .name = "sh_mtu2",
  416. .of_match_table = of_match_ptr(sh_mtu2_of_table),
  417. },
  418. .id_table = sh_mtu2_id_table,
  419. };
  420. static int __init sh_mtu2_init(void)
  421. {
  422. return platform_driver_register(&sh_mtu2_device_driver);
  423. }
  424. static void __exit sh_mtu2_exit(void)
  425. {
  426. platform_driver_unregister(&sh_mtu2_device_driver);
  427. }
  428. #ifdef CONFIG_SUPERH
  429. sh_early_platform_init("earlytimer", &sh_mtu2_device_driver);
  430. #endif
  431. subsys_initcall(sh_mtu2_init);
  432. module_exit(sh_mtu2_exit);
  433. MODULE_AUTHOR("Magnus Damm");
  434. MODULE_DESCRIPTION("SuperH MTU2 Timer Driver");
  435. MODULE_LICENSE("GPL v2");