irq-ls1x.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2019, Jiaxun Yang <[email protected]>
  4. * Loongson-1 platform IRQ support
  5. */
  6. #include <linux/errno.h>
  7. #include <linux/init.h>
  8. #include <linux/types.h>
  9. #include <linux/interrupt.h>
  10. #include <linux/ioport.h>
  11. #include <linux/irqchip.h>
  12. #include <linux/of_address.h>
  13. #include <linux/of_irq.h>
  14. #include <linux/io.h>
  15. #include <linux/irqchip/chained_irq.h>
  16. #define LS_REG_INTC_STATUS 0x00
  17. #define LS_REG_INTC_EN 0x04
  18. #define LS_REG_INTC_SET 0x08
  19. #define LS_REG_INTC_CLR 0x0c
  20. #define LS_REG_INTC_POL 0x10
  21. #define LS_REG_INTC_EDGE 0x14
  22. /**
  23. * struct ls1x_intc_priv - private ls1x-intc data.
  24. * @domain: IRQ domain.
  25. * @intc_base: IO Base of intc registers.
  26. */
  27. struct ls1x_intc_priv {
  28. struct irq_domain *domain;
  29. void __iomem *intc_base;
  30. };
  31. static void ls1x_chained_handle_irq(struct irq_desc *desc)
  32. {
  33. struct ls1x_intc_priv *priv = irq_desc_get_handler_data(desc);
  34. struct irq_chip *chip = irq_desc_get_chip(desc);
  35. u32 pending;
  36. chained_irq_enter(chip, desc);
  37. pending = readl(priv->intc_base + LS_REG_INTC_STATUS) &
  38. readl(priv->intc_base + LS_REG_INTC_EN);
  39. if (!pending)
  40. spurious_interrupt();
  41. while (pending) {
  42. int bit = __ffs(pending);
  43. generic_handle_domain_irq(priv->domain, bit);
  44. pending &= ~BIT(bit);
  45. }
  46. chained_irq_exit(chip, desc);
  47. }
  48. static void ls_intc_set_bit(struct irq_chip_generic *gc,
  49. unsigned int offset,
  50. u32 mask, bool set)
  51. {
  52. if (set)
  53. writel(readl(gc->reg_base + offset) | mask,
  54. gc->reg_base + offset);
  55. else
  56. writel(readl(gc->reg_base + offset) & ~mask,
  57. gc->reg_base + offset);
  58. }
  59. static int ls_intc_set_type(struct irq_data *data, unsigned int type)
  60. {
  61. struct irq_chip_generic *gc = irq_data_get_irq_chip_data(data);
  62. u32 mask = data->mask;
  63. switch (type) {
  64. case IRQ_TYPE_LEVEL_HIGH:
  65. ls_intc_set_bit(gc, LS_REG_INTC_EDGE, mask, false);
  66. ls_intc_set_bit(gc, LS_REG_INTC_POL, mask, true);
  67. break;
  68. case IRQ_TYPE_LEVEL_LOW:
  69. ls_intc_set_bit(gc, LS_REG_INTC_EDGE, mask, false);
  70. ls_intc_set_bit(gc, LS_REG_INTC_POL, mask, false);
  71. break;
  72. case IRQ_TYPE_EDGE_RISING:
  73. ls_intc_set_bit(gc, LS_REG_INTC_EDGE, mask, true);
  74. ls_intc_set_bit(gc, LS_REG_INTC_POL, mask, true);
  75. break;
  76. case IRQ_TYPE_EDGE_FALLING:
  77. ls_intc_set_bit(gc, LS_REG_INTC_EDGE, mask, true);
  78. ls_intc_set_bit(gc, LS_REG_INTC_POL, mask, false);
  79. break;
  80. default:
  81. return -EINVAL;
  82. }
  83. irqd_set_trigger_type(data, type);
  84. return irq_setup_alt_chip(data, type);
  85. }
  86. static int __init ls1x_intc_of_init(struct device_node *node,
  87. struct device_node *parent)
  88. {
  89. struct irq_chip_generic *gc;
  90. struct irq_chip_type *ct;
  91. struct ls1x_intc_priv *priv;
  92. int parent_irq, err = 0;
  93. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  94. if (!priv)
  95. return -ENOMEM;
  96. priv->intc_base = of_iomap(node, 0);
  97. if (!priv->intc_base) {
  98. err = -ENODEV;
  99. goto out_free_priv;
  100. }
  101. parent_irq = irq_of_parse_and_map(node, 0);
  102. if (!parent_irq) {
  103. pr_err("ls1x-irq: unable to get parent irq\n");
  104. err = -ENODEV;
  105. goto out_iounmap;
  106. }
  107. /* Set up an IRQ domain */
  108. priv->domain = irq_domain_add_linear(node, 32, &irq_generic_chip_ops,
  109. NULL);
  110. if (!priv->domain) {
  111. pr_err("ls1x-irq: cannot add IRQ domain\n");
  112. err = -ENOMEM;
  113. goto out_iounmap;
  114. }
  115. err = irq_alloc_domain_generic_chips(priv->domain, 32, 2,
  116. node->full_name, handle_level_irq,
  117. IRQ_NOREQUEST | IRQ_NOPROBE | IRQ_NOAUTOEN, 0,
  118. IRQ_GC_INIT_MASK_CACHE);
  119. if (err) {
  120. pr_err("ls1x-irq: unable to register IRQ domain\n");
  121. goto out_free_domain;
  122. }
  123. /* Mask all irqs */
  124. writel(0x0, priv->intc_base + LS_REG_INTC_EN);
  125. /* Ack all irqs */
  126. writel(0xffffffff, priv->intc_base + LS_REG_INTC_CLR);
  127. /* Set all irqs to high level triggered */
  128. writel(0xffffffff, priv->intc_base + LS_REG_INTC_POL);
  129. gc = irq_get_domain_generic_chip(priv->domain, 0);
  130. gc->reg_base = priv->intc_base;
  131. ct = gc->chip_types;
  132. ct[0].type = IRQ_TYPE_LEVEL_MASK;
  133. ct[0].regs.mask = LS_REG_INTC_EN;
  134. ct[0].regs.ack = LS_REG_INTC_CLR;
  135. ct[0].chip.irq_unmask = irq_gc_mask_set_bit;
  136. ct[0].chip.irq_mask = irq_gc_mask_clr_bit;
  137. ct[0].chip.irq_ack = irq_gc_ack_set_bit;
  138. ct[0].chip.irq_set_type = ls_intc_set_type;
  139. ct[0].handler = handle_level_irq;
  140. ct[1].type = IRQ_TYPE_EDGE_BOTH;
  141. ct[1].regs.mask = LS_REG_INTC_EN;
  142. ct[1].regs.ack = LS_REG_INTC_CLR;
  143. ct[1].chip.irq_unmask = irq_gc_mask_set_bit;
  144. ct[1].chip.irq_mask = irq_gc_mask_clr_bit;
  145. ct[1].chip.irq_ack = irq_gc_ack_set_bit;
  146. ct[1].chip.irq_set_type = ls_intc_set_type;
  147. ct[1].handler = handle_edge_irq;
  148. irq_set_chained_handler_and_data(parent_irq,
  149. ls1x_chained_handle_irq, priv);
  150. return 0;
  151. out_free_domain:
  152. irq_domain_remove(priv->domain);
  153. out_iounmap:
  154. iounmap(priv->intc_base);
  155. out_free_priv:
  156. kfree(priv);
  157. return err;
  158. }
  159. IRQCHIP_DECLARE(ls1x_intc, "loongson,ls1x-intc", ls1x_intc_of_init);