vesafb.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  1. /*
  2. * framebuffer driver for VBE 2.0 compliant graphic boards
  3. *
  4. * switching to graphics mode happens at boot time (while
  5. * running in real mode, see arch/i386/boot/video.S).
  6. *
  7. * (c) 1998 Gerd Knorr <kraxel@goldbach.in-berlin.de>
  8. *
  9. */
  10. #include <linux/module.h>
  11. #include <linux/kernel.h>
  12. #include <linux/errno.h>
  13. #include <linux/string.h>
  14. #include <linux/mm.h>
  15. #include <linux/delay.h>
  16. #include <linux/fb.h>
  17. #include <linux/ioport.h>
  18. #include <linux/init.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/screen_info.h>
  21. #include <linux/io.h>
  22. #include <video/vga.h>
  23. #define dac_reg (0x3c8)
  24. #define dac_val (0x3c9)
  25. /* --------------------------------------------------------------------- */
  26. struct vesafb_par {
  27. u32 pseudo_palette[256];
  28. int wc_cookie;
  29. };
  30. static struct fb_var_screeninfo vesafb_defined = {
  31. .activate = FB_ACTIVATE_NOW,
  32. .height = -1,
  33. .width = -1,
  34. .right_margin = 32,
  35. .upper_margin = 16,
  36. .lower_margin = 4,
  37. .vsync_len = 4,
  38. .vmode = FB_VMODE_NONINTERLACED,
  39. };
  40. static struct fb_fix_screeninfo vesafb_fix = {
  41. .id = "VESA VGA",
  42. .type = FB_TYPE_PACKED_PIXELS,
  43. .accel = FB_ACCEL_NONE,
  44. };
  45. static int inverse __read_mostly;
  46. static int mtrr __read_mostly; /* disable mtrr */
  47. static int vram_remap; /* Set amount of memory to be used */
  48. static int vram_total; /* Set total amount of memory */
  49. static int pmi_setpal __read_mostly = 1; /* pmi for palette changes ??? */
  50. static int ypan __read_mostly; /* 0..nothing, 1..ypan, 2..ywrap */
  51. static void (*pmi_start)(void) __read_mostly;
  52. static void (*pmi_pal) (void) __read_mostly;
  53. static int depth __read_mostly;
  54. static int vga_compat __read_mostly;
  55. /* --------------------------------------------------------------------- */
  56. static int vesafb_pan_display(struct fb_var_screeninfo *var,
  57. struct fb_info *info)
  58. {
  59. #ifdef __i386__
  60. int offset;
  61. offset = (var->yoffset * info->fix.line_length + var->xoffset) / 4;
  62. __asm__ __volatile__(
  63. "call *(%%edi)"
  64. : /* no return value */
  65. : "a" (0x4f07), /* EAX */
  66. "b" (0), /* EBX */
  67. "c" (offset), /* ECX */
  68. "d" (offset >> 16), /* EDX */
  69. "D" (&pmi_start)); /* EDI */
  70. #endif
  71. return 0;
  72. }
  73. static int vesa_setpalette(int regno, unsigned red, unsigned green,
  74. unsigned blue)
  75. {
  76. int shift = 16 - depth;
  77. int err = -EINVAL;
  78. /*
  79. * Try VGA registers first...
  80. */
  81. if (vga_compat) {
  82. outb_p(regno, dac_reg);
  83. outb_p(red >> shift, dac_val);
  84. outb_p(green >> shift, dac_val);
  85. outb_p(blue >> shift, dac_val);
  86. err = 0;
  87. }
  88. #ifdef __i386__
  89. /*
  90. * Fallback to the PMI....
  91. */
  92. if (err && pmi_setpal) {
  93. struct { u_char blue, green, red, pad; } entry;
  94. entry.red = red >> shift;
  95. entry.green = green >> shift;
  96. entry.blue = blue >> shift;
  97. entry.pad = 0;
  98. __asm__ __volatile__(
  99. "call *(%%esi)"
  100. : /* no return value */
  101. : "a" (0x4f09), /* EAX */
  102. "b" (0), /* EBX */
  103. "c" (1), /* ECX */
  104. "d" (regno), /* EDX */
  105. "D" (&entry), /* EDI */
  106. "S" (&pmi_pal)); /* ESI */
  107. err = 0;
  108. }
  109. #endif
  110. return err;
  111. }
  112. static int vesafb_setcolreg(unsigned regno, unsigned red, unsigned green,
  113. unsigned blue, unsigned transp,
  114. struct fb_info *info)
  115. {
  116. int err = 0;
  117. /*
  118. * Set a single color register. The values supplied are
  119. * already rounded down to the hardware's capabilities
  120. * (according to the entries in the `var' structure). Return
  121. * != 0 for invalid regno.
  122. */
  123. if (regno >= info->cmap.len)
  124. return 1;
  125. if (info->var.bits_per_pixel == 8)
  126. err = vesa_setpalette(regno,red,green,blue);
  127. else if (regno < 16) {
  128. switch (info->var.bits_per_pixel) {
  129. case 16:
  130. if (info->var.red.offset == 10) {
  131. /* 1:5:5:5 */
  132. ((u32*) (info->pseudo_palette))[regno] =
  133. ((red & 0xf800) >> 1) |
  134. ((green & 0xf800) >> 6) |
  135. ((blue & 0xf800) >> 11);
  136. } else {
  137. /* 0:5:6:5 */
  138. ((u32*) (info->pseudo_palette))[regno] =
  139. ((red & 0xf800) ) |
  140. ((green & 0xfc00) >> 5) |
  141. ((blue & 0xf800) >> 11);
  142. }
  143. break;
  144. case 24:
  145. case 32:
  146. red >>= 8;
  147. green >>= 8;
  148. blue >>= 8;
  149. ((u32 *)(info->pseudo_palette))[regno] =
  150. (red << info->var.red.offset) |
  151. (green << info->var.green.offset) |
  152. (blue << info->var.blue.offset);
  153. break;
  154. }
  155. }
  156. return err;
  157. }
  158. static void vesafb_destroy(struct fb_info *info)
  159. {
  160. struct vesafb_par *par = info->par;
  161. fb_dealloc_cmap(&info->cmap);
  162. arch_phys_wc_del(par->wc_cookie);
  163. if (info->screen_base)
  164. iounmap(info->screen_base);
  165. release_mem_region(info->apertures->ranges[0].base, info->apertures->ranges[0].size);
  166. }
  167. static struct fb_ops vesafb_ops = {
  168. .owner = THIS_MODULE,
  169. .fb_destroy = vesafb_destroy,
  170. .fb_setcolreg = vesafb_setcolreg,
  171. .fb_pan_display = vesafb_pan_display,
  172. .fb_fillrect = cfb_fillrect,
  173. .fb_copyarea = cfb_copyarea,
  174. .fb_imageblit = cfb_imageblit,
  175. };
  176. static int vesafb_setup(char *options)
  177. {
  178. char *this_opt;
  179. if (!options || !*options)
  180. return 0;
  181. while ((this_opt = strsep(&options, ",")) != NULL) {
  182. if (!*this_opt) continue;
  183. if (! strcmp(this_opt, "inverse"))
  184. inverse=1;
  185. else if (! strcmp(this_opt, "redraw"))
  186. ypan=0;
  187. else if (! strcmp(this_opt, "ypan"))
  188. ypan=1;
  189. else if (! strcmp(this_opt, "ywrap"))
  190. ypan=2;
  191. else if (! strcmp(this_opt, "vgapal"))
  192. pmi_setpal=0;
  193. else if (! strcmp(this_opt, "pmipal"))
  194. pmi_setpal=1;
  195. else if (! strncmp(this_opt, "mtrr:", 5))
  196. mtrr = simple_strtoul(this_opt+5, NULL, 0);
  197. else if (! strcmp(this_opt, "nomtrr"))
  198. mtrr=0;
  199. else if (! strncmp(this_opt, "vtotal:", 7))
  200. vram_total = simple_strtoul(this_opt+7, NULL, 0);
  201. else if (! strncmp(this_opt, "vremap:", 7))
  202. vram_remap = simple_strtoul(this_opt+7, NULL, 0);
  203. }
  204. return 0;
  205. }
  206. static int vesafb_probe(struct platform_device *dev)
  207. {
  208. struct fb_info *info;
  209. struct vesafb_par *par;
  210. int i, err;
  211. unsigned int size_vmode;
  212. unsigned int size_remap;
  213. unsigned int size_total;
  214. char *option = NULL;
  215. /* ignore error return of fb_get_options */
  216. fb_get_options("vesafb", &option);
  217. vesafb_setup(option);
  218. if (screen_info.orig_video_isVGA != VIDEO_TYPE_VLFB)
  219. return -ENODEV;
  220. vga_compat = (screen_info.capabilities & 2) ? 0 : 1;
  221. vesafb_fix.smem_start = screen_info.lfb_base;
  222. vesafb_defined.bits_per_pixel = screen_info.lfb_depth;
  223. if (15 == vesafb_defined.bits_per_pixel)
  224. vesafb_defined.bits_per_pixel = 16;
  225. vesafb_defined.xres = screen_info.lfb_width;
  226. vesafb_defined.yres = screen_info.lfb_height;
  227. vesafb_fix.line_length = screen_info.lfb_linelength;
  228. vesafb_fix.visual = (vesafb_defined.bits_per_pixel == 8) ?
  229. FB_VISUAL_PSEUDOCOLOR : FB_VISUAL_TRUECOLOR;
  230. /* size_vmode -- that is the amount of memory needed for the
  231. * used video mode, i.e. the minimum amount of
  232. * memory we need. */
  233. size_vmode = vesafb_defined.yres * vesafb_fix.line_length;
  234. /* size_total -- all video memory we have. Used for mtrr
  235. * entries, resource allocation and bounds
  236. * checking. */
  237. size_total = screen_info.lfb_size * 65536;
  238. if (vram_total)
  239. size_total = vram_total * 1024 * 1024;
  240. if (size_total < size_vmode)
  241. size_total = size_vmode;
  242. /* size_remap -- the amount of video memory we are going to
  243. * use for vesafb. With modern cards it is no
  244. * option to simply use size_total as that
  245. * wastes plenty of kernel address space. */
  246. size_remap = size_vmode * 2;
  247. if (vram_remap)
  248. size_remap = vram_remap * 1024 * 1024;
  249. if (size_remap < size_vmode)
  250. size_remap = size_vmode;
  251. if (size_remap > size_total)
  252. size_remap = size_total;
  253. vesafb_fix.smem_len = size_remap;
  254. #ifndef __i386__
  255. screen_info.vesapm_seg = 0;
  256. #endif
  257. if (!request_mem_region(vesafb_fix.smem_start, size_total, "vesafb")) {
  258. printk(KERN_WARNING
  259. "vesafb: cannot reserve video memory at 0x%lx\n",
  260. vesafb_fix.smem_start);
  261. /* We cannot make this fatal. Sometimes this comes from magic
  262. spaces our resource handlers simply don't know about */
  263. }
  264. info = framebuffer_alloc(sizeof(struct vesafb_par), &dev->dev);
  265. if (!info) {
  266. release_mem_region(vesafb_fix.smem_start, size_total);
  267. return -ENOMEM;
  268. }
  269. platform_set_drvdata(dev, info);
  270. par = info->par;
  271. info->pseudo_palette = par->pseudo_palette;
  272. /* set vesafb aperture size for generic probing */
  273. info->apertures = alloc_apertures(1);
  274. if (!info->apertures) {
  275. err = -ENOMEM;
  276. goto err;
  277. }
  278. info->apertures->ranges[0].base = screen_info.lfb_base;
  279. info->apertures->ranges[0].size = size_total;
  280. printk(KERN_INFO "vesafb: mode is %dx%dx%d, linelength=%d, pages=%d\n",
  281. vesafb_defined.xres, vesafb_defined.yres, vesafb_defined.bits_per_pixel, vesafb_fix.line_length, screen_info.pages);
  282. if (screen_info.vesapm_seg) {
  283. printk(KERN_INFO "vesafb: protected mode interface info at %04x:%04x\n",
  284. screen_info.vesapm_seg,screen_info.vesapm_off);
  285. }
  286. if (screen_info.vesapm_seg < 0xc000)
  287. ypan = pmi_setpal = 0; /* not available or some DOS TSR ... */
  288. if (ypan || pmi_setpal) {
  289. unsigned short *pmi_base;
  290. pmi_base = (unsigned short*)phys_to_virt(((unsigned long)screen_info.vesapm_seg << 4) + screen_info.vesapm_off);
  291. pmi_start = (void*)((char*)pmi_base + pmi_base[1]);
  292. pmi_pal = (void*)((char*)pmi_base + pmi_base[2]);
  293. printk(KERN_INFO "vesafb: pmi: set display start = %p, set palette = %p\n",pmi_start,pmi_pal);
  294. if (pmi_base[3]) {
  295. printk(KERN_INFO "vesafb: pmi: ports = ");
  296. for (i = pmi_base[3]/2; pmi_base[i] != 0xffff; i++)
  297. printk("%x ",pmi_base[i]);
  298. printk("\n");
  299. if (pmi_base[i] != 0xffff) {
  300. /*
  301. * memory areas not supported (yet?)
  302. *
  303. * Rules are: we have to set up a descriptor for the requested
  304. * memory area and pass it in the ES register to the BIOS function.
  305. */
  306. printk(KERN_INFO "vesafb: can't handle memory requests, pmi disabled\n");
  307. ypan = pmi_setpal = 0;
  308. }
  309. }
  310. }
  311. if (vesafb_defined.bits_per_pixel == 8 && !pmi_setpal && !vga_compat) {
  312. printk(KERN_WARNING "vesafb: hardware palette is unchangeable,\n"
  313. " colors may be incorrect\n");
  314. vesafb_fix.visual = FB_VISUAL_STATIC_PSEUDOCOLOR;
  315. }
  316. vesafb_defined.xres_virtual = vesafb_defined.xres;
  317. vesafb_defined.yres_virtual = vesafb_fix.smem_len / vesafb_fix.line_length;
  318. if (ypan && vesafb_defined.yres_virtual > vesafb_defined.yres) {
  319. printk(KERN_INFO "vesafb: scrolling: %s using protected mode interface, yres_virtual=%d\n",
  320. (ypan > 1) ? "ywrap" : "ypan",vesafb_defined.yres_virtual);
  321. } else {
  322. printk(KERN_INFO "vesafb: scrolling: redraw\n");
  323. vesafb_defined.yres_virtual = vesafb_defined.yres;
  324. ypan = 0;
  325. }
  326. /* some dummy values for timing to make fbset happy */
  327. vesafb_defined.pixclock = 10000000 / vesafb_defined.xres * 1000 / vesafb_defined.yres;
  328. vesafb_defined.left_margin = (vesafb_defined.xres / 8) & 0xf8;
  329. vesafb_defined.hsync_len = (vesafb_defined.xres / 8) & 0xf8;
  330. vesafb_defined.red.offset = screen_info.red_pos;
  331. vesafb_defined.red.length = screen_info.red_size;
  332. vesafb_defined.green.offset = screen_info.green_pos;
  333. vesafb_defined.green.length = screen_info.green_size;
  334. vesafb_defined.blue.offset = screen_info.blue_pos;
  335. vesafb_defined.blue.length = screen_info.blue_size;
  336. vesafb_defined.transp.offset = screen_info.rsvd_pos;
  337. vesafb_defined.transp.length = screen_info.rsvd_size;
  338. if (vesafb_defined.bits_per_pixel <= 8) {
  339. depth = vesafb_defined.green.length;
  340. vesafb_defined.red.length =
  341. vesafb_defined.green.length =
  342. vesafb_defined.blue.length =
  343. vesafb_defined.bits_per_pixel;
  344. }
  345. printk(KERN_INFO "vesafb: %s: "
  346. "size=%d:%d:%d:%d, shift=%d:%d:%d:%d\n",
  347. (vesafb_defined.bits_per_pixel > 8) ?
  348. "Truecolor" : (vga_compat || pmi_setpal) ?
  349. "Pseudocolor" : "Static Pseudocolor",
  350. screen_info.rsvd_size,
  351. screen_info.red_size,
  352. screen_info.green_size,
  353. screen_info.blue_size,
  354. screen_info.rsvd_pos,
  355. screen_info.red_pos,
  356. screen_info.green_pos,
  357. screen_info.blue_pos);
  358. vesafb_fix.ypanstep = ypan ? 1 : 0;
  359. vesafb_fix.ywrapstep = (ypan>1) ? 1 : 0;
  360. /* request failure does not faze us, as vgacon probably has this
  361. * region already (FIXME) */
  362. request_region(0x3c0, 32, "vesafb");
  363. if (mtrr == 3) {
  364. unsigned int temp_size = size_total;
  365. /* Find the largest power-of-two */
  366. temp_size = roundup_pow_of_two(temp_size);
  367. /* Try and find a power of two to add */
  368. do {
  369. par->wc_cookie =
  370. arch_phys_wc_add(vesafb_fix.smem_start,
  371. temp_size);
  372. temp_size >>= 1;
  373. } while (temp_size >= PAGE_SIZE && par->wc_cookie < 0);
  374. info->screen_base = ioremap_wc(vesafb_fix.smem_start, vesafb_fix.smem_len);
  375. } else {
  376. if (mtrr && mtrr != 3)
  377. WARN_ONCE(1, "Only MTRR_TYPE_WRCOMB (3) make sense\n");
  378. info->screen_base = ioremap(vesafb_fix.smem_start, vesafb_fix.smem_len);
  379. }
  380. if (!info->screen_base) {
  381. printk(KERN_ERR
  382. "vesafb: abort, cannot ioremap video memory 0x%x @ 0x%lx\n",
  383. vesafb_fix.smem_len, vesafb_fix.smem_start);
  384. err = -EIO;
  385. goto err;
  386. }
  387. printk(KERN_INFO "vesafb: framebuffer at 0x%lx, mapped to 0x%p, "
  388. "using %dk, total %dk\n",
  389. vesafb_fix.smem_start, info->screen_base,
  390. size_remap/1024, size_total/1024);
  391. info->fbops = &vesafb_ops;
  392. info->var = vesafb_defined;
  393. info->fix = vesafb_fix;
  394. info->flags = FBINFO_FLAG_DEFAULT | FBINFO_MISC_FIRMWARE |
  395. (ypan ? FBINFO_HWACCEL_YPAN : 0);
  396. if (!ypan)
  397. info->fbops->fb_pan_display = NULL;
  398. if (fb_alloc_cmap(&info->cmap, 256, 0) < 0) {
  399. err = -ENOMEM;
  400. goto err;
  401. }
  402. if (register_framebuffer(info)<0) {
  403. err = -EINVAL;
  404. fb_dealloc_cmap(&info->cmap);
  405. goto err;
  406. }
  407. fb_info(info, "%s frame buffer device\n", info->fix.id);
  408. return 0;
  409. err:
  410. arch_phys_wc_del(par->wc_cookie);
  411. if (info->screen_base)
  412. iounmap(info->screen_base);
  413. framebuffer_release(info);
  414. release_mem_region(vesafb_fix.smem_start, size_total);
  415. return err;
  416. }
  417. static int vesafb_remove(struct platform_device *pdev)
  418. {
  419. struct fb_info *info = platform_get_drvdata(pdev);
  420. unregister_framebuffer(info);
  421. framebuffer_release(info);
  422. return 0;
  423. }
  424. static struct platform_driver vesafb_driver = {
  425. .driver = {
  426. .name = "vesa-framebuffer",
  427. },
  428. .probe = vesafb_probe,
  429. .remove = vesafb_remove,
  430. };
  431. module_platform_driver(vesafb_driver);
  432. MODULE_LICENSE("GPL");