tdfxfb.c 37 KB

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