irq-imgpdc.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * IMG PowerDown Controller (PDC)
  4. *
  5. * Copyright 2010-2013 Imagination Technologies Ltd.
  6. *
  7. * Exposes the syswake and PDC peripheral wake interrupts to the system.
  8. *
  9. */
  10. #include <linux/bitops.h>
  11. #include <linux/interrupt.h>
  12. #include <linux/irqdomain.h>
  13. #include <linux/io.h>
  14. #include <linux/kernel.h>
  15. #include <linux/of.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/spinlock.h>
  18. /* PDC interrupt register numbers */
  19. #define PDC_IRQ_STATUS 0x310
  20. #define PDC_IRQ_ENABLE 0x314
  21. #define PDC_IRQ_CLEAR 0x318
  22. #define PDC_IRQ_ROUTE 0x31c
  23. #define PDC_SYS_WAKE_BASE 0x330
  24. #define PDC_SYS_WAKE_STRIDE 0x8
  25. #define PDC_SYS_WAKE_CONFIG_BASE 0x334
  26. #define PDC_SYS_WAKE_CONFIG_STRIDE 0x8
  27. /* PDC interrupt register field masks */
  28. #define PDC_IRQ_SYS3 0x08
  29. #define PDC_IRQ_SYS2 0x04
  30. #define PDC_IRQ_SYS1 0x02
  31. #define PDC_IRQ_SYS0 0x01
  32. #define PDC_IRQ_ROUTE_WU_EN_SYS3 0x08000000
  33. #define PDC_IRQ_ROUTE_WU_EN_SYS2 0x04000000
  34. #define PDC_IRQ_ROUTE_WU_EN_SYS1 0x02000000
  35. #define PDC_IRQ_ROUTE_WU_EN_SYS0 0x01000000
  36. #define PDC_IRQ_ROUTE_WU_EN_WD 0x00040000
  37. #define PDC_IRQ_ROUTE_WU_EN_IR 0x00020000
  38. #define PDC_IRQ_ROUTE_WU_EN_RTC 0x00010000
  39. #define PDC_IRQ_ROUTE_EXT_EN_SYS3 0x00000800
  40. #define PDC_IRQ_ROUTE_EXT_EN_SYS2 0x00000400
  41. #define PDC_IRQ_ROUTE_EXT_EN_SYS1 0x00000200
  42. #define PDC_IRQ_ROUTE_EXT_EN_SYS0 0x00000100
  43. #define PDC_IRQ_ROUTE_EXT_EN_WD 0x00000004
  44. #define PDC_IRQ_ROUTE_EXT_EN_IR 0x00000002
  45. #define PDC_IRQ_ROUTE_EXT_EN_RTC 0x00000001
  46. #define PDC_SYS_WAKE_RESET 0x00000010
  47. #define PDC_SYS_WAKE_INT_MODE 0x0000000e
  48. #define PDC_SYS_WAKE_INT_MODE_SHIFT 1
  49. #define PDC_SYS_WAKE_PIN_VAL 0x00000001
  50. /* PDC interrupt constants */
  51. #define PDC_SYS_WAKE_INT_LOW 0x0
  52. #define PDC_SYS_WAKE_INT_HIGH 0x1
  53. #define PDC_SYS_WAKE_INT_DOWN 0x2
  54. #define PDC_SYS_WAKE_INT_UP 0x3
  55. #define PDC_SYS_WAKE_INT_CHANGE 0x6
  56. #define PDC_SYS_WAKE_INT_NONE 0x4
  57. /**
  58. * struct pdc_intc_priv - private pdc interrupt data.
  59. * @nr_perips: Number of peripheral interrupt signals.
  60. * @nr_syswakes: Number of syswake signals.
  61. * @perip_irqs: List of peripheral IRQ numbers handled.
  62. * @syswake_irq: Shared PDC syswake IRQ number.
  63. * @domain: IRQ domain for PDC peripheral and syswake IRQs.
  64. * @pdc_base: Base of PDC registers.
  65. * @irq_route: Cached version of PDC_IRQ_ROUTE register.
  66. * @lock: Lock to protect the PDC syswake registers and the cached
  67. * values of those registers in this struct.
  68. */
  69. struct pdc_intc_priv {
  70. unsigned int nr_perips;
  71. unsigned int nr_syswakes;
  72. unsigned int *perip_irqs;
  73. unsigned int syswake_irq;
  74. struct irq_domain *domain;
  75. void __iomem *pdc_base;
  76. u32 irq_route;
  77. raw_spinlock_t lock;
  78. };
  79. static void pdc_write(struct pdc_intc_priv *priv, unsigned int reg_offs,
  80. unsigned int data)
  81. {
  82. iowrite32(data, priv->pdc_base + reg_offs);
  83. }
  84. static unsigned int pdc_read(struct pdc_intc_priv *priv,
  85. unsigned int reg_offs)
  86. {
  87. return ioread32(priv->pdc_base + reg_offs);
  88. }
  89. /* Generic IRQ callbacks */
  90. #define SYS0_HWIRQ 8
  91. static unsigned int hwirq_is_syswake(irq_hw_number_t hw)
  92. {
  93. return hw >= SYS0_HWIRQ;
  94. }
  95. static unsigned int hwirq_to_syswake(irq_hw_number_t hw)
  96. {
  97. return hw - SYS0_HWIRQ;
  98. }
  99. static irq_hw_number_t syswake_to_hwirq(unsigned int syswake)
  100. {
  101. return SYS0_HWIRQ + syswake;
  102. }
  103. static struct pdc_intc_priv *irqd_to_priv(struct irq_data *data)
  104. {
  105. return (struct pdc_intc_priv *)data->domain->host_data;
  106. }
  107. /*
  108. * perip_irq_mask() and perip_irq_unmask() use IRQ_ROUTE which also contains
  109. * wake bits, therefore we cannot use the generic irqchip mask callbacks as they
  110. * cache the mask.
  111. */
  112. static void perip_irq_mask(struct irq_data *data)
  113. {
  114. struct pdc_intc_priv *priv = irqd_to_priv(data);
  115. raw_spin_lock(&priv->lock);
  116. priv->irq_route &= ~data->mask;
  117. pdc_write(priv, PDC_IRQ_ROUTE, priv->irq_route);
  118. raw_spin_unlock(&priv->lock);
  119. }
  120. static void perip_irq_unmask(struct irq_data *data)
  121. {
  122. struct pdc_intc_priv *priv = irqd_to_priv(data);
  123. raw_spin_lock(&priv->lock);
  124. priv->irq_route |= data->mask;
  125. pdc_write(priv, PDC_IRQ_ROUTE, priv->irq_route);
  126. raw_spin_unlock(&priv->lock);
  127. }
  128. static int syswake_irq_set_type(struct irq_data *data, unsigned int flow_type)
  129. {
  130. struct pdc_intc_priv *priv = irqd_to_priv(data);
  131. unsigned int syswake = hwirq_to_syswake(data->hwirq);
  132. unsigned int irq_mode;
  133. unsigned int soc_sys_wake_regoff, soc_sys_wake;
  134. /* translate to syswake IRQ mode */
  135. switch (flow_type) {
  136. case IRQ_TYPE_EDGE_BOTH:
  137. irq_mode = PDC_SYS_WAKE_INT_CHANGE;
  138. break;
  139. case IRQ_TYPE_EDGE_RISING:
  140. irq_mode = PDC_SYS_WAKE_INT_UP;
  141. break;
  142. case IRQ_TYPE_EDGE_FALLING:
  143. irq_mode = PDC_SYS_WAKE_INT_DOWN;
  144. break;
  145. case IRQ_TYPE_LEVEL_HIGH:
  146. irq_mode = PDC_SYS_WAKE_INT_HIGH;
  147. break;
  148. case IRQ_TYPE_LEVEL_LOW:
  149. irq_mode = PDC_SYS_WAKE_INT_LOW;
  150. break;
  151. default:
  152. return -EINVAL;
  153. }
  154. raw_spin_lock(&priv->lock);
  155. /* set the IRQ mode */
  156. soc_sys_wake_regoff = PDC_SYS_WAKE_BASE + syswake*PDC_SYS_WAKE_STRIDE;
  157. soc_sys_wake = pdc_read(priv, soc_sys_wake_regoff);
  158. soc_sys_wake &= ~PDC_SYS_WAKE_INT_MODE;
  159. soc_sys_wake |= irq_mode << PDC_SYS_WAKE_INT_MODE_SHIFT;
  160. pdc_write(priv, soc_sys_wake_regoff, soc_sys_wake);
  161. /* and update the handler */
  162. irq_setup_alt_chip(data, flow_type);
  163. raw_spin_unlock(&priv->lock);
  164. return 0;
  165. }
  166. /* applies to both peripheral and syswake interrupts */
  167. static int pdc_irq_set_wake(struct irq_data *data, unsigned int on)
  168. {
  169. struct pdc_intc_priv *priv = irqd_to_priv(data);
  170. irq_hw_number_t hw = data->hwirq;
  171. unsigned int mask = (1 << 16) << hw;
  172. unsigned int dst_irq;
  173. raw_spin_lock(&priv->lock);
  174. if (on)
  175. priv->irq_route |= mask;
  176. else
  177. priv->irq_route &= ~mask;
  178. pdc_write(priv, PDC_IRQ_ROUTE, priv->irq_route);
  179. raw_spin_unlock(&priv->lock);
  180. /* control the destination IRQ wakeup too for standby mode */
  181. if (hwirq_is_syswake(hw))
  182. dst_irq = priv->syswake_irq;
  183. else
  184. dst_irq = priv->perip_irqs[hw];
  185. irq_set_irq_wake(dst_irq, on);
  186. return 0;
  187. }
  188. static void pdc_intc_perip_isr(struct irq_desc *desc)
  189. {
  190. unsigned int irq = irq_desc_get_irq(desc);
  191. struct pdc_intc_priv *priv;
  192. unsigned int i;
  193. priv = (struct pdc_intc_priv *)irq_desc_get_handler_data(desc);
  194. /* find the peripheral number */
  195. for (i = 0; i < priv->nr_perips; ++i)
  196. if (irq == priv->perip_irqs[i])
  197. goto found;
  198. /* should never get here */
  199. return;
  200. found:
  201. /* pass on the interrupt */
  202. generic_handle_domain_irq(priv->domain, i);
  203. }
  204. static void pdc_intc_syswake_isr(struct irq_desc *desc)
  205. {
  206. struct pdc_intc_priv *priv;
  207. unsigned int syswake;
  208. unsigned int status;
  209. priv = (struct pdc_intc_priv *)irq_desc_get_handler_data(desc);
  210. status = pdc_read(priv, PDC_IRQ_STATUS) &
  211. pdc_read(priv, PDC_IRQ_ENABLE);
  212. status &= (1 << priv->nr_syswakes) - 1;
  213. for (syswake = 0; status; status >>= 1, ++syswake) {
  214. /* Has this sys_wake triggered? */
  215. if (!(status & 1))
  216. continue;
  217. generic_handle_domain_irq(priv->domain, syswake_to_hwirq(syswake));
  218. }
  219. }
  220. static void pdc_intc_setup(struct pdc_intc_priv *priv)
  221. {
  222. int i;
  223. unsigned int soc_sys_wake_regoff;
  224. unsigned int soc_sys_wake;
  225. /*
  226. * Mask all syswake interrupts before routing, or we could receive an
  227. * interrupt before we're ready to handle it.
  228. */
  229. pdc_write(priv, PDC_IRQ_ENABLE, 0);
  230. /*
  231. * Enable routing of all syswakes
  232. * Disable all wake sources
  233. */
  234. priv->irq_route = ((PDC_IRQ_ROUTE_EXT_EN_SYS0 << priv->nr_syswakes) -
  235. PDC_IRQ_ROUTE_EXT_EN_SYS0);
  236. pdc_write(priv, PDC_IRQ_ROUTE, priv->irq_route);
  237. /* Initialise syswake IRQ */
  238. for (i = 0; i < priv->nr_syswakes; ++i) {
  239. /* set the IRQ mode to none */
  240. soc_sys_wake_regoff = PDC_SYS_WAKE_BASE + i*PDC_SYS_WAKE_STRIDE;
  241. soc_sys_wake = PDC_SYS_WAKE_INT_NONE
  242. << PDC_SYS_WAKE_INT_MODE_SHIFT;
  243. pdc_write(priv, soc_sys_wake_regoff, soc_sys_wake);
  244. }
  245. }
  246. static int pdc_intc_probe(struct platform_device *pdev)
  247. {
  248. struct pdc_intc_priv *priv;
  249. struct device_node *node = pdev->dev.of_node;
  250. struct resource *res_regs;
  251. struct irq_chip_generic *gc;
  252. unsigned int i;
  253. int irq, ret;
  254. u32 val;
  255. if (!node)
  256. return -ENOENT;
  257. /* Get registers */
  258. res_regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  259. if (res_regs == NULL) {
  260. dev_err(&pdev->dev, "cannot find registers resource\n");
  261. return -ENOENT;
  262. }
  263. /* Allocate driver data */
  264. priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
  265. if (!priv)
  266. return -ENOMEM;
  267. raw_spin_lock_init(&priv->lock);
  268. platform_set_drvdata(pdev, priv);
  269. /* Ioremap the registers */
  270. priv->pdc_base = devm_ioremap(&pdev->dev, res_regs->start,
  271. resource_size(res_regs));
  272. if (!priv->pdc_base)
  273. return -EIO;
  274. /* Get number of peripherals */
  275. ret = of_property_read_u32(node, "num-perips", &val);
  276. if (ret) {
  277. dev_err(&pdev->dev, "No num-perips node property found\n");
  278. return -EINVAL;
  279. }
  280. if (val > SYS0_HWIRQ) {
  281. dev_err(&pdev->dev, "num-perips (%u) out of range\n", val);
  282. return -EINVAL;
  283. }
  284. priv->nr_perips = val;
  285. /* Get number of syswakes */
  286. ret = of_property_read_u32(node, "num-syswakes", &val);
  287. if (ret) {
  288. dev_err(&pdev->dev, "No num-syswakes node property found\n");
  289. return -EINVAL;
  290. }
  291. if (val > SYS0_HWIRQ) {
  292. dev_err(&pdev->dev, "num-syswakes (%u) out of range\n", val);
  293. return -EINVAL;
  294. }
  295. priv->nr_syswakes = val;
  296. /* Get peripheral IRQ numbers */
  297. priv->perip_irqs = devm_kcalloc(&pdev->dev, 4, priv->nr_perips,
  298. GFP_KERNEL);
  299. if (!priv->perip_irqs)
  300. return -ENOMEM;
  301. for (i = 0; i < priv->nr_perips; ++i) {
  302. irq = platform_get_irq(pdev, 1 + i);
  303. if (irq < 0)
  304. return irq;
  305. priv->perip_irqs[i] = irq;
  306. }
  307. /* check if too many were provided */
  308. if (platform_get_irq(pdev, 1 + i) >= 0) {
  309. dev_err(&pdev->dev, "surplus perip IRQs detected\n");
  310. return -EINVAL;
  311. }
  312. /* Get syswake IRQ number */
  313. irq = platform_get_irq(pdev, 0);
  314. if (irq < 0)
  315. return irq;
  316. priv->syswake_irq = irq;
  317. /* Set up an IRQ domain */
  318. priv->domain = irq_domain_add_linear(node, 16, &irq_generic_chip_ops,
  319. priv);
  320. if (unlikely(!priv->domain)) {
  321. dev_err(&pdev->dev, "cannot add IRQ domain\n");
  322. return -ENOMEM;
  323. }
  324. /*
  325. * Set up 2 generic irq chips with 2 chip types.
  326. * The first one for peripheral irqs (only 1 chip type used)
  327. * The second one for syswake irqs (edge and level chip types)
  328. */
  329. ret = irq_alloc_domain_generic_chips(priv->domain, 8, 2, "pdc",
  330. handle_level_irq, 0, 0,
  331. IRQ_GC_INIT_NESTED_LOCK);
  332. if (ret)
  333. goto err_generic;
  334. /* peripheral interrupt chip */
  335. gc = irq_get_domain_generic_chip(priv->domain, 0);
  336. gc->unused = ~(BIT(priv->nr_perips) - 1);
  337. gc->reg_base = priv->pdc_base;
  338. /*
  339. * IRQ_ROUTE contains wake bits, so we can't use the generic versions as
  340. * they cache the mask
  341. */
  342. gc->chip_types[0].regs.mask = PDC_IRQ_ROUTE;
  343. gc->chip_types[0].chip.irq_mask = perip_irq_mask;
  344. gc->chip_types[0].chip.irq_unmask = perip_irq_unmask;
  345. gc->chip_types[0].chip.irq_set_wake = pdc_irq_set_wake;
  346. /* syswake interrupt chip */
  347. gc = irq_get_domain_generic_chip(priv->domain, 8);
  348. gc->unused = ~(BIT(priv->nr_syswakes) - 1);
  349. gc->reg_base = priv->pdc_base;
  350. /* edge interrupts */
  351. gc->chip_types[0].type = IRQ_TYPE_EDGE_BOTH;
  352. gc->chip_types[0].handler = handle_edge_irq;
  353. gc->chip_types[0].regs.ack = PDC_IRQ_CLEAR;
  354. gc->chip_types[0].regs.mask = PDC_IRQ_ENABLE;
  355. gc->chip_types[0].chip.irq_ack = irq_gc_ack_set_bit;
  356. gc->chip_types[0].chip.irq_mask = irq_gc_mask_clr_bit;
  357. gc->chip_types[0].chip.irq_unmask = irq_gc_mask_set_bit;
  358. gc->chip_types[0].chip.irq_set_type = syswake_irq_set_type;
  359. gc->chip_types[0].chip.irq_set_wake = pdc_irq_set_wake;
  360. /* for standby we pass on to the shared syswake IRQ */
  361. gc->chip_types[0].chip.flags = IRQCHIP_MASK_ON_SUSPEND;
  362. /* level interrupts */
  363. gc->chip_types[1].type = IRQ_TYPE_LEVEL_MASK;
  364. gc->chip_types[1].handler = handle_level_irq;
  365. gc->chip_types[1].regs.ack = PDC_IRQ_CLEAR;
  366. gc->chip_types[1].regs.mask = PDC_IRQ_ENABLE;
  367. gc->chip_types[1].chip.irq_ack = irq_gc_ack_set_bit;
  368. gc->chip_types[1].chip.irq_mask = irq_gc_mask_clr_bit;
  369. gc->chip_types[1].chip.irq_unmask = irq_gc_mask_set_bit;
  370. gc->chip_types[1].chip.irq_set_type = syswake_irq_set_type;
  371. gc->chip_types[1].chip.irq_set_wake = pdc_irq_set_wake;
  372. /* for standby we pass on to the shared syswake IRQ */
  373. gc->chip_types[1].chip.flags = IRQCHIP_MASK_ON_SUSPEND;
  374. /* Set up the hardware to enable interrupt routing */
  375. pdc_intc_setup(priv);
  376. /* Setup chained handlers for the peripheral IRQs */
  377. for (i = 0; i < priv->nr_perips; ++i) {
  378. irq = priv->perip_irqs[i];
  379. irq_set_chained_handler_and_data(irq, pdc_intc_perip_isr,
  380. priv);
  381. }
  382. /* Setup chained handler for the syswake IRQ */
  383. irq_set_chained_handler_and_data(priv->syswake_irq,
  384. pdc_intc_syswake_isr, priv);
  385. dev_info(&pdev->dev,
  386. "PDC IRQ controller initialised (%u perip IRQs, %u syswake IRQs)\n",
  387. priv->nr_perips,
  388. priv->nr_syswakes);
  389. return 0;
  390. err_generic:
  391. irq_domain_remove(priv->domain);
  392. return ret;
  393. }
  394. static int pdc_intc_remove(struct platform_device *pdev)
  395. {
  396. struct pdc_intc_priv *priv = platform_get_drvdata(pdev);
  397. irq_domain_remove(priv->domain);
  398. return 0;
  399. }
  400. static const struct of_device_id pdc_intc_match[] = {
  401. { .compatible = "img,pdc-intc" },
  402. {}
  403. };
  404. static struct platform_driver pdc_intc_driver = {
  405. .driver = {
  406. .name = "pdc-intc",
  407. .of_match_table = pdc_intc_match,
  408. },
  409. .probe = pdc_intc_probe,
  410. .remove = pdc_intc_remove,
  411. };
  412. static int __init pdc_intc_init(void)
  413. {
  414. return platform_driver_register(&pdc_intc_driver);
  415. }
  416. core_initcall(pdc_intc_init);