tgafb.c 43 KB

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