Skip to contentSkip to conversation

soc/tegra: pmc: Fix dual edge triggered wakes

PATCH Superseded Petlozu Pravareshwar 2 participants 3 messages Sep 6, 2022 – Sep 27, 2022 Tegra architecture support

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 soc/tegra: pmc: Fix dual edge triggered wakes

When a wake event is defined to be triggered on both positive and negative edge of the input wake signal, it is crucial to know the current state of the signal when going into suspend. The intended way to obtain the current state of the wake signals is to read the WAKE_AOWAKE_SW_STATUS register, which should contains the raw state of the wake signals.

However, this register is edge triggered, an edge will not be generated for signals that are already asserted prior to the assertion of WAKE_LATCH_SW.

To workaround this, change the polarity of the wake level from '0' to '1' while latching the signals, as this will generate an edge for signals that are set to '1'.

---
 drivers/soc/tegra/pmc.c | 141 ++++++++++++++++++++++++++++++++++++++--
 1 file changed, 136 insertions(+), 5 deletions(-)
drivers/soc/tegra/pmc.c +136 -5
index 495d16a4732c..6a86961477e8 100644
@@ -182,6 +182,9 @@
 #define WAKE_AOWAKE_TIER0_ROUTING(x) (0x4b4 + ((x) << 2))
 #define WAKE_AOWAKE_TIER1_ROUTING(x) (0x4c0 + ((x) << 2))
 #define WAKE_AOWAKE_TIER2_ROUTING(x) (0x4cc + ((x) << 2))
+#define WAKE_AOWAKE_SW_STATUS_W_0	0x49c
+#define WAKE_AOWAKE_SW_STATUS(x)	(0x4a0 + ((x) << 2))
+#define WAKE_LATCH_SW			0x498
 
 #define WAKE_AOWAKE_CTRL 0x4f4
 #define  WAKE_AOWAKE_CTRL_INTR_POLARITY BIT(0)
@@ -191,6 +194,12 @@
 #define  TEGRA_SMC_PMC_READ	0xaa
 #define  TEGRA_SMC_PMC_WRITE	0xbb
 
+#define WAKE_NR_EVENTS		96
+#define WAKE_NR_VECTORS		(WAKE_NR_EVENTS / 32)
+
+DECLARE_BITMAP(wake_level_map, WAKE_NR_EVENTS);
+DECLARE_BITMAP(wake_dual_edge_map, WAKE_NR_EVENTS);
+
 struct pmc_clk {
 	struct clk_hw	hw;
 	unsigned long	offs;
@@ -2469,29 +2478,37 @@ static int tegra186_pmc_irq_set_type(struct irq_data *data, unsigned int type)
 {
 	struct tegra_pmc *pmc = irq_data_get_irq_chip_data(data);
 	u32 value;
+	unsigned long wake_id;
 
-	value = readl(pmc->wake + WAKE_AOWAKE_CNTRL(data->hwirq));
+	wake_id = data->hwirq;
+	value = readl(pmc->wake + WAKE_AOWAKE_CNTRL(wake_id));
 
 	switch (type) {
 	case IRQ_TYPE_EDGE_RISING:
 	case IRQ_TYPE_LEVEL_HIGH:
 		value |= WAKE_AOWAKE_CNTRL_LEVEL;
+		set_bit(wake_id, wake_level_map);
+		clear_bit(wake_id, wake_dual_edge_map);
 		break;
 
 	case IRQ_TYPE_EDGE_FALLING:
 	case IRQ_TYPE_LEVEL_LOW:
 		value &= ~WAKE_AOWAKE_CNTRL_LEVEL;
+		clear_bit(wake_id, wake_level_map);
+		clear_bit(wake_id, wake_dual_edge_map);
 		break;
 
 	case IRQ_TYPE_EDGE_RISING | IRQ_TYPE_EDGE_FALLING:
 		value ^= WAKE_AOWAKE_CNTRL_LEVEL;
+		clear_bit(wake_id, wake_level_map);
+		set_bit(wake_id, wake_dual_edge_map);
 		break;
 
 	default:
 		return -EINVAL;
 	}
 
-	writel(value, pmc->wake + WAKE_AOWAKE_CNTRL(data->hwirq));
+	writel(value, pmc->wake + WAKE_AOWAKE_CNTRL(wake_id));
 
 	return 0;
 }
@@ -3074,28 +3091,142 @@ static int tegra_pmc_probe(struct platform_device *pdev)
 	return err;
 }
 
