prm_common.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. /*
  2. * OMAP2+ common Power & Reset Management (PRM) IP block functions
  3. *
  4. * Copyright (C) 2011 Texas Instruments, Inc.
  5. * Tero Kristo <t-kristo@ti.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. *
  11. *
  12. * For historical purposes, the API used to configure the PRM
  13. * interrupt handler refers to it as the "PRCM interrupt." The
  14. * underlying registers are located in the PRM on OMAP3/4.
  15. *
  16. * XXX This code should eventually be moved to a PRM driver.
  17. */
  18. #include <linux/kernel.h>
  19. #include <linux/module.h>
  20. #include <linux/init.h>
  21. #include <linux/io.h>
  22. #include <linux/irq.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/slab.h>
  25. #include <mach/system.h>
  26. #include <plat/common.h>
  27. #include <plat/prcm.h>
  28. #include <plat/irqs.h>
  29. #include "prm2xxx_3xxx.h"
  30. #include "prm44xx.h"
  31. /*
  32. * OMAP_PRCM_MAX_NR_PENDING_REG: maximum number of PRM_IRQ*_MPU regs
  33. * XXX this is technically not needed, since
  34. * omap_prcm_register_chain_handler() could allocate this based on the
  35. * actual amount of memory needed for the SoC
  36. */
  37. #define OMAP_PRCM_MAX_NR_PENDING_REG 2
  38. /*
  39. * prcm_irq_chips: an array of all of the "generic IRQ chips" in use
  40. * by the PRCM interrupt handler code. There will be one 'chip' per
  41. * PRM_{IRQSTATUS,IRQENABLE}_MPU register pair. (So OMAP3 will have
  42. * one "chip" and OMAP4 will have two.)
  43. */
  44. static struct irq_chip_generic **prcm_irq_chips;
  45. /*
  46. * prcm_irq_setup: the PRCM IRQ parameters for the hardware the code
  47. * is currently running on. Defined and passed by initialization code
  48. * that calls omap_prcm_register_chain_handler().
  49. */
  50. static struct omap_prcm_irq_setup *prcm_irq_setup;
  51. /* Private functions */
  52. /*
  53. * Move priority events from events to priority_events array
  54. */
  55. static void omap_prcm_events_filter_priority(unsigned long *events,
  56. unsigned long *priority_events)
  57. {
  58. int i;
  59. for (i = 0; i < prcm_irq_setup->nr_regs; i++) {
  60. priority_events[i] =
  61. events[i] & prcm_irq_setup->priority_mask[i];
  62. events[i] ^= priority_events[i];
  63. }
  64. }
  65. /*
  66. * PRCM Interrupt Handler
  67. *
  68. * This is a common handler for the OMAP PRCM interrupts. Pending
  69. * interrupts are detected by a call to prcm_pending_events and
  70. * dispatched accordingly. Clearing of the wakeup events should be
  71. * done by the SoC specific individual handlers.
  72. */
  73. static void omap_prcm_irq_handler(unsigned int irq, struct irq_desc *desc)
  74. {
  75. unsigned long pending[OMAP_PRCM_MAX_NR_PENDING_REG];
  76. unsigned long priority_pending[OMAP_PRCM_MAX_NR_PENDING_REG];
  77. struct irq_chip *chip = irq_desc_get_chip(desc);
  78. unsigned int virtirq;
  79. int nr_irqs = prcm_irq_setup->nr_regs * 32;
  80. /*
  81. * If we are suspended, mask all interrupts from PRCM level,
  82. * this does not ack them, and they will be pending until we
  83. * re-enable the interrupts, at which point the
  84. * omap_prcm_irq_handler will be executed again. The
  85. * _save_and_clear_irqen() function must ensure that the PRM
  86. * write to disable all IRQs has reached the PRM before
  87. * returning, or spurious PRCM interrupts may occur during
  88. * suspend.
  89. */
  90. if (prcm_irq_setup->suspended) {
  91. prcm_irq_setup->save_and_clear_irqen(prcm_irq_setup->saved_mask);
  92. prcm_irq_setup->suspend_save_flag = true;
  93. }
  94. /*
  95. * Loop until all pending irqs are handled, since
  96. * generic_handle_irq() can cause new irqs to come
  97. */
  98. while (!prcm_irq_setup->suspended) {
  99. prcm_irq_setup->read_pending_irqs(pending);
  100. /* No bit set, then all IRQs are handled */
  101. if (find_first_bit(pending, nr_irqs) >= nr_irqs)
  102. break;
  103. omap_prcm_events_filter_priority(pending, priority_pending);
  104. /*
  105. * Loop on all currently pending irqs so that new irqs
  106. * cannot starve previously pending irqs
  107. */
  108. /* Serve priority events first */
  109. for_each_set_bit(virtirq, priority_pending, nr_irqs)
  110. generic_handle_irq(prcm_irq_setup->base_irq + virtirq);
  111. /* Serve normal events next */
  112. for_each_set_bit(virtirq, pending, nr_irqs)
  113. generic_handle_irq(prcm_irq_setup->base_irq + virtirq);
  114. }
  115. if (chip->irq_ack)
  116. chip->irq_ack(&desc->irq_data);
  117. if (chip->irq_eoi)
  118. chip->irq_eoi(&desc->irq_data);
  119. chip->irq_unmask(&desc->irq_data);
  120. prcm_irq_setup->ocp_barrier(); /* avoid spurious IRQs */
  121. }
  122. /* Public functions */
  123. /**
  124. * omap_prcm_event_to_irq - given a PRCM event name, returns the
  125. * corresponding IRQ on which the handler should be registered
  126. * @name: name of the PRCM interrupt bit to look up - see struct omap_prcm_irq
  127. *
  128. * Returns the Linux internal IRQ ID corresponding to @name upon success,
  129. * or -ENOENT upon failure.
  130. */
  131. int omap_prcm_event_to_irq(const char *name)
  132. {
  133. int i;
  134. if (!prcm_irq_setup || !name)
  135. return -ENOENT;
  136. for (i = 0; i < prcm_irq_setup->nr_irqs; i++)
  137. if (!strcmp(prcm_irq_setup->irqs[i].name, name))
  138. return prcm_irq_setup->base_irq +
  139. prcm_irq_setup->irqs[i].offset;
  140. return -ENOENT;
  141. }
  142. /**
  143. * omap_prcm_irq_cleanup - reverses memory allocated and other steps
  144. * done by omap_prcm_register_chain_handler()
  145. *
  146. * No return value.
  147. */
  148. void omap_prcm_irq_cleanup(void)
  149. {
  150. int i;
  151. if (!prcm_irq_setup) {
  152. pr_err("PRCM: IRQ handler not initialized; cannot cleanup\n");
  153. return;
  154. }
  155. if (prcm_irq_chips) {
  156. for (i = 0; i < prcm_irq_setup->nr_regs; i++) {
  157. if (prcm_irq_chips[i])
  158. irq_remove_generic_chip(prcm_irq_chips[i],
  159. 0xffffffff, 0, 0);
  160. prcm_irq_chips[i] = NULL;
  161. }
  162. kfree(prcm_irq_chips);
  163. prcm_irq_chips = NULL;
  164. }
  165. kfree(prcm_irq_setup->saved_mask);
  166. prcm_irq_setup->saved_mask = NULL;
  167. kfree(prcm_irq_setup->priority_mask);
  168. prcm_irq_setup->priority_mask = NULL;
  169. irq_set_chained_handler(prcm_irq_setup->irq, NULL);
  170. if (prcm_irq_setup->base_irq > 0)
  171. irq_free_descs(prcm_irq_setup->base_irq,
  172. prcm_irq_setup->nr_regs * 32);
  173. prcm_irq_setup->base_irq = 0;
  174. }
  175. void omap_prcm_irq_prepare(void)
  176. {
  177. prcm_irq_setup->suspended = true;
  178. }
  179. void omap_prcm_irq_complete(void)
  180. {
  181. prcm_irq_setup->suspended = false;
  182. /* If we have not saved the masks, do not attempt to restore */
  183. if (!prcm_irq_setup->suspend_save_flag)
  184. return;
  185. prcm_irq_setup->suspend_save_flag = false;
  186. /*
  187. * Re-enable all masked PRCM irq sources, this causes the PRCM
  188. * interrupt to fire immediately if the events were masked
  189. * previously in the chain handler
  190. */
  191. prcm_irq_setup->restore_irqen(prcm_irq_setup->saved_mask);
  192. }
  193. /**
  194. * omap_prcm_register_chain_handler - initializes the prcm chained interrupt
  195. * handler based on provided parameters
  196. * @irq_setup: hardware data about the underlying PRM/PRCM
  197. *
  198. * Set up the PRCM chained interrupt handler on the PRCM IRQ. Sets up
  199. * one generic IRQ chip per PRM interrupt status/enable register pair.
  200. * Returns 0 upon success, -EINVAL if called twice or if invalid
  201. * arguments are passed, or -ENOMEM on any other error.
  202. */
  203. int omap_prcm_register_chain_handler(struct omap_prcm_irq_setup *irq_setup)
  204. {
  205. int nr_regs = irq_setup->nr_regs;
  206. u32 mask[OMAP_PRCM_MAX_NR_PENDING_REG];
  207. int offset, i;
  208. struct irq_chip_generic *gc;
  209. struct irq_chip_type *ct;
  210. if (!irq_setup)
  211. return -EINVAL;
  212. if (prcm_irq_setup) {
  213. pr_err("PRCM: already initialized; won't reinitialize\n");
  214. return -EINVAL;
  215. }
  216. if (nr_regs > OMAP_PRCM_MAX_NR_PENDING_REG) {
  217. pr_err("PRCM: nr_regs too large\n");
  218. return -EINVAL;
  219. }
  220. prcm_irq_setup = irq_setup;
  221. prcm_irq_chips = kzalloc(sizeof(void *) * nr_regs, GFP_KERNEL);
  222. prcm_irq_setup->saved_mask = kzalloc(sizeof(u32) * nr_regs, GFP_KERNEL);
  223. prcm_irq_setup->priority_mask = kzalloc(sizeof(u32) * nr_regs,
  224. GFP_KERNEL);
  225. if (!prcm_irq_chips || !prcm_irq_setup->saved_mask ||
  226. !prcm_irq_setup->priority_mask) {
  227. pr_err("PRCM: kzalloc failed\n");
  228. goto err;
  229. }
  230. memset(mask, 0, sizeof(mask));
  231. for (i = 0; i < irq_setup->nr_irqs; i++) {
  232. offset = irq_setup->irqs[i].offset;
  233. mask[offset >> 5] |= 1 << (offset & 0x1f);
  234. if (irq_setup->irqs[i].priority)
  235. irq_setup->priority_mask[offset >> 5] |=
  236. 1 << (offset & 0x1f);
  237. }
  238. irq_set_chained_handler(irq_setup->irq, omap_prcm_irq_handler);
  239. irq_setup->base_irq = irq_alloc_descs(-1, 0, irq_setup->nr_regs * 32,
  240. 0);
  241. if (irq_setup->base_irq < 0) {
  242. pr_err("PRCM: failed to allocate irq descs: %d\n",
  243. irq_setup->base_irq);
  244. goto err;
  245. }
  246. for (i = 0; i <= irq_setup->nr_regs; i++) {
  247. gc = irq_alloc_generic_chip("PRCM", 1,
  248. irq_setup->base_irq + i * 32, prm_base,
  249. handle_level_irq);
  250. if (!gc) {
  251. pr_err("PRCM: failed to allocate generic chip\n");
  252. goto err;
  253. }
  254. ct = gc->chip_types;
  255. ct->chip.irq_ack = irq_gc_ack_set_bit;
  256. ct->chip.irq_mask = irq_gc_mask_clr_bit;
  257. ct->chip.irq_unmask = irq_gc_mask_set_bit;
  258. ct->regs.ack = irq_setup->ack + i * 4;
  259. ct->regs.mask = irq_setup->mask + i * 4;
  260. irq_setup_generic_chip(gc, mask[i], 0, IRQ_NOREQUEST, 0);
  261. prcm_irq_chips[i] = gc;
  262. }
  263. return 0;
  264. err:
  265. omap_prcm_irq_cleanup();
  266. return -ENOMEM;
  267. }