xilinx_intc.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. /*
  2. * Interrupt controller driver for Xilinx Virtex FPGAs
  3. *
  4. * Copyright (C) 2007 Secret Lab Technologies Ltd.
  5. *
  6. * This file is licensed under the terms of the GNU General Public License
  7. * version 2. This program is licensed "as is" without any warranty of any
  8. * kind, whether express or implied.
  9. *
  10. */
  11. /*
  12. * This is a driver for the interrupt controller typically found in
  13. * Xilinx Virtex FPGA designs.
  14. *
  15. * The interrupt sense levels are hard coded into the FPGA design with
  16. * typically a 1:1 relationship between irq lines and devices (no shared
  17. * irq lines). Therefore, this driver does not attempt to handle edge
  18. * and level interrupts differently.
  19. */
  20. #undef DEBUG
  21. #include <linux/kernel.h>
  22. #include <linux/irq.h>
  23. #include <linux/of.h>
  24. #include <linux/of_address.h>
  25. #include <linux/of_irq.h>
  26. #include <asm/io.h>
  27. #include <asm/processor.h>
  28. #include <asm/i8259.h>
  29. #include <asm/irq.h>
  30. /*
  31. * INTC Registers
  32. */
  33. #define XINTC_ISR 0 /* Interrupt Status */
  34. #define XINTC_IPR 4 /* Interrupt Pending */
  35. #define XINTC_IER 8 /* Interrupt Enable */
  36. #define XINTC_IAR 12 /* Interrupt Acknowledge */
  37. #define XINTC_SIE 16 /* Set Interrupt Enable bits */
  38. #define XINTC_CIE 20 /* Clear Interrupt Enable bits */
  39. #define XINTC_IVR 24 /* Interrupt Vector */
  40. #define XINTC_MER 28 /* Master Enable */
  41. static struct irq_domain *master_irqhost;
  42. #define XILINX_INTC_MAXIRQS (32)
  43. /* The following table allows the interrupt type, edge or level,
  44. * to be cached after being read from the device tree until the interrupt
  45. * is mapped
  46. */
  47. static int xilinx_intc_typetable[XILINX_INTC_MAXIRQS];
  48. /* Map the interrupt type from the device tree to the interrupt types
  49. * used by the interrupt subsystem
  50. */
  51. static unsigned char xilinx_intc_map_senses[] = {
  52. IRQ_TYPE_EDGE_RISING,
  53. IRQ_TYPE_EDGE_FALLING,
  54. IRQ_TYPE_LEVEL_HIGH,
  55. IRQ_TYPE_LEVEL_LOW,
  56. };
  57. /*
  58. * The interrupt controller is setup such that it doesn't work well with
  59. * the level interrupt handler in the kernel because the handler acks the
  60. * interrupt before calling the application interrupt handler. To deal with
  61. * that, we use 2 different irq chips so that different functions can be
  62. * used for level and edge type interrupts.
  63. *
  64. * IRQ Chip common (across level and edge) operations
  65. */
  66. static void xilinx_intc_mask(struct irq_data *d)
  67. {
  68. int irq = irqd_to_hwirq(d);
  69. void * regs = irq_data_get_irq_chip_data(d);
  70. pr_debug("mask: %d\n", irq);
  71. out_be32(regs + XINTC_CIE, 1 << irq);
  72. }
  73. static int xilinx_intc_set_type(struct irq_data *d, unsigned int flow_type)
  74. {
  75. return 0;
  76. }
  77. /*
  78. * IRQ Chip level operations
  79. */
  80. static void xilinx_intc_level_unmask(struct irq_data *d)
  81. {
  82. int irq = irqd_to_hwirq(d);
  83. void * regs = irq_data_get_irq_chip_data(d);
  84. pr_debug("unmask: %d\n", irq);
  85. out_be32(regs + XINTC_SIE, 1 << irq);
  86. /* ack level irqs because they can't be acked during
  87. * ack function since the handle_level_irq function
  88. * acks the irq before calling the inerrupt handler
  89. */
  90. out_be32(regs + XINTC_IAR, 1 << irq);
  91. }
  92. static struct irq_chip xilinx_intc_level_irqchip = {
  93. .name = "Xilinx Level INTC",
  94. .irq_mask = xilinx_intc_mask,
  95. .irq_mask_ack = xilinx_intc_mask,
  96. .irq_unmask = xilinx_intc_level_unmask,
  97. .irq_set_type = xilinx_intc_set_type,
  98. };
  99. /*
  100. * IRQ Chip edge operations
  101. */
  102. static void xilinx_intc_edge_unmask(struct irq_data *d)
  103. {
  104. int irq = irqd_to_hwirq(d);
  105. void *regs = irq_data_get_irq_chip_data(d);
  106. pr_debug("unmask: %d\n", irq);
  107. out_be32(regs + XINTC_SIE, 1 << irq);
  108. }
  109. static void xilinx_intc_edge_ack(struct irq_data *d)
  110. {
  111. int irq = irqd_to_hwirq(d);
  112. void * regs = irq_data_get_irq_chip_data(d);
  113. pr_debug("ack: %d\n", irq);
  114. out_be32(regs + XINTC_IAR, 1 << irq);
  115. }
  116. static struct irq_chip xilinx_intc_edge_irqchip = {
  117. .name = "Xilinx Edge INTC",
  118. .irq_mask = xilinx_intc_mask,
  119. .irq_unmask = xilinx_intc_edge_unmask,
  120. .irq_ack = xilinx_intc_edge_ack,
  121. .irq_set_type = xilinx_intc_set_type,
  122. };
  123. /*
  124. * IRQ Host operations
  125. */
  126. /**
  127. * xilinx_intc_xlate - translate virq# from device tree interrupts property
  128. */
  129. static int xilinx_intc_xlate(struct irq_domain *h, struct device_node *ct,
  130. const u32 *intspec, unsigned int intsize,
  131. irq_hw_number_t *out_hwirq,
  132. unsigned int *out_flags)
  133. {
  134. if ((intsize < 2) || (intspec[0] >= XILINX_INTC_MAXIRQS))
  135. return -EINVAL;
  136. /* keep a copy of the interrupt type til the interrupt is mapped
  137. */
  138. xilinx_intc_typetable[intspec[0]] = xilinx_intc_map_senses[intspec[1]];
  139. /* Xilinx uses 2 interrupt entries, the 1st being the h/w
  140. * interrupt number, the 2nd being the interrupt type, edge or level
  141. */
  142. *out_hwirq = intspec[0];
  143. *out_flags = xilinx_intc_map_senses[intspec[1]];
  144. return 0;
  145. }
  146. static int xilinx_intc_map(struct irq_domain *h, unsigned int virq,
  147. irq_hw_number_t irq)
  148. {
  149. irq_set_chip_data(virq, h->host_data);
  150. if (xilinx_intc_typetable[irq] == IRQ_TYPE_LEVEL_HIGH ||
  151. xilinx_intc_typetable[irq] == IRQ_TYPE_LEVEL_LOW) {
  152. irq_set_chip_and_handler(virq, &xilinx_intc_level_irqchip,
  153. handle_level_irq);
  154. } else {
  155. irq_set_chip_and_handler(virq, &xilinx_intc_edge_irqchip,
  156. handle_edge_irq);
  157. }
  158. return 0;
  159. }
  160. static const struct irq_domain_ops xilinx_intc_ops = {
  161. .map = xilinx_intc_map,
  162. .xlate = xilinx_intc_xlate,
  163. };
  164. struct irq_domain * __init
  165. xilinx_intc_init(struct device_node *np)
  166. {
  167. struct irq_domain * irq;
  168. void * regs;
  169. /* Find and map the intc registers */
  170. regs = of_iomap(np, 0);
  171. if (!regs) {
  172. pr_err("xilinx_intc: could not map registers\n");
  173. return NULL;
  174. }
  175. /* Setup interrupt controller */
  176. out_be32(regs + XINTC_IER, 0); /* disable all irqs */
  177. out_be32(regs + XINTC_IAR, ~(u32) 0); /* Acknowledge pending irqs */
  178. out_be32(regs + XINTC_MER, 0x3UL); /* Turn on the Master Enable. */
  179. /* Allocate and initialize an irq_domain structure. */
  180. irq = irq_domain_add_linear(np, XILINX_INTC_MAXIRQS, &xilinx_intc_ops,
  181. regs);
  182. if (!irq)
  183. panic(__FILE__ ": Cannot allocate IRQ host\n");
  184. return irq;
  185. }
  186. int xilinx_intc_get_irq(void)
  187. {
  188. void * regs = master_irqhost->host_data;
  189. pr_debug("get_irq:\n");
  190. return irq_linear_revmap(master_irqhost, in_be32(regs + XINTC_IVR));
  191. }
  192. #if defined(CONFIG_PPC_I8259)
  193. /*
  194. * Support code for cascading to 8259 interrupt controllers
  195. */
  196. static void xilinx_i8259_cascade(struct irq_desc *desc)
  197. {
  198. struct irq_chip *chip = irq_desc_get_chip(desc);
  199. unsigned int cascade_irq = i8259_irq();
  200. if (cascade_irq)
  201. generic_handle_irq(cascade_irq);
  202. /* Let xilinx_intc end the interrupt */
  203. chip->irq_unmask(&desc->irq_data);
  204. }
  205. static void __init xilinx_i8259_setup_cascade(void)
  206. {
  207. struct device_node *cascade_node;
  208. int cascade_irq;
  209. /* Initialize i8259 controller */
  210. cascade_node = of_find_compatible_node(NULL, NULL, "chrp,iic");
  211. if (!cascade_node)
  212. return;
  213. cascade_irq = irq_of_parse_and_map(cascade_node, 0);
  214. if (!cascade_irq) {
  215. pr_err("virtex_ml510: Failed to map cascade interrupt\n");
  216. goto out;
  217. }
  218. i8259_init(cascade_node, 0);
  219. irq_set_chained_handler(cascade_irq, xilinx_i8259_cascade);
  220. /* Program irq 7 (usb/audio), 14/15 (ide) to level sensitive */
  221. /* This looks like a dirty hack to me --gcl */
  222. outb(0xc0, 0x4d0);
  223. outb(0xc0, 0x4d1);
  224. out:
  225. of_node_put(cascade_node);
  226. }
  227. #else
  228. static inline void xilinx_i8259_setup_cascade(void) { return; }
  229. #endif /* defined(CONFIG_PPC_I8259) */
  230. static const struct of_device_id xilinx_intc_match[] __initconst = {
  231. { .compatible = "xlnx,opb-intc-1.00.c", },
  232. { .compatible = "xlnx,xps-intc-1.00.a", },
  233. {}
  234. };
  235. /*
  236. * Initialize master Xilinx interrupt controller
  237. */
  238. void __init xilinx_intc_init_tree(void)
  239. {
  240. struct device_node *np;
  241. /* find top level interrupt controller */
  242. for_each_matching_node(np, xilinx_intc_match) {
  243. if (!of_get_property(np, "interrupts", NULL))
  244. break;
  245. }
  246. BUG_ON(!np);
  247. master_irqhost = xilinx_intc_init(np);
  248. BUG_ON(!master_irqhost);
  249. irq_set_default_host(master_irqhost);
  250. of_node_put(np);
  251. xilinx_i8259_setup_cascade();
  252. }