prm_common.c 13 KB

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