0130-MIPS-ralink-add-support-for-periodic-timer-irq.patch 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. From f8496dd6c8fbcfd159390c31791fe2a86e48acb9 Mon Sep 17 00:00:00 2001
  2. From: John Crispin <[email protected]>
  3. Date: Sat, 23 Mar 2013 19:44:41 +0100
  4. Subject: [PATCH 130/164] MIPS: ralink: add support for periodic timer irq
  5. Adds a driver for the periodic timer found on Ralink SoC.
  6. Signed-off-by: John Crispin <[email protected]>
  7. ---
  8. arch/mips/ralink/Makefile | 2 +-
  9. arch/mips/ralink/timer.c | 192 +++++++++++++++++++++++++++++++++++++++++++++
  10. 2 files changed, 193 insertions(+), 1 deletion(-)
  11. create mode 100644 arch/mips/ralink/timer.c
  12. --- a/arch/mips/ralink/Makefile
  13. +++ b/arch/mips/ralink/Makefile
  14. @@ -6,7 +6,7 @@
  15. # Copyright (C) 2009-2011 Gabor Juhos <[email protected]>
  16. # Copyright (C) 2013 John Crispin <[email protected]>
  17. -obj-y := prom.o of.o reset.o clk.o irq.o pinmux.o
  18. +obj-y := prom.o of.o reset.o clk.o irq.o pinmux.o timer.o
  19. obj-$(CONFIG_SOC_RT288X) += rt288x.o
  20. obj-$(CONFIG_SOC_RT305X) += rt305x.o
  21. --- /dev/null
  22. +++ b/arch/mips/ralink/timer.c
  23. @@ -0,0 +1,192 @@
  24. +/*
  25. + * This program is free software; you can redistribute it and/or modify it
  26. + * under the terms of the GNU General Public License version 2 as published
  27. + * by the Free Software Foundation.
  28. + *
  29. + * Copyright (C) 2013 John Crispin <[email protected]>
  30. +*/
  31. +
  32. +#include <linux/module.h>
  33. +#include <linux/platform_device.h>
  34. +#include <linux/interrupt.h>
  35. +#include <linux/timer.h>
  36. +#include <linux/of_gpio.h>
  37. +#include <linux/clk.h>
  38. +
  39. +#include <asm/mach-ralink/ralink_regs.h>
  40. +
  41. +#define TIMER_REG_TMRSTAT 0x00
  42. +#define TIMER_REG_TMR0LOAD 0x10
  43. +#define TIMER_REG_TMR0CTL 0x18
  44. +
  45. +#define TMRSTAT_TMR0INT BIT(0)
  46. +
  47. +#define TMR0CTL_ENABLE BIT(7)
  48. +#define TMR0CTL_MODE_PERIODIC BIT(4)
  49. +#define TMR0CTL_PRESCALER 1
  50. +#define TMR0CTL_PRESCALE_VAL (0xf - TMR0CTL_PRESCALER)
  51. +#define TMR0CTL_PRESCALE_DIV (65536 / BIT(TMR0CTL_PRESCALER))
  52. +
  53. +struct rt_timer {
  54. + struct device *dev;
  55. + void __iomem *membase;
  56. + int irq;
  57. + unsigned long timer_freq;
  58. + unsigned long timer_div;
  59. +};
  60. +
  61. +static inline void rt_timer_w32(struct rt_timer *rt, u8 reg, u32 val)
  62. +{
  63. + __raw_writel(val, rt->membase + reg);
  64. +}
  65. +
  66. +static inline u32 rt_timer_r32(struct rt_timer *rt, u8 reg)
  67. +{
  68. + return __raw_readl(rt->membase + reg);
  69. +}
  70. +
  71. +static irqreturn_t rt_timer_irq(int irq, void *_rt)
  72. +{
  73. + struct rt_timer *rt = (struct rt_timer *) _rt;
  74. +
  75. + rt_timer_w32(rt, TIMER_REG_TMR0LOAD, rt->timer_freq / rt->timer_div);
  76. + rt_timer_w32(rt, TIMER_REG_TMRSTAT, TMRSTAT_TMR0INT);
  77. +
  78. + return IRQ_HANDLED;
  79. +}
  80. +
  81. +
  82. +static int rt_timer_request(struct rt_timer *rt)
  83. +{
  84. + int err = request_irq(rt->irq, rt_timer_irq, IRQF_DISABLED,
  85. + dev_name(rt->dev), rt);
  86. + if (err) {
  87. + dev_err(rt->dev, "failed to request irq\n");
  88. + } else {
  89. + u32 t = TMR0CTL_MODE_PERIODIC | TMR0CTL_PRESCALE_VAL;
  90. + rt_timer_w32(rt, TIMER_REG_TMR0CTL, t);
  91. + }
  92. + return err;
  93. +}
  94. +
  95. +static void rt_timer_free(struct rt_timer *rt)
  96. +{
  97. + free_irq(rt->irq, rt);
  98. +}
  99. +
  100. +static int rt_timer_config(struct rt_timer *rt, unsigned long divisor)
  101. +{
  102. + if (rt->timer_freq < divisor)
  103. + rt->timer_div = rt->timer_freq;
  104. + else
  105. + rt->timer_div = divisor;
  106. +
  107. + rt_timer_w32(rt, TIMER_REG_TMR0LOAD, rt->timer_freq / rt->timer_div);
  108. +
  109. + return 0;
  110. +}
  111. +
  112. +static int rt_timer_enable(struct rt_timer *rt)
  113. +{
  114. + u32 t;
  115. +
  116. + rt_timer_w32(rt, TIMER_REG_TMR0LOAD, rt->timer_freq / rt->timer_div);
  117. +
  118. + t = rt_timer_r32(rt, TIMER_REG_TMR0CTL);
  119. + t |= TMR0CTL_ENABLE;
  120. + rt_timer_w32(rt, TIMER_REG_TMR0CTL, t);
  121. +
  122. + return 0;
  123. +}
  124. +
  125. +static void rt_timer_disable(struct rt_timer *rt)
  126. +{
  127. + u32 t;
  128. +
  129. + t = rt_timer_r32(rt, TIMER_REG_TMR0CTL);
  130. + t &= ~TMR0CTL_ENABLE;
  131. + rt_timer_w32(rt, TIMER_REG_TMR0CTL, t);
  132. +}
  133. +
  134. +static int rt_timer_probe(struct platform_device *pdev)
  135. +{
  136. + struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  137. + struct rt_timer *rt;
  138. + struct clk *clk;
  139. +
  140. + if (!res) {
  141. + dev_err(&pdev->dev, "no memory resource found\n");
  142. + return -EINVAL;
  143. + }
  144. +
  145. + rt = devm_kzalloc(&pdev->dev, sizeof(*rt), GFP_KERNEL);
  146. + if (!rt) {
  147. + dev_err(&pdev->dev, "failed to allocate memory\n");
  148. + return -ENOMEM;
  149. + }
  150. +
  151. + rt->irq = platform_get_irq(pdev, 0);
  152. + if (!rt->irq) {
  153. + dev_err(&pdev->dev, "failed to load irq\n");
  154. + return -ENOENT;
  155. + }
  156. +
  157. + rt->membase = devm_request_and_ioremap(&pdev->dev, res);
  158. + if (!rt->membase) {
  159. + dev_err(&pdev->dev, "failed to ioremap\n");
  160. + return -ENOMEM;
  161. + }
  162. +
  163. + clk = devm_clk_get(&pdev->dev, NULL);
  164. + if (IS_ERR(clk)) {
  165. + dev_err(&pdev->dev, "failed get clock rate\n");
  166. + return PTR_ERR(clk);
  167. + }
  168. +
  169. + rt->timer_freq = clk_get_rate(clk) / TMR0CTL_PRESCALE_DIV;
  170. + if (!rt->timer_freq)
  171. + return -EINVAL;
  172. +
  173. + rt->dev = &pdev->dev;
  174. + platform_set_drvdata(pdev, rt);
  175. +
  176. + rt_timer_request(rt);
  177. + rt_timer_config(rt, 2);
  178. + rt_timer_enable(rt);
  179. +
  180. + dev_info(&pdev->dev, "maximum frequncy is %luHz\n", rt->timer_freq);
  181. +
  182. + return 0;
  183. +}
  184. +
  185. +static int rt_timer_remove(struct platform_device *pdev)
  186. +{
  187. + struct rt_timer *rt = platform_get_drvdata(pdev);
  188. +
  189. + rt_timer_disable(rt);
  190. + rt_timer_free(rt);
  191. +
  192. + return 0;
  193. +}
  194. +
  195. +static const struct of_device_id rt_timer_match[] = {
  196. + { .compatible = "ralink,rt2880-timer" },
  197. + {},
  198. +};
  199. +MODULE_DEVICE_TABLE(of, rt_timer_match);
  200. +
  201. +static struct platform_driver rt_timer_driver = {
  202. + .probe = rt_timer_probe,
  203. + .remove = rt_timer_remove,
  204. + .driver = {
  205. + .name = "rt-timer",
  206. + .owner = THIS_MODULE,
  207. + .of_match_table = rt_timer_match
  208. + },
  209. +};
  210. +
  211. +module_platform_driver(rt_timer_driver);
  212. +
  213. +MODULE_DESCRIPTION("Ralink RT2880 timer");
  214. +MODULE_AUTHOR("John Crispin <[email protected]");
  215. +MODULE_LICENSE("GPL");