spd_sdram.c 25 KB

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