irq-sunxi-nmi.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /*
  2. * Allwinner A20/A31 SoCs NMI IRQ chip driver.
  3. *
  4. * Carlo Caione <carlo.caione@gmail.com>
  5. *
  6. * This file is licensed under the terms of the GNU General Public
  7. * License version 2. This program is licensed "as is" without any
  8. * warranty of any kind, whether express or implied.
  9. */
  10. #define DRV_NAME "sunxi-nmi"
  11. #define pr_fmt(fmt) DRV_NAME ": " fmt
  12. #include <linux/bitops.h>
  13. #include <linux/device.h>
  14. #include <linux/io.h>
  15. #include <linux/irq.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/irqdomain.h>
  18. #include <linux/of_irq.h>
  19. #include <linux/of_address.h>
  20. #include <linux/of_platform.h>
  21. #include <linux/irqchip.h>
  22. #include <linux/irqchip/chained_irq.h>
  23. #define SUNXI_NMI_SRC_TYPE_MASK 0x00000003
  24. enum {
  25. SUNXI_SRC_TYPE_LEVEL_LOW = 0,
  26. SUNXI_SRC_TYPE_EDGE_FALLING,
  27. SUNXI_SRC_TYPE_LEVEL_HIGH,
  28. SUNXI_SRC_TYPE_EDGE_RISING,
  29. };
  30. struct sunxi_sc_nmi_reg_offs {
  31. u32 ctrl;
  32. u32 pend;
  33. u32 enable;
  34. };
  35. static struct sunxi_sc_nmi_reg_offs sun7i_reg_offs = {
  36. .ctrl = 0x00,
  37. .pend = 0x04,
  38. .enable = 0x08,
  39. };
  40. static struct sunxi_sc_nmi_reg_offs sun6i_reg_offs = {
  41. .ctrl = 0x00,
  42. .pend = 0x04,
  43. .enable = 0x34,
  44. };
  45. static inline void sunxi_sc_nmi_write(struct irq_chip_generic *gc, u32 off,
  46. u32 val)
  47. {
  48. irq_reg_writel(gc, val, off);
  49. }
  50. static inline u32 sunxi_sc_nmi_read(struct irq_chip_generic *gc, u32 off)
  51. {
  52. return irq_reg_readl(gc, off);
  53. }
  54. static void sunxi_sc_nmi_handle_irq(struct irq_desc *desc)
  55. {
  56. struct irq_domain *domain = irq_desc_get_handler_data(desc);
  57. struct irq_chip *chip = irq_desc_get_chip(desc);
  58. unsigned int virq = irq_find_mapping(domain, 0);
  59. chained_irq_enter(chip, desc);
  60. generic_handle_irq(virq);
  61. chained_irq_exit(chip, desc);
  62. }
  63. static int sunxi_sc_nmi_set_type(struct irq_data *data, unsigned int flow_type)
  64. {
  65. struct irq_chip_generic *gc = irq_data_get_irq_chip_data(data);
  66. struct irq_chip_type *ct = gc->chip_types;
  67. u32 src_type_reg;
  68. u32 ctrl_off = ct->regs.type;
  69. unsigned int src_type;
  70. unsigned int i;
  71. irq_gc_lock(gc);
  72. switch (flow_type & IRQF_TRIGGER_MASK) {
  73. case IRQ_TYPE_EDGE_FALLING:
  74. src_type = SUNXI_SRC_TYPE_EDGE_FALLING;
  75. break;
  76. case IRQ_TYPE_EDGE_RISING:
  77. src_type = SUNXI_SRC_TYPE_EDGE_RISING;
  78. break;
  79. case IRQ_TYPE_LEVEL_HIGH:
  80. src_type = SUNXI_SRC_TYPE_LEVEL_HIGH;
  81. break;
  82. case IRQ_TYPE_NONE:
  83. case IRQ_TYPE_LEVEL_LOW:
  84. src_type = SUNXI_SRC_TYPE_LEVEL_LOW;
  85. break;
  86. default:
  87. irq_gc_unlock(gc);
  88. pr_err("Cannot assign multiple trigger modes to IRQ %d.\n",
  89. data->irq);
  90. return -EBADR;
  91. }
  92. irqd_set_trigger_type(data, flow_type);
  93. irq_setup_alt_chip(data, flow_type);
  94. for (i = 0; i < gc->num_ct; i++, ct++)
  95. if (ct->type & flow_type)
  96. ctrl_off = ct->regs.type;
  97. src_type_reg = sunxi_sc_nmi_read(gc, ctrl_off);
  98. src_type_reg &= ~SUNXI_NMI_SRC_TYPE_MASK;
  99. src_type_reg |= src_type;
  100. sunxi_sc_nmi_write(gc, ctrl_off, src_type_reg);
  101. irq_gc_unlock(gc);
  102. return IRQ_SET_MASK_OK;
  103. }
  104. static int __init sunxi_sc_nmi_irq_init(struct device_node *node,
  105. struct sunxi_sc_nmi_reg_offs *reg_offs)
  106. {
  107. struct irq_domain *domain;
  108. struct irq_chip_generic *gc;
  109. unsigned int irq;
  110. unsigned int clr = IRQ_NOREQUEST | IRQ_NOPROBE | IRQ_NOAUTOEN;
  111. int ret;
  112. domain = irq_domain_add_linear(node, 1, &irq_generic_chip_ops, NULL);
  113. if (!domain) {
  114. pr_err("Could not register interrupt domain.\n");
  115. return -ENOMEM;
  116. }
  117. ret = irq_alloc_domain_generic_chips(domain, 1, 2, DRV_NAME,
  118. handle_fasteoi_irq, clr, 0,
  119. IRQ_GC_INIT_MASK_CACHE);
  120. if (ret) {
  121. pr_err("Could not allocate generic interrupt chip.\n");
  122. goto fail_irqd_remove;
  123. }
  124. irq = irq_of_parse_and_map(node, 0);
  125. if (irq <= 0) {
  126. pr_err("unable to parse irq\n");
  127. ret = -EINVAL;
  128. goto fail_irqd_remove;
  129. }
  130. gc = irq_get_domain_generic_chip(domain, 0);
  131. gc->reg_base = of_io_request_and_map(node, 0, of_node_full_name(node));
  132. if (IS_ERR(gc->reg_base)) {
  133. pr_err("unable to map resource\n");
  134. ret = PTR_ERR(gc->reg_base);
  135. goto fail_irqd_remove;
  136. }
  137. gc->chip_types[0].type = IRQ_TYPE_LEVEL_MASK;
  138. gc->chip_types[0].chip.irq_mask = irq_gc_mask_clr_bit;
  139. gc->chip_types[0].chip.irq_unmask = irq_gc_mask_set_bit;
  140. gc->chip_types[0].chip.irq_eoi = irq_gc_ack_set_bit;
  141. gc->chip_types[0].chip.irq_set_type = sunxi_sc_nmi_set_type;
  142. gc->chip_types[0].chip.flags = IRQCHIP_EOI_THREADED | IRQCHIP_EOI_IF_HANDLED;
  143. gc->chip_types[0].regs.ack = reg_offs->pend;
  144. gc->chip_types[0].regs.mask = reg_offs->enable;
  145. gc->chip_types[0].regs.type = reg_offs->ctrl;
  146. gc->chip_types[1].type = IRQ_TYPE_EDGE_BOTH;
  147. gc->chip_types[1].chip.name = gc->chip_types[0].chip.name;
  148. gc->chip_types[1].chip.irq_ack = irq_gc_ack_set_bit;
  149. gc->chip_types[1].chip.irq_mask = irq_gc_mask_clr_bit;
  150. gc->chip_types[1].chip.irq_unmask = irq_gc_mask_set_bit;
  151. gc->chip_types[1].chip.irq_set_type = sunxi_sc_nmi_set_type;
  152. gc->chip_types[1].regs.ack = reg_offs->pend;
  153. gc->chip_types[1].regs.mask = reg_offs->enable;
  154. gc->chip_types[1].regs.type = reg_offs->ctrl;
  155. gc->chip_types[1].handler = handle_edge_irq;
  156. sunxi_sc_nmi_write(gc, reg_offs->enable, 0);
  157. sunxi_sc_nmi_write(gc, reg_offs->pend, 0x1);
  158. irq_set_chained_handler_and_data(irq, sunxi_sc_nmi_handle_irq, domain);
  159. return 0;
  160. fail_irqd_remove:
  161. irq_domain_remove(domain);
  162. return ret;
  163. }
  164. static int __init sun6i_sc_nmi_irq_init(struct device_node *node,
  165. struct device_node *parent)
  166. {
  167. return sunxi_sc_nmi_irq_init(node, &sun6i_reg_offs);
  168. }
  169. IRQCHIP_DECLARE(sun6i_sc_nmi, "allwinner,sun6i-a31-sc-nmi", sun6i_sc_nmi_irq_init);
  170. static int __init sun7i_sc_nmi_irq_init(struct device_node *node,
  171. struct device_node *parent)
  172. {
  173. return sunxi_sc_nmi_irq_init(node, &sun7i_reg_offs);
  174. }
  175. IRQCHIP_DECLARE(sun7i_sc_nmi, "allwinner,sun7i-a20-sc-nmi", sun7i_sc_nmi_irq_init);