i740fb.c 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310
  1. /*
  2. * i740fb - framebuffer driver for Intel740
  3. * Copyright (c) 2011 Ondrej Zary
  4. *
  5. * Based on old i740fb driver (c) 2001-2002 Andrey Ulanov <drey@rt.mipt.ru>
  6. * which was partially based on:
  7. * VGA 16-color framebuffer driver (c) 1999 Ben Pfaff <pfaffben@debian.org>
  8. * and Petr Vandrovec <VANDROVE@vc.cvut.cz>
  9. * i740 driver from XFree86 (c) 1998-1999 Precision Insight, Inc., Cedar Park,
  10. * Texas.
  11. * i740fb by Patrick LERDA, v0.9
  12. */
  13. #include <linux/module.h>
  14. #include <linux/kernel.h>
  15. #include <linux/errno.h>
  16. #include <linux/string.h>
  17. #include <linux/mm.h>
  18. #include <linux/slab.h>
  19. #include <linux/delay.h>
  20. #include <linux/fb.h>
  21. #include <linux/init.h>
  22. #include <linux/pci.h>
  23. #include <linux/pci_ids.h>
  24. #include <linux/i2c.h>
  25. #include <linux/i2c-algo-bit.h>
  26. #include <linux/console.h>
  27. #include <video/vga.h>
  28. #include "i740_reg.h"
  29. static char *mode_option;
  30. static int mtrr = 1;
  31. struct i740fb_par {
  32. unsigned char __iomem *regs;
  33. bool has_sgram;
  34. int wc_cookie;
  35. bool ddc_registered;
  36. struct i2c_adapter ddc_adapter;
  37. struct i2c_algo_bit_data ddc_algo;
  38. u32 pseudo_palette[16];
  39. struct mutex open_lock;
  40. unsigned int ref_count;
  41. u8 crtc[VGA_CRT_C];
  42. u8 atc[VGA_ATT_C];
  43. u8 gdc[VGA_GFX_C];
  44. u8 seq[VGA_SEQ_C];
  45. u8 misc;
  46. u8 vss;
  47. /* i740 specific registers */
  48. u8 display_cntl;
  49. u8 pixelpipe_cfg0;
  50. u8 pixelpipe_cfg1;
  51. u8 pixelpipe_cfg2;
  52. u8 video_clk2_m;
  53. u8 video_clk2_n;
  54. u8 video_clk2_mn_msbs;
  55. u8 video_clk2_div_sel;
  56. u8 pll_cntl;
  57. u8 address_mapping;
  58. u8 io_cntl;
  59. u8 bitblt_cntl;
  60. u8 ext_vert_total;
  61. u8 ext_vert_disp_end;
  62. u8 ext_vert_sync_start;
  63. u8 ext_vert_blank_start;
  64. u8 ext_horiz_total;
  65. u8 ext_horiz_blank;
  66. u8 ext_offset;
  67. u8 interlace_cntl;
  68. u32 lmi_fifo_watermark;
  69. u8 ext_start_addr;
  70. u8 ext_start_addr_hi;
  71. };
  72. #define DACSPEED8 203
  73. #define DACSPEED16 163
  74. #define DACSPEED24_SG 136
  75. #define DACSPEED24_SD 128
  76. #define DACSPEED32 86
  77. static struct fb_fix_screeninfo i740fb_fix = {
  78. .id = "i740fb",
  79. .type = FB_TYPE_PACKED_PIXELS,
  80. .visual = FB_VISUAL_TRUECOLOR,
  81. .xpanstep = 8,
  82. .ypanstep = 1,
  83. .accel = FB_ACCEL_NONE,
  84. };
  85. static inline void i740outb(struct i740fb_par *par, u16 port, u8 val)
  86. {
  87. vga_mm_w(par->regs, port, val);
  88. }
  89. static inline u8 i740inb(struct i740fb_par *par, u16 port)
  90. {
  91. return vga_mm_r(par->regs, port);
  92. }
  93. static inline void i740outreg(struct i740fb_par *par, u16 port, u8 reg, u8 val)
  94. {
  95. vga_mm_w_fast(par->regs, port, reg, val);
  96. }
  97. static inline u8 i740inreg(struct i740fb_par *par, u16 port, u8 reg)
  98. {
  99. vga_mm_w(par->regs, port, reg);
  100. return vga_mm_r(par->regs, port+1);
  101. }
  102. static inline void i740outreg_mask(struct i740fb_par *par, u16 port, u8 reg,
  103. u8 val, u8 mask)
  104. {
  105. vga_mm_w_fast(par->regs, port, reg, (val & mask)
  106. | (i740inreg(par, port, reg) & ~mask));
  107. }
  108. #define REG_DDC_DRIVE 0x62
  109. #define REG_DDC_STATE 0x63
  110. #define DDC_SCL (1 << 3)
  111. #define DDC_SDA (1 << 2)
  112. static void i740fb_ddc_setscl(void *data, int val)
  113. {
  114. struct i740fb_par *par = data;
  115. i740outreg_mask(par, XRX, REG_DDC_DRIVE, DDC_SCL, DDC_SCL);
  116. i740outreg_mask(par, XRX, REG_DDC_STATE, val ? DDC_SCL : 0, DDC_SCL);
  117. }
  118. static void i740fb_ddc_setsda(void *data, int val)
  119. {
  120. struct i740fb_par *par = data;
  121. i740outreg_mask(par, XRX, REG_DDC_DRIVE, DDC_SDA, DDC_SDA);
  122. i740outreg_mask(par, XRX, REG_DDC_STATE, val ? DDC_SDA : 0, DDC_SDA);
  123. }
  124. static int i740fb_ddc_getscl(void *data)
  125. {
  126. struct i740fb_par *par = data;
  127. i740outreg_mask(par, XRX, REG_DDC_DRIVE, 0, DDC_SCL);
  128. return !!(i740inreg(par, XRX, REG_DDC_STATE) & DDC_SCL);
  129. }
  130. static int i740fb_ddc_getsda(void *data)
  131. {
  132. struct i740fb_par *par = data;
  133. i740outreg_mask(par, XRX, REG_DDC_DRIVE, 0, DDC_SDA);
  134. return !!(i740inreg(par, XRX, REG_DDC_STATE) & DDC_SDA);
  135. }
  136. static int i740fb_setup_ddc_bus(struct fb_info *info)
  137. {
  138. struct i740fb_par *par = info->par;
  139. strlcpy(par->ddc_adapter.name, info->fix.id,
  140. sizeof(par->ddc_adapter.name));
  141. par->ddc_adapter.owner = THIS_MODULE;
  142. par->ddc_adapter.class = I2C_CLASS_DDC;
  143. par->ddc_adapter.algo_data = &par->ddc_algo;
  144. par->ddc_adapter.dev.parent = info->device;
  145. par->ddc_algo.setsda = i740fb_ddc_setsda;
  146. par->ddc_algo.setscl = i740fb_ddc_setscl;
  147. par->ddc_algo.getsda = i740fb_ddc_getsda;
  148. par->ddc_algo.getscl = i740fb_ddc_getscl;
  149. par->ddc_algo.udelay = 10;
  150. par->ddc_algo.timeout = 20;
  151. par->ddc_algo.data = par;
  152. i2c_set_adapdata(&par->ddc_adapter, par);
  153. return i2c_bit_add_bus(&par->ddc_adapter);
  154. }
  155. static int i740fb_open(struct fb_info *info, int user)
  156. {
  157. struct i740fb_par *par = info->par;
  158. mutex_lock(&(par->open_lock));
  159. par->ref_count++;
  160. mutex_unlock(&(par->open_lock));
  161. return 0;
  162. }
  163. static int i740fb_release(struct fb_info *info, int user)
  164. {
  165. struct i740fb_par *par = info->par;
  166. mutex_lock(&(par->open_lock));
  167. if (par->ref_count == 0) {
  168. fb_err(info, "release called with zero refcount\n");
  169. mutex_unlock(&(par->open_lock));
  170. return -EINVAL;
  171. }
  172. par->ref_count--;
  173. mutex_unlock(&(par->open_lock));
  174. return 0;
  175. }
  176. static u32 i740_calc_fifo(struct i740fb_par *par, u32 freq, int bpp)
  177. {
  178. /*
  179. * Would like to calculate these values automatically, but a generic
  180. * algorithm does not seem possible. Note: These FIFO water mark
  181. * values were tested on several cards and seem to eliminate the
  182. * all of the snow and vertical banding, but fine adjustments will
  183. * probably be required for other cards.
  184. */
  185. u32 wm;
  186. switch (bpp) {
  187. case 8:
  188. if (freq > 200)
  189. wm = 0x18120000;
  190. else if (freq > 175)
  191. wm = 0x16110000;
  192. else if (freq > 135)
  193. wm = 0x120E0000;
  194. else
  195. wm = 0x100D0000;
  196. break;
  197. case 15:
  198. case 16:
  199. if (par->has_sgram) {
  200. if (freq > 140)
  201. wm = 0x2C1D0000;
  202. else if (freq > 120)
  203. wm = 0x2C180000;
  204. else if (freq > 100)
  205. wm = 0x24160000;
  206. else if (freq > 90)
  207. wm = 0x18120000;
  208. else if (freq > 50)
  209. wm = 0x16110000;
  210. else if (freq > 32)
  211. wm = 0x13100000;
  212. else
  213. wm = 0x120E0000;
  214. } else {
  215. if (freq > 160)
  216. wm = 0x28200000;
  217. else if (freq > 140)
  218. wm = 0x2A1E0000;
  219. else if (freq > 130)
  220. wm = 0x2B1A0000;
  221. else if (freq > 120)
  222. wm = 0x2C180000;
  223. else if (freq > 100)
  224. wm = 0x24180000;
  225. else if (freq > 90)
  226. wm = 0x18120000;
  227. else if (freq > 50)
  228. wm = 0x16110000;
  229. else if (freq > 32)
  230. wm = 0x13100000;
  231. else
  232. wm = 0x120E0000;
  233. }
  234. break;
  235. case 24:
  236. if (par->has_sgram) {
  237. if (freq > 130)
  238. wm = 0x31200000;
  239. else if (freq > 120)
  240. wm = 0x2E200000;
  241. else if (freq > 100)
  242. wm = 0x2C1D0000;
  243. else if (freq > 80)
  244. wm = 0x25180000;
  245. else if (freq > 64)
  246. wm = 0x24160000;
  247. else if (freq > 49)
  248. wm = 0x18120000;
  249. else if (freq > 32)
  250. wm = 0x16110000;
  251. else
  252. wm = 0x13100000;
  253. } else {
  254. if (freq > 120)
  255. wm = 0x311F0000;
  256. else if (freq > 100)
  257. wm = 0x2C1D0000;
  258. else if (freq > 80)
  259. wm = 0x25180000;
  260. else if (freq > 64)
  261. wm = 0x24160000;
  262. else if (freq > 49)
  263. wm = 0x18120000;
  264. else if (freq > 32)
  265. wm = 0x16110000;
  266. else
  267. wm = 0x13100000;
  268. }
  269. break;
  270. case 32:
  271. if (par->has_sgram) {
  272. if (freq > 80)
  273. wm = 0x2A200000;
  274. else if (freq > 60)
  275. wm = 0x281A0000;
  276. else if (freq > 49)
  277. wm = 0x25180000;
  278. else if (freq > 32)
  279. wm = 0x18120000;
  280. else
  281. wm = 0x16110000;
  282. } else {
  283. if (freq > 80)
  284. wm = 0x29200000;
  285. else if (freq > 60)
  286. wm = 0x281A0000;
  287. else if (freq > 49)
  288. wm = 0x25180000;
  289. else if (freq > 32)
  290. wm = 0x18120000;
  291. else
  292. wm = 0x16110000;
  293. }
  294. break;
  295. }
  296. return wm;
  297. }
  298. /* clock calculation from i740fb by Patrick LERDA */
  299. #define I740_RFREQ 1000000
  300. #define TARGET_MAX_N 30
  301. #define I740_FFIX (1 << 8)
  302. #define I740_RFREQ_FIX (I740_RFREQ / I740_FFIX)
  303. #define I740_REF_FREQ (6667 * I740_FFIX / 100) /* 66.67 MHz */
  304. #define I740_MAX_VCO_FREQ (450 * I740_FFIX) /* 450 MHz */
  305. static void i740_calc_vclk(u32 freq, struct i740fb_par *par)
  306. {
  307. const u32 err_max = freq / (200 * I740_RFREQ / I740_FFIX);
  308. const u32 err_target = freq / (1000 * I740_RFREQ / I740_FFIX);
  309. u32 err_best = 512 * I740_FFIX;
  310. u32 f_err, f_vco;
  311. int m_best = 0, n_best = 0, p_best = 0, d_best = 0;
  312. int m, n;
  313. p_best = min(15, ilog2(I740_MAX_VCO_FREQ / (freq / I740_RFREQ_FIX)));
  314. d_best = 0;
  315. f_vco = (freq * (1 << p_best)) / I740_RFREQ_FIX;
  316. freq = freq / I740_RFREQ_FIX;
  317. n = 2;
  318. do {
  319. n++;
  320. m = ((f_vco * n) / I740_REF_FREQ + 2) / 4;
  321. if (m < 3)
  322. m = 3;
  323. {
  324. u32 f_out = (((m * I740_REF_FREQ * (4 << 2 * d_best))
  325. / n) + ((1 << p_best) / 2)) / (1 << p_best);
  326. f_err = (freq - f_out);
  327. if (abs(f_err) < err_max) {
  328. m_best = m;
  329. n_best = n;
  330. err_best = f_err;
  331. }
  332. }
  333. } while ((abs(f_err) >= err_target) &&
  334. ((n <= TARGET_MAX_N) || (abs(err_best) > err_max)));
  335. if (abs(f_err) < err_target) {
  336. m_best = m;
  337. n_best = n;
  338. }
  339. par->video_clk2_m = (m_best - 2) & 0xFF;
  340. par->video_clk2_n = (n_best - 2) & 0xFF;
  341. par->video_clk2_mn_msbs = ((((n_best - 2) >> 4) & VCO_N_MSBS)
  342. | (((m_best - 2) >> 8) & VCO_M_MSBS));
  343. par->video_clk2_div_sel =
  344. ((p_best << 4) | (d_best ? 4 : 0) | REF_DIV_1);
  345. }
  346. static int i740fb_decode_var(const struct fb_var_screeninfo *var,
  347. struct i740fb_par *par, struct fb_info *info)
  348. {
  349. /*
  350. * Get the video params out of 'var'.
  351. * If a value doesn't fit, round it up, if it's too big, return -EINVAL.
  352. */
  353. u32 xres, right, hslen, left, xtotal;
  354. u32 yres, lower, vslen, upper, ytotal;
  355. u32 vxres, xoffset, vyres, yoffset;
  356. u32 bpp, base, dacspeed24, mem;
  357. u8 r7;
  358. int i;
  359. dev_dbg(info->device, "decode_var: xres: %i, yres: %i, xres_v: %i, xres_v: %i\n",
  360. var->xres, var->yres, var->xres_virtual, var->xres_virtual);
  361. dev_dbg(info->device, " xoff: %i, yoff: %i, bpp: %i, graysc: %i\n",
  362. var->xoffset, var->yoffset, var->bits_per_pixel,
  363. var->grayscale);
  364. dev_dbg(info->device, " activate: %i, nonstd: %i, vmode: %i\n",
  365. var->activate, var->nonstd, var->vmode);
  366. dev_dbg(info->device, " pixclock: %i, hsynclen:%i, vsynclen:%i\n",
  367. var->pixclock, var->hsync_len, var->vsync_len);
  368. dev_dbg(info->device, " left: %i, right: %i, up:%i, lower:%i\n",
  369. var->left_margin, var->right_margin, var->upper_margin,
  370. var->lower_margin);
  371. bpp = var->bits_per_pixel;
  372. switch (bpp) {
  373. case 1 ... 8:
  374. bpp = 8;
  375. if ((1000000 / var->pixclock) > DACSPEED8) {
  376. dev_err(info->device, "requested pixclock %i MHz out of range (max. %i MHz at 8bpp)\n",
  377. 1000000 / var->pixclock, DACSPEED8);
  378. return -EINVAL;
  379. }
  380. break;
  381. case 9 ... 15:
  382. bpp = 15;
  383. case 16:
  384. if ((1000000 / var->pixclock) > DACSPEED16) {
  385. dev_err(info->device, "requested pixclock %i MHz out of range (max. %i MHz at 15/16bpp)\n",
  386. 1000000 / var->pixclock, DACSPEED16);
  387. return -EINVAL;
  388. }
  389. break;
  390. case 17 ... 24:
  391. bpp = 24;
  392. dacspeed24 = par->has_sgram ? DACSPEED24_SG : DACSPEED24_SD;
  393. if ((1000000 / var->pixclock) > dacspeed24) {
  394. dev_err(info->device, "requested pixclock %i MHz out of range (max. %i MHz at 24bpp)\n",
  395. 1000000 / var->pixclock, dacspeed24);
  396. return -EINVAL;
  397. }
  398. break;
  399. case 25 ... 32:
  400. bpp = 32;
  401. if ((1000000 / var->pixclock) > DACSPEED32) {
  402. dev_err(info->device, "requested pixclock %i MHz out of range (max. %i MHz at 32bpp)\n",
  403. 1000000 / var->pixclock, DACSPEED32);
  404. return -EINVAL;
  405. }
  406. break;
  407. default:
  408. return -EINVAL;
  409. }
  410. xres = ALIGN(var->xres, 8);
  411. vxres = ALIGN(var->xres_virtual, 16);
  412. if (vxres < xres)
  413. vxres = xres;
  414. xoffset = ALIGN(var->xoffset, 8);
  415. if (xres + xoffset > vxres)
  416. xoffset = vxres - xres;
  417. left = ALIGN(var->left_margin, 8);
  418. right = ALIGN(var->right_margin, 8);
  419. hslen = ALIGN(var->hsync_len, 8);
  420. yres = var->yres;
  421. vyres = var->yres_virtual;
  422. if (yres > vyres)
  423. vyres = yres;
  424. yoffset = var->yoffset;
  425. if (yres + yoffset > vyres)
  426. yoffset = vyres - yres;
  427. lower = var->lower_margin;
  428. vslen = var->vsync_len;
  429. upper = var->upper_margin;
  430. mem = vxres * vyres * ((bpp + 1) / 8);
  431. if (mem > info->screen_size) {
  432. dev_err(info->device, "not enough video memory (%d KB requested, %ld KB available)\n",
  433. mem >> 10, info->screen_size >> 10);
  434. return -ENOMEM;
  435. }
  436. if (yoffset + yres > vyres)
  437. yoffset = vyres - yres;
  438. xtotal = xres + right + hslen + left;
  439. ytotal = yres + lower + vslen + upper;
  440. par->crtc[VGA_CRTC_H_TOTAL] = (xtotal >> 3) - 5;
  441. par->crtc[VGA_CRTC_H_DISP] = (xres >> 3) - 1;
  442. par->crtc[VGA_CRTC_H_BLANK_START] = ((xres + right) >> 3) - 1;
  443. par->crtc[VGA_CRTC_H_SYNC_START] = (xres + right) >> 3;
  444. par->crtc[VGA_CRTC_H_SYNC_END] = (((xres + right + hslen) >> 3) & 0x1F)
  445. | ((((xres + right + hslen) >> 3) & 0x20) << 2);
  446. par->crtc[VGA_CRTC_H_BLANK_END] = ((xres + right + hslen) >> 3 & 0x1F)
  447. | 0x80;
  448. par->crtc[VGA_CRTC_V_TOTAL] = ytotal - 2;
  449. r7 = 0x10; /* disable linecompare */
  450. if (ytotal & 0x100)
  451. r7 |= 0x01;
  452. if (ytotal & 0x200)
  453. r7 |= 0x20;
  454. par->crtc[VGA_CRTC_PRESET_ROW] = 0;
  455. par->crtc[VGA_CRTC_MAX_SCAN] = 0x40; /* 1 scanline, no linecmp */
  456. if (var->vmode & FB_VMODE_DOUBLE)
  457. par->crtc[VGA_CRTC_MAX_SCAN] |= 0x80;
  458. par->crtc[VGA_CRTC_CURSOR_START] = 0x00;
  459. par->crtc[VGA_CRTC_CURSOR_END] = 0x00;
  460. par->crtc[VGA_CRTC_CURSOR_HI] = 0x00;
  461. par->crtc[VGA_CRTC_CURSOR_LO] = 0x00;
  462. par->crtc[VGA_CRTC_V_DISP_END] = yres-1;
  463. if ((yres-1) & 0x100)
  464. r7 |= 0x02;
  465. if ((yres-1) & 0x200)
  466. r7 |= 0x40;
  467. par->crtc[VGA_CRTC_V_BLANK_START] = yres + lower - 1;
  468. par->crtc[VGA_CRTC_V_SYNC_START] = yres + lower - 1;
  469. if ((yres + lower - 1) & 0x100)
  470. r7 |= 0x0C;
  471. if ((yres + lower - 1) & 0x200) {
  472. par->crtc[VGA_CRTC_MAX_SCAN] |= 0x20;
  473. r7 |= 0x80;
  474. }
  475. /* disabled IRQ */
  476. par->crtc[VGA_CRTC_V_SYNC_END] =
  477. ((yres + lower - 1 + vslen) & 0x0F) & ~0x10;
  478. /* 0x7F for VGA, but some SVGA chips require all 8 bits to be set */
  479. par->crtc[VGA_CRTC_V_BLANK_END] = (yres + lower - 1 + vslen) & 0xFF;
  480. par->crtc[VGA_CRTC_UNDERLINE] = 0x00;
  481. par->crtc[VGA_CRTC_MODE] = 0xC3 ;
  482. par->crtc[VGA_CRTC_LINE_COMPARE] = 0xFF;
  483. par->crtc[VGA_CRTC_OVERFLOW] = r7;
  484. par->vss = 0x00; /* 3DA */
  485. for (i = 0x00; i < 0x10; i++)
  486. par->atc[i] = i;
  487. par->atc[VGA_ATC_MODE] = 0x81;
  488. par->atc[VGA_ATC_OVERSCAN] = 0x00; /* 0 for EGA, 0xFF for VGA */
  489. par->atc[VGA_ATC_PLANE_ENABLE] = 0x0F;
  490. par->atc[VGA_ATC_COLOR_PAGE] = 0x00;
  491. par->misc = 0xC3;
  492. if (var->sync & FB_SYNC_HOR_HIGH_ACT)
  493. par->misc &= ~0x40;
  494. if (var->sync & FB_SYNC_VERT_HIGH_ACT)
  495. par->misc &= ~0x80;
  496. par->seq[VGA_SEQ_CLOCK_MODE] = 0x01;
  497. par->seq[VGA_SEQ_PLANE_WRITE] = 0x0F;
  498. par->seq[VGA_SEQ_CHARACTER_MAP] = 0x00;
  499. par->seq[VGA_SEQ_MEMORY_MODE] = 0x06;
  500. par->gdc[VGA_GFX_SR_VALUE] = 0x00;
  501. par->gdc[VGA_GFX_SR_ENABLE] = 0x00;
  502. par->gdc[VGA_GFX_COMPARE_VALUE] = 0x00;
  503. par->gdc[VGA_GFX_DATA_ROTATE] = 0x00;
  504. par->gdc[VGA_GFX_PLANE_READ] = 0;
  505. par->gdc[VGA_GFX_MODE] = 0x02;
  506. par->gdc[VGA_GFX_MISC] = 0x05;
  507. par->gdc[VGA_GFX_COMPARE_MASK] = 0x0F;
  508. par->gdc[VGA_GFX_BIT_MASK] = 0xFF;
  509. base = (yoffset * vxres + (xoffset & ~7)) >> 2;
  510. switch (bpp) {
  511. case 8:
  512. par->crtc[VGA_CRTC_OFFSET] = vxres >> 3;
  513. par->ext_offset = vxres >> 11;
  514. par->pixelpipe_cfg1 = DISPLAY_8BPP_MODE;
  515. par->bitblt_cntl = COLEXP_8BPP;
  516. break;
  517. case 15: /* 0rrrrrgg gggbbbbb */
  518. case 16: /* rrrrrggg gggbbbbb */
  519. par->pixelpipe_cfg1 = (var->green.length == 6) ?
  520. DISPLAY_16BPP_MODE : DISPLAY_15BPP_MODE;
  521. par->crtc[VGA_CRTC_OFFSET] = vxres >> 2;
  522. par->ext_offset = vxres >> 10;
  523. par->bitblt_cntl = COLEXP_16BPP;
  524. base *= 2;
  525. break;
  526. case 24:
  527. par->crtc[VGA_CRTC_OFFSET] = (vxres * 3) >> 3;
  528. par->ext_offset = (vxres * 3) >> 11;
  529. par->pixelpipe_cfg1 = DISPLAY_24BPP_MODE;
  530. par->bitblt_cntl = COLEXP_24BPP;
  531. base &= 0xFFFFFFFE; /* ...ignore the last bit. */
  532. base *= 3;
  533. break;
  534. case 32:
  535. par->crtc[VGA_CRTC_OFFSET] = vxres >> 1;
  536. par->ext_offset = vxres >> 9;
  537. par->pixelpipe_cfg1 = DISPLAY_32BPP_MODE;
  538. par->bitblt_cntl = COLEXP_RESERVED; /* Unimplemented on i740 */
  539. base *= 4;
  540. break;
  541. }
  542. par->crtc[VGA_CRTC_START_LO] = base & 0x000000FF;
  543. par->crtc[VGA_CRTC_START_HI] = (base & 0x0000FF00) >> 8;
  544. par->ext_start_addr =
  545. ((base & 0x003F0000) >> 16) | EXT_START_ADDR_ENABLE;
  546. par->ext_start_addr_hi = (base & 0x3FC00000) >> 22;
  547. par->pixelpipe_cfg0 = DAC_8_BIT;
  548. par->pixelpipe_cfg2 = DISPLAY_GAMMA_ENABLE | OVERLAY_GAMMA_ENABLE;
  549. par->io_cntl = EXTENDED_CRTC_CNTL;
  550. par->address_mapping = LINEAR_MODE_ENABLE | PAGE_MAPPING_ENABLE;
  551. par->display_cntl = HIRES_MODE;
  552. /* Set the MCLK freq */
  553. par->pll_cntl = PLL_MEMCLK_100000KHZ; /* 100 MHz -- use as default */
  554. /* Calculate the extended CRTC regs */
  555. par->ext_vert_total = (ytotal - 2) >> 8;
  556. par->ext_vert_disp_end = (yres - 1) >> 8;
  557. par->ext_vert_sync_start = (yres + lower) >> 8;
  558. par->ext_vert_blank_start = (yres + lower) >> 8;
  559. par->ext_horiz_total = ((xtotal >> 3) - 5) >> 8;
  560. par->ext_horiz_blank = (((xres + right) >> 3) & 0x40) >> 6;
  561. par->interlace_cntl = INTERLACE_DISABLE;
  562. /* Set the overscan color to 0. (NOTE: This only affects >8bpp mode) */
  563. par->atc[VGA_ATC_OVERSCAN] = 0;
  564. /* Calculate VCLK that most closely matches the requested dot clock */
  565. i740_calc_vclk((((u32)1e9) / var->pixclock) * (u32)(1e3), par);
  566. /* Since we program the clocks ourselves, always use VCLK2. */
  567. par->misc |= 0x0C;
  568. /* Calculate the FIFO Watermark and Burst Length. */
  569. par->lmi_fifo_watermark =
  570. i740_calc_fifo(par, 1000000 / var->pixclock, bpp);
  571. return 0;
  572. }
  573. static int i740fb_check_var(struct fb_var_screeninfo *var, struct fb_info *info)
  574. {
  575. switch (var->bits_per_pixel) {
  576. case 8:
  577. var->red.offset = var->green.offset = var->blue.offset = 0;
  578. var->red.length = var->green.length = var->blue.length = 8;
  579. break;
  580. case 16:
  581. switch (var->green.length) {
  582. default:
  583. case 5:
  584. var->red.offset = 10;
  585. var->green.offset = 5;
  586. var->blue.offset = 0;
  587. var->red.length = 5;
  588. var->green.length = 5;
  589. var->blue.length = 5;
  590. break;
  591. case 6:
  592. var->red.offset = 11;
  593. var->green.offset = 5;
  594. var->blue.offset = 0;
  595. var->red.length = var->blue.length = 5;
  596. break;
  597. }
  598. break;
  599. case 24:
  600. var->red.offset = 16;
  601. var->green.offset = 8;
  602. var->blue.offset = 0;
  603. var->red.length = var->green.length = var->blue.length = 8;
  604. break;
  605. case 32:
  606. var->transp.offset = 24;
  607. var->red.offset = 16;
  608. var->green.offset = 8;
  609. var->blue.offset = 0;
  610. var->transp.length = 8;
  611. var->red.length = var->green.length = var->blue.length = 8;
  612. break;
  613. default:
  614. return -EINVAL;
  615. }
  616. if (var->xres > var->xres_virtual)
  617. var->xres_virtual = var->xres;
  618. if (var->yres > var->yres_virtual)
  619. var->yres_virtual = var->yres;
  620. if (info->monspecs.hfmax && info->monspecs.vfmax &&
  621. info->monspecs.dclkmax && fb_validate_mode(var, info) < 0)
  622. return -EINVAL;
  623. return 0;
  624. }
  625. static void vga_protect(struct i740fb_par *par)
  626. {
  627. /* disable the display */
  628. i740outreg_mask(par, VGA_SEQ_I, VGA_SEQ_CLOCK_MODE, 0x20, 0x20);
  629. i740inb(par, 0x3DA);
  630. i740outb(par, VGA_ATT_W, 0x00); /* enable palette access */
  631. }
  632. static void vga_unprotect(struct i740fb_par *par)
  633. {
  634. /* reenable display */
  635. i740outreg_mask(par, VGA_SEQ_I, VGA_SEQ_CLOCK_MODE, 0, 0x20);
  636. i740inb(par, 0x3DA);
  637. i740outb(par, VGA_ATT_W, 0x20); /* disable palette access */
  638. }
  639. static int i740fb_set_par(struct fb_info *info)
  640. {
  641. struct i740fb_par *par = info->par;
  642. u32 itemp;
  643. int i;
  644. i = i740fb_decode_var(&info->var, par, info);
  645. if (i)
  646. return i;
  647. memset(info->screen_base, 0, info->screen_size);
  648. vga_protect(par);
  649. i740outreg(par, XRX, DRAM_EXT_CNTL, DRAM_REFRESH_DISABLE);
  650. mdelay(1);
  651. i740outreg(par, XRX, VCLK2_VCO_M, par->video_clk2_m);
  652. i740outreg(par, XRX, VCLK2_VCO_N, par->video_clk2_n);
  653. i740outreg(par, XRX, VCLK2_VCO_MN_MSBS, par->video_clk2_mn_msbs);
  654. i740outreg(par, XRX, VCLK2_VCO_DIV_SEL, par->video_clk2_div_sel);
  655. i740outreg_mask(par, XRX, PIXPIPE_CONFIG_0,
  656. par->pixelpipe_cfg0 & DAC_8_BIT, 0x80);
  657. i740inb(par, 0x3DA);
  658. i740outb(par, 0x3C0, 0x00);
  659. /* update misc output register */
  660. i740outb(par, VGA_MIS_W, par->misc | 0x01);
  661. /* synchronous reset on */
  662. i740outreg(par, VGA_SEQ_I, VGA_SEQ_RESET, 0x01);
  663. /* write sequencer registers */
  664. i740outreg(par, VGA_SEQ_I, VGA_SEQ_CLOCK_MODE,
  665. par->seq[VGA_SEQ_CLOCK_MODE] | 0x20);
  666. for (i = 2; i < VGA_SEQ_C; i++)
  667. i740outreg(par, VGA_SEQ_I, i, par->seq[i]);
  668. /* synchronous reset off */
  669. i740outreg(par, VGA_SEQ_I, VGA_SEQ_RESET, 0x03);
  670. /* deprotect CRT registers 0-7 */
  671. i740outreg(par, VGA_CRT_IC, VGA_CRTC_V_SYNC_END,
  672. par->crtc[VGA_CRTC_V_SYNC_END]);
  673. /* write CRT registers */
  674. for (i = 0; i < VGA_CRT_C; i++)
  675. i740outreg(par, VGA_CRT_IC, i, par->crtc[i]);
  676. /* write graphics controller registers */
  677. for (i = 0; i < VGA_GFX_C; i++)
  678. i740outreg(par, VGA_GFX_I, i, par->gdc[i]);
  679. /* write attribute controller registers */
  680. for (i = 0; i < VGA_ATT_C; i++) {
  681. i740inb(par, VGA_IS1_RC); /* reset flip-flop */
  682. i740outb(par, VGA_ATT_IW, i);
  683. i740outb(par, VGA_ATT_IW, par->atc[i]);
  684. }
  685. i740inb(par, VGA_IS1_RC);
  686. i740outb(par, VGA_ATT_IW, 0x20);
  687. i740outreg(par, VGA_CRT_IC, EXT_VERT_TOTAL, par->ext_vert_total);
  688. i740outreg(par, VGA_CRT_IC, EXT_VERT_DISPLAY, par->ext_vert_disp_end);
  689. i740outreg(par, VGA_CRT_IC, EXT_VERT_SYNC_START,
  690. par->ext_vert_sync_start);
  691. i740outreg(par, VGA_CRT_IC, EXT_VERT_BLANK_START,
  692. par->ext_vert_blank_start);
  693. i740outreg(par, VGA_CRT_IC, EXT_HORIZ_TOTAL, par->ext_horiz_total);
  694. i740outreg(par, VGA_CRT_IC, EXT_HORIZ_BLANK, par->ext_horiz_blank);
  695. i740outreg(par, VGA_CRT_IC, EXT_OFFSET, par->ext_offset);
  696. i740outreg(par, VGA_CRT_IC, EXT_START_ADDR_HI, par->ext_start_addr_hi);
  697. i740outreg(par, VGA_CRT_IC, EXT_START_ADDR, par->ext_start_addr);
  698. i740outreg_mask(par, VGA_CRT_IC, INTERLACE_CNTL,
  699. par->interlace_cntl, INTERLACE_ENABLE);
  700. i740outreg_mask(par, XRX, ADDRESS_MAPPING, par->address_mapping, 0x1F);
  701. i740outreg_mask(par, XRX, BITBLT_CNTL, par->bitblt_cntl, COLEXP_MODE);
  702. i740outreg_mask(par, XRX, DISPLAY_CNTL,
  703. par->display_cntl, VGA_WRAP_MODE | GUI_MODE);
  704. i740outreg_mask(par, XRX, PIXPIPE_CONFIG_0, par->pixelpipe_cfg0, 0x9B);
  705. i740outreg_mask(par, XRX, PIXPIPE_CONFIG_2, par->pixelpipe_cfg2, 0x0C);
  706. i740outreg(par, XRX, PLL_CNTL, par->pll_cntl);
  707. i740outreg_mask(par, XRX, PIXPIPE_CONFIG_1,
  708. par->pixelpipe_cfg1, DISPLAY_COLOR_MODE);
  709. itemp = readl(par->regs + FWATER_BLC);
  710. itemp &= ~(LMI_BURST_LENGTH | LMI_FIFO_WATERMARK);
  711. itemp |= par->lmi_fifo_watermark;
  712. writel(itemp, par->regs + FWATER_BLC);
  713. i740outreg(par, XRX, DRAM_EXT_CNTL, DRAM_REFRESH_60HZ);
  714. i740outreg_mask(par, MRX, COL_KEY_CNTL_1, 0, BLANK_DISP_OVERLAY);
  715. i740outreg_mask(par, XRX, IO_CTNL,
  716. par->io_cntl, EXTENDED_ATTR_CNTL | EXTENDED_CRTC_CNTL);
  717. if (par->pixelpipe_cfg1 != DISPLAY_8BPP_MODE) {
  718. i740outb(par, VGA_PEL_MSK, 0xFF);
  719. i740outb(par, VGA_PEL_IW, 0x00);
  720. for (i = 0; i < 256; i++) {
  721. itemp = (par->pixelpipe_cfg0 & DAC_8_BIT) ? i : i >> 2;
  722. i740outb(par, VGA_PEL_D, itemp);
  723. i740outb(par, VGA_PEL_D, itemp);
  724. i740outb(par, VGA_PEL_D, itemp);
  725. }
  726. }
  727. /* Wait for screen to stabilize. */
  728. mdelay(50);
  729. vga_unprotect(par);
  730. info->fix.line_length =
  731. info->var.xres_virtual * info->var.bits_per_pixel / 8;
  732. if (info->var.bits_per_pixel == 8)
  733. info->fix.visual = FB_VISUAL_PSEUDOCOLOR;
  734. else
  735. info->fix.visual = FB_VISUAL_TRUECOLOR;
  736. return 0;
  737. }
  738. static int i740fb_setcolreg(unsigned regno, unsigned red, unsigned green,
  739. unsigned blue, unsigned transp,
  740. struct fb_info *info)
  741. {
  742. u32 r, g, b;
  743. dev_dbg(info->device, "setcolreg: regno: %i, red=%d, green=%d, blue=%d, transp=%d, bpp=%d\n",
  744. regno, red, green, blue, transp, info->var.bits_per_pixel);
  745. switch (info->fix.visual) {
  746. case FB_VISUAL_PSEUDOCOLOR:
  747. if (regno >= 256)
  748. return -EINVAL;
  749. i740outb(info->par, VGA_PEL_IW, regno);
  750. i740outb(info->par, VGA_PEL_D, red >> 8);
  751. i740outb(info->par, VGA_PEL_D, green >> 8);
  752. i740outb(info->par, VGA_PEL_D, blue >> 8);
  753. break;
  754. case FB_VISUAL_TRUECOLOR:
  755. if (regno >= 16)
  756. return -EINVAL;
  757. r = (red >> (16 - info->var.red.length))
  758. << info->var.red.offset;
  759. b = (blue >> (16 - info->var.blue.length))
  760. << info->var.blue.offset;
  761. g = (green >> (16 - info->var.green.length))
  762. << info->var.green.offset;
  763. ((u32 *) info->pseudo_palette)[regno] = r | g | b;
  764. break;
  765. default:
  766. return -EINVAL;
  767. }
  768. return 0;
  769. }
  770. static int i740fb_pan_display(struct fb_var_screeninfo *var,
  771. struct fb_info *info)
  772. {
  773. struct i740fb_par *par = info->par;
  774. u32 base = (var->yoffset * info->var.xres_virtual
  775. + (var->xoffset & ~7)) >> 2;
  776. dev_dbg(info->device, "pan_display: xoffset: %i yoffset: %i base: %i\n",
  777. var->xoffset, var->yoffset, base);
  778. switch (info->var.bits_per_pixel) {
  779. case 8:
  780. break;
  781. case 15:
  782. case 16:
  783. base *= 2;
  784. break;
  785. case 24:
  786. /*
  787. * The last bit does not seem to have any effect on the start
  788. * address register in 24bpp mode, so...
  789. */
  790. base &= 0xFFFFFFFE; /* ...ignore the last bit. */
  791. base *= 3;
  792. break;
  793. case 32:
  794. base *= 4;
  795. break;
  796. }
  797. par->crtc[VGA_CRTC_START_LO] = base & 0x000000FF;
  798. par->crtc[VGA_CRTC_START_HI] = (base & 0x0000FF00) >> 8;
  799. par->ext_start_addr_hi = (base & 0x3FC00000) >> 22;
  800. par->ext_start_addr =
  801. ((base & 0x003F0000) >> 16) | EXT_START_ADDR_ENABLE;
  802. i740outreg(par, VGA_CRT_IC, VGA_CRTC_START_LO, base & 0x000000FF);
  803. i740outreg(par, VGA_CRT_IC, VGA_CRTC_START_HI,
  804. (base & 0x0000FF00) >> 8);
  805. i740outreg(par, VGA_CRT_IC, EXT_START_ADDR_HI,
  806. (base & 0x3FC00000) >> 22);
  807. i740outreg(par, VGA_CRT_IC, EXT_START_ADDR,
  808. ((base & 0x003F0000) >> 16) | EXT_START_ADDR_ENABLE);
  809. return 0;
  810. }
  811. static int i740fb_blank(int blank_mode, struct fb_info *info)
  812. {
  813. struct i740fb_par *par = info->par;
  814. unsigned char SEQ01;
  815. int DPMSSyncSelect;
  816. switch (blank_mode) {
  817. case FB_BLANK_UNBLANK:
  818. case FB_BLANK_NORMAL:
  819. SEQ01 = 0x00;
  820. DPMSSyncSelect = HSYNC_ON | VSYNC_ON;
  821. break;
  822. case FB_BLANK_VSYNC_SUSPEND:
  823. SEQ01 = 0x20;
  824. DPMSSyncSelect = HSYNC_ON | VSYNC_OFF;
  825. break;
  826. case FB_BLANK_HSYNC_SUSPEND:
  827. SEQ01 = 0x20;
  828. DPMSSyncSelect = HSYNC_OFF | VSYNC_ON;
  829. break;
  830. case FB_BLANK_POWERDOWN:
  831. SEQ01 = 0x20;
  832. DPMSSyncSelect = HSYNC_OFF | VSYNC_OFF;
  833. break;
  834. default:
  835. return -EINVAL;
  836. }
  837. /* Turn the screen on/off */
  838. i740outb(par, SRX, 0x01);
  839. SEQ01 |= i740inb(par, SRX + 1) & ~0x20;
  840. i740outb(par, SRX, 0x01);
  841. i740outb(par, SRX + 1, SEQ01);
  842. /* Set the DPMS mode */
  843. i740outreg(par, XRX, DPMS_SYNC_SELECT, DPMSSyncSelect);
  844. /* Let fbcon do a soft blank for us */
  845. return (blank_mode == FB_BLANK_NORMAL) ? 1 : 0;
  846. }
  847. static struct fb_ops i740fb_ops = {
  848. .owner = THIS_MODULE,
  849. .fb_open = i740fb_open,
  850. .fb_release = i740fb_release,
  851. .fb_check_var = i740fb_check_var,
  852. .fb_set_par = i740fb_set_par,
  853. .fb_setcolreg = i740fb_setcolreg,
  854. .fb_blank = i740fb_blank,
  855. .fb_pan_display = i740fb_pan_display,
  856. .fb_fillrect = cfb_fillrect,
  857. .fb_copyarea = cfb_copyarea,
  858. .fb_imageblit = cfb_imageblit,
  859. };
  860. /* ------------------------------------------------------------------------- */
  861. static int i740fb_probe(struct pci_dev *dev, const struct pci_device_id *ent)
  862. {
  863. struct fb_info *info;
  864. struct i740fb_par *par;
  865. int ret, tmp;
  866. bool found = false;
  867. u8 *edid;
  868. info = framebuffer_alloc(sizeof(struct i740fb_par), &(dev->dev));
  869. if (!info) {
  870. dev_err(&(dev->dev), "cannot allocate framebuffer\n");
  871. return -ENOMEM;
  872. }
  873. par = info->par;
  874. mutex_init(&par->open_lock);
  875. info->var.activate = FB_ACTIVATE_NOW;
  876. info->var.bits_per_pixel = 8;
  877. info->fbops = &i740fb_ops;
  878. info->pseudo_palette = par->pseudo_palette;
  879. ret = pci_enable_device(dev);
  880. if (ret) {
  881. dev_err(info->device, "cannot enable PCI device\n");
  882. goto err_enable_device;
  883. }
  884. ret = pci_request_regions(dev, info->fix.id);
  885. if (ret) {
  886. dev_err(info->device, "error requesting regions\n");
  887. goto err_request_regions;
  888. }
  889. info->screen_base = pci_ioremap_wc_bar(dev, 0);
  890. if (!info->screen_base) {
  891. dev_err(info->device, "error remapping base\n");
  892. ret = -ENOMEM;
  893. goto err_ioremap_1;
  894. }
  895. par->regs = pci_ioremap_bar(dev, 1);
  896. if (!par->regs) {
  897. dev_err(info->device, "error remapping MMIO\n");
  898. ret = -ENOMEM;
  899. goto err_ioremap_2;
  900. }
  901. /* detect memory size */
  902. if ((i740inreg(par, XRX, DRAM_ROW_TYPE) & DRAM_ROW_1)
  903. == DRAM_ROW_1_SDRAM)
  904. i740outb(par, XRX, DRAM_ROW_BNDRY_1);
  905. else
  906. i740outb(par, XRX, DRAM_ROW_BNDRY_0);
  907. info->screen_size = i740inb(par, XRX + 1) * 1024 * 1024;
  908. /* detect memory type */
  909. tmp = i740inreg(par, XRX, DRAM_ROW_CNTL_LO);
  910. par->has_sgram = !((tmp & DRAM_RAS_TIMING) ||
  911. (tmp & DRAM_RAS_PRECHARGE));
  912. fb_info(info, "Intel740 on %s, %ld KB %s\n",
  913. pci_name(dev), info->screen_size >> 10,
  914. par->has_sgram ? "SGRAM" : "SDRAM");
  915. info->fix = i740fb_fix;
  916. info->fix.mmio_start = pci_resource_start(dev, 1);
  917. info->fix.mmio_len = pci_resource_len(dev, 1);
  918. info->fix.smem_start = pci_resource_start(dev, 0);
  919. info->fix.smem_len = info->screen_size;
  920. info->flags = FBINFO_DEFAULT | FBINFO_HWACCEL_YPAN;
  921. if (i740fb_setup_ddc_bus(info) == 0) {
  922. par->ddc_registered = true;
  923. edid = fb_ddc_read(&par->ddc_adapter);
  924. if (edid) {
  925. fb_edid_to_monspecs(edid, &info->monspecs);
  926. kfree(edid);
  927. if (!info->monspecs.modedb)
  928. dev_err(info->device,
  929. "error getting mode database\n");
  930. else {
  931. const struct fb_videomode *m;
  932. fb_videomode_to_modelist(
  933. info->monspecs.modedb,
  934. info->monspecs.modedb_len,
  935. &info->modelist);
  936. m = fb_find_best_display(&info->monspecs,
  937. &info->modelist);
  938. if (m) {
  939. fb_videomode_to_var(&info->var, m);
  940. /* fill all other info->var's fields */
  941. if (!i740fb_check_var(&info->var, info))
  942. found = true;
  943. }
  944. }
  945. }
  946. }
  947. if (!mode_option && !found)
  948. mode_option = "640x480-8@60";
  949. if (mode_option) {
  950. ret = fb_find_mode(&info->var, info, mode_option,
  951. info->monspecs.modedb,
  952. info->monspecs.modedb_len,
  953. NULL, info->var.bits_per_pixel);
  954. if (!ret || ret == 4) {
  955. dev_err(info->device, "mode %s not found\n",
  956. mode_option);
  957. ret = -EINVAL;
  958. }
  959. }
  960. fb_destroy_modedb(info->monspecs.modedb);
  961. info->monspecs.modedb = NULL;
  962. /* maximize virtual vertical size for fast scrolling */
  963. info->var.yres_virtual = info->fix.smem_len * 8 /
  964. (info->var.bits_per_pixel * info->var.xres_virtual);
  965. if (ret == -EINVAL)
  966. goto err_find_mode;
  967. ret = fb_alloc_cmap(&info->cmap, 256, 0);
  968. if (ret) {
  969. dev_err(info->device, "cannot allocate colormap\n");
  970. goto err_alloc_cmap;
  971. }
  972. ret = register_framebuffer(info);
  973. if (ret) {
  974. dev_err(info->device, "error registering framebuffer\n");
  975. goto err_reg_framebuffer;
  976. }
  977. fb_info(info, "%s frame buffer device\n", info->fix.id);
  978. pci_set_drvdata(dev, info);
  979. if (mtrr)
  980. par->wc_cookie = arch_phys_wc_add(info->fix.smem_start,
  981. info->fix.smem_len);
  982. return 0;
  983. err_reg_framebuffer:
  984. fb_dealloc_cmap(&info->cmap);
  985. err_alloc_cmap:
  986. err_find_mode:
  987. if (par->ddc_registered)
  988. i2c_del_adapter(&par->ddc_adapter);
  989. pci_iounmap(dev, par->regs);
  990. err_ioremap_2:
  991. pci_iounmap(dev, info->screen_base);
  992. err_ioremap_1:
  993. pci_release_regions(dev);
  994. err_request_regions:
  995. /* pci_disable_device(dev); */
  996. err_enable_device:
  997. framebuffer_release(info);
  998. return ret;
  999. }
  1000. static void i740fb_remove(struct pci_dev *dev)
  1001. {
  1002. struct fb_info *info = pci_get_drvdata(dev);
  1003. if (info) {
  1004. struct i740fb_par *par = info->par;
  1005. arch_phys_wc_del(par->wc_cookie);
  1006. unregister_framebuffer(info);
  1007. fb_dealloc_cmap(&info->cmap);
  1008. if (par->ddc_registered)
  1009. i2c_del_adapter(&par->ddc_adapter);
  1010. pci_iounmap(dev, par->regs);
  1011. pci_iounmap(dev, info->screen_base);
  1012. pci_release_regions(dev);
  1013. /* pci_disable_device(dev); */
  1014. framebuffer_release(info);
  1015. }
  1016. }
  1017. #ifdef CONFIG_PM
  1018. static int i740fb_suspend(struct pci_dev *dev, pm_message_t state)
  1019. {
  1020. struct fb_info *info = pci_get_drvdata(dev);
  1021. struct i740fb_par *par = info->par;
  1022. /* don't disable console during hibernation and wakeup from it */
  1023. if (state.event == PM_EVENT_FREEZE || state.event == PM_EVENT_PRETHAW)
  1024. return 0;
  1025. console_lock();
  1026. mutex_lock(&(par->open_lock));
  1027. /* do nothing if framebuffer is not active */
  1028. if (par->ref_count == 0) {
  1029. mutex_unlock(&(par->open_lock));
  1030. console_unlock();
  1031. return 0;
  1032. }
  1033. fb_set_suspend(info, 1);
  1034. pci_save_state(dev);
  1035. pci_disable_device(dev);
  1036. pci_set_power_state(dev, pci_choose_state(dev, state));
  1037. mutex_unlock(&(par->open_lock));
  1038. console_unlock();
  1039. return 0;
  1040. }
  1041. static int i740fb_resume(struct pci_dev *dev)
  1042. {
  1043. struct fb_info *info = pci_get_drvdata(dev);
  1044. struct i740fb_par *par = info->par;
  1045. console_lock();
  1046. mutex_lock(&(par->open_lock));
  1047. if (par->ref_count == 0)
  1048. goto fail;
  1049. pci_set_power_state(dev, PCI_D0);
  1050. pci_restore_state(dev);
  1051. if (pci_enable_device(dev))
  1052. goto fail;
  1053. i740fb_set_par(info);
  1054. fb_set_suspend(info, 0);
  1055. fail:
  1056. mutex_unlock(&(par->open_lock));
  1057. console_unlock();
  1058. return 0;
  1059. }
  1060. #else
  1061. #define i740fb_suspend NULL
  1062. #define i740fb_resume NULL
  1063. #endif /* CONFIG_PM */
  1064. #define I740_ID_PCI 0x00d1
  1065. #define I740_ID_AGP 0x7800
  1066. static const struct pci_device_id i740fb_id_table[] = {
  1067. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, I740_ID_PCI) },
  1068. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, I740_ID_AGP) },
  1069. { 0 }
  1070. };
  1071. MODULE_DEVICE_TABLE(pci, i740fb_id_table);
  1072. static struct pci_driver i740fb_driver = {
  1073. .name = "i740fb",
  1074. .id_table = i740fb_id_table,
  1075. .probe = i740fb_probe,
  1076. .remove = i740fb_remove,
  1077. .suspend = i740fb_suspend,
  1078. .resume = i740fb_resume,
  1079. };
  1080. #ifndef MODULE
  1081. static int __init i740fb_setup(char *options)
  1082. {
  1083. char *opt;
  1084. if (!options || !*options)
  1085. return 0;
  1086. while ((opt = strsep(&options, ",")) != NULL) {
  1087. if (!*opt)
  1088. continue;
  1089. else if (!strncmp(opt, "mtrr:", 5))
  1090. mtrr = simple_strtoul(opt + 5, NULL, 0);
  1091. else
  1092. mode_option = opt;
  1093. }
  1094. return 0;
  1095. }
  1096. #endif
  1097. static int __init i740fb_init(void)
  1098. {
  1099. #ifndef MODULE
  1100. char *option = NULL;
  1101. if (fb_get_options("i740fb", &option))
  1102. return -ENODEV;
  1103. i740fb_setup(option);
  1104. #endif
  1105. return pci_register_driver(&i740fb_driver);
  1106. }
  1107. static void __exit i740fb_exit(void)
  1108. {
  1109. pci_unregister_driver(&i740fb_driver);
  1110. }
  1111. module_init(i740fb_init);
  1112. module_exit(i740fb_exit);
  1113. MODULE_AUTHOR("(c) 2011 Ondrej Zary <linux@rainbow-software.org>");
  1114. MODULE_LICENSE("GPL");
  1115. MODULE_DESCRIPTION("fbdev driver for Intel740");
  1116. module_param(mode_option, charp, 0444);
  1117. MODULE_PARM_DESC(mode_option, "Default video mode ('640x480-8@60', etc)");
  1118. module_param(mtrr, int, 0444);
  1119. MODULE_PARM_DESC(mtrr, "Enable write-combining with MTRR (1=enable, 0=disable, default=1)");