fb.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /* Copyright (C) 2013 by John Cronin <jncronin@tysos.org>
  2. *
  3. * Permission is hereby granted, free of charge, to any person obtaining a copy
  4. * of this software and associated documentation files (the "Software"), to deal
  5. * in the Software without restriction, including without limitation the rights
  6. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. * copies of the Software, and to permit persons to whom the Software is
  8. * furnished to do so, subject to the following conditions:
  9. * The above copyright notice and this permission notice shall be included in
  10. * all copies or substantial portions of the Software.
  11. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  14. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  17. * THE SOFTWARE.
  18. */
  19. #include <stdio.h>
  20. #include <stdint.h>
  21. #include <string.h>
  22. #include "mbox.h"
  23. #include "fb.h"
  24. #define WIDTH 640
  25. #define HEIGHT 480
  26. #define BYTES_PER_PIXEL 2
  27. #define BPP (BYTES_PER_PIXEL << 3)
  28. #define S_PITCH (WIDTH * BYTES_PER_PIXEL) // The pitch of the backbuffer
  29. #define TAG_ALLOCATE_BUFFER 0x40001
  30. #define TAG_RELEASE_BUFFER 0x48001
  31. #define TAG_BLANK_SCREEN 0x40002
  32. #define TAG_GET_PHYS_WH 0x40003
  33. #define TAG_TEST_PHYS_WH 0x44003
  34. #define TAG_SET_PHYS_WH 0x48003
  35. #define TAG_GET_VIRT_WH 0x40004
  36. #define TAG_TEST_VIRT_WH 0x44004
  37. #define TAG_SET_VIRT_WH 0x48004
  38. #define TAG_GET_DEPTH 0x40005
  39. #define TAG_TEST_DEPTH 0x44005
  40. #define TAG_SET_DEPTH 0x48005
  41. #define TAG_GET_PIXEL_ORDER 0x40006
  42. #define TAG_TEST_PIXEL_ORDER 0x44006
  43. #define TAG_SET_PIXEL_ORDER 0x48006
  44. #define TAG_GET_ALPHA_MODE 0x40007
  45. #define TAG_TEST_ALPHA_MODE 0x44007
  46. #define TAG_SET_ALPHA_MODE 0x48007
  47. #define TAG_GET_PITCH 0x40008
  48. #define TAG_GET_VIRT_OFFSET 0x40009
  49. #define TAG_TEST_VIRT_OFFSET 0x44009
  50. #define TAG_SET_VIRT_OFFSET 0x48009
  51. #define TAG_GET_OVERSCAN 0x4000a
  52. #define TAG_TEST_OVERSCAN 0x4400a
  53. #define TAG_SET_OVERSCAN 0x4800a
  54. #define TAG_GET_PALETTE 0x4000b
  55. #define TAG_TEST_PALETTE 0x4400b
  56. #define TAG_SET_PALETTE 0x4800b
  57. static uint32_t phys_w, phys_h, virt_w, virt_h, pitch;
  58. static uint32_t fb_addr, fb_size;
  59. int fb_init()
  60. {
  61. // define a mailbox buffer
  62. uint32_t mb_addr = 0x40007000; // 0x7000 in L2 cache coherent mode
  63. volatile uint32_t *mailbuffer = (uint32_t *)mb_addr;
  64. /* Get the display size */
  65. // set up the buffer
  66. mailbuffer[0] = 8 * 4; // size of this message
  67. mailbuffer[1] = 0; // this is a request
  68. // next comes the first tag
  69. mailbuffer[2] = TAG_GET_PHYS_WH; // get physical width/height tag
  70. mailbuffer[3] = 0x8; // value buffer size
  71. mailbuffer[4] = 0; // request/response
  72. mailbuffer[5] = 0; // space to return width
  73. mailbuffer[6] = 0; // space to return height
  74. // closing tag
  75. mailbuffer[7] = 0;
  76. // send the message
  77. mbox_write(MBOX_PROP, mb_addr);
  78. // read the response
  79. mbox_read(MBOX_PROP);
  80. /* Check for a valid response */
  81. if(mailbuffer[1] != MBOX_SUCCESS)
  82. return FB_FAIL_GET_RESOLUTION;
  83. phys_w = mailbuffer[5];
  84. phys_h = mailbuffer[6];
  85. /* Request 640x480 if not otherwise specified */
  86. if((phys_w == 0) && (phys_h == 0))
  87. {
  88. phys_w = WIDTH;
  89. phys_h = HEIGHT;
  90. }
  91. if((phys_w == 0) || (phys_h == 0))
  92. return FB_FAIL_INVALID_RESOLUTION;
  93. /* For now set the physical and virtual sizes to be the same */
  94. virt_w = phys_w;
  95. virt_h = phys_h;
  96. /* Now set the physical and virtual sizes and bit depth and allocate the framebuffer */
  97. mailbuffer[0] = 22 * 4; // size of buffer
  98. mailbuffer[1] = 0; // request
  99. mailbuffer[2] = TAG_SET_PHYS_WH;
  100. mailbuffer[3] = 8;
  101. mailbuffer[4] = 8;
  102. mailbuffer[5] = phys_w;
  103. mailbuffer[6] = phys_h;
  104. mailbuffer[7] = TAG_SET_VIRT_WH;
  105. mailbuffer[8] = 8;
  106. mailbuffer[9] = 8;
  107. mailbuffer[10] = virt_w;
  108. mailbuffer[11] = virt_h;
  109. mailbuffer[12] = TAG_SET_DEPTH;
  110. mailbuffer[13] = 4;
  111. mailbuffer[14] = 4;
  112. mailbuffer[15] = BPP;
  113. mailbuffer[16] = TAG_ALLOCATE_BUFFER;
  114. mailbuffer[17] = 8;
  115. mailbuffer[18] = 4; // request size = 4, response size = 8
  116. mailbuffer[19] = 16; // requested alignment of buffer, space for returned address
  117. mailbuffer[20] = 0; // space for returned size
  118. mailbuffer[21] = 0; // terminating tag
  119. mbox_write(MBOX_PROP, mb_addr);
  120. mbox_read(MBOX_PROP);
  121. /* Validate the response */
  122. if(mailbuffer[1] != MBOX_SUCCESS)
  123. return FB_FAIL_SETUP_FB;
  124. /* Check the allocate_buffer response */
  125. if(mailbuffer[18] != (MBOX_SUCCESS | 8))
  126. return FB_FAIL_INVALID_TAG_RESPONSE;
  127. fb_addr = mailbuffer[19];
  128. fb_size = mailbuffer[20];
  129. if((fb_addr == 0) || (fb_size == 0))
  130. return FB_FAIL_INVALID_TAG_DATA;
  131. /* Get the pitch of the display */
  132. mailbuffer[0] = 7 * 4;
  133. mailbuffer[1] = 0;
  134. mailbuffer[2] = TAG_GET_PITCH;
  135. mailbuffer[3] = 4;
  136. mailbuffer[4] = 0;
  137. mailbuffer[5] = 0;
  138. mailbuffer[6] = 0;
  139. mbox_write(MBOX_PROP, mb_addr);
  140. mbox_read(MBOX_PROP);
  141. /* Validate the response */
  142. if(mailbuffer[1] != MBOX_SUCCESS)
  143. return FB_FAIL_INVALID_PITCH_RESPONSE;
  144. if(mailbuffer[4] != (MBOX_SUCCESS | 4))
  145. return FB_FAIL_INVALID_PITCH_RESPONSE;
  146. pitch = mailbuffer[5];
  147. if(pitch == 0)
  148. return FB_FAIL_INVALID_PITCH_DATA;
  149. return 0;
  150. }
  151. int fb_get_bpp()
  152. {
  153. return BPP;
  154. }
  155. int fb_get_byte_size()
  156. {
  157. return virt_w * virt_h * BYTES_PER_PIXEL;
  158. }
  159. int fb_get_width()
  160. {
  161. return virt_w;
  162. }
  163. int fb_get_height()
  164. {
  165. return virt_h;
  166. }
  167. int fb_get_pitch()
  168. {
  169. return pitch;
  170. }
  171. uint8_t *fb_get_framebuffer()
  172. {
  173. return (uint8_t *)fb_addr;
  174. }