sh_tmu.c 11 KB

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