omap-wakeupgen.c 9.6 KB

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