loki-intc.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Code for Fenrir's Loki interrupt controller.
  4. */
  5. #include <linux/delay.h>
  6. #include <linux/irq.h>
  7. #include <linux/bitops.h>
  8. #include <linux/module.h>
  9. #include <linux/moduleparam.h>
  10. #include <linux/interrupt.h>
  11. #include <linux/irqdomain.h>
  12. #include <linux/irqchip.h>
  13. #include <linux/of.h>
  14. #include <linux/of_address.h>
  15. #include <linux/of_irq.h>
  16. #include <linux/of_platform.h>
  17. #include <linux/irqchip.h>
  18. #include <linux/irqdomain.h>
  19. #include "loki.h"
  20. static void loki_mask_irq(struct irq_data *d)
  21. {
  22. struct loki_drvdata *pdata = irq_data_get_irq_chip_data(d);
  23. u32 reg = pdata->readreg32(pdata, REG_LOKI_INTERRUPT_ENABLE);
  24. pdata->writereg32(pdata, REG_LOKI_INTERRUPT_ENABLE, reg & ~(LOKI_INTERRUPT_DUT0));
  25. }
  26. static void loki_unmask_irq(struct irq_data *d)
  27. {
  28. struct loki_drvdata *pdata = irq_data_get_irq_chip_data(d);
  29. u32 reg = pdata->readreg32(pdata, REG_LOKI_INTERRUPT_ENABLE);
  30. pdata->writereg32(pdata, REG_LOKI_INTERRUPT_ENABLE, reg | LOKI_INTERRUPT_DUT0);
  31. }
  32. static struct irq_chip fenrir_loki = {
  33. .name = "loki-intc",
  34. .irq_mask = loki_mask_irq,
  35. .irq_unmask = loki_unmask_irq,
  36. };
  37. static int loki_intc_map(struct irq_domain *d, unsigned int irq, irq_hw_number_t hw)
  38. {
  39. struct irq_chip *chip = &fenrir_loki;
  40. irq_domain_set_info(d, irq, hw, chip, d->host_data,
  41. handle_level_irq, NULL, NULL);
  42. irq_set_status_flags(irq, IRQ_LEVEL);
  43. return 0;
  44. }
  45. static irqreturn_t loki_isrcb(int irq, void *dev_id)
  46. {
  47. struct platform_device *pdev = (struct platform_device *)dev_id;
  48. struct loki_drvdata *pdata;
  49. u32 reg, mask, timeout;
  50. if (!pdev) {
  51. pr_err("LOKI: pdev not set!?\n");
  52. return IRQ_NONE;
  53. }
  54. pdata = platform_get_drvdata(pdev);
  55. if (!pdata) {
  56. pr_err("LOKI: pdata not set!?\n");
  57. return IRQ_NONE;
  58. }
  59. reg = pdata->readreg32(pdata, REG_LOKI_INTERRUPT_STATUS);
  60. mask = pdata->readreg32(pdata, REG_LOKI_INTERRUPT_ENABLE);
  61. timeout = pdata->readreg32(pdata, REG_LOKI_INTERRUPT_TIMEOUT_CLR);
  62. dev_dbg(&pdev->dev, "Got an interrupt. %X - %X - %X\n", reg, mask, timeout);
  63. /* Check the timeout register just in case */
  64. if (timeout != 0) {
  65. dev_warn(&pdev->dev, "Interrupt timeout fired. Will need to be reset\n");
  66. }
  67. if (reg & mask) {
  68. if (reg & LOKI_INTERRUPT_TESTINT) {
  69. dev_warn(&pdev->dev, "Test interrupt fired! Was it on purpose?\n");
  70. /* Disable the interrupt */
  71. pdata->writereg32(pdata, REG_LOKI_INTERRUPT_TEST, 0);
  72. }
  73. else if (reg & LOKI_INTERRUPT_DUT0) {
  74. int logical_irq_num;
  75. /* trigger registered IRQ, if any */
  76. logical_irq_num = irq_find_mapping(pdata->intc.domain, 0);
  77. generic_handle_irq(logical_irq_num);
  78. }
  79. /* Clear interrupts */
  80. pdata->writereg32(pdata, REG_LOKI_INTERRUPT_CLR, reg);
  81. return IRQ_HANDLED;
  82. }
  83. return IRQ_NONE;
  84. }
  85. static const struct irq_domain_ops loki_intc_ops = {
  86. .xlate = irq_domain_xlate_onecell,
  87. .map = loki_intc_map,
  88. };
  89. int loki_intc_probe(struct platform_device *pdev)
  90. {
  91. struct device *dev = &pdev->dev;
  92. struct device_node *of_node = pdev->dev.of_node;
  93. int ret = 0;
  94. struct loki_drvdata *priv_data = platform_get_drvdata(pdev);;
  95. dev_dbg(dev, "Going to register loki's intc...");
  96. /* Setup the interrupt controller */
  97. priv_data->writereg32(priv_data, REG_LOKI_INTERRUPT_ENABLE, LOKI_INTERRUPT_BASE);
  98. priv_data->writereg32(priv_data, REG_LOKI_INTERRUPT_CLR, LOKI_INTERRUPT_BASE | LOKI_INTERRUPT_DUT0);
  99. priv_data->writereg32(priv_data, REG_LOKI_INTERRUPT_TIMEOUT_CLR, 0x2);
  100. priv_data->intc.irq_num = irq_of_parse_and_map(of_node, 0);
  101. if (priv_data->intc.irq_num == 0) {
  102. dev_err(dev, "Could not map IRQ\n");
  103. ret = -ENXIO;
  104. goto exit;
  105. }
  106. ret = devm_request_irq(dev, priv_data->intc.irq_num, &loki_isrcb, IRQF_SHARED, DEVICE_NAME, pdev);
  107. if (ret) {
  108. dev_err(dev, "Failed to request irq\n");
  109. ret = -ENXIO;
  110. goto exit;
  111. }
  112. priv_data->intc.domain = irq_domain_add_linear(of_node, 1, &loki_intc_ops, priv_data);
  113. if (!priv_data->intc.domain) {
  114. dev_err(dev, "Unable to create IRQ domain\n");
  115. ret = -ENXIO;
  116. goto exit;
  117. }
  118. exit:
  119. return ret;
  120. }
  121. int loki_intc_remove(struct platform_device *pdev)
  122. {
  123. struct loki_drvdata *pdata = platform_get_drvdata(pdev);
  124. irq_dispose_mapping(irq_find_mapping(pdata->intc.domain, 0));
  125. irq_domain_remove(pdata->intc.domain);
  126. return 0;
  127. }