PageRenderTime 75ms CodeModel.GetById 31ms RepoModel.GetById 0ms app.codeStats 1ms

/drivers/video/aty/aty128fb.c

https://bitbucket.org/slukk/jb-tsm-kernel-4.2
C | 2570 lines | 1850 code | 425 blank | 295 comment | 232 complexity | 6bbd4576d8c18e46b678dda081a5a1d9 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.0, AGPL-1.0
  1. /* $Id: aty128fb.c,v 1.1.1.1.36.1 1999/12/11 09:03:05 Exp $
  2. * linux/drivers/video/aty128fb.c -- Frame buffer device for ATI Rage128
  3. *
  4. * Copyright (C) 1999-2003, Brad Douglas <brad@neruo.com>
  5. * Copyright (C) 1999, Anthony Tong <atong@uiuc.edu>
  6. *
  7. * Ani Joshi / Jeff Garzik
  8. * - Code cleanup
  9. *
  10. * Michel Danzer <michdaen@iiic.ethz.ch>
  11. * - 15/16 bit cleanup
  12. * - fix panning
  13. *
  14. * Benjamin Herrenschmidt
  15. * - pmac-specific PM stuff
  16. * - various fixes & cleanups
  17. *
  18. * Andreas Hundt <andi@convergence.de>
  19. * - FB_ACTIVATE fixes
  20. *
  21. * Paul Mackerras <paulus@samba.org>
  22. * - Convert to new framebuffer API,
  23. * fix colormap setting at 16 bits/pixel (565)
  24. *
  25. * Paul Mundt
  26. * - PCI hotplug
  27. *
  28. * Jon Smirl <jonsmirl@yahoo.com>
  29. * - PCI ID update
  30. * - replace ROM BIOS search
  31. *
  32. * Based off of Geert's atyfb.c and vfb.c.
  33. *
  34. * TODO:
  35. * - monitor sensing (DDC)
  36. * - virtual display
  37. * - other platform support (only ppc/x86 supported)
  38. * - hardware cursor support
  39. *
  40. * Please cc: your patches to brad@neruo.com.
  41. */
  42. /*
  43. * A special note of gratitude to ATI's devrel for providing documentation,
  44. * example code and hardware. Thanks Nitya. -atong and brad
  45. */
  46. #include <linux/module.h>
  47. #include <linux/moduleparam.h>
  48. #include <linux/kernel.h>
  49. #include <linux/errno.h>
  50. #include <linux/string.h>
  51. #include <linux/mm.h>
  52. #include <linux/vmalloc.h>
  53. #include <linux/delay.h>
  54. #include <linux/interrupt.h>
  55. #include <linux/uaccess.h>
  56. #include <linux/fb.h>
  57. #include <linux/init.h>
  58. #include <linux/pci.h>
  59. #include <linux/ioport.h>
  60. #include <linux/console.h>
  61. #include <linux/backlight.h>
  62. #include <asm/io.h>
  63. #ifdef CONFIG_PPC_PMAC
  64. #include <asm/machdep.h>
  65. #include <asm/pmac_feature.h>
  66. #include <asm/prom.h>
  67. #include <asm/pci-bridge.h>
  68. #include "../macmodes.h"
  69. #endif
  70. #ifdef CONFIG_PMAC_BACKLIGHT
  71. #include <asm/backlight.h>
  72. #endif
  73. #ifdef CONFIG_BOOTX_TEXT
  74. #include <asm/btext.h>
  75. #endif /* CONFIG_BOOTX_TEXT */
  76. #ifdef CONFIG_MTRR
  77. #include <asm/mtrr.h>
  78. #endif
  79. #include <video/aty128.h>
  80. /* Debug flag */
  81. #undef DEBUG
  82. #ifdef DEBUG
  83. #define DBG(fmt, args...) printk(KERN_DEBUG "aty128fb: %s " fmt, __func__, ##args);
  84. #else
  85. #define DBG(fmt, args...)
  86. #endif
  87. #ifndef CONFIG_PPC_PMAC
  88. /* default mode */
  89. static struct fb_var_screeninfo default_var __devinitdata = {
  90. /* 640x480, 60 Hz, Non-Interlaced (25.175 MHz dotclock) */
  91. 640, 480, 640, 480, 0, 0, 8, 0,
  92. {0, 8, 0}, {0, 8, 0}, {0, 8, 0}, {0, 0, 0},
  93. 0, 0, -1, -1, 0, 39722, 48, 16, 33, 10, 96, 2,
  94. 0, FB_VMODE_NONINTERLACED
  95. };
  96. #else /* CONFIG_PPC_PMAC */
  97. /* default to 1024x768 at 75Hz on PPC - this will work
  98. * on the iMac, the usual 640x480 @ 60Hz doesn't. */
  99. static struct fb_var_screeninfo default_var = {
  100. /* 1024x768, 75 Hz, Non-Interlaced (78.75 MHz dotclock) */
  101. 1024, 768, 1024, 768, 0, 0, 8, 0,
  102. {0, 8, 0}, {0, 8, 0}, {0, 8, 0}, {0, 0, 0},
  103. 0, 0, -1, -1, 0, 12699, 160, 32, 28, 1, 96, 3,
  104. FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
  105. FB_VMODE_NONINTERLACED
  106. };
  107. #endif /* CONFIG_PPC_PMAC */
  108. /* default modedb mode */
  109. /* 640x480, 60 Hz, Non-Interlaced (25.172 MHz dotclock) */
  110. static struct fb_videomode defaultmode __devinitdata = {
  111. .refresh = 60,
  112. .xres = 640,
  113. .yres = 480,
  114. .pixclock = 39722,
  115. .left_margin = 48,
  116. .right_margin = 16,
  117. .upper_margin = 33,
  118. .lower_margin = 10,
  119. .hsync_len = 96,
  120. .vsync_len = 2,
  121. .sync = 0,
  122. .vmode = FB_VMODE_NONINTERLACED
  123. };
  124. /* Chip generations */
  125. enum {
  126. rage_128,
  127. rage_128_pci,
  128. rage_128_pro,
  129. rage_128_pro_pci,
  130. rage_M3,
  131. rage_M3_pci,
  132. rage_M4,
  133. rage_128_ultra,
  134. };
  135. /* Must match above enum */
  136. static const char *r128_family[] __devinitdata = {
  137. "AGP",
  138. "PCI",
  139. "PRO AGP",
  140. "PRO PCI",
  141. "M3 AGP",
  142. "M3 PCI",
  143. "M4 AGP",
  144. "Ultra AGP",
  145. };
  146. /*
  147. * PCI driver prototypes
  148. */
  149. static int aty128_probe(struct pci_dev *pdev,
  150. const struct pci_device_id *ent);
  151. static void aty128_remove(struct pci_dev *pdev);
  152. static int aty128_pci_suspend(struct pci_dev *pdev, pm_message_t state);
  153. static int aty128_pci_resume(struct pci_dev *pdev);
  154. static int aty128_do_resume(struct pci_dev *pdev);
  155. /* supported Rage128 chipsets */
  156. static struct pci_device_id aty128_pci_tbl[] = {
  157. { PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_RAGE128_LE,
  158. PCI_ANY_ID, PCI_ANY_ID, 0, 0, rage_M3_pci },
  159. { PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_RAGE128_LF,
  160. PCI_ANY_ID, PCI_ANY_ID, 0, 0, rage_M3 },
  161. { PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_RAGE128_MF,
  162. PCI_ANY_ID, PCI_ANY_ID, 0, 0, rage_M4 },
  163. { PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_RAGE128_ML,
  164. PCI_ANY_ID, PCI_ANY_ID, 0, 0, rage_M4 },
  165. { PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_RAGE128_PA,
  166. PCI_ANY_ID, PCI_ANY_ID, 0, 0, rage_128_pro },
  167. { PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_RAGE128_PB,
  168. PCI_ANY_ID, PCI_ANY_ID, 0, 0, rage_128_pro },
  169. { PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_RAGE128_PC,
  170. PCI_ANY_ID, PCI_ANY_ID, 0, 0, rage_128_pro },
  171. { PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_RAGE128_PD,
  172. PCI_ANY_ID, PCI_ANY_ID, 0, 0, rage_128_pro_pci },
  173. { PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_RAGE128_PE,
  174. PCI_ANY_ID, PCI_ANY_ID, 0, 0, rage_128_pro },
  175. { PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_RAGE128_PF,
  176. PCI_ANY_ID, PCI_ANY_ID, 0, 0, rage_128_pro },
  177. { PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_RAGE128_PG,
  178. PCI_ANY_ID, PCI_ANY_ID, 0, 0, rage_128_pro },
  179. { PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_RAGE128_PH,
  180. PCI_ANY_ID, PCI_ANY_ID, 0, 0, rage_128_pro },
  181. { PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_RAGE128_PI,
  182. PCI_ANY_ID, PCI_ANY_ID, 0, 0, rage_128_pro },
  183. { PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_RAGE128_PJ,
  184. PCI_ANY_ID, PCI_ANY_ID, 0, 0, rage_128_pro },
  185. { PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_RAGE128_PK,
  186. PCI_ANY_ID, PCI_ANY_ID, 0, 0, rage_128_pro },
  187. { PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_RAGE128_PL,
  188. PCI_ANY_ID, PCI_ANY_ID, 0, 0, rage_128_pro },
  189. { PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_RAGE128_PM,
  190. PCI_ANY_ID, PCI_ANY_ID, 0, 0, rage_128_pro },
  191. { PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_RAGE128_PN,
  192. PCI_ANY_ID, PCI_ANY_ID, 0, 0, rage_128_pro },
  193. { PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_RAGE128_PO,
  194. PCI_ANY_ID, PCI_ANY_ID, 0, 0, rage_128_pro },
  195. { PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_RAGE128_PP,
  196. PCI_ANY_ID, PCI_ANY_ID, 0, 0, rage_128_pro_pci },
  197. { PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_RAGE128_PQ,
  198. PCI_ANY_ID, PCI_ANY_ID, 0, 0, rage_128_pro },
  199. { PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_RAGE128_PR,
  200. PCI_ANY_ID, PCI_ANY_ID, 0, 0, rage_128_pro_pci },
  201. { PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_RAGE128_PS,
  202. PCI_ANY_ID, PCI_ANY_ID, 0, 0, rage_128_pro },
  203. { PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_RAGE128_PT,
  204. PCI_ANY_ID, PCI_ANY_ID, 0, 0, rage_128_pro },
  205. { PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_RAGE128_PU,
  206. PCI_ANY_ID, PCI_ANY_ID, 0, 0, rage_128_pro },
  207. { PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_RAGE128_PV,
  208. PCI_ANY_ID, PCI_ANY_ID, 0, 0, rage_128_pro },
  209. { PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_RAGE128_PW,
  210. PCI_ANY_ID, PCI_ANY_ID, 0, 0, rage_128_pro },
  211. { PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_RAGE128_PX,
  212. PCI_ANY_ID, PCI_ANY_ID, 0, 0, rage_128_pro },
  213. { PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_RAGE128_RE,
  214. PCI_ANY_ID, PCI_ANY_ID, 0, 0, rage_128_pci },
  215. { PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_RAGE128_RF,
  216. PCI_ANY_ID, PCI_ANY_ID, 0, 0, rage_128 },
  217. { PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_RAGE128_RG,
  218. PCI_ANY_ID, PCI_ANY_ID, 0, 0, rage_128 },
  219. { PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_RAGE128_RK,
  220. PCI_ANY_ID, PCI_ANY_ID, 0, 0, rage_128_pci },
  221. { PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_RAGE128_RL,
  222. PCI_ANY_ID, PCI_ANY_ID, 0, 0, rage_128 },
  223. { PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_RAGE128_SE,
  224. PCI_ANY_ID, PCI_ANY_ID, 0, 0, rage_128 },
  225. { PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_RAGE128_SF,
  226. PCI_ANY_ID, PCI_ANY_ID, 0, 0, rage_128_pci },
  227. { PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_RAGE128_SG,
  228. PCI_ANY_ID, PCI_ANY_ID, 0, 0, rage_128 },
  229. { PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_RAGE128_SH,
  230. PCI_ANY_ID, PCI_ANY_ID, 0, 0, rage_128 },
  231. { PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_RAGE128_SK,
  232. PCI_ANY_ID, PCI_ANY_ID, 0, 0, rage_128 },
  233. { PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_RAGE128_SL,
  234. PCI_ANY_ID, PCI_ANY_ID, 0, 0, rage_128 },
  235. { PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_RAGE128_SM,
  236. PCI_ANY_ID, PCI_ANY_ID, 0, 0, rage_128 },
  237. { PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_RAGE128_SN,
  238. PCI_ANY_ID, PCI_ANY_ID, 0, 0, rage_128 },
  239. { PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_RAGE128_TF,
  240. PCI_ANY_ID, PCI_ANY_ID, 0, 0, rage_128_ultra },
  241. { PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_RAGE128_TL,
  242. PCI_ANY_ID, PCI_ANY_ID, 0, 0, rage_128_ultra },
  243. { PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_RAGE128_TR,
  244. PCI_ANY_ID, PCI_ANY_ID, 0, 0, rage_128_ultra },
  245. { PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_RAGE128_TS,
  246. PCI_ANY_ID, PCI_ANY_ID, 0, 0, rage_128_ultra },
  247. { PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_RAGE128_TT,
  248. PCI_ANY_ID, PCI_ANY_ID, 0, 0, rage_128_ultra },
  249. { PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_RAGE128_TU,
  250. PCI_ANY_ID, PCI_ANY_ID, 0, 0, rage_128_ultra },
  251. { 0, }
  252. };
  253. MODULE_DEVICE_TABLE(pci, aty128_pci_tbl);
  254. static struct pci_driver aty128fb_driver = {
  255. .name = "aty128fb",
  256. .id_table = aty128_pci_tbl,
  257. .probe = aty128_probe,
  258. .remove = __devexit_p(aty128_remove),
  259. .suspend = aty128_pci_suspend,
  260. .resume = aty128_pci_resume,
  261. };
  262. /* packed BIOS settings */
  263. #ifndef CONFIG_PPC
  264. typedef struct {
  265. u8 clock_chip_type;
  266. u8 struct_size;
  267. u8 accelerator_entry;
  268. u8 VGA_entry;
  269. u16 VGA_table_offset;
  270. u16 POST_table_offset;
  271. u16 XCLK;
  272. u16 MCLK;
  273. u8 num_PLL_blocks;
  274. u8 size_PLL_blocks;
  275. u16 PCLK_ref_freq;
  276. u16 PCLK_ref_divider;
  277. u32 PCLK_min_freq;
  278. u32 PCLK_max_freq;
  279. u16 MCLK_ref_freq;
  280. u16 MCLK_ref_divider;
  281. u32 MCLK_min_freq;
  282. u32 MCLK_max_freq;
  283. u16 XCLK_ref_freq;
  284. u16 XCLK_ref_divider;
  285. u32 XCLK_min_freq;
  286. u32 XCLK_max_freq;
  287. } __attribute__ ((packed)) PLL_BLOCK;
  288. #endif /* !CONFIG_PPC */
  289. /* onboard memory information */
  290. struct aty128_meminfo {
  291. u8 ML;
  292. u8 MB;
  293. u8 Trcd;
  294. u8 Trp;
  295. u8 Twr;
  296. u8 CL;
  297. u8 Tr2w;
  298. u8 LoopLatency;
  299. u8 DspOn;
  300. u8 Rloop;
  301. const char *name;
  302. };
  303. /* various memory configurations */
  304. static const struct aty128_meminfo sdr_128 =
  305. { 4, 4, 3, 3, 1, 3, 1, 16, 30, 16, "128-bit SDR SGRAM (1:1)" };
  306. static const struct aty128_meminfo sdr_64 =
  307. { 4, 8, 3, 3, 1, 3, 1, 17, 46, 17, "64-bit SDR SGRAM (1:1)" };
  308. static const struct aty128_meminfo sdr_sgram =
  309. { 4, 4, 1, 2, 1, 2, 1, 16, 24, 16, "64-bit SDR SGRAM (2:1)" };
  310. static const struct aty128_meminfo ddr_sgram =
  311. { 4, 4, 3, 3, 2, 3, 1, 16, 31, 16, "64-bit DDR SGRAM" };
  312. static struct fb_fix_screeninfo aty128fb_fix __devinitdata = {
  313. .id = "ATY Rage128",
  314. .type = FB_TYPE_PACKED_PIXELS,
  315. .visual = FB_VISUAL_PSEUDOCOLOR,
  316. .xpanstep = 8,
  317. .ypanstep = 1,
  318. .mmio_len = 0x2000,
  319. .accel = FB_ACCEL_ATI_RAGE128,
  320. };
  321. static char *mode_option __devinitdata = NULL;
  322. #ifdef CONFIG_PPC_PMAC
  323. static int default_vmode __devinitdata = VMODE_1024_768_60;
  324. static int default_cmode __devinitdata = CMODE_8;
  325. #endif
  326. static int default_crt_on __devinitdata = 0;
  327. static int default_lcd_on __devinitdata = 1;
  328. #ifdef CONFIG_MTRR
  329. static bool mtrr = true;
  330. #endif
  331. #ifdef CONFIG_PMAC_BACKLIGHT
  332. static int backlight __devinitdata = 1;
  333. #else
  334. static int backlight __devinitdata = 0;
  335. #endif
  336. /* PLL constants */
  337. struct aty128_constants {
  338. u32 ref_clk;
  339. u32 ppll_min;
  340. u32 ppll_max;
  341. u32 ref_divider;
  342. u32 xclk;
  343. u32 fifo_width;
  344. u32 fifo_depth;
  345. };
  346. struct aty128_crtc {
  347. u32 gen_cntl;
  348. u32 h_total, h_sync_strt_wid;
  349. u32 v_total, v_sync_strt_wid;
  350. u32 pitch;
  351. u32 offset, offset_cntl;
  352. u32 xoffset, yoffset;
  353. u32 vxres, vyres;
  354. u32 depth, bpp;
  355. };
  356. struct aty128_pll {
  357. u32 post_divider;
  358. u32 feedback_divider;
  359. u32 vclk;
  360. };
  361. struct aty128_ddafifo {
  362. u32 dda_config;
  363. u32 dda_on_off;
  364. };
  365. /* register values for a specific mode */
  366. struct aty128fb_par {
  367. struct aty128_crtc crtc;
  368. struct aty128_pll pll;
  369. struct aty128_ddafifo fifo_reg;
  370. u32 accel_flags;
  371. struct aty128_constants constants; /* PLL and others */
  372. void __iomem *regbase; /* remapped mmio */
  373. u32 vram_size; /* onboard video ram */
  374. int chip_gen;
  375. const struct aty128_meminfo *mem; /* onboard mem info */
  376. #ifdef CONFIG_MTRR
  377. struct { int vram; int vram_valid; } mtrr;
  378. #endif
  379. int blitter_may_be_busy;
  380. int fifo_slots; /* free slots in FIFO (64 max) */
  381. int pm_reg;
  382. int crt_on, lcd_on;
  383. struct pci_dev *pdev;
  384. struct fb_info *next;
  385. int asleep;
  386. int lock_blank;
  387. u8 red[32]; /* see aty128fb_setcolreg */
  388. u8 green[64];
  389. u8 blue[32];
  390. u32 pseudo_palette[16]; /* used for TRUECOLOR */
  391. };
  392. #define round_div(n, d) ((n+(d/2))/d)
  393. static int aty128fb_check_var(struct fb_var_screeninfo *var,
  394. struct fb_info *info);
  395. static int aty128fb_set_par(struct fb_info *info);
  396. static int aty128fb_setcolreg(u_int regno, u_int red, u_int green, u_int blue,
  397. u_int transp, struct fb_info *info);
  398. static int aty128fb_pan_display(struct fb_var_screeninfo *var,
  399. struct fb_info *fb);
  400. static int aty128fb_blank(int blank, struct fb_info *fb);
  401. static int aty128fb_ioctl(struct fb_info *info, u_int cmd, unsigned long arg);
  402. static int aty128fb_sync(struct fb_info *info);
  403. /*
  404. * Internal routines
  405. */
  406. static int aty128_encode_var(struct fb_var_screeninfo *var,
  407. const struct aty128fb_par *par);
  408. static int aty128_decode_var(struct fb_var_screeninfo *var,
  409. struct aty128fb_par *par);
  410. #if 0
  411. static void __devinit aty128_get_pllinfo(struct aty128fb_par *par,
  412. void __iomem *bios);
  413. static void __devinit __iomem *aty128_map_ROM(struct pci_dev *pdev, const struct aty128fb_par *par);
  414. #endif
  415. static void aty128_timings(struct aty128fb_par *par);
  416. static void aty128_init_engine(struct aty128fb_par *par);
  417. static void aty128_reset_engine(const struct aty128fb_par *par);
  418. static void aty128_flush_pixel_cache(const struct aty128fb_par *par);
  419. static void do_wait_for_fifo(u16 entries, struct aty128fb_par *par);
  420. static void wait_for_fifo(u16 entries, struct aty128fb_par *par);
  421. static void wait_for_idle(struct aty128fb_par *par);
  422. static u32 depth_to_dst(u32 depth);
  423. #ifdef CONFIG_FB_ATY128_BACKLIGHT
  424. static void aty128_bl_set_power(struct fb_info *info, int power);
  425. #endif
  426. #define BIOS_IN8(v) (readb(bios + (v)))
  427. #define BIOS_IN16(v) (readb(bios + (v)) | \
  428. (readb(bios + (v) + 1) << 8))
  429. #define BIOS_IN32(v) (readb(bios + (v)) | \
  430. (readb(bios + (v) + 1) << 8) | \
  431. (readb(bios + (v) + 2) << 16) | \
  432. (readb(bios + (v) + 3) << 24))
  433. static struct fb_ops aty128fb_ops = {
  434. .owner = THIS_MODULE,
  435. .fb_check_var = aty128fb_check_var,
  436. .fb_set_par = aty128fb_set_par,
  437. .fb_setcolreg = aty128fb_setcolreg,
  438. .fb_pan_display = aty128fb_pan_display,
  439. .fb_blank = aty128fb_blank,
  440. .fb_ioctl = aty128fb_ioctl,
  441. .fb_sync = aty128fb_sync,
  442. .fb_fillrect = cfb_fillrect,
  443. .fb_copyarea = cfb_copyarea,
  444. .fb_imageblit = cfb_imageblit,
  445. };
  446. /*
  447. * Functions to read from/write to the mmio registers
  448. * - endian conversions may possibly be avoided by
  449. * using the other register aperture. TODO.
  450. */
  451. static inline u32 _aty_ld_le32(volatile unsigned int regindex,
  452. const struct aty128fb_par *par)
  453. {
  454. return readl (par->regbase + regindex);
  455. }
  456. static inline void _aty_st_le32(volatile unsigned int regindex, u32 val,
  457. const struct aty128fb_par *par)
  458. {
  459. writel (val, par->regbase + regindex);
  460. }
  461. static inline u8 _aty_ld_8(unsigned int regindex,
  462. const struct aty128fb_par *par)
  463. {
  464. return readb (par->regbase + regindex);
  465. }
  466. static inline void _aty_st_8(unsigned int regindex, u8 val,
  467. const struct aty128fb_par *par)
  468. {
  469. writeb (val, par->regbase + regindex);
  470. }
  471. #define aty_ld_le32(regindex) _aty_ld_le32(regindex, par)
  472. #define aty_st_le32(regindex, val) _aty_st_le32(regindex, val, par)
  473. #define aty_ld_8(regindex) _aty_ld_8(regindex, par)
  474. #define aty_st_8(regindex, val) _aty_st_8(regindex, val, par)
  475. /*
  476. * Functions to read from/write to the pll registers
  477. */
  478. #define aty_ld_pll(pll_index) _aty_ld_pll(pll_index, par)
  479. #define aty_st_pll(pll_index, val) _aty_st_pll(pll_index, val, par)
  480. static u32 _aty_ld_pll(unsigned int pll_index,
  481. const struct aty128fb_par *par)
  482. {
  483. aty_st_8(CLOCK_CNTL_INDEX, pll_index & 0x3F);
  484. return aty_ld_le32(CLOCK_CNTL_DATA);
  485. }
  486. static void _aty_st_pll(unsigned int pll_index, u32 val,
  487. const struct aty128fb_par *par)
  488. {
  489. aty_st_8(CLOCK_CNTL_INDEX, (pll_index & 0x3F) | PLL_WR_EN);
  490. aty_st_le32(CLOCK_CNTL_DATA, val);
  491. }
  492. /* return true when the PLL has completed an atomic update */
  493. static int aty_pll_readupdate(const struct aty128fb_par *par)
  494. {
  495. return !(aty_ld_pll(PPLL_REF_DIV) & PPLL_ATOMIC_UPDATE_R);
  496. }
  497. static void aty_pll_wait_readupdate(const struct aty128fb_par *par)
  498. {
  499. unsigned long timeout = jiffies + HZ/100; // should be more than enough
  500. int reset = 1;
  501. while (time_before(jiffies, timeout))
  502. if (aty_pll_readupdate(par)) {
  503. reset = 0;
  504. break;
  505. }
  506. if (reset) /* reset engine?? */
  507. printk(KERN_DEBUG "aty128fb: PLL write timeout!\n");
  508. }
  509. /* tell PLL to update */
  510. static void aty_pll_writeupdate(const struct aty128fb_par *par)
  511. {
  512. aty_pll_wait_readupdate(par);
  513. aty_st_pll(PPLL_REF_DIV,
  514. aty_ld_pll(PPLL_REF_DIV) | PPLL_ATOMIC_UPDATE_W);
  515. }
  516. /* write to the scratch register to test r/w functionality */
  517. static int __devinit register_test(const struct aty128fb_par *par)
  518. {
  519. u32 val;
  520. int flag = 0;
  521. val = aty_ld_le32(BIOS_0_SCRATCH);
  522. aty_st_le32(BIOS_0_SCRATCH, 0x55555555);
  523. if (aty_ld_le32(BIOS_0_SCRATCH) == 0x55555555) {
  524. aty_st_le32(BIOS_0_SCRATCH, 0xAAAAAAAA);
  525. if (aty_ld_le32(BIOS_0_SCRATCH) == 0xAAAAAAAA)
  526. flag = 1;
  527. }
  528. aty_st_le32(BIOS_0_SCRATCH, val); // restore value
  529. return flag;
  530. }
  531. /*
  532. * Accelerator engine functions
  533. */
  534. static void do_wait_for_fifo(u16 entries, struct aty128fb_par *par)
  535. {
  536. int i;
  537. for (;;) {
  538. for (i = 0; i < 2000000; i++) {
  539. par->fifo_slots = aty_ld_le32(GUI_STAT) & 0x0fff;
  540. if (par->fifo_slots >= entries)
  541. return;
  542. }
  543. aty128_reset_engine(par);
  544. }
  545. }
  546. static void wait_for_idle(struct aty128fb_par *par)
  547. {
  548. int i;
  549. do_wait_for_fifo(64, par);
  550. for (;;) {
  551. for (i = 0; i < 2000000; i++) {
  552. if (!(aty_ld_le32(GUI_STAT) & (1 << 31))) {
  553. aty128_flush_pixel_cache(par);
  554. par->blitter_may_be_busy = 0;
  555. return;
  556. }
  557. }
  558. aty128_reset_engine(par);
  559. }
  560. }
  561. static void wait_for_fifo(u16 entries, struct aty128fb_par *par)
  562. {
  563. if (par->fifo_slots < entries)
  564. do_wait_for_fifo(64, par);
  565. par->fifo_slots -= entries;
  566. }
  567. static void aty128_flush_pixel_cache(const struct aty128fb_par *par)
  568. {
  569. int i;
  570. u32 tmp;
  571. tmp = aty_ld_le32(PC_NGUI_CTLSTAT);
  572. tmp &= ~(0x00ff);
  573. tmp |= 0x00ff;
  574. aty_st_le32(PC_NGUI_CTLSTAT, tmp);
  575. for (i = 0; i < 2000000; i++)
  576. if (!(aty_ld_le32(PC_NGUI_CTLSTAT) & PC_BUSY))
  577. break;
  578. }
  579. static void aty128_reset_engine(const struct aty128fb_par *par)
  580. {
  581. u32 gen_reset_cntl, clock_cntl_index, mclk_cntl;
  582. aty128_flush_pixel_cache(par);
  583. clock_cntl_index = aty_ld_le32(CLOCK_CNTL_INDEX);
  584. mclk_cntl = aty_ld_pll(MCLK_CNTL);
  585. aty_st_pll(MCLK_CNTL, mclk_cntl | 0x00030000);
  586. gen_reset_cntl = aty_ld_le32(GEN_RESET_CNTL);
  587. aty_st_le32(GEN_RESET_CNTL, gen_reset_cntl | SOFT_RESET_GUI);
  588. aty_ld_le32(GEN_RESET_CNTL);
  589. aty_st_le32(GEN_RESET_CNTL, gen_reset_cntl & ~(SOFT_RESET_GUI));
  590. aty_ld_le32(GEN_RESET_CNTL);
  591. aty_st_pll(MCLK_CNTL, mclk_cntl);
  592. aty_st_le32(CLOCK_CNTL_INDEX, clock_cntl_index);
  593. aty_st_le32(GEN_RESET_CNTL, gen_reset_cntl);
  594. /* use old pio mode */
  595. aty_st_le32(PM4_BUFFER_CNTL, PM4_BUFFER_CNTL_NONPM4);
  596. DBG("engine reset");
  597. }
  598. static void aty128_init_engine(struct aty128fb_par *par)
  599. {
  600. u32 pitch_value;
  601. wait_for_idle(par);
  602. /* 3D scaler not spoken here */
  603. wait_for_fifo(1, par);
  604. aty_st_le32(SCALE_3D_CNTL, 0x00000000);
  605. aty128_reset_engine(par);
  606. pitch_value = par->crtc.pitch;
  607. if (par->crtc.bpp == 24) {
  608. pitch_value = pitch_value * 3;
  609. }
  610. wait_for_fifo(4, par);
  611. /* setup engine offset registers */
  612. aty_st_le32(DEFAULT_OFFSET, 0x00000000);
  613. /* setup engine pitch registers */
  614. aty_st_le32(DEFAULT_PITCH, pitch_value);
  615. /* set the default scissor register to max dimensions */
  616. aty_st_le32(DEFAULT_SC_BOTTOM_RIGHT, (0x1FFF << 16) | 0x1FFF);
  617. /* set the drawing controls registers */
  618. aty_st_le32(DP_GUI_MASTER_CNTL,
  619. GMC_SRC_PITCH_OFFSET_DEFAULT |
  620. GMC_DST_PITCH_OFFSET_DEFAULT |
  621. GMC_SRC_CLIP_DEFAULT |
  622. GMC_DST_CLIP_DEFAULT |
  623. GMC_BRUSH_SOLIDCOLOR |
  624. (depth_to_dst(par->crtc.depth) << 8) |
  625. GMC_SRC_DSTCOLOR |
  626. GMC_BYTE_ORDER_MSB_TO_LSB |
  627. GMC_DP_CONVERSION_TEMP_6500 |
  628. ROP3_PATCOPY |
  629. GMC_DP_SRC_RECT |
  630. GMC_3D_FCN_EN_CLR |
  631. GMC_DST_CLR_CMP_FCN_CLEAR |
  632. GMC_AUX_CLIP_CLEAR |
  633. GMC_WRITE_MASK_SET);
  634. wait_for_fifo(8, par);
  635. /* clear the line drawing registers */
  636. aty_st_le32(DST_BRES_ERR, 0);
  637. aty_st_le32(DST_BRES_INC, 0);
  638. aty_st_le32(DST_BRES_DEC, 0);
  639. /* set brush color registers */
  640. aty_st_le32(DP_BRUSH_FRGD_CLR, 0xFFFFFFFF); /* white */
  641. aty_st_le32(DP_BRUSH_BKGD_CLR, 0x00000000); /* black */
  642. /* set source color registers */
  643. aty_st_le32(DP_SRC_FRGD_CLR, 0xFFFFFFFF); /* white */
  644. aty_st_le32(DP_SRC_BKGD_CLR, 0x00000000); /* black */
  645. /* default write mask */
  646. aty_st_le32(DP_WRITE_MASK, 0xFFFFFFFF);
  647. /* Wait for all the writes to be completed before returning */
  648. wait_for_idle(par);
  649. }
  650. /* convert depth values to their register representation */
  651. static u32 depth_to_dst(u32 depth)
  652. {
  653. if (depth <= 8)
  654. return DST_8BPP;
  655. else if (depth <= 15)
  656. return DST_15BPP;
  657. else if (depth == 16)
  658. return DST_16BPP;
  659. else if (depth <= 24)
  660. return DST_24BPP;
  661. else if (depth <= 32)
  662. return DST_32BPP;
  663. return -EINVAL;
  664. }
  665. /*
  666. * PLL informations retreival
  667. */
  668. #ifndef __sparc__
  669. static void __iomem * __devinit aty128_map_ROM(const struct aty128fb_par *par, struct pci_dev *dev)
  670. {
  671. u16 dptr;
  672. u8 rom_type;
  673. void __iomem *bios;
  674. size_t rom_size;
  675. /* Fix from ATI for problem with Rage128 hardware not leaving ROM enabled */
  676. unsigned int temp;
  677. temp = aty_ld_le32(RAGE128_MPP_TB_CONFIG);
  678. temp &= 0x00ffffffu;
  679. temp |= 0x04 << 24;
  680. aty_st_le32(RAGE128_MPP_TB_CONFIG, temp);
  681. temp = aty_ld_le32(RAGE128_MPP_TB_CONFIG);
  682. bios = pci_map_rom(dev, &rom_size);
  683. if (!bios) {
  684. printk(KERN_ERR "aty128fb: ROM failed to map\n");
  685. return NULL;
  686. }
  687. /* Very simple test to make sure it appeared */
  688. if (BIOS_IN16(0) != 0xaa55) {
  689. printk(KERN_DEBUG "aty128fb: Invalid ROM signature %x should "
  690. " be 0xaa55\n", BIOS_IN16(0));
  691. goto failed;
  692. }
  693. /* Look for the PCI data to check the ROM type */
  694. dptr = BIOS_IN16(0x18);
  695. /* Check the PCI data signature. If it's wrong, we still assume a normal x86 ROM
  696. * for now, until I've verified this works everywhere. The goal here is more
  697. * to phase out Open Firmware images.
  698. *
  699. * Currently, we only look at the first PCI data, we could iteratre and deal with
  700. * them all, and we should use fb_bios_start relative to start of image and not
  701. * relative start of ROM, but so far, I never found a dual-image ATI card
  702. *
  703. * typedef struct {
  704. * u32 signature; + 0x00
  705. * u16 vendor; + 0x04
  706. * u16 device; + 0x06
  707. * u16 reserved_1; + 0x08
  708. * u16 dlen; + 0x0a
  709. * u8 drevision; + 0x0c
  710. * u8 class_hi; + 0x0d
  711. * u16 class_lo; + 0x0e
  712. * u16 ilen; + 0x10
  713. * u16 irevision; + 0x12
  714. * u8 type; + 0x14
  715. * u8 indicator; + 0x15
  716. * u16 reserved_2; + 0x16
  717. * } pci_data_t;
  718. */
  719. if (BIOS_IN32(dptr) != (('R' << 24) | ('I' << 16) | ('C' << 8) | 'P')) {
  720. printk(KERN_WARNING "aty128fb: PCI DATA signature in ROM incorrect: %08x\n",
  721. BIOS_IN32(dptr));
  722. goto anyway;
  723. }
  724. rom_type = BIOS_IN8(dptr + 0x14);
  725. switch(rom_type) {
  726. case 0:
  727. printk(KERN_INFO "aty128fb: Found Intel x86 BIOS ROM Image\n");
  728. break;
  729. case 1:
  730. printk(KERN_INFO "aty128fb: Found Open Firmware ROM Image\n");
  731. goto failed;
  732. case 2:
  733. printk(KERN_INFO "aty128fb: Found HP PA-RISC ROM Image\n");
  734. goto failed;
  735. default:
  736. printk(KERN_INFO "aty128fb: Found unknown type %d ROM Image\n", rom_type);
  737. goto failed;
  738. }
  739. anyway:
  740. return bios;
  741. failed:
  742. pci_unmap_rom(dev, bios);
  743. return NULL;
  744. }
  745. static void __devinit aty128_get_pllinfo(struct aty128fb_par *par, unsigned char __iomem *bios)
  746. {
  747. unsigned int bios_hdr;
  748. unsigned int bios_pll;
  749. bios_hdr = BIOS_IN16(0x48);
  750. bios_pll = BIOS_IN16(bios_hdr + 0x30);
  751. par->constants.ppll_max = BIOS_IN32(bios_pll + 0x16);
  752. par->constants.ppll_min = BIOS_IN32(bios_pll + 0x12);
  753. par->constants.xclk = BIOS_IN16(bios_pll + 0x08);
  754. par->constants.ref_divider = BIOS_IN16(bios_pll + 0x10);
  755. par->constants.ref_clk = BIOS_IN16(bios_pll + 0x0e);
  756. DBG("ppll_max %d ppll_min %d xclk %d ref_divider %d ref clock %d\n",
  757. par->constants.ppll_max, par->constants.ppll_min,
  758. par->constants.xclk, par->constants.ref_divider,
  759. par->constants.ref_clk);
  760. }
  761. #ifdef CONFIG_X86
  762. static void __iomem * __devinit aty128_find_mem_vbios(struct aty128fb_par *par)
  763. {
  764. /* I simplified this code as we used to miss the signatures in
  765. * a lot of case. It's now closer to XFree, we just don't check
  766. * for signatures at all... Something better will have to be done
  767. * if we end up having conflicts
  768. */
  769. u32 segstart;
  770. unsigned char __iomem *rom_base = NULL;
  771. for (segstart=0x000c0000; segstart<0x000f0000; segstart+=0x00001000) {
  772. rom_base = ioremap(segstart, 0x10000);
  773. if (rom_base == NULL)
  774. return NULL;
  775. if (readb(rom_base) == 0x55 && readb(rom_base + 1) == 0xaa)
  776. break;
  777. iounmap(rom_base);
  778. rom_base = NULL;
  779. }
  780. return rom_base;
  781. }
  782. #endif
  783. #endif /* ndef(__sparc__) */
  784. /* fill in known card constants if pll_block is not available */
  785. static void __devinit aty128_timings(struct aty128fb_par *par)
  786. {
  787. #ifdef CONFIG_PPC_OF
  788. /* instead of a table lookup, assume OF has properly
  789. * setup the PLL registers and use their values
  790. * to set the XCLK values and reference divider values */
  791. u32 x_mpll_ref_fb_div;
  792. u32 xclk_cntl;
  793. u32 Nx, M;
  794. unsigned PostDivSet[] = { 0, 1, 2, 4, 8, 3, 6, 12 };
  795. #endif
  796. if (!par->constants.ref_clk)
  797. par->constants.ref_clk = 2950;
  798. #ifdef CONFIG_PPC_OF
  799. x_mpll_ref_fb_div = aty_ld_pll(X_MPLL_REF_FB_DIV);
  800. xclk_cntl = aty_ld_pll(XCLK_CNTL) & 0x7;
  801. Nx = (x_mpll_ref_fb_div & 0x00ff00) >> 8;
  802. M = x_mpll_ref_fb_div & 0x0000ff;
  803. par->constants.xclk = round_div((2 * Nx * par->constants.ref_clk),
  804. (M * PostDivSet[xclk_cntl]));
  805. par->constants.ref_divider =
  806. aty_ld_pll(PPLL_REF_DIV) & PPLL_REF_DIV_MASK;
  807. #endif
  808. if (!par->constants.ref_divider) {
  809. par->constants.ref_divider = 0x3b;
  810. aty_st_pll(X_MPLL_REF_FB_DIV, 0x004c4c1e);
  811. aty_pll_writeupdate(par);
  812. }
  813. aty_st_pll(PPLL_REF_DIV, par->constants.ref_divider);
  814. aty_pll_writeupdate(par);
  815. /* from documentation */
  816. if (!par->constants.ppll_min)
  817. par->constants.ppll_min = 12500;
  818. if (!par->constants.ppll_max)
  819. par->constants.ppll_max = 25000; /* 23000 on some cards? */
  820. if (!par->constants.xclk)
  821. par->constants.xclk = 0x1d4d; /* same as mclk */
  822. par->constants.fifo_width = 128;
  823. par->constants.fifo_depth = 32;
  824. switch (aty_ld_le32(MEM_CNTL) & 0x3) {
  825. case 0:
  826. par->mem = &sdr_128;
  827. break;
  828. case 1:
  829. par->mem = &sdr_sgram;
  830. break;
  831. case 2:
  832. par->mem = &ddr_sgram;
  833. break;
  834. default:
  835. par->mem = &sdr_sgram;
  836. }
  837. }
  838. /*
  839. * CRTC programming
  840. */
  841. /* Program the CRTC registers */
  842. static void aty128_set_crtc(const struct aty128_crtc *crtc,
  843. const struct aty128fb_par *par)
  844. {
  845. aty_st_le32(CRTC_GEN_CNTL, crtc->gen_cntl);
  846. aty_st_le32(CRTC_H_TOTAL_DISP, crtc->h_total);
  847. aty_st_le32(CRTC_H_SYNC_STRT_WID, crtc->h_sync_strt_wid);
  848. aty_st_le32(CRTC_V_TOTAL_DISP, crtc->v_total);
  849. aty_st_le32(CRTC_V_SYNC_STRT_WID, crtc->v_sync_strt_wid);
  850. aty_st_le32(CRTC_PITCH, crtc->pitch);
  851. aty_st_le32(CRTC_OFFSET, crtc->offset);
  852. aty_st_le32(CRTC_OFFSET_CNTL, crtc->offset_cntl);
  853. /* Disable ATOMIC updating. Is this the right place? */
  854. aty_st_pll(PPLL_CNTL, aty_ld_pll(PPLL_CNTL) & ~(0x00030000));
  855. }
  856. static int aty128_var_to_crtc(const struct fb_var_screeninfo *var,
  857. struct aty128_crtc *crtc,
  858. const struct aty128fb_par *par)
  859. {
  860. u32 xres, yres, vxres, vyres, xoffset, yoffset, bpp, dst;
  861. u32 left, right, upper, lower, hslen, vslen, sync, vmode;
  862. u32 h_total, h_disp, h_sync_strt, h_sync_wid, h_sync_pol;
  863. u32 v_total, v_disp, v_sync_strt, v_sync_wid, v_sync_pol, c_sync;
  864. u32 depth, bytpp;
  865. u8 mode_bytpp[7] = { 0, 0, 1, 2, 2, 3, 4 };
  866. /* input */
  867. xres = var->xres;
  868. yres = var->yres;
  869. vxres = var->xres_virtual;
  870. vyres = var->yres_virtual;
  871. xoffset = var->xoffset;
  872. yoffset = var->yoffset;
  873. bpp = var->bits_per_pixel;
  874. left = var->left_margin;
  875. right = var->right_margin;
  876. upper = var->upper_margin;
  877. lower = var->lower_margin;
  878. hslen = var->hsync_len;
  879. vslen = var->vsync_len;
  880. sync = var->sync;
  881. vmode = var->vmode;
  882. if (bpp != 16)
  883. depth = bpp;
  884. else
  885. depth = (var->green.length == 6) ? 16 : 15;
  886. /* check for mode eligibility
  887. * accept only non interlaced modes */
  888. if ((vmode & FB_VMODE_MASK) != FB_VMODE_NONINTERLACED)
  889. return -EINVAL;
  890. /* convert (and round up) and validate */
  891. xres = (xres + 7) & ~7;
  892. xoffset = (xoffset + 7) & ~7;
  893. if (vxres < xres + xoffset)
  894. vxres = xres + xoffset;
  895. if (vyres < yres + yoffset)
  896. vyres = yres + yoffset;
  897. /* convert depth into ATI register depth */
  898. dst = depth_to_dst(depth);
  899. if (dst == -EINVAL) {
  900. printk(KERN_ERR "aty128fb: Invalid depth or RGBA\n");
  901. return -EINVAL;
  902. }
  903. /* convert register depth to bytes per pixel */
  904. bytpp = mode_bytpp[dst];
  905. /* make sure there is enough video ram for the mode */
  906. if ((u32)(vxres * vyres * bytpp) > par->vram_size) {
  907. printk(KERN_ERR "aty128fb: Not enough memory for mode\n");
  908. return -EINVAL;
  909. }
  910. h_disp = (xres >> 3) - 1;
  911. h_total = (((xres + right + hslen + left) >> 3) - 1) & 0xFFFFL;
  912. v_disp = yres - 1;
  913. v_total = (yres + upper + vslen + lower - 1) & 0xFFFFL;
  914. /* check to make sure h_total and v_total are in range */
  915. if (((h_total >> 3) - 1) > 0x1ff || (v_total - 1) > 0x7FF) {
  916. printk(KERN_ERR "aty128fb: invalid width ranges\n");
  917. return -EINVAL;
  918. }
  919. h_sync_wid = (hslen + 7) >> 3;
  920. if (h_sync_wid == 0)
  921. h_sync_wid = 1;
  922. else if (h_sync_wid > 0x3f) /* 0x3f = max hwidth */
  923. h_sync_wid = 0x3f;
  924. h_sync_strt = (h_disp << 3) + right;
  925. v_sync_wid = vslen;
  926. if (v_sync_wid == 0)
  927. v_sync_wid = 1;
  928. else if (v_sync_wid > 0x1f) /* 0x1f = max vwidth */
  929. v_sync_wid = 0x1f;
  930. v_sync_strt = v_disp + lower;
  931. h_sync_pol = sync & FB_SYNC_HOR_HIGH_ACT ? 0 : 1;
  932. v_sync_pol = sync & FB_SYNC_VERT_HIGH_ACT ? 0 : 1;
  933. c_sync = sync & FB_SYNC_COMP_HIGH_ACT ? (1 << 4) : 0;
  934. crtc->gen_cntl = 0x3000000L | c_sync | (dst << 8);
  935. crtc->h_total = h_total | (h_disp << 16);
  936. crtc->v_total = v_total | (v_disp << 16);
  937. crtc->h_sync_strt_wid = h_sync_strt | (h_sync_wid << 16) |
  938. (h_sync_pol << 23);
  939. crtc->v_sync_strt_wid = v_sync_strt | (v_sync_wid << 16) |
  940. (v_sync_pol << 23);
  941. crtc->pitch = vxres >> 3;
  942. crtc->offset = 0;
  943. if ((var->activate & FB_ACTIVATE_MASK) == FB_ACTIVATE_NOW)
  944. crtc->offset_cntl = 0x00010000;
  945. else
  946. crtc->offset_cntl = 0;
  947. crtc->vxres = vxres;
  948. crtc->vyres = vyres;
  949. crtc->xoffset = xoffset;
  950. crtc->yoffset = yoffset;
  951. crtc->depth = depth;
  952. crtc->bpp = bpp;
  953. return 0;
  954. }
  955. static int aty128_pix_width_to_var(int pix_width, struct fb_var_screeninfo *var)
  956. {
  957. /* fill in pixel info */
  958. var->red.msb_right = 0;
  959. var->green.msb_right = 0;
  960. var->blue.offset = 0;
  961. var->blue.msb_right = 0;
  962. var->transp.offset = 0;
  963. var->transp.length = 0;
  964. var->transp.msb_right = 0;
  965. switch (pix_width) {
  966. case CRTC_PIX_WIDTH_8BPP:
  967. var->bits_per_pixel = 8;
  968. var->red.offset = 0;
  969. var->red.length = 8;
  970. var->green.offset = 0;
  971. var->green.length = 8;
  972. var->blue.length = 8;
  973. break;
  974. case CRTC_PIX_WIDTH_15BPP:
  975. var->bits_per_pixel = 16;
  976. var->red.offset = 10;
  977. var->red.length = 5;
  978. var->green.offset = 5;
  979. var->green.length = 5;
  980. var->blue.length = 5;
  981. break;
  982. case CRTC_PIX_WIDTH_16BPP:
  983. var->bits_per_pixel = 16;
  984. var->red.offset = 11;
  985. var->red.length = 5;
  986. var->green.offset = 5;
  987. var->green.length = 6;
  988. var->blue.length = 5;
  989. break;
  990. case CRTC_PIX_WIDTH_24BPP:
  991. var->bits_per_pixel = 24;
  992. var->red.offset = 16;
  993. var->red.length = 8;
  994. var->green.offset = 8;
  995. var->green.length = 8;
  996. var->blue.length = 8;
  997. break;
  998. case CRTC_PIX_WIDTH_32BPP:
  999. var->bits_per_pixel = 32;
  1000. var->red.offset = 16;
  1001. var->red.length = 8;
  1002. var->green.offset = 8;
  1003. var->green.length = 8;
  1004. var->blue.length = 8;
  1005. var->transp.offset = 24;
  1006. var->transp.length = 8;
  1007. break;
  1008. default:
  1009. printk(KERN_ERR "aty128fb: Invalid pixel width\n");
  1010. return -EINVAL;
  1011. }
  1012. return 0;
  1013. }
  1014. static int aty128_crtc_to_var(const struct aty128_crtc *crtc,
  1015. struct fb_var_screeninfo *var)
  1016. {
  1017. u32 xres, yres, left, right, upper, lower, hslen, vslen, sync;
  1018. u32 h_total, h_disp, h_sync_strt, h_sync_dly, h_sync_wid, h_sync_pol;
  1019. u32 v_total, v_disp, v_sync_strt, v_sync_wid, v_sync_pol, c_sync;
  1020. u32 pix_width;
  1021. /* fun with masking */
  1022. h_total = crtc->h_total & 0x1ff;
  1023. h_disp = (crtc->h_total >> 16) & 0xff;
  1024. h_sync_strt = (crtc->h_sync_strt_wid >> 3) & 0x1ff;
  1025. h_sync_dly = crtc->h_sync_strt_wid & 0x7;
  1026. h_sync_wid = (crtc->h_sync_strt_wid >> 16) & 0x3f;
  1027. h_sync_pol = (crtc->h_sync_strt_wid >> 23) & 0x1;
  1028. v_total = crtc->v_total & 0x7ff;
  1029. v_disp = (crtc->v_total >> 16) & 0x7ff;
  1030. v_sync_strt = crtc->v_sync_strt_wid & 0x7ff;
  1031. v_sync_wid = (crtc->v_sync_strt_wid >> 16) & 0x1f;
  1032. v_sync_pol = (crtc->v_sync_strt_wid >> 23) & 0x1;
  1033. c_sync = crtc->gen_cntl & CRTC_CSYNC_EN ? 1 : 0;
  1034. pix_width = crtc->gen_cntl & CRTC_PIX_WIDTH_MASK;
  1035. /* do conversions */
  1036. xres = (h_disp + 1) << 3;
  1037. yres = v_disp + 1;
  1038. left = ((h_total - h_sync_strt - h_sync_wid) << 3) - h_sync_dly;
  1039. right = ((h_sync_strt - h_disp) << 3) + h_sync_dly;
  1040. hslen = h_sync_wid << 3;
  1041. upper = v_total - v_sync_strt - v_sync_wid;
  1042. lower = v_sync_strt - v_disp;
  1043. vslen = v_sync_wid;
  1044. sync = (h_sync_pol ? 0 : FB_SYNC_HOR_HIGH_ACT) |
  1045. (v_sync_pol ? 0 : FB_SYNC_VERT_HIGH_ACT) |
  1046. (c_sync ? FB_SYNC_COMP_HIGH_ACT : 0);
  1047. aty128_pix_width_to_var(pix_width, var);
  1048. var->xres = xres;
  1049. var->yres = yres;
  1050. var->xres_virtual = crtc->vxres;
  1051. var->yres_virtual = crtc->vyres;
  1052. var->xoffset = crtc->xoffset;
  1053. var->yoffset = crtc->yoffset;
  1054. var->left_margin = left;
  1055. var->right_margin = right;
  1056. var->upper_margin = upper;
  1057. var->lower_margin = lower;
  1058. var->hsync_len = hslen;
  1059. var->vsync_len = vslen;
  1060. var->sync = sync;
  1061. var->vmode = FB_VMODE_NONINTERLACED;
  1062. return 0;
  1063. }
  1064. static void aty128_set_crt_enable(struct aty128fb_par *par, int on)
  1065. {
  1066. if (on) {
  1067. aty_st_le32(CRTC_EXT_CNTL, aty_ld_le32(CRTC_EXT_CNTL) | CRT_CRTC_ON);
  1068. aty_st_le32(DAC_CNTL, (aty_ld_le32(DAC_CNTL) | DAC_PALETTE2_SNOOP_EN));
  1069. } else
  1070. aty_st_le32(CRTC_EXT_CNTL, aty_ld_le32(CRTC_EXT_CNTL) & ~CRT_CRTC_ON);
  1071. }
  1072. static void aty128_set_lcd_enable(struct aty128fb_par *par, int on)
  1073. {
  1074. u32 reg;
  1075. #ifdef CONFIG_FB_ATY128_BACKLIGHT
  1076. struct fb_info *info = pci_get_drvdata(par->pdev);
  1077. #endif
  1078. if (on) {
  1079. reg = aty_ld_le32(LVDS_GEN_CNTL);
  1080. reg |= LVDS_ON | LVDS_EN | LVDS_BLON | LVDS_DIGION;
  1081. reg &= ~LVDS_DISPLAY_DIS;
  1082. aty_st_le32(LVDS_GEN_CNTL, reg);
  1083. #ifdef CONFIG_FB_ATY128_BACKLIGHT
  1084. aty128_bl_set_power(info, FB_BLANK_UNBLANK);
  1085. #endif
  1086. } else {
  1087. #ifdef CONFIG_FB_ATY128_BACKLIGHT
  1088. aty128_bl_set_power(info, FB_BLANK_POWERDOWN);
  1089. #endif
  1090. reg = aty_ld_le32(LVDS_GEN_CNTL);
  1091. reg |= LVDS_DISPLAY_DIS;
  1092. aty_st_le32(LVDS_GEN_CNTL, reg);
  1093. mdelay(100);
  1094. reg &= ~(LVDS_ON /*| LVDS_EN*/);
  1095. aty_st_le32(LVDS_GEN_CNTL, reg);
  1096. }
  1097. }
  1098. static void aty128_set_pll(struct aty128_pll *pll, const struct aty128fb_par *par)
  1099. {
  1100. u32 div3;
  1101. unsigned char post_conv[] = /* register values for post dividers */
  1102. { 2, 0, 1, 4, 2, 2, 6, 2, 3, 2, 2, 2, 7 };
  1103. /* select PPLL_DIV_3 */
  1104. aty_st_le32(CLOCK_CNTL_INDEX, aty_ld_le32(CLOCK_CNTL_INDEX) | (3 << 8));
  1105. /* reset PLL */
  1106. aty_st_pll(PPLL_CNTL,
  1107. aty_ld_pll(PPLL_CNTL) | PPLL_RESET | PPLL_ATOMIC_UPDATE_EN);
  1108. /* write the reference divider */
  1109. aty_pll_wait_readupdate(par);
  1110. aty_st_pll(PPLL_REF_DIV, par->constants.ref_divider & 0x3ff);
  1111. aty_pll_writeupdate(par);
  1112. div3 = aty_ld_pll(PPLL_DIV_3);
  1113. div3 &= ~PPLL_FB3_DIV_MASK;
  1114. div3 |= pll->feedback_divider;
  1115. div3 &= ~PPLL_POST3_DIV_MASK;
  1116. div3 |= post_conv[pll->post_divider] << 16;
  1117. /* write feedback and post dividers */
  1118. aty_pll_wait_readupdate(par);
  1119. aty_st_pll(PPLL_DIV_3, div3);
  1120. aty_pll_writeupdate(par);
  1121. aty_pll_wait_readupdate(par);
  1122. aty_st_pll(HTOTAL_CNTL, 0); /* no horiz crtc adjustment */
  1123. aty_pll_writeupdate(par);
  1124. /* clear the reset, just in case */
  1125. aty_st_pll(PPLL_CNTL, aty_ld_pll(PPLL_CNTL) & ~PPLL_RESET);
  1126. }
  1127. static int aty128_var_to_pll(u32 period_in_ps, struct aty128_pll *pll,
  1128. const struct aty128fb_par *par)
  1129. {
  1130. const struct aty128_constants c = par->constants;
  1131. unsigned char post_dividers[] = {1,2,4,8,3,6,12};
  1132. u32 output_freq;
  1133. u32 vclk; /* in .01 MHz */
  1134. int i = 0;
  1135. u32 n, d;
  1136. vclk = 100000000 / period_in_ps; /* convert units to 10 kHz */
  1137. /* adjust pixel clock if necessary */
  1138. if (vclk > c.ppll_max)
  1139. vclk = c.ppll_max;
  1140. if (vclk * 12 < c.ppll_min)
  1141. vclk = c.ppll_min/12;
  1142. /* now, find an acceptable divider */
  1143. for (i = 0; i < ARRAY_SIZE(post_dividers); i++) {
  1144. output_freq = post_dividers[i] * vclk;
  1145. if (output_freq >= c.ppll_min && output_freq <= c.ppll_max) {
  1146. pll->post_divider = post_dividers[i];
  1147. break;
  1148. }
  1149. }
  1150. if (i == ARRAY_SIZE(post_dividers))
  1151. return -EINVAL;
  1152. /* calculate feedback divider */
  1153. n = c.ref_divider * output_freq;
  1154. d = c.ref_clk;
  1155. pll->feedback_divider = round_div(n, d);
  1156. pll->vclk = vclk;
  1157. DBG("post %d feedback %d vlck %d output %d ref_divider %d "
  1158. "vclk_per: %d\n", pll->post_divider,
  1159. pll->feedback_divider, vclk, output_freq,
  1160. c.ref_divider, period_in_ps);
  1161. return 0;
  1162. }
  1163. static int aty128_pll_to_var(const struct aty128_pll *pll, struct fb_var_screeninfo *var)
  1164. {
  1165. var->pixclock = 100000000 / pll->vclk;
  1166. return 0;
  1167. }
  1168. static void aty128_set_fifo(const struct aty128_ddafifo *dsp,
  1169. const struct aty128fb_par *par)
  1170. {
  1171. aty_st_le32(DDA_CONFIG, dsp->dda_config);
  1172. aty_st_le32(DDA_ON_OFF, dsp->dda_on_off);
  1173. }
  1174. static int aty128_ddafifo(struct aty128_ddafifo *dsp,
  1175. const struct aty128_pll *pll,
  1176. u32 depth,
  1177. const struct aty128fb_par *par)
  1178. {
  1179. const struct aty128_meminfo *m = par->mem;
  1180. u32 xclk = par->constants.xclk;
  1181. u32 fifo_width = par->constants.fifo_width;
  1182. u32 fifo_depth = par->constants.fifo_depth;
  1183. s32 x, b, p, ron, roff;
  1184. u32 n, d, bpp;
  1185. /* round up to multiple of 8 */
  1186. bpp = (depth+7) & ~7;
  1187. n = xclk * fifo_width;
  1188. d = pll->vclk * bpp;
  1189. x = round_div(n, d);
  1190. ron = 4 * m->MB +
  1191. 3 * ((m->Trcd - 2 > 0) ? m->Trcd - 2 : 0) +
  1192. 2 * m->Trp +
  1193. m->Twr +
  1194. m->CL +
  1195. m->Tr2w +
  1196. x;
  1197. DBG("x %x\n", x);
  1198. b = 0;
  1199. while (x) {
  1200. x >>= 1;
  1201. b++;
  1202. }
  1203. p = b + 1;
  1204. ron <<= (11 - p);
  1205. n <<= (11 - p);
  1206. x = round_div(n, d);
  1207. roff = x * (fifo_depth - 4);
  1208. if ((ron + m->Rloop) >= roff) {
  1209. printk(KERN_ERR "aty128fb: Mode out of range!\n");
  1210. return -EINVAL;
  1211. }
  1212. DBG("p: %x rloop: %x x: %x ron: %x roff: %x\n",
  1213. p, m->Rloop, x, ron, roff);
  1214. dsp->dda_config = p << 16 | m->Rloop << 20 | x;
  1215. dsp->dda_on_off = ron << 16 | roff;
  1216. return 0;
  1217. }
  1218. /*
  1219. * This actually sets the video mode.
  1220. */
  1221. static int aty128fb_set_par(struct fb_info *info)
  1222. {
  1223. struct aty128fb_par *par = info->par;
  1224. u32 config;
  1225. int err;
  1226. if ((err = aty128_decode_var(&info->var, par)) != 0)
  1227. return err;
  1228. if (par->blitter_may_be_busy)
  1229. wait_for_idle(par);
  1230. /* clear all registers that may interfere with mode setting */
  1231. aty_st_le32(OVR_CLR, 0);
  1232. aty_st_le32(OVR_WID_LEFT_RIGHT, 0);
  1233. aty_st_le32(OVR_WID_TOP_BOTTOM, 0);
  1234. aty_st_le32(OV0_SCALE_CNTL, 0);
  1235. aty_st_le32(MPP_TB_CONFIG, 0);
  1236. aty_st_le32(MPP_GP_CONFIG, 0);
  1237. aty_st_le32(SUBPIC_CNTL, 0);
  1238. aty_st_le32(VIPH_CONTROL, 0);
  1239. aty_st_le32(I2C_CNTL_1, 0); /* turn off i2c */
  1240. aty_st_le32(GEN_INT_CNTL, 0); /* turn off interrupts */
  1241. aty_st_le32(CAP0_TRIG_CNTL, 0);
  1242. aty_st_le32(CAP1_TRIG_CNTL, 0);
  1243. aty_st_8(CRTC_EXT_CNTL + 1, 4); /* turn video off */
  1244. aty128_set_crtc(&par->crtc, par);
  1245. aty128_set_pll(&par->pll, par);
  1246. aty128_set_fifo(&par->fifo_reg, par);
  1247. config = aty_ld_le32(CNFG_CNTL) & ~3;
  1248. #if defined(__BIG_ENDIAN)
  1249. if (par->crtc.bpp == 32)
  1250. config |= 2; /* make aperture do 32 bit swapping */
  1251. else if (par->crtc.bpp == 16)
  1252. config |= 1; /* make aperture do 16 bit swapping */
  1253. #endif
  1254. aty_st_le32(CNFG_CNTL, config);
  1255. aty_st_8(CRTC_EXT_CNTL + 1, 0); /* turn the video back on */
  1256. info->fix.line_length = (par->crtc.vxres * par->crtc.bpp) >> 3;
  1257. info->fix.visual = par->crtc.bpp == 8 ? FB_VISUAL_PSEUDOCOLOR
  1258. : FB_VISUAL_DIRECTCOLOR;
  1259. if (par->chip_gen == rage_M3) {
  1260. aty128_set_crt_enable(par, par->crt_on);
  1261. aty128_set_lcd_enable(par, par->lcd_on);
  1262. }
  1263. if (par->accel_flags & FB_ACCELF_TEXT)
  1264. aty128_init_engine(par);
  1265. #ifdef CONFIG_BOOTX_TEXT
  1266. btext_update_display(info->fix.smem_start,
  1267. (((par->crtc.h_total>>16) & 0xff)+1)*8,
  1268. ((par->crtc.v_total>>16) & 0x7ff)+1,
  1269. par->crtc.bpp,
  1270. par->crtc.vxres*par->crtc.bpp/8);
  1271. #endif /* CONFIG_BOOTX_TEXT */
  1272. return 0;
  1273. }
  1274. /*
  1275. * encode/decode the User Defined Part of the Display
  1276. */
  1277. static int aty128_decode_var(struct fb_var_screeninfo *var, struct aty128fb_par *par)
  1278. {
  1279. int err;
  1280. struct aty128_crtc crtc;
  1281. struct aty128_pll pll;
  1282. struct aty128_ddafifo fifo_reg;
  1283. if ((err = aty128_var_to_crtc(var, &crtc, par)))
  1284. return err;
  1285. if ((err = aty128_var_to_pll(var->pixclock, &pll, par)))
  1286. return err;
  1287. if ((err = aty128_ddafifo(&fifo_reg, &pll, crtc.depth, par)))
  1288. return err;
  1289. par->crtc = crtc;
  1290. par->pll = pll;
  1291. par->fifo_reg = fifo_reg;
  1292. par->accel_flags = var->accel_flags;
  1293. return 0;
  1294. }
  1295. static int aty128_encode_var(struct fb_var_screeninfo *var,
  1296. const struct aty128fb_par *par)
  1297. {
  1298. int err;
  1299. if ((err = aty128_crtc_to_var(&par->crtc, var)))
  1300. return err;
  1301. if ((err = aty128_pll_to_var(&par->pll, var)))
  1302. return err;
  1303. var->nonstd = 0;
  1304. var->activate = 0;
  1305. var->height = -1;
  1306. var->width = -1;
  1307. var->accel_flags = par->accel_flags;
  1308. return 0;
  1309. }
  1310. static int aty128fb_check_var(struct fb_var_screeninfo *var, struct fb_info *info)
  1311. {
  1312. struct aty128fb_par par;
  1313. int err;
  1314. par = *(struct aty128fb_par *)info->par;
  1315. if ((err = aty128_decode_var(var, &par)) != 0)
  1316. return err;
  1317. aty128_encode_var(var, &par);
  1318. return 0;
  1319. }
  1320. /*
  1321. * Pan or Wrap the Display
  1322. */
  1323. static int aty128fb_pan_display(struct fb_var_screeninfo *var, struct fb_info *fb)
  1324. {
  1325. struct aty128fb_par *par = fb->par;
  1326. u32 xoffset, yoffset;
  1327. u32 offset;
  1328. u32 xres, yres;
  1329. xres = (((par->crtc.h_total >> 16) & 0xff) + 1) << 3;
  1330. yres = ((par->crtc.v_total >> 16) & 0x7ff) + 1;
  1331. xoffset = (var->xoffset +7) & ~7;
  1332. yoffset = var->yoffset;
  1333. if (xoffset+xres > par->crtc.vxres || yoffset+yres > par->crtc.vyres)
  1334. return -EINVAL;
  1335. par->crtc.xoffset = xoffset;
  1336. par->crtc.yoffset = yoffset;
  1337. offset = ((yoffset * par->crtc.vxres + xoffset)*(par->crtc.bpp >> 3)) & ~7;
  1338. if (par->crtc.bpp == 24)
  1339. offset += 8 * (offset % 3); /* Must be multiple of 8 and 3 */
  1340. aty_st_le32(CRTC_OFFSET, offset);
  1341. return 0;
  1342. }
  1343. /*
  1344. * Helper function to store a single palette register
  1345. */
  1346. static void aty128_st_pal(u_int regno, u_int red, u_int green, u_int blue,
  1347. struct aty128fb_par *par)
  1348. {
  1349. if (par->chip_gen == rage_M3) {
  1350. #if 0
  1351. /* Note: For now, on M3, we set palette on both heads, which may
  1352. * be useless. Can someone with a M3 check this ?
  1353. *
  1354. * This code would still be useful if using the second CRTC to
  1355. * do mirroring
  1356. */
  1357. aty_st_le32(DAC_CNTL, aty_ld_le32(DAC_CNTL) | DAC_PALETTE_ACCESS_CNTL);
  1358. aty_st_8(PALETTE_INDEX, regno);
  1359. aty_st_le32(PALETTE_DATA, (red<<16)|(green<<8)|blue);
  1360. #endif
  1361. aty_st_le32(DAC_CNTL, aty_ld_le32(DAC_CNTL) & ~DAC_PALETTE_ACCESS_CNTL);
  1362. }
  1363. aty_st_8(PALETTE_INDEX, regno);
  1364. aty_st_le32(PALETTE_DATA, (red<<16)|(green<<8)|blue);
  1365. }
  1366. static int aty128fb_sync(struct fb_info *info)
  1367. {
  1368. struct aty128fb_par *par = info->par;
  1369. if (par->blitter_may_be_busy)
  1370. wait_for_idle(par);
  1371. return 0;
  1372. }
  1373. #ifndef MODULE
  1374. static int __devinit aty128fb_setup(char *options)
  1375. {
  1376. char *this_opt;
  1377. if (!options || !*options)
  1378. return 0;
  1379. while ((this_opt = strsep(&options, ",")) != NULL) {
  1380. if (!strncmp(this_opt, "lcd:", 4)) {
  1381. default_lcd_on = simple_strtoul(this_opt+4, NULL, 0);
  1382. continue;
  1383. } else if (!strncmp(this_opt, "crt:", 4)) {
  1384. default_crt_on = simple_strtoul(this_opt+4, NULL, 0);
  1385. continue;
  1386. } else if (!strncmp(this_opt, "backlight:", 10)) {
  1387. backlight = simple_strtoul(this_opt+10, NULL, 0);
  1388. continue;
  1389. }
  1390. #ifdef CONFIG_MTRR
  1391. if(!strncmp(this_opt, "nomtrr", 6)) {
  1392. mtrr = 0;
  1393. continue;
  1394. }
  1395. #endif
  1396. #ifdef CONFIG_PPC_PMAC
  1397. /* vmode and cmode deprecated */
  1398. if (!strncmp(this_opt, "vmode:", 6)) {
  1399. unsigned int vmode = simple_strtoul(this_opt+6, NULL, 0);
  1400. if (vmode > 0 && vmode <= VMODE_MAX)
  1401. default_vmode = vmode;
  1402. continue;
  1403. } else if (!strncmp(this_opt, "cmode:", 6)) {
  1404. unsigned int cmode = simple_strtoul(this_opt+6, NULL, 0);
  1405. switch (cmode) {
  1406. case 0:
  1407. case 8:
  1408. default_cmode = CMODE_8;
  1409. break;
  1410. case 15:
  1411. case 16:
  1412. default_cmode = CMODE_16;
  1413. break;
  1414. case 24:
  1415. case 32:
  1416. default_cmode = CMODE_32;
  1417. break;
  1418. }
  1419. continue;
  1420. }
  1421. #endif /* CONFIG_PPC_PMAC */
  1422. mode_option = this_opt;
  1423. }
  1424. return 0;
  1425. }
  1426. #endif /* MODULE */
  1427. /* Backlight */
  1428. #ifdef CONFIG_FB_ATY128_BACKLIGHT
  1429. #define MAX_LEVEL 0xFF
  1430. static int aty128_bl_get_level_brightness(struct aty128fb_par *par,
  1431. int level)
  1432. {
  1433. struct fb_info *info = pci_get_drvdata(par->pdev);
  1434. int atylevel;
  1435. /* Get and convert the value */
  1436. /* No locking of bl_curve since we read a single value */
  1437. atylevel = MAX_LEVEL -
  1438. (info->bl_curve[level] * FB_BACKLIGHT_MAX / MAX_LEVEL);
  1439. if (atylevel < 0)
  1440. atylevel = 0;
  1441. else if (atylevel > MAX_LEVEL)
  1442. atylevel = MAX_LEVEL;
  1443. return atylevel;
  1444. }
  1445. /* We turn off the LCD completely instead of just dimming the backlight.
  1446. * This provides greater power saving and the display is useless without
  1447. * backlight anyway
  1448. */
  1449. #define BACKLIGHT_LVDS_OFF
  1450. /* That one prevents proper CRT output with LCD off */
  1451. #undef BACKLIGHT_DAC_OFF
  1452. static int aty128_bl_update_status(struct backlight_device *bd)
  1453. {
  1454. struct aty128fb_par *par = bl_get_data(bd);
  1455. unsigned int reg = aty_ld_le32(LVDS_GEN_CNTL);
  1456. int level;
  1457. if (bd->props.power != FB_BLANK_UNBLANK ||
  1458. bd->props.fb_blank != FB_BLANK_UNBLANK ||
  1459. !par->lcd_on)
  1460. level = 0;
  1461. else
  1462. level = bd->props.brightness;
  1463. reg |= LVDS_BL_MOD_EN | LVDS_BLON;
  1464. if (level > 0) {
  1465. reg |= LVDS_DIGION;
  1466. if (!(reg & LVDS_ON)) {
  1467. reg &= ~LVDS_BLON;
  1468. aty_st_le32(LVDS_GEN_CNTL, reg);
  1469. aty_ld_le32(LVDS_GEN_CNTL);
  1470. mdelay(10);
  1471. reg |= LVDS_BLON;
  1472. aty_st_le32(LVDS_GEN_CNTL, reg);
  1473. }
  1474. reg &= ~LVDS_BL_MOD_LEVEL_MASK;
  1475. reg |= (aty128_bl_get_level_brightness(par, level) << LVDS_BL_MOD_LEVEL_SHIFT);
  1476. #ifdef BACKLIGHT_LVDS_OFF
  1477. reg |= LVDS_ON | LVDS_EN;
  1478. reg &= ~LVDS_DISPLAY_DIS;
  1479. #endif
  1480. aty_st_le32(LVDS_GEN_CNTL, reg);
  1481. #ifdef BACKLIGHT_DAC_OFF
  1482. aty_st_le32(DAC_CNTL, aty_ld_le32(DAC_CNTL) & (~DAC_PDWN));
  1483. #endif
  1484. } else {
  1485. reg &= ~LVDS_BL_MOD_LEVEL_MASK;
  1486. reg |= (aty128_bl_get_level_brightness(par, 0) << LVDS_BL_MOD_LEVEL_SHIFT);
  1487. #ifdef BACKLIGHT_LVDS_OFF
  1488. reg |= LVDS_DISPLAY_DIS;
  1489. aty_st_le32(LVDS_GEN_CNTL, reg);
  1490. aty_ld_le32(LVDS_GEN_CNTL);
  1491. udelay(10);
  1492. reg &= ~(LVDS_ON | LVDS_EN | LVDS_BLON | LVDS_DIGION);
  1493. #endif
  1494. aty_st_le32(LVDS_GEN_CNTL, reg);
  1495. #ifdef BACKLIGHT_DAC_OFF
  1496. aty_st_le32(DAC_CNTL, aty_ld_le32(DAC_CNTL) | DAC_PDWN);
  1497. #endif
  1498. }
  1499. return 0;
  1500. }
  1501. static int aty128_bl_get_brightness(struct backlight_device *bd)
  1502. {
  1503. return bd->props.brightness;
  1504. }
  1505. static const struct backlight_ops aty128_bl_data = {
  1506. .get_brightness = aty128_bl_get_brightness,
  1507. .update_status = aty128_bl_update_status,
  1508. };
  1509. static void aty128_bl_set_power(struct fb_info *info, int power)
  1510. {
  1511. if (info->bl_dev) {
  1512. info->bl_dev->props.power = power;
  1513. backlight_update_status(info->bl_dev);
  1514. }
  1515. }
  1516. static void aty128_bl_init(struct aty128fb_par *par)
  1517. {
  1518. struct backlight_properties props;
  1519. struct fb_info *info = pci_get_drvdata(par->pdev);
  1520. struct backlight_device *bd;
  1521. char name[12];
  1522. /* Could be extended to Rage128Pro LVDS output too */
  1523. if (par->chip_gen != rage_M3)
  1524. return;
  1525. #ifdef CONFIG_PMAC_BACKLIGHT
  1526. if (!pmac_has_backlight_type("ati"))
  1527. return;
  1528. #endif
  1529. snprintf(name, sizeof(name), "aty128bl%d", info->node);
  1530. memset(&props, 0, sizeof(struct backlight_properties));
  1531. props.type = BACKLIGHT_RAW;
  1532. props.max_brightness = FB_BACKLIGHT_LEVELS - 1;
  1533. bd = backlight_device_register(name, info->dev, par, &aty128_bl_data,
  1534. &props);
  1535. if (IS_ERR(bd)) {
  1536. info->bl_dev = NULL;
  1537. printk(KERN_WARNING "aty128: Backlight registration failed\n");
  1538. goto error;
  1539. }
  1540. info->bl_dev = bd;
  1541. fb_bl_default_curve(info, 0,
  1542. 63 * FB_BACKLIGHT_MAX / MAX_LEVEL,
  1543. 219 * FB_BACKLIGHT_MAX / MAX_LEVEL);
  1544. bd->props.brightness = bd->props.max_brightness;
  1545. bd->props.power = FB_BLANK_UNBLANK;
  1546. backlight_update_status(bd);
  1547. printk("aty128: Backlight initialized (%s)\n", name);
  1548. return;
  1549. error:
  1550. return;
  1551. }
  1552. static void aty128_bl_exit(struct backlight_device *bd)
  1553. {
  1554. backlight_device_unregister(bd);
  1555. printk("aty128: Backlight unloaded\n");
  1556. }
  1557. #endif /* CONFIG_FB_ATY128_BACKLIGHT */
  1558. /*
  1559. * Initialisation
  1560. */
  1561. #ifdef CONFIG_PPC_PMAC__disabled
  1562. static void aty128_early_resume(void *data)
  1563. {
  1564. struct aty128fb_par *par = data;
  1565. if (!console_trylock())
  1566. return;
  1567. pci_restore_state(par->pdev);
  1568. aty128_do_resume(par->pdev);
  1569. console_unlock();
  1570. }
  1571. #endif /* CONFIG_PPC_PMAC */
  1572. static int __devinit aty128_init(struct pci_dev *pdev, const struct pci_device_id *ent)
  1573. {
  1574. struct fb_info *info = pci_get_drvdata(pdev);
  1575. struct aty128fb_par *par = info->par;
  1576. struct fb_var_screeninfo var;
  1577. char video_card[50];
  1578. u8 chip_rev;
  1579. u32 dac;
  1580. /* Get the chip revision */
  1581. chip_rev = (aty_ld_le32(CNFG_CNTL) >> 16) & 0x1F;
  1582. strcpy(video_card, "Rage128 XX ");
  1583. video_card[8] = ent->device >> 8;
  1584. video_card[9] = ent->device & 0xFF;
  1585. /* range check to make sure */
  1586. if (ent->driver_data < ARRAY_SIZE(r128_family))
  1587. strlcat(video_card, r128_family[ent->driver_data], sizeof(video_card));
  1588. printk(KERN_INFO "aty128fb: %s [chip rev 0x%x] ", video_card, chip_rev);
  1589. if (par->vram_size % (1024 * 1024) == 0)
  1590. printk("%dM %s\n", par->vram_size / (1024*1024), par->mem->name);
  1591. else
  1592. printk("%dk %s\n", par->vram_size / 1024, par->mem->name);
  1593. par->chip_gen = ent->driver_data;
  1594. /* fill in info */
  1595. info->fbops = &aty128fb_ops;
  1596. info->flags = FBINFO_FLAG_DEFAULT;
  1597. par->lcd_on = default_lcd_on;
  1598. par->crt_on = default_crt_on;
  1599. var = default_var;
  1600. #ifdef CONFIG_PPC_PMAC
  1601. if (machine_is(powermac)) {
  1602. /* Indicate sleep capability */
  1603. if (par->chip_gen == rage_M3) {
  1604. pmac_call_feature(PMAC_FTR_DEVICE_CAN_WAKE, NULL, 0, 1);
  1605. #if 0 /* Disable the early video resume hack for now as it's causing problems, among
  1606. * others we now rely on the PCI core restoring the config space for us, which
  1607. * isn't the case with that hack, and that code path causes various things to
  1608. * be called with interrupts off while they shouldn't. I'm leaving the code in
  1609. * as it can be useful for debugging purposes
  1610. */
  1611. pmac_set_early_video_resume(aty128_early_resume, par);
  1612. #endif
  1613. }
  1614. /* Find default mode */
  1615. if (mode_option) {
  1616. if (!mac_find_mode(&var, info, mode_option, 8))
  1617. var = default_var;
  1618. } else {
  1619. if (default_vmode <= 0 || default_vmode > VMODE_MAX)
  1620. default_vmode = VMODE_1024_768_60;
  1621. /* iMacs need that resolution
  1622. * PowerMac2,1 first r128 iMacs
  1623. * PowerMac2,2 summer 2000 iMacs
  1624. * PowerMac4,1 january 2001 iMacs "flower power"
  1625. */
  1626. if (of_machine_is_compatible("PowerMac2,1") ||
  1627. of_machine_is_compatible("PowerMac2,2") ||
  1628. of_machine_is_compatible("PowerMac4,1"))
  1629. default_vmode = VMODE_1024_768_75;
  1630. /* iBook SE */
  1631. if (of_machine_is_compatible("PowerBook2,2"))
  1632. default_vmode = VMODE_800_600_60;
  1633. /* PowerBook Firewire (Pismo), iBook Dual USB */
  1634. if (of_machine_is_compatible("PowerBook3,1") ||
  1635. of_machine_is_compatible("PowerBook4,1"))
  1636. default_vmode = VMODE_1024_768_60;
  1637. /* PowerBook Titanium */
  1638. if (of_machine_is_compatible("PowerBook3,2"))
  1639. default_vmode = VMODE_1152_768_60;
  1640. if (default_cmode > 16)
  1641. default_cmode = CMODE_32;
  1642. else if (default_cmode > 8)
  1643. default_cmode = CMODE_16;
  1644. else
  1645. default_cmode = CMODE_8;
  1646. if (mac_vmode_to_var(default_vmode, default_cmode, &var))
  1647. var = default_var;
  1648. }
  1649. } else
  1650. #endif /* CONFIG_PPC_PMAC */
  1651. {
  1652. if (mode_option)
  1653. if (fb_find_mode(&var, info, mode_option, NULL,
  1654. 0, &defaultmode, 8) == 0)
  1655. var = default_var;
  1656. }
  1657. var.accel_flags &= ~FB_ACCELF_TEXT;
  1658. // var.accel_flags |= FB_ACCELF_TEXT;/* FIXME Will add accel later */
  1659. if (aty128fb_check_var(&var, info)) {
  1660. printk(KERN_ERR "aty128fb: Cannot set default mode.\n");
  1661. return 0;
  1662. }
  1663. /* setup the DAC the way we like it */
  1664. dac = aty_ld_le32(DAC_CNTL);
  1665. dac |= (DAC_8BIT_EN | DAC_RANGE_CNTL);
  1666. dac |= DAC_MASK;
  1667. if (par->chip_gen == rage_M3)
  1668. dac |= DAC_PALETTE2_SNOOP_EN;
  1669. aty_st_le32(DAC_CNTL, dac);
  1670. /* turn off bus mastering, just in case */
  1671. aty_st_le32(BUS_CNTL, aty_ld_le32(BUS_CNTL) | BUS_MASTER_DIS);
  1672. info->var = var;
  1673. fb_alloc_cmap(&info->cmap, 256, 0);
  1674. var.activate = FB_ACTIVATE_NOW;
  1675. aty128_init_engine(par);
  1676. par->pm_reg = pci_find_capability(pdev, PCI_CAP_ID_PM);
  1677. par->pdev = pdev;
  1678. par->asleep = 0;
  1679. par->lock_blank = 0;
  1680. #ifdef CONFIG_FB_ATY128_BACKLIGHT
  1681. if (backlight)
  1682. aty128_bl_init(par);
  1683. #endif
  1684. if (register_framebuffer(info) < 0)
  1685. return 0;
  1686. printk(KERN_INFO "fb%d: %s frame buffer device on %s\n",
  1687. info->node, info->fix.id, video_card);
  1688. return 1; /* success! */
  1689. }
  1690. #ifdef CONFIG_PCI
  1691. /* register a card ++ajoshi */
  1692. static int __devinit aty128_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
  1693. {
  1694. unsigned long fb_addr, reg_addr;
  1695. struct aty128fb_par *par;
  1696. struct fb_info *info;
  1697. int err;
  1698. #ifndef __sparc__
  1699. void __iomem *bios = NULL;
  1700. #endif
  1701. /* Enable device in PCI config */
  1702. if ((err = pci_enable_device(pdev))) {
  1703. printk(KERN_ERR "aty128fb: Cannot enable PCI device: %d\n",
  1704. err);
  1705. return -ENODEV;
  1706. }
  1707. fb_addr = pci_resource_start(pdev, 0);
  1708. if (!request_mem_region(fb_addr, pci_resource_len(pdev, 0),
  1709. "aty128fb FB")) {
  1710. printk(KERN_ERR "aty128fb: cannot reserve frame "
  1711. "buffer memory\n");
  1712. return -ENODEV;
  1713. }
  1714. reg_addr = pci_resource_start(pdev, 2);
  1715. if (!request_mem_region(reg_addr, pci_resource_len(pdev, 2),
  1716. "aty128fb MMIO")) {
  1717. printk(KERN_ERR "aty128fb: cannot reserve MMIO region\n");
  1718. goto err_free_fb;
  1719. }
  1720. /* We have the resources. Now virtualize them */
  1721. info = framebuffer_alloc(sizeof(struct aty128fb_par), &pdev->dev);
  1722. if (info == NULL) {
  1723. printk(KERN_ERR "aty128fb: can't alloc fb_info_aty128\n");
  1724. goto err_free_mmio;
  1725. }
  1726. par = info->par;
  1727. info->pseudo_palette = par->pseudo_palette;
  1728. /* Virtualize mmio region */
  1729. info->fix.mmio_start = reg_addr;
  1730. par->regbase = pci_ioremap_bar(pdev, 2);
  1731. if (!par->regbase)
  1732. goto err_free_info;
  1733. /* Grab memory size from the card */
  1734. // How does this relate to the resource length from the PCI hardware?
  1735. par->vram_size = aty_ld_le32(CNFG_MEMSIZE) & 0x03FFFFFF;
  1736. /* Virtualize the framebuffer */
  1737. info->screen_base = ioremap(fb_addr, par->vram_size);
  1738. if (!info->screen_base)
  1739. goto err_unmap_out;
  1740. /* Set up info->fix */
  1741. info->fix = aty128fb_fix;
  1742. info->fix.smem_start = fb_addr;
  1743. info->fix.smem_len = par->vram_size;
  1744. info->fix.mmio_start = reg_addr;
  1745. /* If we can't test scratch registers, something is seriously wrong */
  1746. if (!register_test(par)) {
  1747. printk(KERN_ERR "aty128fb: Can't write to video register!\n");
  1748. goto err_out;
  1749. }
  1750. #ifndef __sparc__
  1751. bios = aty128_map_ROM(par, pdev);
  1752. #ifdef CONFIG_X86
  1753. if (bios == NULL)
  1754. bios = aty128_find_mem_vbios(par);
  1755. #endif
  1756. if (bios == NULL)
  1757. printk(KERN_INFO "aty128fb: BIOS not located, guessing timings.\n");
  1758. else {
  1759. printk(KERN_INFO "aty128fb: Rage128 BIOS located\n");
  1760. aty128_get_pllinfo(par, bios);
  1761. pci_unmap_rom(pdev, bios);
  1762. }
  1763. #endif /* __sparc__ */
  1764. aty128_timings(par);
  1765. pci_set_drvdata(pdev, info);
  1766. if (!aty128_init(pdev, ent))
  1767. goto err_out;
  1768. #ifdef CONFIG_MTRR
  1769. if (mtrr) {
  1770. par->mtrr.vram = mtrr_add(info->fix.smem_start,
  1771. par->vram_size, MTRR_TYPE_WRCOMB, 1);
  1772. par->mtrr.vram_valid = 1;
  1773. /* let there be speed */
  1774. printk(KERN_INFO "aty128fb: Rage128 MTRR set to ON\n");
  1775. }
  1776. #endif /* CONFIG_MTRR */
  1777. return 0;
  1778. err_out:
  1779. iounmap(info->screen_base);
  1780. err_unmap_out:
  1781. iounmap(par->regbase);
  1782. err_free_info:
  1783. framebuffer_release(info);
  1784. err_free_mmio:
  1785. release_mem_region(pci_resource_start(pdev, 2),
  1786. pci_resource_len(pdev, 2));
  1787. err_free_fb:
  1788. release_mem_region(pci_resource_start(pdev, 0),
  1789. pci_resource_len(pdev, 0));
  1790. return -ENODEV;
  1791. }
  1792. static void __devexit aty128_remove(struct pci_dev *pdev)
  1793. {
  1794. struct fb_info *info = pci_get_drvdata(pdev);
  1795. struct aty128fb_par *par;
  1796. if (!info)
  1797. return;
  1798. par = info->par;
  1799. unregister_framebuffer(info);
  1800. #ifdef CONFIG_FB_ATY128_BACKLIGHT
  1801. aty128_bl_exit(info->bl_dev);
  1802. #endif
  1803. #ifdef CONFIG_MTRR
  1804. if (par->mtrr.vram_valid)
  1805. mtrr_del(par->mtrr.vram, info->fix.smem_start,
  1806. par->vram_size);
  1807. #endif /* CONFIG_MTRR */
  1808. iounmap(par->regbase);
  1809. iounmap(info->screen_base);
  1810. release_mem_region(pci_resource_start(pdev, 0),
  1811. pci_resource_len(pdev, 0));
  1812. release_mem_region(pci_resource_start(pdev, 2),
  1813. pci_resource_len(pdev, 2));
  1814. framebuffer_release(info);
  1815. }
  1816. #endif /* CONFIG_PCI */
  1817. /*
  1818. * Blank the display.
  1819. */
  1820. static int aty128fb_blank(int blank, struct fb_info *fb)
  1821. {
  1822. struct aty128fb_par *par = fb->par;
  1823. u8 state;
  1824. if (par->lock_blank || par->asleep)
  1825. return 0;
  1826. switch (blank) {
  1827. case FB_BLANK_NORMAL:
  1828. state = 4;
  1829. break;
  1830. case FB_BLANK_VSYNC_SUSPEND:
  1831. state = 6;
  1832. break;
  1833. case FB_BLANK_HSYNC_SUSPEND:
  1834. state = 5;
  1835. break;
  1836. case FB_BLANK_POWERDOWN:
  1837. state = 7;
  1838. break;
  1839. case FB_BLANK_UNBLANK:
  1840. default:
  1841. state = 0;
  1842. break;
  1843. }
  1844. aty_st_8(CRTC_EXT_CNTL+1, state);
  1845. if (par->chip_gen == rage_M3) {
  1846. aty128_set_crt_enable(par, par->crt_on && !blank);
  1847. aty128_set_lcd_enable(par, par->lcd_on && !blank);
  1848. }
  1849. return 0;
  1850. }
  1851. /*
  1852. * Set a single color register. The values supplied are already
  1853. * rounded down to the hardware's capabilities (according to the
  1854. * entries in the var structure). Return != 0 for invalid regno.
  1855. */
  1856. static int aty128fb_setcolreg(u_int regno, u_int red, u_int green, u_int blue,
  1857. u_int transp, struct fb_info *info)
  1858. {
  1859. struct aty128fb_par *par = info->par;
  1860. if (regno > 255
  1861. || (par->crtc.depth == 16 && regno > 63)
  1862. || (par->crtc.depth == 15 && regno > 31))
  1863. return 1;
  1864. red >>= 8;
  1865. green >>= 8;
  1866. blue >>= 8;
  1867. if (regno < 16) {
  1868. int i;
  1869. u32 *pal = info->pseudo_palette;
  1870. switch (par->crtc.depth) {
  1871. case 15:
  1872. pal[regno] = (regno << 10) | (regno << 5) | regno;
  1873. break;
  1874. case 16:
  1875. pal[regno] = (regno << 11) | (regno << 6) | regno;
  1876. break;
  1877. case 24:
  1878. pal[regno] = (regno << 16) | (regno << 8) | regno;
  1879. break;
  1880. case 32:
  1881. i = (regno << 8) | regno;
  1882. pal[regno] = (i << 16) | i;
  1883. break;
  1884. }
  1885. }
  1886. if (par->crtc.depth == 16 && regno > 0) {
  1887. /*
  1888. * With the 5-6-5 split of bits for RGB at 16 bits/pixel, we
  1889. * have 32 slots for R and B values but 64 slots for G values.
  1890. * Thus the R and B values go in one slot but the G value
  1891. * goes in a different slot, and we have to avoid disturbing
  1892. * the other fields in the slots we touch.
  1893. */
  1894. par->green[regno] = green;
  1895. if (regno < 32) {
  1896. par->red[regno] = red;
  1897. par->blue[regno] = blue;
  1898. aty128_st_pal(regno * 8, red, par->green[regno*2],
  1899. blue, par);
  1900. }
  1901. red = par->red[regno/2];
  1902. blue = par->blue[regno/2];
  1903. regno <<= 2;
  1904. } else if (par->crtc.bpp == 16)
  1905. regno <<= 3;
  1906. aty128_st_pal(regno, red, green, blue, par);
  1907. return 0;
  1908. }
  1909. #define ATY_MIRROR_LCD_ON 0x00000001
  1910. #define ATY_MIRROR_CRT_ON 0x00000002
  1911. /* out param: u32* backlight value: 0 to 15 */
  1912. #define FBIO_ATY128_GET_MIRROR _IOR('@', 1, __u32)
  1913. /* in param: u32* backlight value: 0 to 15 */
  1914. #define FBIO_ATY128_SET_MIRROR _IOW('@', 2, __u32)
  1915. static int aty128fb_ioctl(struct fb_info *info, u_int cmd, u_long arg)
  1916. {
  1917. struct aty128fb_par *par = info->par;
  1918. u32 value;
  1919. int rc;
  1920. switch (cmd) {
  1921. case FBIO_ATY128_SET_MIRROR:
  1922. if (par->chip_gen != rage_M3)
  1923. return -EINVAL;
  1924. rc = get_user(value, (__u32 __user *)arg);
  1925. if (rc)
  1926. return rc;
  1927. par->lcd_on = (value & 0x01) != 0;
  1928. par->crt_on = (value & 0x02) != 0;
  1929. if (!par->crt_on && !par->lcd_on)
  1930. par->lcd_on = 1;
  1931. aty128_set_crt_enable(par, par->crt_on);
  1932. aty128_set_lcd_enable(par, par->lcd_on);
  1933. return 0;
  1934. case FBIO_ATY128_GET_MIRROR:
  1935. if (par->chip_gen != rage_M3)
  1936. return -EINVAL;
  1937. value = (par->crt_on << 1) | par->lcd_on;
  1938. return put_user(value, (__u32 __user *)arg);
  1939. }
  1940. return -EINVAL;
  1941. }
  1942. #if 0
  1943. /*
  1944. * Accelerated functions
  1945. */
  1946. static inline void aty128_rectcopy(int srcx, int srcy, int dstx, int dsty,
  1947. u_int width, u_int height,
  1948. struct fb_info_aty128 *par)
  1949. {
  1950. u32 save_dp_datatype, save_dp_cntl, dstval;
  1951. if (!width || !height)
  1952. return;
  1953. dstval = depth_to_dst(par->current_par.crtc.depth);
  1954. if (dstval == DST_24BPP) {
  1955. srcx *= 3;
  1956. dstx *= 3;
  1957. width *= 3;
  1958. } else if (dstval == -EINVAL) {
  1959. printk("aty128fb: invalid depth or RGBA\n");
  1960. return;
  1961. }
  1962. wait_for_fifo(2, par);
  1963. save_dp_datatype = aty_ld_le32(DP_DATATYPE);
  1964. save_dp_cntl = aty_ld_le32(DP_CNTL);
  1965. wait_for_fifo(6, par);
  1966. aty_st_le32(SRC_Y_X, (srcy << 16) | srcx);
  1967. aty_st_le32(DP_MIX, ROP3_SRCCOPY | DP_SRC_RECT);
  1968. aty_st_le32(DP_CNTL, DST_X_LEFT_TO_RIGHT | DST_Y_TOP_TO_BOTTOM);
  1969. aty_st_le32(DP_DATATYPE, save_dp_datatype | dstval | SRC_DSTCOLOR);
  1970. aty_st_le32(DST_Y_X, (dsty << 16) | dstx);
  1971. aty_st_le32(DST_HEIGHT_WIDTH, (height << 16) | width);
  1972. par->blitter_may_be_busy = 1;
  1973. wait_for_fifo(2, par);
  1974. aty_st_le32(DP_DATATYPE, save_dp_datatype);
  1975. aty_st_le32(DP_CNTL, save_dp_cntl);
  1976. }
  1977. /*
  1978. * Text mode accelerated functions
  1979. */
  1980. static void fbcon_aty128_bmove(struct display *p, int sy, int sx, int dy, int dx,
  1981. int height, int width)
  1982. {
  1983. sx *= fontwidth(p);
  1984. sy *= fontheight(p);
  1985. dx *= fontwidth(p);
  1986. dy *= fontheight(p);
  1987. width *= fontwidth(p);
  1988. height *= fontheight(p);
  1989. aty128_rectcopy(sx, sy, dx, dy, width, height,
  1990. (struct fb_info_aty128 *)p->fb_info);
  1991. }
  1992. #endif /* 0 */
  1993. static void aty128_set_suspend(struct aty128fb_par *par, int suspend)
  1994. {
  1995. u32 pmgt;
  1996. struct pci_dev *pdev = par->pdev;
  1997. if (!par->pm_reg)
  1998. return;
  1999. /* Set the chip into the appropriate suspend mode (we use D2,
  2000. * D3 would require a complete re-initialisation of the chip,
  2001. * including PCI config registers, clocks, AGP configuration, ...)
  2002. *
  2003. * For resume, the core will have already brought us back to D0
  2004. */
  2005. if (suspend) {
  2006. /* Make sure CRTC2 is reset. Remove that the day we decide to
  2007. * actually use CRTC2 and replace it with real code for disabling
  2008. * the CRTC2 output during sleep
  2009. */
  2010. aty_st_le32(CRTC2_GEN_CNTL, aty_ld_le32(CRTC2_GEN_CNTL) &
  2011. ~(CRTC2_EN));
  2012. /* Set the power management mode to be PCI based */
  2013. /* Use this magic value for now */
  2014. pmgt = 0x0c005407;
  2015. aty_st_pll(POWER_MANAGEMENT, pmgt);
  2016. (void)aty_ld_pll(POWER_MANAGEMENT);
  2017. aty_st_le32(BUS_CNTL1, 0x00000010);
  2018. aty_st_le32(MEM_POWER_MISC, 0x0c830000);
  2019. mdelay(100);
  2020. /* Switch PCI power management to D2 */
  2021. pci_set_power_state(pdev, PCI_D2);
  2022. }
  2023. }
  2024. static int aty128_pci_suspend(struct pci_dev *pdev, pm_message_t state)
  2025. {
  2026. struct fb_info *info = pci_get_drvdata(pdev);
  2027. struct aty128fb_par *par = info->par;
  2028. /* Because we may change PCI D state ourselves, we need to
  2029. * first save the config space content so the core can
  2030. * restore it properly on resume.
  2031. */
  2032. pci_save_state(pdev);
  2033. /* We don't do anything but D2, for now we return 0, but
  2034. * we may want to change that. How do we know if the BIOS
  2035. * can properly take care of D3 ? Also, with swsusp, we
  2036. * know we'll be rebooted, ...
  2037. */
  2038. #ifndef CONFIG_PPC_PMAC
  2039. /* HACK ALERT ! Once I find a proper way to say to each driver
  2040. * individually what will happen with it's PCI slot, I'll change
  2041. * that. On laptops, the AGP slot is just unclocked, so D2 is
  2042. * expected, while on desktops, the card is powered off
  2043. */
  2044. return 0;
  2045. #endif /* CONFIG_PPC_PMAC */
  2046. if (state.event == pdev->dev.power.power_state.event)
  2047. return 0;
  2048. printk(KERN_DEBUG "aty128fb: suspending...\n");
  2049. console_lock();
  2050. fb_set_suspend(info, 1);
  2051. /* Make sure engine is reset */
  2052. wait_for_idle(par);
  2053. aty128_reset_engine(par);
  2054. wait_for_idle(par);
  2055. /* Blank display and LCD */
  2056. aty128fb_blank(FB_BLANK_POWERDOWN, info);
  2057. /* Sleep */
  2058. par->asleep = 1;
  2059. par->lock_blank = 1;
  2060. #ifdef CONFIG_PPC_PMAC
  2061. /* On powermac, we have hooks to properly suspend/resume AGP now,
  2062. * use them here. We'll ultimately need some generic support here,
  2063. * but the generic code isn't quite ready for that yet
  2064. */
  2065. pmac_suspend_agp_for_card(pdev);
  2066. #endif /* CONFIG_PPC_PMAC */
  2067. /* We need a way to make sure the fbdev layer will _not_ touch the
  2068. * framebuffer before we put the chip to suspend state. On 2.4, I
  2069. * used dummy fb ops, 2.5 need proper support for this at the
  2070. * fbdev level
  2071. */
  2072. if (state.event != PM_EVENT_ON)
  2073. aty128_set_suspend(par, 1);
  2074. console_unlock();
  2075. pdev->dev.power.power_state = state;
  2076. return 0;
  2077. }
  2078. static int aty128_do_resume(struct pci_dev *pdev)
  2079. {
  2080. struct fb_info *info = pci_get_drvdata(pdev);
  2081. struct aty128fb_par *par = info->par;
  2082. if (pdev->dev.power.power_state.event == PM_EVENT_ON)
  2083. return 0;
  2084. /* PCI state will have been restored by the core, so
  2085. * we should be in D0 now with our config space fully
  2086. * restored
  2087. */
  2088. /* Wakeup chip */
  2089. aty128_set_suspend(par, 0);
  2090. par->asleep = 0;
  2091. /* Restore display & engine */
  2092. aty128_reset_engine(par);
  2093. wait_for_idle(par);
  2094. aty128fb_set_par(info);
  2095. fb_pan_display(info, &info->var);
  2096. fb_set_cmap(&info->cmap, info);
  2097. /* Refresh */
  2098. fb_set_suspend(info, 0);
  2099. /* Unblank */
  2100. par->lock_blank = 0;
  2101. aty128fb_blank(0, info);
  2102. #ifdef CONFIG_PPC_PMAC
  2103. /* On powermac, we have hooks to properly suspend/resume AGP now,
  2104. * use them here. We'll ultimately need some generic support here,
  2105. * but the generic code isn't quite ready for that yet
  2106. */
  2107. pmac_resume_agp_for_card(pdev);
  2108. #endif /* CONFIG_PPC_PMAC */
  2109. pdev->dev.power.power_state = PMSG_ON;
  2110. printk(KERN_DEBUG "aty128fb: resumed !\n");
  2111. return 0;
  2112. }
  2113. static int aty128_pci_resume(struct pci_dev *pdev)
  2114. {
  2115. int rc;
  2116. console_lock();
  2117. rc = aty128_do_resume(pdev);
  2118. console_unlock();
  2119. return rc;
  2120. }
  2121. static int __devinit aty128fb_init(void)
  2122. {
  2123. #ifndef MODULE
  2124. char *option = NULL;
  2125. if (fb_get_options("aty128fb", &option))
  2126. return -ENODEV;
  2127. aty128fb_setup(option);
  2128. #endif
  2129. return pci_register_driver(&aty128fb_driver);
  2130. }
  2131. static void __exit aty128fb_exit(void)
  2132. {
  2133. pci_unregister_driver(&aty128fb_driver);
  2134. }
  2135. module_init(aty128fb_init);
  2136. module_exit(aty128fb_exit);
  2137. MODULE_AUTHOR("(c)1999-2003 Brad Douglas <brad@neruo.com>");
  2138. MODULE_DESCRIPTION("FBDev driver for ATI Rage128 / Pro cards");
  2139. MODULE_LICENSE("GPL");
  2140. module_param(mode_option, charp, 0);
  2141. MODULE_PARM_DESC(mode_option, "Specify resolution as \"<xres>x<yres>[-<bpp>][@<refresh>]\" ");
  2142. #ifdef CONFIG_MTRR
  2143. module_param_named(nomtrr, mtrr, invbool, 0);
  2144. MODULE_PARM_DESC(nomtrr, "bool: Disable MTRR support (0 or 1=disabled) (default=0)");
  2145. #endif