irq-vf610-mscm-ir.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. /*
  2. * Copyright (C) 2014-2015 Toradex AG
  3. * Author: Stefan Agner <stefan@agner.ch>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. *
  9. *
  10. * IRQ chip driver for MSCM interrupt router available on Vybrid SoC's.
  11. * The interrupt router is between the CPU's interrupt controller and the
  12. * peripheral. The router allows to route the peripheral interrupts to
  13. * one of the two available CPU's on Vybrid VF6xx SoC's (Cortex-A5 or
  14. * Cortex-M4). The router will be configured transparently on a IRQ
  15. * request.
  16. *
  17. * o All peripheral interrupts of the Vybrid SoC can be routed to
  18. * CPU 0, CPU 1 or both. The routing is useful for dual-core
  19. * variants of Vybrid SoC such as VF6xx. This driver routes the
  20. * requested interrupt to the CPU currently running on.
  21. *
  22. * o It is required to setup the interrupt router even on single-core
  23. * variants of Vybrid.
  24. */
  25. #include <linux/cpu_pm.h>
  26. #include <linux/io.h>
  27. #include <linux/irq.h>
  28. #include <linux/irqdomain.h>
  29. #include <linux/mfd/syscon.h>
  30. #include <dt-bindings/interrupt-controller/arm-gic.h>
  31. #include <linux/of.h>
  32. #include <linux/of_address.h>
  33. #include <linux/slab.h>
  34. #include <linux/regmap.h>
  35. #include "irqchip.h"
  36. #define MSCM_CPxNUM 0x4
  37. #define MSCM_IRSPRC(n) (0x80 + 2 * (n))
  38. #define MSCM_IRSPRC_CPEN_MASK 0x3
  39. #define MSCM_IRSPRC_NUM 112
  40. struct vf610_mscm_ir_chip_data {
  41. void __iomem *mscm_ir_base;
  42. u16 cpu_mask;
  43. u16 saved_irsprc[MSCM_IRSPRC_NUM];
  44. bool is_nvic;
  45. };
  46. static struct vf610_mscm_ir_chip_data *mscm_ir_data;
  47. static inline void vf610_mscm_ir_save(struct vf610_mscm_ir_chip_data *data)
  48. {
  49. int i;
  50. for (i = 0; i < MSCM_IRSPRC_NUM; i++)
  51. data->saved_irsprc[i] = readw_relaxed(data->mscm_ir_base + MSCM_IRSPRC(i));
  52. }
  53. static inline void vf610_mscm_ir_restore(struct vf610_mscm_ir_chip_data *data)
  54. {
  55. int i;
  56. for (i = 0; i < MSCM_IRSPRC_NUM; i++)
  57. writew_relaxed(data->saved_irsprc[i], data->mscm_ir_base + MSCM_IRSPRC(i));
  58. }
  59. static int vf610_mscm_ir_notifier(struct notifier_block *self,
  60. unsigned long cmd, void *v)
  61. {
  62. switch (cmd) {
  63. case CPU_CLUSTER_PM_ENTER:
  64. vf610_mscm_ir_save(mscm_ir_data);
  65. break;
  66. case CPU_CLUSTER_PM_ENTER_FAILED:
  67. case CPU_CLUSTER_PM_EXIT:
  68. vf610_mscm_ir_restore(mscm_ir_data);
  69. break;
  70. }
  71. return NOTIFY_OK;
  72. }
  73. static struct notifier_block mscm_ir_notifier_block = {
  74. .notifier_call = vf610_mscm_ir_notifier,
  75. };
  76. static void vf610_mscm_ir_enable(struct irq_data *data)
  77. {
  78. irq_hw_number_t hwirq = data->hwirq;
  79. struct vf610_mscm_ir_chip_data *chip_data = data->chip_data;
  80. u16 irsprc;
  81. irsprc = readw_relaxed(chip_data->mscm_ir_base + MSCM_IRSPRC(hwirq));
  82. irsprc &= MSCM_IRSPRC_CPEN_MASK;
  83. WARN_ON(irsprc & ~chip_data->cpu_mask);
  84. writew_relaxed(chip_data->cpu_mask,
  85. chip_data->mscm_ir_base + MSCM_IRSPRC(hwirq));
  86. irq_chip_enable_parent(data);
  87. }
  88. static void vf610_mscm_ir_disable(struct irq_data *data)
  89. {
  90. irq_hw_number_t hwirq = data->hwirq;
  91. struct vf610_mscm_ir_chip_data *chip_data = data->chip_data;
  92. writew_relaxed(0x0, chip_data->mscm_ir_base + MSCM_IRSPRC(hwirq));
  93. irq_chip_disable_parent(data);
  94. }
  95. static struct irq_chip vf610_mscm_ir_irq_chip = {
  96. .name = "mscm-ir",
  97. .irq_mask = irq_chip_mask_parent,
  98. .irq_unmask = irq_chip_unmask_parent,
  99. .irq_eoi = irq_chip_eoi_parent,
  100. .irq_enable = vf610_mscm_ir_enable,
  101. .irq_disable = vf610_mscm_ir_disable,
  102. .irq_retrigger = irq_chip_retrigger_hierarchy,
  103. .irq_set_affinity = irq_chip_set_affinity_parent,
  104. };
  105. static int vf610_mscm_ir_domain_alloc(struct irq_domain *domain, unsigned int virq,
  106. unsigned int nr_irqs, void *arg)
  107. {
  108. int i;
  109. irq_hw_number_t hwirq;
  110. struct of_phandle_args *irq_data = arg;
  111. struct of_phandle_args gic_data;
  112. if (irq_data->args_count != 2)
  113. return -EINVAL;
  114. hwirq = irq_data->args[0];
  115. for (i = 0; i < nr_irqs; i++)
  116. irq_domain_set_hwirq_and_chip(domain, virq + i, hwirq + i,
  117. &vf610_mscm_ir_irq_chip,
  118. domain->host_data);
  119. gic_data.np = domain->parent->of_node;
  120. if (mscm_ir_data->is_nvic) {
  121. gic_data.args_count = 1;
  122. gic_data.args[0] = irq_data->args[0];
  123. } else {
  124. gic_data.args_count = 3;
  125. gic_data.args[0] = GIC_SPI;
  126. gic_data.args[1] = irq_data->args[0];
  127. gic_data.args[2] = irq_data->args[1];
  128. }
  129. return irq_domain_alloc_irqs_parent(domain, virq, nr_irqs, &gic_data);
  130. }
  131. static const struct irq_domain_ops mscm_irq_domain_ops = {
  132. .xlate = irq_domain_xlate_twocell,
  133. .alloc = vf610_mscm_ir_domain_alloc,
  134. .free = irq_domain_free_irqs_common,
  135. };
  136. static int __init vf610_mscm_ir_of_init(struct device_node *node,
  137. struct device_node *parent)
  138. {
  139. struct irq_domain *domain, *domain_parent;
  140. struct regmap *mscm_cp_regmap;
  141. int ret, cpuid;
  142. domain_parent = irq_find_host(parent);
  143. if (!domain_parent) {
  144. pr_err("vf610_mscm_ir: interrupt-parent not found\n");
  145. return -EINVAL;
  146. }
  147. mscm_ir_data = kzalloc(sizeof(*mscm_ir_data), GFP_KERNEL);
  148. if (!mscm_ir_data)
  149. return -ENOMEM;
  150. mscm_ir_data->mscm_ir_base = of_io_request_and_map(node, 0, "mscm-ir");
  151. if (IS_ERR(mscm_ir_data->mscm_ir_base)) {
  152. pr_err("vf610_mscm_ir: unable to map mscm register\n");
  153. ret = PTR_ERR(mscm_ir_data->mscm_ir_base);
  154. goto out_free;
  155. }
  156. mscm_cp_regmap = syscon_regmap_lookup_by_phandle(node, "fsl,cpucfg");
  157. if (IS_ERR(mscm_cp_regmap)) {
  158. ret = PTR_ERR(mscm_cp_regmap);
  159. pr_err("vf610_mscm_ir: regmap lookup for cpucfg failed\n");
  160. goto out_unmap;
  161. }
  162. regmap_read(mscm_cp_regmap, MSCM_CPxNUM, &cpuid);
  163. mscm_ir_data->cpu_mask = 0x1 << cpuid;
  164. domain = irq_domain_add_hierarchy(domain_parent, 0,
  165. MSCM_IRSPRC_NUM, node,
  166. &mscm_irq_domain_ops, mscm_ir_data);
  167. if (!domain) {
  168. ret = -ENOMEM;
  169. goto out_unmap;
  170. }
  171. if (of_device_is_compatible(domain->parent->of_node, "arm,armv7m-nvic"))
  172. mscm_ir_data->is_nvic = true;
  173. cpu_pm_register_notifier(&mscm_ir_notifier_block);
  174. return 0;
  175. out_unmap:
  176. iounmap(mscm_ir_data->mscm_ir_base);
  177. out_free:
  178. kfree(mscm_ir_data);
  179. return ret;
  180. }
  181. IRQCHIP_DECLARE(vf610_mscm_ir, "fsl,vf610-mscm-ir", vf610_mscm_ir_of_init);