cp_intc.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /*
  2. * TI Common Platform Interrupt Controller (cp_intc) driver
  3. *
  4. * Author: Steve Chen <schen@mvista.com>
  5. * Copyright (C) 2008-2009, MontaVista Software, Inc. <source@mvista.com>
  6. *
  7. * This file is licensed under the terms of the GNU General Public License
  8. * version 2. This program is licensed "as is" without any warranty of any
  9. * kind, whether express or implied.
  10. */
  11. #include <linux/export.h>
  12. #include <linux/init.h>
  13. #include <linux/irq.h>
  14. #include <linux/irqchip.h>
  15. #include <linux/irqdomain.h>
  16. #include <linux/io.h>
  17. #include <linux/of.h>
  18. #include <linux/of_address.h>
  19. #include <linux/of_irq.h>
  20. #include <mach/common.h>
  21. #include "cp_intc.h"
  22. static inline unsigned int cp_intc_read(unsigned offset)
  23. {
  24. return __raw_readl(davinci_intc_base + offset);
  25. }
  26. static inline void cp_intc_write(unsigned long value, unsigned offset)
  27. {
  28. __raw_writel(value, davinci_intc_base + offset);
  29. }
  30. static void cp_intc_ack_irq(struct irq_data *d)
  31. {
  32. cp_intc_write(d->hwirq, CP_INTC_SYS_STAT_IDX_CLR);
  33. }
  34. /* Disable interrupt */
  35. static void cp_intc_mask_irq(struct irq_data *d)
  36. {
  37. /* XXX don't know why we need to disable nIRQ here... */
  38. cp_intc_write(1, CP_INTC_HOST_ENABLE_IDX_CLR);
  39. cp_intc_write(d->hwirq, CP_INTC_SYS_ENABLE_IDX_CLR);
  40. cp_intc_write(1, CP_INTC_HOST_ENABLE_IDX_SET);
  41. }
  42. /* Enable interrupt */
  43. static void cp_intc_unmask_irq(struct irq_data *d)
  44. {
  45. cp_intc_write(d->hwirq, CP_INTC_SYS_ENABLE_IDX_SET);
  46. }
  47. static int cp_intc_set_irq_type(struct irq_data *d, unsigned int flow_type)
  48. {
  49. unsigned reg = BIT_WORD(d->hwirq);
  50. unsigned mask = BIT_MASK(d->hwirq);
  51. unsigned polarity = cp_intc_read(CP_INTC_SYS_POLARITY(reg));
  52. unsigned type = cp_intc_read(CP_INTC_SYS_TYPE(reg));
  53. switch (flow_type) {
  54. case IRQ_TYPE_EDGE_RISING:
  55. polarity |= mask;
  56. type |= mask;
  57. break;
  58. case IRQ_TYPE_EDGE_FALLING:
  59. polarity &= ~mask;
  60. type |= mask;
  61. break;
  62. case IRQ_TYPE_LEVEL_HIGH:
  63. polarity |= mask;
  64. type &= ~mask;
  65. break;
  66. case IRQ_TYPE_LEVEL_LOW:
  67. polarity &= ~mask;
  68. type &= ~mask;
  69. break;
  70. default:
  71. return -EINVAL;
  72. }
  73. cp_intc_write(polarity, CP_INTC_SYS_POLARITY(reg));
  74. cp_intc_write(type, CP_INTC_SYS_TYPE(reg));
  75. return 0;
  76. }
  77. static struct irq_chip cp_intc_irq_chip = {
  78. .name = "cp_intc",
  79. .irq_ack = cp_intc_ack_irq,
  80. .irq_mask = cp_intc_mask_irq,
  81. .irq_unmask = cp_intc_unmask_irq,
  82. .irq_set_type = cp_intc_set_irq_type,
  83. .flags = IRQCHIP_SKIP_SET_WAKE,
  84. };
  85. static struct irq_domain *cp_intc_domain;
  86. static int cp_intc_host_map(struct irq_domain *h, unsigned int virq,
  87. irq_hw_number_t hw)
  88. {
  89. pr_debug("cp_intc_host_map(%d, 0x%lx)\n", virq, hw);
  90. irq_set_chip(virq, &cp_intc_irq_chip);
  91. irq_set_probe(virq);
  92. irq_set_handler(virq, handle_edge_irq);
  93. return 0;
  94. }
  95. static const struct irq_domain_ops cp_intc_host_ops = {
  96. .map = cp_intc_host_map,
  97. .xlate = irq_domain_xlate_onetwocell,
  98. };
  99. int __init cp_intc_of_init(struct device_node *node, struct device_node *parent)
  100. {
  101. u32 num_irq = davinci_soc_info.intc_irq_num;
  102. u8 *irq_prio = davinci_soc_info.intc_irq_prios;
  103. u32 *host_map = davinci_soc_info.intc_host_map;
  104. unsigned num_reg = BITS_TO_LONGS(num_irq);
  105. int i, irq_base;
  106. davinci_intc_type = DAVINCI_INTC_TYPE_CP_INTC;
  107. if (node) {
  108. davinci_intc_base = of_iomap(node, 0);
  109. if (of_property_read_u32(node, "ti,intc-size", &num_irq))
  110. pr_warn("unable to get intc-size, default to %d\n",
  111. num_irq);
  112. } else {
  113. davinci_intc_base = ioremap(davinci_soc_info.intc_base, SZ_8K);
  114. }
  115. if (WARN_ON(!davinci_intc_base))
  116. return -EINVAL;
  117. cp_intc_write(0, CP_INTC_GLOBAL_ENABLE);
  118. /* Disable all host interrupts */
  119. cp_intc_write(0, CP_INTC_HOST_ENABLE(0));
  120. /* Disable system interrupts */
  121. for (i = 0; i < num_reg; i++)
  122. cp_intc_write(~0, CP_INTC_SYS_ENABLE_CLR(i));
  123. /* Set to normal mode, no nesting, no priority hold */
  124. cp_intc_write(0, CP_INTC_CTRL);
  125. cp_intc_write(0, CP_INTC_HOST_CTRL);
  126. /* Clear system interrupt status */
  127. for (i = 0; i < num_reg; i++)
  128. cp_intc_write(~0, CP_INTC_SYS_STAT_CLR(i));
  129. /* Enable nIRQ (what about nFIQ?) */
  130. cp_intc_write(1, CP_INTC_HOST_ENABLE_IDX_SET);
  131. /*
  132. * Priority is determined by host channel: lower channel number has
  133. * higher priority i.e. channel 0 has highest priority and channel 31
  134. * had the lowest priority.
  135. */
  136. num_reg = (num_irq + 3) >> 2; /* 4 channels per register */
  137. if (irq_prio) {
  138. unsigned j, k;
  139. u32 val;
  140. for (k = i = 0; i < num_reg; i++) {
  141. for (val = j = 0; j < 4; j++, k++) {
  142. val >>= 8;
  143. if (k < num_irq)
  144. val |= irq_prio[k] << 24;
  145. }
  146. cp_intc_write(val, CP_INTC_CHAN_MAP(i));
  147. }
  148. } else {
  149. /*
  150. * Default everything to channel 15 if priority not specified.
  151. * Note that channel 0-1 are mapped to nFIQ and channels 2-31
  152. * are mapped to nIRQ.
  153. */
  154. for (i = 0; i < num_reg; i++)
  155. cp_intc_write(0x0f0f0f0f, CP_INTC_CHAN_MAP(i));
  156. }
  157. if (host_map)
  158. for (i = 0; host_map[i] != -1; i++)
  159. cp_intc_write(host_map[i], CP_INTC_HOST_MAP(i));
  160. irq_base = irq_alloc_descs(-1, 0, num_irq, 0);
  161. if (irq_base < 0) {
  162. pr_warn("Couldn't allocate IRQ numbers\n");
  163. irq_base = 0;
  164. }
  165. /* create a legacy host */
  166. cp_intc_domain = irq_domain_add_legacy(node, num_irq,
  167. irq_base, 0, &cp_intc_host_ops, NULL);
  168. if (!cp_intc_domain) {
  169. pr_err("cp_intc: failed to allocate irq host!\n");
  170. return -EINVAL;
  171. }
  172. /* Enable global interrupt */
  173. cp_intc_write(1, CP_INTC_GLOBAL_ENABLE);
  174. return 0;
  175. }
  176. void __init cp_intc_init(void)
  177. {
  178. cp_intc_of_init(NULL, NULL);
  179. }
  180. IRQCHIP_DECLARE(cp_intc, "ti,cp-intc", cp_intc_of_init);