omap-wakeupgen.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. /*
  2. * OMAP WakeupGen Source file
  3. *
  4. * OMAP WakeupGen is the interrupt controller extension used along
  5. * with ARM GIC to wake the CPU out from low power states on
  6. * external interrupts. It is responsible for generating wakeup
  7. * event from the incoming interrupts and enable bits. It is
  8. * implemented in MPU always ON power domain. During normal operation,
  9. * WakeupGen delivers external interrupts directly to the GIC.
  10. *
  11. * Copyright (C) 2011 Texas Instruments, Inc.
  12. * Santosh Shilimkar <santosh.shilimkar@ti.com>
  13. *
  14. * This program is free software; you can redistribute it and/or modify
  15. * it under the terms of the GNU General Public License version 2 as
  16. * published by the Free Software Foundation.
  17. */
  18. #include <linux/kernel.h>
  19. #include <linux/init.h>
  20. #include <linux/io.h>
  21. #include <linux/irq.h>
  22. #include <linux/platform_device.h>
  23. #include <linux/cpu.h>
  24. #include <linux/notifier.h>
  25. #include <linux/cpu_pm.h>
  26. #include <asm/hardware/gic.h>
  27. #include <mach/omap-wakeupgen.h>
  28. #include <mach/omap-secure.h>
  29. #include "omap4-sar-layout.h"
  30. #include "common.h"
  31. #define NR_REG_BANKS 4
  32. #define MAX_IRQS 128
  33. #define WKG_MASK_ALL 0x00000000
  34. #define WKG_UNMASK_ALL 0xffffffff
  35. #define CPU_ENA_OFFSET 0x400
  36. #define CPU0_ID 0x0
  37. #define CPU1_ID 0x1
  38. static void __iomem *wakeupgen_base;
  39. static void __iomem *sar_base;
  40. static DEFINE_PER_CPU(u32 [NR_REG_BANKS], irqmasks);
  41. static DEFINE_SPINLOCK(wakeupgen_lock);
  42. static unsigned int irq_target_cpu[NR_IRQS];
  43. /*
  44. * Static helper functions.
  45. */
  46. static inline u32 wakeupgen_readl(u8 idx, u32 cpu)
  47. {
  48. return __raw_readl(wakeupgen_base + OMAP_WKG_ENB_A_0 +
  49. (cpu * CPU_ENA_OFFSET) + (idx * 4));
  50. }
  51. static inline void wakeupgen_writel(u32 val, u8 idx, u32 cpu)
  52. {
  53. __raw_writel(val, wakeupgen_base + OMAP_WKG_ENB_A_0 +
  54. (cpu * CPU_ENA_OFFSET) + (idx * 4));
  55. }
  56. static inline void sar_writel(u32 val, u32 offset, u8 idx)
  57. {
  58. __raw_writel(val, sar_base + offset + (idx * 4));
  59. }
  60. static void _wakeupgen_set_all(unsigned int cpu, unsigned int reg)
  61. {
  62. u8 i;
  63. for (i = 0; i < NR_REG_BANKS; i++)
  64. wakeupgen_writel(reg, i, cpu);
  65. }
  66. static inline int _wakeupgen_get_irq_info(u32 irq, u32 *bit_posn, u8 *reg_index)
  67. {
  68. unsigned int spi_irq;
  69. /*
  70. * PPIs and SGIs are not supported.
  71. */
  72. if (irq < OMAP44XX_IRQ_GIC_START)
  73. return -EINVAL;
  74. /*
  75. * Subtract the GIC offset.
  76. */
  77. spi_irq = irq - OMAP44XX_IRQ_GIC_START;
  78. if (spi_irq > MAX_IRQS) {
  79. pr_err("omap wakeupGen: Invalid IRQ%d\n", irq);
  80. return -EINVAL;
  81. }
  82. /*
  83. * Each WakeupGen register controls 32 interrupt.
  84. * i.e. 1 bit per SPI IRQ
  85. */
  86. *reg_index = spi_irq >> 5;
  87. *bit_posn = spi_irq %= 32;
  88. return 0;
  89. }
  90. static void _wakeupgen_clear(unsigned int irq, unsigned int cpu)
  91. {
  92. u32 val, bit_number;
  93. u8 i;
  94. if (_wakeupgen_get_irq_info(irq, &bit_number, &i))
  95. return;
  96. val = wakeupgen_readl(i, cpu);
  97. val &= ~BIT(bit_number);
  98. wakeupgen_writel(val, i, cpu);
  99. }
  100. static void _wakeupgen_set(unsigned int irq, unsigned int cpu)
  101. {
  102. u32 val, bit_number;
  103. u8 i;
  104. if (_wakeupgen_get_irq_info(irq, &bit_number, &i))
  105. return;
  106. val = wakeupgen_readl(i, cpu);
  107. val |= BIT(bit_number);
  108. wakeupgen_writel(val, i, cpu);
  109. }
  110. static void _wakeupgen_save_masks(unsigned int cpu)
  111. {
  112. u8 i;
  113. for (i = 0; i < NR_REG_BANKS; i++)
  114. per_cpu(irqmasks, cpu)[i] = wakeupgen_readl(i, cpu);
  115. }
  116. static void _wakeupgen_restore_masks(unsigned int cpu)
  117. {
  118. u8 i;
  119. for (i = 0; i < NR_REG_BANKS; i++)
  120. wakeupgen_writel(per_cpu(irqmasks, cpu)[i], i, cpu);
  121. }
  122. /*
  123. * Architecture specific Mask extension
  124. */
  125. static void wakeupgen_mask(struct irq_data *d)
  126. {
  127. unsigned long flags;
  128. spin_lock_irqsave(&wakeupgen_lock, flags);
  129. _wakeupgen_clear(d->irq, irq_target_cpu[d->irq]);
  130. spin_unlock_irqrestore(&wakeupgen_lock, flags);
  131. }
  132. /*
  133. * Architecture specific Unmask extension
  134. */
  135. static void wakeupgen_unmask(struct irq_data *d)
  136. {
  137. unsigned long flags;
  138. spin_lock_irqsave(&wakeupgen_lock, flags);
  139. _wakeupgen_set(d->irq, irq_target_cpu[d->irq]);
  140. spin_unlock_irqrestore(&wakeupgen_lock, flags);
  141. }
  142. /*
  143. * Mask or unmask all interrupts on given CPU.
  144. * 0 = Mask all interrupts on the 'cpu'
  145. * 1 = Unmask all interrupts on the 'cpu'
  146. * Ensure that the initial mask is maintained. This is faster than
  147. * iterating through GIC registers to arrive at the correct masks.
  148. */
  149. static void wakeupgen_irqmask_all(unsigned int cpu, unsigned int set)
  150. {
  151. unsigned long flags;
  152. spin_lock_irqsave(&wakeupgen_lock, flags);
  153. if (set) {
  154. _wakeupgen_save_masks(cpu);
  155. _wakeupgen_set_all(cpu, WKG_MASK_ALL);
  156. } else {
  157. _wakeupgen_set_all(cpu, WKG_UNMASK_ALL);
  158. _wakeupgen_restore_masks(cpu);
  159. }
  160. spin_unlock_irqrestore(&wakeupgen_lock, flags);
  161. }
  162. #ifdef CONFIG_CPU_PM
  163. /*
  164. * Save WakeupGen interrupt context in SAR BANK3. Restore is done by
  165. * ROM code. WakeupGen IP is integrated along with GIC to manage the
  166. * interrupt wakeups from CPU low power states. It manages
  167. * masking/unmasking of Shared peripheral interrupts(SPI). So the
  168. * interrupt enable/disable control should be in sync and consistent
  169. * at WakeupGen and GIC so that interrupts are not lost.
  170. */
  171. static void irq_save_context(void)
  172. {
  173. u32 i, val;
  174. if (omap_rev() == OMAP4430_REV_ES1_0)
  175. return;
  176. if (!sar_base)
  177. sar_base = omap4_get_sar_ram_base();
  178. for (i = 0; i < NR_REG_BANKS; i++) {
  179. /* Save the CPUx interrupt mask for IRQ 0 to 127 */
  180. val = wakeupgen_readl(i, 0);
  181. sar_writel(val, WAKEUPGENENB_OFFSET_CPU0, i);
  182. val = wakeupgen_readl(i, 1);
  183. sar_writel(val, WAKEUPGENENB_OFFSET_CPU1, i);
  184. /*
  185. * Disable the secure interrupts for CPUx. The restore
  186. * code blindly restores secure and non-secure interrupt
  187. * masks from SAR RAM. Secure interrupts are not suppose
  188. * to be enabled from HLOS. So overwrite the SAR location
  189. * so that the secure interrupt remains disabled.
  190. */
  191. sar_writel(0x0, WAKEUPGENENB_SECURE_OFFSET_CPU0, i);
  192. sar_writel(0x0, WAKEUPGENENB_SECURE_OFFSET_CPU1, i);
  193. }
  194. /* Save AuxBoot* registers */
  195. val = __raw_readl(wakeupgen_base + OMAP_AUX_CORE_BOOT_0);
  196. __raw_writel(val, sar_base + AUXCOREBOOT0_OFFSET);
  197. val = __raw_readl(wakeupgen_base + OMAP_AUX_CORE_BOOT_0);
  198. __raw_writel(val, sar_base + AUXCOREBOOT1_OFFSET);
  199. /* Save SyncReq generation logic */
  200. val = __raw_readl(wakeupgen_base + OMAP_AUX_CORE_BOOT_0);
  201. __raw_writel(val, sar_base + AUXCOREBOOT0_OFFSET);
  202. val = __raw_readl(wakeupgen_base + OMAP_AUX_CORE_BOOT_0);
  203. __raw_writel(val, sar_base + AUXCOREBOOT1_OFFSET);
  204. /* Save SyncReq generation logic */
  205. val = __raw_readl(wakeupgen_base + OMAP_PTMSYNCREQ_MASK);
  206. __raw_writel(val, sar_base + PTMSYNCREQ_MASK_OFFSET);
  207. val = __raw_readl(wakeupgen_base + OMAP_PTMSYNCREQ_EN);
  208. __raw_writel(val, sar_base + PTMSYNCREQ_EN_OFFSET);
  209. /* Set the Backup Bit Mask status */
  210. val = __raw_readl(sar_base + SAR_BACKUP_STATUS_OFFSET);
  211. val |= SAR_BACKUP_STATUS_WAKEUPGEN;
  212. __raw_writel(val, sar_base + SAR_BACKUP_STATUS_OFFSET);
  213. }
  214. /*
  215. * Clear WakeupGen SAR backup status.
  216. */
  217. void irq_sar_clear(void)
  218. {
  219. u32 val;
  220. val = __raw_readl(sar_base + SAR_BACKUP_STATUS_OFFSET);
  221. val &= ~SAR_BACKUP_STATUS_WAKEUPGEN;
  222. __raw_writel(val, sar_base + SAR_BACKUP_STATUS_OFFSET);
  223. }
  224. /*
  225. * Save GIC and Wakeupgen interrupt context using secure API
  226. * for HS/EMU devices.
  227. */
  228. static void irq_save_secure_context(void)
  229. {
  230. u32 ret;
  231. ret = omap_secure_dispatcher(OMAP4_HAL_SAVEGIC_INDEX,
  232. FLAG_START_CRITICAL,
  233. 0, 0, 0, 0, 0);
  234. if (ret != API_HAL_RET_VALUE_OK)
  235. pr_err("GIC and Wakeupgen context save failed\n");
  236. }
  237. #endif
  238. #ifdef CONFIG_HOTPLUG_CPU
  239. static int __cpuinit irq_cpu_hotplug_notify(struct notifier_block *self,
  240. unsigned long action, void *hcpu)
  241. {
  242. unsigned int cpu = (unsigned int)hcpu;
  243. switch (action) {
  244. case CPU_ONLINE:
  245. wakeupgen_irqmask_all(cpu, 0);
  246. break;
  247. case CPU_DEAD:
  248. wakeupgen_irqmask_all(cpu, 1);
  249. break;
  250. }
  251. return NOTIFY_OK;
  252. }
  253. static struct notifier_block __refdata irq_hotplug_notifier = {
  254. .notifier_call = irq_cpu_hotplug_notify,
  255. };
  256. static void __init irq_hotplug_init(void)
  257. {
  258. register_hotcpu_notifier(&irq_hotplug_notifier);
  259. }
  260. #else
  261. static void __init irq_hotplug_init(void)
  262. {}
  263. #endif
  264. #ifdef CONFIG_CPU_PM
  265. static int irq_notifier(struct notifier_block *self, unsigned long cmd, void *v)
  266. {
  267. switch (cmd) {
  268. case CPU_CLUSTER_PM_ENTER:
  269. if (omap_type() == OMAP2_DEVICE_TYPE_GP)
  270. irq_save_context();
  271. else
  272. irq_save_secure_context();
  273. break;
  274. case CPU_CLUSTER_PM_EXIT:
  275. if (omap_type() == OMAP2_DEVICE_TYPE_GP)
  276. irq_sar_clear();
  277. break;
  278. }
  279. return NOTIFY_OK;
  280. }
  281. static struct notifier_block irq_notifier_block = {
  282. .notifier_call = irq_notifier,
  283. };
  284. static void __init irq_pm_init(void)
  285. {
  286. cpu_pm_register_notifier(&irq_notifier_block);
  287. }
  288. #else
  289. static void __init irq_pm_init(void)
  290. {}
  291. #endif
  292. /*
  293. * Initialise the wakeupgen module.
  294. */
  295. int __init omap_wakeupgen_init(void)
  296. {
  297. int i;
  298. unsigned int boot_cpu = smp_processor_id();
  299. /* Not supported on OMAP4 ES1.0 silicon */
  300. if (omap_rev() == OMAP4430_REV_ES1_0) {
  301. WARN(1, "WakeupGen: Not supported on OMAP4430 ES1.0\n");
  302. return -EPERM;
  303. }
  304. /* Static mapping, never released */
  305. wakeupgen_base = ioremap(OMAP44XX_WKUPGEN_BASE, SZ_4K);
  306. if (WARN_ON(!wakeupgen_base))
  307. return -ENOMEM;
  308. /* Clear all IRQ bitmasks at wakeupGen level */
  309. for (i = 0; i < NR_REG_BANKS; i++) {
  310. wakeupgen_writel(0, i, CPU0_ID);
  311. wakeupgen_writel(0, i, CPU1_ID);
  312. }
  313. /*
  314. * Override GIC architecture specific functions to add
  315. * OMAP WakeupGen interrupt controller along with GIC
  316. */
  317. gic_arch_extn.irq_mask = wakeupgen_mask;
  318. gic_arch_extn.irq_unmask = wakeupgen_unmask;
  319. gic_arch_extn.flags = IRQCHIP_MASK_ON_SUSPEND | IRQCHIP_SKIP_SET_WAKE;
  320. /*
  321. * FIXME: Add support to set_smp_affinity() once the core
  322. * GIC code has necessary hooks in place.
  323. */
  324. /* Associate all the IRQs to boot CPU like GIC init does. */
  325. for (i = 0; i < NR_IRQS; i++)
  326. irq_target_cpu[i] = boot_cpu;
  327. irq_hotplug_init();
  328. irq_pm_init();
  329. return 0;
  330. }