sh_mtu2.c 12 KB

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