tdfxfb.c 43 KB

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