sdram.c 12 KB

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