ati_radeon_fb.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  1. /*
  2. * ATI Radeon Video card Framebuffer driver.
  3. *
  4. * Copyright 2007 Freescale Semiconductor, Inc.
  5. * Zhang Wei <wei.zhang@freescale.com>
  6. * Jason Jin <jason.jin@freescale.com>
  7. *
  8. * See file CREDITS for list of people who contributed to this
  9. * project.
  10. *
  11. * This program is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU General Public License as
  13. * published by the Free Software Foundation; either version 2 of
  14. * the License, or (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  24. * MA 02111-1307 USA
  25. *
  26. * Some codes of this file is partly ported from Linux kernel
  27. * ATI video framebuffer driver.
  28. *
  29. * Now the driver is tested on below ATI chips:
  30. * 9200
  31. * X300
  32. * X700
  33. *
  34. */
  35. #include <common.h>
  36. #ifdef CONFIG_ATI_RADEON_FB
  37. #include <command.h>
  38. #include <pci.h>
  39. #include <asm/processor.h>
  40. #include <asm/errno.h>
  41. #include <asm/io.h>
  42. #include <malloc.h>
  43. #include <video_fb.h>
  44. #include <radeon.h>
  45. #include "ati_ids.h"
  46. #include "ati_radeon_fb.h"
  47. #undef DEBUG
  48. #ifdef DEBUG
  49. #define DPRINT(x...) printf(x)
  50. #else
  51. #define DPRINT(x...) do{}while(0)
  52. #endif
  53. #ifndef min_t
  54. #define min_t(type,x,y) \
  55. ({ type __x = (x); type __y = (y); __x < __y ? __x: __y; })
  56. #endif
  57. #define MAX_MAPPED_VRAM (2048*2048*4)
  58. #define MIN_MAPPED_VRAM (1024*768*1)
  59. /*#define PCI_VENDOR_ID_ATI*/
  60. #define PCI_CHIP_RV280_5960 0x5960
  61. #define PCI_CHIP_RV280_5961 0x5961
  62. #define PCI_CHIP_RV280_5962 0x5962
  63. #define PCI_CHIP_RV280_5964 0x5964
  64. #define PCI_CHIP_RV370_5B60 0x5B60
  65. #define PCI_CHIP_RV380_5657 0x5657
  66. #define PCI_CHIP_R420_554d 0x554d
  67. static struct pci_device_id ati_radeon_pci_ids[] = {
  68. {PCI_VENDOR_ID_ATI, PCI_CHIP_RV280_5960},
  69. {PCI_VENDOR_ID_ATI, PCI_CHIP_RV280_5961},
  70. {PCI_VENDOR_ID_ATI, PCI_CHIP_RV280_5962},
  71. {PCI_VENDOR_ID_ATI, PCI_CHIP_RV280_5964},
  72. {PCI_VENDOR_ID_ATI, PCI_CHIP_RV370_5B60},
  73. {PCI_VENDOR_ID_ATI, PCI_CHIP_RV380_5657},
  74. {PCI_VENDOR_ID_ATI, PCI_CHIP_R420_554d},
  75. {0, 0}
  76. };
  77. static u16 ati_radeon_id_family_table[][2] = {
  78. {PCI_CHIP_RV280_5960, CHIP_FAMILY_RV280},
  79. {PCI_CHIP_RV280_5961, CHIP_FAMILY_RV280},
  80. {PCI_CHIP_RV280_5962, CHIP_FAMILY_RV280},
  81. {PCI_CHIP_RV280_5964, CHIP_FAMILY_RV280},
  82. {PCI_CHIP_RV370_5B60, CHIP_FAMILY_RV380},
  83. {PCI_CHIP_RV380_5657, CHIP_FAMILY_RV380},
  84. {PCI_CHIP_R420_554d, CHIP_FAMILY_R420},
  85. {0, 0}
  86. };
  87. u16 get_radeon_id_family(u16 device)
  88. {
  89. int i;
  90. for (i=0; ati_radeon_id_family_table[0][i]; i+=2)
  91. if (ati_radeon_id_family_table[0][i] == device)
  92. return ati_radeon_id_family_table[0][i + 1];
  93. return 0;
  94. }
  95. struct radeonfb_info *rinfo;
  96. static void radeon_identify_vram(struct radeonfb_info *rinfo)
  97. {
  98. u32 tmp;
  99. /* framebuffer size */
  100. if ((rinfo->family == CHIP_FAMILY_RS100) ||
  101. (rinfo->family == CHIP_FAMILY_RS200) ||
  102. (rinfo->family == CHIP_FAMILY_RS300)) {
  103. u32 tom = INREG(NB_TOM);
  104. tmp = ((((tom >> 16) - (tom & 0xffff) + 1) << 6) * 1024);
  105. radeon_fifo_wait(6);
  106. OUTREG(MC_FB_LOCATION, tom);
  107. OUTREG(DISPLAY_BASE_ADDR, (tom & 0xffff) << 16);
  108. OUTREG(CRTC2_DISPLAY_BASE_ADDR, (tom & 0xffff) << 16);
  109. OUTREG(OV0_BASE_ADDR, (tom & 0xffff) << 16);
  110. /* This is supposed to fix the crtc2 noise problem. */
  111. OUTREG(GRPH2_BUFFER_CNTL, INREG(GRPH2_BUFFER_CNTL) & ~0x7f0000);
  112. if ((rinfo->family == CHIP_FAMILY_RS100) ||
  113. (rinfo->family == CHIP_FAMILY_RS200)) {
  114. /* This is to workaround the asic bug for RMX, some versions
  115. of BIOS dosen't have this register initialized correctly.
  116. */
  117. OUTREGP(CRTC_MORE_CNTL, CRTC_H_CUTOFF_ACTIVE_EN,
  118. ~CRTC_H_CUTOFF_ACTIVE_EN);
  119. }
  120. } else {
  121. tmp = INREG(CONFIG_MEMSIZE);
  122. }
  123. /* mem size is bits [28:0], mask off the rest */
  124. rinfo->video_ram = tmp & CONFIG_MEMSIZE_MASK;
  125. /*
  126. * Hack to get around some busted production M6's
  127. * reporting no ram
  128. */
  129. if (rinfo->video_ram == 0) {
  130. switch (rinfo->pdev.device) {
  131. case PCI_CHIP_RADEON_LY:
  132. case PCI_CHIP_RADEON_LZ:
  133. rinfo->video_ram = 8192 * 1024;
  134. break;
  135. default:
  136. break;
  137. }
  138. }
  139. /*
  140. * Now try to identify VRAM type
  141. */
  142. if ((rinfo->family >= CHIP_FAMILY_R300) ||
  143. (INREG(MEM_SDRAM_MODE_REG) & (1<<30)))
  144. rinfo->vram_ddr = 1;
  145. else
  146. rinfo->vram_ddr = 0;
  147. tmp = INREG(MEM_CNTL);
  148. if (IS_R300_VARIANT(rinfo)) {
  149. tmp &= R300_MEM_NUM_CHANNELS_MASK;
  150. switch (tmp) {
  151. case 0: rinfo->vram_width = 64; break;
  152. case 1: rinfo->vram_width = 128; break;
  153. case 2: rinfo->vram_width = 256; break;
  154. default: rinfo->vram_width = 128; break;
  155. }
  156. } else if ((rinfo->family == CHIP_FAMILY_RV100) ||
  157. (rinfo->family == CHIP_FAMILY_RS100) ||
  158. (rinfo->family == CHIP_FAMILY_RS200)){
  159. if (tmp & RV100_MEM_HALF_MODE)
  160. rinfo->vram_width = 32;
  161. else
  162. rinfo->vram_width = 64;
  163. } else {
  164. if (tmp & MEM_NUM_CHANNELS_MASK)
  165. rinfo->vram_width = 128;
  166. else
  167. rinfo->vram_width = 64;
  168. }
  169. /* This may not be correct, as some cards can have half of channel disabled
  170. * ToDo: identify these cases
  171. */
  172. DPRINT("radeonfb: Found %ldk of %s %d bits wide videoram\n",
  173. rinfo->video_ram / 1024,
  174. rinfo->vram_ddr ? "DDR" : "SDRAM",
  175. rinfo->vram_width);
  176. }
  177. static void radeon_write_pll_regs(struct radeonfb_info *rinfo, struct radeon_regs *mode)
  178. {
  179. int i;
  180. radeon_fifo_wait(20);
  181. #if 0
  182. /* Workaround from XFree */
  183. if (rinfo->is_mobility) {
  184. /* A temporal workaround for the occational blanking on certain laptop
  185. * panels. This appears to related to the PLL divider registers
  186. * (fail to lock?). It occurs even when all dividers are the same
  187. * with their old settings. In this case we really don't need to
  188. * fiddle with PLL registers. By doing this we can avoid the blanking
  189. * problem with some panels.
  190. */
  191. if ((mode->ppll_ref_div == (INPLL(PPLL_REF_DIV) & PPLL_REF_DIV_MASK)) &&
  192. (mode->ppll_div_3 == (INPLL(PPLL_DIV_3) &
  193. (PPLL_POST3_DIV_MASK | PPLL_FB3_DIV_MASK)))) {
  194. /* We still have to force a switch to selected PPLL div thanks to
  195. * an XFree86 driver bug which will switch it away in some cases
  196. * even when using UseFDev */
  197. OUTREGP(CLOCK_CNTL_INDEX,
  198. mode->clk_cntl_index & PPLL_DIV_SEL_MASK,
  199. ~PPLL_DIV_SEL_MASK);
  200. radeon_pll_errata_after_index(rinfo);
  201. radeon_pll_errata_after_data(rinfo);
  202. return;
  203. }
  204. }
  205. #endif
  206. if(rinfo->pdev.device == PCI_CHIP_RV370_5B60) return;
  207. /* Swich VCKL clock input to CPUCLK so it stays fed while PPLL updates*/
  208. OUTPLLP(VCLK_ECP_CNTL, VCLK_SRC_SEL_CPUCLK, ~VCLK_SRC_SEL_MASK);
  209. /* Reset PPLL & enable atomic update */
  210. OUTPLLP(PPLL_CNTL,
  211. PPLL_RESET | PPLL_ATOMIC_UPDATE_EN | PPLL_VGA_ATOMIC_UPDATE_EN,
  212. ~(PPLL_RESET | PPLL_ATOMIC_UPDATE_EN | PPLL_VGA_ATOMIC_UPDATE_EN));
  213. /* Switch to selected PPLL divider */
  214. OUTREGP(CLOCK_CNTL_INDEX,
  215. mode->clk_cntl_index & PPLL_DIV_SEL_MASK,
  216. ~PPLL_DIV_SEL_MASK);
  217. /* Set PPLL ref. div */
  218. if (rinfo->family == CHIP_FAMILY_R300 ||
  219. rinfo->family == CHIP_FAMILY_RS300 ||
  220. rinfo->family == CHIP_FAMILY_R350 ||
  221. rinfo->family == CHIP_FAMILY_RV350) {
  222. if (mode->ppll_ref_div & R300_PPLL_REF_DIV_ACC_MASK) {
  223. /* When restoring console mode, use saved PPLL_REF_DIV
  224. * setting.
  225. */
  226. OUTPLLP(PPLL_REF_DIV, mode->ppll_ref_div, 0);
  227. } else {
  228. /* R300 uses ref_div_acc field as real ref divider */
  229. OUTPLLP(PPLL_REF_DIV,
  230. (mode->ppll_ref_div << R300_PPLL_REF_DIV_ACC_SHIFT),
  231. ~R300_PPLL_REF_DIV_ACC_MASK);
  232. }
  233. } else
  234. OUTPLLP(PPLL_REF_DIV, mode->ppll_ref_div, ~PPLL_REF_DIV_MASK);
  235. /* Set PPLL divider 3 & post divider*/
  236. OUTPLLP(PPLL_DIV_3, mode->ppll_div_3, ~PPLL_FB3_DIV_MASK);
  237. OUTPLLP(PPLL_DIV_3, mode->ppll_div_3, ~PPLL_POST3_DIV_MASK);
  238. /* Write update */
  239. while (INPLL(PPLL_REF_DIV) & PPLL_ATOMIC_UPDATE_R)
  240. ;
  241. OUTPLLP(PPLL_REF_DIV, PPLL_ATOMIC_UPDATE_W, ~PPLL_ATOMIC_UPDATE_W);
  242. /* Wait read update complete */
  243. /* FIXME: Certain revisions of R300 can't recover here. Not sure of
  244. the cause yet, but this workaround will mask the problem for now.
  245. Other chips usually will pass at the very first test, so the
  246. workaround shouldn't have any effect on them. */
  247. for (i = 0; (i < 10000 && INPLL(PPLL_REF_DIV) & PPLL_ATOMIC_UPDATE_R); i++)
  248. ;
  249. OUTPLL(HTOTAL_CNTL, 0);
  250. /* Clear reset & atomic update */
  251. OUTPLLP(PPLL_CNTL, 0,
  252. ~(PPLL_RESET | PPLL_SLEEP | PPLL_ATOMIC_UPDATE_EN | PPLL_VGA_ATOMIC_UPDATE_EN));
  253. /* We may want some locking ... oh well */
  254. udelay(5000);
  255. /* Switch back VCLK source to PPLL */
  256. OUTPLLP(VCLK_ECP_CNTL, VCLK_SRC_SEL_PPLLCLK, ~VCLK_SRC_SEL_MASK);
  257. }
  258. typedef struct {
  259. u16 reg;
  260. u32 val;
  261. } reg_val;
  262. /* these common regs are cleared before mode setting so they do not
  263. * interfere with anything
  264. */
  265. static reg_val common_regs[] = {
  266. { OVR_CLR, 0 },
  267. { OVR_WID_LEFT_RIGHT, 0 },
  268. { OVR_WID_TOP_BOTTOM, 0 },
  269. { OV0_SCALE_CNTL, 0 },
  270. { SUBPIC_CNTL, 0 },
  271. { VIPH_CONTROL, 0 },
  272. { I2C_CNTL_1, 0 },
  273. { GEN_INT_CNTL, 0 },
  274. { CAP0_TRIG_CNTL, 0 },
  275. { CAP1_TRIG_CNTL, 0 },
  276. };
  277. void radeon_setmode(void)
  278. {
  279. int i;
  280. struct radeon_regs *mode = malloc(sizeof(struct radeon_regs));
  281. mode->crtc_gen_cntl = 0x03000200;
  282. mode->crtc_ext_cntl = 0x00008048;
  283. mode->dac_cntl = 0xff002100;
  284. mode->crtc_h_total_disp = 0x4f0063;
  285. mode->crtc_h_sync_strt_wid = 0x8c02a2;
  286. mode->crtc_v_total_disp = 0x01df020c;
  287. mode->crtc_v_sync_strt_wid = 0x8201ea;
  288. mode->crtc_pitch = 0x00500050;
  289. OUTREG(CRTC_GEN_CNTL, mode->crtc_gen_cntl);
  290. OUTREGP(CRTC_EXT_CNTL, mode->crtc_ext_cntl,
  291. ~(CRTC_HSYNC_DIS | CRTC_VSYNC_DIS | CRTC_DISPLAY_DIS));
  292. OUTREGP(DAC_CNTL, mode->dac_cntl, DAC_RANGE_CNTL | DAC_BLANKING);
  293. OUTREG(CRTC_H_TOTAL_DISP, mode->crtc_h_total_disp);
  294. OUTREG(CRTC_H_SYNC_STRT_WID, mode->crtc_h_sync_strt_wid);
  295. OUTREG(CRTC_V_TOTAL_DISP, mode->crtc_v_total_disp);
  296. OUTREG(CRTC_V_SYNC_STRT_WID, mode->crtc_v_sync_strt_wid);
  297. OUTREG(CRTC_OFFSET, 0);
  298. OUTREG(CRTC_OFFSET_CNTL, 0);
  299. OUTREG(CRTC_PITCH, mode->crtc_pitch);
  300. mode->clk_cntl_index = 0x300;
  301. mode->ppll_ref_div = 0xc;
  302. mode->ppll_div_3 = 0x00030059;
  303. radeon_write_pll_regs(rinfo, mode);
  304. }
  305. int radeon_probe(struct radeonfb_info *rinfo)
  306. {
  307. pci_dev_t pdev;
  308. u16 did;
  309. pdev = pci_find_devices(ati_radeon_pci_ids, 0);
  310. if (pdev != -1) {
  311. pci_read_config_word(pdev, PCI_DEVICE_ID, &did);
  312. printf("ATI Radeon video card (%04x, %04x) found @(%d:%d:%d)\n",
  313. PCI_VENDOR_ID_ATI, did, (pdev >> 16) & 0xff,
  314. (pdev >> 11) & 0x1f, (pdev >> 8) & 0x7);
  315. strcpy(rinfo->name, "ATI Radeon");
  316. rinfo->pdev.vendor = PCI_VENDOR_ID_ATI;
  317. rinfo->pdev.device = did;
  318. rinfo->family = get_radeon_id_family(rinfo->pdev.device);
  319. pci_read_config_dword(pdev, PCI_BASE_ADDRESS_0,
  320. &rinfo->fb_base_phys);
  321. pci_read_config_dword(pdev, PCI_BASE_ADDRESS_2,
  322. &rinfo->mmio_base_phys);
  323. rinfo->fb_base_phys &= 0xfffff000;
  324. rinfo->mmio_base_phys &= ~0x04;
  325. rinfo->mmio_base = (void *)rinfo->mmio_base_phys;
  326. DPRINT("rinfo->mmio_base = 0x%x\n",rinfo->mmio_base);
  327. rinfo->fb_local_base = INREG(MC_FB_LOCATION) << 16;
  328. DPRINT("rinfo->fb_local_base = 0x%x\n",rinfo->fb_local_base);
  329. /* PostBIOS with x86 emulater */
  330. BootVideoCardBIOS(pdev, NULL, 0);
  331. /*
  332. * Check for errata
  333. * (These will be added in the future for the chipfamily
  334. * R300, RV200, RS200, RV100, RS100.)
  335. */
  336. /* Get VRAM size and type */
  337. radeon_identify_vram(rinfo);
  338. rinfo->mapped_vram = min_t(unsigned long, MAX_MAPPED_VRAM,
  339. rinfo->video_ram);
  340. rinfo->fb_base = (void *)rinfo->fb_base_phys;
  341. DPRINT("Radeon: framebuffer base phy address 0x%08x," \
  342. "MMIO base phy address 0x%08x," \
  343. "framebuffer local base 0x%08x.\n ",
  344. rinfo->fb_base_phys, rinfo->mmio_base_phys,
  345. rinfo->fb_local_base);
  346. return 0;
  347. }
  348. return -1;
  349. }
  350. /*
  351. * The Graphic Device
  352. */
  353. GraphicDevice ctfb;
  354. #define CURSOR_SIZE 0x1000 /* in KByte for HW Cursor */
  355. #define PATTERN_ADR (pGD->dprBase + CURSOR_SIZE) /* pattern Memory after Cursor Memory */
  356. #define PATTERN_SIZE 8*8*4 /* 4 Bytes per Pixel 8 x 8 Pixel */
  357. #define ACCELMEMORY (CURSOR_SIZE + PATTERN_SIZE) /* reserved Memory for BITBlt and hw cursor */
  358. void *video_hw_init(void)
  359. {
  360. GraphicDevice *pGD = (GraphicDevice *) & ctfb;
  361. int i;
  362. u32 *vm;
  363. rinfo = malloc(sizeof(struct radeonfb_info));
  364. if(radeon_probe(rinfo)) {
  365. printf("No radeon video card found!\n");
  366. return NULL;
  367. }
  368. /* fill in Graphic device struct */
  369. sprintf (pGD->modeIdent, "%dx%dx%d %ldkHz %ldHz", 640,
  370. 480, 16, (1000 / 1000),
  371. (2000 / 1000));
  372. printf ("%s\n", pGD->modeIdent);
  373. pGD->winSizeX = 640;
  374. pGD->winSizeY = 480;
  375. pGD->plnSizeX = 640;
  376. pGD->plnSizeY = 480;
  377. pGD->gdfBytesPP = 1;
  378. pGD->gdfIndex = GDF__8BIT_INDEX;
  379. pGD->isaBase = CFG_ISA_IO_BASE_ADDRESS;
  380. pGD->pciBase = rinfo->fb_base_phys;
  381. pGD->frameAdrs = rinfo->fb_base_phys;
  382. pGD->memSize = 64 * 1024 * 1024;
  383. /* Cursor Start Address */
  384. pGD->dprBase =
  385. (pGD->winSizeX * pGD->winSizeY * pGD->gdfBytesPP) + rinfo->fb_base_phys;
  386. if ((pGD->dprBase & 0x0fff) != 0) {
  387. /* allign it */
  388. pGD->dprBase &= 0xfffff000;
  389. pGD->dprBase += 0x00001000;
  390. }
  391. DPRINT ("Cursor Start %x Pattern Start %x\n", pGD->dprBase,
  392. PATTERN_ADR);
  393. pGD->vprBase = rinfo->fb_base_phys; /* Dummy */
  394. pGD->cprBase = rinfo->fb_base_phys; /* Dummy */
  395. /* set up Hardware */
  396. /* Clear video memory */
  397. i = pGD->memSize / 4;
  398. vm = (unsigned int *) pGD->pciBase;
  399. while (i--)
  400. *vm++ = 0;
  401. /*SetDrawingEngine (bits_per_pixel);*/
  402. radeon_setmode();
  403. return ((void *) pGD);
  404. }
  405. void video_set_lut (unsigned int index, /* color number */
  406. unsigned char r, /* red */
  407. unsigned char g, /* green */
  408. unsigned char b /* blue */
  409. )
  410. {
  411. OUTREG(PALETTE_INDEX, index);
  412. OUTREG(PALETTE_DATA, (r << 16) | (g << 8) | b);
  413. }
  414. #endif