blob: 7dcbe1736f7e4a40d0815f08c7541f88d9ae7fa4 [file] [log] [blame]
Santosh Shilimkarfcf6efa2010-06-16 22:19:47 +05301/*
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
19#include <linux/kernel.h>
20#include <linux/init.h>
21#include <linux/io.h>
22#include <linux/irq.h>
Marc Zyngier0cc09e82015-10-16 15:21:10 +010023#include <linux/irqchip.h>
Marc Zyngier7136d452015-03-11 15:43:49 +000024#include <linux/irqdomain.h>
25#include <linux/of_address.h>
Santosh Shilimkarfcf6efa2010-06-16 22:19:47 +053026#include <linux/platform_device.h>
27#include <linux/cpu.h>
Santosh Shilimkar0f3cf2e2010-06-16 23:29:31 +053028#include <linux/notifier.h>
29#include <linux/cpu_pm.h>
Santosh Shilimkarfcf6efa2010-06-16 22:19:47 +053030
Tony Lindgren732231a2012-09-20 11:41:16 -070031#include "omap-wakeupgen.h"
Tony Lindgrenc1db9d72012-09-20 11:41:14 -070032#include "omap-secure.h"
Santosh Shilimkar0f3cf2e2010-06-16 23:29:31 +053033
Tony Lindgrendbc04162012-08-31 10:59:07 -070034#include "soc.h"
Santosh Shilimkar0f3cf2e2010-06-16 23:29:31 +053035#include "omap4-sar-layout.h"
36#include "common.h"
Rajendra Nayak6099dd32013-05-27 15:46:44 +053037#include "pm.h"
Santosh Shilimkarfcf6efa2010-06-16 22:19:47 +053038
Afzal Mohammed6246cd02013-10-09 12:42:33 +053039#define AM43XX_NR_REG_BANKS 7
40#define AM43XX_IRQS 224
41#define MAX_NR_REG_BANKS AM43XX_NR_REG_BANKS
42#define MAX_IRQS AM43XX_IRQS
43#define DEFAULT_NR_REG_BANKS 5
44#define DEFAULT_IRQS 160
Santosh Shilimkarfcf6efa2010-06-16 22:19:47 +053045#define WKG_MASK_ALL 0x00000000
46#define WKG_UNMASK_ALL 0xffffffff
47#define CPU_ENA_OFFSET 0x400
48#define CPU0_ID 0x0
49#define CPU1_ID 0x1
Santosh Shilimkar247c4452012-05-09 20:38:35 +053050#define OMAP4_NR_BANKS 4
51#define OMAP4_NR_IRQS 128
Santosh Shilimkarfcf6efa2010-06-16 22:19:47 +053052
Tony Lindgrend0243692019-01-15 10:09:38 -080053#define SYS_NIRQ1_EXT_SYS_IRQ_1 7
54#define SYS_NIRQ2_EXT_SYS_IRQ_2 119
55
Santosh Shilimkarfcf6efa2010-06-16 22:19:47 +053056static void __iomem *wakeupgen_base;
Santosh Shilimkar0f3cf2e2010-06-16 23:29:31 +053057static void __iomem *sar_base;
Thomas Gleixneraecb9e12012-12-07 16:49:47 +020058static DEFINE_RAW_SPINLOCK(wakeupgen_lock);
Tony Lindgrene534e872012-09-04 17:22:45 -070059static unsigned int irq_target_cpu[MAX_IRQS];
Afzal Mohammed6246cd02013-10-09 12:42:33 +053060static unsigned int irq_banks = DEFAULT_NR_REG_BANKS;
61static unsigned int max_irqs = DEFAULT_IRQS;
Santosh Shilimkar247c4452012-05-09 20:38:35 +053062static unsigned int omap_secure_apis;
Santosh Shilimkarfcf6efa2010-06-16 22:19:47 +053063
Dave Gerlache1a9e612017-03-28 20:57:54 -050064#ifdef CONFIG_CPU_PM
65static unsigned int wakeupgen_context[MAX_NR_REG_BANKS];
66#endif
67
68struct omap_wakeupgen_ops {
69 void (*save_context)(void);
70 void (*restore_context)(void);
71};
72
73static struct omap_wakeupgen_ops *wakeupgen_ops;
74
Santosh Shilimkarfcf6efa2010-06-16 22:19:47 +053075/*
76 * Static helper functions.
77 */
78static inline u32 wakeupgen_readl(u8 idx, u32 cpu)
79{
Victor Kamenskyedfaf052014-04-15 20:37:46 +030080 return readl_relaxed(wakeupgen_base + OMAP_WKG_ENB_A_0 +
Santosh Shilimkarfcf6efa2010-06-16 22:19:47 +053081 (cpu * CPU_ENA_OFFSET) + (idx * 4));
82}
83
84static inline void wakeupgen_writel(u32 val, u8 idx, u32 cpu)
85{
Victor Kamenskyedfaf052014-04-15 20:37:46 +030086 writel_relaxed(val, wakeupgen_base + OMAP_WKG_ENB_A_0 +
Santosh Shilimkarfcf6efa2010-06-16 22:19:47 +053087 (cpu * CPU_ENA_OFFSET) + (idx * 4));
88}
89
Santosh Shilimkar0f3cf2e2010-06-16 23:29:31 +053090static inline void sar_writel(u32 val, u32 offset, u8 idx)
91{
Victor Kamenskyedfaf052014-04-15 20:37:46 +030092 writel_relaxed(val, sar_base + offset + (idx * 4));
Santosh Shilimkar0f3cf2e2010-06-16 23:29:31 +053093}
94
Santosh Shilimkarfcf6efa2010-06-16 22:19:47 +053095static inline int _wakeupgen_get_irq_info(u32 irq, u32 *bit_posn, u8 *reg_index)
96{
Santosh Shilimkarfcf6efa2010-06-16 22:19:47 +053097 /*
98 * Each WakeupGen register controls 32 interrupt.
99 * i.e. 1 bit per SPI IRQ
100 */
Marc Zyngier7136d452015-03-11 15:43:49 +0000101 *reg_index = irq >> 5;
102 *bit_posn = irq %= 32;
Santosh Shilimkarfcf6efa2010-06-16 22:19:47 +0530103
104 return 0;
105}
106
107static void _wakeupgen_clear(unsigned int irq, unsigned int cpu)
108{
109 u32 val, bit_number;
110 u8 i;
111
112 if (_wakeupgen_get_irq_info(irq, &bit_number, &i))
113 return;
114
115 val = wakeupgen_readl(i, cpu);
116 val &= ~BIT(bit_number);
117 wakeupgen_writel(val, i, cpu);
118}
119
120static void _wakeupgen_set(unsigned int irq, unsigned int cpu)
121{
122 u32 val, bit_number;
123 u8 i;
124
125 if (_wakeupgen_get_irq_info(irq, &bit_number, &i))
126 return;
127
128 val = wakeupgen_readl(i, cpu);
129 val |= BIT(bit_number);
130 wakeupgen_writel(val, i, cpu);
131}
132
Santosh Shilimkarfcf6efa2010-06-16 22:19:47 +0530133/*
134 * Architecture specific Mask extension
135 */
136static void wakeupgen_mask(struct irq_data *d)
137{
138 unsigned long flags;
139
Thomas Gleixneraecb9e12012-12-07 16:49:47 +0200140 raw_spin_lock_irqsave(&wakeupgen_lock, flags);
Sricharan R1cc4b1a92013-12-03 15:57:24 +0530141 _wakeupgen_clear(d->hwirq, irq_target_cpu[d->hwirq]);
Thomas Gleixneraecb9e12012-12-07 16:49:47 +0200142 raw_spin_unlock_irqrestore(&wakeupgen_lock, flags);
Marc Zyngier7136d452015-03-11 15:43:49 +0000143 irq_chip_mask_parent(d);
Santosh Shilimkarfcf6efa2010-06-16 22:19:47 +0530144}
145
146/*
147 * Architecture specific Unmask extension
148 */
149static void wakeupgen_unmask(struct irq_data *d)
150{
151 unsigned long flags;
152
Thomas Gleixneraecb9e12012-12-07 16:49:47 +0200153 raw_spin_lock_irqsave(&wakeupgen_lock, flags);
Sricharan R1cc4b1a92013-12-03 15:57:24 +0530154 _wakeupgen_set(d->hwirq, irq_target_cpu[d->hwirq]);
Thomas Gleixneraecb9e12012-12-07 16:49:47 +0200155 raw_spin_unlock_irqrestore(&wakeupgen_lock, flags);
Marc Zyngier7136d452015-03-11 15:43:49 +0000156 irq_chip_unmask_parent(d);
Santosh Shilimkarfcf6efa2010-06-16 22:19:47 +0530157}
158
Tony Lindgrend0243692019-01-15 10:09:38 -0800159/*
160 * The sys_nirq pins bypass peripheral modules and are wired directly
161 * to MPUSS wakeupgen. They get automatically inverted for GIC.
162 */
163static int wakeupgen_irq_set_type(struct irq_data *d, unsigned int type)
164{
165 bool inverted = false;
166
167 switch (type) {
168 case IRQ_TYPE_LEVEL_LOW:
169 type &= ~IRQ_TYPE_LEVEL_MASK;
170 type |= IRQ_TYPE_LEVEL_HIGH;
171 inverted = true;
172 break;
173 case IRQ_TYPE_EDGE_FALLING:
174 type &= ~IRQ_TYPE_EDGE_BOTH;
175 type |= IRQ_TYPE_EDGE_RISING;
176 inverted = true;
177 break;
178 default:
179 break;
180 }
181
182 if (inverted && d->hwirq != SYS_NIRQ1_EXT_SYS_IRQ_1 &&
183 d->hwirq != SYS_NIRQ2_EXT_SYS_IRQ_2)
184 pr_warn("wakeupgen: irq%li polarity inverted in dts\n",
185 d->hwirq);
186
187 return irq_chip_set_type_parent(d, type);
188}
189
Kevin Hilmanbb1dbe72012-03-06 12:00:25 -0800190#ifdef CONFIG_HOTPLUG_CPU
Santosh Shilimkar247c4452012-05-09 20:38:35 +0530191static DEFINE_PER_CPU(u32 [MAX_NR_REG_BANKS], irqmasks);
Kevin Hilmanbb1dbe72012-03-06 12:00:25 -0800192
193static void _wakeupgen_save_masks(unsigned int cpu)
194{
195 u8 i;
196
Santosh Shilimkar247c4452012-05-09 20:38:35 +0530197 for (i = 0; i < irq_banks; i++)
Kevin Hilmanbb1dbe72012-03-06 12:00:25 -0800198 per_cpu(irqmasks, cpu)[i] = wakeupgen_readl(i, cpu);
199}
200
201static void _wakeupgen_restore_masks(unsigned int cpu)
202{
203 u8 i;
204
Santosh Shilimkar247c4452012-05-09 20:38:35 +0530205 for (i = 0; i < irq_banks; i++)
Kevin Hilmanbb1dbe72012-03-06 12:00:25 -0800206 wakeupgen_writel(per_cpu(irqmasks, cpu)[i], i, cpu);
207}
208
209static void _wakeupgen_set_all(unsigned int cpu, unsigned int reg)
210{
211 u8 i;
212
Santosh Shilimkar247c4452012-05-09 20:38:35 +0530213 for (i = 0; i < irq_banks; i++)
Kevin Hilmanbb1dbe72012-03-06 12:00:25 -0800214 wakeupgen_writel(reg, i, cpu);
215}
216
Santosh Shilimkarfcf6efa2010-06-16 22:19:47 +0530217/*
218 * Mask or unmask all interrupts on given CPU.
219 * 0 = Mask all interrupts on the 'cpu'
220 * 1 = Unmask all interrupts on the 'cpu'
221 * Ensure that the initial mask is maintained. This is faster than
222 * iterating through GIC registers to arrive at the correct masks.
223 */
224static void wakeupgen_irqmask_all(unsigned int cpu, unsigned int set)
225{
226 unsigned long flags;
227
Thomas Gleixneraecb9e12012-12-07 16:49:47 +0200228 raw_spin_lock_irqsave(&wakeupgen_lock, flags);
Santosh Shilimkarfcf6efa2010-06-16 22:19:47 +0530229 if (set) {
230 _wakeupgen_save_masks(cpu);
231 _wakeupgen_set_all(cpu, WKG_MASK_ALL);
232 } else {
233 _wakeupgen_set_all(cpu, WKG_UNMASK_ALL);
234 _wakeupgen_restore_masks(cpu);
235 }
Thomas Gleixneraecb9e12012-12-07 16:49:47 +0200236 raw_spin_unlock_irqrestore(&wakeupgen_lock, flags);
Santosh Shilimkarfcf6efa2010-06-16 22:19:47 +0530237}
Kevin Hilmanbb1dbe72012-03-06 12:00:25 -0800238#endif
Santosh Shilimkarfcf6efa2010-06-16 22:19:47 +0530239
Santosh Shilimkar0f3cf2e2010-06-16 23:29:31 +0530240#ifdef CONFIG_CPU_PM
Santosh Shilimkar247c4452012-05-09 20:38:35 +0530241static inline void omap4_irq_save_context(void)
Santosh Shilimkar0f3cf2e2010-06-16 23:29:31 +0530242{
243 u32 i, val;
244
245 if (omap_rev() == OMAP4430_REV_ES1_0)
246 return;
247
Santosh Shilimkar247c4452012-05-09 20:38:35 +0530248 for (i = 0; i < irq_banks; i++) {
Santosh Shilimkar0f3cf2e2010-06-16 23:29:31 +0530249 /* Save the CPUx interrupt mask for IRQ 0 to 127 */
250 val = wakeupgen_readl(i, 0);
251 sar_writel(val, WAKEUPGENENB_OFFSET_CPU0, i);
252 val = wakeupgen_readl(i, 1);
253 sar_writel(val, WAKEUPGENENB_OFFSET_CPU1, i);
254
255 /*
256 * Disable the secure interrupts for CPUx. The restore
257 * code blindly restores secure and non-secure interrupt
258 * masks from SAR RAM. Secure interrupts are not suppose
259 * to be enabled from HLOS. So overwrite the SAR location
260 * so that the secure interrupt remains disabled.
261 */
262 sar_writel(0x0, WAKEUPGENENB_SECURE_OFFSET_CPU0, i);
263 sar_writel(0x0, WAKEUPGENENB_SECURE_OFFSET_CPU1, i);
264 }
265
266 /* Save AuxBoot* registers */
Victor Kamenskyedfaf052014-04-15 20:37:46 +0300267 val = readl_relaxed(wakeupgen_base + OMAP_AUX_CORE_BOOT_0);
268 writel_relaxed(val, sar_base + AUXCOREBOOT0_OFFSET);
269 val = readl_relaxed(wakeupgen_base + OMAP_AUX_CORE_BOOT_1);
270 writel_relaxed(val, sar_base + AUXCOREBOOT1_OFFSET);
Santosh Shilimkar0f3cf2e2010-06-16 23:29:31 +0530271
272 /* Save SyncReq generation logic */
Victor Kamenskyedfaf052014-04-15 20:37:46 +0300273 val = readl_relaxed(wakeupgen_base + OMAP_PTMSYNCREQ_MASK);
274 writel_relaxed(val, sar_base + PTMSYNCREQ_MASK_OFFSET);
275 val = readl_relaxed(wakeupgen_base + OMAP_PTMSYNCREQ_EN);
276 writel_relaxed(val, sar_base + PTMSYNCREQ_EN_OFFSET);
Santosh Shilimkar0f3cf2e2010-06-16 23:29:31 +0530277
278 /* Set the Backup Bit Mask status */
Victor Kamenskyedfaf052014-04-15 20:37:46 +0300279 val = readl_relaxed(sar_base + SAR_BACKUP_STATUS_OFFSET);
Santosh Shilimkar0f3cf2e2010-06-16 23:29:31 +0530280 val |= SAR_BACKUP_STATUS_WAKEUPGEN;
Victor Kamenskyedfaf052014-04-15 20:37:46 +0300281 writel_relaxed(val, sar_base + SAR_BACKUP_STATUS_OFFSET);
Santosh Shilimkar247c4452012-05-09 20:38:35 +0530282
283}
284
285static inline void omap5_irq_save_context(void)
286{
287 u32 i, val;
288
289 for (i = 0; i < irq_banks; i++) {
290 /* Save the CPUx interrupt mask for IRQ 0 to 159 */
291 val = wakeupgen_readl(i, 0);
292 sar_writel(val, OMAP5_WAKEUPGENENB_OFFSET_CPU0, i);
293 val = wakeupgen_readl(i, 1);
294 sar_writel(val, OMAP5_WAKEUPGENENB_OFFSET_CPU1, i);
295 sar_writel(0x0, OMAP5_WAKEUPGENENB_SECURE_OFFSET_CPU0, i);
296 sar_writel(0x0, OMAP5_WAKEUPGENENB_SECURE_OFFSET_CPU1, i);
297 }
298
299 /* Save AuxBoot* registers */
Victor Kamenskyedfaf052014-04-15 20:37:46 +0300300 val = readl_relaxed(wakeupgen_base + OMAP_AUX_CORE_BOOT_0);
301 writel_relaxed(val, sar_base + OMAP5_AUXCOREBOOT0_OFFSET);
302 val = readl_relaxed(wakeupgen_base + OMAP_AUX_CORE_BOOT_0);
303 writel_relaxed(val, sar_base + OMAP5_AUXCOREBOOT1_OFFSET);
Santosh Shilimkar247c4452012-05-09 20:38:35 +0530304
305 /* Set the Backup Bit Mask status */
Victor Kamenskyedfaf052014-04-15 20:37:46 +0300306 val = readl_relaxed(sar_base + OMAP5_SAR_BACKUP_STATUS_OFFSET);
Santosh Shilimkar247c4452012-05-09 20:38:35 +0530307 val |= SAR_BACKUP_STATUS_WAKEUPGEN;
Victor Kamenskyedfaf052014-04-15 20:37:46 +0300308 writel_relaxed(val, sar_base + OMAP5_SAR_BACKUP_STATUS_OFFSET);
Santosh Shilimkar247c4452012-05-09 20:38:35 +0530309
310}
311
Dave Gerlache1a9e612017-03-28 20:57:54 -0500312static inline void am43xx_irq_save_context(void)
313{
314 u32 i;
315
316 for (i = 0; i < irq_banks; i++) {
317 wakeupgen_context[i] = wakeupgen_readl(i, 0);
318 wakeupgen_writel(0, i, CPU0_ID);
319 }
320}
321
Santosh Shilimkar247c4452012-05-09 20:38:35 +0530322/*
323 * Save WakeupGen interrupt context in SAR BANK3. Restore is done by
324 * ROM code. WakeupGen IP is integrated along with GIC to manage the
325 * interrupt wakeups from CPU low power states. It manages
326 * masking/unmasking of Shared peripheral interrupts(SPI). So the
327 * interrupt enable/disable control should be in sync and consistent
328 * at WakeupGen and GIC so that interrupts are not lost.
329 */
330static void irq_save_context(void)
331{
Nishanth Menonc783e6f2016-04-01 17:53:05 -0500332 /* DRA7 has no SAR to save */
333 if (soc_is_dra7xx())
334 return;
335
Dave Gerlache1a9e612017-03-28 20:57:54 -0500336 if (wakeupgen_ops && wakeupgen_ops->save_context)
337 wakeupgen_ops->save_context();
Santosh Shilimkar0f3cf2e2010-06-16 23:29:31 +0530338}
339
340/*
341 * Clear WakeupGen SAR backup status.
342 */
Paul Walmsley8c3d4532012-04-13 06:34:26 -0600343static void irq_sar_clear(void)
Santosh Shilimkar0f3cf2e2010-06-16 23:29:31 +0530344{
345 u32 val;
Santosh Shilimkar247c4452012-05-09 20:38:35 +0530346 u32 offset = SAR_BACKUP_STATUS_OFFSET;
Nishanth Menonc783e6f2016-04-01 17:53:05 -0500347 /* DRA7 has no SAR to save */
348 if (soc_is_dra7xx())
349 return;
Santosh Shilimkar247c4452012-05-09 20:38:35 +0530350
351 if (soc_is_omap54xx())
352 offset = OMAP5_SAR_BACKUP_STATUS_OFFSET;
353
Victor Kamenskyedfaf052014-04-15 20:37:46 +0300354 val = readl_relaxed(sar_base + offset);
Santosh Shilimkar0f3cf2e2010-06-16 23:29:31 +0530355 val &= ~SAR_BACKUP_STATUS_WAKEUPGEN;
Victor Kamenskyedfaf052014-04-15 20:37:46 +0300356 writel_relaxed(val, sar_base + offset);
Santosh Shilimkar0f3cf2e2010-06-16 23:29:31 +0530357}
358
Dave Gerlache1a9e612017-03-28 20:57:54 -0500359static void am43xx_irq_restore_context(void)
360{
361 u32 i;
362
363 for (i = 0; i < irq_banks; i++)
364 wakeupgen_writel(wakeupgen_context[i], i, CPU0_ID);
365}
366
367static void irq_restore_context(void)
368{
369 if (wakeupgen_ops && wakeupgen_ops->restore_context)
370 wakeupgen_ops->restore_context();
371}
372
Santosh Shilimkar0f3cf2e2010-06-16 23:29:31 +0530373/*
374 * Save GIC and Wakeupgen interrupt context using secure API
375 * for HS/EMU devices.
376 */
377static void irq_save_secure_context(void)
378{
379 u32 ret;
380 ret = omap_secure_dispatcher(OMAP4_HAL_SAVEGIC_INDEX,
381 FLAG_START_CRITICAL,
382 0, 0, 0, 0, 0);
383 if (ret != API_HAL_RET_VALUE_OK)
384 pr_err("GIC and Wakeupgen context save failed\n");
385}
Dave Gerlache1a9e612017-03-28 20:57:54 -0500386
387/* Define ops for context save and restore for each SoC */
388static struct omap_wakeupgen_ops omap4_wakeupgen_ops = {
389 .save_context = omap4_irq_save_context,
390 .restore_context = irq_sar_clear,
391};
392
393static struct omap_wakeupgen_ops omap5_wakeupgen_ops = {
394 .save_context = omap5_irq_save_context,
395 .restore_context = irq_sar_clear,
396};
397
398static struct omap_wakeupgen_ops am43xx_wakeupgen_ops = {
399 .save_context = am43xx_irq_save_context,
400 .restore_context = am43xx_irq_restore_context,
401};
402#else
403static struct omap_wakeupgen_ops omap4_wakeupgen_ops = {};
404static struct omap_wakeupgen_ops omap5_wakeupgen_ops = {};
405static struct omap_wakeupgen_ops am43xx_wakeupgen_ops = {};
Santosh Shilimkar0f3cf2e2010-06-16 23:29:31 +0530406#endif
407
Santosh Shilimkarb5b4f282010-06-16 22:19:48 +0530408#ifdef CONFIG_HOTPLUG_CPU
Sebastian Andrzej Siewiora4fa9cc2016-09-06 19:04:39 +0200409static int omap_wakeupgen_cpu_online(unsigned int cpu)
Santosh Shilimkarb5b4f282010-06-16 22:19:48 +0530410{
Sebastian Andrzej Siewiora4fa9cc2016-09-06 19:04:39 +0200411 wakeupgen_irqmask_all(cpu, 0);
412 return 0;
Santosh Shilimkarb5b4f282010-06-16 22:19:48 +0530413}
414
Sebastian Andrzej Siewiora4fa9cc2016-09-06 19:04:39 +0200415static int omap_wakeupgen_cpu_dead(unsigned int cpu)
416{
417 wakeupgen_irqmask_all(cpu, 1);
418 return 0;
419}
Santosh Shilimkarb5b4f282010-06-16 22:19:48 +0530420
421static void __init irq_hotplug_init(void)
422{
Sebastian Andrzej Siewiora4fa9cc2016-09-06 19:04:39 +0200423 cpuhp_setup_state_nocalls(CPUHP_AP_ONLINE_DYN, "arm/omap-wake:online",
424 omap_wakeupgen_cpu_online, NULL);
425 cpuhp_setup_state_nocalls(CPUHP_ARM_OMAP_WAKE_DEAD,
426 "arm/omap-wake:dead", NULL,
427 omap_wakeupgen_cpu_dead);
Santosh Shilimkarb5b4f282010-06-16 22:19:48 +0530428}
429#else
430static void __init irq_hotplug_init(void)
431{}
432#endif
433
Santosh Shilimkar0f3cf2e2010-06-16 23:29:31 +0530434#ifdef CONFIG_CPU_PM
435static int irq_notifier(struct notifier_block *self, unsigned long cmd, void *v)
436{
437 switch (cmd) {
438 case CPU_CLUSTER_PM_ENTER:
Andrew F. Davisf8033672019-04-09 08:05:16 -0700439 if (omap_type() == OMAP2_DEVICE_TYPE_GP || soc_is_am43xx())
Santosh Shilimkar0f3cf2e2010-06-16 23:29:31 +0530440 irq_save_context();
441 else
442 irq_save_secure_context();
443 break;
444 case CPU_CLUSTER_PM_EXIT:
Andrew F. Davisf8033672019-04-09 08:05:16 -0700445 if (omap_type() == OMAP2_DEVICE_TYPE_GP || soc_is_am43xx())
Dave Gerlache1a9e612017-03-28 20:57:54 -0500446 irq_restore_context();
Santosh Shilimkar0f3cf2e2010-06-16 23:29:31 +0530447 break;
448 }
449 return NOTIFY_OK;
450}
451
452static struct notifier_block irq_notifier_block = {
453 .notifier_call = irq_notifier,
454};
455
456static void __init irq_pm_init(void)
457{
Santosh Shilimkar247c4452012-05-09 20:38:35 +0530458 /* FIXME: Remove this when MPU OSWR support is added */
Rajendra Nayak6099dd32013-05-27 15:46:44 +0530459 if (!IS_PM44XX_ERRATUM(PM_OMAP4_CPU_OSWR_DISABLE))
Santosh Shilimkar247c4452012-05-09 20:38:35 +0530460 cpu_pm_register_notifier(&irq_notifier_block);
Santosh Shilimkar0f3cf2e2010-06-16 23:29:31 +0530461}
462#else
463static void __init irq_pm_init(void)
464{}
465#endif
466
Santosh Shilimkar247c4452012-05-09 20:38:35 +0530467void __iomem *omap_get_wakeupgen_base(void)
468{
469 return wakeupgen_base;
470}
471
472int omap_secure_apis_support(void)
473{
474 return omap_secure_apis;
475}
476
Marc Zyngier7136d452015-03-11 15:43:49 +0000477static struct irq_chip wakeupgen_chip = {
478 .name = "WUGEN",
479 .irq_eoi = irq_chip_eoi_parent,
480 .irq_mask = wakeupgen_mask,
481 .irq_unmask = wakeupgen_unmask,
482 .irq_retrigger = irq_chip_retrigger_hierarchy,
Tony Lindgrend0243692019-01-15 10:09:38 -0800483 .irq_set_type = wakeupgen_irq_set_type,
Marc Zyngier7136d452015-03-11 15:43:49 +0000484 .flags = IRQCHIP_SKIP_SET_WAKE | IRQCHIP_MASK_ON_SUSPEND,
485#ifdef CONFIG_SMP
486 .irq_set_affinity = irq_chip_set_affinity_parent,
487#endif
488};
489
Marc Zyngierf833f572015-10-13 12:51:33 +0100490static int wakeupgen_domain_translate(struct irq_domain *d,
491 struct irq_fwspec *fwspec,
492 unsigned long *hwirq,
493 unsigned int *type)
Marc Zyngier7136d452015-03-11 15:43:49 +0000494{
Marc Zyngierf833f572015-10-13 12:51:33 +0100495 if (is_of_node(fwspec->fwnode)) {
496 if (fwspec->param_count != 3)
497 return -EINVAL;
Marc Zyngier7136d452015-03-11 15:43:49 +0000498
Marc Zyngierf833f572015-10-13 12:51:33 +0100499 /* No PPI should point to this domain */
500 if (fwspec->param[0] != 0)
501 return -EINVAL;
502
503 *hwirq = fwspec->param[1];
504 *type = fwspec->param[2];
505 return 0;
506 }
507
508 return -EINVAL;
Marc Zyngier7136d452015-03-11 15:43:49 +0000509}
510
511static int wakeupgen_domain_alloc(struct irq_domain *domain,
512 unsigned int virq,
513 unsigned int nr_irqs, void *data)
514{
Marc Zyngierf833f572015-10-13 12:51:33 +0100515 struct irq_fwspec *fwspec = data;
516 struct irq_fwspec parent_fwspec;
Marc Zyngier7136d452015-03-11 15:43:49 +0000517 irq_hw_number_t hwirq;
518 int i;
519
Marc Zyngierf833f572015-10-13 12:51:33 +0100520 if (fwspec->param_count != 3)
Marc Zyngier7136d452015-03-11 15:43:49 +0000521 return -EINVAL; /* Not GIC compliant */
Marc Zyngierf833f572015-10-13 12:51:33 +0100522 if (fwspec->param[0] != 0)
Marc Zyngier7136d452015-03-11 15:43:49 +0000523 return -EINVAL; /* No PPI should point to this domain */
524
Marc Zyngierf833f572015-10-13 12:51:33 +0100525 hwirq = fwspec->param[1];
Marc Zyngier7136d452015-03-11 15:43:49 +0000526 if (hwirq >= MAX_IRQS)
527 return -EINVAL; /* Can't deal with this */
528
529 for (i = 0; i < nr_irqs; i++)
530 irq_domain_set_hwirq_and_chip(domain, virq + i, hwirq + i,
531 &wakeupgen_chip, NULL);
532
Marc Zyngierf833f572015-10-13 12:51:33 +0100533 parent_fwspec = *fwspec;
534 parent_fwspec.fwnode = domain->parent->fwnode;
535 return irq_domain_alloc_irqs_parent(domain, virq, nr_irqs,
536 &parent_fwspec);
Marc Zyngier7136d452015-03-11 15:43:49 +0000537}
538
Krzysztof Kozlowski82979552015-04-27 21:49:44 +0900539static const struct irq_domain_ops wakeupgen_domain_ops = {
Marc Zyngierf833f572015-10-13 12:51:33 +0100540 .translate = wakeupgen_domain_translate,
541 .alloc = wakeupgen_domain_alloc,
542 .free = irq_domain_free_irqs_common,
Marc Zyngier7136d452015-03-11 15:43:49 +0000543};
544
Santosh Shilimkarfcf6efa2010-06-16 22:19:47 +0530545/*
546 * Initialise the wakeupgen module.
547 */
Marc Zyngier7136d452015-03-11 15:43:49 +0000548static int __init wakeupgen_init(struct device_node *node,
549 struct device_node *parent)
Santosh Shilimkarfcf6efa2010-06-16 22:19:47 +0530550{
Marc Zyngier7136d452015-03-11 15:43:49 +0000551 struct irq_domain *parent_domain, *domain;
Santosh Shilimkarfcf6efa2010-06-16 22:19:47 +0530552 int i;
553 unsigned int boot_cpu = smp_processor_id();
Santosh Shilimkar4664d4d2013-02-08 17:07:31 +0530554 u32 val;
Santosh Shilimkarfcf6efa2010-06-16 22:19:47 +0530555
Marc Zyngier7136d452015-03-11 15:43:49 +0000556 if (!parent) {
Rob Herringa8e65e02017-07-21 14:28:32 -0500557 pr_err("%pOF: no parent, giving up\n", node);
Marc Zyngier7136d452015-03-11 15:43:49 +0000558 return -ENODEV;
559 }
560
561 parent_domain = irq_find_host(parent);
562 if (!parent_domain) {
Rob Herringa8e65e02017-07-21 14:28:32 -0500563 pr_err("%pOF: unable to obtain parent domain\n", node);
Marc Zyngier7136d452015-03-11 15:43:49 +0000564 return -ENXIO;
565 }
Santosh Shilimkarfcf6efa2010-06-16 22:19:47 +0530566 /* Not supported on OMAP4 ES1.0 silicon */
567 if (omap_rev() == OMAP4430_REV_ES1_0) {
568 WARN(1, "WakeupGen: Not supported on OMAP4430 ES1.0\n");
569 return -EPERM;
570 }
571
572 /* Static mapping, never released */
Marc Zyngier7136d452015-03-11 15:43:49 +0000573 wakeupgen_base = of_iomap(node, 0);
Santosh Shilimkarfcf6efa2010-06-16 22:19:47 +0530574 if (WARN_ON(!wakeupgen_base))
575 return -ENOMEM;
576
Santosh Shilimkar247c4452012-05-09 20:38:35 +0530577 if (cpu_is_omap44xx()) {
578 irq_banks = OMAP4_NR_BANKS;
579 max_irqs = OMAP4_NR_IRQS;
580 omap_secure_apis = 1;
Dave Gerlache1a9e612017-03-28 20:57:54 -0500581 wakeupgen_ops = &omap4_wakeupgen_ops;
582 } else if (soc_is_omap54xx()) {
583 wakeupgen_ops = &omap5_wakeupgen_ops;
Afzal Mohammed6246cd02013-10-09 12:42:33 +0530584 } else if (soc_is_am43xx()) {
585 irq_banks = AM43XX_NR_REG_BANKS;
586 max_irqs = AM43XX_IRQS;
Dave Gerlache1a9e612017-03-28 20:57:54 -0500587 wakeupgen_ops = &am43xx_wakeupgen_ops;
Santosh Shilimkar247c4452012-05-09 20:38:35 +0530588 }
589
Marc Zyngier7136d452015-03-11 15:43:49 +0000590 domain = irq_domain_add_hierarchy(parent_domain, 0, max_irqs,
591 node, &wakeupgen_domain_ops,
592 NULL);
593 if (!domain) {
594 iounmap(wakeupgen_base);
595 return -ENOMEM;
596 }
597
Santosh Shilimkarfcf6efa2010-06-16 22:19:47 +0530598 /* Clear all IRQ bitmasks at wakeupGen level */
Santosh Shilimkar247c4452012-05-09 20:38:35 +0530599 for (i = 0; i < irq_banks; i++) {
Santosh Shilimkarfcf6efa2010-06-16 22:19:47 +0530600 wakeupgen_writel(0, i, CPU0_ID);
Afzal Mohammed6246cd02013-10-09 12:42:33 +0530601 if (!soc_is_am43xx())
602 wakeupgen_writel(0, i, CPU1_ID);
Santosh Shilimkarfcf6efa2010-06-16 22:19:47 +0530603 }
604
605 /*
Santosh Shilimkarfcf6efa2010-06-16 22:19:47 +0530606 * FIXME: Add support to set_smp_affinity() once the core
607 * GIC code has necessary hooks in place.
608 */
609
610 /* Associate all the IRQs to boot CPU like GIC init does. */
Santosh Shilimkar247c4452012-05-09 20:38:35 +0530611 for (i = 0; i < max_irqs; i++)
Santosh Shilimkarfcf6efa2010-06-16 22:19:47 +0530612 irq_target_cpu[i] = boot_cpu;
613
Santosh Shilimkar4664d4d2013-02-08 17:07:31 +0530614 /*
615 * Enables OMAP5 ES2 PM Mode using ES2_PM_MODE in AMBA_IF_MODE
616 * 0x0: ES1 behavior, CPU cores would enter and exit OFF mode together.
617 * 0x1: ES2 behavior, CPU cores are allowed to enter/exit OFF mode
618 * independently.
619 * This needs to be set one time thanks to always ON domain.
620 *
621 * We do not support ES1 behavior anymore. OMAP5 is assumed to be
622 * ES2.0, and the same is applicable for DRA7.
623 */
624 if (soc_is_omap54xx() || soc_is_dra7xx()) {
625 val = __raw_readl(wakeupgen_base + OMAP_AMBA_IF_MODE);
626 val |= BIT(5);
627 omap_smc1(OMAP5_MON_AMBA_IF_INDEX, val);
628 }
629
Santosh Shilimkarb5b4f282010-06-16 22:19:48 +0530630 irq_hotplug_init();
Santosh Shilimkar0f3cf2e2010-06-16 23:29:31 +0530631 irq_pm_init();
Santosh Shilimkarb5b4f282010-06-16 22:19:48 +0530632
Tony Lindgrenfe27f162018-02-09 09:35:56 -0800633 sar_base = omap4_get_sar_ram_base();
634
Santosh Shilimkarfcf6efa2010-06-16 22:19:47 +0530635 return 0;
636}
Marc Zyngier0cc09e82015-10-16 15:21:10 +0100637IRQCHIP_DECLARE(ti_wakeupgen, "ti,omap4-wugen-mpu", wakeupgen_init);