timer.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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 int rt_timer_config(struct rt_timer *rt, unsigned long divisor)
  61. {
  62. if (rt->timer_freq < divisor)
  63. rt->timer_div = rt->timer_freq;
  64. else
  65. rt->timer_div = divisor;
  66. rt_timer_w32(rt, TIMER_REG_TMR0LOAD, rt->timer_freq / rt->timer_div);
  67. return 0;
  68. }
  69. static int rt_timer_enable(struct rt_timer *rt)
  70. {
  71. u32 t;
  72. rt_timer_w32(rt, TIMER_REG_TMR0LOAD, rt->timer_freq / rt->timer_div);
  73. t = rt_timer_r32(rt, TIMER_REG_TMR0CTL);
  74. t |= TMR0CTL_ENABLE;
  75. rt_timer_w32(rt, TIMER_REG_TMR0CTL, t);
  76. return 0;
  77. }
  78. static int rt_timer_probe(struct platform_device *pdev)
  79. {
  80. struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  81. struct rt_timer *rt;
  82. struct clk *clk;
  83. rt = devm_kzalloc(&pdev->dev, sizeof(*rt), GFP_KERNEL);
  84. if (!rt) {
  85. dev_err(&pdev->dev, "failed to allocate memory\n");
  86. return -ENOMEM;
  87. }
  88. rt->irq = platform_get_irq(pdev, 0);
  89. if (!rt->irq) {
  90. dev_err(&pdev->dev, "failed to load irq\n");
  91. return -ENOENT;
  92. }
  93. rt->membase = devm_ioremap_resource(&pdev->dev, res);
  94. if (IS_ERR(rt->membase))
  95. return PTR_ERR(rt->membase);
  96. clk = devm_clk_get(&pdev->dev, NULL);
  97. if (IS_ERR(clk)) {
  98. dev_err(&pdev->dev, "failed get clock rate\n");
  99. return PTR_ERR(clk);
  100. }
  101. rt->timer_freq = clk_get_rate(clk) / TMR0CTL_PRESCALE_DIV;
  102. if (!rt->timer_freq)
  103. return -EINVAL;
  104. rt->dev = &pdev->dev;
  105. platform_set_drvdata(pdev, rt);
  106. rt_timer_request(rt);
  107. rt_timer_config(rt, 2);
  108. rt_timer_enable(rt);
  109. dev_info(&pdev->dev, "maximum frequency is %luHz\n", rt->timer_freq);
  110. return 0;
  111. }
  112. static const struct of_device_id rt_timer_match[] = {
  113. { .compatible = "ralink,rt2880-timer" },
  114. {},
  115. };
  116. static struct platform_driver rt_timer_driver = {
  117. .probe = rt_timer_probe,
  118. .driver = {
  119. .name = "rt-timer",
  120. .of_match_table = rt_timer_match,
  121. .suppress_bind_attrs = true,
  122. },
  123. };
  124. builtin_platform_driver(rt_timer_driver);