irq-imgpdc.c 13 KB

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