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