tgafb.c 42 KB

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