1 /*
2  * File      : mbox.c
3  * Copyright (c) 2006-2021, RT-Thread Development Team
4  *
5  * SPDX-License-Identifier: Apache-2.0
6  *
7  * Change Logs:
8  * Date           Author         Notes
9  * 2019-08-29     zdzn           first version
10  */
11 
12 /* mailbox message buffer */
13 #include "mbox.h"
14 #include "mmu.h"
15 
16 volatile unsigned int *mbox = (volatile unsigned int *) MBOX_ADDR;
17 /**
18  * Make a mailbox call. Returns 0 on failure, non-zero on success
19  */
init_mbox_mmu_map()20 void init_mbox_mmu_map()
21 {
22     rt_hw_change_mmu_table(MBOX_ADDR, 96, MBOX_ADDR, STRONG_ORDER_MEM);
23 }
24 
mbox_call(unsigned char ch,int mmu_enable)25 int mbox_call(unsigned char ch, int mmu_enable)
26 {
27     unsigned int r = (((MBOX_ADDR)&~0xF) | (ch&0xF));
28     if (mmu_enable)
29         r = BUS_ADDRESS(r);
30     /* wait until we can write to the mailbox */
31     do
32     {
33         asm volatile("nop");
34     } while (*MBOX_STATUS & MBOX_FULL);
35     /* write the address of our message to the mailbox with channel identifier */
36     *MBOX_WRITE = r;
37     /* now wait for the response */
38     while (1)
39     {
40         /* is there a response? */
41         do
42         {
43             asm volatile("nop");
44         }
45         while (*MBOX_STATUS & MBOX_EMPTY);
46         /* is it a response to our message? */
47         if (r == *MBOX_READ)
48         {
49             /* is it a valid successful response? */
50             return mbox[1] == MBOX_RESPONSE;
51         }
52     }
53     return 0;
54 }
55