irq-imgpdc.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  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(unsigned int irq, struct irq_desc *desc)
  188. {
  189. struct pdc_intc_priv *priv;
  190. unsigned int i, irq_no;
  191. priv = (struct pdc_intc_priv *)irq_desc_get_handler_data(desc);
  192. /* find the peripheral number */
  193. for (i = 0; i < priv->nr_perips; ++i)
  194. if (irq == priv->perip_irqs[i])
  195. goto found;
  196. /* should never get here */
  197. return;
  198. found:
  199. /* pass on the interrupt */
  200. irq_no = irq_linear_revmap(priv->domain, i);
  201. generic_handle_irq(irq_no);
  202. }
  203. static void pdc_intc_syswake_isr(unsigned int irq, struct irq_desc *desc)
  204. {
  205. struct pdc_intc_priv *priv;
  206. unsigned int syswake, irq_no;
  207. unsigned int status;
  208. priv = (struct pdc_intc_priv *)irq_desc_get_handler_data(desc);
  209. status = pdc_read(priv, PDC_IRQ_STATUS) &
  210. pdc_read(priv, PDC_IRQ_ENABLE);
  211. status &= (1 << priv->nr_syswakes) - 1;
  212. for (syswake = 0; status; status >>= 1, ++syswake) {
  213. /* Has this sys_wake triggered? */
  214. if (!(status & 1))
  215. continue;
  216. irq_no = irq_linear_revmap(priv->domain,
  217. syswake_to_hwirq(syswake));
  218. generic_handle_irq(irq_no);
  219. }
  220. }
  221. static void pdc_intc_setup(struct pdc_intc_priv *priv)
  222. {
  223. int i;
  224. unsigned int soc_sys_wake_regoff;
  225. unsigned int soc_sys_wake;
  226. /*
  227. * Mask all syswake interrupts before routing, or we could receive an
  228. * interrupt before we're ready to handle it.
  229. */
  230. pdc_write(priv, PDC_IRQ_ENABLE, 0);
  231. /*
  232. * Enable routing of all syswakes
  233. * Disable all wake sources
  234. */
  235. priv->irq_route = ((PDC_IRQ_ROUTE_EXT_EN_SYS0 << priv->nr_syswakes) -
  236. PDC_IRQ_ROUTE_EXT_EN_SYS0);
  237. pdc_write(priv, PDC_IRQ_ROUTE, priv->irq_route);
  238. /* Initialise syswake IRQ */
  239. for (i = 0; i < priv->nr_syswakes; ++i) {
  240. /* set the IRQ mode to none */
  241. soc_sys_wake_regoff = PDC_SYS_WAKE_BASE + i*PDC_SYS_WAKE_STRIDE;
  242. soc_sys_wake = PDC_SYS_WAKE_INT_NONE
  243. << PDC_SYS_WAKE_INT_MODE_SHIFT;
  244. pdc_write(priv, soc_sys_wake_regoff, soc_sys_wake);
  245. }
  246. }
  247. static int pdc_intc_probe(struct platform_device *pdev)
  248. {
  249. struct pdc_intc_priv *priv;
  250. struct device_node *node = pdev->dev.of_node;
  251. struct resource *res_regs;
  252. struct irq_chip_generic *gc;
  253. unsigned int i;
  254. int irq, ret;
  255. u32 val;
  256. if (!node)
  257. return -ENOENT;
  258. /* Get registers */
  259. res_regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  260. if (res_regs == NULL) {
  261. dev_err(&pdev->dev, "cannot find registers resource\n");
  262. return -ENOENT;
  263. }
  264. /* Allocate driver data */
  265. priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
  266. if (!priv) {
  267. dev_err(&pdev->dev, "cannot allocate device data\n");
  268. return -ENOMEM;
  269. }
  270. raw_spin_lock_init(&priv->lock);
  271. platform_set_drvdata(pdev, priv);
  272. /* Ioremap the registers */
  273. priv->pdc_base = devm_ioremap(&pdev->dev, res_regs->start,
  274. res_regs->end - res_regs->start);
  275. if (!priv->pdc_base)
  276. return -EIO;
  277. /* Get number of peripherals */
  278. ret = of_property_read_u32(node, "num-perips", &val);
  279. if (ret) {
  280. dev_err(&pdev->dev, "No num-perips node property found\n");
  281. return -EINVAL;
  282. }
  283. if (val > SYS0_HWIRQ) {
  284. dev_err(&pdev->dev, "num-perips (%u) out of range\n", val);
  285. return -EINVAL;
  286. }
  287. priv->nr_perips = val;
  288. /* Get number of syswakes */
  289. ret = of_property_read_u32(node, "num-syswakes", &val);
  290. if (ret) {
  291. dev_err(&pdev->dev, "No num-syswakes node property found\n");
  292. return -EINVAL;
  293. }
  294. if (val > SYS0_HWIRQ) {
  295. dev_err(&pdev->dev, "num-syswakes (%u) out of range\n", val);
  296. return -EINVAL;
  297. }
  298. priv->nr_syswakes = val;
  299. /* Get peripheral IRQ numbers */
  300. priv->perip_irqs = devm_kzalloc(&pdev->dev, 4 * priv->nr_perips,
  301. GFP_KERNEL);
  302. if (!priv->perip_irqs) {
  303. dev_err(&pdev->dev, "cannot allocate perip IRQ list\n");
  304. return -ENOMEM;
  305. }
  306. for (i = 0; i < priv->nr_perips; ++i) {
  307. irq = platform_get_irq(pdev, 1 + i);
  308. if (irq < 0) {
  309. dev_err(&pdev->dev, "cannot find perip IRQ #%u\n", i);
  310. return irq;
  311. }
  312. priv->perip_irqs[i] = irq;
  313. }
  314. /* check if too many were provided */
  315. if (platform_get_irq(pdev, 1 + i) >= 0) {
  316. dev_err(&pdev->dev, "surplus perip IRQs detected\n");
  317. return -EINVAL;
  318. }
  319. /* Get syswake IRQ number */
  320. irq = platform_get_irq(pdev, 0);
  321. if (irq < 0) {
  322. dev_err(&pdev->dev, "cannot find syswake IRQ\n");
  323. return irq;
  324. }
  325. priv->syswake_irq = irq;
  326. /* Set up an IRQ domain */
  327. priv->domain = irq_domain_add_linear(node, 16, &irq_generic_chip_ops,
  328. priv);
  329. if (unlikely(!priv->domain)) {
  330. dev_err(&pdev->dev, "cannot add IRQ domain\n");
  331. return -ENOMEM;
  332. }
  333. /*
  334. * Set up 2 generic irq chips with 2 chip types.
  335. * The first one for peripheral irqs (only 1 chip type used)
  336. * The second one for syswake irqs (edge and level chip types)
  337. */
  338. ret = irq_alloc_domain_generic_chips(priv->domain, 8, 2, "pdc",
  339. handle_level_irq, 0, 0,
  340. IRQ_GC_INIT_NESTED_LOCK);
  341. if (ret)
  342. goto err_generic;
  343. /* peripheral interrupt chip */
  344. gc = irq_get_domain_generic_chip(priv->domain, 0);
  345. gc->unused = ~(BIT(priv->nr_perips) - 1);
  346. gc->reg_base = priv->pdc_base;
  347. /*
  348. * IRQ_ROUTE contains wake bits, so we can't use the generic versions as
  349. * they cache the mask
  350. */
  351. gc->chip_types[0].regs.mask = PDC_IRQ_ROUTE;
  352. gc->chip_types[0].chip.irq_mask = perip_irq_mask;
  353. gc->chip_types[0].chip.irq_unmask = perip_irq_unmask;
  354. gc->chip_types[0].chip.irq_set_wake = pdc_irq_set_wake;
  355. /* syswake interrupt chip */
  356. gc = irq_get_domain_generic_chip(priv->domain, 8);
  357. gc->unused = ~(BIT(priv->nr_syswakes) - 1);
  358. gc->reg_base = priv->pdc_base;
  359. /* edge interrupts */
  360. gc->chip_types[0].type = IRQ_TYPE_EDGE_BOTH;
  361. gc->chip_types[0].handler = handle_edge_irq;
  362. gc->chip_types[0].regs.ack = PDC_IRQ_CLEAR;
  363. gc->chip_types[0].regs.mask = PDC_IRQ_ENABLE;
  364. gc->chip_types[0].chip.irq_ack = irq_gc_ack_set_bit;
  365. gc->chip_types[0].chip.irq_mask = irq_gc_mask_clr_bit;
  366. gc->chip_types[0].chip.irq_unmask = irq_gc_mask_set_bit;
  367. gc->chip_types[0].chip.irq_set_type = syswake_irq_set_type;
  368. gc->chip_types[0].chip.irq_set_wake = pdc_irq_set_wake;
  369. /* for standby we pass on to the shared syswake IRQ */
  370. gc->chip_types[0].chip.flags = IRQCHIP_MASK_ON_SUSPEND;
  371. /* level interrupts */
  372. gc->chip_types[1].type = IRQ_TYPE_LEVEL_MASK;
  373. gc->chip_types[1].handler = handle_level_irq;
  374. gc->chip_types[1].regs.ack = PDC_IRQ_CLEAR;
  375. gc->chip_types[1].regs.mask = PDC_IRQ_ENABLE;
  376. gc->chip_types[1].chip.irq_ack = irq_gc_ack_set_bit;
  377. gc->chip_types[1].chip.irq_mask = irq_gc_mask_clr_bit;
  378. gc->chip_types[1].chip.irq_unmask = irq_gc_mask_set_bit;
  379. gc->chip_types[1].chip.irq_set_type = syswake_irq_set_type;
  380. gc->chip_types[1].chip.irq_set_wake = pdc_irq_set_wake;
  381. /* for standby we pass on to the shared syswake IRQ */
  382. gc->chip_types[1].chip.flags = IRQCHIP_MASK_ON_SUSPEND;
  383. /* Set up the hardware to enable interrupt routing */
  384. pdc_intc_setup(priv);
  385. /* Setup chained handlers for the peripheral IRQs */
  386. for (i = 0; i < priv->nr_perips; ++i) {
  387. irq = priv->perip_irqs[i];
  388. irq_set_handler_data(irq, priv);
  389. irq_set_chained_handler(irq, pdc_intc_perip_isr);
  390. }
  391. /* Setup chained handler for the syswake IRQ */
  392. irq_set_handler_data(priv->syswake_irq, priv);
  393. irq_set_chained_handler(priv->syswake_irq, pdc_intc_syswake_isr);
  394. dev_info(&pdev->dev,
  395. "PDC IRQ controller initialised (%u perip IRQs, %u syswake IRQs)\n",
  396. priv->nr_perips,
  397. priv->nr_syswakes);
  398. return 0;
  399. err_generic:
  400. irq_domain_remove(priv->domain);
  401. return ret;
  402. }
  403. static int pdc_intc_remove(struct platform_device *pdev)
  404. {
  405. struct pdc_intc_priv *priv = platform_get_drvdata(pdev);
  406. irq_domain_remove(priv->domain);
  407. return 0;
  408. }
  409. static const struct of_device_id pdc_intc_match[] = {
  410. { .compatible = "img,pdc-intc" },
  411. {}
  412. };
  413. static struct platform_driver pdc_intc_driver = {
  414. .driver = {
  415. .name = "pdc-intc",
  416. .of_match_table = pdc_intc_match,
  417. },
  418. .probe = pdc_intc_probe,
  419. .remove = pdc_intc_remove,
  420. };
  421. static int __init pdc_intc_init(void)
  422. {
  423. return platform_driver_register(&pdc_intc_driver);
  424. }
  425. core_initcall(pdc_intc_init);