tdfxfb.c 42 KB

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