sh_mtu2.c 8.2 KB

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