-#if defined(CONFIG_PM_SLEEP) && defined(CONFIG_ARM)
+#ifdef CONFIG_PM_SLEEP
+#ifdef CONFIG_ARM64
+/*
+ * Ensures that sufficient time is passed for a register write to
+ * serialize into the 32KHz domain.
+ */
+static void wke_32kwritel(u32 val, u32 reg)
+{
+	writel(val, pmc->wake + reg);
+	udelay(130);
+}
+
+static void wke_write_wake_level(int wake, int level)
+{
+	u32 val;
+	u32 reg = WAKE_AOWAKE_CNTRL(wake);
+
+	val = readl(pmc->wake + reg);
+	if (level)
+		val |= WAKE_AOWAKE_CNTRL_LEVEL;
+	else
+		val &= ~WAKE_AOWAKE_CNTRL_LEVEL;
+	writel(val, pmc->wake + reg);
+}
+
+static void wke_write_wake_levels(unsigned long *lvl)
+{
+	int i;
+
+	for (i = 0; i < WAKE_NR_EVENTS; i++)
+		wke_write_wake_level(i, test_bit(i, lvl));
+}
+
+static void wke_clear_sw_wake_status(void)
+{
+	wke_32kwritel(1, WAKE_AOWAKE_SW_STATUS_W_0);
+}
+
+static void wke_read_sw_wake_status(unsigned long *status_map)
+{
+	u32 status[WAKE_NR_VECTORS];
+	int i;
+
+	for (i = 0; i < WAKE_NR_EVENTS; i++)
+		wke_write_wake_level(i, 0);
+
+	wke_clear_sw_wake_status();
+
+	wke_32kwritel(1, WAKE_LATCH_SW);
+
+	/*
+	 * WAKE_AOWAKE_SW_STATUS is edge triggered, so in order to
+	 * obtain the current status of the input wake signals, change
+	 * the polarity of the wake level from 0->1 while latching to force
+	 * a positive edge if the sampled signal is '1'.
+	 */
+	for (i = 0; i < WAKE_NR_EVENTS; i++)
+		wke_write_wake_level(i, 1);
+
+	/*
+	 * Wait for the update to be synced into the 32kHz domain,
+	 * and let enough time lapse, so that the wake signals have time to
+	 * be sampled.
+	 */
+	udelay(300);
+
+	wke_32kwritel(0, WAKE_LATCH_SW);
+
+	for (i = 0; i < WAKE_NR_VECTORS; i++)
+		status[i] = readl(pmc->wake + WAKE_AOWAKE_SW_STATUS(i));
+
+	bitmap_from_arr32(status_map, status, WAKE_NR_EVENTS);
+}
+
+static void wke_clear_wake_status(void)
+{
+	u32 status;
+	int i, wake;
+	unsigned long ulong_status;
+
+	for (i = 0; i < WAKE_NR_VECTORS; i++) {
+		status = readl(pmc->wake + WAKE_AOWAKE_STATUS_R(i));
+		status = status & readl(pmc->wake +
+				WAKE_AOWAKE_TIER2_ROUTING(i));
+		ulong_status = (unsigned long)status;
+		for_each_set_bit(wake, &ulong_status, 32)
+			wke_32kwritel(0x1,
+				WAKE_AOWAKE_STATUS_W((i * 32) + wake));
+	}
+}
+#endif /* CONFIG_ARM64 */
+
 static int tegra_pmc_suspend(struct device *dev)
 {
+#ifdef CONFIG_ARM
 	struct tegra_pmc *pmc = dev_get_drvdata(dev);
 
 	tegra_pmc_writel(pmc, virt_to_phys(tegra_resume), PMC_SCRATCH41);
+#else /* CONFIG_ARM64 */
+	DECLARE_BITMAP(status, WAKE_NR_EVENTS);
+	DECLARE_BITMAP(lvl, WAKE_NR_EVENTS);
+	DECLARE_BITMAP(wake_level, WAKE_NR_EVENTS);
+	int i;
+
+	wke_read_sw_wake_status(status);
+
+	/* flip the wakeup trigger for dual-edge triggered pads
+	 * which are currently asserting as wakeups
+	 */
+	for (i = 0; i < BITS_TO_LONGS(WAKE_NR_EVENTS); i++) {
+		lvl[i] = ~status[i] & wake_dual_edge_map[i];
+		wake_level[i] = lvl[i] | wake_level_map[i];
+	}
+
+	/* Clear PMC Wake Status registers while going to suspend */
+	wke_clear_wake_status();
 
+	wke_write_wake_levels(wake_level);
+#endif
 	return 0;
 }
 
 static int tegra_pmc_resume(struct device *dev)
 {
+#ifdef CONFIG_ARM
 	struct tegra_pmc *pmc = dev_get_drvdata(dev);
 
 	tegra_pmc_writel(pmc, 0x0, PMC_SCRATCH41);
+#endif
 
 	return 0;
 }
 
 static SIMPLE_DEV_PM_OPS(tegra_pmc_pm_ops, tegra_pmc_suspend, tegra_pmc_resume);
 
