sh_tmu.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * SuperH Timer Support - TMU
  4. *
  5. * Copyright (C) 2009 Magnus Damm
  6. */
  7. #include <linux/clk.h>
  8. #include <linux/clockchips.h>
  9. #include <linux/clocksource.h>
  10. #include <linux/delay.h>
  11. #include <linux/err.h>
  12. #include <linux/init.h>
  13. #include <linux/interrupt.h>
  14. #include <linux/io.h>
  15. #include <linux/ioport.h>
  16. #include <linux/irq.h>
  17. #include <linux/module.h>
  18. #include <linux/of.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/pm_domain.h>
  21. #include <linux/pm_runtime.h>
  22. #include <linux/sh_timer.h>
  23. #include <linux/slab.h>
  24. #include <linux/spinlock.h>
  25. #ifdef CONFIG_SUPERH
  26. #include <asm/platform_early.h>
  27. #endif
  28. enum sh_tmu_model {
  29. SH_TMU,
  30. SH_TMU_SH3,
  31. };
  32. struct sh_tmu_device;
  33. struct sh_tmu_channel {
  34. struct sh_tmu_device *tmu;
  35. unsigned int index;
  36. void __iomem *base;
  37. int irq;
  38. unsigned long periodic;
  39. struct clock_event_device ced;
  40. struct clocksource cs;
  41. bool cs_enabled;
  42. unsigned int enable_count;
  43. };
  44. struct sh_tmu_device {
  45. struct platform_device *pdev;
  46. void __iomem *mapbase;
  47. struct clk *clk;
  48. unsigned long rate;
  49. enum sh_tmu_model model;
  50. raw_spinlock_t lock; /* Protect the shared start/stop register */
  51. struct sh_tmu_channel *channels;
  52. unsigned int num_channels;
  53. bool has_clockevent;
  54. bool has_clocksource;
  55. };
  56. #define TSTR -1 /* shared register */
  57. #define TCOR 0 /* channel register */
  58. #define TCNT 1 /* channel register */
  59. #define TCR 2 /* channel register */
  60. #define TCR_UNF (1 << 8)
  61. #define TCR_UNIE (1 << 5)
  62. #define TCR_TPSC_CLK4 (0 << 0)
  63. #define TCR_TPSC_CLK16 (1 << 0)
  64. #define TCR_TPSC_CLK64 (2 << 0)
  65. #define TCR_TPSC_CLK256 (3 << 0)
  66. #define TCR_TPSC_CLK1024 (4 << 0)
  67. #define TCR_TPSC_MASK (7 << 0)
  68. static inline unsigned long sh_tmu_read(struct sh_tmu_channel *ch, int reg_nr)
  69. {
  70. unsigned long offs;
  71. if (reg_nr == TSTR) {
  72. switch (ch->tmu->model) {
  73. case SH_TMU_SH3:
  74. return ioread8(ch->tmu->mapbase + 2);
  75. case SH_TMU:
  76. return ioread8(ch->tmu->mapbase + 4);
  77. }
  78. }
  79. offs = reg_nr << 2;
  80. if (reg_nr == TCR)
  81. return ioread16(ch->base + offs);
  82. else
  83. return ioread32(ch->base + offs);
  84. }
  85. static inline void sh_tmu_write(struct sh_tmu_channel *ch, int reg_nr,
  86. unsigned long value)
  87. {
  88. unsigned long offs;
  89. if (reg_nr == TSTR) {
  90. switch (ch->tmu->model) {
  91. case SH_TMU_SH3:
  92. return iowrite8(value, ch->tmu->mapbase + 2);
  93. case SH_TMU:
  94. return iowrite8(value, ch->tmu->mapbase + 4);
  95. }
  96. }
  97. offs = reg_nr << 2;
  98. if (reg_nr == TCR)
  99. iowrite16(value, ch->base + offs);
  100. else
  101. iowrite32(value, ch->base + offs);
  102. }
  103. static void sh_tmu_start_stop_ch(struct sh_tmu_channel *ch, int start)
  104. {
  105. unsigned long flags, value;
  106. /* start stop register shared by multiple timer channels */
  107. raw_spin_lock_irqsave(&ch->tmu->lock, flags);
  108. value = sh_tmu_read(ch, TSTR);
  109. if (start)
  110. value |= 1 << ch->index;
  111. else
  112. value &= ~(1 << ch->index);
  113. sh_tmu_write(ch, TSTR, value);
  114. raw_spin_unlock_irqrestore(&ch->tmu->lock, flags);
  115. }
  116. static int __sh_tmu_enable(struct sh_tmu_channel *ch)
  117. {
  118. /* make sure channel is disabled */
  119. sh_tmu_start_stop_ch(ch, 0);
  120. /* maximum timeout */
  121. sh_tmu_write(ch, TCOR, 0xffffffff);
  122. sh_tmu_write(ch, TCNT, 0xffffffff);
  123. /* configure channel to parent clock / 4, irq off */
  124. sh_tmu_write(ch, TCR, TCR_TPSC_CLK4);
  125. /* enable channel */
  126. sh_tmu_start_stop_ch(ch, 1);
  127. return 0;
  128. }
  129. static int sh_tmu_enable(struct sh_tmu_channel *ch)
  130. {
  131. if (ch->enable_count++ > 0)
  132. return 0;
  133. dev_pm_syscore_device(&ch->tmu->pdev->dev, true);
  134. return __sh_tmu_enable(ch);
  135. }
  136. static void __sh_tmu_disable(struct sh_tmu_channel *ch)
  137. {
  138. /* disable channel */
  139. sh_tmu_start_stop_ch(ch, 0);
  140. /* disable interrupts in TMU block */
  141. sh_tmu_write(ch, TCR, TCR_TPSC_CLK4);
  142. }
  143. static void sh_tmu_disable(struct sh_tmu_channel *ch)
  144. {
  145. if (WARN_ON(ch->enable_count == 0))
  146. return;
  147. if (--ch->enable_count > 0)
  148. return;
  149. __sh_tmu_disable(ch);
  150. dev_pm_syscore_device(&ch->tmu->pdev->dev, false);
  151. }
  152. static void sh_tmu_set_next(struct sh_tmu_channel *ch, unsigned long delta,
  153. int periodic)
  154. {
  155. /* stop timer */
  156. sh_tmu_start_stop_ch(ch, 0);
  157. /* acknowledge interrupt */
  158. sh_tmu_read(ch, TCR);
  159. /* enable interrupt */
  160. sh_tmu_write(ch, TCR, TCR_UNIE | TCR_TPSC_CLK4);
  161. /* reload delta value in case of periodic timer */
  162. if (periodic)
  163. sh_tmu_write(ch, TCOR, delta);
  164. else
  165. sh_tmu_write(ch, TCOR, 0xffffffff);
  166. sh_tmu_write(ch, TCNT, delta);
  167. /* start timer */
  168. sh_tmu_start_stop_ch(ch, 1);
  169. }
  170. static irqreturn_t sh_tmu_interrupt(int irq, void *dev_id)
  171. {
  172. struct sh_tmu_channel *ch = dev_id;
  173. /* disable or acknowledge interrupt */
  174. if (clockevent_state_oneshot(&ch->ced))
  175. sh_tmu_write(ch, TCR, TCR_TPSC_CLK4);
  176. else
  177. sh_tmu_write(ch, TCR, TCR_UNIE | TCR_TPSC_CLK4);
  178. /* notify clockevent layer */
  179. ch->ced.event_handler(&ch->ced);
  180. return IRQ_HANDLED;
  181. }
  182. static struct sh_tmu_channel *cs_to_sh_tmu(struct clocksource *cs)
  183. {
  184. return container_of(cs, struct sh_tmu_channel, cs);
  185. }
  186. static u64 sh_tmu_clocksource_read(struct clocksource *cs)
  187. {
  188. struct sh_tmu_channel *ch = cs_to_sh_tmu(cs);
  189. return sh_tmu_read(ch, TCNT) ^ 0xffffffff;
  190. }
  191. static int sh_tmu_clocksource_enable(struct clocksource *cs)
  192. {
  193. struct sh_tmu_channel *ch = cs_to_sh_tmu(cs);
  194. int ret;
  195. if (WARN_ON(ch->cs_enabled))
  196. return 0;
  197. ret = sh_tmu_enable(ch);
  198. if (!ret)
  199. ch->cs_enabled = true;
  200. return ret;
  201. }
  202. static void sh_tmu_clocksource_disable(struct clocksource *cs)
  203. {
  204. struct sh_tmu_channel *ch = cs_to_sh_tmu(cs);
  205. if (WARN_ON(!ch->cs_enabled))
  206. return;
  207. sh_tmu_disable(ch);
  208. ch->cs_enabled = false;
  209. }
  210. static void sh_tmu_clocksource_suspend(struct clocksource *cs)
  211. {
  212. struct sh_tmu_channel *ch = cs_to_sh_tmu(cs);
  213. if (!ch->cs_enabled)
  214. return;
  215. if (--ch->enable_count == 0) {
  216. __sh_tmu_disable(ch);
  217. dev_pm_genpd_suspend(&ch->tmu->pdev->dev);
  218. }
  219. }
  220. static void sh_tmu_clocksource_resume(struct clocksource *cs)
  221. {
  222. struct sh_tmu_channel *ch = cs_to_sh_tmu(cs);
  223. if (!ch->cs_enabled)
  224. return;
  225. if (ch->enable_count++ == 0) {
  226. dev_pm_genpd_resume(&ch->tmu->pdev->dev);
  227. __sh_tmu_enable(ch);
  228. }
  229. }
  230. static int sh_tmu_register_clocksource(struct sh_tmu_channel *ch,
  231. const char *name)
  232. {
  233. struct clocksource *cs = &ch->cs;
  234. cs->name = name;
  235. cs->rating = 200;
  236. cs->read = sh_tmu_clocksource_read;
  237. cs->enable = sh_tmu_clocksource_enable;
  238. cs->disable = sh_tmu_clocksource_disable;
  239. cs->suspend = sh_tmu_clocksource_suspend;
  240. cs->resume = sh_tmu_clocksource_resume;
  241. cs->mask = CLOCKSOURCE_MASK(32);
  242. cs->flags = CLOCK_SOURCE_IS_CONTINUOUS;
  243. dev_info(&ch->tmu->pdev->dev, "ch%u: used as clock source\n",
  244. ch->index);
  245. clocksource_register_hz(cs, ch->tmu->rate);
  246. return 0;
  247. }
  248. static struct sh_tmu_channel *ced_to_sh_tmu(struct clock_event_device *ced)
  249. {
  250. return container_of(ced, struct sh_tmu_channel, ced);
  251. }
  252. static void sh_tmu_clock_event_start(struct sh_tmu_channel *ch, int periodic)
  253. {
  254. sh_tmu_enable(ch);
  255. if (periodic) {
  256. ch->periodic = (ch->tmu->rate + HZ/2) / HZ;
  257. sh_tmu_set_next(ch, ch->periodic, 1);
  258. }
  259. }
  260. static int sh_tmu_clock_event_shutdown(struct clock_event_device *ced)
  261. {
  262. struct sh_tmu_channel *ch = ced_to_sh_tmu(ced);
  263. if (clockevent_state_oneshot(ced) || clockevent_state_periodic(ced))
  264. sh_tmu_disable(ch);
  265. return 0;
  266. }
  267. static int sh_tmu_clock_event_set_state(struct clock_event_device *ced,
  268. int periodic)
  269. {
  270. struct sh_tmu_channel *ch = ced_to_sh_tmu(ced);
  271. /* deal with old setting first */
  272. if (clockevent_state_oneshot(ced) || clockevent_state_periodic(ced))
  273. sh_tmu_disable(ch);
  274. dev_info(&ch->tmu->pdev->dev, "ch%u: used for %s clock events\n",
  275. ch->index, periodic ? "periodic" : "oneshot");
  276. sh_tmu_clock_event_start(ch, periodic);
  277. return 0;
  278. }
  279. static int sh_tmu_clock_event_set_oneshot(struct clock_event_device *ced)
  280. {
  281. return sh_tmu_clock_event_set_state(ced, 0);
  282. }
  283. static int sh_tmu_clock_event_set_periodic(struct clock_event_device *ced)
  284. {
  285. return sh_tmu_clock_event_set_state(ced, 1);
  286. }
  287. static int sh_tmu_clock_event_next(unsigned long delta,
  288. struct clock_event_device *ced)
  289. {
  290. struct sh_tmu_channel *ch = ced_to_sh_tmu(ced);
  291. BUG_ON(!clockevent_state_oneshot(ced));
  292. /* program new delta value */
  293. sh_tmu_set_next(ch, delta, 0);
  294. return 0;
  295. }
  296. static void sh_tmu_clock_event_suspend(struct clock_event_device *ced)
  297. {
  298. dev_pm_genpd_suspend(&ced_to_sh_tmu(ced)->tmu->pdev->dev);
  299. }
  300. static void sh_tmu_clock_event_resume(struct clock_event_device *ced)
  301. {
  302. dev_pm_genpd_resume(&ced_to_sh_tmu(ced)->tmu->pdev->dev);
  303. }
  304. static void sh_tmu_register_clockevent(struct sh_tmu_channel *ch,
  305. const char *name)
  306. {
  307. struct clock_event_device *ced = &ch->ced;
  308. int ret;
  309. ced->name = name;
  310. ced->features = CLOCK_EVT_FEAT_PERIODIC;
  311. ced->features |= CLOCK_EVT_FEAT_ONESHOT;
  312. ced->rating = 200;
  313. ced->cpumask = cpu_possible_mask;
  314. ced->set_next_event = sh_tmu_clock_event_next;
  315. ced->set_state_shutdown = sh_tmu_clock_event_shutdown;
  316. ced->set_state_periodic = sh_tmu_clock_event_set_periodic;
  317. ced->set_state_oneshot = sh_tmu_clock_event_set_oneshot;
  318. ced->suspend = sh_tmu_clock_event_suspend;
  319. ced->resume = sh_tmu_clock_event_resume;
  320. dev_info(&ch->tmu->pdev->dev, "ch%u: used for clock events\n",
  321. ch->index);
  322. clockevents_config_and_register(ced, ch->tmu->rate, 0x300, 0xffffffff);
  323. ret = request_irq(ch->irq, sh_tmu_interrupt,
  324. IRQF_TIMER | IRQF_IRQPOLL | IRQF_NOBALANCING,
  325. dev_name(&ch->tmu->pdev->dev), ch);
  326. if (ret) {
  327. dev_err(&ch->tmu->pdev->dev, "ch%u: failed to request irq %d\n",
  328. ch->index, ch->irq);
  329. return;
  330. }
  331. }
  332. static int sh_tmu_register(struct sh_tmu_channel *ch, const char *name,
  333. bool clockevent, bool clocksource)
  334. {
  335. if (clockevent) {
  336. ch->tmu->has_clockevent = true;
  337. sh_tmu_register_clockevent(ch, name);
  338. } else if (clocksource) {
  339. ch->tmu->has_clocksource = true;
  340. sh_tmu_register_clocksource(ch, name);
  341. }
  342. return 0;
  343. }
  344. static int sh_tmu_channel_setup(struct sh_tmu_channel *ch, unsigned int index,
  345. bool clockevent, bool clocksource,
  346. struct sh_tmu_device *tmu)
  347. {
  348. /* Skip unused channels. */
  349. if (!clockevent && !clocksource)
  350. return 0;
  351. ch->tmu = tmu;
  352. ch->index = index;
  353. if (tmu->model == SH_TMU_SH3)
  354. ch->base = tmu->mapbase + 4 + ch->index * 12;
  355. else
  356. ch->base = tmu->mapbase + 8 + ch->index * 12;
  357. ch->irq = platform_get_irq(tmu->pdev, index);
  358. if (ch->irq < 0)
  359. return ch->irq;
  360. ch->cs_enabled = false;
  361. ch->enable_count = 0;
  362. return sh_tmu_register(ch, dev_name(&tmu->pdev->dev),
  363. clockevent, clocksource);
  364. }
  365. static int sh_tmu_map_memory(struct sh_tmu_device *tmu)
  366. {
  367. struct resource *res;
  368. res = platform_get_resource(tmu->pdev, IORESOURCE_MEM, 0);
  369. if (!res) {
  370. dev_err(&tmu->pdev->dev, "failed to get I/O memory\n");
  371. return -ENXIO;
  372. }
  373. tmu->mapbase = ioremap(res->start, resource_size(res));
  374. if (tmu->mapbase == NULL)
  375. return -ENXIO;
  376. return 0;
  377. }
  378. static int sh_tmu_parse_dt(struct sh_tmu_device *tmu)
  379. {
  380. struct device_node *np = tmu->pdev->dev.of_node;
  381. tmu->model = SH_TMU;
  382. tmu->num_channels = 3;
  383. of_property_read_u32(np, "#renesas,channels", &tmu->num_channels);
  384. if (tmu->num_channels != 2 && tmu->num_channels != 3) {
  385. dev_err(&tmu->pdev->dev, "invalid number of channels %u\n",
  386. tmu->num_channels);
  387. return -EINVAL;
  388. }
  389. return 0;
  390. }
  391. static int sh_tmu_setup(struct sh_tmu_device *tmu, struct platform_device *pdev)
  392. {
  393. unsigned int i;
  394. int ret;
  395. tmu->pdev = pdev;
  396. raw_spin_lock_init(&tmu->lock);
  397. if (IS_ENABLED(CONFIG_OF) && pdev->dev.of_node) {
  398. ret = sh_tmu_parse_dt(tmu);
  399. if (ret < 0)
  400. return ret;
  401. } else if (pdev->dev.platform_data) {
  402. const struct platform_device_id *id = pdev->id_entry;
  403. struct sh_timer_config *cfg = pdev->dev.platform_data;
  404. tmu->model = id->driver_data;
  405. tmu->num_channels = hweight8(cfg->channels_mask);
  406. } else {
  407. dev_err(&tmu->pdev->dev, "missing platform data\n");
  408. return -ENXIO;
  409. }
  410. /* Get hold of clock. */
  411. tmu->clk = clk_get(&tmu->pdev->dev, "fck");
  412. if (IS_ERR(tmu->clk)) {
  413. dev_err(&tmu->pdev->dev, "cannot get clock\n");
  414. return PTR_ERR(tmu->clk);
  415. }
  416. ret = clk_prepare(tmu->clk);
  417. if (ret < 0)
  418. goto err_clk_put;
  419. /* Determine clock rate. */
  420. ret = clk_enable(tmu->clk);
  421. if (ret < 0)
  422. goto err_clk_unprepare;
  423. tmu->rate = clk_get_rate(tmu->clk) / 4;
  424. /* Map the memory resource. */
  425. ret = sh_tmu_map_memory(tmu);
  426. if (ret < 0) {
  427. dev_err(&tmu->pdev->dev, "failed to remap I/O memory\n");
  428. goto err_clk_unprepare;
  429. }
  430. /* Allocate and setup the channels. */
  431. tmu->channels = kzalloc_objs(*tmu->channels, tmu->num_channels);
  432. if (tmu->channels == NULL) {
  433. ret = -ENOMEM;
  434. goto err_unmap;
  435. }
  436. /*
  437. * Use the first channel as a clock event device and the second channel
  438. * as a clock source.
  439. */
  440. for (i = 0; i < tmu->num_channels; ++i) {
  441. ret = sh_tmu_channel_setup(&tmu->channels[i], i,
  442. i == 0, i == 1, tmu);
  443. if (ret < 0)
  444. goto err_unmap;
  445. }
  446. platform_set_drvdata(pdev, tmu);
  447. return 0;
  448. err_unmap:
  449. kfree(tmu->channels);
  450. iounmap(tmu->mapbase);
  451. err_clk_unprepare:
  452. clk_unprepare(tmu->clk);
  453. err_clk_put:
  454. clk_put(tmu->clk);
  455. return ret;
  456. }
  457. static int sh_tmu_probe(struct platform_device *pdev)
  458. {
  459. struct sh_tmu_device *tmu = platform_get_drvdata(pdev);
  460. int ret;
  461. if (!is_sh_early_platform_device(pdev)) {
  462. pm_runtime_set_active(&pdev->dev);
  463. pm_runtime_enable(&pdev->dev);
  464. }
  465. if (tmu) {
  466. dev_info(&pdev->dev, "kept as earlytimer\n");
  467. goto out;
  468. }
  469. tmu = kzalloc_obj(*tmu);
  470. if (tmu == NULL)
  471. return -ENOMEM;
  472. ret = sh_tmu_setup(tmu, pdev);
  473. if (ret) {
  474. kfree(tmu);
  475. pm_runtime_idle(&pdev->dev);
  476. return ret;
  477. }
  478. if (is_sh_early_platform_device(pdev))
  479. return 0;
  480. out:
  481. if (tmu->has_clockevent || tmu->has_clocksource)
  482. pm_runtime_irq_safe(&pdev->dev);
  483. return 0;
  484. }
  485. static const struct platform_device_id sh_tmu_id_table[] = {
  486. { "sh-tmu", SH_TMU },
  487. { "sh-tmu-sh3", SH_TMU_SH3 },
  488. { }
  489. };
  490. MODULE_DEVICE_TABLE(platform, sh_tmu_id_table);
  491. static const struct of_device_id sh_tmu_of_table[] __maybe_unused = {
  492. { .compatible = "renesas,tmu" },
  493. { }
  494. };
  495. MODULE_DEVICE_TABLE(of, sh_tmu_of_table);
  496. static struct platform_driver sh_tmu_device_driver = {
  497. .probe = sh_tmu_probe,
  498. .driver = {
  499. .name = "sh_tmu",
  500. .of_match_table = of_match_ptr(sh_tmu_of_table),
  501. .suppress_bind_attrs = true,
  502. },
  503. .id_table = sh_tmu_id_table,
  504. };
  505. static int __init sh_tmu_init(void)
  506. {
  507. return platform_driver_register(&sh_tmu_device_driver);
  508. }
  509. static void __exit sh_tmu_exit(void)
  510. {
  511. platform_driver_unregister(&sh_tmu_device_driver);
  512. }
  513. #ifdef CONFIG_SUPERH
  514. sh_early_platform_init("earlytimer", &sh_tmu_device_driver);
  515. #endif
  516. subsys_initcall(sh_tmu_init);
  517. module_exit(sh_tmu_exit);
  518. MODULE_AUTHOR("Magnus Damm");
  519. MODULE_DESCRIPTION("SuperH TMU Timer Driver");