sh_tmu.c 11 KB

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