irq-tegra.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. /*
  2. * Driver code for Tegra's Legacy Interrupt Controller
  3. *
  4. * Author: Marc Zyngier <marc.zyngier@arm.com>
  5. *
  6. * Heavily based on the original arch/arm/mach-tegra/irq.c code:
  7. * Copyright (C) 2011 Google, Inc.
  8. *
  9. * Author:
  10. * Colin Cross <ccross@android.com>
  11. *
  12. * Copyright (C) 2010,2013, NVIDIA Corporation
  13. *
  14. * This software is licensed under the terms of the GNU General Public
  15. * License version 2, as published by the Free Software Foundation, and
  16. * may be copied, distributed, and modified under those terms.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU General Public License for more details.
  22. *
  23. */
  24. #include <linux/io.h>
  25. #include <linux/irq.h>
  26. #include <linux/irqchip.h>
  27. #include <linux/irqdomain.h>
  28. #include <linux/of_address.h>
  29. #include <linux/slab.h>
  30. #include <linux/syscore_ops.h>
  31. #include <dt-bindings/interrupt-controller/arm-gic.h>
  32. #define ICTLR_CPU_IEP_VFIQ 0x08
  33. #define ICTLR_CPU_IEP_FIR 0x14
  34. #define ICTLR_CPU_IEP_FIR_SET 0x18
  35. #define ICTLR_CPU_IEP_FIR_CLR 0x1c
  36. #define ICTLR_CPU_IER 0x20
  37. #define ICTLR_CPU_IER_SET 0x24
  38. #define ICTLR_CPU_IER_CLR 0x28
  39. #define ICTLR_CPU_IEP_CLASS 0x2C
  40. #define ICTLR_COP_IER 0x30
  41. #define ICTLR_COP_IER_SET 0x34
  42. #define ICTLR_COP_IER_CLR 0x38
  43. #define ICTLR_COP_IEP_CLASS 0x3c
  44. #define TEGRA_MAX_NUM_ICTLRS 6
  45. static unsigned int num_ictlrs;
  46. struct tegra_ictlr_soc {
  47. unsigned int num_ictlrs;
  48. };
  49. static const struct tegra_ictlr_soc tegra20_ictlr_soc = {
  50. .num_ictlrs = 4,
  51. };
  52. static const struct tegra_ictlr_soc tegra30_ictlr_soc = {
  53. .num_ictlrs = 5,
  54. };
  55. static const struct tegra_ictlr_soc tegra210_ictlr_soc = {
  56. .num_ictlrs = 6,
  57. };
  58. static const struct of_device_id ictlr_matches[] = {
  59. { .compatible = "nvidia,tegra210-ictlr", .data = &tegra210_ictlr_soc },
  60. { .compatible = "nvidia,tegra30-ictlr", .data = &tegra30_ictlr_soc },
  61. { .compatible = "nvidia,tegra20-ictlr", .data = &tegra20_ictlr_soc },
  62. { }
  63. };
  64. struct tegra_ictlr_info {
  65. void __iomem *base[TEGRA_MAX_NUM_ICTLRS];
  66. #ifdef CONFIG_PM_SLEEP
  67. u32 cop_ier[TEGRA_MAX_NUM_ICTLRS];
  68. u32 cop_iep[TEGRA_MAX_NUM_ICTLRS];
  69. u32 cpu_ier[TEGRA_MAX_NUM_ICTLRS];
  70. u32 cpu_iep[TEGRA_MAX_NUM_ICTLRS];
  71. u32 ictlr_wake_mask[TEGRA_MAX_NUM_ICTLRS];
  72. #endif
  73. };
  74. static struct tegra_ictlr_info *lic;
  75. static inline void tegra_ictlr_write_mask(struct irq_data *d, unsigned long reg)
  76. {
  77. void __iomem *base = d->chip_data;
  78. u32 mask;
  79. mask = BIT(d->hwirq % 32);
  80. writel_relaxed(mask, base + reg);
  81. }
  82. static void tegra_mask(struct irq_data *d)
  83. {
  84. tegra_ictlr_write_mask(d, ICTLR_CPU_IER_CLR);
  85. irq_chip_mask_parent(d);
  86. }
  87. static void tegra_unmask(struct irq_data *d)
  88. {
  89. tegra_ictlr_write_mask(d, ICTLR_CPU_IER_SET);
  90. irq_chip_unmask_parent(d);
  91. }
  92. static void tegra_eoi(struct irq_data *d)
  93. {
  94. tegra_ictlr_write_mask(d, ICTLR_CPU_IEP_FIR_CLR);
  95. irq_chip_eoi_parent(d);
  96. }
  97. static int tegra_retrigger(struct irq_data *d)
  98. {
  99. tegra_ictlr_write_mask(d, ICTLR_CPU_IEP_FIR_SET);
  100. return irq_chip_retrigger_hierarchy(d);
  101. }
  102. #ifdef CONFIG_PM_SLEEP
  103. static int tegra_set_wake(struct irq_data *d, unsigned int enable)
  104. {
  105. u32 irq = d->hwirq;
  106. u32 index, mask;
  107. index = (irq / 32);
  108. mask = BIT(irq % 32);
  109. if (enable)
  110. lic->ictlr_wake_mask[index] |= mask;
  111. else
  112. lic->ictlr_wake_mask[index] &= ~mask;
  113. /*
  114. * Do *not* call into the parent, as the GIC doesn't have any
  115. * wake-up facility...
  116. */
  117. return 0;
  118. }
  119. static int tegra_ictlr_suspend(void)
  120. {
  121. unsigned long flags;
  122. unsigned int i;
  123. local_irq_save(flags);
  124. for (i = 0; i < num_ictlrs; i++) {
  125. void __iomem *ictlr = lic->base[i];
  126. /* Save interrupt state */
  127. lic->cpu_ier[i] = readl_relaxed(ictlr + ICTLR_CPU_IER);
  128. lic->cpu_iep[i] = readl_relaxed(ictlr + ICTLR_CPU_IEP_CLASS);
  129. lic->cop_ier[i] = readl_relaxed(ictlr + ICTLR_COP_IER);
  130. lic->cop_iep[i] = readl_relaxed(ictlr + ICTLR_COP_IEP_CLASS);
  131. /* Disable COP interrupts */
  132. writel_relaxed(~0ul, ictlr + ICTLR_COP_IER_CLR);
  133. /* Disable CPU interrupts */
  134. writel_relaxed(~0ul, ictlr + ICTLR_CPU_IER_CLR);
  135. /* Enable the wakeup sources of ictlr */
  136. writel_relaxed(lic->ictlr_wake_mask[i], ictlr + ICTLR_CPU_IER_SET);
  137. }
  138. local_irq_restore(flags);
  139. return 0;
  140. }
  141. static void tegra_ictlr_resume(void)
  142. {
  143. unsigned long flags;
  144. unsigned int i;
  145. local_irq_save(flags);
  146. for (i = 0; i < num_ictlrs; i++) {
  147. void __iomem *ictlr = lic->base[i];
  148. writel_relaxed(lic->cpu_iep[i],
  149. ictlr + ICTLR_CPU_IEP_CLASS);
  150. writel_relaxed(~0ul, ictlr + ICTLR_CPU_IER_CLR);
  151. writel_relaxed(lic->cpu_ier[i],
  152. ictlr + ICTLR_CPU_IER_SET);
  153. writel_relaxed(lic->cop_iep[i],
  154. ictlr + ICTLR_COP_IEP_CLASS);
  155. writel_relaxed(~0ul, ictlr + ICTLR_COP_IER_CLR);
  156. writel_relaxed(lic->cop_ier[i],
  157. ictlr + ICTLR_COP_IER_SET);
  158. }
  159. local_irq_restore(flags);
  160. }
  161. static struct syscore_ops tegra_ictlr_syscore_ops = {
  162. .suspend = tegra_ictlr_suspend,
  163. .resume = tegra_ictlr_resume,
  164. };
  165. static void tegra_ictlr_syscore_init(void)
  166. {
  167. register_syscore_ops(&tegra_ictlr_syscore_ops);
  168. }
  169. #else
  170. #define tegra_set_wake NULL
  171. static inline void tegra_ictlr_syscore_init(void) {}
  172. #endif
  173. static struct irq_chip tegra_ictlr_chip = {
  174. .name = "LIC",
  175. .irq_eoi = tegra_eoi,
  176. .irq_mask = tegra_mask,
  177. .irq_unmask = tegra_unmask,
  178. .irq_retrigger = tegra_retrigger,
  179. .irq_set_wake = tegra_set_wake,
  180. .irq_set_type = irq_chip_set_type_parent,
  181. .flags = IRQCHIP_MASK_ON_SUSPEND,
  182. #ifdef CONFIG_SMP
  183. .irq_set_affinity = irq_chip_set_affinity_parent,
  184. #endif
  185. };
  186. static int tegra_ictlr_domain_translate(struct irq_domain *d,
  187. struct irq_fwspec *fwspec,
  188. unsigned long *hwirq,
  189. unsigned int *type)
  190. {
  191. if (is_of_node(fwspec->fwnode)) {
  192. if (fwspec->param_count != 3)
  193. return -EINVAL;
  194. /* No PPI should point to this domain */
  195. if (fwspec->param[0] != 0)
  196. return -EINVAL;
  197. *hwirq = fwspec->param[1];
  198. *type = fwspec->param[2];
  199. return 0;
  200. }
  201. return -EINVAL;
  202. }
  203. static int tegra_ictlr_domain_alloc(struct irq_domain *domain,
  204. unsigned int virq,
  205. unsigned int nr_irqs, void *data)
  206. {
  207. struct irq_fwspec *fwspec = data;
  208. struct irq_fwspec parent_fwspec;
  209. struct tegra_ictlr_info *info = domain->host_data;
  210. irq_hw_number_t hwirq;
  211. unsigned int i;
  212. if (fwspec->param_count != 3)
  213. return -EINVAL; /* Not GIC compliant */
  214. if (fwspec->param[0] != GIC_SPI)
  215. return -EINVAL; /* No PPI should point to this domain */
  216. hwirq = fwspec->param[1];
  217. if (hwirq >= (num_ictlrs * 32))
  218. return -EINVAL;
  219. for (i = 0; i < nr_irqs; i++) {
  220. int ictlr = (hwirq + i) / 32;
  221. irq_domain_set_hwirq_and_chip(domain, virq + i, hwirq + i,
  222. &tegra_ictlr_chip,
  223. info->base[ictlr]);
  224. }
  225. parent_fwspec = *fwspec;
  226. parent_fwspec.fwnode = domain->parent->fwnode;
  227. return irq_domain_alloc_irqs_parent(domain, virq, nr_irqs,
  228. &parent_fwspec);
  229. }
  230. static void tegra_ictlr_domain_free(struct irq_domain *domain,
  231. unsigned int virq,
  232. unsigned int nr_irqs)
  233. {
  234. unsigned int i;
  235. for (i = 0; i < nr_irqs; i++) {
  236. struct irq_data *d = irq_domain_get_irq_data(domain, virq + i);
  237. irq_domain_reset_irq_data(d);
  238. }
  239. }
  240. static const struct irq_domain_ops tegra_ictlr_domain_ops = {
  241. .translate = tegra_ictlr_domain_translate,
  242. .alloc = tegra_ictlr_domain_alloc,
  243. .free = tegra_ictlr_domain_free,
  244. };
  245. static int __init tegra_ictlr_init(struct device_node *node,
  246. struct device_node *parent)
  247. {
  248. struct irq_domain *parent_domain, *domain;
  249. const struct of_device_id *match;
  250. const struct tegra_ictlr_soc *soc;
  251. unsigned int i;
  252. int err;
  253. if (!parent) {
  254. pr_err("%s: no parent, giving up\n", node->full_name);
  255. return -ENODEV;
  256. }
  257. parent_domain = irq_find_host(parent);
  258. if (!parent_domain) {
  259. pr_err("%s: unable to obtain parent domain\n", node->full_name);
  260. return -ENXIO;
  261. }
  262. match = of_match_node(ictlr_matches, node);
  263. if (!match) /* Should never happen... */
  264. return -ENODEV;
  265. soc = match->data;
  266. lic = kzalloc(sizeof(*lic), GFP_KERNEL);
  267. if (!lic)
  268. return -ENOMEM;
  269. for (i = 0; i < TEGRA_MAX_NUM_ICTLRS; i++) {
  270. void __iomem *base;
  271. base = of_iomap(node, i);
  272. if (!base)
  273. break;
  274. lic->base[i] = base;
  275. /* Disable all interrupts */
  276. writel_relaxed(~0UL, base + ICTLR_CPU_IER_CLR);
  277. /* All interrupts target IRQ */
  278. writel_relaxed(0, base + ICTLR_CPU_IEP_CLASS);
  279. num_ictlrs++;
  280. }
  281. if (!num_ictlrs) {
  282. pr_err("%s: no valid regions, giving up\n", node->full_name);
  283. err = -ENOMEM;
  284. goto out_free;
  285. }
  286. WARN(num_ictlrs != soc->num_ictlrs,
  287. "%s: Found %u interrupt controllers in DT; expected %u.\n",
  288. node->full_name, num_ictlrs, soc->num_ictlrs);
  289. domain = irq_domain_add_hierarchy(parent_domain, 0, num_ictlrs * 32,
  290. node, &tegra_ictlr_domain_ops,
  291. lic);
  292. if (!domain) {
  293. pr_err("%s: failed to allocated domain\n", node->full_name);
  294. err = -ENOMEM;
  295. goto out_unmap;
  296. }
  297. tegra_ictlr_syscore_init();
  298. pr_info("%s: %d interrupts forwarded to %s\n",
  299. node->full_name, num_ictlrs * 32, parent->full_name);
  300. return 0;
  301. out_unmap:
  302. for (i = 0; i < num_ictlrs; i++)
  303. iounmap(lic->base[i]);
  304. out_free:
  305. kfree(lic);
  306. return err;
  307. }
  308. IRQCHIP_DECLARE(tegra20_ictlr, "nvidia,tegra20-ictlr", tegra_ictlr_init);
  309. IRQCHIP_DECLARE(tegra30_ictlr, "nvidia,tegra30-ictlr", tegra_ictlr_init);
  310. IRQCHIP_DECLARE(tegra210_ictlr, "nvidia,tegra210-ictlr", tegra_ictlr_init);