pq2ads-pci-pic.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /*
  2. * PQ2 ADS-style PCI interrupt controller
  3. *
  4. * Copyright 2007 Freescale Semiconductor, Inc.
  5. * Author: Scott Wood <scottwood@freescale.com>
  6. *
  7. * Loosely based on mpc82xx ADS support by Vitaly Bordug <vbordug@ru.mvista.com>
  8. * Copyright (c) 2006 MontaVista Software, Inc.
  9. *
  10. * This program is free software; you can redistribute it and/or modify it
  11. * under the terms of the GNU General Public License version 2 as published
  12. * by the Free Software Foundation.
  13. */
  14. #include <linux/init.h>
  15. #include <linux/spinlock.h>
  16. #include <linux/irq.h>
  17. #include <linux/types.h>
  18. #include <linux/slab.h>
  19. #include <asm/io.h>
  20. #include <asm/prom.h>
  21. #include <asm/cpm2.h>
  22. #include "pq2.h"
  23. static DEFINE_RAW_SPINLOCK(pci_pic_lock);
  24. struct pq2ads_pci_pic {
  25. struct device_node *node;
  26. struct irq_domain *host;
  27. struct {
  28. u32 stat;
  29. u32 mask;
  30. } __iomem *regs;
  31. };
  32. #define NUM_IRQS 32
  33. static void pq2ads_pci_mask_irq(struct irq_data *d)
  34. {
  35. struct pq2ads_pci_pic *priv = irq_data_get_irq_chip_data(d);
  36. int irq = NUM_IRQS - irqd_to_hwirq(d) - 1;
  37. if (irq != -1) {
  38. unsigned long flags;
  39. raw_spin_lock_irqsave(&pci_pic_lock, flags);
  40. setbits32(&priv->regs->mask, 1 << irq);
  41. mb();
  42. raw_spin_unlock_irqrestore(&pci_pic_lock, flags);
  43. }
  44. }
  45. static void pq2ads_pci_unmask_irq(struct irq_data *d)
  46. {
  47. struct pq2ads_pci_pic *priv = irq_data_get_irq_chip_data(d);
  48. int irq = NUM_IRQS - irqd_to_hwirq(d) - 1;
  49. if (irq != -1) {
  50. unsigned long flags;
  51. raw_spin_lock_irqsave(&pci_pic_lock, flags);
  52. clrbits32(&priv->regs->mask, 1 << irq);
  53. raw_spin_unlock_irqrestore(&pci_pic_lock, flags);
  54. }
  55. }
  56. static struct irq_chip pq2ads_pci_ic = {
  57. .name = "PQ2 ADS PCI",
  58. .irq_mask = pq2ads_pci_mask_irq,
  59. .irq_mask_ack = pq2ads_pci_mask_irq,
  60. .irq_ack = pq2ads_pci_mask_irq,
  61. .irq_unmask = pq2ads_pci_unmask_irq,
  62. .irq_enable = pq2ads_pci_unmask_irq,
  63. .irq_disable = pq2ads_pci_mask_irq
  64. };
  65. static void pq2ads_pci_irq_demux(struct irq_desc *desc)
  66. {
  67. struct pq2ads_pci_pic *priv = irq_desc_get_handler_data(desc);
  68. u32 stat, mask, pend;
  69. int bit;
  70. for (;;) {
  71. stat = in_be32(&priv->regs->stat);
  72. mask = in_be32(&priv->regs->mask);
  73. pend = stat & ~mask;
  74. if (!pend)
  75. break;
  76. for (bit = 0; pend != 0; ++bit, pend <<= 1) {
  77. if (pend & 0x80000000) {
  78. int virq = irq_linear_revmap(priv->host, bit);
  79. generic_handle_irq(virq);
  80. }
  81. }
  82. }
  83. }
  84. static int pci_pic_host_map(struct irq_domain *h, unsigned int virq,
  85. irq_hw_number_t hw)
  86. {
  87. irq_set_status_flags(virq, IRQ_LEVEL);
  88. irq_set_chip_data(virq, h->host_data);
  89. irq_set_chip_and_handler(virq, &pq2ads_pci_ic, handle_level_irq);
  90. return 0;
  91. }
  92. static const struct irq_domain_ops pci_pic_host_ops = {
  93. .map = pci_pic_host_map,
  94. };
  95. int __init pq2ads_pci_init_irq(void)
  96. {
  97. struct pq2ads_pci_pic *priv;
  98. struct irq_domain *host;
  99. struct device_node *np;
  100. int ret = -ENODEV;
  101. int irq;
  102. np = of_find_compatible_node(NULL, NULL, "fsl,pq2ads-pci-pic");
  103. if (!np) {
  104. printk(KERN_ERR "No pci pic node in device tree.\n");
  105. of_node_put(np);
  106. goto out;
  107. }
  108. irq = irq_of_parse_and_map(np, 0);
  109. if (irq == NO_IRQ) {
  110. printk(KERN_ERR "No interrupt in pci pic node.\n");
  111. of_node_put(np);
  112. goto out;
  113. }
  114. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  115. if (!priv) {
  116. of_node_put(np);
  117. ret = -ENOMEM;
  118. goto out_unmap_irq;
  119. }
  120. /* PCI interrupt controller registers: status and mask */
  121. priv->regs = of_iomap(np, 0);
  122. if (!priv->regs) {
  123. printk(KERN_ERR "Cannot map PCI PIC registers.\n");
  124. goto out_free_kmalloc;
  125. }
  126. /* mask all PCI interrupts */
  127. out_be32(&priv->regs->mask, ~0);
  128. mb();
  129. host = irq_domain_add_linear(np, NUM_IRQS, &pci_pic_host_ops, priv);
  130. if (!host) {
  131. ret = -ENOMEM;
  132. goto out_unmap_regs;
  133. }
  134. priv->host = host;
  135. irq_set_handler_data(irq, priv);
  136. irq_set_chained_handler(irq, pq2ads_pci_irq_demux);
  137. of_node_put(np);
  138. return 0;
  139. out_unmap_regs:
  140. iounmap(priv->regs);
  141. out_free_kmalloc:
  142. kfree(priv);
  143. of_node_put(np);
  144. out_unmap_irq:
  145. irq_dispose_mapping(irq);
  146. out:
  147. return ret;
  148. }