irq-renesas-intc-irqpin.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  1. /*
  2. * Renesas INTC External IRQ Pin 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-intc-irqpin.h>
  31. #define INTC_IRQPIN_MAX 8 /* maximum 8 interrupts per driver instance */
  32. #define INTC_IRQPIN_REG_SENSE 0 /* ICRn */
  33. #define INTC_IRQPIN_REG_PRIO 1 /* INTPRInn */
  34. #define INTC_IRQPIN_REG_SOURCE 2 /* INTREQnn */
  35. #define INTC_IRQPIN_REG_MASK 3 /* INTMSKnn */
  36. #define INTC_IRQPIN_REG_CLEAR 4 /* INTMSKCLRnn */
  37. #define INTC_IRQPIN_REG_NR 5
  38. /* INTC external IRQ PIN hardware register access:
  39. *
  40. * SENSE is read-write 32-bit with 2-bits or 4-bits per IRQ (*)
  41. * PRIO is read-write 32-bit with 4-bits per IRQ (**)
  42. * SOURCE is read-only 32-bit or 8-bit with 1-bit per IRQ (***)
  43. * MASK is write-only 32-bit or 8-bit with 1-bit per IRQ (***)
  44. * CLEAR is write-only 32-bit or 8-bit with 1-bit per IRQ (***)
  45. *
  46. * (*) May be accessed by more than one driver instance - lock needed
  47. * (**) Read-modify-write access by one driver instance - lock needed
  48. * (***) Accessed by one driver instance only - no locking needed
  49. */
  50. struct intc_irqpin_iomem {
  51. void __iomem *iomem;
  52. unsigned long (*read)(void __iomem *iomem);
  53. void (*write)(void __iomem *iomem, unsigned long data);
  54. int width;
  55. };
  56. struct intc_irqpin_irq {
  57. int hw_irq;
  58. int requested_irq;
  59. int domain_irq;
  60. struct intc_irqpin_priv *p;
  61. };
  62. struct intc_irqpin_priv {
  63. struct intc_irqpin_iomem iomem[INTC_IRQPIN_REG_NR];
  64. struct intc_irqpin_irq irq[INTC_IRQPIN_MAX];
  65. struct renesas_intc_irqpin_config config;
  66. unsigned int number_of_irqs;
  67. struct platform_device *pdev;
  68. struct irq_chip irq_chip;
  69. struct irq_domain *irq_domain;
  70. };
  71. static unsigned long intc_irqpin_read32(void __iomem *iomem)
  72. {
  73. return ioread32(iomem);
  74. }
  75. static unsigned long intc_irqpin_read8(void __iomem *iomem)
  76. {
  77. return ioread8(iomem);
  78. }
  79. static void intc_irqpin_write32(void __iomem *iomem, unsigned long data)
  80. {
  81. iowrite32(data, iomem);
  82. }
  83. static void intc_irqpin_write8(void __iomem *iomem, unsigned long data)
  84. {
  85. iowrite8(data, iomem);
  86. }
  87. static inline unsigned long intc_irqpin_read(struct intc_irqpin_priv *p,
  88. int reg)
  89. {
  90. struct intc_irqpin_iomem *i = &p->iomem[reg];
  91. return i->read(i->iomem);
  92. }
  93. static inline void intc_irqpin_write(struct intc_irqpin_priv *p,
  94. int reg, unsigned long data)
  95. {
  96. struct intc_irqpin_iomem *i = &p->iomem[reg];
  97. i->write(i->iomem, data);
  98. }
  99. static inline unsigned long intc_irqpin_hwirq_mask(struct intc_irqpin_priv *p,
  100. int reg, int hw_irq)
  101. {
  102. return BIT((p->iomem[reg].width - 1) - hw_irq);
  103. }
  104. static inline void intc_irqpin_irq_write_hwirq(struct intc_irqpin_priv *p,
  105. int reg, int hw_irq)
  106. {
  107. intc_irqpin_write(p, reg, intc_irqpin_hwirq_mask(p, reg, hw_irq));
  108. }
  109. static DEFINE_RAW_SPINLOCK(intc_irqpin_lock); /* only used by slow path */
  110. static void intc_irqpin_read_modify_write(struct intc_irqpin_priv *p,
  111. int reg, int shift,
  112. int width, int value)
  113. {
  114. unsigned long flags;
  115. unsigned long tmp;
  116. raw_spin_lock_irqsave(&intc_irqpin_lock, flags);
  117. tmp = intc_irqpin_read(p, reg);
  118. tmp &= ~(((1 << width) - 1) << shift);
  119. tmp |= value << shift;
  120. intc_irqpin_write(p, reg, tmp);
  121. raw_spin_unlock_irqrestore(&intc_irqpin_lock, flags);
  122. }
  123. static void intc_irqpin_mask_unmask_prio(struct intc_irqpin_priv *p,
  124. int irq, int do_mask)
  125. {
  126. int bitfield_width = 4; /* PRIO assumed to have fixed bitfield width */
  127. int shift = (7 - irq) * bitfield_width; /* PRIO assumed to be 32-bit */
  128. intc_irqpin_read_modify_write(p, INTC_IRQPIN_REG_PRIO,
  129. shift, bitfield_width,
  130. do_mask ? 0 : (1 << bitfield_width) - 1);
  131. }
  132. static int intc_irqpin_set_sense(struct intc_irqpin_priv *p, int irq, int value)
  133. {
  134. int bitfield_width = p->config.sense_bitfield_width;
  135. int shift = (7 - irq) * bitfield_width; /* SENSE assumed to be 32-bit */
  136. dev_dbg(&p->pdev->dev, "sense irq = %d, mode = %d\n", irq, value);
  137. if (value >= (1 << bitfield_width))
  138. return -EINVAL;
  139. intc_irqpin_read_modify_write(p, INTC_IRQPIN_REG_SENSE, shift,
  140. bitfield_width, value);
  141. return 0;
  142. }
  143. static void intc_irqpin_dbg(struct intc_irqpin_irq *i, char *str)
  144. {
  145. dev_dbg(&i->p->pdev->dev, "%s (%d:%d:%d)\n",
  146. str, i->requested_irq, i->hw_irq, i->domain_irq);
  147. }
  148. static void intc_irqpin_irq_enable(struct irq_data *d)
  149. {
  150. struct intc_irqpin_priv *p = irq_data_get_irq_chip_data(d);
  151. int hw_irq = irqd_to_hwirq(d);
  152. intc_irqpin_dbg(&p->irq[hw_irq], "enable");
  153. intc_irqpin_irq_write_hwirq(p, INTC_IRQPIN_REG_CLEAR, hw_irq);
  154. }
  155. static void intc_irqpin_irq_disable(struct irq_data *d)
  156. {
  157. struct intc_irqpin_priv *p = irq_data_get_irq_chip_data(d);
  158. int hw_irq = irqd_to_hwirq(d);
  159. intc_irqpin_dbg(&p->irq[hw_irq], "disable");
  160. intc_irqpin_irq_write_hwirq(p, INTC_IRQPIN_REG_MASK, hw_irq);
  161. }
  162. static void intc_irqpin_irq_enable_force(struct irq_data *d)
  163. {
  164. struct intc_irqpin_priv *p = irq_data_get_irq_chip_data(d);
  165. int irq = p->irq[irqd_to_hwirq(d)].requested_irq;
  166. intc_irqpin_irq_enable(d);
  167. /* enable interrupt through parent interrupt controller,
  168. * assumes non-shared interrupt with 1:1 mapping
  169. * needed for busted IRQs on some SoCs like sh73a0
  170. */
  171. irq_get_chip(irq)->irq_unmask(irq_get_irq_data(irq));
  172. }
  173. static void intc_irqpin_irq_disable_force(struct irq_data *d)
  174. {
  175. struct intc_irqpin_priv *p = irq_data_get_irq_chip_data(d);
  176. int irq = p->irq[irqd_to_hwirq(d)].requested_irq;
  177. /* disable interrupt through parent interrupt controller,
  178. * assumes non-shared interrupt with 1:1 mapping
  179. * needed for busted IRQs on some SoCs like sh73a0
  180. */
  181. irq_get_chip(irq)->irq_mask(irq_get_irq_data(irq));
  182. intc_irqpin_irq_disable(d);
  183. }
  184. #define INTC_IRQ_SENSE_VALID 0x10
  185. #define INTC_IRQ_SENSE(x) (x + INTC_IRQ_SENSE_VALID)
  186. static unsigned char intc_irqpin_sense[IRQ_TYPE_SENSE_MASK + 1] = {
  187. [IRQ_TYPE_EDGE_FALLING] = INTC_IRQ_SENSE(0x00),
  188. [IRQ_TYPE_EDGE_RISING] = INTC_IRQ_SENSE(0x01),
  189. [IRQ_TYPE_LEVEL_LOW] = INTC_IRQ_SENSE(0x02),
  190. [IRQ_TYPE_LEVEL_HIGH] = INTC_IRQ_SENSE(0x03),
  191. [IRQ_TYPE_EDGE_BOTH] = INTC_IRQ_SENSE(0x04),
  192. };
  193. static int intc_irqpin_irq_set_type(struct irq_data *d, unsigned int type)
  194. {
  195. unsigned char value = intc_irqpin_sense[type & IRQ_TYPE_SENSE_MASK];
  196. struct intc_irqpin_priv *p = irq_data_get_irq_chip_data(d);
  197. if (!(value & INTC_IRQ_SENSE_VALID))
  198. return -EINVAL;
  199. return intc_irqpin_set_sense(p, irqd_to_hwirq(d),
  200. value ^ INTC_IRQ_SENSE_VALID);
  201. }
  202. static irqreturn_t intc_irqpin_irq_handler(int irq, void *dev_id)
  203. {
  204. struct intc_irqpin_irq *i = dev_id;
  205. struct intc_irqpin_priv *p = i->p;
  206. unsigned long bit;
  207. intc_irqpin_dbg(i, "demux1");
  208. bit = intc_irqpin_hwirq_mask(p, INTC_IRQPIN_REG_SOURCE, i->hw_irq);
  209. if (intc_irqpin_read(p, INTC_IRQPIN_REG_SOURCE) & bit) {
  210. intc_irqpin_write(p, INTC_IRQPIN_REG_SOURCE, ~bit);
  211. intc_irqpin_dbg(i, "demux2");
  212. generic_handle_irq(i->domain_irq);
  213. return IRQ_HANDLED;
  214. }
  215. return IRQ_NONE;
  216. }
  217. static int intc_irqpin_irq_domain_map(struct irq_domain *h, unsigned int virq,
  218. irq_hw_number_t hw)
  219. {
  220. struct intc_irqpin_priv *p = h->host_data;
  221. p->irq[hw].domain_irq = virq;
  222. p->irq[hw].hw_irq = hw;
  223. intc_irqpin_dbg(&p->irq[hw], "map");
  224. irq_set_chip_data(virq, h->host_data);
  225. irq_set_chip_and_handler(virq, &p->irq_chip, handle_level_irq);
  226. set_irq_flags(virq, IRQF_VALID); /* kill me now */
  227. return 0;
  228. }
  229. static struct irq_domain_ops intc_irqpin_irq_domain_ops = {
  230. .map = intc_irqpin_irq_domain_map,
  231. .xlate = irq_domain_xlate_twocell,
  232. };
  233. static int intc_irqpin_probe(struct platform_device *pdev)
  234. {
  235. struct renesas_intc_irqpin_config *pdata = pdev->dev.platform_data;
  236. struct intc_irqpin_priv *p;
  237. struct intc_irqpin_iomem *i;
  238. struct resource *io[INTC_IRQPIN_REG_NR];
  239. struct resource *irq;
  240. struct irq_chip *irq_chip;
  241. void (*enable_fn)(struct irq_data *d);
  242. void (*disable_fn)(struct irq_data *d);
  243. const char *name = dev_name(&pdev->dev);
  244. int ret;
  245. int k;
  246. p = devm_kzalloc(&pdev->dev, sizeof(*p), GFP_KERNEL);
  247. if (!p) {
  248. dev_err(&pdev->dev, "failed to allocate driver data\n");
  249. ret = -ENOMEM;
  250. goto err0;
  251. }
  252. /* deal with driver instance configuration */
  253. if (pdata)
  254. memcpy(&p->config, pdata, sizeof(*pdata));
  255. if (!p->config.sense_bitfield_width)
  256. p->config.sense_bitfield_width = 4; /* default to 4 bits */
  257. p->pdev = pdev;
  258. platform_set_drvdata(pdev, p);
  259. /* get hold of manadatory IOMEM */
  260. for (k = 0; k < INTC_IRQPIN_REG_NR; k++) {
  261. io[k] = platform_get_resource(pdev, IORESOURCE_MEM, k);
  262. if (!io[k]) {
  263. dev_err(&pdev->dev, "not enough IOMEM resources\n");
  264. ret = -EINVAL;
  265. goto err0;
  266. }
  267. }
  268. /* allow any number of IRQs between 1 and INTC_IRQPIN_MAX */
  269. for (k = 0; k < INTC_IRQPIN_MAX; k++) {
  270. irq = platform_get_resource(pdev, IORESOURCE_IRQ, k);
  271. if (!irq)
  272. break;
  273. p->irq[k].p = p;
  274. p->irq[k].requested_irq = irq->start;
  275. }
  276. p->number_of_irqs = k;
  277. if (p->number_of_irqs < 1) {
  278. dev_err(&pdev->dev, "not enough IRQ resources\n");
  279. ret = -EINVAL;
  280. goto err0;
  281. }
  282. /* ioremap IOMEM and setup read/write callbacks */
  283. for (k = 0; k < INTC_IRQPIN_REG_NR; k++) {
  284. i = &p->iomem[k];
  285. switch (resource_size(io[k])) {
  286. case 1:
  287. i->width = 8;
  288. i->read = intc_irqpin_read8;
  289. i->write = intc_irqpin_write8;
  290. break;
  291. case 4:
  292. i->width = 32;
  293. i->read = intc_irqpin_read32;
  294. i->write = intc_irqpin_write32;
  295. break;
  296. default:
  297. dev_err(&pdev->dev, "IOMEM size mismatch\n");
  298. ret = -EINVAL;
  299. goto err0;
  300. }
  301. i->iomem = devm_ioremap_nocache(&pdev->dev, io[k]->start,
  302. resource_size(io[k]));
  303. if (!i->iomem) {
  304. dev_err(&pdev->dev, "failed to remap IOMEM\n");
  305. ret = -ENXIO;
  306. goto err0;
  307. }
  308. }
  309. /* mask all interrupts using priority */
  310. for (k = 0; k < p->number_of_irqs; k++)
  311. intc_irqpin_mask_unmask_prio(p, k, 1);
  312. /* use more severe masking method if requested */
  313. if (p->config.control_parent) {
  314. enable_fn = intc_irqpin_irq_enable_force;
  315. disable_fn = intc_irqpin_irq_disable_force;
  316. } else {
  317. enable_fn = intc_irqpin_irq_enable;
  318. disable_fn = intc_irqpin_irq_disable;
  319. }
  320. irq_chip = &p->irq_chip;
  321. irq_chip->name = name;
  322. irq_chip->irq_mask = disable_fn;
  323. irq_chip->irq_unmask = enable_fn;
  324. irq_chip->irq_enable = enable_fn;
  325. irq_chip->irq_disable = disable_fn;
  326. irq_chip->irq_set_type = intc_irqpin_irq_set_type;
  327. irq_chip->flags = IRQCHIP_SKIP_SET_WAKE;
  328. p->irq_domain = irq_domain_add_simple(pdev->dev.of_node,
  329. p->number_of_irqs,
  330. p->config.irq_base,
  331. &intc_irqpin_irq_domain_ops, p);
  332. if (!p->irq_domain) {
  333. ret = -ENXIO;
  334. dev_err(&pdev->dev, "cannot initialize irq domain\n");
  335. goto err0;
  336. }
  337. /* request and set priority on interrupts one by one */
  338. for (k = 0; k < p->number_of_irqs; k++) {
  339. if (devm_request_irq(&pdev->dev, p->irq[k].requested_irq,
  340. intc_irqpin_irq_handler,
  341. 0, name, &p->irq[k])) {
  342. dev_err(&pdev->dev, "failed to request low IRQ\n");
  343. ret = -ENOENT;
  344. goto err1;
  345. }
  346. intc_irqpin_mask_unmask_prio(p, k, 0);
  347. }
  348. dev_info(&pdev->dev, "driving %d irqs\n", p->number_of_irqs);
  349. /* warn in case of mismatch if irq base is specified */
  350. if (p->config.irq_base) {
  351. if (p->config.irq_base != p->irq[0].domain_irq)
  352. dev_warn(&pdev->dev, "irq base mismatch (%d/%d)\n",
  353. p->config.irq_base, p->irq[0].domain_irq);
  354. }
  355. return 0;
  356. err1:
  357. irq_domain_remove(p->irq_domain);
  358. err0:
  359. return ret;
  360. }
  361. static int intc_irqpin_remove(struct platform_device *pdev)
  362. {
  363. struct intc_irqpin_priv *p = platform_get_drvdata(pdev);
  364. irq_domain_remove(p->irq_domain);
  365. return 0;
  366. }
  367. static const struct of_device_id intc_irqpin_dt_ids[] = {
  368. { .compatible = "renesas,intc-irqpin", },
  369. {},
  370. };
  371. MODULE_DEVICE_TABLE(of, intc_irqpin_dt_ids);
  372. static struct platform_driver intc_irqpin_device_driver = {
  373. .probe = intc_irqpin_probe,
  374. .remove = intc_irqpin_remove,
  375. .driver = {
  376. .name = "renesas_intc_irqpin",
  377. .of_match_table = intc_irqpin_dt_ids,
  378. .owner = THIS_MODULE,
  379. }
  380. };
  381. static int __init intc_irqpin_init(void)
  382. {
  383. return platform_driver_register(&intc_irqpin_device_driver);
  384. }
  385. postcore_initcall(intc_irqpin_init);
  386. static void __exit intc_irqpin_exit(void)
  387. {
  388. platform_driver_unregister(&intc_irqpin_device_driver);
  389. }
  390. module_exit(intc_irqpin_exit);
  391. MODULE_AUTHOR("Magnus Damm");
  392. MODULE_DESCRIPTION("Renesas INTC External IRQ Pin Driver");
  393. MODULE_LICENSE("GPL v2");