timer.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /*
  2. * Ralink RT2880 timer
  3. * Author: John Crispin
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License version 2 as published
  7. * by the Free Software Foundation.
  8. *
  9. * Copyright (C) 2013 John Crispin <john@phrozen.org>
  10. */
  11. #include <linux/platform_device.h>
  12. #include <linux/interrupt.h>
  13. #include <linux/timer.h>
  14. #include <linux/of_gpio.h>
  15. #include <linux/clk.h>
  16. #include <asm/mach-ralink/ralink_regs.h>
  17. #define TIMER_REG_TMRSTAT 0x00
  18. #define TIMER_REG_TMR0LOAD 0x10
  19. #define TIMER_REG_TMR0CTL 0x18
  20. #define TMRSTAT_TMR0INT BIT(0)
  21. #define TMR0CTL_ENABLE BIT(7)
  22. #define TMR0CTL_MODE_PERIODIC BIT(4)
  23. #define TMR0CTL_PRESCALER 1
  24. #define TMR0CTL_PRESCALE_VAL (0xf - TMR0CTL_PRESCALER)
  25. #define TMR0CTL_PRESCALE_DIV (65536 / BIT(TMR0CTL_PRESCALER))
  26. struct rt_timer {
  27. struct device *dev;
  28. void __iomem *membase;
  29. int irq;
  30. unsigned long timer_freq;
  31. unsigned long timer_div;
  32. };
  33. static inline void rt_timer_w32(struct rt_timer *rt, u8 reg, u32 val)
  34. {
  35. __raw_writel(val, rt->membase + reg);
  36. }
  37. static inline u32 rt_timer_r32(struct rt_timer *rt, u8 reg)
  38. {
  39. return __raw_readl(rt->membase + reg);
  40. }
  41. static irqreturn_t rt_timer_irq(int irq, void *_rt)
  42. {
  43. struct rt_timer *rt = (struct rt_timer *) _rt;
  44. rt_timer_w32(rt, TIMER_REG_TMR0LOAD, rt->timer_freq / rt->timer_div);
  45. rt_timer_w32(rt, TIMER_REG_TMRSTAT, TMRSTAT_TMR0INT);
  46. return IRQ_HANDLED;
  47. }
  48. static int rt_timer_request(struct rt_timer *rt)
  49. {
  50. int err = request_irq(rt->irq, rt_timer_irq, 0,
  51. dev_name(rt->dev), rt);
  52. if (err) {
  53. dev_err(rt->dev, "failed to request irq\n");
  54. } else {
  55. u32 t = TMR0CTL_MODE_PERIODIC | TMR0CTL_PRESCALE_VAL;
  56. rt_timer_w32(rt, TIMER_REG_TMR0CTL, t);
  57. }
  58. return err;
  59. }
  60. static void rt_timer_free(struct rt_timer *rt)
  61. {
  62. free_irq(rt->irq, rt);
  63. }
  64. static int rt_timer_config(struct rt_timer *rt, unsigned long divisor)
  65. {
  66. if (rt->timer_freq < divisor)
  67. rt->timer_div = rt->timer_freq;
  68. else
  69. rt->timer_div = divisor;
  70. rt_timer_w32(rt, TIMER_REG_TMR0LOAD, rt->timer_freq / rt->timer_div);
  71. return 0;
  72. }
  73. static int rt_timer_enable(struct rt_timer *rt)
  74. {
  75. u32 t;
  76. rt_timer_w32(rt, TIMER_REG_TMR0LOAD, rt->timer_freq / rt->timer_div);
  77. t = rt_timer_r32(rt, TIMER_REG_TMR0CTL);
  78. t |= TMR0CTL_ENABLE;
  79. rt_timer_w32(rt, TIMER_REG_TMR0CTL, t);
  80. return 0;
  81. }
  82. static void rt_timer_disable(struct rt_timer *rt)
  83. {
  84. u32 t;
  85. t = rt_timer_r32(rt, TIMER_REG_TMR0CTL);
  86. t &= ~TMR0CTL_ENABLE;
  87. rt_timer_w32(rt, TIMER_REG_TMR0CTL, t);
  88. }
  89. static int rt_timer_probe(struct platform_device *pdev)
  90. {
  91. struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  92. struct rt_timer *rt;
  93. struct clk *clk;
  94. rt = devm_kzalloc(&pdev->dev, sizeof(*rt), GFP_KERNEL);
  95. if (!rt) {
  96. dev_err(&pdev->dev, "failed to allocate memory\n");
  97. return -ENOMEM;
  98. }
  99. rt->irq = platform_get_irq(pdev, 0);
  100. if (!rt->irq) {
  101. dev_err(&pdev->dev, "failed to load irq\n");
  102. return -ENOENT;
  103. }
  104. rt->membase = devm_ioremap_resource(&pdev->dev, res);
  105. if (IS_ERR(rt->membase))
  106. return PTR_ERR(rt->membase);
  107. clk = devm_clk_get(&pdev->dev, NULL);
  108. if (IS_ERR(clk)) {
  109. dev_err(&pdev->dev, "failed get clock rate\n");
  110. return PTR_ERR(clk);
  111. }
  112. rt->timer_freq = clk_get_rate(clk) / TMR0CTL_PRESCALE_DIV;
  113. if (!rt->timer_freq)
  114. return -EINVAL;
  115. rt->dev = &pdev->dev;
  116. platform_set_drvdata(pdev, rt);
  117. rt_timer_request(rt);
  118. rt_timer_config(rt, 2);
  119. rt_timer_enable(rt);
  120. dev_info(&pdev->dev, "maximum frequency is %luHz\n", rt->timer_freq);
  121. return 0;
  122. }
  123. static const struct of_device_id rt_timer_match[] = {
  124. { .compatible = "ralink,rt2880-timer" },
  125. {},
  126. };
  127. static struct platform_driver rt_timer_driver = {
  128. .probe = rt_timer_probe,
  129. .driver = {
  130. .name = "rt-timer",
  131. .of_match_table = rt_timer_match,
  132. .suppress_bind_attrs = true,
  133. },
  134. };
  135. builtin_platform_driver(rt_timer_driver);