spd_sdram.c 25 KB

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