-#endif
+#endif /*CONFIG_PM_SLEEP */
 
 static const char * const tegra20_powergates[] = {
 	[TEGRA_POWERGATE_CPU] = "cpu",
@@ -4069,7 +4200,7 @@ static struct platform_driver tegra_pmc_driver = {
 		.name = "tegra-pmc",
 		.suppress_bind_attrs = true,
 		.of_match_table = tegra_pmc_match,
-#if defined(CONFIG_PM_SLEEP) && defined(CONFIG_ARM)
+#if defined(CONFIG_PM_SLEEP)
 		.pm = &tegra_pmc_pm_ops,
 #endif
 		.sync_state = tegra_pmc_sync_state,
Signature · 1 line
2.17.1
#1 lore raw
Quoted 36 lines · Petlozu Pravareshwar

When a wake event is defined to be triggered on both positive and negative edge of the input wake signal, it is crucial to know the current state of the signal when going into suspend. The intended way to obtain the current state of the wake signals is to read the WAKE_AOWAKE_SW_STATUS register, which should contains the raw state of the wake signals.

However, this register is edge triggered, an edge will not be generated for signals that are already asserted prior to the assertion of WAKE_LATCH_SW.

To workaround this, change the polarity of the wake level from '0' to '1' while latching the signals, as this will generate an edge for signals that are set to '1'.

---
 drivers/soc/tegra/pmc.c | 141 ++++++++++++++++++++++++++++++++++++++--
 1 file changed, 136 insertions(+), 5 deletions(-)
drivers/soc/tegra/pmc.c +3 -0
index 495d16a4732c..6a86961477e8 100644
@@ -182,6 +182,9 @@
 #define WAKE_AOWAKE_TIER0_ROUTING(x) (0x4b4 + ((x) << 2))
 #define WAKE_AOWAKE_TIER1_ROUTING(x) (0x4c0 + ((x) << 2))
 #define WAKE_AOWAKE_TIER2_ROUTING(x) (0x4cc + ((x) << 2))
+#define WAKE_AOWAKE_SW_STATUS_W_0	0x49c
+#define WAKE_AOWAKE_SW_STATUS(x)	(0x4a0 + ((x) << 2))
+#define WAKE_LATCH_SW			0x498
 
 #define WAKE_AOWAKE_CTRL 0x4f4
 #define  WAKE_AOWAKE_CTRL_INTR_POLARITY BIT(0)

drivers/soc/tegra/pmc.c line 198

Quoted 5 lines
drivers/soc/tegra/pmc.c +2 -0
@@ -191,6 +194,12 @@
 #define  TEGRA_SMC_PMC_READ	0xaa
 #define  TEGRA_SMC_PMC_WRITE	0xbb
 
+#define WAKE_NR_EVENTS		96
+#define WAKE_NR_VECTORS		(WAKE_NR_EVENTS / 32)

Are these always 96? Can they not vary by SoC generation?

drivers/soc/tegra/pmc.c line 201

Quoted 3 lines
drivers/soc/tegra/pmc.c +3 -0
+
+DECLARE_BITMAP(wake_level_map, WAKE_NR_EVENTS);
+DECLARE_BITMAP(wake_dual_edge_map, WAKE_NR_EVENTS);

These should not be global variables but rather part of struct pmc. Might be worth to allocate them dynamically based on a parameterized per-SoC num_events.

drivers/soc/tegra/pmc.c static int tegra186_pmc_irq_set_type(struct irq_data *data, unsigned int type) · line 2481

Quoted 9 lines
drivers/soc/tegra/pmc.c +2 -0
+
 struct pmc_clk {
 	struct clk_hw	hw;
 	unsigned long	offs;
@@ -2469,29 +2478,37 @@ static int tegra186_pmc_irq_set_type(struct irq_data *data, unsigned int type)
 {
 	struct tegra_pmc *pmc = irq_data_get_irq_chip_data(data);
 	u32 value;
+	unsigned long wake_id;

The usefulness of that local variable is questionable. Might as well just keep using data->hwirq.

Quoted 128 lines
drivers/soc/tegra/pmc.c +97 -3
-	value = readl(pmc->wake + WAKE_AOWAKE_CNTRL(data->hwirq));
+	wake_id = data->hwirq;
+	value = readl(pmc->wake + WAKE_AOWAKE_CNTRL(wake_id));
 
 	switch (type) {
 	case IRQ_TYPE_EDGE_RISING:
 	case IRQ_TYPE_LEVEL_HIGH:
 		value |= WAKE_AOWAKE_CNTRL_LEVEL;
+		set_bit(wake_id, wake_level_map);
+		clear_bit(wake_id, wake_dual_edge_map);
 		break;
 
 	case IRQ_TYPE_EDGE_FALLING:
 	case IRQ_TYPE_LEVEL_LOW:
 		value &= ~WAKE_AOWAKE_CNTRL_LEVEL;
+		clear_bit(wake_id, wake_level_map);
+		clear_bit(wake_id, wake_dual_edge_map);
 		break;
 
 	case IRQ_TYPE_EDGE_RISING | IRQ_TYPE_EDGE_FALLING:
 		value ^= WAKE_AOWAKE_CNTRL_LEVEL;
+		clear_bit(wake_id, wake_level_map);
+		set_bit(wake_id, wake_dual_edge_map);
 		break;
 
 	default:
 		return -EINVAL;
 	}
 
-	writel(value, pmc->wake + WAKE_AOWAKE_CNTRL(data->hwirq));
+	writel(value, pmc->wake + WAKE_AOWAKE_CNTRL(wake_id));
 
 	return 0;
 }
@@ -3074,28 +3091,142 @@ static int tegra_pmc_probe(struct platform_device *pdev)
 	return err;
 }
 
-#if defined(CONFIG_PM_SLEEP) && defined(CONFIG_ARM)
+#ifdef CONFIG_PM_SLEEP
+#ifdef CONFIG_ARM64
+/*
+ * Ensures that sufficient time is passed for a register write to
+ * serialize into the 32KHz domain.
+ */
+static void wke_32kwritel(u32 val, u32 reg)
+{
+	writel(val, pmc->wake + reg);
+	udelay(130);
+}
+
+static void wke_write_wake_level(int wake, int level)
+{
+	u32 val;
+	u32 reg = WAKE_AOWAKE_CNTRL(wake);
+
+	val = readl(pmc->wake + reg);
+	if (level)
+		val |= WAKE_AOWAKE_CNTRL_LEVEL;
+	else
+		val &= ~WAKE_AOWAKE_CNTRL_LEVEL;
+	writel(val, pmc->wake + reg);
+}
+
+static void wke_write_wake_levels(unsigned long *lvl)
+{
+	int i;
+
+	for (i = 0; i < WAKE_NR_EVENTS; i++)
+		wke_write_wake_level(i, test_bit(i, lvl));
+}
+
+static void wke_clear_sw_wake_status(void)
+{
+	wke_32kwritel(1, WAKE_AOWAKE_SW_STATUS_W_0);
+}
+
+static void wke_read_sw_wake_status(unsigned long *status_map)
+{
+	u32 status[WAKE_NR_VECTORS];
+	int i;
+
+	for (i = 0; i < WAKE_NR_EVENTS; i++)
+		wke_write_wake_level(i, 0);
+
+	wke_clear_sw_wake_status();
+
+	wke_32kwritel(1, WAKE_LATCH_SW);
+
+	/*
+	 * WAKE_AOWAKE_SW_STATUS is edge triggered, so in order to
+	 * obtain the current status of the input wake signals, change
+	 * the polarity of the wake level from 0->1 while latching to force
+	 * a positive edge if the sampled signal is '1'.
+	 */
+	for (i = 0; i < WAKE_NR_EVENTS; i++)
+		wke_write_wake_level(i, 1);
+
+	/*
+	 * Wait for the update to be synced into the 32kHz domain,
+	 * and let enough time lapse, so that the wake signals have time to
+	 * be sampled.
+	 */
+	udelay(300);
+
+	wke_32kwritel(0, WAKE_LATCH_SW);
+
+	for (i = 0; i < WAKE_NR_VECTORS; i++)
+		status[i] = readl(pmc->wake + WAKE_AOWAKE_SW_STATUS(i));
+
+	bitmap_from_arr32(status_map, status, WAKE_NR_EVENTS);
+}
+
+static void wke_clear_wake_status(void)
+{
+	u32 status;
+	int i, wake;
+	unsigned long ulong_status;
+
+	for (i = 0; i < WAKE_NR_VECTORS; i++) {
+		status = readl(pmc->wake + WAKE_AOWAKE_STATUS_R(i));
+		status = status & readl(pmc->wake +
+				WAKE_AOWAKE_TIER2_ROUTING(i));
+		ulong_status = (unsigned long)status;
+		for_each_set_bit(wake, &ulong_status, 32)
+			wke_32kwritel(0x1,
+				WAKE_AOWAKE_STATUS_W((i * 32) + wake));

drivers/soc/tegra/pmc.c static int tegra_pmc_probe(struct platform_device *pdev) · line 3191

Quoted 10 lines
drivers/soc/tegra/pmc.c +5 -0
+	}
+}
+#endif /* CONFIG_ARM64 */
+
 static int tegra_pmc_suspend(struct device *dev)
 {
+#ifdef CONFIG_ARM
 	struct tegra_pmc *pmc = dev_get_drvdata(dev);
 
 	tegra_pmc_writel(pmc, virt_to_phys(tegra_resume), PMC_SCRATCH41);

If this code is really no longer needed on 64-bit ARM, this should be a separate patch. Also, these conditionals are becoming a bit unwieldy, so I wonder if we should instead move to C conditionals using IS_ENABLED() to make this a bit more readable and get improved code coverage at compile time.

drivers/soc/tegra/pmc.c static int tegra_pmc_probe(struct platform_device *pdev) · line 3192

Quoted 1 line
drivers/soc/tegra/pmc.c +1 -0
+#else /* CONFIG_ARM64 */

The implied condition here seems a bit broad. I think it might be better to conditionalize this code on the setting of tegra_pmc_soc.wake_events. If that's != NULL, then execute all this context store/restore, otherwise don't.

Quoted 4 lines
drivers/soc/tegra/pmc.c +4 -0
+	DECLARE_BITMAP(status, WAKE_NR_EVENTS);
+	DECLARE_BITMAP(lvl, WAKE_NR_EVENTS);
+	DECLARE_BITMAP(wake_level, WAKE_NR_EVENTS);
+	int i;

drivers/soc/tegra/pmc.c static int tegra_pmc_probe(struct platform_device *pdev) · line 3206

Quoted 10 lines
drivers/soc/tegra/pmc.c +10 -0
+
+	wke_read_sw_wake_status(status);
+
+	/* flip the wakeup trigger for dual-edge triggered pads
+	 * which are currently asserting as wakeups
+	 */
+	for (i = 0; i < BITS_TO_LONGS(WAKE_NR_EVENTS); i++) {
+		lvl[i] = ~status[i] & wake_dual_edge_map[i];
+		wake_level[i] = lvl[i] | wake_level_map[i];
+	}

I think these can be done using:

	bitmap_andnot(lvl, wake_dual_edge_map, status, WAKE_NR_EVENTS);
	bitmap_or(wake_level, lvl, wake_level_map, WAKE_NR_EVENTS);
Quoted 14 lines
drivers/soc/tegra/pmc.c +6 -0
+
+	/* Clear PMC Wake Status registers while going to suspend */
+	wke_clear_wake_status();
 
+	wke_write_wake_levels(wake_level);
+#endif
 	return 0;
 }
 
 static int tegra_pmc_resume(struct device *dev)
 {
+#ifdef CONFIG_ARM
 	struct tegra_pmc *pmc = dev_get_drvdata(dev);
 

drivers/soc/tegra/pmc.c static int tegra_pmc_probe(struct platform_device *pdev) · line 3229

Quoted 10 lines
drivers/soc/tegra/pmc.c +2 -1
 	tegra_pmc_writel(pmc, 0x0, PMC_SCRATCH41);
+#endif
 
 	return 0;
 }
 
 static SIMPLE_DEV_PM_OPS(tegra_pmc_pm_ops, tegra_pmc_suspend, tegra_pmc_resume);
 
-#endif
+#endif /*CONFIG_PM_SLEEP */

Perhaps we should then also drop the CONFIG_PM_SLEEP conditional and instead mark tegra_pmc_suspend()/tegra_pmc_resume() as __maybe_unused. Could also be a separate patch.

Thierry

Quoted 15 lines
drivers/soc/tegra/pmc.c +1 -1
 static const char * const tegra20_powergates[] = {
 	[TEGRA_POWERGATE_CPU] = "cpu",
@@ -4069,7 +4200,7 @@ static struct platform_driver tegra_pmc_driver = {
 		.name = "tegra-pmc",
 		.suppress_bind_attrs = true,
 		.of_match_table = tegra_pmc_match,
-#if defined(CONFIG_PM_SLEEP) && defined(CONFIG_ARM)
+#if defined(CONFIG_PM_SLEEP)
 		.pm = &tegra_pmc_pm_ops,
 #endif
 		.sync_state = tegra_pmc_sync_state,
Signature · 1 line
2.17.1
#2 lore raw
Quoted 40 lines · Petlozu Pravareshwar
Quoted 5 lines

When a wake event is defined to be triggered on both positive and negative edge of the input wake signal, it is crucial to know the current state of the signal when going into suspend.
The intended way to obtain the current state of the wake signals is to read the WAKE_AOWAKE_SW_STATUS register, which should contains the

raw

Quoted 25 lines

state of the wake signals.

However, this register is edge triggered, an edge will not be generated for signals that are already asserted prior to the assertion of WAKE_LATCH_SW.

To workaround this, change the polarity of the wake level from '0' to '1' while latching the signals, as this will generate an edge for signals that are set to '1'.

---
 drivers/soc/tegra/pmc.c | 141
++++++++++++++++++++++++++++++++++++++--
 1 file changed, 136 insertions(+), 5 deletions(-)
drivers/soc/tegra/pmc.c index renamed from drivers/soc/tegra/pmc.c +0 -0

495d16a4732c..6a86961477e8 100644

drivers/soc/tegra/pmc.c +0 -0
@@ -182,6 +182,9 @@
 #define WAKE_AOWAKE_TIER0_ROUTING(x) (0x4b4 + ((x) << 2))  #define

WAKE_AOWAKE_TIER1_ROUTING(x) (0x4c0 + ((x) << 2)) #define WAKE_AOWAKE_TIER2_ROUTING(x) (0x4cc + ((x) << 2))

drivers/soc/tegra/pmc.c

Quoted 6 lines
drivers/soc/tegra/pmc.c +3 -0
+#define WAKE_AOWAKE_SW_STATUS_W_0	0x49c
+#define WAKE_AOWAKE_SW_STATUS(x)	(0x4a0 + ((x) << 2))
+#define WAKE_LATCH_SW			0x498
 
 #define WAKE_AOWAKE_CTRL 0x4f4
 #define  WAKE_AOWAKE_CTRL_INTR_POLARITY BIT(0) @@ -191,6 +194,12

@@

Quoted 7 lines

drivers/soc/tegra/pmc.c line 198

Quoted 5 lines

#define TEGRA_SMC_PMC_READ 0xaa
#define TEGRA_SMC_PMC_WRITE 0xbb

drivers/soc/tegra/pmc.c +2 -0
+#define WAKE_NR_EVENTS		96
+#define WAKE_NR_VECTORS		(WAKE_NR_EVENTS / 32)

Are these always 96? Can they not vary by SoC generation?

Yes. They can vary by SoC generation. I will create a variable in struct tegra_pmc_soc for the maximum number of wake events supported and use that per-SoC variable.

Quoted 8 lines

drivers/soc/tegra/pmc.c line 201

Quoted 4 lines
drivers/soc/tegra/pmc.c +3 -0
+
+DECLARE_BITMAP(wake_level_map, WAKE_NR_EVENTS);
+DECLARE_BITMAP(wake_dual_edge_map, WAKE_NR_EVENTS);

These should not be global variables but rather part of struct pmc. Might be worth to allocate them dynamically based on a parameterized per- SoC num_events.

Agree.

Quoted 13 lines

drivers/soc/tegra/pmc.c static int tegra186_pmc_irq_set_type(struct · line 2481

Quoted 10 lines
drivers/soc/tegra/pmc.c +2 -0
+
 struct pmc_clk {
 	struct clk_hw	hw;
 	unsigned long	offs;
@@ -2469,29 +2478,37 @@ static int tegra186_pmc_irq_set_type(struct
 irq_data *data, unsigned int type)  {
 	struct tegra_pmc *pmc = irq_data_get_irq_chip_data(data);
 	u32 value;
+	unsigned long wake_id;

The usefulness of that local variable is questionable. Might as well just keep using data->hwirq.

Agree.

Quoted 131 lines

drivers/soc/tegra/pmc.c static int tegra_pmc_probe(struct

Quoted 37 lines
drivers/soc/tegra/pmc.c +9 -2
-	value = readl(pmc->wake + WAKE_AOWAKE_CNTRL(data->hwirq));
+	wake_id = data->hwirq;
+	value = readl(pmc->wake + WAKE_AOWAKE_CNTRL(wake_id));
 
 	switch (type) {
 	case IRQ_TYPE_EDGE_RISING:
 	case IRQ_TYPE_LEVEL_HIGH:
 		value |= WAKE_AOWAKE_CNTRL_LEVEL;
+		set_bit(wake_id, wake_level_map);
+		clear_bit(wake_id, wake_dual_edge_map);
 		break;
 
 	case IRQ_TYPE_EDGE_FALLING:
 	case IRQ_TYPE_LEVEL_LOW:
 		value &= ~WAKE_AOWAKE_CNTRL_LEVEL;
+		clear_bit(wake_id, wake_level_map);
+		clear_bit(wake_id, wake_dual_edge_map);
 		break;
 
 	case IRQ_TYPE_EDGE_RISING | IRQ_TYPE_EDGE_FALLING:
 		value ^= WAKE_AOWAKE_CNTRL_LEVEL;
+		clear_bit(wake_id, wake_level_map);
+		set_bit(wake_id, wake_dual_edge_map);
 		break;
 
 	default:
 		return -EINVAL;
 	}
 
-	writel(value, pmc->wake + WAKE_AOWAKE_CNTRL(data->hwirq));
+	writel(value, pmc->wake + WAKE_AOWAKE_CNTRL(wake_id));
 
 	return 0;
 }
@@ -3074,28 +3091,142 @@ static int tegra_pmc_probe(struct

platform_device *pdev)

Quoted 57 lines
drivers/soc/tegra/pmc.c +53 -1
 	return err;
 }
 
-#if defined(CONFIG_PM_SLEEP) && defined(CONFIG_ARM)
+#ifdef CONFIG_PM_SLEEP
+#ifdef CONFIG_ARM64
+/*
+ * Ensures that sufficient time is passed for a register write to
+ * serialize into the 32KHz domain.
+ */
+static void wke_32kwritel(u32 val, u32 reg) {
+	writel(val, pmc->wake + reg);
+	udelay(130);
+}
+
+static void wke_write_wake_level(int wake, int level) {
+	u32 val;
+	u32 reg = WAKE_AOWAKE_CNTRL(wake);
+
+	val = readl(pmc->wake + reg);
+	if (level)
+		val |= WAKE_AOWAKE_CNTRL_LEVEL;
+	else
+		val &= ~WAKE_AOWAKE_CNTRL_LEVEL;
+	writel(val, pmc->wake + reg);
+}
+
+static void wke_write_wake_levels(unsigned long *lvl) {
+	int i;
+
+	for (i = 0; i < WAKE_NR_EVENTS; i++)
+		wke_write_wake_level(i, test_bit(i, lvl)); }
+
+static void wke_clear_sw_wake_status(void) {
+	wke_32kwritel(1, WAKE_AOWAKE_SW_STATUS_W_0); }
+
+static void wke_read_sw_wake_status(unsigned long *status_map) {
+	u32 status[WAKE_NR_VECTORS];
+	int i;
+
+	for (i = 0; i < WAKE_NR_EVENTS; i++)
+		wke_write_wake_level(i, 0);
+
+	wke_clear_sw_wake_status();
+
+	wke_32kwritel(1, WAKE_LATCH_SW);
+
+	/*
+	 * WAKE_AOWAKE_SW_STATUS is edge triggered, so in order to
+	 * obtain the current status of the input wake signals, change
+	 * the polarity of the wake level from 0->1 while latching to force
+	 * a positive edge if the sampled signal is '1'.
+	 */
+	for (i = 0; i < WAKE_NR_EVENTS; i++)
+		wke_write_wake_level(i, 1);
+
+	/*

drivers/soc/tegra/pmc.c static int tegra_pmc_probe(struct platform_device *pdev)

Quoted 10 lines
drivers/soc/tegra/pmc.c +10 -0
+	 * Wait for the update to be synced into the 32kHz domain,
+	 * and let enough time lapse, so that the wake signals have time to
+	 * be sampled.
+	 */
+	udelay(300);
+
+	wke_32kwritel(0, WAKE_LATCH_SW);
+
+	for (i = 0; i < WAKE_NR_VECTORS; i++)
+		status[i] = readl(pmc->wake +

WAKE_AOWAKE_SW_STATUS(i));

Quoted 6 lines
drivers/soc/tegra/pmc.c +6 -0
+
+	bitmap_from_arr32(status_map, status, WAKE_NR_EVENTS); }
+
+static void wke_clear_wake_status(void) {
+	u32 status;
+	int i, wake;

drivers/soc/tegra/pmc.c static int tegra_pmc_probe(struct platform_device *pdev)

Quoted 10 lines
drivers/soc/tegra/pmc.c +10 -0
+	unsigned long ulong_status;
+
+	for (i = 0; i < WAKE_NR_VECTORS; i++) {
+		status = readl(pmc->wake + WAKE_AOWAKE_STATUS_R(i));
+		status = status & readl(pmc->wake +
+				WAKE_AOWAKE_TIER2_ROUTING(i));
+		ulong_status = (unsigned long)status;
+		for_each_set_bit(wake, &ulong_status, 32)
+			wke_32kwritel(0x1,
+				WAKE_AOWAKE_STATUS_W((i * 32) +

wake));

Quoted 8 lines
drivers/soc/tegra/pmc.c +5 -0
+	}
+}
+#endif /* CONFIG_ARM64 */
+
 static int tegra_pmc_suspend(struct device *dev)  {
+#ifdef CONFIG_ARM
 	struct tegra_pmc *pmc = dev_get_drvdata(dev);
 
Quoted 8 lines

drivers/soc/tegra/pmc.c static int tegra_pmc_probe(struct · line 3181

Quoted 1 line
drivers/soc/tegra/pmc.c +0 -0
 	tegra_pmc_writel(pmc, virt_to_phys(tegra_resume),

PMC_SCRATCH41);

If this code is really no longer needed on 64-bit ARM, this should be a separate patch. Also, these conditionals are becoming a bit unwieldy, so I wonder if we should instead move to C conditionals using IS_ENABLED() to make this a bit more readable and get improved code coverage at compile time.

Agree.

Quoted 6 lines

drivers/soc/tegra/pmc.c static int tegra_pmc_probe(struct platform_device *pdev) · line 3192

Quoted 2 lines
drivers/soc/tegra/pmc.c +1 -0
+#else /* CONFIG_ARM64 */

The implied condition here seems a bit broad. I think it might be better to conditionalize this code on the setting of tegra_pmc_soc.wake_events. If that's != NULL, then execute all this context store/restore, otherwise don't.

Actually, on further analysis I see couple of issues here.

In the below code, we are trying to flip the wakeup trigger level for dual-edge triggered wakes which are currently asserting as wakeups. I think ideally this should be done after all the device PM suspend callbacks have executed. So instead of tegra_pmc_suspend(), a struct syscore_ops.suspend callback could be a better place to ensure this code gets executed very late during system suspend.

Also, wake related HW register organization is different for tegra210 SoC compared to tegra186 and beyond SoCs. Below code is actually applicable only for tegra186 and beyond SoCs. Currently per-SoC "init" pointer is NULL for tegra186 and beyond SoCs. I am planning to set this pointer to a new init API to do register_syscore_ops() which would register a syscore suspend callback API containing the below code. I will update the patch accordingly.

Quoted 18 lines
Quoted 5 lines
drivers/soc/tegra/pmc.c +4 -0
+	DECLARE_BITMAP(status, WAKE_NR_EVENTS);
+	DECLARE_BITMAP(lvl, WAKE_NR_EVENTS);
+	DECLARE_BITMAP(wake_level, WAKE_NR_EVENTS);
+	int i;

drivers/soc/tegra/pmc.c static int tegra_pmc_probe(struct platform_device *pdev) · line 3206

Quoted 10 lines
drivers/soc/tegra/pmc.c +10 -0
+
+	wke_read_sw_wake_status(status);
+
+	/* flip the wakeup trigger for dual-edge triggered pads
+	 * which are currently asserting as wakeups
+	 */
+	for (i = 0; i < BITS_TO_LONGS(WAKE_NR_EVENTS); i++) {
+		lvl[i] = ~status[i] & wake_dual_edge_map[i];
+		wake_level[i] = lvl[i] | wake_level_map[i];
+	}

I think these can be done using:

Quoted 3 lines

bitmap_andnot(lvl, wake_dual_edge_map, status,

WAKE_NR_EVENTS);
	bitmap_or(wake_level, lvl, wake_level_map, WAKE_NR_EVENTS);

Agree.

Quoted 23 lines
Quoted 23 lines
drivers/soc/tegra/pmc.c +7 -0
+
+	/* Clear PMC Wake Status registers while going to suspend */
+	wke_clear_wake_status();
 
+	wke_write_wake_levels(wake_level);
+#endif
 	return 0;
 }
 
 static int tegra_pmc_resume(struct device *dev)  {
+#ifdef CONFIG_ARM
 	struct tegra_pmc *pmc = dev_get_drvdata(dev);
 
 	tegra_pmc_writel(pmc, 0x0, PMC_SCRATCH41);
+#endif
 
 	return 0;
 }
 
 static SIMPLE_DEV_PM_OPS(tegra_pmc_pm_ops, tegra_pmc_suspend,
 tegra_pmc_resume);
 
Quoted 7 lines

drivers/soc/tegra/pmc.c static int tegra_pmc_probe(struct platform_device *pdev) · line 3229

Quoted 2 lines
drivers/soc/tegra/pmc.c +1 -1
-#endif
+#endif /*CONFIG_PM_SLEEP */

Perhaps we should then also drop the CONFIG_PM_SLEEP conditional and instead mark tegra_pmc_suspend()/tegra_pmc_resume() as __maybe_unused.
Could also be a separate patch.

Agree.

Quoted 19 lines

Thierry

drivers/soc/tegra/pmc.c static struct platform_driver tegra_pmc_driver =

Quoted 4 lines
drivers/soc/tegra/pmc.c +0 -0
 static const char * const tegra20_powergates[] = {
 	[TEGRA_POWERGATE_CPU] = "cpu",
@@ -4069,7 +4200,7 @@ static struct platform_driver tegra_pmc_driver =

{

Quoted 11 lines
drivers/soc/tegra/pmc.c +1 -2
 		.name = "tegra-pmc",
 		.suppress_bind_attrs = true,
 		.of_match_table = tegra_pmc_match,
-#if defined(CONFIG_PM_SLEEP) && defined(CONFIG_ARM)
+#if defined(CONFIG_PM_SLEEP)
 		.pm = &tegra_pmc_pm_ops,
 #endif
 		.sync_state = tegra_pmc_sync_state,
--

2.17.1