vga16fb.c 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486
  1. /*
  2. * linux/drivers/video/vga16.c -- VGA 16-color framebuffer driver
  3. *
  4. * Copyright 1999 Ben Pfaff <pfaffben@debian.org> and Petr Vandrovec <VANDROVE@vc.cvut.cz>
  5. * Based on VGA info at http://www.goodnet.com/~tinara/FreeVGA/home.htm
  6. * Based on VESA framebuffer (c) 1998 Gerd Knorr <kraxel@goldbach.in-berlin.de>
  7. *
  8. * This file is subject to the terms and conditions of the GNU General
  9. * Public License. See the file COPYING in the main directory of this
  10. * archive for more details.
  11. */
  12. #include <linux/module.h>
  13. #include <linux/kernel.h>
  14. #include <linux/errno.h>
  15. #include <linux/string.h>
  16. #include <linux/mm.h>
  17. #include <linux/tty.h>
  18. #include <linux/slab.h>
  19. #include <linux/delay.h>
  20. #include <linux/fb.h>
  21. #include <linux/ioport.h>
  22. #include <linux/init.h>
  23. #include <linux/platform_device.h>
  24. #include <asm/io.h>
  25. #include <video/vga.h>
  26. #define GRAPHICS_ADDR_REG VGA_GFX_I /* Graphics address register. */
  27. #define GRAPHICS_DATA_REG VGA_GFX_D /* Graphics data register. */
  28. #define SET_RESET_INDEX VGA_GFX_SR_VALUE /* Set/Reset Register index. */
  29. #define ENABLE_SET_RESET_INDEX VGA_GFX_SR_ENABLE /* Enable Set/Reset Register index. */
  30. #define DATA_ROTATE_INDEX VGA_GFX_DATA_ROTATE /* Data Rotate Register index. */
  31. #define GRAPHICS_MODE_INDEX VGA_GFX_MODE /* Graphics Mode Register index. */
  32. #define BIT_MASK_INDEX VGA_GFX_BIT_MASK /* Bit Mask Register index. */
  33. #define dac_reg (VGA_PEL_IW)
  34. #define dac_val (VGA_PEL_D)
  35. #define VGA_FB_PHYS 0xA0000
  36. #define VGA_FB_PHYS_LEN 65536
  37. #define MODE_SKIP4 1
  38. #define MODE_8BPP 2
  39. #define MODE_CFB 4
  40. #define MODE_TEXT 8
  41. /* --------------------------------------------------------------------- */
  42. /*
  43. * card parameters
  44. */
  45. struct vga16fb_par {
  46. /* structure holding original VGA register settings when the
  47. screen is blanked */
  48. struct {
  49. unsigned char SeqCtrlIndex; /* Sequencer Index reg. */
  50. unsigned char CrtCtrlIndex; /* CRT-Contr. Index reg. */
  51. unsigned char CrtMiscIO; /* Miscellaneous register */
  52. unsigned char HorizontalTotal; /* CRT-Controller:00h */
  53. unsigned char HorizDisplayEnd; /* CRT-Controller:01h */
  54. unsigned char StartHorizRetrace;/* CRT-Controller:04h */
  55. unsigned char EndHorizRetrace; /* CRT-Controller:05h */
  56. unsigned char Overflow; /* CRT-Controller:07h */
  57. unsigned char StartVertRetrace; /* CRT-Controller:10h */
  58. unsigned char EndVertRetrace; /* CRT-Controller:11h */
  59. unsigned char ModeControl; /* CRT-Controller:17h */
  60. unsigned char ClockingMode; /* Seq-Controller:01h */
  61. } vga_state;
  62. struct vgastate state;
  63. atomic_t ref_count;
  64. int palette_blanked, vesa_blanked, mode, isVGA;
  65. u8 misc, pel_msk, vss, clkdiv;
  66. u8 crtc[VGA_CRT_C];
  67. };
  68. /* --------------------------------------------------------------------- */
  69. static struct fb_var_screeninfo vga16fb_defined __initdata = {
  70. .xres = 640,
  71. .yres = 480,
  72. .xres_virtual = 640,
  73. .yres_virtual = 480,
  74. .bits_per_pixel = 4,
  75. .activate = FB_ACTIVATE_TEST,
  76. .height = -1,
  77. .width = -1,
  78. .pixclock = 39721,
  79. .left_margin = 48,
  80. .right_margin = 16,
  81. .upper_margin = 33,
  82. .lower_margin = 10,
  83. .hsync_len = 96,
  84. .vsync_len = 2,
  85. .vmode = FB_VMODE_NONINTERLACED,
  86. };
  87. /* name should not depend on EGA/VGA */
  88. static struct fb_fix_screeninfo vga16fb_fix __initdata = {
  89. .id = "VGA16 VGA",
  90. .smem_start = VGA_FB_PHYS,
  91. .smem_len = VGA_FB_PHYS_LEN,
  92. .type = FB_TYPE_VGA_PLANES,
  93. .type_aux = FB_AUX_VGA_PLANES_VGA4,
  94. .visual = FB_VISUAL_PSEUDOCOLOR,
  95. .xpanstep = 8,
  96. .ypanstep = 1,
  97. .line_length = 640/8,
  98. .accel = FB_ACCEL_NONE
  99. };
  100. /* The VGA's weird architecture often requires that we read a byte and
  101. write a byte to the same location. It doesn't matter *what* byte
  102. we write, however. This is because all the action goes on behind
  103. the scenes in the VGA's 32-bit latch register, and reading and writing
  104. video memory just invokes latch behavior.
  105. To avoid race conditions (is this necessary?), reading and writing
  106. the memory byte should be done with a single instruction. One
  107. suitable instruction is the x86 bitwise OR. The following
  108. read-modify-write routine should optimize to one such bitwise
  109. OR. */
  110. static inline void rmw(volatile char __iomem *p)
  111. {
  112. readb(p);
  113. writeb(1, p);
  114. }
  115. /* Set the Graphics Mode Register, and return its previous value.
  116. Bits 0-1 are write mode, bit 3 is read mode. */
  117. static inline int setmode(int mode)
  118. {
  119. int oldmode;
  120. vga_io_w(GRAPHICS_ADDR_REG, GRAPHICS_MODE_INDEX);
  121. oldmode = vga_io_r(GRAPHICS_DATA_REG);
  122. vga_io_w(GRAPHICS_DATA_REG, mode);
  123. return oldmode;
  124. }
  125. /* Select the Bit Mask Register and return its value. */
  126. static inline int selectmask(void)
  127. {
  128. return vga_io_rgfx(BIT_MASK_INDEX);
  129. }
  130. /* Set the value of the Bit Mask Register. It must already have been
  131. selected with selectmask(). */
  132. static inline void setmask(int mask)
  133. {
  134. vga_io_w(GRAPHICS_DATA_REG, mask);
  135. }
  136. /* Set the Data Rotate Register and return its old value.
  137. Bits 0-2 are rotate count, bits 3-4 are logical operation
  138. (0=NOP, 1=AND, 2=OR, 3=XOR). */
  139. static inline int setop(int op)
  140. {
  141. int oldop;
  142. vga_io_w(GRAPHICS_ADDR_REG, DATA_ROTATE_INDEX);
  143. oldop = vga_io_r(GRAPHICS_DATA_REG);
  144. vga_io_w(GRAPHICS_DATA_REG, op);
  145. return oldop;
  146. }
  147. /* Set the Enable Set/Reset Register and return its old value.
  148. The code here always uses value 0xf for thsi register. */
  149. static inline int setsr(int sr)
  150. {
  151. int oldsr;
  152. vga_io_w(GRAPHICS_ADDR_REG, ENABLE_SET_RESET_INDEX);
  153. oldsr = vga_io_r(GRAPHICS_DATA_REG);
  154. vga_io_w(GRAPHICS_DATA_REG, sr);
  155. return oldsr;
  156. }
  157. /* Set the Set/Reset Register and return its old value. */
  158. static inline int setcolor(int color)
  159. {
  160. int oldcolor;
  161. vga_io_w(GRAPHICS_ADDR_REG, SET_RESET_INDEX);
  162. oldcolor = vga_io_r(GRAPHICS_DATA_REG);
  163. vga_io_w(GRAPHICS_DATA_REG, color);
  164. return oldcolor;
  165. }
  166. /* Return the value in the Graphics Address Register. */
  167. static inline int getindex(void)
  168. {
  169. return vga_io_r(GRAPHICS_ADDR_REG);
  170. }
  171. /* Set the value in the Graphics Address Register. */
  172. static inline void setindex(int index)
  173. {
  174. vga_io_w(GRAPHICS_ADDR_REG, index);
  175. }
  176. static void vga16fb_pan_var(struct fb_info *info,
  177. struct fb_var_screeninfo *var)
  178. {
  179. struct vga16fb_par *par = info->par;
  180. u32 xoffset, pos;
  181. xoffset = var->xoffset;
  182. if (info->var.bits_per_pixel == 8) {
  183. pos = (info->var.xres_virtual * var->yoffset + xoffset) >> 2;
  184. } else if (par->mode & MODE_TEXT) {
  185. int fh = 16; // FIXME !!! font height. Fugde for now.
  186. pos = (info->var.xres_virtual * (var->yoffset / fh) + xoffset) >> 3;
  187. } else {
  188. if (info->var.nonstd)
  189. xoffset--;
  190. pos = (info->var.xres_virtual * var->yoffset + xoffset) >> 3;
  191. }
  192. vga_io_wcrt(VGA_CRTC_START_HI, pos >> 8);
  193. vga_io_wcrt(VGA_CRTC_START_LO, pos & 0xFF);
  194. /* if we support CFB4, then we must! support xoffset with pixel
  195. * granularity if someone supports xoffset in bit resolution */
  196. vga_io_r(VGA_IS1_RC); /* reset flip-flop */
  197. vga_io_w(VGA_ATT_IW, VGA_ATC_PEL);
  198. if (var->bits_per_pixel == 8)
  199. vga_io_w(VGA_ATT_IW, (xoffset & 3) << 1);
  200. else
  201. vga_io_w(VGA_ATT_IW, xoffset & 7);
  202. vga_io_r(VGA_IS1_RC);
  203. vga_io_w(VGA_ATT_IW, 0x20);
  204. }
  205. static void vga16fb_update_fix(struct fb_info *info)
  206. {
  207. if (info->var.bits_per_pixel == 4) {
  208. if (info->var.nonstd) {
  209. info->fix.type = FB_TYPE_PACKED_PIXELS;
  210. info->fix.line_length = info->var.xres_virtual / 2;
  211. } else {
  212. info->fix.type = FB_TYPE_VGA_PLANES;
  213. info->fix.type_aux = FB_AUX_VGA_PLANES_VGA4;
  214. info->fix.line_length = info->var.xres_virtual / 8;
  215. }
  216. } else if (info->var.bits_per_pixel == 0) {
  217. info->fix.type = FB_TYPE_TEXT;
  218. info->fix.type_aux = FB_AUX_TEXT_CGA;
  219. info->fix.line_length = info->var.xres_virtual / 4;
  220. } else { /* 8bpp */
  221. if (info->var.nonstd) {
  222. info->fix.type = FB_TYPE_VGA_PLANES;
  223. info->fix.type_aux = FB_AUX_VGA_PLANES_CFB8;
  224. info->fix.line_length = info->var.xres_virtual / 4;
  225. } else {
  226. info->fix.type = FB_TYPE_PACKED_PIXELS;
  227. info->fix.line_length = info->var.xres_virtual;
  228. }
  229. }
  230. }
  231. static void vga16fb_clock_chip(struct vga16fb_par *par,
  232. unsigned int pixclock,
  233. const struct fb_info *info,
  234. int mul, int div)
  235. {
  236. static struct {
  237. u32 pixclock;
  238. u8 misc;
  239. u8 seq_clock_mode;
  240. } *ptr, *best, vgaclocks[] = {
  241. { 79442 /* 12.587 */, 0x00, 0x08},
  242. { 70616 /* 14.161 */, 0x04, 0x08},
  243. { 39721 /* 25.175 */, 0x00, 0x00},
  244. { 35308 /* 28.322 */, 0x04, 0x00},
  245. { 0 /* bad */, 0x00, 0x00}};
  246. int err;
  247. pixclock = (pixclock * mul) / div;
  248. best = vgaclocks;
  249. err = pixclock - best->pixclock;
  250. if (err < 0) err = -err;
  251. for (ptr = vgaclocks + 1; ptr->pixclock; ptr++) {
  252. int tmp;
  253. tmp = pixclock - ptr->pixclock;
  254. if (tmp < 0) tmp = -tmp;
  255. if (tmp < err) {
  256. err = tmp;
  257. best = ptr;
  258. }
  259. }
  260. par->misc |= best->misc;
  261. par->clkdiv = best->seq_clock_mode;
  262. pixclock = (best->pixclock * div) / mul;
  263. }
  264. #define FAIL(X) return -EINVAL
  265. static int vga16fb_open(struct fb_info *info, int user)
  266. {
  267. struct vga16fb_par *par = info->par;
  268. int cnt = atomic_read(&par->ref_count);
  269. if (!cnt) {
  270. memset(&par->state, 0, sizeof(struct vgastate));
  271. par->state.flags = VGA_SAVE_FONTS | VGA_SAVE_MODE |
  272. VGA_SAVE_CMAP;
  273. save_vga(&par->state);
  274. }
  275. atomic_inc(&par->ref_count);
  276. return 0;
  277. }
  278. static int vga16fb_release(struct fb_info *info, int user)
  279. {
  280. struct vga16fb_par *par = info->par;
  281. int cnt = atomic_read(&par->ref_count);
  282. if (!cnt)
  283. return -EINVAL;
  284. if (cnt == 1)
  285. restore_vga(&par->state);
  286. atomic_dec(&par->ref_count);
  287. return 0;
  288. }
  289. static int vga16fb_check_var(struct fb_var_screeninfo *var,
  290. struct fb_info *info)
  291. {
  292. struct vga16fb_par *par = info->par;
  293. u32 xres, right, hslen, left, xtotal;
  294. u32 yres, lower, vslen, upper, ytotal;
  295. u32 vxres, xoffset, vyres, yoffset;
  296. u32 pos;
  297. u8 r7, rMode;
  298. int shift;
  299. int mode;
  300. u32 maxmem;
  301. par->pel_msk = 0xFF;
  302. if (var->bits_per_pixel == 4) {
  303. if (var->nonstd) {
  304. if (!par->isVGA)
  305. return -EINVAL;
  306. shift = 3;
  307. mode = MODE_SKIP4 | MODE_CFB;
  308. maxmem = 16384;
  309. par->pel_msk = 0x0F;
  310. } else {
  311. shift = 3;
  312. mode = 0;
  313. maxmem = 65536;
  314. }
  315. } else if (var->bits_per_pixel == 8) {
  316. if (!par->isVGA)
  317. return -EINVAL; /* no support on EGA */
  318. shift = 2;
  319. if (var->nonstd) {
  320. mode = MODE_8BPP | MODE_CFB;
  321. maxmem = 65536;
  322. } else {
  323. mode = MODE_SKIP4 | MODE_8BPP | MODE_CFB;
  324. maxmem = 16384;
  325. }
  326. } else
  327. return -EINVAL;
  328. xres = (var->xres + 7) & ~7;
  329. vxres = (var->xres_virtual + 0xF) & ~0xF;
  330. xoffset = (var->xoffset + 7) & ~7;
  331. left = (var->left_margin + 7) & ~7;
  332. right = (var->right_margin + 7) & ~7;
  333. hslen = (var->hsync_len + 7) & ~7;
  334. if (vxres < xres)
  335. vxres = xres;
  336. if (xres + xoffset > vxres)
  337. xoffset = vxres - xres;
  338. var->xres = xres;
  339. var->right_margin = right;
  340. var->hsync_len = hslen;
  341. var->left_margin = left;
  342. var->xres_virtual = vxres;
  343. var->xoffset = xoffset;
  344. xres >>= shift;
  345. right >>= shift;
  346. hslen >>= shift;
  347. left >>= shift;
  348. vxres >>= shift;
  349. xtotal = xres + right + hslen + left;
  350. if (xtotal >= 256)
  351. FAIL("xtotal too big");
  352. if (hslen > 32)
  353. FAIL("hslen too big");
  354. if (right + hslen + left > 64)
  355. FAIL("hblank too big");
  356. par->crtc[VGA_CRTC_H_TOTAL] = xtotal - 5;
  357. par->crtc[VGA_CRTC_H_BLANK_START] = xres - 1;
  358. par->crtc[VGA_CRTC_H_DISP] = xres - 1;
  359. pos = xres + right;
  360. par->crtc[VGA_CRTC_H_SYNC_START] = pos;
  361. pos += hslen;
  362. par->crtc[VGA_CRTC_H_SYNC_END] = pos & 0x1F;
  363. pos += left - 2; /* blank_end + 2 <= total + 5 */
  364. par->crtc[VGA_CRTC_H_BLANK_END] = (pos & 0x1F) | 0x80;
  365. if (pos & 0x20)
  366. par->crtc[VGA_CRTC_H_SYNC_END] |= 0x80;
  367. yres = var->yres;
  368. lower = var->lower_margin;
  369. vslen = var->vsync_len;
  370. upper = var->upper_margin;
  371. vyres = var->yres_virtual;
  372. yoffset = var->yoffset;
  373. if (yres > vyres)
  374. vyres = yres;
  375. if (vxres * vyres > maxmem) {
  376. vyres = maxmem / vxres;
  377. if (vyres < yres)
  378. return -ENOMEM;
  379. }
  380. if (yoffset + yres > vyres)
  381. yoffset = vyres - yres;
  382. var->yres = yres;
  383. var->lower_margin = lower;
  384. var->vsync_len = vslen;
  385. var->upper_margin = upper;
  386. var->yres_virtual = vyres;
  387. var->yoffset = yoffset;
  388. if (var->vmode & FB_VMODE_DOUBLE) {
  389. yres <<= 1;
  390. lower <<= 1;
  391. vslen <<= 1;
  392. upper <<= 1;
  393. }
  394. ytotal = yres + lower + vslen + upper;
  395. if (ytotal > 1024) {
  396. ytotal >>= 1;
  397. yres >>= 1;
  398. lower >>= 1;
  399. vslen >>= 1;
  400. upper >>= 1;
  401. rMode = 0x04;
  402. } else
  403. rMode = 0x00;
  404. if (ytotal > 1024)
  405. FAIL("ytotal too big");
  406. if (vslen > 16)
  407. FAIL("vslen too big");
  408. par->crtc[VGA_CRTC_V_TOTAL] = ytotal - 2;
  409. r7 = 0x10; /* disable linecompare */
  410. if (ytotal & 0x100) r7 |= 0x01;
  411. if (ytotal & 0x200) r7 |= 0x20;
  412. par->crtc[VGA_CRTC_PRESET_ROW] = 0;
  413. par->crtc[VGA_CRTC_MAX_SCAN] = 0x40; /* 1 scanline, no linecmp */
  414. if (var->vmode & FB_VMODE_DOUBLE)
  415. par->crtc[VGA_CRTC_MAX_SCAN] |= 0x80;
  416. par->crtc[VGA_CRTC_CURSOR_START] = 0x20;
  417. par->crtc[VGA_CRTC_CURSOR_END] = 0x00;
  418. if ((mode & (MODE_CFB | MODE_8BPP)) == MODE_CFB)
  419. xoffset--;
  420. pos = yoffset * vxres + (xoffset >> shift);
  421. par->crtc[VGA_CRTC_START_HI] = pos >> 8;
  422. par->crtc[VGA_CRTC_START_LO] = pos & 0xFF;
  423. par->crtc[VGA_CRTC_CURSOR_HI] = 0x00;
  424. par->crtc[VGA_CRTC_CURSOR_LO] = 0x00;
  425. pos = yres - 1;
  426. par->crtc[VGA_CRTC_V_DISP_END] = pos & 0xFF;
  427. par->crtc[VGA_CRTC_V_BLANK_START] = pos & 0xFF;
  428. if (pos & 0x100)
  429. r7 |= 0x0A; /* 0x02 -> DISP_END, 0x08 -> BLANK_START */
  430. if (pos & 0x200) {
  431. r7 |= 0x40; /* 0x40 -> DISP_END */
  432. par->crtc[VGA_CRTC_MAX_SCAN] |= 0x20; /* BLANK_START */
  433. }
  434. pos += lower;
  435. par->crtc[VGA_CRTC_V_SYNC_START] = pos & 0xFF;
  436. if (pos & 0x100)
  437. r7 |= 0x04;
  438. if (pos & 0x200)
  439. r7 |= 0x80;
  440. pos += vslen;
  441. par->crtc[VGA_CRTC_V_SYNC_END] = (pos & 0x0F) & ~0x10; /* disabled IRQ */
  442. pos += upper - 1; /* blank_end + 1 <= ytotal + 2 */
  443. par->crtc[VGA_CRTC_V_BLANK_END] = pos & 0xFF; /* 0x7F for original VGA,
  444. but some SVGA chips requires all 8 bits to set */
  445. if (vxres >= 512)
  446. FAIL("vxres too long");
  447. par->crtc[VGA_CRTC_OFFSET] = vxres >> 1;
  448. if (mode & MODE_SKIP4)
  449. par->crtc[VGA_CRTC_UNDERLINE] = 0x5F; /* 256, cfb8 */
  450. else
  451. par->crtc[VGA_CRTC_UNDERLINE] = 0x1F; /* 16, vgap */
  452. par->crtc[VGA_CRTC_MODE] = rMode | ((mode & MODE_TEXT) ? 0xA3 : 0xE3);
  453. par->crtc[VGA_CRTC_LINE_COMPARE] = 0xFF;
  454. par->crtc[VGA_CRTC_OVERFLOW] = r7;
  455. par->vss = 0x00; /* 3DA */
  456. par->misc = 0xE3; /* enable CPU, ports 0x3Dx, positive sync */
  457. if (var->sync & FB_SYNC_HOR_HIGH_ACT)
  458. par->misc &= ~0x40;
  459. if (var->sync & FB_SYNC_VERT_HIGH_ACT)
  460. par->misc &= ~0x80;
  461. par->mode = mode;
  462. if (mode & MODE_8BPP)
  463. /* pixel clock == vga clock / 2 */
  464. vga16fb_clock_chip(par, var->pixclock, info, 1, 2);
  465. else
  466. /* pixel clock == vga clock */
  467. vga16fb_clock_chip(par, var->pixclock, info, 1, 1);
  468. var->red.offset = var->green.offset = var->blue.offset =
  469. var->transp.offset = 0;
  470. var->red.length = var->green.length = var->blue.length =
  471. (par->isVGA) ? 6 : 2;
  472. var->transp.length = 0;
  473. var->activate = FB_ACTIVATE_NOW;
  474. var->height = -1;
  475. var->width = -1;
  476. var->accel_flags = 0;
  477. return 0;
  478. }
  479. #undef FAIL
  480. static int vga16fb_set_par(struct fb_info *info)
  481. {
  482. struct vga16fb_par *par = info->par;
  483. u8 gdc[VGA_GFX_C];
  484. u8 seq[VGA_SEQ_C];
  485. u8 atc[VGA_ATT_C];
  486. int fh, i;
  487. seq[VGA_SEQ_CLOCK_MODE] = 0x01 | par->clkdiv;
  488. if (par->mode & MODE_TEXT)
  489. seq[VGA_SEQ_PLANE_WRITE] = 0x03;
  490. else
  491. seq[VGA_SEQ_PLANE_WRITE] = 0x0F;
  492. seq[VGA_SEQ_CHARACTER_MAP] = 0x00;
  493. if (par->mode & MODE_TEXT)
  494. seq[VGA_SEQ_MEMORY_MODE] = 0x03;
  495. else if (par->mode & MODE_SKIP4)
  496. seq[VGA_SEQ_MEMORY_MODE] = 0x0E;
  497. else
  498. seq[VGA_SEQ_MEMORY_MODE] = 0x06;
  499. gdc[VGA_GFX_SR_VALUE] = 0x00;
  500. gdc[VGA_GFX_SR_ENABLE] = 0x00;
  501. gdc[VGA_GFX_COMPARE_VALUE] = 0x00;
  502. gdc[VGA_GFX_DATA_ROTATE] = 0x00;
  503. gdc[VGA_GFX_PLANE_READ] = 0;
  504. if (par->mode & MODE_TEXT) {
  505. gdc[VGA_GFX_MODE] = 0x10;
  506. gdc[VGA_GFX_MISC] = 0x06;
  507. } else {
  508. if (par->mode & MODE_CFB)
  509. gdc[VGA_GFX_MODE] = 0x40;
  510. else
  511. gdc[VGA_GFX_MODE] = 0x00;
  512. gdc[VGA_GFX_MISC] = 0x05;
  513. }
  514. gdc[VGA_GFX_COMPARE_MASK] = 0x0F;
  515. gdc[VGA_GFX_BIT_MASK] = 0xFF;
  516. for (i = 0x00; i < 0x10; i++)
  517. atc[i] = i;
  518. if (par->mode & MODE_TEXT)
  519. atc[VGA_ATC_MODE] = 0x04;
  520. else if (par->mode & MODE_8BPP)
  521. atc[VGA_ATC_MODE] = 0x41;
  522. else
  523. atc[VGA_ATC_MODE] = 0x81;
  524. atc[VGA_ATC_OVERSCAN] = 0x00; /* 0 for EGA, 0xFF for VGA */
  525. atc[VGA_ATC_PLANE_ENABLE] = 0x0F;
  526. if (par->mode & MODE_8BPP)
  527. atc[VGA_ATC_PEL] = (info->var.xoffset & 3) << 1;
  528. else
  529. atc[VGA_ATC_PEL] = info->var.xoffset & 7;
  530. atc[VGA_ATC_COLOR_PAGE] = 0x00;
  531. if (par->mode & MODE_TEXT) {
  532. fh = 16; // FIXME !!! Fudge font height.
  533. par->crtc[VGA_CRTC_MAX_SCAN] = (par->crtc[VGA_CRTC_MAX_SCAN]
  534. & ~0x1F) | (fh - 1);
  535. }
  536. vga_io_w(VGA_MIS_W, vga_io_r(VGA_MIS_R) | 0x01);
  537. /* Enable graphics register modification */
  538. if (!par->isVGA) {
  539. vga_io_w(EGA_GFX_E0, 0x00);
  540. vga_io_w(EGA_GFX_E1, 0x01);
  541. }
  542. /* update misc output register */
  543. vga_io_w(VGA_MIS_W, par->misc);
  544. /* synchronous reset on */
  545. vga_io_wseq(0x00, 0x01);
  546. if (par->isVGA)
  547. vga_io_w(VGA_PEL_MSK, par->pel_msk);
  548. /* write sequencer registers */
  549. vga_io_wseq(VGA_SEQ_CLOCK_MODE, seq[VGA_SEQ_CLOCK_MODE] | 0x20);
  550. for (i = 2; i < VGA_SEQ_C; i++) {
  551. vga_io_wseq(i, seq[i]);
  552. }
  553. /* synchronous reset off */
  554. vga_io_wseq(0x00, 0x03);
  555. /* deprotect CRT registers 0-7 */
  556. vga_io_wcrt(VGA_CRTC_V_SYNC_END, par->crtc[VGA_CRTC_V_SYNC_END]);
  557. /* write CRT registers */
  558. for (i = 0; i < VGA_CRTC_REGS; i++) {
  559. vga_io_wcrt(i, par->crtc[i]);
  560. }
  561. /* write graphics controller registers */
  562. for (i = 0; i < VGA_GFX_C; i++) {
  563. vga_io_wgfx(i, gdc[i]);
  564. }
  565. /* write attribute controller registers */
  566. for (i = 0; i < VGA_ATT_C; i++) {
  567. vga_io_r(VGA_IS1_RC); /* reset flip-flop */
  568. vga_io_wattr(i, atc[i]);
  569. }
  570. /* Wait for screen to stabilize. */
  571. mdelay(50);
  572. vga_io_wseq(VGA_SEQ_CLOCK_MODE, seq[VGA_SEQ_CLOCK_MODE]);
  573. vga_io_r(VGA_IS1_RC);
  574. vga_io_w(VGA_ATT_IW, 0x20);
  575. vga16fb_update_fix(info);
  576. return 0;
  577. }
  578. static void ega16_setpalette(int regno, unsigned red, unsigned green, unsigned blue)
  579. {
  580. static unsigned char map[] = { 000, 001, 010, 011 };
  581. int val;
  582. if (regno >= 16)
  583. return;
  584. val = map[red>>14] | ((map[green>>14]) << 1) | ((map[blue>>14]) << 2);
  585. vga_io_r(VGA_IS1_RC); /* ! 0x3BA */
  586. vga_io_wattr(regno, val);
  587. vga_io_r(VGA_IS1_RC); /* some clones need it */
  588. vga_io_w(VGA_ATT_IW, 0x20); /* unblank screen */
  589. }
  590. static void vga16_setpalette(int regno, unsigned red, unsigned green, unsigned blue)
  591. {
  592. outb(regno, dac_reg);
  593. outb(red >> 10, dac_val);
  594. outb(green >> 10, dac_val);
  595. outb(blue >> 10, dac_val);
  596. }
  597. static int vga16fb_setcolreg(unsigned regno, unsigned red, unsigned green,
  598. unsigned blue, unsigned transp,
  599. struct fb_info *info)
  600. {
  601. struct vga16fb_par *par = info->par;
  602. int gray;
  603. /*
  604. * Set a single color register. The values supplied are
  605. * already rounded down to the hardware's capabilities
  606. * (according to the entries in the `var' structure). Return
  607. * != 0 for invalid regno.
  608. */
  609. if (regno >= 256)
  610. return 1;
  611. gray = info->var.grayscale;
  612. if (gray) {
  613. /* gray = 0.30*R + 0.59*G + 0.11*B */
  614. red = green = blue = (red * 77 + green * 151 + blue * 28) >> 8;
  615. }
  616. if (par->isVGA)
  617. vga16_setpalette(regno,red,green,blue);
  618. else
  619. ega16_setpalette(regno,red,green,blue);
  620. return 0;
  621. }
  622. static int vga16fb_pan_display(struct fb_var_screeninfo *var,
  623. struct fb_info *info)
  624. {
  625. vga16fb_pan_var(info, var);
  626. return 0;
  627. }
  628. /* The following VESA blanking code is taken from vgacon.c. The VGA
  629. blanking code was originally by Huang shi chao, and modified by
  630. Christoph Rimek (chrimek@toppoint.de) and todd j. derr
  631. (tjd@barefoot.org) for Linux. */
  632. #define attrib_port VGA_ATC_IW
  633. #define seq_port_reg VGA_SEQ_I
  634. #define seq_port_val VGA_SEQ_D
  635. #define gr_port_reg VGA_GFX_I
  636. #define gr_port_val VGA_GFX_D
  637. #define video_misc_rd VGA_MIS_R
  638. #define video_misc_wr VGA_MIS_W
  639. #define vga_video_port_reg VGA_CRT_IC
  640. #define vga_video_port_val VGA_CRT_DC
  641. static void vga_vesa_blank(struct vga16fb_par *par, int mode)
  642. {
  643. unsigned char SeqCtrlIndex;
  644. unsigned char CrtCtrlIndex;
  645. //cli();
  646. SeqCtrlIndex = vga_io_r(seq_port_reg);
  647. CrtCtrlIndex = vga_io_r(vga_video_port_reg);
  648. /* save original values of VGA controller registers */
  649. if(!par->vesa_blanked) {
  650. par->vga_state.CrtMiscIO = vga_io_r(video_misc_rd);
  651. //sti();
  652. par->vga_state.HorizontalTotal = vga_io_rcrt(0x00); /* HorizontalTotal */
  653. par->vga_state.HorizDisplayEnd = vga_io_rcrt(0x01); /* HorizDisplayEnd */
  654. par->vga_state.StartHorizRetrace = vga_io_rcrt(0x04); /* StartHorizRetrace */
  655. par->vga_state.EndHorizRetrace = vga_io_rcrt(0x05); /* EndHorizRetrace */
  656. par->vga_state.Overflow = vga_io_rcrt(0x07); /* Overflow */
  657. par->vga_state.StartVertRetrace = vga_io_rcrt(0x10); /* StartVertRetrace */
  658. par->vga_state.EndVertRetrace = vga_io_rcrt(0x11); /* EndVertRetrace */
  659. par->vga_state.ModeControl = vga_io_rcrt(0x17); /* ModeControl */
  660. par->vga_state.ClockingMode = vga_io_rseq(0x01); /* ClockingMode */
  661. }
  662. /* assure that video is enabled */
  663. /* "0x20" is VIDEO_ENABLE_bit in register 01 of sequencer */
  664. //cli();
  665. vga_io_wseq(0x01, par->vga_state.ClockingMode | 0x20);
  666. /* test for vertical retrace in process.... */
  667. if ((par->vga_state.CrtMiscIO & 0x80) == 0x80)
  668. vga_io_w(video_misc_wr, par->vga_state.CrtMiscIO & 0xef);
  669. /*
  670. * Set <End of vertical retrace> to minimum (0) and
  671. * <Start of vertical Retrace> to maximum (incl. overflow)
  672. * Result: turn off vertical sync (VSync) pulse.
  673. */
  674. if (mode & FB_BLANK_VSYNC_SUSPEND) {
  675. outb_p(0x10,vga_video_port_reg); /* StartVertRetrace */
  676. outb_p(0xff,vga_video_port_val); /* maximum value */
  677. outb_p(0x11,vga_video_port_reg); /* EndVertRetrace */
  678. outb_p(0x40,vga_video_port_val); /* minimum (bits 0..3) */
  679. outb_p(0x07,vga_video_port_reg); /* Overflow */
  680. outb_p(par->vga_state.Overflow | 0x84,vga_video_port_val); /* bits 9,10 of vert. retrace */
  681. }
  682. if (mode & FB_BLANK_HSYNC_SUSPEND) {
  683. /*
  684. * Set <End of horizontal retrace> to minimum (0) and
  685. * <Start of horizontal Retrace> to maximum
  686. * Result: turn off horizontal sync (HSync) pulse.
  687. */
  688. outb_p(0x04,vga_video_port_reg); /* StartHorizRetrace */
  689. outb_p(0xff,vga_video_port_val); /* maximum */
  690. outb_p(0x05,vga_video_port_reg); /* EndHorizRetrace */
  691. outb_p(0x00,vga_video_port_val); /* minimum (0) */
  692. }
  693. /* restore both index registers */
  694. outb_p(SeqCtrlIndex,seq_port_reg);
  695. outb_p(CrtCtrlIndex,vga_video_port_reg);
  696. //sti();
  697. }
  698. static void vga_vesa_unblank(struct vga16fb_par *par)
  699. {
  700. unsigned char SeqCtrlIndex;
  701. unsigned char CrtCtrlIndex;
  702. //cli();
  703. SeqCtrlIndex = vga_io_r(seq_port_reg);
  704. CrtCtrlIndex = vga_io_r(vga_video_port_reg);
  705. /* restore original values of VGA controller registers */
  706. vga_io_w(video_misc_wr, par->vga_state.CrtMiscIO);
  707. /* HorizontalTotal */
  708. vga_io_wcrt(0x00, par->vga_state.HorizontalTotal);
  709. /* HorizDisplayEnd */
  710. vga_io_wcrt(0x01, par->vga_state.HorizDisplayEnd);
  711. /* StartHorizRetrace */
  712. vga_io_wcrt(0x04, par->vga_state.StartHorizRetrace);
  713. /* EndHorizRetrace */
  714. vga_io_wcrt(0x05, par->vga_state.EndHorizRetrace);
  715. /* Overflow */
  716. vga_io_wcrt(0x07, par->vga_state.Overflow);
  717. /* StartVertRetrace */
  718. vga_io_wcrt(0x10, par->vga_state.StartVertRetrace);
  719. /* EndVertRetrace */
  720. vga_io_wcrt(0x11, par->vga_state.EndVertRetrace);
  721. /* ModeControl */
  722. vga_io_wcrt(0x17, par->vga_state.ModeControl);
  723. /* ClockingMode */
  724. vga_io_wseq(0x01, par->vga_state.ClockingMode);
  725. /* restore index/control registers */
  726. vga_io_w(seq_port_reg, SeqCtrlIndex);
  727. vga_io_w(vga_video_port_reg, CrtCtrlIndex);
  728. //sti();
  729. }
  730. static void vga_pal_blank(void)
  731. {
  732. int i;
  733. for (i=0; i<16; i++) {
  734. outb_p (i, dac_reg) ;
  735. outb_p (0, dac_val) ;
  736. outb_p (0, dac_val) ;
  737. outb_p (0, dac_val) ;
  738. }
  739. }
  740. /* 0 unblank, 1 blank, 2 no vsync, 3 no hsync, 4 off */
  741. static int vga16fb_blank(int blank, struct fb_info *info)
  742. {
  743. struct vga16fb_par *par = info->par;
  744. switch (blank) {
  745. case FB_BLANK_UNBLANK: /* Unblank */
  746. if (par->vesa_blanked) {
  747. vga_vesa_unblank(par);
  748. par->vesa_blanked = 0;
  749. }
  750. if (par->palette_blanked) {
  751. par->palette_blanked = 0;
  752. }
  753. break;
  754. case FB_BLANK_NORMAL: /* blank */
  755. vga_pal_blank();
  756. par->palette_blanked = 1;
  757. break;
  758. default: /* VESA blanking */
  759. vga_vesa_blank(par, blank);
  760. par->vesa_blanked = 1;
  761. break;
  762. }
  763. return 0;
  764. }
  765. static void vga_8planes_fillrect(struct fb_info *info, const struct fb_fillrect *rect)
  766. {
  767. u32 dx = rect->dx, width = rect->width;
  768. char oldindex = getindex();
  769. char oldmode = setmode(0x40);
  770. char oldmask = selectmask();
  771. int line_ofs, height;
  772. char oldop, oldsr;
  773. char __iomem *where;
  774. dx /= 4;
  775. where = info->screen_base + dx + rect->dy * info->fix.line_length;
  776. if (rect->rop == ROP_COPY) {
  777. oldop = setop(0);
  778. oldsr = setsr(0);
  779. width /= 4;
  780. line_ofs = info->fix.line_length - width;
  781. setmask(0xff);
  782. height = rect->height;
  783. while (height--) {
  784. int x;
  785. /* we can do memset... */
  786. for (x = width; x > 0; --x) {
  787. writeb(rect->color, where);
  788. where++;
  789. }
  790. where += line_ofs;
  791. }
  792. } else {
  793. char oldcolor = setcolor(0xf);
  794. int y;
  795. oldop = setop(0x18);
  796. oldsr = setsr(0xf);
  797. setmask(0x0F);
  798. for (y = 0; y < rect->height; y++) {
  799. rmw(where);
  800. rmw(where+1);
  801. where += info->fix.line_length;
  802. }
  803. setcolor(oldcolor);
  804. }
  805. setmask(oldmask);
  806. setsr(oldsr);
  807. setop(oldop);
  808. setmode(oldmode);
  809. setindex(oldindex);
  810. }
  811. static void vga16fb_fillrect(struct fb_info *info, const struct fb_fillrect *rect)
  812. {
  813. int x, x2, y2, vxres, vyres, width, height, line_ofs;
  814. char __iomem *dst;
  815. vxres = info->var.xres_virtual;
  816. vyres = info->var.yres_virtual;
  817. if (!rect->width || !rect->height || rect->dx > vxres || rect->dy > vyres)
  818. return;
  819. /* We could use hardware clipping but on many cards you get around
  820. * hardware clipping by writing to framebuffer directly. */
  821. x2 = rect->dx + rect->width;
  822. y2 = rect->dy + rect->height;
  823. x2 = x2 < vxres ? x2 : vxres;
  824. y2 = y2 < vyres ? y2 : vyres;
  825. width = x2 - rect->dx;
  826. switch (info->fix.type) {
  827. case FB_TYPE_VGA_PLANES:
  828. if (info->fix.type_aux == FB_AUX_VGA_PLANES_VGA4) {
  829. height = y2 - rect->dy;
  830. width = rect->width/8;
  831. line_ofs = info->fix.line_length - width;
  832. dst = info->screen_base + (rect->dx/8) + rect->dy * info->fix.line_length;
  833. switch (rect->rop) {
  834. case ROP_COPY:
  835. setmode(0);
  836. setop(0);
  837. setsr(0xf);
  838. setcolor(rect->color);
  839. selectmask();
  840. setmask(0xff);
  841. while (height--) {
  842. for (x = 0; x < width; x++) {
  843. writeb(0, dst);
  844. dst++;
  845. }
  846. dst += line_ofs;
  847. }
  848. break;
  849. case ROP_XOR:
  850. setmode(0);
  851. setop(0x18);
  852. setsr(0xf);
  853. setcolor(0xf);
  854. selectmask();
  855. setmask(0xff);
  856. while (height--) {
  857. for (x = 0; x < width; x++) {
  858. rmw(dst);
  859. dst++;
  860. }
  861. dst += line_ofs;
  862. }
  863. break;
  864. }
  865. } else
  866. vga_8planes_fillrect(info, rect);
  867. break;
  868. case FB_TYPE_PACKED_PIXELS:
  869. default:
  870. cfb_fillrect(info, rect);
  871. break;
  872. }
  873. }
  874. static void vga_8planes_copyarea(struct fb_info *info, const struct fb_copyarea *area)
  875. {
  876. char oldindex = getindex();
  877. char oldmode = setmode(0x41);
  878. char oldop = setop(0);
  879. char oldsr = setsr(0xf);
  880. int height, line_ofs, x;
  881. u32 sx, dx, width;
  882. char __iomem *dest;
  883. char __iomem *src;
  884. height = area->height;
  885. sx = area->sx / 4;
  886. dx = area->dx / 4;
  887. width = area->width / 4;
  888. if (area->dy < area->sy || (area->dy == area->sy && dx < sx)) {
  889. line_ofs = info->fix.line_length - width;
  890. dest = info->screen_base + dx + area->dy * info->fix.line_length;
  891. src = info->screen_base + sx + area->sy * info->fix.line_length;
  892. while (height--) {
  893. for (x = 0; x < width; x++) {
  894. readb(src);
  895. writeb(0, dest);
  896. src++;
  897. dest++;
  898. }
  899. src += line_ofs;
  900. dest += line_ofs;
  901. }
  902. } else {
  903. line_ofs = info->fix.line_length - width;
  904. dest = info->screen_base + dx + width +
  905. (area->dy + height - 1) * info->fix.line_length;
  906. src = info->screen_base + sx + width +
  907. (area->sy + height - 1) * info->fix.line_length;
  908. while (height--) {
  909. for (x = 0; x < width; x++) {
  910. --src;
  911. --dest;
  912. readb(src);
  913. writeb(0, dest);
  914. }
  915. src -= line_ofs;
  916. dest -= line_ofs;
  917. }
  918. }
  919. setsr(oldsr);
  920. setop(oldop);
  921. setmode(oldmode);
  922. setindex(oldindex);
  923. }
  924. static void vga16fb_copyarea(struct fb_info *info, const struct fb_copyarea *area)
  925. {
  926. u32 dx = area->dx, dy = area->dy, sx = area->sx, sy = area->sy;
  927. int x, x2, y2, old_dx, old_dy, vxres, vyres;
  928. int height, width, line_ofs;
  929. char __iomem *dst = NULL;
  930. char __iomem *src = NULL;
  931. vxres = info->var.xres_virtual;
  932. vyres = info->var.yres_virtual;
  933. if (area->dx > vxres || area->sx > vxres || area->dy > vyres ||
  934. area->sy > vyres)
  935. return;
  936. /* clip the destination */
  937. old_dx = area->dx;
  938. old_dy = area->dy;
  939. /*
  940. * We could use hardware clipping but on many cards you get around
  941. * hardware clipping by writing to framebuffer directly.
  942. */
  943. x2 = area->dx + area->width;
  944. y2 = area->dy + area->height;
  945. dx = area->dx > 0 ? area->dx : 0;
  946. dy = area->dy > 0 ? area->dy : 0;
  947. x2 = x2 < vxres ? x2 : vxres;
  948. y2 = y2 < vyres ? y2 : vyres;
  949. width = x2 - dx;
  950. height = y2 - dy;
  951. /* update sx1,sy1 */
  952. sx += (dx - old_dx);
  953. sy += (dy - old_dy);
  954. /* the source must be completely inside the virtual screen */
  955. if (sx < 0 || sy < 0 || (sx + width) > vxres || (sy + height) > vyres)
  956. return;
  957. switch (info->fix.type) {
  958. case FB_TYPE_VGA_PLANES:
  959. if (info->fix.type_aux == FB_AUX_VGA_PLANES_VGA4) {
  960. width = width/8;
  961. height = height;
  962. line_ofs = info->fix.line_length - width;
  963. setmode(1);
  964. setop(0);
  965. setsr(0xf);
  966. if (dy < sy || (dy == sy && dx < sx)) {
  967. dst = info->screen_base + (dx/8) + dy * info->fix.line_length;
  968. src = info->screen_base + (sx/8) + sy * info->fix.line_length;
  969. while (height--) {
  970. for (x = 0; x < width; x++) {
  971. readb(src);
  972. writeb(0, dst);
  973. dst++;
  974. src++;
  975. }
  976. src += line_ofs;
  977. dst += line_ofs;
  978. }
  979. } else {
  980. dst = info->screen_base + (dx/8) + width +
  981. (dy + height - 1) * info->fix.line_length;
  982. src = info->screen_base + (sx/8) + width +
  983. (sy + height - 1) * info->fix.line_length;
  984. while (height--) {
  985. for (x = 0; x < width; x++) {
  986. dst--;
  987. src--;
  988. readb(src);
  989. writeb(0, dst);
  990. }
  991. src -= line_ofs;
  992. dst -= line_ofs;
  993. }
  994. }
  995. } else
  996. vga_8planes_copyarea(info, area);
  997. break;
  998. case FB_TYPE_PACKED_PIXELS:
  999. default:
  1000. cfb_copyarea(info, area);
  1001. break;
  1002. }
  1003. }
  1004. #ifdef __LITTLE_ENDIAN
  1005. static unsigned int transl_l[] =
  1006. {0x0,0x8,0x4,0xC,0x2,0xA,0x6,0xE,0x1,0x9,0x5,0xD,0x3,0xB,0x7,0xF};
  1007. static unsigned int transl_h[] =
  1008. {0x000, 0x800, 0x400, 0xC00, 0x200, 0xA00, 0x600, 0xE00,
  1009. 0x100, 0x900, 0x500, 0xD00, 0x300, 0xB00, 0x700, 0xF00};
  1010. #else
  1011. #ifdef __BIG_ENDIAN
  1012. static unsigned int transl_h[] =
  1013. {0x0,0x8,0x4,0xC,0x2,0xA,0x6,0xE,0x1,0x9,0x5,0xD,0x3,0xB,0x7,0xF};
  1014. static unsigned int transl_l[] =
  1015. {0x000, 0x800, 0x400, 0xC00, 0x200, 0xA00, 0x600, 0xE00,
  1016. 0x100, 0x900, 0x500, 0xD00, 0x300, 0xB00, 0x700, 0xF00};
  1017. #else
  1018. #error "Only __BIG_ENDIAN and __LITTLE_ENDIAN are supported in vga-planes"
  1019. #endif
  1020. #endif
  1021. static void vga_8planes_imageblit(struct fb_info *info, const struct fb_image *image)
  1022. {
  1023. char oldindex = getindex();
  1024. char oldmode = setmode(0x40);
  1025. char oldop = setop(0);
  1026. char oldsr = setsr(0);
  1027. char oldmask = selectmask();
  1028. const char *cdat = image->data;
  1029. u32 dx = image->dx;
  1030. char __iomem *where;
  1031. int y;
  1032. dx /= 4;
  1033. where = info->screen_base + dx + image->dy * info->fix.line_length;
  1034. setmask(0xff);
  1035. writeb(image->bg_color, where);
  1036. readb(where);
  1037. selectmask();
  1038. setmask(image->fg_color ^ image->bg_color);
  1039. setmode(0x42);
  1040. setop(0x18);
  1041. for (y = 0; y < image->height; y++, where += info->fix.line_length)
  1042. writew(transl_h[cdat[y]&0xF] | transl_l[cdat[y] >> 4], where);
  1043. setmask(oldmask);
  1044. setsr(oldsr);
  1045. setop(oldop);
  1046. setmode(oldmode);
  1047. setindex(oldindex);
  1048. }
  1049. static void vga_imageblit_expand(struct fb_info *info, const struct fb_image *image)
  1050. {
  1051. char __iomem *where = info->screen_base + (image->dx/8) +
  1052. image->dy * info->fix.line_length;
  1053. struct vga16fb_par *par = info->par;
  1054. char *cdat = (char *) image->data;
  1055. char __iomem *dst;
  1056. int x, y;
  1057. switch (info->fix.type) {
  1058. case FB_TYPE_VGA_PLANES:
  1059. if (info->fix.type_aux == FB_AUX_VGA_PLANES_VGA4) {
  1060. if (par->isVGA) {
  1061. setmode(2);
  1062. setop(0);
  1063. setsr(0xf);
  1064. setcolor(image->fg_color);
  1065. selectmask();
  1066. setmask(0xff);
  1067. writeb(image->bg_color, where);
  1068. rmb();
  1069. readb(where); /* fill latches */
  1070. setmode(3);
  1071. wmb();
  1072. for (y = 0; y < image->height; y++) {
  1073. dst = where;
  1074. for (x = image->width/8; x--;)
  1075. writeb(*cdat++, dst++);
  1076. where += info->fix.line_length;
  1077. }
  1078. wmb();
  1079. } else {
  1080. setmode(0);
  1081. setop(0);
  1082. setsr(0xf);
  1083. setcolor(image->bg_color);
  1084. selectmask();
  1085. setmask(0xff);
  1086. for (y = 0; y < image->height; y++) {
  1087. dst = where;
  1088. for (x=image->width/8; x--;){
  1089. rmw(dst);
  1090. setcolor(image->fg_color);
  1091. selectmask();
  1092. if (*cdat) {
  1093. setmask(*cdat++);
  1094. rmw(dst++);
  1095. }
  1096. }
  1097. where += info->fix.line_length;
  1098. }
  1099. }
  1100. } else
  1101. vga_8planes_imageblit(info, image);
  1102. break;
  1103. case FB_TYPE_PACKED_PIXELS:
  1104. default:
  1105. cfb_imageblit(info, image);
  1106. break;
  1107. }
  1108. }
  1109. static void vga_imageblit_color(struct fb_info *info, const struct fb_image *image)
  1110. {
  1111. /*
  1112. * Draw logo
  1113. */
  1114. struct vga16fb_par *par = info->par;
  1115. char __iomem *where =
  1116. info->screen_base + image->dy * info->fix.line_length +
  1117. image->dx/8;
  1118. const char *cdat = image->data;
  1119. char __iomem *dst;
  1120. int x, y;
  1121. switch (info->fix.type) {
  1122. case FB_TYPE_VGA_PLANES:
  1123. if (info->fix.type_aux == FB_AUX_VGA_PLANES_VGA4 &&
  1124. par->isVGA) {
  1125. setsr(0xf);
  1126. setop(0);
  1127. setmode(0);
  1128. for (y = 0; y < image->height; y++) {
  1129. for (x = 0; x < image->width; x++) {
  1130. dst = where + x/8;
  1131. setcolor(*cdat);
  1132. selectmask();
  1133. setmask(1 << (7 - (x % 8)));
  1134. fb_readb(dst);
  1135. fb_writeb(0, dst);
  1136. cdat++;
  1137. }
  1138. where += info->fix.line_length;
  1139. }
  1140. }
  1141. break;
  1142. case FB_TYPE_PACKED_PIXELS:
  1143. cfb_imageblit(info, image);
  1144. break;
  1145. default:
  1146. break;
  1147. }
  1148. }
  1149. static void vga16fb_imageblit(struct fb_info *info, const struct fb_image *image)
  1150. {
  1151. if (image->depth == 1)
  1152. vga_imageblit_expand(info, image);
  1153. else
  1154. vga_imageblit_color(info, image);
  1155. }
  1156. static struct fb_ops vga16fb_ops = {
  1157. .owner = THIS_MODULE,
  1158. .fb_open = vga16fb_open,
  1159. .fb_release = vga16fb_release,
  1160. .fb_check_var = vga16fb_check_var,
  1161. .fb_set_par = vga16fb_set_par,
  1162. .fb_setcolreg = vga16fb_setcolreg,
  1163. .fb_pan_display = vga16fb_pan_display,
  1164. .fb_blank = vga16fb_blank,
  1165. .fb_fillrect = vga16fb_fillrect,
  1166. .fb_copyarea = vga16fb_copyarea,
  1167. .fb_imageblit = vga16fb_imageblit,
  1168. };
  1169. #ifndef MODULE
  1170. static int vga16fb_setup(char *options)
  1171. {
  1172. char *this_opt;
  1173. if (!options || !*options)
  1174. return 0;
  1175. while ((this_opt = strsep(&options, ",")) != NULL) {
  1176. if (!*this_opt) continue;
  1177. }
  1178. return 0;
  1179. }
  1180. #endif
  1181. static int __init vga16fb_probe(struct device *device)
  1182. {
  1183. struct platform_device *dev = to_platform_device(device);
  1184. struct fb_info *info;
  1185. struct vga16fb_par *par;
  1186. int i;
  1187. int ret = 0;
  1188. printk(KERN_DEBUG "vga16fb: initializing\n");
  1189. info = framebuffer_alloc(sizeof(struct vga16fb_par), &dev->dev);
  1190. if (!info) {
  1191. ret = -ENOMEM;
  1192. goto err_fb_alloc;
  1193. }
  1194. /* XXX share VGA_FB_PHYS and I/O region with vgacon and others */
  1195. info->screen_base = (void __iomem *)VGA_MAP_MEM(VGA_FB_PHYS);
  1196. if (!info->screen_base) {
  1197. printk(KERN_ERR "vga16fb: unable to map device\n");
  1198. ret = -ENOMEM;
  1199. goto err_ioremap;
  1200. }
  1201. printk(KERN_INFO "vga16fb: mapped to 0x%p\n", info->screen_base);
  1202. par = info->par;
  1203. par->isVGA = ORIG_VIDEO_ISVGA;
  1204. par->palette_blanked = 0;
  1205. par->vesa_blanked = 0;
  1206. i = par->isVGA? 6 : 2;
  1207. vga16fb_defined.red.length = i;
  1208. vga16fb_defined.green.length = i;
  1209. vga16fb_defined.blue.length = i;
  1210. /* name should not depend on EGA/VGA */
  1211. info->fbops = &vga16fb_ops;
  1212. info->var = vga16fb_defined;
  1213. info->fix = vga16fb_fix;
  1214. info->flags = FBINFO_FLAG_DEFAULT |
  1215. FBINFO_HWACCEL_YPAN;
  1216. i = (info->var.bits_per_pixel == 8) ? 256 : 16;
  1217. ret = fb_alloc_cmap(&info->cmap, i, 0);
  1218. if (ret) {
  1219. printk(KERN_ERR "vga16fb: unable to allocate colormap\n");
  1220. ret = -ENOMEM;
  1221. goto err_alloc_cmap;
  1222. }
  1223. if (vga16fb_check_var(&info->var, info)) {
  1224. printk(KERN_ERR "vga16fb: unable to validate variable\n");
  1225. ret = -EINVAL;
  1226. goto err_check_var;
  1227. }
  1228. vga16fb_update_fix(info);
  1229. if (register_framebuffer(info) < 0) {
  1230. printk(KERN_ERR "vga16fb: unable to register framebuffer\n");
  1231. ret = -EINVAL;
  1232. goto err_check_var;
  1233. }
  1234. printk(KERN_INFO "fb%d: %s frame buffer device\n",
  1235. info->node, info->fix.id);
  1236. dev_set_drvdata(device, info);
  1237. return 0;
  1238. err_check_var:
  1239. fb_dealloc_cmap(&info->cmap);
  1240. err_alloc_cmap:
  1241. iounmap(info->screen_base);
  1242. err_ioremap:
  1243. framebuffer_release(info);
  1244. err_fb_alloc:
  1245. return ret;
  1246. }
  1247. static int vga16fb_remove(struct device *device)
  1248. {
  1249. struct fb_info *info = dev_get_drvdata(device);
  1250. if (info) {
  1251. unregister_framebuffer(info);
  1252. iounmap(info->screen_base);
  1253. fb_dealloc_cmap(&info->cmap);
  1254. /* XXX unshare VGA regions */
  1255. framebuffer_release(info);
  1256. }
  1257. return 0;
  1258. }
  1259. static struct device_driver vga16fb_driver = {
  1260. .name = "vga16fb",
  1261. .bus = &platform_bus_type,
  1262. .probe = vga16fb_probe,
  1263. .remove = vga16fb_remove,
  1264. };
  1265. static struct platform_device vga16fb_device = {
  1266. .name = "vga16fb",
  1267. };
  1268. static int __init vga16fb_init(void)
  1269. {
  1270. int ret;
  1271. #ifndef MODULE
  1272. char *option = NULL;
  1273. if (fb_get_options("vga16fb", &option))
  1274. return -ENODEV;
  1275. vga16fb_setup(option);
  1276. #endif
  1277. ret = driver_register(&vga16fb_driver);
  1278. if (!ret) {
  1279. ret = platform_device_register(&vga16fb_device);
  1280. if (ret)
  1281. driver_unregister(&vga16fb_driver);
  1282. }
  1283. return ret;
  1284. }
  1285. static void __exit vga16fb_exit(void)
  1286. {
  1287. platform_device_unregister(&vga16fb_device);
  1288. driver_unregister(&vga16fb_driver);
  1289. }
  1290. MODULE_LICENSE("GPL");
  1291. module_init(vga16fb_init);
  1292. module_exit(vga16fb_exit);
  1293. /*
  1294. * Overrides for Emacs so that we follow Linus's tabbing style.
  1295. * ---------------------------------------------------------------------------
  1296. * Local variables:
  1297. * c-basic-offset: 8
  1298. * End:
  1299. */