sdram.c 12 KB

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