ati_radeon_fb.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * ATI Radeon Video card Framebuffer driver.
  4. *
  5. * Copyright 2007 Freescale Semiconductor, Inc.
  6. * Zhang Wei <wei.zhang@freescale.com>
  7. * Jason Jin <jason.jin@freescale.com>
  8. *
  9. * Some codes of this file is partly ported from Linux kernel
  10. * ATI video framebuffer driver.
  11. *
  12. * Now the driver is tested on below ATI chips:
  13. * 9200
  14. * X300
  15. * X700
  16. */
  17. #include <common.h>
  18. #include <linux/delay.h>
  19. #include <command.h>
  20. #include <bios_emul.h>
  21. #include <env.h>
  22. #include <pci.h>
  23. #include <asm/processor.h>
  24. #include <linux/errno.h>
  25. #include <asm/io.h>
  26. #include <malloc.h>
  27. #include <video_fb.h>
  28. #include "videomodes.h"
  29. #include <radeon.h>
  30. #include "ati_ids.h"
  31. #include "ati_radeon_fb.h"
  32. #undef DEBUG
  33. #ifdef DEBUG
  34. #define DPRINT(x...) printf(x)
  35. #else
  36. #define DPRINT(x...) do{}while(0)
  37. #endif
  38. #define MAX_MAPPED_VRAM (2048*2048*4)
  39. #define MIN_MAPPED_VRAM (1024*768*1)
  40. #define RADEON_BUFFER_ALIGN 0x00000fff
  41. #define SURF_UPPER_BOUND(x,y,bpp) (((((x) * (((y) + 15) & ~15) * (bpp)/8) + RADEON_BUFFER_ALIGN) \
  42. & ~RADEON_BUFFER_ALIGN) - 1)
  43. #define RADEON_CRT_PITCH(width, bpp) ((((width) * (bpp) + ((bpp) * 8 - 1)) / ((bpp) * 8)) | \
  44. ((((width) * (bpp) + ((bpp) * 8 - 1)) / ((bpp) * 8)) << 16))
  45. #define CRTC_H_TOTAL_DISP_VAL(htotal, hdisp) \
  46. (((((htotal) / 8) - 1) & 0x3ff) | (((((hdisp) / 8) - 1) & 0x1ff) << 16))
  47. #define CRTC_HSYNC_STRT_WID_VAL(hsync_srtr, hsync_wid) \
  48. (((hsync_srtr) & 0x1fff) | (((hsync_wid) & 0x3f) << 16))
  49. #define CRTC_V_TOTAL_DISP_VAL(vtotal, vdisp) \
  50. ((((vtotal) - 1) & 0xffff) | (((vdisp) - 1) << 16))
  51. #define CRTC_VSYNC_STRT_WID_VAL(vsync_srtr, vsync_wid) \
  52. ((((vsync_srtr) - 1) & 0xfff) | (((vsync_wid) & 0x1f) << 16))
  53. /*#define PCI_VENDOR_ID_ATI*/
  54. #define PCI_CHIP_RV280_5960 0x5960
  55. #define PCI_CHIP_RV280_5961 0x5961
  56. #define PCI_CHIP_RV280_5962 0x5962
  57. #define PCI_CHIP_RV280_5964 0x5964
  58. #define PCI_CHIP_RV280_5C63 0x5C63
  59. #define PCI_CHIP_RV370_5B60 0x5B60
  60. #define PCI_CHIP_RV380_5657 0x5657
  61. #define PCI_CHIP_R420_554d 0x554d
  62. static struct pci_device_id ati_radeon_pci_ids[] = {
  63. {PCI_VENDOR_ID_ATI, PCI_CHIP_RV280_5960},
  64. {PCI_VENDOR_ID_ATI, PCI_CHIP_RV280_5961},
  65. {PCI_VENDOR_ID_ATI, PCI_CHIP_RV280_5962},
  66. {PCI_VENDOR_ID_ATI, PCI_CHIP_RV280_5964},
  67. {PCI_VENDOR_ID_ATI, PCI_CHIP_RV280_5C63},
  68. {PCI_VENDOR_ID_ATI, PCI_CHIP_RV370_5B60},
  69. {PCI_VENDOR_ID_ATI, PCI_CHIP_RV380_5657},
  70. {PCI_VENDOR_ID_ATI, PCI_CHIP_R420_554d},
  71. {0, 0}
  72. };
  73. static u16 ati_radeon_id_family_table[][2] = {
  74. {PCI_CHIP_RV280_5960, CHIP_FAMILY_RV280},
  75. {PCI_CHIP_RV280_5961, CHIP_FAMILY_RV280},
  76. {PCI_CHIP_RV280_5962, CHIP_FAMILY_RV280},
  77. {PCI_CHIP_RV280_5964, CHIP_FAMILY_RV280},
  78. {PCI_CHIP_RV280_5C63, CHIP_FAMILY_RV280},
  79. {PCI_CHIP_RV370_5B60, CHIP_FAMILY_RV380},
  80. {PCI_CHIP_RV380_5657, CHIP_FAMILY_RV380},
  81. {PCI_CHIP_R420_554d, CHIP_FAMILY_R420},
  82. {0, 0}
  83. };
  84. u16 get_radeon_id_family(u16 device)
  85. {
  86. int i;
  87. for (i=0; ati_radeon_id_family_table[0][i]; i+=2)
  88. if (ati_radeon_id_family_table[0][i] == device)
  89. return ati_radeon_id_family_table[0][i + 1];
  90. return 0;
  91. }
  92. struct radeonfb_info *rinfo;
  93. static void radeon_identify_vram(struct radeonfb_info *rinfo)
  94. {
  95. u32 tmp;
  96. /* framebuffer size */
  97. if ((rinfo->family == CHIP_FAMILY_RS100) ||
  98. (rinfo->family == CHIP_FAMILY_RS200) ||
  99. (rinfo->family == CHIP_FAMILY_RS300)) {
  100. u32 tom = INREG(NB_TOM);
  101. tmp = ((((tom >> 16) - (tom & 0xffff) + 1) << 6) * 1024);
  102. radeon_fifo_wait(6);
  103. OUTREG(MC_FB_LOCATION, tom);
  104. OUTREG(DISPLAY_BASE_ADDR, (tom & 0xffff) << 16);
  105. OUTREG(CRTC2_DISPLAY_BASE_ADDR, (tom & 0xffff) << 16);
  106. OUTREG(OV0_BASE_ADDR, (tom & 0xffff) << 16);
  107. /* This is supposed to fix the crtc2 noise problem. */
  108. OUTREG(GRPH2_BUFFER_CNTL, INREG(GRPH2_BUFFER_CNTL) & ~0x7f0000);
  109. if ((rinfo->family == CHIP_FAMILY_RS100) ||
  110. (rinfo->family == CHIP_FAMILY_RS200)) {
  111. /* This is to workaround the asic bug for RMX, some versions
  112. of BIOS dosen't have this register initialized correctly.
  113. */
  114. OUTREGP(CRTC_MORE_CNTL, CRTC_H_CUTOFF_ACTIVE_EN,
  115. ~CRTC_H_CUTOFF_ACTIVE_EN);
  116. }
  117. } else {
  118. tmp = INREG(CONFIG_MEMSIZE);
  119. }
  120. /* mem size is bits [28:0], mask off the rest */
  121. rinfo->video_ram = tmp & CONFIG_MEMSIZE_MASK;
  122. /*
  123. * Hack to get around some busted production M6's
  124. * reporting no ram
  125. */
  126. if (rinfo->video_ram == 0) {
  127. switch (rinfo->pdev.device) {
  128. case PCI_CHIP_RADEON_LY:
  129. case PCI_CHIP_RADEON_LZ:
  130. rinfo->video_ram = 8192 * 1024;
  131. break;
  132. default:
  133. break;
  134. }
  135. }
  136. /*
  137. * Now try to identify VRAM type
  138. */
  139. if ((rinfo->family >= CHIP_FAMILY_R300) ||
  140. (INREG(MEM_SDRAM_MODE_REG) & (1<<30)))
  141. rinfo->vram_ddr = 1;
  142. else
  143. rinfo->vram_ddr = 0;
  144. tmp = INREG(MEM_CNTL);
  145. if (IS_R300_VARIANT(rinfo)) {
  146. tmp &= R300_MEM_NUM_CHANNELS_MASK;
  147. switch (tmp) {
  148. case 0: rinfo->vram_width = 64; break;
  149. case 1: rinfo->vram_width = 128; break;
  150. case 2: rinfo->vram_width = 256; break;
  151. default: rinfo->vram_width = 128; break;
  152. }
  153. } else if ((rinfo->family == CHIP_FAMILY_RV100) ||
  154. (rinfo->family == CHIP_FAMILY_RS100) ||
  155. (rinfo->family == CHIP_FAMILY_RS200)){
  156. if (tmp & RV100_MEM_HALF_MODE)
  157. rinfo->vram_width = 32;
  158. else
  159. rinfo->vram_width = 64;
  160. } else {
  161. if (tmp & MEM_NUM_CHANNELS_MASK)
  162. rinfo->vram_width = 128;
  163. else
  164. rinfo->vram_width = 64;
  165. }
  166. /* This may not be correct, as some cards can have half of channel disabled
  167. * ToDo: identify these cases
  168. */
  169. DPRINT("radeonfb: Found %dk of %s %d bits wide videoram\n",
  170. rinfo->video_ram / 1024,
  171. rinfo->vram_ddr ? "DDR" : "SDRAM",
  172. rinfo->vram_width);
  173. }
  174. static void radeon_write_pll_regs(struct radeonfb_info *rinfo, struct radeon_regs *mode)
  175. {
  176. int i;
  177. radeon_fifo_wait(20);
  178. #if 0
  179. /* Workaround from XFree */
  180. if (rinfo->is_mobility) {
  181. /* A temporal workaround for the occational blanking on certain laptop
  182. * panels. This appears to related to the PLL divider registers
  183. * (fail to lock?). It occurs even when all dividers are the same
  184. * with their old settings. In this case we really don't need to
  185. * fiddle with PLL registers. By doing this we can avoid the blanking
  186. * problem with some panels.
  187. */
  188. if ((mode->ppll_ref_div == (INPLL(PPLL_REF_DIV) & PPLL_REF_DIV_MASK)) &&
  189. (mode->ppll_div_3 == (INPLL(PPLL_DIV_3) &
  190. (PPLL_POST3_DIV_MASK | PPLL_FB3_DIV_MASK)))) {
  191. /* We still have to force a switch to selected PPLL div thanks to
  192. * an XFree86 driver bug which will switch it away in some cases
  193. * even when using UseFDev */
  194. OUTREGP(CLOCK_CNTL_INDEX,
  195. mode->clk_cntl_index & PPLL_DIV_SEL_MASK,
  196. ~PPLL_DIV_SEL_MASK);
  197. radeon_pll_errata_after_index(rinfo);
  198. radeon_pll_errata_after_data(rinfo);
  199. return;
  200. }
  201. }
  202. #endif
  203. if(rinfo->pdev.device == PCI_CHIP_RV370_5B60) return;
  204. /* Swich VCKL clock input to CPUCLK so it stays fed while PPLL updates*/
  205. OUTPLLP(VCLK_ECP_CNTL, VCLK_SRC_SEL_CPUCLK, ~VCLK_SRC_SEL_MASK);
  206. /* Reset PPLL & enable atomic update */
  207. OUTPLLP(PPLL_CNTL,
  208. PPLL_RESET | PPLL_ATOMIC_UPDATE_EN | PPLL_VGA_ATOMIC_UPDATE_EN,
  209. ~(PPLL_RESET | PPLL_ATOMIC_UPDATE_EN | PPLL_VGA_ATOMIC_UPDATE_EN));
  210. /* Switch to selected PPLL divider */
  211. OUTREGP(CLOCK_CNTL_INDEX,
  212. mode->clk_cntl_index & PPLL_DIV_SEL_MASK,
  213. ~PPLL_DIV_SEL_MASK);
  214. /* Set PPLL ref. div */
  215. if (rinfo->family == CHIP_FAMILY_R300 ||
  216. rinfo->family == CHIP_FAMILY_RS300 ||
  217. rinfo->family == CHIP_FAMILY_R350 ||
  218. rinfo->family == CHIP_FAMILY_RV350) {
  219. if (mode->ppll_ref_div & R300_PPLL_REF_DIV_ACC_MASK) {
  220. /* When restoring console mode, use saved PPLL_REF_DIV
  221. * setting.
  222. */
  223. OUTPLLP(PPLL_REF_DIV, mode->ppll_ref_div, 0);
  224. } else {
  225. /* R300 uses ref_div_acc field as real ref divider */
  226. OUTPLLP(PPLL_REF_DIV,
  227. (mode->ppll_ref_div << R300_PPLL_REF_DIV_ACC_SHIFT),
  228. ~R300_PPLL_REF_DIV_ACC_MASK);
  229. }
  230. } else
  231. OUTPLLP(PPLL_REF_DIV, mode->ppll_ref_div, ~PPLL_REF_DIV_MASK);
  232. /* Set PPLL divider 3 & post divider*/
  233. OUTPLLP(PPLL_DIV_3, mode->ppll_div_3, ~PPLL_FB3_DIV_MASK);
  234. OUTPLLP(PPLL_DIV_3, mode->ppll_div_3, ~PPLL_POST3_DIV_MASK);
  235. /* Write update */
  236. while (INPLL(PPLL_REF_DIV) & PPLL_ATOMIC_UPDATE_R)
  237. ;
  238. OUTPLLP(PPLL_REF_DIV, PPLL_ATOMIC_UPDATE_W, ~PPLL_ATOMIC_UPDATE_W);
  239. /* Wait read update complete */
  240. /* FIXME: Certain revisions of R300 can't recover here. Not sure of
  241. the cause yet, but this workaround will mask the problem for now.
  242. Other chips usually will pass at the very first test, so the
  243. workaround shouldn't have any effect on them. */
  244. for (i = 0; (i < 10000 && INPLL(PPLL_REF_DIV) & PPLL_ATOMIC_UPDATE_R); i++)
  245. ;
  246. OUTPLL(HTOTAL_CNTL, 0);
  247. /* Clear reset & atomic update */
  248. OUTPLLP(PPLL_CNTL, 0,
  249. ~(PPLL_RESET | PPLL_SLEEP | PPLL_ATOMIC_UPDATE_EN | PPLL_VGA_ATOMIC_UPDATE_EN));
  250. /* We may want some locking ... oh well */
  251. udelay(5000);
  252. /* Switch back VCLK source to PPLL */
  253. OUTPLLP(VCLK_ECP_CNTL, VCLK_SRC_SEL_PPLLCLK, ~VCLK_SRC_SEL_MASK);
  254. }
  255. typedef struct {
  256. u16 reg;
  257. u32 val;
  258. } reg_val;
  259. #if 0 /* unused ? -> scheduled for removal */
  260. /* these common regs are cleared before mode setting so they do not
  261. * interfere with anything
  262. */
  263. static reg_val common_regs[] = {
  264. { OVR_CLR, 0 },
  265. { OVR_WID_LEFT_RIGHT, 0 },
  266. { OVR_WID_TOP_BOTTOM, 0 },
  267. { OV0_SCALE_CNTL, 0 },
  268. { SUBPIC_CNTL, 0 },
  269. { VIPH_CONTROL, 0 },
  270. { I2C_CNTL_1, 0 },
  271. { GEN_INT_CNTL, 0 },
  272. { CAP0_TRIG_CNTL, 0 },
  273. { CAP1_TRIG_CNTL, 0 },
  274. };
  275. #endif /* 0 */
  276. void radeon_setmode(void)
  277. {
  278. struct radeon_regs *mode = malloc(sizeof(struct radeon_regs));
  279. mode->crtc_gen_cntl = 0x03000200;
  280. mode->crtc_ext_cntl = 0x00008048;
  281. mode->dac_cntl = 0xff002100;
  282. mode->crtc_h_total_disp = 0x4f0063;
  283. mode->crtc_h_sync_strt_wid = 0x8c02a2;
  284. mode->crtc_v_total_disp = 0x01df020c;
  285. mode->crtc_v_sync_strt_wid = 0x8201ea;
  286. mode->crtc_pitch = 0x00500050;
  287. OUTREG(CRTC_GEN_CNTL, mode->crtc_gen_cntl);
  288. OUTREGP(CRTC_EXT_CNTL, mode->crtc_ext_cntl,
  289. ~(CRTC_HSYNC_DIS | CRTC_VSYNC_DIS | CRTC_DISPLAY_DIS));
  290. OUTREGP(DAC_CNTL, mode->dac_cntl, DAC_RANGE_CNTL | DAC_BLANKING);
  291. OUTREG(CRTC_H_TOTAL_DISP, mode->crtc_h_total_disp);
  292. OUTREG(CRTC_H_SYNC_STRT_WID, mode->crtc_h_sync_strt_wid);
  293. OUTREG(CRTC_V_TOTAL_DISP, mode->crtc_v_total_disp);
  294. OUTREG(CRTC_V_SYNC_STRT_WID, mode->crtc_v_sync_strt_wid);
  295. OUTREG(CRTC_OFFSET, 0);
  296. OUTREG(CRTC_OFFSET_CNTL, 0);
  297. OUTREG(CRTC_PITCH, mode->crtc_pitch);
  298. mode->clk_cntl_index = 0x300;
  299. mode->ppll_ref_div = 0xc;
  300. mode->ppll_div_3 = 0x00030059;
  301. radeon_write_pll_regs(rinfo, mode);
  302. }
  303. static void set_pal(void)
  304. {
  305. int idx, val = 0;
  306. for (idx = 0; idx < 256; idx++) {
  307. OUTREG8(PALETTE_INDEX, idx);
  308. OUTREG(PALETTE_DATA, val);
  309. val += 0x00010101;
  310. }
  311. }
  312. void radeon_setmode_9200(int vesa_idx, int bpp)
  313. {
  314. struct radeon_regs *mode = malloc(sizeof(struct radeon_regs));
  315. mode->crtc_gen_cntl = CRTC_EN | CRTC_EXT_DISP_EN;
  316. mode->crtc_ext_cntl = VGA_ATI_LINEAR | XCRT_CNT_EN | CRTC_CRT_ON;
  317. mode->dac_cntl = DAC_MASK_ALL | DAC_VGA_ADR_EN | DAC_8BIT_EN;
  318. mode->crtc_offset_cntl = CRTC_OFFSET_CNTL__CRTC_TILE_EN;
  319. switch (bpp) {
  320. case 24:
  321. mode->crtc_gen_cntl |= 0x6 << 8; /* x888 */
  322. #if defined(__BIG_ENDIAN)
  323. mode->surface_cntl = NONSURF_AP0_SWP_32BPP | NONSURF_AP1_SWP_32BPP;
  324. mode->surf_info[0] = NONSURF_AP0_SWP_32BPP | NONSURF_AP1_SWP_32BPP;
  325. #endif
  326. break;
  327. case 16:
  328. mode->crtc_gen_cntl |= 0x4 << 8; /* 565 */
  329. #if defined(__BIG_ENDIAN)
  330. mode->surface_cntl = NONSURF_AP0_SWP_16BPP | NONSURF_AP1_SWP_16BPP;
  331. mode->surf_info[0] = NONSURF_AP0_SWP_16BPP | NONSURF_AP1_SWP_16BPP;
  332. #endif
  333. break;
  334. default:
  335. mode->crtc_gen_cntl |= 0x2 << 8; /* palette */
  336. mode->surface_cntl = 0x00000000;
  337. break;
  338. }
  339. switch (vesa_idx) {
  340. case RES_MODE_1280x1024:
  341. mode->crtc_h_total_disp = CRTC_H_TOTAL_DISP_VAL(1688,1280);
  342. mode->crtc_v_total_disp = CRTC_V_TOTAL_DISP_VAL(1066,1024);
  343. mode->crtc_v_sync_strt_wid = CRTC_VSYNC_STRT_WID_VAL(1025,3);
  344. #if defined(CONFIG_RADEON_VREFRESH_75HZ)
  345. mode->crtc_h_sync_strt_wid = CRTC_HSYNC_STRT_WID_VAL(1288,18);
  346. mode->ppll_div_3 = 0x00010078;
  347. #else /* default @ 60 Hz */
  348. mode->crtc_h_sync_strt_wid = CRTC_HSYNC_STRT_WID_VAL(1320,14);
  349. mode->ppll_div_3 = 0x00010060;
  350. #endif
  351. /*
  352. * for this mode pitch expands to the same value for 32, 16 and 8 bpp,
  353. * so we set it here once only.
  354. */
  355. mode->crtc_pitch = RADEON_CRT_PITCH(1280,32);
  356. switch (bpp) {
  357. case 24:
  358. mode->surf_info[0] |= R200_SURF_TILE_COLOR_MACRO | (1280 * 4 / 16);
  359. mode->surf_upper_bound[0] = SURF_UPPER_BOUND(1280,1024,32);
  360. break;
  361. case 16:
  362. mode->surf_info[0] |= R200_SURF_TILE_COLOR_MACRO | (1280 * 2 / 16);
  363. mode->surf_upper_bound[0] = SURF_UPPER_BOUND(1280,1024,16);
  364. break;
  365. default: /* 8 bpp */
  366. mode->surf_info[0] = R200_SURF_TILE_COLOR_MACRO | (1280 * 1 / 16);
  367. mode->surf_upper_bound[0] = SURF_UPPER_BOUND(1280,1024,8);
  368. break;
  369. }
  370. break;
  371. case RES_MODE_1024x768:
  372. #if defined(CONFIG_RADEON_VREFRESH_75HZ)
  373. mode->crtc_h_total_disp = CRTC_H_TOTAL_DISP_VAL(1312,1024);
  374. mode->crtc_h_sync_strt_wid = CRTC_HSYNC_STRT_WID_VAL(1032,12);
  375. mode->crtc_v_total_disp = CRTC_V_TOTAL_DISP_VAL(800,768);
  376. mode->crtc_v_sync_strt_wid = CRTC_VSYNC_STRT_WID_VAL(769,3);
  377. mode->ppll_div_3 = 0x0002008c;
  378. #else /* @ 60 Hz */
  379. mode->crtc_h_total_disp = CRTC_H_TOTAL_DISP_VAL(1344,1024);
  380. mode->crtc_h_sync_strt_wid = CRTC_HSYNC_STRT_WID_VAL(1040,17) | CRTC_H_SYNC_POL;
  381. mode->crtc_v_total_disp = CRTC_V_TOTAL_DISP_VAL(806,768);
  382. mode->crtc_v_sync_strt_wid = CRTC_VSYNC_STRT_WID_VAL(771,6) | CRTC_V_SYNC_POL;
  383. mode->ppll_div_3 = 0x00020074;
  384. #endif
  385. /* also same pitch value for 32, 16 and 8 bpp */
  386. mode->crtc_pitch = RADEON_CRT_PITCH(1024,32);
  387. switch (bpp) {
  388. case 24:
  389. mode->surf_info[0] |= R200_SURF_TILE_COLOR_MACRO | (1024 * 4 / 16);
  390. mode->surf_upper_bound[0] = SURF_UPPER_BOUND(1024,768,32);
  391. break;
  392. case 16:
  393. mode->surf_info[0] |= R200_SURF_TILE_COLOR_MACRO | (1024 * 2 / 16);
  394. mode->surf_upper_bound[0] = SURF_UPPER_BOUND(1024,768,16);
  395. break;
  396. default: /* 8 bpp */
  397. mode->surf_info[0] = R200_SURF_TILE_COLOR_MACRO | (1024 * 1 / 16);
  398. mode->surf_upper_bound[0] = SURF_UPPER_BOUND(1024,768,8);
  399. break;
  400. }
  401. break;
  402. case RES_MODE_800x600:
  403. mode->crtc_h_total_disp = CRTC_H_TOTAL_DISP_VAL(1056,800);
  404. #if defined(CONFIG_RADEON_VREFRESH_75HZ)
  405. mode->crtc_h_sync_strt_wid = CRTC_HSYNC_STRT_WID_VAL(808,10);
  406. mode->crtc_v_total_disp = CRTC_V_TOTAL_DISP_VAL(625,600);
  407. mode->crtc_v_sync_strt_wid = CRTC_VSYNC_STRT_WID_VAL(601,3);
  408. mode->ppll_div_3 = 0x000300b0;
  409. #else /* @ 60 Hz */
  410. mode->crtc_h_sync_strt_wid = CRTC_HSYNC_STRT_WID_VAL(832,16);
  411. mode->crtc_v_total_disp = CRTC_V_TOTAL_DISP_VAL(628,600);
  412. mode->crtc_v_sync_strt_wid = CRTC_VSYNC_STRT_WID_VAL(601,4);
  413. mode->ppll_div_3 = 0x0003008e;
  414. #endif
  415. switch (bpp) {
  416. case 24:
  417. mode->crtc_pitch = RADEON_CRT_PITCH(832,32);
  418. mode->surf_info[0] |= R200_SURF_TILE_COLOR_MACRO | (832 * 4 / 16);
  419. mode->surf_upper_bound[0] = SURF_UPPER_BOUND(832,600,32);
  420. break;
  421. case 16:
  422. mode->crtc_pitch = RADEON_CRT_PITCH(896,16);
  423. mode->surf_info[0] |= R200_SURF_TILE_COLOR_MACRO | (896 * 2 / 16);
  424. mode->surf_upper_bound[0] = SURF_UPPER_BOUND(896,600,16);
  425. break;
  426. default: /* 8 bpp */
  427. mode->crtc_pitch = RADEON_CRT_PITCH(1024,8);
  428. mode->surf_info[0] = R200_SURF_TILE_COLOR_MACRO | (1024 * 1 / 16);
  429. mode->surf_upper_bound[0] = SURF_UPPER_BOUND(1024,600,8);
  430. break;
  431. }
  432. break;
  433. default: /* RES_MODE_640x480 */
  434. #if defined(CONFIG_RADEON_VREFRESH_75HZ)
  435. mode->crtc_h_total_disp = CRTC_H_TOTAL_DISP_VAL(840,640);
  436. mode->crtc_h_sync_strt_wid = CRTC_HSYNC_STRT_WID_VAL(648,8) | CRTC_H_SYNC_POL;
  437. mode->crtc_v_total_disp = CRTC_V_TOTAL_DISP_VAL(500,480);
  438. mode->crtc_v_sync_strt_wid = CRTC_VSYNC_STRT_WID_VAL(481,3) | CRTC_V_SYNC_POL;
  439. mode->ppll_div_3 = 0x00030070;
  440. #else /* @ 60 Hz */
  441. mode->crtc_h_total_disp = CRTC_H_TOTAL_DISP_VAL(800,640);
  442. mode->crtc_h_sync_strt_wid = CRTC_HSYNC_STRT_WID_VAL(674,12) | CRTC_H_SYNC_POL;
  443. mode->crtc_v_total_disp = CRTC_V_TOTAL_DISP_VAL(525,480);
  444. mode->crtc_v_sync_strt_wid = CRTC_VSYNC_STRT_WID_VAL(491,2) | CRTC_V_SYNC_POL;
  445. mode->ppll_div_3 = 0x00030059;
  446. #endif
  447. /* also same pitch value for 32, 16 and 8 bpp */
  448. mode->crtc_pitch = RADEON_CRT_PITCH(640,32);
  449. switch (bpp) {
  450. case 24:
  451. mode->surf_info[0] |= R200_SURF_TILE_COLOR_MACRO | (640 * 4 / 16);
  452. mode->surf_upper_bound[0] = SURF_UPPER_BOUND(640,480,32);
  453. break;
  454. case 16:
  455. mode->surf_info[0] |= R200_SURF_TILE_COLOR_MACRO | (640 * 2 / 16);
  456. mode->surf_upper_bound[0] = SURF_UPPER_BOUND(640,480,16);
  457. break;
  458. default: /* 8 bpp */
  459. mode->crtc_offset_cntl = 0x00000000;
  460. break;
  461. }
  462. break;
  463. }
  464. OUTREG(CRTC_GEN_CNTL, mode->crtc_gen_cntl | CRTC_DISP_REQ_EN_B);
  465. OUTREGP(CRTC_EXT_CNTL, mode->crtc_ext_cntl,
  466. (CRTC_HSYNC_DIS | CRTC_VSYNC_DIS | CRTC_DISPLAY_DIS));
  467. OUTREGP(DAC_CNTL, mode->dac_cntl, DAC_RANGE_CNTL | DAC_BLANKING);
  468. OUTREG(CRTC_H_TOTAL_DISP, mode->crtc_h_total_disp);
  469. OUTREG(CRTC_H_SYNC_STRT_WID, mode->crtc_h_sync_strt_wid);
  470. OUTREG(CRTC_V_TOTAL_DISP, mode->crtc_v_total_disp);
  471. OUTREG(CRTC_V_SYNC_STRT_WID, mode->crtc_v_sync_strt_wid);
  472. OUTREG(CRTC_OFFSET, 0);
  473. OUTREG(CRTC_OFFSET_CNTL, mode->crtc_offset_cntl);
  474. OUTREG(CRTC_PITCH, mode->crtc_pitch);
  475. OUTREG(CRTC_GEN_CNTL, mode->crtc_gen_cntl);
  476. mode->clk_cntl_index = 0x300;
  477. mode->ppll_ref_div = 0xc;
  478. radeon_write_pll_regs(rinfo, mode);
  479. OUTREGP(CRTC_EXT_CNTL, mode->crtc_ext_cntl,
  480. ~(CRTC_HSYNC_DIS | CRTC_VSYNC_DIS | CRTC_DISPLAY_DIS));
  481. OUTREG(SURFACE0_INFO, mode->surf_info[0]);
  482. OUTREG(SURFACE0_LOWER_BOUND, 0);
  483. OUTREG(SURFACE0_UPPER_BOUND, mode->surf_upper_bound[0]);
  484. OUTREG(SURFACE_CNTL, mode->surface_cntl);
  485. if (bpp > 8)
  486. set_pal();
  487. free(mode);
  488. }
  489. #include "../bios_emulator/include/biosemu.h"
  490. int radeon_probe(struct radeonfb_info *rinfo)
  491. {
  492. pci_dev_t pdev;
  493. u16 did;
  494. pdev = pci_find_devices(ati_radeon_pci_ids, 0);
  495. if (pdev != -1) {
  496. pci_read_config_word(pdev, PCI_DEVICE_ID, &did);
  497. printf("ATI Radeon video card (%04x, %04x) found @(%d:%d:%d)\n",
  498. PCI_VENDOR_ID_ATI, did, (pdev >> 16) & 0xff,
  499. (pdev >> 11) & 0x1f, (pdev >> 8) & 0x7);
  500. strcpy(rinfo->name, "ATI Radeon");
  501. rinfo->pdev.vendor = PCI_VENDOR_ID_ATI;
  502. rinfo->pdev.device = did;
  503. rinfo->family = get_radeon_id_family(rinfo->pdev.device);
  504. pci_read_config_dword(pdev, PCI_BASE_ADDRESS_0,
  505. &rinfo->fb_base_bus);
  506. pci_read_config_dword(pdev, PCI_BASE_ADDRESS_2,
  507. &rinfo->mmio_base_bus);
  508. rinfo->fb_base_bus &= 0xfffff000;
  509. rinfo->mmio_base_bus &= ~0x04;
  510. rinfo->mmio_base = pci_bus_to_virt(pdev, rinfo->mmio_base_bus,
  511. PCI_REGION_MEM, 0, MAP_NOCACHE);
  512. DPRINT("rinfo->mmio_base = 0x%p bus=0x%x\n",
  513. rinfo->mmio_base, rinfo->mmio_base_bus);
  514. rinfo->fb_local_base = INREG(MC_FB_LOCATION) << 16;
  515. DPRINT("rinfo->fb_local_base = 0x%x\n",rinfo->fb_local_base);
  516. /* PostBIOS with x86 emulater */
  517. if (!BootVideoCardBIOS(pdev, NULL, 0))
  518. return -1;
  519. /*
  520. * Check for errata
  521. * (These will be added in the future for the chipfamily
  522. * R300, RV200, RS200, RV100, RS100.)
  523. */
  524. /* Get VRAM size and type */
  525. radeon_identify_vram(rinfo);
  526. rinfo->mapped_vram = min_t(unsigned long, MAX_MAPPED_VRAM,
  527. rinfo->video_ram);
  528. rinfo->fb_base = pci_bus_to_virt(pdev, rinfo->fb_base_bus,
  529. PCI_REGION_MEM, 0, MAP_NOCACHE);
  530. DPRINT("Radeon: framebuffer base address 0x%08x, "
  531. "bus address 0x%08x\n"
  532. "MMIO base address 0x%08x, bus address 0x%08x, "
  533. "framebuffer local base 0x%08x.\n ",
  534. (u32)rinfo->fb_base, rinfo->fb_base_bus,
  535. (u32)rinfo->mmio_base, rinfo->mmio_base_bus,
  536. rinfo->fb_local_base);
  537. return 0;
  538. }
  539. return -1;
  540. }
  541. /*
  542. * The Graphic Device
  543. */
  544. GraphicDevice ctfb;
  545. #define CURSOR_SIZE 0x1000 /* in KByte for HW Cursor */
  546. #define PATTERN_ADR (pGD->dprBase + CURSOR_SIZE) /* pattern Memory after Cursor Memory */
  547. #define PATTERN_SIZE 8*8*4 /* 4 Bytes per Pixel 8 x 8 Pixel */
  548. #define ACCELMEMORY (CURSOR_SIZE + PATTERN_SIZE) /* reserved Memory for BITBlt and hw cursor */
  549. void *video_hw_init(void)
  550. {
  551. GraphicDevice *pGD = (GraphicDevice *) & ctfb;
  552. u32 *vm;
  553. char *penv;
  554. unsigned long t1, hsynch, vsynch;
  555. int bits_per_pixel, i, tmp, vesa_idx = 0, videomode;
  556. struct ctfb_res_modes *res_mode;
  557. struct ctfb_res_modes var_mode;
  558. rinfo = malloc(sizeof(struct radeonfb_info));
  559. printf("Video: ");
  560. if(radeon_probe(rinfo)) {
  561. printf("No radeon video card found!\n");
  562. return NULL;
  563. }
  564. tmp = 0;
  565. videomode = CONFIG_SYS_DEFAULT_VIDEO_MODE;
  566. /* get video mode via environment */
  567. penv = env_get("videomode");
  568. if (penv) {
  569. /* deceide if it is a string */
  570. if (penv[0] <= '9') {
  571. videomode = (int) simple_strtoul (penv, NULL, 16);
  572. tmp = 1;
  573. }
  574. } else {
  575. tmp = 1;
  576. }
  577. if (tmp) {
  578. /* parameter are vesa modes */
  579. /* search params */
  580. for (i = 0; i < VESA_MODES_COUNT; i++) {
  581. if (vesa_modes[i].vesanr == videomode)
  582. break;
  583. }
  584. if (i == VESA_MODES_COUNT) {
  585. printf ("no VESA Mode found, switching to mode 0x%x ", CONFIG_SYS_DEFAULT_VIDEO_MODE);
  586. i = 0;
  587. }
  588. res_mode = (struct ctfb_res_modes *) &res_mode_init[vesa_modes[i].resindex];
  589. bits_per_pixel = vesa_modes[i].bits_per_pixel;
  590. vesa_idx = vesa_modes[i].resindex;
  591. } else {
  592. res_mode = (struct ctfb_res_modes *) &var_mode;
  593. bits_per_pixel = video_get_params (res_mode, penv);
  594. }
  595. /* calculate hsynch and vsynch freq (info only) */
  596. t1 = (res_mode->left_margin + res_mode->xres +
  597. res_mode->right_margin + res_mode->hsync_len) / 8;
  598. t1 *= 8;
  599. t1 *= res_mode->pixclock;
  600. t1 /= 1000;
  601. hsynch = 1000000000L / t1;
  602. t1 *= (res_mode->upper_margin + res_mode->yres +
  603. res_mode->lower_margin + res_mode->vsync_len);
  604. t1 /= 1000;
  605. vsynch = 1000000000L / t1;
  606. /* fill in Graphic device struct */
  607. sprintf (pGD->modeIdent, "%dx%dx%d %ldkHz %ldHz", res_mode->xres,
  608. res_mode->yres, bits_per_pixel, (hsynch / 1000),
  609. (vsynch / 1000));
  610. printf ("%s\n", pGD->modeIdent);
  611. pGD->winSizeX = res_mode->xres;
  612. pGD->winSizeY = res_mode->yres;
  613. pGD->plnSizeX = res_mode->xres;
  614. pGD->plnSizeY = res_mode->yres;
  615. switch (bits_per_pixel) {
  616. case 24:
  617. pGD->gdfBytesPP = 4;
  618. pGD->gdfIndex = GDF_32BIT_X888RGB;
  619. if (res_mode->xres == 800) {
  620. pGD->winSizeX = 832;
  621. pGD->plnSizeX = 832;
  622. }
  623. break;
  624. case 16:
  625. pGD->gdfBytesPP = 2;
  626. pGD->gdfIndex = GDF_16BIT_565RGB;
  627. if (res_mode->xres == 800) {
  628. pGD->winSizeX = 896;
  629. pGD->plnSizeX = 896;
  630. }
  631. break;
  632. default:
  633. if (res_mode->xres == 800) {
  634. pGD->winSizeX = 1024;
  635. pGD->plnSizeX = 1024;
  636. }
  637. pGD->gdfBytesPP = 1;
  638. pGD->gdfIndex = GDF__8BIT_INDEX;
  639. break;
  640. }
  641. pGD->isaBase = CONFIG_SYS_ISA_IO_BASE_ADDRESS;
  642. pGD->pciBase = (unsigned int)rinfo->fb_base;
  643. pGD->frameAdrs = (unsigned int)rinfo->fb_base;
  644. pGD->memSize = 64 * 1024 * 1024;
  645. /* Cursor Start Address */
  646. pGD->dprBase = (pGD->winSizeX * pGD->winSizeY * pGD->gdfBytesPP) +
  647. (unsigned int)rinfo->fb_base;
  648. if ((pGD->dprBase & 0x0fff) != 0) {
  649. /* allign it */
  650. pGD->dprBase &= 0xfffff000;
  651. pGD->dprBase += 0x00001000;
  652. }
  653. DPRINT ("Cursor Start %x Pattern Start %x\n", pGD->dprBase,
  654. PATTERN_ADR);
  655. pGD->vprBase = (unsigned int)rinfo->fb_base; /* Dummy */
  656. pGD->cprBase = (unsigned int)rinfo->fb_base; /* Dummy */
  657. /* set up Hardware */
  658. /* Clear video memory (only visible screen area) */
  659. i = pGD->winSizeX * pGD->winSizeY * pGD->gdfBytesPP / 4;
  660. vm = (unsigned int *) pGD->pciBase;
  661. while (i--)
  662. *vm++ = 0;
  663. /*SetDrawingEngine (bits_per_pixel);*/
  664. if (rinfo->family == CHIP_FAMILY_RV280)
  665. radeon_setmode_9200(vesa_idx, bits_per_pixel);
  666. else
  667. radeon_setmode();
  668. return ((void *) pGD);
  669. }
  670. void video_set_lut (unsigned int index, /* color number */
  671. unsigned char r, /* red */
  672. unsigned char g, /* green */
  673. unsigned char b /* blue */
  674. )
  675. {
  676. OUTREG(PALETTE_INDEX, index);
  677. OUTREG(PALETTE_DATA, (r << 16) | (g << 8) | b);
  678. }