irq-renesas-irqc.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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/clk.h>
  20. #include <linux/init.h>
  21. #include <linux/platform_device.h>
  22. #include <linux/spinlock.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/ioport.h>
  25. #include <linux/io.h>
  26. #include <linux/irq.h>
  27. #include <linux/irqdomain.h>
  28. #include <linux/err.h>
  29. #include <linux/slab.h>
  30. #include <linux/module.h>
  31. #include <linux/platform_data/irq-renesas-irqc.h>
  32. #include <linux/pm_runtime.h>
  33. #define IRQC_IRQ_MAX 32 /* maximum 32 interrupts per driver instance */
  34. #define IRQC_REQ_STS 0x00 /* Interrupt Request Status Register */
  35. #define IRQC_EN_STS 0x04 /* Interrupt Enable Status Register */
  36. #define IRQC_EN_SET 0x08 /* Interrupt Enable Set Register */
  37. #define IRQC_INT_CPU_BASE(n) (0x000 + ((n) * 0x10))
  38. /* SYS-CPU vs. RT-CPU */
  39. #define DETECT_STATUS 0x100 /* IRQn Detect Status Register */
  40. #define MONITOR 0x104 /* IRQn Signal Level Monitor Register */
  41. #define HLVL_STS 0x108 /* IRQn High Level Detect Status Register */
  42. #define LLVL_STS 0x10c /* IRQn Low Level Detect Status Register */
  43. #define S_R_EDGE_STS 0x110 /* IRQn Sync Rising Edge Detect Status Reg. */
  44. #define S_F_EDGE_STS 0x114 /* IRQn Sync Falling Edge Detect Status Reg. */
  45. #define A_R_EDGE_STS 0x118 /* IRQn Async Rising Edge Detect Status Reg. */
  46. #define A_F_EDGE_STS 0x11c /* IRQn Async Falling Edge Detect Status Reg. */
  47. #define CHTEN_STS 0x120 /* Chattering Reduction Status Register */
  48. #define IRQC_CONFIG(n) (0x180 + ((n) * 0x04))
  49. /* IRQn Configuration Register */
  50. struct irqc_irq {
  51. int hw_irq;
  52. int requested_irq;
  53. int domain_irq;
  54. struct irqc_priv *p;
  55. };
  56. struct irqc_priv {
  57. void __iomem *iomem;
  58. void __iomem *cpu_int_base;
  59. struct irqc_irq irq[IRQC_IRQ_MAX];
  60. struct renesas_irqc_config config;
  61. unsigned int number_of_irqs;
  62. struct platform_device *pdev;
  63. struct irq_chip irq_chip;
  64. struct irq_domain *irq_domain;
  65. struct clk *clk;
  66. };
  67. static void irqc_dbg(struct irqc_irq *i, char *str)
  68. {
  69. dev_dbg(&i->p->pdev->dev, "%s (%d:%d:%d)\n",
  70. str, i->requested_irq, i->hw_irq, i->domain_irq);
  71. }
  72. static void irqc_irq_enable(struct irq_data *d)
  73. {
  74. struct irqc_priv *p = irq_data_get_irq_chip_data(d);
  75. int hw_irq = irqd_to_hwirq(d);
  76. irqc_dbg(&p->irq[hw_irq], "enable");
  77. iowrite32(BIT(hw_irq), p->cpu_int_base + IRQC_EN_SET);
  78. }
  79. static void irqc_irq_disable(struct irq_data *d)
  80. {
  81. struct irqc_priv *p = irq_data_get_irq_chip_data(d);
  82. int hw_irq = irqd_to_hwirq(d);
  83. irqc_dbg(&p->irq[hw_irq], "disable");
  84. iowrite32(BIT(hw_irq), p->cpu_int_base + IRQC_EN_STS);
  85. }
  86. static unsigned char irqc_sense[IRQ_TYPE_SENSE_MASK + 1] = {
  87. [IRQ_TYPE_LEVEL_LOW] = 0x01,
  88. [IRQ_TYPE_LEVEL_HIGH] = 0x02,
  89. [IRQ_TYPE_EDGE_FALLING] = 0x04, /* Synchronous */
  90. [IRQ_TYPE_EDGE_RISING] = 0x08, /* Synchronous */
  91. [IRQ_TYPE_EDGE_BOTH] = 0x0c, /* Synchronous */
  92. };
  93. static int irqc_irq_set_type(struct irq_data *d, unsigned int type)
  94. {
  95. struct irqc_priv *p = irq_data_get_irq_chip_data(d);
  96. int hw_irq = irqd_to_hwirq(d);
  97. unsigned char value = irqc_sense[type & IRQ_TYPE_SENSE_MASK];
  98. u32 tmp;
  99. irqc_dbg(&p->irq[hw_irq], "sense");
  100. if (!value)
  101. return -EINVAL;
  102. tmp = ioread32(p->iomem + IRQC_CONFIG(hw_irq));
  103. tmp &= ~0x3f;
  104. tmp |= value;
  105. iowrite32(tmp, p->iomem + IRQC_CONFIG(hw_irq));
  106. return 0;
  107. }
  108. static int irqc_irq_set_wake(struct irq_data *d, unsigned int on)
  109. {
  110. struct irqc_priv *p = irq_data_get_irq_chip_data(d);
  111. if (!p->clk)
  112. return 0;
  113. if (on)
  114. clk_enable(p->clk);
  115. else
  116. clk_disable(p->clk);
  117. return 0;
  118. }
  119. static irqreturn_t irqc_irq_handler(int irq, void *dev_id)
  120. {
  121. struct irqc_irq *i = dev_id;
  122. struct irqc_priv *p = i->p;
  123. u32 bit = BIT(i->hw_irq);
  124. irqc_dbg(i, "demux1");
  125. if (ioread32(p->iomem + DETECT_STATUS) & bit) {
  126. iowrite32(bit, p->iomem + DETECT_STATUS);
  127. irqc_dbg(i, "demux2");
  128. generic_handle_irq(i->domain_irq);
  129. return IRQ_HANDLED;
  130. }
  131. return IRQ_NONE;
  132. }
  133. static int irqc_irq_domain_map(struct irq_domain *h, unsigned int virq,
  134. irq_hw_number_t hw)
  135. {
  136. struct irqc_priv *p = h->host_data;
  137. p->irq[hw].domain_irq = virq;
  138. p->irq[hw].hw_irq = hw;
  139. irqc_dbg(&p->irq[hw], "map");
  140. irq_set_chip_data(virq, h->host_data);
  141. irq_set_chip_and_handler(virq, &p->irq_chip, handle_level_irq);
  142. set_irq_flags(virq, IRQF_VALID); /* kill me now */
  143. return 0;
  144. }
  145. static struct irq_domain_ops irqc_irq_domain_ops = {
  146. .map = irqc_irq_domain_map,
  147. .xlate = irq_domain_xlate_twocell,
  148. };
  149. static int irqc_probe(struct platform_device *pdev)
  150. {
  151. struct renesas_irqc_config *pdata = pdev->dev.platform_data;
  152. struct irqc_priv *p;
  153. struct resource *io;
  154. struct resource *irq;
  155. struct irq_chip *irq_chip;
  156. const char *name = dev_name(&pdev->dev);
  157. int ret;
  158. int k;
  159. p = kzalloc(sizeof(*p), GFP_KERNEL);
  160. if (!p) {
  161. dev_err(&pdev->dev, "failed to allocate driver data\n");
  162. ret = -ENOMEM;
  163. goto err0;
  164. }
  165. /* deal with driver instance configuration */
  166. if (pdata)
  167. memcpy(&p->config, pdata, sizeof(*pdata));
  168. p->pdev = pdev;
  169. platform_set_drvdata(pdev, p);
  170. p->clk = devm_clk_get(&pdev->dev, NULL);
  171. if (IS_ERR(p->clk)) {
  172. dev_warn(&pdev->dev, "unable to get clock\n");
  173. p->clk = NULL;
  174. }
  175. pm_runtime_enable(&pdev->dev);
  176. pm_runtime_get_sync(&pdev->dev);
  177. /* get hold of manadatory IOMEM */
  178. io = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  179. if (!io) {
  180. dev_err(&pdev->dev, "not enough IOMEM resources\n");
  181. ret = -EINVAL;
  182. goto err1;
  183. }
  184. /* allow any number of IRQs between 1 and IRQC_IRQ_MAX */
  185. for (k = 0; k < IRQC_IRQ_MAX; k++) {
  186. irq = platform_get_resource(pdev, IORESOURCE_IRQ, k);
  187. if (!irq)
  188. break;
  189. p->irq[k].p = p;
  190. p->irq[k].requested_irq = irq->start;
  191. }
  192. p->number_of_irqs = k;
  193. if (p->number_of_irqs < 1) {
  194. dev_err(&pdev->dev, "not enough IRQ resources\n");
  195. ret = -EINVAL;
  196. goto err1;
  197. }
  198. /* ioremap IOMEM and setup read/write callbacks */
  199. p->iomem = ioremap_nocache(io->start, resource_size(io));
  200. if (!p->iomem) {
  201. dev_err(&pdev->dev, "failed to remap IOMEM\n");
  202. ret = -ENXIO;
  203. goto err2;
  204. }
  205. p->cpu_int_base = p->iomem + IRQC_INT_CPU_BASE(0); /* SYS-SPI */
  206. irq_chip = &p->irq_chip;
  207. irq_chip->name = name;
  208. irq_chip->irq_mask = irqc_irq_disable;
  209. irq_chip->irq_unmask = irqc_irq_enable;
  210. irq_chip->irq_set_type = irqc_irq_set_type;
  211. irq_chip->irq_set_wake = irqc_irq_set_wake;
  212. irq_chip->flags = IRQCHIP_MASK_ON_SUSPEND;
  213. p->irq_domain = irq_domain_add_simple(pdev->dev.of_node,
  214. p->number_of_irqs,
  215. p->config.irq_base,
  216. &irqc_irq_domain_ops, p);
  217. if (!p->irq_domain) {
  218. ret = -ENXIO;
  219. dev_err(&pdev->dev, "cannot initialize irq domain\n");
  220. goto err2;
  221. }
  222. /* request interrupts one by one */
  223. for (k = 0; k < p->number_of_irqs; k++) {
  224. if (request_irq(p->irq[k].requested_irq, irqc_irq_handler,
  225. 0, name, &p->irq[k])) {
  226. dev_err(&pdev->dev, "failed to request IRQ\n");
  227. ret = -ENOENT;
  228. goto err3;
  229. }
  230. }
  231. dev_info(&pdev->dev, "driving %d irqs\n", p->number_of_irqs);
  232. /* warn in case of mismatch if irq base is specified */
  233. if (p->config.irq_base) {
  234. if (p->config.irq_base != p->irq[0].domain_irq)
  235. dev_warn(&pdev->dev, "irq base mismatch (%d/%d)\n",
  236. p->config.irq_base, p->irq[0].domain_irq);
  237. }
  238. return 0;
  239. err3:
  240. while (--k >= 0)
  241. free_irq(p->irq[k].requested_irq, &p->irq[k]);
  242. irq_domain_remove(p->irq_domain);
  243. err2:
  244. iounmap(p->iomem);
  245. err1:
  246. pm_runtime_put(&pdev->dev);
  247. pm_runtime_disable(&pdev->dev);
  248. kfree(p);
  249. err0:
  250. return ret;
  251. }
  252. static int irqc_remove(struct platform_device *pdev)
  253. {
  254. struct irqc_priv *p = platform_get_drvdata(pdev);
  255. int k;
  256. for (k = 0; k < p->number_of_irqs; k++)
  257. free_irq(p->irq[k].requested_irq, &p->irq[k]);
  258. irq_domain_remove(p->irq_domain);
  259. iounmap(p->iomem);
  260. pm_runtime_put(&pdev->dev);
  261. pm_runtime_disable(&pdev->dev);
  262. kfree(p);
  263. return 0;
  264. }
  265. static const struct of_device_id irqc_dt_ids[] = {
  266. { .compatible = "renesas,irqc", },
  267. {},
  268. };
  269. MODULE_DEVICE_TABLE(of, irqc_dt_ids);
  270. static struct platform_driver irqc_device_driver = {
  271. .probe = irqc_probe,
  272. .remove = irqc_remove,
  273. .driver = {
  274. .name = "renesas_irqc",
  275. .of_match_table = irqc_dt_ids,
  276. }
  277. };
  278. static int __init irqc_init(void)
  279. {
  280. return platform_driver_register(&irqc_device_driver);
  281. }
  282. postcore_initcall(irqc_init);
  283. static void __exit irqc_exit(void)
  284. {
  285. platform_driver_unregister(&irqc_device_driver);
  286. }
  287. module_exit(irqc_exit);
  288. MODULE_AUTHOR("Magnus Damm");
  289. MODULE_DESCRIPTION("Renesas IRQC Driver");
  290. MODULE_LICENSE("GPL v2");