vesafb.c 15 KB

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