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 const 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 const 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 const 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. /* fall through */
  451. case 24:
  452. var->red.offset = 16;
  453. var->green.offset = 8;
  454. var->blue.offset = 0;
  455. var->red.length = var->green.length = var->blue.length = 8;
  456. break;
  457. }
  458. var->width = -1;
  459. var->height = -1;
  460. var->accel_flags = FB_ACCELF_TEXT;
  461. DPRINTK("Checking graphics mode at %dx%d depth %d\n",
  462. var->xres, var->yres, var->bits_per_pixel);
  463. return 0;
  464. }
  465. static int tdfxfb_set_par(struct fb_info *info)
  466. {
  467. struct tdfx_par *par = info->par;
  468. u32 hdispend = info->var.xres;
  469. u32 hsyncsta = hdispend + info->var.right_margin;
  470. u32 hsyncend = hsyncsta + info->var.hsync_len;
  471. u32 htotal = hsyncend + info->var.left_margin;
  472. u32 hd, hs, he, ht, hbs, hbe;
  473. u32 vd, vs, ve, vt, vbs, vbe;
  474. struct banshee_reg reg;
  475. int fout, freq;
  476. u32 wd;
  477. u32 cpp = (info->var.bits_per_pixel + 7) >> 3;
  478. memset(&reg, 0, sizeof(reg));
  479. reg.vidcfg = VIDCFG_VIDPROC_ENABLE | VIDCFG_DESK_ENABLE |
  480. VIDCFG_CURS_X11 |
  481. ((cpp - 1) << VIDCFG_PIXFMT_SHIFT) |
  482. (cpp != 1 ? VIDCFG_CLUT_BYPASS : 0);
  483. /* PLL settings */
  484. freq = PICOS2KHZ(info->var.pixclock);
  485. reg.vidcfg &= ~VIDCFG_2X;
  486. if (freq > par->max_pixclock / 2) {
  487. freq = freq > par->max_pixclock ? par->max_pixclock : freq;
  488. reg.dacmode |= DACMODE_2X;
  489. reg.vidcfg |= VIDCFG_2X;
  490. hdispend >>= 1;
  491. hsyncsta >>= 1;
  492. hsyncend >>= 1;
  493. htotal >>= 1;
  494. }
  495. wd = (hdispend >> 3) - 1;
  496. hd = wd;
  497. hs = (hsyncsta >> 3) - 1;
  498. he = (hsyncend >> 3) - 1;
  499. ht = (htotal >> 3) - 1;
  500. hbs = hd;
  501. hbe = ht;
  502. if ((info->var.vmode & FB_VMODE_MASK) == FB_VMODE_DOUBLE) {
  503. vd = (info->var.yres << 1) - 1;
  504. vs = vd + (info->var.lower_margin << 1);
  505. ve = vs + (info->var.vsync_len << 1);
  506. vt = ve + (info->var.upper_margin << 1) - 1;
  507. reg.screensize = info->var.xres | (info->var.yres << 13);
  508. reg.vidcfg |= VIDCFG_HALF_MODE;
  509. reg.crt[0x09] = 0x80;
  510. } else {
  511. vd = info->var.yres - 1;
  512. vs = vd + info->var.lower_margin;
  513. ve = vs + info->var.vsync_len;
  514. vt = ve + info->var.upper_margin - 1;
  515. reg.screensize = info->var.xres | (info->var.yres << 12);
  516. reg.vidcfg &= ~VIDCFG_HALF_MODE;
  517. }
  518. vbs = vd;
  519. vbe = vt;
  520. /* this is all pretty standard VGA register stuffing */
  521. reg.misc[0x00] = 0x0f |
  522. (info->var.xres < 400 ? 0xa0 :
  523. info->var.xres < 480 ? 0x60 :
  524. info->var.xres < 768 ? 0xe0 : 0x20);
  525. reg.gra[0x05] = 0x40;
  526. reg.gra[0x06] = 0x05;
  527. reg.gra[0x07] = 0x0f;
  528. reg.gra[0x08] = 0xff;
  529. reg.att[0x00] = 0x00;
  530. reg.att[0x01] = 0x01;
  531. reg.att[0x02] = 0x02;
  532. reg.att[0x03] = 0x03;
  533. reg.att[0x04] = 0x04;
  534. reg.att[0x05] = 0x05;
  535. reg.att[0x06] = 0x06;
  536. reg.att[0x07] = 0x07;
  537. reg.att[0x08] = 0x08;
  538. reg.att[0x09] = 0x09;
  539. reg.att[0x0a] = 0x0a;
  540. reg.att[0x0b] = 0x0b;
  541. reg.att[0x0c] = 0x0c;
  542. reg.att[0x0d] = 0x0d;
  543. reg.att[0x0e] = 0x0e;
  544. reg.att[0x0f] = 0x0f;
  545. reg.att[0x10] = 0x41;
  546. reg.att[0x12] = 0x0f;
  547. reg.seq[0x00] = 0x03;
  548. reg.seq[0x01] = 0x01; /* fixme: clkdiv2? */
  549. reg.seq[0x02] = 0x0f;
  550. reg.seq[0x03] = 0x00;
  551. reg.seq[0x04] = 0x0e;
  552. reg.crt[0x00] = ht - 4;
  553. reg.crt[0x01] = hd;
  554. reg.crt[0x02] = hbs;
  555. reg.crt[0x03] = 0x80 | (hbe & 0x1f);
  556. reg.crt[0x04] = hs;
  557. reg.crt[0x05] = ((hbe & 0x20) << 2) | (he & 0x1f);
  558. reg.crt[0x06] = vt;
  559. reg.crt[0x07] = ((vs & 0x200) >> 2) |
  560. ((vd & 0x200) >> 3) |
  561. ((vt & 0x200) >> 4) | 0x10 |
  562. ((vbs & 0x100) >> 5) |
  563. ((vs & 0x100) >> 6) |
  564. ((vd & 0x100) >> 7) |
  565. ((vt & 0x100) >> 8);
  566. reg.crt[0x09] |= 0x40 | ((vbs & 0x200) >> 4);
  567. reg.crt[0x10] = vs;
  568. reg.crt[0x11] = (ve & 0x0f) | 0x20;
  569. reg.crt[0x12] = vd;
  570. reg.crt[0x13] = wd;
  571. reg.crt[0x15] = vbs;
  572. reg.crt[0x16] = vbe + 1;
  573. reg.crt[0x17] = 0xc3;
  574. reg.crt[0x18] = 0xff;
  575. /* Banshee's nonvga stuff */
  576. reg.ext[0x00] = (((ht & 0x100) >> 8) |
  577. ((hd & 0x100) >> 6) |
  578. ((hbs & 0x100) >> 4) |
  579. ((hbe & 0x40) >> 1) |
  580. ((hs & 0x100) >> 2) |
  581. ((he & 0x20) << 2));
  582. reg.ext[0x01] = (((vt & 0x400) >> 10) |
  583. ((vd & 0x400) >> 8) |
  584. ((vbs & 0x400) >> 6) |
  585. ((vbe & 0x400) >> 4));
  586. reg.vgainit0 = VGAINIT0_8BIT_DAC |
  587. VGAINIT0_EXT_ENABLE |
  588. VGAINIT0_WAKEUP_3C3 |
  589. VGAINIT0_ALT_READBACK |
  590. VGAINIT0_EXTSHIFTOUT;
  591. reg.vgainit1 = tdfx_inl(par, VGAINIT1) & 0x1fffff;
  592. if (hwcursor)
  593. reg.curspataddr = info->fix.smem_len;
  594. reg.cursloc = 0;
  595. reg.cursc0 = 0;
  596. reg.cursc1 = 0xffffff;
  597. reg.stride = info->var.xres * cpp;
  598. reg.startaddr = info->var.yoffset * reg.stride
  599. + info->var.xoffset * cpp;
  600. reg.vidpll = do_calc_pll(freq, &fout);
  601. #if 0
  602. reg.mempll = do_calc_pll(..., &fout);
  603. reg.gfxpll = do_calc_pll(..., &fout);
  604. #endif
  605. if ((info->var.vmode & FB_VMODE_MASK) == FB_VMODE_INTERLACED)
  606. reg.vidcfg |= VIDCFG_INTERLACE;
  607. reg.miscinit0 = tdfx_inl(par, MISCINIT0);
  608. #if defined(__BIG_ENDIAN)
  609. switch (info->var.bits_per_pixel) {
  610. case 8:
  611. case 24:
  612. reg.miscinit0 &= ~(1 << 30);
  613. reg.miscinit0 &= ~(1 << 31);
  614. break;
  615. case 16:
  616. reg.miscinit0 |= (1 << 30);
  617. reg.miscinit0 |= (1 << 31);
  618. break;
  619. case 32:
  620. reg.miscinit0 |= (1 << 30);
  621. reg.miscinit0 &= ~(1 << 31);
  622. break;
  623. }
  624. #endif
  625. do_write_regs(info, &reg);
  626. /* Now change fb_fix_screeninfo according to changes in par */
  627. info->fix.line_length = reg.stride;
  628. info->fix.visual = (info->var.bits_per_pixel == 8)
  629. ? FB_VISUAL_PSEUDOCOLOR
  630. : FB_VISUAL_TRUECOLOR;
  631. DPRINTK("Graphics mode is now set at %dx%d depth %d\n",
  632. info->var.xres, info->var.yres, info->var.bits_per_pixel);
  633. return 0;
  634. }
  635. /* A handy macro shamelessly pinched from matroxfb */
  636. #define CNVT_TOHW(val, width) ((((val) << (width)) + 0x7FFF - (val)) >> 16)
  637. static int tdfxfb_setcolreg(unsigned regno, unsigned red, unsigned green,
  638. unsigned blue, unsigned transp,
  639. struct fb_info *info)
  640. {
  641. struct tdfx_par *par = info->par;
  642. u32 rgbcol;
  643. if (regno >= info->cmap.len || regno > 255)
  644. return 1;
  645. /* grayscale works only partially under directcolor */
  646. if (info->var.grayscale) {
  647. /* grayscale = 0.30*R + 0.59*G + 0.11*B */
  648. blue = (red * 77 + green * 151 + blue * 28) >> 8;
  649. green = blue;
  650. red = blue;
  651. }
  652. switch (info->fix.visual) {
  653. case FB_VISUAL_PSEUDOCOLOR:
  654. rgbcol = (((u32)red & 0xff00) << 8) |
  655. (((u32)green & 0xff00) << 0) |
  656. (((u32)blue & 0xff00) >> 8);
  657. do_setpalentry(par, regno, rgbcol);
  658. break;
  659. /* Truecolor has no hardware color palettes. */
  660. case FB_VISUAL_TRUECOLOR:
  661. if (regno < 16) {
  662. rgbcol = (CNVT_TOHW(red, info->var.red.length) <<
  663. info->var.red.offset) |
  664. (CNVT_TOHW(green, info->var.green.length) <<
  665. info->var.green.offset) |
  666. (CNVT_TOHW(blue, info->var.blue.length) <<
  667. info->var.blue.offset) |
  668. (CNVT_TOHW(transp, info->var.transp.length) <<
  669. info->var.transp.offset);
  670. par->palette[regno] = rgbcol;
  671. }
  672. break;
  673. default:
  674. DPRINTK("bad depth %u\n", info->var.bits_per_pixel);
  675. break;
  676. }
  677. return 0;
  678. }
  679. /* 0 unblank, 1 blank, 2 no vsync, 3 no hsync, 4 off */
  680. static int tdfxfb_blank(int blank, struct fb_info *info)
  681. {
  682. struct tdfx_par *par = info->par;
  683. int vgablank = 1;
  684. u32 dacmode = tdfx_inl(par, DACMODE);
  685. dacmode &= ~(BIT(1) | BIT(3));
  686. switch (blank) {
  687. case FB_BLANK_UNBLANK: /* Screen: On; HSync: On, VSync: On */
  688. vgablank = 0;
  689. break;
  690. case FB_BLANK_NORMAL: /* Screen: Off; HSync: On, VSync: On */
  691. break;
  692. case FB_BLANK_VSYNC_SUSPEND: /* Screen: Off; HSync: On, VSync: Off */
  693. dacmode |= BIT(3);
  694. break;
  695. case FB_BLANK_HSYNC_SUSPEND: /* Screen: Off; HSync: Off, VSync: On */
  696. dacmode |= BIT(1);
  697. break;
  698. case FB_BLANK_POWERDOWN: /* Screen: Off; HSync: Off, VSync: Off */
  699. dacmode |= BIT(1) | BIT(3);
  700. break;
  701. }
  702. banshee_make_room(par, 1);
  703. tdfx_outl(par, DACMODE, dacmode);
  704. if (vgablank)
  705. vga_disable_video(par);
  706. else
  707. vga_enable_video(par);
  708. return 0;
  709. }
  710. /*
  711. * Set the starting position of the visible screen to var->yoffset
  712. */
  713. static int tdfxfb_pan_display(struct fb_var_screeninfo *var,
  714. struct fb_info *info)
  715. {
  716. struct tdfx_par *par = info->par;
  717. u32 addr = var->yoffset * info->fix.line_length;
  718. if (nopan || var->xoffset)
  719. return -EINVAL;
  720. banshee_make_room(par, 1);
  721. tdfx_outl(par, VIDDESKSTART, addr);
  722. return 0;
  723. }
  724. #ifdef CONFIG_FB_3DFX_ACCEL
  725. /*
  726. * FillRect 2D command (solidfill or invert (via ROP_XOR))
  727. */
  728. static void tdfxfb_fillrect(struct fb_info *info,
  729. const struct fb_fillrect *rect)
  730. {
  731. struct tdfx_par *par = info->par;
  732. u32 bpp = info->var.bits_per_pixel;
  733. u32 stride = info->fix.line_length;
  734. u32 fmt = stride | ((bpp + ((bpp == 8) ? 0 : 8)) << 13);
  735. int tdfx_rop;
  736. u32 dx = rect->dx;
  737. u32 dy = rect->dy;
  738. u32 dstbase = 0;
  739. if (rect->rop == ROP_COPY)
  740. tdfx_rop = TDFX_ROP_COPY;
  741. else
  742. tdfx_rop = TDFX_ROP_XOR;
  743. /* assume always rect->height < 4096 */
  744. if (dy + rect->height > 4095) {
  745. dstbase = stride * dy;
  746. dy = 0;
  747. }
  748. /* assume always rect->width < 4096 */
  749. if (dx + rect->width > 4095) {
  750. dstbase += dx * bpp >> 3;
  751. dx = 0;
  752. }
  753. banshee_make_room(par, 6);
  754. tdfx_outl(par, DSTFORMAT, fmt);
  755. if (info->fix.visual == FB_VISUAL_PSEUDOCOLOR) {
  756. tdfx_outl(par, COLORFORE, rect->color);
  757. } else { /* FB_VISUAL_TRUECOLOR */
  758. tdfx_outl(par, COLORFORE, par->palette[rect->color]);
  759. }
  760. tdfx_outl(par, COMMAND_2D, COMMAND_2D_FILLRECT | (tdfx_rop << 24));
  761. tdfx_outl(par, DSTBASE, dstbase);
  762. tdfx_outl(par, DSTSIZE, rect->width | (rect->height << 16));
  763. tdfx_outl(par, LAUNCH_2D, dx | (dy << 16));
  764. }
  765. /*
  766. * Screen-to-Screen BitBlt 2D command (for the bmove fb op.)
  767. */
  768. static void tdfxfb_copyarea(struct fb_info *info,
  769. const struct fb_copyarea *area)
  770. {
  771. struct tdfx_par *par = info->par;
  772. u32 sx = area->sx, sy = area->sy, dx = area->dx, dy = area->dy;
  773. u32 bpp = info->var.bits_per_pixel;
  774. u32 stride = info->fix.line_length;
  775. u32 blitcmd = COMMAND_2D_S2S_BITBLT | (TDFX_ROP_COPY << 24);
  776. u32 fmt = stride | ((bpp + ((bpp == 8) ? 0 : 8)) << 13);
  777. u32 dstbase = 0;
  778. u32 srcbase = 0;
  779. /* assume always area->height < 4096 */
  780. if (sy + area->height > 4095) {
  781. srcbase = stride * sy;
  782. sy = 0;
  783. }
  784. /* assume always area->width < 4096 */
  785. if (sx + area->width > 4095) {
  786. srcbase += sx * bpp >> 3;
  787. sx = 0;
  788. }
  789. /* assume always area->height < 4096 */
  790. if (dy + area->height > 4095) {
  791. dstbase = stride * dy;
  792. dy = 0;
  793. }
  794. /* assume always area->width < 4096 */
  795. if (dx + area->width > 4095) {
  796. dstbase += dx * bpp >> 3;
  797. dx = 0;
  798. }
  799. if (area->sx <= area->dx) {
  800. /* -X */
  801. blitcmd |= BIT(14);
  802. sx += area->width - 1;
  803. dx += area->width - 1;
  804. }
  805. if (area->sy <= area->dy) {
  806. /* -Y */
  807. blitcmd |= BIT(15);
  808. sy += area->height - 1;
  809. dy += area->height - 1;
  810. }
  811. banshee_make_room(par, 8);
  812. tdfx_outl(par, SRCFORMAT, fmt);
  813. tdfx_outl(par, DSTFORMAT, fmt);
  814. tdfx_outl(par, COMMAND_2D, blitcmd);
  815. tdfx_outl(par, DSTSIZE, area->width | (area->height << 16));
  816. tdfx_outl(par, DSTXY, dx | (dy << 16));
  817. tdfx_outl(par, SRCBASE, srcbase);
  818. tdfx_outl(par, DSTBASE, dstbase);
  819. tdfx_outl(par, LAUNCH_2D, sx | (sy << 16));
  820. }
  821. static void tdfxfb_imageblit(struct fb_info *info, const struct fb_image *image)
  822. {
  823. struct tdfx_par *par = info->par;
  824. int size = image->height * ((image->width * image->depth + 7) >> 3);
  825. int fifo_free;
  826. int i, stride = info->fix.line_length;
  827. u32 bpp = info->var.bits_per_pixel;
  828. u32 dstfmt = stride | ((bpp + ((bpp == 8) ? 0 : 8)) << 13);
  829. u8 *chardata = (u8 *) image->data;
  830. u32 srcfmt;
  831. u32 dx = image->dx;
  832. u32 dy = image->dy;
  833. u32 dstbase = 0;
  834. if (image->depth != 1) {
  835. #ifdef BROKEN_CODE
  836. banshee_make_room(par, 6 + ((size + 3) >> 2));
  837. srcfmt = stride | ((bpp + ((bpp == 8) ? 0 : 8)) << 13) |
  838. 0x400000;
  839. #else
  840. cfb_imageblit(info, image);
  841. #endif
  842. return;
  843. }
  844. banshee_make_room(par, 9);
  845. switch (info->fix.visual) {
  846. case FB_VISUAL_PSEUDOCOLOR:
  847. tdfx_outl(par, COLORFORE, image->fg_color);
  848. tdfx_outl(par, COLORBACK, image->bg_color);
  849. break;
  850. case FB_VISUAL_TRUECOLOR:
  851. default:
  852. tdfx_outl(par, COLORFORE,
  853. par->palette[image->fg_color]);
  854. tdfx_outl(par, COLORBACK,
  855. par->palette[image->bg_color]);
  856. }
  857. #ifdef __BIG_ENDIAN
  858. srcfmt = 0x400000 | BIT(20);
  859. #else
  860. srcfmt = 0x400000;
  861. #endif
  862. /* assume always image->height < 4096 */
  863. if (dy + image->height > 4095) {
  864. dstbase = stride * dy;
  865. dy = 0;
  866. }
  867. /* assume always image->width < 4096 */
  868. if (dx + image->width > 4095) {
  869. dstbase += dx * bpp >> 3;
  870. dx = 0;
  871. }
  872. tdfx_outl(par, DSTBASE, dstbase);
  873. tdfx_outl(par, SRCXY, 0);
  874. tdfx_outl(par, DSTXY, dx | (dy << 16));
  875. tdfx_outl(par, COMMAND_2D,
  876. COMMAND_2D_H2S_BITBLT | (TDFX_ROP_COPY << 24));
  877. tdfx_outl(par, SRCFORMAT, srcfmt);
  878. tdfx_outl(par, DSTFORMAT, dstfmt);
  879. tdfx_outl(par, DSTSIZE, image->width | (image->height << 16));
  880. /* A count of how many free FIFO entries we've requested.
  881. * When this goes negative, we need to request more. */
  882. fifo_free = 0;
  883. /* Send four bytes at a time of data */
  884. for (i = (size >> 2); i > 0; i--) {
  885. if (--fifo_free < 0) {
  886. fifo_free = 31;
  887. banshee_make_room(par, fifo_free);
  888. }
  889. tdfx_outl(par, LAUNCH_2D, *(u32 *)chardata);
  890. chardata += 4;
  891. }
  892. /* Send the leftovers now */
  893. banshee_make_room(par, 3);
  894. switch (size % 4) {
  895. case 0:
  896. break;
  897. case 1:
  898. tdfx_outl(par, LAUNCH_2D, *chardata);
  899. break;
  900. case 2:
  901. tdfx_outl(par, LAUNCH_2D, *(u16 *)chardata);
  902. break;
  903. case 3:
  904. tdfx_outl(par, LAUNCH_2D,
  905. *(u16 *)chardata | (chardata[3] << 24));
  906. break;
  907. }
  908. }
  909. #endif /* CONFIG_FB_3DFX_ACCEL */
  910. static int tdfxfb_cursor(struct fb_info *info, struct fb_cursor *cursor)
  911. {
  912. struct tdfx_par *par = info->par;
  913. u32 vidcfg;
  914. if (!hwcursor)
  915. return -EINVAL; /* just to force soft_cursor() call */
  916. /* Too large of a cursor or wrong bpp :-( */
  917. if (cursor->image.width > 64 ||
  918. cursor->image.height > 64 ||
  919. cursor->image.depth > 1)
  920. return -EINVAL;
  921. vidcfg = tdfx_inl(par, VIDPROCCFG);
  922. if (cursor->enable)
  923. tdfx_outl(par, VIDPROCCFG, vidcfg | VIDCFG_HWCURSOR_ENABLE);
  924. else
  925. tdfx_outl(par, VIDPROCCFG, vidcfg & ~VIDCFG_HWCURSOR_ENABLE);
  926. /*
  927. * If the cursor is not be changed this means either we want the
  928. * current cursor state (if enable is set) or we want to query what
  929. * we can do with the cursor (if enable is not set)
  930. */
  931. if (!cursor->set)
  932. return 0;
  933. /* fix cursor color - XFree86 forgets to restore it properly */
  934. if (cursor->set & FB_CUR_SETCMAP) {
  935. struct fb_cmap cmap = info->cmap;
  936. u32 bg_idx = cursor->image.bg_color;
  937. u32 fg_idx = cursor->image.fg_color;
  938. unsigned long bg_color, fg_color;
  939. fg_color = (((u32)cmap.red[fg_idx] & 0xff00) << 8) |
  940. (((u32)cmap.green[fg_idx] & 0xff00) << 0) |
  941. (((u32)cmap.blue[fg_idx] & 0xff00) >> 8);
  942. bg_color = (((u32)cmap.red[bg_idx] & 0xff00) << 8) |
  943. (((u32)cmap.green[bg_idx] & 0xff00) << 0) |
  944. (((u32)cmap.blue[bg_idx] & 0xff00) >> 8);
  945. banshee_make_room(par, 2);
  946. tdfx_outl(par, HWCURC0, bg_color);
  947. tdfx_outl(par, HWCURC1, fg_color);
  948. }
  949. if (cursor->set & FB_CUR_SETPOS) {
  950. int x = cursor->image.dx;
  951. int y = cursor->image.dy - info->var.yoffset;
  952. x += 63;
  953. y += 63;
  954. banshee_make_room(par, 1);
  955. tdfx_outl(par, HWCURLOC, (y << 16) + x);
  956. }
  957. if (cursor->set & (FB_CUR_SETIMAGE | FB_CUR_SETSHAPE)) {
  958. /*
  959. * Voodoo 3 and above cards use 2 monochrome cursor patterns.
  960. * The reason is so the card can fetch 8 words at a time
  961. * and are stored on chip for use for the next 8 scanlines.
  962. * This reduces the number of times for access to draw the
  963. * cursor for each screen refresh.
  964. * Each pattern is a bitmap of 64 bit wide and 64 bit high
  965. * (total of 8192 bits or 1024 bytes). The two patterns are
  966. * stored in such a way that pattern 0 always resides in the
  967. * lower half (least significant 64 bits) of a 128 bit word
  968. * and pattern 1 the upper half. If you examine the data of
  969. * the cursor image the graphics card uses then from the
  970. * beginning you see line one of pattern 0, line one of
  971. * pattern 1, line two of pattern 0, line two of pattern 1,
  972. * etc etc. The linear stride for the cursor is always 16 bytes
  973. * (128 bits) which is the maximum cursor width times two for
  974. * the two monochrome patterns.
  975. */
  976. u8 __iomem *cursorbase = info->screen_base + info->fix.smem_len;
  977. u8 *bitmap = (u8 *)cursor->image.data;
  978. u8 *mask = (u8 *)cursor->mask;
  979. int i;
  980. fb_memset(cursorbase, 0, 1024);
  981. for (i = 0; i < cursor->image.height; i++) {
  982. int h = 0;
  983. int j = (cursor->image.width + 7) >> 3;
  984. for (; j > 0; j--) {
  985. u8 data = *mask ^ *bitmap;
  986. if (cursor->rop == ROP_COPY)
  987. data = *mask & *bitmap;
  988. /* Pattern 0. Copy the cursor mask to it */
  989. fb_writeb(*mask, cursorbase + h);
  990. mask++;
  991. /* Pattern 1. Copy the cursor bitmap to it */
  992. fb_writeb(data, cursorbase + h + 8);
  993. bitmap++;
  994. h++;
  995. }
  996. cursorbase += 16;
  997. }
  998. }
  999. return 0;
  1000. }
  1001. static struct fb_ops tdfxfb_ops = {
  1002. .owner = THIS_MODULE,
  1003. .fb_check_var = tdfxfb_check_var,
  1004. .fb_set_par = tdfxfb_set_par,
  1005. .fb_setcolreg = tdfxfb_setcolreg,
  1006. .fb_blank = tdfxfb_blank,
  1007. .fb_pan_display = tdfxfb_pan_display,
  1008. .fb_sync = banshee_wait_idle,
  1009. .fb_cursor = tdfxfb_cursor,
  1010. #ifdef CONFIG_FB_3DFX_ACCEL
  1011. .fb_fillrect = tdfxfb_fillrect,
  1012. .fb_copyarea = tdfxfb_copyarea,
  1013. .fb_imageblit = tdfxfb_imageblit,
  1014. #else
  1015. .fb_fillrect = cfb_fillrect,
  1016. .fb_copyarea = cfb_copyarea,
  1017. .fb_imageblit = cfb_imageblit,
  1018. #endif
  1019. };
  1020. #ifdef CONFIG_FB_3DFX_I2C
  1021. /* The voo GPIO registers don't have individual masks for each bit
  1022. so we always have to read before writing. */
  1023. static void tdfxfb_i2c_setscl(void *data, int val)
  1024. {
  1025. struct tdfxfb_i2c_chan *chan = data;
  1026. struct tdfx_par *par = chan->par;
  1027. unsigned int r;
  1028. r = tdfx_inl(par, VIDSERPARPORT);
  1029. if (val)
  1030. r |= I2C_SCL_OUT;
  1031. else
  1032. r &= ~I2C_SCL_OUT;
  1033. tdfx_outl(par, VIDSERPARPORT, r);
  1034. tdfx_inl(par, VIDSERPARPORT); /* flush posted write */
  1035. }
  1036. static void tdfxfb_i2c_setsda(void *data, int val)
  1037. {
  1038. struct tdfxfb_i2c_chan *chan = data;
  1039. struct tdfx_par *par = chan->par;
  1040. unsigned int r;
  1041. r = tdfx_inl(par, VIDSERPARPORT);
  1042. if (val)
  1043. r |= I2C_SDA_OUT;
  1044. else
  1045. r &= ~I2C_SDA_OUT;
  1046. tdfx_outl(par, VIDSERPARPORT, r);
  1047. tdfx_inl(par, VIDSERPARPORT); /* flush posted write */
  1048. }
  1049. /* The GPIO pins are open drain, so the pins always remain outputs.
  1050. We rely on the i2c-algo-bit routines to set the pins high before
  1051. reading the input from other chips. */
  1052. static int tdfxfb_i2c_getscl(void *data)
  1053. {
  1054. struct tdfxfb_i2c_chan *chan = data;
  1055. struct tdfx_par *par = chan->par;
  1056. return (0 != (tdfx_inl(par, VIDSERPARPORT) & I2C_SCL_IN));
  1057. }
  1058. static int tdfxfb_i2c_getsda(void *data)
  1059. {
  1060. struct tdfxfb_i2c_chan *chan = data;
  1061. struct tdfx_par *par = chan->par;
  1062. return (0 != (tdfx_inl(par, VIDSERPARPORT) & I2C_SDA_IN));
  1063. }
  1064. static void tdfxfb_ddc_setscl(void *data, int val)
  1065. {
  1066. struct tdfxfb_i2c_chan *chan = data;
  1067. struct tdfx_par *par = chan->par;
  1068. unsigned int r;
  1069. r = tdfx_inl(par, VIDSERPARPORT);
  1070. if (val)
  1071. r |= DDC_SCL_OUT;
  1072. else
  1073. r &= ~DDC_SCL_OUT;
  1074. tdfx_outl(par, VIDSERPARPORT, r);
  1075. tdfx_inl(par, VIDSERPARPORT); /* flush posted write */
  1076. }
  1077. static void tdfxfb_ddc_setsda(void *data, int val)
  1078. {
  1079. struct tdfxfb_i2c_chan *chan = data;
  1080. struct tdfx_par *par = chan->par;
  1081. unsigned int r;
  1082. r = tdfx_inl(par, VIDSERPARPORT);
  1083. if (val)
  1084. r |= DDC_SDA_OUT;
  1085. else
  1086. r &= ~DDC_SDA_OUT;
  1087. tdfx_outl(par, VIDSERPARPORT, r);
  1088. tdfx_inl(par, VIDSERPARPORT); /* flush posted write */
  1089. }
  1090. static int tdfxfb_ddc_getscl(void *data)
  1091. {
  1092. struct tdfxfb_i2c_chan *chan = data;
  1093. struct tdfx_par *par = chan->par;
  1094. return (0 != (tdfx_inl(par, VIDSERPARPORT) & DDC_SCL_IN));
  1095. }
  1096. static int tdfxfb_ddc_getsda(void *data)
  1097. {
  1098. struct tdfxfb_i2c_chan *chan = data;
  1099. struct tdfx_par *par = chan->par;
  1100. return (0 != (tdfx_inl(par, VIDSERPARPORT) & DDC_SDA_IN));
  1101. }
  1102. static int tdfxfb_setup_ddc_bus(struct tdfxfb_i2c_chan *chan, const char *name,
  1103. struct device *dev)
  1104. {
  1105. int rc;
  1106. strlcpy(chan->adapter.name, name, sizeof(chan->adapter.name));
  1107. chan->adapter.owner = THIS_MODULE;
  1108. chan->adapter.class = I2C_CLASS_DDC;
  1109. chan->adapter.algo_data = &chan->algo;
  1110. chan->adapter.dev.parent = dev;
  1111. chan->algo.setsda = tdfxfb_ddc_setsda;
  1112. chan->algo.setscl = tdfxfb_ddc_setscl;
  1113. chan->algo.getsda = tdfxfb_ddc_getsda;
  1114. chan->algo.getscl = tdfxfb_ddc_getscl;
  1115. chan->algo.udelay = 10;
  1116. chan->algo.timeout = msecs_to_jiffies(500);
  1117. chan->algo.data = chan;
  1118. i2c_set_adapdata(&chan->adapter, chan);
  1119. rc = i2c_bit_add_bus(&chan->adapter);
  1120. if (rc == 0)
  1121. DPRINTK("I2C bus %s registered.\n", name);
  1122. else
  1123. chan->par = NULL;
  1124. return rc;
  1125. }
  1126. static int tdfxfb_setup_i2c_bus(struct tdfxfb_i2c_chan *chan, const char *name,
  1127. struct device *dev)
  1128. {
  1129. int rc;
  1130. strlcpy(chan->adapter.name, name, sizeof(chan->adapter.name));
  1131. chan->adapter.owner = THIS_MODULE;
  1132. chan->adapter.algo_data = &chan->algo;
  1133. chan->adapter.dev.parent = dev;
  1134. chan->algo.setsda = tdfxfb_i2c_setsda;
  1135. chan->algo.setscl = tdfxfb_i2c_setscl;
  1136. chan->algo.getsda = tdfxfb_i2c_getsda;
  1137. chan->algo.getscl = tdfxfb_i2c_getscl;
  1138. chan->algo.udelay = 10;
  1139. chan->algo.timeout = msecs_to_jiffies(500);
  1140. chan->algo.data = chan;
  1141. i2c_set_adapdata(&chan->adapter, chan);
  1142. rc = i2c_bit_add_bus(&chan->adapter);
  1143. if (rc == 0)
  1144. DPRINTK("I2C bus %s registered.\n", name);
  1145. else
  1146. chan->par = NULL;
  1147. return rc;
  1148. }
  1149. static void tdfxfb_create_i2c_busses(struct fb_info *info)
  1150. {
  1151. struct tdfx_par *par = info->par;
  1152. tdfx_outl(par, VIDINFORMAT, 0x8160);
  1153. tdfx_outl(par, VIDSERPARPORT, 0xcffc0020);
  1154. par->chan[0].par = par;
  1155. par->chan[1].par = par;
  1156. tdfxfb_setup_ddc_bus(&par->chan[0], "Voodoo3-DDC", info->dev);
  1157. tdfxfb_setup_i2c_bus(&par->chan[1], "Voodoo3-I2C", info->dev);
  1158. }
  1159. static void tdfxfb_delete_i2c_busses(struct tdfx_par *par)
  1160. {
  1161. if (par->chan[0].par)
  1162. i2c_del_adapter(&par->chan[0].adapter);
  1163. par->chan[0].par = NULL;
  1164. if (par->chan[1].par)
  1165. i2c_del_adapter(&par->chan[1].adapter);
  1166. par->chan[1].par = NULL;
  1167. }
  1168. static int tdfxfb_probe_i2c_connector(struct tdfx_par *par,
  1169. struct fb_monspecs *specs)
  1170. {
  1171. u8 *edid = NULL;
  1172. DPRINTK("Probe DDC Bus\n");
  1173. if (par->chan[0].par)
  1174. edid = fb_ddc_read(&par->chan[0].adapter);
  1175. if (edid) {
  1176. fb_edid_to_monspecs(edid, specs);
  1177. kfree(edid);
  1178. return 0;
  1179. }
  1180. return 1;
  1181. }
  1182. #endif /* CONFIG_FB_3DFX_I2C */
  1183. /**
  1184. * tdfxfb_probe - Device Initializiation
  1185. *
  1186. * @pdev: PCI Device to initialize
  1187. * @id: PCI Device ID
  1188. *
  1189. * Initializes and allocates resources for PCI device @pdev.
  1190. *
  1191. */
  1192. static int tdfxfb_probe(struct pci_dev *pdev, const struct pci_device_id *id)
  1193. {
  1194. struct tdfx_par *default_par;
  1195. struct fb_info *info;
  1196. int err, lpitch;
  1197. struct fb_monspecs *specs;
  1198. bool found;
  1199. err = pci_enable_device(pdev);
  1200. if (err) {
  1201. printk(KERN_ERR "tdfxfb: Can't enable pdev: %d\n", err);
  1202. return err;
  1203. }
  1204. info = framebuffer_alloc(sizeof(struct tdfx_par), &pdev->dev);
  1205. if (!info)
  1206. return -ENOMEM;
  1207. default_par = info->par;
  1208. info->fix = tdfx_fix;
  1209. /* Configure the default fb_fix_screeninfo first */
  1210. switch (pdev->device) {
  1211. case PCI_DEVICE_ID_3DFX_BANSHEE:
  1212. strcpy(info->fix.id, "3Dfx Banshee");
  1213. default_par->max_pixclock = BANSHEE_MAX_PIXCLOCK;
  1214. break;
  1215. case PCI_DEVICE_ID_3DFX_VOODOO3:
  1216. strcpy(info->fix.id, "3Dfx Voodoo3");
  1217. default_par->max_pixclock = VOODOO3_MAX_PIXCLOCK;
  1218. break;
  1219. case PCI_DEVICE_ID_3DFX_VOODOO5:
  1220. strcpy(info->fix.id, "3Dfx Voodoo5");
  1221. default_par->max_pixclock = VOODOO5_MAX_PIXCLOCK;
  1222. break;
  1223. }
  1224. info->fix.mmio_start = pci_resource_start(pdev, 0);
  1225. info->fix.mmio_len = pci_resource_len(pdev, 0);
  1226. if (!request_mem_region(info->fix.mmio_start, info->fix.mmio_len,
  1227. "tdfx regbase")) {
  1228. printk(KERN_ERR "tdfxfb: Can't reserve regbase\n");
  1229. goto out_err;
  1230. }
  1231. default_par->regbase_virt =
  1232. ioremap_nocache(info->fix.mmio_start, info->fix.mmio_len);
  1233. if (!default_par->regbase_virt) {
  1234. printk(KERN_ERR "fb: Can't remap %s register area.\n",
  1235. info->fix.id);
  1236. goto out_err_regbase;
  1237. }
  1238. info->fix.smem_start = pci_resource_start(pdev, 1);
  1239. info->fix.smem_len = do_lfb_size(default_par, pdev->device);
  1240. if (!info->fix.smem_len) {
  1241. printk(KERN_ERR "fb: Can't count %s memory.\n", info->fix.id);
  1242. goto out_err_regbase;
  1243. }
  1244. if (!request_mem_region(info->fix.smem_start,
  1245. pci_resource_len(pdev, 1), "tdfx smem")) {
  1246. printk(KERN_ERR "tdfxfb: Can't reserve smem\n");
  1247. goto out_err_regbase;
  1248. }
  1249. info->screen_base = ioremap_wc(info->fix.smem_start,
  1250. info->fix.smem_len);
  1251. if (!info->screen_base) {
  1252. printk(KERN_ERR "fb: Can't remap %s framebuffer.\n",
  1253. info->fix.id);
  1254. goto out_err_screenbase;
  1255. }
  1256. default_par->iobase = pci_resource_start(pdev, 2);
  1257. if (!request_region(pci_resource_start(pdev, 2),
  1258. pci_resource_len(pdev, 2), "tdfx iobase")) {
  1259. printk(KERN_ERR "tdfxfb: Can't reserve iobase\n");
  1260. goto out_err_screenbase;
  1261. }
  1262. printk(KERN_INFO "fb: %s memory = %dK\n", info->fix.id,
  1263. info->fix.smem_len >> 10);
  1264. if (!nomtrr)
  1265. default_par->wc_cookie= arch_phys_wc_add(info->fix.smem_start,
  1266. info->fix.smem_len);
  1267. info->fix.ypanstep = nopan ? 0 : 1;
  1268. info->fix.ywrapstep = nowrap ? 0 : 1;
  1269. info->fbops = &tdfxfb_ops;
  1270. info->pseudo_palette = default_par->palette;
  1271. info->flags = FBINFO_DEFAULT | FBINFO_HWACCEL_YPAN;
  1272. #ifdef CONFIG_FB_3DFX_ACCEL
  1273. info->flags |= FBINFO_HWACCEL_FILLRECT |
  1274. FBINFO_HWACCEL_COPYAREA |
  1275. FBINFO_HWACCEL_IMAGEBLIT |
  1276. FBINFO_READS_FAST;
  1277. #endif
  1278. /* reserve 8192 bits for cursor */
  1279. /* the 2.4 driver says PAGE_MASK boundary is not enough for Voodoo4 */
  1280. if (hwcursor)
  1281. info->fix.smem_len = (info->fix.smem_len - 1024) &
  1282. (PAGE_MASK << 1);
  1283. specs = &info->monspecs;
  1284. found = false;
  1285. info->var.bits_per_pixel = 8;
  1286. #ifdef CONFIG_FB_3DFX_I2C
  1287. tdfxfb_create_i2c_busses(info);
  1288. err = tdfxfb_probe_i2c_connector(default_par, specs);
  1289. if (!err) {
  1290. if (specs->modedb == NULL)
  1291. DPRINTK("Unable to get Mode Database\n");
  1292. else {
  1293. const struct fb_videomode *m;
  1294. fb_videomode_to_modelist(specs->modedb,
  1295. specs->modedb_len,
  1296. &info->modelist);
  1297. m = fb_find_best_display(specs, &info->modelist);
  1298. if (m) {
  1299. fb_videomode_to_var(&info->var, m);
  1300. /* fill all other info->var's fields */
  1301. if (tdfxfb_check_var(&info->var, info) < 0)
  1302. info->var = tdfx_var;
  1303. else
  1304. found = true;
  1305. }
  1306. }
  1307. }
  1308. #endif
  1309. if (!mode_option && !found)
  1310. mode_option = "640x480@60";
  1311. if (mode_option) {
  1312. err = fb_find_mode(&info->var, info, mode_option,
  1313. specs->modedb, specs->modedb_len,
  1314. NULL, info->var.bits_per_pixel);
  1315. if (!err || err == 4)
  1316. info->var = tdfx_var;
  1317. }
  1318. if (found) {
  1319. fb_destroy_modedb(specs->modedb);
  1320. specs->modedb = NULL;
  1321. }
  1322. /* maximize virtual vertical length */
  1323. lpitch = info->var.xres_virtual * ((info->var.bits_per_pixel + 7) >> 3);
  1324. info->var.yres_virtual = info->fix.smem_len / lpitch;
  1325. if (info->var.yres_virtual < info->var.yres)
  1326. goto out_err_iobase;
  1327. if (fb_alloc_cmap(&info->cmap, 256, 0) < 0) {
  1328. printk(KERN_ERR "tdfxfb: Can't allocate color map\n");
  1329. goto out_err_iobase;
  1330. }
  1331. if (register_framebuffer(info) < 0) {
  1332. printk(KERN_ERR "tdfxfb: can't register framebuffer\n");
  1333. fb_dealloc_cmap(&info->cmap);
  1334. goto out_err_iobase;
  1335. }
  1336. /*
  1337. * Our driver data
  1338. */
  1339. pci_set_drvdata(pdev, info);
  1340. return 0;
  1341. out_err_iobase:
  1342. #ifdef CONFIG_FB_3DFX_I2C
  1343. tdfxfb_delete_i2c_busses(default_par);
  1344. #endif
  1345. arch_phys_wc_del(default_par->wc_cookie);
  1346. release_region(pci_resource_start(pdev, 2),
  1347. pci_resource_len(pdev, 2));
  1348. out_err_screenbase:
  1349. if (info->screen_base)
  1350. iounmap(info->screen_base);
  1351. release_mem_region(info->fix.smem_start, pci_resource_len(pdev, 1));
  1352. out_err_regbase:
  1353. /*
  1354. * Cleanup after anything that was remapped/allocated.
  1355. */
  1356. if (default_par->regbase_virt)
  1357. iounmap(default_par->regbase_virt);
  1358. release_mem_region(info->fix.mmio_start, info->fix.mmio_len);
  1359. out_err:
  1360. framebuffer_release(info);
  1361. return -ENXIO;
  1362. }
  1363. #ifndef MODULE
  1364. static void __init tdfxfb_setup(char *options)
  1365. {
  1366. char *this_opt;
  1367. if (!options || !*options)
  1368. return;
  1369. while ((this_opt = strsep(&options, ",")) != NULL) {
  1370. if (!*this_opt)
  1371. continue;
  1372. if (!strcmp(this_opt, "nopan")) {
  1373. nopan = 1;
  1374. } else if (!strcmp(this_opt, "nowrap")) {
  1375. nowrap = 1;
  1376. } else if (!strncmp(this_opt, "hwcursor=", 9)) {
  1377. hwcursor = simple_strtoul(this_opt + 9, NULL, 0);
  1378. } else if (!strncmp(this_opt, "nomtrr", 6)) {
  1379. nomtrr = 1;
  1380. } else {
  1381. mode_option = this_opt;
  1382. }
  1383. }
  1384. }
  1385. #endif
  1386. /**
  1387. * tdfxfb_remove - Device removal
  1388. *
  1389. * @pdev: PCI Device to cleanup
  1390. *
  1391. * Releases all resources allocated during the course of the driver's
  1392. * lifetime for the PCI device @pdev.
  1393. *
  1394. */
  1395. static void tdfxfb_remove(struct pci_dev *pdev)
  1396. {
  1397. struct fb_info *info = pci_get_drvdata(pdev);
  1398. struct tdfx_par *par = info->par;
  1399. unregister_framebuffer(info);
  1400. #ifdef CONFIG_FB_3DFX_I2C
  1401. tdfxfb_delete_i2c_busses(par);
  1402. #endif
  1403. arch_phys_wc_del(par->wc_cookie);
  1404. iounmap(par->regbase_virt);
  1405. iounmap(info->screen_base);
  1406. /* Clean up after reserved regions */
  1407. release_region(pci_resource_start(pdev, 2),
  1408. pci_resource_len(pdev, 2));
  1409. release_mem_region(pci_resource_start(pdev, 1),
  1410. pci_resource_len(pdev, 1));
  1411. release_mem_region(pci_resource_start(pdev, 0),
  1412. pci_resource_len(pdev, 0));
  1413. fb_dealloc_cmap(&info->cmap);
  1414. framebuffer_release(info);
  1415. }
  1416. static int __init tdfxfb_init(void)
  1417. {
  1418. #ifndef MODULE
  1419. char *option = NULL;
  1420. if (fb_get_options("tdfxfb", &option))
  1421. return -ENODEV;
  1422. tdfxfb_setup(option);
  1423. #endif
  1424. return pci_register_driver(&tdfxfb_driver);
  1425. }
  1426. static void __exit tdfxfb_exit(void)
  1427. {
  1428. pci_unregister_driver(&tdfxfb_driver);
  1429. }
  1430. MODULE_AUTHOR("Hannu Mallat <hmallat@cc.hut.fi>");
  1431. MODULE_DESCRIPTION("3Dfx framebuffer device driver");
  1432. MODULE_LICENSE("GPL");
  1433. module_param(hwcursor, int, 0644);
  1434. MODULE_PARM_DESC(hwcursor, "Enable hardware cursor "
  1435. "(1=enable, 0=disable, default=1)");
  1436. module_param(mode_option, charp, 0);
  1437. MODULE_PARM_DESC(mode_option, "Initial video mode e.g. '648x480-8@60'");
  1438. module_param(nomtrr, bool, 0);
  1439. MODULE_PARM_DESC(nomtrr, "Disable MTRR support (default: enabled)");
  1440. module_init(tdfxfb_init);
  1441. module_exit(tdfxfb_exit);