spd_sdram.c 26 KB

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