sm712.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. /*
  2. * GRUB -- GRand Unified Bootloader
  3. * Copyright (C) 2005,2006,2007,2008,2009,2010 Free Software Foundation, Inc.
  4. *
  5. * GRUB is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * GRUB is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #define grub_video_render_target grub_video_fbrender_target
  19. #include <grub/err.h>
  20. #include <grub/types.h>
  21. #include <grub/dl.h>
  22. #include <grub/misc.h>
  23. #include <grub/mm.h>
  24. #include <grub/video.h>
  25. #include <grub/video_fb.h>
  26. #include <grub/pci.h>
  27. static struct
  28. {
  29. struct grub_video_mode_info mode_info;
  30. struct grub_video_render_target *render_target;
  31. unsigned int bytes_per_scan_line;
  32. unsigned int bytes_per_pixel;
  33. grub_uint8_t *ptr;
  34. int index_color_mode;
  35. int mapped;
  36. grub_uint32_t base;
  37. grub_pci_device_t dev;
  38. } framebuffer;
  39. static grub_err_t
  40. grub_video_sm712_video_init (void)
  41. {
  42. /* Reset frame buffer. */
  43. grub_memset (&framebuffer, 0, sizeof(framebuffer));
  44. return grub_video_fb_init ();
  45. }
  46. static grub_err_t
  47. grub_video_sm712_video_fini (void)
  48. {
  49. if (framebuffer.mapped)
  50. grub_pci_device_unmap_range (framebuffer.dev, framebuffer.ptr,
  51. 1024 * 600 * 2);
  52. return grub_video_fb_fini ();
  53. }
  54. static grub_err_t
  55. grub_video_sm712_setup (unsigned int width, unsigned int height,
  56. unsigned int mode_type, unsigned int mode_mask __attribute__ ((unused)))
  57. {
  58. int depth;
  59. grub_err_t err;
  60. int found = 0;
  61. auto int NESTED_FUNC_ATTR find_card (grub_pci_device_t dev, grub_pci_id_t pciid __attribute__ ((unused)));
  62. int NESTED_FUNC_ATTR find_card (grub_pci_device_t dev, grub_pci_id_t pciid __attribute__ ((unused)))
  63. {
  64. grub_pci_address_t addr;
  65. grub_uint32_t class;
  66. addr = grub_pci_make_address (dev, GRUB_PCI_REG_CLASS);
  67. class = grub_pci_read (addr);
  68. if (((class >> 16) & 0xffff) != 0x0300 || pciid != 0x0712126f)
  69. return 0;
  70. found = 1;
  71. addr = grub_pci_make_address (dev, GRUB_PCI_REG_ADDRESS_REG0);
  72. framebuffer.base = grub_pci_read (addr);
  73. framebuffer.dev = dev;
  74. return 1;
  75. }
  76. /* Decode depth from mode_type. If it is zero, then autodetect. */
  77. depth = (mode_type & GRUB_VIDEO_MODE_TYPE_DEPTH_MASK)
  78. >> GRUB_VIDEO_MODE_TYPE_DEPTH_POS;
  79. if ((width != 1024 && width != 0) || (height != 600 && height != 0)
  80. || (depth != 16 && depth != 0))
  81. return grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET,
  82. "Only 1024x600x16 is supported");
  83. grub_pci_iterate (find_card);
  84. if (!found)
  85. return grub_error (GRUB_ERR_IO, "Couldn't find graphics card");
  86. if (found && framebuffer.base == 0)
  87. {
  88. /* FIXME: change framebuffer base */
  89. }
  90. /* Fill mode info details. */
  91. framebuffer.mode_info.width = 1024;
  92. framebuffer.mode_info.height = 600;
  93. framebuffer.mode_info.mode_type = GRUB_VIDEO_MODE_TYPE_RGB;
  94. framebuffer.mode_info.bpp = 16;
  95. framebuffer.mode_info.bytes_per_pixel = 2;
  96. framebuffer.mode_info.pitch = 1024 * 2;
  97. framebuffer.mode_info.number_of_colors = 256;
  98. framebuffer.mode_info.red_mask_size = 5;
  99. framebuffer.mode_info.red_field_pos = 11;
  100. framebuffer.mode_info.green_mask_size = 6;
  101. framebuffer.mode_info.green_field_pos = 5;
  102. framebuffer.mode_info.blue_mask_size = 5;
  103. framebuffer.mode_info.blue_field_pos = 0;
  104. framebuffer.mode_info.reserved_mask_size = 0;
  105. framebuffer.mode_info.reserved_field_pos = 0;
  106. framebuffer.mode_info.blit_format = grub_video_get_blit_format (&framebuffer.mode_info);
  107. /* We can safely discard volatile attribute. */
  108. framebuffer.ptr = (void *) grub_pci_device_map_range (framebuffer.dev,
  109. framebuffer.base,
  110. 1024 * 600 * 2);
  111. framebuffer.mapped = 1;
  112. err = grub_video_fb_create_render_target_from_pointer (&framebuffer.render_target, &framebuffer.mode_info, framebuffer.ptr);
  113. if (err)
  114. return err;
  115. err = grub_video_fb_set_active_render_target (framebuffer.render_target);
  116. if (err)
  117. return err;
  118. /* Copy default palette to initialize emulated palette. */
  119. err = grub_video_fb_set_palette (0, GRUB_VIDEO_FBSTD_NUMCOLORS,
  120. grub_video_fbstd_colors);
  121. return err;
  122. }
  123. static grub_err_t
  124. grub_video_sm712_set_palette (unsigned int start, unsigned int count,
  125. struct grub_video_palette_data *palette_data)
  126. {
  127. if (framebuffer.index_color_mode)
  128. {
  129. /* TODO: Implement setting indexed color mode palette to hardware. */
  130. }
  131. /* Then set color to emulated palette. */
  132. return grub_video_fb_set_palette (start, count, palette_data);
  133. }
  134. static grub_err_t
  135. grub_video_sm712_swap_buffers (void)
  136. {
  137. /* TODO: Implement buffer swapping. */
  138. return GRUB_ERR_NONE;
  139. }
  140. static grub_err_t
  141. grub_video_sm712_set_active_render_target (struct grub_video_render_target *target)
  142. {
  143. if (target == GRUB_VIDEO_RENDER_TARGET_DISPLAY)
  144. target = framebuffer.render_target;
  145. return grub_video_fb_set_active_render_target (target);
  146. }
  147. static grub_err_t
  148. grub_video_sm712_get_info_and_fini (struct grub_video_mode_info *mode_info,
  149. void **framebuf)
  150. {
  151. grub_memcpy (mode_info, &(framebuffer.mode_info), sizeof (*mode_info));
  152. *framebuf = (char *) framebuffer.ptr;
  153. grub_video_fb_fini ();
  154. return GRUB_ERR_NONE;
  155. }
  156. static struct grub_video_adapter grub_video_sm712_adapter =
  157. {
  158. .name = "SM712 Video Driver",
  159. .id = GRUB_VIDEO_DRIVER_SM712,
  160. .init = grub_video_sm712_video_init,
  161. .fini = grub_video_sm712_video_fini,
  162. .setup = grub_video_sm712_setup,
  163. .get_info = grub_video_fb_get_info,
  164. .get_info_and_fini = grub_video_sm712_get_info_and_fini,
  165. .set_palette = grub_video_sm712_set_palette,
  166. .get_palette = grub_video_fb_get_palette,
  167. .set_viewport = grub_video_fb_set_viewport,
  168. .get_viewport = grub_video_fb_get_viewport,
  169. .map_color = grub_video_fb_map_color,
  170. .map_rgb = grub_video_fb_map_rgb,
  171. .map_rgba = grub_video_fb_map_rgba,
  172. .unmap_color = grub_video_fb_unmap_color,
  173. .fill_rect = grub_video_fb_fill_rect,
  174. .blit_bitmap = grub_video_fb_blit_bitmap,
  175. .blit_render_target = grub_video_fb_blit_render_target,
  176. .scroll = grub_video_fb_scroll,
  177. .swap_buffers = grub_video_sm712_swap_buffers,
  178. .create_render_target = grub_video_fb_create_render_target,
  179. .delete_render_target = grub_video_fb_delete_render_target,
  180. .set_active_render_target = grub_video_sm712_set_active_render_target,
  181. .get_active_render_target = grub_video_fb_get_active_render_target,
  182. .next = 0
  183. };
  184. GRUB_MOD_INIT(video_sm712)
  185. {
  186. grub_video_register (&grub_video_sm712_adapter);
  187. }
  188. GRUB_MOD_FINI(video_sm712)
  189. {
  190. grub_video_unregister (&grub_video_sm712_adapter);
  191. }