PageRenderTime 61ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/drivers/video/aty/radeon_base.c

https://github.com/mstsirkin/linux
C | 1677 lines | 1193 code | 222 blank | 262 comment | 206 complexity | 47db87adaf985f97ed5210eafb80cebf MD5 | raw file
  1. /*
  2. * drivers/video/aty/radeon_base.c
  3. *
  4. * framebuffer driver for ATI Radeon chipset video boards
  5. *
  6. * Copyright 2003 Ben. Herrenschmidt <benh@kernel.crashing.org>
  7. * Copyright 2000 Ani Joshi <ajoshi@kernel.crashing.org>
  8. *
  9. * i2c bits from Luca Tettamanti <kronos@kronoz.cjb.net>
  10. *
  11. * Special thanks to ATI DevRel team for their hardware donations.
  12. *
  13. * ...Insert GPL boilerplate here...
  14. *
  15. * Significant portions of this driver apdated from XFree86 Radeon
  16. * driver which has the following copyright notice:
  17. *
  18. * Copyright 2000 ATI Technologies Inc., Markham, Ontario, and
  19. * VA Linux Systems Inc., Fremont, California.
  20. *
  21. * All Rights Reserved.
  22. *
  23. * Permission is hereby granted, free of charge, to any person obtaining
  24. * a copy of this software and associated documentation files (the
  25. * "Software"), to deal in the Software without restriction, including
  26. * without limitation on the rights to use, copy, modify, merge,
  27. * publish, distribute, sublicense, and/or sell copies of the Software,
  28. * and to permit persons to whom the Software is furnished to do so,
  29. * subject to the following conditions:
  30. *
  31. * The above copyright notice and this permission notice (including the
  32. * next paragraph) shall be included in all copies or substantial
  33. * portions of the Software.
  34. *
  35. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  36. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  37. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  38. * NON-INFRINGEMENT. IN NO EVENT SHALL ATI, VA LINUX SYSTEMS AND/OR
  39. * THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  40. * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  41. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  42. * DEALINGS IN THE SOFTWARE.
  43. *
  44. * XFree86 driver authors:
  45. *
  46. * Kevin E. Martin <martin@xfree86.org>
  47. * Rickard E. Faith <faith@valinux.com>
  48. * Alan Hourihane <alanh@fairlite.demon.co.uk>
  49. *
  50. */
  51. #define RADEON_VERSION "0.2.0"
  52. #include "radeonfb.h"
  53. #include <linux/module.h>
  54. #include <linux/moduleparam.h>
  55. #include <linux/kernel.h>
  56. #include <linux/errno.h>
  57. #include <linux/string.h>
  58. #include <linux/ctype.h>
  59. #include <linux/mm.h>
  60. #include <linux/slab.h>
  61. #include <linux/delay.h>
  62. #include <linux/time.h>
  63. #include <linux/fb.h>
  64. #include <linux/ioport.h>
  65. #include <linux/init.h>
  66. #include <linux/pci.h>
  67. #include <linux/vmalloc.h>
  68. #include <linux/device.h>
  69. #include <asm/io.h>
  70. #include <linux/uaccess.h>
  71. #ifdef CONFIG_PPC_OF
  72. #include <asm/pci-bridge.h>
  73. #include "../macmodes.h"
  74. #ifdef CONFIG_BOOTX_TEXT
  75. #include <asm/btext.h>
  76. #endif
  77. #endif /* CONFIG_PPC_OF */
  78. #ifdef CONFIG_MTRR
  79. #include <asm/mtrr.h>
  80. #endif
  81. #include <video/radeon.h>
  82. #include <linux/radeonfb.h>
  83. #include "../edid.h" // MOVE THAT TO include/video
  84. #include "ati_ids.h"
  85. #define MAX_MAPPED_VRAM (2048*2048*4)
  86. #define MIN_MAPPED_VRAM (1024*768*1)
  87. #define CHIP_DEF(id, family, flags) \
  88. { PCI_VENDOR_ID_ATI, id, PCI_ANY_ID, PCI_ANY_ID, 0, 0, (flags) | (CHIP_FAMILY_##family) }
  89. static struct pci_device_id radeonfb_pci_table[] = {
  90. /* Radeon Xpress 200m */
  91. CHIP_DEF(PCI_CHIP_RS480_5955, RS480, CHIP_HAS_CRTC2 | CHIP_IS_IGP | CHIP_IS_MOBILITY),
  92. CHIP_DEF(PCI_CHIP_RS482_5975, RS480, CHIP_HAS_CRTC2 | CHIP_IS_IGP | CHIP_IS_MOBILITY),
  93. /* Mobility M6 */
  94. CHIP_DEF(PCI_CHIP_RADEON_LY, RV100, CHIP_HAS_CRTC2 | CHIP_IS_MOBILITY),
  95. CHIP_DEF(PCI_CHIP_RADEON_LZ, RV100, CHIP_HAS_CRTC2 | CHIP_IS_MOBILITY),
  96. /* Radeon VE/7000 */
  97. CHIP_DEF(PCI_CHIP_RV100_QY, RV100, CHIP_HAS_CRTC2),
  98. CHIP_DEF(PCI_CHIP_RV100_QZ, RV100, CHIP_HAS_CRTC2),
  99. CHIP_DEF(PCI_CHIP_RN50, RV100, CHIP_HAS_CRTC2),
  100. /* Radeon IGP320M (U1) */
  101. CHIP_DEF(PCI_CHIP_RS100_4336, RS100, CHIP_HAS_CRTC2 | CHIP_IS_IGP | CHIP_IS_MOBILITY),
  102. /* Radeon IGP320 (A3) */
  103. CHIP_DEF(PCI_CHIP_RS100_4136, RS100, CHIP_HAS_CRTC2 | CHIP_IS_IGP),
  104. /* IGP330M/340M/350M (U2) */
  105. CHIP_DEF(PCI_CHIP_RS200_4337, RS200, CHIP_HAS_CRTC2 | CHIP_IS_IGP | CHIP_IS_MOBILITY),
  106. /* IGP330/340/350 (A4) */
  107. CHIP_DEF(PCI_CHIP_RS200_4137, RS200, CHIP_HAS_CRTC2 | CHIP_IS_IGP),
  108. /* Mobility 7000 IGP */
  109. CHIP_DEF(PCI_CHIP_RS250_4437, RS200, CHIP_HAS_CRTC2 | CHIP_IS_IGP | CHIP_IS_MOBILITY),
  110. /* 7000 IGP (A4+) */
  111. CHIP_DEF(PCI_CHIP_RS250_4237, RS200, CHIP_HAS_CRTC2 | CHIP_IS_IGP),
  112. /* 8500 AIW */
  113. CHIP_DEF(PCI_CHIP_R200_BB, R200, CHIP_HAS_CRTC2),
  114. CHIP_DEF(PCI_CHIP_R200_BC, R200, CHIP_HAS_CRTC2),
  115. /* 8700/8800 */
  116. CHIP_DEF(PCI_CHIP_R200_QH, R200, CHIP_HAS_CRTC2),
  117. /* 8500 */
  118. CHIP_DEF(PCI_CHIP_R200_QL, R200, CHIP_HAS_CRTC2),
  119. /* 9100 */
  120. CHIP_DEF(PCI_CHIP_R200_QM, R200, CHIP_HAS_CRTC2),
  121. /* Mobility M7 */
  122. CHIP_DEF(PCI_CHIP_RADEON_LW, RV200, CHIP_HAS_CRTC2 | CHIP_IS_MOBILITY),
  123. CHIP_DEF(PCI_CHIP_RADEON_LX, RV200, CHIP_HAS_CRTC2 | CHIP_IS_MOBILITY),
  124. /* 7500 */
  125. CHIP_DEF(PCI_CHIP_RV200_QW, RV200, CHIP_HAS_CRTC2),
  126. CHIP_DEF(PCI_CHIP_RV200_QX, RV200, CHIP_HAS_CRTC2),
  127. /* Mobility M9 */
  128. CHIP_DEF(PCI_CHIP_RV250_Ld, RV250, CHIP_HAS_CRTC2 | CHIP_IS_MOBILITY),
  129. CHIP_DEF(PCI_CHIP_RV250_Le, RV250, CHIP_HAS_CRTC2 | CHIP_IS_MOBILITY),
  130. CHIP_DEF(PCI_CHIP_RV250_Lf, RV250, CHIP_HAS_CRTC2 | CHIP_IS_MOBILITY),
  131. CHIP_DEF(PCI_CHIP_RV250_Lg, RV250, CHIP_HAS_CRTC2 | CHIP_IS_MOBILITY),
  132. /* 9000/Pro */
  133. CHIP_DEF(PCI_CHIP_RV250_If, RV250, CHIP_HAS_CRTC2),
  134. CHIP_DEF(PCI_CHIP_RV250_Ig, RV250, CHIP_HAS_CRTC2),
  135. CHIP_DEF(PCI_CHIP_RC410_5A62, RC410, CHIP_HAS_CRTC2 | CHIP_IS_IGP | CHIP_IS_MOBILITY),
  136. /* Mobility 9100 IGP (U3) */
  137. CHIP_DEF(PCI_CHIP_RS300_5835, RS300, CHIP_HAS_CRTC2 | CHIP_IS_IGP | CHIP_IS_MOBILITY),
  138. CHIP_DEF(PCI_CHIP_RS350_7835, RS300, CHIP_HAS_CRTC2 | CHIP_IS_IGP | CHIP_IS_MOBILITY),
  139. /* 9100 IGP (A5) */
  140. CHIP_DEF(PCI_CHIP_RS300_5834, RS300, CHIP_HAS_CRTC2 | CHIP_IS_IGP),
  141. CHIP_DEF(PCI_CHIP_RS350_7834, RS300, CHIP_HAS_CRTC2 | CHIP_IS_IGP),
  142. /* Mobility 9200 (M9+) */
  143. CHIP_DEF(PCI_CHIP_RV280_5C61, RV280, CHIP_HAS_CRTC2 | CHIP_IS_MOBILITY),
  144. CHIP_DEF(PCI_CHIP_RV280_5C63, RV280, CHIP_HAS_CRTC2 | CHIP_IS_MOBILITY),
  145. /* 9200 */
  146. CHIP_DEF(PCI_CHIP_RV280_5960, RV280, CHIP_HAS_CRTC2),
  147. CHIP_DEF(PCI_CHIP_RV280_5961, RV280, CHIP_HAS_CRTC2),
  148. CHIP_DEF(PCI_CHIP_RV280_5962, RV280, CHIP_HAS_CRTC2),
  149. CHIP_DEF(PCI_CHIP_RV280_5964, RV280, CHIP_HAS_CRTC2),
  150. /* 9500 */
  151. CHIP_DEF(PCI_CHIP_R300_AD, R300, CHIP_HAS_CRTC2),
  152. CHIP_DEF(PCI_CHIP_R300_AE, R300, CHIP_HAS_CRTC2),
  153. /* 9600TX / FireGL Z1 */
  154. CHIP_DEF(PCI_CHIP_R300_AF, R300, CHIP_HAS_CRTC2),
  155. CHIP_DEF(PCI_CHIP_R300_AG, R300, CHIP_HAS_CRTC2),
  156. /* 9700/9500/Pro/FireGL X1 */
  157. CHIP_DEF(PCI_CHIP_R300_ND, R300, CHIP_HAS_CRTC2),
  158. CHIP_DEF(PCI_CHIP_R300_NE, R300, CHIP_HAS_CRTC2),
  159. CHIP_DEF(PCI_CHIP_R300_NF, R300, CHIP_HAS_CRTC2),
  160. CHIP_DEF(PCI_CHIP_R300_NG, R300, CHIP_HAS_CRTC2),
  161. /* Mobility M10/M11 */
  162. CHIP_DEF(PCI_CHIP_RV350_NP, RV350, CHIP_HAS_CRTC2 | CHIP_IS_MOBILITY),
  163. CHIP_DEF(PCI_CHIP_RV350_NQ, RV350, CHIP_HAS_CRTC2 | CHIP_IS_MOBILITY),
  164. CHIP_DEF(PCI_CHIP_RV350_NR, RV350, CHIP_HAS_CRTC2 | CHIP_IS_MOBILITY),
  165. CHIP_DEF(PCI_CHIP_RV350_NS, RV350, CHIP_HAS_CRTC2 | CHIP_IS_MOBILITY),
  166. CHIP_DEF(PCI_CHIP_RV350_NT, RV350, CHIP_HAS_CRTC2 | CHIP_IS_MOBILITY),
  167. CHIP_DEF(PCI_CHIP_RV350_NV, RV350, CHIP_HAS_CRTC2 | CHIP_IS_MOBILITY),
  168. /* 9600/FireGL T2 */
  169. CHIP_DEF(PCI_CHIP_RV350_AP, RV350, CHIP_HAS_CRTC2),
  170. CHIP_DEF(PCI_CHIP_RV350_AQ, RV350, CHIP_HAS_CRTC2),
  171. CHIP_DEF(PCI_CHIP_RV360_AR, RV350, CHIP_HAS_CRTC2),
  172. CHIP_DEF(PCI_CHIP_RV350_AS, RV350, CHIP_HAS_CRTC2),
  173. CHIP_DEF(PCI_CHIP_RV350_AT, RV350, CHIP_HAS_CRTC2),
  174. CHIP_DEF(PCI_CHIP_RV350_AV, RV350, CHIP_HAS_CRTC2),
  175. /* 9800/Pro/FileGL X2 */
  176. CHIP_DEF(PCI_CHIP_R350_AH, R350, CHIP_HAS_CRTC2),
  177. CHIP_DEF(PCI_CHIP_R350_AI, R350, CHIP_HAS_CRTC2),
  178. CHIP_DEF(PCI_CHIP_R350_AJ, R350, CHIP_HAS_CRTC2),
  179. CHIP_DEF(PCI_CHIP_R350_AK, R350, CHIP_HAS_CRTC2),
  180. CHIP_DEF(PCI_CHIP_R350_NH, R350, CHIP_HAS_CRTC2),
  181. CHIP_DEF(PCI_CHIP_R350_NI, R350, CHIP_HAS_CRTC2),
  182. CHIP_DEF(PCI_CHIP_R360_NJ, R350, CHIP_HAS_CRTC2),
  183. CHIP_DEF(PCI_CHIP_R350_NK, R350, CHIP_HAS_CRTC2),
  184. /* Newer stuff */
  185. CHIP_DEF(PCI_CHIP_RV380_3E50, RV380, CHIP_HAS_CRTC2),
  186. CHIP_DEF(PCI_CHIP_RV380_3E54, RV380, CHIP_HAS_CRTC2),
  187. CHIP_DEF(PCI_CHIP_RV380_3150, RV380, CHIP_HAS_CRTC2 | CHIP_IS_MOBILITY),
  188. CHIP_DEF(PCI_CHIP_RV380_3154, RV380, CHIP_HAS_CRTC2 | CHIP_IS_MOBILITY),
  189. CHIP_DEF(PCI_CHIP_RV370_5B60, RV380, CHIP_HAS_CRTC2),
  190. CHIP_DEF(PCI_CHIP_RV370_5B62, RV380, CHIP_HAS_CRTC2),
  191. CHIP_DEF(PCI_CHIP_RV370_5B63, RV380, CHIP_HAS_CRTC2),
  192. CHIP_DEF(PCI_CHIP_RV370_5B64, RV380, CHIP_HAS_CRTC2),
  193. CHIP_DEF(PCI_CHIP_RV370_5B65, RV380, CHIP_HAS_CRTC2),
  194. CHIP_DEF(PCI_CHIP_RV370_5460, RV380, CHIP_HAS_CRTC2 | CHIP_IS_MOBILITY),
  195. CHIP_DEF(PCI_CHIP_RV370_5464, RV380, CHIP_HAS_CRTC2 | CHIP_IS_MOBILITY),
  196. CHIP_DEF(PCI_CHIP_R420_JH, R420, CHIP_HAS_CRTC2),
  197. CHIP_DEF(PCI_CHIP_R420_JI, R420, CHIP_HAS_CRTC2),
  198. CHIP_DEF(PCI_CHIP_R420_JJ, R420, CHIP_HAS_CRTC2),
  199. CHIP_DEF(PCI_CHIP_R420_JK, R420, CHIP_HAS_CRTC2),
  200. CHIP_DEF(PCI_CHIP_R420_JL, R420, CHIP_HAS_CRTC2),
  201. CHIP_DEF(PCI_CHIP_R420_JM, R420, CHIP_HAS_CRTC2),
  202. CHIP_DEF(PCI_CHIP_R420_JN, R420, CHIP_HAS_CRTC2 | CHIP_IS_MOBILITY),
  203. CHIP_DEF(PCI_CHIP_R420_JP, R420, CHIP_HAS_CRTC2),
  204. CHIP_DEF(PCI_CHIP_R423_UH, R420, CHIP_HAS_CRTC2),
  205. CHIP_DEF(PCI_CHIP_R423_UI, R420, CHIP_HAS_CRTC2),
  206. CHIP_DEF(PCI_CHIP_R423_UJ, R420, CHIP_HAS_CRTC2),
  207. CHIP_DEF(PCI_CHIP_R423_UK, R420, CHIP_HAS_CRTC2),
  208. CHIP_DEF(PCI_CHIP_R423_UQ, R420, CHIP_HAS_CRTC2),
  209. CHIP_DEF(PCI_CHIP_R423_UR, R420, CHIP_HAS_CRTC2),
  210. CHIP_DEF(PCI_CHIP_R423_UT, R420, CHIP_HAS_CRTC2),
  211. CHIP_DEF(PCI_CHIP_R423_5D57, R420, CHIP_HAS_CRTC2),
  212. /* Original Radeon/7200 */
  213. CHIP_DEF(PCI_CHIP_RADEON_QD, RADEON, 0),
  214. CHIP_DEF(PCI_CHIP_RADEON_QE, RADEON, 0),
  215. CHIP_DEF(PCI_CHIP_RADEON_QF, RADEON, 0),
  216. CHIP_DEF(PCI_CHIP_RADEON_QG, RADEON, 0),
  217. { 0, }
  218. };
  219. MODULE_DEVICE_TABLE(pci, radeonfb_pci_table);
  220. typedef struct {
  221. u16 reg;
  222. u32 val;
  223. } reg_val;
  224. /* these common regs are cleared before mode setting so they do not
  225. * interfere with anything
  226. */
  227. static reg_val common_regs[] = {
  228. { OVR_CLR, 0 },
  229. { OVR_WID_LEFT_RIGHT, 0 },
  230. { OVR_WID_TOP_BOTTOM, 0 },
  231. { OV0_SCALE_CNTL, 0 },
  232. { SUBPIC_CNTL, 0 },
  233. { VIPH_CONTROL, 0 },
  234. { I2C_CNTL_1, 0 },
  235. { GEN_INT_CNTL, 0 },
  236. { CAP0_TRIG_CNTL, 0 },
  237. { CAP1_TRIG_CNTL, 0 },
  238. };
  239. /*
  240. * globals
  241. */
  242. static char *mode_option;
  243. static char *monitor_layout;
  244. static int noaccel = 0;
  245. static int default_dynclk = -2;
  246. static int nomodeset = 0;
  247. static int ignore_edid = 0;
  248. static int mirror = 0;
  249. static int panel_yres = 0;
  250. static int force_dfp = 0;
  251. static int force_measure_pll = 0;
  252. #ifdef CONFIG_MTRR
  253. static int nomtrr = 0;
  254. #endif
  255. static int force_sleep;
  256. static int ignore_devlist;
  257. #ifdef CONFIG_PMAC_BACKLIGHT
  258. static int backlight = 1;
  259. #else
  260. static int backlight = 0;
  261. #endif
  262. /*
  263. * prototypes
  264. */
  265. static void radeon_unmap_ROM(struct radeonfb_info *rinfo, struct pci_dev *dev)
  266. {
  267. if (!rinfo->bios_seg)
  268. return;
  269. pci_unmap_rom(dev, rinfo->bios_seg);
  270. }
  271. static int __devinit radeon_map_ROM(struct radeonfb_info *rinfo, struct pci_dev *dev)
  272. {
  273. void __iomem *rom;
  274. u16 dptr;
  275. u8 rom_type;
  276. size_t rom_size;
  277. /* If this is a primary card, there is a shadow copy of the
  278. * ROM somewhere in the first meg. We will just ignore the copy
  279. * and use the ROM directly.
  280. */
  281. /* Fix from ATI for problem with Radeon hardware not leaving ROM enabled */
  282. unsigned int temp;
  283. temp = INREG(MPP_TB_CONFIG);
  284. temp &= 0x00ffffffu;
  285. temp |= 0x04 << 24;
  286. OUTREG(MPP_TB_CONFIG, temp);
  287. temp = INREG(MPP_TB_CONFIG);
  288. rom = pci_map_rom(dev, &rom_size);
  289. if (!rom) {
  290. printk(KERN_ERR "radeonfb (%s): ROM failed to map\n",
  291. pci_name(rinfo->pdev));
  292. return -ENOMEM;
  293. }
  294. rinfo->bios_seg = rom;
  295. /* Very simple test to make sure it appeared */
  296. if (BIOS_IN16(0) != 0xaa55) {
  297. printk(KERN_DEBUG "radeonfb (%s): Invalid ROM signature %x "
  298. "should be 0xaa55\n",
  299. pci_name(rinfo->pdev), BIOS_IN16(0));
  300. goto failed;
  301. }
  302. /* Look for the PCI data to check the ROM type */
  303. dptr = BIOS_IN16(0x18);
  304. /* Check the PCI data signature. If it's wrong, we still assume a normal x86 ROM
  305. * for now, until I've verified this works everywhere. The goal here is more
  306. * to phase out Open Firmware images.
  307. *
  308. * Currently, we only look at the first PCI data, we could iteratre and deal with
  309. * them all, and we should use fb_bios_start relative to start of image and not
  310. * relative start of ROM, but so far, I never found a dual-image ATI card
  311. *
  312. * typedef struct {
  313. * u32 signature; + 0x00
  314. * u16 vendor; + 0x04
  315. * u16 device; + 0x06
  316. * u16 reserved_1; + 0x08
  317. * u16 dlen; + 0x0a
  318. * u8 drevision; + 0x0c
  319. * u8 class_hi; + 0x0d
  320. * u16 class_lo; + 0x0e
  321. * u16 ilen; + 0x10
  322. * u16 irevision; + 0x12
  323. * u8 type; + 0x14
  324. * u8 indicator; + 0x15
  325. * u16 reserved_2; + 0x16
  326. * } pci_data_t;
  327. */
  328. if (BIOS_IN32(dptr) != (('R' << 24) | ('I' << 16) | ('C' << 8) | 'P')) {
  329. printk(KERN_WARNING "radeonfb (%s): PCI DATA signature in ROM"
  330. "incorrect: %08x\n", pci_name(rinfo->pdev), BIOS_IN32(dptr));
  331. goto anyway;
  332. }
  333. rom_type = BIOS_IN8(dptr + 0x14);
  334. switch(rom_type) {
  335. case 0:
  336. printk(KERN_INFO "radeonfb: Found Intel x86 BIOS ROM Image\n");
  337. break;
  338. case 1:
  339. printk(KERN_INFO "radeonfb: Found Open Firmware ROM Image\n");
  340. goto failed;
  341. case 2:
  342. printk(KERN_INFO "radeonfb: Found HP PA-RISC ROM Image\n");
  343. goto failed;
  344. default:
  345. printk(KERN_INFO "radeonfb: Found unknown type %d ROM Image\n", rom_type);
  346. goto failed;
  347. }
  348. anyway:
  349. /* Locate the flat panel infos, do some sanity checking !!! */
  350. rinfo->fp_bios_start = BIOS_IN16(0x48);
  351. return 0;
  352. failed:
  353. rinfo->bios_seg = NULL;
  354. radeon_unmap_ROM(rinfo, dev);
  355. return -ENXIO;
  356. }
  357. #ifdef CONFIG_X86
  358. static int __devinit radeon_find_mem_vbios(struct radeonfb_info *rinfo)
  359. {
  360. /* I simplified this code as we used to miss the signatures in
  361. * a lot of case. It's now closer to XFree, we just don't check
  362. * for signatures at all... Something better will have to be done
  363. * if we end up having conflicts
  364. */
  365. u32 segstart;
  366. void __iomem *rom_base = NULL;
  367. for(segstart=0x000c0000; segstart<0x000f0000; segstart+=0x00001000) {
  368. rom_base = ioremap(segstart, 0x10000);
  369. if (rom_base == NULL)
  370. return -ENOMEM;
  371. if (readb(rom_base) == 0x55 && readb(rom_base + 1) == 0xaa)
  372. break;
  373. iounmap(rom_base);
  374. rom_base = NULL;
  375. }
  376. if (rom_base == NULL)
  377. return -ENXIO;
  378. /* Locate the flat panel infos, do some sanity checking !!! */
  379. rinfo->bios_seg = rom_base;
  380. rinfo->fp_bios_start = BIOS_IN16(0x48);
  381. return 0;
  382. }
  383. #endif
  384. #if defined(CONFIG_PPC_OF) || defined(CONFIG_SPARC)
  385. /*
  386. * Read XTAL (ref clock), SCLK and MCLK from Open Firmware device
  387. * tree. Hopefully, ATI OF driver is kind enough to fill these
  388. */
  389. static int __devinit radeon_read_xtal_OF (struct radeonfb_info *rinfo)
  390. {
  391. struct device_node *dp = rinfo->of_node;
  392. const u32 *val;
  393. if (dp == NULL)
  394. return -ENODEV;
  395. val = of_get_property(dp, "ATY,RefCLK", NULL);
  396. if (!val || !*val) {
  397. printk(KERN_WARNING "radeonfb: No ATY,RefCLK property !\n");
  398. return -EINVAL;
  399. }
  400. rinfo->pll.ref_clk = (*val) / 10;
  401. val = of_get_property(dp, "ATY,SCLK", NULL);
  402. if (val && *val)
  403. rinfo->pll.sclk = (*val) / 10;
  404. val = of_get_property(dp, "ATY,MCLK", NULL);
  405. if (val && *val)
  406. rinfo->pll.mclk = (*val) / 10;
  407. return 0;
  408. }
  409. #endif /* CONFIG_PPC_OF || CONFIG_SPARC */
  410. /*
  411. * Read PLL infos from chip registers
  412. */
  413. static int __devinit radeon_probe_pll_params(struct radeonfb_info *rinfo)
  414. {
  415. unsigned char ppll_div_sel;
  416. unsigned Ns, Nm, M;
  417. unsigned sclk, mclk, tmp, ref_div;
  418. int hTotal, vTotal, num, denom, m, n;
  419. unsigned long long hz, vclk;
  420. long xtal;
  421. struct timeval start_tv, stop_tv;
  422. long total_secs, total_usecs;
  423. int i;
  424. /* Ugh, we cut interrupts, bad bad bad, but we want some precision
  425. * here, so... --BenH
  426. */
  427. /* Flush PCI buffers ? */
  428. tmp = INREG16(DEVICE_ID);
  429. local_irq_disable();
  430. for(i=0; i<1000000; i++)
  431. if (((INREG(CRTC_VLINE_CRNT_VLINE) >> 16) & 0x3ff) == 0)
  432. break;
  433. do_gettimeofday(&start_tv);
  434. for(i=0; i<1000000; i++)
  435. if (((INREG(CRTC_VLINE_CRNT_VLINE) >> 16) & 0x3ff) != 0)
  436. break;
  437. for(i=0; i<1000000; i++)
  438. if (((INREG(CRTC_VLINE_CRNT_VLINE) >> 16) & 0x3ff) == 0)
  439. break;
  440. do_gettimeofday(&stop_tv);
  441. local_irq_enable();
  442. total_secs = stop_tv.tv_sec - start_tv.tv_sec;
  443. if (total_secs > 10)
  444. return -1;
  445. total_usecs = stop_tv.tv_usec - start_tv.tv_usec;
  446. total_usecs += total_secs * 1000000;
  447. if (total_usecs < 0)
  448. total_usecs = -total_usecs;
  449. hz = 1000000/total_usecs;
  450. hTotal = ((INREG(CRTC_H_TOTAL_DISP) & 0x1ff) + 1) * 8;
  451. vTotal = ((INREG(CRTC_V_TOTAL_DISP) & 0x3ff) + 1);
  452. vclk = (long long)hTotal * (long long)vTotal * hz;
  453. switch((INPLL(PPLL_REF_DIV) & 0x30000) >> 16) {
  454. case 0:
  455. default:
  456. num = 1;
  457. denom = 1;
  458. break;
  459. case 1:
  460. n = ((INPLL(M_SPLL_REF_FB_DIV) >> 16) & 0xff);
  461. m = (INPLL(M_SPLL_REF_FB_DIV) & 0xff);
  462. num = 2*n;
  463. denom = 2*m;
  464. break;
  465. case 2:
  466. n = ((INPLL(M_SPLL_REF_FB_DIV) >> 8) & 0xff);
  467. m = (INPLL(M_SPLL_REF_FB_DIV) & 0xff);
  468. num = 2*n;
  469. denom = 2*m;
  470. break;
  471. }
  472. ppll_div_sel = INREG8(CLOCK_CNTL_INDEX + 1) & 0x3;
  473. radeon_pll_errata_after_index(rinfo);
  474. n = (INPLL(PPLL_DIV_0 + ppll_div_sel) & 0x7ff);
  475. m = (INPLL(PPLL_REF_DIV) & 0x3ff);
  476. num *= n;
  477. denom *= m;
  478. switch ((INPLL(PPLL_DIV_0 + ppll_div_sel) >> 16) & 0x7) {
  479. case 1:
  480. denom *= 2;
  481. break;
  482. case 2:
  483. denom *= 4;
  484. break;
  485. case 3:
  486. denom *= 8;
  487. break;
  488. case 4:
  489. denom *= 3;
  490. break;
  491. case 6:
  492. denom *= 6;
  493. break;
  494. case 7:
  495. denom *= 12;
  496. break;
  497. }
  498. vclk *= denom;
  499. do_div(vclk, 1000 * num);
  500. xtal = vclk;
  501. if ((xtal > 26900) && (xtal < 27100))
  502. xtal = 2700;
  503. else if ((xtal > 14200) && (xtal < 14400))
  504. xtal = 1432;
  505. else if ((xtal > 29400) && (xtal < 29600))
  506. xtal = 2950;
  507. else {
  508. printk(KERN_WARNING "xtal calculation failed: %ld\n", xtal);
  509. return -1;
  510. }
  511. tmp = INPLL(M_SPLL_REF_FB_DIV);
  512. ref_div = INPLL(PPLL_REF_DIV) & 0x3ff;
  513. Ns = (tmp & 0xff0000) >> 16;
  514. Nm = (tmp & 0xff00) >> 8;
  515. M = (tmp & 0xff);
  516. sclk = round_div((2 * Ns * xtal), (2 * M));
  517. mclk = round_div((2 * Nm * xtal), (2 * M));
  518. /* we're done, hopefully these are sane values */
  519. rinfo->pll.ref_clk = xtal;
  520. rinfo->pll.ref_div = ref_div;
  521. rinfo->pll.sclk = sclk;
  522. rinfo->pll.mclk = mclk;
  523. return 0;
  524. }
  525. /*
  526. * Retrieve PLL infos by different means (BIOS, Open Firmware, register probing...)
  527. */
  528. static void __devinit radeon_get_pllinfo(struct radeonfb_info *rinfo)
  529. {
  530. /*
  531. * In the case nothing works, these are defaults; they are mostly
  532. * incomplete, however. It does provide ppll_max and _min values
  533. * even for most other methods, however.
  534. */
  535. switch (rinfo->chipset) {
  536. case PCI_DEVICE_ID_ATI_RADEON_QW:
  537. case PCI_DEVICE_ID_ATI_RADEON_QX:
  538. rinfo->pll.ppll_max = 35000;
  539. rinfo->pll.ppll_min = 12000;
  540. rinfo->pll.mclk = 23000;
  541. rinfo->pll.sclk = 23000;
  542. rinfo->pll.ref_clk = 2700;
  543. break;
  544. case PCI_DEVICE_ID_ATI_RADEON_QL:
  545. case PCI_DEVICE_ID_ATI_RADEON_QN:
  546. case PCI_DEVICE_ID_ATI_RADEON_QO:
  547. case PCI_DEVICE_ID_ATI_RADEON_Ql:
  548. case PCI_DEVICE_ID_ATI_RADEON_BB:
  549. rinfo->pll.ppll_max = 35000;
  550. rinfo->pll.ppll_min = 12000;
  551. rinfo->pll.mclk = 27500;
  552. rinfo->pll.sclk = 27500;
  553. rinfo->pll.ref_clk = 2700;
  554. break;
  555. case PCI_DEVICE_ID_ATI_RADEON_Id:
  556. case PCI_DEVICE_ID_ATI_RADEON_Ie:
  557. case PCI_DEVICE_ID_ATI_RADEON_If:
  558. case PCI_DEVICE_ID_ATI_RADEON_Ig:
  559. rinfo->pll.ppll_max = 35000;
  560. rinfo->pll.ppll_min = 12000;
  561. rinfo->pll.mclk = 25000;
  562. rinfo->pll.sclk = 25000;
  563. rinfo->pll.ref_clk = 2700;
  564. break;
  565. case PCI_DEVICE_ID_ATI_RADEON_ND:
  566. case PCI_DEVICE_ID_ATI_RADEON_NE:
  567. case PCI_DEVICE_ID_ATI_RADEON_NF:
  568. case PCI_DEVICE_ID_ATI_RADEON_NG:
  569. rinfo->pll.ppll_max = 40000;
  570. rinfo->pll.ppll_min = 20000;
  571. rinfo->pll.mclk = 27000;
  572. rinfo->pll.sclk = 27000;
  573. rinfo->pll.ref_clk = 2700;
  574. break;
  575. case PCI_DEVICE_ID_ATI_RADEON_QD:
  576. case PCI_DEVICE_ID_ATI_RADEON_QE:
  577. case PCI_DEVICE_ID_ATI_RADEON_QF:
  578. case PCI_DEVICE_ID_ATI_RADEON_QG:
  579. default:
  580. rinfo->pll.ppll_max = 35000;
  581. rinfo->pll.ppll_min = 12000;
  582. rinfo->pll.mclk = 16600;
  583. rinfo->pll.sclk = 16600;
  584. rinfo->pll.ref_clk = 2700;
  585. break;
  586. }
  587. rinfo->pll.ref_div = INPLL(PPLL_REF_DIV) & PPLL_REF_DIV_MASK;
  588. #if defined(CONFIG_PPC_OF) || defined(CONFIG_SPARC)
  589. /*
  590. * Retrieve PLL infos from Open Firmware first
  591. */
  592. if (!force_measure_pll && radeon_read_xtal_OF(rinfo) == 0) {
  593. printk(KERN_INFO "radeonfb: Retrieved PLL infos from Open Firmware\n");
  594. goto found;
  595. }
  596. #endif /* CONFIG_PPC_OF || CONFIG_SPARC */
  597. /*
  598. * Check out if we have an X86 which gave us some PLL informations
  599. * and if yes, retrieve them
  600. */
  601. if (!force_measure_pll && rinfo->bios_seg) {
  602. u16 pll_info_block = BIOS_IN16(rinfo->fp_bios_start + 0x30);
  603. rinfo->pll.sclk = BIOS_IN16(pll_info_block + 0x08);
  604. rinfo->pll.mclk = BIOS_IN16(pll_info_block + 0x0a);
  605. rinfo->pll.ref_clk = BIOS_IN16(pll_info_block + 0x0e);
  606. rinfo->pll.ref_div = BIOS_IN16(pll_info_block + 0x10);
  607. rinfo->pll.ppll_min = BIOS_IN32(pll_info_block + 0x12);
  608. rinfo->pll.ppll_max = BIOS_IN32(pll_info_block + 0x16);
  609. printk(KERN_INFO "radeonfb: Retrieved PLL infos from BIOS\n");
  610. goto found;
  611. }
  612. /*
  613. * We didn't get PLL parameters from either OF or BIOS, we try to
  614. * probe them
  615. */
  616. if (radeon_probe_pll_params(rinfo) == 0) {
  617. printk(KERN_INFO "radeonfb: Retrieved PLL infos from registers\n");
  618. goto found;
  619. }
  620. /*
  621. * Fall back to already-set defaults...
  622. */
  623. printk(KERN_INFO "radeonfb: Used default PLL infos\n");
  624. found:
  625. /*
  626. * Some methods fail to retrieve SCLK and MCLK values, we apply default
  627. * settings in this case (200Mhz). If that really happens often, we
  628. * could fetch from registers instead...
  629. */
  630. if (rinfo->pll.mclk == 0)
  631. rinfo->pll.mclk = 20000;
  632. if (rinfo->pll.sclk == 0)
  633. rinfo->pll.sclk = 20000;
  634. printk("radeonfb: Reference=%d.%02d MHz (RefDiv=%d) Memory=%d.%02d Mhz, System=%d.%02d MHz\n",
  635. rinfo->pll.ref_clk / 100, rinfo->pll.ref_clk % 100,
  636. rinfo->pll.ref_div,
  637. rinfo->pll.mclk / 100, rinfo->pll.mclk % 100,
  638. rinfo->pll.sclk / 100, rinfo->pll.sclk % 100);
  639. printk("radeonfb: PLL min %d max %d\n", rinfo->pll.ppll_min, rinfo->pll.ppll_max);
  640. }
  641. static int radeonfb_check_var (struct fb_var_screeninfo *var, struct fb_info *info)
  642. {
  643. struct radeonfb_info *rinfo = info->par;
  644. struct fb_var_screeninfo v;
  645. int nom, den;
  646. unsigned int pitch;
  647. if (radeon_match_mode(rinfo, &v, var))
  648. return -EINVAL;
  649. switch (v.bits_per_pixel) {
  650. case 0 ... 8:
  651. v.bits_per_pixel = 8;
  652. break;
  653. case 9 ... 16:
  654. v.bits_per_pixel = 16;
  655. break;
  656. case 17 ... 24:
  657. #if 0 /* Doesn't seem to work */
  658. v.bits_per_pixel = 24;
  659. break;
  660. #endif
  661. return -EINVAL;
  662. case 25 ... 32:
  663. v.bits_per_pixel = 32;
  664. break;
  665. default:
  666. return -EINVAL;
  667. }
  668. switch (var_to_depth(&v)) {
  669. case 8:
  670. nom = den = 1;
  671. v.red.offset = v.green.offset = v.blue.offset = 0;
  672. v.red.length = v.green.length = v.blue.length = 8;
  673. v.transp.offset = v.transp.length = 0;
  674. break;
  675. case 15:
  676. nom = 2;
  677. den = 1;
  678. v.red.offset = 10;
  679. v.green.offset = 5;
  680. v.blue.offset = 0;
  681. v.red.length = v.green.length = v.blue.length = 5;
  682. v.transp.offset = v.transp.length = 0;
  683. break;
  684. case 16:
  685. nom = 2;
  686. den = 1;
  687. v.red.offset = 11;
  688. v.green.offset = 5;
  689. v.blue.offset = 0;
  690. v.red.length = 5;
  691. v.green.length = 6;
  692. v.blue.length = 5;
  693. v.transp.offset = v.transp.length = 0;
  694. break;
  695. case 24:
  696. nom = 4;
  697. den = 1;
  698. v.red.offset = 16;
  699. v.green.offset = 8;
  700. v.blue.offset = 0;
  701. v.red.length = v.blue.length = v.green.length = 8;
  702. v.transp.offset = v.transp.length = 0;
  703. break;
  704. case 32:
  705. nom = 4;
  706. den = 1;
  707. v.red.offset = 16;
  708. v.green.offset = 8;
  709. v.blue.offset = 0;
  710. v.red.length = v.blue.length = v.green.length = 8;
  711. v.transp.offset = 24;
  712. v.transp.length = 8;
  713. break;
  714. default:
  715. printk ("radeonfb: mode %dx%dx%d rejected, color depth invalid\n",
  716. var->xres, var->yres, var->bits_per_pixel);
  717. return -EINVAL;
  718. }
  719. if (v.yres_virtual < v.yres)
  720. v.yres_virtual = v.yres;
  721. if (v.xres_virtual < v.xres)
  722. v.xres_virtual = v.xres;
  723. /* XXX I'm adjusting xres_virtual to the pitch, that may help XFree
  724. * with some panels, though I don't quite like this solution
  725. */
  726. if (rinfo->info->flags & FBINFO_HWACCEL_DISABLED) {
  727. v.xres_virtual = v.xres_virtual & ~7ul;
  728. } else {
  729. pitch = ((v.xres_virtual * ((v.bits_per_pixel + 1) / 8) + 0x3f)
  730. & ~(0x3f)) >> 6;
  731. v.xres_virtual = (pitch << 6) / ((v.bits_per_pixel + 1) / 8);
  732. }
  733. if (((v.xres_virtual * v.yres_virtual * nom) / den) > rinfo->mapped_vram)
  734. return -EINVAL;
  735. if (v.xres_virtual < v.xres)
  736. v.xres = v.xres_virtual;
  737. if (v.xoffset < 0)
  738. v.xoffset = 0;
  739. if (v.yoffset < 0)
  740. v.yoffset = 0;
  741. if (v.xoffset > v.xres_virtual - v.xres)
  742. v.xoffset = v.xres_virtual - v.xres - 1;
  743. if (v.yoffset > v.yres_virtual - v.yres)
  744. v.yoffset = v.yres_virtual - v.yres - 1;
  745. v.red.msb_right = v.green.msb_right = v.blue.msb_right =
  746. v.transp.offset = v.transp.length =
  747. v.transp.msb_right = 0;
  748. memcpy(var, &v, sizeof(v));
  749. return 0;
  750. }
  751. static int radeonfb_pan_display (struct fb_var_screeninfo *var,
  752. struct fb_info *info)
  753. {
  754. struct radeonfb_info *rinfo = info->par;
  755. if ((var->xoffset + var->xres > var->xres_virtual)
  756. || (var->yoffset + var->yres > var->yres_virtual))
  757. return -EINVAL;
  758. if (rinfo->asleep)
  759. return 0;
  760. radeon_fifo_wait(2);
  761. OUTREG(CRTC_OFFSET, ((var->yoffset * var->xres_virtual + var->xoffset)
  762. * var->bits_per_pixel / 8) & ~7);
  763. return 0;
  764. }
  765. static int radeonfb_ioctl (struct fb_info *info, unsigned int cmd,
  766. unsigned long arg)
  767. {
  768. struct radeonfb_info *rinfo = info->par;
  769. unsigned int tmp;
  770. u32 value = 0;
  771. int rc;
  772. switch (cmd) {
  773. /*
  774. * TODO: set mirror accordingly for non-Mobility chipsets with 2 CRTC's
  775. * and do something better using 2nd CRTC instead of just hackish
  776. * routing to second output
  777. */
  778. case FBIO_RADEON_SET_MIRROR:
  779. if (!rinfo->is_mobility)
  780. return -EINVAL;
  781. rc = get_user(value, (__u32 __user *)arg);
  782. if (rc)
  783. return rc;
  784. radeon_fifo_wait(2);
  785. if (value & 0x01) {
  786. tmp = INREG(LVDS_GEN_CNTL);
  787. tmp |= (LVDS_ON | LVDS_BLON);
  788. } else {
  789. tmp = INREG(LVDS_GEN_CNTL);
  790. tmp &= ~(LVDS_ON | LVDS_BLON);
  791. }
  792. OUTREG(LVDS_GEN_CNTL, tmp);
  793. if (value & 0x02) {
  794. tmp = INREG(CRTC_EXT_CNTL);
  795. tmp |= CRTC_CRT_ON;
  796. mirror = 1;
  797. } else {
  798. tmp = INREG(CRTC_EXT_CNTL);
  799. tmp &= ~CRTC_CRT_ON;
  800. mirror = 0;
  801. }
  802. OUTREG(CRTC_EXT_CNTL, tmp);
  803. return 0;
  804. case FBIO_RADEON_GET_MIRROR:
  805. if (!rinfo->is_mobility)
  806. return -EINVAL;
  807. tmp = INREG(LVDS_GEN_CNTL);
  808. if ((LVDS_ON | LVDS_BLON) & tmp)
  809. value |= 0x01;
  810. tmp = INREG(CRTC_EXT_CNTL);
  811. if (CRTC_CRT_ON & tmp)
  812. value |= 0x02;
  813. return put_user(value, (__u32 __user *)arg);
  814. default:
  815. return -EINVAL;
  816. }
  817. return -EINVAL;
  818. }
  819. int radeon_screen_blank(struct radeonfb_info *rinfo, int blank, int mode_switch)
  820. {
  821. u32 val;
  822. u32 tmp_pix_clks;
  823. int unblank = 0;
  824. if (rinfo->lock_blank)
  825. return 0;
  826. radeon_engine_idle();
  827. val = INREG(CRTC_EXT_CNTL);
  828. val &= ~(CRTC_DISPLAY_DIS | CRTC_HSYNC_DIS |
  829. CRTC_VSYNC_DIS);
  830. switch (blank) {
  831. case FB_BLANK_VSYNC_SUSPEND:
  832. val |= (CRTC_DISPLAY_DIS | CRTC_VSYNC_DIS);
  833. break;
  834. case FB_BLANK_HSYNC_SUSPEND:
  835. val |= (CRTC_DISPLAY_DIS | CRTC_HSYNC_DIS);
  836. break;
  837. case FB_BLANK_POWERDOWN:
  838. val |= (CRTC_DISPLAY_DIS | CRTC_VSYNC_DIS |
  839. CRTC_HSYNC_DIS);
  840. break;
  841. case FB_BLANK_NORMAL:
  842. val |= CRTC_DISPLAY_DIS;
  843. break;
  844. case FB_BLANK_UNBLANK:
  845. default:
  846. unblank = 1;
  847. }
  848. OUTREG(CRTC_EXT_CNTL, val);
  849. switch (rinfo->mon1_type) {
  850. case MT_DFP:
  851. if (unblank)
  852. OUTREGP(FP_GEN_CNTL, (FP_FPON | FP_TMDS_EN),
  853. ~(FP_FPON | FP_TMDS_EN));
  854. else {
  855. if (mode_switch || blank == FB_BLANK_NORMAL)
  856. break;
  857. OUTREGP(FP_GEN_CNTL, 0, ~(FP_FPON | FP_TMDS_EN));
  858. }
  859. break;
  860. case MT_LCD:
  861. del_timer_sync(&rinfo->lvds_timer);
  862. val = INREG(LVDS_GEN_CNTL);
  863. if (unblank) {
  864. u32 target_val = (val & ~LVDS_DISPLAY_DIS) | LVDS_BLON | LVDS_ON
  865. | LVDS_EN | (rinfo->init_state.lvds_gen_cntl
  866. & (LVDS_DIGON | LVDS_BL_MOD_EN));
  867. if ((val ^ target_val) == LVDS_DISPLAY_DIS)
  868. OUTREG(LVDS_GEN_CNTL, target_val);
  869. else if ((val ^ target_val) != 0) {
  870. OUTREG(LVDS_GEN_CNTL, target_val
  871. & ~(LVDS_ON | LVDS_BL_MOD_EN));
  872. rinfo->init_state.lvds_gen_cntl &= ~LVDS_STATE_MASK;
  873. rinfo->init_state.lvds_gen_cntl |=
  874. target_val & LVDS_STATE_MASK;
  875. if (mode_switch) {
  876. radeon_msleep(rinfo->panel_info.pwr_delay);
  877. OUTREG(LVDS_GEN_CNTL, target_val);
  878. }
  879. else {
  880. rinfo->pending_lvds_gen_cntl = target_val;
  881. mod_timer(&rinfo->lvds_timer,
  882. jiffies +
  883. msecs_to_jiffies(rinfo->panel_info.pwr_delay));
  884. }
  885. }
  886. } else {
  887. val |= LVDS_DISPLAY_DIS;
  888. OUTREG(LVDS_GEN_CNTL, val);
  889. /* We don't do a full switch-off on a simple mode switch */
  890. if (mode_switch || blank == FB_BLANK_NORMAL)
  891. break;
  892. /* Asic bug, when turning off LVDS_ON, we have to make sure
  893. * RADEON_PIXCLK_LVDS_ALWAYS_ON bit is off
  894. */
  895. tmp_pix_clks = INPLL(PIXCLKS_CNTL);
  896. if (rinfo->is_mobility || rinfo->is_IGP)
  897. OUTPLLP(PIXCLKS_CNTL, 0, ~PIXCLK_LVDS_ALWAYS_ONb);
  898. val &= ~(LVDS_BL_MOD_EN);
  899. OUTREG(LVDS_GEN_CNTL, val);
  900. udelay(100);
  901. val &= ~(LVDS_ON | LVDS_EN);
  902. OUTREG(LVDS_GEN_CNTL, val);
  903. val &= ~LVDS_DIGON;
  904. rinfo->pending_lvds_gen_cntl = val;
  905. mod_timer(&rinfo->lvds_timer,
  906. jiffies +
  907. msecs_to_jiffies(rinfo->panel_info.pwr_delay));
  908. rinfo->init_state.lvds_gen_cntl &= ~LVDS_STATE_MASK;
  909. rinfo->init_state.lvds_gen_cntl |= val & LVDS_STATE_MASK;
  910. if (rinfo->is_mobility || rinfo->is_IGP)
  911. OUTPLL(PIXCLKS_CNTL, tmp_pix_clks);
  912. }
  913. break;
  914. case MT_CRT:
  915. // todo: powerdown DAC
  916. default:
  917. break;
  918. }
  919. return 0;
  920. }
  921. static int radeonfb_blank (int blank, struct fb_info *info)
  922. {
  923. struct radeonfb_info *rinfo = info->par;
  924. if (rinfo->asleep)
  925. return 0;
  926. return radeon_screen_blank(rinfo, blank, 0);
  927. }
  928. static int radeon_setcolreg (unsigned regno, unsigned red, unsigned green,
  929. unsigned blue, unsigned transp,
  930. struct radeonfb_info *rinfo)
  931. {
  932. u32 pindex;
  933. unsigned int i;
  934. if (regno > 255)
  935. return -EINVAL;
  936. red >>= 8;
  937. green >>= 8;
  938. blue >>= 8;
  939. rinfo->palette[regno].red = red;
  940. rinfo->palette[regno].green = green;
  941. rinfo->palette[regno].blue = blue;
  942. /* default */
  943. pindex = regno;
  944. if (!rinfo->asleep) {
  945. radeon_fifo_wait(9);
  946. if (rinfo->bpp == 16) {
  947. pindex = regno * 8;
  948. if (rinfo->depth == 16 && regno > 63)
  949. return -EINVAL;
  950. if (rinfo->depth == 15 && regno > 31)
  951. return -EINVAL;
  952. /* For 565, the green component is mixed one order
  953. * below
  954. */
  955. if (rinfo->depth == 16) {
  956. OUTREG(PALETTE_INDEX, pindex>>1);
  957. OUTREG(PALETTE_DATA,
  958. (rinfo->palette[regno>>1].red << 16) |
  959. (green << 8) |
  960. (rinfo->palette[regno>>1].blue));
  961. green = rinfo->palette[regno<<1].green;
  962. }
  963. }
  964. if (rinfo->depth != 16 || regno < 32) {
  965. OUTREG(PALETTE_INDEX, pindex);
  966. OUTREG(PALETTE_DATA, (red << 16) |
  967. (green << 8) | blue);
  968. }
  969. }
  970. if (regno < 16) {
  971. u32 *pal = rinfo->info->pseudo_palette;
  972. switch (rinfo->depth) {
  973. case 15:
  974. pal[regno] = (regno << 10) | (regno << 5) | regno;
  975. break;
  976. case 16:
  977. pal[regno] = (regno << 11) | (regno << 5) | regno;
  978. break;
  979. case 24:
  980. pal[regno] = (regno << 16) | (regno << 8) | regno;
  981. break;
  982. case 32:
  983. i = (regno << 8) | regno;
  984. pal[regno] = (i << 16) | i;
  985. break;
  986. }
  987. }
  988. return 0;
  989. }
  990. static int radeonfb_setcolreg (unsigned regno, unsigned red, unsigned green,
  991. unsigned blue, unsigned transp,
  992. struct fb_info *info)
  993. {
  994. struct radeonfb_info *rinfo = info->par;
  995. u32 dac_cntl2, vclk_cntl = 0;
  996. int rc;
  997. if (!rinfo->asleep) {
  998. if (rinfo->is_mobility) {
  999. vclk_cntl = INPLL(VCLK_ECP_CNTL);
  1000. OUTPLL(VCLK_ECP_CNTL,
  1001. vclk_cntl & ~PIXCLK_DAC_ALWAYS_ONb);
  1002. }
  1003. /* Make sure we are on first palette */
  1004. if (rinfo->has_CRTC2) {
  1005. dac_cntl2 = INREG(DAC_CNTL2);
  1006. dac_cntl2 &= ~DAC2_PALETTE_ACCESS_CNTL;
  1007. OUTREG(DAC_CNTL2, dac_cntl2);
  1008. }
  1009. }
  1010. rc = radeon_setcolreg (regno, red, green, blue, transp, rinfo);
  1011. if (!rinfo->asleep && rinfo->is_mobility)
  1012. OUTPLL(VCLK_ECP_CNTL, vclk_cntl);
  1013. return rc;
  1014. }
  1015. static int radeonfb_setcmap(struct fb_cmap *cmap, struct fb_info *info)
  1016. {
  1017. struct radeonfb_info *rinfo = info->par;
  1018. u16 *red, *green, *blue, *transp;
  1019. u32 dac_cntl2, vclk_cntl = 0;
  1020. int i, start, rc = 0;
  1021. if (!rinfo->asleep) {
  1022. if (rinfo->is_mobility) {
  1023. vclk_cntl = INPLL(VCLK_ECP_CNTL);
  1024. OUTPLL(VCLK_ECP_CNTL,
  1025. vclk_cntl & ~PIXCLK_DAC_ALWAYS_ONb);
  1026. }
  1027. /* Make sure we are on first palette */
  1028. if (rinfo->has_CRTC2) {
  1029. dac_cntl2 = INREG(DAC_CNTL2);
  1030. dac_cntl2 &= ~DAC2_PALETTE_ACCESS_CNTL;
  1031. OUTREG(DAC_CNTL2, dac_cntl2);
  1032. }
  1033. }
  1034. red = cmap->red;
  1035. green = cmap->green;
  1036. blue = cmap->blue;
  1037. transp = cmap->transp;
  1038. start = cmap->start;
  1039. for (i = 0; i < cmap->len; i++) {
  1040. u_int hred, hgreen, hblue, htransp = 0xffff;
  1041. hred = *red++;
  1042. hgreen = *green++;
  1043. hblue = *blue++;
  1044. if (transp)
  1045. htransp = *transp++;
  1046. rc = radeon_setcolreg (start++, hred, hgreen, hblue, htransp,
  1047. rinfo);
  1048. if (rc)
  1049. break;
  1050. }
  1051. if (!rinfo->asleep && rinfo->is_mobility)
  1052. OUTPLL(VCLK_ECP_CNTL, vclk_cntl);
  1053. return rc;
  1054. }
  1055. static void radeon_save_state (struct radeonfb_info *rinfo,
  1056. struct radeon_regs *save)
  1057. {
  1058. /* CRTC regs */
  1059. save->crtc_gen_cntl = INREG(CRTC_GEN_CNTL);
  1060. save->crtc_ext_cntl = INREG(CRTC_EXT_CNTL);
  1061. save->crtc_more_cntl = INREG(CRTC_MORE_CNTL);
  1062. save->dac_cntl = INREG(DAC_CNTL);
  1063. save->crtc_h_total_disp = INREG(CRTC_H_TOTAL_DISP);
  1064. save->crtc_h_sync_strt_wid = INREG(CRTC_H_SYNC_STRT_WID);
  1065. save->crtc_v_total_disp = INREG(CRTC_V_TOTAL_DISP);
  1066. save->crtc_v_sync_strt_wid = INREG(CRTC_V_SYNC_STRT_WID);
  1067. save->crtc_pitch = INREG(CRTC_PITCH);
  1068. save->surface_cntl = INREG(SURFACE_CNTL);
  1069. /* FP regs */
  1070. save->fp_crtc_h_total_disp = INREG(FP_CRTC_H_TOTAL_DISP);
  1071. save->fp_crtc_v_total_disp = INREG(FP_CRTC_V_TOTAL_DISP);
  1072. save->fp_gen_cntl = INREG(FP_GEN_CNTL);
  1073. save->fp_h_sync_strt_wid = INREG(FP_H_SYNC_STRT_WID);
  1074. save->fp_horz_stretch = INREG(FP_HORZ_STRETCH);
  1075. save->fp_v_sync_strt_wid = INREG(FP_V_SYNC_STRT_WID);
  1076. save->fp_vert_stretch = INREG(FP_VERT_STRETCH);
  1077. save->lvds_gen_cntl = INREG(LVDS_GEN_CNTL);
  1078. save->lvds_pll_cntl = INREG(LVDS_PLL_CNTL);
  1079. save->tmds_crc = INREG(TMDS_CRC);
  1080. save->tmds_transmitter_cntl = INREG(TMDS_TRANSMITTER_CNTL);
  1081. save->vclk_ecp_cntl = INPLL(VCLK_ECP_CNTL);
  1082. /* PLL regs */
  1083. save->clk_cntl_index = INREG(CLOCK_CNTL_INDEX) & ~0x3f;
  1084. radeon_pll_errata_after_index(rinfo);
  1085. save->ppll_div_3 = INPLL(PPLL_DIV_3);
  1086. save->ppll_ref_div = INPLL(PPLL_REF_DIV);
  1087. }
  1088. static void radeon_write_pll_regs(struct radeonfb_info *rinfo, struct radeon_regs *mode)
  1089. {
  1090. int i;
  1091. radeon_fifo_wait(20);
  1092. /* Workaround from XFree */
  1093. if (rinfo->is_mobility) {
  1094. /* A temporal workaround for the occasional blanking on certain laptop
  1095. * panels. This appears to related to the PLL divider registers
  1096. * (fail to lock?). It occurs even when all dividers are the same
  1097. * with their old settings. In this case we really don't need to
  1098. * fiddle with PLL registers. By doing this we can avoid the blanking
  1099. * problem with some panels.
  1100. */
  1101. if ((mode->ppll_ref_div == (INPLL(PPLL_REF_DIV) & PPLL_REF_DIV_MASK)) &&
  1102. (mode->ppll_div_3 == (INPLL(PPLL_DIV_3) &
  1103. (PPLL_POST3_DIV_MASK | PPLL_FB3_DIV_MASK)))) {
  1104. /* We still have to force a switch to selected PPLL div thanks to
  1105. * an XFree86 driver bug which will switch it away in some cases
  1106. * even when using UseFDev */
  1107. OUTREGP(CLOCK_CNTL_INDEX,
  1108. mode->clk_cntl_index & PPLL_DIV_SEL_MASK,
  1109. ~PPLL_DIV_SEL_MASK);
  1110. radeon_pll_errata_after_index(rinfo);
  1111. radeon_pll_errata_after_data(rinfo);
  1112. return;
  1113. }
  1114. }
  1115. /* Swich VCKL clock input to CPUCLK so it stays fed while PPLL updates*/
  1116. OUTPLLP(VCLK_ECP_CNTL, VCLK_SRC_SEL_CPUCLK, ~VCLK_SRC_SEL_MASK);
  1117. /* Reset PPLL & enable atomic update */
  1118. OUTPLLP(PPLL_CNTL,
  1119. PPLL_RESET | PPLL_ATOMIC_UPDATE_EN | PPLL_VGA_ATOMIC_UPDATE_EN,
  1120. ~(PPLL_RESET | PPLL_ATOMIC_UPDATE_EN | PPLL_VGA_ATOMIC_UPDATE_EN));
  1121. /* Switch to selected PPLL divider */
  1122. OUTREGP(CLOCK_CNTL_INDEX,
  1123. mode->clk_cntl_index & PPLL_DIV_SEL_MASK,
  1124. ~PPLL_DIV_SEL_MASK);
  1125. radeon_pll_errata_after_index(rinfo);
  1126. radeon_pll_errata_after_data(rinfo);
  1127. /* Set PPLL ref. div */
  1128. if (IS_R300_VARIANT(rinfo) ||
  1129. rinfo->family == CHIP_FAMILY_RS300 ||
  1130. rinfo->family == CHIP_FAMILY_RS400 ||
  1131. rinfo->family == CHIP_FAMILY_RS480) {
  1132. if (mode->ppll_ref_div & R300_PPLL_REF_DIV_ACC_MASK) {
  1133. /* When restoring console mode, use saved PPLL_REF_DIV
  1134. * setting.
  1135. */
  1136. OUTPLLP(PPLL_REF_DIV, mode->ppll_ref_div, 0);
  1137. } else {
  1138. /* R300 uses ref_div_acc field as real ref divider */
  1139. OUTPLLP(PPLL_REF_DIV,
  1140. (mode->ppll_ref_div << R300_PPLL_REF_DIV_ACC_SHIFT),
  1141. ~R300_PPLL_REF_DIV_ACC_MASK);
  1142. }
  1143. } else
  1144. OUTPLLP(PPLL_REF_DIV, mode->ppll_ref_div, ~PPLL_REF_DIV_MASK);
  1145. /* Set PPLL divider 3 & post divider*/
  1146. OUTPLLP(PPLL_DIV_3, mode->ppll_div_3, ~PPLL_FB3_DIV_MASK);
  1147. OUTPLLP(PPLL_DIV_3, mode->ppll_div_3, ~PPLL_POST3_DIV_MASK);
  1148. /* Write update */
  1149. while (INPLL(PPLL_REF_DIV) & PPLL_ATOMIC_UPDATE_R)
  1150. ;
  1151. OUTPLLP(PPLL_REF_DIV, PPLL_ATOMIC_UPDATE_W, ~PPLL_ATOMIC_UPDATE_W);
  1152. /* Wait read update complete */
  1153. /* FIXME: Certain revisions of R300 can't recover here. Not sure of
  1154. the cause yet, but this workaround will mask the problem for now.
  1155. Other chips usually will pass at the very first test, so the
  1156. workaround shouldn't have any effect on them. */
  1157. for (i = 0; (i < 10000 && INPLL(PPLL_REF_DIV) & PPLL_ATOMIC_UPDATE_R); i++)
  1158. ;
  1159. OUTPLL(HTOTAL_CNTL, 0);
  1160. /* Clear reset & atomic update */
  1161. OUTPLLP(PPLL_CNTL, 0,
  1162. ~(PPLL_RESET | PPLL_SLEEP | PPLL_ATOMIC_UPDATE_EN | PPLL_VGA_ATOMIC_UPDATE_EN));
  1163. /* We may want some locking ... oh well */
  1164. radeon_msleep(5);
  1165. /* Switch back VCLK source to PPLL */
  1166. OUTPLLP(VCLK_ECP_CNTL, VCLK_SRC_SEL_PPLLCLK, ~VCLK_SRC_SEL_MASK);
  1167. }
  1168. /*
  1169. * Timer function for delayed LVDS panel power up/down
  1170. */
  1171. static void radeon_lvds_timer_func(unsigned long data)
  1172. {
  1173. struct radeonfb_info *rinfo = (struct radeonfb_info *)data;
  1174. radeon_engine_idle();
  1175. OUTREG(LVDS_GEN_CNTL, rinfo->pending_lvds_gen_cntl);
  1176. }
  1177. /*
  1178. * Apply a video mode. This will apply the whole register set, including
  1179. * the PLL registers, to the card
  1180. */
  1181. void radeon_write_mode (struct radeonfb_info *rinfo, struct radeon_regs *mode,
  1182. int regs_only)
  1183. {
  1184. int i;
  1185. int primary_mon = PRIMARY_MONITOR(rinfo);
  1186. if (nomodeset)
  1187. return;
  1188. if (!regs_only)
  1189. radeon_screen_blank(rinfo, FB_BLANK_NORMAL, 0);
  1190. radeon_fifo_wait(31);
  1191. for (i=0; i<10; i++)
  1192. OUTREG(common_regs[i].reg, common_regs[i].val);
  1193. /* Apply surface registers */
  1194. for (i=0; i<8; i++) {
  1195. OUTREG(SURFACE0_LOWER_BOUND + 0x10*i, mode->surf_lower_bound[i]);
  1196. OUTREG(SURFACE0_UPPER_BOUND + 0x10*i, mode->surf_upper_bound[i]);
  1197. OUTREG(SURFACE0_INFO + 0x10*i, mode->surf_info[i]);
  1198. }
  1199. OUTREG(CRTC_GEN_CNTL, mode->crtc_gen_cntl);
  1200. OUTREGP(CRTC_EXT_CNTL, mode->crtc_ext_cntl,
  1201. ~(CRTC_HSYNC_DIS | CRTC_VSYNC_DIS | CRTC_DISPLAY_DIS));
  1202. OUTREG(CRTC_MORE_CNTL, mode->crtc_more_cntl);
  1203. OUTREGP(DAC_CNTL, mode->dac_cntl, DAC_RANGE_CNTL | DAC_BLANKING);
  1204. OUTREG(CRTC_H_TOTAL_DISP, mode->crtc_h_total_disp);
  1205. OUTREG(CRTC_H_SYNC_STRT_WID, mode->crtc_h_sync_strt_wid);
  1206. OUTREG(CRTC_V_TOTAL_DISP, mode->crtc_v_total_disp);
  1207. OUTREG(CRTC_V_SYNC_STRT_WID, mode->crtc_v_sync_strt_wid);
  1208. OUTREG(CRTC_OFFSET, 0);
  1209. OUTREG(CRTC_OFFSET_CNTL, 0);
  1210. OUTREG(CRTC_PITCH, mode->crtc_pitch);
  1211. OUTREG(SURFACE_CNTL, mode->surface_cntl);
  1212. radeon_write_pll_regs(rinfo, mode);
  1213. if ((primary_mon == MT_DFP) || (primary_mon == MT_LCD)) {
  1214. radeon_fifo_wait(10);
  1215. OUTREG(FP_CRTC_H_TOTAL_DISP, mode->fp_crtc_h_total_disp);
  1216. OUTREG(FP_CRTC_V_TOTAL_DISP, mode->fp_crtc_v_total_disp);
  1217. OUTREG(FP_H_SYNC_STRT_WID, mode->fp_h_sync_strt_wid);
  1218. OUTREG(FP_V_SYNC_STRT_WID, mode->fp_v_sync_strt_wid);
  1219. OUTREG(FP_HORZ_STRETCH, mode->fp_horz_stretch);
  1220. OUTREG(FP_VERT_STRETCH, mode->fp_vert_stretch);
  1221. OUTREG(FP_GEN_CNTL, mode->fp_gen_cntl);
  1222. OUTREG(TMDS_CRC, mode->tmds_crc);
  1223. OUTREG(TMDS_TRANSMITTER_CNTL, mode->tmds_transmitter_cntl);
  1224. }
  1225. if (!regs_only)
  1226. radeon_screen_blank(rinfo, FB_BLANK_UNBLANK, 0);
  1227. radeon_fifo_wait(2);
  1228. OUTPLL(VCLK_ECP_CNTL, mode->vclk_ecp_cntl);
  1229. return;
  1230. }
  1231. /*
  1232. * Calculate the PLL values for a given mode
  1233. */
  1234. static void radeon_calc_pll_regs(struct radeonfb_info *rinfo, struct radeon_regs *regs,
  1235. unsigned long freq)
  1236. {
  1237. const struct {
  1238. int divider;
  1239. int bitvalue;
  1240. } *post_div,
  1241. post_divs[] = {
  1242. { 1, 0 },
  1243. { 2, 1 },
  1244. { 4, 2 },
  1245. { 8, 3 },
  1246. { 3, 4 },
  1247. { 16, 5 },
  1248. { 6, 6 },
  1249. { 12, 7 },
  1250. { 0, 0 },
  1251. };
  1252. int fb_div, pll_output_freq = 0;
  1253. int uses_dvo = 0;
  1254. /* Check if the DVO port is enabled and sourced from the primary CRTC. I'm
  1255. * not sure which model starts having FP2_GEN_CNTL, I assume anything more
  1256. * recent than an r(v)100...
  1257. */
  1258. #if 1
  1259. /* XXX I had reports of flicker happening with the cinema display
  1260. * on TMDS1 that seem to be fixed if I also forbit odd dividers in
  1261. * this case. This could just be a bandwidth calculation issue, I
  1262. * haven't implemented the bandwidth code yet, but in the meantime,
  1263. * forcing uses_dvo to 1 fixes it and shouln't have bad side effects,
  1264. * I haven't seen a case were were absolutely needed an odd PLL
  1265. * divider. I'll find a better fix once I have more infos on the
  1266. * real cause of the problem.
  1267. */
  1268. while (rinfo->has_CRTC2) {
  1269. u32 fp2_gen_cntl = INREG(FP2_GEN_CNTL);
  1270. u32 disp_output_cntl;
  1271. int source;
  1272. /* FP2 path not enabled */
  1273. if ((fp2_gen_cntl & FP2_ON) == 0)
  1274. break;
  1275. /* Not all chip revs have the same format for this register,
  1276. * extract the source selection
  1277. */
  1278. if (rinfo->family == CHIP_FAMILY_R200 || IS_R300_VARIANT(rinfo)) {
  1279. source = (fp2_gen_cntl >> 10) & 0x3;
  1280. /* sourced from transform unit, check for transform unit
  1281. * own source
  1282. */
  1283. if (source == 3) {
  1284. disp_output_cntl = INREG(DISP_OUTPUT_CNTL);
  1285. source = (disp_output_cntl >> 12) & 0x3;
  1286. }
  1287. } else
  1288. source = (fp2_gen_cntl >> 13) & 0x1;
  1289. /* sourced from CRTC2 -> exit */
  1290. if (source == 1)
  1291. break;
  1292. /* so we end up on CRTC1, let's set uses_dvo to 1 now */
  1293. uses_dvo = 1;
  1294. break;
  1295. }
  1296. #else
  1297. uses_dvo = 1;
  1298. #endif
  1299. if (freq > rinfo->pll.ppll_max)
  1300. freq = rinfo->pll.ppll_max;
  1301. if (freq*12 < rinfo->pll.ppll_min)
  1302. freq = rinfo->pll.ppll_min / 12;
  1303. pr_debug("freq = %lu, PLL min = %u, PLL max = %u\n",
  1304. freq, rinfo->pll.ppll_min, rinfo->pll.ppll_max);
  1305. for (post_div = &post_divs[0]; post_div->divider; ++post_div) {
  1306. pll_output_freq = post_div->divider * freq;
  1307. /* If we output to the DVO port (external TMDS), we don't allow an
  1308. * odd PLL divider as those aren't supported on this path
  1309. */
  1310. if (uses_dvo && (post_div->divider & 1))
  1311. continue;
  1312. if (pll_output_freq >= rinfo->pll.ppll_min &&
  1313. pll_output_freq <= rinfo->pll.ppll_max)
  1314. break;
  1315. }
  1316. /* If we fall through the bottom, try the "default value"
  1317. given by the terminal post_div->bitvalue */
  1318. if ( !post_div->divider ) {
  1319. post_div = &post_divs[post_div->bitvalue];
  1320. pll_output_freq = post_div->divider * freq;
  1321. }
  1322. pr_debug("ref_div = %d, ref_clk = %d, output_freq = %d\n",
  1323. rinfo->pll.ref_div, rinfo->pll.ref_clk,
  1324. pll_output_freq);
  1325. /* If we fall through the bottom, try the "default value"
  1326. given by the terminal post_div->bitvalue */
  1327. if ( !post_div->divider ) {
  1328. post_div = &post_divs[post_div->bitvalue];
  1329. pll_output_freq = post_div->divider * freq;
  1330. }
  1331. pr_debug("ref_div = %d, ref_clk = %d, output_freq = %d\n",
  1332. rinfo->pll.ref_div, rinfo->pll.ref_clk,
  1333. pll_output_freq);
  1334. fb_div = round_div(rinfo->pll.ref_div*pll_output_freq,
  1335. rinfo->pll.ref_clk);
  1336. regs->ppll_ref_div = rinfo->pll.ref_div;
  1337. regs->ppll_div_3 = fb_div | (post_div->bitvalue << 16);
  1338. pr_debug("post div = 0x%x\n", post_div->bitvalue);
  1339. pr_debug("fb_div = 0x%x\n", fb_div);
  1340. pr_debug("ppll_div_3 = 0x%x\n", regs->ppll_div_3);
  1341. }
  1342. static int radeonfb_set_par(struct fb_info *info)
  1343. {
  1344. struct radeonfb_info *rinfo = info->par;
  1345. struct fb_var_screeninfo *mode = &info->var;
  1346. struct radeon_regs *newmode;
  1347. int hTotal, vTotal, hSyncStart, hSyncEnd,
  1348. hSyncPol, vSyncStart, vSyncEnd, vSyncPol, cSync;
  1349. u8 hsync_adj_tab[] = {0, 0x12, 9, 9, 6, 5};
  1350. u8 hsync_fudge_fp[] = {2, 2, 0, 0, 5, 5};
  1351. u32 sync, h_sync_pol, v_sync_pol, dotClock, pixClock;
  1352. int i, freq;
  1353. int format = 0;
  1354. int nopllcalc = 0;
  1355. int hsync_start, hsync_fudge, bytpp, hsync_wid, vsync_wid;
  1356. int primary_mon = PRIMARY_MONITOR(rinfo);
  1357. int depth = var_to_depth(mode);
  1358. int use_rmx = 0;
  1359. newmode = kmalloc(sizeof(struct radeon_regs), GFP_KERNEL);
  1360. if (!newmode)
  1361. return -ENOMEM;
  1362. /* We always want engine to be idle on a mode switch, even
  1363. * if we won't actually change the mode
  1364. */
  1365. radeon_engine_idle();
  1366. hSyncStart = mode->xres + mode->right_margin;
  1367. hSyncEnd = hSyncStart + mode->hsync_len;
  1368. hTotal = hSyncEnd + mode->left_margin;
  1369. vSyncStart = mode->yres + mode->lower_margin;
  1370. vSyncEnd = vSyncStart + mode->vsync_len;
  1371. vTotal = vSyncEnd + mode->upper_margin;
  1372. pixClock = mode->pixclock;
  1373. sync = mode->sync;
  1374. h_sync_pol = sync & FB_SYNC_HOR_HIGH_ACT ? 0 : 1;
  1375. v_sync_pol = sync & FB_SYNC_VERT_HIGH_ACT ? 0 : 1;
  1376. if (primary_mon == MT_DFP || primary_mon == MT_LCD) {
  1377. if (rinfo->panel_info.xres < mode->xres)
  1378. mode->xres = rinfo->panel_info.xres;
  1379. if (rinfo->panel_info.yres < mode->yres)
  1380. mode->yres = rinfo->panel_info.yres;
  1381. hTotal = mode->xres + rinfo->panel_info.hblank;
  1382. hSyncStart = mode->xres + rinfo->panel_info.hOver_plus;
  1383. hSyncEnd = hSyncStart + rinfo->panel_info.hSync_width;
  1384. vTotal = mode->yres + rinfo->panel_info.vblank;
  1385. vSyncStart = mode->yres + rinfo->panel_info.vOver_plus;
  1386. vSyncEnd = vSyncStart + rinfo->panel_info.vSync_width;
  1387. h_sync_pol = !rinfo->panel_info.hAct_high;
  1388. v_sync_pol = !rinfo->panel_info.vAct_high;
  1389. pixClock = 100000000 / rinfo->panel_info.clock;
  1390. if (rinfo->panel_info.use_bios_dividers) {
  1391. nopllcalc = 1;
  1392. newmode->ppll_div_3 = rinfo->panel_info.fbk_divider |
  1393. (rinfo->panel_info.post_divider << 16);
  1394. newmode->ppll_ref_div = rinfo->panel_info.ref_divider;
  1395. }
  1396. }
  1397. dotClock = 1000000000 / pixClock;
  1398. freq = dotClock / 10; /* x100 */
  1399. pr_debug("hStart = %d, hEnd = %d, hTotal = %d\n",
  1400. hSyncStart, hSyncEnd, hTotal);
  1401. pr_debug("vStart = %d, vEnd = %d, vTotal = %d\n",
  1402. vSyncStart, vSyncEnd, vTotal);
  1403. hsync_wid = (hSyncEnd - hSyncStart) / 8;
  1404. vsync_wid = vSyncEnd - vSyncStart;
  1405. if (hsync_wid == 0)
  1406. hsync_wid = 1;
  1407. else if (hsync_wid > 0x3f) /* max */
  1408. hsync_wid = 0x3f;
  1409. if (vsync_wid == 0)
  1410. vsync_wid = 1;
  1411. else if (vsync_wid > 0x1f) /* max */
  1412. vsync_wid = 0x1f;
  1413. hSyncPol = mode->sync & FB_SYNC_HOR_HIGH_ACT ? 0 : 1;
  1414. vSyncPol = mode->sync & FB_SYNC_VERT_HIGH_ACT ? 0 : 1;
  1415. cSync = mode->sync & FB_SYNC_COMP_HIGH_ACT ? (1 << 4) : 0;
  1416. format = radeon_get_dstbpp(depth);
  1417. bytpp = mode->bits_per_pixel >> 3;
  1418. if ((primary_mon == MT_DFP) || (primary_mon == MT_LCD))
  1419. hsync_fudge = hsync_fudge_fp[format-1];
  1420. else
  1421. hsync_fudge = hsync_adj_tab[format-1];
  1422. hsync_start = hSyncStart - 8 + hsync_fudge;
  1423. newmode->crtc_gen_cntl = CRTC_EXT_DISP_EN | CRTC_EN |
  1424. (format << 8);
  1425. /* Clear auto-center etc... */
  1426. newmode->crtc_more_cntl = rinfo->init_state.crtc_more_cntl;
  1427. newmode->crtc_more_cntl &= 0xfffffff0;
  1428. if ((primary_mon == MT_DFP) || (primary_mon == MT_LCD)) {
  1429. newmode->crtc_ext_cntl = VGA_ATI_LINEAR | XCRT_CNT_EN;
  1430. if (mirror)
  1431. newmode->crtc_ext_cntl |= CRTC_CRT_ON;
  1432. newmode->crtc_gen_cntl &= ~(CRTC_DBL_SCAN_EN |
  1433. CRTC_INTERLACE_EN);
  1434. } else {
  1435. newmode->crtc_ext_cntl = VGA_ATI_LINEAR | XCRT_CNT_EN |
  1436. CRTC_CRT_ON;
  1437. }
  1438. newmode->dac_cntl = /* INREG(DAC_CNTL) | */ DAC_MASK_ALL | DAC_VGA_ADR_EN |
  1439. DAC_8BIT_EN;
  1440. newmode->crtc_h_total_disp = ((((hTotal / 8) - 1) & 0x3ff) |
  1441. (((mode->xres / 8) - 1) << 16));
  1442. newmode->crtc_h_sync_strt_wid = ((hsync_start & 0x1fff) |
  1443. (hsync_wid << 16) | (h_sync_pol << 23));
  1444. newmode->crtc_v_total_disp = ((vTotal - 1) & 0xffff) |
  1445. ((mode->yres - 1) << 16);
  1446. newmode->crtc_v_sync_strt_wid = (((vSyncStart - 1) & 0xfff) |
  1447. (vsync_wid << 16) | (v_sync_pol << 23));
  1448. if (!(info->flags & FBINFO_HWACCEL_DISABLED)) {
  1449. /* We first calculate the engine pitch */
  1450. rinfo->pitch = ((mode->xres_virtual * ((mode->bits_per_pixel + 1) / 8) + 0x3f)
  1451. & ~(0x3f)) >> 6;
  1452. /* Then, re-multiply it to get the CRTC pitch */
  1453. newmode->crtc_pitch = (rinfo->pitch << 3) / ((mode->bits_per_pixel + 1) / 8);
  1454. } else
  1455. newmode->cr