nvidia.c 39 KB

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