xilinx_intc.c 7.6 KB

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