tdfxfb.c 38 KB

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