PageRenderTime 61ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/drivers/video/i810/i810_main.c

https://github.com/mstsirkin/linux
C | 1865 lines | 1312 code | 255 blank | 298 comment | 191 complexity | 4a9d1833c8e803bc83f3bbd841cfc3eb MD5 | raw file
  1. /*-*- linux-c -*-
  2. * linux/drivers/video/i810_main.c -- Intel 810 frame buffer device
  3. *
  4. * Copyright (C) 2001 Antonino Daplas<adaplas@pol.net>
  5. * All Rights Reserved
  6. *
  7. * Contributors:
  8. * Michael Vogt <mvogt@acm.org> - added support for Intel 815 chipsets
  9. * and enabling the power-on state of
  10. * external VGA connectors for
  11. * secondary displays
  12. *
  13. * Fredrik Andersson <krueger@shell.linux.se> - alpha testing of
  14. * the VESA GTF
  15. *
  16. * Brad Corrion <bcorrion@web-co.com> - alpha testing of customized
  17. * timings support
  18. *
  19. * The code framework is a modification of vfb.c by Geert Uytterhoeven.
  20. * DotClock and PLL calculations are partly based on i810_driver.c
  21. * in xfree86 v4.0.3 by Precision Insight.
  22. * Watermark calculation and tables are based on i810_wmark.c
  23. * in xfre86 v4.0.3 by Precision Insight. Slight modifications
  24. * only to allow for integer operations instead of floating point.
  25. *
  26. * This file is subject to the terms and conditions of the GNU General Public
  27. * License. See the file COPYING in the main directory of this archive for
  28. * more details.
  29. */
  30. #include <linux/module.h>
  31. #include <linux/kernel.h>
  32. #include <linux/errno.h>
  33. #include <linux/string.h>
  34. #include <linux/mm.h>
  35. #include <linux/slab.h>
  36. #include <linux/fb.h>
  37. #include <linux/init.h>
  38. #include <linux/pci.h>
  39. #include <linux/pci_ids.h>
  40. #include <linux/resource.h>
  41. #include <linux/unistd.h>
  42. #include <linux/console.h>
  43. #include <asm/io.h>
  44. #include <asm/div64.h>
  45. #include <asm/page.h>
  46. #include "i810_regs.h"
  47. #include "i810.h"
  48. #include "i810_main.h"
  49. /*
  50. * voffset - framebuffer offset in MiB from aperture start address. In order for
  51. * the driver to work with X, we must try to use memory holes left untouched by X. The
  52. * following table lists where X's different surfaces start at.
  53. *
  54. * ---------------------------------------------
  55. * : : 64 MiB : 32 MiB :
  56. * ----------------------------------------------
  57. * : FrontBuffer : 0 : 0 :
  58. * : DepthBuffer : 48 : 16 :
  59. * : BackBuffer : 56 : 24 :
  60. * ----------------------------------------------
  61. *
  62. * So for chipsets with 64 MiB Aperture sizes, 32 MiB for v_offset is okay, allowing up to
  63. * 15 + 1 MiB of Framebuffer memory. For 32 MiB Aperture sizes, a v_offset of 8 MiB should
  64. * work, allowing 7 + 1 MiB of Framebuffer memory.
  65. * Note, the size of the hole may change depending on how much memory you allocate to X,
  66. * and how the memory is split up between these surfaces.
  67. *
  68. * Note: Anytime the DepthBuffer or FrontBuffer is overlapped, X would still run but with
  69. * DRI disabled. But if the Frontbuffer is overlapped, X will fail to load.
  70. *
  71. * Experiment with v_offset to find out which works best for you.
  72. */
  73. static u32 v_offset_default __devinitdata; /* For 32 MiB Aper size, 8 should be the default */
  74. static u32 voffset __devinitdata;
  75. static int i810fb_cursor(struct fb_info *info, struct fb_cursor *cursor);
  76. static int __devinit i810fb_init_pci (struct pci_dev *dev,
  77. const struct pci_device_id *entry);
  78. static void __exit i810fb_remove_pci(struct pci_dev *dev);
  79. static int i810fb_resume(struct pci_dev *dev);
  80. static int i810fb_suspend(struct pci_dev *dev, pm_message_t state);
  81. /* Chipset Specific Functions */
  82. static int i810fb_set_par (struct fb_info *info);
  83. static int i810fb_getcolreg (u8 regno, u8 *red, u8 *green, u8 *blue,
  84. u8 *transp, struct fb_info *info);
  85. static int i810fb_setcolreg (unsigned regno, unsigned red, unsigned green, unsigned blue,
  86. unsigned transp, struct fb_info *info);
  87. static int i810fb_pan_display(struct fb_var_screeninfo *var, struct fb_info *info);
  88. static int i810fb_blank (int blank_mode, struct fb_info *info);
  89. /* Initialization */
  90. static void i810fb_release_resource (struct fb_info *info, struct i810fb_par *par);
  91. /* PCI */
  92. static const char *i810_pci_list[] __devinitdata = {
  93. "Intel(R) 810 Framebuffer Device" ,
  94. "Intel(R) 810-DC100 Framebuffer Device" ,
  95. "Intel(R) 810E Framebuffer Device" ,
  96. "Intel(R) 815 (Internal Graphics 100Mhz FSB) Framebuffer Device" ,
  97. "Intel(R) 815 (Internal Graphics only) Framebuffer Device" ,
  98. "Intel(R) 815 (Internal Graphics with AGP) Framebuffer Device"
  99. };
  100. static struct pci_device_id i810fb_pci_tbl[] = {
  101. { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82810_IG1,
  102. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
  103. { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82810_IG3,
  104. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 1 },
  105. { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82810E_IG,
  106. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 2 },
  107. /* mvo: added i815 PCI-ID */
  108. { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82815_100,
  109. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 3 },
  110. { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82815_NOAGP,
  111. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 4 },
  112. { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82815_CGC,
  113. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 5 },
  114. { 0 },
  115. };
  116. static struct pci_driver i810fb_driver = {
  117. .name = "i810fb",
  118. .id_table = i810fb_pci_tbl,
  119. .probe = i810fb_init_pci,
  120. .remove = __exit_p(i810fb_remove_pci),
  121. .suspend = i810fb_suspend,
  122. .resume = i810fb_resume,
  123. };
  124. static char *mode_option __devinitdata = NULL;
  125. static int vram __devinitdata = 4;
  126. static int bpp __devinitdata = 8;
  127. static int mtrr __devinitdata;
  128. static int accel __devinitdata;
  129. static int hsync1 __devinitdata;
  130. static int hsync2 __devinitdata;
  131. static int vsync1 __devinitdata;
  132. static int vsync2 __devinitdata;
  133. static int xres __devinitdata;
  134. static int yres;
  135. static int vyres __devinitdata;
  136. static int sync __devinitdata;
  137. static int extvga __devinitdata;
  138. static int dcolor __devinitdata;
  139. static int ddc3 __devinitdata = 2;
  140. /*------------------------------------------------------------*/
  141. /**************************************************************
  142. * Hardware Low Level Routines *
  143. **************************************************************/
  144. /**
  145. * i810_screen_off - turns off/on display
  146. * @mmio: address of register space
  147. * @mode: on or off
  148. *
  149. * DESCRIPTION:
  150. * Blanks/unblanks the display
  151. */
  152. static void i810_screen_off(u8 __iomem *mmio, u8 mode)
  153. {
  154. u32 count = WAIT_COUNT;
  155. u8 val;
  156. i810_writeb(SR_INDEX, mmio, SR01);
  157. val = i810_readb(SR_DATA, mmio);
  158. val = (mode == OFF) ? val | SCR_OFF :
  159. val & ~SCR_OFF;
  160. while((i810_readw(DISP_SL, mmio) & 0xFFF) && count--);
  161. i810_writeb(SR_INDEX, mmio, SR01);
  162. i810_writeb(SR_DATA, mmio, val);
  163. }
  164. /**
  165. * i810_dram_off - turns off/on dram refresh
  166. * @mmio: address of register space
  167. * @mode: on or off
  168. *
  169. * DESCRIPTION:
  170. * Turns off DRAM refresh. Must be off for only 2 vsyncs
  171. * before data becomes corrupt
  172. */
  173. static void i810_dram_off(u8 __iomem *mmio, u8 mode)
  174. {
  175. u8 val;
  176. val = i810_readb(DRAMCH, mmio);
  177. val &= DRAM_OFF;
  178. val = (mode == OFF) ? val : val | DRAM_ON;
  179. i810_writeb(DRAMCH, mmio, val);
  180. }
  181. /**
  182. * i810_protect_regs - allows rw/ro mode of certain VGA registers
  183. * @mmio: address of register space
  184. * @mode: protect/unprotect
  185. *
  186. * DESCRIPTION:
  187. * The IBM VGA standard allows protection of certain VGA registers.
  188. * This will protect or unprotect them.
  189. */
  190. static void i810_protect_regs(u8 __iomem *mmio, int mode)
  191. {
  192. u8 reg;
  193. i810_writeb(CR_INDEX_CGA, mmio, CR11);
  194. reg = i810_readb(CR_DATA_CGA, mmio);
  195. reg = (mode == OFF) ? reg & ~0x80 :
  196. reg | 0x80;
  197. i810_writeb(CR_INDEX_CGA, mmio, CR11);
  198. i810_writeb(CR_DATA_CGA, mmio, reg);
  199. }
  200. /**
  201. * i810_load_pll - loads values for the hardware PLL clock
  202. * @par: pointer to i810fb_par structure
  203. *
  204. * DESCRIPTION:
  205. * Loads the P, M, and N registers.
  206. */
  207. static void i810_load_pll(struct i810fb_par *par)
  208. {
  209. u32 tmp1, tmp2;
  210. u8 __iomem *mmio = par->mmio_start_virtual;
  211. tmp1 = par->regs.M | par->regs.N << 16;
  212. tmp2 = i810_readl(DCLK_2D, mmio);
  213. tmp2 &= ~MN_MASK;
  214. i810_writel(DCLK_2D, mmio, tmp1 | tmp2);
  215. tmp1 = par->regs.P;
  216. tmp2 = i810_readl(DCLK_0DS, mmio);
  217. tmp2 &= ~(P_OR << 16);
  218. i810_writel(DCLK_0DS, mmio, (tmp1 << 16) | tmp2);
  219. i810_writeb(MSR_WRITE, mmio, par->regs.msr | 0xC8 | 1);
  220. }
  221. /**
  222. * i810_load_vga - load standard VGA registers
  223. * @par: pointer to i810fb_par structure
  224. *
  225. * DESCRIPTION:
  226. * Load values to VGA registers
  227. */
  228. static void i810_load_vga(struct i810fb_par *par)
  229. {
  230. u8 __iomem *mmio = par->mmio_start_virtual;
  231. /* interlace */
  232. i810_writeb(CR_INDEX_CGA, mmio, CR70);
  233. i810_writeb(CR_DATA_CGA, mmio, par->interlace);
  234. i810_writeb(CR_INDEX_CGA, mmio, CR00);
  235. i810_writeb(CR_DATA_CGA, mmio, par->regs.cr00);
  236. i810_writeb(CR_INDEX_CGA, mmio, CR01);
  237. i810_writeb(CR_DATA_CGA, mmio, par->regs.cr01);
  238. i810_writeb(CR_INDEX_CGA, mmio, CR02);
  239. i810_writeb(CR_DATA_CGA, mmio, par->regs.cr02);
  240. i810_writeb(CR_INDEX_CGA, mmio, CR03);
  241. i810_writeb(CR_DATA_CGA, mmio, par->regs.cr03);
  242. i810_writeb(CR_INDEX_CGA, mmio, CR04);
  243. i810_writeb(CR_DATA_CGA, mmio, par->regs.cr04);
  244. i810_writeb(CR_INDEX_CGA, mmio, CR05);
  245. i810_writeb(CR_DATA_CGA, mmio, par->regs.cr05);
  246. i810_writeb(CR_INDEX_CGA, mmio, CR06);
  247. i810_writeb(CR_DATA_CGA, mmio, par->regs.cr06);
  248. i810_writeb(CR_INDEX_CGA, mmio, CR09);
  249. i810_writeb(CR_DATA_CGA, mmio, par->regs.cr09);
  250. i810_writeb(CR_INDEX_CGA, mmio, CR10);
  251. i810_writeb(CR_DATA_CGA, mmio, par->regs.cr10);
  252. i810_writeb(CR_INDEX_CGA, mmio, CR11);
  253. i810_writeb(CR_DATA_CGA, mmio, par->regs.cr11);
  254. i810_writeb(CR_INDEX_CGA, mmio, CR12);
  255. i810_writeb(CR_DATA_CGA, mmio, par->regs.cr12);
  256. i810_writeb(CR_INDEX_CGA, mmio, CR15);
  257. i810_writeb(CR_DATA_CGA, mmio, par->regs.cr15);
  258. i810_writeb(CR_INDEX_CGA, mmio, CR16);
  259. i810_writeb(CR_DATA_CGA, mmio, par->regs.cr16);
  260. }
  261. /**
  262. * i810_load_vgax - load extended VGA registers
  263. * @par: pointer to i810fb_par structure
  264. *
  265. * DESCRIPTION:
  266. * Load values to extended VGA registers
  267. */
  268. static void i810_load_vgax(struct i810fb_par *par)
  269. {
  270. u8 __iomem *mmio = par->mmio_start_virtual;
  271. i810_writeb(CR_INDEX_CGA, mmio, CR30);
  272. i810_writeb(CR_DATA_CGA, mmio, par->regs.cr30);
  273. i810_writeb(CR_INDEX_CGA, mmio, CR31);
  274. i810_writeb(CR_DATA_CGA, mmio, par->regs.cr31);
  275. i810_writeb(CR_INDEX_CGA, mmio, CR32);
  276. i810_writeb(CR_DATA_CGA, mmio, par->regs.cr32);
  277. i810_writeb(CR_INDEX_CGA, mmio, CR33);
  278. i810_writeb(CR_DATA_CGA, mmio, par->regs.cr33);
  279. i810_writeb(CR_INDEX_CGA, mmio, CR35);
  280. i810_writeb(CR_DATA_CGA, mmio, par->regs.cr35);
  281. i810_writeb(CR_INDEX_CGA, mmio, CR39);
  282. i810_writeb(CR_DATA_CGA, mmio, par->regs.cr39);
  283. }
  284. /**
  285. * i810_load_2d - load grahics registers
  286. * @par: pointer to i810fb_par structure
  287. *
  288. * DESCRIPTION:
  289. * Load values to graphics registers
  290. */
  291. static void i810_load_2d(struct i810fb_par *par)
  292. {
  293. u32 tmp;
  294. u8 tmp8;
  295. u8 __iomem *mmio = par->mmio_start_virtual;
  296. i810_writel(FW_BLC, mmio, par->watermark);
  297. tmp = i810_readl(PIXCONF, mmio);
  298. tmp |= 1 | 1 << 20;
  299. i810_writel(PIXCONF, mmio, tmp);
  300. i810_writel(OVRACT, mmio, par->ovract);
  301. i810_writeb(GR_INDEX, mmio, GR10);
  302. tmp8 = i810_readb(GR_DATA, mmio);
  303. tmp8 |= 2;
  304. i810_writeb(GR_INDEX, mmio, GR10);
  305. i810_writeb(GR_DATA, mmio, tmp8);
  306. }
  307. /**
  308. * i810_hires - enables high resolution mode
  309. * @mmio: address of register space
  310. */
  311. static void i810_hires(u8 __iomem *mmio)
  312. {
  313. u8 val;
  314. i810_writeb(CR_INDEX_CGA, mmio, CR80);
  315. val = i810_readb(CR_DATA_CGA, mmio);
  316. i810_writeb(CR_INDEX_CGA, mmio, CR80);
  317. i810_writeb(CR_DATA_CGA, mmio, val | 1);
  318. /* Stop LCD displays from flickering */
  319. i810_writel(MEM_MODE, mmio, i810_readl(MEM_MODE, mmio) | 4);
  320. }
  321. /**
  322. * i810_load_pitch - loads the characters per line of the display
  323. * @par: pointer to i810fb_par structure
  324. *
  325. * DESCRIPTION:
  326. * Loads the characters per line
  327. */
  328. static void i810_load_pitch(struct i810fb_par *par)
  329. {
  330. u32 tmp, pitch;
  331. u8 val;
  332. u8 __iomem *mmio = par->mmio_start_virtual;
  333. pitch = par->pitch >> 3;
  334. i810_writeb(SR_INDEX, mmio, SR01);
  335. val = i810_readb(SR_DATA, mmio);
  336. val &= 0xE0;
  337. val |= 1 | 1 << 2;
  338. i810_writeb(SR_INDEX, mmio, SR01);
  339. i810_writeb(SR_DATA, mmio, val);
  340. tmp = pitch & 0xFF;
  341. i810_writeb(CR_INDEX_CGA, mmio, CR13);
  342. i810_writeb(CR_DATA_CGA, mmio, (u8) tmp);
  343. tmp = pitch >> 8;
  344. i810_writeb(CR_INDEX_CGA, mmio, CR41);
  345. val = i810_readb(CR_DATA_CGA, mmio) & ~0x0F;
  346. i810_writeb(CR_INDEX_CGA, mmio, CR41);
  347. i810_writeb(CR_DATA_CGA, mmio, (u8) tmp | val);
  348. }
  349. /**
  350. * i810_load_color - loads the color depth of the display
  351. * @par: pointer to i810fb_par structure
  352. *
  353. * DESCRIPTION:
  354. * Loads the color depth of the display and the graphics engine
  355. */
  356. static void i810_load_color(struct i810fb_par *par)
  357. {
  358. u8 __iomem *mmio = par->mmio_start_virtual;
  359. u32 reg1;
  360. u16 reg2;
  361. reg1 = i810_readl(PIXCONF, mmio) & ~(0xF0000 | 1 << 27);
  362. reg2 = i810_readw(BLTCNTL, mmio) & ~0x30;
  363. reg1 |= 0x8000 | par->pixconf;
  364. reg2 |= par->bltcntl;
  365. i810_writel(PIXCONF, mmio, reg1);
  366. i810_writew(BLTCNTL, mmio, reg2);
  367. }
  368. /**
  369. * i810_load_regs - loads all registers for the mode
  370. * @par: pointer to i810fb_par structure
  371. *
  372. * DESCRIPTION:
  373. * Loads registers
  374. */
  375. static void i810_load_regs(struct i810fb_par *par)
  376. {
  377. u8 __iomem *mmio = par->mmio_start_virtual;
  378. i810_screen_off(mmio, OFF);
  379. i810_protect_regs(mmio, OFF);
  380. i810_dram_off(mmio, OFF);
  381. i810_load_pll(par);
  382. i810_load_vga(par);
  383. i810_load_vgax(par);
  384. i810_dram_off(mmio, ON);
  385. i810_load_2d(par);
  386. i810_hires(mmio);
  387. i810_screen_off(mmio, ON);
  388. i810_protect_regs(mmio, ON);
  389. i810_load_color(par);
  390. i810_load_pitch(par);
  391. }
  392. static void i810_write_dac(u8 regno, u8 red, u8 green, u8 blue,
  393. u8 __iomem *mmio)
  394. {
  395. i810_writeb(CLUT_INDEX_WRITE, mmio, regno);
  396. i810_writeb(CLUT_DATA, mmio, red);
  397. i810_writeb(CLUT_DATA, mmio, green);
  398. i810_writeb(CLUT_DATA, mmio, blue);
  399. }
  400. static void i810_read_dac(u8 regno, u8 *red, u8 *green, u8 *blue,
  401. u8 __iomem *mmio)
  402. {
  403. i810_writeb(CLUT_INDEX_READ, mmio, regno);
  404. *red = i810_readb(CLUT_DATA, mmio);
  405. *green = i810_readb(CLUT_DATA, mmio);
  406. *blue = i810_readb(CLUT_DATA, mmio);
  407. }
  408. /************************************************************
  409. * VGA State Restore *
  410. ************************************************************/
  411. static void i810_restore_pll(struct i810fb_par *par)
  412. {
  413. u32 tmp1, tmp2;
  414. u8 __iomem *mmio = par->mmio_start_virtual;
  415. tmp1 = par->hw_state.dclk_2d;
  416. tmp2 = i810_readl(DCLK_2D, mmio);
  417. tmp1 &= ~MN_MASK;
  418. tmp2 &= MN_MASK;
  419. i810_writel(DCLK_2D, mmio, tmp1 | tmp2);
  420. tmp1 = par->hw_state.dclk_1d;
  421. tmp2 = i810_readl(DCLK_1D, mmio);
  422. tmp1 &= ~MN_MASK;
  423. tmp2 &= MN_MASK;
  424. i810_writel(DCLK_1D, mmio, tmp1 | tmp2);
  425. i810_writel(DCLK_0DS, mmio, par->hw_state.dclk_0ds);
  426. }
  427. static void i810_restore_dac(struct i810fb_par *par)
  428. {
  429. u32 tmp1, tmp2;
  430. u8 __iomem *mmio = par->mmio_start_virtual;
  431. tmp1 = par->hw_state.pixconf;
  432. tmp2 = i810_readl(PIXCONF, mmio);
  433. tmp1 &= DAC_BIT;
  434. tmp2 &= ~DAC_BIT;
  435. i810_writel(PIXCONF, mmio, tmp1 | tmp2);
  436. }
  437. static void i810_restore_vgax(struct i810fb_par *par)
  438. {
  439. u8 i, j;
  440. u8 __iomem *mmio = par->mmio_start_virtual;
  441. for (i = 0; i < 4; i++) {
  442. i810_writeb(CR_INDEX_CGA, mmio, CR30+i);
  443. i810_writeb(CR_DATA_CGA, mmio, *(&(par->hw_state.cr30) + i));
  444. }
  445. i810_writeb(CR_INDEX_CGA, mmio, CR35);
  446. i810_writeb(CR_DATA_CGA, mmio, par->hw_state.cr35);
  447. i810_writeb(CR_INDEX_CGA, mmio, CR39);
  448. i810_writeb(CR_DATA_CGA, mmio, par->hw_state.cr39);
  449. i810_writeb(CR_INDEX_CGA, mmio, CR41);
  450. i810_writeb(CR_DATA_CGA, mmio, par->hw_state.cr39);
  451. /*restore interlace*/
  452. i810_writeb(CR_INDEX_CGA, mmio, CR70);
  453. i = par->hw_state.cr70;
  454. i &= INTERLACE_BIT;
  455. j = i810_readb(CR_DATA_CGA, mmio);
  456. i810_writeb(CR_INDEX_CGA, mmio, CR70);
  457. i810_writeb(CR_DATA_CGA, mmio, j | i);
  458. i810_writeb(CR_INDEX_CGA, mmio, CR80);
  459. i810_writeb(CR_DATA_CGA, mmio, par->hw_state.cr80);
  460. i810_writeb(MSR_WRITE, mmio, par->hw_state.msr);
  461. i810_writeb(SR_INDEX, mmio, SR01);
  462. i = (par->hw_state.sr01) & ~0xE0 ;
  463. j = i810_readb(SR_DATA, mmio) & 0xE0;
  464. i810_writeb(SR_INDEX, mmio, SR01);
  465. i810_writeb(SR_DATA, mmio, i | j);
  466. }
  467. static void i810_restore_vga(struct i810fb_par *par)
  468. {
  469. u8 i;
  470. u8 __iomem *mmio = par->mmio_start_virtual;
  471. for (i = 0; i < 10; i++) {
  472. i810_writeb(CR_INDEX_CGA, mmio, CR00 + i);
  473. i810_writeb(CR_DATA_CGA, mmio, *((&par->hw_state.cr00) + i));
  474. }
  475. for (i = 0; i < 8; i++) {
  476. i810_writeb(CR_INDEX_CGA, mmio, CR10 + i);
  477. i810_writeb(CR_DATA_CGA, mmio, *((&par->hw_state.cr10) + i));
  478. }
  479. }
  480. static void i810_restore_addr_map(struct i810fb_par *par)
  481. {
  482. u8 tmp;
  483. u8 __iomem *mmio = par->mmio_start_virtual;
  484. i810_writeb(GR_INDEX, mmio, GR10);
  485. tmp = i810_readb(GR_DATA, mmio);
  486. tmp &= ADDR_MAP_MASK;
  487. tmp |= par->hw_state.gr10;
  488. i810_writeb(GR_INDEX, mmio, GR10);
  489. i810_writeb(GR_DATA, mmio, tmp);
  490. }
  491. static void i810_restore_2d(struct i810fb_par *par)
  492. {
  493. u32 tmp_long;
  494. u16 tmp_word;
  495. u8 __iomem *mmio = par->mmio_start_virtual;
  496. tmp_word = i810_readw(BLTCNTL, mmio);
  497. tmp_word &= ~(3 << 4);
  498. tmp_word |= par->hw_state.bltcntl;
  499. i810_writew(BLTCNTL, mmio, tmp_word);
  500. i810_dram_off(mmio, OFF);
  501. i810_writel(PIXCONF, mmio, par->hw_state.pixconf);
  502. i810_dram_off(mmio, ON);
  503. tmp_word = i810_readw(HWSTAM, mmio);
  504. tmp_word &= 3 << 13;
  505. tmp_word |= par->hw_state.hwstam;
  506. i810_writew(HWSTAM, mmio, tmp_word);
  507. tmp_long = i810_readl(FW_BLC, mmio);
  508. tmp_long &= FW_BLC_MASK;
  509. tmp_long |= par->hw_state.fw_blc;
  510. i810_writel(FW_BLC, mmio, tmp_long);
  511. i810_writel(HWS_PGA, mmio, par->hw_state.hws_pga);
  512. i810_writew(IER, mmio, par->hw_state.ier);
  513. i810_writew(IMR, mmio, par->hw_state.imr);
  514. i810_writel(DPLYSTAS, mmio, par->hw_state.dplystas);
  515. }
  516. static void i810_restore_vga_state(struct i810fb_par *par)
  517. {
  518. u8 __iomem *mmio = par->mmio_start_virtual;
  519. i810_screen_off(mmio, OFF);
  520. i810_protect_regs(mmio, OFF);
  521. i810_dram_off(mmio, OFF);
  522. i810_restore_pll(par);
  523. i810_restore_dac(par);
  524. i810_restore_vga(par);
  525. i810_restore_vgax(par);
  526. i810_restore_addr_map(par);
  527. i810_dram_off(mmio, ON);
  528. i810_restore_2d(par);
  529. i810_screen_off(mmio, ON);
  530. i810_protect_regs(mmio, ON);
  531. }
  532. /***********************************************************************
  533. * VGA State Save *
  534. ***********************************************************************/
  535. static void i810_save_vgax(struct i810fb_par *par)
  536. {
  537. u8 i;
  538. u8 __iomem *mmio = par->mmio_start_virtual;
  539. for (i = 0; i < 4; i++) {
  540. i810_writeb(CR_INDEX_CGA, mmio, CR30 + i);
  541. *(&(par->hw_state.cr30) + i) = i810_readb(CR_DATA_CGA, mmio);
  542. }
  543. i810_writeb(CR_INDEX_CGA, mmio, CR35);
  544. par->hw_state.cr35 = i810_readb(CR_DATA_CGA, mmio);
  545. i810_writeb(CR_INDEX_CGA, mmio, CR39);
  546. par->hw_state.cr39 = i810_readb(CR_DATA_CGA, mmio);
  547. i810_writeb(CR_INDEX_CGA, mmio, CR41);
  548. par->hw_state.cr41 = i810_readb(CR_DATA_CGA, mmio);
  549. i810_writeb(CR_INDEX_CGA, mmio, CR70);
  550. par->hw_state.cr70 = i810_readb(CR_DATA_CGA, mmio);
  551. par->hw_state.msr = i810_readb(MSR_READ, mmio);
  552. i810_writeb(CR_INDEX_CGA, mmio, CR80);
  553. par->hw_state.cr80 = i810_readb(CR_DATA_CGA, mmio);
  554. i810_writeb(SR_INDEX, mmio, SR01);
  555. par->hw_state.sr01 = i810_readb(SR_DATA, mmio);
  556. }
  557. static void i810_save_vga(struct i810fb_par *par)
  558. {
  559. u8 i;
  560. u8 __iomem *mmio = par->mmio_start_virtual;
  561. for (i = 0; i < 10; i++) {
  562. i810_writeb(CR_INDEX_CGA, mmio, CR00 + i);
  563. *((&par->hw_state.cr00) + i) = i810_readb(CR_DATA_CGA, mmio);
  564. }
  565. for (i = 0; i < 8; i++) {
  566. i810_writeb(CR_INDEX_CGA, mmio, CR10 + i);
  567. *((&par->hw_state.cr10) + i) = i810_readb(CR_DATA_CGA, mmio);
  568. }
  569. }
  570. static void i810_save_2d(struct i810fb_par *par)
  571. {
  572. u8 __iomem *mmio = par->mmio_start_virtual;
  573. par->hw_state.dclk_2d = i810_readl(DCLK_2D, mmio);
  574. par->hw_state.dclk_1d = i810_readl(DCLK_1D, mmio);
  575. par->hw_state.dclk_0ds = i810_readl(DCLK_0DS, mmio);
  576. par->hw_state.pixconf = i810_readl(PIXCONF, mmio);
  577. par->hw_state.fw_blc = i810_readl(FW_BLC, mmio);
  578. par->hw_state.bltcntl = i810_readw(BLTCNTL, mmio);
  579. par->hw_state.hwstam = i810_readw(HWSTAM, mmio);
  580. par->hw_state.hws_pga = i810_readl(HWS_PGA, mmio);
  581. par->hw_state.ier = i810_readw(IER, mmio);
  582. par->hw_state.imr = i810_readw(IMR, mmio);
  583. par->hw_state.dplystas = i810_readl(DPLYSTAS, mmio);
  584. }
  585. static void i810_save_vga_state(struct i810fb_par *par)
  586. {
  587. i810_save_vga(par);
  588. i810_save_vgax(par);
  589. i810_save_2d(par);
  590. }
  591. /************************************************************
  592. * Helpers *
  593. ************************************************************/
  594. /**
  595. * get_line_length - calculates buffer pitch in bytes
  596. * @par: pointer to i810fb_par structure
  597. * @xres_virtual: virtual resolution of the frame
  598. * @bpp: bits per pixel
  599. *
  600. * DESCRIPTION:
  601. * Calculates buffer pitch in bytes.
  602. */
  603. static u32 get_line_length(struct i810fb_par *par, int xres_virtual, int bpp)
  604. {
  605. u32 length;
  606. length = xres_virtual*bpp;
  607. length = (length+31)&-32;
  608. length >>= 3;
  609. return length;
  610. }
  611. /**
  612. * i810_calc_dclk - calculates the P, M, and N values of a pixelclock value
  613. * @freq: target pixelclock in picoseconds
  614. * @m: where to write M register
  615. * @n: where to write N register
  616. * @p: where to write P register
  617. *
  618. * DESCRIPTION:
  619. * Based on the formula Freq_actual = (4*M*Freq_ref)/(N^P)
  620. * Repeatedly computes the Freq until the actual Freq is equal to
  621. * the target Freq or until the loop count is zero. In the latter
  622. * case, the actual frequency nearest the target will be used.
  623. */
  624. static void i810_calc_dclk(u32 freq, u32 *m, u32 *n, u32 *p)
  625. {
  626. u32 m_reg, n_reg, p_divisor, n_target_max;
  627. u32 m_target, n_target, p_target, n_best, m_best, mod;
  628. u32 f_out, target_freq, diff = 0, mod_min, diff_min;
  629. diff_min = mod_min = 0xFFFFFFFF;
  630. n_best = m_best = m_target = f_out = 0;
  631. target_freq = freq;
  632. n_target_max = 30;
  633. /*
  634. * find P such that target freq is 16x reference freq (Hz).
  635. */
  636. p_divisor = 1;
  637. p_target = 0;
  638. while(!((1000000 * p_divisor)/(16 * 24 * target_freq)) &&
  639. p_divisor <= 32) {
  640. p_divisor <<= 1;
  641. p_target++;
  642. }
  643. n_reg = m_reg = n_target = 3;
  644. while (diff_min && mod_min && (n_target < n_target_max)) {
  645. f_out = (p_divisor * n_reg * 1000000)/(4 * 24 * m_reg);
  646. mod = (p_divisor * n_reg * 1000000) % (4 * 24 * m_reg);
  647. m_target = m_reg;
  648. n_target = n_reg;
  649. if (f_out <= target_freq) {
  650. n_reg++;
  651. diff = target_freq - f_out;
  652. } else {
  653. m_reg++;
  654. diff = f_out - target_freq;
  655. }
  656. if (diff_min > diff) {
  657. diff_min = diff;
  658. n_best = n_target;
  659. m_best = m_target;
  660. }
  661. if (!diff && mod_min > mod) {
  662. mod_min = mod;
  663. n_best = n_target;
  664. m_best = m_target;
  665. }
  666. }
  667. if (m) *m = (m_best - 2) & 0x3FF;
  668. if (n) *n = (n_best - 2) & 0x3FF;
  669. if (p) *p = (p_target << 4);
  670. }
  671. /*************************************************************
  672. * Hardware Cursor Routines *
  673. *************************************************************/
  674. /**
  675. * i810_enable_cursor - show or hide the hardware cursor
  676. * @mmio: address of register space
  677. * @mode: show (1) or hide (0)
  678. *
  679. * Description:
  680. * Shows or hides the hardware cursor
  681. */
  682. static void i810_enable_cursor(u8 __iomem *mmio, int mode)
  683. {
  684. u32 temp;
  685. temp = i810_readl(PIXCONF, mmio);
  686. temp = (mode == ON) ? temp | CURSOR_ENABLE_MASK :
  687. temp & ~CURSOR_ENABLE_MASK;
  688. i810_writel(PIXCONF, mmio, temp);
  689. }
  690. static void i810_reset_cursor_image(struct i810fb_par *par)
  691. {
  692. u8 __iomem *addr = par->cursor_heap.virtual;
  693. int i, j;
  694. for (i = 64; i--; ) {
  695. for (j = 0; j < 8; j++) {
  696. i810_writeb(j, addr, 0xff);
  697. i810_writeb(j+8, addr, 0x00);
  698. }
  699. addr +=16;
  700. }
  701. }
  702. static void i810_load_cursor_image(int width, int height, u8 *data,
  703. struct i810fb_par *par)
  704. {
  705. u8 __iomem *addr = par->cursor_heap.virtual;
  706. int i, j, w = width/8;
  707. int mod = width % 8, t_mask, d_mask;
  708. t_mask = 0xff >> mod;
  709. d_mask = ~(0xff >> mod);
  710. for (i = height; i--; ) {
  711. for (j = 0; j < w; j++) {
  712. i810_writeb(j+0, addr, 0x00);
  713. i810_writeb(j+8, addr, *data++);
  714. }
  715. if (mod) {
  716. i810_writeb(j+0, addr, t_mask);
  717. i810_writeb(j+8, addr, *data++ & d_mask);
  718. }
  719. addr += 16;
  720. }
  721. }
  722. static void i810_load_cursor_colors(int fg, int bg, struct fb_info *info)
  723. {
  724. struct i810fb_par *par = info->par;
  725. u8 __iomem *mmio = par->mmio_start_virtual;
  726. u8 red, green, blue, trans, temp;
  727. i810fb_getcolreg(bg, &red, &green, &blue, &trans, info);
  728. temp = i810_readb(PIXCONF1, mmio);
  729. i810_writeb(PIXCONF1, mmio, temp | EXTENDED_PALETTE);
  730. i810_write_dac(4, red, green, blue, mmio);
  731. i810_writeb(PIXCONF1, mmio, temp);
  732. i810fb_getcolreg(fg, &red, &green, &blue, &trans, info);
  733. temp = i810_readb(PIXCONF1, mmio);
  734. i810_writeb(PIXCONF1, mmio, temp | EXTENDED_PALETTE);
  735. i810_write_dac(5, red, green, blue, mmio);
  736. i810_writeb(PIXCONF1, mmio, temp);
  737. }
  738. /**
  739. * i810_init_cursor - initializes the cursor
  740. * @par: pointer to i810fb_par structure
  741. *
  742. * DESCRIPTION:
  743. * Initializes the cursor registers
  744. */
  745. static void i810_init_cursor(struct i810fb_par *par)
  746. {
  747. u8 __iomem *mmio = par->mmio_start_virtual;
  748. i810_enable_cursor(mmio, OFF);
  749. i810_writel(CURBASE, mmio, par->cursor_heap.physical);
  750. i810_writew(CURCNTR, mmio, COORD_ACTIVE | CURSOR_MODE_64_XOR);
  751. }
  752. /*********************************************************************
  753. * Framebuffer hook helpers *
  754. *********************************************************************/
  755. /**
  756. * i810_round_off - Round off values to capability of hardware
  757. * @var: pointer to fb_var_screeninfo structure
  758. *
  759. * DESCRIPTION:
  760. * @var contains user-defined information for the mode to be set.
  761. * This will try modify those values to ones nearest the
  762. * capability of the hardware
  763. */
  764. static void i810_round_off(struct fb_var_screeninfo *var)
  765. {
  766. u32 xres, yres, vxres, vyres;
  767. /*
  768. * Presently supports only these configurations
  769. */
  770. xres = var->xres;
  771. yres = var->yres;
  772. vxres = var->xres_virtual;
  773. vyres = var->yres_virtual;
  774. var->bits_per_pixel += 7;
  775. var->bits_per_pixel &= ~7;
  776. if (var->bits_per_pixel < 8)
  777. var->bits_per_pixel = 8;
  778. if (var->bits_per_pixel > 32)
  779. var->bits_per_pixel = 32;
  780. round_off_xres(&xres);
  781. if (xres < 40)
  782. xres = 40;
  783. if (xres > 2048)
  784. xres = 2048;
  785. xres = (xres + 7) & ~7;
  786. if (vxres < xres)
  787. vxres = xres;
  788. round_off_yres(&xres, &yres);
  789. if (yres < 1)
  790. yres = 1;
  791. if (yres >= 2048)
  792. yres = 2048;
  793. if (vyres < yres)
  794. vyres = yres;
  795. if (var->bits_per_pixel == 32)
  796. var->accel_flags = 0;
  797. /* round of horizontal timings to nearest 8 pixels */
  798. var->left_margin = (var->left_margin + 4) & ~7;
  799. var->right_margin = (var->right_margin + 4) & ~7;
  800. var->hsync_len = (var->hsync_len + 4) & ~7;
  801. if (var->vmode & FB_VMODE_INTERLACED) {
  802. if (!((yres + var->upper_margin + var->vsync_len +
  803. var->lower_margin) & 1))
  804. var->upper_margin++;
  805. }
  806. var->xres = xres;
  807. var->yres = yres;
  808. var->xres_virtual = vxres;
  809. var->yres_virtual = vyres;
  810. }
  811. /**
  812. * set_color_bitfields - sets rgba fields
  813. * @var: pointer to fb_var_screeninfo
  814. *
  815. * DESCRIPTION:
  816. * The length, offset and ordering for each color field
  817. * (red, green, blue) will be set as specified
  818. * by the hardware
  819. */
  820. static void set_color_bitfields(struct fb_var_screeninfo *var)
  821. {
  822. switch (var->bits_per_pixel) {
  823. case 8:
  824. var->red.offset = 0;
  825. var->red.length = 8;
  826. var->green.offset = 0;
  827. var->green.length = 8;
  828. var->blue.offset = 0;
  829. var->blue.length = 8;
  830. var->transp.offset = 0;
  831. var->transp.length = 0;
  832. break;
  833. case 16:
  834. var->green.length = (var->green.length == 5) ? 5 : 6;
  835. var->red.length = 5;
  836. var->blue.length = 5;
  837. var->transp.length = 6 - var->green.length;
  838. var->blue.offset = 0;
  839. var->green.offset = 5;
  840. var->red.offset = 5 + var->green.length;
  841. var->transp.offset = (5 + var->red.offset) & 15;
  842. break;
  843. case 24: /* RGB 888 */
  844. case 32: /* RGBA 8888 */
  845. var->red.offset = 16;
  846. var->red.length = 8;
  847. var->green.offset = 8;
  848. var->green.length = 8;
  849. var->blue.offset = 0;
  850. var->blue.length = 8;
  851. var->transp.length = var->bits_per_pixel - 24;
  852. var->transp.offset = (var->transp.length) ? 24 : 0;
  853. break;
  854. }
  855. var->red.msb_right = 0;
  856. var->green.msb_right = 0;
  857. var->blue.msb_right = 0;
  858. var->transp.msb_right = 0;
  859. }
  860. /**
  861. * i810_check_params - check if contents in var are valid
  862. * @var: pointer to fb_var_screeninfo
  863. * @info: pointer to fb_info
  864. *
  865. * DESCRIPTION:
  866. * This will check if the framebuffer size is sufficient
  867. * for the current mode and if the user's monitor has the
  868. * required specifications to display the current mode.
  869. */
  870. static int i810_check_params(struct fb_var_screeninfo *var,
  871. struct fb_info *info)
  872. {
  873. struct i810fb_par *par = info->par;
  874. int line_length, vidmem, mode_valid = 0, retval = 0;
  875. u32 vyres = var->yres_virtual, vxres = var->xres_virtual;
  876. /*
  877. * Memory limit
  878. */
  879. line_length = get_line_length(par, vxres, var->bits_per_pixel);
  880. vidmem = line_length*vyres;
  881. if (vidmem > par->fb.size) {
  882. vyres = par->fb.size/line_length;
  883. if (vyres < var->yres) {
  884. vyres = info->var.yres;
  885. vxres = par->fb.size/vyres;
  886. vxres /= var->bits_per_pixel >> 3;
  887. line_length = get_line_length(par, vxres,
  888. var->bits_per_pixel);
  889. vidmem = line_length * info->var.yres;
  890. if (vxres < var->xres) {
  891. printk("i810fb: required video memory, "
  892. "%d bytes, for %dx%d-%d (virtual) "
  893. "is out of range\n",
  894. vidmem, vxres, vyres,
  895. var->bits_per_pixel);
  896. return -ENOMEM;
  897. }
  898. }
  899. }
  900. var->xres_virtual = vxres;
  901. var->yres_virtual = vyres;
  902. /*
  903. * Monitor limit
  904. */
  905. switch (var->bits_per_pixel) {
  906. case 8:
  907. info->monspecs.dclkmax = 234000000;
  908. break;
  909. case 16:
  910. info->monspecs.dclkmax = 229000000;
  911. break;
  912. case 24:
  913. case 32:
  914. info->monspecs.dclkmax = 204000000;
  915. break;
  916. }
  917. info->monspecs.dclkmin = 15000000;
  918. if (!fb_validate_mode(var, info))
  919. mode_valid = 1;
  920. #ifdef CONFIG_FB_I810_I2C
  921. if (!mode_valid && info->monspecs.gtf &&
  922. !fb_get_mode(FB_MAXTIMINGS, 0, var, info))
  923. mode_valid = 1;
  924. if (!mode_valid && info->monspecs.modedb_len) {
  925. const struct fb_videomode *mode;
  926. mode = fb_find_best_mode(var, &info->modelist);
  927. if (mode) {
  928. fb_videomode_to_var(var, mode);
  929. mode_valid = 1;
  930. }
  931. }
  932. #endif
  933. if (!mode_valid && info->monspecs.modedb_len == 0) {
  934. if (fb_get_mode(FB_MAXTIMINGS, 0, var, info)) {
  935. int default_sync = (info->monspecs.hfmin-HFMIN)
  936. |(info->monspecs.hfmax-HFMAX)
  937. |(info->monspecs.vfmin-VFMIN)
  938. |(info->monspecs.vfmax-VFMAX);
  939. printk("i810fb: invalid video mode%s\n",
  940. default_sync ? "" : ". Specifying "
  941. "vsyncN/hsyncN parameters may help");
  942. retval = -EINVAL;
  943. }
  944. }
  945. return retval;
  946. }
  947. /**
  948. * encode_fix - fill up fb_fix_screeninfo structure
  949. * @fix: pointer to fb_fix_screeninfo
  950. * @info: pointer to fb_info
  951. *
  952. * DESCRIPTION:
  953. * This will set up parameters that are unmodifiable by the user.
  954. */
  955. static int encode_fix(struct fb_fix_screeninfo *fix, struct fb_info *info)
  956. {
  957. struct i810fb_par *par = info->par;
  958. memset(fix, 0, sizeof(struct fb_fix_screeninfo));
  959. strcpy(fix->id, "I810");
  960. mutex_lock(&info->mm_lock);
  961. fix->smem_start = par->fb.physical;
  962. fix->smem_len = par->fb.size;
  963. mutex_unlock(&info->mm_lock);
  964. fix->type = FB_TYPE_PACKED_PIXELS;
  965. fix->type_aux = 0;
  966. fix->xpanstep = 8;
  967. fix->ypanstep = 1;
  968. switch (info->var.bits_per_pixel) {
  969. case 8:
  970. fix->visual = FB_VISUAL_PSEUDOCOLOR;
  971. break;
  972. case 16:
  973. case 24:
  974. case 32:
  975. if (info->var.nonstd)
  976. fix->visual = FB_VISUAL_DIRECTCOLOR;
  977. else
  978. fix->visual = FB_VISUAL_TRUECOLOR;
  979. break;
  980. default:
  981. return -EINVAL;
  982. }
  983. fix->ywrapstep = 0;
  984. fix->line_length = par->pitch;
  985. fix->mmio_start = par->mmio_start_phys;
  986. fix->mmio_len = MMIO_SIZE;
  987. fix->accel = FB_ACCEL_I810;
  988. return 0;
  989. }
  990. /**
  991. * decode_var - modify par according to contents of var
  992. * @var: pointer to fb_var_screeninfo
  993. * @par: pointer to i810fb_par
  994. *
  995. * DESCRIPTION:
  996. * Based on the contents of @var, @par will be dynamically filled up.
  997. * @par contains all information necessary to modify the hardware.
  998. */
  999. static void decode_var(const struct fb_var_screeninfo *var,
  1000. struct i810fb_par *par)
  1001. {
  1002. u32 xres, yres, vxres, vyres;
  1003. xres = var->xres;
  1004. yres = var->yres;
  1005. vxres = var->xres_virtual;
  1006. vyres = var->yres_virtual;
  1007. switch (var->bits_per_pixel) {
  1008. case 8:
  1009. par->pixconf = PIXCONF8;
  1010. par->bltcntl = 0;
  1011. par->depth = 1;
  1012. par->blit_bpp = BPP8;
  1013. break;
  1014. case 16:
  1015. if (var->green.length == 5)
  1016. par->pixconf = PIXCONF15;
  1017. else
  1018. par->pixconf = PIXCONF16;
  1019. par->bltcntl = 16;
  1020. par->depth = 2;
  1021. par->blit_bpp = BPP16;
  1022. break;
  1023. case 24:
  1024. par->pixconf = PIXCONF24;
  1025. par->bltcntl = 32;
  1026. par->depth = 3;
  1027. par->blit_bpp = BPP24;
  1028. break;
  1029. case 32:
  1030. par->pixconf = PIXCONF32;
  1031. par->bltcntl = 0;
  1032. par->depth = 4;
  1033. par->blit_bpp = 3 << 24;
  1034. break;
  1035. }
  1036. if (var->nonstd && var->bits_per_pixel != 8)
  1037. par->pixconf |= 1 << 27;
  1038. i810_calc_dclk(var->pixclock, &par->regs.M,
  1039. &par->regs.N, &par->regs.P);
  1040. i810fb_encode_registers(var, par, xres, yres);
  1041. par->watermark = i810_get_watermark(var, par);
  1042. par->pitch = get_line_length(par, vxres, var->bits_per_pixel);
  1043. }
  1044. /**
  1045. * i810fb_getcolreg - gets red, green and blue values of the hardware DAC
  1046. * @regno: DAC index
  1047. * @red: red
  1048. * @green: green
  1049. * @blue: blue
  1050. * @transp: transparency (alpha)
  1051. * @info: pointer to fb_info
  1052. *
  1053. * DESCRIPTION:
  1054. * Gets the red, green and blue values of the hardware DAC as pointed by @regno
  1055. * and writes them to @red, @green and @blue respectively
  1056. */
  1057. static int i810fb_getcolreg(u8 regno, u8 *red, u8 *green, u8 *blue,
  1058. u8 *transp, struct fb_info *info)
  1059. {
  1060. struct i810fb_par *par = info->par;
  1061. u8 __iomem *mmio = par->mmio_start_virtual;
  1062. u8 temp;
  1063. if (info->fix.visual == FB_VISUAL_DIRECTCOLOR) {
  1064. if ((info->var.green.length == 5 && regno > 31) ||
  1065. (info->var.green.length == 6 && regno > 63))
  1066. return 1;
  1067. }
  1068. temp = i810_readb(PIXCONF1, mmio);
  1069. i810_writeb(PIXCONF1, mmio, temp & ~EXTENDED_PALETTE);
  1070. if (info->fix.visual == FB_VISUAL_DIRECTCOLOR &&
  1071. info->var.green.length == 5)
  1072. i810_read_dac(regno * 8, red, green, blue, mmio);
  1073. else if (info->fix.visual == FB_VISUAL_DIRECTCOLOR &&
  1074. info->var.green.length == 6) {
  1075. u8 tmp;
  1076. i810_read_dac(regno * 8, red, &tmp, blue, mmio);
  1077. i810_read_dac(regno * 4, &tmp, green, &tmp, mmio);
  1078. }
  1079. else
  1080. i810_read_dac(regno, red, green, blue, mmio);
  1081. *transp = 0;
  1082. i810_writeb(PIXCONF1, mmio, temp);
  1083. return 0;
  1084. }
  1085. /******************************************************************
  1086. * Framebuffer device-specific hooks *
  1087. ******************************************************************/
  1088. static int i810fb_open(struct fb_info *info, int user)
  1089. {
  1090. struct i810fb_par *par = info->par;
  1091. mutex_lock(&par->open_lock);
  1092. if (par->use_count == 0) {
  1093. memset(&par->state, 0, sizeof(struct vgastate));
  1094. par->state.flags = VGA_SAVE_CMAP;
  1095. par->state.vgabase = par->mmio_start_virtual;
  1096. save_vga(&par->state);
  1097. i810_save_vga_state(par);
  1098. }
  1099. par->use_count++;
  1100. mutex_unlock(&par->open_lock);
  1101. return 0;
  1102. }
  1103. static int i810fb_release(struct fb_info *info, int user)
  1104. {
  1105. struct i810fb_par *par = info->par;
  1106. mutex_lock(&par->open_lock);
  1107. if (par->use_count == 0) {
  1108. mutex_unlock(&par->open_lock);
  1109. return -EINVAL;
  1110. }
  1111. if (par->use_count == 1) {
  1112. i810_restore_vga_state(par);
  1113. restore_vga(&par->state);
  1114. }
  1115. par->use_count--;
  1116. mutex_unlock(&par->open_lock);
  1117. return 0;
  1118. }
  1119. static int i810fb_setcolreg(unsigned regno, unsigned red, unsigned green,
  1120. unsigned blue, unsigned transp,
  1121. struct fb_info *info)
  1122. {
  1123. struct i810fb_par *par = info->par;
  1124. u8 __iomem *mmio = par->mmio_start_virtual;
  1125. u8 temp;
  1126. int i;
  1127. if (regno > 255) return 1;
  1128. if (info->fix.visual == FB_VISUAL_DIRECTCOLOR) {
  1129. if ((info->var.green.length == 5 && regno > 31) ||
  1130. (info->var.green.length == 6 && regno > 63))
  1131. return 1;
  1132. }
  1133. if (info->var.grayscale)
  1134. red = green = blue = (19595 * red + 38470 * green +
  1135. 7471 * blue) >> 16;
  1136. temp = i810_readb(PIXCONF1, mmio);
  1137. i810_writeb(PIXCONF1, mmio, temp & ~EXTENDED_PALETTE);
  1138. if (info->fix.visual == FB_VISUAL_DIRECTCOLOR &&
  1139. info->var.green.length == 5) {
  1140. for (i = 0; i < 8; i++)
  1141. i810_write_dac((u8) (regno * 8) + i, (u8) red,
  1142. (u8) green, (u8) blue, mmio);
  1143. } else if (info->fix.visual == FB_VISUAL_DIRECTCOLOR &&
  1144. info->var.green.length == 6) {
  1145. u8 r, g, b;
  1146. if (regno < 32) {
  1147. for (i = 0; i < 8; i++)
  1148. i810_write_dac((u8) (regno * 8) + i,
  1149. (u8) red, (u8) green,
  1150. (u8) blue, mmio);
  1151. }
  1152. i810_read_dac((u8) (regno*4), &r, &g, &b, mmio);
  1153. for (i = 0; i < 4; i++)
  1154. i810_write_dac((u8) (regno*4) + i, r, (u8) green,
  1155. b, mmio);
  1156. } else if (info->fix.visual == FB_VISUAL_PSEUDOCOLOR) {
  1157. i810_write_dac((u8) regno, (u8) red, (u8) green,
  1158. (u8) blue, mmio);
  1159. }
  1160. i810_writeb(PIXCONF1, mmio, temp);
  1161. if (regno < 16) {
  1162. switch (info->var.bits_per_pixel) {
  1163. case 16:
  1164. if (info->fix.visual == FB_VISUAL_DIRECTCOLOR) {
  1165. if (info->var.green.length == 5)
  1166. ((u32 *)info->pseudo_palette)[regno] =
  1167. (regno << 10) | (regno << 5) |
  1168. regno;
  1169. else
  1170. ((u32 *)info->pseudo_palette)[regno] =
  1171. (regno << 11) | (regno << 5) |
  1172. regno;
  1173. } else {
  1174. if (info->var.green.length == 5) {
  1175. /* RGB 555 */
  1176. ((u32 *)info->pseudo_palette)[regno] =
  1177. ((red & 0xf800) >> 1) |
  1178. ((green & 0xf800) >> 6) |
  1179. ((blue & 0xf800) >> 11);
  1180. } else {
  1181. /* RGB 565 */
  1182. ((u32 *)info->pseudo_palette)[regno] =
  1183. (red & 0xf800) |
  1184. ((green & 0xf800) >> 5) |
  1185. ((blue & 0xf800) >> 11);
  1186. }
  1187. }
  1188. break;
  1189. case 24: /* RGB 888 */
  1190. case 32: /* RGBA 8888 */
  1191. if (info->fix.visual == FB_VISUAL_DIRECTCOLOR)
  1192. ((u32 *)info->pseudo_palette)[regno] =
  1193. (regno << 16) | (regno << 8) |
  1194. regno;
  1195. else
  1196. ((u32 *)info->pseudo_palette)[regno] =
  1197. ((red & 0xff00) << 8) |
  1198. (green & 0xff00) |
  1199. ((blue & 0xff00) >> 8);
  1200. break;
  1201. }
  1202. }
  1203. return 0;
  1204. }
  1205. static int i810fb_pan_display(struct fb_var_screeninfo *var,
  1206. struct fb_info *info)
  1207. {
  1208. struct i810fb_par *par = info->par;
  1209. u32 total;
  1210. total = var->xoffset * par->depth +
  1211. var->yoffset * info->fix.line_length;
  1212. i810fb_load_front(total, info);
  1213. return 0;
  1214. }
  1215. static int i810fb_blank (int blank_mode, struct fb_info *info)
  1216. {
  1217. struct i810fb_par *par = info->par;
  1218. u8 __iomem *mmio = par->mmio_start_virtual;
  1219. int mode = 0, pwr, scr_off = 0;
  1220. pwr = i810_readl(PWR_CLKC, mmio);
  1221. switch (blank_mode) {
  1222. case FB_BLANK_UNBLANK:
  1223. mode = POWERON;
  1224. pwr |= 1;
  1225. scr_off = ON;
  1226. break;
  1227. case FB_BLANK_NORMAL:
  1228. mode = POWERON;
  1229. pwr |= 1;
  1230. scr_off = OFF;
  1231. break;
  1232. case FB_BLANK_VSYNC_SUSPEND:
  1233. mode = STANDBY;
  1234. pwr |= 1;
  1235. scr_off = OFF;
  1236. break;
  1237. case FB_BLANK_HSYNC_SUSPEND:
  1238. mode = SUSPEND;
  1239. pwr |= 1;
  1240. scr_off = OFF;
  1241. break;
  1242. case FB_BLANK_POWERDOWN:
  1243. mode = POWERDOWN;
  1244. pwr &= ~1;
  1245. scr_off = OFF;
  1246. break;
  1247. default:
  1248. return -EINVAL;
  1249. }
  1250. i810_screen_off(mmio, scr_off);
  1251. i810_writel(HVSYNC, mmio, mode);
  1252. i810_writel(PWR_CLKC, mmio, pwr);
  1253. return 0;
  1254. }
  1255. static int i810fb_set_par(struct fb_info *info)
  1256. {
  1257. struct i810fb_par *par = info->par;
  1258. decode_var(&info->var, par);
  1259. i810_load_regs(par);
  1260. i810_init_cursor(par);
  1261. encode_fix(&info->fix, info);
  1262. if (info->var.accel_flags && !(par->dev_flags & LOCKUP)) {
  1263. info->flags = FBINFO_DEFAULT | FBINFO_HWACCEL_YPAN |
  1264. FBINFO_HWACCEL_COPYAREA | FBINFO_HWACCEL_FILLRECT |
  1265. FBINFO_HWACCEL_IMAGEBLIT;
  1266. info->pixmap.scan_align = 2;
  1267. } else {
  1268. info->pixmap.scan_align = 1;
  1269. info->flags = FBINFO_DEFAULT | FBINFO_HWACCEL_YPAN;
  1270. }
  1271. return 0;
  1272. }
  1273. static int i810fb_check_var(struct fb_var_screeninfo *var,
  1274. struct fb_info *info)
  1275. {
  1276. int err;
  1277. if (IS_DVT) {
  1278. var->vmode &= ~FB_VMODE_MASK;
  1279. var->vmode |= FB_VMODE_NONINTERLACED;
  1280. }
  1281. if (var->vmode & FB_VMODE_DOUBLE) {
  1282. var->vmode &= ~FB_VMODE_MASK;
  1283. var->vmode |= FB_VMODE_NONINTERLACED;
  1284. }
  1285. i810_round_off(var);
  1286. if ((err = i810_check_params(var, info)))
  1287. return err;
  1288. i810fb_fill_var_timings(var);
  1289. set_color_bitfields(var);
  1290. return 0;
  1291. }
  1292. static int i810fb_cursor(struct fb_info *info, struct fb_cursor *cursor)
  1293. {
  1294. struct i810fb_par *par = info->par;
  1295. u8 __iomem *mmio = par->mmio_start_virtual;
  1296. if (par->dev_flags & LOCKUP)
  1297. return -ENXIO;
  1298. if (cursor->image.width > 64 || cursor->image.height > 64)
  1299. return -ENXIO;
  1300. if ((i810_readl(CURBASE, mmio) & 0xf) != par->cursor_heap.physical) {
  1301. i810_init_cursor(par);
  1302. cursor->set |= FB_CUR_SETALL;
  1303. }
  1304. i810_enable_cursor(mmio, OFF);
  1305. if (cursor->set & FB_CUR_SETPOS) {
  1306. u32 tmp;
  1307. tmp = (cursor->image.dx - info->var.xoffset) & 0xffff;
  1308. tmp |= (cursor->image.dy - info->var.yoffset) << 16;
  1309. i810_writel(CURPOS, mmio, tmp);
  1310. }
  1311. if (cursor->set & FB_CUR_SETSIZE)
  1312. i810_reset_cursor_image(par);
  1313. if (cursor->set & FB_CUR_SETCMAP)
  1314. i810_load_cursor_colors(cursor->image.fg_color,
  1315. cursor->image.bg_color,
  1316. info);
  1317. if (cursor->set & (FB_CUR_SETSHAPE | FB_CUR_SETIMAGE)) {
  1318. int size = ((cursor->image.width + 7) >> 3) *
  1319. cursor->image.height;
  1320. int i;
  1321. u8 *data = kmalloc(64 * 8, GFP_ATOMIC);
  1322. if (data == NULL)
  1323. return -ENOMEM;
  1324. switch (cursor->rop) {
  1325. case ROP_XOR:
  1326. for (i = 0; i < size; i++)
  1327. data[i] = cursor->image.data[i] ^ cursor->mask[i];
  1328. break;
  1329. case ROP_COPY:
  1330. default:
  1331. for (i = 0; i < size; i++)
  1332. data[i] = cursor->image.data[i] & cursor->mask[i];
  1333. break;
  1334. }
  1335. i810_load_cursor_image(cursor->image.width,
  1336. cursor->image.height, data,
  1337. par);
  1338. kfree(data);
  1339. }
  1340. if (cursor->enable)
  1341. i810_enable_cursor(mmio, ON);
  1342. return 0;
  1343. }
  1344. static struct fb_ops i810fb_ops __devinitdata = {
  1345. .owner = THIS_MODULE,
  1346. .fb_open = i810fb_open,
  1347. .fb_release = i810fb_release,
  1348. .fb_check_var = i810fb_check_var,
  1349. .fb_set_par = i810fb_set_par,
  1350. .fb_setcolreg = i810fb_setcolreg,
  1351. .fb_blank = i810fb_blank,
  1352. .fb_pan_display = i810fb_pan_display,
  1353. .fb_fillrect = i810fb_fillrect,
  1354. .fb_copyarea = i810fb_copyarea,
  1355. .fb_imageblit = i810fb_imageblit,
  1356. .fb_cursor = i810fb_cursor,
  1357. .fb_sync = i810fb_sync,
  1358. };
  1359. /***********************************************************************
  1360. * Power Management *
  1361. ***********************************************************************/
  1362. static int i810fb_suspend(struct pci_dev *dev, pm_message_t mesg)
  1363. {
  1364. struct fb_info *info = pci_get_drvdata(dev);
  1365. struct i810fb_par *par = info->par;
  1366. par->cur_state = mesg.event;
  1367. switch (mesg.event) {
  1368. case PM_EVENT_FREEZE:
  1369. case PM_EVENT_PRETHAW:
  1370. dev->dev.power.power_state = mesg;
  1371. return 0;
  1372. }
  1373. console_lock();
  1374. fb_set_suspend(info, 1);
  1375. if (info->fbops->fb_sync)
  1376. info->fbops->fb_sync(info);
  1377. i810fb_blank(FB_BLANK_POWERDOWN, info);
  1378. agp_unbind_memory(par->i810_gtt.i810_fb_memory);
  1379. agp_unbind_memory(par->i810_gtt.i810_cursor_memory);
  1380. pci_save_state(dev);
  1381. pci_disable_device(dev);
  1382. pci_set_power_state(dev, pci_choose_state(dev, mesg));
  1383. console_unlock();
  1384. return 0;
  1385. }
  1386. static int i810fb_resume(struct pci_dev *dev)
  1387. {
  1388. struct fb_info *info = pci_get_drvdata(dev);
  1389. struct i810fb_par *par = info->par;
  1390. int cur_state = par->cur_state;
  1391. par->cur_state = PM_EVENT_ON;
  1392. if (cur_state == PM_EVENT_FREEZE) {
  1393. pci_set_power_state(dev, PCI_D0);
  1394. return 0;
  1395. }
  1396. console_lock();
  1397. pci_set_power_state(dev, PCI_D0);
  1398. pci_restore_state(dev);
  1399. if (pci_enable_device(dev))
  1400. goto fail;
  1401. pci_set_master(dev);
  1402. agp_bind_memory(par->i810_gtt.i810_fb_memory,
  1403. par->fb.offset);
  1404. agp_bind_memory(par->i810_gtt.i810_cursor_memory,
  1405. par->cursor_heap.offset);
  1406. i810fb_set_par(info);
  1407. fb_set_suspend (info, 0);
  1408. info->fbops->fb_blank(VESA_NO_BLANKING, info);
  1409. fail:
  1410. console_unlock();
  1411. return 0;
  1412. }
  1413. /***********************************************************************
  1414. * AGP resource allocation *
  1415. ***********************************************************************/
  1416. static void __devinit i810_fix_pointers(struct i810fb_par *par)
  1417. {
  1418. par->fb.physical = par->aperture.physical+(par->fb.offset << 12);
  1419. par->fb.virtual = par->aperture.virtual+(par->fb.offset << 12);
  1420. par->iring.physical = par->aperture.physical +
  1421. (par->iring.offset << 12);
  1422. par->iring.virtual = par->aperture.virtual +
  1423. (par->iring.offset << 12);
  1424. par->cursor_heap.virtual = par->aperture.virtual+
  1425. (par->cursor_heap.offset << 12);
  1426. }
  1427. static void __devinit i810_fix_offsets(struct i810fb_par *par)
  1428. {
  1429. if (vram + 1 > par->aperture.size >> 20)
  1430. vram = (par->aperture.size >> 20) - 1;
  1431. if (v_offset_default > (par->aperture.size >> 20))
  1432. v_offset_default = (par->aperture.size >> 20);
  1433. if (vram + v_offset_default + 1 > par->aperture.size >> 20)
  1434. v_offset_default = (par->aperture.size >> 20) - (vram + 1);
  1435. par->fb.size = vram << 20;
  1436. par->fb.offset = v_offset_default << 20;
  1437. par->fb.offset >>= 12;
  1438. par->iring.offset = par->fb.offset + (par->fb.size >> 12);
  1439. par->iring.size = RINGBUFFER_SIZE;
  1440. par->cursor_heap.offset = par->iring.offset + (RINGBUFFER_SIZE >> 12);
  1441. par->cursor_heap.size = 4096;
  1442. }
  1443. static int __devinit i810_alloc_agp_mem(struct fb_info *info)
  1444. {
  1445. struct i810fb_par *par = info->par;
  1446. int size;
  1447. struct agp_bridge_data *bridge;
  1448. i810_fix_offsets(par);
  1449. size = par->fb.size + par->iring.size;
  1450. if (!(bridge = agp_backend_acquire(par->dev))) {
  1451. printk("i810fb_alloc_fbmem: cannot acquire agpgart\n");
  1452. return -ENODEV;
  1453. }
  1454. if (!(par->i810_gtt.i810_fb_memory =
  1455. agp_allocate_memory(bridge, size >> 12, AGP_NORMAL_MEMORY))) {
  1456. printk("i810fb_alloc_fbmem: can't allocate framebuffer "
  1457. "memory\n");
  1458. agp_backend_release(bridge);
  1459. return -ENOMEM;
  1460. }
  1461. if (agp_bind_memory(par->i810_gtt.i810_fb_memory,
  1462. par->fb.offset)) {
  1463. printk("i810fb_alloc_fbmem: can't bind framebuffer memory\n");
  1464. agp_backend_release(bridge);
  1465. return -EBUSY;
  1466. }
  1467. if (!(par->i810_gtt.i810_cursor_memory =
  1468. agp_allocate_memory(bridge, par->cursor_heap.size >> 12,
  1469. AGP_PHYSICAL_MEMORY))) {
  1470. printk("i810fb_alloc_cursormem: can't allocate"
  1471. "cursor memory\n");
  1472. agp_backend_release(bridge);
  1473. return -ENOMEM;
  1474. }
  1475. if (agp_bind_memory(par->i810_gtt.i810_cursor_memory,
  1476. par->cursor_heap.offset)) {
  1477. printk("i810fb_alloc_cursormem: cannot bind cursor memory\n");
  1478. agp_backend_release(bridge);
  1479. return -EBUSY;
  1480. }
  1481. par->cursor_heap.physical = par->i810_gtt.i810_cursor_memory->physical;
  1482. i810_fix_pointers(par);
  1483. agp_backend_release(bridge);
  1484. return 0;
  1485. }
  1486. /***************************************************************
  1487. * Initialization *
  1488. ***************************************************************/
  1489. /**
  1490. * i810_init_monspecs
  1491. * @info: pointer to device specific info structure
  1492. *
  1493. * DESCRIPTION:
  1494. * Sets the user monitor's horizontal and vertical
  1495. * frequency limits
  1496. */
  1497. static void __devinit i810_init_monspecs(struct fb_info *info)
  1498. {
  1499. if (!hsync1)
  1500. hsync1 = HFMIN;
  1501. if (!hsync2)
  1502. hsync2 = HFMAX;
  1503. if (!info->monspecs.hfmax)
  1504. info->monspecs.hfmax = hsync2;
  1505. if (!info->monspecs.hfmin)
  1506. info->monspecs.hfmin = hsync1;
  1507. if (hsync2 < hsync1)
  1508. info->monspecs.hfmin = hsync2;
  1509. if (!vsync1)
  1510. vsync1 = VFMIN;
  1511. if (!vsync2)
  1512. vsync2 = VFMAX;
  1513. if (IS_DVT && vsync1 < 60)
  1514. vsync1 = 60;
  1515. if (!info->monspecs.vfmax)
  1516. info->monspecs.vfmax = vsync2;
  1517. if (!info->monspecs.vfmin)
  1518. info->monspecs.vfmin = vsync1;
  1519. if (vsync2 < vsync1)
  1520. info->monspecs.vfmin = vsync2;
  1521. }
  1522. /**
  1523. * i810_init_defaults - initializes default values to use
  1524. * @par: pointer to i810fb_par structure
  1525. * @info: pointer to current fb_info structure
  1526. */
  1527. static void __devinit i810_init_defaults(struct i810fb_par *par,
  1528. struct fb_info *info)
  1529. {
  1530. mutex_init(&par->open_lock);
  1531. if (voffset)
  1532. v_offset_default = voffset;
  1533. else if (par->aperture.size > 32 * 1024 * 1024)
  1534. v_offset_default = 16;
  1535. else
  1536. v_offset_default = 8;
  1537. if (!vram)
  1538. vram = 1;
  1539. if (accel)
  1540. par->dev_flags |= HAS_ACCELERATION;
  1541. if (sync)
  1542. par->dev_flags |= ALWAYS_SYNC;
  1543. par->ddc_num = ddc3;
  1544. if (bpp < 8)
  1545. bpp = 8;
  1546. par->i810fb_ops = i810fb_ops;
  1547. if (xres)
  1548. info->var.xres = xres;
  1549. else
  1550. info->var.xres = 640;
  1551. if (yres)
  1552. info->var.yres = yres;
  1553. else
  1554. info->var.yres = 480;
  1555. if (!vyres)
  1556. vyres = (vram << 20)/(info->var.xres*bpp >> 3);
  1557. info->var.yres_virtual = vyres;
  1558. info->var.bits_per_pixel = bpp;
  1559. if (dcolor)
  1560. info->var.nonstd = 1;
  1561. if (par->dev_flags & HAS_ACCELERATION)
  1562. info->var.accel_flags = 1;
  1563. i810_init_monspecs(info);
  1564. }
  1565. /**
  1566. * i810_init_device - initialize device
  1567. * @par: pointer to i810fb_par structure
  1568. */
  1569. static void __devinit i810_init_device(struct i810fb_par *par)
  1570. {
  1571. u8 reg;
  1572. u8 __iomem *mmio = par->mmio_start_virtual;
  1573. if (mtrr) set_mtrr(par);
  1574. i810_init_cursor(par);
  1575. /* mvo: enable external vga-connector (for laptops) */
  1576. if (extvga) {
  1577. i810_writel(HVSYNC, mmio, 0);
  1578. i810_writel(PWR_CLKC, mmio, 3);
  1579. }
  1580. pci_read_config_byte(par->dev, 0x50, &reg);
  1581. reg &= FREQ_MASK;
  1582. par->mem_freq = (reg) ? 133 : 100;
  1583. }
  1584. static int __devinit
  1585. i810_allocate_pci_resource(struct i810fb_par *par,
  1586. const struct pci_device_id *entry)
  1587. {
  1588. int err;
  1589. if ((err = pci_enable_device(par->dev))) {
  1590. printk("i810fb_init: cannot enable device\n");
  1591. return err;
  1592. }
  1593. par->res_flags |= PCI_DEVICE_ENABLED;
  1594. if (pci_resource_len(par->dev, 0) > 512 * 1024) {
  1595. par->aperture.physical = pci_resource_start(par->dev, 0);
  1596. par->aperture.size = pci_resource_len(par->dev, 0);
  1597. par->mmio_start_phys = pci_resource_start(par->dev, 1);
  1598. } else {
  1599. par->aperture.physical = pci_resource_start(par->dev, 1);
  1600. par->aperture.size = pci_resource_len(par->dev, 1);
  1601. par->mmio_start_phys = pci_resource_start(par->dev, 0);
  1602. }
  1603. if (!par->aperture.size) {
  1604. printk("i810fb_init: device is disabled\n");
  1605. return -ENOMEM;
  1606. }
  1607. if (!request_mem_region(par->aperture.physical,
  1608. par->aperture.size,
  1609. i810_pci_list[entry->driver_data])) {
  1610. printk("i810fb_init: cannot request fra