sh_mtu2.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. /*
  2. * SuperH Timer Support - MTU2
  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/clockchips.h>
  30. #include <linux/sh_timer.h>
  31. #include <linux/slab.h>
  32. #include <linux/module.h>
  33. #include <linux/pm_domain.h>
  34. struct sh_mtu2_priv {
  35. void __iomem *mapbase;
  36. struct clk *clk;
  37. struct irqaction irqaction;
  38. struct platform_device *pdev;
  39. unsigned long rate;
  40. unsigned long periodic;
  41. struct clock_event_device ced;
  42. };
  43. static DEFINE_SPINLOCK(sh_mtu2_lock);
  44. #define TSTR -1 /* shared register */
  45. #define TCR 0 /* channel register */
  46. #define TMDR 1 /* channel register */
  47. #define TIOR 2 /* channel register */
  48. #define TIER 3 /* channel register */
  49. #define TSR 4 /* channel register */
  50. #define TCNT 5 /* channel register */
  51. #define TGR 6 /* channel register */
  52. static unsigned long mtu2_reg_offs[] = {
  53. [TCR] = 0,
  54. [TMDR] = 1,
  55. [TIOR] = 2,
  56. [TIER] = 4,
  57. [TSR] = 5,
  58. [TCNT] = 6,
  59. [TGR] = 8,
  60. };
  61. static inline unsigned long sh_mtu2_read(struct sh_mtu2_priv *p, int reg_nr)
  62. {
  63. struct sh_timer_config *cfg = p->pdev->dev.platform_data;
  64. void __iomem *base = p->mapbase;
  65. unsigned long offs;
  66. if (reg_nr == TSTR)
  67. return ioread8(base + cfg->channel_offset);
  68. offs = mtu2_reg_offs[reg_nr];
  69. if ((reg_nr == TCNT) || (reg_nr == TGR))
  70. return ioread16(base + offs);
  71. else
  72. return ioread8(base + offs);
  73. }
  74. static inline void sh_mtu2_write(struct sh_mtu2_priv *p, int reg_nr,
  75. unsigned long value)
  76. {
  77. struct sh_timer_config *cfg = p->pdev->dev.platform_data;
  78. void __iomem *base = p->mapbase;
  79. unsigned long offs;
  80. if (reg_nr == TSTR) {
  81. iowrite8(value, base + cfg->channel_offset);
  82. return;
  83. }
  84. offs = mtu2_reg_offs[reg_nr];
  85. if ((reg_nr == TCNT) || (reg_nr == TGR))
  86. iowrite16(value, base + offs);
  87. else
  88. iowrite8(value, base + offs);
  89. }
  90. static void sh_mtu2_start_stop_ch(struct sh_mtu2_priv *p, int start)
  91. {
  92. struct sh_timer_config *cfg = p->pdev->dev.platform_data;
  93. unsigned long flags, value;
  94. /* start stop register shared by multiple timer channels */
  95. spin_lock_irqsave(&sh_mtu2_lock, flags);
  96. value = sh_mtu2_read(p, TSTR);
  97. if (start)
  98. value |= 1 << cfg->timer_bit;
  99. else
  100. value &= ~(1 << cfg->timer_bit);
  101. sh_mtu2_write(p, TSTR, value);
  102. spin_unlock_irqrestore(&sh_mtu2_lock, flags);
  103. }
  104. static int sh_mtu2_enable(struct sh_mtu2_priv *p)
  105. {
  106. int ret;
  107. /* enable clock */
  108. ret = clk_enable(p->clk);
  109. if (ret) {
  110. dev_err(&p->pdev->dev, "cannot enable clock\n");
  111. return ret;
  112. }
  113. /* make sure channel is disabled */
  114. sh_mtu2_start_stop_ch(p, 0);
  115. p->rate = clk_get_rate(p->clk) / 64;
  116. p->periodic = (p->rate + HZ/2) / HZ;
  117. /* "Periodic Counter Operation" */
  118. sh_mtu2_write(p, TCR, 0x23); /* TGRA clear, divide clock by 64 */
  119. sh_mtu2_write(p, TIOR, 0);
  120. sh_mtu2_write(p, TGR, p->periodic);
  121. sh_mtu2_write(p, TCNT, 0);
  122. sh_mtu2_write(p, TMDR, 0);
  123. sh_mtu2_write(p, TIER, 0x01);
  124. /* enable channel */
  125. sh_mtu2_start_stop_ch(p, 1);
  126. return 0;
  127. }
  128. static void sh_mtu2_disable(struct sh_mtu2_priv *p)
  129. {
  130. /* disable channel */
  131. sh_mtu2_start_stop_ch(p, 0);
  132. /* stop clock */
  133. clk_disable(p->clk);
  134. }
  135. static irqreturn_t sh_mtu2_interrupt(int irq, void *dev_id)
  136. {
  137. struct sh_mtu2_priv *p = dev_id;
  138. /* acknowledge interrupt */
  139. sh_mtu2_read(p, TSR);
  140. sh_mtu2_write(p, TSR, 0xfe);
  141. /* notify clockevent layer */
  142. p->ced.event_handler(&p->ced);
  143. return IRQ_HANDLED;
  144. }
  145. static struct sh_mtu2_priv *ced_to_sh_mtu2(struct clock_event_device *ced)
  146. {
  147. return container_of(ced, struct sh_mtu2_priv, ced);
  148. }
  149. static void sh_mtu2_clock_event_mode(enum clock_event_mode mode,
  150. struct clock_event_device *ced)
  151. {
  152. struct sh_mtu2_priv *p = ced_to_sh_mtu2(ced);
  153. int disabled = 0;
  154. /* deal with old setting first */
  155. switch (ced->mode) {
  156. case CLOCK_EVT_MODE_PERIODIC:
  157. sh_mtu2_disable(p);
  158. disabled = 1;
  159. break;
  160. default:
  161. break;
  162. }
  163. switch (mode) {
  164. case CLOCK_EVT_MODE_PERIODIC:
  165. dev_info(&p->pdev->dev, "used for periodic clock events\n");
  166. sh_mtu2_enable(p);
  167. break;
  168. case CLOCK_EVT_MODE_UNUSED:
  169. if (!disabled)
  170. sh_mtu2_disable(p);
  171. break;
  172. case CLOCK_EVT_MODE_SHUTDOWN:
  173. default:
  174. break;
  175. }
  176. }
  177. static void sh_mtu2_register_clockevent(struct sh_mtu2_priv *p,
  178. char *name, unsigned long rating)
  179. {
  180. struct clock_event_device *ced = &p->ced;
  181. int ret;
  182. memset(ced, 0, sizeof(*ced));
  183. ced->name = name;
  184. ced->features = CLOCK_EVT_FEAT_PERIODIC;
  185. ced->rating = rating;
  186. ced->cpumask = cpumask_of(0);
  187. ced->set_mode = sh_mtu2_clock_event_mode;
  188. dev_info(&p->pdev->dev, "used for clock events\n");
  189. clockevents_register_device(ced);
  190. ret = setup_irq(p->irqaction.irq, &p->irqaction);
  191. if (ret) {
  192. dev_err(&p->pdev->dev, "failed to request irq %d\n",
  193. p->irqaction.irq);
  194. return;
  195. }
  196. }
  197. static int sh_mtu2_register(struct sh_mtu2_priv *p, char *name,
  198. unsigned long clockevent_rating)
  199. {
  200. if (clockevent_rating)
  201. sh_mtu2_register_clockevent(p, name, clockevent_rating);
  202. return 0;
  203. }
  204. static int sh_mtu2_setup(struct sh_mtu2_priv *p, struct platform_device *pdev)
  205. {
  206. struct sh_timer_config *cfg = pdev->dev.platform_data;
  207. struct resource *res;
  208. int irq, ret;
  209. ret = -ENXIO;
  210. memset(p, 0, sizeof(*p));
  211. p->pdev = pdev;
  212. if (!cfg) {
  213. dev_err(&p->pdev->dev, "missing platform data\n");
  214. goto err0;
  215. }
  216. platform_set_drvdata(pdev, p);
  217. res = platform_get_resource(p->pdev, IORESOURCE_MEM, 0);
  218. if (!res) {
  219. dev_err(&p->pdev->dev, "failed to get I/O memory\n");
  220. goto err0;
  221. }
  222. irq = platform_get_irq(p->pdev, 0);
  223. if (irq < 0) {
  224. dev_err(&p->pdev->dev, "failed to get irq\n");
  225. goto err0;
  226. }
  227. /* map memory, let mapbase point to our channel */
  228. p->mapbase = ioremap_nocache(res->start, resource_size(res));
  229. if (p->mapbase == NULL) {
  230. dev_err(&p->pdev->dev, "failed to remap I/O memory\n");
  231. goto err0;
  232. }
  233. /* setup data for setup_irq() (too early for request_irq()) */
  234. p->irqaction.name = dev_name(&p->pdev->dev);
  235. p->irqaction.handler = sh_mtu2_interrupt;
  236. p->irqaction.dev_id = p;
  237. p->irqaction.irq = irq;
  238. p->irqaction.flags = IRQF_DISABLED | IRQF_TIMER | \
  239. IRQF_IRQPOLL | IRQF_NOBALANCING;
  240. /* get hold of clock */
  241. p->clk = clk_get(&p->pdev->dev, "mtu2_fck");
  242. if (IS_ERR(p->clk)) {
  243. dev_err(&p->pdev->dev, "cannot get clock\n");
  244. ret = PTR_ERR(p->clk);
  245. goto err1;
  246. }
  247. return sh_mtu2_register(p, (char *)dev_name(&p->pdev->dev),
  248. cfg->clockevent_rating);
  249. err1:
  250. iounmap(p->mapbase);
  251. err0:
  252. return ret;
  253. }
  254. static int __devinit sh_mtu2_probe(struct platform_device *pdev)
  255. {
  256. struct sh_mtu2_priv *p = platform_get_drvdata(pdev);
  257. int ret;
  258. if (!is_early_platform_device(pdev))
  259. pm_genpd_dev_always_on(&pdev->dev, true);
  260. if (p) {
  261. dev_info(&pdev->dev, "kept as earlytimer\n");
  262. return 0;
  263. }
  264. p = kmalloc(sizeof(*p), GFP_KERNEL);
  265. if (p == NULL) {
  266. dev_err(&pdev->dev, "failed to allocate driver data\n");
  267. return -ENOMEM;
  268. }
  269. ret = sh_mtu2_setup(p, pdev);
  270. if (ret) {
  271. kfree(p);
  272. platform_set_drvdata(pdev, NULL);
  273. }
  274. return ret;
  275. }
  276. static int __devexit sh_mtu2_remove(struct platform_device *pdev)
  277. {
  278. return -EBUSY; /* cannot unregister clockevent */
  279. }
  280. static struct platform_driver sh_mtu2_device_driver = {
  281. .probe = sh_mtu2_probe,
  282. .remove = __devexit_p(sh_mtu2_remove),
  283. .driver = {
  284. .name = "sh_mtu2",
  285. }
  286. };
  287. static int __init sh_mtu2_init(void)
  288. {
  289. return platform_driver_register(&sh_mtu2_device_driver);
  290. }
  291. static void __exit sh_mtu2_exit(void)
  292. {
  293. platform_driver_unregister(&sh_mtu2_device_driver);
  294. }
  295. early_platform_init("earlytimer", &sh_mtu2_device_driver);
  296. module_init(sh_mtu2_init);
  297. module_exit(sh_mtu2_exit);
  298. MODULE_AUTHOR("Magnus Damm");
  299. MODULE_DESCRIPTION("SuperH MTU2 Timer Driver");
  300. MODULE_LICENSE("GPL v2");