omap-wakeupgen.c 14 KB

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