tridentfb.c 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661
  1. /*
  2. * Frame buffer driver for Trident TGUI, Blade and Image series
  3. *
  4. * Copyright 2001, 2002 - Jani Monoses <jani@iv.ro>
  5. * Copyright 2009 Krzysztof Helt <krzysztof.h1@wp.pl>
  6. *
  7. * CREDITS:(in order of appearance)
  8. * skeletonfb.c by Geert Uytterhoeven and other fb code in drivers/video
  9. * Special thanks ;) to Mattia Crivellini <tia@mclink.it>
  10. * much inspired by the XFree86 4.x Trident driver sources
  11. * by Alan Hourihane the FreeVGA project
  12. * Francesco Salvestrini <salvestrini@users.sf.net> XP support,
  13. * code, suggestions
  14. * TODO:
  15. * timing value tweaking so it looks good on every monitor in every mode
  16. */
  17. #include <linux/module.h>
  18. #include <linux/fb.h>
  19. #include <linux/init.h>
  20. #include <linux/pci.h>
  21. #include <linux/slab.h>
  22. #include <linux/delay.h>
  23. #include <video/vga.h>
  24. #include <video/trident.h>
  25. struct tridentfb_par {
  26. void __iomem *io_virt; /* iospace virtual memory address */
  27. u32 pseudo_pal[16];
  28. int chip_id;
  29. int flatpanel;
  30. void (*init_accel) (struct tridentfb_par *, int, int);
  31. void (*wait_engine) (struct tridentfb_par *);
  32. void (*fill_rect)
  33. (struct tridentfb_par *par, u32, u32, u32, u32, u32, u32);
  34. void (*copy_rect)
  35. (struct tridentfb_par *par, u32, u32, u32, u32, u32, u32);
  36. void (*image_blit)
  37. (struct tridentfb_par *par, const char*,
  38. u32, u32, u32, u32, u32, u32);
  39. unsigned char eng_oper; /* engine operation... */
  40. };
  41. static struct fb_fix_screeninfo tridentfb_fix = {
  42. .id = "Trident",
  43. .type = FB_TYPE_PACKED_PIXELS,
  44. .ypanstep = 1,
  45. .visual = FB_VISUAL_PSEUDOCOLOR,
  46. .accel = FB_ACCEL_NONE,
  47. };
  48. /* defaults which are normally overriden by user values */
  49. /* video mode */
  50. static char *mode_option __devinitdata = "640x480-8@60";
  51. static int bpp __devinitdata = 8;
  52. static int noaccel __devinitdata;
  53. static int center;
  54. static int stretch;
  55. static int fp __devinitdata;
  56. static int crt __devinitdata;
  57. static int memsize __devinitdata;
  58. static int memdiff __devinitdata;
  59. static int nativex;
  60. module_param(mode_option, charp, 0);
  61. MODULE_PARM_DESC(mode_option, "Initial video mode e.g. '648x480-8@60'");
  62. module_param_named(mode, mode_option, charp, 0);
  63. MODULE_PARM_DESC(mode, "Initial video mode e.g. '648x480-8@60' (deprecated)");
  64. module_param(bpp, int, 0);
  65. module_param(center, int, 0);
  66. module_param(stretch, int, 0);
  67. module_param(noaccel, int, 0);
  68. module_param(memsize, int, 0);
  69. module_param(memdiff, int, 0);
  70. module_param(nativex, int, 0);
  71. module_param(fp, int, 0);
  72. MODULE_PARM_DESC(fp, "Define if flatpanel is connected");
  73. module_param(crt, int, 0);
  74. MODULE_PARM_DESC(crt, "Define if CRT is connected");
  75. static inline int is_oldclock(int id)
  76. {
  77. return (id == TGUI9440) ||
  78. (id == TGUI9660) ||
  79. (id == CYBER9320);
  80. }
  81. static inline int is_oldprotect(int id)
  82. {
  83. return is_oldclock(id) ||
  84. (id == PROVIDIA9685) ||
  85. (id == CYBER9382) ||
  86. (id == CYBER9385);
  87. }
  88. static inline int is_blade(int id)
  89. {
  90. return (id == BLADE3D) ||
  91. (id == CYBERBLADEE4) ||
  92. (id == CYBERBLADEi7) ||
  93. (id == CYBERBLADEi7D) ||
  94. (id == CYBERBLADEi1) ||
  95. (id == CYBERBLADEi1D) ||
  96. (id == CYBERBLADEAi1) ||
  97. (id == CYBERBLADEAi1D);
  98. }
  99. static inline int is_xp(int id)
  100. {
  101. return (id == CYBERBLADEXPAi1) ||
  102. (id == CYBERBLADEXPm8) ||
  103. (id == CYBERBLADEXPm16);
  104. }
  105. static inline int is3Dchip(int id)
  106. {
  107. return is_blade(id) || is_xp(id) ||
  108. (id == CYBER9397) || (id == CYBER9397DVD) ||
  109. (id == CYBER9520) || (id == CYBER9525DVD) ||
  110. (id == IMAGE975) || (id == IMAGE985);
  111. }
  112. static inline int iscyber(int id)
  113. {
  114. switch (id) {
  115. case CYBER9388:
  116. case CYBER9382:
  117. case CYBER9385:
  118. case CYBER9397:
  119. case CYBER9397DVD:
  120. case CYBER9520:
  121. case CYBER9525DVD:
  122. case CYBERBLADEE4:
  123. case CYBERBLADEi7D:
  124. case CYBERBLADEi1:
  125. case CYBERBLADEi1D:
  126. case CYBERBLADEAi1:
  127. case CYBERBLADEAi1D:
  128. case CYBERBLADEXPAi1:
  129. return 1;
  130. case CYBER9320:
  131. case CYBERBLADEi7: /* VIA MPV4 integrated version */
  132. default:
  133. /* case CYBERBLDAEXPm8: Strange */
  134. /* case CYBERBLDAEXPm16: Strange */
  135. return 0;
  136. }
  137. }
  138. static inline void t_outb(struct tridentfb_par *p, u8 val, u16 reg)
  139. {
  140. fb_writeb(val, p->io_virt + reg);
  141. }
  142. static inline u8 t_inb(struct tridentfb_par *p, u16 reg)
  143. {
  144. return fb_readb(p->io_virt + reg);
  145. }
  146. static inline void writemmr(struct tridentfb_par *par, u16 r, u32 v)
  147. {
  148. fb_writel(v, par->io_virt + r);
  149. }
  150. static inline u32 readmmr(struct tridentfb_par *par, u16 r)
  151. {
  152. return fb_readl(par->io_virt + r);
  153. }
  154. /*
  155. * Blade specific acceleration.
  156. */
  157. #define point(x, y) ((y) << 16 | (x))
  158. static void blade_init_accel(struct tridentfb_par *par, int pitch, int bpp)
  159. {
  160. int v1 = (pitch >> 3) << 20;
  161. int tmp = bpp == 24 ? 2 : (bpp >> 4);
  162. int v2 = v1 | (tmp << 29);
  163. writemmr(par, 0x21C0, v2);
  164. writemmr(par, 0x21C4, v2);
  165. writemmr(par, 0x21B8, v2);
  166. writemmr(par, 0x21BC, v2);
  167. writemmr(par, 0x21D0, v1);
  168. writemmr(par, 0x21D4, v1);
  169. writemmr(par, 0x21C8, v1);
  170. writemmr(par, 0x21CC, v1);
  171. writemmr(par, 0x216C, 0);
  172. }
  173. static void blade_wait_engine(struct tridentfb_par *par)
  174. {
  175. while (readmmr(par, STATUS) & 0xFA800000)
  176. cpu_relax();
  177. }
  178. static void blade_fill_rect(struct tridentfb_par *par,
  179. u32 x, u32 y, u32 w, u32 h, u32 c, u32 rop)
  180. {
  181. writemmr(par, COLOR, c);
  182. writemmr(par, ROP, rop ? ROP_X : ROP_S);
  183. writemmr(par, CMD, 0x20000000 | 1 << 19 | 1 << 4 | 2 << 2);
  184. writemmr(par, DST1, point(x, y));
  185. writemmr(par, DST2, point(x + w - 1, y + h - 1));
  186. }
  187. static void blade_image_blit(struct tridentfb_par *par, const char *data,
  188. u32 x, u32 y, u32 w, u32 h, u32 c, u32 b)
  189. {
  190. unsigned size = ((w + 31) >> 5) * h;
  191. writemmr(par, COLOR, c);
  192. writemmr(par, BGCOLOR, b);
  193. writemmr(par, CMD, 0xa0000000 | 3 << 19);
  194. writemmr(par, DST1, point(x, y));
  195. writemmr(par, DST2, point(x + w - 1, y + h - 1));
  196. memcpy(par->io_virt + 0x10000, data, 4 * size);
  197. }
  198. static void blade_copy_rect(struct tridentfb_par *par,
  199. u32 x1, u32 y1, u32 x2, u32 y2, u32 w, u32 h)
  200. {
  201. int direction = 2;
  202. u32 s1 = point(x1, y1);
  203. u32 s2 = point(x1 + w - 1, y1 + h - 1);
  204. u32 d1 = point(x2, y2);
  205. u32 d2 = point(x2 + w - 1, y2 + h - 1);
  206. if ((y1 > y2) || ((y1 == y2) && (x1 > x2)))
  207. direction = 0;
  208. writemmr(par, ROP, ROP_S);
  209. writemmr(par, CMD, 0xE0000000 | 1 << 19 | 1 << 4 | 1 << 2 | direction);
  210. writemmr(par, SRC1, direction ? s2 : s1);
  211. writemmr(par, SRC2, direction ? s1 : s2);
  212. writemmr(par, DST1, direction ? d2 : d1);
  213. writemmr(par, DST2, direction ? d1 : d2);
  214. }
  215. /*
  216. * BladeXP specific acceleration functions
  217. */
  218. static void xp_init_accel(struct tridentfb_par *par, int pitch, int bpp)
  219. {
  220. unsigned char x = bpp == 24 ? 3 : (bpp >> 4);
  221. int v1 = pitch << (bpp == 24 ? 20 : (18 + x));
  222. switch (pitch << (bpp >> 3)) {
  223. case 8192:
  224. case 512:
  225. x |= 0x00;
  226. break;
  227. case 1024:
  228. x |= 0x04;
  229. break;
  230. case 2048:
  231. x |= 0x08;
  232. break;
  233. case 4096:
  234. x |= 0x0C;
  235. break;
  236. }
  237. t_outb(par, x, 0x2125);
  238. par->eng_oper = x | 0x40;
  239. writemmr(par, 0x2154, v1);
  240. writemmr(par, 0x2150, v1);
  241. t_outb(par, 3, 0x2126);
  242. }
  243. static void xp_wait_engine(struct tridentfb_par *par)
  244. {
  245. int count = 0;
  246. int timeout = 0;
  247. while (t_inb(par, STATUS) & 0x80) {
  248. count++;
  249. if (count == 10000000) {
  250. /* Timeout */
  251. count = 9990000;
  252. timeout++;
  253. if (timeout == 8) {
  254. /* Reset engine */
  255. t_outb(par, 0x00, STATUS);
  256. return;
  257. }
  258. }
  259. cpu_relax();
  260. }
  261. }
  262. static void xp_fill_rect(struct tridentfb_par *par,
  263. u32 x, u32 y, u32 w, u32 h, u32 c, u32 rop)
  264. {
  265. writemmr(par, 0x2127, ROP_P);
  266. writemmr(par, 0x2158, c);
  267. writemmr(par, DRAWFL, 0x4000);
  268. writemmr(par, OLDDIM, point(h, w));
  269. writemmr(par, OLDDST, point(y, x));
  270. t_outb(par, 0x01, OLDCMD);
  271. t_outb(par, par->eng_oper, 0x2125);
  272. }
  273. static void xp_copy_rect(struct tridentfb_par *par,
  274. u32 x1, u32 y1, u32 x2, u32 y2, u32 w, u32 h)
  275. {
  276. u32 x1_tmp, x2_tmp, y1_tmp, y2_tmp;
  277. int direction = 0x0004;
  278. if ((x1 < x2) && (y1 == y2)) {
  279. direction |= 0x0200;
  280. x1_tmp = x1 + w - 1;
  281. x2_tmp = x2 + w - 1;
  282. } else {
  283. x1_tmp = x1;
  284. x2_tmp = x2;
  285. }
  286. if (y1 < y2) {
  287. direction |= 0x0100;
  288. y1_tmp = y1 + h - 1;
  289. y2_tmp = y2 + h - 1;
  290. } else {
  291. y1_tmp = y1;
  292. y2_tmp = y2;
  293. }
  294. writemmr(par, DRAWFL, direction);
  295. t_outb(par, ROP_S, 0x2127);
  296. writemmr(par, OLDSRC, point(y1_tmp, x1_tmp));
  297. writemmr(par, OLDDST, point(y2_tmp, x2_tmp));
  298. writemmr(par, OLDDIM, point(h, w));
  299. t_outb(par, 0x01, OLDCMD);
  300. }
  301. /*
  302. * Image specific acceleration functions
  303. */
  304. static void image_init_accel(struct tridentfb_par *par, int pitch, int bpp)
  305. {
  306. int tmp = bpp == 24 ? 2: (bpp >> 4);
  307. writemmr(par, 0x2120, 0xF0000000);
  308. writemmr(par, 0x2120, 0x40000000 | tmp);
  309. writemmr(par, 0x2120, 0x80000000);
  310. writemmr(par, 0x2144, 0x00000000);
  311. writemmr(par, 0x2148, 0x00000000);
  312. writemmr(par, 0x2150, 0x00000000);
  313. writemmr(par, 0x2154, 0x00000000);
  314. writemmr(par, 0x2120, 0x60000000 | (pitch << 16) | pitch);
  315. writemmr(par, 0x216C, 0x00000000);
  316. writemmr(par, 0x2170, 0x00000000);
  317. writemmr(par, 0x217C, 0x00000000);
  318. writemmr(par, 0x2120, 0x10000000);
  319. writemmr(par, 0x2130, (2047 << 16) | 2047);
  320. }
  321. static void image_wait_engine(struct tridentfb_par *par)
  322. {
  323. while (readmmr(par, 0x2164) & 0xF0000000)
  324. cpu_relax();
  325. }
  326. static void image_fill_rect(struct tridentfb_par *par,
  327. u32 x, u32 y, u32 w, u32 h, u32 c, u32 rop)
  328. {
  329. writemmr(par, 0x2120, 0x80000000);
  330. writemmr(par, 0x2120, 0x90000000 | ROP_S);
  331. writemmr(par, 0x2144, c);
  332. writemmr(par, DST1, point(x, y));
  333. writemmr(par, DST2, point(x + w - 1, y + h - 1));
  334. writemmr(par, 0x2124, 0x80000000 | 3 << 22 | 1 << 10 | 1 << 9);
  335. }
  336. static void image_copy_rect(struct tridentfb_par *par,
  337. u32 x1, u32 y1, u32 x2, u32 y2, u32 w, u32 h)
  338. {
  339. int direction = 0x4;
  340. u32 s1 = point(x1, y1);
  341. u32 s2 = point(x1 + w - 1, y1 + h - 1);
  342. u32 d1 = point(x2, y2);
  343. u32 d2 = point(x2 + w - 1, y2 + h - 1);
  344. if ((y1 > y2) || ((y1 == y2) && (x1 > x2)))
  345. direction = 0;
  346. writemmr(par, 0x2120, 0x80000000);
  347. writemmr(par, 0x2120, 0x90000000 | ROP_S);
  348. writemmr(par, SRC1, direction ? s2 : s1);
  349. writemmr(par, SRC2, direction ? s1 : s2);
  350. writemmr(par, DST1, direction ? d2 : d1);
  351. writemmr(par, DST2, direction ? d1 : d2);
  352. writemmr(par, 0x2124,
  353. 0x80000000 | 1 << 22 | 1 << 10 | 1 << 7 | direction);
  354. }
  355. /*
  356. * TGUI 9440/96XX acceleration
  357. */
  358. static void tgui_init_accel(struct tridentfb_par *par, int pitch, int bpp)
  359. {
  360. unsigned char x = bpp == 24 ? 3 : (bpp >> 4);
  361. /* disable clipping */
  362. writemmr(par, 0x2148, 0);
  363. writemmr(par, 0x214C, point(4095, 2047));
  364. switch ((pitch * bpp) / 8) {
  365. case 8192:
  366. case 512:
  367. x |= 0x00;
  368. break;
  369. case 1024:
  370. x |= 0x04;
  371. break;
  372. case 2048:
  373. x |= 0x08;
  374. break;
  375. case 4096:
  376. x |= 0x0C;
  377. break;
  378. }
  379. fb_writew(x, par->io_virt + 0x2122);
  380. }
  381. static void tgui_fill_rect(struct tridentfb_par *par,
  382. u32 x, u32 y, u32 w, u32 h, u32 c, u32 rop)
  383. {
  384. t_outb(par, ROP_P, 0x2127);
  385. writemmr(par, OLDCLR, c);
  386. writemmr(par, DRAWFL, 0x4020);
  387. writemmr(par, OLDDIM, point(w - 1, h - 1));
  388. writemmr(par, OLDDST, point(x, y));
  389. t_outb(par, 1, OLDCMD);
  390. }
  391. static void tgui_copy_rect(struct tridentfb_par *par,
  392. u32 x1, u32 y1, u32 x2, u32 y2, u32 w, u32 h)
  393. {
  394. int flags = 0;
  395. u16 x1_tmp, x2_tmp, y1_tmp, y2_tmp;
  396. if ((x1 < x2) && (y1 == y2)) {
  397. flags |= 0x0200;
  398. x1_tmp = x1 + w - 1;
  399. x2_tmp = x2 + w - 1;
  400. } else {
  401. x1_tmp = x1;
  402. x2_tmp = x2;
  403. }
  404. if (y1 < y2) {
  405. flags |= 0x0100;
  406. y1_tmp = y1 + h - 1;
  407. y2_tmp = y2 + h - 1;
  408. } else {
  409. y1_tmp = y1;
  410. y2_tmp = y2;
  411. }
  412. writemmr(par, DRAWFL, 0x4 | flags);
  413. t_outb(par, ROP_S, 0x2127);
  414. writemmr(par, OLDSRC, point(x1_tmp, y1_tmp));
  415. writemmr(par, OLDDST, point(x2_tmp, y2_tmp));
  416. writemmr(par, OLDDIM, point(w - 1, h - 1));
  417. t_outb(par, 1, OLDCMD);
  418. }
  419. /*
  420. * Accel functions called by the upper layers
  421. */
  422. static void tridentfb_fillrect(struct fb_info *info,
  423. const struct fb_fillrect *fr)
  424. {
  425. struct tridentfb_par *par = info->par;
  426. int col;
  427. if (info->flags & FBINFO_HWACCEL_DISABLED) {
  428. cfb_fillrect(info, fr);
  429. return;
  430. }
  431. if (info->var.bits_per_pixel == 8) {
  432. col = fr->color;
  433. col |= col << 8;
  434. col |= col << 16;
  435. } else
  436. col = ((u32 *)(info->pseudo_palette))[fr->color];
  437. par->wait_engine(par);
  438. par->fill_rect(par, fr->dx, fr->dy, fr->width,
  439. fr->height, col, fr->rop);
  440. }
  441. static void tridentfb_imageblit(struct fb_info *info,
  442. const struct fb_image *img)
  443. {
  444. struct tridentfb_par *par = info->par;
  445. int col, bgcol;
  446. if ((info->flags & FBINFO_HWACCEL_DISABLED) || img->depth != 1) {
  447. cfb_imageblit(info, img);
  448. return;
  449. }
  450. if (info->var.bits_per_pixel == 8) {
  451. col = img->fg_color;
  452. col |= col << 8;
  453. col |= col << 16;
  454. bgcol = img->bg_color;
  455. bgcol |= bgcol << 8;
  456. bgcol |= bgcol << 16;
  457. } else {
  458. col = ((u32 *)(info->pseudo_palette))[img->fg_color];
  459. bgcol = ((u32 *)(info->pseudo_palette))[img->bg_color];
  460. }
  461. par->wait_engine(par);
  462. if (par->image_blit)
  463. par->image_blit(par, img->data, img->dx, img->dy,
  464. img->width, img->height, col, bgcol);
  465. else
  466. cfb_imageblit(info, img);
  467. }
  468. static void tridentfb_copyarea(struct fb_info *info,
  469. const struct fb_copyarea *ca)
  470. {
  471. struct tridentfb_par *par = info->par;
  472. if (info->flags & FBINFO_HWACCEL_DISABLED) {
  473. cfb_copyarea(info, ca);
  474. return;
  475. }
  476. par->wait_engine(par);
  477. par->copy_rect(par, ca->sx, ca->sy, ca->dx, ca->dy,
  478. ca->width, ca->height);
  479. }
  480. static int tridentfb_sync(struct fb_info *info)
  481. {
  482. struct tridentfb_par *par = info->par;
  483. if (!(info->flags & FBINFO_HWACCEL_DISABLED))
  484. par->wait_engine(par);
  485. return 0;
  486. }
  487. /*
  488. * Hardware access functions
  489. */
  490. static inline unsigned char read3X4(struct tridentfb_par *par, int reg)
  491. {
  492. return vga_mm_rcrt(par->io_virt, reg);
  493. }
  494. static inline void write3X4(struct tridentfb_par *par, int reg,
  495. unsigned char val)
  496. {
  497. vga_mm_wcrt(par->io_virt, reg, val);
  498. }
  499. static inline unsigned char read3CE(struct tridentfb_par *par,
  500. unsigned char reg)
  501. {
  502. return vga_mm_rgfx(par->io_virt, reg);
  503. }
  504. static inline void writeAttr(struct tridentfb_par *par, int reg,
  505. unsigned char val)
  506. {
  507. fb_readb(par->io_virt + VGA_IS1_RC); /* flip-flop to index */
  508. vga_mm_wattr(par->io_virt, reg, val);
  509. }
  510. static inline void write3CE(struct tridentfb_par *par, int reg,
  511. unsigned char val)
  512. {
  513. vga_mm_wgfx(par->io_virt, reg, val);
  514. }
  515. static void enable_mmio(struct tridentfb_par *par)
  516. {
  517. /* Goto New Mode */
  518. vga_io_rseq(0x0B);
  519. /* Unprotect registers */
  520. vga_io_wseq(NewMode1, 0x80);
  521. if (!is_oldprotect(par->chip_id))
  522. vga_io_wseq(Protection, 0x92);
  523. /* Enable MMIO */
  524. outb(PCIReg, 0x3D4);
  525. outb(inb(0x3D5) | 0x01, 0x3D5);
  526. }
  527. static void disable_mmio(struct tridentfb_par *par)
  528. {
  529. /* Goto New Mode */
  530. vga_mm_rseq(par->io_virt, 0x0B);
  531. /* Unprotect registers */
  532. vga_mm_wseq(par->io_virt, NewMode1, 0x80);
  533. if (!is_oldprotect(par->chip_id))
  534. vga_mm_wseq(par->io_virt, Protection, 0x92);
  535. /* Disable MMIO */
  536. t_outb(par, PCIReg, 0x3D4);
  537. t_outb(par, t_inb(par, 0x3D5) & ~0x01, 0x3D5);
  538. }
  539. static inline void crtc_unlock(struct tridentfb_par *par)
  540. {
  541. write3X4(par, VGA_CRTC_V_SYNC_END,
  542. read3X4(par, VGA_CRTC_V_SYNC_END) & 0x7F);
  543. }
  544. /* Return flat panel's maximum x resolution */
  545. static int __devinit get_nativex(struct tridentfb_par *par)
  546. {
  547. int x, y, tmp;
  548. if (nativex)
  549. return nativex;
  550. tmp = (read3CE(par, VertStretch) >> 4) & 3;
  551. switch (tmp) {
  552. case 0:
  553. x = 1280; y = 1024;
  554. break;
  555. case 2:
  556. x = 1024; y = 768;
  557. break;
  558. case 3:
  559. x = 800; y = 600;
  560. break;
  561. case 4:
  562. x = 1400; y = 1050;
  563. break;
  564. case 1:
  565. default:
  566. x = 640; y = 480;
  567. break;
  568. }
  569. output("%dx%d flat panel found\n", x, y);
  570. return x;
  571. }
  572. /* Set pitch */
  573. static inline void set_lwidth(struct tridentfb_par *par, int width)
  574. {
  575. write3X4(par, VGA_CRTC_OFFSET, width & 0xFF);
  576. write3X4(par, AddColReg,
  577. (read3X4(par, AddColReg) & 0xCF) | ((width & 0x300) >> 4));
  578. }
  579. /* For resolutions smaller than FP resolution stretch */
  580. static void screen_stretch(struct tridentfb_par *par)
  581. {
  582. if (par->chip_id != CYBERBLADEXPAi1)
  583. write3CE(par, BiosReg, 0);
  584. else
  585. write3CE(par, BiosReg, 8);
  586. write3CE(par, VertStretch, (read3CE(par, VertStretch) & 0x7C) | 1);
  587. write3CE(par, HorStretch, (read3CE(par, HorStretch) & 0x7C) | 1);
  588. }
  589. /* For resolutions smaller than FP resolution center */
  590. static inline void screen_center(struct tridentfb_par *par)
  591. {
  592. write3CE(par, VertStretch, (read3CE(par, VertStretch) & 0x7C) | 0x80);
  593. write3CE(par, HorStretch, (read3CE(par, HorStretch) & 0x7C) | 0x80);
  594. }
  595. /* Address of first shown pixel in display memory */
  596. static void set_screen_start(struct tridentfb_par *par, int base)
  597. {
  598. u8 tmp;
  599. write3X4(par, VGA_CRTC_START_LO, base & 0xFF);
  600. write3X4(par, VGA_CRTC_START_HI, (base & 0xFF00) >> 8);
  601. tmp = read3X4(par, CRTCModuleTest) & 0xDF;
  602. write3X4(par, CRTCModuleTest, tmp | ((base & 0x10000) >> 11));
  603. tmp = read3X4(par, CRTHiOrd) & 0xF8;
  604. write3X4(par, CRTHiOrd, tmp | ((base & 0xE0000) >> 17));
  605. }
  606. /* Set dotclock frequency */
  607. static void set_vclk(struct tridentfb_par *par, unsigned long freq)
  608. {
  609. int m, n, k;
  610. unsigned long fi, d, di;
  611. unsigned char best_m = 0, best_n = 0, best_k = 0;
  612. unsigned char hi, lo;
  613. unsigned char shift = !is_oldclock(par->chip_id) ? 2 : 1;
  614. d = 20000;
  615. for (k = shift; k >= 0; k--)
  616. for (m = 1; m < 32; m++) {
  617. n = ((m + 2) << shift) - 8;
  618. for (n = (n < 0 ? 0 : n); n < 122; n++) {
  619. fi = ((14318l * (n + 8)) / (m + 2)) >> k;
  620. di = abs(fi - freq);
  621. if (di < d || (di == d && k == best_k)) {
  622. d = di;
  623. best_n = n;
  624. best_m = m;
  625. best_k = k;
  626. }
  627. if (fi > freq)
  628. break;
  629. }
  630. }
  631. if (is_oldclock(par->chip_id)) {
  632. lo = best_n | (best_m << 7);
  633. hi = (best_m >> 1) | (best_k << 4);
  634. } else {
  635. lo = best_n;
  636. hi = best_m | (best_k << 6);
  637. }
  638. if (is3Dchip(par->chip_id)) {
  639. vga_mm_wseq(par->io_virt, ClockHigh, hi);
  640. vga_mm_wseq(par->io_virt, ClockLow, lo);
  641. } else {
  642. t_outb(par, lo, 0x43C8);
  643. t_outb(par, hi, 0x43C9);
  644. }
  645. debug("VCLK = %X %X\n", hi, lo);
  646. }
  647. /* Set number of lines for flat panels*/
  648. static void set_number_of_lines(struct tridentfb_par *par, int lines)
  649. {
  650. int tmp = read3CE(par, CyberEnhance) & 0x8F;
  651. if (lines > 1024)
  652. tmp |= 0x50;
  653. else if (lines > 768)
  654. tmp |= 0x30;
  655. else if (lines > 600)
  656. tmp |= 0x20;
  657. else if (lines > 480)
  658. tmp |= 0x10;
  659. write3CE(par, CyberEnhance, tmp);
  660. }
  661. /*
  662. * If we see that FP is active we assume we have one.
  663. * Otherwise we have a CRT display. User can override.
  664. */
  665. static int __devinit is_flatpanel(struct tridentfb_par *par)
  666. {
  667. if (fp)
  668. return 1;
  669. if (crt || !iscyber(par->chip_id))
  670. return 0;
  671. return (read3CE(par, FPConfig) & 0x10) ? 1 : 0;
  672. }
  673. /* Try detecting the video memory size */
  674. static unsigned int __devinit get_memsize(struct tridentfb_par *par)
  675. {
  676. unsigned char tmp, tmp2;
  677. unsigned int k;
  678. /* If memory size provided by user */
  679. if (memsize)
  680. k = memsize * Kb;
  681. else
  682. switch (par->chip_id) {
  683. case CYBER9525DVD:
  684. k = 2560 * Kb;
  685. break;
  686. default:
  687. tmp = read3X4(par, SPR) & 0x0F;
  688. switch (tmp) {
  689. case 0x01:
  690. k = 512 * Kb;
  691. break;
  692. case 0x02:
  693. k = 6 * Mb; /* XP */
  694. break;
  695. case 0x03:
  696. k = 1 * Mb;
  697. break;
  698. case 0x04:
  699. k = 8 * Mb;
  700. break;
  701. case 0x06:
  702. k = 10 * Mb; /* XP */
  703. break;
  704. case 0x07:
  705. k = 2 * Mb;
  706. break;
  707. case 0x08:
  708. k = 12 * Mb; /* XP */
  709. break;
  710. case 0x0A:
  711. k = 14 * Mb; /* XP */
  712. break;
  713. case 0x0C:
  714. k = 16 * Mb; /* XP */
  715. break;
  716. case 0x0E: /* XP */
  717. tmp2 = vga_mm_rseq(par->io_virt, 0xC1);
  718. switch (tmp2) {
  719. case 0x00:
  720. k = 20 * Mb;
  721. break;
  722. case 0x01:
  723. k = 24 * Mb;
  724. break;
  725. case 0x10:
  726. k = 28 * Mb;
  727. break;
  728. case 0x11:
  729. k = 32 * Mb;
  730. break;
  731. default:
  732. k = 1 * Mb;
  733. break;
  734. }
  735. break;
  736. case 0x0F:
  737. k = 4 * Mb;
  738. break;
  739. default:
  740. k = 1 * Mb;
  741. break;
  742. }
  743. }
  744. k -= memdiff * Kb;
  745. output("framebuffer size = %d Kb\n", k / Kb);
  746. return k;
  747. }
  748. /* See if we can handle the video mode described in var */
  749. static int tridentfb_check_var(struct fb_var_screeninfo *var,
  750. struct fb_info *info)
  751. {
  752. struct tridentfb_par *par = info->par;
  753. int bpp = var->bits_per_pixel;
  754. int line_length;
  755. int ramdac = 230000; /* 230MHz for most 3D chips */
  756. debug("enter\n");
  757. /* check color depth */
  758. if (bpp == 24)
  759. bpp = var->bits_per_pixel = 32;
  760. if (bpp != 8 && bpp != 16 && bpp != 32)
  761. return -EINVAL;
  762. if (par->chip_id == TGUI9440 && bpp == 32)
  763. return -EINVAL;
  764. /* check whether resolution fits on panel and in memory */
  765. if (par->flatpanel && nativex && var->xres > nativex)
  766. return -EINVAL;
  767. /* various resolution checks */
  768. var->xres = (var->xres + 7) & ~0x7;
  769. if (var->xres > var->xres_virtual)
  770. var->xres_virtual = var->xres;
  771. if (var->yres > var->yres_virtual)
  772. var->yres_virtual = var->yres;
  773. if (var->xres_virtual > 4095 || var->yres > 2048)
  774. return -EINVAL;
  775. /* prevent from position overflow for acceleration */
  776. if (var->yres_virtual > 0xffff)
  777. return -EINVAL;
  778. line_length = var->xres_virtual * bpp / 8;
  779. if (!is3Dchip(par->chip_id) &&
  780. !(info->flags & FBINFO_HWACCEL_DISABLED)) {
  781. /* acceleration requires line length to be power of 2 */
  782. if (line_length <= 512)
  783. var->xres_virtual = 512 * 8 / bpp;
  784. else if (line_length <= 1024)
  785. var->xres_virtual = 1024 * 8 / bpp;
  786. else if (line_length <= 2048)
  787. var->xres_virtual = 2048 * 8 / bpp;
  788. else if (line_length <= 4096)
  789. var->xres_virtual = 4096 * 8 / bpp;
  790. else if (line_length <= 8192)
  791. var->xres_virtual = 8192 * 8 / bpp;
  792. else
  793. return -EINVAL;
  794. line_length = var->xres_virtual * bpp / 8;
  795. }
  796. /* datasheet specifies how to set panning only up to 4 MB */
  797. if (line_length * (var->yres_virtual - var->yres) > (4 << 20))
  798. var->yres_virtual = ((4 << 20) / line_length) + var->yres;
  799. if (line_length * var->yres_virtual > info->fix.smem_len)
  800. return -EINVAL;
  801. switch (bpp) {
  802. case 8:
  803. var->red.offset = 0;
  804. var->red.length = 8;
  805. var->green = var->red;
  806. var->blue = var->red;
  807. break;
  808. case 16:
  809. var->red.offset = 11;
  810. var->green.offset = 5;
  811. var->blue.offset = 0;
  812. var->red.length = 5;
  813. var->green.length = 6;
  814. var->blue.length = 5;
  815. break;
  816. case 32:
  817. var->red.offset = 16;
  818. var->green.offset = 8;
  819. var->blue.offset = 0;
  820. var->red.length = 8;
  821. var->green.length = 8;
  822. var->blue.length = 8;
  823. break;
  824. default:
  825. return -EINVAL;
  826. }
  827. if (is_xp(par->chip_id))
  828. ramdac = 350000;
  829. switch (par->chip_id) {
  830. case TGUI9440:
  831. ramdac = (bpp >= 16) ? 45000 : 90000;
  832. break;
  833. case CYBER9320:
  834. case TGUI9660:
  835. ramdac = 135000;
  836. break;
  837. case PROVIDIA9685:
  838. case CYBER9388:
  839. case CYBER9382:
  840. case CYBER9385:
  841. ramdac = 170000;
  842. break;
  843. }
  844. /* The clock is doubled for 32 bpp */
  845. if (bpp == 32)
  846. ramdac /= 2;
  847. if (PICOS2KHZ(var->pixclock) > ramdac)
  848. return -EINVAL;
  849. debug("exit\n");
  850. return 0;
  851. }
  852. /* Pan the display */
  853. static int tridentfb_pan_display(struct fb_var_screeninfo *var,
  854. struct fb_info *info)
  855. {
  856. struct tridentfb_par *par = info->par;
  857. unsigned int offset;
  858. debug("enter\n");
  859. offset = (var->xoffset + (var->yoffset * var->xres_virtual))
  860. * var->bits_per_pixel / 32;
  861. set_screen_start(par, offset);
  862. debug("exit\n");
  863. return 0;
  864. }
  865. static inline void shadowmode_on(struct tridentfb_par *par)
  866. {
  867. write3CE(par, CyberControl, read3CE(par, CyberControl) | 0x81);
  868. }
  869. static inline void shadowmode_off(struct tridentfb_par *par)
  870. {
  871. write3CE(par, CyberControl, read3CE(par, CyberControl) & 0x7E);
  872. }
  873. /* Set the hardware to the requested video mode */
  874. static int tridentfb_set_par(struct fb_info *info)
  875. {
  876. struct tridentfb_par *par = info->par;
  877. u32 htotal, hdispend, hsyncstart, hsyncend, hblankstart, hblankend;
  878. u32 vtotal, vdispend, vsyncstart, vsyncend, vblankstart, vblankend;
  879. struct fb_var_screeninfo *var = &info->var;
  880. int bpp = var->bits_per_pixel;
  881. unsigned char tmp;
  882. unsigned long vclk;
  883. debug("enter\n");
  884. hdispend = var->xres / 8 - 1;
  885. hsyncstart = (var->xres + var->right_margin) / 8;
  886. hsyncend = (var->xres + var->right_margin + var->hsync_len) / 8;
  887. htotal = (var->xres + var->left_margin + var->right_margin +
  888. var->hsync_len) / 8 - 5;
  889. hblankstart = hdispend + 1;
  890. hblankend = htotal + 3;
  891. vdispend = var->yres - 1;
  892. vsyncstart = var->yres + var->lower_margin;
  893. vsyncend = vsyncstart + var->vsync_len;
  894. vtotal = var->upper_margin + vsyncend - 2;
  895. vblankstart = vdispend + 1;
  896. vblankend = vtotal;
  897. if (info->var.vmode & FB_VMODE_INTERLACED) {
  898. vtotal /= 2;
  899. vdispend /= 2;
  900. vsyncstart /= 2;
  901. vsyncend /= 2;
  902. vblankstart /= 2;
  903. vblankend /= 2;
  904. }
  905. enable_mmio(par);
  906. crtc_unlock(par);
  907. write3CE(par, CyberControl, 8);
  908. tmp = 0xEB;
  909. if (var->sync & FB_SYNC_HOR_HIGH_ACT)
  910. tmp &= ~0x40;
  911. if (var->sync & FB_SYNC_VERT_HIGH_ACT)
  912. tmp &= ~0x80;
  913. if (par->flatpanel && var->xres < nativex) {
  914. /*
  915. * on flat panels with native size larger
  916. * than requested resolution decide whether
  917. * we stretch or center
  918. */
  919. t_outb(par, tmp | 0xC0, VGA_MIS_W);
  920. shadowmode_on(par);
  921. if (center)
  922. screen_center(par);
  923. else if (stretch)
  924. screen_stretch(par);
  925. } else {
  926. t_outb(par, tmp, VGA_MIS_W);
  927. write3CE(par, CyberControl, 8);
  928. }
  929. /* vertical timing values */
  930. write3X4(par, VGA_CRTC_V_TOTAL, vtotal & 0xFF);
  931. write3X4(par, VGA_CRTC_V_DISP_END, vdispend & 0xFF);
  932. write3X4(par, VGA_CRTC_V_SYNC_START, vsyncstart & 0xFF);
  933. write3X4(par, VGA_CRTC_V_SYNC_END, (vsyncend & 0x0F));
  934. write3X4(par, VGA_CRTC_V_BLANK_START, vblankstart & 0xFF);
  935. write3X4(par, VGA_CRTC_V_BLANK_END, vblankend & 0xFF);
  936. /* horizontal timing values */
  937. write3X4(par, VGA_CRTC_H_TOTAL, htotal & 0xFF);
  938. write3X4(par, VGA_CRTC_H_DISP, hdispend & 0xFF);
  939. write3X4(par, VGA_CRTC_H_SYNC_START, hsyncstart & 0xFF);
  940. write3X4(par, VGA_CRTC_H_SYNC_END,
  941. (hsyncend & 0x1F) | ((hblankend & 0x20) << 2));
  942. write3X4(par, VGA_CRTC_H_BLANK_START, hblankstart & 0xFF);
  943. write3X4(par, VGA_CRTC_H_BLANK_END, hblankend & 0x1F);
  944. /* higher bits of vertical timing values */
  945. tmp = 0x10;
  946. if (vtotal & 0x100) tmp |= 0x01;
  947. if (vdispend & 0x100) tmp |= 0x02;
  948. if (vsyncstart & 0x100) tmp |= 0x04;
  949. if (vblankstart & 0x100) tmp |= 0x08;
  950. if (vtotal & 0x200) tmp |= 0x20;
  951. if (vdispend & 0x200) tmp |= 0x40;
  952. if (vsyncstart & 0x200) tmp |= 0x80;
  953. write3X4(par, VGA_CRTC_OVERFLOW, tmp);
  954. tmp = read3X4(par, CRTHiOrd) & 0x07;
  955. tmp |= 0x08; /* line compare bit 10 */
  956. if (vtotal & 0x400) tmp |= 0x80;
  957. if (vblankstart & 0x400) tmp |= 0x40;
  958. if (vsyncstart & 0x400) tmp |= 0x20;
  959. if (vdispend & 0x400) tmp |= 0x10;
  960. write3X4(par, CRTHiOrd, tmp);
  961. tmp = (htotal >> 8) & 0x01;
  962. tmp |= (hdispend >> 7) & 0x02;
  963. tmp |= (hsyncstart >> 5) & 0x08;
  964. tmp |= (hblankstart >> 4) & 0x10;
  965. write3X4(par, HorizOverflow, tmp);
  966. tmp = 0x40;
  967. if (vblankstart & 0x200) tmp |= 0x20;
  968. //FIXME if (info->var.vmode & FB_VMODE_DOUBLE) tmp |= 0x80; /* double scan for 200 line modes */
  969. write3X4(par, VGA_CRTC_MAX_SCAN, tmp);
  970. write3X4(par, VGA_CRTC_LINE_COMPARE, 0xFF);
  971. write3X4(par, VGA_CRTC_PRESET_ROW, 0);
  972. write3X4(par, VGA_CRTC_MODE, 0xC3);
  973. write3X4(par, LinearAddReg, 0x20); /* enable linear addressing */
  974. tmp = (info->var.vmode & FB_VMODE_INTERLACED) ? 0x84 : 0x80;
  975. /* enable access extended memory */
  976. write3X4(par, CRTCModuleTest, tmp);
  977. tmp = read3CE(par, MiscIntContReg) & ~0x4;
  978. if (info->var.vmode & FB_VMODE_INTERLACED)
  979. tmp |= 0x4;
  980. write3CE(par, MiscIntContReg, tmp);
  981. /* enable GE for text acceleration */
  982. write3X4(par, GraphEngReg, 0x80);
  983. switch (bpp) {
  984. case 8:
  985. tmp = 0x00;
  986. break;
  987. case 16:
  988. tmp = 0x05;
  989. break;
  990. case 24:
  991. tmp = 0x29;
  992. break;
  993. case 32:
  994. tmp = 0x09;
  995. break;
  996. }
  997. write3X4(par, PixelBusReg, tmp);
  998. tmp = read3X4(par, DRAMControl);
  999. if (!is_oldprotect(par->chip_id))
  1000. tmp |= 0x10;
  1001. if (iscyber(par->chip_id))
  1002. tmp |= 0x20;
  1003. write3X4(par, DRAMControl, tmp); /* both IO, linear enable */
  1004. write3X4(par, InterfaceSel, read3X4(par, InterfaceSel) | 0x40);
  1005. if (!is_xp(par->chip_id))
  1006. write3X4(par, Performance, read3X4(par, Performance) | 0x10);
  1007. /* MMIO & PCI read and write burst enable */
  1008. if (par->chip_id != TGUI9440 && par->chip_id != IMAGE975)
  1009. write3X4(par, PCIReg, read3X4(par, PCIReg) | 0x06);
  1010. vga_mm_wseq(par->io_virt, 0, 3);
  1011. vga_mm_wseq(par->io_virt, 1, 1); /* set char clock 8 dots wide */
  1012. /* enable 4 maps because needed in chain4 mode */
  1013. vga_mm_wseq(par->io_virt, 2, 0x0F);
  1014. vga_mm_wseq(par->io_virt, 3, 0);
  1015. vga_mm_wseq(par->io_virt, 4, 0x0E); /* memory mode enable bitmaps ?? */
  1016. /* convert from picoseconds to kHz */
  1017. vclk = PICOS2KHZ(info->var.pixclock);
  1018. /* divide clock by 2 if 32bpp chain4 mode display and CPU path */
  1019. tmp = read3CE(par, MiscExtFunc) & 0xF0;
  1020. if (bpp == 32 || (par->chip_id == TGUI9440 && bpp == 16)) {
  1021. tmp |= 8;
  1022. vclk *= 2;
  1023. }
  1024. set_vclk(par, vclk);
  1025. write3CE(par, MiscExtFunc, tmp | 0x12);
  1026. write3CE(par, 0x5, 0x40); /* no CGA compat, allow 256 col */
  1027. write3CE(par, 0x6, 0x05); /* graphics mode */
  1028. write3CE(par, 0x7, 0x0F); /* planes? */
  1029. /* graphics mode and support 256 color modes */
  1030. writeAttr(par, 0x10, 0x41);
  1031. writeAttr(par, 0x12, 0x0F); /* planes */
  1032. writeAttr(par, 0x13, 0); /* horizontal pel panning */
  1033. /* colors */
  1034. for (tmp = 0; tmp < 0x10; tmp++)
  1035. writeAttr(par, tmp, tmp);
  1036. fb_readb(par->io_virt + VGA_IS1_RC); /* flip-flop to index */
  1037. t_outb(par, 0x20, VGA_ATT_W); /* enable attr */
  1038. switch (bpp) {
  1039. case 8:
  1040. tmp = 0;
  1041. break;
  1042. case 16:
  1043. tmp = 0x30;
  1044. break;
  1045. case 24:
  1046. case 32:
  1047. tmp = 0xD0;
  1048. break;
  1049. }
  1050. t_inb(par, VGA_PEL_IW);
  1051. t_inb(par, VGA_PEL_MSK);
  1052. t_inb(par, VGA_PEL_MSK);
  1053. t_inb(par, VGA_PEL_MSK);
  1054. t_inb(par, VGA_PEL_MSK);
  1055. t_outb(par, tmp, VGA_PEL_MSK);
  1056. t_inb(par, VGA_PEL_IW);
  1057. if (par->flatpanel)
  1058. set_number_of_lines(par, info->var.yres);
  1059. info->fix.line_length = info->var.xres_virtual * bpp / 8;
  1060. set_lwidth(par, info->fix.line_length / 8);
  1061. if (!(info->flags & FBINFO_HWACCEL_DISABLED))
  1062. par->init_accel(par, info->var.xres_virtual, bpp);
  1063. info->fix.visual = (bpp == 8) ? FB_VISUAL_PSEUDOCOLOR : FB_VISUAL_TRUECOLOR;
  1064. info->cmap.len = (bpp == 8) ? 256 : 16;
  1065. debug("exit\n");
  1066. return 0;
  1067. }
  1068. /* Set one color register */
  1069. static int tridentfb_setcolreg(unsigned regno, unsigned red, unsigned green,
  1070. unsigned blue, unsigned transp,
  1071. struct fb_info *info)
  1072. {
  1073. int bpp = info->var.bits_per_pixel;
  1074. struct tridentfb_par *par = info->par;
  1075. if (regno >= info->cmap.len)
  1076. return 1;
  1077. if (bpp == 8) {
  1078. t_outb(par, 0xFF, VGA_PEL_MSK);
  1079. t_outb(par, regno, VGA_PEL_IW);
  1080. t_outb(par, red >> 10, VGA_PEL_D);
  1081. t_outb(par, green >> 10, VGA_PEL_D);
  1082. t_outb(par, blue >> 10, VGA_PEL_D);
  1083. } else if (regno < 16) {
  1084. if (bpp == 16) { /* RGB 565 */
  1085. u32 col;
  1086. col = (red & 0xF800) | ((green & 0xFC00) >> 5) |
  1087. ((blue & 0xF800) >> 11);
  1088. col |= col << 16;
  1089. ((u32 *)(info->pseudo_palette))[regno] = col;
  1090. } else if (bpp == 32) /* ARGB 8888 */
  1091. ((u32 *)info->pseudo_palette)[regno] =
  1092. ((transp & 0xFF00) << 16) |
  1093. ((red & 0xFF00) << 8) |
  1094. ((green & 0xFF00)) |
  1095. ((blue & 0xFF00) >> 8);
  1096. }
  1097. return 0;
  1098. }
  1099. /* Try blanking the screen. For flat panels it does nothing */
  1100. static int tridentfb_blank(int blank_mode, struct fb_info *info)
  1101. {
  1102. unsigned char PMCont, DPMSCont;
  1103. struct tridentfb_par *par = info->par;
  1104. debug("enter\n");
  1105. if (par->flatpanel)
  1106. return 0;
  1107. t_outb(par, 0x04, 0x83C8); /* Read DPMS Control */
  1108. PMCont = t_inb(par, 0x83C6) & 0xFC;
  1109. DPMSCont = read3CE(par, PowerStatus) & 0xFC;
  1110. switch (blank_mode) {
  1111. case FB_BLANK_UNBLANK:
  1112. /* Screen: On, HSync: On, VSync: On */
  1113. case FB_BLANK_NORMAL:
  1114. /* Screen: Off, HSync: On, VSync: On */
  1115. PMCont |= 0x03;
  1116. DPMSCont |= 0x00;
  1117. break;
  1118. case FB_BLANK_HSYNC_SUSPEND:
  1119. /* Screen: Off, HSync: Off, VSync: On */
  1120. PMCont |= 0x02;
  1121. DPMSCont |= 0x01;
  1122. break;
  1123. case FB_BLANK_VSYNC_SUSPEND:
  1124. /* Screen: Off, HSync: On, VSync: Off */
  1125. PMCont |= 0x02;
  1126. DPMSCont |= 0x02;
  1127. break;
  1128. case FB_BLANK_POWERDOWN:
  1129. /* Screen: Off, HSync: Off, VSync: Off */
  1130. PMCont |= 0x00;
  1131. DPMSCont |= 0x03;
  1132. break;
  1133. }
  1134. write3CE(par, PowerStatus, DPMSCont);
  1135. t_outb(par, 4, 0x83C8);
  1136. t_outb(par, PMCont, 0x83C6);
  1137. debug("exit\n");
  1138. /* let fbcon do a softblank for us */
  1139. return (blank_mode == FB_BLANK_NORMAL) ? 1 : 0;
  1140. }
  1141. static struct fb_ops tridentfb_ops = {
  1142. .owner = THIS_MODULE,
  1143. .fb_setcolreg = tridentfb_setcolreg,
  1144. .fb_pan_display = tridentfb_pan_display,
  1145. .fb_blank = tridentfb_blank,
  1146. .fb_check_var = tridentfb_check_var,
  1147. .fb_set_par = tridentfb_set_par,
  1148. .fb_fillrect = tridentfb_fillrect,
  1149. .fb_copyarea = tridentfb_copyarea,
  1150. .fb_imageblit = tridentfb_imageblit,
  1151. .fb_sync = tridentfb_sync,
  1152. };
  1153. static int __devinit trident_pci_probe(struct pci_dev *dev,
  1154. const struct pci_device_id *id)
  1155. {
  1156. int err;
  1157. unsigned char revision;
  1158. struct fb_info *info;
  1159. struct tridentfb_par *default_par;
  1160. int chip3D;
  1161. int chip_id;
  1162. err = pci_enable_device(dev);
  1163. if (err)
  1164. return err;
  1165. info = framebuffer_alloc(sizeof(struct tridentfb_par), &dev->dev);
  1166. if (!info)
  1167. return -ENOMEM;
  1168. default_par = info->par;
  1169. chip_id = id->device;
  1170. /* If PCI id is 0x9660 then further detect chip type */
  1171. if (chip_id == TGUI9660) {
  1172. revision = vga_io_rseq(RevisionID);
  1173. switch (revision) {
  1174. case 0x21:
  1175. chip_id = PROVIDIA9685;
  1176. break;
  1177. case 0x22:
  1178. case 0x23:
  1179. chip_id = CYBER9397;
  1180. break;
  1181. case 0x2A:
  1182. chip_id = CYBER9397DVD;
  1183. break;
  1184. case 0x30:
  1185. case 0x33:
  1186. case 0x34:
  1187. case 0x35:
  1188. case 0x38:
  1189. case 0x3A:
  1190. case 0xB3:
  1191. chip_id = CYBER9385;
  1192. break;
  1193. case 0x40 ... 0x43:
  1194. chip_id = CYBER9382;
  1195. break;
  1196. case 0x4A:
  1197. chip_id = CYBER9388;
  1198. break;
  1199. default:
  1200. break;
  1201. }
  1202. }
  1203. chip3D = is3Dchip(chip_id);
  1204. if (is_xp(chip_id)) {
  1205. default_par->init_accel = xp_init_accel;
  1206. default_par->wait_engine = xp_wait_engine;
  1207. default_par->fill_rect = xp_fill_rect;
  1208. default_par->copy_rect = xp_copy_rect;
  1209. tridentfb_fix.accel = FB_ACCEL_TRIDENT_BLADEXP;
  1210. } else if (is_blade(chip_id)) {
  1211. default_par->init_accel = blade_init_accel;
  1212. default_par->wait_engine = blade_wait_engine;
  1213. default_par->fill_rect = blade_fill_rect;
  1214. default_par->copy_rect = blade_copy_rect;
  1215. default_par->image_blit = blade_image_blit;
  1216. tridentfb_fix.accel = FB_ACCEL_TRIDENT_BLADE3D;
  1217. } else if (chip3D) { /* 3DImage family left */
  1218. default_par->init_accel = image_init_accel;
  1219. default_par->wait_engine = image_wait_engine;
  1220. default_par->fill_rect = image_fill_rect;
  1221. default_par->copy_rect = image_copy_rect;
  1222. tridentfb_fix.accel = FB_ACCEL_TRIDENT_3DIMAGE;
  1223. } else { /* TGUI 9440/96XX family */
  1224. default_par->init_accel = tgui_init_accel;
  1225. default_par->wait_engine = xp_wait_engine;
  1226. default_par->fill_rect = tgui_fill_rect;
  1227. default_par->copy_rect = tgui_copy_rect;
  1228. tridentfb_fix.accel = FB_ACCEL_TRIDENT_TGUI;
  1229. }
  1230. default_par->chip_id = chip_id;
  1231. /* setup MMIO region */
  1232. tridentfb_fix.mmio_start = pci_resource_start(dev, 1);
  1233. tridentfb_fix.mmio_len = pci_resource_len(dev, 1);
  1234. if (!request_mem_region(tridentfb_fix.mmio_start,
  1235. tridentfb_fix.mmio_len, "tridentfb")) {
  1236. debug("request_region failed!\n");
  1237. framebuffer_release(info);
  1238. return -1;
  1239. }
  1240. default_par->io_virt = ioremap_nocache(tridentfb_fix.mmio_start,
  1241. tridentfb_fix.mmio_len);
  1242. if (!default_par->io_virt) {
  1243. debug("ioremap failed\n");
  1244. err = -1;
  1245. goto out_unmap1;
  1246. }
  1247. enable_mmio(default_par);
  1248. /* setup framebuffer memory */
  1249. tridentfb_fix.smem_start = pci_resource_start(dev, 0);
  1250. tridentfb_fix.smem_len = get_memsize(default_par);
  1251. if (!request_mem_region(tridentfb_fix.smem_start,
  1252. tridentfb_fix.smem_len, "tridentfb")) {
  1253. debug("request_mem_region failed!\n");
  1254. disable_mmio(info->par);
  1255. err = -1;
  1256. goto out_unmap1;
  1257. }
  1258. info->screen_base = ioremap_nocache(tridentfb_fix.smem_start,
  1259. tridentfb_fix.smem_len);
  1260. if (!info->screen_base) {
  1261. debug("ioremap failed\n");
  1262. err = -1;
  1263. goto out_unmap2;
  1264. }
  1265. default_par->flatpanel = is_flatpanel(default_par);
  1266. if (default_par->flatpanel)
  1267. nativex = get_nativex(default_par);
  1268. info->fix = tridentfb_fix;
  1269. info->fbops = &tridentfb_ops;
  1270. info->pseudo_palette = default_par->pseudo_pal;
  1271. info->flags = FBINFO_DEFAULT | FBINFO_HWACCEL_YPAN;
  1272. if (!noaccel && default_par->init_accel) {
  1273. info->flags &= ~FBINFO_HWACCEL_DISABLED;
  1274. info->flags |= FBINFO_HWACCEL_COPYAREA;
  1275. info->flags |= FBINFO_HWACCEL_FILLRECT;
  1276. } else
  1277. info->flags |= FBINFO_HWACCEL_DISABLED;
  1278. if (is_blade(chip_id) && chip_id != BLADE3D)
  1279. info->flags |= FBINFO_READS_FAST;
  1280. info->pixmap.addr = kmalloc(4096, GFP_KERNEL);
  1281. if (!info->pixmap.addr) {
  1282. err = -ENOMEM;
  1283. goto out_unmap2;
  1284. }
  1285. info->pixmap.size = 4096;
  1286. info->pixmap.buf_align = 4;
  1287. info->pixmap.scan_align = 1;
  1288. info->pixmap.access_align = 32;
  1289. info->pixmap.flags = FB_PIXMAP_SYSTEM;
  1290. if (default_par->image_blit) {
  1291. info->flags |= FBINFO_HWACCEL_IMAGEBLIT;
  1292. info->pixmap.scan_align = 4;
  1293. }
  1294. if (noaccel) {
  1295. printk(KERN_DEBUG "disabling acceleration\n");
  1296. info->flags |= FBINFO_HWACCEL_DISABLED;
  1297. info->pixmap.scan_align = 1;
  1298. }
  1299. if (!fb_find_mode(&info->var, info,
  1300. mode_option, NULL, 0, NULL, bpp)) {
  1301. err = -EINVAL;
  1302. goto out_unmap2;
  1303. }
  1304. err = fb_alloc_cmap(&info->cmap, 256, 0);
  1305. if (err < 0)
  1306. goto out_unmap2;
  1307. info->var.activate |= FB_ACTIVATE_NOW;
  1308. info->device = &dev->dev;
  1309. if (register_framebuffer(info) < 0) {
  1310. printk(KERN_ERR "tridentfb: could not register framebuffer\n");
  1311. fb_dealloc_cmap(&info->cmap);
  1312. err = -EINVAL;
  1313. goto out_unmap2;
  1314. }
  1315. output("fb%d: %s frame buffer device %dx%d-%dbpp\n",
  1316. info->node, info->fix.id, info->var.xres,
  1317. info->var.yres, info->var.bits_per_pixel);
  1318. pci_set_drvdata(dev, info);
  1319. return 0;
  1320. out_unmap2:
  1321. kfree(info->pixmap.addr);
  1322. if (info->screen_base)
  1323. iounmap(info->screen_base);
  1324. release_mem_region(tridentfb_fix.smem_start, tridentfb_fix.smem_len);
  1325. disable_mmio(info->par);
  1326. out_unmap1:
  1327. if (default_par->io_virt)
  1328. iounmap(default_par->io_virt);
  1329. release_mem_region(tridentfb_fix.mmio_start, tridentfb_fix.mmio_len);
  1330. framebuffer_release(info);
  1331. return err;
  1332. }
  1333. static void __devexit trident_pci_remove(struct pci_dev *dev)
  1334. {
  1335. struct fb_info *info = pci_get_drvdata(dev);
  1336. struct tridentfb_par *par = info->par;
  1337. unregister_framebuffer(info);
  1338. iounmap(par->io_virt);
  1339. iounmap(info->screen_base);
  1340. release_mem_region(tridentfb_fix.smem_start, tridentfb_fix.smem_len);
  1341. release_mem_region(tridentfb_fix.mmio_start, tridentfb_fix.mmio_len);
  1342. pci_set_drvdata(dev, NULL);
  1343. kfree(info->pixmap.addr);
  1344. fb_dealloc_cmap(&info->cmap);
  1345. framebuffer_release(info);
  1346. }
  1347. /* List of boards that we are trying to support */
  1348. static struct pci_device_id trident_devices[] = {
  1349. {PCI_VENDOR_ID_TRIDENT, BLADE3D, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
  1350. {PCI_VENDOR_ID_TRIDENT, CYBERBLADEi7, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
  1351. {PCI_VENDOR_ID_TRIDENT, CYBERBLADEi7D, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
  1352. {PCI_VENDOR_ID_TRIDENT, CYBERBLADEi1, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
  1353. {PCI_VENDOR_ID_TRIDENT, CYBERBLADEi1D, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
  1354. {PCI_VENDOR_ID_TRIDENT, CYBERBLADEAi1, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
  1355. {PCI_VENDOR_ID_TRIDENT, CYBERBLADEAi1D, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
  1356. {PCI_VENDOR_ID_TRIDENT, CYBERBLADEE4, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
  1357. {PCI_VENDOR_ID_TRIDENT, TGUI9440, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
  1358. {PCI_VENDOR_ID_TRIDENT, TGUI9660, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
  1359. {PCI_VENDOR_ID_TRIDENT, IMAGE975, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
  1360. {PCI_VENDOR_ID_TRIDENT, IMAGE985, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
  1361. {PCI_VENDOR_ID_TRIDENT, CYBER9320, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
  1362. {PCI_VENDOR_ID_TRIDENT, CYBER9388, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
  1363. {PCI_VENDOR_ID_TRIDENT, CYBER9520, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
  1364. {PCI_VENDOR_ID_TRIDENT, CYBER9525DVD, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
  1365. {PCI_VENDOR_ID_TRIDENT, CYBER9397, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
  1366. {PCI_VENDOR_ID_TRIDENT, CYBER9397DVD, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
  1367. {PCI_VENDOR_ID_TRIDENT, CYBERBLADEXPAi1, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
  1368. {PCI_VENDOR_ID_TRIDENT, CYBERBLADEXPm8, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
  1369. {PCI_VENDOR_ID_TRIDENT, CYBERBLADEXPm16, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
  1370. {0,}
  1371. };
  1372. MODULE_DEVICE_TABLE(pci, trident_devices);
  1373. static struct pci_driver tridentfb_pci_driver = {
  1374. .name = "tridentfb",
  1375. .id_table = trident_devices,
  1376. .probe = trident_pci_probe,
  1377. .remove = __devexit_p(trident_pci_remove)
  1378. };
  1379. /*
  1380. * Parse user specified options (`video=trident:')
  1381. * example:
  1382. * video=trident:800x600,bpp=16,noaccel
  1383. */
  1384. #ifndef MODULE
  1385. static int __init tridentfb_setup(char *options)
  1386. {
  1387. char *opt;
  1388. if (!options || !*options)
  1389. return 0;
  1390. while ((opt = strsep(&options, ",")) != NULL) {
  1391. if (!*opt)
  1392. continue;
  1393. if (!strncmp(opt, "noaccel", 7))
  1394. noaccel = 1;
  1395. else if (!strncmp(opt, "fp", 2))
  1396. fp = 1;
  1397. else if (!strncmp(opt, "crt", 3))
  1398. fp = 0;
  1399. else if (!strncmp(opt, "bpp=", 4))
  1400. bpp = simple_strtoul(opt + 4, NULL, 0);
  1401. else if (!strncmp(opt, "center", 6))
  1402. center = 1;
  1403. else if (!strncmp(opt, "stretch", 7))
  1404. stretch = 1;
  1405. else if (!strncmp(opt, "memsize=", 8))
  1406. memsize = simple_strtoul(opt + 8, NULL, 0);
  1407. else if (!strncmp(opt, "memdiff=", 8))
  1408. memdiff = simple_strtoul(opt + 8, NULL, 0);
  1409. else if (!strncmp(opt, "nativex=", 8))
  1410. nativex = simple_strtoul(opt + 8, NULL, 0);
  1411. else
  1412. mode_option = opt;
  1413. }
  1414. return 0;
  1415. }
  1416. #endif
  1417. static int __init tridentfb_init(void)
  1418. {
  1419. #ifndef MODULE
  1420. char *option = NULL;
  1421. if (fb_get_options("tridentfb", &option))
  1422. return -ENODEV;
  1423. tridentfb_setup(option);
  1424. #endif
  1425. return pci_register_driver(&tridentfb_pci_driver);
  1426. }
  1427. static void __exit tridentfb_exit(void)
  1428. {
  1429. pci_unregister_driver(&tridentfb_pci_driver);
  1430. }
  1431. module_init(tridentfb_init);
  1432. module_exit(tridentfb_exit);
  1433. MODULE_AUTHOR("Jani Monoses <jani@iv.ro>");
  1434. MODULE_DESCRIPTION("Framebuffer driver for Trident cards");
  1435. MODULE_LICENSE("GPL");
  1436. MODULE_ALIAS("cyblafb");