irq-renesas-irqc.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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. static unsigned char irqc_sense[IRQ_TYPE_SENSE_MASK + 1] = {
  74. [IRQ_TYPE_LEVEL_LOW] = 0x01,
  75. [IRQ_TYPE_LEVEL_HIGH] = 0x02,
  76. [IRQ_TYPE_EDGE_FALLING] = 0x04, /* Synchronous */
  77. [IRQ_TYPE_EDGE_RISING] = 0x08, /* Synchronous */
  78. [IRQ_TYPE_EDGE_BOTH] = 0x0c, /* Synchronous */
  79. };
  80. static int irqc_irq_set_type(struct irq_data *d, unsigned int type)
  81. {
  82. struct irqc_priv *p = irq_data_get_irq_chip_data(d);
  83. int hw_irq = irqd_to_hwirq(d);
  84. unsigned char value = irqc_sense[type & IRQ_TYPE_SENSE_MASK];
  85. unsigned long tmp;
  86. irqc_dbg(&p->irq[hw_irq], "sense");
  87. if (!value)
  88. return -EINVAL;
  89. tmp = ioread32(p->iomem + IRQC_CONFIG(hw_irq));
  90. tmp &= ~0x3f;
  91. tmp |= value;
  92. iowrite32(tmp, p->iomem + IRQC_CONFIG(hw_irq));
  93. return 0;
  94. }
  95. static irqreturn_t irqc_irq_handler(int irq, void *dev_id)
  96. {
  97. struct irqc_irq *i = dev_id;
  98. struct irqc_priv *p = i->p;
  99. unsigned long bit = BIT(i->hw_irq);
  100. irqc_dbg(i, "demux1");
  101. if (ioread32(p->iomem + DETECT_STATUS) & bit) {
  102. iowrite32(bit, p->iomem + DETECT_STATUS);
  103. irqc_dbg(i, "demux2");
  104. generic_handle_irq(i->domain_irq);
  105. return IRQ_HANDLED;
  106. }
  107. return IRQ_NONE;
  108. }
  109. static int irqc_irq_domain_map(struct irq_domain *h, unsigned int virq,
  110. irq_hw_number_t hw)
  111. {
  112. struct irqc_priv *p = h->host_data;
  113. p->irq[hw].domain_irq = virq;
  114. p->irq[hw].hw_irq = hw;
  115. irqc_dbg(&p->irq[hw], "map");
  116. irq_set_chip_data(virq, h->host_data);
  117. irq_set_chip_and_handler(virq, &p->irq_chip, handle_level_irq);
  118. set_irq_flags(virq, IRQF_VALID); /* kill me now */
  119. return 0;
  120. }
  121. static struct irq_domain_ops irqc_irq_domain_ops = {
  122. .map = irqc_irq_domain_map,
  123. .xlate = irq_domain_xlate_twocell,
  124. };
  125. static int irqc_probe(struct platform_device *pdev)
  126. {
  127. struct renesas_irqc_config *pdata = pdev->dev.platform_data;
  128. struct irqc_priv *p;
  129. struct resource *io;
  130. struct resource *irq;
  131. struct irq_chip *irq_chip;
  132. const char *name = dev_name(&pdev->dev);
  133. int ret;
  134. int k;
  135. p = kzalloc(sizeof(*p), GFP_KERNEL);
  136. if (!p) {
  137. dev_err(&pdev->dev, "failed to allocate driver data\n");
  138. ret = -ENOMEM;
  139. goto err0;
  140. }
  141. /* deal with driver instance configuration */
  142. if (pdata)
  143. memcpy(&p->config, pdata, sizeof(*pdata));
  144. p->pdev = pdev;
  145. platform_set_drvdata(pdev, p);
  146. /* get hold of manadatory IOMEM */
  147. io = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  148. if (!io) {
  149. dev_err(&pdev->dev, "not enough IOMEM resources\n");
  150. ret = -EINVAL;
  151. goto err1;
  152. }
  153. /* allow any number of IRQs between 1 and IRQC_IRQ_MAX */
  154. for (k = 0; k < IRQC_IRQ_MAX; k++) {
  155. irq = platform_get_resource(pdev, IORESOURCE_IRQ, k);
  156. if (!irq)
  157. break;
  158. p->irq[k].p = p;
  159. p->irq[k].requested_irq = irq->start;
  160. }
  161. p->number_of_irqs = k;
  162. if (p->number_of_irqs < 1) {
  163. dev_err(&pdev->dev, "not enough IRQ resources\n");
  164. ret = -EINVAL;
  165. goto err1;
  166. }
  167. /* ioremap IOMEM and setup read/write callbacks */
  168. p->iomem = ioremap_nocache(io->start, resource_size(io));
  169. if (!p->iomem) {
  170. dev_err(&pdev->dev, "failed to remap IOMEM\n");
  171. ret = -ENXIO;
  172. goto err2;
  173. }
  174. p->cpu_int_base = p->iomem + IRQC_INT_CPU_BASE(0); /* SYS-SPI */
  175. irq_chip = &p->irq_chip;
  176. irq_chip->name = name;
  177. irq_chip->irq_mask = irqc_irq_disable;
  178. irq_chip->irq_unmask = irqc_irq_enable;
  179. irq_chip->irq_set_type = irqc_irq_set_type;
  180. irq_chip->flags = IRQCHIP_SKIP_SET_WAKE | IRQCHIP_MASK_ON_SUSPEND;
  181. p->irq_domain = irq_domain_add_simple(pdev->dev.of_node,
  182. p->number_of_irqs,
  183. p->config.irq_base,
  184. &irqc_irq_domain_ops, p);
  185. if (!p->irq_domain) {
  186. ret = -ENXIO;
  187. dev_err(&pdev->dev, "cannot initialize irq domain\n");
  188. goto err2;
  189. }
  190. /* request interrupts one by one */
  191. for (k = 0; k < p->number_of_irqs; k++) {
  192. if (request_irq(p->irq[k].requested_irq, irqc_irq_handler,
  193. 0, name, &p->irq[k])) {
  194. dev_err(&pdev->dev, "failed to request IRQ\n");
  195. ret = -ENOENT;
  196. goto err3;
  197. }
  198. }
  199. dev_info(&pdev->dev, "driving %d irqs\n", p->number_of_irqs);
  200. /* warn in case of mismatch if irq base is specified */
  201. if (p->config.irq_base) {
  202. if (p->config.irq_base != p->irq[0].domain_irq)
  203. dev_warn(&pdev->dev, "irq base mismatch (%d/%d)\n",
  204. p->config.irq_base, p->irq[0].domain_irq);
  205. }
  206. return 0;
  207. err3:
  208. while (--k >= 0)
  209. free_irq(p->irq[k].requested_irq, &p->irq[k]);
  210. irq_domain_remove(p->irq_domain);
  211. err2:
  212. iounmap(p->iomem);
  213. err1:
  214. kfree(p);
  215. err0:
  216. return ret;
  217. }
  218. static int irqc_remove(struct platform_device *pdev)
  219. {
  220. struct irqc_priv *p = platform_get_drvdata(pdev);
  221. int k;
  222. for (k = 0; k < p->number_of_irqs; k++)
  223. free_irq(p->irq[k].requested_irq, &p->irq[k]);
  224. irq_domain_remove(p->irq_domain);
  225. iounmap(p->iomem);
  226. kfree(p);
  227. return 0;
  228. }
  229. static const struct of_device_id irqc_dt_ids[] = {
  230. { .compatible = "renesas,irqc", },
  231. {},
  232. };
  233. MODULE_DEVICE_TABLE(of, irqc_dt_ids);
  234. static struct platform_driver irqc_device_driver = {
  235. .probe = irqc_probe,
  236. .remove = irqc_remove,
  237. .driver = {
  238. .name = "renesas_irqc",
  239. .of_match_table = irqc_dt_ids,
  240. .owner = THIS_MODULE,
  241. }
  242. };
  243. static int __init irqc_init(void)
  244. {
  245. return platform_driver_register(&irqc_device_driver);
  246. }
  247. postcore_initcall(irqc_init);
  248. static void __exit irqc_exit(void)
  249. {
  250. platform_driver_unregister(&irqc_device_driver);
  251. }
  252. module_exit(irqc_exit);
  253. MODULE_AUTHOR("Magnus Damm");
  254. MODULE_DESCRIPTION("Renesas IRQC Driver");
  255. MODULE_LICENSE("GPL v2");