sh_tmu.c 12 KB

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