spd_sdram.c 25 KB

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