sh_mtu2.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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. struct sh_mtu2_priv {
  32. void __iomem *mapbase;
  33. struct clk *clk;
  34. struct irqaction irqaction;
  35. struct platform_device *pdev;
  36. unsigned long rate;
  37. unsigned long periodic;
  38. struct clock_event_device ced;
  39. };
  40. static DEFINE_SPINLOCK(sh_mtu2_lock);
  41. #define TSTR -1 /* shared register */
  42. #define TCR 0 /* channel register */
  43. #define TMDR 1 /* channel register */
  44. #define TIOR 2 /* channel register */
  45. #define TIER 3 /* channel register */
  46. #define TSR 4 /* channel register */
  47. #define TCNT 5 /* channel register */
  48. #define TGR 6 /* channel register */
  49. static unsigned long mtu2_reg_offs[] = {
  50. [TCR] = 0,
  51. [TMDR] = 1,
  52. [TIOR] = 2,
  53. [TIER] = 4,
  54. [TSR] = 5,
  55. [TCNT] = 6,
  56. [TGR] = 8,
  57. };
  58. static inline unsigned long sh_mtu2_read(struct sh_mtu2_priv *p, int reg_nr)
  59. {
  60. struct sh_timer_config *cfg = p->pdev->dev.platform_data;
  61. void __iomem *base = p->mapbase;
  62. unsigned long offs;
  63. if (reg_nr == TSTR)
  64. return ioread8(base + cfg->channel_offset);
  65. offs = mtu2_reg_offs[reg_nr];
  66. if ((reg_nr == TCNT) || (reg_nr == TGR))
  67. return ioread16(base + offs);
  68. else
  69. return ioread8(base + offs);
  70. }
  71. static inline void sh_mtu2_write(struct sh_mtu2_priv *p, int reg_nr,
  72. unsigned long value)
  73. {
  74. struct sh_timer_config *cfg = p->pdev->dev.platform_data;
  75. void __iomem *base = p->mapbase;
  76. unsigned long offs;
  77. if (reg_nr == TSTR) {
  78. iowrite8(value, base + cfg->channel_offset);
  79. return;
  80. }
  81. offs = mtu2_reg_offs[reg_nr];
  82. if ((reg_nr == TCNT) || (reg_nr == TGR))
  83. iowrite16(value, base + offs);
  84. else
  85. iowrite8(value, base + offs);
  86. }
  87. static void sh_mtu2_start_stop_ch(struct sh_mtu2_priv *p, int start)
  88. {
  89. struct sh_timer_config *cfg = p->pdev->dev.platform_data;
  90. unsigned long flags, value;
  91. /* start stop register shared by multiple timer channels */
  92. spin_lock_irqsave(&sh_mtu2_lock, flags);
  93. value = sh_mtu2_read(p, TSTR);
  94. if (start)
  95. value |= 1 << cfg->timer_bit;
  96. else
  97. value &= ~(1 << cfg->timer_bit);
  98. sh_mtu2_write(p, TSTR, value);
  99. spin_unlock_irqrestore(&sh_mtu2_lock, flags);
  100. }
  101. static int sh_mtu2_enable(struct sh_mtu2_priv *p)
  102. {
  103. struct sh_timer_config *cfg = p->pdev->dev.platform_data;
  104. int ret;
  105. /* enable clock */
  106. ret = clk_enable(p->clk);
  107. if (ret) {
  108. pr_err("sh_mtu2: cannot enable clock \"%s\"\n", cfg->clk);
  109. return ret;
  110. }
  111. /* make sure channel is disabled */
  112. sh_mtu2_start_stop_ch(p, 0);
  113. p->rate = clk_get_rate(p->clk) / 64;
  114. p->periodic = (p->rate + HZ/2) / HZ;
  115. /* "Periodic Counter Operation" */
  116. sh_mtu2_write(p, TCR, 0x23); /* TGRA clear, divide clock by 64 */
  117. sh_mtu2_write(p, TIOR, 0);
  118. sh_mtu2_write(p, TGR, p->periodic);
  119. sh_mtu2_write(p, TCNT, 0);
  120. sh_mtu2_write(p, TMDR, 0);
  121. sh_mtu2_write(p, TIER, 0x01);
  122. /* enable channel */
  123. sh_mtu2_start_stop_ch(p, 1);
  124. return 0;
  125. }
  126. static void sh_mtu2_disable(struct sh_mtu2_priv *p)
  127. {
  128. /* disable channel */
  129. sh_mtu2_start_stop_ch(p, 0);
  130. /* stop clock */
  131. clk_disable(p->clk);
  132. }
  133. static irqreturn_t sh_mtu2_interrupt(int irq, void *dev_id)
  134. {
  135. struct sh_mtu2_priv *p = dev_id;
  136. /* acknowledge interrupt */
  137. sh_mtu2_read(p, TSR);
  138. sh_mtu2_write(p, TSR, 0xfe);
  139. /* notify clockevent layer */
  140. p->ced.event_handler(&p->ced);
  141. return IRQ_HANDLED;
  142. }
  143. static struct sh_mtu2_priv *ced_to_sh_mtu2(struct clock_event_device *ced)
  144. {
  145. return container_of(ced, struct sh_mtu2_priv, ced);
  146. }
  147. static void sh_mtu2_clock_event_mode(enum clock_event_mode mode,
  148. struct clock_event_device *ced)
  149. {
  150. struct sh_mtu2_priv *p = ced_to_sh_mtu2(ced);
  151. int disabled = 0;
  152. /* deal with old setting first */
  153. switch (ced->mode) {
  154. case CLOCK_EVT_MODE_PERIODIC:
  155. sh_mtu2_disable(p);
  156. disabled = 1;
  157. break;
  158. default:
  159. break;
  160. }
  161. switch (mode) {
  162. case CLOCK_EVT_MODE_PERIODIC:
  163. pr_info("sh_mtu2: %s used for periodic clock events\n",
  164. ced->name);
  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. ret = setup_irq(p->irqaction.irq, &p->irqaction);
  188. if (ret) {
  189. pr_err("sh_mtu2: failed to request irq %d\n",
  190. p->irqaction.irq);
  191. return;
  192. }
  193. pr_info("sh_mtu2: %s used for clock events\n", ced->name);
  194. clockevents_register_device(ced);
  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. pr_err("sh_mtu2: 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 = cfg->name;
  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 | IRQF_IRQPOLL;
  238. /* get hold of clock */
  239. p->clk = clk_get(&p->pdev->dev, cfg->clk);
  240. if (IS_ERR(p->clk)) {
  241. pr_err("sh_mtu2: cannot get clock \"%s\"\n", cfg->clk);
  242. ret = PTR_ERR(p->clk);
  243. goto err1;
  244. }
  245. return sh_mtu2_register(p, cfg->name, cfg->clockevent_rating);
  246. err1:
  247. iounmap(p->mapbase);
  248. err0:
  249. return ret;
  250. }
  251. static int __devinit sh_mtu2_probe(struct platform_device *pdev)
  252. {
  253. struct sh_mtu2_priv *p = platform_get_drvdata(pdev);
  254. struct sh_timer_config *cfg = pdev->dev.platform_data;
  255. int ret;
  256. if (p) {
  257. pr_info("sh_mtu2: %s kept as earlytimer\n", cfg->name);
  258. return 0;
  259. }
  260. p = kmalloc(sizeof(*p), GFP_KERNEL);
  261. if (p == NULL) {
  262. dev_err(&pdev->dev, "failed to allocate driver data\n");
  263. return -ENOMEM;
  264. }
  265. ret = sh_mtu2_setup(p, pdev);
  266. if (ret) {
  267. kfree(p);
  268. platform_set_drvdata(pdev, NULL);
  269. }
  270. return ret;
  271. }
  272. static int __devexit sh_mtu2_remove(struct platform_device *pdev)
  273. {
  274. return -EBUSY; /* cannot unregister clockevent */
  275. }
  276. static struct platform_driver sh_mtu2_device_driver = {
  277. .probe = sh_mtu2_probe,
  278. .remove = __devexit_p(sh_mtu2_remove),
  279. .driver = {
  280. .name = "sh_mtu2",
  281. }
  282. };
  283. static int __init sh_mtu2_init(void)
  284. {
  285. return platform_driver_register(&sh_mtu2_device_driver);
  286. }
  287. static void __exit sh_mtu2_exit(void)
  288. {
  289. platform_driver_unregister(&sh_mtu2_device_driver);
  290. }
  291. early_platform_init("earlytimer", &sh_mtu2_device_driver);
  292. module_init(sh_mtu2_init);
  293. module_exit(sh_mtu2_exit);
  294. MODULE_AUTHOR("Magnus Damm");
  295. MODULE_DESCRIPTION("SuperH MTU2 Timer Driver");
  296. MODULE_LICENSE("GPL v2");