i740fb.c 33 KB

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