spd_sdram.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * (C) Copyright 2006-2007 Freescale Semiconductor, Inc.
  4. *
  5. * (C) Copyright 2006
  6. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  7. *
  8. * Copyright (C) 2004-2006 Freescale Semiconductor, Inc.
  9. * (C) Copyright 2003 Motorola Inc.
  10. * Xianghua Xiao (X.Xiao@motorola.com)
  11. */
  12. #include <common.h>
  13. #include <asm/processor.h>
  14. #include <asm/io.h>
  15. #include <i2c.h>
  16. #include <spd.h>
  17. #include <asm/mmu.h>
  18. #include <spd_sdram.h>
  19. DECLARE_GLOBAL_DATA_PTR;
  20. void board_add_ram_info(int use_default)
  21. {
  22. volatile immap_t *immap = (immap_t *) CONFIG_SYS_IMMR;
  23. volatile ddr83xx_t *ddr = &immap->ddr;
  24. char buf[32];
  25. printf(" (DDR%d", ((ddr->sdram_cfg & SDRAM_CFG_SDRAM_TYPE_MASK)
  26. >> SDRAM_CFG_SDRAM_TYPE_SHIFT) - 1);
  27. #if defined(CONFIG_MPC8308) || defined(CONFIG_MPC831x)
  28. if ((ddr->sdram_cfg & SDRAM_CFG_DBW_MASK) == SDRAM_CFG_DBW_16)
  29. puts(", 16-bit");
  30. else if ((ddr->sdram_cfg & SDRAM_CFG_DBW_MASK) == SDRAM_CFG_DBW_32)
  31. puts(", 32-bit");
  32. else
  33. puts(", unknown width");
  34. #else
  35. if (ddr->sdram_cfg & SDRAM_CFG_32_BE)
  36. puts(", 32-bit");
  37. else
  38. puts(", 64-bit");
  39. #endif
  40. if (ddr->sdram_cfg & SDRAM_CFG_ECC_EN)
  41. puts(", ECC on");
  42. else
  43. puts(", ECC off");
  44. printf(", %s MHz)", strmhz(buf, gd->mem_clk));
  45. #if defined(CONFIG_SYS_LB_SDRAM) && defined(CONFIG_SYS_LBC_SDRAM_SIZE)
  46. puts("\nSDRAM: ");
  47. print_size (CONFIG_SYS_LBC_SDRAM_SIZE * 1024 * 1024, " (local bus)");
  48. #endif
  49. }
  50. #ifdef CONFIG_SPD_EEPROM
  51. #ifndef CONFIG_SYS_READ_SPD
  52. #define CONFIG_SYS_READ_SPD i2c_read
  53. #endif
  54. #ifndef SPD_EEPROM_OFFSET
  55. #define SPD_EEPROM_OFFSET 0
  56. #endif
  57. #ifndef SPD_EEPROM_ADDR_LEN
  58. #define SPD_EEPROM_ADDR_LEN 1
  59. #endif
  60. /*
  61. * Convert picoseconds into clock cycles (rounding up if needed).
  62. */
  63. int
  64. picos_to_clk(int picos)
  65. {
  66. unsigned int mem_bus_clk;
  67. int clks;
  68. mem_bus_clk = gd->mem_clk >> 1;
  69. clks = picos / (1000000000 / (mem_bus_clk / 1000));
  70. if (picos % (1000000000 / (mem_bus_clk / 1000)) != 0)
  71. clks++;
  72. return clks;
  73. }
  74. unsigned int banksize(unsigned char row_dens)
  75. {
  76. return ((row_dens >> 2) | ((row_dens & 3) << 6)) << 24;
  77. }
  78. int read_spd(uint addr)
  79. {
  80. return ((int) addr);
  81. }
  82. #undef SPD_DEBUG
  83. #ifdef SPD_DEBUG
  84. static void spd_debug(spd_eeprom_t *spd)
  85. {
  86. printf ("\nDIMM type: %-18.18s\n", spd->mpart);
  87. printf ("SPD size: %d\n", spd->info_size);
  88. printf ("EEPROM size: %d\n", 1 << spd->chip_size);
  89. printf ("Memory type: %d\n", spd->mem_type);
  90. printf ("Row addr: %d\n", spd->nrow_addr);
  91. printf ("Column addr: %d\n", spd->ncol_addr);
  92. printf ("# of rows: %d\n", spd->nrows);
  93. printf ("Row density: %d\n", spd->row_dens);
  94. printf ("# of banks: %d\n", spd->nbanks);
  95. printf ("Data width: %d\n",
  96. 256 * spd->dataw_msb + spd->dataw_lsb);
  97. printf ("Chip width: %d\n", spd->primw);
  98. printf ("Refresh rate: %02X\n", spd->refresh);
  99. printf ("CAS latencies: %02X\n", spd->cas_lat);
  100. printf ("Write latencies: %02X\n", spd->write_lat);
  101. printf ("tRP: %d\n", spd->trp);
  102. printf ("tRCD: %d\n", spd->trcd);
  103. printf ("\n");
  104. }
  105. #endif /* SPD_DEBUG */
  106. long int spd_sdram()
  107. {
  108. volatile immap_t *immap = (immap_t *)CONFIG_SYS_IMMR;
  109. volatile ddr83xx_t *ddr = &immap->ddr;
  110. volatile law83xx_t *ecm = &immap->sysconf.ddrlaw[0];
  111. spd_eeprom_t spd;
  112. unsigned int n_ranks;
  113. unsigned int odt_rd_cfg, odt_wr_cfg;
  114. unsigned char twr_clk, twtr_clk;
  115. unsigned int sdram_type;
  116. unsigned int memsize;
  117. unsigned int law_size;
  118. unsigned char caslat, caslat_ctrl;
  119. unsigned int trfc, trfc_clk, trfc_low;
  120. unsigned int trcd_clk, trtp_clk;
  121. unsigned char cke_min_clk;
  122. unsigned char add_lat, wr_lat;
  123. unsigned char wr_data_delay;
  124. unsigned char four_act;
  125. unsigned char cpo;
  126. unsigned char burstlen;
  127. unsigned char odt_cfg, mode_odt_enable;
  128. unsigned int max_bus_clk;
  129. unsigned int max_data_rate, effective_data_rate;
  130. unsigned int ddrc_clk;
  131. unsigned int refresh_clk;
  132. unsigned int sdram_cfg;
  133. unsigned int ddrc_ecc_enable;
  134. unsigned int pvr = get_pvr();
  135. /*
  136. * First disable the memory controller (could be enabled
  137. * by the debugger)
  138. */
  139. clrsetbits_be32(&ddr->sdram_cfg, SDRAM_CFG_MEM_EN, 0);
  140. sync();
  141. isync();
  142. /* Read SPD parameters with I2C */
  143. CONFIG_SYS_READ_SPD(SPD_EEPROM_ADDRESS, SPD_EEPROM_OFFSET,
  144. SPD_EEPROM_ADDR_LEN, (uchar *) &spd, sizeof(spd));
  145. #ifdef SPD_DEBUG
  146. spd_debug(&spd);
  147. #endif
  148. /* Check the memory type */
  149. if (spd.mem_type != SPD_MEMTYPE_DDR && spd.mem_type != SPD_MEMTYPE_DDR2) {
  150. debug("DDR: Module mem type is %02X\n", spd.mem_type);
  151. return 0;
  152. }
  153. /* Check the number of physical bank */
  154. if (spd.mem_type == SPD_MEMTYPE_DDR) {
  155. n_ranks = spd.nrows;
  156. } else {
  157. n_ranks = (spd.nrows & 0x7) + 1;
  158. }
  159. if (n_ranks > 2) {
  160. printf("DDR: The number of physical bank is %02X\n", n_ranks);
  161. return 0;
  162. }
  163. /* Check if the number of row of the module is in the range of DDRC */
  164. if (spd.nrow_addr < 12 || spd.nrow_addr > 15) {
  165. printf("DDR: Row number is out of range of DDRC, row=%02X\n",
  166. spd.nrow_addr);
  167. return 0;
  168. }
  169. /* Check if the number of col of the module is in the range of DDRC */
  170. if (spd.ncol_addr < 8 || spd.ncol_addr > 11) {
  171. printf("DDR: Col number is out of range of DDRC, col=%02X\n",
  172. spd.ncol_addr);
  173. return 0;
  174. }
  175. #ifdef CONFIG_SYS_DDRCDR_VALUE
  176. /*
  177. * Adjust DDR II IO voltage biasing. It just makes it work.
  178. */
  179. if(spd.mem_type == SPD_MEMTYPE_DDR2) {
  180. immap->sysconf.ddrcdr = CONFIG_SYS_DDRCDR_VALUE;
  181. }
  182. udelay(50000);
  183. #endif
  184. /*
  185. * ODT configuration recommendation from DDR Controller Chapter.
  186. */
  187. odt_rd_cfg = 0; /* Never assert ODT */
  188. odt_wr_cfg = 0; /* Never assert ODT */
  189. if (spd.mem_type == SPD_MEMTYPE_DDR2) {
  190. odt_wr_cfg = 1; /* Assert ODT on writes to CSn */
  191. }
  192. /* Setup DDR chip select register */
  193. #ifdef CONFIG_SYS_83XX_DDR_USES_CS0
  194. ddr->csbnds[0].csbnds = (banksize(spd.row_dens) >> 24) - 1;
  195. ddr->cs_config[0] = ( 1 << 31
  196. | (odt_rd_cfg << 20)
  197. | (odt_wr_cfg << 16)
  198. | ((spd.nbanks == 8 ? 1 : 0) << 14)
  199. | ((spd.nrow_addr - 12) << 8)
  200. | (spd.ncol_addr - 8) );
  201. debug("\n");
  202. debug("cs0_bnds = 0x%08x\n",ddr->csbnds[0].csbnds);
  203. debug("cs0_config = 0x%08x\n",ddr->cs_config[0]);
  204. if (n_ranks == 2) {
  205. ddr->csbnds[1].csbnds = ( (banksize(spd.row_dens) >> 8)
  206. | ((banksize(spd.row_dens) >> 23) - 1) );
  207. ddr->cs_config[1] = ( 1<<31
  208. | (odt_rd_cfg << 20)
  209. | (odt_wr_cfg << 16)
  210. | ((spd.nbanks == 8 ? 1 : 0) << 14)
  211. | ((spd.nrow_addr - 12) << 8)
  212. | (spd.ncol_addr - 8) );
  213. debug("cs1_bnds = 0x%08x\n",ddr->csbnds[1].csbnds);
  214. debug("cs1_config = 0x%08x\n",ddr->cs_config[1]);
  215. }
  216. #else
  217. ddr->csbnds[2].csbnds = (banksize(spd.row_dens) >> 24) - 1;
  218. ddr->cs_config[2] = ( 1 << 31
  219. | (odt_rd_cfg << 20)
  220. | (odt_wr_cfg << 16)
  221. | ((spd.nbanks == 8 ? 1 : 0) << 14)
  222. | ((spd.nrow_addr - 12) << 8)
  223. | (spd.ncol_addr - 8) );
  224. debug("\n");
  225. debug("cs2_bnds = 0x%08x\n",ddr->csbnds[2].csbnds);
  226. debug("cs2_config = 0x%08x\n",ddr->cs_config[2]);
  227. if (n_ranks == 2) {
  228. ddr->csbnds[3].csbnds = ( (banksize(spd.row_dens) >> 8)
  229. | ((banksize(spd.row_dens) >> 23) - 1) );
  230. ddr->cs_config[3] = ( 1<<31
  231. | (odt_rd_cfg << 20)
  232. | (odt_wr_cfg << 16)
  233. | ((spd.nbanks == 8 ? 1 : 0) << 14)
  234. | ((spd.nrow_addr - 12) << 8)
  235. | (spd.ncol_addr - 8) );
  236. debug("cs3_bnds = 0x%08x\n",ddr->csbnds[3].csbnds);
  237. debug("cs3_config = 0x%08x\n",ddr->cs_config[3]);
  238. }
  239. #endif
  240. /*
  241. * Figure out memory size in Megabytes.
  242. */
  243. memsize = n_ranks * banksize(spd.row_dens) / 0x100000;
  244. /*
  245. * First supported LAW size is 16M, at LAWAR_SIZE_16M == 23.
  246. */
  247. law_size = 19 + __ilog2(memsize);
  248. /*
  249. * Set up LAWBAR for all of DDR.
  250. */
  251. ecm->bar = CONFIG_SYS_DDR_SDRAM_BASE & 0xfffff000;
  252. ecm->ar = (LAWAR_EN | LAWAR_TRGT_IF_DDR | (LAWAR_SIZE & law_size));
  253. debug("DDR:bar=0x%08x\n", ecm->bar);
  254. debug("DDR:ar=0x%08x\n", ecm->ar);
  255. /*
  256. * Find the largest CAS by locating the highest 1 bit
  257. * in the spd.cas_lat field. Translate it to a DDR
  258. * controller field value:
  259. *
  260. * CAS Lat DDR I DDR II Ctrl
  261. * Clocks SPD Bit SPD Bit Value
  262. * ------- ------- ------- -----
  263. * 1.0 0 0001
  264. * 1.5 1 0010
  265. * 2.0 2 2 0011
  266. * 2.5 3 0100
  267. * 3.0 4 3 0101
  268. * 3.5 5 0110
  269. * 4.0 6 4 0111
  270. * 4.5 1000
  271. * 5.0 5 1001
  272. */
  273. caslat = __ilog2(spd.cas_lat);
  274. if ((spd.mem_type == SPD_MEMTYPE_DDR)
  275. && (caslat > 6)) {
  276. printf("DDR I: Invalid SPD CAS Latency: 0x%x.\n", spd.cas_lat);
  277. return 0;
  278. } else if (spd.mem_type == SPD_MEMTYPE_DDR2
  279. && (caslat < 2 || caslat > 5)) {
  280. printf("DDR II: Invalid SPD CAS Latency: 0x%x.\n",
  281. spd.cas_lat);
  282. return 0;
  283. }
  284. debug("DDR: caslat SPD bit is %d\n", caslat);
  285. max_bus_clk = 1000 *10 / (((spd.clk_cycle & 0xF0) >> 4) * 10
  286. + (spd.clk_cycle & 0x0f));
  287. max_data_rate = max_bus_clk * 2;
  288. debug("DDR:Module maximum data rate is: %d MHz\n", max_data_rate);
  289. ddrc_clk = gd->mem_clk / 1000000;
  290. effective_data_rate = 0;
  291. if (max_data_rate >= 460) { /* it is DDR2-800, 667, 533 */
  292. if (spd.cas_lat & 0x08)
  293. caslat = 3;
  294. else
  295. caslat = 4;
  296. if (ddrc_clk <= 460 && ddrc_clk > 350)
  297. effective_data_rate = 400;
  298. else if (ddrc_clk <=350 && ddrc_clk > 280)
  299. effective_data_rate = 333;
  300. else if (ddrc_clk <= 280 && ddrc_clk > 230)
  301. effective_data_rate = 266;
  302. else
  303. effective_data_rate = 200;
  304. } else if (max_data_rate >= 390 && max_data_rate < 460) { /* it is DDR 400 */
  305. if (ddrc_clk <= 460 && ddrc_clk > 350) {
  306. /* DDR controller clk at 350~460 */
  307. effective_data_rate = 400; /* 5ns */
  308. caslat = caslat;
  309. } else if (ddrc_clk <= 350 && ddrc_clk > 280) {
  310. /* DDR controller clk at 280~350 */
  311. effective_data_rate = 333; /* 6ns */
  312. if (spd.clk_cycle2 == 0x60)
  313. caslat = caslat - 1;
  314. else
  315. caslat = caslat;
  316. } else if (ddrc_clk <= 280 && ddrc_clk > 230) {
  317. /* DDR controller clk at 230~280 */
  318. effective_data_rate = 266; /* 7.5ns */
  319. if (spd.clk_cycle3 == 0x75)
  320. caslat = caslat - 2;
  321. else if (spd.clk_cycle2 == 0x75)
  322. caslat = caslat - 1;
  323. else
  324. caslat = caslat;
  325. } else if (ddrc_clk <= 230 && ddrc_clk > 90) {
  326. /* DDR controller clk at 90~230 */
  327. effective_data_rate = 200; /* 10ns */
  328. if (spd.clk_cycle3 == 0xa0)
  329. caslat = caslat - 2;
  330. else if (spd.clk_cycle2 == 0xa0)
  331. caslat = caslat - 1;
  332. else
  333. caslat = caslat;
  334. }
  335. } else if (max_data_rate >= 323) { /* it is DDR 333 */
  336. if (ddrc_clk <= 350 && ddrc_clk > 280) {
  337. /* DDR controller clk at 280~350 */
  338. effective_data_rate = 333; /* 6ns */
  339. caslat = caslat;
  340. } else if (ddrc_clk <= 280 && ddrc_clk > 230) {
  341. /* DDR controller clk at 230~280 */
  342. effective_data_rate = 266; /* 7.5ns */
  343. if (spd.clk_cycle2 == 0x75)
  344. caslat = caslat - 1;
  345. else
  346. caslat = caslat;
  347. } else if (ddrc_clk <= 230 && ddrc_clk > 90) {
  348. /* DDR controller clk at 90~230 */
  349. effective_data_rate = 200; /* 10ns */
  350. if (spd.clk_cycle3 == 0xa0)
  351. caslat = caslat - 2;
  352. else if (spd.clk_cycle2 == 0xa0)
  353. caslat = caslat - 1;
  354. else
  355. caslat = caslat;
  356. }
  357. } else if (max_data_rate >= 256) { /* it is DDR 266 */
  358. if (ddrc_clk <= 350 && ddrc_clk > 280) {
  359. /* DDR controller clk at 280~350 */
  360. printf("DDR: DDR controller freq is more than "
  361. "max data rate of the module\n");
  362. return 0;
  363. } else if (ddrc_clk <= 280 && ddrc_clk > 230) {
  364. /* DDR controller clk at 230~280 */
  365. effective_data_rate = 266; /* 7.5ns */
  366. caslat = caslat;
  367. } else if (ddrc_clk <= 230 && ddrc_clk > 90) {
  368. /* DDR controller clk at 90~230 */
  369. effective_data_rate = 200; /* 10ns */
  370. if (spd.clk_cycle2 == 0xa0)
  371. caslat = caslat - 1;
  372. }
  373. } else if (max_data_rate >= 190) { /* it is DDR 200 */
  374. if (ddrc_clk <= 350 && ddrc_clk > 230) {
  375. /* DDR controller clk at 230~350 */
  376. printf("DDR: DDR controller freq is more than "
  377. "max data rate of the module\n");
  378. return 0;
  379. } else if (ddrc_clk <= 230 && ddrc_clk > 90) {
  380. /* DDR controller clk at 90~230 */
  381. effective_data_rate = 200; /* 10ns */
  382. caslat = caslat;
  383. }
  384. }
  385. debug("DDR:Effective data rate is: %dMHz\n", effective_data_rate);
  386. debug("DDR:The MSB 1 of CAS Latency is: %d\n", caslat);
  387. /*
  388. * Errata DDR6 work around: input enable 2 cycles earlier.
  389. * including MPC834x Rev1.0/1.1 and MPC8360 Rev1.1/1.2.
  390. */
  391. if(PVR_MAJ(pvr) <= 1 && spd.mem_type == SPD_MEMTYPE_DDR){
  392. if (caslat == 2)
  393. ddr->debug_reg = 0x201c0000; /* CL=2 */
  394. else if (caslat == 3)
  395. ddr->debug_reg = 0x202c0000; /* CL=2.5 */
  396. else if (caslat == 4)
  397. ddr->debug_reg = 0x202c0000; /* CL=3.0 */
  398. __asm__ __volatile__ ("sync");
  399. debug("Errata DDR6 (debug_reg=0x%08x)\n", ddr->debug_reg);
  400. }
  401. /*
  402. * Convert caslat clocks to DDR controller value.
  403. * Force caslat_ctrl to be DDR Controller field-sized.
  404. */
  405. if (spd.mem_type == SPD_MEMTYPE_DDR) {
  406. caslat_ctrl = (caslat + 1) & 0x07;
  407. } else {
  408. caslat_ctrl = (2 * caslat - 1) & 0x0f;
  409. }
  410. debug("DDR: effective data rate is %d MHz\n", effective_data_rate);
  411. debug("DDR: caslat SPD bit is %d, controller field is 0x%x\n",
  412. caslat, caslat_ctrl);
  413. /*
  414. * Timing Config 0.
  415. * Avoid writing for DDR I.
  416. */
  417. if (spd.mem_type == SPD_MEMTYPE_DDR2) {
  418. unsigned char taxpd_clk = 8; /* By the book. */
  419. unsigned char tmrd_clk = 2; /* By the book. */
  420. unsigned char act_pd_exit = 2; /* Empirical? */
  421. unsigned char pre_pd_exit = 6; /* Empirical? */
  422. ddr->timing_cfg_0 = (0
  423. | ((act_pd_exit & 0x7) << 20) /* ACT_PD_EXIT */
  424. | ((pre_pd_exit & 0x7) << 16) /* PRE_PD_EXIT */
  425. | ((taxpd_clk & 0xf) << 8) /* ODT_PD_EXIT */
  426. | ((tmrd_clk & 0xf) << 0) /* MRS_CYC */
  427. );
  428. debug("DDR: timing_cfg_0 = 0x%08x\n", ddr->timing_cfg_0);
  429. }
  430. /*
  431. * For DDR I, WRREC(Twr) and WRTORD(Twtr) are not in SPD,
  432. * use conservative value.
  433. * For DDR II, they are bytes 36 and 37, in quarter nanos.
  434. */
  435. if (spd.mem_type == SPD_MEMTYPE_DDR) {
  436. twr_clk = 3; /* Clocks */
  437. twtr_clk = 1; /* Clocks */
  438. } else {
  439. twr_clk = picos_to_clk(spd.twr * 250);
  440. twtr_clk = picos_to_clk(spd.twtr * 250);
  441. if (twtr_clk < 2)
  442. twtr_clk = 2;
  443. }
  444. /*
  445. * Calculate Trfc, in picos.
  446. * DDR I: Byte 42 straight up in ns.
  447. * DDR II: Byte 40 and 42 swizzled some, in ns.
  448. */
  449. if (spd.mem_type == SPD_MEMTYPE_DDR) {
  450. trfc = spd.trfc * 1000; /* up to ps */
  451. } else {
  452. unsigned int byte40_table_ps[8] = {
  453. 0,
  454. 250,
  455. 330,
  456. 500,
  457. 660,
  458. 750,
  459. 0,
  460. 0
  461. };
  462. trfc = (((spd.trctrfc_ext & 0x1) * 256) + spd.trfc) * 1000
  463. + byte40_table_ps[(spd.trctrfc_ext >> 1) & 0x7];
  464. }
  465. trfc_clk = picos_to_clk(trfc);
  466. /*
  467. * Trcd, Byte 29, from quarter nanos to ps and clocks.
  468. */
  469. trcd_clk = picos_to_clk(spd.trcd * 250) & 0x7;
  470. /*
  471. * Convert trfc_clk to DDR controller fields. DDR I should
  472. * fit in the REFREC field (16-19) of TIMING_CFG_1, but the
  473. * 83xx controller has an extended REFREC field of three bits.
  474. * The controller automatically adds 8 clocks to this value,
  475. * so preadjust it down 8 first before splitting it up.
  476. */
  477. trfc_low = (trfc_clk - 8) & 0xf;
  478. ddr->timing_cfg_1 =
  479. (((picos_to_clk(spd.trp * 250) & 0x07) << 28 ) | /* PRETOACT */
  480. ((picos_to_clk(spd.tras * 1000) & 0x0f ) << 24 ) | /* ACTTOPRE */
  481. (trcd_clk << 20 ) | /* ACTTORW */
  482. (caslat_ctrl << 16 ) | /* CASLAT */
  483. (trfc_low << 12 ) | /* REFEC */
  484. ((twr_clk & 0x07) << 8) | /* WRRREC */
  485. ((picos_to_clk(spd.trrd * 250) & 0x07) << 4) | /* ACTTOACT */
  486. ((twtr_clk & 0x07) << 0) /* WRTORD */
  487. );
  488. /*
  489. * Additive Latency
  490. * For DDR I, 0.
  491. * For DDR II, with ODT enabled, use "a value" less than ACTTORW,
  492. * which comes from Trcd, and also note that:
  493. * add_lat + caslat must be >= 4
  494. */
  495. add_lat = 0;
  496. if (spd.mem_type == SPD_MEMTYPE_DDR2
  497. && (odt_wr_cfg || odt_rd_cfg)
  498. && (caslat < 4)) {
  499. add_lat = 4 - caslat;
  500. if ((add_lat + caslat) < 4) {
  501. add_lat = 0;
  502. }
  503. }
  504. /*
  505. * Write Data Delay
  506. * Historically 0x2 == 4/8 clock delay.
  507. * Empirically, 0x3 == 6/8 clock delay is suggested for DDR I 266.
  508. */
  509. wr_data_delay = 2;
  510. #ifdef CONFIG_SYS_DDR_WRITE_DATA_DELAY
  511. wr_data_delay = CONFIG_SYS_DDR_WRITE_DATA_DELAY;
  512. #endif
  513. /*
  514. * Write Latency
  515. * Read to Precharge
  516. * Minimum CKE Pulse Width.
  517. * Four Activate Window
  518. */
  519. if (spd.mem_type == SPD_MEMTYPE_DDR) {
  520. /*
  521. * This is a lie. It should really be 1, but if it is
  522. * set to 1, bits overlap into the old controller's
  523. * otherwise unused ACSM field. If we leave it 0, then
  524. * the HW will magically treat it as 1 for DDR 1. Oh Yea.
  525. */
  526. wr_lat = 0;
  527. trtp_clk = 2; /* By the book. */
  528. cke_min_clk = 1; /* By the book. */
  529. four_act = 1; /* By the book. */
  530. } else {
  531. wr_lat = caslat - 1;
  532. /* Convert SPD value from quarter nanos to picos. */
  533. trtp_clk = picos_to_clk(spd.trtp * 250);
  534. if (trtp_clk < 2)
  535. trtp_clk = 2;
  536. trtp_clk += add_lat;
  537. cke_min_clk = 3; /* By the book. */
  538. four_act = picos_to_clk(37500); /* By the book. 1k pages? */
  539. }
  540. /*
  541. * Empirically set ~MCAS-to-preamble override for DDR 2.
  542. * Your mileage will vary.
  543. */
  544. cpo = 0;
  545. if (spd.mem_type == SPD_MEMTYPE_DDR2) {
  546. #ifdef CONFIG_SYS_DDR_CPO
  547. cpo = CONFIG_SYS_DDR_CPO;
  548. #else
  549. if (effective_data_rate == 266) {
  550. cpo = 0x4; /* READ_LAT + 1/2 */
  551. } else if (effective_data_rate == 333) {
  552. cpo = 0x6; /* READ_LAT + 1 */
  553. } else if (effective_data_rate == 400) {
  554. cpo = 0x7; /* READ_LAT + 5/4 */
  555. } else {
  556. /* Automatic calibration */
  557. cpo = 0x1f;
  558. }
  559. #endif
  560. }
  561. ddr->timing_cfg_2 = (0
  562. | ((add_lat & 0x7) << 28) /* ADD_LAT */
  563. | ((cpo & 0x1f) << 23) /* CPO */
  564. | ((wr_lat & 0x7) << 19) /* WR_LAT */
  565. | ((trtp_clk & 0x7) << 13) /* RD_TO_PRE */
  566. | ((wr_data_delay & 0x7) << 10) /* WR_DATA_DELAY */
  567. | ((cke_min_clk & 0x7) << 6) /* CKE_PLS */
  568. | ((four_act & 0x1f) << 0) /* FOUR_ACT */
  569. );
  570. debug("DDR:timing_cfg_1=0x%08x\n", ddr->timing_cfg_1);
  571. debug("DDR:timing_cfg_2=0x%08x\n", ddr->timing_cfg_2);
  572. /* Check DIMM data bus width */
  573. if (spd.dataw_lsb < 64) {
  574. if (spd.mem_type == SPD_MEMTYPE_DDR)
  575. burstlen = 0x03; /* 32 bit data bus, burst len is 8 */
  576. else
  577. burstlen = 0x02; /* 32 bit data bus, burst len is 4 */
  578. debug("\n DDR DIMM: data bus width is 32 bit");
  579. } else {
  580. burstlen = 0x02; /* Others act as 64 bit bus, burst len is 4 */
  581. debug("\n DDR DIMM: data bus width is 64 bit");
  582. }
  583. /* Is this an ECC DDR chip? */
  584. if (spd.config == 0x02)
  585. debug(" with ECC\n");
  586. else
  587. debug(" without ECC\n");
  588. /* Burst length is always 4 for 64 bit data bus, 8 for 32 bit data bus,
  589. Burst type is sequential
  590. */
  591. if (spd.mem_type == SPD_MEMTYPE_DDR) {
  592. switch (caslat) {
  593. case 1:
  594. ddr->sdram_mode = 0x50 | burstlen; /* CL=1.5 */
  595. break;
  596. case 2:
  597. ddr->sdram_mode = 0x20 | burstlen; /* CL=2.0 */
  598. break;
  599. case 3:
  600. ddr->sdram_mode = 0x60 | burstlen; /* CL=2.5 */
  601. break;
  602. case 4:
  603. ddr->sdram_mode = 0x30 | burstlen; /* CL=3.0 */
  604. break;
  605. default:
  606. printf("DDR:only CL 1.5, 2.0, 2.5, 3.0 is supported\n");
  607. return 0;
  608. }
  609. } else {
  610. mode_odt_enable = 0x0; /* Default disabled */
  611. if (odt_wr_cfg || odt_rd_cfg) {
  612. /*
  613. * Bits 6 and 2 in Extended MRS(1)
  614. * Bit 2 == 0x04 == 75 Ohm, with 2 DIMM modules.
  615. * Bit 6 == 0x40 == 150 Ohm, with 1 DIMM module.
  616. */
  617. mode_odt_enable = 0x40; /* 150 Ohm */
  618. }
  619. ddr->sdram_mode =
  620. (0
  621. | (1 << (16 + 10)) /* DQS Differential disable */
  622. #ifdef CONFIG_SYS_DDR_MODE_WEAK
  623. | (1 << (16 + 1)) /* weak driver (~60%) */
  624. #endif
  625. | (add_lat << (16 + 3)) /* Additive Latency in EMRS1 */
  626. | (mode_odt_enable << 16) /* ODT Enable in EMRS1 */
  627. | ((twr_clk - 1) << 9) /* Write Recovery Autopre */
  628. | (caslat << 4) /* caslat */
  629. | (burstlen << 0) /* Burst length */
  630. );
  631. }
  632. debug("DDR:sdram_mode=0x%08x\n", ddr->sdram_mode);
  633. /*
  634. * Clear EMRS2 and EMRS3.
  635. */
  636. ddr->sdram_mode2 = 0;
  637. debug("DDR: sdram_mode2 = 0x%08x\n", ddr->sdram_mode2);
  638. switch (spd.refresh) {
  639. case 0x00:
  640. case 0x80:
  641. refresh_clk = picos_to_clk(15625000);
  642. break;
  643. case 0x01:
  644. case 0x81:
  645. refresh_clk = picos_to_clk(3900000);
  646. break;
  647. case 0x02:
  648. case 0x82:
  649. refresh_clk = picos_to_clk(7800000);
  650. break;
  651. case 0x03:
  652. case 0x83:
  653. refresh_clk = picos_to_clk(31300000);
  654. break;
  655. case 0x04:
  656. case 0x84:
  657. refresh_clk = picos_to_clk(62500000);
  658. break;
  659. case 0x05:
  660. case 0x85:
  661. refresh_clk = picos_to_clk(125000000);
  662. break;
  663. default:
  664. refresh_clk = 0x512;
  665. break;
  666. }
  667. /*
  668. * Set BSTOPRE to 0x100 for page mode
  669. * If auto-charge is used, set BSTOPRE = 0
  670. */
  671. ddr->sdram_interval = ((refresh_clk & 0x3fff) << 16) | 0x100;
  672. debug("DDR:sdram_interval=0x%08x\n", ddr->sdram_interval);
  673. /*
  674. * SDRAM Cfg 2
  675. */
  676. odt_cfg = 0;
  677. #ifndef CONFIG_NEVER_ASSERT_ODT_TO_CPU
  678. if (odt_rd_cfg | odt_wr_cfg) {
  679. odt_cfg = 0x2; /* ODT to IOs during reads */
  680. }
  681. #endif
  682. if (spd.mem_type == SPD_MEMTYPE_DDR2) {
  683. ddr->sdram_cfg2 = (0
  684. | (0 << 26) /* True DQS */
  685. | (odt_cfg << 21) /* ODT only read */
  686. | (1 << 12) /* 1 refresh at a time */
  687. );
  688. debug("DDR: sdram_cfg2 = 0x%08x\n", ddr->sdram_cfg2);
  689. }
  690. #ifdef CONFIG_SYS_DDR_SDRAM_CLK_CNTL /* Optional platform specific value */
  691. ddr->sdram_clk_cntl = CONFIG_SYS_DDR_SDRAM_CLK_CNTL;
  692. #endif
  693. debug("DDR:sdram_clk_cntl=0x%08x\n", ddr->sdram_clk_cntl);
  694. asm("sync;isync");
  695. udelay(600);
  696. /*
  697. * Figure out the settings for the sdram_cfg register. Build up
  698. * the value in 'sdram_cfg' before writing since the write into
  699. * the register will actually enable the memory controller, and all
  700. * settings must be done before enabling.
  701. *
  702. * sdram_cfg[0] = 1 (ddr sdram logic enable)
  703. * sdram_cfg[1] = 1 (self-refresh-enable)
  704. * sdram_cfg[5:7] = (SDRAM type = DDR SDRAM)
  705. * 010 DDR 1 SDRAM
  706. * 011 DDR 2 SDRAM
  707. * sdram_cfg[12] = 0 (32_BE =0 , 64 bit bus mode)
  708. * sdram_cfg[13] = 0 (8_BE =0, 4-beat bursts)
  709. */
  710. if (spd.mem_type == SPD_MEMTYPE_DDR)
  711. sdram_type = SDRAM_CFG_SDRAM_TYPE_DDR1;
  712. else
  713. sdram_type = SDRAM_CFG_SDRAM_TYPE_DDR2;
  714. sdram_cfg = (0
  715. | SDRAM_CFG_MEM_EN /* DDR enable */
  716. | SDRAM_CFG_SREN /* Self refresh */
  717. | sdram_type /* SDRAM type */
  718. );
  719. /* sdram_cfg[3] = RD_EN - registered DIMM enable */
  720. if (spd.mod_attr & 0x02)
  721. sdram_cfg |= SDRAM_CFG_RD_EN;
  722. /* The DIMM is 32bit width */
  723. if (spd.dataw_lsb < 64) {
  724. if (spd.mem_type == SPD_MEMTYPE_DDR)
  725. sdram_cfg |= SDRAM_CFG_32_BE | SDRAM_CFG_8_BE;
  726. if (spd.mem_type == SPD_MEMTYPE_DDR2)
  727. sdram_cfg |= SDRAM_CFG_32_BE;
  728. }
  729. ddrc_ecc_enable = 0;
  730. #if defined(CONFIG_DDR_ECC)
  731. /* Enable ECC with sdram_cfg[2] */
  732. if (spd.config == 0x02) {
  733. sdram_cfg |= 0x20000000;
  734. ddrc_ecc_enable = 1;
  735. /* disable error detection */
  736. ddr->err_disable = ~ECC_ERROR_ENABLE;
  737. /* set single bit error threshold to maximum value,
  738. * reset counter to zero */
  739. ddr->err_sbe = (255 << ECC_ERROR_MAN_SBET_SHIFT) |
  740. (0 << ECC_ERROR_MAN_SBEC_SHIFT);
  741. }
  742. debug("DDR:err_disable=0x%08x\n", ddr->err_disable);
  743. debug("DDR:err_sbe=0x%08x\n", ddr->err_sbe);
  744. #endif
  745. debug(" DDRC ECC mode: %s\n", ddrc_ecc_enable ? "ON":"OFF");
  746. #if defined(CONFIG_DDR_2T_TIMING)
  747. /*
  748. * Enable 2T timing by setting sdram_cfg[16].
  749. */
  750. sdram_cfg |= SDRAM_CFG_2T_EN;
  751. #endif
  752. /* Enable controller, and GO! */
  753. ddr->sdram_cfg = sdram_cfg;
  754. asm("sync;isync");
  755. udelay(500);
  756. debug("DDR:sdram_cfg=0x%08x\n", ddr->sdram_cfg);
  757. return memsize; /*in MBytes*/
  758. }
  759. #endif /* CONFIG_SPD_EEPROM */
  760. #if defined(CONFIG_DDR_ECC) && !defined(CONFIG_ECC_INIT_VIA_DDRCONTROLLER)
  761. /*
  762. * Use timebase counter, get_timer() is not available
  763. * at this point of initialization yet.
  764. */
  765. static __inline__ unsigned long get_tbms (void)
  766. {
  767. unsigned long tbl;
  768. unsigned long tbu1, tbu2;
  769. unsigned long ms;
  770. unsigned long long tmp;
  771. ulong tbclk = get_tbclk();
  772. /* get the timebase ticks */
  773. do {
  774. asm volatile ("mftbu %0":"=r" (tbu1):);
  775. asm volatile ("mftb %0":"=r" (tbl):);
  776. asm volatile ("mftbu %0":"=r" (tbu2):);
  777. } while (tbu1 != tbu2);
  778. /* convert ticks to ms */
  779. tmp = (unsigned long long)(tbu1);
  780. tmp = (tmp << 32);
  781. tmp += (unsigned long long)(tbl);
  782. ms = tmp/(tbclk/1000);
  783. return ms;
  784. }
  785. /*
  786. * Initialize all of memory for ECC, then enable errors.
  787. */
  788. void ddr_enable_ecc(unsigned int dram_size)
  789. {
  790. volatile immap_t *immap = (immap_t *)CONFIG_SYS_IMMR;
  791. volatile ddr83xx_t *ddr= &immap->ddr;
  792. unsigned long t_start, t_end;
  793. register u64 *p;
  794. register uint size;
  795. unsigned int pattern[2];
  796. icache_enable();
  797. t_start = get_tbms();
  798. pattern[0] = 0xdeadbeef;
  799. pattern[1] = 0xdeadbeef;
  800. #if defined(CONFIG_DDR_ECC_INIT_VIA_DMA)
  801. dma_meminit(pattern[0], dram_size);
  802. #else
  803. debug("ddr init: CPU FP write method\n");
  804. size = dram_size;
  805. for (p = 0; p < (u64*)(size); p++) {
  806. ppcDWstore((u32*)p, pattern);
  807. }
  808. __asm__ __volatile__ ("sync");
  809. #endif
  810. t_end = get_tbms();
  811. icache_disable();
  812. debug("\nREADY!!\n");
  813. debug("ddr init duration: %ld ms\n", t_end - t_start);
  814. /* Clear All ECC Errors */
  815. if ((ddr->err_detect & ECC_ERROR_DETECT_MME) == ECC_ERROR_DETECT_MME)
  816. ddr->err_detect |= ECC_ERROR_DETECT_MME;
  817. if ((ddr->err_detect & ECC_ERROR_DETECT_MBE) == ECC_ERROR_DETECT_MBE)
  818. ddr->err_detect |= ECC_ERROR_DETECT_MBE;
  819. if ((ddr->err_detect & ECC_ERROR_DETECT_SBE) == ECC_ERROR_DETECT_SBE)
  820. ddr->err_detect |= ECC_ERROR_DETECT_SBE;
  821. if ((ddr->err_detect & ECC_ERROR_DETECT_MSE) == ECC_ERROR_DETECT_MSE)
  822. ddr->err_detect |= ECC_ERROR_DETECT_MSE;
  823. /* Disable ECC-Interrupts */
  824. ddr->err_int_en &= ECC_ERR_INT_DISABLE;
  825. /* Enable errors for ECC */
  826. ddr->err_disable &= ECC_ERROR_ENABLE;
  827. __asm__ __volatile__ ("sync");
  828. __asm__ __volatile__ ("isync");
  829. }
  830. #endif /* CONFIG_DDR_ECC */