sis315pro.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  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. #include <grub/vga.h>
  28. #include <grub/cache.h>
  29. #define GRUB_SIS315PRO_PCIID 0x03251039
  30. #define GRUB_SIS315PRO_TOTAL_MEMORY_SPACE 0x800000
  31. #define GRUB_SIS315PRO_MMIO_SPACE 0x1000
  32. static struct
  33. {
  34. struct grub_video_mode_info mode_info;
  35. grub_uint8_t *ptr;
  36. volatile grub_uint8_t *direct_ptr;
  37. int mapped;
  38. grub_uint32_t base;
  39. grub_uint32_t mmiobase;
  40. volatile grub_uint32_t *mmioptr;
  41. grub_pci_device_t dev;
  42. grub_port_t io;
  43. } framebuffer;
  44. static grub_uint8_t
  45. read_sis_cmd (grub_uint8_t addr)
  46. {
  47. grub_outb (addr, framebuffer.io + 0x44);
  48. return grub_inb (framebuffer.io + 0x45);
  49. }
  50. static void
  51. write_sis_cmd (grub_uint8_t val, grub_uint8_t addr)
  52. {
  53. grub_outb (addr, framebuffer.io + 0x44);
  54. grub_outb (val, framebuffer.io + 0x45);
  55. }
  56. #ifndef TEST
  57. static grub_err_t
  58. grub_video_sis315pro_video_init (void)
  59. {
  60. /* Reset frame buffer. */
  61. grub_memset (&framebuffer, 0, sizeof(framebuffer));
  62. return grub_video_fb_init ();
  63. }
  64. static grub_err_t
  65. grub_video_sis315pro_video_fini (void)
  66. {
  67. if (framebuffer.mapped)
  68. {
  69. grub_pci_device_unmap_range (framebuffer.dev, framebuffer.ptr,
  70. GRUB_SIS315PRO_TOTAL_MEMORY_SPACE);
  71. grub_pci_device_unmap_range (framebuffer.dev, framebuffer.direct_ptr,
  72. GRUB_SIS315PRO_TOTAL_MEMORY_SPACE);
  73. }
  74. return grub_video_fb_fini ();
  75. }
  76. #endif
  77. #include "sis315_init.c"
  78. #ifndef TEST
  79. /* Helper for grub_video_sis315pro_setup. */
  80. static int
  81. find_card (grub_pci_device_t dev, grub_pci_id_t pciid, void *data)
  82. {
  83. int *found = data;
  84. grub_pci_address_t addr;
  85. grub_uint32_t class;
  86. addr = grub_pci_make_address (dev, GRUB_PCI_REG_CLASS);
  87. class = grub_pci_read (addr);
  88. if (((class >> 16) & 0xffff) != GRUB_PCI_CLASS_DISPLAY_VGA
  89. || pciid != GRUB_SIS315PRO_PCIID)
  90. return 0;
  91. *found = 1;
  92. addr = grub_pci_make_address (dev, GRUB_PCI_REG_ADDRESS_REG0);
  93. framebuffer.base = grub_pci_read (addr) & GRUB_PCI_ADDR_MEM_MASK;
  94. addr = grub_pci_make_address (dev, GRUB_PCI_REG_ADDRESS_REG1);
  95. framebuffer.mmiobase = grub_pci_read (addr) & GRUB_PCI_ADDR_MEM_MASK;
  96. addr = grub_pci_make_address (dev, GRUB_PCI_REG_ADDRESS_REG2);
  97. framebuffer.io = (grub_pci_read (addr) & GRUB_PCI_ADDR_IO_MASK)
  98. + GRUB_MACHINE_PCI_IO_BASE;
  99. framebuffer.dev = dev;
  100. return 1;
  101. }
  102. #endif
  103. static grub_err_t
  104. grub_video_sis315pro_setup (unsigned int width, unsigned int height,
  105. unsigned int mode_type,
  106. unsigned int mode_mask __attribute__ ((unused)))
  107. {
  108. int depth;
  109. grub_err_t err;
  110. int found = 0;
  111. unsigned i;
  112. #ifndef TEST
  113. /* Decode depth from mode_type. If it is zero, then autodetect. */
  114. depth = (mode_type & GRUB_VIDEO_MODE_TYPE_DEPTH_MASK)
  115. >> GRUB_VIDEO_MODE_TYPE_DEPTH_POS;
  116. if ((width != 640 && width != 0) || (height != 480 && height != 0)
  117. || (depth != 8 && depth != 0))
  118. return grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET,
  119. "Only 640x480x8 is supported");
  120. grub_pci_iterate (find_card, &found);
  121. if (!found)
  122. return grub_error (GRUB_ERR_IO, "Couldn't find graphics card");
  123. #endif
  124. /* Fill mode info details. */
  125. framebuffer.mode_info.width = 640;
  126. framebuffer.mode_info.height = 480;
  127. framebuffer.mode_info.mode_type = (GRUB_VIDEO_MODE_TYPE_INDEX_COLOR
  128. | GRUB_VIDEO_MODE_TYPE_DOUBLE_BUFFERED
  129. | GRUB_VIDEO_MODE_TYPE_UPDATING_SWAP);
  130. framebuffer.mode_info.bpp = 8;
  131. framebuffer.mode_info.bytes_per_pixel = 1;
  132. framebuffer.mode_info.pitch = 640 * 1;
  133. framebuffer.mode_info.number_of_colors = 16;
  134. framebuffer.mode_info.red_mask_size = 0;
  135. framebuffer.mode_info.red_field_pos = 0;
  136. framebuffer.mode_info.green_mask_size = 0;
  137. framebuffer.mode_info.green_field_pos = 0;
  138. framebuffer.mode_info.blue_mask_size = 0;
  139. framebuffer.mode_info.blue_field_pos = 0;
  140. framebuffer.mode_info.reserved_mask_size = 0;
  141. framebuffer.mode_info.reserved_field_pos = 0;
  142. #ifndef TEST
  143. framebuffer.mode_info.blit_format
  144. = grub_video_get_blit_format (&framebuffer.mode_info);
  145. #endif
  146. #ifndef TEST
  147. if (found && (framebuffer.base == 0 || framebuffer.mmiobase == 0))
  148. {
  149. grub_pci_address_t addr;
  150. /* FIXME: choose address dynamically if needed. */
  151. framebuffer.base = 0x40000000;
  152. framebuffer.mmiobase = 0x04000000;
  153. framebuffer.io = 0xb300;
  154. addr = grub_pci_make_address (framebuffer.dev, GRUB_PCI_REG_ADDRESS_REG0);
  155. grub_pci_write (addr, framebuffer.base | GRUB_PCI_ADDR_MEM_PREFETCH);
  156. addr = grub_pci_make_address (framebuffer.dev, GRUB_PCI_REG_ADDRESS_REG1);
  157. grub_pci_write (addr, framebuffer.mmiobase);
  158. addr = grub_pci_make_address (framebuffer.dev, GRUB_PCI_REG_ADDRESS_REG2);
  159. grub_pci_write (addr, framebuffer.io | GRUB_PCI_ADDR_SPACE_IO);
  160. /* Set latency. */
  161. addr = grub_pci_make_address (framebuffer.dev, GRUB_PCI_REG_CACHELINE);
  162. grub_pci_write (addr, 0x80004700);
  163. /* Enable address spaces. */
  164. addr = grub_pci_make_address (framebuffer.dev, GRUB_PCI_REG_COMMAND);
  165. grub_pci_write (addr, 0x7);
  166. addr = grub_pci_make_address (framebuffer.dev, 0x30);
  167. grub_pci_write (addr, 0x04060001);
  168. framebuffer.io += GRUB_MACHINE_PCI_IO_BASE;
  169. }
  170. #endif
  171. /* We can safely discard volatile attribute. */
  172. #ifndef TEST
  173. framebuffer.ptr
  174. = grub_pci_device_map_range_cached (framebuffer.dev,
  175. framebuffer.base,
  176. GRUB_SIS315PRO_TOTAL_MEMORY_SPACE);
  177. framebuffer.direct_ptr
  178. = grub_pci_device_map_range (framebuffer.dev,
  179. framebuffer.base,
  180. GRUB_SIS315PRO_TOTAL_MEMORY_SPACE);
  181. framebuffer.mmioptr = grub_pci_device_map_range (framebuffer.dev,
  182. framebuffer.mmiobase,
  183. GRUB_SIS315PRO_MMIO_SPACE);
  184. #endif
  185. framebuffer.mapped = 1;
  186. #ifndef TEST
  187. /* Prevent garbage from appearing on the screen. */
  188. grub_memset (framebuffer.ptr, 0,
  189. framebuffer.mode_info.height * framebuffer.mode_info.pitch);
  190. grub_arch_sync_dma_caches (framebuffer.ptr,
  191. framebuffer.mode_info.height
  192. * framebuffer.mode_info.pitch);
  193. #endif
  194. grub_outb (GRUB_VGA_IO_MISC_NEGATIVE_VERT_POLARITY
  195. | GRUB_VGA_IO_MISC_NEGATIVE_HORIZ_POLARITY
  196. | GRUB_VGA_IO_MISC_UPPER_64K
  197. | GRUB_VGA_IO_MISC_EXTERNAL_CLOCK_0
  198. | GRUB_VGA_IO_MISC_28MHZ
  199. | GRUB_VGA_IO_MISC_ENABLE_VRAM_ACCESS
  200. | GRUB_VGA_IO_MISC_COLOR,
  201. GRUB_VGA_IO_MISC_WRITE + GRUB_MACHINE_PCI_IO_BASE);
  202. grub_vga_sr_write (0x86, 5);
  203. for (i = 6; i <= 0x27; i++)
  204. grub_vga_sr_write (0, i);
  205. for (i = 0x31; i <= 0x3d; i++)
  206. grub_vga_sr_write (0, i);
  207. for (i = 0; i < ARRAY_SIZE (sr_dump); i++)
  208. grub_vga_sr_write (sr_dump[i].val, sr_dump[i].reg);
  209. for (i = 0x30; i < 0x40; i++)
  210. grub_vga_cr_write (0, i);
  211. grub_vga_cr_write (0x77, 0x40);
  212. grub_vga_cr_write (0x77, 0x41);
  213. grub_vga_cr_write (0x00, 0x42);
  214. grub_vga_cr_write (0x5b, 0x43);
  215. grub_vga_cr_write (0x00, 0x44);
  216. grub_vga_cr_write (0x23, 0x48);
  217. grub_vga_cr_write (0xaa, 0x49);
  218. grub_vga_cr_write (0x02, 0x37);
  219. grub_vga_cr_write (0x20, 0x5b);
  220. grub_vga_cr_write (0x00, 0x83);
  221. grub_vga_cr_write (0x80, 0x63);
  222. grub_vga_cr_write (0x0c, GRUB_VGA_CR_VSYNC_END);
  223. grub_vga_cr_write (0x5f, GRUB_VGA_CR_HTOTAL);
  224. grub_vga_cr_write (0x4f, GRUB_VGA_CR_HORIZ_END);
  225. grub_vga_cr_write (0x50, GRUB_VGA_CR_HBLANK_START);
  226. grub_vga_cr_write (0x82, GRUB_VGA_CR_HBLANK_END);
  227. grub_vga_cr_write (0x54, GRUB_VGA_CR_HORIZ_SYNC_PULSE_START);
  228. grub_vga_cr_write (0x80, GRUB_VGA_CR_HORIZ_SYNC_PULSE_END);
  229. grub_vga_cr_write (0x0b, GRUB_VGA_CR_VERT_TOTAL);
  230. grub_vga_cr_write (0x3e, GRUB_VGA_CR_OVERFLOW);
  231. grub_vga_cr_write (0x00, GRUB_VGA_CR_BYTE_PANNING);
  232. grub_vga_cr_write (0x40, GRUB_VGA_CR_CELL_HEIGHT);
  233. grub_vga_cr_write (0x00, GRUB_VGA_CR_CURSOR_START);
  234. grub_vga_cr_write (0x00, GRUB_VGA_CR_CURSOR_END);
  235. grub_vga_cr_write (0x00, GRUB_VGA_CR_START_ADDR_HIGH_REGISTER);
  236. grub_vga_cr_write (0x00, GRUB_VGA_CR_START_ADDR_LOW_REGISTER);
  237. grub_vga_cr_write (0x00, GRUB_VGA_CR_CURSOR_ADDR_HIGH);
  238. grub_vga_cr_write (0x00, GRUB_VGA_CR_CURSOR_ADDR_LOW);
  239. grub_vga_cr_write (0xea, GRUB_VGA_CR_VSYNC_START);
  240. grub_vga_cr_write (0x8c, GRUB_VGA_CR_VSYNC_END);
  241. grub_vga_cr_write (0xdf, GRUB_VGA_CR_VDISPLAY_END);
  242. grub_vga_cr_write (0x28, GRUB_VGA_CR_PITCH);
  243. grub_vga_cr_write (0x40, GRUB_VGA_CR_UNDERLINE_LOCATION);
  244. grub_vga_cr_write (0xe7, GRUB_VGA_CR_VERTICAL_BLANK_START);
  245. grub_vga_cr_write (0x04, GRUB_VGA_CR_VERTICAL_BLANK_END);
  246. grub_vga_cr_write (0xa3, GRUB_VGA_CR_MODE);
  247. grub_vga_cr_write (0xff, GRUB_VGA_CR_LINE_COMPARE);
  248. grub_vga_cr_write (0x0c, GRUB_VGA_CR_VSYNC_END);
  249. grub_vga_cr_write (0x5f, GRUB_VGA_CR_HTOTAL);
  250. grub_vga_cr_write (0x4f, GRUB_VGA_CR_HORIZ_END);
  251. grub_vga_cr_write (0x50, GRUB_VGA_CR_HBLANK_START);
  252. grub_vga_cr_write (0x82, GRUB_VGA_CR_HBLANK_END);
  253. grub_vga_cr_write (0x55, GRUB_VGA_CR_HORIZ_SYNC_PULSE_START);
  254. grub_vga_cr_write (0x81, GRUB_VGA_CR_HORIZ_SYNC_PULSE_END);
  255. grub_vga_cr_write (0x0b, GRUB_VGA_CR_VERT_TOTAL);
  256. grub_vga_cr_write (0x3e, GRUB_VGA_CR_OVERFLOW);
  257. grub_vga_cr_write (0xe9, GRUB_VGA_CR_VSYNC_START);
  258. grub_vga_cr_write (0x8b, GRUB_VGA_CR_VSYNC_END);
  259. grub_vga_cr_write (0xdf, GRUB_VGA_CR_VDISPLAY_END);
  260. grub_vga_cr_write (0xe7, GRUB_VGA_CR_VERTICAL_BLANK_START);
  261. grub_vga_cr_write (0x04, GRUB_VGA_CR_VERTICAL_BLANK_END);
  262. grub_vga_cr_write (0x40, GRUB_VGA_CR_CELL_HEIGHT);
  263. grub_vga_cr_write (0x50, GRUB_VGA_CR_PITCH);
  264. grub_vga_cr_write (0x00, 0x19);
  265. grub_vga_cr_write (0x00, 0x1a);
  266. grub_vga_cr_write (0x6c, 0x52);
  267. grub_vga_cr_write (0x2e, 0x34);
  268. grub_vga_cr_write (0x00, 0x31);
  269. grub_vga_cr_write (0, GRUB_VGA_CR_START_ADDR_HIGH_REGISTER);
  270. grub_vga_cr_write (0, GRUB_VGA_CR_START_ADDR_LOW_REGISTER);
  271. for (i = 0; i < 16; i++)
  272. grub_vga_write_arx (i, i);
  273. grub_vga_write_arx (1, GRUB_VGA_ARX_MODE);
  274. grub_vga_write_arx (0, GRUB_VGA_ARX_OVERSCAN);
  275. grub_vga_write_arx (0, GRUB_VGA_ARX_COLOR_PLANE_ENABLE);
  276. grub_vga_write_arx (0, GRUB_VGA_ARX_HORIZONTAL_PANNING);
  277. grub_vga_write_arx (0, GRUB_VGA_ARX_COLOR_SELECT);
  278. grub_outb (0xff, GRUB_VGA_IO_PIXEL_MASK + GRUB_MACHINE_PCI_IO_BASE);
  279. for (i = 0; i < ARRAY_SIZE (gr); i++)
  280. grub_vga_gr_write (gr[i], i);
  281. for (i = 0; i < GRUB_VIDEO_FBSTD_NUMCOLORS; i++)
  282. grub_vga_palette_write (i, grub_video_fbstd_colors[i].r,
  283. grub_video_fbstd_colors[i].g,
  284. grub_video_fbstd_colors[i].b);
  285. #if 1
  286. {
  287. if (read_sis_cmd (0x5) != 0xa1)
  288. write_sis_cmd (0x86, 0x5);
  289. write_sis_cmd (read_sis_cmd (0x20) | 0xa1, 0x20);
  290. write_sis_cmd (read_sis_cmd (0x1e) | 0xda, 0x1e);
  291. #define IND_SIS_CMDQUEUE_SET 0x26
  292. #define IND_SIS_CMDQUEUE_THRESHOLD 0x27
  293. #define COMMAND_QUEUE_THRESHOLD 0x1F
  294. #define SIS_CMD_QUEUE_RESET 0x01
  295. #define SIS_AGP_CMDQUEUE_ENABLE 0x80 /* 315/330/340 series SR26 */
  296. #define SIS_VRAM_CMDQUEUE_ENABLE 0x40
  297. #define SIS_MMIO_CMD_ENABLE 0x20
  298. #define SIS_CMD_QUEUE_SIZE_512k 0x00
  299. #define SIS_CMD_QUEUE_SIZE_1M 0x04
  300. #define SIS_CMD_QUEUE_SIZE_2M 0x08
  301. #define SIS_CMD_QUEUE_SIZE_4M 0x0C
  302. #define SIS_CMD_QUEUE_RESET 0x01
  303. #define SIS_CMD_AUTO_CORR 0x02
  304. write_sis_cmd (COMMAND_QUEUE_THRESHOLD, IND_SIS_CMDQUEUE_THRESHOLD);
  305. write_sis_cmd (SIS_CMD_QUEUE_RESET, IND_SIS_CMDQUEUE_SET);
  306. framebuffer.mmioptr[0x85C4 / 4] = framebuffer.mmioptr[0x85C8 / 4];
  307. write_sis_cmd (SIS_MMIO_CMD_ENABLE | SIS_CMD_AUTO_CORR, IND_SIS_CMDQUEUE_SET);
  308. framebuffer.mmioptr[0x85C0 / 4] = (0x1000000 - (512 * 1024));
  309. }
  310. #endif
  311. #ifndef TEST
  312. err = grub_video_fb_setup (mode_type, mode_mask,
  313. &framebuffer.mode_info,
  314. framebuffer.ptr, NULL, NULL);
  315. if (err)
  316. return err;
  317. /* Copy default palette to initialize emulated palette. */
  318. err = grub_video_fb_set_palette (0, GRUB_VIDEO_FBSTD_EXT_NUMCOLORS,
  319. grub_video_fbstd_colors);
  320. #endif
  321. return err;
  322. }
  323. #ifndef TEST
  324. static grub_err_t
  325. grub_video_sis315pro_swap_buffers (void)
  326. {
  327. grub_size_t s;
  328. s = (framebuffer.mode_info.height
  329. * framebuffer.mode_info.pitch
  330. * framebuffer.mode_info.bytes_per_pixel);
  331. grub_video_fb_swap_buffers ();
  332. grub_arch_sync_dma_caches (framebuffer.ptr, s);
  333. return GRUB_ERR_NONE;
  334. }
  335. static grub_err_t
  336. grub_video_sis315pro_get_info_and_fini (struct grub_video_mode_info *mode_info,
  337. void **framebuf)
  338. {
  339. grub_memcpy (mode_info, &(framebuffer.mode_info), sizeof (*mode_info));
  340. *framebuf = (void *) framebuffer.direct_ptr;
  341. grub_video_fb_fini ();
  342. return GRUB_ERR_NONE;
  343. }
  344. static struct grub_video_adapter grub_video_sis315pro_adapter =
  345. {
  346. .name = "SIS315PRO Video Driver",
  347. .id = GRUB_VIDEO_DRIVER_SIS315PRO,
  348. .prio = GRUB_VIDEO_ADAPTER_PRIO_NATIVE,
  349. .init = grub_video_sis315pro_video_init,
  350. .fini = grub_video_sis315pro_video_fini,
  351. .setup = grub_video_sis315pro_setup,
  352. .get_info = grub_video_fb_get_info,
  353. .get_info_and_fini = grub_video_sis315pro_get_info_and_fini,
  354. .set_palette = grub_video_fb_set_palette,
  355. .get_palette = grub_video_fb_get_palette,
  356. .set_viewport = grub_video_fb_set_viewport,
  357. .get_viewport = grub_video_fb_get_viewport,
  358. .set_region = grub_video_fb_set_region,
  359. .get_region = grub_video_fb_get_region,
  360. .set_area_status = grub_video_fb_set_area_status,
  361. .get_area_status = grub_video_fb_get_area_status,
  362. .map_color = grub_video_fb_map_color,
  363. .map_rgb = grub_video_fb_map_rgb,
  364. .map_rgba = grub_video_fb_map_rgba,
  365. .unmap_color = grub_video_fb_unmap_color,
  366. .fill_rect = grub_video_fb_fill_rect,
  367. .blit_bitmap = grub_video_fb_blit_bitmap,
  368. .blit_render_target = grub_video_fb_blit_render_target,
  369. .scroll = grub_video_fb_scroll,
  370. .swap_buffers = grub_video_sis315pro_swap_buffers,
  371. .create_render_target = grub_video_fb_create_render_target,
  372. .delete_render_target = grub_video_fb_delete_render_target,
  373. .set_active_render_target = grub_video_fb_set_active_render_target,
  374. .get_active_render_target = grub_video_fb_get_active_render_target,
  375. .next = 0
  376. };
  377. GRUB_MOD_INIT(video_sis315pro)
  378. {
  379. grub_video_register (&grub_video_sis315pro_adapter);
  380. }
  381. GRUB_MOD_FINI(video_sis315pro)
  382. {
  383. grub_video_unregister (&grub_video_sis315pro_adapter);
  384. }
  385. #else
  386. int
  387. main ()
  388. {
  389. grub_video_sis315pro_setup (640, 400, 0, 0);
  390. }
  391. #endif