irq-renesas-intc-irqpin.c 15 KB

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