prm_common.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  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 <plat/prcm.h>
  26. #include "prm2xxx_3xxx.h"
  27. #include "prm44xx.h"
  28. /*
  29. * OMAP_PRCM_MAX_NR_PENDING_REG: maximum number of PRM_IRQ*_MPU regs
  30. * XXX this is technically not needed, since
  31. * omap_prcm_register_chain_handler() could allocate this based on the
  32. * actual amount of memory needed for the SoC
  33. */
  34. #define OMAP_PRCM_MAX_NR_PENDING_REG 2
  35. /*
  36. * prcm_irq_chips: an array of all of the "generic IRQ chips" in use
  37. * by the PRCM interrupt handler code. There will be one 'chip' per
  38. * PRM_{IRQSTATUS,IRQENABLE}_MPU register pair. (So OMAP3 will have
  39. * one "chip" and OMAP4 will have two.)
  40. */
  41. static struct irq_chip_generic **prcm_irq_chips;
  42. /*
  43. * prcm_irq_setup: the PRCM IRQ parameters for the hardware the code
  44. * is currently running on. Defined and passed by initialization code
  45. * that calls omap_prcm_register_chain_handler().
  46. */
  47. static struct omap_prcm_irq_setup *prcm_irq_setup;
  48. /* Private functions */
  49. /*
  50. * Move priority events from events to priority_events array
  51. */
  52. static void omap_prcm_events_filter_priority(unsigned long *events,
  53. unsigned long *priority_events)
  54. {
  55. int i;
  56. for (i = 0; i < prcm_irq_setup->nr_regs; i++) {
  57. priority_events[i] =
  58. events[i] & prcm_irq_setup->priority_mask[i];
  59. events[i] ^= priority_events[i];
  60. }
  61. }
  62. /*
  63. * PRCM Interrupt Handler
  64. *
  65. * This is a common handler for the OMAP PRCM interrupts. Pending
  66. * interrupts are detected by a call to prcm_pending_events and
  67. * dispatched accordingly. Clearing of the wakeup events should be
  68. * done by the SoC specific individual handlers.
  69. */
  70. static void omap_prcm_irq_handler(unsigned int irq, struct irq_desc *desc)
  71. {
  72. unsigned long pending[OMAP_PRCM_MAX_NR_PENDING_REG];
  73. unsigned long priority_pending[OMAP_PRCM_MAX_NR_PENDING_REG];
  74. struct irq_chip *chip = irq_desc_get_chip(desc);
  75. unsigned int virtirq;
  76. int nr_irq = prcm_irq_setup->nr_regs * 32;
  77. /*
  78. * If we are suspended, mask all interrupts from PRCM level,
  79. * this does not ack them, and they will be pending until we
  80. * re-enable the interrupts, at which point the
  81. * omap_prcm_irq_handler will be executed again. The
  82. * _save_and_clear_irqen() function must ensure that the PRM
  83. * write to disable all IRQs has reached the PRM before
  84. * returning, or spurious PRCM interrupts may occur during
  85. * suspend.
  86. */
  87. if (prcm_irq_setup->suspended) {
  88. prcm_irq_setup->save_and_clear_irqen(prcm_irq_setup->saved_mask);
  89. prcm_irq_setup->suspend_save_flag = true;
  90. }
  91. /*
  92. * Loop until all pending irqs are handled, since
  93. * generic_handle_irq() can cause new irqs to come
  94. */
  95. while (!prcm_irq_setup->suspended) {
  96. prcm_irq_setup->read_pending_irqs(pending);
  97. /* No bit set, then all IRQs are handled */
  98. if (find_first_bit(pending, nr_irq) >= nr_irq)
  99. break;
  100. omap_prcm_events_filter_priority(pending, priority_pending);
  101. /*
  102. * Loop on all currently pending irqs so that new irqs
  103. * cannot starve previously pending irqs
  104. */
  105. /* Serve priority events first */
  106. for_each_set_bit(virtirq, priority_pending, nr_irq)
  107. generic_handle_irq(prcm_irq_setup->base_irq + virtirq);
  108. /* Serve normal events next */
  109. for_each_set_bit(virtirq, pending, nr_irq)
  110. generic_handle_irq(prcm_irq_setup->base_irq + virtirq);
  111. }
  112. if (chip->irq_ack)
  113. chip->irq_ack(&desc->irq_data);
  114. if (chip->irq_eoi)
  115. chip->irq_eoi(&desc->irq_data);
  116. chip->irq_unmask(&desc->irq_data);
  117. prcm_irq_setup->ocp_barrier(); /* avoid spurious IRQs */
  118. }
  119. /* Public functions */
  120. /**
  121. * omap_prcm_event_to_irq - given a PRCM event name, returns the
  122. * corresponding IRQ on which the handler should be registered
  123. * @name: name of the PRCM interrupt bit to look up - see struct omap_prcm_irq
  124. *
  125. * Returns the Linux internal IRQ ID corresponding to @name upon success,
  126. * or -ENOENT upon failure.
  127. */
  128. int omap_prcm_event_to_irq(const char *name)
  129. {
  130. int i;
  131. if (!prcm_irq_setup || !name)
  132. return -ENOENT;
  133. for (i = 0; i < prcm_irq_setup->nr_irqs; i++)
  134. if (!strcmp(prcm_irq_setup->irqs[i].name, name))
  135. return prcm_irq_setup->base_irq +
  136. prcm_irq_setup->irqs[i].offset;
  137. return -ENOENT;
  138. }
  139. /**
  140. * omap_prcm_irq_cleanup - reverses memory allocated and other steps
  141. * done by omap_prcm_register_chain_handler()
  142. *
  143. * No return value.
  144. */
  145. void omap_prcm_irq_cleanup(void)
  146. {
  147. int i;
  148. if (!prcm_irq_setup) {
  149. pr_err("PRCM: IRQ handler not initialized; cannot cleanup\n");
  150. return;
  151. }
  152. if (prcm_irq_chips) {
  153. for (i = 0; i < prcm_irq_setup->nr_regs; i++) {
  154. if (prcm_irq_chips[i])
  155. irq_remove_generic_chip(prcm_irq_chips[i],
  156. 0xffffffff, 0, 0);
  157. prcm_irq_chips[i] = NULL;
  158. }
  159. kfree(prcm_irq_chips);
  160. prcm_irq_chips = NULL;
  161. }
  162. kfree(prcm_irq_setup->saved_mask);
  163. prcm_irq_setup->saved_mask = NULL;
  164. kfree(prcm_irq_setup->priority_mask);
  165. prcm_irq_setup->priority_mask = NULL;
  166. irq_set_chained_handler(prcm_irq_setup->irq, NULL);
  167. if (prcm_irq_setup->base_irq > 0)
  168. irq_free_descs(prcm_irq_setup->base_irq,
  169. prcm_irq_setup->nr_regs * 32);
  170. prcm_irq_setup->base_irq = 0;
  171. }
  172. void omap_prcm_irq_prepare(void)
  173. {
  174. prcm_irq_setup->suspended = true;
  175. }
  176. void omap_prcm_irq_complete(void)
  177. {
  178. prcm_irq_setup->suspended = false;
  179. /* If we have not saved the masks, do not attempt to restore */
  180. if (!prcm_irq_setup->suspend_save_flag)
  181. return;
  182. prcm_irq_setup->suspend_save_flag = false;
  183. /*
  184. * Re-enable all masked PRCM irq sources, this causes the PRCM
  185. * interrupt to fire immediately if the events were masked
  186. * previously in the chain handler
  187. */
  188. prcm_irq_setup->restore_irqen(prcm_irq_setup->saved_mask);
  189. }
  190. /**
  191. * omap_prcm_register_chain_handler - initializes the prcm chained interrupt
  192. * handler based on provided parameters
  193. * @irq_setup: hardware data about the underlying PRM/PRCM
  194. *
  195. * Set up the PRCM chained interrupt handler on the PRCM IRQ. Sets up
  196. * one generic IRQ chip per PRM interrupt status/enable register pair.
  197. * Returns 0 upon success, -EINVAL if called twice or if invalid
  198. * arguments are passed, or -ENOMEM on any other error.
  199. */
  200. int omap_prcm_register_chain_handler(struct omap_prcm_irq_setup *irq_setup)
  201. {
  202. int nr_regs;
  203. u32 mask[OMAP_PRCM_MAX_NR_PENDING_REG];
  204. int offset, i;
  205. struct irq_chip_generic *gc;
  206. struct irq_chip_type *ct;
  207. if (!irq_setup)
  208. return -EINVAL;
  209. nr_regs = irq_setup->nr_regs;
  210. if (prcm_irq_setup) {
  211. pr_err("PRCM: already initialized; won't reinitialize\n");
  212. return -EINVAL;
  213. }
  214. if (nr_regs > OMAP_PRCM_MAX_NR_PENDING_REG) {
  215. pr_err("PRCM: nr_regs too large\n");
  216. return -EINVAL;
  217. }
  218. prcm_irq_setup = irq_setup;
  219. prcm_irq_chips = kzalloc(sizeof(void *) * nr_regs, GFP_KERNEL);
  220. prcm_irq_setup->saved_mask = kzalloc(sizeof(u32) * nr_regs, GFP_KERNEL);
  221. prcm_irq_setup->priority_mask = kzalloc(sizeof(u32) * nr_regs,
  222. GFP_KERNEL);
  223. if (!prcm_irq_chips || !prcm_irq_setup->saved_mask ||
  224. !prcm_irq_setup->priority_mask) {
  225. pr_err("PRCM: kzalloc failed\n");
  226. goto err;
  227. }
  228. memset(mask, 0, sizeof(mask));
  229. for (i = 0; i < irq_setup->nr_irqs; i++) {
  230. offset = irq_setup->irqs[i].offset;
  231. mask[offset >> 5] |= 1 << (offset & 0x1f);
  232. if (irq_setup->irqs[i].priority)
  233. irq_setup->priority_mask[offset >> 5] |=
  234. 1 << (offset & 0x1f);
  235. }
  236. irq_set_chained_handler(irq_setup->irq, omap_prcm_irq_handler);
  237. irq_setup->base_irq = irq_alloc_descs(-1, 0, irq_setup->nr_regs * 32,
  238. 0);
  239. if (irq_setup->base_irq < 0) {
  240. pr_err("PRCM: failed to allocate irq descs: %d\n",
  241. irq_setup->base_irq);
  242. goto err;
  243. }
  244. for (i = 0; i < irq_setup->nr_regs; i++) {
  245. gc = irq_alloc_generic_chip("PRCM", 1,
  246. irq_setup->base_irq + i * 32, prm_base,
  247. handle_level_irq);
  248. if (!gc) {
  249. pr_err("PRCM: failed to allocate generic chip\n");
  250. goto err;
  251. }
  252. ct = gc->chip_types;
  253. ct->chip.irq_ack = irq_gc_ack_set_bit;
  254. ct->chip.irq_mask = irq_gc_mask_clr_bit;
  255. ct->chip.irq_unmask = irq_gc_mask_set_bit;
  256. ct->regs.ack = irq_setup->ack + i * 4;
  257. ct->regs.mask = irq_setup->mask + i * 4;
  258. irq_setup_generic_chip(gc, mask[i], 0, IRQ_NOREQUEST, 0);
  259. prcm_irq_chips[i] = gc;
  260. }
  261. return 0;
  262. err:
  263. omap_prcm_irq_cleanup();
  264. return -ENOMEM;
  265. }
  266. /*
  267. * Stubbed functions so that common files continue to build when
  268. * custom builds are used
  269. * XXX These are temporary and should be removed at the earliest possible
  270. * opportunity
  271. */
  272. u32 __weak omap2_prm_read_mod_reg(s16 module, u16 idx)
  273. {
  274. WARN(1, "prm: omap2xxx/omap3xxx specific function called on non-omap2xxx/3xxx\n");
  275. return 0;
  276. }
  277. void __weak omap2_prm_write_mod_reg(u32 val, s16 module, u16 idx)
  278. {
  279. WARN(1, "prm: omap2xxx/omap3xxx specific function called on non-omap2xxx/3xxx\n");
  280. }
  281. u32 __weak omap2_prm_rmw_mod_reg_bits(u32 mask, u32 bits,
  282. s16 module, s16 idx)
  283. {
  284. WARN(1, "prm: omap2xxx/omap3xxx specific function called on non-omap2xxx/3xxx\n");
  285. return 0;
  286. }
  287. u32 __weak omap2_prm_set_mod_reg_bits(u32 bits, s16 module, s16 idx)
  288. {
  289. WARN(1, "prm: omap2xxx/omap3xxx specific function called on non-omap2xxx/3xxx\n");
  290. return 0;
  291. }
  292. u32 __weak omap2_prm_clear_mod_reg_bits(u32 bits, s16 module, s16 idx)
  293. {
  294. WARN(1, "prm: omap2xxx/omap3xxx specific function called on non-omap2xxx/3xxx\n");
  295. return 0;
  296. }
  297. u32 __weak omap2_prm_read_mod_bits_shift(s16 domain, s16 idx, u32 mask)
  298. {
  299. WARN(1, "prm: omap2xxx/omap3xxx specific function called on non-omap2xxx/3xxx\n");
  300. return 0;
  301. }
  302. int __weak omap2_prm_is_hardreset_asserted(s16 prm_mod, u8 shift)
  303. {
  304. WARN(1, "prm: omap2xxx/omap3xxx specific function called on non-omap2xxx/3xxx\n");
  305. return 0;
  306. }
  307. int __weak omap2_prm_assert_hardreset(s16 prm_mod, u8 shift)
  308. {
  309. WARN(1, "prm: omap2xxx/omap3xxx specific function called on non-omap2xxx/3xxx\n");
  310. return 0;
  311. }
  312. int __weak omap2_prm_deassert_hardreset(s16 prm_mod, u8 rst_shift,
  313. u8 st_shift)
  314. {
  315. WARN(1, "prm: omap2xxx/omap3xxx specific function called on non-omap2xxx/3xxx\n");
  316. return 0;
  317. }