1 /*-
2  * Copyright (C) 2013-2014 Daisuke Aoyama <aoyama@peach.ne.jp>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26 
27 #ifndef _BCM2835_MBOX_PROP_H_
28 #define _BCM2835_MBOX_PROP_H_
29 
30 #include <sys/cdefs.h>
31 #include <sys/types.h>
32 
33 /*
34  * Mailbox property interface:
35  * https://github.com/raspberrypi/firmware/wiki/Mailbox-property-interface
36  */
37 #define BCM2835_MBOX_CODE_REQ			0
38 #define BCM2835_MBOX_CODE_RESP_SUCCESS		0x80000000
39 #define BCM2835_MBOX_CODE_RESP_ERROR		0x80000001
40 #define BCM2835_MBOX_TAG_VAL_LEN_RESPONSE	0x80000000
41 
42 struct bcm2835_mbox_hdr {
43 	uint32_t	buf_size;
44 	uint32_t	code;
45 };
46 
47 struct bcm2835_mbox_tag_hdr {
48 	uint32_t	tag;
49 	uint32_t	val_buf_size;
50 	uint32_t	val_len;
51 };
52 
53 #define	BCM2835_MBOX_INIT_TAG(tag_, tagid_) do {		\
54 	(tag_)->tag_hdr.tag = BCM2835_MBOX_TAG_##tagid_;	\
55 	(tag_)->tag_hdr.val_buf_size = sizeof((tag_)->body);	\
56 	(tag_)->tag_hdr.val_len = sizeof((tag_)->body.req);	\
57 } while (0)
58 
59 #define BCM2835_MBOX_TAG_FIRMWARE_REVISION	0x00000001
60 
61 #define BCM2835_MBOX_POWER_ID_EMMC		0x00000000
62 #define BCM2835_MBOX_POWER_ID_UART0		0x00000001
63 #define BCM2835_MBOX_POWER_ID_UART1		0x00000002
64 #define BCM2835_MBOX_POWER_ID_USB_HCD		0x00000003
65 #define BCM2835_MBOX_POWER_ID_I2C0		0x00000004
66 #define BCM2835_MBOX_POWER_ID_I2C1		0x00000005
67 #define BCM2835_MBOX_POWER_ID_I2C2		0x00000006
68 #define BCM2835_MBOX_POWER_ID_SPI		0x00000007
69 #define BCM2835_MBOX_POWER_ID_CCP2TX		0x00000008
70 
71 #define BCM2835_MBOX_POWER_ON			(1 << 0)
72 #define BCM2835_MBOX_POWER_WAIT			(1 << 1)
73 
74 #define BCM2835_MBOX_TAG_GET_POWER_STATE	0x00020001
75 #define BCM2835_MBOX_TAG_SET_POWER_STATE	0x00028001
76 
77 struct msg_get_power_state {
78 	struct bcm2835_mbox_hdr hdr;
79 	struct bcm2835_mbox_tag_hdr tag_hdr;
80 	union {
81 		struct {
82 			uint32_t device_id;
83 		} req;
84 		struct {
85 			uint32_t device_id;
86 			uint32_t state;
87 		} resp;
88 	} body;
89 	uint32_t end_tag;
90 };
91 
92 struct msg_set_power_state {
93 	struct bcm2835_mbox_hdr hdr;
94 	struct bcm2835_mbox_tag_hdr tag_hdr;
95 	union {
96 		struct {
97 			uint32_t device_id;
98 			uint32_t state;
99 		} req;
100 		struct {
101 			uint32_t device_id;
102 			uint32_t state;
103 		} resp;
104 	} body;
105 	uint32_t end_tag;
106 };
107 
108 /* Sets the power state for a given device */
109 int bcm2835_mbox_set_power_state(uint32_t, boolean_t);
110 
111 #define BCM2835_MBOX_TAG_NOTIFY_XHCI_RESET	0x00030058
112 
113 struct msg_notify_xhci_reset {
114 	struct bcm2835_mbox_hdr hdr;
115 	struct bcm2835_mbox_tag_hdr tag_hdr;
116 	union {
117 		struct {
118 			uint32_t pci_device_addr;
119 		} req;
120 		struct {
121 		} resp;
122 	} body;
123 	uint32_t end_tag;
124 };
125 
126 /* Prompts the VideoCore processor to reload the xhci firmware. */
127 int bcm2835_mbox_notify_xhci_reset(uint32_t);
128 
129 #define BCM2835_MBOX_CLOCK_ID_EMMC		0x00000001
130 #define BCM2838_MBOX_CLOCK_ID_EMMC2		0x0000000c
131 
132 #define BCM2835_MBOX_TAG_GET_CLOCK_RATE		0x00030002
133 
134 struct msg_get_clock_rate {
135 	struct bcm2835_mbox_hdr hdr;
136 	struct bcm2835_mbox_tag_hdr tag_hdr;
137 	union {
138 		struct {
139 			uint32_t clock_id;
140 		} req;
141 		struct {
142 			uint32_t clock_id;
143 			uint32_t rate_hz;
144 		} resp;
145 	} body;
146 	uint32_t end_tag;
147 };
148 
149 int bcm2835_mbox_get_clock_rate(uint32_t, uint32_t *);
150 
151 #define BCM2835_MBOX_TURBO_ON			1
152 #define BCM2835_MBOX_TURBO_OFF			0
153 
154 #define BCM2835_MBOX_TAG_GET_TURBO		0x00030009
155 #define BCM2835_MBOX_TAG_SET_TURBO		0x00038009
156 
157 struct msg_get_turbo {
158 	struct bcm2835_mbox_hdr hdr;
159 	struct bcm2835_mbox_tag_hdr tag_hdr;
160 	union {
161 		struct {
162 			uint32_t id;
163 		} req;
164 		struct {
165 			uint32_t id;
166 			uint32_t level;
167 		} resp;
168 	} body;
169 	uint32_t end_tag;
170 };
171 
172 struct msg_set_turbo {
173 	struct bcm2835_mbox_hdr hdr;
174 	struct bcm2835_mbox_tag_hdr tag_hdr;
175 	union {
176 		struct {
177 			uint32_t id;
178 			uint32_t level;
179 		} req;
180 		struct {
181 			uint32_t id;
182 			uint32_t level;
183 		} resp;
184 	} body;
185 	uint32_t end_tag;
186 };
187 
188 #define BCM2835_MBOX_VOLTAGE_ID_CORE		0x00000001
189 #define BCM2835_MBOX_VOLTAGE_ID_SDRAM_C		0x00000002
190 #define BCM2835_MBOX_VOLTAGE_ID_SDRAM_P		0x00000003
191 #define BCM2835_MBOX_VOLTAGE_ID_SDRAM_I		0x00000004
192 
193 #define BCM2835_MBOX_TAG_GET_VOLTAGE		0x00030003
194 #define BCM2835_MBOX_TAG_SET_VOLTAGE		0x00038003
195 #define BCM2835_MBOX_TAG_GET_MAX_VOLTAGE	0x00030005
196 #define BCM2835_MBOX_TAG_GET_MIN_VOLTAGE	0x00030008
197 
198 struct msg_get_voltage {
199 	struct bcm2835_mbox_hdr hdr;
200 	struct bcm2835_mbox_tag_hdr tag_hdr;
201 	union {
202 		struct {
203 			uint32_t voltage_id;
204 		} req;
205 		struct {
206 			uint32_t voltage_id;
207 			uint32_t value;
208 		} resp;
209 	} body;
210 	uint32_t end_tag;
211 };
212 
213 struct msg_set_voltage {
214 	struct bcm2835_mbox_hdr hdr;
215 	struct bcm2835_mbox_tag_hdr tag_hdr;
216 	union {
217 		struct {
218 			uint32_t voltage_id;
219 			uint32_t value;
220 		} req;
221 		struct {
222 			uint32_t voltage_id;
223 			uint32_t value;
224 		} resp;
225 	} body;
226 	uint32_t end_tag;
227 };
228 
229 struct msg_get_max_voltage {
230 	struct bcm2835_mbox_hdr hdr;
231 	struct bcm2835_mbox_tag_hdr tag_hdr;
232 	union {
233 		struct {
234 			uint32_t voltage_id;
235 		} req;
236 		struct {
237 			uint32_t voltage_id;
238 			uint32_t value;
239 		} resp;
240 	} body;
241 	uint32_t end_tag;
242 };
243 
244 struct msg_get_min_voltage {
245 	struct bcm2835_mbox_hdr hdr;
246 	struct bcm2835_mbox_tag_hdr tag_hdr;
247 	union {
248 		struct {
249 			uint32_t voltage_id;
250 		} req;
251 		struct {
252 			uint32_t voltage_id;
253 			uint32_t value;
254 		} resp;
255 	} body;
256 	uint32_t end_tag;
257 };
258 
259 #define BCM2835_MBOX_TAG_GET_TEMPERATURE	0x00030006
260 #define BCM2835_MBOX_TAG_GET_MAX_TEMPERATURE	0x0003000a
261 
262 struct msg_get_temperature {
263 	struct bcm2835_mbox_hdr hdr;
264 	struct bcm2835_mbox_tag_hdr tag_hdr;
265 	union {
266 		struct {
267 			uint32_t temperature_id;
268 		} req;
269 		struct {
270 			uint32_t temperature_id;
271 			uint32_t value;
272 		} resp;
273 	} body;
274 	uint32_t end_tag;
275 };
276 
277 struct msg_get_max_temperature {
278 	struct bcm2835_mbox_hdr hdr;
279 	struct bcm2835_mbox_tag_hdr tag_hdr;
280 	union {
281 		struct {
282 			uint32_t temperature_id;
283 		} req;
284 		struct {
285 			uint32_t temperature_id;
286 			uint32_t value;
287 		} resp;
288 	} body;
289 	uint32_t end_tag;
290 };
291 
292 #define	BCM2835_MBOX_TAG_GET_PHYSICAL_W_H	0x00040003
293 #define	BCM2835_MBOX_TAG_SET_PHYSICAL_W_H	0x00048003
294 #define	BCM2835_MBOX_TAG_GET_VIRTUAL_W_H	0x00040004
295 #define	BCM2835_MBOX_TAG_SET_VIRTUAL_W_H	0x00048004
296 
297 struct bcm2835_mbox_tag_fb_w_h {
298 	struct bcm2835_mbox_tag_hdr tag_hdr;
299 	union {
300 		struct {
301 			uint32_t width;
302 			uint32_t height;
303 		} req;
304 		struct {
305 			uint32_t width;
306 			uint32_t height;
307 		} resp;
308 	} body;
309 };
310 
311 #define	BCM2835_MBOX_TAG_GET_DEPTH		0x00040005
312 #define	BCM2835_MBOX_TAG_SET_DEPTH		0x00048005
313 
314 struct bcm2835_mbox_tag_depth {
315 	struct bcm2835_mbox_tag_hdr tag_hdr;
316 	union {
317 		struct {
318 			uint32_t bpp;
319 		} req;
320 		struct {
321 			uint32_t bpp;
322 		} resp;
323 	} body;
324 };
325 
326 #define	BCM2835_MBOX_TAG_GET_ALPHA_MODE		0x00040007
327 #define	BCM2835_MBOX_TAG_SET_ALPHA_MODE		0x00048007
328 
329 #define	BCM2835_MBOX_ALPHA_MODE_0_OPAQUE	0
330 #define	BCM2835_MBOX_ALPHA_MODE_0_TRANSPARENT	1
331 #define	BCM2835_MBOX_ALPHA_MODE_IGNORED		2
332 
333 struct bcm2835_mbox_tag_alpha_mode {
334 	struct bcm2835_mbox_tag_hdr tag_hdr;
335 	union {
336 		struct {
337 			uint32_t alpha;
338 		} req;
339 		struct {
340 			uint32_t alpha;
341 		} resp;
342 	} body;
343 };
344 
345 #define	BCM2835_MBOX_TAG_GET_VIRTUAL_OFFSET	0x00040009
346 #define	BCM2835_MBOX_TAG_SET_VIRTUAL_OFFSET	0x00048009
347 
348 struct bcm2835_mbox_tag_virtual_offset {
349 	struct bcm2835_mbox_tag_hdr tag_hdr;
350 	union {
351 		struct {
352 			uint32_t x;
353 			uint32_t y;
354 		} req;
355 		struct {
356 			uint32_t x;
357 			uint32_t y;
358 		} resp;
359 	} body;
360 };
361 
362 #define	BCM2835_MBOX_TAG_GET_PITCH		0x00040008
363 
364 struct bcm2835_mbox_tag_pitch {
365 	struct bcm2835_mbox_tag_hdr tag_hdr;
366 	union {
367 		struct {
368 		} req;
369 		struct {
370 			uint32_t pitch;
371 		} resp;
372 	} body;
373 };
374 
375 #define	BCM2835_MBOX_TAG_ALLOCATE_BUFFER	0x00040001
376 
377 struct bcm2835_mbox_tag_allocate_buffer {
378 	struct bcm2835_mbox_tag_hdr tag_hdr;
379 	union {
380 		struct {
381 			uint32_t alignment;
382 		} req;
383 		struct {
384 			uint32_t fb_address;
385 			uint32_t fb_size;
386 		} resp;
387 	} body;
388 };
389 
390 #define	BCM2835_MBOX_TAG_RELEASE_BUFFER		0x00048001
391 
392 struct bcm2835_mbox_tag_release_buffer {
393 	struct bcm2835_mbox_tag_hdr tag_hdr;
394 	union {
395 		struct {
396 		} req;
397 		struct {
398 		} resp;
399 	} body;
400 };
401 
402 #define	BCM2835_MBOX_TAG_GET_TOUCHBUF		0x0004000f
403 
404 struct bcm2835_mbox_tag_touchbuf {
405 	struct bcm2835_mbox_hdr hdr;
406 	struct bcm2835_mbox_tag_hdr tag_hdr;
407 	union {
408 		struct {
409 		} req;
410 		struct {
411 			uint32_t address;
412 		} resp;
413 	} body;
414 	uint32_t end_tag;
415 };
416 
417 struct bcm2835_fb_config {
418 	uint32_t xres;
419 	uint32_t yres;
420 	uint32_t vxres;
421 	uint32_t vyres;
422 	uint32_t xoffset;
423 	uint32_t yoffset;
424 	uint32_t bpp;
425 	uint32_t pitch;
426 	uint32_t base;
427 	uint32_t size;
428 };
429 
430 struct msg_fb_get_w_h {
431 	struct bcm2835_mbox_hdr hdr;
432 	struct bcm2835_mbox_tag_fb_w_h physical_w_h;
433 	uint32_t end_tag;
434 };
435 
436 int bcm2835_mbox_fb_get_w_h(struct bcm2835_fb_config *);
437 
438 struct msg_fb_get_bpp {
439 	struct bcm2835_mbox_hdr hdr;
440 	struct bcm2835_mbox_tag_depth bpp;
441 	uint32_t end_tag;
442 };
443 
444 int bcm2835_mbox_fb_get_bpp(struct bcm2835_fb_config *);
445 
446 struct msg_fb_setup {
447 	struct bcm2835_mbox_hdr hdr;
448 	struct bcm2835_mbox_tag_fb_w_h physical_w_h;
449 	struct bcm2835_mbox_tag_fb_w_h virtual_w_h;
450 	struct bcm2835_mbox_tag_virtual_offset offset;
451 	struct bcm2835_mbox_tag_depth depth;
452 	struct bcm2835_mbox_tag_alpha_mode alpha;
453 	struct bcm2835_mbox_tag_allocate_buffer buffer;
454 	struct bcm2835_mbox_tag_pitch pitch;
455 	uint32_t end_tag;
456 };
457 
458 int bcm2835_mbox_fb_init(struct bcm2835_fb_config *);
459 
460 int bcm2835_mbox_property(void *, size_t);
461 
462 #endif /* _BCM2835_MBOX_PROP_H_ */
463