nvidia.c 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579
  1. /*
  2. * linux/drivers/video/nvidia/nvidia.c - nVidia fb driver
  3. *
  4. * Copyright 2004 Antonino Daplas <adaplas@pol.net>
  5. *
  6. * This file is subject to the terms and conditions of the GNU General Public
  7. * License. See the file COPYING in the main directory of this archive
  8. * for more details.
  9. *
  10. */
  11. #include <linux/module.h>
  12. #include <linux/kernel.h>
  13. #include <linux/errno.h>
  14. #include <linux/string.h>
  15. #include <linux/mm.h>
  16. #include <linux/slab.h>
  17. #include <linux/delay.h>
  18. #include <linux/fb.h>
  19. #include <linux/init.h>
  20. #include <linux/pci.h>
  21. #include <linux/console.h>
  22. #include <linux/backlight.h>
  23. #ifdef CONFIG_BOOTX_TEXT
  24. #include <asm/btext.h>
  25. #endif
  26. #include "nv_local.h"
  27. #include "nv_type.h"
  28. #include "nv_proto.h"
  29. #include "nv_dma.h"
  30. #ifdef CONFIG_FB_NVIDIA_DEBUG
  31. #define NVTRACE printk
  32. #else
  33. #define NVTRACE if (0) printk
  34. #endif
  35. #define NVTRACE_ENTER(...) NVTRACE("%s START\n", __func__)
  36. #define NVTRACE_LEAVE(...) NVTRACE("%s END\n", __func__)
  37. #ifdef CONFIG_FB_NVIDIA_DEBUG
  38. #define assert(expr) \
  39. if (!(expr)) { \
  40. printk( "Assertion failed! %s,%s,%s,line=%d\n",\
  41. #expr,__FILE__,__func__,__LINE__); \
  42. BUG(); \
  43. }
  44. #else
  45. #define assert(expr)
  46. #endif
  47. #define PFX "nvidiafb: "
  48. /* HW cursor parameters */
  49. #define MAX_CURS 32
  50. static struct pci_device_id nvidiafb_pci_tbl[] = {
  51. {PCI_VENDOR_ID_NVIDIA, PCI_ANY_ID, PCI_ANY_ID, PCI_ANY_ID,
  52. PCI_BASE_CLASS_DISPLAY << 16, 0xff0000, 0},
  53. { 0, }
  54. };
  55. MODULE_DEVICE_TABLE(pci, nvidiafb_pci_tbl);
  56. /* command line data, set in nvidiafb_setup() */
  57. static int flatpanel = -1; /* Autodetect later */
  58. static int fpdither = -1;
  59. static int forceCRTC = -1;
  60. static int hwcur = 0;
  61. static int noaccel = 0;
  62. static int noscale = 0;
  63. static int paneltweak = 0;
  64. static int vram = 0;
  65. static int bpp = 8;
  66. static int reverse_i2c;
  67. static bool nomtrr = false;
  68. #ifdef CONFIG_PMAC_BACKLIGHT
  69. static int backlight = 1;
  70. #else
  71. static int backlight = 0;
  72. #endif
  73. static char *mode_option = NULL;
  74. static struct fb_fix_screeninfo nvidiafb_fix = {
  75. .type = FB_TYPE_PACKED_PIXELS,
  76. .xpanstep = 8,
  77. .ypanstep = 1,
  78. };
  79. static struct fb_var_screeninfo nvidiafb_default_var = {
  80. .xres = 640,
  81. .yres = 480,
  82. .xres_virtual = 640,
  83. .yres_virtual = 480,
  84. .bits_per_pixel = 8,
  85. .red = {0, 8, 0},
  86. .green = {0, 8, 0},
  87. .blue = {0, 8, 0},
  88. .transp = {0, 0, 0},
  89. .activate = FB_ACTIVATE_NOW,
  90. .height = -1,
  91. .width = -1,
  92. .pixclock = 39721,
  93. .left_margin = 40,
  94. .right_margin = 24,
  95. .upper_margin = 32,
  96. .lower_margin = 11,
  97. .hsync_len = 96,
  98. .vsync_len = 2,
  99. .vmode = FB_VMODE_NONINTERLACED
  100. };
  101. static void nvidiafb_load_cursor_image(struct nvidia_par *par, u8 * data8,
  102. u16 bg, u16 fg, u32 w, u32 h)
  103. {
  104. u32 *data = (u32 *) data8;
  105. int i, j, k = 0;
  106. u32 b, tmp;
  107. w = (w + 1) & ~1;
  108. for (i = 0; i < h; i++) {
  109. b = *data++;
  110. reverse_order(&b);
  111. for (j = 0; j < w / 2; j++) {
  112. tmp = 0;
  113. #if defined (__BIG_ENDIAN)
  114. tmp = (b & (1 << 31)) ? fg << 16 : bg << 16;
  115. b <<= 1;
  116. tmp |= (b & (1 << 31)) ? fg : bg;
  117. b <<= 1;
  118. #else
  119. tmp = (b & 1) ? fg : bg;
  120. b >>= 1;
  121. tmp |= (b & 1) ? fg << 16 : bg << 16;
  122. b >>= 1;
  123. #endif
  124. NV_WR32(&par->CURSOR[k++], 0, tmp);
  125. }
  126. k += (MAX_CURS - w) / 2;
  127. }
  128. }
  129. static void nvidia_write_clut(struct nvidia_par *par,
  130. u8 regnum, u8 red, u8 green, u8 blue)
  131. {
  132. NVWriteDacMask(par, 0xff);
  133. NVWriteDacWriteAddr(par, regnum);
  134. NVWriteDacData(par, red);
  135. NVWriteDacData(par, green);
  136. NVWriteDacData(par, blue);
  137. }
  138. static void nvidia_read_clut(struct nvidia_par *par,
  139. u8 regnum, u8 * red, u8 * green, u8 * blue)
  140. {
  141. NVWriteDacMask(par, 0xff);
  142. NVWriteDacReadAddr(par, regnum);
  143. *red = NVReadDacData(par);
  144. *green = NVReadDacData(par);
  145. *blue = NVReadDacData(par);
  146. }
  147. static int nvidia_panel_tweak(struct nvidia_par *par,
  148. struct _riva_hw_state *state)
  149. {
  150. int tweak = 0;
  151. if (par->paneltweak) {
  152. tweak = par->paneltweak;
  153. } else {
  154. /* begin flat panel hacks */
  155. /* This is unfortunate, but some chips need this register
  156. tweaked or else you get artifacts where adjacent pixels are
  157. swapped. There are no hard rules for what to set here so all
  158. we can do is experiment and apply hacks. */
  159. if(((par->Chipset & 0xffff) == 0x0328) && (state->bpp == 32)) {
  160. /* At least one NV34 laptop needs this workaround. */
  161. tweak = -1;
  162. }
  163. if((par->Chipset & 0xfff0) == 0x0310) {
  164. tweak = 1;
  165. }
  166. /* end flat panel hacks */
  167. }
  168. return tweak;
  169. }
  170. static void nvidia_screen_off(struct nvidia_par *par, int on)
  171. {
  172. unsigned char tmp;
  173. if (on) {
  174. /*
  175. * Turn off screen and disable sequencer.
  176. */
  177. tmp = NVReadSeq(par, 0x01);
  178. NVWriteSeq(par, 0x00, 0x01); /* Synchronous Reset */
  179. NVWriteSeq(par, 0x01, tmp | 0x20); /* disable the display */
  180. } else {
  181. /*
  182. * Reenable sequencer, then turn on screen.
  183. */
  184. tmp = NVReadSeq(par, 0x01);
  185. NVWriteSeq(par, 0x01, tmp & ~0x20); /* reenable display */
  186. NVWriteSeq(par, 0x00, 0x03); /* End Reset */
  187. }
  188. }
  189. static void nvidia_save_vga(struct nvidia_par *par,
  190. struct _riva_hw_state *state)
  191. {
  192. int i;
  193. NVTRACE_ENTER();
  194. NVLockUnlock(par, 0);
  195. NVUnloadStateExt(par, state);
  196. state->misc_output = NVReadMiscOut(par);
  197. for (i = 0; i < NUM_CRT_REGS; i++)
  198. state->crtc[i] = NVReadCrtc(par, i);
  199. for (i = 0; i < NUM_ATC_REGS; i++)
  200. state->attr[i] = NVReadAttr(par, i);
  201. for (i = 0; i < NUM_GRC_REGS; i++)
  202. state->gra[i] = NVReadGr(par, i);
  203. for (i = 0; i < NUM_SEQ_REGS; i++)
  204. state->seq[i] = NVReadSeq(par, i);
  205. NVTRACE_LEAVE();
  206. }
  207. #undef DUMP_REG
  208. static void nvidia_write_regs(struct nvidia_par *par,
  209. struct _riva_hw_state *state)
  210. {
  211. int i;
  212. NVTRACE_ENTER();
  213. NVLoadStateExt(par, state);
  214. NVWriteMiscOut(par, state->misc_output);
  215. for (i = 1; i < NUM_SEQ_REGS; i++) {
  216. #ifdef DUMP_REG
  217. printk(" SEQ[%02x] = %08x\n", i, state->seq[i]);
  218. #endif
  219. NVWriteSeq(par, i, state->seq[i]);
  220. }
  221. /* Ensure CRTC registers 0-7 are unlocked by clearing bit 7 of CRTC[17] */
  222. NVWriteCrtc(par, 0x11, state->crtc[0x11] & ~0x80);
  223. for (i = 0; i < NUM_CRT_REGS; i++) {
  224. switch (i) {
  225. case 0x19:
  226. case 0x20 ... 0x40:
  227. break;
  228. default:
  229. #ifdef DUMP_REG
  230. printk("CRTC[%02x] = %08x\n", i, state->crtc[i]);
  231. #endif
  232. NVWriteCrtc(par, i, state->crtc[i]);
  233. }
  234. }
  235. for (i = 0; i < NUM_GRC_REGS; i++) {
  236. #ifdef DUMP_REG
  237. printk(" GRA[%02x] = %08x\n", i, state->gra[i]);
  238. #endif
  239. NVWriteGr(par, i, state->gra[i]);
  240. }
  241. for (i = 0; i < NUM_ATC_REGS; i++) {
  242. #ifdef DUMP_REG
  243. printk("ATTR[%02x] = %08x\n", i, state->attr[i]);
  244. #endif
  245. NVWriteAttr(par, i, state->attr[i]);
  246. }
  247. NVTRACE_LEAVE();
  248. }
  249. static int nvidia_calc_regs(struct fb_info *info)
  250. {
  251. struct nvidia_par *par = info->par;
  252. struct _riva_hw_state *state = &par->ModeReg;
  253. int i, depth = fb_get_color_depth(&info->var, &info->fix);
  254. int h_display = info->var.xres / 8 - 1;
  255. int h_start = (info->var.xres + info->var.right_margin) / 8 - 1;
  256. int h_end = (info->var.xres + info->var.right_margin +
  257. info->var.hsync_len) / 8 - 1;
  258. int h_total = (info->var.xres + info->var.right_margin +
  259. info->var.hsync_len + info->var.left_margin) / 8 - 5;
  260. int h_blank_s = h_display;
  261. int h_blank_e = h_total + 4;
  262. int v_display = info->var.yres - 1;
  263. int v_start = info->var.yres + info->var.lower_margin - 1;
  264. int v_end = (info->var.yres + info->var.lower_margin +
  265. info->var.vsync_len) - 1;
  266. int v_total = (info->var.yres + info->var.lower_margin +
  267. info->var.vsync_len + info->var.upper_margin) - 2;
  268. int v_blank_s = v_display;
  269. int v_blank_e = v_total + 1;
  270. /*
  271. * Set all CRTC values.
  272. */
  273. if (info->var.vmode & FB_VMODE_INTERLACED)
  274. v_total |= 1;
  275. if (par->FlatPanel == 1) {
  276. v_start = v_total - 3;
  277. v_end = v_total - 2;
  278. v_blank_s = v_start;
  279. h_start = h_total - 5;
  280. h_end = h_total - 2;
  281. h_blank_e = h_total + 4;
  282. }
  283. state->crtc[0x0] = Set8Bits(h_total);
  284. state->crtc[0x1] = Set8Bits(h_display);
  285. state->crtc[0x2] = Set8Bits(h_blank_s);
  286. state->crtc[0x3] = SetBitField(h_blank_e, 4: 0, 4:0)
  287. | SetBit(7);
  288. state->crtc[0x4] = Set8Bits(h_start);
  289. state->crtc[0x5] = SetBitField(h_blank_e, 5: 5, 7:7)
  290. | SetBitField(h_end, 4: 0, 4:0);
  291. state->crtc[0x6] = SetBitField(v_total, 7: 0, 7:0);
  292. state->crtc[0x7] = SetBitField(v_total, 8: 8, 0:0)
  293. | SetBitField(v_display, 8: 8, 1:1)
  294. | SetBitField(v_start, 8: 8, 2:2)
  295. | SetBitField(v_blank_s, 8: 8, 3:3)
  296. | SetBit(4)
  297. | SetBitField(v_total, 9: 9, 5:5)
  298. | SetBitField(v_display, 9: 9, 6:6)
  299. | SetBitField(v_start, 9: 9, 7:7);
  300. state->crtc[0x9] = SetBitField(v_blank_s, 9: 9, 5:5)
  301. | SetBit(6)
  302. | ((info->var.vmode & FB_VMODE_DOUBLE) ? 0x80 : 0x00);
  303. state->crtc[0x10] = Set8Bits(v_start);
  304. state->crtc[0x11] = SetBitField(v_end, 3: 0, 3:0) | SetBit(5);
  305. state->crtc[0x12] = Set8Bits(v_display);
  306. state->crtc[0x13] = ((info->var.xres_virtual / 8) *
  307. (info->var.bits_per_pixel / 8));
  308. state->crtc[0x15] = Set8Bits(v_blank_s);
  309. state->crtc[0x16] = Set8Bits(v_blank_e);
  310. state->attr[0x10] = 0x01;
  311. if (par->Television)
  312. state->attr[0x11] = 0x00;
  313. state->screen = SetBitField(h_blank_e, 6: 6, 4:4)
  314. | SetBitField(v_blank_s, 10: 10, 3:3)
  315. | SetBitField(v_start, 10: 10, 2:2)
  316. | SetBitField(v_display, 10: 10, 1:1)
  317. | SetBitField(v_total, 10: 10, 0:0);
  318. state->horiz = SetBitField(h_total, 8: 8, 0:0)
  319. | SetBitField(h_display, 8: 8, 1:1)
  320. | SetBitField(h_blank_s, 8: 8, 2:2)
  321. | SetBitField(h_start, 8: 8, 3:3);
  322. state->extra = SetBitField(v_total, 11: 11, 0:0)
  323. | SetBitField(v_display, 11: 11, 2:2)
  324. | SetBitField(v_start, 11: 11, 4:4)
  325. | SetBitField(v_blank_s, 11: 11, 6:6);
  326. if (info->var.vmode & FB_VMODE_INTERLACED) {
  327. h_total = (h_total >> 1) & ~1;
  328. state->interlace = Set8Bits(h_total);
  329. state->horiz |= SetBitField(h_total, 8: 8, 4:4);
  330. } else {
  331. state->interlace = 0xff; /* interlace off */
  332. }
  333. /*
  334. * Calculate the extended registers.
  335. */
  336. if (depth < 24)
  337. i = depth;
  338. else
  339. i = 32;
  340. if (par->Architecture >= NV_ARCH_10)
  341. par->CURSOR = (volatile u32 __iomem *)(info->screen_base +
  342. par->CursorStart);
  343. if (info->var.sync & FB_SYNC_HOR_HIGH_ACT)
  344. state->misc_output &= ~0x40;
  345. else
  346. state->misc_output |= 0x40;
  347. if (info->var.sync & FB_SYNC_VERT_HIGH_ACT)
  348. state->misc_output &= ~0x80;
  349. else
  350. state->misc_output |= 0x80;
  351. NVCalcStateExt(par, state, i, info->var.xres_virtual,
  352. info->var.xres, info->var.yres_virtual,
  353. 1000000000 / info->var.pixclock, info->var.vmode);
  354. state->scale = NV_RD32(par->PRAMDAC, 0x00000848) & 0xfff000ff;
  355. if (par->FlatPanel == 1) {
  356. state->pixel |= (1 << 7);
  357. if (!par->fpScaler || (par->fpWidth <= info->var.xres)
  358. || (par->fpHeight <= info->var.yres)) {
  359. state->scale |= (1 << 8);
  360. }
  361. if (!par->crtcSync_read) {
  362. state->crtcSync = NV_RD32(par->PRAMDAC, 0x0828);
  363. par->crtcSync_read = 1;
  364. }
  365. par->PanelTweak = nvidia_panel_tweak(par, state);
  366. }
  367. state->vpll = state->pll;
  368. state->vpll2 = state->pll;
  369. state->vpllB = state->pllB;
  370. state->vpll2B = state->pllB;
  371. VGA_WR08(par->PCIO, 0x03D4, 0x1C);
  372. state->fifo = VGA_RD08(par->PCIO, 0x03D5) & ~(1<<5);
  373. if (par->CRTCnumber) {
  374. state->head = NV_RD32(par->PCRTC0, 0x00000860) & ~0x00001000;
  375. state->head2 = NV_RD32(par->PCRTC0, 0x00002860) | 0x00001000;
  376. state->crtcOwner = 3;
  377. state->pllsel |= 0x20000800;
  378. state->vpll = NV_RD32(par->PRAMDAC0, 0x00000508);
  379. if (par->twoStagePLL)
  380. state->vpllB = NV_RD32(par->PRAMDAC0, 0x00000578);
  381. } else if (par->twoHeads) {
  382. state->head = NV_RD32(par->PCRTC0, 0x00000860) | 0x00001000;
  383. state->head2 = NV_RD32(par->PCRTC0, 0x00002860) & ~0x00001000;
  384. state->crtcOwner = 0;
  385. state->vpll2 = NV_RD32(par->PRAMDAC0, 0x0520);
  386. if (par->twoStagePLL)
  387. state->vpll2B = NV_RD32(par->PRAMDAC0, 0x057C);
  388. }
  389. state->cursorConfig = 0x00000100;
  390. if (info->var.vmode & FB_VMODE_DOUBLE)
  391. state->cursorConfig |= (1 << 4);
  392. if (par->alphaCursor) {
  393. if ((par->Chipset & 0x0ff0) != 0x0110)
  394. state->cursorConfig |= 0x04011000;
  395. else
  396. state->cursorConfig |= 0x14011000;
  397. state->general |= (1 << 29);
  398. } else
  399. state->cursorConfig |= 0x02000000;
  400. if (par->twoHeads) {
  401. if ((par->Chipset & 0x0ff0) == 0x0110) {
  402. state->dither = NV_RD32(par->PRAMDAC, 0x0528) &
  403. ~0x00010000;
  404. if (par->FPDither)
  405. state->dither |= 0x00010000;
  406. } else {
  407. state->dither = NV_RD32(par->PRAMDAC, 0x083C) & ~1;
  408. if (par->FPDither)
  409. state->dither |= 1;
  410. }
  411. }
  412. state->timingH = 0;
  413. state->timingV = 0;
  414. state->displayV = info->var.xres;
  415. return 0;
  416. }
  417. static void nvidia_init_vga(struct fb_info *info)
  418. {
  419. struct nvidia_par *par = info->par;
  420. struct _riva_hw_state *state = &par->ModeReg;
  421. int i;
  422. for (i = 0; i < 0x10; i++)
  423. state->attr[i] = i;
  424. state->attr[0x10] = 0x41;
  425. state->attr[0x11] = 0xff;
  426. state->attr[0x12] = 0x0f;
  427. state->attr[0x13] = 0x00;
  428. state->attr[0x14] = 0x00;
  429. memset(state->crtc, 0x00, NUM_CRT_REGS);
  430. state->crtc[0x0a] = 0x20;
  431. state->crtc[0x17] = 0xe3;
  432. state->crtc[0x18] = 0xff;
  433. state->crtc[0x28] = 0x40;
  434. memset(state->gra, 0x00, NUM_GRC_REGS);
  435. state->gra[0x05] = 0x40;
  436. state->gra[0x06] = 0x05;
  437. state->gra[0x07] = 0x0f;
  438. state->gra[0x08] = 0xff;
  439. state->seq[0x00] = 0x03;
  440. state->seq[0x01] = 0x01;
  441. state->seq[0x02] = 0x0f;
  442. state->seq[0x03] = 0x00;
  443. state->seq[0x04] = 0x0e;
  444. state->misc_output = 0xeb;
  445. }
  446. static int nvidiafb_cursor(struct fb_info *info, struct fb_cursor *cursor)
  447. {
  448. struct nvidia_par *par = info->par;
  449. u8 data[MAX_CURS * MAX_CURS / 8];
  450. int i, set = cursor->set;
  451. u16 fg, bg;
  452. if (cursor->image.width > MAX_CURS || cursor->image.height > MAX_CURS)
  453. return -ENXIO;
  454. NVShowHideCursor(par, 0);
  455. if (par->cursor_reset) {
  456. set = FB_CUR_SETALL;
  457. par->cursor_reset = 0;
  458. }
  459. if (set & FB_CUR_SETSIZE)
  460. memset_io(par->CURSOR, 0, MAX_CURS * MAX_CURS * 2);
  461. if (set & FB_CUR_SETPOS) {
  462. u32 xx, yy, temp;
  463. yy = cursor->image.dy - info->var.yoffset;
  464. xx = cursor->image.dx - info->var.xoffset;
  465. temp = xx & 0xFFFF;
  466. temp |= yy << 16;
  467. NV_WR32(par->PRAMDAC, 0x0000300, temp);
  468. }
  469. if (set & (FB_CUR_SETSHAPE | FB_CUR_SETCMAP | FB_CUR_SETIMAGE)) {
  470. u32 bg_idx = cursor->image.bg_color;
  471. u32 fg_idx = cursor->image.fg_color;
  472. u32 s_pitch = (cursor->image.width + 7) >> 3;
  473. u32 d_pitch = MAX_CURS / 8;
  474. u8 *dat = (u8 *) cursor->image.data;
  475. u8 *msk = (u8 *) cursor->mask;
  476. u8 *src;
  477. src = kmalloc(s_pitch * cursor->image.height, GFP_ATOMIC);
  478. if (src) {
  479. switch (cursor->rop) {
  480. case ROP_XOR:
  481. for (i = 0; i < s_pitch * cursor->image.height; i++)
  482. src[i] = dat[i] ^ msk[i];
  483. break;
  484. case ROP_COPY:
  485. default:
  486. for (i = 0; i < s_pitch * cursor->image.height; i++)
  487. src[i] = dat[i] & msk[i];
  488. break;
  489. }
  490. fb_pad_aligned_buffer(data, d_pitch, src, s_pitch,
  491. cursor->image.height);
  492. bg = ((info->cmap.red[bg_idx] & 0xf8) << 7) |
  493. ((info->cmap.green[bg_idx] & 0xf8) << 2) |
  494. ((info->cmap.blue[bg_idx] & 0xf8) >> 3) | 1 << 15;
  495. fg = ((info->cmap.red[fg_idx] & 0xf8) << 7) |
  496. ((info->cmap.green[fg_idx] & 0xf8) << 2) |
  497. ((info->cmap.blue[fg_idx] & 0xf8) >> 3) | 1 << 15;
  498. NVLockUnlock(par, 0);
  499. nvidiafb_load_cursor_image(par, data, bg, fg,
  500. cursor->image.width,
  501. cursor->image.height);
  502. kfree(src);
  503. }
  504. }
  505. if (cursor->enable)
  506. NVShowHideCursor(par, 1);
  507. return 0;
  508. }
  509. static int nvidiafb_set_par(struct fb_info *info)
  510. {
  511. struct nvidia_par *par = info->par;
  512. NVTRACE_ENTER();
  513. NVLockUnlock(par, 1);
  514. if (!par->FlatPanel || !par->twoHeads)
  515. par->FPDither = 0;
  516. if (par->FPDither < 0) {
  517. if ((par->Chipset & 0x0ff0) == 0x0110)
  518. par->FPDither = !!(NV_RD32(par->PRAMDAC, 0x0528)
  519. & 0x00010000);
  520. else
  521. par->FPDither = !!(NV_RD32(par->PRAMDAC, 0x083C) & 1);
  522. printk(KERN_INFO PFX "Flat panel dithering %s\n",
  523. par->FPDither ? "enabled" : "disabled");
  524. }
  525. info->fix.visual = (info->var.bits_per_pixel == 8) ?
  526. FB_VISUAL_PSEUDOCOLOR : FB_VISUAL_DIRECTCOLOR;
  527. nvidia_init_vga(info);
  528. nvidia_calc_regs(info);
  529. NVLockUnlock(par, 0);
  530. if (par->twoHeads) {
  531. VGA_WR08(par->PCIO, 0x03D4, 0x44);
  532. VGA_WR08(par->PCIO, 0x03D5, par->ModeReg.crtcOwner);
  533. NVLockUnlock(par, 0);
  534. }
  535. nvidia_screen_off(par, 1);
  536. nvidia_write_regs(par, &par->ModeReg);
  537. NVSetStartAddress(par, 0);
  538. #if defined (__BIG_ENDIAN)
  539. /* turn on LFB swapping */
  540. {
  541. unsigned char tmp;
  542. VGA_WR08(par->PCIO, 0x3d4, 0x46);
  543. tmp = VGA_RD08(par->PCIO, 0x3d5);
  544. tmp |= (1 << 7);
  545. VGA_WR08(par->PCIO, 0x3d5, tmp);
  546. }
  547. #endif
  548. info->fix.line_length = (info->var.xres_virtual *
  549. info->var.bits_per_pixel) >> 3;
  550. if (info->var.accel_flags) {
  551. info->fbops->fb_imageblit = nvidiafb_imageblit;
  552. info->fbops->fb_fillrect = nvidiafb_fillrect;
  553. info->fbops->fb_copyarea = nvidiafb_copyarea;
  554. info->fbops->fb_sync = nvidiafb_sync;
  555. info->pixmap.scan_align = 4;
  556. info->flags &= ~FBINFO_HWACCEL_DISABLED;
  557. info->flags |= FBINFO_READS_FAST;
  558. NVResetGraphics(info);
  559. } else {
  560. info->fbops->fb_imageblit = cfb_imageblit;
  561. info->fbops->fb_fillrect = cfb_fillrect;
  562. info->fbops->fb_copyarea = cfb_copyarea;
  563. info->fbops->fb_sync = NULL;
  564. info->pixmap.scan_align = 1;
  565. info->flags |= FBINFO_HWACCEL_DISABLED;
  566. info->flags &= ~FBINFO_READS_FAST;
  567. }
  568. par->cursor_reset = 1;
  569. nvidia_screen_off(par, 0);
  570. #ifdef CONFIG_BOOTX_TEXT
  571. /* Update debug text engine */
  572. btext_update_display(info->fix.smem_start,
  573. info->var.xres, info->var.yres,
  574. info->var.bits_per_pixel, info->fix.line_length);
  575. #endif
  576. NVLockUnlock(par, 0);
  577. NVTRACE_LEAVE();
  578. return 0;
  579. }
  580. static int nvidiafb_setcolreg(unsigned regno, unsigned red, unsigned green,
  581. unsigned blue, unsigned transp,
  582. struct fb_info *info)
  583. {
  584. struct nvidia_par *par = info->par;
  585. int i;
  586. NVTRACE_ENTER();
  587. if (regno >= (1 << info->var.green.length))
  588. return -EINVAL;
  589. if (info->var.grayscale) {
  590. /* gray = 0.30*R + 0.59*G + 0.11*B */
  591. red = green = blue = (red * 77 + green * 151 + blue * 28) >> 8;
  592. }
  593. if (regno < 16 && info->fix.visual == FB_VISUAL_DIRECTCOLOR) {
  594. ((u32 *) info->pseudo_palette)[regno] =
  595. (regno << info->var.red.offset) |
  596. (regno << info->var.green.offset) |
  597. (regno << info->var.blue.offset);
  598. }
  599. switch (info->var.bits_per_pixel) {
  600. case 8:
  601. /* "transparent" stuff is completely ignored. */
  602. nvidia_write_clut(par, regno, red >> 8, green >> 8, blue >> 8);
  603. break;
  604. case 16:
  605. if (info->var.green.length == 5) {
  606. for (i = 0; i < 8; i++) {
  607. nvidia_write_clut(par, regno * 8 + i, red >> 8,
  608. green >> 8, blue >> 8);
  609. }
  610. } else {
  611. u8 r, g, b;
  612. if (regno < 32) {
  613. for (i = 0; i < 8; i++) {
  614. nvidia_write_clut(par, regno * 8 + i,
  615. red >> 8, green >> 8,
  616. blue >> 8);
  617. }
  618. }
  619. nvidia_read_clut(par, regno * 4, &r, &g, &b);
  620. for (i = 0; i < 4; i++)
  621. nvidia_write_clut(par, regno * 4 + i, r,
  622. green >> 8, b);
  623. }
  624. break;
  625. case 32:
  626. nvidia_write_clut(par, regno, red >> 8, green >> 8, blue >> 8);
  627. break;
  628. default:
  629. /* do nothing */
  630. break;
  631. }
  632. NVTRACE_LEAVE();
  633. return 0;
  634. }
  635. static int nvidiafb_check_var(struct fb_var_screeninfo *var,
  636. struct fb_info *info)
  637. {
  638. struct nvidia_par *par = info->par;
  639. int memlen, vramlen, mode_valid = 0;
  640. int pitch, err = 0;
  641. NVTRACE_ENTER();
  642. var->transp.offset = 0;
  643. var->transp.length = 0;
  644. var->xres &= ~7;
  645. if (var->bits_per_pixel <= 8)
  646. var->bits_per_pixel = 8;
  647. else if (var->bits_per_pixel <= 16)
  648. var->bits_per_pixel = 16;
  649. else
  650. var->bits_per_pixel = 32;
  651. switch (var->bits_per_pixel) {
  652. case 8:
  653. var->red.offset = 0;
  654. var->red.length = 8;
  655. var->green.offset = 0;
  656. var->green.length = 8;
  657. var->blue.offset = 0;
  658. var->blue.length = 8;
  659. var->transp.offset = 0;
  660. var->transp.length = 0;
  661. break;
  662. case 16:
  663. var->green.length = (var->green.length < 6) ? 5 : 6;
  664. var->red.length = 5;
  665. var->blue.length = 5;
  666. var->transp.length = 6 - var->green.length;
  667. var->blue.offset = 0;
  668. var->green.offset = 5;
  669. var->red.offset = 5 + var->green.length;
  670. var->transp.offset = (5 + var->red.offset) & 15;
  671. break;
  672. case 32: /* RGBA 8888 */
  673. var->red.offset = 16;
  674. var->red.length = 8;
  675. var->green.offset = 8;
  676. var->green.length = 8;
  677. var->blue.offset = 0;
  678. var->blue.length = 8;
  679. var->transp.length = 8;
  680. var->transp.offset = 24;
  681. break;
  682. }
  683. var->red.msb_right = 0;
  684. var->green.msb_right = 0;
  685. var->blue.msb_right = 0;
  686. var->transp.msb_right = 0;
  687. if (!info->monspecs.hfmax || !info->monspecs.vfmax ||
  688. !info->monspecs.dclkmax || !fb_validate_mode(var, info))
  689. mode_valid = 1;
  690. /* calculate modeline if supported by monitor */
  691. if (!mode_valid && info->monspecs.gtf) {
  692. if (!fb_get_mode(FB_MAXTIMINGS, 0, var, info))
  693. mode_valid = 1;
  694. }
  695. if (!mode_valid) {
  696. const struct fb_videomode *mode;
  697. mode = fb_find_best_mode(var, &info->modelist);
  698. if (mode) {
  699. fb_videomode_to_var(var, mode);
  700. mode_valid = 1;
  701. }
  702. }
  703. if (!mode_valid && info->monspecs.modedb_len)
  704. return -EINVAL;
  705. /*
  706. * If we're on a flat panel, check if the mode is outside of the
  707. * panel dimensions. If so, cap it and try for the next best mode
  708. * before bailing out.
  709. */
  710. if (par->fpWidth && par->fpHeight && (par->fpWidth < var->xres ||
  711. par->fpHeight < var->yres)) {
  712. const struct fb_videomode *mode;
  713. var->xres = par->fpWidth;
  714. var->yres = par->fpHeight;
  715. mode = fb_find_best_mode(var, &info->modelist);
  716. if (!mode) {
  717. printk(KERN_ERR PFX "mode out of range of flat "
  718. "panel dimensions\n");
  719. return -EINVAL;
  720. }
  721. fb_videomode_to_var(var, mode);
  722. }
  723. if (var->yres_virtual < var->yres)
  724. var->yres_virtual = var->yres;
  725. if (var->xres_virtual < var->xres)
  726. var->xres_virtual = var->xres;
  727. var->xres_virtual = (var->xres_virtual + 63) & ~63;
  728. vramlen = info->screen_size;
  729. pitch = ((var->xres_virtual * var->bits_per_pixel) + 7) / 8;
  730. memlen = pitch * var->yres_virtual;
  731. if (memlen > vramlen) {
  732. var->yres_virtual = vramlen / pitch;
  733. if (var->yres_virtual < var->yres) {
  734. var->yres_virtual = var->yres;
  735. var->xres_virtual = vramlen / var->yres_virtual;
  736. var->xres_virtual /= var->bits_per_pixel / 8;
  737. var->xres_virtual &= ~63;
  738. pitch = (var->xres_virtual *
  739. var->bits_per_pixel + 7) / 8;
  740. memlen = pitch * var->yres;
  741. if (var->xres_virtual < var->xres) {
  742. printk("nvidiafb: required video memory, "
  743. "%d bytes, for %dx%d-%d (virtual) "
  744. "is out of range\n",
  745. memlen, var->xres_virtual,
  746. var->yres_virtual, var->bits_per_pixel);
  747. err = -ENOMEM;
  748. }
  749. }
  750. }
  751. if (var->accel_flags) {
  752. if (var->yres_virtual > 0x7fff)
  753. var->yres_virtual = 0x7fff;
  754. if (var->xres_virtual > 0x7fff)
  755. var->xres_virtual = 0x7fff;
  756. }
  757. var->xres_virtual &= ~63;
  758. NVTRACE_LEAVE();
  759. return err;
  760. }
  761. static int nvidiafb_pan_display(struct fb_var_screeninfo *var,
  762. struct fb_info *info)
  763. {
  764. struct nvidia_par *par = info->par;
  765. u32 total;
  766. total = var->yoffset * info->fix.line_length + var->xoffset;
  767. NVSetStartAddress(par, total);
  768. return 0;
  769. }
  770. static int nvidiafb_blank(int blank, struct fb_info *info)
  771. {
  772. struct nvidia_par *par = info->par;
  773. unsigned char tmp, vesa;
  774. tmp = NVReadSeq(par, 0x01) & ~0x20; /* screen on/off */
  775. vesa = NVReadCrtc(par, 0x1a) & ~0xc0; /* sync on/off */
  776. NVTRACE_ENTER();
  777. if (blank)
  778. tmp |= 0x20;
  779. switch (blank) {
  780. case FB_BLANK_UNBLANK:
  781. case FB_BLANK_NORMAL:
  782. break;
  783. case FB_BLANK_VSYNC_SUSPEND:
  784. vesa |= 0x80;
  785. break;
  786. case FB_BLANK_HSYNC_SUSPEND:
  787. vesa |= 0x40;
  788. break;
  789. case FB_BLANK_POWERDOWN:
  790. vesa |= 0xc0;
  791. break;
  792. }
  793. NVWriteSeq(par, 0x01, tmp);
  794. NVWriteCrtc(par, 0x1a, vesa);
  795. NVTRACE_LEAVE();
  796. return 0;
  797. }
  798. /*
  799. * Because the VGA registers are not mapped linearly in its MMIO space,
  800. * restrict VGA register saving and restore to x86 only, where legacy VGA IO
  801. * access is legal. Consequently, we must also check if the device is the
  802. * primary display.
  803. */
  804. #ifdef CONFIG_X86
  805. static void save_vga_x86(struct nvidia_par *par)
  806. {
  807. struct resource *res= &par->pci_dev->resource[PCI_ROM_RESOURCE];
  808. if (res && res->flags & IORESOURCE_ROM_SHADOW) {
  809. memset(&par->vgastate, 0, sizeof(par->vgastate));
  810. par->vgastate.flags = VGA_SAVE_MODE | VGA_SAVE_FONTS |
  811. VGA_SAVE_CMAP;
  812. save_vga(&par->vgastate);
  813. }
  814. }
  815. static void restore_vga_x86(struct nvidia_par *par)
  816. {
  817. struct resource *res= &par->pci_dev->resource[PCI_ROM_RESOURCE];
  818. if (res && res->flags & IORESOURCE_ROM_SHADOW)
  819. restore_vga(&par->vgastate);
  820. }
  821. #else
  822. #define save_vga_x86(x) do {} while (0)
  823. #define restore_vga_x86(x) do {} while (0)
  824. #endif /* X86 */
  825. static int nvidiafb_open(struct fb_info *info, int user)
  826. {
  827. struct nvidia_par *par = info->par;
  828. if (!par->open_count) {
  829. save_vga_x86(par);
  830. nvidia_save_vga(par, &par->initial_state);
  831. }
  832. par->open_count++;
  833. return 0;
  834. }
  835. static int nvidiafb_release(struct fb_info *info, int user)
  836. {
  837. struct nvidia_par *par = info->par;
  838. int err = 0;
  839. if (!par->open_count) {
  840. err = -EINVAL;
  841. goto done;
  842. }
  843. if (par->open_count == 1) {
  844. nvidia_write_regs(par, &par->initial_state);
  845. restore_vga_x86(par);
  846. }
  847. par->open_count--;
  848. done:
  849. return err;
  850. }
  851. static struct fb_ops nvidia_fb_ops = {
  852. .owner = THIS_MODULE,
  853. .fb_open = nvidiafb_open,
  854. .fb_release = nvidiafb_release,
  855. .fb_check_var = nvidiafb_check_var,
  856. .fb_set_par = nvidiafb_set_par,
  857. .fb_setcolreg = nvidiafb_setcolreg,
  858. .fb_pan_display = nvidiafb_pan_display,
  859. .fb_blank = nvidiafb_blank,
  860. .fb_fillrect = nvidiafb_fillrect,
  861. .fb_copyarea = nvidiafb_copyarea,
  862. .fb_imageblit = nvidiafb_imageblit,
  863. .fb_cursor = nvidiafb_cursor,
  864. .fb_sync = nvidiafb_sync,
  865. };
  866. #ifdef CONFIG_PM
  867. static int nvidiafb_suspend(struct pci_dev *dev, pm_message_t mesg)
  868. {
  869. struct fb_info *info = pci_get_drvdata(dev);
  870. struct nvidia_par *par = info->par;
  871. if (mesg.event == PM_EVENT_PRETHAW)
  872. mesg.event = PM_EVENT_FREEZE;
  873. console_lock();
  874. par->pm_state = mesg.event;
  875. if (mesg.event & PM_EVENT_SLEEP) {
  876. fb_set_suspend(info, 1);
  877. nvidiafb_blank(FB_BLANK_POWERDOWN, info);
  878. nvidia_write_regs(par, &par->SavedReg);
  879. pci_save_state(dev);
  880. pci_disable_device(dev);
  881. pci_set_power_state(dev, pci_choose_state(dev, mesg));
  882. }
  883. dev->dev.power.power_state = mesg;
  884. console_unlock();
  885. return 0;
  886. }
  887. static int nvidiafb_resume(struct pci_dev *dev)
  888. {
  889. struct fb_info *info = pci_get_drvdata(dev);
  890. struct nvidia_par *par = info->par;
  891. console_lock();
  892. pci_set_power_state(dev, PCI_D0);
  893. if (par->pm_state != PM_EVENT_FREEZE) {
  894. pci_restore_state(dev);
  895. if (pci_enable_device(dev))
  896. goto fail;
  897. pci_set_master(dev);
  898. }
  899. par->pm_state = PM_EVENT_ON;
  900. nvidiafb_set_par(info);
  901. fb_set_suspend (info, 0);
  902. nvidiafb_blank(FB_BLANK_UNBLANK, info);
  903. fail:
  904. console_unlock();
  905. return 0;
  906. }
  907. #else
  908. #define nvidiafb_suspend NULL
  909. #define nvidiafb_resume NULL
  910. #endif
  911. static int nvidia_set_fbinfo(struct fb_info *info)
  912. {
  913. struct fb_monspecs *specs = &info->monspecs;
  914. struct fb_videomode modedb;
  915. struct nvidia_par *par = info->par;
  916. int lpitch;
  917. NVTRACE_ENTER();
  918. info->flags = FBINFO_DEFAULT
  919. | FBINFO_HWACCEL_IMAGEBLIT
  920. | FBINFO_HWACCEL_FILLRECT
  921. | FBINFO_HWACCEL_COPYAREA
  922. | FBINFO_HWACCEL_YPAN;
  923. fb_videomode_to_modelist(info->monspecs.modedb,
  924. info->monspecs.modedb_len, &info->modelist);
  925. fb_var_to_videomode(&modedb, &nvidiafb_default_var);
  926. switch (bpp) {
  927. case 0 ... 8:
  928. bpp = 8;
  929. break;
  930. case 9 ... 16:
  931. bpp = 16;
  932. break;
  933. default:
  934. bpp = 32;
  935. break;
  936. }
  937. if (specs->modedb != NULL) {
  938. const struct fb_videomode *mode;
  939. mode = fb_find_best_display(specs, &info->modelist);
  940. fb_videomode_to_var(&nvidiafb_default_var, mode);
  941. nvidiafb_default_var.bits_per_pixel = bpp;
  942. } else if (par->fpWidth && par->fpHeight) {
  943. char buf[16];
  944. memset(buf, 0, 16);
  945. snprintf(buf, 15, "%dx%dMR", par->fpWidth, par->fpHeight);
  946. fb_find_mode(&nvidiafb_default_var, info, buf, specs->modedb,
  947. specs->modedb_len, &modedb, bpp);
  948. }
  949. if (mode_option)
  950. fb_find_mode(&nvidiafb_default_var, info, mode_option,
  951. specs->modedb, specs->modedb_len, &modedb, bpp);
  952. info->var = nvidiafb_default_var;
  953. info->fix.visual = (info->var.bits_per_pixel == 8) ?
  954. FB_VISUAL_PSEUDOCOLOR : FB_VISUAL_DIRECTCOLOR;
  955. info->pseudo_palette = par->pseudo_palette;
  956. fb_alloc_cmap(&info->cmap, 256, 0);
  957. fb_destroy_modedb(info->monspecs.modedb);
  958. info->monspecs.modedb = NULL;
  959. /* maximize virtual vertical length */
  960. lpitch = info->var.xres_virtual *
  961. ((info->var.bits_per_pixel + 7) >> 3);
  962. info->var.yres_virtual = info->screen_size / lpitch;
  963. info->pixmap.scan_align = 4;
  964. info->pixmap.buf_align = 4;
  965. info->pixmap.access_align = 32;
  966. info->pixmap.size = 8 * 1024;
  967. info->pixmap.flags = FB_PIXMAP_SYSTEM;
  968. if (!hwcur)
  969. info->fbops->fb_cursor = NULL;
  970. info->var.accel_flags = (!noaccel);
  971. switch (par->Architecture) {
  972. case NV_ARCH_04:
  973. info->fix.accel = FB_ACCEL_NV4;
  974. break;
  975. case NV_ARCH_10:
  976. info->fix.accel = FB_ACCEL_NV_10;
  977. break;
  978. case NV_ARCH_20:
  979. info->fix.accel = FB_ACCEL_NV_20;
  980. break;
  981. case NV_ARCH_30:
  982. info->fix.accel = FB_ACCEL_NV_30;
  983. break;
  984. case NV_ARCH_40:
  985. info->fix.accel = FB_ACCEL_NV_40;
  986. break;
  987. }
  988. NVTRACE_LEAVE();
  989. return nvidiafb_check_var(&info->var, info);
  990. }
  991. static u32 nvidia_get_chipset(struct fb_info *info)
  992. {
  993. struct nvidia_par *par = info->par;
  994. u32 id = (par->pci_dev->vendor << 16) | par->pci_dev->device;
  995. printk(KERN_INFO PFX "Device ID: %x \n", id);
  996. if ((id & 0xfff0) == 0x00f0 ||
  997. (id & 0xfff0) == 0x02e0) {
  998. /* pci-e */
  999. id = NV_RD32(par->REGS, 0x1800);
  1000. if ((id & 0x0000ffff) == 0x000010DE)
  1001. id = 0x10DE0000 | (id >> 16);
  1002. else if ((id & 0xffff0000) == 0xDE100000) /* wrong endian */
  1003. id = 0x10DE0000 | ((id << 8) & 0x0000ff00) |
  1004. ((id >> 8) & 0x000000ff);
  1005. printk(KERN_INFO PFX "Subsystem ID: %x \n", id);
  1006. }
  1007. return id;
  1008. }
  1009. static u32 nvidia_get_arch(struct fb_info *info)
  1010. {
  1011. struct nvidia_par *par = info->par;
  1012. u32 arch = 0;
  1013. switch (par->Chipset & 0x0ff0) {
  1014. case 0x0100: /* GeForce 256 */
  1015. case 0x0110: /* GeForce2 MX */
  1016. case 0x0150: /* GeForce2 */
  1017. case 0x0170: /* GeForce4 MX */
  1018. case 0x0180: /* GeForce4 MX (8x AGP) */
  1019. case 0x01A0: /* nForce */
  1020. case 0x01F0: /* nForce2 */
  1021. arch = NV_ARCH_10;
  1022. break;
  1023. case 0x0200: /* GeForce3 */
  1024. case 0x0250: /* GeForce4 Ti */
  1025. case 0x0280: /* GeForce4 Ti (8x AGP) */
  1026. arch = NV_ARCH_20;
  1027. break;
  1028. case 0x0300: /* GeForceFX 5800 */
  1029. case 0x0310: /* GeForceFX 5600 */
  1030. case 0x0320: /* GeForceFX 5200 */
  1031. case 0x0330: /* GeForceFX 5900 */
  1032. case 0x0340: /* GeForceFX 5700 */
  1033. arch = NV_ARCH_30;
  1034. break;
  1035. case 0x0040: /* GeForce 6800 */
  1036. case 0x00C0: /* GeForce 6800 */
  1037. case 0x0120: /* GeForce 6800 */
  1038. case 0x0140: /* GeForce 6600 */
  1039. case 0x0160: /* GeForce 6200 */
  1040. case 0x01D0: /* GeForce 7200, 7300, 7400 */
  1041. case 0x0090: /* GeForce 7800 */
  1042. case 0x0210: /* GeForce 6800 */
  1043. case 0x0220: /* GeForce 6200 */
  1044. case 0x0240: /* GeForce 6100 */
  1045. case 0x0290: /* GeForce 7900 */
  1046. case 0x0390: /* GeForce 7600 */
  1047. case 0x03D0:
  1048. arch = NV_ARCH_40;
  1049. break;
  1050. case 0x0020: /* TNT, TNT2 */
  1051. arch = NV_ARCH_04;
  1052. break;
  1053. default: /* unknown architecture */
  1054. break;
  1055. }
  1056. return arch;
  1057. }
  1058. static int nvidiafb_probe(struct pci_dev *pd, const struct pci_device_id *ent)
  1059. {
  1060. struct nvidia_par *par;
  1061. struct fb_info *info;
  1062. unsigned short cmd;
  1063. NVTRACE_ENTER();
  1064. assert(pd != NULL);
  1065. info = framebuffer_alloc(sizeof(struct nvidia_par), &pd->dev);
  1066. if (!info)
  1067. goto err_out;
  1068. par = info->par;
  1069. par->pci_dev = pd;
  1070. info->pixmap.addr = kzalloc(8 * 1024, GFP_KERNEL);
  1071. if (info->pixmap.addr == NULL)
  1072. goto err_out_kfree;
  1073. if (pci_enable_device(pd)) {
  1074. printk(KERN_ERR PFX "cannot enable PCI device\n");
  1075. goto err_out_enable;
  1076. }
  1077. if (pci_request_regions(pd, "nvidiafb")) {
  1078. printk(KERN_ERR PFX "cannot request PCI regions\n");
  1079. goto err_out_enable;
  1080. }
  1081. par->FlatPanel = flatpanel;
  1082. if (flatpanel == 1)
  1083. printk(KERN_INFO PFX "flatpanel support enabled\n");
  1084. par->FPDither = fpdither;
  1085. par->CRTCnumber = forceCRTC;
  1086. par->FpScale = (!noscale);
  1087. par->paneltweak = paneltweak;
  1088. par->reverse_i2c = reverse_i2c;
  1089. /* enable IO and mem if not already done */
  1090. pci_read_config_word(pd, PCI_COMMAND, &cmd);
  1091. cmd |= (PCI_COMMAND_IO | PCI_COMMAND_MEMORY);
  1092. pci_write_config_word(pd, PCI_COMMAND, cmd);
  1093. nvidiafb_fix.mmio_start = pci_resource_start(pd, 0);
  1094. nvidiafb_fix.smem_start = pci_resource_start(pd, 1);
  1095. nvidiafb_fix.mmio_len = pci_resource_len(pd, 0);
  1096. par->REGS = ioremap(nvidiafb_fix.mmio_start, nvidiafb_fix.mmio_len);
  1097. if (!par->REGS) {
  1098. printk(KERN_ERR PFX "cannot ioremap MMIO base\n");
  1099. goto err_out_free_base0;
  1100. }
  1101. par->Chipset = nvidia_get_chipset(info);
  1102. par->Architecture = nvidia_get_arch(info);
  1103. if (par->Architecture == 0) {
  1104. printk(KERN_ERR PFX "unknown NV_ARCH\n");
  1105. goto err_out_arch;
  1106. }
  1107. sprintf(nvidiafb_fix.id, "NV%x", (pd->device & 0x0ff0) >> 4);
  1108. if (NVCommonSetup(info))
  1109. goto err_out_arch;
  1110. par->FbAddress = nvidiafb_fix.smem_start;
  1111. par->FbMapSize = par->RamAmountKBytes * 1024;
  1112. if (vram && vram * 1024 * 1024 < par->FbMapSize)
  1113. par->FbMapSize = vram * 1024 * 1024;
  1114. /* Limit amount of vram to 64 MB */
  1115. if (par->FbMapSize > 64 * 1024 * 1024)
  1116. par->FbMapSize = 64 * 1024 * 1024;
  1117. if(par->Architecture >= NV_ARCH_40)
  1118. par->FbUsableSize = par->FbMapSize - (560 * 1024);
  1119. else
  1120. par->FbUsableSize = par->FbMapSize - (128 * 1024);
  1121. par->ScratchBufferSize = (par->Architecture < NV_ARCH_10) ? 8 * 1024 :
  1122. 16 * 1024;
  1123. par->ScratchBufferStart = par->FbUsableSize - par->ScratchBufferSize;
  1124. par->CursorStart = par->FbUsableSize + (32 * 1024);
  1125. info->screen_base = ioremap_wc(nvidiafb_fix.smem_start,
  1126. par->FbMapSize);
  1127. info->screen_size = par->FbUsableSize;
  1128. nvidiafb_fix.smem_len = par->RamAmountKBytes * 1024;
  1129. if (!info->screen_base) {
  1130. printk(KERN_ERR PFX "cannot ioremap FB base\n");
  1131. goto err_out_free_base1;
  1132. }
  1133. par->FbStart = info->screen_base;
  1134. if (!nomtrr)
  1135. par->wc_cookie = arch_phys_wc_add(nvidiafb_fix.smem_start,
  1136. par->RamAmountKBytes * 1024);
  1137. info->fbops = &nvidia_fb_ops;
  1138. info->fix = nvidiafb_fix;
  1139. if (nvidia_set_fbinfo(info) < 0) {
  1140. printk(KERN_ERR PFX "error setting initial video mode\n");
  1141. goto err_out_iounmap_fb;
  1142. }
  1143. nvidia_save_vga(par, &par->SavedReg);
  1144. pci_set_drvdata(pd, info);
  1145. if (backlight)
  1146. nvidia_bl_init(par);
  1147. if (register_framebuffer(info) < 0) {
  1148. printk(KERN_ERR PFX "error registering nVidia framebuffer\n");
  1149. goto err_out_iounmap_fb;
  1150. }
  1151. printk(KERN_INFO PFX
  1152. "PCI nVidia %s framebuffer (%dMB @ 0x%lX)\n",
  1153. info->fix.id,
  1154. par->FbMapSize / (1024 * 1024), info->fix.smem_start);
  1155. NVTRACE_LEAVE();
  1156. return 0;
  1157. err_out_iounmap_fb:
  1158. iounmap(info->screen_base);
  1159. err_out_free_base1:
  1160. fb_destroy_modedb(info->monspecs.modedb);
  1161. nvidia_delete_i2c_busses(par);
  1162. err_out_arch:
  1163. iounmap(par->REGS);
  1164. err_out_free_base0:
  1165. pci_release_regions(pd);
  1166. err_out_enable:
  1167. kfree(info->pixmap.addr);
  1168. err_out_kfree:
  1169. framebuffer_release(info);
  1170. err_out:
  1171. return -ENODEV;
  1172. }
  1173. static void nvidiafb_remove(struct pci_dev *pd)
  1174. {
  1175. struct fb_info *info = pci_get_drvdata(pd);
  1176. struct nvidia_par *par = info->par;
  1177. NVTRACE_ENTER();
  1178. unregister_framebuffer(info);
  1179. nvidia_bl_exit(par);
  1180. arch_phys_wc_del(par->wc_cookie);
  1181. iounmap(info->screen_base);
  1182. fb_destroy_modedb(info->monspecs.modedb);
  1183. nvidia_delete_i2c_busses(par);
  1184. iounmap(par->REGS);
  1185. pci_release_regions(pd);
  1186. kfree(info->pixmap.addr);
  1187. framebuffer_release(info);
  1188. NVTRACE_LEAVE();
  1189. }
  1190. /* ------------------------------------------------------------------------- *
  1191. *
  1192. * initialization
  1193. *
  1194. * ------------------------------------------------------------------------- */
  1195. #ifndef MODULE
  1196. static int nvidiafb_setup(char *options)
  1197. {
  1198. char *this_opt;
  1199. NVTRACE_ENTER();
  1200. if (!options || !*options)
  1201. return 0;
  1202. while ((this_opt = strsep(&options, ",")) != NULL) {
  1203. if (!strncmp(this_opt, "forceCRTC", 9)) {
  1204. char *p;
  1205. p = this_opt + 9;
  1206. if (!*p || !*(++p))
  1207. continue;
  1208. forceCRTC = *p - '0';
  1209. if (forceCRTC < 0 || forceCRTC > 1)
  1210. forceCRTC = -1;
  1211. } else if (!strncmp(this_opt, "flatpanel", 9)) {
  1212. flatpanel = 1;
  1213. } else if (!strncmp(this_opt, "hwcur", 5)) {
  1214. hwcur = 1;
  1215. } else if (!strncmp(this_opt, "noaccel", 6)) {
  1216. noaccel = 1;
  1217. } else if (!strncmp(this_opt, "noscale", 7)) {
  1218. noscale = 1;
  1219. } else if (!strncmp(this_opt, "reverse_i2c", 11)) {
  1220. reverse_i2c = 1;
  1221. } else if (!strncmp(this_opt, "paneltweak:", 11)) {
  1222. paneltweak = simple_strtoul(this_opt+11, NULL, 0);
  1223. } else if (!strncmp(this_opt, "vram:", 5)) {
  1224. vram = simple_strtoul(this_opt+5, NULL, 0);
  1225. } else if (!strncmp(this_opt, "backlight:", 10)) {
  1226. backlight = simple_strtoul(this_opt+10, NULL, 0);
  1227. } else if (!strncmp(this_opt, "nomtrr", 6)) {
  1228. nomtrr = true;
  1229. } else if (!strncmp(this_opt, "fpdither:", 9)) {
  1230. fpdither = simple_strtol(this_opt+9, NULL, 0);
  1231. } else if (!strncmp(this_opt, "bpp:", 4)) {
  1232. bpp = simple_strtoul(this_opt+4, NULL, 0);
  1233. } else
  1234. mode_option = this_opt;
  1235. }
  1236. NVTRACE_LEAVE();
  1237. return 0;
  1238. }
  1239. #endif /* !MODULE */
  1240. static struct pci_driver nvidiafb_driver = {
  1241. .name = "nvidiafb",
  1242. .id_table = nvidiafb_pci_tbl,
  1243. .probe = nvidiafb_probe,
  1244. .suspend = nvidiafb_suspend,
  1245. .resume = nvidiafb_resume,
  1246. .remove = nvidiafb_remove,
  1247. };
  1248. /* ------------------------------------------------------------------------- *
  1249. *
  1250. * modularization
  1251. *
  1252. * ------------------------------------------------------------------------- */
  1253. static int nvidiafb_init(void)
  1254. {
  1255. #ifndef MODULE
  1256. char *option = NULL;
  1257. if (fb_get_options("nvidiafb", &option))
  1258. return -ENODEV;
  1259. nvidiafb_setup(option);
  1260. #endif
  1261. return pci_register_driver(&nvidiafb_driver);
  1262. }
  1263. module_init(nvidiafb_init);
  1264. static void __exit nvidiafb_exit(void)
  1265. {
  1266. pci_unregister_driver(&nvidiafb_driver);
  1267. }
  1268. module_exit(nvidiafb_exit);
  1269. module_param(flatpanel, int, 0);
  1270. MODULE_PARM_DESC(flatpanel,
  1271. "Enables experimental flat panel support for some chipsets. "
  1272. "(0=disabled, 1=enabled, -1=autodetect) (default=-1)");
  1273. module_param(fpdither, int, 0);
  1274. MODULE_PARM_DESC(fpdither,
  1275. "Enables dithering of flat panel for 6 bits panels. "
  1276. "(0=disabled, 1=enabled, -1=autodetect) (default=-1)");
  1277. module_param(hwcur, int, 0);
  1278. MODULE_PARM_DESC(hwcur,
  1279. "Enables hardware cursor implementation. (0 or 1=enabled) "
  1280. "(default=0)");
  1281. module_param(noaccel, int, 0);
  1282. MODULE_PARM_DESC(noaccel,
  1283. "Disables hardware acceleration. (0 or 1=disable) "
  1284. "(default=0)");
  1285. module_param(noscale, int, 0);
  1286. MODULE_PARM_DESC(noscale,
  1287. "Disables screen scaleing. (0 or 1=disable) "
  1288. "(default=0, do scaling)");
  1289. module_param(paneltweak, int, 0);
  1290. MODULE_PARM_DESC(paneltweak,
  1291. "Tweak display settings for flatpanels. "
  1292. "(default=0, no tweaks)");
  1293. module_param(forceCRTC, int, 0);
  1294. MODULE_PARM_DESC(forceCRTC,
  1295. "Forces usage of a particular CRTC in case autodetection "
  1296. "fails. (0 or 1) (default=autodetect)");
  1297. module_param(vram, int, 0);
  1298. MODULE_PARM_DESC(vram,
  1299. "amount of framebuffer memory to remap in MiB"
  1300. "(default=0 - remap entire memory)");
  1301. module_param(mode_option, charp, 0);
  1302. MODULE_PARM_DESC(mode_option, "Specify initial video mode");
  1303. module_param(bpp, int, 0);
  1304. MODULE_PARM_DESC(bpp, "pixel width in bits"
  1305. "(default=8)");
  1306. module_param(reverse_i2c, int, 0);
  1307. MODULE_PARM_DESC(reverse_i2c, "reverse port assignment of the i2c bus");
  1308. module_param(nomtrr, bool, false);
  1309. MODULE_PARM_DESC(nomtrr, "Disables MTRR support (0 or 1=disabled) "
  1310. "(default=0)");
  1311. MODULE_AUTHOR("Antonino Daplas");
  1312. MODULE_DESCRIPTION("Framebuffer driver for nVidia graphics chipset");
  1313. MODULE_LICENSE("GPL");