tdfxfb.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417
  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. * Lots of the information here comes from the Daryll Strauss' Banshee
  14. * patches to the XF86 server, and the rest comes from the 3dfx
  15. * Banshee specification. I'm very much indebted to Daryll for his
  16. * work on the X server.
  17. *
  18. * Voodoo3 support was contributed Harold Oga. Lots of additions
  19. * (proper acceleration, 24 bpp, hardware cursor) and bug fixes by Attila
  20. * Kesmarki. Thanks guys!
  21. *
  22. * Voodoo1 and Voodoo2 support aren't relevant to this driver as they
  23. * behave very differently from the Voodoo3/4/5. For anyone wanting to
  24. * use frame buffer on the Voodoo1/2, see the sstfb driver (which is
  25. * located at http://www.sourceforge.net/projects/sstfb).
  26. *
  27. * While I _am_ grateful to 3Dfx for releasing the specs for Banshee,
  28. * I do wish the next version is a bit more complete. Without the XF86
  29. * patches I couldn't have gotten even this far... for instance, the
  30. * extensions to the VGA register set go completely unmentioned in the
  31. * spec! Also, lots of references are made to the 'SST core', but no
  32. * spec is publicly available, AFAIK.
  33. *
  34. * The structure of this driver comes pretty much from the Permedia
  35. * driver by Ilario Nardinocchi, which in turn is based on skeletonfb.
  36. *
  37. * TODO:
  38. * - multihead support (basically need to support an array of fb_infos)
  39. * - support other architectures (PPC, Alpha); does the fact that the VGA
  40. * core can be accessed only thru I/O (not memory mapped) complicate
  41. * things?
  42. *
  43. * Version history:
  44. *
  45. * 0.1.4 (released 2002-05-28) ported over to new fbdev api by James Simmons
  46. *
  47. * 0.1.3 (released 1999-11-02) added Attila's panning support, code
  48. * reorg, hwcursor address page size alignment
  49. * (for mmaping both frame buffer and regs),
  50. * and my changes to get rid of hardcoded
  51. * VGA i/o register locations (uses PCI
  52. * configuration info now)
  53. * 0.1.2 (released 1999-10-19) added Attila Kesmarki's bug fixes and
  54. * improvements
  55. * 0.1.1 (released 1999-10-07) added Voodoo3 support by Harold Oga.
  56. * 0.1.0 (released 1999-10-06) initial version
  57. *
  58. */
  59. #include <linux/module.h>
  60. #include <linux/kernel.h>
  61. #include <linux/errno.h>
  62. #include <linux/string.h>
  63. #include <linux/mm.h>
  64. #include <linux/slab.h>
  65. #include <linux/fb.h>
  66. #include <linux/init.h>
  67. #include <linux/pci.h>
  68. #include <asm/io.h>
  69. #include <video/tdfx.h>
  70. #undef TDFXFB_DEBUG
  71. #ifdef TDFXFB_DEBUG
  72. #define DPRINTK(a,b...) printk(KERN_DEBUG "fb: %s: " a, __FUNCTION__ , ## b)
  73. #else
  74. #define DPRINTK(a,b...)
  75. #endif
  76. #ifdef CONFIG_MTRR
  77. #include <asm/mtrr.h>
  78. #else
  79. /* duplicate asm/mtrr.h defines to work on archs without mtrr */
  80. #define MTRR_TYPE_WRCOMB 1
  81. static inline int mtrr_add(unsigned long base, unsigned long size,
  82. unsigned int type, char increment)
  83. {
  84. return -ENODEV;
  85. }
  86. static inline int mtrr_del(int reg, unsigned long base,
  87. unsigned long size)
  88. {
  89. return -ENODEV;
  90. }
  91. #endif
  92. #define BANSHEE_MAX_PIXCLOCK 270000
  93. #define VOODOO3_MAX_PIXCLOCK 300000
  94. #define VOODOO5_MAX_PIXCLOCK 350000
  95. static struct fb_fix_screeninfo tdfx_fix __devinitdata = {
  96. .id = "3Dfx",
  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. /* Banshee doesn't support interlace, but Voodoo4/5 and probably Voodoo3 do. */
  409. /* no direct information about device id now? use max_pixclock for this... */
  410. if (((var->vmode & FB_VMODE_MASK) == FB_VMODE_INTERLACED) &&
  411. (par->max_pixclock < VOODOO3_MAX_PIXCLOCK)) {
  412. DPRINTK("interlace not supported\n");
  413. return -EINVAL;
  414. }
  415. var->xres = (var->xres + 15) & ~15; /* could sometimes be 8 */
  416. lpitch = var->xres * ((var->bits_per_pixel + 7) >> 3);
  417. if (var->xres < 320 || var->xres > 2048) {
  418. DPRINTK("width not supported: %u\n", var->xres);
  419. return -EINVAL;
  420. }
  421. if (var->yres < 200 || var->yres > 2048) {
  422. DPRINTK("height not supported: %u\n", var->yres);
  423. return -EINVAL;
  424. }
  425. if (lpitch * var->yres_virtual > info->fix.smem_len) {
  426. var->yres_virtual = info->fix.smem_len / lpitch;
  427. if (var->yres_virtual < var->yres) {
  428. DPRINTK("no memory for screen (%ux%ux%u)\n",
  429. var->xres, var->yres_virtual,
  430. var->bits_per_pixel);
  431. return -EINVAL;
  432. }
  433. }
  434. if (PICOS2KHZ(var->pixclock) > par->max_pixclock) {
  435. DPRINTK("pixclock too high (%ldKHz)\n",
  436. PICOS2KHZ(var->pixclock));
  437. return -EINVAL;
  438. }
  439. var->transp.offset = 0;
  440. var->transp.length = 0;
  441. switch (var->bits_per_pixel) {
  442. case 8:
  443. var->red.length = var->green.length = var->blue.length = 8;
  444. break;
  445. case 16:
  446. var->red.offset = 11;
  447. var->red.length = 5;
  448. var->green.offset = 5;
  449. var->green.length = 6;
  450. var->blue.offset = 0;
  451. var->blue.length = 5;
  452. break;
  453. case 32:
  454. var->transp.offset = 24;
  455. var->transp.length = 8;
  456. case 24:
  457. var->red.offset = 16;
  458. var->green.offset = 8;
  459. var->blue.offset = 0;
  460. var->red.length = var->green.length = var->blue.length = 8;
  461. break;
  462. }
  463. var->height = var->width = -1;
  464. var->accel_flags = FB_ACCELF_TEXT;
  465. DPRINTK("Checking graphics mode at %dx%d depth %d\n",
  466. var->xres, var->yres, var->bits_per_pixel);
  467. return 0;
  468. }
  469. static int tdfxfb_set_par(struct fb_info *info)
  470. {
  471. struct tdfx_par *par = info->par;
  472. u32 hdispend = info->var.xres;
  473. u32 hsyncsta = hdispend + info->var.right_margin;
  474. u32 hsyncend = hsyncsta + info->var.hsync_len;
  475. u32 htotal = hsyncend + info->var.left_margin;
  476. u32 hd, hs, he, ht, hbs, hbe;
  477. u32 vd, vs, ve, vt, vbs, vbe;
  478. struct banshee_reg reg;
  479. int fout, freq;
  480. u32 wd;
  481. u32 cpp = (info->var.bits_per_pixel + 7) >> 3;
  482. memset(&reg, 0, sizeof(reg));
  483. reg.vidcfg = VIDCFG_VIDPROC_ENABLE | VIDCFG_DESK_ENABLE |
  484. VIDCFG_CURS_X11 |
  485. ((cpp - 1) << VIDCFG_PIXFMT_SHIFT) |
  486. (cpp != 1 ? VIDCFG_CLUT_BYPASS : 0);
  487. /* PLL settings */
  488. freq = PICOS2KHZ(info->var.pixclock);
  489. reg.vidcfg &= ~VIDCFG_2X;
  490. if (freq > par->max_pixclock / 2) {
  491. freq = freq > par->max_pixclock ? par->max_pixclock : freq;
  492. reg.dacmode |= DACMODE_2X;
  493. reg.vidcfg |= VIDCFG_2X;
  494. hdispend >>= 1;
  495. hsyncsta >>= 1;
  496. hsyncend >>= 1;
  497. htotal >>= 1;
  498. }
  499. hd = wd = (hdispend >> 3) - 1;
  500. hs = (hsyncsta >> 3) - 1;
  501. he = (hsyncend >> 3) - 1;
  502. ht = (htotal >> 3) - 1;
  503. hbs = hd;
  504. hbe = ht;
  505. if ((info->var.vmode & FB_VMODE_MASK) == FB_VMODE_DOUBLE) {
  506. vbs = vd = (info->var.yres << 1) - 1;
  507. vs = vd + (info->var.lower_margin << 1);
  508. ve = vs + (info->var.vsync_len << 1);
  509. vbe = vt = ve + (info->var.upper_margin << 1) - 1;
  510. reg.screensize = info->var.xres | (info->var.yres << 13);
  511. reg.vidcfg |= VIDCFG_HALF_MODE;
  512. reg.crt[0x09] = 0x80;
  513. } else {
  514. vbs = vd = info->var.yres - 1;
  515. vs = vd + info->var.lower_margin;
  516. ve = vs + info->var.vsync_len;
  517. vbe = vt = ve + info->var.upper_margin - 1;
  518. reg.screensize = info->var.xres | (info->var.yres << 12);
  519. reg.vidcfg &= ~VIDCFG_HALF_MODE;
  520. }
  521. /* this is all pretty standard VGA register stuffing */
  522. reg.misc[0x00] = 0x0f |
  523. (info->var.xres < 400 ? 0xa0 :
  524. info->var.xres < 480 ? 0x60 :
  525. info->var.xres < 768 ? 0xe0 : 0x20);
  526. reg.gra[0x05] = 0x40;
  527. reg.gra[0x06] = 0x05;
  528. reg.gra[0x07] = 0x0f;
  529. reg.gra[0x08] = 0xff;
  530. reg.att[0x00] = 0x00;
  531. reg.att[0x01] = 0x01;
  532. reg.att[0x02] = 0x02;
  533. reg.att[0x03] = 0x03;
  534. reg.att[0x04] = 0x04;
  535. reg.att[0x05] = 0x05;
  536. reg.att[0x06] = 0x06;
  537. reg.att[0x07] = 0x07;
  538. reg.att[0x08] = 0x08;
  539. reg.att[0x09] = 0x09;
  540. reg.att[0x0a] = 0x0a;
  541. reg.att[0x0b] = 0x0b;
  542. reg.att[0x0c] = 0x0c;
  543. reg.att[0x0d] = 0x0d;
  544. reg.att[0x0e] = 0x0e;
  545. reg.att[0x0f] = 0x0f;
  546. reg.att[0x10] = 0x41;
  547. reg.att[0x12] = 0x0f;
  548. reg.seq[0x00] = 0x03;
  549. reg.seq[0x01] = 0x01; /* fixme: clkdiv2? */
  550. reg.seq[0x02] = 0x0f;
  551. reg.seq[0x03] = 0x00;
  552. reg.seq[0x04] = 0x0e;
  553. reg.crt[0x00] = ht - 4;
  554. reg.crt[0x01] = hd;
  555. reg.crt[0x02] = hbs;
  556. reg.crt[0x03] = 0x80 | (hbe & 0x1f);
  557. reg.crt[0x04] = hs;
  558. reg.crt[0x05] = ((hbe & 0x20) << 2) | (he & 0x1f);
  559. reg.crt[0x06] = vt;
  560. reg.crt[0x07] = ((vs & 0x200) >> 2) |
  561. ((vd & 0x200) >> 3) |
  562. ((vt & 0x200) >> 4) | 0x10 |
  563. ((vbs & 0x100) >> 5) |
  564. ((vs & 0x100) >> 6) |
  565. ((vd & 0x100) >> 7) |
  566. ((vt & 0x100) >> 8);
  567. reg.crt[0x09] |= 0x40 | ((vbs & 0x200) >> 4);
  568. reg.crt[0x10] = vs;
  569. reg.crt[0x11] = (ve & 0x0f) | 0x20;
  570. reg.crt[0x12] = vd;
  571. reg.crt[0x13] = wd;
  572. reg.crt[0x15] = vbs;
  573. reg.crt[0x16] = vbe + 1;
  574. reg.crt[0x17] = 0xc3;
  575. reg.crt[0x18] = 0xff;
  576. /* Banshee's nonvga stuff */
  577. reg.ext[0x00] = (((ht & 0x100) >> 8) |
  578. ((hd & 0x100) >> 6) |
  579. ((hbs & 0x100) >> 4) |
  580. ((hbe & 0x40) >> 1) |
  581. ((hs & 0x100) >> 2) |
  582. ((he & 0x20) << 2));
  583. reg.ext[0x01] = (((vt & 0x400) >> 10) |
  584. ((vd & 0x400) >> 8) |
  585. ((vbs & 0x400) >> 6) |
  586. ((vbe & 0x400) >> 4));
  587. reg.vgainit0 = VGAINIT0_8BIT_DAC |
  588. VGAINIT0_EXT_ENABLE |
  589. VGAINIT0_WAKEUP_3C3 |
  590. VGAINIT0_ALT_READBACK |
  591. VGAINIT0_EXTSHIFTOUT;
  592. reg.vgainit1 = tdfx_inl(par, VGAINIT1) & 0x1fffff;
  593. if (hwcursor)
  594. reg.curspataddr = info->fix.smem_len;
  595. reg.cursloc = 0;
  596. reg.cursc0 = 0;
  597. reg.cursc1 = 0xffffff;
  598. reg.stride = info->var.xres * cpp;
  599. reg.startaddr = info->var.yoffset * reg.stride
  600. + info->var.xoffset * cpp;
  601. reg.vidpll = do_calc_pll(freq, &fout);
  602. #if 0
  603. reg.mempll = do_calc_pll(..., &fout);
  604. reg.gfxpll = do_calc_pll(..., &fout);
  605. #endif
  606. if ((info->var.vmode & FB_VMODE_MASK) == FB_VMODE_INTERLACED)
  607. reg.vidcfg |= VIDCFG_INTERLACE;
  608. reg.miscinit0 = tdfx_inl(par, MISCINIT0);
  609. #if defined(__BIG_ENDIAN)
  610. switch (info->var.bits_per_pixel) {
  611. case 8:
  612. case 24:
  613. reg.miscinit0 &= ~(1 << 30);
  614. reg.miscinit0 &= ~(1 << 31);
  615. break;
  616. case 16:
  617. reg.miscinit0 |= (1 << 30);
  618. reg.miscinit0 |= (1 << 31);
  619. break;
  620. case 32:
  621. reg.miscinit0 |= (1 << 30);
  622. reg.miscinit0 &= ~(1 << 31);
  623. break;
  624. }
  625. #endif
  626. do_write_regs(info, &reg);
  627. /* Now change fb_fix_screeninfo according to changes in par */
  628. info->fix.line_length = reg.stride;
  629. info->fix.visual = (info->var.bits_per_pixel == 8)
  630. ? FB_VISUAL_PSEUDOCOLOR
  631. : FB_VISUAL_TRUECOLOR;
  632. DPRINTK("Graphics mode is now set at %dx%d depth %d\n",
  633. info->var.xres, info->var.yres, info->var.bits_per_pixel);
  634. return 0;
  635. }
  636. /* A handy macro shamelessly pinched from matroxfb */
  637. #define CNVT_TOHW(val, width) ((((val)<<(width))+0x7FFF-(val))>>16)
  638. static int tdfxfb_setcolreg(unsigned regno, unsigned red, unsigned green,
  639. unsigned blue, unsigned transp,
  640. struct fb_info *info)
  641. {
  642. struct tdfx_par *par = info->par;
  643. u32 rgbcol;
  644. if (regno >= info->cmap.len || regno > 255)
  645. return 1;
  646. /* grayscale works only partially under directcolor */
  647. if (info->var.grayscale) {
  648. /* grayscale = 0.30*R + 0.59*G + 0.11*B */
  649. red = green = blue = (red * 77 + green * 151 + blue * 28) >> 8;
  650. }
  651. switch (info->fix.visual) {
  652. case FB_VISUAL_PSEUDOCOLOR:
  653. rgbcol =(((u32)red & 0xff00) << 8) |
  654. (((u32)green & 0xff00) << 0) |
  655. (((u32)blue & 0xff00) >> 8);
  656. do_setpalentry(par, regno, rgbcol);
  657. break;
  658. /* Truecolor has no hardware color palettes. */
  659. case FB_VISUAL_TRUECOLOR:
  660. if (regno < 16) {
  661. rgbcol = (CNVT_TOHW(red, info->var.red.length) <<
  662. info->var.red.offset) |
  663. (CNVT_TOHW(green, info->var.green.length) <<
  664. info->var.green.offset) |
  665. (CNVT_TOHW(blue, info->var.blue.length) <<
  666. info->var.blue.offset) |
  667. (CNVT_TOHW(transp, info->var.transp.length) <<
  668. info->var.transp.offset);
  669. par->palette[regno] = rgbcol;
  670. }
  671. break;
  672. default:
  673. DPRINTK("bad depth %u\n", info->var.bits_per_pixel);
  674. break;
  675. }
  676. return 0;
  677. }
  678. /* 0 unblank, 1 blank, 2 no vsync, 3 no hsync, 4 off */
  679. static int tdfxfb_blank(int blank, struct fb_info *info)
  680. {
  681. struct tdfx_par *par = info->par;
  682. int vgablank = 1;
  683. u32 dacmode = tdfx_inl(par, DACMODE);
  684. dacmode &= ~(BIT(1) | BIT(3));
  685. switch (blank) {
  686. case FB_BLANK_UNBLANK: /* Screen: On; HSync: On, VSync: On */
  687. vgablank = 0;
  688. break;
  689. case FB_BLANK_NORMAL: /* Screen: Off; HSync: On, VSync: On */
  690. break;
  691. case FB_BLANK_VSYNC_SUSPEND: /* Screen: Off; HSync: On, VSync: Off */
  692. dacmode |= BIT(3);
  693. break;
  694. case FB_BLANK_HSYNC_SUSPEND: /* Screen: Off; HSync: Off, VSync: On */
  695. dacmode |= BIT(1);
  696. break;
  697. case FB_BLANK_POWERDOWN: /* Screen: Off; HSync: Off, VSync: Off */
  698. dacmode |= BIT(1) | BIT(3);
  699. break;
  700. }
  701. banshee_make_room(par, 1);
  702. tdfx_outl(par, DACMODE, dacmode);
  703. if (vgablank)
  704. vga_disable_video(par);
  705. else
  706. vga_enable_video(par);
  707. return 0;
  708. }
  709. /*
  710. * Set the starting position of the visible screen to var->yoffset
  711. */
  712. static int tdfxfb_pan_display(struct fb_var_screeninfo *var,
  713. struct fb_info *info)
  714. {
  715. struct tdfx_par *par = info->par;
  716. u32 addr = var->yoffset * info->fix.line_length;
  717. if (nopan || var->xoffset || (var->yoffset > var->yres_virtual))
  718. return -EINVAL;
  719. if ((var->yoffset + var->yres > var->yres_virtual && nowrap))
  720. return -EINVAL;
  721. banshee_make_room(par, 1);
  722. tdfx_outl(par, VIDDESKSTART, addr);
  723. info->var.xoffset = var->xoffset;
  724. info->var.yoffset = var->yoffset;
  725. return 0;
  726. }
  727. #ifdef CONFIG_FB_3DFX_ACCEL
  728. /*
  729. * FillRect 2D command (solidfill or invert (via ROP_XOR))
  730. */
  731. static void tdfxfb_fillrect(struct fb_info *info,
  732. const struct fb_fillrect *rect)
  733. {
  734. struct tdfx_par *par = info->par;
  735. u32 bpp = info->var.bits_per_pixel;
  736. u32 stride = info->fix.line_length;
  737. u32 fmt= stride | ((bpp + ((bpp == 8) ? 0 : 8)) << 13);
  738. int tdfx_rop;
  739. u32 dx = rect->dx;
  740. u32 dy = rect->dy;
  741. u32 dstbase = 0;
  742. if (rect->rop == ROP_COPY)
  743. tdfx_rop = TDFX_ROP_COPY;
  744. else
  745. tdfx_rop = TDFX_ROP_XOR;
  746. /* asume always rect->height < 4096 */
  747. if (dy + rect->height > 4095) {
  748. dstbase = stride * dy;
  749. dy = 0;
  750. }
  751. /* asume always rect->width < 4096 */
  752. if (dx + rect->width > 4095) {
  753. dstbase += dx * bpp >> 3;
  754. dx = 0;
  755. }
  756. banshee_make_room(par, 6);
  757. tdfx_outl(par, DSTFORMAT, fmt);
  758. if (info->fix.visual == FB_VISUAL_PSEUDOCOLOR) {
  759. tdfx_outl(par, COLORFORE, rect->color);
  760. } else { /* FB_VISUAL_TRUECOLOR */
  761. tdfx_outl(par, COLORFORE, par->palette[rect->color]);
  762. }
  763. tdfx_outl(par, COMMAND_2D, COMMAND_2D_FILLRECT | (tdfx_rop << 24));
  764. tdfx_outl(par, DSTBASE, dstbase);
  765. tdfx_outl(par, DSTSIZE, rect->width | (rect->height << 16));
  766. tdfx_outl(par, LAUNCH_2D, dx | (dy << 16));
  767. }
  768. /*
  769. * Screen-to-Screen BitBlt 2D command (for the bmove fb op.)
  770. */
  771. static void tdfxfb_copyarea(struct fb_info *info,
  772. const struct fb_copyarea *area)
  773. {
  774. struct tdfx_par *par = info->par;
  775. u32 sx = area->sx, sy = area->sy, dx = area->dx, dy = area->dy;
  776. u32 bpp = info->var.bits_per_pixel;
  777. u32 stride = info->fix.line_length;
  778. u32 blitcmd = COMMAND_2D_S2S_BITBLT | (TDFX_ROP_COPY << 24);
  779. u32 fmt = stride | ((bpp + ((bpp == 8) ? 0 : 8)) << 13);
  780. u32 dstbase = 0;
  781. u32 srcbase = 0;
  782. /* asume always area->height < 4096 */
  783. if (sy + area->height > 4095) {
  784. srcbase = stride * sy;
  785. sy = 0;
  786. }
  787. /* asume always area->width < 4096 */
  788. if (sx + area->width > 4095) {
  789. srcbase += sx * bpp >> 3;
  790. sx = 0;
  791. }
  792. /* asume always area->height < 4096 */
  793. if (dy + area->height > 4095) {
  794. dstbase = stride * dy;
  795. dy = 0;
  796. }
  797. /* asume always area->width < 4096 */
  798. if (dx + area->width > 4095) {
  799. dstbase += dx * bpp >> 3;
  800. dx = 0;
  801. }
  802. if (area->sx <= area->dx) {
  803. //-X
  804. blitcmd |= BIT(14);
  805. sx += area->width - 1;
  806. dx += area->width - 1;
  807. }
  808. if (area->sy <= area->dy) {
  809. //-Y
  810. blitcmd |= BIT(15);
  811. sy += area->height - 1;
  812. dy += area->height - 1;
  813. }
  814. banshee_make_room(par, 8);
  815. tdfx_outl(par, SRCFORMAT, fmt);
  816. tdfx_outl(par, DSTFORMAT, fmt);
  817. tdfx_outl(par, COMMAND_2D, blitcmd);
  818. tdfx_outl(par, DSTSIZE, area->width | (area->height << 16));
  819. tdfx_outl(par, DSTXY, dx | (dy << 16));
  820. tdfx_outl(par, SRCBASE, srcbase);
  821. tdfx_outl(par, DSTBASE, dstbase);
  822. tdfx_outl(par, LAUNCH_2D, sx | (sy << 16));
  823. }
  824. static void tdfxfb_imageblit(struct fb_info *info, const struct fb_image *image)
  825. {
  826. struct tdfx_par *par = info->par;
  827. int size = image->height * ((image->width * image->depth + 7) >> 3);
  828. int fifo_free;
  829. int i, stride = info->fix.line_length;
  830. u32 bpp = info->var.bits_per_pixel;
  831. u32 dstfmt = stride | ((bpp + ((bpp == 8) ? 0 : 8)) << 13);
  832. u8 *chardata = (u8 *) image->data;
  833. u32 srcfmt;
  834. u32 dx = image->dx;
  835. u32 dy = image->dy;
  836. u32 dstbase = 0;
  837. if (image->depth != 1) {
  838. //banshee_make_room(par, 6 + ((size + 3) >> 2));
  839. //srcfmt = stride | ((bpp+((bpp==8) ? 0 : 8)) << 13) | 0x400000;
  840. cfb_imageblit(info, image);
  841. return;
  842. }
  843. banshee_make_room(par, 9);
  844. switch (info->fix.visual) {
  845. case FB_VISUAL_PSEUDOCOLOR:
  846. tdfx_outl(par, COLORFORE, image->fg_color);
  847. tdfx_outl(par, COLORBACK, image->bg_color);
  848. break;
  849. case FB_VISUAL_TRUECOLOR:
  850. default:
  851. tdfx_outl(par, COLORFORE,
  852. par->palette[image->fg_color]);
  853. tdfx_outl(par, COLORBACK,
  854. par->palette[image->bg_color]);
  855. }
  856. #ifdef __BIG_ENDIAN
  857. srcfmt = 0x400000 | BIT(20);
  858. #else
  859. srcfmt = 0x400000;
  860. #endif
  861. /* asume always image->height < 4096 */
  862. if (dy + image->height > 4095) {
  863. dstbase = stride * dy;
  864. dy = 0;
  865. }
  866. /* asume always image->width < 4096 */
  867. if (dx + image->width > 4095) {
  868. dstbase += dx * bpp >> 3;
  869. dx = 0;
  870. }
  871. tdfx_outl(par, DSTBASE, dstbase);
  872. tdfx_outl(par, SRCXY, 0);
  873. tdfx_outl(par, DSTXY, dx | (dy << 16));
  874. tdfx_outl(par, COMMAND_2D, COMMAND_2D_H2S_BITBLT | (TDFX_ROP_COPY << 24));
  875. tdfx_outl(par, SRCFORMAT, srcfmt);
  876. tdfx_outl(par, DSTFORMAT, dstfmt);
  877. tdfx_outl(par, DSTSIZE, image->width | (image->height << 16));
  878. /* A count of how many free FIFO entries we've requested.
  879. * When this goes negative, we need to request more. */
  880. fifo_free = 0;
  881. /* Send four bytes at a time of data */
  882. for (i = (size >> 2); i > 0; i--) {
  883. if (--fifo_free < 0) {
  884. fifo_free = 31;
  885. banshee_make_room(par, fifo_free);
  886. }
  887. tdfx_outl(par, LAUNCH_2D, *(u32*)chardata);
  888. chardata += 4;
  889. }
  890. /* Send the leftovers now */
  891. banshee_make_room(par, 3);
  892. switch (size % 4) {
  893. case 0:
  894. break;
  895. case 1:
  896. tdfx_outl(par, LAUNCH_2D, *chardata);
  897. break;
  898. case 2:
  899. tdfx_outl(par, LAUNCH_2D, *(u16*)chardata);
  900. break;
  901. case 3:
  902. tdfx_outl(par, LAUNCH_2D,
  903. *(u16*)chardata | ((chardata[3]) << 24));
  904. break;
  905. }
  906. }
  907. #endif /* CONFIG_FB_3DFX_ACCEL */
  908. static int tdfxfb_cursor(struct fb_info *info, struct fb_cursor *cursor)
  909. {
  910. struct tdfx_par *par = info->par;
  911. u32 vidcfg;
  912. if (!hwcursor)
  913. return -EINVAL; /* just to force soft_cursor() call */
  914. /* Too large of a cursor or wrong bpp :-( */
  915. if (cursor->image.width > 64 ||
  916. cursor->image.height > 64 ||
  917. cursor->image.depth > 1)
  918. return -EINVAL;
  919. vidcfg = tdfx_inl(par, VIDPROCCFG);
  920. if (cursor->enable)
  921. tdfx_outl(par, VIDPROCCFG, vidcfg | VIDCFG_HWCURSOR_ENABLE);
  922. else
  923. tdfx_outl(par, VIDPROCCFG, vidcfg & ~VIDCFG_HWCURSOR_ENABLE);
  924. /*
  925. * If the cursor is not be changed this means either we want the
  926. * current cursor state (if enable is set) or we want to query what
  927. * we can do with the cursor (if enable is not set)
  928. */
  929. if (!cursor->set)
  930. return 0;
  931. /* fix cursor color - XFree86 forgets to restore it properly */
  932. if (cursor->set & FB_CUR_SETCMAP) {
  933. struct fb_cmap cmap = info->cmap;
  934. u32 bg_idx = cursor->image.bg_color;
  935. u32 fg_idx = cursor->image.fg_color;
  936. unsigned long bg_color, fg_color;
  937. fg_color = (((u32)cmap.red[fg_idx] & 0xff00) << 8) |
  938. (((u32)cmap.green[fg_idx] & 0xff00) << 0) |
  939. (((u32)cmap.blue[fg_idx] & 0xff00) >> 8);
  940. bg_color = (((u32)cmap.red[bg_idx] & 0xff00) << 8) |
  941. (((u32)cmap.green[bg_idx] & 0xff00) << 0) |
  942. (((u32)cmap.blue[bg_idx] & 0xff00) >> 8);
  943. banshee_make_room(par, 2);
  944. tdfx_outl(par, HWCURC0, bg_color);
  945. tdfx_outl(par, HWCURC1, fg_color);
  946. }
  947. if (cursor->set & FB_CUR_SETPOS) {
  948. int x = cursor->image.dx;
  949. int y = cursor->image.dy - info->var.yoffset;
  950. x += 63;
  951. y += 63;
  952. banshee_make_room(par, 1);
  953. tdfx_outl(par, HWCURLOC, (y << 16) + x);
  954. }
  955. if (cursor->set & (FB_CUR_SETIMAGE | FB_CUR_SETSHAPE)) {
  956. /*
  957. * Voodoo 3 and above cards use 2 monochrome cursor patterns.
  958. * The reason is so the card can fetch 8 words at a time
  959. * and are stored on chip for use for the next 8 scanlines.
  960. * This reduces the number of times for access to draw the
  961. * cursor for each screen refresh.
  962. * Each pattern is a bitmap of 64 bit wide and 64 bit high
  963. * (total of 8192 bits or 1024 bytes). The two patterns are
  964. * stored in such a way that pattern 0 always resides in the
  965. * lower half (least significant 64 bits) of a 128 bit word
  966. * and pattern 1 the upper half. If you examine the data of
  967. * the cursor image the graphics card uses then from the
  968. * begining you see line one of pattern 0, line one of
  969. * pattern 1, line two of pattern 0, line two of pattern 1,
  970. * etc etc. The linear stride for the cursor is always 16 bytes
  971. * (128 bits) which is the maximum cursor width times two for
  972. * the two monochrome patterns.
  973. */
  974. u8 __iomem *cursorbase = info->screen_base + info->fix.smem_len;
  975. u8 *bitmap = (u8 *)cursor->image.data;
  976. u8 *mask = (u8 *)cursor->mask;
  977. int i;
  978. fb_memset(cursorbase, 0, 1024);
  979. for (i = 0; i < cursor->image.height; i++) {
  980. int h = 0;
  981. int j = (cursor->image.width + 7) >> 3;
  982. for (; j > 0; j--) {
  983. u8 data = *mask ^ *bitmap;
  984. if (cursor->rop == ROP_COPY)
  985. data = *mask & *bitmap;
  986. /* Pattern 0. Copy the cursor mask to it */
  987. fb_writeb(*mask, cursorbase + h);
  988. mask++;
  989. /* Pattern 1. Copy the cursor bitmap to it */
  990. fb_writeb(data, cursorbase + h + 8);
  991. bitmap++;
  992. h++;
  993. }
  994. cursorbase += 16;
  995. }
  996. }
  997. return 0;
  998. }
  999. static struct fb_ops tdfxfb_ops = {
  1000. .owner = THIS_MODULE,
  1001. .fb_check_var = tdfxfb_check_var,
  1002. .fb_set_par = tdfxfb_set_par,
  1003. .fb_setcolreg = tdfxfb_setcolreg,
  1004. .fb_blank = tdfxfb_blank,
  1005. .fb_pan_display = tdfxfb_pan_display,
  1006. .fb_sync = banshee_wait_idle,
  1007. .fb_cursor = tdfxfb_cursor,
  1008. #ifdef CONFIG_FB_3DFX_ACCEL
  1009. .fb_fillrect = tdfxfb_fillrect,
  1010. .fb_copyarea = tdfxfb_copyarea,
  1011. .fb_imageblit = tdfxfb_imageblit,
  1012. #else
  1013. .fb_fillrect = cfb_fillrect,
  1014. .fb_copyarea = cfb_copyarea,
  1015. .fb_imageblit = cfb_imageblit,
  1016. #endif
  1017. };
  1018. /**
  1019. * tdfxfb_probe - Device Initializiation
  1020. *
  1021. * @pdev: PCI Device to initialize
  1022. * @id: PCI Device ID
  1023. *
  1024. * Initializes and allocates resources for PCI device @pdev.
  1025. *
  1026. */
  1027. static int __devinit tdfxfb_probe(struct pci_dev *pdev,
  1028. const struct pci_device_id *id)
  1029. {
  1030. struct tdfx_par *default_par;
  1031. struct fb_info *info;
  1032. int err, lpitch;
  1033. if ((err = pci_enable_device(pdev))) {
  1034. printk(KERN_WARNING "tdfxfb: Can't enable pdev: %d\n", err);
  1035. return err;
  1036. }
  1037. info = framebuffer_alloc(sizeof(struct tdfx_par), &pdev->dev);
  1038. if (!info)
  1039. return -ENOMEM;
  1040. default_par = info->par;
  1041. /* Configure the default fb_fix_screeninfo first */
  1042. switch (pdev->device) {
  1043. case PCI_DEVICE_ID_3DFX_BANSHEE:
  1044. strcat(tdfx_fix.id, " Banshee");
  1045. default_par->max_pixclock = BANSHEE_MAX_PIXCLOCK;
  1046. break;
  1047. case PCI_DEVICE_ID_3DFX_VOODOO3:
  1048. strcat(tdfx_fix.id, " Voodoo3");
  1049. default_par->max_pixclock = VOODOO3_MAX_PIXCLOCK;
  1050. break;
  1051. case PCI_DEVICE_ID_3DFX_VOODOO5:
  1052. strcat(tdfx_fix.id, " Voodoo5");
  1053. default_par->max_pixclock = VOODOO5_MAX_PIXCLOCK;
  1054. break;
  1055. }
  1056. tdfx_fix.mmio_start = pci_resource_start(pdev, 0);
  1057. tdfx_fix.mmio_len = pci_resource_len(pdev, 0);
  1058. if (!request_mem_region(tdfx_fix.mmio_start, tdfx_fix.mmio_len,
  1059. "tdfx regbase")) {
  1060. printk(KERN_WARNING "tdfxfb: Can't reserve regbase\n");
  1061. goto out_err;
  1062. }
  1063. default_par->regbase_virt =
  1064. ioremap_nocache(tdfx_fix.mmio_start, tdfx_fix.mmio_len);
  1065. if (!default_par->regbase_virt) {
  1066. printk("fb: Can't remap %s register area.\n", tdfx_fix.id);
  1067. goto out_err_regbase;
  1068. }
  1069. tdfx_fix.smem_start = pci_resource_start(pdev, 1);
  1070. if (!(tdfx_fix.smem_len = do_lfb_size(default_par, pdev->device))) {
  1071. printk("fb: Can't count %s memory.\n", tdfx_fix.id);
  1072. goto out_err_regbase;
  1073. }
  1074. if (!request_mem_region(tdfx_fix.smem_start,
  1075. pci_resource_len(pdev, 1), "tdfx smem")) {
  1076. printk(KERN_WARNING "tdfxfb: Can't reserve smem\n");
  1077. goto out_err_regbase;
  1078. }
  1079. info->screen_base = ioremap_nocache(tdfx_fix.smem_start,
  1080. tdfx_fix.smem_len);
  1081. if (!info->screen_base) {
  1082. printk("fb: Can't remap %s framebuffer.\n", tdfx_fix.id);
  1083. goto out_err_screenbase;
  1084. }
  1085. default_par->iobase = pci_resource_start(pdev, 2);
  1086. if (!request_region(pci_resource_start(pdev, 2),
  1087. pci_resource_len(pdev, 2), "tdfx iobase")) {
  1088. printk(KERN_WARNING "tdfxfb: Can't reserve iobase\n");
  1089. goto out_err_screenbase;
  1090. }
  1091. printk("fb: %s memory = %dK\n", tdfx_fix.id, tdfx_fix.smem_len >> 10);
  1092. default_par->mtrr_handle = -1;
  1093. if (!nomtrr)
  1094. default_par->mtrr_handle =
  1095. mtrr_add(tdfx_fix.smem_start, tdfx_fix.smem_len,
  1096. MTRR_TYPE_WRCOMB, 1);
  1097. tdfx_fix.ypanstep = nopan ? 0 : 1;
  1098. tdfx_fix.ywrapstep = nowrap ? 0 : 1;
  1099. info->fbops = &tdfxfb_ops;
  1100. info->fix = tdfx_fix;
  1101. info->pseudo_palette = default_par->palette;
  1102. info->flags = FBINFO_DEFAULT | FBINFO_HWACCEL_YPAN;
  1103. #ifdef CONFIG_FB_3DFX_ACCEL
  1104. info->flags |= FBINFO_HWACCEL_FILLRECT |
  1105. FBINFO_HWACCEL_COPYAREA |
  1106. FBINFO_HWACCEL_IMAGEBLIT |
  1107. FBINFO_READS_FAST;
  1108. #endif
  1109. /* reserve 8192 bits for cursor */
  1110. /* the 2.4 driver says PAGE_MASK boundary is not enough for Voodoo4 */
  1111. if (hwcursor)
  1112. info->fix.smem_len = (info->fix.smem_len - 1024) &
  1113. (PAGE_MASK << 1);
  1114. if (!mode_option)
  1115. mode_option = "640x480@60";
  1116. err = fb_find_mode(&info->var, info, mode_option, NULL, 0, NULL, 8);
  1117. if (!err || err == 4)
  1118. info->var = tdfx_var;
  1119. /* maximize virtual vertical length */
  1120. lpitch = info->var.xres_virtual * ((info->var.bits_per_pixel + 7) >> 3);
  1121. info->var.yres_virtual = info->fix.smem_len / lpitch;
  1122. if (info->var.yres_virtual < info->var.yres)
  1123. goto out_err_iobase;
  1124. if (fb_alloc_cmap(&info->cmap, 256, 0) < 0) {
  1125. printk(KERN_WARNING "tdfxfb: Can't allocate color map\n");
  1126. goto out_err_iobase;
  1127. }
  1128. if (register_framebuffer(info) < 0) {
  1129. printk("tdfxfb: can't register framebuffer\n");
  1130. fb_dealloc_cmap(&info->cmap);
  1131. goto out_err_iobase;
  1132. }
  1133. /*
  1134. * Our driver data
  1135. */
  1136. pci_set_drvdata(pdev, info);
  1137. return 0;
  1138. out_err_iobase:
  1139. if (default_par->mtrr_handle >= 0)
  1140. mtrr_del(default_par->mtrr_handle, info->fix.smem_start,
  1141. info->fix.smem_len);
  1142. release_mem_region(pci_resource_start(pdev, 2),
  1143. pci_resource_len(pdev, 2));
  1144. out_err_screenbase:
  1145. if (info->screen_base)
  1146. iounmap(info->screen_base);
  1147. release_mem_region(tdfx_fix.smem_start, pci_resource_len(pdev, 1));
  1148. out_err_regbase:
  1149. /*
  1150. * Cleanup after anything that was remapped/allocated.
  1151. */
  1152. if (default_par->regbase_virt)
  1153. iounmap(default_par->regbase_virt);
  1154. release_mem_region(tdfx_fix.mmio_start, tdfx_fix.mmio_len);
  1155. out_err:
  1156. framebuffer_release(info);
  1157. return -ENXIO;
  1158. }
  1159. #ifndef MODULE
  1160. static void tdfxfb_setup(char *options)
  1161. {
  1162. char *this_opt;
  1163. if (!options || !*options)
  1164. return;
  1165. while ((this_opt = strsep(&options, ",")) != NULL) {
  1166. if (!*this_opt)
  1167. continue;
  1168. if (!strcmp(this_opt, "nopan")) {
  1169. nopan = 1;
  1170. } else if (!strcmp(this_opt, "nowrap")) {
  1171. nowrap = 1;
  1172. } else if (!strncmp(this_opt, "hwcursor=", 9)) {
  1173. hwcursor = simple_strtoul(this_opt + 9, NULL, 0);
  1174. #ifdef CONFIG_MTRR
  1175. } else if (!strncmp(this_opt, "nomtrr", 6)) {
  1176. nomtrr = 1;
  1177. #endif
  1178. } else {
  1179. mode_option = this_opt;
  1180. }
  1181. }
  1182. }
  1183. #endif
  1184. /**
  1185. * tdfxfb_remove - Device removal
  1186. *
  1187. * @pdev: PCI Device to cleanup
  1188. *
  1189. * Releases all resources allocated during the course of the driver's
  1190. * lifetime for the PCI device @pdev.
  1191. *
  1192. */
  1193. static void __devexit tdfxfb_remove(struct pci_dev *pdev)
  1194. {
  1195. struct fb_info *info = pci_get_drvdata(pdev);
  1196. struct tdfx_par *par = info->par;
  1197. unregister_framebuffer(info);
  1198. if (par->mtrr_handle >= 0)
  1199. mtrr_del(par->mtrr_handle, info->fix.smem_start,
  1200. info->fix.smem_len);
  1201. iounmap(par->regbase_virt);
  1202. iounmap(info->screen_base);
  1203. /* Clean up after reserved regions */
  1204. release_region(pci_resource_start(pdev, 2),
  1205. pci_resource_len(pdev, 2));
  1206. release_mem_region(pci_resource_start(pdev, 1),
  1207. pci_resource_len(pdev, 1));
  1208. release_mem_region(pci_resource_start(pdev, 0),
  1209. pci_resource_len(pdev, 0));
  1210. pci_set_drvdata(pdev, NULL);
  1211. framebuffer_release(info);
  1212. }
  1213. static int __init tdfxfb_init(void)
  1214. {
  1215. #ifndef MODULE
  1216. char *option = NULL;
  1217. if (fb_get_options("tdfxfb", &option))
  1218. return -ENODEV;
  1219. tdfxfb_setup(option);
  1220. #endif
  1221. return pci_register_driver(&tdfxfb_driver);
  1222. }
  1223. static void __exit tdfxfb_exit(void)
  1224. {
  1225. pci_unregister_driver(&tdfxfb_driver);
  1226. }
  1227. MODULE_AUTHOR("Hannu Mallat <hmallat@cc.hut.fi>");
  1228. MODULE_DESCRIPTION("3Dfx framebuffer device driver");
  1229. MODULE_LICENSE("GPL");
  1230. module_param(hwcursor, int, 0644);
  1231. MODULE_PARM_DESC(hwcursor, "Enable hardware cursor "
  1232. "(1=enable, 0=disable, default=1)");
  1233. #ifdef CONFIG_MTRR
  1234. module_param(nomtrr, bool, 0);
  1235. MODULE_PARM_DESC(nomtrr, "Disable MTRR support (default: enabled)");
  1236. #endif
  1237. module_init(tdfxfb_init);
  1238. module_exit(tdfxfb_exit);