1 // SPDX-License-Identifier: BSD-2-Clause
2 /*
3  * Copyright (C) 2019, Theobroma Systems Design und Consulting GmbH
4  */
5 
6 #include <common.h>
7 #include <io.h>
8 #include <kernel/panic.h>
9 #include <mm/core_memprot.h>
10 #include <platform.h>
11 #include <platform_config.h>
12 
13 #define SGRF_DDRRGN_CON0_16(n)		((n) * 4)
14 #define SGRF_DDR_RGN_0_16_WMSK		GENMASK_32(11, 0)
15 
16 register_phys_mem_pgdir(MEM_AREA_IO_SEC, SGRF_BASE, SGRF_SIZE);
17 
platform_secure_ddr_region(int rgn,paddr_t st,size_t sz)18 int platform_secure_ddr_region(int rgn, paddr_t st, size_t sz)
19 {
20 	vaddr_t sgrf_base = (vaddr_t)phys_to_virt_io(SGRF_BASE, SGRF_SIZE);
21 	paddr_t ed = st + sz;
22 	uint32_t st_mb = st / SIZE_M(1);
23 	uint32_t ed_mb = ed / SIZE_M(1);
24 
25 	if (!sgrf_base)
26 		panic();
27 
28 	assert(rgn <= 7);
29 	assert(st < ed);
30 
31 	/* Check aligned 1MB */
32 	assert(st % SIZE_M(1) == 0);
33 	assert(ed % SIZE_M(1) == 0);
34 
35 	DMSG("protecting region %d: 0x%lx-0x%lx\n", rgn, st, ed);
36 
37 	/* Set ddr region addr start */
38 	io_write32(sgrf_base + SGRF_DDRRGN_CON0_16(rgn),
39 		   BITS_WITH_WMASK(st_mb, SGRF_DDR_RGN_0_16_WMSK, 0));
40 
41 	/* Set ddr region addr end */
42 	io_write32(sgrf_base + SGRF_DDRRGN_CON0_16(rgn + 8),
43 		   BITS_WITH_WMASK((ed_mb - 1), SGRF_DDR_RGN_0_16_WMSK, 0));
44 
45 	io_write32(sgrf_base + SGRF_DDRRGN_CON0_16(16),
46 		   BIT_WITH_WMSK(rgn));
47 
48 	return 0;
49 }
50