tgafb.c 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552
  1. /*
  2. * linux/drivers/video/tgafb.c -- DEC 21030 TGA frame buffer device
  3. *
  4. * Copyright (C) 1995 Jay Estabrook
  5. * Copyright (C) 1997 Geert Uytterhoeven
  6. * Copyright (C) 1999,2000 Martin Lucina, Tom Zerucha
  7. * Copyright (C) 2002 Richard Henderson
  8. *
  9. * This file is subject to the terms and conditions of the GNU General Public
  10. * License. See the file COPYING in the main directory of this archive for
  11. * more details.
  12. */
  13. #include <linux/module.h>
  14. #include <linux/kernel.h>
  15. #include <linux/sched.h>
  16. #include <linux/errno.h>
  17. #include <linux/string.h>
  18. #include <linux/mm.h>
  19. #include <linux/slab.h>
  20. #include <linux/delay.h>
  21. #include <linux/init.h>
  22. #include <linux/fb.h>
  23. #include <linux/pci.h>
  24. #include <linux/selection.h>
  25. #include <asm/io.h>
  26. #include <video/tgafb.h>
  27. /*
  28. * Local functions.
  29. */
  30. static int tgafb_check_var(struct fb_var_screeninfo *, struct fb_info *);
  31. static int tgafb_set_par(struct fb_info *);
  32. static void tgafb_set_pll(struct tga_par *, int);
  33. static int tgafb_setcolreg(unsigned, unsigned, unsigned, unsigned,
  34. unsigned, struct fb_info *);
  35. static int tgafb_blank(int, struct fb_info *);
  36. static void tgafb_init_fix(struct fb_info *);
  37. static void tgafb_imageblit(struct fb_info *, const struct fb_image *);
  38. static void tgafb_fillrect(struct fb_info *, const struct fb_fillrect *);
  39. static void tgafb_copyarea(struct fb_info *, const struct fb_copyarea *);
  40. static int tgafb_pci_register(struct pci_dev *, const struct pci_device_id *);
  41. static void tgafb_pci_unregister(struct pci_dev *);
  42. static const char *mode_option = "640x480@60";
  43. /*
  44. * Frame buffer operations
  45. */
  46. static struct fb_ops tgafb_ops = {
  47. .owner = THIS_MODULE,
  48. .fb_check_var = tgafb_check_var,
  49. .fb_set_par = tgafb_set_par,
  50. .fb_setcolreg = tgafb_setcolreg,
  51. .fb_blank = tgafb_blank,
  52. .fb_fillrect = tgafb_fillrect,
  53. .fb_copyarea = tgafb_copyarea,
  54. .fb_imageblit = tgafb_imageblit,
  55. };
  56. /*
  57. * PCI registration operations
  58. */
  59. static struct pci_device_id const tgafb_pci_table[] = {
  60. { PCI_VENDOR_ID_DEC, PCI_DEVICE_ID_DEC_TGA, PCI_ANY_ID, PCI_ANY_ID,
  61. 0, 0, 0 }
  62. };
  63. static struct pci_driver tgafb_driver = {
  64. .name = "tgafb",
  65. .id_table = tgafb_pci_table,
  66. .probe = tgafb_pci_register,
  67. .remove = __devexit_p(tgafb_pci_unregister),
  68. };
  69. /**
  70. * tgafb_check_var - Optional function. Validates a var passed in.
  71. * @var: frame buffer variable screen structure
  72. * @info: frame buffer structure that represents a single frame buffer
  73. */
  74. static int
  75. tgafb_check_var(struct fb_var_screeninfo *var, struct fb_info *info)
  76. {
  77. struct tga_par *par = (struct tga_par *)info->par;
  78. if (par->tga_type == TGA_TYPE_8PLANE) {
  79. if (var->bits_per_pixel != 8)
  80. return -EINVAL;
  81. } else {
  82. if (var->bits_per_pixel != 32)
  83. return -EINVAL;
  84. }
  85. if (var->xres_virtual != var->xres || var->yres_virtual != var->yres)
  86. return -EINVAL;
  87. if (var->nonstd)
  88. return -EINVAL;
  89. if (1000000000 / var->pixclock > TGA_PLL_MAX_FREQ)
  90. return -EINVAL;
  91. if ((var->vmode & FB_VMODE_MASK) != FB_VMODE_NONINTERLACED)
  92. return -EINVAL;
  93. /* Some of the acceleration routines assume the line width is
  94. a multiple of 64 bytes. */
  95. if (var->xres * (par->tga_type == TGA_TYPE_8PLANE ? 1 : 4) % 64)
  96. return -EINVAL;
  97. return 0;
  98. }
  99. /**
  100. * tgafb_set_par - Optional function. Alters the hardware state.
  101. * @info: frame buffer structure that represents a single frame buffer
  102. */
  103. static int
  104. tgafb_set_par(struct fb_info *info)
  105. {
  106. static unsigned int const deep_presets[4] = {
  107. 0x00014000,
  108. 0x0001440d,
  109. 0xffffffff,
  110. 0x0001441d
  111. };
  112. static unsigned int const rasterop_presets[4] = {
  113. 0x00000003,
  114. 0x00000303,
  115. 0xffffffff,
  116. 0x00000303
  117. };
  118. static unsigned int const mode_presets[4] = {
  119. 0x00002000,
  120. 0x00002300,
  121. 0xffffffff,
  122. 0x00002300
  123. };
  124. static unsigned int const base_addr_presets[4] = {
  125. 0x00000000,
  126. 0x00000001,
  127. 0xffffffff,
  128. 0x00000001
  129. };
  130. struct tga_par *par = (struct tga_par *) info->par;
  131. u32 htimings, vtimings, pll_freq;
  132. u8 tga_type;
  133. int i, j;
  134. /* Encode video timings. */
  135. htimings = (((info->var.xres/4) & TGA_HORIZ_ACT_LSB)
  136. | (((info->var.xres/4) & 0x600 << 19) & TGA_HORIZ_ACT_MSB));
  137. vtimings = (info->var.yres & TGA_VERT_ACTIVE);
  138. htimings |= ((info->var.right_margin/4) << 9) & TGA_HORIZ_FP;
  139. vtimings |= (info->var.lower_margin << 11) & TGA_VERT_FP;
  140. htimings |= ((info->var.hsync_len/4) << 14) & TGA_HORIZ_SYNC;
  141. vtimings |= (info->var.vsync_len << 16) & TGA_VERT_SYNC;
  142. htimings |= ((info->var.left_margin/4) << 21) & TGA_HORIZ_BP;
  143. vtimings |= (info->var.upper_margin << 22) & TGA_VERT_BP;
  144. if (info->var.sync & FB_SYNC_HOR_HIGH_ACT)
  145. htimings |= TGA_HORIZ_POLARITY;
  146. if (info->var.sync & FB_SYNC_VERT_HIGH_ACT)
  147. vtimings |= TGA_VERT_POLARITY;
  148. par->htimings = htimings;
  149. par->vtimings = vtimings;
  150. par->sync_on_green = !!(info->var.sync & FB_SYNC_ON_GREEN);
  151. /* Store other useful values in par. */
  152. par->xres = info->var.xres;
  153. par->yres = info->var.yres;
  154. par->pll_freq = pll_freq = 1000000000 / info->var.pixclock;
  155. par->bits_per_pixel = info->var.bits_per_pixel;
  156. tga_type = par->tga_type;
  157. /* First, disable video. */
  158. TGA_WRITE_REG(par, TGA_VALID_VIDEO | TGA_VALID_BLANK, TGA_VALID_REG);
  159. /* Write the DEEP register. */
  160. while (TGA_READ_REG(par, TGA_CMD_STAT_REG) & 1) /* wait for not busy */
  161. continue;
  162. mb();
  163. TGA_WRITE_REG(par, deep_presets[tga_type], TGA_DEEP_REG);
  164. while (TGA_READ_REG(par, TGA_CMD_STAT_REG) & 1) /* wait for not busy */
  165. continue;
  166. mb();
  167. /* Write some more registers. */
  168. TGA_WRITE_REG(par, rasterop_presets[tga_type], TGA_RASTEROP_REG);
  169. TGA_WRITE_REG(par, mode_presets[tga_type], TGA_MODE_REG);
  170. TGA_WRITE_REG(par, base_addr_presets[tga_type], TGA_BASE_ADDR_REG);
  171. /* Calculate & write the PLL. */
  172. tgafb_set_pll(par, pll_freq);
  173. /* Write some more registers. */
  174. TGA_WRITE_REG(par, 0xffffffff, TGA_PLANEMASK_REG);
  175. TGA_WRITE_REG(par, 0xffffffff, TGA_PIXELMASK_REG);
  176. /* Init video timing regs. */
  177. TGA_WRITE_REG(par, htimings, TGA_HORIZ_REG);
  178. TGA_WRITE_REG(par, vtimings, TGA_VERT_REG);
  179. /* Initalise RAMDAC. */
  180. if (tga_type == TGA_TYPE_8PLANE) {
  181. /* Init BT485 RAMDAC registers. */
  182. BT485_WRITE(par, 0xa2 | (par->sync_on_green ? 0x8 : 0x0),
  183. BT485_CMD_0);
  184. BT485_WRITE(par, 0x01, BT485_ADDR_PAL_WRITE);
  185. BT485_WRITE(par, 0x14, BT485_CMD_3); /* cursor 64x64 */
  186. BT485_WRITE(par, 0x40, BT485_CMD_1);
  187. BT485_WRITE(par, 0x20, BT485_CMD_2); /* cursor off, for now */
  188. BT485_WRITE(par, 0xff, BT485_PIXEL_MASK);
  189. /* Fill palette registers. */
  190. BT485_WRITE(par, 0x00, BT485_ADDR_PAL_WRITE);
  191. TGA_WRITE_REG(par, BT485_DATA_PAL, TGA_RAMDAC_SETUP_REG);
  192. for (i = 0; i < 16; i++) {
  193. j = color_table[i];
  194. TGA_WRITE_REG(par, default_red[j]|(BT485_DATA_PAL<<8),
  195. TGA_RAMDAC_REG);
  196. TGA_WRITE_REG(par, default_grn[j]|(BT485_DATA_PAL<<8),
  197. TGA_RAMDAC_REG);
  198. TGA_WRITE_REG(par, default_blu[j]|(BT485_DATA_PAL<<8),
  199. TGA_RAMDAC_REG);
  200. }
  201. for (i = 0; i < 240*3; i += 4) {
  202. TGA_WRITE_REG(par, 0x55|(BT485_DATA_PAL<<8),
  203. TGA_RAMDAC_REG);
  204. TGA_WRITE_REG(par, 0x00|(BT485_DATA_PAL<<8),
  205. TGA_RAMDAC_REG);
  206. TGA_WRITE_REG(par, 0x00|(BT485_DATA_PAL<<8),
  207. TGA_RAMDAC_REG);
  208. TGA_WRITE_REG(par, 0x00|(BT485_DATA_PAL<<8),
  209. TGA_RAMDAC_REG);
  210. }
  211. } else { /* 24-plane or 24plusZ */
  212. /* Init BT463 registers. */
  213. BT463_WRITE(par, BT463_REG_ACC, BT463_CMD_REG_0, 0x40);
  214. BT463_WRITE(par, BT463_REG_ACC, BT463_CMD_REG_1, 0x08);
  215. BT463_WRITE(par, BT463_REG_ACC, BT463_CMD_REG_2,
  216. (par->sync_on_green ? 0x80 : 0x40));
  217. BT463_WRITE(par, BT463_REG_ACC, BT463_READ_MASK_0, 0xff);
  218. BT463_WRITE(par, BT463_REG_ACC, BT463_READ_MASK_1, 0xff);
  219. BT463_WRITE(par, BT463_REG_ACC, BT463_READ_MASK_2, 0xff);
  220. BT463_WRITE(par, BT463_REG_ACC, BT463_READ_MASK_3, 0x0f);
  221. BT463_WRITE(par, BT463_REG_ACC, BT463_BLINK_MASK_0, 0x00);
  222. BT463_WRITE(par, BT463_REG_ACC, BT463_BLINK_MASK_1, 0x00);
  223. BT463_WRITE(par, BT463_REG_ACC, BT463_BLINK_MASK_2, 0x00);
  224. BT463_WRITE(par, BT463_REG_ACC, BT463_BLINK_MASK_3, 0x00);
  225. /* Fill the palette. */
  226. BT463_LOAD_ADDR(par, 0x0000);
  227. TGA_WRITE_REG(par, BT463_PALETTE<<2, TGA_RAMDAC_REG);
  228. for (i = 0; i < 16; i++) {
  229. j = color_table[i];
  230. TGA_WRITE_REG(par, default_red[j]|(BT463_PALETTE<<10),
  231. TGA_RAMDAC_REG);
  232. TGA_WRITE_REG(par, default_grn[j]|(BT463_PALETTE<<10),
  233. TGA_RAMDAC_REG);
  234. TGA_WRITE_REG(par, default_blu[j]|(BT463_PALETTE<<10),
  235. TGA_RAMDAC_REG);
  236. }
  237. for (i = 0; i < 512*3; i += 4) {
  238. TGA_WRITE_REG(par, 0x55|(BT463_PALETTE<<10),
  239. TGA_RAMDAC_REG);
  240. TGA_WRITE_REG(par, 0x00|(BT463_PALETTE<<10),
  241. TGA_RAMDAC_REG);
  242. TGA_WRITE_REG(par, 0x00|(BT463_PALETTE<<10),
  243. TGA_RAMDAC_REG);
  244. TGA_WRITE_REG(par, 0x00|(BT463_PALETTE<<10),
  245. TGA_RAMDAC_REG);
  246. }
  247. /* Fill window type table after start of vertical retrace. */
  248. while (!(TGA_READ_REG(par, TGA_INTR_STAT_REG) & 0x01))
  249. continue;
  250. TGA_WRITE_REG(par, 0x01, TGA_INTR_STAT_REG);
  251. mb();
  252. while (!(TGA_READ_REG(par, TGA_INTR_STAT_REG) & 0x01))
  253. continue;
  254. TGA_WRITE_REG(par, 0x01, TGA_INTR_STAT_REG);
  255. BT463_LOAD_ADDR(par, BT463_WINDOW_TYPE_BASE);
  256. TGA_WRITE_REG(par, BT463_REG_ACC<<2, TGA_RAMDAC_SETUP_REG);
  257. for (i = 0; i < 16; i++) {
  258. TGA_WRITE_REG(par, 0x00|(BT463_REG_ACC<<10),
  259. TGA_RAMDAC_REG);
  260. TGA_WRITE_REG(par, 0x01|(BT463_REG_ACC<<10),
  261. TGA_RAMDAC_REG);
  262. TGA_WRITE_REG(par, 0x80|(BT463_REG_ACC<<10),
  263. TGA_RAMDAC_REG);
  264. }
  265. }
  266. /* Finally, enable video scan (and pray for the monitor... :-) */
  267. TGA_WRITE_REG(par, TGA_VALID_VIDEO, TGA_VALID_REG);
  268. return 0;
  269. }
  270. #define DIFFCHECK(X) \
  271. do { \
  272. if (m <= 0x3f) { \
  273. int delta = f - (TGA_PLL_BASE_FREQ * (X)) / (r << shift); \
  274. if (delta < 0) \
  275. delta = -delta; \
  276. if (delta < min_diff) \
  277. min_diff = delta, vm = m, va = a, vr = r; \
  278. } \
  279. } while (0)
  280. static void
  281. tgafb_set_pll(struct tga_par *par, int f)
  282. {
  283. int n, shift, base, min_diff, target;
  284. int r,a,m,vm = 34, va = 1, vr = 30;
  285. for (r = 0 ; r < 12 ; r++)
  286. TGA_WRITE_REG(par, !r, TGA_CLOCK_REG);
  287. if (f > TGA_PLL_MAX_FREQ)
  288. f = TGA_PLL_MAX_FREQ;
  289. if (f >= TGA_PLL_MAX_FREQ / 2)
  290. shift = 0;
  291. else if (f >= TGA_PLL_MAX_FREQ / 4)
  292. shift = 1;
  293. else
  294. shift = 2;
  295. TGA_WRITE_REG(par, shift & 1, TGA_CLOCK_REG);
  296. TGA_WRITE_REG(par, shift >> 1, TGA_CLOCK_REG);
  297. for (r = 0 ; r < 10 ; r++)
  298. TGA_WRITE_REG(par, 0, TGA_CLOCK_REG);
  299. if (f <= 120000) {
  300. TGA_WRITE_REG(par, 0, TGA_CLOCK_REG);
  301. TGA_WRITE_REG(par, 0, TGA_CLOCK_REG);
  302. }
  303. else if (f <= 200000) {
  304. TGA_WRITE_REG(par, 1, TGA_CLOCK_REG);
  305. TGA_WRITE_REG(par, 0, TGA_CLOCK_REG);
  306. }
  307. else {
  308. TGA_WRITE_REG(par, 0, TGA_CLOCK_REG);
  309. TGA_WRITE_REG(par, 1, TGA_CLOCK_REG);
  310. }
  311. TGA_WRITE_REG(par, 1, TGA_CLOCK_REG);
  312. TGA_WRITE_REG(par, 0, TGA_CLOCK_REG);
  313. TGA_WRITE_REG(par, 0, TGA_CLOCK_REG);
  314. TGA_WRITE_REG(par, 1, TGA_CLOCK_REG);
  315. TGA_WRITE_REG(par, 0, TGA_CLOCK_REG);
  316. TGA_WRITE_REG(par, 1, TGA_CLOCK_REG);
  317. target = (f << shift) / TGA_PLL_BASE_FREQ;
  318. min_diff = TGA_PLL_MAX_FREQ;
  319. r = 7 / target;
  320. if (!r) r = 1;
  321. base = target * r;
  322. while (base < 449) {
  323. for (n = base < 7 ? 7 : base; n < base + target && n < 449; n++) {
  324. m = ((n + 3) / 7) - 1;
  325. a = 0;
  326. DIFFCHECK((m + 1) * 7);
  327. m++;
  328. DIFFCHECK((m + 1) * 7);
  329. m = (n / 6) - 1;
  330. if ((a = n % 6))
  331. DIFFCHECK(n);
  332. }
  333. r++;
  334. base += target;
  335. }
  336. vr--;
  337. for (r = 0; r < 8; r++)
  338. TGA_WRITE_REG(par, (vm >> r) & 1, TGA_CLOCK_REG);
  339. for (r = 0; r < 8 ; r++)
  340. TGA_WRITE_REG(par, (va >> r) & 1, TGA_CLOCK_REG);
  341. for (r = 0; r < 7 ; r++)
  342. TGA_WRITE_REG(par, (vr >> r) & 1, TGA_CLOCK_REG);
  343. TGA_WRITE_REG(par, ((vr >> 7) & 1)|2, TGA_CLOCK_REG);
  344. }
  345. /**
  346. * tgafb_setcolreg - Optional function. Sets a color register.
  347. * @regno: boolean, 0 copy local, 1 get_user() function
  348. * @red: frame buffer colormap structure
  349. * @green: The green value which can be up to 16 bits wide
  350. * @blue: The blue value which can be up to 16 bits wide.
  351. * @transp: If supported the alpha value which can be up to 16 bits wide.
  352. * @info: frame buffer info structure
  353. */
  354. static int
  355. tgafb_setcolreg(unsigned regno, unsigned red, unsigned green, unsigned blue,
  356. unsigned transp, struct fb_info *info)
  357. {
  358. struct tga_par *par = (struct tga_par *) info->par;
  359. if (regno > 255)
  360. return 1;
  361. red >>= 8;
  362. green >>= 8;
  363. blue >>= 8;
  364. if (par->tga_type == TGA_TYPE_8PLANE) {
  365. BT485_WRITE(par, regno, BT485_ADDR_PAL_WRITE);
  366. TGA_WRITE_REG(par, BT485_DATA_PAL, TGA_RAMDAC_SETUP_REG);
  367. TGA_WRITE_REG(par, red|(BT485_DATA_PAL<<8),TGA_RAMDAC_REG);
  368. TGA_WRITE_REG(par, green|(BT485_DATA_PAL<<8),TGA_RAMDAC_REG);
  369. TGA_WRITE_REG(par, blue|(BT485_DATA_PAL<<8),TGA_RAMDAC_REG);
  370. } else if (regno < 16) {
  371. u32 value = (red << 16) | (green << 8) | blue;
  372. ((u32 *)info->pseudo_palette)[regno] = value;
  373. }
  374. return 0;
  375. }
  376. /**
  377. * tgafb_blank - Optional function. Blanks the display.
  378. * @blank_mode: the blank mode we want.
  379. * @info: frame buffer structure that represents a single frame buffer
  380. */
  381. static int
  382. tgafb_blank(int blank, struct fb_info *info)
  383. {
  384. struct tga_par *par = (struct tga_par *) info->par;
  385. u32 vhcr, vvcr, vvvr;
  386. unsigned long flags;
  387. local_irq_save(flags);
  388. vhcr = TGA_READ_REG(par, TGA_HORIZ_REG);
  389. vvcr = TGA_READ_REG(par, TGA_VERT_REG);
  390. vvvr = TGA_READ_REG(par, TGA_VALID_REG);
  391. vvvr &= ~(TGA_VALID_VIDEO | TGA_VALID_BLANK);
  392. switch (blank) {
  393. case FB_BLANK_UNBLANK: /* Unblanking */
  394. if (par->vesa_blanked) {
  395. TGA_WRITE_REG(par, vhcr & 0xbfffffff, TGA_HORIZ_REG);
  396. TGA_WRITE_REG(par, vvcr & 0xbfffffff, TGA_VERT_REG);
  397. par->vesa_blanked = 0;
  398. }
  399. TGA_WRITE_REG(par, vvvr | TGA_VALID_VIDEO, TGA_VALID_REG);
  400. break;
  401. case FB_BLANK_NORMAL: /* Normal blanking */
  402. TGA_WRITE_REG(par, vvvr | TGA_VALID_VIDEO | TGA_VALID_BLANK,
  403. TGA_VALID_REG);
  404. break;
  405. case FB_BLANK_VSYNC_SUSPEND: /* VESA blank (vsync off) */
  406. TGA_WRITE_REG(par, vvcr | 0x40000000, TGA_VERT_REG);
  407. TGA_WRITE_REG(par, vvvr | TGA_VALID_BLANK, TGA_VALID_REG);
  408. par->vesa_blanked = 1;
  409. break;
  410. case FB_BLANK_HSYNC_SUSPEND: /* VESA blank (hsync off) */
  411. TGA_WRITE_REG(par, vhcr | 0x40000000, TGA_HORIZ_REG);
  412. TGA_WRITE_REG(par, vvvr | TGA_VALID_BLANK, TGA_VALID_REG);
  413. par->vesa_blanked = 1;
  414. break;
  415. case FB_BLANK_POWERDOWN: /* Poweroff */
  416. TGA_WRITE_REG(par, vhcr | 0x40000000, TGA_HORIZ_REG);
  417. TGA_WRITE_REG(par, vvcr | 0x40000000, TGA_VERT_REG);
  418. TGA_WRITE_REG(par, vvvr | TGA_VALID_BLANK, TGA_VALID_REG);
  419. par->vesa_blanked = 1;
  420. break;
  421. }
  422. local_irq_restore(flags);
  423. return 0;
  424. }
  425. /*
  426. * Acceleration.
  427. */
  428. /**
  429. * tgafb_imageblit - REQUIRED function. Can use generic routines if
  430. * non acclerated hardware and packed pixel based.
  431. * Copies a image from system memory to the screen.
  432. *
  433. * @info: frame buffer structure that represents a single frame buffer
  434. * @image: structure defining the image.
  435. */
  436. static void
  437. tgafb_imageblit(struct fb_info *info, const struct fb_image *image)
  438. {
  439. static unsigned char const bitrev[256] = {
  440. 0x00, 0x80, 0x40, 0xc0, 0x20, 0xa0, 0x60, 0xe0,
  441. 0x10, 0x90, 0x50, 0xd0, 0x30, 0xb0, 0x70, 0xf0,
  442. 0x08, 0x88, 0x48, 0xc8, 0x28, 0xa8, 0x68, 0xe8,
  443. 0x18, 0x98, 0x58, 0xd8, 0x38, 0xb8, 0x78, 0xf8,
  444. 0x04, 0x84, 0x44, 0xc4, 0x24, 0xa4, 0x64, 0xe4,
  445. 0x14, 0x94, 0x54, 0xd4, 0x34, 0xb4, 0x74, 0xf4,
  446. 0x0c, 0x8c, 0x4c, 0xcc, 0x2c, 0xac, 0x6c, 0xec,
  447. 0x1c, 0x9c, 0x5c, 0xdc, 0x3c, 0xbc, 0x7c, 0xfc,
  448. 0x02, 0x82, 0x42, 0xc2, 0x22, 0xa2, 0x62, 0xe2,
  449. 0x12, 0x92, 0x52, 0xd2, 0x32, 0xb2, 0x72, 0xf2,
  450. 0x0a, 0x8a, 0x4a, 0xca, 0x2a, 0xaa, 0x6a, 0xea,
  451. 0x1a, 0x9a, 0x5a, 0xda, 0x3a, 0xba, 0x7a, 0xfa,
  452. 0x06, 0x86, 0x46, 0xc6, 0x26, 0xa6, 0x66, 0xe6,
  453. 0x16, 0x96, 0x56, 0xd6, 0x36, 0xb6, 0x76, 0xf6,
  454. 0x0e, 0x8e, 0x4e, 0xce, 0x2e, 0xae, 0x6e, 0xee,
  455. 0x1e, 0x9e, 0x5e, 0xde, 0x3e, 0xbe, 0x7e, 0xfe,
  456. 0x01, 0x81, 0x41, 0xc1, 0x21, 0xa1, 0x61, 0xe1,
  457. 0x11, 0x91, 0x51, 0xd1, 0x31, 0xb1, 0x71, 0xf1,
  458. 0x09, 0x89, 0x49, 0xc9, 0x29, 0xa9, 0x69, 0xe9,
  459. 0x19, 0x99, 0x59, 0xd9, 0x39, 0xb9, 0x79, 0xf9,
  460. 0x05, 0x85, 0x45, 0xc5, 0x25, 0xa5, 0x65, 0xe5,
  461. 0x15, 0x95, 0x55, 0xd5, 0x35, 0xb5, 0x75, 0xf5,
  462. 0x0d, 0x8d, 0x4d, 0xcd, 0x2d, 0xad, 0x6d, 0xed,
  463. 0x1d, 0x9d, 0x5d, 0xdd, 0x3d, 0xbd, 0x7d, 0xfd,
  464. 0x03, 0x83, 0x43, 0xc3, 0x23, 0xa3, 0x63, 0xe3,
  465. 0x13, 0x93, 0x53, 0xd3, 0x33, 0xb3, 0x73, 0xf3,
  466. 0x0b, 0x8b, 0x4b, 0xcb, 0x2b, 0xab, 0x6b, 0xeb,
  467. 0x1b, 0x9b, 0x5b, 0xdb, 0x3b, 0xbb, 0x7b, 0xfb,
  468. 0x07, 0x87, 0x47, 0xc7, 0x27, 0xa7, 0x67, 0xe7,
  469. 0x17, 0x97, 0x57, 0xd7, 0x37, 0xb7, 0x77, 0xf7,
  470. 0x0f, 0x8f, 0x4f, 0xcf, 0x2f, 0xaf, 0x6f, 0xef,
  471. 0x1f, 0x9f, 0x5f, 0xdf, 0x3f, 0xbf, 0x7f, 0xff
  472. };
  473. struct tga_par *par = (struct tga_par *) info->par;
  474. u32 fgcolor, bgcolor, dx, dy, width, height, vxres, vyres, pixelmask;
  475. unsigned long rincr, line_length, shift, pos, is8bpp;
  476. unsigned long i, j;
  477. const unsigned char *data;
  478. void __iomem *regs_base;
  479. void __iomem *fb_base;
  480. dx = image->dx;
  481. dy = image->dy;
  482. width = image->width;
  483. height = image->height;
  484. vxres = info->var.xres_virtual;
  485. vyres = info->var.yres_virtual;
  486. line_length = info->fix.line_length;
  487. rincr = (width + 7) / 8;
  488. /* Crop the image to the screen. */
  489. if (dx > vxres || dy > vyres)
  490. return;
  491. if (dx + width > vxres)
  492. width = vxres - dx;
  493. if (dy + height > vyres)
  494. height = vyres - dy;
  495. /* For copies that aren't pixel expansion, there's little we
  496. can do better than the generic code. */
  497. /* ??? There is a DMA write mode; I wonder if that could be
  498. made to pull the data from the image buffer... */
  499. if (image->depth > 1) {
  500. cfb_imageblit(info, image);
  501. return;
  502. }
  503. regs_base = par->tga_regs_base;
  504. fb_base = par->tga_fb_base;
  505. is8bpp = info->var.bits_per_pixel == 8;
  506. /* Expand the color values to fill 32-bits. */
  507. /* ??? Would be nice to notice colour changes elsewhere, so
  508. that we can do this only when necessary. */
  509. fgcolor = image->fg_color;
  510. bgcolor = image->bg_color;
  511. if (is8bpp) {
  512. fgcolor |= fgcolor << 8;
  513. fgcolor |= fgcolor << 16;
  514. bgcolor |= bgcolor << 8;
  515. bgcolor |= bgcolor << 16;
  516. } else {
  517. if (fgcolor < 16)
  518. fgcolor = ((u32 *)info->pseudo_palette)[fgcolor];
  519. if (bgcolor < 16)
  520. bgcolor = ((u32 *)info->pseudo_palette)[bgcolor];
  521. }
  522. __raw_writel(fgcolor, regs_base + TGA_FOREGROUND_REG);
  523. __raw_writel(bgcolor, regs_base + TGA_BACKGROUND_REG);
  524. /* Acquire proper alignment; set up the PIXELMASK register
  525. so that we only write the proper character cell. */
  526. pos = dy * line_length;
  527. if (is8bpp) {
  528. pos += dx;
  529. shift = pos & 3;
  530. pos &= -4;
  531. } else {
  532. pos += dx * 4;
  533. shift = (pos & 7) >> 2;
  534. pos &= -8;
  535. }
  536. data = (const unsigned char *) image->data;
  537. /* Enable opaque stipple mode. */
  538. __raw_writel((is8bpp
  539. ? TGA_MODE_SBM_8BPP | TGA_MODE_OPAQUE_STIPPLE
  540. : TGA_MODE_SBM_24BPP | TGA_MODE_OPAQUE_STIPPLE),
  541. regs_base + TGA_MODE_REG);
  542. if (width + shift <= 32) {
  543. unsigned long bwidth;
  544. /* Handle common case of imaging a single character, in
  545. a font less than 32 pixels wide. */
  546. pixelmask = (1 << width) - 1;
  547. pixelmask <<= shift;
  548. __raw_writel(pixelmask, regs_base + TGA_PIXELMASK_REG);
  549. wmb();
  550. bwidth = (width + 7) / 8;
  551. for (i = 0; i < height; ++i) {
  552. u32 mask = 0;
  553. /* The image data is bit big endian; we need
  554. little endian. */
  555. for (j = 0; j < bwidth; ++j)
  556. mask |= bitrev[data[j]] << (j * 8);
  557. __raw_writel(mask << shift, fb_base + pos);
  558. pos += line_length;
  559. data += rincr;
  560. }
  561. wmb();
  562. __raw_writel(0xffffffff, regs_base + TGA_PIXELMASK_REG);
  563. } else if (shift == 0) {
  564. unsigned long pos0 = pos;
  565. const unsigned char *data0 = data;
  566. unsigned long bincr = (is8bpp ? 8 : 8*4);
  567. unsigned long bwidth;
  568. /* Handle another common case in which accel_putcs
  569. generates a large bitmap, which happens to be aligned.
  570. Allow the tail to be misaligned. This case is
  571. interesting because we've not got to hold partial
  572. bytes across the words being written. */
  573. wmb();
  574. bwidth = (width / 8) & -4;
  575. for (i = 0; i < height; ++i) {
  576. for (j = 0; j < bwidth; j += 4) {
  577. u32 mask = 0;
  578. mask |= bitrev[data[j+0]] << (0 * 8);
  579. mask |= bitrev[data[j+1]] << (1 * 8);
  580. mask |= bitrev[data[j+2]] << (2 * 8);
  581. mask |= bitrev[data[j+3]] << (3 * 8);
  582. __raw_writel(mask, fb_base + pos + j*bincr);
  583. }
  584. pos += line_length;
  585. data += rincr;
  586. }
  587. wmb();
  588. pixelmask = (1ul << (width & 31)) - 1;
  589. if (pixelmask) {
  590. __raw_writel(pixelmask, regs_base + TGA_PIXELMASK_REG);
  591. wmb();
  592. pos = pos0 + bwidth*bincr;
  593. data = data0 + bwidth;
  594. bwidth = ((width & 31) + 7) / 8;
  595. for (i = 0; i < height; ++i) {
  596. u32 mask = 0;
  597. for (j = 0; j < bwidth; ++j)
  598. mask |= bitrev[data[j]] << (j * 8);
  599. __raw_writel(mask, fb_base + pos);
  600. pos += line_length;
  601. data += rincr;
  602. }
  603. wmb();
  604. __raw_writel(0xffffffff, regs_base + TGA_PIXELMASK_REG);
  605. }
  606. } else {
  607. unsigned long pos0 = pos;
  608. const unsigned char *data0 = data;
  609. unsigned long bincr = (is8bpp ? 8 : 8*4);
  610. unsigned long bwidth;
  611. /* Finally, handle the generic case of misaligned start.
  612. Here we split the write into 16-bit spans. This allows
  613. us to use only one pixel mask, instead of four as would
  614. be required by writing 24-bit spans. */
  615. pixelmask = 0xffff << shift;
  616. __raw_writel(pixelmask, regs_base + TGA_PIXELMASK_REG);
  617. wmb();
  618. bwidth = (width / 8) & -2;
  619. for (i = 0; i < height; ++i) {
  620. for (j = 0; j < bwidth; j += 2) {
  621. u32 mask = 0;
  622. mask |= bitrev[data[j+0]] << (0 * 8);
  623. mask |= bitrev[data[j+1]] << (1 * 8);
  624. mask <<= shift;
  625. __raw_writel(mask, fb_base + pos + j*bincr);
  626. }
  627. pos += line_length;
  628. data += rincr;
  629. }
  630. wmb();
  631. pixelmask = ((1ul << (width & 15)) - 1) << shift;
  632. if (pixelmask) {
  633. __raw_writel(pixelmask, regs_base + TGA_PIXELMASK_REG);
  634. wmb();
  635. pos = pos0 + bwidth*bincr;
  636. data = data0 + bwidth;
  637. bwidth = (width & 15) > 8;
  638. for (i = 0; i < height; ++i) {
  639. u32 mask = bitrev[data[0]];
  640. if (bwidth)
  641. mask |= bitrev[data[1]] << 8;
  642. mask <<= shift;
  643. __raw_writel(mask, fb_base + pos);
  644. pos += line_length;
  645. data += rincr;
  646. }
  647. wmb();
  648. }
  649. __raw_writel(0xffffffff, regs_base + TGA_PIXELMASK_REG);
  650. }
  651. /* Disable opaque stipple mode. */
  652. __raw_writel((is8bpp
  653. ? TGA_MODE_SBM_8BPP | TGA_MODE_SIMPLE
  654. : TGA_MODE_SBM_24BPP | TGA_MODE_SIMPLE),
  655. regs_base + TGA_MODE_REG);
  656. }
  657. /**
  658. * tgafb_fillrect - REQUIRED function. Can use generic routines if
  659. * non acclerated hardware and packed pixel based.
  660. * Draws a rectangle on the screen.
  661. *
  662. * @info: frame buffer structure that represents a single frame buffer
  663. * @rect: structure defining the rectagle and operation.
  664. */
  665. static void
  666. tgafb_fillrect(struct fb_info *info, const struct fb_fillrect *rect)
  667. {
  668. struct tga_par *par = (struct tga_par *) info->par;
  669. int is8bpp = info->var.bits_per_pixel == 8;
  670. u32 dx, dy, width, height, vxres, vyres, color;
  671. unsigned long pos, align, line_length, i, j;
  672. void __iomem *regs_base;
  673. void __iomem *fb_base;
  674. dx = rect->dx;
  675. dy = rect->dy;
  676. width = rect->width;
  677. height = rect->height;
  678. vxres = info->var.xres_virtual;
  679. vyres = info->var.yres_virtual;
  680. line_length = info->fix.line_length;
  681. regs_base = par->tga_regs_base;
  682. fb_base = par->tga_fb_base;
  683. /* Crop the rectangle to the screen. */
  684. if (dx > vxres || dy > vyres || !width || !height)
  685. return;
  686. if (dx + width > vxres)
  687. width = vxres - dx;
  688. if (dy + height > vyres)
  689. height = vyres - dy;
  690. pos = dy * line_length + dx * (is8bpp ? 1 : 4);
  691. /* ??? We could implement ROP_XOR with opaque fill mode
  692. and a RasterOp setting of GXxor, but as far as I can
  693. tell, this mode is not actually used in the kernel.
  694. Thus I am ignoring it for now. */
  695. if (rect->rop != ROP_COPY) {
  696. cfb_fillrect(info, rect);
  697. return;
  698. }
  699. /* Expand the color value to fill 8 pixels. */
  700. color = rect->color;
  701. if (is8bpp) {
  702. color |= color << 8;
  703. color |= color << 16;
  704. __raw_writel(color, regs_base + TGA_BLOCK_COLOR0_REG);
  705. __raw_writel(color, regs_base + TGA_BLOCK_COLOR1_REG);
  706. } else {
  707. if (color < 16)
  708. color = ((u32 *)info->pseudo_palette)[color];
  709. __raw_writel(color, regs_base + TGA_BLOCK_COLOR0_REG);
  710. __raw_writel(color, regs_base + TGA_BLOCK_COLOR1_REG);
  711. __raw_writel(color, regs_base + TGA_BLOCK_COLOR2_REG);
  712. __raw_writel(color, regs_base + TGA_BLOCK_COLOR3_REG);
  713. __raw_writel(color, regs_base + TGA_BLOCK_COLOR4_REG);
  714. __raw_writel(color, regs_base + TGA_BLOCK_COLOR5_REG);
  715. __raw_writel(color, regs_base + TGA_BLOCK_COLOR6_REG);
  716. __raw_writel(color, regs_base + TGA_BLOCK_COLOR7_REG);
  717. }
  718. /* The DATA register holds the fill mask for block fill mode.
  719. Since we're not stippling, this is all ones. */
  720. __raw_writel(0xffffffff, regs_base + TGA_DATA_REG);
  721. /* Enable block fill mode. */
  722. __raw_writel((is8bpp
  723. ? TGA_MODE_SBM_8BPP | TGA_MODE_BLOCK_FILL
  724. : TGA_MODE_SBM_24BPP | TGA_MODE_BLOCK_FILL),
  725. regs_base + TGA_MODE_REG);
  726. wmb();
  727. /* We can fill 2k pixels per operation. Notice blocks that fit
  728. the width of the screen so that we can take advantage of this
  729. and fill more than one line per write. */
  730. if (width == line_length)
  731. width *= height, height = 1;
  732. /* The write into the frame buffer must be aligned to 4 bytes,
  733. but we are allowed to encode the offset within the word in
  734. the data word written. */
  735. align = (pos & 3) << 16;
  736. pos &= -4;
  737. if (width <= 2048) {
  738. u32 data;
  739. data = (width - 1) | align;
  740. for (i = 0; i < height; ++i) {
  741. __raw_writel(data, fb_base + pos);
  742. pos += line_length;
  743. }
  744. } else {
  745. unsigned long Bpp = (is8bpp ? 1 : 4);
  746. unsigned long nwidth = width & -2048;
  747. u32 fdata, ldata;
  748. fdata = (2048 - 1) | align;
  749. ldata = ((width & 2047) - 1) | align;
  750. for (i = 0; i < height; ++i) {
  751. for (j = 0; j < nwidth; j += 2048)
  752. __raw_writel(fdata, fb_base + pos + j*Bpp);
  753. if (j < width)
  754. __raw_writel(ldata, fb_base + pos + j*Bpp);
  755. pos += line_length;
  756. }
  757. }
  758. wmb();
  759. /* Disable block fill mode. */
  760. __raw_writel((is8bpp
  761. ? TGA_MODE_SBM_8BPP | TGA_MODE_SIMPLE
  762. : TGA_MODE_SBM_24BPP | TGA_MODE_SIMPLE),
  763. regs_base + TGA_MODE_REG);
  764. }
  765. /**
  766. * tgafb_copyarea - REQUIRED function. Can use generic routines if
  767. * non acclerated hardware and packed pixel based.
  768. * Copies on area of the screen to another area.
  769. *
  770. * @info: frame buffer structure that represents a single frame buffer
  771. * @area: structure defining the source and destination.
  772. */
  773. /* Handle the special case of copying entire lines, e.g. during scrolling.
  774. We can avoid a lot of needless computation in this case. In the 8bpp
  775. case we need to use the COPY64 registers instead of mask writes into
  776. the frame buffer to achieve maximum performance. */
  777. static inline void
  778. copyarea_line_8bpp(struct fb_info *info, u32 dy, u32 sy,
  779. u32 height, u32 width)
  780. {
  781. struct tga_par *par = (struct tga_par *) info->par;
  782. void __iomem *tga_regs = par->tga_regs_base;
  783. unsigned long dpos, spos, i, n64;
  784. /* Set up the MODE and PIXELSHIFT registers. */
  785. __raw_writel(TGA_MODE_SBM_8BPP | TGA_MODE_COPY, tga_regs+TGA_MODE_REG);
  786. __raw_writel(0, tga_regs+TGA_PIXELSHIFT_REG);
  787. wmb();
  788. n64 = (height * width) / 64;
  789. if (dy < sy) {
  790. spos = (sy + height) * width;
  791. dpos = (dy + height) * width;
  792. for (i = 0; i < n64; ++i) {
  793. spos -= 64;
  794. dpos -= 64;
  795. __raw_writel(spos, tga_regs+TGA_COPY64_SRC);
  796. wmb();
  797. __raw_writel(dpos, tga_regs+TGA_COPY64_DST);
  798. wmb();
  799. }
  800. } else {
  801. spos = sy * width;
  802. dpos = dy * width;
  803. for (i = 0; i < n64; ++i) {
  804. __raw_writel(spos, tga_regs+TGA_COPY64_SRC);
  805. wmb();
  806. __raw_writel(dpos, tga_regs+TGA_COPY64_DST);
  807. wmb();
  808. spos += 64;
  809. dpos += 64;
  810. }
  811. }
  812. /* Reset the MODE register to normal. */
  813. __raw_writel(TGA_MODE_SBM_8BPP|TGA_MODE_SIMPLE, tga_regs+TGA_MODE_REG);
  814. }
  815. static inline void
  816. copyarea_line_32bpp(struct fb_info *info, u32 dy, u32 sy,
  817. u32 height, u32 width)
  818. {
  819. struct tga_par *par = (struct tga_par *) info->par;
  820. void __iomem *tga_regs = par->tga_regs_base;
  821. void __iomem *tga_fb = par->tga_fb_base;
  822. void __iomem *src;
  823. void __iomem *dst;
  824. unsigned long i, n16;
  825. /* Set up the MODE and PIXELSHIFT registers. */
  826. __raw_writel(TGA_MODE_SBM_24BPP | TGA_MODE_COPY, tga_regs+TGA_MODE_REG);
  827. __raw_writel(0, tga_regs+TGA_PIXELSHIFT_REG);
  828. wmb();
  829. n16 = (height * width) / 16;
  830. if (dy < sy) {
  831. src = tga_fb + (sy + height) * width * 4;
  832. dst = tga_fb + (dy + height) * width * 4;
  833. for (i = 0; i < n16; ++i) {
  834. src -= 64;
  835. dst -= 64;
  836. __raw_writel(0xffff, src);
  837. wmb();
  838. __raw_writel(0xffff, dst);
  839. wmb();
  840. }
  841. } else {
  842. src = tga_fb + sy * width * 4;
  843. dst = tga_fb + dy * width * 4;
  844. for (i = 0; i < n16; ++i) {
  845. __raw_writel(0xffff, src);
  846. wmb();
  847. __raw_writel(0xffff, dst);
  848. wmb();
  849. src += 64;
  850. dst += 64;
  851. }
  852. }
  853. /* Reset the MODE register to normal. */
  854. __raw_writel(TGA_MODE_SBM_24BPP|TGA_MODE_SIMPLE, tga_regs+TGA_MODE_REG);
  855. }
  856. /* The general case of forward copy in 8bpp mode. */
  857. static inline void
  858. copyarea_foreward_8bpp(struct fb_info *info, u32 dx, u32 dy, u32 sx, u32 sy,
  859. u32 height, u32 width, u32 line_length)
  860. {
  861. struct tga_par *par = (struct tga_par *) info->par;
  862. unsigned long i, copied, left;
  863. unsigned long dpos, spos, dalign, salign, yincr;
  864. u32 smask_first, dmask_first, dmask_last;
  865. int pixel_shift, need_prime, need_second;
  866. unsigned long n64, n32, xincr_first;
  867. void __iomem *tga_regs;
  868. void __iomem *tga_fb;
  869. yincr = line_length;
  870. if (dy > sy) {
  871. dy += height - 1;
  872. sy += height - 1;
  873. yincr = -yincr;
  874. }
  875. /* Compute the offsets and alignments in the frame buffer.
  876. More than anything else, these control how we do copies. */
  877. dpos = dy * line_length + dx;
  878. spos = sy * line_length + sx;
  879. dalign = dpos & 7;
  880. salign = spos & 7;
  881. dpos &= -8;
  882. spos &= -8;
  883. /* Compute the value for the PIXELSHIFT register. This controls
  884. both non-co-aligned source and destination and copy direction. */
  885. if (dalign >= salign)
  886. pixel_shift = dalign - salign;
  887. else
  888. pixel_shift = 8 - (salign - dalign);
  889. /* Figure out if we need an additional priming step for the
  890. residue register. */
  891. need_prime = (salign > dalign);
  892. if (need_prime)
  893. dpos -= 8;
  894. /* Begin by copying the leading unaligned destination. Copy enough
  895. to make the next destination address 32-byte aligned. */
  896. copied = 32 - (dalign + (dpos & 31));
  897. if (copied == 32)
  898. copied = 0;
  899. xincr_first = (copied + 7) & -8;
  900. smask_first = dmask_first = (1ul << copied) - 1;
  901. smask_first <<= salign;
  902. dmask_first <<= dalign + need_prime*8;
  903. if (need_prime && copied > 24)
  904. copied -= 8;
  905. left = width - copied;
  906. /* Care for small copies. */
  907. if (copied > width) {
  908. u32 t;
  909. t = (1ul << width) - 1;
  910. t <<= dalign + need_prime*8;
  911. dmask_first &= t;
  912. left = 0;
  913. }
  914. /* Attempt to use 64-byte copies. This is only possible if the
  915. source and destination are co-aligned at 64 bytes. */
  916. n64 = need_second = 0;
  917. if ((dpos & 63) == (spos & 63)
  918. && (height == 1 || line_length % 64 == 0)) {
  919. /* We may need a 32-byte copy to ensure 64 byte alignment. */
  920. need_second = (dpos + xincr_first) & 63;
  921. if ((need_second & 32) != need_second)
  922. printk(KERN_ERR "tgafb: need_second wrong\n");
  923. if (left >= need_second + 64) {
  924. left -= need_second;
  925. n64 = left / 64;
  926. left %= 64;
  927. } else
  928. need_second = 0;
  929. }
  930. /* Copy trailing full 32-byte sections. This will be the main
  931. loop if the 64 byte loop can't be used. */
  932. n32 = left / 32;
  933. left %= 32;
  934. /* Copy the trailing unaligned destination. */
  935. dmask_last = (1ul << left) - 1;
  936. tga_regs = par->tga_regs_base;
  937. tga_fb = par->tga_fb_base;
  938. /* Set up the MODE and PIXELSHIFT registers. */
  939. __raw_writel(TGA_MODE_SBM_8BPP|TGA_MODE_COPY, tga_regs+TGA_MODE_REG);
  940. __raw_writel(pixel_shift, tga_regs+TGA_PIXELSHIFT_REG);
  941. wmb();
  942. for (i = 0; i < height; ++i) {
  943. unsigned long j;
  944. void __iomem *sfb;
  945. void __iomem *dfb;
  946. sfb = tga_fb + spos;
  947. dfb = tga_fb + dpos;
  948. if (dmask_first) {
  949. __raw_writel(smask_first, sfb);
  950. wmb();
  951. __raw_writel(dmask_first, dfb);
  952. wmb();
  953. sfb += xincr_first;
  954. dfb += xincr_first;
  955. }
  956. if (need_second) {
  957. __raw_writel(0xffffffff, sfb);
  958. wmb();
  959. __raw_writel(0xffffffff, dfb);
  960. wmb();
  961. sfb += 32;
  962. dfb += 32;
  963. }
  964. if (n64 && (((unsigned long)sfb | (unsigned long)dfb) & 63))
  965. printk(KERN_ERR
  966. "tgafb: misaligned copy64 (s:%p, d:%p)\n",
  967. sfb, dfb);
  968. for (j = 0; j < n64; ++j) {
  969. __raw_writel(sfb - tga_fb, tga_regs+TGA_COPY64_SRC);
  970. wmb();
  971. __raw_writel(dfb - tga_fb, tga_regs+TGA_COPY64_DST);
  972. wmb();
  973. sfb += 64;
  974. dfb += 64;
  975. }
  976. for (j = 0; j < n32; ++j) {
  977. __raw_writel(0xffffffff, sfb);
  978. wmb();
  979. __raw_writel(0xffffffff, dfb);
  980. wmb();
  981. sfb += 32;
  982. dfb += 32;
  983. }
  984. if (dmask_last) {
  985. __raw_writel(0xffffffff, sfb);
  986. wmb();
  987. __raw_writel(dmask_last, dfb);
  988. wmb();
  989. }
  990. spos += yincr;
  991. dpos += yincr;
  992. }
  993. /* Reset the MODE register to normal. */
  994. __raw_writel(TGA_MODE_SBM_8BPP|TGA_MODE_SIMPLE, tga_regs+TGA_MODE_REG);
  995. }
  996. /* The (almost) general case of backward copy in 8bpp mode. */
  997. static inline void
  998. copyarea_backward_8bpp(struct fb_info *info, u32 dx, u32 dy, u32 sx, u32 sy,
  999. u32 height, u32 width, u32 line_length,
  1000. const struct fb_copyarea *area)
  1001. {
  1002. struct tga_par *par = (struct tga_par *) info->par;
  1003. unsigned long i, left, yincr;
  1004. unsigned long depos, sepos, dealign, sealign;
  1005. u32 mask_first, mask_last;
  1006. unsigned long n32;
  1007. void __iomem *tga_regs;
  1008. void __iomem *tga_fb;
  1009. yincr = line_length;
  1010. if (dy > sy) {
  1011. dy += height - 1;
  1012. sy += height - 1;
  1013. yincr = -yincr;
  1014. }
  1015. /* Compute the offsets and alignments in the frame buffer.
  1016. More than anything else, these control how we do copies. */
  1017. depos = dy * line_length + dx + width;
  1018. sepos = sy * line_length + sx + width;
  1019. dealign = depos & 7;
  1020. sealign = sepos & 7;
  1021. /* ??? The documentation appears to be incorrect (or very
  1022. misleading) wrt how pixel shifting works in backward copy
  1023. mode, i.e. when PIXELSHIFT is negative. I give up for now.
  1024. Do handle the common case of co-aligned backward copies,
  1025. but frob everything else back on generic code. */
  1026. if (dealign != sealign) {
  1027. cfb_copyarea(info, area);
  1028. return;
  1029. }
  1030. /* We begin the copy with the trailing pixels of the
  1031. unaligned destination. */
  1032. mask_first = (1ul << dealign) - 1;
  1033. left = width - dealign;
  1034. /* Care for small copies. */
  1035. if (dealign > width) {
  1036. mask_first ^= (1ul << (dealign - width)) - 1;
  1037. left = 0;
  1038. }
  1039. /* Next copy full words at a time. */
  1040. n32 = left / 32;
  1041. left %= 32;
  1042. /* Finally copy the unaligned head of the span. */
  1043. mask_last = -1 << (32 - left);
  1044. tga_regs = par->tga_regs_base;
  1045. tga_fb = par->tga_fb_base;
  1046. /* Set up the MODE and PIXELSHIFT registers. */
  1047. __raw_writel(TGA_MODE_SBM_8BPP|TGA_MODE_COPY, tga_regs+TGA_MODE_REG);
  1048. __raw_writel(0, tga_regs+TGA_PIXELSHIFT_REG);
  1049. wmb();
  1050. for (i = 0; i < height; ++i) {
  1051. unsigned long j;
  1052. void __iomem *sfb;
  1053. void __iomem *dfb;
  1054. sfb = tga_fb + sepos;
  1055. dfb = tga_fb + depos;
  1056. if (mask_first) {
  1057. __raw_writel(mask_first, sfb);
  1058. wmb();
  1059. __raw_writel(mask_first, dfb);
  1060. wmb();
  1061. }
  1062. for (j = 0; j < n32; ++j) {
  1063. sfb -= 32;
  1064. dfb -= 32;
  1065. __raw_writel(0xffffffff, sfb);
  1066. wmb();
  1067. __raw_writel(0xffffffff, dfb);
  1068. wmb();
  1069. }
  1070. if (mask_last) {
  1071. sfb -= 32;
  1072. dfb -= 32;
  1073. __raw_writel(mask_last, sfb);
  1074. wmb();
  1075. __raw_writel(mask_last, dfb);
  1076. wmb();
  1077. }
  1078. sepos += yincr;
  1079. depos += yincr;
  1080. }
  1081. /* Reset the MODE register to normal. */
  1082. __raw_writel(TGA_MODE_SBM_8BPP|TGA_MODE_SIMPLE, tga_regs+TGA_MODE_REG);
  1083. }
  1084. static void
  1085. tgafb_copyarea(struct fb_info *info, const struct fb_copyarea *area)
  1086. {
  1087. unsigned long dx, dy, width, height, sx, sy, vxres, vyres;
  1088. unsigned long line_length, bpp;
  1089. dx = area->dx;
  1090. dy = area->dy;
  1091. width = area->width;
  1092. height = area->height;
  1093. sx = area->sx;
  1094. sy = area->sy;
  1095. vxres = info->var.xres_virtual;
  1096. vyres = info->var.yres_virtual;
  1097. line_length = info->fix.line_length;
  1098. /* The top left corners must be in the virtual screen. */
  1099. if (dx > vxres || sx > vxres || dy > vyres || sy > vyres)
  1100. return;
  1101. /* Clip the destination. */
  1102. if (dx + width > vxres)
  1103. width = vxres - dx;
  1104. if (dy + height > vyres)
  1105. height = vyres - dy;
  1106. /* The source must be completely inside the virtual screen. */
  1107. if (sx + width > vxres || sy + height > vyres)
  1108. return;
  1109. bpp = info->var.bits_per_pixel;
  1110. /* Detect copies of the entire line. */
  1111. if (width * (bpp >> 3) == line_length) {
  1112. if (bpp == 8)
  1113. copyarea_line_8bpp(info, dy, sy, height, width);
  1114. else
  1115. copyarea_line_32bpp(info, dy, sy, height, width);
  1116. }
  1117. /* ??? The documentation is unclear to me exactly how the pixelshift
  1118. register works in 32bpp mode. Since I don't have hardware to test,
  1119. give up for now and fall back on the generic routines. */
  1120. else if (bpp == 32)
  1121. cfb_copyarea(info, area);
  1122. /* Detect overlapping source and destination that requires
  1123. a backward copy. */
  1124. else if (dy == sy && dx > sx && dx < sx + width)
  1125. copyarea_backward_8bpp(info, dx, dy, sx, sy, height,
  1126. width, line_length, area);
  1127. else
  1128. copyarea_foreward_8bpp(info, dx, dy, sx, sy, height,
  1129. width, line_length);
  1130. }
  1131. /*
  1132. * Initialisation
  1133. */
  1134. static void
  1135. tgafb_init_fix(struct fb_info *info)
  1136. {
  1137. struct tga_par *par = (struct tga_par *)info->par;
  1138. u8 tga_type = par->tga_type;
  1139. const char *tga_type_name;
  1140. switch (tga_type) {
  1141. case TGA_TYPE_8PLANE:
  1142. tga_type_name = "Digital ZLXp-E1";
  1143. break;
  1144. case TGA_TYPE_24PLANE:
  1145. tga_type_name = "Digital ZLXp-E2";
  1146. break;
  1147. case TGA_TYPE_24PLUSZ:
  1148. tga_type_name = "Digital ZLXp-E3";
  1149. break;
  1150. default:
  1151. tga_type_name = "Unknown";
  1152. break;
  1153. }
  1154. strlcpy(info->fix.id, tga_type_name, sizeof(info->fix.id));
  1155. info->fix.type = FB_TYPE_PACKED_PIXELS;
  1156. info->fix.type_aux = 0;
  1157. info->fix.visual = (tga_type == TGA_TYPE_8PLANE
  1158. ? FB_VISUAL_PSEUDOCOLOR
  1159. : FB_VISUAL_TRUECOLOR);
  1160. info->fix.line_length = par->xres * (par->bits_per_pixel >> 3);
  1161. info->fix.smem_start = (size_t) par->tga_fb_base;
  1162. info->fix.smem_len = info->fix.line_length * par->yres;
  1163. info->fix.mmio_start = (size_t) par->tga_regs_base;
  1164. info->fix.mmio_len = 512;
  1165. info->fix.xpanstep = 0;
  1166. info->fix.ypanstep = 0;
  1167. info->fix.ywrapstep = 0;
  1168. info->fix.accel = FB_ACCEL_DEC_TGA;
  1169. }
  1170. static __devinit int
  1171. tgafb_pci_register(struct pci_dev *pdev, const struct pci_device_id *ent)
  1172. {
  1173. static unsigned int const fb_offset_presets[4] = {
  1174. TGA_8PLANE_FB_OFFSET,
  1175. TGA_24PLANE_FB_OFFSET,
  1176. 0xffffffff,
  1177. TGA_24PLUSZ_FB_OFFSET
  1178. };
  1179. struct all_info {
  1180. struct fb_info info;
  1181. struct tga_par par;
  1182. u32 pseudo_palette[16];
  1183. } *all;
  1184. void __iomem *mem_base;
  1185. unsigned long bar0_start, bar0_len;
  1186. u8 tga_type;
  1187. int ret;
  1188. /* Enable device in PCI config. */
  1189. if (pci_enable_device(pdev)) {
  1190. printk(KERN_ERR "tgafb: Cannot enable PCI device\n");
  1191. return -ENODEV;
  1192. }
  1193. /* Allocate the fb and par structures. */
  1194. all = kmalloc(sizeof(*all), GFP_KERNEL);
  1195. if (!all) {
  1196. printk(KERN_ERR "tgafb: Cannot allocate memory\n");
  1197. return -ENOMEM;
  1198. }
  1199. memset(all, 0, sizeof(*all));
  1200. pci_set_drvdata(pdev, all);
  1201. /* Request the mem regions. */
  1202. bar0_start = pci_resource_start(pdev, 0);
  1203. bar0_len = pci_resource_len(pdev, 0);
  1204. ret = -ENODEV;
  1205. if (!request_mem_region (bar0_start, bar0_len, "tgafb")) {
  1206. printk(KERN_ERR "tgafb: cannot reserve FB region\n");
  1207. goto err0;
  1208. }
  1209. /* Map the framebuffer. */
  1210. mem_base = ioremap(bar0_start, bar0_len);
  1211. if (!mem_base) {
  1212. printk(KERN_ERR "tgafb: Cannot map MMIO\n");
  1213. goto err1;
  1214. }
  1215. /* Grab info about the card. */
  1216. tga_type = (readl(mem_base) >> 12) & 0x0f;
  1217. all->par.pdev = pdev;
  1218. all->par.tga_mem_base = mem_base;
  1219. all->par.tga_fb_base = mem_base + fb_offset_presets[tga_type];
  1220. all->par.tga_regs_base = mem_base + TGA_REGS_OFFSET;
  1221. all->par.tga_type = tga_type;
  1222. pci_read_config_byte(pdev, PCI_REVISION_ID, &all->par.tga_chip_rev);
  1223. /* Setup framebuffer. */
  1224. all->info.flags = FBINFO_DEFAULT | FBINFO_HWACCEL_COPYAREA |
  1225. FBINFO_HWACCEL_IMAGEBLIT | FBINFO_HWACCEL_FILLRECT;
  1226. all->info.fbops = &tgafb_ops;
  1227. all->info.screen_base = all->par.tga_fb_base;
  1228. all->info.par = &all->par;
  1229. all->info.pseudo_palette = all->pseudo_palette;
  1230. /* This should give a reasonable default video mode. */
  1231. ret = fb_find_mode(&all->info.var, &all->info, mode_option,
  1232. NULL, 0, NULL,
  1233. tga_type == TGA_TYPE_8PLANE ? 8 : 32);
  1234. if (ret == 0 || ret == 4) {
  1235. printk(KERN_ERR "tgafb: Could not find valid video mode\n");
  1236. ret = -EINVAL;
  1237. goto err1;
  1238. }
  1239. if (fb_alloc_cmap(&all->info.cmap, 256, 0)) {
  1240. printk(KERN_ERR "tgafb: Could not allocate color map\n");
  1241. ret = -ENOMEM;
  1242. goto err1;
  1243. }
  1244. tgafb_set_par(&all->info);
  1245. tgafb_init_fix(&all->info);
  1246. all->info.device = &pdev->dev;
  1247. if (register_framebuffer(&all->info) < 0) {
  1248. printk(KERN_ERR "tgafb: Could not register framebuffer\n");
  1249. ret = -EINVAL;
  1250. goto err1;
  1251. }
  1252. printk(KERN_INFO "tgafb: DC21030 [TGA] detected, rev=0x%02x\n",
  1253. all->par.tga_chip_rev);
  1254. printk(KERN_INFO "tgafb: at PCI bus %d, device %d, function %d\n",
  1255. pdev->bus->number, PCI_SLOT(pdev->devfn),
  1256. PCI_FUNC(pdev->devfn));
  1257. printk(KERN_INFO "fb%d: %s frame buffer device at 0x%lx\n",
  1258. all->info.node, all->info.fix.id, bar0_start);
  1259. return 0;
  1260. err1:
  1261. release_mem_region(bar0_start, bar0_len);
  1262. err0:
  1263. kfree(all);
  1264. return ret;
  1265. }
  1266. static void __exit
  1267. tgafb_pci_unregister(struct pci_dev *pdev)
  1268. {
  1269. struct fb_info *info = pci_get_drvdata(pdev);
  1270. struct tga_par *par = info->par;
  1271. if (!info)
  1272. return;
  1273. unregister_framebuffer(info);
  1274. iounmap(par->tga_mem_base);
  1275. release_mem_region(pci_resource_start(pdev, 0),
  1276. pci_resource_len(pdev, 0));
  1277. kfree(info);
  1278. }
  1279. #ifdef MODULE
  1280. static void __exit
  1281. tgafb_exit(void)
  1282. {
  1283. pci_unregister_driver(&tgafb_driver);
  1284. }
  1285. #endif /* MODULE */
  1286. #ifndef MODULE
  1287. int __init
  1288. tgafb_setup(char *arg)
  1289. {
  1290. char *this_opt;
  1291. if (arg && *arg) {
  1292. while ((this_opt = strsep(&arg, ","))) {
  1293. if (!*this_opt)
  1294. continue;
  1295. if (!strncmp(this_opt, "mode:", 5))
  1296. mode_option = this_opt+5;
  1297. else
  1298. printk(KERN_ERR
  1299. "tgafb: unknown parameter %s\n",
  1300. this_opt);
  1301. }
  1302. }
  1303. return 0;
  1304. }
  1305. #endif /* !MODULE */
  1306. int __init
  1307. tgafb_init(void)
  1308. {
  1309. #ifndef MODULE
  1310. char *option = NULL;
  1311. if (fb_get_options("tgafb", &option))
  1312. return -ENODEV;
  1313. tgafb_setup(option);
  1314. #endif
  1315. return pci_register_driver(&tgafb_driver);
  1316. }
  1317. /*
  1318. * Modularisation
  1319. */
  1320. module_init(tgafb_init);
  1321. #ifdef MODULE
  1322. module_exit(tgafb_exit);
  1323. #endif
  1324. MODULE_DESCRIPTION("framebuffer driver for TGA chipset");
  1325. MODULE_LICENSE("GPL");