1 /*
2  * Copyright (c) 2006-2021, RT-Thread Development Team
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  *
6  * Change Logs:
7  * Date           Author         Notes
8  * 2020-09-10     bigmagic       first version
9  */
10 
11 #ifndef __MBOX_H__
12 #define __MBOX_H__
13 
14 #include <rtthread.h>
15 #include "board.h"
16 
17 //https://github.com/raspberrypi/firmware/wiki/Mailbox-property-interface
18 //https://github.com/hermanhermitage/videocoreiv
19 
20 /* a properly aligned buffer */
21 extern volatile unsigned int* mbox;
22 
23 #define MBOX_REQUEST    0
24 
25 /* channels */
26 #define MBOX_CH_POWER   0
27 #define MBOX_CH_FB      1
28 #define MBOX_CH_VUART   2
29 #define MBOX_CH_VCHIQ   3
30 #define MBOX_CH_LEDS    4
31 #define MBOX_CH_BTNS    5
32 #define MBOX_CH_TOUCH   6
33 #define MBOX_CH_COUNT   7
34 #define MBOX_CH_PROP    8
35 
36 /* tags */
37 #define MBOX_TAG_SETPOWER       0x28001
38 #define MBOX_TAG_SETCLKRATE     0x38002
39 #define MBOX_GET_MAC_ADDRESS    0x10003
40 #define MBOX_TAG_LAST           0
41 
42 #define MMIO_BASE       0xFE000000
43 #define VIDEOCORE_MBOX  (MMIO_BASE+0x0000B880)
44 #define MBOX_READ       ((volatile unsigned int*)(VIDEOCORE_MBOX+0x0))
45 #define MBOX_POLL       ((volatile unsigned int*)(VIDEOCORE_MBOX+0x10))
46 #define MBOX_SENDER     ((volatile unsigned int*)(VIDEOCORE_MBOX+0x14))
47 #define MBOX_STATUS     ((volatile unsigned int*)(VIDEOCORE_MBOX+0x18))
48 #define MBOX_CONFIG     ((volatile unsigned int*)(VIDEOCORE_MBOX+0x1C))
49 #define MBOX_WRITE      ((volatile unsigned int*)(VIDEOCORE_MBOX+0x20))
50 #define MBOX_RESPONSE   0x80000000
51 #define MBOX_FULL       0x80000000
52 #define MBOX_EMPTY      0x40000000
53 
54 #define DEVICE_ID_SD_CARD        (0)
55 #define DEVICE_ID_USB_HCD        (3)
56 #define POWER_STATE_OFF          (0 << 0)
57 #define POWER_STATE_ON           (1 << 0)
58 #define POWER_STATE_WAIT         (1 << 1)
59 #define POWER_STATE_NO_DEVICE    (1 << 1)    // in response
60 #define MMU_ENABLE               (1)
61 #define MMU_DISABLE              (0)
62 
63 /*
64  * raspi hardware info
65  */
66 enum {
67     MBOX_TAG_HARDWARE_GET_MODEL         = 0x00010001,
68     MBOX_TAG_HARDWARE_GET_REV           = 0x00010002,
69     MBOX_TAG_HARDWARE_GET_MAC_ADDRESS   = 0x00010003,
70     MBOX_TAG_HARDWARE_GET_SERIAL        = 0x00010004,
71     MBOX_TAG_HARDWARE_GET_ARM_MEMORY    = 0x00010005,
72     MBOX_TAG_HARDWARE_GET_VC_MEMORY     = 0x00010006,
73     MBOX_TAG_HARDWARE_GET_CLOCKS        = 0x00010007,
74 };
75 
76 /*
77  * raspi clock
78  */
79 enum {
80     MBOX_TAG_CLOCK_GET_TURBO    = 0x00030009,
81     MBOX_TAG_CLOCK_SET_TURBO    = 0x00038009,
82     MBOX_TAG_CLOCK_GET_STATE    = 0x00030001,
83     MBOX_TAG_CLOCK_SET_STATE    = 0x00038001,
84     MBOX_TAG_CLOCK_GET_RATE     = 0x00030002,
85     MBOX_TAG_CLOCK_SET_RATE     = 0x00038002,
86     MBOX_TAG_CLOCK_GET_MAX_RATE = 0x00030004,
87     MBOX_TAG_CLOCK_GET_MIN_RATE = 0x00030007,
88 };
89 
90 /*
91  * raspi power
92  */
93 enum {
94     MBOX_TAG_POWER_GET_STATE    = 0x00020001,
95     MBOX_TAG_POWER_SET_STATE    = 0x00028001,
96 };
97 
98 /*
99  * raspi temperature
100  */
101 enum {
102     MBOX_TAG_TEMP_GET       = 0x00030006,
103     MBOX_TAG_TEMP_GET_MAX   = 0x0003000A,
104 };
105 
106 /*
107  * raspi Memory
108  */
109 enum {
110     MBOX_TAG_ALLOCATE_MEMORY = 0x0003000C, // Memory: Allocates Contiguous Memory On The GPU (Response: Handle)
111     MBOX_TAG_LOCK_MEMORY     = 0x0003000D, // Memory: Unlock Buffer (Response: Status)
112     MBOX_TAG_UNLOCK_MEMORY   = 0x0003000E, // Memory: Unlock Buffer (Response: Status)
113     MBOX_TAG_RELEASE_MEMORY  = 0x0003000F, // Memory: Free The Memory Buffer (Response: Status)
114     MBOX_TAG_EXECUTE_CODE    = 0x00030010, // Memory: Calls The Function At Given (Bus) Address And With Arguments Given
115 };
116 
117 /*
118  * raspi GPU
119  */
120 enum {
121     MBOX_TAG_EXECUTE_QPU = 0x00030011, // QPU: Calls The QPU Function At Given (Bus) Address And With Arguments Given (Response: Number Of QPUs, Control, No Flush, Timeout In ms)
122     MBOX_TAG_ENABLE_QPU  = 0x00030012, // QPU: Enables The QPU (Response: Enable State)
123 };
124 
125 /*
126  * raspi HDMI
127  */
128 #define MBOX_TAG_GET_EDID_BLOCK        0x00030020 // HDMI: Read Specificed EDID Block From Attached HDMI/DVI Device (Response: Block Number, Status, EDID Block (128 Bytes))
129 
130 /*
131  * raspi NOTIFY
132  */
133 #define MBOX_TAG_NOTIFY_REBOOT         0x00030048
134 #define MBOX_TAG_NOTIFY_XHCI_RESET     0x00030058
135 
136 /*
137 * touch
138 */
139 #define MBOX_TAG_GET_TOUCHBUF          (0x0004000F)
140 
141 #define MBOX_ADDR 0x08000000
142 
143 #define    RES_CLK_ID           (0x000000000)
144 #define    EMMC_CLK_ID          (0x000000001)
145 #define    UART_CLK_ID          (0x000000002)
146 #define    ARM_CLK_ID           (0x000000003)
147 #define    CORE_CLK_ID          (0x000000004)
148 #define    V3D_CLK_ID           (0x000000005)
149 #define    H264_CLK_ID          (0x000000006)
150 #define    ISP_CLK_ID           (0x000000007)
151 #define    SDRAM_CLK_ID         (0x000000008)
152 #define    PIXEL_CLK_ID         (0x000000009)
153 #define    PWM_CLK_ID           (0x00000000a)
154 
155 int mbox_call(unsigned char ch, int mmu_enable);
156 int bcm271x_mbox_get_touch(void);
157 int bcm271x_notify_reboot(void);
158 int bcm271x_notify_xhci_reset(void);
159 int bcm271x_gpu_enable(void);
160 int bcm271x_mbox_hardware_get_model(void);
161 int bcm271x_mbox_hardware_get_revison(void);
162 int bcm271x_mbox_hardware_get_mac_address(uint8_t * mac);
163 int bcm271x_mbox_hardware_get_serial(rt_uint64_t* sn);
164 int bcm271x_mbox_hardware_get_arm_memory(rt_uint32_t * base, rt_uint32_t * size);
165 int bcm271x_mbox_hardware_get_vc_memory(rt_uint32_t * base, rt_uint32_t * size);
166 int bcm271x_mbox_clock_get_turbo(void);
167 int bcm271x_mbox_clock_set_turbo(int level);
168 int bcm271x_mbox_clock_get_state(int id);
169 int bcm271x_mbox_clock_set_state(int id, int state);
170 int bcm271x_mbox_clock_get_rate(int id);
171 int bcm271x_mbox_clock_set_rate(int id, int rate);
172 int bcm271x_mbox_clock_get_max_rate(int id);
173 int bcm271x_mbox_clock_get_min_rate(int id);
174 int bcm271x_mbox_power_get_state(int id);
175 int bcm271x_mbox_power_set_state(int id, int state);
176 int bcm271x_mbox_temp_get(void);
177 int bcm271x_mbox_temp_get_max(void);
178 
179 #endif
180