From d8cadae6b52d168a8b3370b4953a121b48174312 Mon Sep 17 00:00:00 2001
From: Sebastian Pop <s.pop@samsung.com>
Date: Wed, 31 May 2017 10:09:45 -0500
Subject: [PATCH 3/3] hi6220-sysconfig: turn on debug mode on all cpus

Loop through all cpus and turn on debug mode.

Heavily inspired by
https://github.com/96boards-hikey/arm-trusted-firmware/commit/543f1db7fd7e0aadcf947adedd63eaed817ef402

Signed-off-by: George Burgess <gbiv@google.com>
Signed-off-by: Sebastian Pop <s.pop@samsung.com>
---
 drivers/misc/hi6220-sysconfig.c | 42 ++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 41 insertions(+), 1 deletion(-)

diff --git a/drivers/misc/hi6220-sysconfig.c b/drivers/misc/hi6220-sysconfig.c
index c61bfbb..e8adfa7 100644
--- a/drivers/misc/hi6220-sysconfig.c
+++ b/drivers/misc/hi6220-sysconfig.c
@@ -4,10 +4,41 @@
 #define ACPU_SC_CLKEN 0x00C
 #define ACPU_SC_CLK_STAT 0x014
 
+// Heavily inspired by
+// https://github.com/96boards-hikey/arm-trusted-firmware/commit/543f1db7fd7e0aadcf947adedd63eaed817ef402
+static int ensure_debug_mode_on(void __iomem *base, unsigned int cpu,
+				unsigned int cluster)
+{
+	void __iomem *base_off;
+	unsigned int data, expected;
+	// Some number high enough to basically never fail, but also to not lock
+	// up the system in case my code is broken.
+	const unsigned int max_loop_retries = 10000;
+	unsigned int loop_retries = 0;
+
+	base_off = base + 0x02c;
+	data = readl_relaxed(base_off);
+	if (cluster)
+		expected = 1 << (cpu + 8);
+	else
+		expected = 1 << cpu;
+	writel_relaxed((data | expected), base_off);
+
+	for (loop_retries = 0; loop_retries < max_loop_retries;
+	     loop_retries++) {
+		/* RAW barrier */
+		data = readl_relaxed(base_off);
+		if (data & expected)
+			break;
+	}
+	return loop_retries == max_loop_retries;
+}
+
 static int __init hi6220_sysconf(void)
 {
         static void __iomem *base = NULL;
- 
+	unsigned int cpu, cluster;
+
 	base = ioremap(SOC_HI6220_ACPU_SCTRL_BASE_ADDR, SZ_4K);
 	if (base == NULL) {
 		pr_err("hi6220: asctl reg iomap failed!\n");
@@ -21,6 +52,15 @@ static int __init hi6220_sysconf(void)
 	pr_err("%s:after sctrl ACPU_SC_CLK_STAT is %x\n",
 		__func__, 
 		readl(base + ACPU_SC_CLK_STAT));
+	for (cluster = 0; cluster < 2; cluster++) {
+		for (cpu = 0; cpu < 4; cpu++) {
+			if (ensure_debug_mode_on(base, cpu, cluster)) {
+				pr_err("hi6220: flipping debug mode for cpu %d "
+				       "failed.\n", cpu + cluster*4);
+				// Keep trying for the remainder anyway.
+			}
+		}
+	}
 
 	iounmap(base);
 
-- 
2.6.3

