sstfb.c 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533
  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 bool vgapass; /* enable VGA passthrough cable */
  86. static int mem; /* mem size in MB, 0 = autodetect */
  87. static bool clipping = 1; /* use clipping (slower, safer) */
  88. static int gfxclk; /* force FBI freq in Mhz . Dangerous */
  89. static bool 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 = 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[] = {
  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. fb_info(info, "Enabling VGA pass-through\n");
  620. } else {
  621. sst_write(FBIINIT0, fbiinit0 | DIS_VGA_PASSTHROUGH);
  622. fb_info(info, "Disabling VGA pass-through\n");
  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 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 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 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 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[] = {
  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 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 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 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 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 sstfb_probe(struct pci_dev *pdev, const struct pci_device_id *id)
  1157. {
  1158. struct fb_info *info;
  1159. struct fb_fix_screeninfo *fix;
  1160. struct sstfb_par *par;
  1161. struct sst_spec *spec;
  1162. int err;
  1163. /* Enable device in PCI config. */
  1164. if ((err=pci_enable_device(pdev))) {
  1165. printk(KERN_ERR "cannot enable device\n");
  1166. return err;
  1167. }
  1168. /* Allocate the fb and par structures. */
  1169. info = framebuffer_alloc(sizeof(struct sstfb_par), &pdev->dev);
  1170. if (!info)
  1171. return -ENOMEM;
  1172. pci_set_drvdata(pdev, info);
  1173. par = info->par;
  1174. fix = &info->fix;
  1175. par->type = id->driver_data;
  1176. spec = &voodoo_spec[par->type];
  1177. f_ddprintk("found device : %s\n", spec->name);
  1178. par->dev = pdev;
  1179. par->revision = pdev->revision;
  1180. fix->mmio_start = pci_resource_start(pdev,0);
  1181. fix->mmio_len = 0x400000;
  1182. fix->smem_start = fix->mmio_start + 0x400000;
  1183. if (!request_mem_region(fix->mmio_start, fix->mmio_len, "sstfb MMIO")) {
  1184. printk(KERN_ERR "sstfb: cannot reserve mmio memory\n");
  1185. goto fail_mmio_mem;
  1186. }
  1187. if (!request_mem_region(fix->smem_start, 0x400000,"sstfb FB")) {
  1188. printk(KERN_ERR "sstfb: cannot reserve fb memory\n");
  1189. goto fail_fb_mem;
  1190. }
  1191. par->mmio_vbase = ioremap_nocache(fix->mmio_start,
  1192. fix->mmio_len);
  1193. if (!par->mmio_vbase) {
  1194. printk(KERN_ERR "sstfb: cannot remap register area %#lx\n",
  1195. fix->mmio_start);
  1196. goto fail_mmio_remap;
  1197. }
  1198. info->screen_base = ioremap_nocache(fix->smem_start, 0x400000);
  1199. if (!info->screen_base) {
  1200. printk(KERN_ERR "sstfb: cannot remap framebuffer %#lx\n",
  1201. fix->smem_start);
  1202. goto fail_fb_remap;
  1203. }
  1204. if (!sst_init(info, par)) {
  1205. printk(KERN_ERR "sstfb: Init failed\n");
  1206. goto fail;
  1207. }
  1208. sst_get_memsize(info, &fix->smem_len);
  1209. strlcpy(fix->id, spec->name, sizeof(fix->id));
  1210. printk(KERN_INFO "%s (revision %d) with %s dac\n",
  1211. fix->id, par->revision, par->dac_sw.name);
  1212. printk(KERN_INFO "framebuffer at %#lx, mapped to 0x%p, size %dMB\n",
  1213. fix->smem_start, info->screen_base,
  1214. fix->smem_len >> 20);
  1215. f_ddprintk("regbase_virt: %#lx\n", par->mmio_vbase);
  1216. f_ddprintk("membase_phys: %#lx\n", fix->smem_start);
  1217. f_ddprintk("fbbase_virt: %p\n", info->screen_base);
  1218. info->flags = FBINFO_DEFAULT;
  1219. info->fbops = &sstfb_ops;
  1220. info->pseudo_palette = par->palette;
  1221. fix->type = FB_TYPE_PACKED_PIXELS;
  1222. fix->visual = FB_VISUAL_TRUECOLOR;
  1223. fix->accel = FB_ACCEL_NONE; /* FIXME */
  1224. /*
  1225. * According to the specs, the linelength must be of 1024 *pixels*
  1226. * and the 24bpp mode is in fact a 32 bpp mode (and both are in
  1227. * fact dithered to 16bit).
  1228. */
  1229. fix->line_length = 2048; /* default value, for 24 or 32bit: 4096 */
  1230. fb_find_mode(&info->var, info, mode_option, NULL, 0, NULL, 16);
  1231. if (sstfb_check_var(&info->var, info)) {
  1232. printk(KERN_ERR "sstfb: invalid video mode.\n");
  1233. goto fail;
  1234. }
  1235. if (sstfb_set_par(info)) {
  1236. printk(KERN_ERR "sstfb: can't set default video mode.\n");
  1237. goto fail;
  1238. }
  1239. if (fb_alloc_cmap(&info->cmap, 256, 0)) {
  1240. printk(KERN_ERR "sstfb: can't alloc cmap memory.\n");
  1241. goto fail;
  1242. }
  1243. /* register fb */
  1244. info->device = &pdev->dev;
  1245. if (register_framebuffer(info) < 0) {
  1246. printk(KERN_ERR "sstfb: can't register framebuffer.\n");
  1247. goto fail_register;
  1248. }
  1249. sstfb_clear_screen(info);
  1250. if (device_create_file(info->dev, &device_attrs[0]))
  1251. printk(KERN_WARNING "sstfb: can't create sysfs entry.\n");
  1252. fb_info(info, "%s frame buffer device at 0x%p\n",
  1253. fix->id, info->screen_base);
  1254. return 0;
  1255. fail_register:
  1256. fb_dealloc_cmap(&info->cmap);
  1257. fail:
  1258. iounmap(info->screen_base);
  1259. fail_fb_remap:
  1260. iounmap(par->mmio_vbase);
  1261. fail_mmio_remap:
  1262. release_mem_region(fix->smem_start, 0x400000);
  1263. fail_fb_mem:
  1264. release_mem_region(fix->mmio_start, info->fix.mmio_len);
  1265. fail_mmio_mem:
  1266. framebuffer_release(info);
  1267. return -ENXIO; /* no voodoo detected */
  1268. }
  1269. static void sstfb_remove(struct pci_dev *pdev)
  1270. {
  1271. struct sstfb_par *par;
  1272. struct fb_info *info;
  1273. info = pci_get_drvdata(pdev);
  1274. par = info->par;
  1275. device_remove_file(info->dev, &device_attrs[0]);
  1276. sst_shutdown(info);
  1277. iounmap(info->screen_base);
  1278. iounmap(par->mmio_vbase);
  1279. release_mem_region(info->fix.smem_start, 0x400000);
  1280. release_mem_region(info->fix.mmio_start, info->fix.mmio_len);
  1281. fb_dealloc_cmap(&info->cmap);
  1282. unregister_framebuffer(info);
  1283. framebuffer_release(info);
  1284. }
  1285. static const struct pci_device_id sstfb_id_tbl[] = {
  1286. { PCI_DEVICE(PCI_VENDOR_ID_3DFX, PCI_DEVICE_ID_3DFX_VOODOO ),
  1287. .driver_data = ID_VOODOO1, },
  1288. { PCI_DEVICE(PCI_VENDOR_ID_3DFX, PCI_DEVICE_ID_3DFX_VOODOO2),
  1289. .driver_data = ID_VOODOO2, },
  1290. { 0 },
  1291. };
  1292. static struct pci_driver sstfb_driver = {
  1293. .name = "sstfb",
  1294. .id_table = sstfb_id_tbl,
  1295. .probe = sstfb_probe,
  1296. .remove = sstfb_remove,
  1297. };
  1298. static int sstfb_init(void)
  1299. {
  1300. char *option = NULL;
  1301. if (fb_get_options("sstfb", &option))
  1302. return -ENODEV;
  1303. sstfb_setup(option);
  1304. return pci_register_driver(&sstfb_driver);
  1305. }
  1306. static void sstfb_exit(void)
  1307. {
  1308. pci_unregister_driver(&sstfb_driver);
  1309. }
  1310. module_init(sstfb_init);
  1311. module_exit(sstfb_exit);
  1312. MODULE_AUTHOR("(c) 2000,2002 Ghozlane Toumi <gtoumi@laposte.net>");
  1313. MODULE_DESCRIPTION("FBDev driver for 3dfx Voodoo Graphics and Voodoo2 based video boards");
  1314. MODULE_LICENSE("GPL");
  1315. module_param(mem, int, 0);
  1316. MODULE_PARM_DESC(mem, "Size of frame buffer memory in MB (1, 2, 4 MB, default=autodetect)");
  1317. module_param(vgapass, bool, 0);
  1318. MODULE_PARM_DESC(vgapass, "Enable VGA PassThrough mode (0 or 1) (default=0)");
  1319. module_param(clipping, bool, 0);
  1320. MODULE_PARM_DESC(clipping, "Enable clipping (slower, safer) (0 or 1) (default=1)");
  1321. module_param(gfxclk, int, 0);
  1322. MODULE_PARM_DESC(gfxclk, "Force graphic chip frequency in MHz. DANGEROUS. (default=auto)");
  1323. module_param(slowpci, bool, 0);
  1324. MODULE_PARM_DESC(slowpci, "Uses slow PCI settings (0 or 1) (default=0)");
  1325. module_param(mode_option, charp, 0);
  1326. MODULE_PARM_DESC(mode_option, "Initial video mode (default=" DEFAULT_VIDEO_MODE ")");