irq.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /*
  2. * This program is free software; you can redistribute it and/or modify it
  3. * under the terms of the GNU General Public License as published by the
  4. * Free Software Foundation; either version 2 of the License, or (at your
  5. * option) any later version.
  6. *
  7. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
  8. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  9. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
  10. * NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  11. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  12. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  13. * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  14. * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  15. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  16. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  17. *
  18. * You should have received a copy of the GNU General Public License along
  19. * with this program; if not, write to the Free Software Foundation, Inc.,
  20. * 675 Mass Ave, Cambridge, MA 02139, USA.
  21. *
  22. * Copyright 2002 MontaVista Software Inc.
  23. * Author: MontaVista Software, Inc.
  24. * stevel@mvista.com or source@mvista.com
  25. */
  26. #include <linux/bitops.h>
  27. #include <linux/errno.h>
  28. #include <linux/init.h>
  29. #include <linux/io.h>
  30. #include <linux/kernel_stat.h>
  31. #include <linux/signal.h>
  32. #include <linux/sched.h>
  33. #include <linux/types.h>
  34. #include <linux/interrupt.h>
  35. #include <linux/ioport.h>
  36. #include <linux/timex.h>
  37. #include <linux/random.h>
  38. #include <linux/delay.h>
  39. #include <asm/bootinfo.h>
  40. #include <asm/time.h>
  41. #include <asm/mipsregs.h>
  42. #include <asm/mach-rc32434/irq.h>
  43. #include <asm/mach-rc32434/gpio.h>
  44. struct intr_group {
  45. u32 mask; /* mask of valid bits in pending/mask registers */
  46. volatile u32 *base_addr;
  47. };
  48. #define RC32434_NR_IRQS (GROUP4_IRQ_BASE + 32)
  49. #if (NR_IRQS < RC32434_NR_IRQS)
  50. #error Too little irqs defined. Did you override <asm/irq.h> ?
  51. #endif
  52. static const struct intr_group intr_group[NUM_INTR_GROUPS] = {
  53. {
  54. .mask = 0x0000efff,
  55. .base_addr = (u32 *) KSEG1ADDR(IC_GROUP0_PEND + 0 * IC_GROUP_OFFSET)},
  56. {
  57. .mask = 0x00001fff,
  58. .base_addr = (u32 *) KSEG1ADDR(IC_GROUP0_PEND + 1 * IC_GROUP_OFFSET)},
  59. {
  60. .mask = 0x00000007,
  61. .base_addr = (u32 *) KSEG1ADDR(IC_GROUP0_PEND + 2 * IC_GROUP_OFFSET)},
  62. {
  63. .mask = 0x0003ffff,
  64. .base_addr = (u32 *) KSEG1ADDR(IC_GROUP0_PEND + 3 * IC_GROUP_OFFSET)},
  65. {
  66. .mask = 0xffffffff,
  67. .base_addr = (u32 *) KSEG1ADDR(IC_GROUP0_PEND + 4 * IC_GROUP_OFFSET)}
  68. };
  69. #define READ_PEND(base) (*(base))
  70. #define READ_MASK(base) (*(base + 2))
  71. #define WRITE_MASK(base, val) (*(base + 2) = (val))
  72. static inline int irq_to_group(unsigned int irq_nr)
  73. {
  74. return (irq_nr - GROUP0_IRQ_BASE) >> 5;
  75. }
  76. static inline int group_to_ip(unsigned int group)
  77. {
  78. return group + 2;
  79. }
  80. static inline void enable_local_irq(unsigned int ip)
  81. {
  82. int ipnum = 0x100 << ip;
  83. set_c0_status(ipnum);
  84. }
  85. static inline void disable_local_irq(unsigned int ip)
  86. {
  87. int ipnum = 0x100 << ip;
  88. clear_c0_status(ipnum);
  89. }
  90. static inline void ack_local_irq(unsigned int ip)
  91. {
  92. int ipnum = 0x100 << ip;
  93. clear_c0_cause(ipnum);
  94. }
  95. static void rb532_enable_irq(struct irq_data *d)
  96. {
  97. unsigned int group, intr_bit, irq_nr = d->irq;
  98. int ip = irq_nr - GROUP0_IRQ_BASE;
  99. volatile unsigned int *addr;
  100. if (ip < 0)
  101. enable_local_irq(irq_nr);
  102. else {
  103. group = ip >> 5;
  104. ip &= (1 << 5) - 1;
  105. intr_bit = 1 << ip;
  106. enable_local_irq(group_to_ip(group));
  107. addr = intr_group[group].base_addr;
  108. WRITE_MASK(addr, READ_MASK(addr) & ~intr_bit);
  109. }
  110. }
  111. static void rb532_disable_irq(struct irq_data *d)
  112. {
  113. unsigned int group, intr_bit, mask, irq_nr = d->irq;
  114. int ip = irq_nr - GROUP0_IRQ_BASE;
  115. volatile unsigned int *addr;
  116. if (ip < 0) {
  117. disable_local_irq(irq_nr);
  118. } else {
  119. group = ip >> 5;
  120. ip &= (1 << 5) - 1;
  121. intr_bit = 1 << ip;
  122. addr = intr_group[group].base_addr;
  123. mask = READ_MASK(addr);
  124. mask |= intr_bit;
  125. WRITE_MASK(addr, mask);
  126. /* There is a maximum of 14 GPIO interrupts */
  127. if (group == GPIO_MAPPED_IRQ_GROUP && irq_nr <= (GROUP4_IRQ_BASE + 13))
  128. rb532_gpio_set_istat(0, irq_nr - GPIO_MAPPED_IRQ_BASE);
  129. /*
  130. * if there are no more interrupts enabled in this
  131. * group, disable corresponding IP
  132. */
  133. if (mask == intr_group[group].mask)
  134. disable_local_irq(group_to_ip(group));
  135. }
  136. }
  137. static void rb532_mask_and_ack_irq(struct irq_data *d)
  138. {
  139. rb532_disable_irq(d);
  140. ack_local_irq(group_to_ip(irq_to_group(d->irq)));
  141. }
  142. static int rb532_set_type(struct irq_data *d, unsigned type)
  143. {
  144. int gpio = d->irq - GPIO_MAPPED_IRQ_BASE;
  145. int group = irq_to_group(d->irq);
  146. if (group != GPIO_MAPPED_IRQ_GROUP || d->irq > (GROUP4_IRQ_BASE + 13))
  147. return (type == IRQ_TYPE_LEVEL_HIGH) ? 0 : -EINVAL;
  148. switch (type) {
  149. case IRQ_TYPE_LEVEL_HIGH:
  150. rb532_gpio_set_ilevel(1, gpio);
  151. break;
  152. case IRQ_TYPE_LEVEL_LOW:
  153. rb532_gpio_set_ilevel(0, gpio);
  154. break;
  155. default:
  156. return -EINVAL;
  157. }
  158. return 0;
  159. }
  160. static struct irq_chip rc32434_irq_type = {
  161. .name = "RB532",
  162. .irq_ack = rb532_disable_irq,
  163. .irq_mask = rb532_disable_irq,
  164. .irq_mask_ack = rb532_mask_and_ack_irq,
  165. .irq_unmask = rb532_enable_irq,
  166. .irq_set_type = rb532_set_type,
  167. };
  168. void __init arch_init_irq(void)
  169. {
  170. int i;
  171. pr_info("Initializing IRQ's: %d out of %d\n", RC32434_NR_IRQS, NR_IRQS);
  172. for (i = 0; i < RC32434_NR_IRQS; i++)
  173. irq_set_chip_and_handler(i, &rc32434_irq_type,
  174. handle_level_irq);
  175. }
  176. /* Main Interrupt dispatcher */
  177. asmlinkage void plat_irq_dispatch(void)
  178. {
  179. unsigned int ip, pend, group;
  180. volatile unsigned int *addr;
  181. unsigned int cp0_cause = read_c0_cause() & read_c0_status();
  182. if (cp0_cause & CAUSEF_IP7) {
  183. do_IRQ(7);
  184. } else {
  185. ip = (cp0_cause & 0x7c00);
  186. if (ip) {
  187. group = 21 + (fls(ip) - 32);
  188. addr = intr_group[group].base_addr;
  189. pend = READ_PEND(addr);
  190. pend &= ~READ_MASK(addr); /* only unmasked interrupts */
  191. pend = 39 + (fls(pend) - 32);
  192. do_IRQ((group << 5) + pend);
  193. }
  194. }
  195. }