irq-renesas-intc-irqpin.c 15 KB

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