sdram.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  1. /*
  2. * (C) Copyright 2005
  3. * Stefan Roese, DENX Software Engineering, sr@denx.de.
  4. *
  5. * See file CREDITS for list of people who contributed to this
  6. * project.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation; either version 2 of
  11. * the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  21. * MA 02111-1307 USA
  22. */
  23. #include <common.h>
  24. #include <asm/processor.h>
  25. #include <asm/immap_85xx.h>
  26. #include <asm/processor.h>
  27. #include <asm/mmu.h>
  28. struct sdram_conf_s {
  29. unsigned long size;
  30. unsigned long reg;
  31. #ifdef CONFIG_TQM8548
  32. unsigned long refresh;
  33. #endif /* CONFIG_TQM8548 */
  34. };
  35. typedef struct sdram_conf_s sdram_conf_t;
  36. #ifdef CONFIG_TQM8548
  37. #ifdef CONFIG_TQM8548_AG
  38. sdram_conf_t ddr_cs_conf[] = {
  39. {(1024 << 20), 0x80044202, 0x0002D000}, /* 1024MB, 14x10(4) */
  40. { (512 << 20), 0x80044102, 0x0001A000}, /* 512MB, 13x10(4) */
  41. { (256 << 20), 0x80040102, 0x00014000}, /* 256MB, 13x10(4) */
  42. { (128 << 20), 0x80040101, 0x0000C000}, /* 128MB, 13x9(4) */
  43. };
  44. #else /* !CONFIG_TQM8548_AG */
  45. sdram_conf_t ddr_cs_conf[] = {
  46. {(512 << 20), 0x80044102, 0x0001A000}, /* 512MB, 13x10(4) */
  47. {(256 << 20), 0x80040102, 0x00014000}, /* 256MB, 13x10(4) */
  48. {(128 << 20), 0x80040101, 0x0000C000}, /* 128MB, 13x9(4) */
  49. };
  50. #endif /* CONFIG_TQM8548_AG */
  51. #else /* !CONFIG_TQM8548 */
  52. sdram_conf_t ddr_cs_conf[] = {
  53. {(512 << 20), 0x80000202}, /* 512MB, 14x10(4) */
  54. {(256 << 20), 0x80000102}, /* 256MB, 13x10(4) */
  55. {(128 << 20), 0x80000101}, /* 128MB, 13x9(4) */
  56. {( 64 << 20), 0x80000001}, /* 64MB, 12x9(4) */
  57. };
  58. #endif /* CONFIG_TQM8548 */
  59. #define N_DDR_CS_CONF (sizeof(ddr_cs_conf) / sizeof(ddr_cs_conf[0]))
  60. int cas_latency (void);
  61. /*
  62. * Autodetect onboard DDR SDRAM on 85xx platforms
  63. *
  64. * NOTE: Some of the hardcoded values are hardware dependant,
  65. * so this should be extended for other future boards
  66. * using this routine!
  67. */
  68. long int sdram_setup (int casl)
  69. {
  70. int i;
  71. volatile ccsr_ddr_t *ddr = (void *)(CONFIG_SYS_MPC85xx_DDR_ADDR);
  72. #ifdef CONFIG_TQM8548
  73. volatile ccsr_gur_t *gur = (void *)(CONFIG_SYS_MPC85xx_GUTS_ADDR);
  74. #if defined(CONFIG_TQM8548_AG) || defined(CONFIG_TQM8548_BE)
  75. volatile ccsr_local_ecm_t *ecm = (void *)(CONFIG_SYS_MPC85xx_ECM_ADDR);
  76. #endif
  77. #else /* !CONFIG_TQM8548 */
  78. unsigned long cfg_ddr_timing1;
  79. unsigned long cfg_ddr_mode;
  80. #endif /* CONFIG_TQM8548 */
  81. /*
  82. * Disable memory controller.
  83. */
  84. ddr->cs0_config = 0;
  85. ddr->sdram_cfg = 0;
  86. #ifdef CONFIG_TQM8548
  87. /* Timing and refresh settings for DDR2-533 and below */
  88. ddr->cs0_bnds = (ddr_cs_conf[0].size - 1) >> 24;
  89. ddr->cs0_config = ddr_cs_conf[0].reg;
  90. ddr->timing_cfg_3 = 0x00020000;
  91. /* TIMING CFG 1, 533MHz
  92. * PRETOACT: 4 Clocks
  93. * ACTTOPRE: 12 Clocks
  94. * ACTTORW: 4 Clocks
  95. * CASLAT: 4 Clocks
  96. * REFREC: EXT_REFREC:REFREC 53 Clocks
  97. * WRREC: 4 Clocks
  98. * ACTTOACT: 3 Clocks
  99. * WRTORD: 2 Clocks
  100. */
  101. ddr->timing_cfg_1 = 0x4C47D432;
  102. /* TIMING CFG 2, 533MHz
  103. * ADD_LAT: 3 Clocks
  104. * CPO: READLAT + 1
  105. * WR_LAT: 3 Clocks
  106. * RD_TO_PRE: 2 Clocks
  107. * WR_DATA_DELAY: 1/2 Clock
  108. * CKE_PLS: 3 Clock
  109. * FOUR_ACT: 14 Clocks
  110. */
  111. ddr->timing_cfg_2 = 0x331848CE;
  112. /* DDR SDRAM Mode, 533MHz
  113. * MRS: Extended Mode Register
  114. * OUT: Outputs enabled
  115. * RDQS: no
  116. * DQS: enabled
  117. * OCD: default state
  118. * RTT: 75 Ohms
  119. * Posted CAS: 3 Clocks
  120. * ODS: reduced strength
  121. * DLL: enabled
  122. * MR: Mode Register
  123. * PD: fast exit
  124. * WR: 4 Clocks
  125. * DLL: no DLL reset
  126. * TM: normal
  127. * CAS latency: 4 Clocks
  128. * BT: sequential
  129. * Burst length: 4
  130. */
  131. ddr->sdram_mode = 0x439E0642;
  132. /* DDR SDRAM Interval, 533MHz
  133. * REFINT: 1040 Clocks
  134. * BSTOPRE: 256
  135. */
  136. ddr->sdram_interval = (1040 << 16) | 0x100;
  137. /*
  138. * Workaround for erratum DDR19 according to MPC8548 Device Errata
  139. * document, Rev. 1: DDR IO receiver must be set to an acceptable
  140. * bias point by modifying a hidden register.
  141. */
  142. if (SVR_REV (get_svr ()) < 0x21)
  143. gur->ddrioovcr = 0x90000000; /* enable, VSEL 1.8V */
  144. /* DDR SDRAM CFG 2
  145. * FRC_SR: normal mode
  146. * SR_IE: no self-refresh interrupt
  147. * DLL_RST_DIS: don't care, leave at reset value
  148. * DQS_CFG: differential DQS signals
  149. * ODT_CFG: assert ODT to internal IOs only during reads to DRAM
  150. * LVWx_CFG: don't care, leave at reset value
  151. * NUM_PR: 1 refresh will be issued at a time
  152. * DM_CFG: don't care, leave at reset value
  153. * D_INIT: no data initialization
  154. */
  155. ddr->sdram_cfg_2 = 0x04401000;
  156. /* DDR SDRAM MODE 2
  157. * MRS: Extended Mode Register 2
  158. */
  159. ddr->sdram_mode_2 = 0x8000C000;
  160. /* DDR SDRAM CLK CNTL
  161. * CLK_ADJUST: 1/2 Clock 0x02000000
  162. * CLK_ADJUST: 5/8 Clock 0x02800000
  163. */
  164. ddr->sdram_clk_cntl = 0x02800000;
  165. /* wait for clock stabilization */
  166. asm ("sync;isync;msync");
  167. udelay (1000);
  168. #if defined(CONFIG_TQM8548_AG) || defined(CONFIG_TQM8548_BE)
  169. /*
  170. * Workaround for erratum DDR20 according to MPC8548 Device Errata
  171. * document, Rev. 1: "CKE signal may not function correctly after
  172. * assertion of HRESET"
  173. */
  174. /* 1. Configure DDR register as is done in normal DDR configuration.
  175. * Do not set DDR_SDRAM_CFG[MEM_EN].
  176. *
  177. * 2. Set reserved bit EEBACR[3] at offset 0x1000
  178. */
  179. ecm->eebacr |= 0x10000000;
  180. /*
  181. * 3. Before DDR_SDRAM_CFG[MEM_EN] is set, write DDR_SDRAM_CFG_2[D_INIT]
  182. *
  183. * DDR_SDRAM_CFG_2:
  184. * FRC_SR: normal mode
  185. * SR_IE: no self-refresh interrupt
  186. * DLL_RST_DIS: don't care, leave at reset value
  187. * DQS_CFG: differential DQS signals
  188. * ODT_CFG: assert ODT to internal IOs only during reads to DRAM
  189. * LVWx_CFG: don't care, leave at reset value
  190. * NUM_PR: 1 refresh will be issued at a time
  191. * DM_CFG: don't care, leave at reset value
  192. * D_INIT: enable data initialization
  193. */
  194. ddr->sdram_cfg_2 |= 0x00000010;
  195. /*
  196. * 4. Before DDR_SDRAM_CFG[MEM_EN] set, write D3[21] to disable data
  197. * training
  198. */
  199. ddr->debug_3 |= 0x00000400;
  200. /*
  201. * 5. Wait 200 micro-seconds
  202. */
  203. udelay (200);
  204. /*
  205. * 6. Set DDR_SDRAM_CFG[MEM_EN]
  206. *
  207. * BTW, initialize DDR_SDRAM_CFG:
  208. * MEM_EN: enabled
  209. * SREN: don't care, leave at reset value
  210. * ECC_EN: no error report
  211. * RD_EN: no registered DIMMs
  212. * SDRAM_TYPE: DDR2
  213. * DYN_PWR: no power management
  214. * 32_BE: don't care, leave at reset value
  215. * 8_BE: 4 beat burst
  216. * NCAP: don't care, leave at reset value
  217. * 2T_EN: 1T Timing
  218. * BA_INTLV_CTL: no interleaving
  219. * x32_EN: x16 organization
  220. * PCHB8: MA[10] for auto-precharge
  221. * HSE: half strength for single and 2-layer stacks
  222. * (full strength for 3- and 4-layer stacks not
  223. * yet considered)
  224. * MEM_HALT: no halt
  225. * BI: automatic initialization
  226. */
  227. ddr->sdram_cfg = 0x83000008;
  228. /*
  229. * 7. Poll DDR_SDRAM_CFG_2[D_INIT] until it is cleared by hardware
  230. */
  231. asm ("sync;isync;msync");
  232. while (ddr->sdram_cfg_2 & 0x00000010)
  233. asm ("eieio");
  234. /*
  235. * 8. Clear D3[21] to re-enable data training
  236. */
  237. ddr->debug_3 &= ~0x00000400;
  238. /*
  239. * 9. Set D2(21) to force data training to run
  240. */
  241. ddr->debug_2 |= 0x00000400;
  242. /*
  243. * 10. Poll on D2[21] until it is cleared by hardware
  244. */
  245. asm ("sync;isync;msync");
  246. while (ddr->debug_2 & 0x00000400)
  247. asm ("eieio");
  248. /*
  249. * 11. Clear reserved bit EEBACR[3] at offset 0x1000
  250. */
  251. ecm->eebacr &= ~0x10000000;
  252. #else /* !(CONFIG_TQM8548_AG || CONFIG_TQM8548_BE) */
  253. /* DDR SDRAM CLK CNTL
  254. * MEM_EN: enabled
  255. * SREN: don't care, leave at reset value
  256. * ECC_EN: no error report
  257. * RD_EN: no register DIMMs
  258. * SDRAM_TYPE: DDR2
  259. * DYN_PWR: no power management
  260. * 32_BE: don't care, leave at reset value
  261. * 8_BE: 4 beat burst
  262. * NCAP: don't care, leave at reset value
  263. * 2T_EN: 1T Timing
  264. * BA_INTLV_CTL: no interleaving
  265. * x32_EN: x16 organization
  266. * PCHB8: MA[10] for auto-precharge
  267. * HSE: half strength for single and 2-layer stacks
  268. * (full strength for 3- and 4-layer stacks no yet considered)
  269. * MEM_HALT: no halt
  270. * BI: automatic initialization
  271. */
  272. ddr->sdram_cfg = 0x83000008;
  273. #endif /* CONFIG_TQM8548_AG || CONFIG_TQM8548_BE */
  274. asm ("sync; isync; msync");
  275. udelay (1000);
  276. #else /* !CONFIG_TQM8548 */
  277. switch (casl) {
  278. case 20:
  279. cfg_ddr_timing1 = 0x47405331 | (3 << 16);
  280. cfg_ddr_mode = 0x40020002 | (2 << 4);
  281. break;
  282. case 25:
  283. cfg_ddr_timing1 = 0x47405331 | (4 << 16);
  284. cfg_ddr_mode = 0x40020002 | (6 << 4);
  285. break;
  286. case 30:
  287. default:
  288. cfg_ddr_timing1 = 0x47405331 | (5 << 16);
  289. cfg_ddr_mode = 0x40020002 | (3 << 4);
  290. break;
  291. }
  292. ddr->cs0_bnds = (ddr_cs_conf[0].size - 1) >> 24;
  293. ddr->cs0_config = ddr_cs_conf[0].reg;
  294. ddr->timing_cfg_1 = cfg_ddr_timing1;
  295. ddr->timing_cfg_2 = 0x00000800; /* P9-45,may need tuning */
  296. ddr->sdram_mode = cfg_ddr_mode;
  297. ddr->sdram_interval = 0x05160100; /* autocharge,no open page */
  298. ddr->err_disable = 0x0000000D;
  299. asm ("sync; isync; msync");
  300. udelay (1000);
  301. ddr->sdram_cfg = 0xc2000000; /* unbuffered,no DYN_PWR */
  302. asm ("sync; isync; msync");
  303. udelay (1000);
  304. #endif /* CONFIG_TQM8548 */
  305. for (i = 0; i < N_DDR_CS_CONF; i++) {
  306. ddr->cs0_config = ddr_cs_conf[i].reg;
  307. if (get_ram_size (0, ddr_cs_conf[i].size) ==
  308. ddr_cs_conf[i].size) {
  309. /*
  310. * size detected -> set Chip Select Bounds Register
  311. */
  312. ddr->cs0_bnds = (ddr_cs_conf[i].size - 1) >> 24;
  313. break;
  314. }
  315. }
  316. #ifdef CONFIG_TQM8548
  317. if (i < N_DDR_CS_CONF) {
  318. /* Adjust refresh rate for DDR2 */
  319. ddr->timing_cfg_3 = ddr_cs_conf[i].refresh & 0x00070000;
  320. ddr->timing_cfg_1 = (ddr->timing_cfg_1 & 0xFFFF0FFF) |
  321. (ddr_cs_conf[i].refresh & 0x0000F000);
  322. return ddr_cs_conf[i].size;
  323. }
  324. #endif /* CONFIG_TQM8548 */
  325. /* return size if detected, else return 0 */
  326. return (i < N_DDR_CS_CONF) ? ddr_cs_conf[i].size : 0;
  327. }
  328. void board_add_ram_info (int use_default)
  329. {
  330. int casl;
  331. if (use_default)
  332. casl = CONFIG_DDR_DEFAULT_CL;
  333. else
  334. casl = cas_latency ();
  335. puts (" (CL=");
  336. switch (casl) {
  337. case 20:
  338. puts ("2)");
  339. break;
  340. case 25:
  341. puts ("2.5)");
  342. break;
  343. case 30:
  344. puts ("3)");
  345. break;
  346. }
  347. }
  348. phys_size_t initdram (int board_type)
  349. {
  350. long dram_size = 0;
  351. int casl;
  352. #if defined(CONFIG_DDR_DLL)
  353. /*
  354. * This DLL-Override only used on TQM8540 and TQM8560
  355. */
  356. {
  357. volatile ccsr_gur_t *gur = (void *)(CONFIG_SYS_MPC85xx_GUTS_ADDR);
  358. int i, x;
  359. x = 10;
  360. /*
  361. * Work around to stabilize DDR DLL
  362. */
  363. gur->ddrdllcr = 0x81000000;
  364. asm ("sync; isync; msync");
  365. udelay (200);
  366. while (gur->ddrdllcr != 0x81000100) {
  367. gur->devdisr = gur->devdisr | 0x00010000;
  368. asm ("sync; isync; msync");
  369. for (i = 0; i < x; i++)
  370. ;
  371. gur->devdisr = gur->devdisr & 0xfff7ffff;
  372. asm ("sync; isync; msync");
  373. x++;
  374. }
  375. }
  376. #endif
  377. casl = cas_latency ();
  378. dram_size = sdram_setup (casl);
  379. if ((dram_size == 0) && (casl != CONFIG_DDR_DEFAULT_CL)) {
  380. /*
  381. * Try again with default CAS latency
  382. */
  383. puts ("Problem with CAS lantency");
  384. board_add_ram_info (1);
  385. puts (", using default CL!\n");
  386. casl = CONFIG_DDR_DEFAULT_CL;
  387. dram_size = sdram_setup (casl);
  388. puts (" ");
  389. }
  390. return dram_size;
  391. }
  392. #if defined(CONFIG_SYS_DRAM_TEST)
  393. int testdram (void)
  394. {
  395. uint *pstart = (uint *) CONFIG_SYS_MEMTEST_START;
  396. uint *pend = (uint *) CONFIG_SYS_MEMTEST_END;
  397. uint *p;
  398. printf ("SDRAM test phase 1:\n");
  399. for (p = pstart; p < pend; p++)
  400. *p = 0xaaaaaaaa;
  401. for (p = pstart; p < pend; p++) {
  402. if (*p != 0xaaaaaaaa) {
  403. printf ("SDRAM test fails at: %08x\n", (uint) p);
  404. return 1;
  405. }
  406. }
  407. printf ("SDRAM test phase 2:\n");
  408. for (p = pstart; p < pend; p++)
  409. *p = 0x55555555;
  410. for (p = pstart; p < pend; p++) {
  411. if (*p != 0x55555555) {
  412. printf ("SDRAM test fails at: %08x\n", (uint) p);
  413. return 1;
  414. }
  415. }
  416. printf ("SDRAM test passed.\n");
  417. return 0;
  418. }
  419. #endif