socrates_fpga_pic.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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. static DEFINE_RAW_SPINLOCK(socrates_fpga_pic_lock);
  44. static void __iomem *socrates_fpga_pic_iobase;
  45. static struct irq_domain *socrates_fpga_pic_irq_host;
  46. static unsigned int socrates_fpga_irqs[3];
  47. static inline uint32_t socrates_fpga_pic_read(int reg)
  48. {
  49. return in_be32(socrates_fpga_pic_iobase + reg);
  50. }
  51. static inline void socrates_fpga_pic_write(int reg, uint32_t val)
  52. {
  53. out_be32(socrates_fpga_pic_iobase + reg, val);
  54. }
  55. static inline unsigned int socrates_fpga_pic_get_irq(unsigned int irq)
  56. {
  57. uint32_t cause;
  58. unsigned long flags;
  59. int i;
  60. /* Check irq line routed to the MPIC */
  61. for (i = 0; i < 3; i++) {
  62. if (irq == socrates_fpga_irqs[i])
  63. break;
  64. }
  65. if (i == 3)
  66. return NO_IRQ;
  67. raw_spin_lock_irqsave(&socrates_fpga_pic_lock, flags);
  68. cause = socrates_fpga_pic_read(FPGA_PIC_IRQMASK(i));
  69. raw_spin_unlock_irqrestore(&socrates_fpga_pic_lock, flags);
  70. for (i = SOCRATES_FPGA_NUM_IRQS - 1; i >= 0; i--) {
  71. if (cause >> (i + 16))
  72. break;
  73. }
  74. return irq_linear_revmap(socrates_fpga_pic_irq_host,
  75. (irq_hw_number_t)i);
  76. }
  77. void socrates_fpga_pic_cascade(unsigned int irq, struct irq_desc *desc)
  78. {
  79. struct irq_chip *chip = irq_desc_get_chip(desc);
  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. chip->irq_eoi(&desc->irq_data);
  89. }
  90. static void socrates_fpga_pic_ack(struct irq_data *d)
  91. {
  92. unsigned long flags;
  93. unsigned int irq_line, hwirq = irqd_to_hwirq(d);
  94. uint32_t mask;
  95. irq_line = fpga_irqs[hwirq].irq_line;
  96. raw_spin_lock_irqsave(&socrates_fpga_pic_lock, flags);
  97. mask = socrates_fpga_pic_read(FPGA_PIC_IRQMASK(irq_line))
  98. & SOCRATES_FPGA_IRQ_MASK;
  99. mask |= (1 << (hwirq + 16));
  100. socrates_fpga_pic_write(FPGA_PIC_IRQMASK(irq_line), mask);
  101. raw_spin_unlock_irqrestore(&socrates_fpga_pic_lock, flags);
  102. }
  103. static void socrates_fpga_pic_mask(struct irq_data *d)
  104. {
  105. unsigned long flags;
  106. unsigned int hwirq = irqd_to_hwirq(d);
  107. int irq_line;
  108. u32 mask;
  109. irq_line = fpga_irqs[hwirq].irq_line;
  110. raw_spin_lock_irqsave(&socrates_fpga_pic_lock, flags);
  111. mask = socrates_fpga_pic_read(FPGA_PIC_IRQMASK(irq_line))
  112. & SOCRATES_FPGA_IRQ_MASK;
  113. mask &= ~(1 << hwirq);
  114. socrates_fpga_pic_write(FPGA_PIC_IRQMASK(irq_line), mask);
  115. raw_spin_unlock_irqrestore(&socrates_fpga_pic_lock, flags);
  116. }
  117. static void socrates_fpga_pic_mask_ack(struct irq_data *d)
  118. {
  119. unsigned long flags;
  120. unsigned int hwirq = irqd_to_hwirq(d);
  121. int irq_line;
  122. u32 mask;
  123. irq_line = fpga_irqs[hwirq].irq_line;
  124. raw_spin_lock_irqsave(&socrates_fpga_pic_lock, flags);
  125. mask = socrates_fpga_pic_read(FPGA_PIC_IRQMASK(irq_line))
  126. & SOCRATES_FPGA_IRQ_MASK;
  127. mask &= ~(1 << hwirq);
  128. mask |= (1 << (hwirq + 16));
  129. socrates_fpga_pic_write(FPGA_PIC_IRQMASK(irq_line), mask);
  130. raw_spin_unlock_irqrestore(&socrates_fpga_pic_lock, flags);
  131. }
  132. static void socrates_fpga_pic_unmask(struct irq_data *d)
  133. {
  134. unsigned long flags;
  135. unsigned int hwirq = irqd_to_hwirq(d);
  136. int irq_line;
  137. u32 mask;
  138. irq_line = fpga_irqs[hwirq].irq_line;
  139. raw_spin_lock_irqsave(&socrates_fpga_pic_lock, flags);
  140. mask = socrates_fpga_pic_read(FPGA_PIC_IRQMASK(irq_line))
  141. & SOCRATES_FPGA_IRQ_MASK;
  142. mask |= (1 << hwirq);
  143. socrates_fpga_pic_write(FPGA_PIC_IRQMASK(irq_line), mask);
  144. raw_spin_unlock_irqrestore(&socrates_fpga_pic_lock, flags);
  145. }
  146. static void socrates_fpga_pic_eoi(struct irq_data *d)
  147. {
  148. unsigned long flags;
  149. unsigned int hwirq = irqd_to_hwirq(d);
  150. int irq_line;
  151. u32 mask;
  152. irq_line = fpga_irqs[hwirq].irq_line;
  153. raw_spin_lock_irqsave(&socrates_fpga_pic_lock, flags);
  154. mask = socrates_fpga_pic_read(FPGA_PIC_IRQMASK(irq_line))
  155. & SOCRATES_FPGA_IRQ_MASK;
  156. mask |= (1 << (hwirq + 16));
  157. socrates_fpga_pic_write(FPGA_PIC_IRQMASK(irq_line), mask);
  158. raw_spin_unlock_irqrestore(&socrates_fpga_pic_lock, flags);
  159. }
  160. static int socrates_fpga_pic_set_type(struct irq_data *d,
  161. unsigned int flow_type)
  162. {
  163. unsigned long flags;
  164. unsigned int hwirq = irqd_to_hwirq(d);
  165. int polarity;
  166. u32 mask;
  167. if (fpga_irqs[hwirq].type != IRQ_TYPE_NONE)
  168. return -EINVAL;
  169. switch (flow_type & IRQ_TYPE_SENSE_MASK) {
  170. case IRQ_TYPE_LEVEL_HIGH:
  171. polarity = 1;
  172. break;
  173. case IRQ_TYPE_LEVEL_LOW:
  174. polarity = 0;
  175. break;
  176. default:
  177. return -EINVAL;
  178. }
  179. raw_spin_lock_irqsave(&socrates_fpga_pic_lock, flags);
  180. mask = socrates_fpga_pic_read(FPGA_PIC_IRQCFG);
  181. if (polarity)
  182. mask |= (1 << hwirq);
  183. else
  184. mask &= ~(1 << hwirq);
  185. socrates_fpga_pic_write(FPGA_PIC_IRQCFG, mask);
  186. raw_spin_unlock_irqrestore(&socrates_fpga_pic_lock, flags);
  187. return 0;
  188. }
  189. static struct irq_chip socrates_fpga_pic_chip = {
  190. .name = "FPGA-PIC",
  191. .irq_ack = socrates_fpga_pic_ack,
  192. .irq_mask = socrates_fpga_pic_mask,
  193. .irq_mask_ack = socrates_fpga_pic_mask_ack,
  194. .irq_unmask = socrates_fpga_pic_unmask,
  195. .irq_eoi = socrates_fpga_pic_eoi,
  196. .irq_set_type = socrates_fpga_pic_set_type,
  197. };
  198. static int socrates_fpga_pic_host_map(struct irq_domain *h, unsigned int virq,
  199. irq_hw_number_t hwirq)
  200. {
  201. /* All interrupts are LEVEL sensitive */
  202. irq_set_status_flags(virq, IRQ_LEVEL);
  203. irq_set_chip_and_handler(virq, &socrates_fpga_pic_chip,
  204. handle_fasteoi_irq);
  205. return 0;
  206. }
  207. static int socrates_fpga_pic_host_xlate(struct irq_domain *h,
  208. struct device_node *ct, const u32 *intspec, unsigned int intsize,
  209. irq_hw_number_t *out_hwirq, unsigned int *out_flags)
  210. {
  211. struct socrates_fpga_irq_info *fpga_irq = &fpga_irqs[intspec[0]];
  212. *out_hwirq = intspec[0];
  213. if (fpga_irq->type == IRQ_TYPE_NONE) {
  214. /* type is configurable */
  215. if (intspec[1] != IRQ_TYPE_LEVEL_LOW &&
  216. intspec[1] != IRQ_TYPE_LEVEL_HIGH) {
  217. pr_warning("FPGA PIC: invalid irq type, "
  218. "setting default active low\n");
  219. *out_flags = IRQ_TYPE_LEVEL_LOW;
  220. } else {
  221. *out_flags = intspec[1];
  222. }
  223. } else {
  224. /* type is fixed */
  225. *out_flags = fpga_irq->type;
  226. }
  227. /* Use specified interrupt routing */
  228. if (intspec[2] <= 2)
  229. fpga_irq->irq_line = intspec[2];
  230. else
  231. pr_warning("FPGA PIC: invalid irq routing\n");
  232. return 0;
  233. }
  234. static const struct irq_domain_ops socrates_fpga_pic_host_ops = {
  235. .map = socrates_fpga_pic_host_map,
  236. .xlate = socrates_fpga_pic_host_xlate,
  237. };
  238. void socrates_fpga_pic_init(struct device_node *pic)
  239. {
  240. unsigned long flags;
  241. int i;
  242. /* Setup an irq_domain structure */
  243. socrates_fpga_pic_irq_host = irq_domain_add_linear(pic,
  244. SOCRATES_FPGA_NUM_IRQS, &socrates_fpga_pic_host_ops, NULL);
  245. if (socrates_fpga_pic_irq_host == NULL) {
  246. pr_err("FPGA PIC: Unable to allocate host\n");
  247. return;
  248. }
  249. for (i = 0; i < 3; i++) {
  250. socrates_fpga_irqs[i] = irq_of_parse_and_map(pic, i);
  251. if (socrates_fpga_irqs[i] == NO_IRQ) {
  252. pr_warning("FPGA PIC: can't get irq%d.\n", i);
  253. continue;
  254. }
  255. irq_set_chained_handler(socrates_fpga_irqs[i],
  256. socrates_fpga_pic_cascade);
  257. }
  258. socrates_fpga_pic_iobase = of_iomap(pic, 0);
  259. raw_spin_lock_irqsave(&socrates_fpga_pic_lock, flags);
  260. socrates_fpga_pic_write(FPGA_PIC_IRQMASK(0),
  261. SOCRATES_FPGA_IRQ_MASK << 16);
  262. socrates_fpga_pic_write(FPGA_PIC_IRQMASK(1),
  263. SOCRATES_FPGA_IRQ_MASK << 16);
  264. socrates_fpga_pic_write(FPGA_PIC_IRQMASK(2),
  265. SOCRATES_FPGA_IRQ_MASK << 16);
  266. raw_spin_unlock_irqrestore(&socrates_fpga_pic_lock, flags);
  267. pr_info("FPGA PIC: Setting up Socrates FPGA PIC\n");
  268. }