cp_intc.c 5.5 KB

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