prm_common.c 11 KB

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