timer.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /*
  2. * This file is subject to the terms and conditions of the GNU General Public
  3. * License. See the file "COPYING" in the main directory of this archive
  4. * for more details.
  5. *
  6. * Copyright (C) 2008 Maxime Bizon <mbizon@freebox.fr>
  7. */
  8. #include <linux/kernel.h>
  9. #include <linux/err.h>
  10. #include <linux/module.h>
  11. #include <linux/spinlock.h>
  12. #include <linux/interrupt.h>
  13. #include <linux/clk.h>
  14. #include <bcm63xx_cpu.h>
  15. #include <bcm63xx_io.h>
  16. #include <bcm63xx_timer.h>
  17. #include <bcm63xx_regs.h>
  18. static DEFINE_RAW_SPINLOCK(timer_reg_lock);
  19. static DEFINE_RAW_SPINLOCK(timer_data_lock);
  20. static struct clk *periph_clk;
  21. static struct timer_data {
  22. void (*cb)(void *);
  23. void *data;
  24. } timer_data[BCM63XX_TIMER_COUNT];
  25. static irqreturn_t timer_interrupt(int irq, void *dev_id)
  26. {
  27. u32 stat;
  28. int i;
  29. raw_spin_lock(&timer_reg_lock);
  30. stat = bcm_timer_readl(TIMER_IRQSTAT_REG);
  31. bcm_timer_writel(stat, TIMER_IRQSTAT_REG);
  32. raw_spin_unlock(&timer_reg_lock);
  33. for (i = 0; i < BCM63XX_TIMER_COUNT; i++) {
  34. if (!(stat & TIMER_IRQSTAT_TIMER_CAUSE(i)))
  35. continue;
  36. raw_spin_lock(&timer_data_lock);
  37. if (!timer_data[i].cb) {
  38. raw_spin_unlock(&timer_data_lock);
  39. continue;
  40. }
  41. timer_data[i].cb(timer_data[i].data);
  42. raw_spin_unlock(&timer_data_lock);
  43. }
  44. return IRQ_HANDLED;
  45. }
  46. int bcm63xx_timer_enable(int id)
  47. {
  48. u32 reg;
  49. unsigned long flags;
  50. if (id >= BCM63XX_TIMER_COUNT)
  51. return -EINVAL;
  52. raw_spin_lock_irqsave(&timer_reg_lock, flags);
  53. reg = bcm_timer_readl(TIMER_CTLx_REG(id));
  54. reg |= TIMER_CTL_ENABLE_MASK;
  55. bcm_timer_writel(reg, TIMER_CTLx_REG(id));
  56. reg = bcm_timer_readl(TIMER_IRQSTAT_REG);
  57. reg |= TIMER_IRQSTAT_TIMER_IR_EN(id);
  58. bcm_timer_writel(reg, TIMER_IRQSTAT_REG);
  59. raw_spin_unlock_irqrestore(&timer_reg_lock, flags);
  60. return 0;
  61. }
  62. EXPORT_SYMBOL(bcm63xx_timer_enable);
  63. int bcm63xx_timer_disable(int id)
  64. {
  65. u32 reg;
  66. unsigned long flags;
  67. if (id >= BCM63XX_TIMER_COUNT)
  68. return -EINVAL;
  69. raw_spin_lock_irqsave(&timer_reg_lock, flags);
  70. reg = bcm_timer_readl(TIMER_CTLx_REG(id));
  71. reg &= ~TIMER_CTL_ENABLE_MASK;
  72. bcm_timer_writel(reg, TIMER_CTLx_REG(id));
  73. reg = bcm_timer_readl(TIMER_IRQSTAT_REG);
  74. reg &= ~TIMER_IRQSTAT_TIMER_IR_EN(id);
  75. bcm_timer_writel(reg, TIMER_IRQSTAT_REG);
  76. raw_spin_unlock_irqrestore(&timer_reg_lock, flags);
  77. return 0;
  78. }
  79. EXPORT_SYMBOL(bcm63xx_timer_disable);
  80. int bcm63xx_timer_register(int id, void (*callback)(void *data), void *data)
  81. {
  82. unsigned long flags;
  83. int ret;
  84. if (id >= BCM63XX_TIMER_COUNT || !callback)
  85. return -EINVAL;
  86. ret = 0;
  87. raw_spin_lock_irqsave(&timer_data_lock, flags);
  88. if (timer_data[id].cb) {
  89. ret = -EBUSY;
  90. goto out;
  91. }
  92. timer_data[id].cb = callback;
  93. timer_data[id].data = data;
  94. out:
  95. raw_spin_unlock_irqrestore(&timer_data_lock, flags);
  96. return ret;
  97. }
  98. EXPORT_SYMBOL(bcm63xx_timer_register);
  99. void bcm63xx_timer_unregister(int id)
  100. {
  101. unsigned long flags;
  102. if (id >= BCM63XX_TIMER_COUNT)
  103. return;
  104. raw_spin_lock_irqsave(&timer_data_lock, flags);
  105. timer_data[id].cb = NULL;
  106. raw_spin_unlock_irqrestore(&timer_data_lock, flags);
  107. }
  108. EXPORT_SYMBOL(bcm63xx_timer_unregister);
  109. unsigned int bcm63xx_timer_countdown(unsigned int countdown_us)
  110. {
  111. return (clk_get_rate(periph_clk) / (1000 * 1000)) * countdown_us;
  112. }
  113. EXPORT_SYMBOL(bcm63xx_timer_countdown);
  114. int bcm63xx_timer_set(int id, int monotonic, unsigned int countdown_us)
  115. {
  116. u32 reg, countdown;
  117. unsigned long flags;
  118. if (id >= BCM63XX_TIMER_COUNT)
  119. return -EINVAL;
  120. countdown = bcm63xx_timer_countdown(countdown_us);
  121. if (countdown & ~TIMER_CTL_COUNTDOWN_MASK)
  122. return -EINVAL;
  123. raw_spin_lock_irqsave(&timer_reg_lock, flags);
  124. reg = bcm_timer_readl(TIMER_CTLx_REG(id));
  125. if (monotonic)
  126. reg &= ~TIMER_CTL_MONOTONIC_MASK;
  127. else
  128. reg |= TIMER_CTL_MONOTONIC_MASK;
  129. reg &= ~TIMER_CTL_COUNTDOWN_MASK;
  130. reg |= countdown;
  131. bcm_timer_writel(reg, TIMER_CTLx_REG(id));
  132. raw_spin_unlock_irqrestore(&timer_reg_lock, flags);
  133. return 0;
  134. }
  135. EXPORT_SYMBOL(bcm63xx_timer_set);
  136. int bcm63xx_timer_init(void)
  137. {
  138. int ret, irq;
  139. u32 reg;
  140. reg = bcm_timer_readl(TIMER_IRQSTAT_REG);
  141. reg &= ~TIMER_IRQSTAT_TIMER0_IR_EN;
  142. reg &= ~TIMER_IRQSTAT_TIMER1_IR_EN;
  143. reg &= ~TIMER_IRQSTAT_TIMER2_IR_EN;
  144. bcm_timer_writel(reg, TIMER_IRQSTAT_REG);
  145. periph_clk = clk_get(NULL, "periph");
  146. if (IS_ERR(periph_clk))
  147. return -ENODEV;
  148. irq = bcm63xx_get_irq_number(IRQ_TIMER);
  149. ret = request_irq(irq, timer_interrupt, 0, "bcm63xx_timer", NULL);
  150. if (ret) {
  151. pr_err("%s: failed to register irq\n", __func__);
  152. return ret;
  153. }
  154. return 0;
  155. }
  156. arch_initcall(bcm63xx_timer_init);