tdfxfb.c 37 KB

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