205-nmi-add-driver.patch 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. From b9ad0253e6c68ac3d37fd2ed8ed9bf8a334e4b65 Mon Sep 17 00:00:00 2001
  2. From: Carlo Caione <carlo@caione.org>
  3. Date: Sat, 15 Mar 2014 14:40:59 +0100
  4. Subject: [PATCH] ARM: sun7i/sun6i: irqchip: Add irqchip driver for NMI
  5. controller
  6. Allwinner A20/A31 SoCs have special registers to control / (un)mask /
  7. acknowledge NMI. This NMI controller is separated and independent from GIC.
  8. This patch adds a new irqchip to manage NMI.
  9. Signed-off-by: Carlo Caione <carlo@caione.org>
  10. Acked-by: Maxime Ripard <maxime.ripard@free-electrons.com>
  11. ---
  12. drivers/irqchip/Makefile | 1 +
  13. drivers/irqchip/irq-sunxi-nmi.c | 208 ++++++++++++++++++++++++++++++++++++++++
  14. 2 files changed, 209 insertions(+)
  15. create mode 100644 drivers/irqchip/irq-sunxi-nmi.c
  16. --- a/drivers/irqchip/Makefile
  17. +++ b/drivers/irqchip/Makefile
  18. @@ -12,6 +12,7 @@ obj-$(CONFIG_METAG_PERFCOUNTER_IRQS) +=
  19. obj-$(CONFIG_ARCH_MOXART) += irq-moxart.o
  20. obj-$(CONFIG_ORION_IRQCHIP) += irq-orion.o
  21. obj-$(CONFIG_ARCH_SUNXI) += irq-sun4i.o
  22. +obj-$(CONFIG_ARCH_SUNXI) += irq-sunxi-nmi.o
  23. obj-$(CONFIG_ARCH_SPEAR3XX) += spear-shirq.o
  24. obj-$(CONFIG_ARM_GIC) += irq-gic.o
  25. obj-$(CONFIG_ARM_NVIC) += irq-nvic.o
  26. --- /dev/null
  27. +++ b/drivers/irqchip/irq-sunxi-nmi.c
  28. @@ -0,0 +1,208 @@
  29. +/*
  30. + * Allwinner A20/A31 SoCs NMI IRQ chip driver.
  31. + *
  32. + * Carlo Caione <carlo.caione@gmail.com>
  33. + *
  34. + * This file is licensed under the terms of the GNU General Public
  35. + * License version 2. This program is licensed "as is" without any
  36. + * warranty of any kind, whether express or implied.
  37. + */
  38. +
  39. +#include <linux/bitops.h>
  40. +#include <linux/device.h>
  41. +#include <linux/io.h>
  42. +#include <linux/irq.h>
  43. +#include <linux/interrupt.h>
  44. +#include <linux/irqdomain.h>
  45. +#include <linux/of_irq.h>
  46. +#include <linux/of_address.h>
  47. +#include <linux/of_platform.h>
  48. +#include <linux/irqchip/chained_irq.h>
  49. +#include "irqchip.h"
  50. +
  51. +#define SUNXI_NMI_SRC_TYPE_MASK 0x00000003
  52. +
  53. +enum {
  54. + SUNXI_SRC_TYPE_LEVEL_LOW = 0,
  55. + SUNXI_SRC_TYPE_EDGE_FALLING,
  56. + SUNXI_SRC_TYPE_LEVEL_HIGH,
  57. + SUNXI_SRC_TYPE_EDGE_RISING,
  58. +};
  59. +
  60. +struct sunxi_sc_nmi_reg_offs {
  61. + u32 ctrl;
  62. + u32 pend;
  63. + u32 enable;
  64. +};
  65. +
  66. +static struct sunxi_sc_nmi_reg_offs sun7i_reg_offs = {
  67. + .ctrl = 0x00,
  68. + .pend = 0x04,
  69. + .enable = 0x08,
  70. +};
  71. +
  72. +static struct sunxi_sc_nmi_reg_offs sun6i_reg_offs = {
  73. + .ctrl = 0x00,
  74. + .pend = 0x04,
  75. + .enable = 0x34,
  76. +};
  77. +
  78. +static inline void sunxi_sc_nmi_write(struct irq_chip_generic *gc, u32 off,
  79. + u32 val)
  80. +{
  81. + irq_reg_writel(val, gc->reg_base + off);
  82. +}
  83. +
  84. +static inline u32 sunxi_sc_nmi_read(struct irq_chip_generic *gc, u32 off)
  85. +{
  86. + return irq_reg_readl(gc->reg_base + off);
  87. +}
  88. +
  89. +static void sunxi_sc_nmi_handle_irq(unsigned int irq, struct irq_desc *desc)
  90. +{
  91. + struct irq_domain *domain = irq_desc_get_handler_data(desc);
  92. + struct irq_chip *chip = irq_get_chip(irq);
  93. + unsigned int virq = irq_find_mapping(domain, 0);
  94. +
  95. + chained_irq_enter(chip, desc);
  96. + generic_handle_irq(virq);
  97. + chained_irq_exit(chip, desc);
  98. +}
  99. +
  100. +static int sunxi_sc_nmi_set_type(struct irq_data *data, unsigned int flow_type)
  101. +{
  102. + struct irq_chip_generic *gc = irq_data_get_irq_chip_data(data);
  103. + struct irq_chip_type *ct = gc->chip_types;
  104. + u32 src_type_reg;
  105. + u32 ctrl_off = ct->regs.type;
  106. + unsigned int src_type;
  107. + unsigned int i;
  108. +
  109. + irq_gc_lock(gc);
  110. +
  111. + switch (flow_type & IRQF_TRIGGER_MASK) {
  112. + case IRQ_TYPE_EDGE_FALLING:
  113. + src_type = SUNXI_SRC_TYPE_EDGE_FALLING;
  114. + break;
  115. + case IRQ_TYPE_EDGE_RISING:
  116. + src_type = SUNXI_SRC_TYPE_EDGE_RISING;
  117. + break;
  118. + case IRQ_TYPE_LEVEL_HIGH:
  119. + src_type = SUNXI_SRC_TYPE_LEVEL_HIGH;
  120. + break;
  121. + case IRQ_TYPE_NONE:
  122. + case IRQ_TYPE_LEVEL_LOW:
  123. + src_type = SUNXI_SRC_TYPE_LEVEL_LOW;
  124. + break;
  125. + default:
  126. + irq_gc_unlock(gc);
  127. + pr_err("%s: Cannot assign multiple trigger modes to IRQ %d.\n",
  128. + __func__, data->irq);
  129. + return -EBADR;
  130. + }
  131. +
  132. + irqd_set_trigger_type(data, flow_type);
  133. + irq_setup_alt_chip(data, flow_type);
  134. +
  135. + for (i = 0; i <= gc->num_ct; i++, ct++)
  136. + if (ct->type & flow_type)
  137. + ctrl_off = ct->regs.type;
  138. +
  139. + src_type_reg = sunxi_sc_nmi_read(gc, ctrl_off);
  140. + src_type_reg &= ~SUNXI_NMI_SRC_TYPE_MASK;
  141. + src_type_reg |= src_type;
  142. + sunxi_sc_nmi_write(gc, ctrl_off, src_type_reg);
  143. +
  144. + irq_gc_unlock(gc);
  145. +
  146. + return IRQ_SET_MASK_OK;
  147. +}
  148. +
  149. +static int __init sunxi_sc_nmi_irq_init(struct device_node *node,
  150. + struct sunxi_sc_nmi_reg_offs *reg_offs)
  151. +{
  152. + struct irq_domain *domain;
  153. + struct irq_chip_generic *gc;
  154. + unsigned int irq;
  155. + unsigned int clr = IRQ_NOREQUEST | IRQ_NOPROBE | IRQ_NOAUTOEN;
  156. + int ret;
  157. +
  158. +
  159. + domain = irq_domain_add_linear(node, 1, &irq_generic_chip_ops, NULL);
  160. + if (!domain) {
  161. + pr_err("%s: Could not register interrupt domain.\n", node->name);
  162. + return -ENOMEM;
  163. + }
  164. +
  165. + ret = irq_alloc_domain_generic_chips(domain, 1, 2, node->name,
  166. + handle_fasteoi_irq, clr, 0,
  167. + IRQ_GC_INIT_MASK_CACHE);
  168. + if (ret) {
  169. + pr_err("%s: Could not allocate generic interrupt chip.\n",
  170. + node->name);
  171. + goto fail_irqd_remove;
  172. + }
  173. +
  174. + irq = irq_of_parse_and_map(node, 0);
  175. + if (irq <= 0) {
  176. + pr_err("%s: unable to parse irq\n", node->name);
  177. + ret = -EINVAL;
  178. + goto fail_irqd_remove;
  179. + }
  180. +
  181. + gc = irq_get_domain_generic_chip(domain, 0);
  182. + gc->reg_base = of_iomap(node, 0);
  183. + if (!gc->reg_base) {
  184. + pr_err("%s: unable to map resource\n", node->name);
  185. + ret = -ENOMEM;
  186. + goto fail_irqd_remove;
  187. + }
  188. +
  189. + gc->chip_types[0].type = IRQ_TYPE_LEVEL_MASK;
  190. + gc->chip_types[0].chip.irq_mask = irq_gc_mask_clr_bit;
  191. + gc->chip_types[0].chip.irq_unmask = irq_gc_mask_set_bit;
  192. + gc->chip_types[0].chip.irq_eoi = irq_gc_ack_set_bit;
  193. + gc->chip_types[0].chip.irq_set_type = sunxi_sc_nmi_set_type;
  194. + gc->chip_types[0].chip.flags = IRQCHIP_EOI_THREADED | IRQCHIP_EOI_IF_HANDLED;
  195. + gc->chip_types[0].regs.ack = reg_offs->pend;
  196. + gc->chip_types[0].regs.mask = reg_offs->enable;
  197. + gc->chip_types[0].regs.type = reg_offs->ctrl;
  198. +
  199. + gc->chip_types[1].type = IRQ_TYPE_EDGE_BOTH;
  200. + gc->chip_types[1].chip.name = gc->chip_types[0].chip.name;
  201. + gc->chip_types[1].chip.irq_ack = irq_gc_ack_set_bit;
  202. + gc->chip_types[1].chip.irq_mask = irq_gc_mask_clr_bit;
  203. + gc->chip_types[1].chip.irq_unmask = irq_gc_mask_set_bit;
  204. + gc->chip_types[1].chip.irq_set_type = sunxi_sc_nmi_set_type;
  205. + gc->chip_types[1].regs.ack = reg_offs->pend;
  206. + gc->chip_types[1].regs.mask = reg_offs->enable;
  207. + gc->chip_types[1].regs.type = reg_offs->ctrl;
  208. + gc->chip_types[1].handler = handle_edge_irq;
  209. +
  210. + sunxi_sc_nmi_write(gc, reg_offs->enable, 0);
  211. + sunxi_sc_nmi_write(gc, reg_offs->pend, 0x1);
  212. +
  213. + irq_set_handler_data(irq, domain);
  214. + irq_set_chained_handler(irq, sunxi_sc_nmi_handle_irq);
  215. +
  216. + return 0;
  217. +
  218. +fail_irqd_remove:
  219. + irq_domain_remove(domain);
  220. +
  221. + return ret;
  222. +}
  223. +
  224. +static int __init sun6i_sc_nmi_irq_init(struct device_node *node,
  225. + struct device_node *parent)
  226. +{
  227. + return sunxi_sc_nmi_irq_init(node, &sun6i_reg_offs);
  228. +}
  229. +IRQCHIP_DECLARE(sun6i_sc_nmi, "allwinner,sun6i-a31-sc-nmi", sun6i_sc_nmi_irq_init);
  230. +
  231. +static int __init sun7i_sc_nmi_irq_init(struct device_node *node,
  232. + struct device_node *parent)
  233. +{
  234. + return sunxi_sc_nmi_irq_init(node, &sun7i_reg_offs);
  235. +}
  236. +IRQCHIP_DECLARE(sun7i_sc_nmi, "allwinner,sun7i-a20-sc-nmi", sun7i_sc_nmi_irq_init);