1 /* Copyright(c) 2022 Intel Corporation. All rights reserved.
2  * SPDX-License-Identifier: Apache-2.0
3  */
4 
5 #include <zephyr/cache.h>
6 #include <zephyr/device.h>
7 #include <zephyr/devicetree.h>
8 #include <zephyr/arch/cpu.h>
9 
10 #include <adsp_memory.h>
11 #include <adsp_shim.h>
12 #include <mem_window.h>
13 #include <soc_util.h>
14 
15 /* host windows */
16 #define DMWBA(win_base) (win_base + 0x0)
17 #define DMWLO(win_base) (win_base + 0x4)
18 
19 #define DT_DRV_COMPAT intel_adsp_mem_window
20 
mem_win_init(const struct device * dev)21 __imr int mem_win_init(const struct device *dev)
22 {
23 	const struct mem_win_config *config = dev->config;
24 
25 	if (config->initialize) {
26 		bbzero((void *)config->mem_base, config->size);
27 	}
28 
29 	sys_write32(config->size | 0x7, DMWLO(config->base_addr));
30 	if (config->read_only) {
31 		sys_write32((config->mem_base | ADSP_DMWBA_READONLY | ADSP_DMWBA_ENABLE),
32 			    DMWBA(config->base_addr));
33 	} else {
34 		sys_write32((config->mem_base | ADSP_DMWBA_ENABLE), DMWBA(config->base_addr));
35 	}
36 
37 	return 0;
38 }
39 
mem_window_idle_exit(void)40 void mem_window_idle_exit(void)
41 {
42 	mem_win_init(DEVICE_DT_GET(MEM_WINDOW_NODE(0)));
43 	mem_win_init(DEVICE_DT_GET(MEM_WINDOW_NODE(1)));
44 	mem_win_init(DEVICE_DT_GET(MEM_WINDOW_NODE(2)));
45 	mem_win_init(DEVICE_DT_GET(MEM_WINDOW_NODE(3)));
46 }
47 
48 #define MEM_WINDOW_DEFINE(n)                                                                       \
49 	static const struct mem_win_config mem_win_config_##n = {                                  \
50 		.base_addr = DT_REG_ADDR(MEM_WINDOW_NODE(n)),                                      \
51 		.size = WIN_SIZE(n),                                                               \
52 		.offset = WIN_OFFSET(n),                                                           \
53 		.read_only = DT_PROP(MEM_WINDOW_NODE(n), read_only),                               \
54 		.mem_base = DT_REG_ADDR(DT_PHANDLE(MEM_WINDOW_NODE(n), memory)) + WIN_OFFSET(n),   \
55 		.initialize = DT_PROP(MEM_WINDOW_NODE(n), initialize),                             \
56 	};                                                                                         \
57 	DEVICE_DT_DEFINE(MEM_WINDOW_NODE(n), mem_win_init, NULL, NULL,                             \
58 			&mem_win_config_##n, PRE_KERNEL_1,                                         \
59 			CONFIG_KERNEL_INIT_PRIORITY_DEFAULT, NULL);
60 
61 #if DT_NODE_HAS_STATUS(MEM_WINDOW_NODE(0), okay)
62 MEM_WINDOW_DEFINE(0)
63 #endif
64 #if DT_NODE_HAS_STATUS(MEM_WINDOW_NODE(1), okay)
65 MEM_WINDOW_DEFINE(1)
66 #endif
67 #if DT_NODE_HAS_STATUS(MEM_WINDOW_NODE(2), okay)
68 MEM_WINDOW_DEFINE(2)
69 #endif
70 #if DT_NODE_HAS_STATUS(MEM_WINDOW_NODE(3), okay)
71 MEM_WINDOW_DEFINE(3)
72 #endif
73