Add TZASC configure codes to imx6ul platforms

In platform init stage TZASC will be configured to
protect the DRAM that Trusty OS running in.

Differnet target will have different TZASC regions
config.

Currently two spaces defined in TZASC regions tables:
  * Linux/Android space
    Secure/Non-secure world R/W.
  * Trusty OS space
    Secure world accessible R/W only.

Change-Id: I1473fff048311ab86c9ec1c9c9cbce2c77a15e88
Signed-off-by: Wang Haoran <elven.wang@nxp.com>
diff --git a/platform/imx/common/include/platform/tzasc.h b/platform/imx/common/include/platform/tzasc.h
new file mode 100644
index 0000000..a968cd9
--- /dev/null
+++ b/platform/imx/common/include/platform/tzasc.h
@@ -0,0 +1,94 @@
+/*
+ * Copyright (C) 2016 Freescale Semiconductor, Inc.
+ * Copyright 2017 NXP
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright notice,
+ * this list of conditions and the following disclaimer.
+ *
+ * 2. Redistributions in binary form must reproduce the above copyright notice,
+ * this list of conditions and the following disclaimer in the documentation
+ * and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#ifndef TZASC_H_
+#define TZASC_H_
+
+#include <reg.h>
+#include <imx-regs.h>
+#include <sys/types.h>
+
+#define TZ_IRQ				140
+
+#define TZ_REG(r)			(*REG32(r))
+
+#define TZ_CONFIG 			(TZ_BASE_VIRT + 0x000)
+#define TZ_ACTION 			(TZ_BASE_VIRT + 0x004)
+#define TZ_INT_STATUS			(TZ_BASE_VIRT + 0x010)
+#define TZ_INT_CLEAR			(TZ_BASE_VIRT + 0x014)
+#define TZ_FAIL_ADDR_LOW		(TZ_BASE_VIRT + 0x020)
+#define TZ_FAIL_ADDR_HIGH		(TZ_BASE_VIRT + 0x024)
+#define TZ_FAIL_CONTROL			(TZ_BASE_VIRT + 0x028)
+#define TZ_FAIL_ID			(TZ_BASE_VIRT + 0x02c)
+#define TZ_SPECULATION_CTL		(TZ_BASE_VIRT + 0x030)
+#define TZ_SPECULATION_CTL_DISABLE_ALL	(0x3)
+
+#define TZ_REGION_BASE			(TZ_BASE_VIRT + 0x100)
+#define TZ_GET_REGION_ADDR(n)		(TZ_REGION_BASE + (n * 0x10))
+
+#define TZ_SP_NS_W			(0x1 << 0)
+#define TZ_SP_NS_R			(0x1 << 1)
+#define TZ_SP_S_W			(0x1 << 2)
+#define TZ_SP_S_R			(0x1 << 3)
+
+#define TZ_SP_SHIFT			28
+#define TZ_ATTR_SP_S_WR_ONLR		(0x0 | ((TZ_SP_S_W | TZ_SP_S_R) << TZ_SP_SHIFT))
+#define TZ_ATTR_SP_ALL			(0x0 | ((TZ_SP_S_W | TZ_SP_S_R | TZ_SP_NS_R | TZ_SP_NS_W) << TZ_SP_SHIFT))
+
+#define TZ_REGION_SIZE_4M		0x15
+#define TZ_REGION_SIZE_8M		0x16
+#define TZ_REGION_SIZE_16M		0x17
+#define TZ_REGION_SIZE_32M		0x18
+#define TZ_REGION_SIZE_64M		0x19
+#define TZ_REGION_SIZE_128M		0x1A
+#define TZ_REGION_SIZE_256M		0x1B
+#define TZ_REGION_SIZE_512M		0x1C
+#define TZ_REGION_SIZE_1G		0x1D
+#define TZ_REGION_SIZE_2G		0x1E
+#define TZ_REGION_SIZE_4G		0x1F
+
+
+#define TZ_REGION_SIZE_SHIFT		1
+#define TZ_ATTR_REGION_SIZE(s)		(0x0 | ((s) << TZ_REGION_SIZE_SHIFT))
+
+#define TZ_ATTR_DISABLE_REGION		0x0
+
+#define TZ_REGION_ENABLE		0x1
+#define TZ_REGION_DISABLE		0x0
+#define TZ_ATTR(sp,sub_dis,size,en)	(0x0 | (sp | sub_dis | size | en))
+
+
+typedef struct tzasc_region{
+	paddr_t addr_l;
+	paddr_t addr_h;
+	uint32_t attr;
+} tzasc_region_t;
+
+int initial_tzasc(tzasc_region_t* regions);
+
+#endif
diff --git a/platform/imx/platform.c b/platform/imx/platform.c
index e92187f..a5e89b8 100644
--- a/platform/imx/platform.c
+++ b/platform/imx/platform.c
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2015 Google Inc. All rights reserved
+ * Copyright (c) 2017 Google Inc. All rights reserved
  *
  * Permission is hereby granted, free of charge, to any person obtaining
  * a copy of this software and associated documentation files
@@ -28,6 +28,10 @@
 #include <kernel/vm.h>
 #include <lk/init.h>
 #include <string.h>
+#ifdef WITH_TZASC
+#include <platform/tzasc.h>
+#include <tzasc_regions.h>
+#endif
 
 
 #define ARM_GENERIC_TIMER_INT_CNTV 27
@@ -119,6 +123,15 @@
 	/* Map for all SoC IPs. */
 	generic_arm64_map_regs("soc", SOC_REGS_VIRT, SOC_REGS_PHY, SOC_REGS_SIZE);
 
