tdfxfb.c 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631
  1. /*
  2. *
  3. * tdfxfb.c
  4. *
  5. * Author: Hannu Mallat <hmallat@cc.hut.fi>
  6. *
  7. * Copyright © 1999 Hannu Mallat
  8. * All rights reserved
  9. *
  10. * Created : Thu Sep 23 18:17:43 1999, hmallat
  11. * Last modified: Tue Nov 2 21:19:47 1999, hmallat
  12. *
  13. * I2C part copied from the i2c-voodoo3.c driver by:
  14. * Frodo Looijaard <frodol@dds.nl>,
  15. * Philip Edelbrock <phil@netroedge.com>,
  16. * Ralph Metzler <rjkm@thp.uni-koeln.de>, and
  17. * Mark D. Studebaker <mdsxyz123@yahoo.com>
  18. *
  19. * Lots of the information here comes from the Daryll Strauss' Banshee
  20. * patches to the XF86 server, and the rest comes from the 3dfx
  21. * Banshee specification. I'm very much indebted to Daryll for his
  22. * work on the X server.
  23. *
  24. * Voodoo3 support was contributed Harold Oga. Lots of additions
  25. * (proper acceleration, 24 bpp, hardware cursor) and bug fixes by Attila
  26. * Kesmarki. Thanks guys!
  27. *
  28. * Voodoo1 and Voodoo2 support aren't relevant to this driver as they
  29. * behave very differently from the Voodoo3/4/5. For anyone wanting to
  30. * use frame buffer on the Voodoo1/2, see the sstfb driver (which is
  31. * located at http://www.sourceforge.net/projects/sstfb).
  32. *
  33. * While I _am_ grateful to 3Dfx for releasing the specs for Banshee,
  34. * I do wish the next version is a bit more complete. Without the XF86
  35. * patches I couldn't have gotten even this far... for instance, the
  36. * extensions to the VGA register set go completely unmentioned in the
  37. * spec! Also, lots of references are made to the 'SST core', but no
  38. * spec is publicly available, AFAIK.
  39. *
  40. * The structure of this driver comes pretty much from the Permedia
  41. * driver by Ilario Nardinocchi, which in turn is based on skeletonfb.
  42. *
  43. * TODO:
  44. * - multihead support (basically need to support an array of fb_infos)
  45. * - support other architectures (PPC, Alpha); does the fact that the VGA
  46. * core can be accessed only thru I/O (not memory mapped) complicate
  47. * things?
  48. *
  49. * Version history:
  50. *
  51. * 0.1.4 (released 2002-05-28) ported over to new fbdev api by James Simmons
  52. *
  53. * 0.1.3 (released 1999-11-02) added Attila's panning support, code
  54. * reorg, hwcursor address page size alignment
  55. * (for mmaping both frame buffer and regs),
  56. * and my changes to get rid of hardcoded
  57. * VGA i/o register locations (uses PCI
  58. * configuration info now)
  59. * 0.1.2 (released 1999-10-19) added Attila Kesmarki's bug fixes and
  60. * improvements
  61. * 0.1.1 (released 1999-10-07) added Voodoo3 support by Harold Oga.
  62. * 0.1.0 (released 1999-10-06) initial version
  63. *
  64. */
  65. #include <linux/module.h>
  66. #include <linux/kernel.h>
  67. #include <linux/errno.h>
  68. #include <linux/string.h>
  69. #include <linux/mm.h>
  70. #include <linux/slab.h>
  71. #include <linux/fb.h>
  72. #include <linux/init.h>
  73. #include <linux/pci.h>
  74. #include <asm/io.h>
  75. #include <video/tdfx.h>
  76. #define DPRINTK(a, b...) pr_debug("fb: %s: " a, __func__ , ## b)
  77. #ifdef CONFIG_MTRR
  78. #include <asm/mtrr.h>
  79. #else
  80. /* duplicate asm/mtrr.h defines to work on archs without mtrr */
  81. #define MTRR_TYPE_WRCOMB 1
  82. static inline int mtrr_add(unsigned long base, unsigned long size,
  83. unsigned int type, char increment)
  84. {
  85. return -ENODEV;
  86. }
  87. static inline int mtrr_del(int reg, unsigned long base,
  88. unsigned long size)
  89. {
  90. return -ENODEV;
  91. }
  92. #endif
  93. #define BANSHEE_MAX_PIXCLOCK 270000
  94. #define VOODOO3_MAX_PIXCLOCK 300000
  95. #define VOODOO5_MAX_PIXCLOCK 350000
  96. static struct fb_fix_screeninfo tdfx_fix __devinitdata = {
  97. .type = FB_TYPE_PACKED_PIXELS,
  98. .visual = FB_VISUAL_PSEUDOCOLOR,
  99. .ypanstep = 1,
  100. .ywrapstep = 1,
  101. .accel = FB_ACCEL_3DFX_BANSHEE
  102. };
  103. static struct fb_var_screeninfo tdfx_var __devinitdata = {
  104. /* "640x480, 8 bpp @ 60 Hz */
  105. .xres = 640,
  106. .yres = 480,
  107. .xres_virtual = 640,
  108. .yres_virtual = 1024,
  109. .bits_per_pixel = 8,
  110. .red = {0, 8, 0},
  111. .blue = {0, 8, 0},
  112. .green = {0, 8, 0},
  113. .activate = FB_ACTIVATE_NOW,
  114. .height = -1,
  115. .width = -1,
  116. .accel_flags = FB_ACCELF_TEXT,
  117. .pixclock = 39722,
  118. .left_margin = 40,
  119. .right_margin = 24,
  120. .upper_margin = 32,
  121. .lower_margin = 11,
  122. .hsync_len = 96,
  123. .vsync_len = 2,
  124. .vmode = FB_VMODE_NONINTERLACED
  125. };
  126. /*
  127. * PCI driver prototypes
  128. */
  129. static int __devinit tdfxfb_probe(struct pci_dev *pdev,
  130. const struct pci_device_id *id);
  131. static void __devexit tdfxfb_remove(struct pci_dev *pdev);
  132. static struct pci_device_id tdfxfb_id_table[] = {
  133. { PCI_VENDOR_ID_3DFX, PCI_DEVICE_ID_3DFX_BANSHEE,
  134. PCI_ANY_ID, PCI_ANY_ID, PCI_BASE_CLASS_DISPLAY << 16,
  135. 0xff0000, 0 },
  136. { PCI_VENDOR_ID_3DFX, PCI_DEVICE_ID_3DFX_VOODOO3,
  137. PCI_ANY_ID, PCI_ANY_ID, PCI_BASE_CLASS_DISPLAY << 16,
  138. 0xff0000, 0 },
  139. { PCI_VENDOR_ID_3DFX, PCI_DEVICE_ID_3DFX_VOODOO5,
  140. PCI_ANY_ID, PCI_ANY_ID, PCI_BASE_CLASS_DISPLAY << 16,
  141. 0xff0000, 0 },
  142. { 0, }
  143. };
  144. static struct pci_driver tdfxfb_driver = {
  145. .name = "tdfxfb",
  146. .id_table = tdfxfb_id_table,
  147. .probe = tdfxfb_probe,
  148. .remove = __devexit_p(tdfxfb_remove),
  149. };
  150. MODULE_DEVICE_TABLE(pci, tdfxfb_id_table);
  151. /*
  152. * Driver data
  153. */
  154. static int nopan;
  155. static int nowrap = 1; /* not implemented (yet) */
  156. static int hwcursor = 1;
  157. static char *mode_option __devinitdata;
  158. /* mtrr option */
  159. static int nomtrr __devinitdata;
  160. /* -------------------------------------------------------------------------
  161. * Hardware-specific funcions
  162. * ------------------------------------------------------------------------- */
  163. static inline u8 vga_inb(struct tdfx_par *par, u32 reg)
  164. {
  165. return inb(par->iobase + reg - 0x300);
  166. }
  167. static inline void vga_outb(struct tdfx_par *par, u32 reg, u8 val)
  168. {
  169. outb(val, par->iobase + reg - 0x300);
  170. }
  171. static inline void gra_outb(struct tdfx_par *par, u32 idx, u8 val)
  172. {
  173. vga_outb(par, GRA_I, idx);
  174. wmb();
  175. vga_outb(par, GRA_D, val);
  176. wmb();
  177. }
  178. static inline void seq_outb(struct tdfx_par *par, u32 idx, u8 val)
  179. {
  180. vga_outb(par, SEQ_I, idx);
  181. wmb();
  182. vga_outb(par, SEQ_D, val);
  183. wmb();
  184. }
  185. static inline u8 seq_inb(struct tdfx_par *par, u32 idx)
  186. {
  187. vga_outb(par, SEQ_I, idx);
  188. mb();
  189. return vga_inb(par, SEQ_D);
  190. }
  191. static inline void crt_outb(struct tdfx_par *par, u32 idx, u8 val)
  192. {
  193. vga_outb(par, CRT_I, idx);
  194. wmb();
  195. vga_outb(par, CRT_D, val);
  196. wmb();
  197. }
  198. static inline u8 crt_inb(struct tdfx_par *par, u32 idx)
  199. {
  200. vga_outb(par, CRT_I, idx);
  201. mb();
  202. return vga_inb(par, CRT_D);
  203. }
  204. static inline void att_outb(struct tdfx_par *par, u32 idx, u8 val)
  205. {
  206. unsigned char tmp;
  207. tmp = vga_inb(par, IS1_R);
  208. vga_outb(par, ATT_IW, idx);
  209. vga_outb(par, ATT_IW, val);
  210. }
  211. static inline void vga_disable_video(struct tdfx_par *par)
  212. {
  213. unsigned char s;
  214. s = seq_inb(par, 0x01) | 0x20;
  215. seq_outb(par, 0x00, 0x01);
  216. seq_outb(par, 0x01, s);
  217. seq_outb(par, 0x00, 0x03);
  218. }
  219. static inline void vga_enable_video(struct tdfx_par *par)
  220. {
  221. unsigned char s;
  222. s = seq_inb(par, 0x01) & 0xdf;
  223. seq_outb(par, 0x00, 0x01);
  224. seq_outb(par, 0x01, s);
  225. seq_outb(par, 0x00, 0x03);
  226. }
  227. static inline void vga_enable_palette(struct tdfx_par *par)
  228. {
  229. vga_inb(par, IS1_R);
  230. mb();
  231. vga_outb(par, ATT_IW, 0x20);
  232. }
  233. static inline u32 tdfx_inl(struct tdfx_par *par, unsigned int reg)
  234. {
  235. return readl(par->regbase_virt + reg);
  236. }
  237. static inline void tdfx_outl(struct tdfx_par *par, unsigned int reg, u32 val)
  238. {
  239. writel(val, par->regbase_virt + reg);
  240. }
  241. static inline void banshee_make_room(struct tdfx_par *par, int size)
  242. {
  243. /* Note: The Voodoo3's onboard FIFO has 32 slots. This loop
  244. * won't quit if you ask for more. */
  245. while ((tdfx_inl(par, STATUS) & 0x1f) < size - 1)
  246. cpu_relax();
  247. }
  248. static int banshee_wait_idle(struct fb_info *info)
  249. {
  250. struct tdfx_par *par = info->par;
  251. int i = 0;
  252. banshee_make_room(par, 1);
  253. tdfx_outl(par, COMMAND_3D, COMMAND_3D_NOP);
  254. do {
  255. if ((tdfx_inl(par, STATUS) & STATUS_BUSY) == 0)
  256. i++;
  257. } while (i < 3);
  258. return 0;
  259. }
  260. /*
  261. * Set the color of a palette entry in 8bpp mode
  262. */
  263. static inline void do_setpalentry(struct tdfx_par *par, unsigned regno, u32 c)
  264. {
  265. banshee_make_room(par, 2);
  266. tdfx_outl(par, DACADDR, regno);
  267. /* read after write makes it working */
  268. tdfx_inl(par, DACADDR);
  269. tdfx_outl(par, DACDATA, c);
  270. }
  271. static u32 do_calc_pll(int freq, int *freq_out)
  272. {
  273. int m, n, k, best_m, best_n, best_k, best_error;
  274. int fref = 14318;
  275. best_error = freq;
  276. best_n = best_m = best_k = 0;
  277. for (k = 3; k >= 0; k--) {
  278. for (m = 63; m >= 0; m--) {
  279. /*
  280. * Estimate value of n that produces target frequency
  281. * with current m and k
  282. */
  283. int n_estimated = ((freq * (m + 2) << k) / fref) - 2;
  284. /* Search neighborhood of estimated n */
  285. for (n = max(0, n_estimated);
  286. n <= min(255, n_estimated + 1);
  287. n++) {
  288. /*
  289. * Calculate PLL freqency with current m, k and
  290. * estimated n
  291. */
  292. int f = (fref * (n + 2) / (m + 2)) >> k;
  293. int error = abs(f - freq);
  294. /*
  295. * If this is the closest we've come to the
  296. * target frequency then remember n, m and k
  297. */
  298. if (error < best_error) {
  299. best_error = error;
  300. best_n = n;
  301. best_m = m;
  302. best_k = k;
  303. }
  304. }
  305. }
  306. }
  307. n = best_n;
  308. m = best_m;
  309. k = best_k;
  310. *freq_out = (fref * (n + 2) / (m + 2)) >> k;
  311. return (n << 8) | (m << 2) | k;
  312. }
  313. static void do_write_regs(struct fb_info *info, struct banshee_reg *reg)
  314. {
  315. struct tdfx_par *par = info->par;
  316. int i;
  317. banshee_wait_idle(info);
  318. tdfx_outl(par, MISCINIT1, tdfx_inl(par, MISCINIT1) | 0x01);
  319. crt_outb(par, 0x11, crt_inb(par, 0x11) & 0x7f); /* CRT unprotect */
  320. banshee_make_room(par, 3);
  321. tdfx_outl(par, VGAINIT1, reg->vgainit1 & 0x001FFFFF);
  322. tdfx_outl(par, VIDPROCCFG, reg->vidcfg & ~0x00000001);
  323. #if 0
  324. tdfx_outl(par, PLLCTRL1, reg->mempll);
  325. tdfx_outl(par, PLLCTRL2, reg->gfxpll);
  326. #endif
  327. tdfx_outl(par, PLLCTRL0, reg->vidpll);
  328. vga_outb(par, MISC_W, reg->misc[0x00] | 0x01);
  329. for (i = 0; i < 5; i++)
  330. seq_outb(par, i, reg->seq[i]);
  331. for (i = 0; i < 25; i++)
  332. crt_outb(par, i, reg->crt[i]);
  333. for (i = 0; i < 9; i++)
  334. gra_outb(par, i, reg->gra[i]);
  335. for (i = 0; i < 21; i++)
  336. att_outb(par, i, reg->att[i]);
  337. crt_outb(par, 0x1a, reg->ext[0]);
  338. crt_outb(par, 0x1b, reg->ext[1]);
  339. vga_enable_palette(par);
  340. vga_enable_video(par);
  341. banshee_make_room(par, 9);
  342. tdfx_outl(par, VGAINIT0, reg->vgainit0);
  343. tdfx_outl(par, DACMODE, reg->dacmode);
  344. tdfx_outl(par, VIDDESKSTRIDE, reg->stride);
  345. tdfx_outl(par, HWCURPATADDR, reg->curspataddr);
  346. tdfx_outl(par, VIDSCREENSIZE, reg->screensize);
  347. tdfx_outl(par, VIDDESKSTART, reg->startaddr);
  348. tdfx_outl(par, VIDPROCCFG, reg->vidcfg);
  349. tdfx_outl(par, VGAINIT1, reg->vgainit1);
  350. tdfx_outl(par, MISCINIT0, reg->miscinit0);
  351. banshee_make_room(par, 8);
  352. tdfx_outl(par, SRCBASE, reg->startaddr);
  353. tdfx_outl(par, DSTBASE, reg->startaddr);
  354. tdfx_outl(par, COMMANDEXTRA_2D, 0);
  355. tdfx_outl(par, CLIP0MIN, 0);
  356. tdfx_outl(par, CLIP0MAX, 0x0fff0fff);
  357. tdfx_outl(par, CLIP1MIN, 0);
  358. tdfx_outl(par, CLIP1MAX, 0x0fff0fff);
  359. tdfx_outl(par, SRCXY, 0);
  360. banshee_wait_idle(info);
  361. }
  362. static unsigned long do_lfb_size(struct tdfx_par *par, unsigned short dev_id)
  363. {
  364. u32 draminit0 = tdfx_inl(par, DRAMINIT0);
  365. u32 draminit1 = tdfx_inl(par, DRAMINIT1);
  366. u32 miscinit1;
  367. int num_chips = (draminit0 & DRAMINIT0_SGRAM_NUM) ? 8 : 4;
  368. int chip_size; /* in MB */
  369. int has_sgram = draminit1 & DRAMINIT1_MEM_SDRAM;
  370. if (dev_id < PCI_DEVICE_ID_3DFX_VOODOO5) {
  371. /* Banshee/Voodoo3 */
  372. chip_size = 2;
  373. if (has_sgram && !(draminit0 & DRAMINIT0_SGRAM_TYPE))
  374. chip_size = 1;
  375. } else {
  376. /* Voodoo4/5 */
  377. has_sgram = 0;
  378. chip_size = draminit0 & DRAMINIT0_SGRAM_TYPE_MASK;
  379. chip_size = 1 << (chip_size >> DRAMINIT0_SGRAM_TYPE_SHIFT);
  380. }
  381. /* disable block writes for SDRAM */
  382. miscinit1 = tdfx_inl(par, MISCINIT1);
  383. miscinit1 |= has_sgram ? 0 : MISCINIT1_2DBLOCK_DIS;
  384. miscinit1 |= MISCINIT1_CLUT_INV;
  385. banshee_make_room(par, 1);
  386. tdfx_outl(par, MISCINIT1, miscinit1);
  387. return num_chips * chip_size * 1024l * 1024;
  388. }
  389. /* ------------------------------------------------------------------------- */
  390. static int tdfxfb_check_var(struct fb_var_screeninfo *var, struct fb_info *info)
  391. {
  392. struct tdfx_par *par = info->par;
  393. u32 lpitch;
  394. if (var->bits_per_pixel != 8 && var->bits_per_pixel != 16 &&
  395. var->bits_per_pixel != 24 && var->bits_per_pixel != 32) {
  396. DPRINTK("depth not supported: %u\n", var->bits_per_pixel);
  397. return -EINVAL;
  398. }
  399. if (var->xres != var->xres_virtual)
  400. var->xres_virtual = var->xres;
  401. if (var->yres > var->yres_virtual)
  402. var->yres_virtual = var->yres;
  403. if (var->xoffset) {
  404. DPRINTK("xoffset not supported\n");
  405. return -EINVAL;
  406. }
  407. var->yoffset = 0;
  408. /*
  409. * Banshee doesn't support interlace, but Voodoo4/5 and probably
  410. * Voodoo3 do.
  411. * no direct information about device id now?
  412. * use max_pixclock for this...
  413. */
  414. if (((var->vmode & FB_VMODE_MASK) == FB_VMODE_INTERLACED) &&
  415. (par->max_pixclock < VOODOO3_MAX_PIXCLOCK)) {
  416. DPRINTK("interlace not supported\n");
  417. return -EINVAL;
  418. }
  419. var->xres = (var->xres + 15) & ~15; /* could sometimes be 8 */
  420. lpitch = var->xres * ((var->bits_per_pixel + 7) >> 3);
  421. if (var->xres < 320 || var->xres > 2048) {
  422. DPRINTK("width not supported: %u\n", var->xres);
  423. return -EINVAL;
  424. }
  425. if (var->yres < 200 || var->yres > 2048) {
  426. DPRINTK("height not supported: %u\n", var->yres);
  427. return -EINVAL;
  428. }
  429. if (lpitch * var->yres_virtual > info->fix.smem_len) {
  430. var->yres_virtual = info->fix.smem_len / lpitch;
  431. if (var->yres_virtual < var->yres) {
  432. DPRINTK("no memory for screen (%ux%ux%u)\n",
  433. var->xres, var->yres_virtual,
  434. var->bits_per_pixel);
  435. return -EINVAL;
  436. }
  437. }
  438. if (PICOS2KHZ(var->pixclock) > par->max_pixclock) {
  439. DPRINTK("pixclock too high (%ldKHz)\n",
  440. PICOS2KHZ(var->pixclock));
  441. return -EINVAL;
  442. }
  443. var->transp.offset = 0;
  444. var->transp.length = 0;
  445. switch (var->bits_per_pixel) {
  446. case 8:
  447. var->red.length = 8;
  448. var->red.offset = 0;
  449. var->green = var->red;
  450. var->blue = var->red;
  451. break;
  452. case 16:
  453. var->red.offset = 11;
  454. var->red.length = 5;
  455. var->green.offset = 5;
  456. var->green.length = 6;
  457. var->blue.offset = 0;
  458. var->blue.length = 5;
  459. break;
  460. case 32:
  461. var->transp.offset = 24;
  462. var->transp.length = 8;
  463. case 24:
  464. var->red.offset = 16;
  465. var->green.offset = 8;
  466. var->blue.offset = 0;
  467. var->red.length = var->green.length = var->blue.length = 8;
  468. break;
  469. }
  470. var->width = -1;
  471. var->height = -1;
  472. var->accel_flags = FB_ACCELF_TEXT;
  473. DPRINTK("Checking graphics mode at %dx%d depth %d\n",
  474. var->xres, var->yres, var->bits_per_pixel);
  475. return 0;
  476. }
  477. static int tdfxfb_set_par(struct fb_info *info)
  478. {
  479. struct tdfx_par *par = info->par;
  480. u32 hdispend = info->var.xres;
  481. u32 hsyncsta = hdispend + info->var.right_margin;
  482. u32 hsyncend = hsyncsta + info->var.hsync_len;
  483. u32 htotal = hsyncend + info->var.left_margin;
  484. u32 hd, hs, he, ht, hbs, hbe;
  485. u32 vd, vs, ve, vt, vbs, vbe;
  486. struct banshee_reg reg;
  487. int fout, freq;
  488. u32 wd;
  489. u32 cpp = (info->var.bits_per_pixel + 7) >> 3;
  490. memset(&reg, 0, sizeof(reg));
  491. reg.vidcfg = VIDCFG_VIDPROC_ENABLE | VIDCFG_DESK_ENABLE |
  492. VIDCFG_CURS_X11 |
  493. ((cpp - 1) << VIDCFG_PIXFMT_SHIFT) |
  494. (cpp != 1 ? VIDCFG_CLUT_BYPASS : 0);
  495. /* PLL settings */
  496. freq = PICOS2KHZ(info->var.pixclock);
  497. reg.vidcfg &= ~VIDCFG_2X;
  498. if (freq > par->max_pixclock / 2) {
  499. freq = freq > par->max_pixclock ? par->max_pixclock : freq;
  500. reg.dacmode |= DACMODE_2X;
  501. reg.vidcfg |= VIDCFG_2X;
  502. hdispend >>= 1;
  503. hsyncsta >>= 1;
  504. hsyncend >>= 1;
  505. htotal >>= 1;
  506. }
  507. wd = (hdispend >> 3) - 1;
  508. hd = wd;
  509. hs = (hsyncsta >> 3) - 1;
  510. he = (hsyncend >> 3) - 1;
  511. ht = (htotal >> 3) - 1;
  512. hbs = hd;
  513. hbe = ht;
  514. if ((info->var.vmode & FB_VMODE_MASK) == FB_VMODE_DOUBLE) {
  515. vd = (info->var.yres << 1) - 1;
  516. vs = vd + (info->var.lower_margin << 1);
  517. ve = vs + (info->var.vsync_len << 1);
  518. vt = ve + (info->var.upper_margin << 1) - 1;
  519. reg.screensize = info->var.xres | (info->var.yres << 13);
  520. reg.vidcfg |= VIDCFG_HALF_MODE;
  521. reg.crt[0x09] = 0x80;
  522. } else {
  523. vd = info->var.yres - 1;
  524. vs = vd + info->var.lower_margin;
  525. ve = vs + info->var.vsync_len;
  526. vt = ve + info->var.upper_margin - 1;
  527. reg.screensize = info->var.xres | (info->var.yres << 12);
  528. reg.vidcfg &= ~VIDCFG_HALF_MODE;
  529. }
  530. vbs = vd;
  531. vbe = vt;
  532. /* this is all pretty standard VGA register stuffing */
  533. reg.misc[0x00] = 0x0f |
  534. (info->var.xres < 400 ? 0xa0 :
  535. info->var.xres < 480 ? 0x60 :
  536. info->var.xres < 768 ? 0xe0 : 0x20);
  537. reg.gra[0x05] = 0x40;
  538. reg.gra[0x06] = 0x05;
  539. reg.gra[0x07] = 0x0f;
  540. reg.gra[0x08] = 0xff;
  541. reg.att[0x00] = 0x00;
  542. reg.att[0x01] = 0x01;
  543. reg.att[0x02] = 0x02;
  544. reg.att[0x03] = 0x03;
  545. reg.att[0x04] = 0x04;
  546. reg.att[0x05] = 0x05;
  547. reg.att[0x06] = 0x06;
  548. reg.att[0x07] = 0x07;
  549. reg.att[0x08] = 0x08;
  550. reg.att[0x09] = 0x09;
  551. reg.att[0x0a] = 0x0a;
  552. reg.att[0x0b] = 0x0b;
  553. reg.att[0x0c] = 0x0c;
  554. reg.att[0x0d] = 0x0d;
  555. reg.att[0x0e] = 0x0e;
  556. reg.att[0x0f] = 0x0f;
  557. reg.att[0x10] = 0x41;
  558. reg.att[0x12] = 0x0f;
  559. reg.seq[0x00] = 0x03;
  560. reg.seq[0x01] = 0x01; /* fixme: clkdiv2? */
  561. reg.seq[0x02] = 0x0f;
  562. reg.seq[0x03] = 0x00;
  563. reg.seq[0x04] = 0x0e;
  564. reg.crt[0x00] = ht - 4;
  565. reg.crt[0x01] = hd;
  566. reg.crt[0x02] = hbs;
  567. reg.crt[0x03] = 0x80 | (hbe & 0x1f);
  568. reg.crt[0x04] = hs;
  569. reg.crt[0x05] = ((hbe & 0x20) << 2) | (he & 0x1f);
  570. reg.crt[0x06] = vt;
  571. reg.crt[0x07] = ((vs & 0x200) >> 2) |
  572. ((vd & 0x200) >> 3) |
  573. ((vt & 0x200) >> 4) | 0x10 |
  574. ((vbs & 0x100) >> 5) |
  575. ((vs & 0x100) >> 6) |
  576. ((vd & 0x100) >> 7) |
  577. ((vt & 0x100) >> 8);
  578. reg.crt[0x09] |= 0x40 | ((vbs & 0x200) >> 4);
  579. reg.crt[0x10] = vs;
  580. reg.crt[0x11] = (ve & 0x0f) | 0x20;
  581. reg.crt[0x12] = vd;
  582. reg.crt[0x13] = wd;
  583. reg.crt[0x15] = vbs;
  584. reg.crt[0x16] = vbe + 1;
  585. reg.crt[0x17] = 0xc3;
  586. reg.crt[0x18] = 0xff;
  587. /* Banshee's nonvga stuff */
  588. reg.ext[0x00] = (((ht & 0x100) >> 8) |
  589. ((hd & 0x100) >> 6) |
  590. ((hbs & 0x100) >> 4) |
  591. ((hbe & 0x40) >> 1) |
  592. ((hs & 0x100) >> 2) |
  593. ((he & 0x20) << 2));
  594. reg.ext[0x01] = (((vt & 0x400) >> 10) |
  595. ((vd & 0x400) >> 8) |
  596. ((vbs & 0x400) >> 6) |
  597. ((vbe & 0x400) >> 4));
  598. reg.vgainit0 = VGAINIT0_8BIT_DAC |
  599. VGAINIT0_EXT_ENABLE |
  600. VGAINIT0_WAKEUP_3C3 |
  601. VGAINIT0_ALT_READBACK |
  602. VGAINIT0_EXTSHIFTOUT;
  603. reg.vgainit1 = tdfx_inl(par, VGAINIT1) & 0x1fffff;
  604. if (hwcursor)
  605. reg.curspataddr = info->fix.smem_len;
  606. reg.cursloc = 0;
  607. reg.cursc0 = 0;
  608. reg.cursc1 = 0xffffff;
  609. reg.stride = info->var.xres * cpp;
  610. reg.startaddr = info->var.yoffset * reg.stride
  611. + info->var.xoffset * cpp;
  612. reg.vidpll = do_calc_pll(freq, &fout);
  613. #if 0
  614. reg.mempll = do_calc_pll(..., &fout);
  615. reg.gfxpll = do_calc_pll(..., &fout);
  616. #endif
  617. if ((info->var.vmode & FB_VMODE_MASK) == FB_VMODE_INTERLACED)
  618. reg.vidcfg |= VIDCFG_INTERLACE;
  619. reg.miscinit0 = tdfx_inl(par, MISCINIT0);
  620. #if defined(__BIG_ENDIAN)
  621. switch (info->var.bits_per_pixel) {
  622. case 8:
  623. case 24:
  624. reg.miscinit0 &= ~(1 << 30);
  625. reg.miscinit0 &= ~(1 << 31);
  626. break;
  627. case 16:
  628. reg.miscinit0 |= (1 << 30);
  629. reg.miscinit0 |= (1 << 31);
  630. break;
  631. case 32:
  632. reg.miscinit0 |= (1 << 30);
  633. reg.miscinit0 &= ~(1 << 31);
  634. break;
  635. }
  636. #endif
  637. do_write_regs(info, &reg);
  638. /* Now change fb_fix_screeninfo according to changes in par */
  639. info->fix.line_length = reg.stride;
  640. info->fix.visual = (info->var.bits_per_pixel == 8)
  641. ? FB_VISUAL_PSEUDOCOLOR
  642. : FB_VISUAL_TRUECOLOR;
  643. DPRINTK("Graphics mode is now set at %dx%d depth %d\n",
  644. info->var.xres, info->var.yres, info->var.bits_per_pixel);
  645. return 0;
  646. }
  647. /* A handy macro shamelessly pinched from matroxfb */
  648. #define CNVT_TOHW(val, width) ((((val) << (width)) + 0x7FFF - (val)) >> 16)
  649. static int tdfxfb_setcolreg(unsigned regno, unsigned red, unsigned green,
  650. unsigned blue, unsigned transp,
  651. struct fb_info *info)
  652. {
  653. struct tdfx_par *par = info->par;
  654. u32 rgbcol;
  655. if (regno >= info->cmap.len || regno > 255)
  656. return 1;
  657. /* grayscale works only partially under directcolor */
  658. if (info->var.grayscale) {
  659. /* grayscale = 0.30*R + 0.59*G + 0.11*B */
  660. blue = (red * 77 + green * 151 + blue * 28) >> 8;
  661. green = blue;
  662. red = blue;
  663. }
  664. switch (info->fix.visual) {
  665. case FB_VISUAL_PSEUDOCOLOR:
  666. rgbcol = (((u32)red & 0xff00) << 8) |
  667. (((u32)green & 0xff00) << 0) |
  668. (((u32)blue & 0xff00) >> 8);
  669. do_setpalentry(par, regno, rgbcol);
  670. break;
  671. /* Truecolor has no hardware color palettes. */
  672. case FB_VISUAL_TRUECOLOR:
  673. if (regno < 16) {
  674. rgbcol = (CNVT_TOHW(red, info->var.red.length) <<
  675. info->var.red.offset) |
  676. (CNVT_TOHW(green, info->var.green.length) <<
  677. info->var.green.offset) |
  678. (CNVT_TOHW(blue, info->var.blue.length) <<
  679. info->var.blue.offset) |
  680. (CNVT_TOHW(transp, info->var.transp.length) <<
  681. info->var.transp.offset);
  682. par->palette[regno] = rgbcol;
  683. }
  684. break;
  685. default:
  686. DPRINTK("bad depth %u\n", info->var.bits_per_pixel);
  687. break;
  688. }
  689. return 0;
  690. }
  691. /* 0 unblank, 1 blank, 2 no vsync, 3 no hsync, 4 off */
  692. static int tdfxfb_blank(int blank, struct fb_info *info)
  693. {
  694. struct tdfx_par *par = info->par;
  695. int vgablank = 1;
  696. u32 dacmode = tdfx_inl(par, DACMODE);
  697. dacmode &= ~(BIT(1) | BIT(3));
  698. switch (blank) {
  699. case FB_BLANK_UNBLANK: /* Screen: On; HSync: On, VSync: On */
  700. vgablank = 0;
  701. break;
  702. case FB_BLANK_NORMAL: /* Screen: Off; HSync: On, VSync: On */
  703. break;
  704. case FB_BLANK_VSYNC_SUSPEND: /* Screen: Off; HSync: On, VSync: Off */
  705. dacmode |= BIT(3);
  706. break;
  707. case FB_BLANK_HSYNC_SUSPEND: /* Screen: Off; HSync: Off, VSync: On */
  708. dacmode |= BIT(1);
  709. break;
  710. case FB_BLANK_POWERDOWN: /* Screen: Off; HSync: Off, VSync: Off */
  711. dacmode |= BIT(1) | BIT(3);
  712. break;
  713. }
  714. banshee_make_room(par, 1);
  715. tdfx_outl(par, DACMODE, dacmode);
  716. if (vgablank)
  717. vga_disable_video(par);
  718. else
  719. vga_enable_video(par);
  720. return 0;
  721. }
  722. /*
  723. * Set the starting position of the visible screen to var->yoffset
  724. */
  725. static int tdfxfb_pan_display(struct fb_var_screeninfo *var,
  726. struct fb_info *info)
  727. {
  728. struct tdfx_par *par = info->par;
  729. u32 addr = var->yoffset * info->fix.line_length;
  730. if (nopan || var->xoffset)
  731. return -EINVAL;
  732. banshee_make_room(par, 1);
  733. tdfx_outl(par, VIDDESKSTART, addr);
  734. return 0;
  735. }
  736. #ifdef CONFIG_FB_3DFX_ACCEL
  737. /*
  738. * FillRect 2D command (solidfill or invert (via ROP_XOR))
  739. */
  740. static void tdfxfb_fillrect(struct fb_info *info,
  741. const struct fb_fillrect *rect)
  742. {
  743. struct tdfx_par *par = info->par;
  744. u32 bpp = info->var.bits_per_pixel;
  745. u32 stride = info->fix.line_length;
  746. u32 fmt = stride | ((bpp + ((bpp == 8) ? 0 : 8)) << 13);
  747. int tdfx_rop;
  748. u32 dx = rect->dx;
  749. u32 dy = rect->dy;
  750. u32 dstbase = 0;
  751. if (rect->rop == ROP_COPY)
  752. tdfx_rop = TDFX_ROP_COPY;
  753. else
  754. tdfx_rop = TDFX_ROP_XOR;
  755. /* asume always rect->height < 4096 */
  756. if (dy + rect->height > 4095) {
  757. dstbase = stride * dy;
  758. dy = 0;
  759. }
  760. /* asume always rect->width < 4096 */
  761. if (dx + rect->width > 4095) {
  762. dstbase += dx * bpp >> 3;
  763. dx = 0;
  764. }
  765. banshee_make_room(par, 6);
  766. tdfx_outl(par, DSTFORMAT, fmt);
  767. if (info->fix.visual == FB_VISUAL_PSEUDOCOLOR) {
  768. tdfx_outl(par, COLORFORE, rect->color);
  769. } else { /* FB_VISUAL_TRUECOLOR */
  770. tdfx_outl(par, COLORFORE, par->palette[rect->color]);
  771. }
  772. tdfx_outl(par, COMMAND_2D, COMMAND_2D_FILLRECT | (tdfx_rop << 24));
  773. tdfx_outl(par, DSTBASE, dstbase);
  774. tdfx_outl(par, DSTSIZE, rect->width | (rect->height << 16));
  775. tdfx_outl(par, LAUNCH_2D, dx | (dy << 16));
  776. }
  777. /*
  778. * Screen-to-Screen BitBlt 2D command (for the bmove fb op.)
  779. */
  780. static void tdfxfb_copyarea(struct fb_info *info,
  781. const struct fb_copyarea *area)
  782. {
  783. struct tdfx_par *par = info->par;
  784. u32 sx = area->sx, sy = area->sy, dx = area->dx, dy = area->dy;
  785. u32 bpp = info->var.bits_per_pixel;
  786. u32 stride = info->fix.line_length;
  787. u32 blitcmd = COMMAND_2D_S2S_BITBLT | (TDFX_ROP_COPY << 24);
  788. u32 fmt = stride | ((bpp + ((bpp == 8) ? 0 : 8)) << 13);
  789. u32 dstbase = 0;
  790. u32 srcbase = 0;
  791. /* asume always area->height < 4096 */
  792. if (sy + area->height > 4095) {
  793. srcbase = stride * sy;
  794. sy = 0;
  795. }
  796. /* asume always area->width < 4096 */
  797. if (sx + area->width > 4095) {
  798. srcbase += sx * bpp >> 3;
  799. sx = 0;
  800. }
  801. /* asume always area->height < 4096 */
  802. if (dy + area->height > 4095) {
  803. dstbase = stride * dy;
  804. dy = 0;
  805. }
  806. /* asume always area->width < 4096 */
  807. if (dx + area->width > 4095) {
  808. dstbase += dx * bpp >> 3;
  809. dx = 0;
  810. }
  811. if (area->sx <= area->dx) {
  812. /* -X */
  813. blitcmd |= BIT(14);
  814. sx += area->width - 1;
  815. dx += area->width - 1;
  816. }
  817. if (area->sy <= area->dy) {
  818. /* -Y */
  819. blitcmd |= BIT(15);
  820. sy += area->height - 1;
  821. dy += area->height - 1;
  822. }
  823. banshee_make_room(par, 8);
  824. tdfx_outl(par, SRCFORMAT, fmt);
  825. tdfx_outl(par, DSTFORMAT, fmt);
  826. tdfx_outl(par, COMMAND_2D, blitcmd);
  827. tdfx_outl(par, DSTSIZE, area->width | (area->height << 16));
  828. tdfx_outl(par, DSTXY, dx | (dy << 16));
  829. tdfx_outl(par, SRCBASE, srcbase);
  830. tdfx_outl(par, DSTBASE, dstbase);
  831. tdfx_outl(par, LAUNCH_2D, sx | (sy << 16));
  832. }
  833. static void tdfxfb_imageblit(struct fb_info *info, const struct fb_image *image)
  834. {
  835. struct tdfx_par *par = info->par;
  836. int size = image->height * ((image->width * image->depth + 7) >> 3);
  837. int fifo_free;
  838. int i, stride = info->fix.line_length;
  839. u32 bpp = info->var.bits_per_pixel;
  840. u32 dstfmt = stride | ((bpp + ((bpp == 8) ? 0 : 8)) << 13);
  841. u8 *chardata = (u8 *) image->data;
  842. u32 srcfmt;
  843. u32 dx = image->dx;
  844. u32 dy = image->dy;
  845. u32 dstbase = 0;
  846. if (image->depth != 1) {
  847. #ifdef BROKEN_CODE
  848. banshee_make_room(par, 6 + ((size + 3) >> 2));
  849. srcfmt = stride | ((bpp + ((bpp == 8) ? 0 : 8)) << 13) |
  850. 0x400000;
  851. #else
  852. cfb_imageblit(info, image);
  853. #endif
  854. return;
  855. }
  856. banshee_make_room(par, 9);
  857. switch (info->fix.visual) {
  858. case FB_VISUAL_PSEUDOCOLOR:
  859. tdfx_outl(par, COLORFORE, image->fg_color);
  860. tdfx_outl(par, COLORBACK, image->bg_color);
  861. break;
  862. case FB_VISUAL_TRUECOLOR:
  863. default:
  864. tdfx_outl(par, COLORFORE,
  865. par->palette[image->fg_color]);
  866. tdfx_outl(par, COLORBACK,
  867. par->palette[image->bg_color]);
  868. }
  869. #ifdef __BIG_ENDIAN
  870. srcfmt = 0x400000 | BIT(20);
  871. #else
  872. srcfmt = 0x400000;
  873. #endif
  874. /* asume always image->height < 4096 */
  875. if (dy + image->height > 4095) {
  876. dstbase = stride * dy;
  877. dy = 0;
  878. }
  879. /* asume always image->width < 4096 */
  880. if (dx + image->width > 4095) {
  881. dstbase += dx * bpp >> 3;
  882. dx = 0;
  883. }
  884. tdfx_outl(par, DSTBASE, dstbase);
  885. tdfx_outl(par, SRCXY, 0);
  886. tdfx_outl(par, DSTXY, dx | (dy << 16));
  887. tdfx_outl(par, COMMAND_2D,
  888. COMMAND_2D_H2S_BITBLT | (TDFX_ROP_COPY << 24));
  889. tdfx_outl(par, SRCFORMAT, srcfmt);
  890. tdfx_outl(par, DSTFORMAT, dstfmt);
  891. tdfx_outl(par, DSTSIZE, image->width | (image->height << 16));
  892. /* A count of how many free FIFO entries we've requested.
  893. * When this goes negative, we need to request more. */
  894. fifo_free = 0;
  895. /* Send four bytes at a time of data */
  896. for (i = (size >> 2); i > 0; i--) {
  897. if (--fifo_free < 0) {
  898. fifo_free = 31;
  899. banshee_make_room(par, fifo_free);
  900. }
  901. tdfx_outl(par, LAUNCH_2D, *(u32 *)chardata);
  902. chardata += 4;
  903. }
  904. /* Send the leftovers now */
  905. banshee_make_room(par, 3);
  906. switch (size % 4) {
  907. case 0:
  908. break;
  909. case 1:
  910. tdfx_outl(par, LAUNCH_2D, *chardata);
  911. break;
  912. case 2:
  913. tdfx_outl(par, LAUNCH_2D, *(u16 *)chardata);
  914. break;
  915. case 3:
  916. tdfx_outl(par, LAUNCH_2D,
  917. *(u16 *)chardata | (chardata[3] << 24));
  918. break;
  919. }
  920. }
  921. #endif /* CONFIG_FB_3DFX_ACCEL */
  922. static int tdfxfb_cursor(struct fb_info *info, struct fb_cursor *cursor)
  923. {
  924. struct tdfx_par *par = info->par;
  925. u32 vidcfg;
  926. if (!hwcursor)
  927. return -EINVAL; /* just to force soft_cursor() call */
  928. /* Too large of a cursor or wrong bpp :-( */
  929. if (cursor->image.width > 64 ||
  930. cursor->image.height > 64 ||
  931. cursor->image.depth > 1)
  932. return -EINVAL;
  933. vidcfg = tdfx_inl(par, VIDPROCCFG);
  934. if (cursor->enable)
  935. tdfx_outl(par, VIDPROCCFG, vidcfg | VIDCFG_HWCURSOR_ENABLE);
  936. else
  937. tdfx_outl(par, VIDPROCCFG, vidcfg & ~VIDCFG_HWCURSOR_ENABLE);
  938. /*
  939. * If the cursor is not be changed this means either we want the
  940. * current cursor state (if enable is set) or we want to query what
  941. * we can do with the cursor (if enable is not set)
  942. */
  943. if (!cursor->set)
  944. return 0;
  945. /* fix cursor color - XFree86 forgets to restore it properly */
  946. if (cursor->set & FB_CUR_SETCMAP) {
  947. struct fb_cmap cmap = info->cmap;
  948. u32 bg_idx = cursor->image.bg_color;
  949. u32 fg_idx = cursor->image.fg_color;
  950. unsigned long bg_color, fg_color;
  951. fg_color = (((u32)cmap.red[fg_idx] & 0xff00) << 8) |
  952. (((u32)cmap.green[fg_idx] & 0xff00) << 0) |
  953. (((u32)cmap.blue[fg_idx] & 0xff00) >> 8);
  954. bg_color = (((u32)cmap.red[bg_idx] & 0xff00) << 8) |
  955. (((u32)cmap.green[bg_idx] & 0xff00) << 0) |
  956. (((u32)cmap.blue[bg_idx] & 0xff00) >> 8);
  957. banshee_make_room(par, 2);
  958. tdfx_outl(par, HWCURC0, bg_color);
  959. tdfx_outl(par, HWCURC1, fg_color);
  960. }
  961. if (cursor->set & FB_CUR_SETPOS) {
  962. int x = cursor->image.dx;
  963. int y = cursor->image.dy - info->var.yoffset;
  964. x += 63;
  965. y += 63;
  966. banshee_make_room(par, 1);
  967. tdfx_outl(par, HWCURLOC, (y << 16) + x);
  968. }
  969. if (cursor->set & (FB_CUR_SETIMAGE | FB_CUR_SETSHAPE)) {
  970. /*
  971. * Voodoo 3 and above cards use 2 monochrome cursor patterns.
  972. * The reason is so the card can fetch 8 words at a time
  973. * and are stored on chip for use for the next 8 scanlines.
  974. * This reduces the number of times for access to draw the
  975. * cursor for each screen refresh.
  976. * Each pattern is a bitmap of 64 bit wide and 64 bit high
  977. * (total of 8192 bits or 1024 bytes). The two patterns are
  978. * stored in such a way that pattern 0 always resides in the
  979. * lower half (least significant 64 bits) of a 128 bit word
  980. * and pattern 1 the upper half. If you examine the data of
  981. * the cursor image the graphics card uses then from the
  982. * begining you see line one of pattern 0, line one of
  983. * pattern 1, line two of pattern 0, line two of pattern 1,
  984. * etc etc. The linear stride for the cursor is always 16 bytes
  985. * (128 bits) which is the maximum cursor width times two for
  986. * the two monochrome patterns.
  987. */
  988. u8 __iomem *cursorbase = info->screen_base + info->fix.smem_len;
  989. u8 *bitmap = (u8 *)cursor->image.data;
  990. u8 *mask = (u8 *)cursor->mask;
  991. int i;
  992. fb_memset(cursorbase, 0, 1024);
  993. for (i = 0; i < cursor->image.height; i++) {
  994. int h = 0;
  995. int j = (cursor->image.width + 7) >> 3;
  996. for (; j > 0; j--) {
  997. u8 data = *mask ^ *bitmap;
  998. if (cursor->rop == ROP_COPY)
  999. data = *mask & *bitmap;
  1000. /* Pattern 0. Copy the cursor mask to it */
  1001. fb_writeb(*mask, cursorbase + h);
  1002. mask++;
  1003. /* Pattern 1. Copy the cursor bitmap to it */
  1004. fb_writeb(data, cursorbase + h + 8);
  1005. bitmap++;
  1006. h++;
  1007. }
  1008. cursorbase += 16;
  1009. }
  1010. }
  1011. return 0;
  1012. }
  1013. static struct fb_ops tdfxfb_ops = {
  1014. .owner = THIS_MODULE,
  1015. .fb_check_var = tdfxfb_check_var,
  1016. .fb_set_par = tdfxfb_set_par,
  1017. .fb_setcolreg = tdfxfb_setcolreg,
  1018. .fb_blank = tdfxfb_blank,
  1019. .fb_pan_display = tdfxfb_pan_display,
  1020. .fb_sync = banshee_wait_idle,
  1021. .fb_cursor = tdfxfb_cursor,
  1022. #ifdef CONFIG_FB_3DFX_ACCEL
  1023. .fb_fillrect = tdfxfb_fillrect,
  1024. .fb_copyarea = tdfxfb_copyarea,
  1025. .fb_imageblit = tdfxfb_imageblit,
  1026. #else
  1027. .fb_fillrect = cfb_fillrect,
  1028. .fb_copyarea = cfb_copyarea,
  1029. .fb_imageblit = cfb_imageblit,
  1030. #endif
  1031. };
  1032. #ifdef CONFIG_FB_3DFX_I2C
  1033. /* The voo GPIO registers don't have individual masks for each bit
  1034. so we always have to read before writing. */
  1035. static void tdfxfb_i2c_setscl(void *data, int val)
  1036. {
  1037. struct tdfxfb_i2c_chan *chan = data;
  1038. struct tdfx_par *par = chan->par;
  1039. unsigned int r;
  1040. r = tdfx_inl(par, VIDSERPARPORT);
  1041. if (val)
  1042. r |= I2C_SCL_OUT;
  1043. else
  1044. r &= ~I2C_SCL_OUT;
  1045. tdfx_outl(par, VIDSERPARPORT, r);
  1046. tdfx_inl(par, VIDSERPARPORT); /* flush posted write */
  1047. }
  1048. static void tdfxfb_i2c_setsda(void *data, int val)
  1049. {
  1050. struct tdfxfb_i2c_chan *chan = data;
  1051. struct tdfx_par *par = chan->par;
  1052. unsigned int r;
  1053. r = tdfx_inl(par, VIDSERPARPORT);
  1054. if (val)
  1055. r |= I2C_SDA_OUT;
  1056. else
  1057. r &= ~I2C_SDA_OUT;
  1058. tdfx_outl(par, VIDSERPARPORT, r);
  1059. tdfx_inl(par, VIDSERPARPORT); /* flush posted write */
  1060. }
  1061. /* The GPIO pins are open drain, so the pins always remain outputs.
  1062. We rely on the i2c-algo-bit routines to set the pins high before
  1063. reading the input from other chips. */
  1064. static int tdfxfb_i2c_getscl(void *data)
  1065. {
  1066. struct tdfxfb_i2c_chan *chan = data;
  1067. struct tdfx_par *par = chan->par;
  1068. return (0 != (tdfx_inl(par, VIDSERPARPORT) & I2C_SCL_IN));
  1069. }
  1070. static int tdfxfb_i2c_getsda(void *data)
  1071. {
  1072. struct tdfxfb_i2c_chan *chan = data;
  1073. struct tdfx_par *par = chan->par;
  1074. return (0 != (tdfx_inl(par, VIDSERPARPORT) & I2C_SDA_IN));
  1075. }
  1076. static void tdfxfb_ddc_setscl(void *data, int val)
  1077. {
  1078. struct tdfxfb_i2c_chan *chan = data;
  1079. struct tdfx_par *par = chan->par;
  1080. unsigned int r;
  1081. r = tdfx_inl(par, VIDSERPARPORT);
  1082. if (val)
  1083. r |= DDC_SCL_OUT;
  1084. else
  1085. r &= ~DDC_SCL_OUT;
  1086. tdfx_outl(par, VIDSERPARPORT, r);
  1087. tdfx_inl(par, VIDSERPARPORT); /* flush posted write */
  1088. }
  1089. static void tdfxfb_ddc_setsda(void *data, int val)
  1090. {
  1091. struct tdfxfb_i2c_chan *chan = data;
  1092. struct tdfx_par *par = chan->par;
  1093. unsigned int r;
  1094. r = tdfx_inl(par, VIDSERPARPORT);
  1095. if (val)
  1096. r |= DDC_SDA_OUT;
  1097. else
  1098. r &= ~DDC_SDA_OUT;
  1099. tdfx_outl(par, VIDSERPARPORT, r);
  1100. tdfx_inl(par, VIDSERPARPORT); /* flush posted write */
  1101. }
  1102. static int tdfxfb_ddc_getscl(void *data)
  1103. {
  1104. struct tdfxfb_i2c_chan *chan = data;
  1105. struct tdfx_par *par = chan->par;
  1106. return (0 != (tdfx_inl(par, VIDSERPARPORT) & DDC_SCL_IN));
  1107. }
  1108. static int tdfxfb_ddc_getsda(void *data)
  1109. {
  1110. struct tdfxfb_i2c_chan *chan = data;
  1111. struct tdfx_par *par = chan->par;
  1112. return (0 != (tdfx_inl(par, VIDSERPARPORT) & DDC_SDA_IN));
  1113. }
  1114. static int __devinit tdfxfb_setup_ddc_bus(struct tdfxfb_i2c_chan *chan,
  1115. const char *name, struct device *dev)
  1116. {
  1117. int rc;
  1118. strlcpy(chan->adapter.name, name, sizeof(chan->adapter.name));
  1119. chan->adapter.owner = THIS_MODULE;
  1120. chan->adapter.class = I2C_CLASS_DDC;
  1121. chan->adapter.algo_data = &chan->algo;
  1122. chan->adapter.dev.parent = dev;
  1123. chan->algo.setsda = tdfxfb_ddc_setsda;
  1124. chan->algo.setscl = tdfxfb_ddc_setscl;
  1125. chan->algo.getsda = tdfxfb_ddc_getsda;
  1126. chan->algo.getscl = tdfxfb_ddc_getscl;
  1127. chan->algo.udelay = 10;
  1128. chan->algo.timeout = msecs_to_jiffies(500);
  1129. chan->algo.data = chan;
  1130. i2c_set_adapdata(&chan->adapter, chan);
  1131. rc = i2c_bit_add_bus(&chan->adapter);
  1132. if (rc == 0)
  1133. DPRINTK("I2C bus %s registered.\n", name);
  1134. else
  1135. chan->par = NULL;
  1136. return rc;
  1137. }
  1138. static int __devinit tdfxfb_setup_i2c_bus(struct tdfxfb_i2c_chan *chan,
  1139. const char *name, struct device *dev)
  1140. {
  1141. int rc;
  1142. strlcpy(chan->adapter.name, name, sizeof(chan->adapter.name));
  1143. chan->adapter.owner = THIS_MODULE;
  1144. chan->adapter.class = I2C_CLASS_TV_ANALOG;
  1145. chan->adapter.algo_data = &chan->algo;
  1146. chan->adapter.dev.parent = dev;
  1147. chan->algo.setsda = tdfxfb_i2c_setsda;
  1148. chan->algo.setscl = tdfxfb_i2c_setscl;
  1149. chan->algo.getsda = tdfxfb_i2c_getsda;
  1150. chan->algo.getscl = tdfxfb_i2c_getscl;
  1151. chan->algo.udelay = 10;
  1152. chan->algo.timeout = msecs_to_jiffies(500);
  1153. chan->algo.data = chan;
  1154. i2c_set_adapdata(&chan->adapter, chan);
  1155. rc = i2c_bit_add_bus(&chan->adapter);
  1156. if (rc == 0)
  1157. DPRINTK("I2C bus %s registered.\n", name);
  1158. else
  1159. chan->par = NULL;
  1160. return rc;
  1161. }
  1162. static void __devinit tdfxfb_create_i2c_busses(struct fb_info *info)
  1163. {
  1164. struct tdfx_par *par = info->par;
  1165. tdfx_outl(par, VIDINFORMAT, 0x8160);
  1166. tdfx_outl(par, VIDSERPARPORT, 0xcffc0020);
  1167. par->chan[0].par = par;
  1168. par->chan[1].par = par;
  1169. tdfxfb_setup_ddc_bus(&par->chan[0], "Voodoo3-DDC", info->dev);
  1170. tdfxfb_setup_i2c_bus(&par->chan[1], "Voodoo3-I2C", info->dev);
  1171. }
  1172. static void tdfxfb_delete_i2c_busses(struct tdfx_par *par)
  1173. {
  1174. if (par->chan[0].par)
  1175. i2c_del_adapter(&par->chan[0].adapter);
  1176. par->chan[0].par = NULL;
  1177. if (par->chan[1].par)
  1178. i2c_del_adapter(&par->chan[1].adapter);
  1179. par->chan[1].par = NULL;
  1180. }
  1181. #endif /* CONFIG_FB_3DFX_I2C */
  1182. /**
  1183. * tdfxfb_probe - Device Initializiation
  1184. *
  1185. * @pdev: PCI Device to initialize
  1186. * @id: PCI Device ID
  1187. *
  1188. * Initializes and allocates resources for PCI device @pdev.
  1189. *
  1190. */
  1191. static int __devinit tdfxfb_probe(struct pci_dev *pdev,
  1192. const struct pci_device_id *id)
  1193. {
  1194. struct tdfx_par *default_par;
  1195. struct fb_info *info;
  1196. int err, lpitch;
  1197. err = pci_enable_device(pdev);
  1198. if (err) {
  1199. printk(KERN_ERR "tdfxfb: Can't enable pdev: %d\n", err);
  1200. return err;
  1201. }
  1202. info = framebuffer_alloc(sizeof(struct tdfx_par), &pdev->dev);
  1203. if (!info)
  1204. return -ENOMEM;
  1205. default_par = info->par;
  1206. info->fix = tdfx_fix;
  1207. /* Configure the default fb_fix_screeninfo first */
  1208. switch (pdev->device) {
  1209. case PCI_DEVICE_ID_3DFX_BANSHEE:
  1210. strcpy(info->fix.id, "3Dfx Banshee");
  1211. default_par->max_pixclock = BANSHEE_MAX_PIXCLOCK;
  1212. break;
  1213. case PCI_DEVICE_ID_3DFX_VOODOO3:
  1214. strcpy(info->fix.id, "3Dfx Voodoo3");
  1215. default_par->max_pixclock = VOODOO3_MAX_PIXCLOCK;
  1216. break;
  1217. case PCI_DEVICE_ID_3DFX_VOODOO5:
  1218. strcpy(info->fix.id, "3Dfx Voodoo5");
  1219. default_par->max_pixclock = VOODOO5_MAX_PIXCLOCK;
  1220. break;
  1221. }
  1222. info->fix.mmio_start = pci_resource_start(pdev, 0);
  1223. info->fix.mmio_len = pci_resource_len(pdev, 0);
  1224. if (!request_mem_region(info->fix.mmio_start, info->fix.mmio_len,
  1225. "tdfx regbase")) {
  1226. printk(KERN_ERR "tdfxfb: Can't reserve regbase\n");
  1227. goto out_err;
  1228. }
  1229. default_par->regbase_virt =
  1230. ioremap_nocache(info->fix.mmio_start, info->fix.mmio_len);
  1231. if (!default_par->regbase_virt) {
  1232. printk(KERN_ERR "fb: Can't remap %s register area.\n",
  1233. info->fix.id);
  1234. goto out_err_regbase;
  1235. }
  1236. info->fix.smem_start = pci_resource_start(pdev, 1);
  1237. info->fix.smem_len = do_lfb_size(default_par, pdev->device);
  1238. if (!info->fix.smem_len) {
  1239. printk(KERN_ERR "fb: Can't count %s memory.\n", info->fix.id);
  1240. goto out_err_regbase;
  1241. }
  1242. if (!request_mem_region(info->fix.smem_start,
  1243. pci_resource_len(pdev, 1), "tdfx smem")) {
  1244. printk(KERN_ERR "tdfxfb: Can't reserve smem\n");
  1245. goto out_err_regbase;
  1246. }
  1247. info->screen_base = ioremap_nocache(info->fix.smem_start,
  1248. info->fix.smem_len);
  1249. if (!info->screen_base) {
  1250. printk(KERN_ERR "fb: Can't remap %s framebuffer.\n",
  1251. info->fix.id);
  1252. goto out_err_screenbase;
  1253. }
  1254. default_par->iobase = pci_resource_start(pdev, 2);
  1255. if (!request_region(pci_resource_start(pdev, 2),
  1256. pci_resource_len(pdev, 2), "tdfx iobase")) {
  1257. printk(KERN_ERR "tdfxfb: Can't reserve iobase\n");
  1258. goto out_err_screenbase;
  1259. }
  1260. printk(KERN_INFO "fb: %s memory = %dK\n", info->fix.id,
  1261. info->fix.smem_len >> 10);
  1262. default_par->mtrr_handle = -1;
  1263. if (!nomtrr)
  1264. default_par->mtrr_handle =
  1265. mtrr_add(info->fix.smem_start, info->fix.smem_len,
  1266. MTRR_TYPE_WRCOMB, 1);
  1267. info->fix.ypanstep = nopan ? 0 : 1;
  1268. info->fix.ywrapstep = nowrap ? 0 : 1;
  1269. info->fbops = &tdfxfb_ops;
  1270. info->pseudo_palette = default_par->palette;
  1271. info->flags = FBINFO_DEFAULT | FBINFO_HWACCEL_YPAN;
  1272. #ifdef CONFIG_FB_3DFX_ACCEL
  1273. info->flags |= FBINFO_HWACCEL_FILLRECT |
  1274. FBINFO_HWACCEL_COPYAREA |
  1275. FBINFO_HWACCEL_IMAGEBLIT |
  1276. FBINFO_READS_FAST;
  1277. #endif
  1278. /* reserve 8192 bits for cursor */
  1279. /* the 2.4 driver says PAGE_MASK boundary is not enough for Voodoo4 */
  1280. if (hwcursor)
  1281. info->fix.smem_len = (info->fix.smem_len - 1024) &
  1282. (PAGE_MASK << 1);
  1283. #ifdef CONFIG_FB_3DFX_I2C
  1284. tdfxfb_create_i2c_busses(info);
  1285. #endif
  1286. if (!mode_option)
  1287. mode_option = "640x480@60";
  1288. err = fb_find_mode(&info->var, info, mode_option, NULL, 0, NULL, 8);
  1289. if (!err || err == 4)
  1290. info->var = tdfx_var;
  1291. /* maximize virtual vertical length */
  1292. lpitch = info->var.xres_virtual * ((info->var.bits_per_pixel + 7) >> 3);
  1293. info->var.yres_virtual = info->fix.smem_len / lpitch;
  1294. if (info->var.yres_virtual < info->var.yres)
  1295. goto out_err_iobase;
  1296. if (fb_alloc_cmap(&info->cmap, 256, 0) < 0) {
  1297. printk(KERN_ERR "tdfxfb: Can't allocate color map\n");
  1298. goto out_err_iobase;
  1299. }
  1300. if (register_framebuffer(info) < 0) {
  1301. printk(KERN_ERR "tdfxfb: can't register framebuffer\n");
  1302. fb_dealloc_cmap(&info->cmap);
  1303. goto out_err_iobase;
  1304. }
  1305. /*
  1306. * Our driver data
  1307. */
  1308. pci_set_drvdata(pdev, info);
  1309. return 0;
  1310. out_err_iobase:
  1311. #ifdef CONFIG_FB_3DFX_I2C
  1312. tdfxfb_delete_i2c_busses(default_par);
  1313. #endif
  1314. if (default_par->mtrr_handle >= 0)
  1315. mtrr_del(default_par->mtrr_handle, info->fix.smem_start,
  1316. info->fix.smem_len);
  1317. release_mem_region(pci_resource_start(pdev, 2),
  1318. pci_resource_len(pdev, 2));
  1319. out_err_screenbase:
  1320. if (info->screen_base)
  1321. iounmap(info->screen_base);
  1322. release_mem_region(info->fix.smem_start, pci_resource_len(pdev, 1));
  1323. out_err_regbase:
  1324. /*
  1325. * Cleanup after anything that was remapped/allocated.
  1326. */
  1327. if (default_par->regbase_virt)
  1328. iounmap(default_par->regbase_virt);
  1329. release_mem_region(info->fix.mmio_start, info->fix.mmio_len);
  1330. out_err:
  1331. framebuffer_release(info);
  1332. return -ENXIO;
  1333. }
  1334. #ifndef MODULE
  1335. static void __init tdfxfb_setup(char *options)
  1336. {
  1337. char *this_opt;
  1338. if (!options || !*options)
  1339. return;
  1340. while ((this_opt = strsep(&options, ",")) != NULL) {
  1341. if (!*this_opt)
  1342. continue;
  1343. if (!strcmp(this_opt, "nopan")) {
  1344. nopan = 1;
  1345. } else if (!strcmp(this_opt, "nowrap")) {
  1346. nowrap = 1;
  1347. } else if (!strncmp(this_opt, "hwcursor=", 9)) {
  1348. hwcursor = simple_strtoul(this_opt + 9, NULL, 0);
  1349. #ifdef CONFIG_MTRR
  1350. } else if (!strncmp(this_opt, "nomtrr", 6)) {
  1351. nomtrr = 1;
  1352. #endif
  1353. } else {
  1354. mode_option = this_opt;
  1355. }
  1356. }
  1357. }
  1358. #endif
  1359. /**
  1360. * tdfxfb_remove - Device removal
  1361. *
  1362. * @pdev: PCI Device to cleanup
  1363. *
  1364. * Releases all resources allocated during the course of the driver's
  1365. * lifetime for the PCI device @pdev.
  1366. *
  1367. */
  1368. static void __devexit tdfxfb_remove(struct pci_dev *pdev)
  1369. {
  1370. struct fb_info *info = pci_get_drvdata(pdev);
  1371. struct tdfx_par *par = info->par;
  1372. unregister_framebuffer(info);
  1373. #ifdef CONFIG_FB_3DFX_I2C
  1374. tdfxfb_delete_i2c_busses(par);
  1375. #endif
  1376. if (par->mtrr_handle >= 0)
  1377. mtrr_del(par->mtrr_handle, info->fix.smem_start,
  1378. info->fix.smem_len);
  1379. iounmap(par->regbase_virt);
  1380. iounmap(info->screen_base);
  1381. /* Clean up after reserved regions */
  1382. release_region(pci_resource_start(pdev, 2),
  1383. pci_resource_len(pdev, 2));
  1384. release_mem_region(pci_resource_start(pdev, 1),
  1385. pci_resource_len(pdev, 1));
  1386. release_mem_region(pci_resource_start(pdev, 0),
  1387. pci_resource_len(pdev, 0));
  1388. pci_set_drvdata(pdev, NULL);
  1389. fb_dealloc_cmap(&info->cmap);
  1390. framebuffer_release(info);
  1391. }
  1392. static int __init tdfxfb_init(void)
  1393. {
  1394. #ifndef MODULE
  1395. char *option = NULL;
  1396. if (fb_get_options("tdfxfb", &option))
  1397. return -ENODEV;
  1398. tdfxfb_setup(option);
  1399. #endif
  1400. return pci_register_driver(&tdfxfb_driver);
  1401. }
  1402. static void __exit tdfxfb_exit(void)
  1403. {
  1404. pci_unregister_driver(&tdfxfb_driver);
  1405. }
  1406. MODULE_AUTHOR("Hannu Mallat <hmallat@cc.hut.fi>");
  1407. MODULE_DESCRIPTION("3Dfx framebuffer device driver");
  1408. MODULE_LICENSE("GPL");
  1409. module_param(hwcursor, int, 0644);
  1410. MODULE_PARM_DESC(hwcursor, "Enable hardware cursor "
  1411. "(1=enable, 0=disable, default=1)");
  1412. module_param(mode_option, charp, 0);
  1413. MODULE_PARM_DESC(mode_option, "Initial video mode e.g. '648x480-8@60'");
  1414. #ifdef CONFIG_MTRR
  1415. module_param(nomtrr, bool, 0);
  1416. MODULE_PARM_DESC(nomtrr, "Disable MTRR support (default: enabled)");
  1417. #endif
  1418. module_init(tdfxfb_init);
  1419. module_exit(tdfxfb_exit);