1 /*
2  * Copyright (c) 2022 Intel Corporation
3  * SPDX-License-Identifier: Apache-2.0
4  */
5 
6 #include <zephyr/device.h>
7 #include <zephyr/init.h>
8 #include <zephyr/kernel.h>
9 #include <zephyr/sys/winstream.h>
10 #include <zephyr/devicetree.h>
11 
12 #include <adsp_memory.h>
13 #include <mem_window.h>
14 #include <soc.h>
15 
16 struct k_spinlock trace_lock;
17 
18 static struct sys_winstream *winstream;
19 
winstream_console_trace_out(int8_t * str,size_t len)20 void winstream_console_trace_out(int8_t *str, size_t len)
21 {
22 	if (len == 0) {
23 		return;
24 	}
25 
26 #ifdef CONFIG_ADSP_TRACE_SIMCALL
27 	register int a2 __asm__("a2") = 4; /* SYS_write */
28 	register int a3 __asm__("a3") = 1; /* fd 1 == stdout */
29 	register int a4 __asm__("a4") = (int)str;
30 	register int a5 __asm__("a5") = len;
31 
32 	__asm__ volatile("simcall" : "+r"(a2), "+r"(a3) : "r"(a4), "r"(a5) : "memory");
33 #endif
34 
35 	k_spinlock_key_t key = k_spin_lock(&trace_lock);
36 
37 	sys_winstream_write(winstream, str, len);
38 	k_spin_unlock(&trace_lock, key);
39 }
40 
arch_printk_char_out(int c)41 int arch_printk_char_out(int c)
42 {
43 	int8_t s = c;
44 
45 	winstream_console_trace_out(&s, 1);
46 	return 0;
47 }
48 
winstream_console_init(void)49 static int winstream_console_init(void)
50 {
51 	const struct device *dev = DEVICE_DT_GET(DT_CHOSEN(zephyr_console));
52 
53 	if (!device_is_ready(dev)) {
54 		return -ENODEV;
55 	}
56 	const struct mem_win_config *config = dev->config;
57 	void *buf =
58 		arch_xtensa_uncached_ptr((__sparse_force void __sparse_cache *)config->mem_base);
59 
60 	winstream = sys_winstream_init(buf, config->size);
61 
62 	return 0;
63 }
64 
65 SYS_INIT(winstream_console_init, PRE_KERNEL_1, CONFIG_CONSOLE_INIT_PRIORITY);
66