nvidia.c 39 KB

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