1 /* Copyright 2023 The ChromiumOS Authors
2 * SPDX-License-Identifier: Apache-2.0
3 */
4 #include <stdlib.h>
5 #include <zephyr/kernel.h>
6 #include <zephyr/ztest.h>
7 #include <soc.h>
8
9 /* Simple test of SOC-specific hardware on the MediaTek Audio DSP
10 * family. Right now just CPU speed and host mailbox interrupts.
11 */
12
13 #define NOM_HZ CONFIG_SYS_CLOCK_HW_CYCLES_PER_SEC
14
ccount(void)15 static uint32_t ccount(void)
16 {
17 uint32_t t;
18
19 __asm__ volatile("rsr %0, CCOUNT" : "=r"(t));
20 return t;
21 }
22
cpu_hz(void)23 static uint32_t cpu_hz(void)
24 {
25 uint32_t t0 = k_cycle_get_32(), cc0 = ccount();
26
27 k_msleep(100);
28
29 uint32_t t1 = k_cycle_get_32(), cc1 = ccount();
30 uint32_t hz = ((uint64_t)cc1 - cc0) * NOM_HZ / (t1 - t0);
31
32 printk("(measured %u Hz CPU clock vs. %d Hz timer)\n", hz, NOM_HZ);
33 return hz;
34 }
35
ZTEST(mtk_adsp,cpu_freq)36 ZTEST(mtk_adsp, cpu_freq)
37 {
38 #ifdef CONFIG_SOC_SERIES_MT8195
39 int freqs[] = { 26, 370, 540, 720 };
40
41 for (int i = 0; i < ARRAY_SIZE(freqs); i++) {
42 printk("Checking CPU freq entry %d (expect %d MHz)\n", i, freqs[i]);
43 mtk_adsp_set_cpu_freq(freqs[i]);
44
45 /* Compute error as an inverse, i.e. "one part in":
46 * 100 means 1% off, 1000 is 0.1%, etc...
47 */
48 uint32_t hz0 = 1000000 * freqs[i], hz = cpu_hz();
49 int32_t err = hz0 / abs((int32_t)(hz0 - hz));
50
51 zassert_true(err > 200);
52 }
53 #else
54 (void)cpu_hz();
55 #endif
56 }
57
58 #define MBOX0 DEVICE_DT_GET(DT_INST(0, mediatek_mbox))
59 #define MBOX1 DEVICE_DT_GET(DT_INST(1, mediatek_mbox))
60
61 K_SEM_DEFINE(mbox_sem, 0, 1);
62 static bool mbox1_fired;
63
mbox_fn(const struct device * mbox,void * arg)64 static void mbox_fn(const struct device *mbox, void *arg)
65 {
66 zassert_equal(arg, NULL);
67 mbox1_fired = true;
68 k_sem_give(&mbox_sem);
69 }
70
71 /* Test in/out interrupts from the host. This relies on a SOF driver
72 * on the host, which has the behavior of "replying" with an interrupt
73 * on mbox1 after receiving a "command" on mbox0 (you can also see it
74 * whine about the invalid IPC message in the kernel logs).
75 *
76 * Note that there's a catch: on older kernels, SOF's "reply" comes
77 * after a timeout (it's an invalid command, afterall) which is 165
78 * seconds! But the test does pass.
79 */
ZTEST(mtk_adsp,mbox)80 ZTEST(mtk_adsp, mbox)
81 {
82 /* Different SOCs transmit the replies on different devices! Just listen to both */
83 mtk_adsp_mbox_set_handler(MBOX0, 1, mbox_fn, NULL);
84 mtk_adsp_mbox_set_handler(MBOX1, 1, mbox_fn, NULL);
85
86 /* First signal the host with a reply on the second channel,
87 * that effects a reply to anything it thinks it might have
88 * sent us
89 */
90 mtk_adsp_mbox_signal(MBOX1, 1);
91
92 mtk_adsp_mbox_signal(MBOX0, 0);
93
94 printk("Waiting for reply from SOF driver, be patient: long timeout...\n");
95 k_sem_take(&mbox_sem, K_FOREVER);
96 zassert_true(mbox1_fired);
97 }
98
mtk_adsp_setup(void)99 static void *mtk_adsp_setup(void)
100 {
101 return NULL;
102 }
103
104 ZTEST_SUITE(mtk_adsp, NULL, mtk_adsp_setup, NULL, NULL, NULL);
105