irq-sni-exiu.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Driver for Socionext External Interrupt Unit (EXIU)
  4. *
  5. * Copyright (c) 2017-2019 Linaro, Ltd. <[email protected]>
  6. *
  7. * Based on irq-tegra.c:
  8. * Copyright (C) 2011 Google, Inc.
  9. * Copyright (C) 2010,2013, NVIDIA Corporation
  10. */
  11. #include <linux/interrupt.h>
  12. #include <linux/io.h>
  13. #include <linux/irq.h>
  14. #include <linux/irqchip.h>
  15. #include <linux/irqdomain.h>
  16. #include <linux/of.h>
  17. #include <linux/of_address.h>
  18. #include <linux/of_irq.h>
  19. #include <linux/platform_device.h>
  20. #include <dt-bindings/interrupt-controller/arm-gic.h>
  21. #define NUM_IRQS 32
  22. #define EIMASK 0x00
  23. #define EISRCSEL 0x04
  24. #define EIREQSTA 0x08
  25. #define EIRAWREQSTA 0x0C
  26. #define EIREQCLR 0x10
  27. #define EILVL 0x14
  28. #define EIEDG 0x18
  29. #define EISIR 0x1C
  30. struct exiu_irq_data {
  31. void __iomem *base;
  32. u32 spi_base;
  33. };
  34. static void exiu_irq_ack(struct irq_data *d)
  35. {
  36. struct exiu_irq_data *data = irq_data_get_irq_chip_data(d);
  37. writel(BIT(d->hwirq), data->base + EIREQCLR);
  38. }
  39. static void exiu_irq_eoi(struct irq_data *d)
  40. {
  41. struct exiu_irq_data *data = irq_data_get_irq_chip_data(d);
  42. /*
  43. * Level triggered interrupts are latched and must be cleared during
  44. * EOI or the interrupt will be jammed on. Of course if a level
  45. * triggered interrupt is still asserted then the write will not clear
  46. * the interrupt.
  47. */
  48. if (irqd_is_level_type(d))
  49. writel(BIT(d->hwirq), data->base + EIREQCLR);
  50. irq_chip_eoi_parent(d);
  51. }
  52. static void exiu_irq_mask(struct irq_data *d)
  53. {
  54. struct exiu_irq_data *data = irq_data_get_irq_chip_data(d);
  55. u32 val;
  56. val = readl_relaxed(data->base + EIMASK) | BIT(d->hwirq);
  57. writel_relaxed(val, data->base + EIMASK);
  58. irq_chip_mask_parent(d);
  59. }
  60. static void exiu_irq_unmask(struct irq_data *d)
  61. {
  62. struct exiu_irq_data *data = irq_data_get_irq_chip_data(d);
  63. u32 val;
  64. val = readl_relaxed(data->base + EIMASK) & ~BIT(d->hwirq);
  65. writel_relaxed(val, data->base + EIMASK);
  66. irq_chip_unmask_parent(d);
  67. }
  68. static void exiu_irq_enable(struct irq_data *d)
  69. {
  70. struct exiu_irq_data *data = irq_data_get_irq_chip_data(d);
  71. u32 val;
  72. /* clear interrupts that were latched while disabled */
  73. writel_relaxed(BIT(d->hwirq), data->base + EIREQCLR);
  74. val = readl_relaxed(data->base + EIMASK) & ~BIT(d->hwirq);
  75. writel_relaxed(val, data->base + EIMASK);
  76. irq_chip_enable_parent(d);
  77. }
  78. static int exiu_irq_set_type(struct irq_data *d, unsigned int type)
  79. {
  80. struct exiu_irq_data *data = irq_data_get_irq_chip_data(d);
  81. u32 val;
  82. val = readl_relaxed(data->base + EILVL);
  83. if (type == IRQ_TYPE_EDGE_RISING || type == IRQ_TYPE_LEVEL_HIGH)
  84. val |= BIT(d->hwirq);
  85. else
  86. val &= ~BIT(d->hwirq);
  87. writel_relaxed(val, data->base + EILVL);
  88. val = readl_relaxed(data->base + EIEDG);
  89. if (type == IRQ_TYPE_LEVEL_LOW || type == IRQ_TYPE_LEVEL_HIGH) {
  90. val &= ~BIT(d->hwirq);
  91. irq_set_handler_locked(d, handle_fasteoi_irq);
  92. } else {
  93. val |= BIT(d->hwirq);
  94. irq_set_handler_locked(d, handle_fasteoi_ack_irq);
  95. }
  96. writel_relaxed(val, data->base + EIEDG);
  97. writel_relaxed(BIT(d->hwirq), data->base + EIREQCLR);
  98. return irq_chip_set_type_parent(d, IRQ_TYPE_LEVEL_HIGH);
  99. }
  100. static struct irq_chip exiu_irq_chip = {
  101. .name = "EXIU",
  102. .irq_ack = exiu_irq_ack,
  103. .irq_eoi = exiu_irq_eoi,
  104. .irq_enable = exiu_irq_enable,
  105. .irq_mask = exiu_irq_mask,
  106. .irq_unmask = exiu_irq_unmask,
  107. .irq_set_type = exiu_irq_set_type,
  108. .irq_set_affinity = irq_chip_set_affinity_parent,
  109. .flags = IRQCHIP_SET_TYPE_MASKED |
  110. IRQCHIP_SKIP_SET_WAKE |
  111. IRQCHIP_EOI_THREADED |
  112. IRQCHIP_MASK_ON_SUSPEND,
  113. };
  114. static int exiu_domain_translate(struct irq_domain *domain,
  115. struct irq_fwspec *fwspec,
  116. unsigned long *hwirq,
  117. unsigned int *type)
  118. {
  119. struct exiu_irq_data *info = domain->host_data;
  120. if (is_of_node(fwspec->fwnode)) {
  121. if (fwspec->param_count != 3)
  122. return -EINVAL;
  123. if (fwspec->param[0] != GIC_SPI)
  124. return -EINVAL; /* No PPI should point to this domain */
  125. *hwirq = fwspec->param[1] - info->spi_base;
  126. *type = fwspec->param[2] & IRQ_TYPE_SENSE_MASK;
  127. } else {
  128. if (fwspec->param_count != 2)
  129. return -EINVAL;
  130. *hwirq = fwspec->param[0];
  131. *type = fwspec->param[1] & IRQ_TYPE_SENSE_MASK;
  132. }
  133. return 0;
  134. }
  135. static int exiu_domain_alloc(struct irq_domain *dom, unsigned int virq,
  136. unsigned int nr_irqs, void *data)
  137. {
  138. struct irq_fwspec *fwspec = data;
  139. struct irq_fwspec parent_fwspec;
  140. struct exiu_irq_data *info = dom->host_data;
  141. irq_hw_number_t hwirq;
  142. parent_fwspec = *fwspec;
  143. if (is_of_node(dom->parent->fwnode)) {
  144. if (fwspec->param_count != 3)
  145. return -EINVAL; /* Not GIC compliant */
  146. if (fwspec->param[0] != GIC_SPI)
  147. return -EINVAL; /* No PPI should point to this domain */
  148. hwirq = fwspec->param[1] - info->spi_base;
  149. } else {
  150. hwirq = fwspec->param[0];
  151. parent_fwspec.param[0] = hwirq + info->spi_base + 32;
  152. }
  153. WARN_ON(nr_irqs != 1);
  154. irq_domain_set_hwirq_and_chip(dom, virq, hwirq, &exiu_irq_chip, info);
  155. parent_fwspec.fwnode = dom->parent->fwnode;
  156. return irq_domain_alloc_irqs_parent(dom, virq, nr_irqs, &parent_fwspec);
  157. }
  158. static const struct irq_domain_ops exiu_domain_ops = {
  159. .translate = exiu_domain_translate,
  160. .alloc = exiu_domain_alloc,
  161. .free = irq_domain_free_irqs_common,
  162. };
  163. static struct exiu_irq_data *exiu_init(const struct fwnode_handle *fwnode,
  164. struct resource *res)
  165. {
  166. struct exiu_irq_data *data;
  167. int err;
  168. data = kzalloc(sizeof(*data), GFP_KERNEL);
  169. if (!data)
  170. return ERR_PTR(-ENOMEM);
  171. if (fwnode_property_read_u32_array(fwnode, "socionext,spi-base",
  172. &data->spi_base, 1)) {
  173. err = -ENODEV;
  174. goto out_free;
  175. }
  176. data->base = ioremap(res->start, resource_size(res));
  177. if (!data->base) {
  178. err = -ENODEV;
  179. goto out_free;
  180. }
  181. /* clear and mask all interrupts */
  182. writel_relaxed(0xFFFFFFFF, data->base + EIREQCLR);
  183. writel_relaxed(0xFFFFFFFF, data->base + EIMASK);
  184. return data;
  185. out_free:
  186. kfree(data);
  187. return ERR_PTR(err);
  188. }
  189. static int __init exiu_dt_init(struct device_node *node,
  190. struct device_node *parent)
  191. {
  192. struct irq_domain *parent_domain, *domain;
  193. struct exiu_irq_data *data;
  194. struct resource res;
  195. if (!parent) {
  196. pr_err("%pOF: no parent, giving up\n", node);
  197. return -ENODEV;
  198. }
  199. parent_domain = irq_find_host(parent);
  200. if (!parent_domain) {
  201. pr_err("%pOF: unable to obtain parent domain\n", node);
  202. return -ENXIO;
  203. }
  204. if (of_address_to_resource(node, 0, &res)) {
  205. pr_err("%pOF: failed to parse memory resource\n", node);
  206. return -ENXIO;
  207. }
  208. data = exiu_init(of_node_to_fwnode(node), &res);
  209. if (IS_ERR(data))
  210. return PTR_ERR(data);
  211. domain = irq_domain_add_hierarchy(parent_domain, 0, NUM_IRQS, node,
  212. &exiu_domain_ops, data);
  213. if (!domain) {
  214. pr_err("%pOF: failed to allocate domain\n", node);
  215. goto out_unmap;
  216. }
  217. pr_info("%pOF: %d interrupts forwarded to %pOF\n", node, NUM_IRQS,
  218. parent);
  219. return 0;
  220. out_unmap:
  221. iounmap(data->base);
  222. kfree(data);
  223. return -ENOMEM;
  224. }
  225. IRQCHIP_DECLARE(exiu, "socionext,synquacer-exiu", exiu_dt_init);
  226. #ifdef CONFIG_ACPI
  227. static int exiu_acpi_probe(struct platform_device *pdev)
  228. {
  229. struct irq_domain *domain;
  230. struct exiu_irq_data *data;
  231. struct resource *res;
  232. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  233. if (!res) {
  234. dev_err(&pdev->dev, "failed to parse memory resource\n");
  235. return -ENXIO;
  236. }
  237. data = exiu_init(dev_fwnode(&pdev->dev), res);
  238. if (IS_ERR(data))
  239. return PTR_ERR(data);
  240. domain = acpi_irq_create_hierarchy(0, NUM_IRQS, dev_fwnode(&pdev->dev),
  241. &exiu_domain_ops, data);
  242. if (!domain) {
  243. dev_err(&pdev->dev, "failed to create IRQ domain\n");
  244. goto out_unmap;
  245. }
  246. dev_info(&pdev->dev, "%d interrupts forwarded\n", NUM_IRQS);
  247. return 0;
  248. out_unmap:
  249. iounmap(data->base);
  250. kfree(data);
  251. return -ENOMEM;
  252. }
  253. static const struct acpi_device_id exiu_acpi_ids[] = {
  254. { "SCX0008" },
  255. { /* sentinel */ }
  256. };
  257. MODULE_DEVICE_TABLE(acpi, exiu_acpi_ids);
  258. static struct platform_driver exiu_driver = {
  259. .driver = {
  260. .name = "exiu",
  261. .acpi_match_table = exiu_acpi_ids,
  262. },
  263. .probe = exiu_acpi_probe,
  264. };
  265. builtin_platform_driver(exiu_driver);
  266. #endif