tdfxfb.c 43 KB

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