socrates_fpga_pic.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. /*
  2. * Copyright (C) 2008 Ilya Yanok, Emcraft Systems
  3. *
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. *
  9. */
  10. #include <linux/irq.h>
  11. #include <linux/of_platform.h>
  12. #include <linux/io.h>
  13. /*
  14. * The FPGA supports 9 interrupt sources, which can be routed to 3
  15. * interrupt request lines of the MPIC. The line to be used can be
  16. * specified through the third cell of FDT property "interrupts".
  17. */
  18. #define SOCRATES_FPGA_NUM_IRQS 9
  19. #define FPGA_PIC_IRQCFG (0x0)
  20. #define FPGA_PIC_IRQMASK(n) (0x4 + 0x4 * (n))
  21. #define SOCRATES_FPGA_IRQ_MASK ((1 << SOCRATES_FPGA_NUM_IRQS) - 1)
  22. struct socrates_fpga_irq_info {
  23. unsigned int irq_line;
  24. int type;
  25. };
  26. /*
  27. * Interrupt routing and type table
  28. *
  29. * IRQ_TYPE_NONE means the interrupt type is configurable,
  30. * otherwise it's fixed to the specified value.
  31. */
  32. static struct socrates_fpga_irq_info fpga_irqs[SOCRATES_FPGA_NUM_IRQS] = {
  33. [0] = {0, IRQ_TYPE_NONE},
  34. [1] = {0, IRQ_TYPE_LEVEL_HIGH},
  35. [2] = {0, IRQ_TYPE_LEVEL_LOW},
  36. [3] = {0, IRQ_TYPE_NONE},
  37. [4] = {0, IRQ_TYPE_NONE},
  38. [5] = {0, IRQ_TYPE_NONE},
  39. [6] = {0, IRQ_TYPE_NONE},
  40. [7] = {0, IRQ_TYPE_NONE},
  41. [8] = {0, IRQ_TYPE_LEVEL_HIGH},
  42. };
  43. #define socrates_fpga_irq_to_hw(virq) ((unsigned int)irq_map[virq].hwirq)
  44. static DEFINE_SPINLOCK(socrates_fpga_pic_lock);
  45. static void __iomem *socrates_fpga_pic_iobase;
  46. static struct irq_host *socrates_fpga_pic_irq_host;
  47. static unsigned int socrates_fpga_irqs[3];
  48. static inline uint32_t socrates_fpga_pic_read(int reg)
  49. {
  50. return in_be32(socrates_fpga_pic_iobase + reg);
  51. }
  52. static inline void socrates_fpga_pic_write(int reg, uint32_t val)
  53. {
  54. out_be32(socrates_fpga_pic_iobase + reg, val);
  55. }
  56. static inline unsigned int socrates_fpga_pic_get_irq(unsigned int irq)
  57. {
  58. uint32_t cause;
  59. unsigned long flags;
  60. int i;
  61. /* Check irq line routed to the MPIC */
  62. for (i = 0; i < 3; i++) {
  63. if (irq == socrates_fpga_irqs[i])
  64. break;
  65. }
  66. if (i == 3)
  67. return NO_IRQ;
  68. spin_lock_irqsave(&socrates_fpga_pic_lock, flags);
  69. cause = socrates_fpga_pic_read(FPGA_PIC_IRQMASK(i));
  70. spin_unlock_irqrestore(&socrates_fpga_pic_lock, flags);
  71. for (i = SOCRATES_FPGA_NUM_IRQS - 1; i >= 0; i--) {
  72. if (cause >> (i + 16))
  73. break;
  74. }
  75. return irq_linear_revmap(socrates_fpga_pic_irq_host,
  76. (irq_hw_number_t)i);
  77. }
  78. void socrates_fpga_pic_cascade(unsigned int irq, struct irq_desc *desc)
  79. {
  80. unsigned int cascade_irq;
  81. /*
  82. * See if we actually have an interrupt, call generic handling code if
  83. * we do.
  84. */
  85. cascade_irq = socrates_fpga_pic_get_irq(irq);
  86. if (cascade_irq != NO_IRQ)
  87. generic_handle_irq(cascade_irq);
  88. desc->chip->eoi(irq);
  89. }
  90. static void socrates_fpga_pic_ack(unsigned int virq)
  91. {
  92. unsigned long flags;
  93. unsigned int hwirq, irq_line;
  94. uint32_t mask;
  95. hwirq = socrates_fpga_irq_to_hw(virq);
  96. irq_line = fpga_irqs[hwirq].irq_line;
  97. spin_lock_irqsave(&socrates_fpga_pic_lock, flags);
  98. mask = socrates_fpga_pic_read(FPGA_PIC_IRQMASK(irq_line))
  99. & SOCRATES_FPGA_IRQ_MASK;
  100. mask |= (1 << (hwirq + 16));
  101. socrates_fpga_pic_write(FPGA_PIC_IRQMASK(irq_line), mask);
  102. spin_unlock_irqrestore(&socrates_fpga_pic_lock, flags);
  103. }
  104. static void socrates_fpga_pic_mask(unsigned int virq)
  105. {
  106. unsigned long flags;
  107. unsigned int hwirq;
  108. int irq_line;
  109. u32 mask;
  110. hwirq = socrates_fpga_irq_to_hw(virq);
  111. irq_line = fpga_irqs[hwirq].irq_line;
  112. spin_lock_irqsave(&socrates_fpga_pic_lock, flags);
  113. mask = socrates_fpga_pic_read(FPGA_PIC_IRQMASK(irq_line))
  114. & SOCRATES_FPGA_IRQ_MASK;
  115. mask &= ~(1 << hwirq);
  116. socrates_fpga_pic_write(FPGA_PIC_IRQMASK(irq_line), mask);
  117. spin_unlock_irqrestore(&socrates_fpga_pic_lock, flags);
  118. }
  119. static void socrates_fpga_pic_mask_ack(unsigned int virq)
  120. {
  121. unsigned long flags;
  122. unsigned int hwirq;
  123. int irq_line;
  124. u32 mask;
  125. hwirq = socrates_fpga_irq_to_hw(virq);
  126. irq_line = fpga_irqs[hwirq].irq_line;
  127. spin_lock_irqsave(&socrates_fpga_pic_lock, flags);
  128. mask = socrates_fpga_pic_read(FPGA_PIC_IRQMASK(irq_line))
  129. & SOCRATES_FPGA_IRQ_MASK;
  130. mask &= ~(1 << hwirq);
  131. mask |= (1 << (hwirq + 16));
  132. socrates_fpga_pic_write(FPGA_PIC_IRQMASK(irq_line), mask);
  133. spin_unlock_irqrestore(&socrates_fpga_pic_lock, flags);
  134. }
  135. static void socrates_fpga_pic_unmask(unsigned int virq)
  136. {
  137. unsigned long flags;
  138. unsigned int hwirq;
  139. int irq_line;
  140. u32 mask;
  141. hwirq = socrates_fpga_irq_to_hw(virq);
  142. irq_line = fpga_irqs[hwirq].irq_line;
  143. spin_lock_irqsave(&socrates_fpga_pic_lock, flags);
  144. mask = socrates_fpga_pic_read(FPGA_PIC_IRQMASK(irq_line))
  145. & SOCRATES_FPGA_IRQ_MASK;
  146. mask |= (1 << hwirq);
  147. socrates_fpga_pic_write(FPGA_PIC_IRQMASK(irq_line), mask);
  148. spin_unlock_irqrestore(&socrates_fpga_pic_lock, flags);
  149. }
  150. static void socrates_fpga_pic_eoi(unsigned int virq)
  151. {
  152. unsigned long flags;
  153. unsigned int hwirq;
  154. int irq_line;
  155. u32 mask;
  156. hwirq = socrates_fpga_irq_to_hw(virq);
  157. irq_line = fpga_irqs[hwirq].irq_line;
  158. spin_lock_irqsave(&socrates_fpga_pic_lock, flags);
  159. mask = socrates_fpga_pic_read(FPGA_PIC_IRQMASK(irq_line))
  160. & SOCRATES_FPGA_IRQ_MASK;
  161. mask |= (1 << (hwirq + 16));
  162. socrates_fpga_pic_write(FPGA_PIC_IRQMASK(irq_line), mask);
  163. spin_unlock_irqrestore(&socrates_fpga_pic_lock, flags);
  164. }
  165. static int socrates_fpga_pic_set_type(unsigned int virq,
  166. unsigned int flow_type)
  167. {
  168. unsigned long flags;
  169. unsigned int hwirq;
  170. int polarity;
  171. u32 mask;
  172. hwirq = socrates_fpga_irq_to_hw(virq);
  173. if (fpga_irqs[hwirq].type != IRQ_TYPE_NONE)
  174. return -EINVAL;
  175. switch (flow_type & IRQ_TYPE_SENSE_MASK) {
  176. case IRQ_TYPE_LEVEL_HIGH:
  177. polarity = 1;
  178. break;
  179. case IRQ_TYPE_LEVEL_LOW:
  180. polarity = 0;
  181. break;
  182. default:
  183. return -EINVAL;
  184. }
  185. spin_lock_irqsave(&socrates_fpga_pic_lock, flags);
  186. mask = socrates_fpga_pic_read(FPGA_PIC_IRQCFG);
  187. if (polarity)
  188. mask |= (1 << hwirq);
  189. else
  190. mask &= ~(1 << hwirq);
  191. socrates_fpga_pic_write(FPGA_PIC_IRQCFG, mask);
  192. spin_unlock_irqrestore(&socrates_fpga_pic_lock, flags);
  193. return 0;
  194. }
  195. static struct irq_chip socrates_fpga_pic_chip = {
  196. .typename = " FPGA-PIC ",
  197. .ack = socrates_fpga_pic_ack,
  198. .mask = socrates_fpga_pic_mask,
  199. .mask_ack = socrates_fpga_pic_mask_ack,
  200. .unmask = socrates_fpga_pic_unmask,
  201. .eoi = socrates_fpga_pic_eoi,
  202. .set_type = socrates_fpga_pic_set_type,
  203. };
  204. static int socrates_fpga_pic_host_map(struct irq_host *h, unsigned int virq,
  205. irq_hw_number_t hwirq)
  206. {
  207. /* All interrupts are LEVEL sensitive */
  208. get_irq_desc(virq)->status |= IRQ_LEVEL;
  209. set_irq_chip_and_handler(virq, &socrates_fpga_pic_chip,
  210. handle_fasteoi_irq);
  211. return 0;
  212. }
  213. static int socrates_fpga_pic_host_xlate(struct irq_host *h,
  214. struct device_node *ct, u32 *intspec, unsigned int intsize,
  215. irq_hw_number_t *out_hwirq, unsigned int *out_flags)
  216. {
  217. struct socrates_fpga_irq_info *fpga_irq = &fpga_irqs[intspec[0]];
  218. *out_hwirq = intspec[0];
  219. if (fpga_irq->type == IRQ_TYPE_NONE) {
  220. /* type is configurable */
  221. if (intspec[1] != IRQ_TYPE_LEVEL_LOW &&
  222. intspec[1] != IRQ_TYPE_LEVEL_HIGH) {
  223. pr_warning("FPGA PIC: invalid irq type, "
  224. "setting default active low\n");
  225. *out_flags = IRQ_TYPE_LEVEL_LOW;
  226. } else {
  227. *out_flags = intspec[1];
  228. }
  229. } else {
  230. /* type is fixed */
  231. *out_flags = fpga_irq->type;
  232. }
  233. /* Use specified interrupt routing */
  234. if (intspec[2] <= 2)
  235. fpga_irq->irq_line = intspec[2];
  236. else
  237. pr_warning("FPGA PIC: invalid irq routing\n");
  238. return 0;
  239. }
  240. static struct irq_host_ops socrates_fpga_pic_host_ops = {
  241. .map = socrates_fpga_pic_host_map,
  242. .xlate = socrates_fpga_pic_host_xlate,
  243. };
  244. void socrates_fpga_pic_init(struct device_node *pic)
  245. {
  246. unsigned long flags;
  247. int i;
  248. /* Setup an irq_host structure */
  249. socrates_fpga_pic_irq_host = irq_alloc_host(pic, IRQ_HOST_MAP_LINEAR,
  250. SOCRATES_FPGA_NUM_IRQS, &socrates_fpga_pic_host_ops,
  251. SOCRATES_FPGA_NUM_IRQS);
  252. if (socrates_fpga_pic_irq_host == NULL) {
  253. pr_err("FPGA PIC: Unable to allocate host\n");
  254. return;
  255. }
  256. for (i = 0; i < 3; i++) {
  257. socrates_fpga_irqs[i] = irq_of_parse_and_map(pic, i);
  258. if (socrates_fpga_irqs[i] == NO_IRQ) {
  259. pr_warning("FPGA PIC: can't get irq%d.\n", i);
  260. continue;
  261. }
  262. set_irq_chained_handler(socrates_fpga_irqs[i],
  263. socrates_fpga_pic_cascade);
  264. }
  265. socrates_fpga_pic_iobase = of_iomap(pic, 0);
  266. spin_lock_irqsave(&socrates_fpga_pic_lock, flags);
  267. socrates_fpga_pic_write(FPGA_PIC_IRQMASK(0),
  268. SOCRATES_FPGA_IRQ_MASK << 16);
  269. socrates_fpga_pic_write(FPGA_PIC_IRQMASK(1),
  270. SOCRATES_FPGA_IRQ_MASK << 16);
  271. socrates_fpga_pic_write(FPGA_PIC_IRQMASK(2),
  272. SOCRATES_FPGA_IRQ_MASK << 16);
  273. spin_unlock_irqrestore(&socrates_fpga_pic_lock, flags);
  274. pr_info("FPGA PIC: Setting up Socrates FPGA PIC\n");
  275. }