sh_tmu.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562
  1. /*
  2. * SuperH Timer Support - TMU
  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. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. #include <linux/init.h>
  20. #include <linux/platform_device.h>
  21. #include <linux/spinlock.h>
  22. #include <linux/interrupt.h>
  23. #include <linux/ioport.h>
  24. #include <linux/delay.h>
  25. #include <linux/io.h>
  26. #include <linux/clk.h>
  27. #include <linux/irq.h>
  28. #include <linux/err.h>
  29. #include <linux/clocksource.h>
  30. #include <linux/clockchips.h>
  31. #include <linux/sh_timer.h>
  32. #include <linux/slab.h>
  33. #include <linux/module.h>
  34. #include <linux/pm_domain.h>
  35. #include <linux/pm_runtime.h>
  36. struct sh_tmu_priv {
  37. void __iomem *mapbase;
  38. struct clk *clk;
  39. struct irqaction irqaction;
  40. struct platform_device *pdev;
  41. unsigned long rate;
  42. unsigned long periodic;
  43. struct clock_event_device ced;
  44. struct clocksource cs;
  45. bool cs_enabled;
  46. unsigned int enable_count;
  47. };
  48. static DEFINE_SPINLOCK(sh_tmu_lock);
  49. #define TSTR -1 /* shared register */
  50. #define TCOR 0 /* channel register */
  51. #define TCNT 1 /* channel register */
  52. #define TCR 2 /* channel register */
  53. static inline unsigned long sh_tmu_read(struct sh_tmu_priv *p, int reg_nr)
  54. {
  55. struct sh_timer_config *cfg = p->pdev->dev.platform_data;
  56. void __iomem *base = p->mapbase;
  57. unsigned long offs;
  58. if (reg_nr == TSTR)
  59. return ioread8(base - cfg->channel_offset);
  60. offs = reg_nr << 2;
  61. if (reg_nr == TCR)
  62. return ioread16(base + offs);
  63. else
  64. return ioread32(base + offs);
  65. }
  66. static inline void sh_tmu_write(struct sh_tmu_priv *p, int reg_nr,
  67. unsigned long value)
  68. {
  69. struct sh_timer_config *cfg = p->pdev->dev.platform_data;
  70. void __iomem *base = p->mapbase;
  71. unsigned long offs;
  72. if (reg_nr == TSTR) {
  73. iowrite8(value, base - cfg->channel_offset);
  74. return;
  75. }
  76. offs = reg_nr << 2;
  77. if (reg_nr == TCR)
  78. iowrite16(value, base + offs);
  79. else
  80. iowrite32(value, base + offs);
  81. }
  82. static void sh_tmu_start_stop_ch(struct sh_tmu_priv *p, int start)
  83. {
  84. struct sh_timer_config *cfg = p->pdev->dev.platform_data;
  85. unsigned long flags, value;
  86. /* start stop register shared by multiple timer channels */
  87. spin_lock_irqsave(&sh_tmu_lock, flags);
  88. value = sh_tmu_read(p, TSTR);
  89. if (start)
  90. value |= 1 << cfg->timer_bit;
  91. else
  92. value &= ~(1 << cfg->timer_bit);
  93. sh_tmu_write(p, TSTR, value);
  94. spin_unlock_irqrestore(&sh_tmu_lock, flags);
  95. }
  96. static int __sh_tmu_enable(struct sh_tmu_priv *p)
  97. {
  98. int ret;
  99. /* enable clock */
  100. ret = clk_enable(p->clk);
  101. if (ret) {
  102. dev_err(&p->pdev->dev, "cannot enable clock\n");
  103. return ret;
  104. }
  105. /* make sure channel is disabled */
  106. sh_tmu_start_stop_ch(p, 0);
  107. /* maximum timeout */
  108. sh_tmu_write(p, TCOR, 0xffffffff);
  109. sh_tmu_write(p, TCNT, 0xffffffff);
  110. /* configure channel to parent clock / 4, irq off */
  111. p->rate = clk_get_rate(p->clk) / 4;
  112. sh_tmu_write(p, TCR, 0x0000);
  113. /* enable channel */
  114. sh_tmu_start_stop_ch(p, 1);
  115. return 0;
  116. }
  117. static int sh_tmu_enable(struct sh_tmu_priv *p)
  118. {
  119. if (p->enable_count++ > 0)
  120. return 0;
  121. pm_runtime_get_sync(&p->pdev->dev);
  122. dev_pm_syscore_device(&p->pdev->dev, true);
  123. return __sh_tmu_enable(p);
  124. }
  125. static void __sh_tmu_disable(struct sh_tmu_priv *p)
  126. {
  127. /* disable channel */
  128. sh_tmu_start_stop_ch(p, 0);
  129. /* disable interrupts in TMU block */
  130. sh_tmu_write(p, TCR, 0x0000);
  131. /* stop clock */
  132. clk_disable(p->clk);
  133. }
  134. static void sh_tmu_disable(struct sh_tmu_priv *p)
  135. {
  136. if (WARN_ON(p->enable_count == 0))
  137. return;
  138. if (--p->enable_count > 0)
  139. return;
  140. __sh_tmu_disable(p);
  141. dev_pm_syscore_device(&p->pdev->dev, false);
  142. pm_runtime_put(&p->pdev->dev);
  143. }
  144. static void sh_tmu_set_next(struct sh_tmu_priv *p, unsigned long delta,
  145. int periodic)
  146. {
  147. /* stop timer */
  148. sh_tmu_start_stop_ch(p, 0);
  149. /* acknowledge interrupt */
  150. sh_tmu_read(p, TCR);
  151. /* enable interrupt */
  152. sh_tmu_write(p, TCR, 0x0020);
  153. /* reload delta value in case of periodic timer */
  154. if (periodic)
  155. sh_tmu_write(p, TCOR, delta);
  156. else
  157. sh_tmu_write(p, TCOR, 0xffffffff);
  158. sh_tmu_write(p, TCNT, delta);
  159. /* start timer */
  160. sh_tmu_start_stop_ch(p, 1);
  161. }
  162. static irqreturn_t sh_tmu_interrupt(int irq, void *dev_id)
  163. {
  164. struct sh_tmu_priv *p = dev_id;
  165. /* disable or acknowledge interrupt */
  166. if (p->ced.mode == CLOCK_EVT_MODE_ONESHOT)
  167. sh_tmu_write(p, TCR, 0x0000);
  168. else
  169. sh_tmu_write(p, TCR, 0x0020);
  170. /* notify clockevent layer */
  171. p->ced.event_handler(&p->ced);
  172. return IRQ_HANDLED;
  173. }
  174. static struct sh_tmu_priv *cs_to_sh_tmu(struct clocksource *cs)
  175. {
  176. return container_of(cs, struct sh_tmu_priv, cs);
  177. }
  178. static cycle_t sh_tmu_clocksource_read(struct clocksource *cs)
  179. {
  180. struct sh_tmu_priv *p = cs_to_sh_tmu(cs);
  181. return sh_tmu_read(p, TCNT) ^ 0xffffffff;
  182. }
  183. static int sh_tmu_clocksource_enable(struct clocksource *cs)
  184. {
  185. struct sh_tmu_priv *p = cs_to_sh_tmu(cs);
  186. int ret;
  187. if (WARN_ON(p->cs_enabled))
  188. return 0;
  189. ret = sh_tmu_enable(p);
  190. if (!ret) {
  191. __clocksource_updatefreq_hz(cs, p->rate);
  192. p->cs_enabled = true;
  193. }
  194. return ret;
  195. }
  196. static void sh_tmu_clocksource_disable(struct clocksource *cs)
  197. {
  198. struct sh_tmu_priv *p = cs_to_sh_tmu(cs);
  199. if (WARN_ON(!p->cs_enabled))
  200. return;
  201. sh_tmu_disable(p);
  202. p->cs_enabled = false;
  203. }
  204. static void sh_tmu_clocksource_suspend(struct clocksource *cs)
  205. {
  206. struct sh_tmu_priv *p = cs_to_sh_tmu(cs);
  207. if (!p->cs_enabled)
  208. return;
  209. if (--p->enable_count == 0) {
  210. __sh_tmu_disable(p);
  211. pm_genpd_syscore_poweroff(&p->pdev->dev);
  212. }
  213. }
  214. static void sh_tmu_clocksource_resume(struct clocksource *cs)
  215. {
  216. struct sh_tmu_priv *p = cs_to_sh_tmu(cs);
  217. if (!p->cs_enabled)
  218. return;
  219. if (p->enable_count++ == 0) {
  220. pm_genpd_syscore_poweron(&p->pdev->dev);
  221. __sh_tmu_enable(p);
  222. }
  223. }
  224. static int sh_tmu_register_clocksource(struct sh_tmu_priv *p,
  225. char *name, unsigned long rating)
  226. {
  227. struct clocksource *cs = &p->cs;
  228. cs->name = name;
  229. cs->rating = rating;
  230. cs->read = sh_tmu_clocksource_read;
  231. cs->enable = sh_tmu_clocksource_enable;
  232. cs->disable = sh_tmu_clocksource_disable;
  233. cs->suspend = sh_tmu_clocksource_suspend;
  234. cs->resume = sh_tmu_clocksource_resume;
  235. cs->mask = CLOCKSOURCE_MASK(32);
  236. cs->flags = CLOCK_SOURCE_IS_CONTINUOUS;
  237. dev_info(&p->pdev->dev, "used as clock source\n");
  238. /* Register with dummy 1 Hz value, gets updated in ->enable() */
  239. clocksource_register_hz(cs, 1);
  240. return 0;
  241. }
  242. static struct sh_tmu_priv *ced_to_sh_tmu(struct clock_event_device *ced)
  243. {
  244. return container_of(ced, struct sh_tmu_priv, ced);
  245. }
  246. static void sh_tmu_clock_event_start(struct sh_tmu_priv *p, int periodic)
  247. {
  248. struct clock_event_device *ced = &p->ced;
  249. sh_tmu_enable(p);
  250. /* TODO: calculate good shift from rate and counter bit width */
  251. ced->shift = 32;
  252. ced->mult = div_sc(p->rate, NSEC_PER_SEC, ced->shift);
  253. ced->max_delta_ns = clockevent_delta2ns(0xffffffff, ced);
  254. ced->min_delta_ns = 5000;
  255. if (periodic) {
  256. p->periodic = (p->rate + HZ/2) / HZ;
  257. sh_tmu_set_next(p, p->periodic, 1);
  258. }
  259. }
  260. static void sh_tmu_clock_event_mode(enum clock_event_mode mode,
  261. struct clock_event_device *ced)
  262. {
  263. struct sh_tmu_priv *p = ced_to_sh_tmu(ced);
  264. int disabled = 0;
  265. /* deal with old setting first */
  266. switch (ced->mode) {
  267. case CLOCK_EVT_MODE_PERIODIC:
  268. case CLOCK_EVT_MODE_ONESHOT:
  269. sh_tmu_disable(p);
  270. disabled = 1;
  271. break;
  272. default:
  273. break;
  274. }
  275. switch (mode) {
  276. case CLOCK_EVT_MODE_PERIODIC:
  277. dev_info(&p->pdev->dev, "used for periodic clock events\n");
  278. sh_tmu_clock_event_start(p, 1);
  279. break;
  280. case CLOCK_EVT_MODE_ONESHOT:
  281. dev_info(&p->pdev->dev, "used for oneshot clock events\n");
  282. sh_tmu_clock_event_start(p, 0);
  283. break;
  284. case CLOCK_EVT_MODE_UNUSED:
  285. if (!disabled)
  286. sh_tmu_disable(p);
  287. break;
  288. case CLOCK_EVT_MODE_SHUTDOWN:
  289. default:
  290. break;
  291. }
  292. }
  293. static int sh_tmu_clock_event_next(unsigned long delta,
  294. struct clock_event_device *ced)
  295. {
  296. struct sh_tmu_priv *p = ced_to_sh_tmu(ced);
  297. BUG_ON(ced->mode != CLOCK_EVT_MODE_ONESHOT);
  298. /* program new delta value */
  299. sh_tmu_set_next(p, delta, 0);
  300. return 0;
  301. }
  302. static void sh_tmu_clock_event_suspend(struct clock_event_device *ced)
  303. {
  304. pm_genpd_syscore_poweroff(&ced_to_sh_tmu(ced)->pdev->dev);
  305. }
  306. static void sh_tmu_clock_event_resume(struct clock_event_device *ced)
  307. {
  308. pm_genpd_syscore_poweron(&ced_to_sh_tmu(ced)->pdev->dev);
  309. }
  310. static void sh_tmu_register_clockevent(struct sh_tmu_priv *p,
  311. char *name, unsigned long rating)
  312. {
  313. struct clock_event_device *ced = &p->ced;
  314. int ret;
  315. memset(ced, 0, sizeof(*ced));
  316. ced->name = name;
  317. ced->features = CLOCK_EVT_FEAT_PERIODIC;
  318. ced->features |= CLOCK_EVT_FEAT_ONESHOT;
  319. ced->rating = rating;
  320. ced->cpumask = cpumask_of(0);
  321. ced->set_next_event = sh_tmu_clock_event_next;
  322. ced->set_mode = sh_tmu_clock_event_mode;
  323. ced->suspend = sh_tmu_clock_event_suspend;
  324. ced->resume = sh_tmu_clock_event_resume;
  325. dev_info(&p->pdev->dev, "used for clock events\n");
  326. clockevents_register_device(ced);
  327. ret = setup_irq(p->irqaction.irq, &p->irqaction);
  328. if (ret) {
  329. dev_err(&p->pdev->dev, "failed to request irq %d\n",
  330. p->irqaction.irq);
  331. return;
  332. }
  333. }
  334. static int sh_tmu_register(struct sh_tmu_priv *p, char *name,
  335. unsigned long clockevent_rating,
  336. unsigned long clocksource_rating)
  337. {
  338. if (clockevent_rating)
  339. sh_tmu_register_clockevent(p, name, clockevent_rating);
  340. else if (clocksource_rating)
  341. sh_tmu_register_clocksource(p, name, clocksource_rating);
  342. return 0;
  343. }
  344. static int sh_tmu_setup(struct sh_tmu_priv *p, struct platform_device *pdev)
  345. {
  346. struct sh_timer_config *cfg = pdev->dev.platform_data;
  347. struct resource *res;
  348. int irq, ret;
  349. ret = -ENXIO;
  350. memset(p, 0, sizeof(*p));
  351. p->pdev = pdev;
  352. if (!cfg) {
  353. dev_err(&p->pdev->dev, "missing platform data\n");
  354. goto err0;
  355. }
  356. platform_set_drvdata(pdev, p);
  357. res = platform_get_resource(p->pdev, IORESOURCE_MEM, 0);
  358. if (!res) {
  359. dev_err(&p->pdev->dev, "failed to get I/O memory\n");
  360. goto err0;
  361. }
  362. irq = platform_get_irq(p->pdev, 0);
  363. if (irq < 0) {
  364. dev_err(&p->pdev->dev, "failed to get irq\n");
  365. goto err0;
  366. }
  367. /* map memory, let mapbase point to our channel */
  368. p->mapbase = ioremap_nocache(res->start, resource_size(res));
  369. if (p->mapbase == NULL) {
  370. dev_err(&p->pdev->dev, "failed to remap I/O memory\n");
  371. goto err0;
  372. }
  373. /* setup data for setup_irq() (too early for request_irq()) */
  374. p->irqaction.name = dev_name(&p->pdev->dev);
  375. p->irqaction.handler = sh_tmu_interrupt;
  376. p->irqaction.dev_id = p;
  377. p->irqaction.irq = irq;
  378. p->irqaction.flags = IRQF_DISABLED | IRQF_TIMER | \
  379. IRQF_IRQPOLL | IRQF_NOBALANCING;
  380. /* get hold of clock */
  381. p->clk = clk_get(&p->pdev->dev, "tmu_fck");
  382. if (IS_ERR(p->clk)) {
  383. dev_err(&p->pdev->dev, "cannot get clock\n");
  384. ret = PTR_ERR(p->clk);
  385. goto err1;
  386. }
  387. p->cs_enabled = false;
  388. p->enable_count = 0;
  389. return sh_tmu_register(p, (char *)dev_name(&p->pdev->dev),
  390. cfg->clockevent_rating,
  391. cfg->clocksource_rating);
  392. err1:
  393. iounmap(p->mapbase);
  394. err0:
  395. return ret;
  396. }
  397. static int __devinit sh_tmu_probe(struct platform_device *pdev)
  398. {
  399. struct sh_tmu_priv *p = platform_get_drvdata(pdev);
  400. struct sh_timer_config *cfg = pdev->dev.platform_data;
  401. int ret;
  402. if (!is_early_platform_device(pdev)) {
  403. pm_runtime_set_active(&pdev->dev);
  404. pm_runtime_enable(&pdev->dev);
  405. }
  406. if (p) {
  407. dev_info(&pdev->dev, "kept as earlytimer\n");
  408. goto out;
  409. }
  410. p = kmalloc(sizeof(*p), GFP_KERNEL);
  411. if (p == NULL) {
  412. dev_err(&pdev->dev, "failed to allocate driver data\n");
  413. return -ENOMEM;
  414. }
  415. ret = sh_tmu_setup(p, pdev);
  416. if (ret) {
  417. kfree(p);
  418. platform_set_drvdata(pdev, NULL);
  419. pm_runtime_idle(&pdev->dev);
  420. return ret;
  421. }
  422. if (is_early_platform_device(pdev))
  423. return 0;
  424. out:
  425. if (cfg->clockevent_rating || cfg->clocksource_rating)
  426. pm_runtime_irq_safe(&pdev->dev);
  427. else
  428. pm_runtime_idle(&pdev->dev);
  429. return 0;
  430. }
  431. static int __devexit sh_tmu_remove(struct platform_device *pdev)
  432. {
  433. return -EBUSY; /* cannot unregister clockevent and clocksource */
  434. }
  435. static struct platform_driver sh_tmu_device_driver = {
  436. .probe = sh_tmu_probe,
  437. .remove = __devexit_p(sh_tmu_remove),
  438. .driver = {
  439. .name = "sh_tmu",
  440. }
  441. };
  442. static int __init sh_tmu_init(void)
  443. {
  444. return platform_driver_register(&sh_tmu_device_driver);
  445. }
  446. static void __exit sh_tmu_exit(void)
  447. {
  448. platform_driver_unregister(&sh_tmu_device_driver);
  449. }
  450. early_platform_init("earlytimer", &sh_tmu_device_driver);
  451. module_init(sh_tmu_init);
  452. module_exit(sh_tmu_exit);
  453. MODULE_AUTHOR("Magnus Damm");
  454. MODULE_DESCRIPTION("SuperH TMU Timer Driver");
  455. MODULE_LICENSE("GPL v2");