sh_mtu2.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  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 void sh_mtu2_clock_event_mode(enum clock_event_mode mode,
  235. struct clock_event_device *ced)
  236. {
  237. struct sh_mtu2_channel *ch = ced_to_sh_mtu2(ced);
  238. int disabled = 0;
  239. /* deal with old setting first */
  240. switch (ced->mode) {
  241. case CLOCK_EVT_MODE_PERIODIC:
  242. sh_mtu2_disable(ch);
  243. disabled = 1;
  244. break;
  245. default:
  246. break;
  247. }
  248. switch (mode) {
  249. case CLOCK_EVT_MODE_PERIODIC:
  250. dev_info(&ch->mtu->pdev->dev,
  251. "ch%u: used for periodic clock events\n", ch->index);
  252. sh_mtu2_enable(ch);
  253. break;
  254. case CLOCK_EVT_MODE_UNUSED:
  255. if (!disabled)
  256. sh_mtu2_disable(ch);
  257. break;
  258. case CLOCK_EVT_MODE_SHUTDOWN:
  259. default:
  260. break;
  261. }
  262. }
  263. static void sh_mtu2_clock_event_suspend(struct clock_event_device *ced)
  264. {
  265. pm_genpd_syscore_poweroff(&ced_to_sh_mtu2(ced)->mtu->pdev->dev);
  266. }
  267. static void sh_mtu2_clock_event_resume(struct clock_event_device *ced)
  268. {
  269. pm_genpd_syscore_poweron(&ced_to_sh_mtu2(ced)->mtu->pdev->dev);
  270. }
  271. static void sh_mtu2_register_clockevent(struct sh_mtu2_channel *ch,
  272. const char *name)
  273. {
  274. struct clock_event_device *ced = &ch->ced;
  275. ced->name = name;
  276. ced->features = CLOCK_EVT_FEAT_PERIODIC;
  277. ced->rating = 200;
  278. ced->cpumask = cpu_possible_mask;
  279. ced->set_mode = sh_mtu2_clock_event_mode;
  280. ced->suspend = sh_mtu2_clock_event_suspend;
  281. ced->resume = sh_mtu2_clock_event_resume;
  282. dev_info(&ch->mtu->pdev->dev, "ch%u: used for clock events\n",
  283. ch->index);
  284. clockevents_register_device(ced);
  285. }
  286. static int sh_mtu2_register(struct sh_mtu2_channel *ch, const char *name)
  287. {
  288. ch->mtu->has_clockevent = true;
  289. sh_mtu2_register_clockevent(ch, name);
  290. return 0;
  291. }
  292. static int sh_mtu2_setup_channel(struct sh_mtu2_channel *ch, unsigned int index,
  293. struct sh_mtu2_device *mtu)
  294. {
  295. static const unsigned int channel_offsets[] = {
  296. 0x300, 0x380, 0x000,
  297. };
  298. char name[6];
  299. int irq;
  300. int ret;
  301. ch->mtu = mtu;
  302. sprintf(name, "tgi%ua", index);
  303. irq = platform_get_irq_byname(mtu->pdev, name);
  304. if (irq < 0) {
  305. /* Skip channels with no declared interrupt. */
  306. return 0;
  307. }
  308. ret = request_irq(irq, sh_mtu2_interrupt,
  309. IRQF_TIMER | IRQF_IRQPOLL | IRQF_NOBALANCING,
  310. dev_name(&ch->mtu->pdev->dev), ch);
  311. if (ret) {
  312. dev_err(&ch->mtu->pdev->dev, "ch%u: failed to request irq %d\n",
  313. index, irq);
  314. return ret;
  315. }
  316. ch->base = mtu->mapbase + channel_offsets[index];
  317. ch->index = index;
  318. return sh_mtu2_register(ch, dev_name(&mtu->pdev->dev));
  319. }
  320. static int sh_mtu2_map_memory(struct sh_mtu2_device *mtu)
  321. {
  322. struct resource *res;
  323. res = platform_get_resource(mtu->pdev, IORESOURCE_MEM, 0);
  324. if (!res) {
  325. dev_err(&mtu->pdev->dev, "failed to get I/O memory\n");
  326. return -ENXIO;
  327. }
  328. mtu->mapbase = ioremap_nocache(res->start, resource_size(res));
  329. if (mtu->mapbase == NULL)
  330. return -ENXIO;
  331. return 0;
  332. }
  333. static int sh_mtu2_setup(struct sh_mtu2_device *mtu,
  334. struct platform_device *pdev)
  335. {
  336. unsigned int i;
  337. int ret;
  338. mtu->pdev = pdev;
  339. raw_spin_lock_init(&mtu->lock);
  340. /* Get hold of clock. */
  341. mtu->clk = clk_get(&mtu->pdev->dev, "fck");
  342. if (IS_ERR(mtu->clk)) {
  343. dev_err(&mtu->pdev->dev, "cannot get clock\n");
  344. return PTR_ERR(mtu->clk);
  345. }
  346. ret = clk_prepare(mtu->clk);
  347. if (ret < 0)
  348. goto err_clk_put;
  349. /* Map the memory resource. */
  350. ret = sh_mtu2_map_memory(mtu);
  351. if (ret < 0) {
  352. dev_err(&mtu->pdev->dev, "failed to remap I/O memory\n");
  353. goto err_clk_unprepare;
  354. }
  355. /* Allocate and setup the channels. */
  356. mtu->num_channels = 3;
  357. mtu->channels = kzalloc(sizeof(*mtu->channels) * mtu->num_channels,
  358. GFP_KERNEL);
  359. if (mtu->channels == NULL) {
  360. ret = -ENOMEM;
  361. goto err_unmap;
  362. }
  363. for (i = 0; i < mtu->num_channels; ++i) {
  364. ret = sh_mtu2_setup_channel(&mtu->channels[i], i, mtu);
  365. if (ret < 0)
  366. goto err_unmap;
  367. }
  368. platform_set_drvdata(pdev, mtu);
  369. return 0;
  370. err_unmap:
  371. kfree(mtu->channels);
  372. iounmap(mtu->mapbase);
  373. err_clk_unprepare:
  374. clk_unprepare(mtu->clk);
  375. err_clk_put:
  376. clk_put(mtu->clk);
  377. return ret;
  378. }
  379. static int sh_mtu2_probe(struct platform_device *pdev)
  380. {
  381. struct sh_mtu2_device *mtu = platform_get_drvdata(pdev);
  382. int ret;
  383. if (!is_early_platform_device(pdev)) {
  384. pm_runtime_set_active(&pdev->dev);
  385. pm_runtime_enable(&pdev->dev);
  386. }
  387. if (mtu) {
  388. dev_info(&pdev->dev, "kept as earlytimer\n");
  389. goto out;
  390. }
  391. mtu = kzalloc(sizeof(*mtu), GFP_KERNEL);
  392. if (mtu == NULL)
  393. return -ENOMEM;
  394. ret = sh_mtu2_setup(mtu, pdev);
  395. if (ret) {
  396. kfree(mtu);
  397. pm_runtime_idle(&pdev->dev);
  398. return ret;
  399. }
  400. if (is_early_platform_device(pdev))
  401. return 0;
  402. out:
  403. if (mtu->has_clockevent)
  404. pm_runtime_irq_safe(&pdev->dev);
  405. else
  406. pm_runtime_idle(&pdev->dev);
  407. return 0;
  408. }
  409. static int sh_mtu2_remove(struct platform_device *pdev)
  410. {
  411. return -EBUSY; /* cannot unregister clockevent */
  412. }
  413. static const struct platform_device_id sh_mtu2_id_table[] = {
  414. { "sh-mtu2", 0 },
  415. { },
  416. };
  417. MODULE_DEVICE_TABLE(platform, sh_mtu2_id_table);
  418. static const struct of_device_id sh_mtu2_of_table[] __maybe_unused = {
  419. { .compatible = "renesas,mtu2" },
  420. { }
  421. };
  422. MODULE_DEVICE_TABLE(of, sh_mtu2_of_table);
  423. static struct platform_driver sh_mtu2_device_driver = {
  424. .probe = sh_mtu2_probe,
  425. .remove = sh_mtu2_remove,
  426. .driver = {
  427. .name = "sh_mtu2",
  428. .of_match_table = of_match_ptr(sh_mtu2_of_table),
  429. },
  430. .id_table = sh_mtu2_id_table,
  431. };
  432. static int __init sh_mtu2_init(void)
  433. {
  434. return platform_driver_register(&sh_mtu2_device_driver);
  435. }
  436. static void __exit sh_mtu2_exit(void)
  437. {
  438. platform_driver_unregister(&sh_mtu2_device_driver);
  439. }
  440. early_platform_init("earlytimer", &sh_mtu2_device_driver);
  441. subsys_initcall(sh_mtu2_init);
  442. module_exit(sh_mtu2_exit);
  443. MODULE_AUTHOR("Magnus Damm");
  444. MODULE_DESCRIPTION("SuperH MTU2 Timer Driver");
  445. MODULE_LICENSE("GPL v2");