omap-wakeupgen.c 14 KB

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