sstfb.c 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534
  1. /*
  2. * linux/drivers/video/sstfb.c -- voodoo graphics frame buffer
  3. *
  4. * Copyright (c) 2000-2002 Ghozlane Toumi <gtoumi@laposte.net>
  5. *
  6. * Created 15 Jan 2000 by Ghozlane Toumi
  7. *
  8. * Contributions (and many thanks) :
  9. *
  10. * 03/2001 James Simmons <jsimmons@infradead.org>
  11. * 04/2001 Paul Mundt <lethal@chaoticdreams.org>
  12. * 05/2001 Urs Ganse <ursg@uni.de>
  13. * (initial work on voodoo2 port, interlace)
  14. * 09/2002 Helge Deller <deller@gmx.de>
  15. * (enable driver on big-endian machines (hppa), ioctl fixes)
  16. * 12/2002 Helge Deller <deller@gmx.de>
  17. * (port driver to new frambuffer infrastructure)
  18. * 01/2003 Helge Deller <deller@gmx.de>
  19. * (initial work on fb hardware acceleration for voodoo2)
  20. * 08/2006 Alan Cox <alan@redhat.com>
  21. * Remove never finished and bogus 24/32bit support
  22. * Clean up macro abuse
  23. * Minor tidying for format.
  24. * 12/2006 Helge Deller <deller@gmx.de>
  25. * add /sys/class/graphics/fbX/vgapass sysfs-interface
  26. * add module option "mode_option" to set initial screen mode
  27. * use fbdev default videomode database
  28. * remove debug functions from ioctl
  29. */
  30. /*
  31. * The voodoo1 has the following memory mapped address space:
  32. * 0x000000 - 0x3fffff : registers (4MB)
  33. * 0x400000 - 0x7fffff : linear frame buffer (4MB)
  34. * 0x800000 - 0xffffff : texture memory (8MB)
  35. */
  36. /*
  37. * misc notes, TODOs, toASKs, and deep thoughts
  38. -TODO: at one time or another test that the mode is acceptable by the monitor
  39. -ASK: Can I choose different ordering for the color bitfields (rgba argb ...)
  40. which one should i use ? is there any preferred one ? It seems ARGB is
  41. the one ...
  42. -TODO: in set_var check the validity of timings (hsync vsync)...
  43. -TODO: check and recheck the use of sst_wait_idle : we don't flush the fifo via
  44. a nop command. so it's ok as long as the commands we pass don't go
  45. through the fifo. warning: issuing a nop command seems to need pci_fifo
  46. -FIXME: in case of failure in the init sequence, be sure we return to a safe
  47. state.
  48. - FIXME: Use accelerator for 2D scroll
  49. -FIXME: 4MB boards have banked memory (FbiInit2 bits 1 & 20)
  50. */
  51. /*
  52. * debug info
  53. * SST_DEBUG : enable debugging
  54. * SST_DEBUG_REG : debug registers
  55. * 0 : no debug
  56. * 1 : dac calls, [un]set_bits, FbiInit
  57. * 2 : insane debug level (log every register read/write)
  58. * SST_DEBUG_FUNC : functions
  59. * 0 : no debug
  60. * 1 : function call / debug ioctl
  61. * 2 : variables
  62. * 3 : flood . you don't want to do that. trust me.
  63. * SST_DEBUG_VAR : debug display/var structs
  64. * 0 : no debug
  65. * 1 : dumps display, fb_var
  66. *
  67. * sstfb specific ioctls:
  68. * toggle vga (0x46db) : toggle vga_pass_through
  69. */
  70. #undef SST_DEBUG
  71. /*
  72. * Includes
  73. */
  74. #include <linux/string.h>
  75. #include <linux/kernel.h>
  76. #include <linux/module.h>
  77. #include <linux/fb.h>
  78. #include <linux/pci.h>
  79. #include <linux/delay.h>
  80. #include <linux/init.h>
  81. #include <asm/io.h>
  82. #include <linux/uaccess.h>
  83. #include <video/sstfb.h>
  84. /* initialized by setup */
  85. static int vgapass; /* enable VGA passthrough cable */
  86. static int mem; /* mem size in MB, 0 = autodetect */
  87. static int clipping = 1; /* use clipping (slower, safer) */
  88. static int gfxclk; /* force FBI freq in Mhz . Dangerous */
  89. static int slowpci; /* slow PCI settings */
  90. /*
  91. Possible default video modes: 800x600@60, 640x480@75, 1024x768@76, 640x480@60
  92. */
  93. #define DEFAULT_VIDEO_MODE "640x480@60"
  94. static char *mode_option __devinitdata = DEFAULT_VIDEO_MODE;
  95. enum {
  96. ID_VOODOO1 = 0,
  97. ID_VOODOO2 = 1,
  98. };
  99. #define IS_VOODOO2(par) ((par)->type == ID_VOODOO2)
  100. static struct sst_spec voodoo_spec[] __devinitdata = {
  101. { .name = "Voodoo Graphics", .default_gfx_clock = 50000, .max_gfxclk = 60 },
  102. { .name = "Voodoo2", .default_gfx_clock = 75000, .max_gfxclk = 85 },
  103. };
  104. /*
  105. * debug functions
  106. */
  107. #if (SST_DEBUG_REG > 0)
  108. static void sst_dbg_print_read_reg(u32 reg, u32 val) {
  109. const char *regname;
  110. switch (reg) {
  111. case FBIINIT0: regname = "FbiInit0"; break;
  112. case FBIINIT1: regname = "FbiInit1"; break;
  113. case FBIINIT2: regname = "FbiInit2"; break;
  114. case FBIINIT3: regname = "FbiInit3"; break;
  115. case FBIINIT4: regname = "FbiInit4"; break;
  116. case FBIINIT5: regname = "FbiInit5"; break;
  117. case FBIINIT6: regname = "FbiInit6"; break;
  118. default: regname = NULL; break;
  119. }
  120. if (regname == NULL)
  121. r_ddprintk("sst_read(%#x): %#x\n", reg, val);
  122. else
  123. r_dprintk(" sst_read(%s): %#x\n", regname, val);
  124. }
  125. static void sst_dbg_print_write_reg(u32 reg, u32 val) {
  126. const char *regname;
  127. switch (reg) {
  128. case FBIINIT0: regname = "FbiInit0"; break;
  129. case FBIINIT1: regname = "FbiInit1"; break;
  130. case FBIINIT2: regname = "FbiInit2"; break;
  131. case FBIINIT3: regname = "FbiInit3"; break;
  132. case FBIINIT4: regname = "FbiInit4"; break;
  133. case FBIINIT5: regname = "FbiInit5"; break;
  134. case FBIINIT6: regname = "FbiInit6"; break;
  135. default: regname = NULL; break;
  136. }
  137. if (regname == NULL)
  138. r_ddprintk("sst_write(%#x, %#x)\n", reg, val);
  139. else
  140. r_dprintk(" sst_write(%s, %#x)\n", regname, val);
  141. }
  142. #else /* (SST_DEBUG_REG > 0) */
  143. # define sst_dbg_print_read_reg(reg, val) do {} while(0)
  144. # define sst_dbg_print_write_reg(reg, val) do {} while(0)
  145. #endif /* (SST_DEBUG_REG > 0) */
  146. /*
  147. * hardware access functions
  148. */
  149. /* register access */
  150. #define sst_read(reg) __sst_read(par->mmio_vbase, reg)
  151. #define sst_write(reg,val) __sst_write(par->mmio_vbase, reg, val)
  152. #define sst_set_bits(reg,val) __sst_set_bits(par->mmio_vbase, reg, val)
  153. #define sst_unset_bits(reg,val) __sst_unset_bits(par->mmio_vbase, reg, val)
  154. #define sst_dac_read(reg) __sst_dac_read(par->mmio_vbase, reg)
  155. #define sst_dac_write(reg,val) __sst_dac_write(par->mmio_vbase, reg, val)
  156. #define dac_i_read(reg) __dac_i_read(par->mmio_vbase, reg)
  157. #define dac_i_write(reg,val) __dac_i_write(par->mmio_vbase, reg, val)
  158. static inline u32 __sst_read(u8 __iomem *vbase, u32 reg)
  159. {
  160. u32 ret = readl(vbase + reg);
  161. sst_dbg_print_read_reg(reg, ret);
  162. return ret;
  163. }
  164. static inline void __sst_write(u8 __iomem *vbase, u32 reg, u32 val)
  165. {
  166. sst_dbg_print_write_reg(reg, val);
  167. writel(val, vbase + reg);
  168. }
  169. static inline void __sst_set_bits(u8 __iomem *vbase, u32 reg, u32 val)
  170. {
  171. r_dprintk("sst_set_bits(%#x, %#x)\n", reg, val);
  172. __sst_write(vbase, reg, __sst_read(vbase, reg) | val);
  173. }
  174. static inline void __sst_unset_bits(u8 __iomem *vbase, u32 reg, u32 val)
  175. {
  176. r_dprintk("sst_unset_bits(%#x, %#x)\n", reg, val);
  177. __sst_write(vbase, reg, __sst_read(vbase, reg) & ~val);
  178. }
  179. /*
  180. * wait for the fbi chip. ASK: what happens if the fbi is stuck ?
  181. *
  182. * the FBI is supposed to be ready if we receive 5 time
  183. * in a row a "idle" answer to our requests
  184. */
  185. #define sst_wait_idle() __sst_wait_idle(par->mmio_vbase)
  186. static int __sst_wait_idle(u8 __iomem *vbase)
  187. {
  188. int count = 0;
  189. /* if (doFBINOP) __sst_write(vbase, NOPCMD, 0); */
  190. while(1) {
  191. if (__sst_read(vbase, STATUS) & STATUS_FBI_BUSY) {
  192. f_dddprintk("status: busy\n");
  193. /* FIXME basically, this is a busy wait. maybe not that good. oh well;
  194. * this is a small loop after all.
  195. * Or maybe we should use mdelay() or udelay() here instead ? */
  196. count = 0;
  197. } else {
  198. count++;
  199. f_dddprintk("status: idle(%d)\n", count);
  200. }
  201. if (count >= 5) return 1;
  202. /* XXX do something to avoid hanging the machine if the voodoo is out */
  203. }
  204. }
  205. /* dac access */
  206. /* dac_read should be remaped to FbiInit2 (via the pci reg init_enable) */
  207. static u8 __sst_dac_read(u8 __iomem *vbase, u8 reg)
  208. {
  209. u8 ret;
  210. reg &= 0x07;
  211. __sst_write(vbase, DAC_DATA, ((u32)reg << 8) | DAC_READ_CMD );
  212. __sst_wait_idle(vbase);
  213. /* udelay(10); */
  214. ret = __sst_read(vbase, DAC_READ) & 0xff;
  215. r_dprintk("sst_dac_read(%#x): %#x\n", reg, ret);
  216. return ret;
  217. }
  218. static void __sst_dac_write(u8 __iomem *vbase, u8 reg, u8 val)
  219. {
  220. r_dprintk("sst_dac_write(%#x, %#x)\n", reg, val);
  221. reg &= 0x07;
  222. __sst_write(vbase, DAC_DATA,(((u32)reg << 8)) | (u32)val);
  223. __sst_wait_idle(vbase);
  224. }
  225. /* indexed access to ti/att dacs */
  226. static u32 __dac_i_read(u8 __iomem *vbase, u8 reg)
  227. {
  228. u32 ret;
  229. __sst_dac_write(vbase, DACREG_ADDR_I, reg);
  230. ret = __sst_dac_read(vbase, DACREG_DATA_I);
  231. r_dprintk("sst_dac_read_i(%#x): %#x\n", reg, ret);
  232. return ret;
  233. }
  234. static void __dac_i_write(u8 __iomem *vbase, u8 reg,u8 val)
  235. {
  236. r_dprintk("sst_dac_write_i(%#x, %#x)\n", reg, val);
  237. __sst_dac_write(vbase, DACREG_ADDR_I, reg);
  238. __sst_dac_write(vbase, DACREG_DATA_I, val);
  239. }
  240. /* compute the m,n,p , returns the real freq
  241. * (ics datasheet : N <-> N1 , P <-> N2)
  242. *
  243. * Fout= Fref * (M+2)/( 2^P * (N+2))
  244. * we try to get close to the asked freq
  245. * with P as high, and M as low as possible
  246. * range:
  247. * ti/att : 0 <= M <= 255; 0 <= P <= 3; 0<= N <= 63
  248. * ics : 1 <= M <= 127; 0 <= P <= 3; 1<= N <= 31
  249. * we'll use the lowest limitation, should be precise enouth
  250. */
  251. static int sst_calc_pll(const int freq, int *freq_out, struct pll_timing *t)
  252. {
  253. int m, m2, n, p, best_err, fout;
  254. int best_n = -1;
  255. int best_m = -1;
  256. best_err = freq;
  257. p = 3;
  258. /* f * 2^P = vco should be less than VCOmax ~ 250 MHz for ics*/
  259. while (((1 << p) * freq > VCO_MAX) && (p >= 0))
  260. p--;
  261. if (p == -1)
  262. return -EINVAL;
  263. for (n = 1; n < 32; n++) {
  264. /* calc 2 * m so we can round it later*/
  265. m2 = (2 * freq * (1 << p) * (n + 2) ) / DAC_FREF - 4 ;
  266. m = (m2 % 2 ) ? m2/2+1 : m2/2 ;
  267. if (m >= 128)
  268. break;
  269. fout = (DAC_FREF * (m + 2)) / ((1 << p) * (n + 2));
  270. if ((abs(fout - freq) < best_err) && (m > 0)) {
  271. best_n = n;
  272. best_m = m;
  273. best_err = abs(fout - freq);
  274. /* we get the lowest m , allowing 0.5% error in freq*/
  275. if (200*best_err < freq) break;
  276. }
  277. }
  278. if (best_n == -1) /* unlikely, but who knows ? */
  279. return -EINVAL;
  280. t->p = p;
  281. t->n = best_n;
  282. t->m = best_m;
  283. *freq_out = (DAC_FREF * (t->m + 2)) / ((1 << t->p) * (t->n + 2));
  284. f_ddprintk ("m: %d, n: %d, p: %d, F: %dKhz\n",
  285. t->m, t->n, t->p, *freq_out);
  286. return 0;
  287. }
  288. /*
  289. * clear lfb screen
  290. */
  291. static void sstfb_clear_screen(struct fb_info *info)
  292. {
  293. /* clear screen */
  294. fb_memset(info->screen_base, 0, info->fix.smem_len);
  295. }
  296. /**
  297. * sstfb_check_var - Optional function. Validates a var passed in.
  298. * @var: frame buffer variable screen structure
  299. * @info: frame buffer structure that represents a single frame buffer
  300. *
  301. * Limit to the abilities of a single chip as SLI is not supported
  302. * by this driver.
  303. */
  304. static int sstfb_check_var(struct fb_var_screeninfo *var,
  305. struct fb_info *info)
  306. {
  307. struct sstfb_par *par = info->par;
  308. int hSyncOff = var->xres + var->right_margin + var->left_margin;
  309. int vSyncOff = var->yres + var->lower_margin + var->upper_margin;
  310. int vBackPorch = var->left_margin, yDim = var->yres;
  311. int vSyncOn = var->vsync_len;
  312. int tiles_in_X, real_length;
  313. unsigned int freq;
  314. if (sst_calc_pll(PICOS2KHZ(var->pixclock), &freq, &par->pll)) {
  315. printk(KERN_ERR "sstfb: Pixclock at %ld KHZ out of range\n",
  316. PICOS2KHZ(var->pixclock));
  317. return -EINVAL;
  318. }
  319. var->pixclock = KHZ2PICOS(freq);
  320. if (var->vmode & FB_VMODE_INTERLACED)
  321. vBackPorch += (vBackPorch % 2);
  322. if (var->vmode & FB_VMODE_DOUBLE) {
  323. vBackPorch <<= 1;
  324. yDim <<=1;
  325. vSyncOn <<=1;
  326. vSyncOff <<=1;
  327. }
  328. switch (var->bits_per_pixel) {
  329. case 0 ... 16 :
  330. var->bits_per_pixel = 16;
  331. break;
  332. default :
  333. printk(KERN_ERR "sstfb: Unsupported bpp %d\n", var->bits_per_pixel);
  334. return -EINVAL;
  335. }
  336. /* validity tests */
  337. if (var->xres <= 1 || yDim <= 0 || var->hsync_len <= 1 ||
  338. hSyncOff <= 1 || var->left_margin <= 2 || vSyncOn <= 0 ||
  339. vSyncOff <= 0 || vBackPorch <= 0) {
  340. return -EINVAL;
  341. }
  342. if (IS_VOODOO2(par)) {
  343. /* Voodoo 2 limits */
  344. tiles_in_X = (var->xres + 63 ) / 64 * 2;
  345. if (var->xres > POW2(11) || yDim >= POW2(11)) {
  346. printk(KERN_ERR "sstfb: Unsupported resolution %dx%d\n",
  347. var->xres, var->yres);
  348. return -EINVAL;
  349. }
  350. if (var->hsync_len > POW2(9) || hSyncOff > POW2(11) ||
  351. var->left_margin - 2 >= POW2(9) || vSyncOn >= POW2(13) ||
  352. vSyncOff >= POW2(13) || vBackPorch >= POW2(9) ||
  353. tiles_in_X >= POW2(6) || tiles_in_X <= 0) {
  354. printk(KERN_ERR "sstfb: Unsupported timings\n");
  355. return -EINVAL;
  356. }
  357. } else {
  358. /* Voodoo limits */
  359. tiles_in_X = (var->xres + 63 ) / 64;
  360. if (var->vmode) {
  361. printk(KERN_ERR "sstfb: Interlace/doublescan not supported %#x\n",
  362. var->vmode);
  363. return -EINVAL;
  364. }
  365. if (var->xres > POW2(10) || var->yres >= POW2(10)) {
  366. printk(KERN_ERR "sstfb: Unsupported resolution %dx%d\n",
  367. var->xres, var->yres);
  368. return -EINVAL;
  369. }
  370. if (var->hsync_len > POW2(8) || hSyncOff - 1 > POW2(10) ||
  371. var->left_margin - 2 >= POW2(8) || vSyncOn >= POW2(12) ||
  372. vSyncOff >= POW2(12) || vBackPorch >= POW2(8) ||
  373. tiles_in_X >= POW2(4) || tiles_in_X <= 0) {
  374. printk(KERN_ERR "sstfb: Unsupported timings\n");
  375. return -EINVAL;
  376. }
  377. }
  378. /* it seems that the fbi uses tiles of 64x16 pixels to "map" the mem */
  379. /* FIXME: i don't like this... looks wrong */
  380. real_length = tiles_in_X * (IS_VOODOO2(par) ? 32 : 64 )
  381. * ((var->bits_per_pixel == 16) ? 2 : 4);
  382. if (real_length * yDim > info->fix.smem_len) {
  383. printk(KERN_ERR "sstfb: Not enough video memory\n");
  384. return -ENOMEM;
  385. }
  386. var->sync &= (FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT);
  387. var->vmode &= (FB_VMODE_INTERLACED | FB_VMODE_DOUBLE);
  388. var->xoffset = 0;
  389. var->yoffset = 0;
  390. var->height = -1;
  391. var->width = -1;
  392. /*
  393. * correct the color bit fields
  394. */
  395. /* var->{red|green|blue}.msb_right = 0; */
  396. switch (var->bits_per_pixel) {
  397. case 16: /* RGB 565 LfbMode 0 */
  398. var->red.length = 5;
  399. var->green.length = 6;
  400. var->blue.length = 5;
  401. var->transp.length = 0;
  402. var->red.offset = 11;
  403. var->green.offset = 5;
  404. var->blue.offset = 0;
  405. var->transp.offset = 0;
  406. break;
  407. default:
  408. return -EINVAL;
  409. }
  410. return 0;
  411. }
  412. /**
  413. * sstfb_set_par - Optional function. Alters the hardware state.
  414. * @info: frame buffer structure that represents a single frame buffer
  415. */
  416. static int sstfb_set_par(struct fb_info *info)
  417. {
  418. struct sstfb_par *par = info->par;
  419. u32 lfbmode, fbiinit1, fbiinit2, fbiinit3, fbiinit5, fbiinit6=0;
  420. struct pci_dev *sst_dev = par->dev;
  421. unsigned int freq;
  422. int ntiles;
  423. par->hSyncOff = info->var.xres + info->var.right_margin + info->var.left_margin;
  424. par->yDim = info->var.yres;
  425. par->vSyncOn = info->var.vsync_len;
  426. par->vSyncOff = info->var.yres + info->var.lower_margin + info->var.upper_margin;
  427. par->vBackPorch = info->var.upper_margin;
  428. /* We need par->pll */
  429. sst_calc_pll(PICOS2KHZ(info->var.pixclock), &freq, &par->pll);
  430. if (info->var.vmode & FB_VMODE_INTERLACED)
  431. par->vBackPorch += (par->vBackPorch % 2);
  432. if (info->var.vmode & FB_VMODE_DOUBLE) {
  433. par->vBackPorch <<= 1;
  434. par->yDim <<=1;
  435. par->vSyncOn <<=1;
  436. par->vSyncOff <<=1;
  437. }
  438. if (IS_VOODOO2(par)) {
  439. /* voodoo2 has 32 pixel wide tiles , BUT strange things
  440. happen with odd number of tiles */
  441. par->tiles_in_X = (info->var.xres + 63 ) / 64 * 2;
  442. } else {
  443. /* voodoo1 has 64 pixels wide tiles. */
  444. par->tiles_in_X = (info->var.xres + 63 ) / 64;
  445. }
  446. f_ddprintk("hsync_len hSyncOff vsync_len vSyncOff\n");
  447. f_ddprintk("%-7d %-8d %-7d %-8d\n",
  448. info->var.hsync_len, par->hSyncOff,
  449. par->vSyncOn, par->vSyncOff);
  450. f_ddprintk("left_margin upper_margin xres yres Freq\n");
  451. f_ddprintk("%-10d %-10d %-4d %-4d %-8ld\n",
  452. info->var.left_margin, info->var.upper_margin,
  453. info->var.xres, info->var.yres, PICOS2KHZ(info->var.pixclock));
  454. sst_write(NOPCMD, 0);
  455. sst_wait_idle();
  456. pci_write_config_dword(sst_dev, PCI_INIT_ENABLE, PCI_EN_INIT_WR);
  457. sst_set_bits(FBIINIT1, VIDEO_RESET);
  458. sst_set_bits(FBIINIT0, FBI_RESET | FIFO_RESET);
  459. sst_unset_bits(FBIINIT2, EN_DRAM_REFRESH);
  460. sst_wait_idle();
  461. /*sst_unset_bits (FBIINIT0, FBI_RESET); / reenable FBI ? */
  462. sst_write(BACKPORCH, par->vBackPorch << 16 | (info->var.left_margin - 2));
  463. sst_write(VIDEODIMENSIONS, par->yDim << 16 | (info->var.xres - 1));
  464. sst_write(HSYNC, (par->hSyncOff - 1) << 16 | (info->var.hsync_len - 1));
  465. sst_write(VSYNC, par->vSyncOff << 16 | par->vSyncOn);
  466. fbiinit2 = sst_read(FBIINIT2);
  467. fbiinit3 = sst_read(FBIINIT3);
  468. /* everything is reset. we enable fbiinit2/3 remap : dac access ok */
  469. pci_write_config_dword(sst_dev, PCI_INIT_ENABLE,
  470. PCI_EN_INIT_WR | PCI_REMAP_DAC );
  471. par->dac_sw.set_vidmod(info, info->var.bits_per_pixel);
  472. /* set video clock */
  473. par->dac_sw.set_pll(info, &par->pll, VID_CLOCK);
  474. /* disable fbiinit2/3 remap */
  475. pci_write_config_dword(sst_dev, PCI_INIT_ENABLE,
  476. PCI_EN_INIT_WR);
  477. /* restore fbiinit2/3 */
  478. sst_write(FBIINIT2,fbiinit2);
  479. sst_write(FBIINIT3,fbiinit3);
  480. fbiinit1 = (sst_read(FBIINIT1) & VIDEO_MASK)
  481. | EN_DATA_OE
  482. | EN_BLANK_OE
  483. | EN_HVSYNC_OE
  484. | EN_DCLK_OE
  485. /* | (15 << TILES_IN_X_SHIFT) */
  486. | SEL_INPUT_VCLK_2X
  487. /* | (2 << VCLK_2X_SEL_DEL_SHIFT)
  488. | (2 << VCLK_DEL_SHIFT) */;
  489. /* try with vclk_in_delay =0 (bits 29:30) , vclk_out_delay =0 (bits(27:28)
  490. in (near) future set them accordingly to revision + resolution (cf glide)
  491. first understand what it stands for :)
  492. FIXME: there are some artefacts... check for the vclk_in_delay
  493. lets try with 6ns delay in both vclk_out & in...
  494. doh... they're still there :\
  495. */
  496. ntiles = par->tiles_in_X;
  497. if (IS_VOODOO2(par)) {
  498. fbiinit1 |= ((ntiles & 0x20) >> 5) << TILES_IN_X_MSB_SHIFT
  499. | ((ntiles & 0x1e) >> 1) << TILES_IN_X_SHIFT;
  500. /* as the only value of importance for us in fbiinit6 is tiles in X (lsb),
  501. and as reading fbinit 6 will return crap (see FBIINIT6_DEFAULT) we just
  502. write our value. BTW due to the dac unable to read odd number of tiles, this
  503. field is always null ... */
  504. fbiinit6 = (ntiles & 0x1) << TILES_IN_X_LSB_SHIFT;
  505. }
  506. else
  507. fbiinit1 |= ntiles << TILES_IN_X_SHIFT;
  508. switch (info->var.bits_per_pixel) {
  509. case 16:
  510. fbiinit1 |= SEL_SOURCE_VCLK_2X_SEL;
  511. break;
  512. default:
  513. return -EINVAL;
  514. }
  515. sst_write(FBIINIT1, fbiinit1);
  516. if (IS_VOODOO2(par)) {
  517. sst_write(FBIINIT6, fbiinit6);
  518. fbiinit5=sst_read(FBIINIT5) & FBIINIT5_MASK ;
  519. if (info->var.vmode & FB_VMODE_INTERLACED)
  520. fbiinit5 |= INTERLACE;
  521. if (info->var.vmode & FB_VMODE_DOUBLE)
  522. fbiinit5 |= VDOUBLESCAN;
  523. if (info->var.sync & FB_SYNC_HOR_HIGH_ACT)
  524. fbiinit5 |= HSYNC_HIGH;
  525. if (info->var.sync & FB_SYNC_VERT_HIGH_ACT)
  526. fbiinit5 |= VSYNC_HIGH;
  527. sst_write(FBIINIT5, fbiinit5);
  528. }
  529. sst_wait_idle();
  530. sst_unset_bits(FBIINIT1, VIDEO_RESET);
  531. sst_unset_bits(FBIINIT0, FBI_RESET | FIFO_RESET);
  532. sst_set_bits(FBIINIT2, EN_DRAM_REFRESH);
  533. /* disables fbiinit writes */
  534. pci_write_config_dword(sst_dev, PCI_INIT_ENABLE, PCI_EN_FIFO_WR);
  535. /* set lfbmode : set mode + front buffer for reads/writes
  536. + disable pipeline */
  537. switch (info->var.bits_per_pixel) {
  538. case 16:
  539. lfbmode = LFB_565;
  540. break;
  541. default:
  542. return -EINVAL;
  543. }
  544. #if defined(__BIG_ENDIAN)
  545. /* Enable byte-swizzle functionality in hardware.
  546. * With this enabled, all our read- and write-accesses to
  547. * the voodoo framebuffer can be done in native format, and
  548. * the hardware will automatically convert it to little-endian.
  549. * - tested on HP-PARISC, Helge Deller <deller@gmx.de> */
  550. lfbmode |= ( LFB_WORD_SWIZZLE_WR | LFB_BYTE_SWIZZLE_WR |
  551. LFB_WORD_SWIZZLE_RD | LFB_BYTE_SWIZZLE_RD );
  552. #endif
  553. if (clipping) {
  554. sst_write(LFBMODE, lfbmode | EN_PXL_PIPELINE);
  555. /*
  556. * Set "clipping" dimensions. If clipping is disabled and
  557. * writes to offscreen areas of the framebuffer are performed,
  558. * the "behaviour is undefined" (_very_ undefined) - Urs
  559. */
  560. /* btw, it requires enabling pixel pipeline in LFBMODE .
  561. off screen read/writes will just wrap and read/print pixels
  562. on screen. Ugly but not that dangerous */
  563. f_ddprintk("setting clipping dimensions 0..%d, 0..%d\n",
  564. info->var.xres - 1, par->yDim - 1);
  565. sst_write(CLIP_LEFT_RIGHT, info->var.xres);
  566. sst_write(CLIP_LOWY_HIGHY, par->yDim);
  567. sst_set_bits(FBZMODE, EN_CLIPPING | EN_RGB_WRITE);
  568. } else {
  569. /* no clipping : direct access, no pipeline */
  570. sst_write(LFBMODE, lfbmode);
  571. }
  572. return 0;
  573. }
  574. /**
  575. * sstfb_setcolreg - Optional function. Sets a color register.
  576. * @regno: hardware colormap register
  577. * @red: frame buffer colormap structure
  578. * @green: The green value which can be up to 16 bits wide
  579. * @blue: The blue value which can be up to 16 bits wide.
  580. * @transp: If supported the alpha value which can be up to 16 bits wide.
  581. * @info: frame buffer info structure
  582. */
  583. static int sstfb_setcolreg(u_int regno, u_int red, u_int green, u_int blue,
  584. u_int transp, struct fb_info *info)
  585. {
  586. struct sstfb_par *par = info->par;
  587. u32 col;
  588. f_dddprintk("sstfb_setcolreg\n");
  589. f_dddprintk("%-2d rgbt: %#x, %#x, %#x, %#x\n",
  590. regno, red, green, blue, transp);
  591. if (regno > 15)
  592. return 0;
  593. red >>= (16 - info->var.red.length);
  594. green >>= (16 - info->var.green.length);
  595. blue >>= (16 - info->var.blue.length);
  596. transp >>= (16 - info->var.transp.length);
  597. col = (red << info->var.red.offset)
  598. | (green << info->var.green.offset)
  599. | (blue << info->var.blue.offset)
  600. | (transp << info->var.transp.offset);
  601. par->palette[regno] = col;
  602. return 0;
  603. }
  604. static void sstfb_setvgapass( struct fb_info *info, int enable )
  605. {
  606. struct sstfb_par *par = info->par;
  607. struct pci_dev *sst_dev = par->dev;
  608. u32 fbiinit0, tmp;
  609. enable = enable ? 1:0;
  610. if (par->vgapass == enable)
  611. return;
  612. par->vgapass = enable;
  613. pci_read_config_dword(sst_dev, PCI_INIT_ENABLE, &tmp);
  614. pci_write_config_dword(sst_dev, PCI_INIT_ENABLE,
  615. tmp | PCI_EN_INIT_WR );
  616. fbiinit0 = sst_read (FBIINIT0);
  617. if (par->vgapass) {
  618. sst_write(FBIINIT0, fbiinit0 & ~DIS_VGA_PASSTHROUGH);
  619. printk(KERN_INFO "fb%d: Enabling VGA pass-through\n", info->node );
  620. } else {
  621. sst_write(FBIINIT0, fbiinit0 | DIS_VGA_PASSTHROUGH);
  622. printk(KERN_INFO "fb%d: Disabling VGA pass-through\n", info->node );
  623. }
  624. pci_write_config_dword(sst_dev, PCI_INIT_ENABLE, tmp);
  625. }
  626. static ssize_t store_vgapass(struct device *device, struct device_attribute *attr,
  627. const char *buf, size_t count)
  628. {
  629. struct fb_info *info = dev_get_drvdata(device);
  630. char ** last = NULL;
  631. int val;
  632. val = simple_strtoul(buf, last, 0);
  633. sstfb_setvgapass(info, val);
  634. return count;
  635. }
  636. static ssize_t show_vgapass(struct device *device, struct device_attribute *attr,
  637. char *buf)
  638. {
  639. struct fb_info *info = dev_get_drvdata(device);
  640. struct sstfb_par *par = info->par;
  641. return snprintf(buf, PAGE_SIZE, "%d\n", par->vgapass);
  642. }
  643. static struct device_attribute device_attrs[] = {
  644. __ATTR(vgapass, S_IRUGO|S_IWUSR, show_vgapass, store_vgapass)
  645. };
  646. static int sstfb_ioctl(struct fb_info *info, unsigned int cmd,
  647. unsigned long arg)
  648. {
  649. struct sstfb_par *par;
  650. u32 val;
  651. switch (cmd) {
  652. /* set/get VGA pass_through mode */
  653. case SSTFB_SET_VGAPASS:
  654. if (copy_from_user(&val, (void __user *)arg, sizeof(val)))
  655. return -EFAULT;
  656. sstfb_setvgapass(info, val);
  657. return 0;
  658. case SSTFB_GET_VGAPASS:
  659. par = info->par;
  660. val = par->vgapass;
  661. if (copy_to_user((void __user *)arg, &val, sizeof(val)))
  662. return -EFAULT;
  663. return 0;
  664. }
  665. return -EINVAL;
  666. }
  667. /*
  668. * Screen-to-Screen BitBlt 2D command (for the bmove fb op.) - Voodoo2 only
  669. */
  670. #if 0
  671. static void sstfb_copyarea(struct fb_info *info, const struct fb_copyarea *area)
  672. {
  673. struct sstfb_par *par = info->par;
  674. u32 stride = info->fix.line_length;
  675. if (!IS_VOODOO2(par))
  676. return;
  677. sst_write(BLTSRCBASEADDR, 0);
  678. sst_write(BLTDSTBASEADDR, 0);
  679. sst_write(BLTROP, BLTROP_COPY);
  680. sst_write(BLTXYSTRIDES, stride | (stride << 16));
  681. sst_write(BLTSRCXY, area->sx | (area->sy << 16));
  682. sst_write(BLTDSTXY, area->dx | (area->dy << 16));
  683. sst_write(BLTSIZE, area->width | (area->height << 16));
  684. sst_write(BLTCOMMAND, BLT_SCR2SCR_BITBLT | LAUNCH_BITBLT |
  685. (BLT_16BPP_FMT << 3) /* | BIT(14) */ | BIT(15) );
  686. sst_wait_idle();
  687. }
  688. #endif
  689. /*
  690. * FillRect 2D command (solidfill or invert (via ROP_XOR)) - Voodoo2 only
  691. */
  692. #if 0
  693. static void sstfb_fillrect(struct fb_info *info, const struct fb_fillrect *rect)
  694. {
  695. struct sstfb_par *par = info->par;
  696. u32 stride = info->fix.line_length;
  697. if (!IS_VOODOO2(par))
  698. return;
  699. sst_write(BLTCLIPX, info->var.xres);
  700. sst_write(BLTCLIPY, info->var.yres);
  701. sst_write(BLTDSTBASEADDR, 0);
  702. sst_write(BLTCOLOR, rect->color);
  703. sst_write(BLTROP, rect->rop == ROP_COPY ? BLTROP_COPY : BLTROP_XOR);
  704. sst_write(BLTXYSTRIDES, stride | (stride << 16));
  705. sst_write(BLTDSTXY, rect->dx | (rect->dy << 16));
  706. sst_write(BLTSIZE, rect->width | (rect->height << 16));
  707. sst_write(BLTCOMMAND, BLT_RECFILL_BITBLT | LAUNCH_BITBLT
  708. | (BLT_16BPP_FMT << 3) /* | BIT(14) */ | BIT(15) | BIT(16) );
  709. sst_wait_idle();
  710. }
  711. #endif
  712. /*
  713. * get lfb size
  714. */
  715. static int __devinit sst_get_memsize(struct fb_info *info, __u32 *memsize)
  716. {
  717. u8 __iomem *fbbase_virt = info->screen_base;
  718. /* force memsize */
  719. if (mem >= 1 && mem <= 4) {
  720. *memsize = (mem * 0x100000);
  721. printk(KERN_INFO "supplied memsize: %#x\n", *memsize);
  722. return 1;
  723. }
  724. writel(0xdeadbeef, fbbase_virt);
  725. writel(0xdeadbeef, fbbase_virt+0x100000);
  726. writel(0xdeadbeef, fbbase_virt+0x200000);
  727. f_ddprintk("0MB: %#x, 1MB: %#x, 2MB: %#x\n",
  728. readl(fbbase_virt), readl(fbbase_virt + 0x100000),
  729. readl(fbbase_virt + 0x200000));
  730. writel(0xabcdef01, fbbase_virt);
  731. f_ddprintk("0MB: %#x, 1MB: %#x, 2MB: %#x\n",
  732. readl(fbbase_virt), readl(fbbase_virt + 0x100000),
  733. readl(fbbase_virt + 0x200000));
  734. /* checks for 4mb lfb, then 2, then defaults to 1 */
  735. if (readl(fbbase_virt + 0x200000) == 0xdeadbeef)
  736. *memsize = 0x400000;
  737. else if (readl(fbbase_virt + 0x100000) == 0xdeadbeef)
  738. *memsize = 0x200000;
  739. else
  740. *memsize = 0x100000;
  741. f_ddprintk("detected memsize: %dMB\n", *memsize >> 20);
  742. return 1;
  743. }
  744. /*
  745. * DAC detection routines
  746. */
  747. /* fbi should be idle, and fifo emty and mem disabled */
  748. /* supposed to detect AT&T ATT20C409 and Ti TVP3409 ramdacs */
  749. static int __devinit sst_detect_att(struct fb_info *info)
  750. {
  751. struct sstfb_par *par = info->par;
  752. int i, mir, dir;
  753. for (i = 0; i < 3; i++) {
  754. sst_dac_write(DACREG_WMA, 0); /* backdoor */
  755. sst_dac_read(DACREG_RMR); /* read 4 times RMR */
  756. sst_dac_read(DACREG_RMR);
  757. sst_dac_read(DACREG_RMR);
  758. sst_dac_read(DACREG_RMR);
  759. /* the fifth time, CR0 is read */
  760. sst_dac_read(DACREG_RMR);
  761. /* the 6th, manufacturer id register */
  762. mir = sst_dac_read(DACREG_RMR);
  763. /*the 7th, device ID register */
  764. dir = sst_dac_read(DACREG_RMR);
  765. f_ddprintk("mir: %#x, dir: %#x\n", mir, dir);
  766. if (mir == DACREG_MIR_ATT && dir == DACREG_DIR_ATT) {
  767. return 1;
  768. }
  769. }
  770. return 0;
  771. }
  772. static int __devinit sst_detect_ti(struct fb_info *info)
  773. {
  774. struct sstfb_par *par = info->par;
  775. int i, mir, dir;
  776. for (i = 0; i<3; i++) {
  777. sst_dac_write(DACREG_WMA, 0); /* backdoor */
  778. sst_dac_read(DACREG_RMR); /* read 4 times RMR */
  779. sst_dac_read(DACREG_RMR);
  780. sst_dac_read(DACREG_RMR);
  781. sst_dac_read(DACREG_RMR);
  782. /* the fifth time, CR0 is read */
  783. sst_dac_read(DACREG_RMR);
  784. /* the 6th, manufacturer id register */
  785. mir = sst_dac_read(DACREG_RMR);
  786. /*the 7th, device ID register */
  787. dir = sst_dac_read(DACREG_RMR);
  788. f_ddprintk("mir: %#x, dir: %#x\n", mir, dir);
  789. if ((mir == DACREG_MIR_TI ) && (dir == DACREG_DIR_TI)) {
  790. return 1;
  791. }
  792. }
  793. return 0;
  794. }
  795. /*
  796. * try to detect ICS5342 ramdac
  797. * we get the 1st byte (M value) of preset f1,f7 and fB
  798. * why those 3 ? mmmh... for now, i'll do it the glide way...
  799. * and ask questions later. anyway, it seems that all the freq registers are
  800. * really at their default state (cf specs) so i ask again, why those 3 regs ?
  801. * mmmmh.. it seems that's much more ugly than i thought. we use f0 and fA for
  802. * pll programming, so in fact, we *hope* that the f1, f7 & fB won't be
  803. * touched...
  804. * is it really safe ? how can i reset this ramdac ? geee...
  805. */
  806. static int __devinit sst_detect_ics(struct fb_info *info)
  807. {
  808. struct sstfb_par *par = info->par;
  809. int m_clk0_1, m_clk0_7, m_clk1_b;
  810. int n_clk0_1, n_clk0_7, n_clk1_b;
  811. int i;
  812. for (i = 0; i<5; i++ ) {
  813. sst_dac_write(DACREG_ICS_PLLRMA, 0x1); /* f1 */
  814. m_clk0_1 = sst_dac_read(DACREG_ICS_PLLDATA);
  815. n_clk0_1 = sst_dac_read(DACREG_ICS_PLLDATA);
  816. sst_dac_write(DACREG_ICS_PLLRMA, 0x7); /* f7 */
  817. m_clk0_7 = sst_dac_read(DACREG_ICS_PLLDATA);
  818. n_clk0_7 = sst_dac_read(DACREG_ICS_PLLDATA);
  819. sst_dac_write(DACREG_ICS_PLLRMA, 0xb); /* fB */
  820. m_clk1_b= sst_dac_read(DACREG_ICS_PLLDATA);
  821. n_clk1_b= sst_dac_read(DACREG_ICS_PLLDATA);
  822. f_ddprintk("m_clk0_1: %#x, m_clk0_7: %#x, m_clk1_b: %#x\n",
  823. m_clk0_1, m_clk0_7, m_clk1_b);
  824. f_ddprintk("n_clk0_1: %#x, n_clk0_7: %#x, n_clk1_b: %#x\n",
  825. n_clk0_1, n_clk0_7, n_clk1_b);
  826. if (( m_clk0_1 == DACREG_ICS_PLL_CLK0_1_INI)
  827. && (m_clk0_7 == DACREG_ICS_PLL_CLK0_7_INI)
  828. && (m_clk1_b == DACREG_ICS_PLL_CLK1_B_INI)) {
  829. return 1;
  830. }
  831. }
  832. return 0;
  833. }
  834. /*
  835. * gfx, video, pci fifo should be reset, dram refresh disabled
  836. * see detect_dac
  837. */
  838. static int sst_set_pll_att_ti(struct fb_info *info,
  839. const struct pll_timing *t, const int clock)
  840. {
  841. struct sstfb_par *par = info->par;
  842. u8 cr0, cc;
  843. /* enable indexed mode */
  844. sst_dac_write(DACREG_WMA, 0); /* backdoor */
  845. sst_dac_read(DACREG_RMR); /* 1 time: RMR */
  846. sst_dac_read(DACREG_RMR); /* 2 RMR */
  847. sst_dac_read(DACREG_RMR); /* 3 // */
  848. sst_dac_read(DACREG_RMR); /* 4 // */
  849. cr0 = sst_dac_read(DACREG_RMR); /* 5 CR0 */
  850. sst_dac_write(DACREG_WMA, 0);
  851. sst_dac_read(DACREG_RMR);
  852. sst_dac_read(DACREG_RMR);
  853. sst_dac_read(DACREG_RMR);
  854. sst_dac_read(DACREG_RMR);
  855. sst_dac_write(DACREG_RMR, (cr0 & 0xf0)
  856. | DACREG_CR0_EN_INDEXED
  857. | DACREG_CR0_8BIT
  858. | DACREG_CR0_PWDOWN );
  859. /* so, now we are in indexed mode . dunno if its common, but
  860. i find this way of doing things a little bit weird :p */
  861. udelay(300);
  862. cc = dac_i_read(DACREG_CC_I);
  863. switch (clock) {
  864. case VID_CLOCK:
  865. dac_i_write(DACREG_AC0_I, t->m);
  866. dac_i_write(DACREG_AC1_I, t->p << 6 | t->n);
  867. dac_i_write(DACREG_CC_I,
  868. (cc & 0x0f) | DACREG_CC_CLKA | DACREG_CC_CLKA_C);
  869. break;
  870. case GFX_CLOCK:
  871. dac_i_write(DACREG_BD0_I, t->m);
  872. dac_i_write(DACREG_BD1_I, t->p << 6 | t->n);
  873. dac_i_write(DACREG_CC_I,
  874. (cc & 0xf0) | DACREG_CC_CLKB | DACREG_CC_CLKB_D);
  875. break;
  876. default:
  877. dprintk("%s: wrong clock code '%d'\n",
  878. __func__, clock);
  879. return 0;
  880. }
  881. udelay(300);
  882. /* power up the dac & return to "normal" non-indexed mode */
  883. dac_i_write(DACREG_CR0_I,
  884. cr0 & ~DACREG_CR0_PWDOWN & ~DACREG_CR0_EN_INDEXED);
  885. return 1;
  886. }
  887. static int sst_set_pll_ics(struct fb_info *info,
  888. const struct pll_timing *t, const int clock)
  889. {
  890. struct sstfb_par *par = info->par;
  891. u8 pll_ctrl;
  892. sst_dac_write(DACREG_ICS_PLLRMA, DACREG_ICS_PLL_CTRL);
  893. pll_ctrl = sst_dac_read(DACREG_ICS_PLLDATA);
  894. switch(clock) {
  895. case VID_CLOCK:
  896. sst_dac_write(DACREG_ICS_PLLWMA, 0x0); /* CLK0, f0 */
  897. sst_dac_write(DACREG_ICS_PLLDATA, t->m);
  898. sst_dac_write(DACREG_ICS_PLLDATA, t->p << 5 | t->n);
  899. /* selects freq f0 for clock 0 */
  900. sst_dac_write(DACREG_ICS_PLLWMA, DACREG_ICS_PLL_CTRL);
  901. sst_dac_write(DACREG_ICS_PLLDATA,
  902. (pll_ctrl & 0xd8)
  903. | DACREG_ICS_CLK0
  904. | DACREG_ICS_CLK0_0);
  905. break;
  906. case GFX_CLOCK :
  907. sst_dac_write(DACREG_ICS_PLLWMA, 0xa); /* CLK1, fA */
  908. sst_dac_write(DACREG_ICS_PLLDATA, t->m);
  909. sst_dac_write(DACREG_ICS_PLLDATA, t->p << 5 | t->n);
  910. /* selects freq fA for clock 1 */
  911. sst_dac_write(DACREG_ICS_PLLWMA, DACREG_ICS_PLL_CTRL);
  912. sst_dac_write(DACREG_ICS_PLLDATA,
  913. (pll_ctrl & 0xef) | DACREG_ICS_CLK1_A);
  914. break;
  915. default:
  916. dprintk("%s: wrong clock code '%d'\n",
  917. __func__, clock);
  918. return 0;
  919. }
  920. udelay(300);
  921. return 1;
  922. }
  923. static void sst_set_vidmod_att_ti(struct fb_info *info, const int bpp)
  924. {
  925. struct sstfb_par *par = info->par;
  926. u8 cr0;
  927. sst_dac_write(DACREG_WMA, 0); /* backdoor */
  928. sst_dac_read(DACREG_RMR); /* read 4 times RMR */
  929. sst_dac_read(DACREG_RMR);
  930. sst_dac_read(DACREG_RMR);
  931. sst_dac_read(DACREG_RMR);
  932. /* the fifth time, CR0 is read */
  933. cr0 = sst_dac_read(DACREG_RMR);
  934. sst_dac_write(DACREG_WMA, 0); /* backdoor */
  935. sst_dac_read(DACREG_RMR); /* read 4 times RMR */
  936. sst_dac_read(DACREG_RMR);
  937. sst_dac_read(DACREG_RMR);
  938. sst_dac_read(DACREG_RMR);
  939. /* cr0 */
  940. switch(bpp) {
  941. case 16:
  942. sst_dac_write(DACREG_RMR, (cr0 & 0x0f) | DACREG_CR0_16BPP);
  943. break;
  944. default:
  945. dprintk("%s: bad depth '%u'\n", __func__, bpp);
  946. break;
  947. }
  948. }
  949. static void sst_set_vidmod_ics(struct fb_info *info, const int bpp)
  950. {
  951. struct sstfb_par *par = info->par;
  952. switch(bpp) {
  953. case 16:
  954. sst_dac_write(DACREG_ICS_CMD, DACREG_ICS_CMD_16BPP);
  955. break;
  956. default:
  957. dprintk("%s: bad depth '%u'\n", __func__, bpp);
  958. break;
  959. }
  960. }
  961. /*
  962. * detect dac type
  963. * prerequisite : write to FbiInitx enabled, video and fbi and pci fifo reset,
  964. * dram refresh disabled, FbiInit remaped.
  965. * TODO: mmh.. maybe i should put the "prerequisite" in the func ...
  966. */
  967. static struct dac_switch dacs[] __devinitdata = {
  968. { .name = "TI TVP3409",
  969. .detect = sst_detect_ti,
  970. .set_pll = sst_set_pll_att_ti,
  971. .set_vidmod = sst_set_vidmod_att_ti },
  972. { .name = "AT&T ATT20C409",
  973. .detect = sst_detect_att,
  974. .set_pll = sst_set_pll_att_ti,
  975. .set_vidmod = sst_set_vidmod_att_ti },
  976. { .name = "ICS ICS5342",
  977. .detect = sst_detect_ics,
  978. .set_pll = sst_set_pll_ics,
  979. .set_vidmod = sst_set_vidmod_ics },
  980. };
  981. static int __devinit sst_detect_dactype(struct fb_info *info, struct sstfb_par *par)
  982. {
  983. int i, ret = 0;
  984. for (i = 0; i < ARRAY_SIZE(dacs); i++) {
  985. ret = dacs[i].detect(info);
  986. if (ret)
  987. break;
  988. }
  989. if (!ret)
  990. return 0;
  991. f_dprintk("%s found %s\n", __func__, dacs[i].name);
  992. par->dac_sw = dacs[i];
  993. return 1;
  994. }
  995. /*
  996. * Internal Routines
  997. */
  998. static int __devinit sst_init(struct fb_info *info, struct sstfb_par *par)
  999. {
  1000. u32 fbiinit0, fbiinit1, fbiinit4;
  1001. struct pci_dev *dev = par->dev;
  1002. struct pll_timing gfx_timings;
  1003. struct sst_spec *spec;
  1004. int Fout;
  1005. int gfx_clock;
  1006. spec = &voodoo_spec[par->type];
  1007. f_ddprintk(" fbiinit0 fbiinit1 fbiinit2 fbiinit3 fbiinit4 "
  1008. " fbiinit6\n");
  1009. f_ddprintk("%0#10x %0#10x %0#10x %0#10x %0#10x %0#10x\n",
  1010. sst_read(FBIINIT0), sst_read(FBIINIT1), sst_read(FBIINIT2),
  1011. sst_read(FBIINIT3), sst_read(FBIINIT4), sst_read(FBIINIT6));
  1012. /* disable video clock */
  1013. pci_write_config_dword(dev, PCI_VCLK_DISABLE, 0);
  1014. /* enable writing to init registers, disable pci fifo */
  1015. pci_write_config_dword(dev, PCI_INIT_ENABLE, PCI_EN_INIT_WR);
  1016. /* reset video */
  1017. sst_set_bits(FBIINIT1, VIDEO_RESET);
  1018. sst_wait_idle();
  1019. /* reset gfx + pci fifo */
  1020. sst_set_bits(FBIINIT0, FBI_RESET | FIFO_RESET);
  1021. sst_wait_idle();
  1022. /* unreset fifo */
  1023. /*sst_unset_bits(FBIINIT0, FIFO_RESET);
  1024. sst_wait_idle();*/
  1025. /* unreset FBI */
  1026. /*sst_unset_bits(FBIINIT0, FBI_RESET);
  1027. sst_wait_idle();*/
  1028. /* disable dram refresh */
  1029. sst_unset_bits(FBIINIT2, EN_DRAM_REFRESH);
  1030. sst_wait_idle();
  1031. /* remap fbinit2/3 to dac */
  1032. pci_write_config_dword(dev, PCI_INIT_ENABLE,
  1033. PCI_EN_INIT_WR | PCI_REMAP_DAC );
  1034. /* detect dac type */
  1035. if (!sst_detect_dactype(info, par)) {
  1036. printk(KERN_ERR "sstfb: unknown dac type.\n");
  1037. //FIXME watch it: we are not in a safe state, bad bad bad.
  1038. return 0;
  1039. }
  1040. /* set graphic clock */
  1041. gfx_clock = spec->default_gfx_clock;
  1042. if ((gfxclk >10 ) && (gfxclk < spec->max_gfxclk)) {
  1043. printk(KERN_INFO "sstfb: Using supplied graphic freq : %dMHz\n", gfxclk);
  1044. gfx_clock = gfxclk *1000;
  1045. } else if (gfxclk) {
  1046. printk(KERN_WARNING "sstfb: %dMhz is way out of spec! Using default\n", gfxclk);
  1047. }
  1048. sst_calc_pll(gfx_clock, &Fout, &gfx_timings);
  1049. par->dac_sw.set_pll(info, &gfx_timings, GFX_CLOCK);
  1050. /* disable fbiinit remap */
  1051. pci_write_config_dword(dev, PCI_INIT_ENABLE,
  1052. PCI_EN_INIT_WR| PCI_EN_FIFO_WR );
  1053. /* defaults init registers */
  1054. /* FbiInit0: unreset gfx, unreset fifo */
  1055. fbiinit0 = FBIINIT0_DEFAULT;
  1056. fbiinit1 = FBIINIT1_DEFAULT;
  1057. fbiinit4 = FBIINIT4_DEFAULT;
  1058. par->vgapass = vgapass;
  1059. if (par->vgapass)
  1060. fbiinit0 &= ~DIS_VGA_PASSTHROUGH;
  1061. else
  1062. fbiinit0 |= DIS_VGA_PASSTHROUGH;
  1063. if (slowpci) {
  1064. fbiinit1 |= SLOW_PCI_WRITES;
  1065. fbiinit4 |= SLOW_PCI_READS;
  1066. } else {
  1067. fbiinit1 &= ~SLOW_PCI_WRITES;
  1068. fbiinit4 &= ~SLOW_PCI_READS;
  1069. }
  1070. sst_write(FBIINIT0, fbiinit0);
  1071. sst_wait_idle();
  1072. sst_write(FBIINIT1, fbiinit1);
  1073. sst_wait_idle();
  1074. sst_write(FBIINIT2, FBIINIT2_DEFAULT);
  1075. sst_wait_idle();
  1076. sst_write(FBIINIT3, FBIINIT3_DEFAULT);
  1077. sst_wait_idle();
  1078. sst_write(FBIINIT4, fbiinit4);
  1079. sst_wait_idle();
  1080. if (IS_VOODOO2(par)) {
  1081. sst_write(FBIINIT6, FBIINIT6_DEFAULT);
  1082. sst_wait_idle();
  1083. }
  1084. pci_write_config_dword(dev, PCI_INIT_ENABLE, PCI_EN_FIFO_WR);
  1085. pci_write_config_dword(dev, PCI_VCLK_ENABLE, 0);
  1086. return 1;
  1087. }
  1088. static void __devexit sst_shutdown(struct fb_info *info)
  1089. {
  1090. struct sstfb_par *par = info->par;
  1091. struct pci_dev *dev = par->dev;
  1092. struct pll_timing gfx_timings;
  1093. int Fout;
  1094. /* reset video, gfx, fifo, disable dram + remap fbiinit2/3 */
  1095. pci_write_config_dword(dev, PCI_INIT_ENABLE, PCI_EN_INIT_WR);
  1096. sst_set_bits(FBIINIT1, VIDEO_RESET | EN_BLANKING);
  1097. sst_unset_bits(FBIINIT2, EN_DRAM_REFRESH);
  1098. sst_set_bits(FBIINIT0, FBI_RESET | FIFO_RESET);
  1099. sst_wait_idle();
  1100. pci_write_config_dword(dev, PCI_INIT_ENABLE,
  1101. PCI_EN_INIT_WR | PCI_REMAP_DAC);
  1102. /* set 20Mhz gfx clock */
  1103. sst_calc_pll(20000, &Fout, &gfx_timings);
  1104. par->dac_sw.set_pll(info, &gfx_timings, GFX_CLOCK);
  1105. /* TODO maybe shutdown the dac, vrefresh and so on... */
  1106. pci_write_config_dword(dev, PCI_INIT_ENABLE,
  1107. PCI_EN_INIT_WR);
  1108. sst_unset_bits(FBIINIT0, FBI_RESET | FIFO_RESET | DIS_VGA_PASSTHROUGH);
  1109. pci_write_config_dword(dev, PCI_VCLK_DISABLE,0);
  1110. /* maybe keep fbiinit* and PCI_INIT_enable in the fb_info struct
  1111. * from start ? */
  1112. pci_write_config_dword(dev, PCI_INIT_ENABLE, 0);
  1113. }
  1114. /*
  1115. * Interface to the world
  1116. */
  1117. static int __devinit sstfb_setup(char *options)
  1118. {
  1119. char *this_opt;
  1120. if (!options || !*options)
  1121. return 0;
  1122. while ((this_opt = strsep(&options, ",")) != NULL) {
  1123. if (!*this_opt) continue;
  1124. f_ddprintk("option %s\n", this_opt);
  1125. if (!strcmp(this_opt, "vganopass"))
  1126. vgapass = 0;
  1127. else if (!strcmp(this_opt, "vgapass"))
  1128. vgapass = 1;
  1129. else if (!strcmp(this_opt, "clipping"))
  1130. clipping = 1;
  1131. else if (!strcmp(this_opt, "noclipping"))
  1132. clipping = 0;
  1133. else if (!strcmp(this_opt, "fastpci"))
  1134. slowpci = 0;
  1135. else if (!strcmp(this_opt, "slowpci"))
  1136. slowpci = 1;
  1137. else if (!strncmp(this_opt, "mem:",4))
  1138. mem = simple_strtoul (this_opt+4, NULL, 0);
  1139. else if (!strncmp(this_opt, "gfxclk:",7))
  1140. gfxclk = simple_strtoul (this_opt+7, NULL, 0);
  1141. else
  1142. mode_option = this_opt;
  1143. }
  1144. return 0;
  1145. }
  1146. static struct fb_ops sstfb_ops = {
  1147. .owner = THIS_MODULE,
  1148. .fb_check_var = sstfb_check_var,
  1149. .fb_set_par = sstfb_set_par,
  1150. .fb_setcolreg = sstfb_setcolreg,
  1151. .fb_fillrect = cfb_fillrect, /* sstfb_fillrect */
  1152. .fb_copyarea = cfb_copyarea, /* sstfb_copyarea */
  1153. .fb_imageblit = cfb_imageblit,
  1154. .fb_ioctl = sstfb_ioctl,
  1155. };
  1156. static int __devinit sstfb_probe(struct pci_dev *pdev,
  1157. const struct pci_device_id *id)
  1158. {
  1159. struct fb_info *info;
  1160. struct fb_fix_screeninfo *fix;
  1161. struct sstfb_par *par;
  1162. struct sst_spec *spec;
  1163. int err;
  1164. /* Enable device in PCI config. */
  1165. if ((err=pci_enable_device(pdev))) {
  1166. printk(KERN_ERR "cannot enable device\n");
  1167. return err;
  1168. }
  1169. /* Allocate the fb and par structures. */
  1170. info = framebuffer_alloc(sizeof(struct sstfb_par), &pdev->dev);
  1171. if (!info)
  1172. return -ENOMEM;
  1173. pci_set_drvdata(pdev, info);
  1174. par = info->par;
  1175. fix = &info->fix;
  1176. par->type = id->driver_data;
  1177. spec = &voodoo_spec[par->type];
  1178. f_ddprintk("found device : %s\n", spec->name);
  1179. par->dev = pdev;
  1180. par->revision = pdev->revision;
  1181. fix->mmio_start = pci_resource_start(pdev,0);
  1182. fix->mmio_len = 0x400000;
  1183. fix->smem_start = fix->mmio_start + 0x400000;
  1184. if (!request_mem_region(fix->mmio_start, fix->mmio_len, "sstfb MMIO")) {
  1185. printk(KERN_ERR "sstfb: cannot reserve mmio memory\n");
  1186. goto fail_mmio_mem;
  1187. }
  1188. if (!request_mem_region(fix->smem_start, 0x400000,"sstfb FB")) {
  1189. printk(KERN_ERR "sstfb: cannot reserve fb memory\n");
  1190. goto fail_fb_mem;
  1191. }
  1192. par->mmio_vbase = ioremap_nocache(fix->mmio_start,
  1193. fix->mmio_len);
  1194. if (!par->mmio_vbase) {
  1195. printk(KERN_ERR "sstfb: cannot remap register area %#lx\n",
  1196. fix->mmio_start);
  1197. goto fail_mmio_remap;
  1198. }
  1199. info->screen_base = ioremap_nocache(fix->smem_start, 0x400000);
  1200. if (!info->screen_base) {
  1201. printk(KERN_ERR "sstfb: cannot remap framebuffer %#lx\n",
  1202. fix->smem_start);
  1203. goto fail_fb_remap;
  1204. }
  1205. if (!sst_init(info, par)) {
  1206. printk(KERN_ERR "sstfb: Init failed\n");
  1207. goto fail;
  1208. }
  1209. sst_get_memsize(info, &fix->smem_len);
  1210. strlcpy(fix->id, spec->name, sizeof(fix->id));
  1211. printk(KERN_INFO "%s (revision %d) with %s dac\n",
  1212. fix->id, par->revision, par->dac_sw.name);
  1213. printk(KERN_INFO "framebuffer at %#lx, mapped to 0x%p, size %dMB\n",
  1214. fix->smem_start, info->screen_base,
  1215. fix->smem_len >> 20);
  1216. f_ddprintk("regbase_virt: %#lx\n", par->mmio_vbase);
  1217. f_ddprintk("membase_phys: %#lx\n", fix->smem_start);
  1218. f_ddprintk("fbbase_virt: %p\n", info->screen_base);
  1219. info->flags = FBINFO_DEFAULT;
  1220. info->fbops = &sstfb_ops;
  1221. info->pseudo_palette = par->palette;
  1222. fix->type = FB_TYPE_PACKED_PIXELS;
  1223. fix->visual = FB_VISUAL_TRUECOLOR;
  1224. fix->accel = FB_ACCEL_NONE; /* FIXME */
  1225. /*
  1226. * According to the specs, the linelength must be of 1024 *pixels*
  1227. * and the 24bpp mode is in fact a 32 bpp mode (and both are in
  1228. * fact dithered to 16bit).
  1229. */
  1230. fix->line_length = 2048; /* default value, for 24 or 32bit: 4096 */
  1231. fb_find_mode(&info->var, info, mode_option, NULL, 0, NULL, 16);
  1232. if (sstfb_check_var(&info->var, info)) {
  1233. printk(KERN_ERR "sstfb: invalid video mode.\n");
  1234. goto fail;
  1235. }
  1236. if (sstfb_set_par(info)) {
  1237. printk(KERN_ERR "sstfb: can't set default video mode.\n");
  1238. goto fail;
  1239. }
  1240. if (fb_alloc_cmap(&info->cmap, 256, 0)) {
  1241. printk(KERN_ERR "sstfb: can't alloc cmap memory.\n");
  1242. goto fail;
  1243. }
  1244. /* register fb */
  1245. info->device = &pdev->dev;
  1246. if (register_framebuffer(info) < 0) {
  1247. printk(KERN_ERR "sstfb: can't register framebuffer.\n");
  1248. goto fail_register;
  1249. }
  1250. sstfb_clear_screen(info);
  1251. if (device_create_file(info->dev, &device_attrs[0]))
  1252. printk(KERN_WARNING "sstfb: can't create sysfs entry.\n");
  1253. printk(KERN_INFO "fb%d: %s frame buffer device at 0x%p\n",
  1254. info->node, fix->id, info->screen_base);
  1255. return 0;
  1256. fail_register:
  1257. fb_dealloc_cmap(&info->cmap);
  1258. fail:
  1259. iounmap(info->screen_base);
  1260. fail_fb_remap:
  1261. iounmap(par->mmio_vbase);
  1262. fail_mmio_remap:
  1263. release_mem_region(fix->smem_start, 0x400000);
  1264. fail_fb_mem:
  1265. release_mem_region(fix->mmio_start, info->fix.mmio_len);
  1266. fail_mmio_mem:
  1267. framebuffer_release(info);
  1268. return -ENXIO; /* no voodoo detected */
  1269. }
  1270. static void __devexit sstfb_remove(struct pci_dev *pdev)
  1271. {
  1272. struct sstfb_par *par;
  1273. struct fb_info *info;
  1274. info = pci_get_drvdata(pdev);
  1275. par = info->par;
  1276. device_remove_file(info->dev, &device_attrs[0]);
  1277. sst_shutdown(info);
  1278. iounmap(info->screen_base);
  1279. iounmap(par->mmio_vbase);
  1280. release_mem_region(info->fix.smem_start, 0x400000);
  1281. release_mem_region(info->fix.mmio_start, info->fix.mmio_len);
  1282. fb_dealloc_cmap(&info->cmap);
  1283. unregister_framebuffer(info);
  1284. framebuffer_release(info);
  1285. }
  1286. static const struct pci_device_id sstfb_id_tbl[] = {
  1287. { PCI_DEVICE(PCI_VENDOR_ID_3DFX, PCI_DEVICE_ID_3DFX_VOODOO ),
  1288. .driver_data = ID_VOODOO1, },
  1289. { PCI_DEVICE(PCI_VENDOR_ID_3DFX, PCI_DEVICE_ID_3DFX_VOODOO2),
  1290. .driver_data = ID_VOODOO2, },
  1291. { 0 },
  1292. };
  1293. static struct pci_driver sstfb_driver = {
  1294. .name = "sstfb",
  1295. .id_table = sstfb_id_tbl,
  1296. .probe = sstfb_probe,
  1297. .remove = __devexit_p(sstfb_remove),
  1298. };
  1299. static int __devinit sstfb_init(void)
  1300. {
  1301. char *option = NULL;
  1302. if (fb_get_options("sstfb", &option))
  1303. return -ENODEV;
  1304. sstfb_setup(option);
  1305. return pci_register_driver(&sstfb_driver);
  1306. }
  1307. static void __devexit sstfb_exit(void)
  1308. {
  1309. pci_unregister_driver(&sstfb_driver);
  1310. }
  1311. module_init(sstfb_init);
  1312. module_exit(sstfb_exit);
  1313. MODULE_AUTHOR("(c) 2000,2002 Ghozlane Toumi <gtoumi@laposte.net>");
  1314. MODULE_DESCRIPTION("FBDev driver for 3dfx Voodoo Graphics and Voodoo2 based video boards");
  1315. MODULE_LICENSE("GPL");
  1316. module_param(mem, int, 0);
  1317. MODULE_PARM_DESC(mem, "Size of frame buffer memory in MB (1, 2, 4 MB, default=autodetect)");
  1318. module_param(vgapass, bool, 0);
  1319. MODULE_PARM_DESC(vgapass, "Enable VGA PassThrough mode (0 or 1) (default=0)");
  1320. module_param(clipping, bool, 0);
  1321. MODULE_PARM_DESC(clipping, "Enable clipping (slower, safer) (0 or 1) (default=1)");
  1322. module_param(gfxclk, int, 0);
  1323. MODULE_PARM_DESC(gfxclk, "Force graphic chip frequency in MHz. DANGEROUS. (default=auto)");
  1324. module_param(slowpci, bool, 0);
  1325. MODULE_PARM_DESC(slowpci, "Uses slow PCI settings (0 or 1) (default=0)");
  1326. module_param(mode_option, charp, 0);
  1327. MODULE_PARM_DESC(mode_option, "Initial video mode (default=" DEFAULT_VIDEO_MODE ")");