xilinx_intc.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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_host *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(unsigned int virq)
  65. {
  66. int irq = virq_to_hw(virq);
  67. void * regs = get_irq_chip_data(virq);
  68. pr_debug("mask: %d\n", irq);
  69. out_be32(regs + XINTC_CIE, 1 << irq);
  70. }
  71. static int xilinx_intc_set_type(unsigned int virq, unsigned int flow_type)
  72. {
  73. struct irq_desc *desc = get_irq_desc(virq);
  74. desc->status &= ~(IRQ_TYPE_SENSE_MASK | IRQ_LEVEL);
  75. desc->status |= flow_type & IRQ_TYPE_SENSE_MASK;
  76. if (flow_type & (IRQ_TYPE_LEVEL_HIGH | IRQ_TYPE_LEVEL_LOW))
  77. desc->status |= IRQ_LEVEL;
  78. return 0;
  79. }
  80. /*
  81. * IRQ Chip level operations
  82. */
  83. static void xilinx_intc_level_unmask(unsigned int virq)
  84. {
  85. int irq = virq_to_hw(virq);
  86. void * regs = get_irq_chip_data(virq);
  87. pr_debug("unmask: %d\n", irq);
  88. out_be32(regs + XINTC_SIE, 1 << irq);
  89. /* ack level irqs because they can't be acked during
  90. * ack function since the handle_level_irq function
  91. * acks the irq before calling the inerrupt handler
  92. */
  93. out_be32(regs + XINTC_IAR, 1 << irq);
  94. }
  95. static struct irq_chip xilinx_intc_level_irqchip = {
  96. .typename = "Xilinx Level INTC",
  97. .mask = xilinx_intc_mask,
  98. .mask_ack = xilinx_intc_mask,
  99. .unmask = xilinx_intc_level_unmask,
  100. .set_type = xilinx_intc_set_type,
  101. };
  102. /*
  103. * IRQ Chip edge operations
  104. */
  105. static void xilinx_intc_edge_unmask(unsigned int virq)
  106. {
  107. int irq = virq_to_hw(virq);
  108. void *regs = get_irq_chip_data(virq);
  109. pr_debug("unmask: %d\n", irq);
  110. out_be32(regs + XINTC_SIE, 1 << irq);
  111. }
  112. static void xilinx_intc_edge_ack(unsigned int virq)
  113. {
  114. int irq = virq_to_hw(virq);
  115. void * regs = get_irq_chip_data(virq);
  116. pr_debug("ack: %d\n", irq);
  117. out_be32(regs + XINTC_IAR, 1 << irq);
  118. }
  119. static struct irq_chip xilinx_intc_edge_irqchip = {
  120. .typename = "Xilinx Edge INTC",
  121. .mask = xilinx_intc_mask,
  122. .unmask = xilinx_intc_edge_unmask,
  123. .ack = xilinx_intc_edge_ack,
  124. .set_type = xilinx_intc_set_type,
  125. };
  126. /*
  127. * IRQ Host operations
  128. */
  129. /**
  130. * xilinx_intc_xlate - translate virq# from device tree interrupts property
  131. */
  132. static int xilinx_intc_xlate(struct irq_host *h, struct device_node *ct,
  133. u32 *intspec, unsigned int intsize,
  134. irq_hw_number_t *out_hwirq,
  135. unsigned int *out_flags)
  136. {
  137. if ((intsize < 2) || (intspec[0] >= XILINX_INTC_MAXIRQS))
  138. return -EINVAL;
  139. /* keep a copy of the interrupt type til the interrupt is mapped
  140. */
  141. xilinx_intc_typetable[intspec[0]] = xilinx_intc_map_senses[intspec[1]];
  142. /* Xilinx uses 2 interrupt entries, the 1st being the h/w
  143. * interrupt number, the 2nd being the interrupt type, edge or level
  144. */
  145. *out_hwirq = intspec[0];
  146. *out_flags = xilinx_intc_map_senses[intspec[1]];
  147. return 0;
  148. }
  149. static int xilinx_intc_map(struct irq_host *h, unsigned int virq,
  150. irq_hw_number_t irq)
  151. {
  152. set_irq_chip_data(virq, h->host_data);
  153. if (xilinx_intc_typetable[irq] == IRQ_TYPE_LEVEL_HIGH ||
  154. xilinx_intc_typetable[irq] == IRQ_TYPE_LEVEL_LOW) {
  155. set_irq_chip_and_handler(virq, &xilinx_intc_level_irqchip,
  156. handle_level_irq);
  157. } else {
  158. set_irq_chip_and_handler(virq, &xilinx_intc_edge_irqchip,
  159. handle_edge_irq);
  160. }
  161. return 0;
  162. }
  163. static struct irq_host_ops xilinx_intc_ops = {
  164. .map = xilinx_intc_map,
  165. .xlate = xilinx_intc_xlate,
  166. };
  167. struct irq_host * __init
  168. xilinx_intc_init(struct device_node *np)
  169. {
  170. struct irq_host * irq;
  171. void * regs;
  172. /* Find and map the intc registers */
  173. regs = of_iomap(np, 0);
  174. if (!regs) {
  175. pr_err("xilinx_intc: could not map registers\n");
  176. return NULL;
  177. }
  178. /* Setup interrupt controller */
  179. out_be32(regs + XINTC_IER, 0); /* disable all irqs */
  180. out_be32(regs + XINTC_IAR, ~(u32) 0); /* Acknowledge pending irqs */
  181. out_be32(regs + XINTC_MER, 0x3UL); /* Turn on the Master Enable. */
  182. /* Allocate and initialize an irq_host structure. */
  183. irq = irq_alloc_host(np, IRQ_HOST_MAP_LINEAR, XILINX_INTC_MAXIRQS,
  184. &xilinx_intc_ops, -1);
  185. if (!irq)
  186. panic(__FILE__ ": Cannot allocate IRQ host\n");
  187. irq->host_data = regs;
  188. return irq;
  189. }
  190. int xilinx_intc_get_irq(void)
  191. {
  192. void * regs = master_irqhost->host_data;
  193. pr_debug("get_irq:\n");
  194. return irq_linear_revmap(master_irqhost, in_be32(regs + XINTC_IVR));
  195. }
  196. #if defined(CONFIG_PPC_I8259)
  197. /*
  198. * Support code for cascading to 8259 interrupt controllers
  199. */
  200. static void xilinx_i8259_cascade(unsigned int irq, struct irq_desc *desc)
  201. {
  202. unsigned int cascade_irq = i8259_irq();
  203. if (cascade_irq)
  204. generic_handle_irq(cascade_irq);
  205. /* Let xilinx_intc end the interrupt */
  206. desc->chip->ack(irq);
  207. desc->chip->unmask(irq);
  208. }
  209. static void __init xilinx_i8259_setup_cascade(void)
  210. {
  211. struct device_node *cascade_node;
  212. int cascade_irq;
  213. /* Initialize i8259 controller */
  214. cascade_node = of_find_compatible_node(NULL, NULL, "chrp,iic");
  215. if (!cascade_node)
  216. return;
  217. cascade_irq = irq_of_parse_and_map(cascade_node, 0);
  218. if (!cascade_irq) {
  219. pr_err("virtex_ml510: Failed to map cascade interrupt\n");
  220. goto out;
  221. }
  222. i8259_init(cascade_node, 0);
  223. set_irq_chained_handler(cascade_irq, xilinx_i8259_cascade);
  224. /* Program irq 7 (usb/audio), 14/15 (ide) to level sensitive */
  225. /* This looks like a dirty hack to me --gcl */
  226. outb(0xc0, 0x4d0);
  227. outb(0xc0, 0x4d1);
  228. out:
  229. of_node_put(cascade_node);
  230. }
  231. #else
  232. static inline void xilinx_i8259_setup_cascade(void) { return; }
  233. #endif /* defined(CONFIG_PPC_I8259) */
  234. static struct of_device_id xilinx_intc_match[] __initconst = {
  235. { .compatible = "xlnx,opb-intc-1.00.c", },
  236. { .compatible = "xlnx,xps-intc-1.00.a", },
  237. {}
  238. };
  239. /*
  240. * Initialize master Xilinx interrupt controller
  241. */
  242. void __init xilinx_intc_init_tree(void)
  243. {
  244. struct device_node *np;
  245. /* find top level interrupt controller */
  246. for_each_matching_node(np, xilinx_intc_match) {
  247. if (!of_get_property(np, "interrupts", NULL))
  248. break;
  249. }
  250. BUG_ON(!np);
  251. master_irqhost = xilinx_intc_init(np);
  252. BUG_ON(!master_irqhost);
  253. irq_set_default_host(master_irqhost);
  254. of_node_put(np);
  255. xilinx_i8259_setup_cascade();
  256. }