1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements.  See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership.  The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License.  You may obtain a copy of the License at
9  *
10  *  http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied.  See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  */
19 
20 #include <string.h>
21 #include <assert.h>
22 #include <DA1469xAB.h>
23 #include <da1469x_config.h>
24 #include <zephyr/sys/util.h>
25 #include <system_DA1469x.h>
26 
27 enum REMAP_ADDR0 {
28 	REMAP_ADDR0_ROM = 0x0,
29 	REMAP_ADDR0_OTP,
30 	REMAP_ADDR0_QSPIF_CACHED,
31 	REMAP_ADDR0_SYSRAM,
32 	REMAP_ADDR0_MAX,
33 };
34 
black_orca_phy_addr(uint32_t addr)35 uint32_t black_orca_phy_addr(uint32_t addr)
36 {
37 	uint32_t remap_addr0;
38 	uint32_t phy_addr;
39 
40 	static const uint32_t remap_bases[] = {
41 		[REMAP_ADDR0_ROM] = MCU_ROM_BASE,
42 		[REMAP_ADDR0_OTP] = MCU_OTP_M_BASE,
43 		[REMAP_ADDR0_QSPIF_CACHED] = MCU_QSPIF_M_CACHED_BASE,
44 		[REMAP_ADDR0_SYSRAM] = MCU_SYSRAM_M_BASE
45 	};
46 
47 	static const uint32_t flash_region_sizes[] = {
48 		MB(32),
49 		MB(16),
50 		MB(8),
51 		MB(4),
52 		MB(2),
53 		MB(1),
54 		KB(512),
55 		KB(256)
56 	};
57 
58 	remap_addr0 = CRG_TOP->SYS_CTRL_REG & CRG_TOP_SYS_CTRL_REG_REMAP_ADR0_Msk;
59 	assert(remap_addr0 < REMAP_ADDR0_MAX);
60 
61 	if (remap_addr0 != REMAP_ADDR0_QSPIF_CACHED) {
62 		if (IS_REMAPPED_ADDRESS(addr)) {
63 			phy_addr = addr + remap_bases[remap_addr0];
64 		} else {
65 			phy_addr = addr;
66 		}
67 	} else {
68 		/* FLASH_REGION_BASE corresponds to Flash region base address bits [31:16] */
69 		uint32_t flash_region_base = ((CACHE->CACHE_FLASH_REG & CACHE_CACHE_FLASH_REG_FLASH_REGION_BASE_Msk) >>
70 																CACHE_CACHE_FLASH_REG_FLASH_REGION_BASE_Pos) << 16;
71 		/* FLASH_REGION_OFFSET corresponds to Flash region base address bits [13:2]. Offset is expressed in words. */
72 		flash_region_base += ((CACHE->CACHE_FLASH_REG & CACHE_CACHE_FLASH_REG_FLASH_REGION_OFFSET_Msk) >>
73 														CACHE_CACHE_FLASH_REG_FLASH_REGION_OFFSET_Pos) << 2;
74 		__unused uint32_t flash_region_size = flash_region_sizes[CACHE->CACHE_FLASH_REG & CACHE_CACHE_FLASH_REG_FLASH_REGION_SIZE_Msk];
75 
76 		if (IS_REMAPPED_ADDRESS(addr)) {
77 			assert(addr < flash_region_size);
78 
79 			phy_addr = addr + flash_region_base;
80 		} else if (IS_QSPIF_CACHED_ADDRESS(addr)) {
81 			assert(addr >= flash_region_base && addr < (flash_region_base + flash_region_size));
82 
83 			phy_addr = addr;
84 		} else {
85 			phy_addr = addr;
86 		}
87 	}
88 
89 	return phy_addr;
90 }