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