cpm2_pic.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. /*
  2. * Platform information definitions.
  3. *
  4. * Copied from arch/ppc/syslib/cpm2_pic.c with minor subsequent updates
  5. * to make in work in arch/powerpc/. Original (c) belongs to Dan Malek.
  6. *
  7. * Author: Vitaly Bordug <vbordug@ru.mvista.com>
  8. *
  9. * 1999-2001 (c) Dan Malek <dan@embeddedalley.com>
  10. * 2006 (c) MontaVista Software, Inc.
  11. *
  12. * This file is licensed under the terms of the GNU General Public License
  13. * version 2. This program is licensed "as is" without any warranty of any
  14. * kind, whether express or implied.
  15. */
  16. /* The CPM2 internal interrupt controller. It is usually
  17. * the only interrupt controller.
  18. * There are two 32-bit registers (high/low) for up to 64
  19. * possible interrupts.
  20. *
  21. * Now, the fun starts.....Interrupt Numbers DO NOT MAP
  22. * in a simple arithmetic fashion to mask or pending registers.
  23. * That is, interrupt 4 does not map to bit position 4.
  24. * We create two tables, indexed by vector number, to indicate
  25. * which register to use and which bit in the register to use.
  26. */
  27. #include <linux/stddef.h>
  28. #include <linux/sched.h>
  29. #include <linux/signal.h>
  30. #include <linux/irq.h>
  31. #include <linux/irqdomain.h>
  32. #include <asm/immap_cpm2.h>
  33. #include <asm/io.h>
  34. #include "cpm2_pic.h"
  35. /* External IRQS */
  36. #define CPM2_IRQ_EXT1 19
  37. #define CPM2_IRQ_EXT7 25
  38. /* Port C IRQS */
  39. #define CPM2_IRQ_PORTC15 48
  40. #define CPM2_IRQ_PORTC0 63
  41. static intctl_cpm2_t __iomem *cpm2_intctl;
  42. static struct irq_domain *cpm2_pic_host;
  43. static unsigned long ppc_cached_irq_mask[2]; /* 2 32-bit registers */
  44. static const u_char irq_to_siureg[] = {
  45. 1, 1, 1, 1, 1, 1, 1, 1,
  46. 1, 1, 1, 1, 1, 1, 1, 1,
  47. 0, 0, 0, 0, 0, 0, 0, 0,
  48. 0, 0, 0, 0, 0, 0, 0, 0,
  49. 1, 1, 1, 1, 1, 1, 1, 1,
  50. 1, 1, 1, 1, 1, 1, 1, 1,
  51. 0, 0, 0, 0, 0, 0, 0, 0,
  52. 0, 0, 0, 0, 0, 0, 0, 0
  53. };
  54. /* bit numbers do not match the docs, these are precomputed so the bit for
  55. * a given irq is (1 << irq_to_siubit[irq]) */
  56. static const u_char irq_to_siubit[] = {
  57. 0, 15, 14, 13, 12, 11, 10, 9,
  58. 8, 7, 6, 5, 4, 3, 2, 1,
  59. 2, 1, 0, 14, 13, 12, 11, 10,
  60. 9, 8, 7, 6, 5, 4, 3, 0,
  61. 31, 30, 29, 28, 27, 26, 25, 24,
  62. 23, 22, 21, 20, 19, 18, 17, 16,
  63. 16, 17, 18, 19, 20, 21, 22, 23,
  64. 24, 25, 26, 27, 28, 29, 30, 31,
  65. };
  66. static void cpm2_mask_irq(struct irq_data *d)
  67. {
  68. int bit, word;
  69. unsigned int irq_nr = irqd_to_hwirq(d);
  70. bit = irq_to_siubit[irq_nr];
  71. word = irq_to_siureg[irq_nr];
  72. ppc_cached_irq_mask[word] &= ~(1 << bit);
  73. out_be32(&cpm2_intctl->ic_simrh + word, ppc_cached_irq_mask[word]);
  74. }
  75. static void cpm2_unmask_irq(struct irq_data *d)
  76. {
  77. int bit, word;
  78. unsigned int irq_nr = irqd_to_hwirq(d);
  79. bit = irq_to_siubit[irq_nr];
  80. word = irq_to_siureg[irq_nr];
  81. ppc_cached_irq_mask[word] |= 1 << bit;
  82. out_be32(&cpm2_intctl->ic_simrh + word, ppc_cached_irq_mask[word]);
  83. }
  84. static void cpm2_ack(struct irq_data *d)
  85. {
  86. int bit, word;
  87. unsigned int irq_nr = irqd_to_hwirq(d);
  88. bit = irq_to_siubit[irq_nr];
  89. word = irq_to_siureg[irq_nr];
  90. out_be32(&cpm2_intctl->ic_sipnrh + word, 1 << bit);
  91. }
  92. static void cpm2_end_irq(struct irq_data *d)
  93. {
  94. int bit, word;
  95. unsigned int irq_nr = irqd_to_hwirq(d);
  96. bit = irq_to_siubit[irq_nr];
  97. word = irq_to_siureg[irq_nr];
  98. ppc_cached_irq_mask[word] |= 1 << bit;
  99. out_be32(&cpm2_intctl->ic_simrh + word, ppc_cached_irq_mask[word]);
  100. /*
  101. * Work around large numbers of spurious IRQs on PowerPC 82xx
  102. * systems.
  103. */
  104. mb();
  105. }
  106. static int cpm2_set_irq_type(struct irq_data *d, unsigned int flow_type)
  107. {
  108. unsigned int src = irqd_to_hwirq(d);
  109. unsigned int vold, vnew, edibit;
  110. /* Port C interrupts are either IRQ_TYPE_EDGE_FALLING or
  111. * IRQ_TYPE_EDGE_BOTH (default). All others are IRQ_TYPE_EDGE_FALLING
  112. * or IRQ_TYPE_LEVEL_LOW (default)
  113. */
  114. if (src >= CPM2_IRQ_PORTC15 && src <= CPM2_IRQ_PORTC0) {
  115. if (flow_type == IRQ_TYPE_NONE)
  116. flow_type = IRQ_TYPE_EDGE_BOTH;
  117. if (flow_type != IRQ_TYPE_EDGE_BOTH &&
  118. flow_type != IRQ_TYPE_EDGE_FALLING)
  119. goto err_sense;
  120. } else {
  121. if (flow_type == IRQ_TYPE_NONE)
  122. flow_type = IRQ_TYPE_LEVEL_LOW;
  123. if (flow_type & (IRQ_TYPE_EDGE_RISING | IRQ_TYPE_LEVEL_HIGH))
  124. goto err_sense;
  125. }
  126. irqd_set_trigger_type(d, flow_type);
  127. if (flow_type & IRQ_TYPE_LEVEL_LOW)
  128. irq_set_handler_locked(d, handle_level_irq);
  129. else
  130. irq_set_handler_locked(d, handle_edge_irq);
  131. /* internal IRQ senses are LEVEL_LOW
  132. * EXT IRQ and Port C IRQ senses are programmable
  133. */
  134. if (src >= CPM2_IRQ_EXT1 && src <= CPM2_IRQ_EXT7)
  135. edibit = (14 - (src - CPM2_IRQ_EXT1));
  136. else
  137. if (src >= CPM2_IRQ_PORTC15 && src <= CPM2_IRQ_PORTC0)
  138. edibit = (31 - (CPM2_IRQ_PORTC0 - src));
  139. else
  140. return (flow_type & IRQ_TYPE_LEVEL_LOW) ?
  141. IRQ_SET_MASK_OK_NOCOPY : -EINVAL;
  142. vold = in_be32(&cpm2_intctl->ic_siexr);
  143. if ((flow_type & IRQ_TYPE_SENSE_MASK) == IRQ_TYPE_EDGE_FALLING)
  144. vnew = vold | (1 << edibit);
  145. else
  146. vnew = vold & ~(1 << edibit);
  147. if (vold != vnew)
  148. out_be32(&cpm2_intctl->ic_siexr, vnew);
  149. return IRQ_SET_MASK_OK_NOCOPY;
  150. err_sense:
  151. pr_err("CPM2 PIC: sense type 0x%x not supported\n", flow_type);
  152. return -EINVAL;
  153. }
  154. static struct irq_chip cpm2_pic = {
  155. .name = "CPM2 SIU",
  156. .irq_mask = cpm2_mask_irq,
  157. .irq_unmask = cpm2_unmask_irq,
  158. .irq_ack = cpm2_ack,
  159. .irq_eoi = cpm2_end_irq,
  160. .irq_set_type = cpm2_set_irq_type,
  161. .flags = IRQCHIP_EOI_IF_HANDLED,
  162. };
  163. unsigned int cpm2_get_irq(void)
  164. {
  165. int irq;
  166. unsigned long bits;
  167. /* For CPM2, read the SIVEC register and shift the bits down
  168. * to get the irq number. */
  169. bits = in_be32(&cpm2_intctl->ic_sivec);
  170. irq = bits >> 26;
  171. if (irq == 0)
  172. return(-1);
  173. return irq_linear_revmap(cpm2_pic_host, irq);
  174. }
  175. static int cpm2_pic_host_map(struct irq_domain *h, unsigned int virq,
  176. irq_hw_number_t hw)
  177. {
  178. pr_debug("cpm2_pic_host_map(%d, 0x%lx)\n", virq, hw);
  179. irq_set_status_flags(virq, IRQ_LEVEL);
  180. irq_set_chip_and_handler(virq, &cpm2_pic, handle_level_irq);
  181. return 0;
  182. }
  183. static const struct irq_domain_ops cpm2_pic_host_ops = {
  184. .map = cpm2_pic_host_map,
  185. .xlate = irq_domain_xlate_onetwocell,
  186. };
  187. void cpm2_pic_init(struct device_node *node)
  188. {
  189. int i;
  190. cpm2_intctl = &cpm2_immr->im_intctl;
  191. /* Clear the CPM IRQ controller, in case it has any bits set
  192. * from the bootloader
  193. */
  194. /* Mask out everything */
  195. out_be32(&cpm2_intctl->ic_simrh, 0x00000000);
  196. out_be32(&cpm2_intctl->ic_simrl, 0x00000000);
  197. wmb();
  198. /* Ack everything */
  199. out_be32(&cpm2_intctl->ic_sipnrh, 0xffffffff);
  200. out_be32(&cpm2_intctl->ic_sipnrl, 0xffffffff);
  201. wmb();
  202. /* Dummy read of the vector */
  203. i = in_be32(&cpm2_intctl->ic_sivec);
  204. rmb();
  205. /* Initialize the default interrupt mapping priorities,
  206. * in case the boot rom changed something on us.
  207. */
  208. out_be16(&cpm2_intctl->ic_sicr, 0);
  209. out_be32(&cpm2_intctl->ic_scprrh, 0x05309770);
  210. out_be32(&cpm2_intctl->ic_scprrl, 0x05309770);
  211. /* create a legacy host */
  212. cpm2_pic_host = irq_domain_add_linear(node, 64, &cpm2_pic_host_ops, NULL);
  213. if (cpm2_pic_host == NULL) {
  214. printk(KERN_ERR "CPM2 PIC: failed to allocate irq host!\n");
  215. return;
  216. }
  217. }