tgafb.c 40 KB

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