timer.c 3.4 KB

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