Add lock_otp method to Barebox

This command locks the OTP against future writes.

Also make otp_lock(..) return an integer indicating the success status.

Change-Id: I3ce263e6175bbf8af591c6367fcf73af4c0019b6
diff --git a/commands/otp_key.c b/commands/otp_key.c
index b9d0c3b..cf89599 100644
--- a/commands/otp_key.c
+++ b/commands/otp_key.c
@@ -234,6 +234,29 @@
 	BAREBOX_CMD_HELP(cmd_verify_key_help)
 BAREBOX_CMD_END
 
+static int do_lock_otp(struct command *cmdtp, int argc, char *argv[])
+{
+	if (otp_lock() != 0) {
+		printf("Error: Locking the OTP failed. OTP MAY or MAY NOT be locked!\n");
+		return -1;
+	}
+
+	printf("OTP is now write-locked\n");
+	return 0;
+}
+
+static const __maybe_unused char cmd_lock_otp_help[] =
+"Usage: lock_otp\n"
+"Locks the OTP against further writes.\n"
+"WARNING: This cannot be undone!\n";
+
+BAREBOX_CMD_START(lock_otp)
+	.cmd		= do_lock_otp,
+	.usage		= "lock otp against future writes",
+	BAREBOX_CMD_HELP(cmd_lock_otp_help)
+BAREBOX_CMD_END
+
+
 static int do_enable_auth(struct command *cmdtp, int argc, char *argv[])
 {
 	uint8_t bytes[2], one = 1;
diff --git a/drivers/otp/c2k_otp.c b/drivers/otp/c2k_otp.c
index 3642588..478ac28 100644
--- a/drivers/otp/c2k_otp.c
+++ b/drivers/otp/c2k_otp.c
@@ -225,7 +225,15 @@
 }
 EXPORT_SYMBOL(otp_read);
 
-void otp_lock(void)
+/*
+ * Locks the OTP against future writes.
+ *
+ * Returns 0 on success, non-zero otherwise. Note that a
+ * non-zero return does not mean that the OTP is unlocked, only
+ * that the lock bit did not respond before a timeout expired.
+ * The OTP may become locked in the future.
+ */
+int otp_lock(void)
 {
 	u32 i;
 	u32 pgm2cpump_counter, cpump2web_counter, web_counter, web2cpump_counter;
@@ -311,10 +319,12 @@
 		udelay(100);
 	}
 
-	if (!(readl(OTP_SECURE_LOCK_OUTPUT) & 1))
+	if (!(readl(OTP_SECURE_LOCK_OUTPUT) & 1)) {
 		printk("Timeout waiting for Security Lock Status Going high \n ");
+		return -1;
+	}
 
-	return;
+	return 0;
 }
 EXPORT_SYMBOL(otp_lock);
 
diff --git a/include/c2k_otp.h b/include/c2k_otp.h
index 6485f71..8ee00c6 100644
--- a/include/c2k_otp.h
+++ b/include/c2k_otp.h
@@ -5,6 +5,6 @@
 
 int otp_read(u32 s_addr, u8 *read_data, int size) ;
 int otp_write(u32 offset, u8 *prog_data, int size);
-void otp_lock(void);
+int otp_lock(void);
 
 #endif