omap-wakeupgen.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  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 <linux/irqchip/arm-gic.h>
  27. #include "omap-wakeupgen.h"
  28. #include "omap-secure.h"
  29. #include "soc.h"
  30. #include "omap4-sar-layout.h"
  31. #include "common.h"
  32. #include "pm.h"
  33. #define AM43XX_NR_REG_BANKS 7
  34. #define AM43XX_IRQS 224
  35. #define MAX_NR_REG_BANKS AM43XX_NR_REG_BANKS
  36. #define MAX_IRQS AM43XX_IRQS
  37. #define DEFAULT_NR_REG_BANKS 5
  38. #define DEFAULT_IRQS 160
  39. #define WKG_MASK_ALL 0x00000000
  40. #define WKG_UNMASK_ALL 0xffffffff
  41. #define CPU_ENA_OFFSET 0x400
  42. #define CPU0_ID 0x0
  43. #define CPU1_ID 0x1
  44. #define OMAP4_NR_BANKS 4
  45. #define OMAP4_NR_IRQS 128
  46. static void __iomem *wakeupgen_base;
  47. static void __iomem *sar_base;
  48. static DEFINE_RAW_SPINLOCK(wakeupgen_lock);
  49. static unsigned int irq_target_cpu[MAX_IRQS];
  50. static unsigned int irq_banks = DEFAULT_NR_REG_BANKS;
  51. static unsigned int max_irqs = DEFAULT_IRQS;
  52. static unsigned int omap_secure_apis;
  53. /*
  54. * Static helper functions.
  55. */
  56. static inline u32 wakeupgen_readl(u8 idx, u32 cpu)
  57. {
  58. return readl_relaxed(wakeupgen_base + OMAP_WKG_ENB_A_0 +
  59. (cpu * CPU_ENA_OFFSET) + (idx * 4));
  60. }
  61. static inline void wakeupgen_writel(u32 val, u8 idx, u32 cpu)
  62. {
  63. writel_relaxed(val, wakeupgen_base + OMAP_WKG_ENB_A_0 +
  64. (cpu * CPU_ENA_OFFSET) + (idx * 4));
  65. }
  66. static inline void sar_writel(u32 val, u32 offset, u8 idx)
  67. {
  68. writel_relaxed(val, sar_base + offset + (idx * 4));
  69. }
  70. static inline int _wakeupgen_get_irq_info(u32 irq, u32 *bit_posn, u8 *reg_index)
  71. {
  72. unsigned int spi_irq;
  73. /*
  74. * PPIs and SGIs are not supported.
  75. */
  76. if (irq < OMAP44XX_IRQ_GIC_START)
  77. return -EINVAL;
  78. /*
  79. * Subtract the GIC offset.
  80. */
  81. spi_irq = irq - OMAP44XX_IRQ_GIC_START;
  82. if (spi_irq > MAX_IRQS) {
  83. pr_err("omap wakeupGen: Invalid IRQ%d\n", irq);
  84. return -EINVAL;
  85. }
  86. /*
  87. * Each WakeupGen register controls 32 interrupt.
  88. * i.e. 1 bit per SPI IRQ
  89. */
  90. *reg_index = spi_irq >> 5;
  91. *bit_posn = spi_irq %= 32;
  92. return 0;
  93. }
  94. static void _wakeupgen_clear(unsigned int irq, unsigned int cpu)
  95. {
  96. u32 val, bit_number;
  97. u8 i;
  98. if (_wakeupgen_get_irq_info(irq, &bit_number, &i))
  99. return;
  100. val = wakeupgen_readl(i, cpu);
  101. val &= ~BIT(bit_number);
  102. wakeupgen_writel(val, i, cpu);
  103. }
  104. static void _wakeupgen_set(unsigned int irq, unsigned int cpu)
  105. {
  106. u32 val, bit_number;
  107. u8 i;
  108. if (_wakeupgen_get_irq_info(irq, &bit_number, &i))
  109. return;
  110. val = wakeupgen_readl(i, cpu);
  111. val |= BIT(bit_number);
  112. wakeupgen_writel(val, i, cpu);
  113. }
  114. /*
  115. * Architecture specific Mask extension
  116. */
  117. static void wakeupgen_mask(struct irq_data *d)
  118. {
  119. unsigned long flags;
  120. raw_spin_lock_irqsave(&wakeupgen_lock, flags);
  121. _wakeupgen_clear(d->hwirq, irq_target_cpu[d->hwirq]);
  122. raw_spin_unlock_irqrestore(&wakeupgen_lock, flags);
  123. }
  124. /*
  125. * Architecture specific Unmask extension
  126. */
  127. static void wakeupgen_unmask(struct irq_data *d)
  128. {
  129. unsigned long flags;
  130. raw_spin_lock_irqsave(&wakeupgen_lock, flags);
  131. _wakeupgen_set(d->hwirq, irq_target_cpu[d->hwirq]);
  132. raw_spin_unlock_irqrestore(&wakeupgen_lock, flags);
  133. }
  134. #ifdef CONFIG_HOTPLUG_CPU
  135. static DEFINE_PER_CPU(u32 [MAX_NR_REG_BANKS], irqmasks);
  136. static void _wakeupgen_save_masks(unsigned int cpu)
  137. {
  138. u8 i;
  139. for (i = 0; i < irq_banks; i++)
  140. per_cpu(irqmasks, cpu)[i] = wakeupgen_readl(i, cpu);
  141. }
  142. static void _wakeupgen_restore_masks(unsigned int cpu)
  143. {
  144. u8 i;
  145. for (i = 0; i < irq_banks; i++)
  146. wakeupgen_writel(per_cpu(irqmasks, cpu)[i], i, cpu);
  147. }
  148. static void _wakeupgen_set_all(unsigned int cpu, unsigned int reg)
  149. {
  150. u8 i;
  151. for (i = 0; i < irq_banks; i++)
  152. wakeupgen_writel(reg, i, cpu);
  153. }
  154. /*
  155. * Mask or unmask all interrupts on given CPU.
  156. * 0 = Mask all interrupts on the 'cpu'
  157. * 1 = Unmask all interrupts on the 'cpu'
  158. * Ensure that the initial mask is maintained. This is faster than
  159. * iterating through GIC registers to arrive at the correct masks.
  160. */
  161. static void wakeupgen_irqmask_all(unsigned int cpu, unsigned int set)
  162. {
  163. unsigned long flags;
  164. raw_spin_lock_irqsave(&wakeupgen_lock, flags);
  165. if (set) {
  166. _wakeupgen_save_masks(cpu);
  167. _wakeupgen_set_all(cpu, WKG_MASK_ALL);
  168. } else {
  169. _wakeupgen_set_all(cpu, WKG_UNMASK_ALL);
  170. _wakeupgen_restore_masks(cpu);
  171. }
  172. raw_spin_unlock_irqrestore(&wakeupgen_lock, flags);
  173. }
  174. #endif
  175. #ifdef CONFIG_CPU_PM
  176. static inline void omap4_irq_save_context(void)
  177. {
  178. u32 i, val;
  179. if (omap_rev() == OMAP4430_REV_ES1_0)
  180. return;
  181. for (i = 0; i < irq_banks; i++) {
  182. /* Save the CPUx interrupt mask for IRQ 0 to 127 */
  183. val = wakeupgen_readl(i, 0);
  184. sar_writel(val, WAKEUPGENENB_OFFSET_CPU0, i);
  185. val = wakeupgen_readl(i, 1);
  186. sar_writel(val, WAKEUPGENENB_OFFSET_CPU1, i);
  187. /*
  188. * Disable the secure interrupts for CPUx. The restore
  189. * code blindly restores secure and non-secure interrupt
  190. * masks from SAR RAM. Secure interrupts are not suppose
  191. * to be enabled from HLOS. So overwrite the SAR location
  192. * so that the secure interrupt remains disabled.
  193. */
  194. sar_writel(0x0, WAKEUPGENENB_SECURE_OFFSET_CPU0, i);
  195. sar_writel(0x0, WAKEUPGENENB_SECURE_OFFSET_CPU1, i);
  196. }
  197. /* Save AuxBoot* registers */
  198. val = readl_relaxed(wakeupgen_base + OMAP_AUX_CORE_BOOT_0);
  199. writel_relaxed(val, sar_base + AUXCOREBOOT0_OFFSET);
  200. val = readl_relaxed(wakeupgen_base + OMAP_AUX_CORE_BOOT_1);
  201. writel_relaxed(val, sar_base + AUXCOREBOOT1_OFFSET);
  202. /* Save SyncReq generation logic */
  203. val = readl_relaxed(wakeupgen_base + OMAP_PTMSYNCREQ_MASK);
  204. writel_relaxed(val, sar_base + PTMSYNCREQ_MASK_OFFSET);
  205. val = readl_relaxed(wakeupgen_base + OMAP_PTMSYNCREQ_EN);
  206. writel_relaxed(val, sar_base + PTMSYNCREQ_EN_OFFSET);
  207. /* Set the Backup Bit Mask status */
  208. val = readl_relaxed(sar_base + SAR_BACKUP_STATUS_OFFSET);
  209. val |= SAR_BACKUP_STATUS_WAKEUPGEN;
  210. writel_relaxed(val, sar_base + SAR_BACKUP_STATUS_OFFSET);
  211. }
  212. static inline void omap5_irq_save_context(void)
  213. {
  214. u32 i, val;
  215. for (i = 0; i < irq_banks; i++) {
  216. /* Save the CPUx interrupt mask for IRQ 0 to 159 */
  217. val = wakeupgen_readl(i, 0);
  218. sar_writel(val, OMAP5_WAKEUPGENENB_OFFSET_CPU0, i);
  219. val = wakeupgen_readl(i, 1);
  220. sar_writel(val, OMAP5_WAKEUPGENENB_OFFSET_CPU1, i);
  221. sar_writel(0x0, OMAP5_WAKEUPGENENB_SECURE_OFFSET_CPU0, i);
  222. sar_writel(0x0, OMAP5_WAKEUPGENENB_SECURE_OFFSET_CPU1, i);
  223. }
  224. /* Save AuxBoot* registers */
  225. val = readl_relaxed(wakeupgen_base + OMAP_AUX_CORE_BOOT_0);
  226. writel_relaxed(val, sar_base + OMAP5_AUXCOREBOOT0_OFFSET);
  227. val = readl_relaxed(wakeupgen_base + OMAP_AUX_CORE_BOOT_0);
  228. writel_relaxed(val, sar_base + OMAP5_AUXCOREBOOT1_OFFSET);
  229. /* Set the Backup Bit Mask status */
  230. val = readl_relaxed(sar_base + OMAP5_SAR_BACKUP_STATUS_OFFSET);
  231. val |= SAR_BACKUP_STATUS_WAKEUPGEN;
  232. writel_relaxed(val, sar_base + OMAP5_SAR_BACKUP_STATUS_OFFSET);
  233. }
  234. /*
  235. * Save WakeupGen interrupt context in SAR BANK3. Restore is done by
  236. * ROM code. WakeupGen IP is integrated along with GIC to manage the
  237. * interrupt wakeups from CPU low power states. It manages
  238. * masking/unmasking of Shared peripheral interrupts(SPI). So the
  239. * interrupt enable/disable control should be in sync and consistent
  240. * at WakeupGen and GIC so that interrupts are not lost.
  241. */
  242. static void irq_save_context(void)
  243. {
  244. if (!sar_base)
  245. sar_base = omap4_get_sar_ram_base();
  246. if (soc_is_omap54xx())
  247. omap5_irq_save_context();
  248. else
  249. omap4_irq_save_context();
  250. }
  251. /*
  252. * Clear WakeupGen SAR backup status.
  253. */
  254. static void irq_sar_clear(void)
  255. {
  256. u32 val;
  257. u32 offset = SAR_BACKUP_STATUS_OFFSET;
  258. if (soc_is_omap54xx())
  259. offset = OMAP5_SAR_BACKUP_STATUS_OFFSET;
  260. val = readl_relaxed(sar_base + offset);
  261. val &= ~SAR_BACKUP_STATUS_WAKEUPGEN;
  262. writel_relaxed(val, sar_base + offset);
  263. }
  264. /*
  265. * Save GIC and Wakeupgen interrupt context using secure API
  266. * for HS/EMU devices.
  267. */
  268. static void irq_save_secure_context(void)
  269. {
  270. u32 ret;
  271. ret = omap_secure_dispatcher(OMAP4_HAL_SAVEGIC_INDEX,
  272. FLAG_START_CRITICAL,
  273. 0, 0, 0, 0, 0);
  274. if (ret != API_HAL_RET_VALUE_OK)
  275. pr_err("GIC and Wakeupgen context save failed\n");
  276. }
  277. #endif
  278. #ifdef CONFIG_HOTPLUG_CPU
  279. static int irq_cpu_hotplug_notify(struct notifier_block *self,
  280. unsigned long action, void *hcpu)
  281. {
  282. unsigned int cpu = (unsigned int)hcpu;
  283. switch (action) {
  284. case CPU_ONLINE:
  285. wakeupgen_irqmask_all(cpu, 0);
  286. break;
  287. case CPU_DEAD:
  288. wakeupgen_irqmask_all(cpu, 1);
  289. break;
  290. }
  291. return NOTIFY_OK;
  292. }
  293. static struct notifier_block __refdata irq_hotplug_notifier = {
  294. .notifier_call = irq_cpu_hotplug_notify,
  295. };
  296. static void __init irq_hotplug_init(void)
  297. {
  298. register_hotcpu_notifier(&irq_hotplug_notifier);
  299. }
  300. #else
  301. static void __init irq_hotplug_init(void)
  302. {}
  303. #endif
  304. #ifdef CONFIG_CPU_PM
  305. static int irq_notifier(struct notifier_block *self, unsigned long cmd, void *v)
  306. {
  307. switch (cmd) {
  308. case CPU_CLUSTER_PM_ENTER:
  309. if (omap_type() == OMAP2_DEVICE_TYPE_GP)
  310. irq_save_context();
  311. else
  312. irq_save_secure_context();
  313. break;
  314. case CPU_CLUSTER_PM_EXIT:
  315. if (omap_type() == OMAP2_DEVICE_TYPE_GP)
  316. irq_sar_clear();
  317. break;
  318. }
  319. return NOTIFY_OK;
  320. }
  321. static struct notifier_block irq_notifier_block = {
  322. .notifier_call = irq_notifier,
  323. };
  324. static void __init irq_pm_init(void)
  325. {
  326. /* FIXME: Remove this when MPU OSWR support is added */
  327. if (!IS_PM44XX_ERRATUM(PM_OMAP4_CPU_OSWR_DISABLE))
  328. cpu_pm_register_notifier(&irq_notifier_block);
  329. }
  330. #else
  331. static void __init irq_pm_init(void)
  332. {}
  333. #endif
  334. void __iomem *omap_get_wakeupgen_base(void)
  335. {
  336. return wakeupgen_base;
  337. }
  338. int omap_secure_apis_support(void)
  339. {
  340. return omap_secure_apis;
  341. }
  342. /*
  343. * Initialise the wakeupgen module.
  344. */
  345. int __init omap_wakeupgen_init(void)
  346. {
  347. int i;
  348. unsigned int boot_cpu = smp_processor_id();
  349. u32 val;
  350. /* Not supported on OMAP4 ES1.0 silicon */
  351. if (omap_rev() == OMAP4430_REV_ES1_0) {
  352. WARN(1, "WakeupGen: Not supported on OMAP4430 ES1.0\n");
  353. return -EPERM;
  354. }
  355. /* Static mapping, never released */
  356. wakeupgen_base = ioremap(OMAP_WKUPGEN_BASE, SZ_4K);
  357. if (WARN_ON(!wakeupgen_base))
  358. return -ENOMEM;
  359. if (cpu_is_omap44xx()) {
  360. irq_banks = OMAP4_NR_BANKS;
  361. max_irqs = OMAP4_NR_IRQS;
  362. omap_secure_apis = 1;
  363. } else if (soc_is_am43xx()) {
  364. irq_banks = AM43XX_NR_REG_BANKS;
  365. max_irqs = AM43XX_IRQS;
  366. }
  367. /* Clear all IRQ bitmasks at wakeupGen level */
  368. for (i = 0; i < irq_banks; i++) {
  369. wakeupgen_writel(0, i, CPU0_ID);
  370. if (!soc_is_am43xx())
  371. wakeupgen_writel(0, i, CPU1_ID);
  372. }
  373. /*
  374. * Override GIC architecture specific functions to add
  375. * OMAP WakeupGen interrupt controller along with GIC
  376. */
  377. gic_arch_extn.irq_mask = wakeupgen_mask;
  378. gic_arch_extn.irq_unmask = wakeupgen_unmask;
  379. gic_arch_extn.flags = IRQCHIP_MASK_ON_SUSPEND | IRQCHIP_SKIP_SET_WAKE;
  380. /*
  381. * FIXME: Add support to set_smp_affinity() once the core
  382. * GIC code has necessary hooks in place.
  383. */
  384. /* Associate all the IRQs to boot CPU like GIC init does. */
  385. for (i = 0; i < max_irqs; i++)
  386. irq_target_cpu[i] = boot_cpu;
  387. /*
  388. * Enables OMAP5 ES2 PM Mode using ES2_PM_MODE in AMBA_IF_MODE
  389. * 0x0: ES1 behavior, CPU cores would enter and exit OFF mode together.
  390. * 0x1: ES2 behavior, CPU cores are allowed to enter/exit OFF mode
  391. * independently.
  392. * This needs to be set one time thanks to always ON domain.
  393. *
  394. * We do not support ES1 behavior anymore. OMAP5 is assumed to be
  395. * ES2.0, and the same is applicable for DRA7.
  396. */
  397. if (soc_is_omap54xx() || soc_is_dra7xx()) {
  398. val = __raw_readl(wakeupgen_base + OMAP_AMBA_IF_MODE);
  399. val |= BIT(5);
  400. omap_smc1(OMAP5_MON_AMBA_IF_INDEX, val);
  401. }
  402. irq_hotplug_init();
  403. irq_pm_init();
  404. return 0;
  405. }