tdfxfb.c 42 KB

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