+#ifdef WITH_TZASC
+	/* Initialize TZASC. */
+	generic_arm64_map_regs("tzasc", TZ_BASE_VIRT, TZ_BASE, TZ_REG_SIZE);
+	if (initial_tzasc(tzasc_regions) != 0)
+		dprintf(CRITICAL, "TZASC init error!\n");
+	else
+		dprintf(CRITICAL, "TZASC inited.\n");
+#endif
+
 }
 
 LK_INIT_HOOK(platform_after_vm, platform_after_vm_init, LK_INIT_LEVEL_VM + 1);
diff --git a/platform/imx/rules.mk b/platform/imx/rules.mk
index 16569b8..d38e8ee 100644
--- a/platform/imx/rules.mk
+++ b/platform/imx/rules.mk
@@ -1,5 +1,5 @@
 #
-# Copyright (c) 2015, Google, Inc. All rights reserved
+# Copyright (c) 2017, Google, Inc. All rights reserved
 #
 # Permission is hereby granted, free of charge, to any person obtaining
 # a copy of this software and associated documentation files
@@ -41,6 +41,14 @@
 	$(LOCAL_DIR)/platform.c \
 	$(LOCAL_DIR)/drivers/imx_uart.c \
 
+ifeq (true,$(call TOBOOL,$(WITH_TZASC)))
+MODULE_SRCS += \
+	$(LOCAL_DIR)/tzasc.c
+
+GLOBAL_DEFINES += \
+	WITH_TZASC=1
+endif
+
 MODULE_DEPS += \
 	dev/interrupt/arm_gic \
 	dev/timer/arm_generic
