irq-renesas-irqc.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. /*
  2. * Renesas IRQC Driver
  3. *
  4. * Copyright (C) 2013 Magnus Damm
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. #include <linux/init.h>
  20. #include <linux/platform_device.h>
  21. #include <linux/spinlock.h>
  22. #include <linux/interrupt.h>
  23. #include <linux/ioport.h>
  24. #include <linux/io.h>
  25. #include <linux/irq.h>
  26. #include <linux/irqdomain.h>
  27. #include <linux/err.h>
  28. #include <linux/slab.h>
  29. #include <linux/module.h>
  30. #include <linux/platform_data/irq-renesas-irqc.h>
  31. #define IRQC_IRQ_MAX 32 /* maximum 32 interrupts per driver instance */
  32. #define IRQC_REQ_STS 0x00
  33. #define IRQC_EN_STS 0x04
  34. #define IRQC_EN_SET 0x08
  35. #define IRQC_INT_CPU_BASE(n) (0x000 + ((n) * 0x10))
  36. #define DETECT_STATUS 0x100
  37. #define IRQC_CONFIG(n) (0x180 + ((n) * 0x04))
  38. struct irqc_irq {
  39. int hw_irq;
  40. int requested_irq;
  41. int domain_irq;
  42. struct irqc_priv *p;
  43. };
  44. struct irqc_priv {
  45. void __iomem *iomem;
  46. void __iomem *cpu_int_base;
  47. struct irqc_irq irq[IRQC_IRQ_MAX];
  48. struct renesas_irqc_config config;
  49. unsigned int number_of_irqs;
  50. struct platform_device *pdev;
  51. struct irq_chip irq_chip;
  52. struct irq_domain *irq_domain;
  53. };
  54. static void irqc_dbg(struct irqc_irq *i, char *str)
  55. {
  56. dev_dbg(&i->p->pdev->dev, "%s (%d:%d:%d)\n",
  57. str, i->requested_irq, i->hw_irq, i->domain_irq);
  58. }
  59. static void irqc_irq_enable(struct irq_data *d)
  60. {
  61. struct irqc_priv *p = irq_data_get_irq_chip_data(d);
  62. int hw_irq = irqd_to_hwirq(d);
  63. irqc_dbg(&p->irq[hw_irq], "enable");
  64. iowrite32(BIT(hw_irq), p->cpu_int_base + IRQC_EN_SET);
  65. }
  66. static void irqc_irq_disable(struct irq_data *d)
  67. {
  68. struct irqc_priv *p = irq_data_get_irq_chip_data(d);
  69. int hw_irq = irqd_to_hwirq(d);
  70. irqc_dbg(&p->irq[hw_irq], "disable");
  71. iowrite32(BIT(hw_irq), p->cpu_int_base + IRQC_EN_STS);
  72. }
  73. #define INTC_IRQ_SENSE_VALID 0x10
  74. #define INTC_IRQ_SENSE(x) (x + INTC_IRQ_SENSE_VALID)
  75. static unsigned char irqc_sense[IRQ_TYPE_SENSE_MASK + 1] = {
  76. [IRQ_TYPE_LEVEL_LOW] = INTC_IRQ_SENSE(0x01),
  77. [IRQ_TYPE_LEVEL_HIGH] = INTC_IRQ_SENSE(0x02),
  78. [IRQ_TYPE_EDGE_FALLING] = INTC_IRQ_SENSE(0x04), /* Synchronous */
  79. [IRQ_TYPE_EDGE_RISING] = INTC_IRQ_SENSE(0x08), /* Synchronous */
  80. [IRQ_TYPE_EDGE_BOTH] = INTC_IRQ_SENSE(0x0c), /* Synchronous */
  81. };
  82. static int irqc_irq_set_type(struct irq_data *d, unsigned int type)
  83. {
  84. struct irqc_priv *p = irq_data_get_irq_chip_data(d);
  85. int hw_irq = irqd_to_hwirq(d);
  86. unsigned char value = irqc_sense[type & IRQ_TYPE_SENSE_MASK];
  87. unsigned long tmp;
  88. irqc_dbg(&p->irq[hw_irq], "sense");
  89. if (!(value & INTC_IRQ_SENSE_VALID))
  90. return -EINVAL;
  91. tmp = ioread32(p->iomem + IRQC_CONFIG(hw_irq));
  92. tmp &= ~0x3f;
  93. tmp |= value ^ INTC_IRQ_SENSE_VALID;
  94. iowrite32(tmp, p->iomem + IRQC_CONFIG(hw_irq));
  95. return 0;
  96. }
  97. static irqreturn_t irqc_irq_handler(int irq, void *dev_id)
  98. {
  99. struct irqc_irq *i = dev_id;
  100. struct irqc_priv *p = i->p;
  101. unsigned long bit = BIT(i->hw_irq);
  102. irqc_dbg(i, "demux1");
  103. if (ioread32(p->iomem + DETECT_STATUS) & bit) {
  104. iowrite32(bit, p->iomem + DETECT_STATUS);
  105. irqc_dbg(i, "demux2");
  106. generic_handle_irq(i->domain_irq);
  107. return IRQ_HANDLED;
  108. }
  109. return IRQ_NONE;
  110. }
  111. static int irqc_irq_domain_map(struct irq_domain *h, unsigned int virq,
  112. irq_hw_number_t hw)
  113. {
  114. struct irqc_priv *p = h->host_data;
  115. p->irq[hw].domain_irq = virq;
  116. p->irq[hw].hw_irq = hw;
  117. irqc_dbg(&p->irq[hw], "map");
  118. irq_set_chip_data(virq, h->host_data);
  119. irq_set_chip_and_handler(virq, &p->irq_chip, handle_level_irq);
  120. set_irq_flags(virq, IRQF_VALID); /* kill me now */
  121. return 0;
  122. }
  123. static struct irq_domain_ops irqc_irq_domain_ops = {
  124. .map = irqc_irq_domain_map,
  125. .xlate = irq_domain_xlate_twocell,
  126. };
  127. static int irqc_probe(struct platform_device *pdev)
  128. {
  129. struct renesas_irqc_config *pdata = pdev->dev.platform_data;
  130. struct irqc_priv *p;
  131. struct resource *io;
  132. struct resource *irq;
  133. struct irq_chip *irq_chip;
  134. const char *name = dev_name(&pdev->dev);
  135. int ret;
  136. int k;
  137. p = kzalloc(sizeof(*p), GFP_KERNEL);
  138. if (!p) {
  139. dev_err(&pdev->dev, "failed to allocate driver data\n");
  140. ret = -ENOMEM;
  141. goto err0;
  142. }
  143. /* deal with driver instance configuration */
  144. if (pdata)
  145. memcpy(&p->config, pdata, sizeof(*pdata));
  146. p->pdev = pdev;
  147. platform_set_drvdata(pdev, p);
  148. /* get hold of manadatory IOMEM */
  149. io = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  150. if (!io) {
  151. dev_err(&pdev->dev, "not enough IOMEM resources\n");
  152. ret = -EINVAL;
  153. goto err1;
  154. }
  155. /* allow any number of IRQs between 1 and IRQC_IRQ_MAX */
  156. for (k = 0; k < IRQC_IRQ_MAX; k++) {
  157. irq = platform_get_resource(pdev, IORESOURCE_IRQ, k);
  158. if (!irq)
  159. break;
  160. p->irq[k].p = p;
  161. p->irq[k].requested_irq = irq->start;
  162. }
  163. p->number_of_irqs = k;
  164. if (p->number_of_irqs < 1) {
  165. dev_err(&pdev->dev, "not enough IRQ resources\n");
  166. ret = -EINVAL;
  167. goto err1;
  168. }
  169. /* ioremap IOMEM and setup read/write callbacks */
  170. p->iomem = ioremap_nocache(io->start, resource_size(io));
  171. if (!p->iomem) {
  172. dev_err(&pdev->dev, "failed to remap IOMEM\n");
  173. ret = -ENXIO;
  174. goto err2;
  175. }
  176. p->cpu_int_base = p->iomem + IRQC_INT_CPU_BASE(0); /* SYS-SPI */
  177. irq_chip = &p->irq_chip;
  178. irq_chip->name = name;
  179. irq_chip->irq_mask = irqc_irq_disable;
  180. irq_chip->irq_unmask = irqc_irq_enable;
  181. irq_chip->irq_enable = irqc_irq_enable;
  182. irq_chip->irq_disable = irqc_irq_disable;
  183. irq_chip->irq_set_type = irqc_irq_set_type;
  184. irq_chip->flags = IRQCHIP_SKIP_SET_WAKE;
  185. p->irq_domain = irq_domain_add_simple(pdev->dev.of_node,
  186. p->number_of_irqs,
  187. p->config.irq_base,
  188. &irqc_irq_domain_ops, p);
  189. if (!p->irq_domain) {
  190. ret = -ENXIO;
  191. dev_err(&pdev->dev, "cannot initialize irq domain\n");
  192. goto err2;
  193. }
  194. /* request interrupts one by one */
  195. for (k = 0; k < p->number_of_irqs; k++) {
  196. if (request_irq(p->irq[k].requested_irq, irqc_irq_handler,
  197. 0, name, &p->irq[k])) {
  198. dev_err(&pdev->dev, "failed to request IRQ\n");
  199. ret = -ENOENT;
  200. goto err3;
  201. }
  202. }
  203. dev_info(&pdev->dev, "driving %d irqs\n", p->number_of_irqs);
  204. /* warn in case of mismatch if irq base is specified */
  205. if (p->config.irq_base) {
  206. if (p->config.irq_base != p->irq[0].domain_irq)
  207. dev_warn(&pdev->dev, "irq base mismatch (%d/%d)\n",
  208. p->config.irq_base, p->irq[0].domain_irq);
  209. }
  210. return 0;
  211. err3:
  212. while (--k >= 0)
  213. free_irq(p->irq[k].requested_irq, &p->irq[k]);
  214. irq_domain_remove(p->irq_domain);
  215. err2:
  216. iounmap(p->iomem);
  217. err1:
  218. kfree(p);
  219. err0:
  220. return ret;
  221. }
  222. static int irqc_remove(struct platform_device *pdev)
  223. {
  224. struct irqc_priv *p = platform_get_drvdata(pdev);
  225. int k;
  226. for (k = 0; k < p->number_of_irqs; k++)
  227. free_irq(p->irq[k].requested_irq, &p->irq[k]);
  228. irq_domain_remove(p->irq_domain);
  229. iounmap(p->iomem);
  230. kfree(p);
  231. return 0;
  232. }
  233. static const struct of_device_id irqc_dt_ids[] = {
  234. { .compatible = "renesas,irqc", },
  235. {},
  236. };
  237. MODULE_DEVICE_TABLE(of, irqc_dt_ids);
  238. static struct platform_driver irqc_device_driver = {
  239. .probe = irqc_probe,
  240. .remove = irqc_remove,
  241. .driver = {
  242. .name = "renesas_irqc",
  243. .of_match_table = irqc_dt_ids,
  244. .owner = THIS_MODULE,
  245. }
  246. };
  247. static int __init irqc_init(void)
  248. {
  249. return platform_driver_register(&irqc_device_driver);
  250. }
  251. postcore_initcall(irqc_init);
  252. static void __exit irqc_exit(void)
  253. {
  254. platform_driver_unregister(&irqc_device_driver);
  255. }
  256. module_exit(irqc_exit);
  257. MODULE_AUTHOR("Magnus Damm");
  258. MODULE_DESCRIPTION("Renesas IRQC Driver");
  259. MODULE_LICENSE("GPL v2");