tdfxfb.c 37 KB

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