diff --git a/platform/imx/tzasc.c b/platform/imx/tzasc.c
new file mode 100644
index 0000000..b065aff
--- /dev/null
+++ b/platform/imx/tzasc.c
@@ -0,0 +1,76 @@
+/*
+ * Copyright (C) 2016 Freescale Semiconductor, Inc.
+ * Copyright 2017 NXP
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright notice,
+ * this list of conditions and the following disclaimer.
+ *
+ * 2. Redistributions in binary form must reproduce the above copyright notice,
+ * this list of conditions and the following disclaimer in the documentation
+ * and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include <platform/tzasc.h>
+
+int check_region_table_end(tzasc_region_t *region) {
+
+	if ((region->addr_l | region->addr_h | region->attr) == 0)
+		return 1;
+
+	return 0;
+}
+
+int write_tzasc_region(tzasc_region_t *region, int region_num) {
+
+	if (region != NULL) {
+		TZ_REG(TZ_GET_REGION_ADDR(region_num)) = region->addr_l;
+		TZ_REG(TZ_GET_REGION_ADDR(region_num) + 0x4) = region->addr_h;
+		TZ_REG(TZ_GET_REGION_ADDR(region_num) + 0x8) = region->attr;
+	} else {
+		return -1;
+	}
+
+    return 0;
+}
+
+int initial_tzasc(tzasc_region_t* regions) {
+
+	int ret = 0;
+	/*
+	 * ACTION field 0x2 means
+	 * sets tzasc_int HIGH and issues an OKAY response
+	 */
+	TZ_REG(TZ_ACTION) = 0x2;
+
+	//From number 0 region to config.
+	int region_num = 0;
+	while(!(check_region_table_end(regions))) {
+		if (write_tzasc_region(regions, region_num)) {
+			ret = -1;
+			goto out;
+		}
+		region_num++;
+		regions++;
+	}
+
+	TZ_REG(TZ_INT_CLEAR) = 0;
+
+out:
+	return ret;
+}
diff --git a/target/imx/soc/imx6ul/include/imx-regs.h b/target/imx/soc/imx6ul/include/imx-regs.h
index cef42be..6b4af61 100644
--- a/target/imx/soc/imx6ul/include/imx-regs.h
+++ b/target/imx/soc/imx6ul/include/imx-regs.h
@@ -51,4 +51,11 @@
 
 #define GIC_REG_SIZE 0x2000
 
+/* Registers for TZASC */
+#define TZ_BASE ((0x02100000 + 0x80000) + 0x50000)
+#define TZ_BASE_VIRT (0x70000000 + TZ_BASE)
+#define TZ_REG_SIZE 0x4000
+#define TZ_BYPASS_GPR_BASE 0x20E4024
+#define TZ_BYPASS_GPR_BASE_VIRT (0x70000000 + TZ_BYPASS_GPR_BASE)
+
 #endif
diff --git a/target/imx/soc/imx6ul/include/tzasc_regions.h b/target/imx/soc/imx6ul/include/tzasc_regions.h
new file mode 100644
index 0000000..5aeb5bb
--- /dev/null
+++ b/target/imx/soc/imx6ul/include/tzasc_regions.h
@@ -0,0 +1,66 @@
+/*
+ * Copyright (C) 2016 Freescale Semiconductor, Inc.
+ * Copyright 2017 NXP
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright notice,
+ * this list of conditions and the following disclaimer.
+ *
+ * 2. Redistributions in binary form must reproduce the above copyright notice,
+ * this list of conditions and the following disclaimer in the documentation
+ * and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#ifndef _TZASC_REGIONS_H_
+#define _TZASC_REGIONS_H_
+
+#include <platform/tzasc.h>
+
+tzasc_region_t tzasc_regions[] = {
+	{
+	.addr_l = 0,
+	.addr_h = 0,
+	.attr = TZ_ATTR(TZ_ATTR_SP_ALL,
+		TZ_ATTR_DISABLE_REGION,
+		TZ_ATTR_REGION_SIZE(TZ_REGION_SIZE_4G),
+		TZ_REGION_ENABLE)
+	},
+
+	{
+	.addr_l = 0x80000000,
+	.addr_h = 0,
+	.attr = TZ_ATTR(TZ_ATTR_SP_ALL,
+		TZ_ATTR_DISABLE_REGION,
+		TZ_ATTR_REGION_SIZE(TZ_REGION_SIZE_512M),
+		TZ_REGION_ENABLE)
+	},
+
+	{
+	.addr_l = MEMBASE,
+	.addr_h = 0x0,
+	.attr = TZ_ATTR(TZ_ATTR_SP_S_WR_ONLR,
+		TZ_ATTR_DISABLE_REGION,
+		TZ_ATTR_REGION_SIZE(TZ_REGION_SIZE_32M),
+		TZ_REGION_ENABLE)
+	},
+
+	{0}
+};
+
+
+#endif