spd_sdram.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654
  1. /*
  2. * (C) Copyright 2006 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. * Change log:
  30. *
  31. * 20050101: Eran Liberty (liberty@freescale.com)
  32. * Initial file creating (porting from 85XX & 8260)
  33. * 20060601: Dave Liu (daveliu@freescale.com)
  34. * DDR ECC support
  35. * unify variable names for 83xx
  36. * code cleanup
  37. */
  38. #include <common.h>
  39. #include <asm/processor.h>
  40. #include <i2c.h>
  41. #include <spd.h>
  42. #include <asm/mmu.h>
  43. #include <spd_sdram.h>
  44. #ifdef CONFIG_SPD_EEPROM
  45. #if defined(CONFIG_DDR_ECC) && !defined(CONFIG_ECC_INIT_VIA_DDRC)
  46. extern void dma_init(void);
  47. extern uint dma_check(void);
  48. extern int dma_xfer(void *dest, uint count, void *src);
  49. #endif
  50. #ifndef CFG_READ_SPD
  51. #define CFG_READ_SPD i2c_read
  52. #endif
  53. /*
  54. * Convert picoseconds into clock cycles (rounding up if needed).
  55. */
  56. extern ulong get_ddr_clk(ulong dummy);
  57. int
  58. picos_to_clk(int picos)
  59. {
  60. unsigned int ddr_bus_clk;
  61. int clks;
  62. ddr_bus_clk = get_ddr_clk(0) >> 1;
  63. clks = picos / ((1000000000 / ddr_bus_clk) * 1000);
  64. if (picos % ((1000000000 / ddr_bus_clk) * 1000) !=0) {
  65. clks++;
  66. }
  67. return clks;
  68. }
  69. unsigned int banksize(unsigned char row_dens)
  70. {
  71. return ((row_dens >> 2) | ((row_dens & 3) << 6)) << 24;
  72. }
  73. int read_spd(uint addr)
  74. {
  75. return ((int) addr);
  76. }
  77. #undef SPD_DEBUG
  78. #ifdef SPD_DEBUG
  79. static void spd_debug(spd_eeprom_t *spd)
  80. {
  81. printf ("\nDIMM type: %-18.18s\n", spd->mpart);
  82. printf ("SPD size: %d\n", spd->info_size);
  83. printf ("EEPROM size: %d\n", 1 << spd->chip_size);
  84. printf ("Memory type: %d\n", spd->mem_type);
  85. printf ("Row addr: %d\n", spd->nrow_addr);
  86. printf ("Column addr: %d\n", spd->ncol_addr);
  87. printf ("# of rows: %d\n", spd->nrows);
  88. printf ("Row density: %d\n", spd->row_dens);
  89. printf ("# of banks: %d\n", spd->nbanks);
  90. printf ("Data width: %d\n",
  91. 256 * spd->dataw_msb + spd->dataw_lsb);
  92. printf ("Chip width: %d\n", spd->primw);
  93. printf ("Refresh rate: %02X\n", spd->refresh);
  94. printf ("CAS latencies: %02X\n", spd->cas_lat);
  95. printf ("Write latencies: %02X\n", spd->write_lat);
  96. printf ("tRP: %d\n", spd->trp);
  97. printf ("tRCD: %d\n", spd->trcd);
  98. printf ("\n");
  99. }
  100. #endif /* SPD_DEBUG */
  101. long int spd_sdram()
  102. {
  103. volatile immap_t *immap = (immap_t *)CFG_IMMRBAR;
  104. volatile ddr83xx_t *ddr = &immap->ddr;
  105. volatile law83xx_t *ecm = &immap->sysconf.ddrlaw[0];
  106. spd_eeprom_t spd;
  107. unsigned int memsize;
  108. unsigned int law_size;
  109. unsigned char caslat, caslat_ctrl;
  110. unsigned char burstlen;
  111. unsigned int max_bus_clk;
  112. unsigned int max_data_rate, effective_data_rate;
  113. unsigned int ddrc_clk;
  114. unsigned int refresh_clk;
  115. unsigned sdram_cfg;
  116. unsigned int ddrc_ecc_enable;
  117. /* Read SPD parameters with I2C */
  118. CFG_READ_SPD(SPD_EEPROM_ADDRESS, 0, 1, (uchar *) & spd, sizeof (spd));
  119. #ifdef SPD_DEBUG
  120. spd_debug(&spd);
  121. #endif
  122. /* Check the memory type */
  123. if (spd.mem_type != SPD_MEMTYPE_DDR) {
  124. printf("DDR: Module mem type is %02X\n", spd.mem_type);
  125. return 0;
  126. }
  127. /* Check the number of physical bank */
  128. if (spd.nrows > 2) {
  129. printf("DDR: The number of physical bank is %02X\n", spd.nrows);
  130. return 0;
  131. }
  132. /* Check if the number of row of the module is in the range of DDRC */
  133. if (spd.nrow_addr < 12 || spd.nrow_addr > 14) {
  134. printf("DDR: Row number is out of range of DDRC, row=%02X\n",
  135. spd.nrow_addr);
  136. return 0;
  137. }
  138. /* Check if the number of col of the module is in the range of DDRC */
  139. if (spd.ncol_addr < 8 || spd.ncol_addr > 11) {
  140. printf("DDR: Col number is out of range of DDRC, col=%02X\n",
  141. spd.ncol_addr);
  142. return 0;
  143. }
  144. /* Setup DDR chip select register */
  145. #ifdef CFG_83XX_DDR_USES_CS0
  146. ddr->csbnds[0].csbnds = (banksize(spd.row_dens) >> 24) - 1;
  147. ddr->cs_config[0] = ( 1 << 31
  148. | (spd.nrow_addr - 12) << 8
  149. | (spd.ncol_addr - 8) );
  150. debug("\n");
  151. debug("cs0_bnds = 0x%08x\n",ddr->csbnds[0].csbnds);
  152. debug("cs0_config = 0x%08x\n",ddr->cs_config[0]);
  153. if (spd.nrows == 2) {
  154. ddr->csbnds[1].csbnds = ( (banksize(spd.row_dens) >> 8)
  155. | ((banksize(spd.row_dens) >> 23) - 1) );
  156. ddr->cs_config[1] = ( 1<<31
  157. | (spd.nrow_addr-12) << 8
  158. | (spd.ncol_addr-8) );
  159. debug("cs1_bnds = 0x%08x\n",ddr->csbnds[1].csbnds);
  160. debug("cs1_config = 0x%08x\n",ddr->cs_config[1]);
  161. }
  162. #else
  163. ddr->csbnds[2].csbnds = (banksize(spd.row_dens) >> 24) - 1;
  164. ddr->cs_config[2] = ( 1 << 31
  165. | (spd.nrow_addr - 12) << 8
  166. | (spd.ncol_addr - 8) );
  167. debug("\n");
  168. debug("cs2_bnds = 0x%08x\n",ddr->csbnds[2].csbnds);
  169. debug("cs2_config = 0x%08x\n",ddr->cs_config[2]);
  170. if (spd.nrows == 2) {
  171. ddr->csbnds[3].csbnds = ( (banksize(spd.row_dens) >> 8)
  172. | ((banksize(spd.row_dens) >> 23) - 1) );
  173. ddr->cs_config[3] = ( 1<<31
  174. | (spd.nrow_addr-12) << 8
  175. | (spd.ncol_addr-8) );
  176. debug("cs3_bnds = 0x%08x\n",ddr->csbnds[3].csbnds);
  177. debug("cs3_config = 0x%08x\n",ddr->cs_config[3]);
  178. }
  179. #endif
  180. if (spd.mem_type != 0x07) {
  181. puts("No DDR module found!\n");
  182. return 0;
  183. }
  184. /*
  185. * Figure out memory size in Megabytes.
  186. */
  187. memsize = spd.nrows * banksize(spd.row_dens) / 0x100000;
  188. /*
  189. * First supported LAW size is 16M, at LAWAR_SIZE_16M == 23.
  190. */
  191. law_size = 19 + __ilog2(memsize);
  192. /*
  193. * Set up LAWBAR for all of DDR.
  194. */
  195. ecm->bar = ((CFG_DDR_SDRAM_BASE>>12) & 0xfffff);
  196. ecm->ar = (LAWAR_EN | LAWAR_TRGT_IF_DDR | (LAWAR_SIZE & law_size));
  197. debug("DDR:bar=0x%08x\n", ecm->bar);
  198. debug("DDR:ar=0x%08x\n", ecm->ar);
  199. /*
  200. * Find the largest CAS by locating the highest 1 bit
  201. * in the spd.cas_lat field. Translate it to a DDR
  202. * controller field value:
  203. *
  204. * CAS Lat DDR I Ctrl
  205. * Clocks SPD Bit Value
  206. * -------+--------+---------
  207. * 1.0 0 001
  208. * 1.5 1 010
  209. * 2.0 2 011
  210. * 2.5 3 100
  211. * 3.0 4 101
  212. * 3.5 5 110
  213. * 4.0 6 111
  214. */
  215. caslat = __ilog2(spd.cas_lat);
  216. if (caslat > 6 ) {
  217. printf("DDR: Invalid SPD CAS Latency, caslat=%02X\n",
  218. spd.cas_lat);
  219. return 0;
  220. }
  221. max_bus_clk = 1000 *10 / (((spd.clk_cycle & 0xF0) >> 4) * 10
  222. + (spd.clk_cycle & 0x0f));
  223. max_data_rate = max_bus_clk * 2;
  224. debug("DDR:Module maximum data rate is: %dMhz\n", max_data_rate);
  225. ddrc_clk = get_ddr_clk(0) / 1000000;
  226. if (max_data_rate >= 390) { /* it is DDR 400 */
  227. if (ddrc_clk <= 410 && ddrc_clk > 350) {
  228. /* DDR controller clk at 350~410 */
  229. effective_data_rate = 400; /* 5ns */
  230. caslat = caslat;
  231. } else if (ddrc_clk <= 350 && ddrc_clk > 280) {
  232. /* DDR controller clk at 280~350 */
  233. effective_data_rate = 333; /* 6ns */
  234. if (spd.clk_cycle2 == 0x60) {
  235. caslat = caslat - 1;
  236. } else {
  237. caslat = caslat;
  238. }
  239. } else if (ddrc_clk <= 280 && ddrc_clk > 230) {
  240. /* DDR controller clk at 230~280 */
  241. effective_data_rate = 266; /* 7.5ns */
  242. if (spd.clk_cycle3 == 0x75) {
  243. caslat = caslat - 2;
  244. } else if (spd.clk_cycle2 == 0x60) {
  245. caslat = caslat - 1;
  246. } else {
  247. caslat = caslat;
  248. }
  249. } else if (ddrc_clk <= 230 && ddrc_clk > 90) {
  250. /* DDR controller clk at 90~230 */
  251. effective_data_rate = 200; /* 10ns */
  252. if (spd.clk_cycle3 == 0x75) {
  253. caslat = caslat - 2;
  254. } else if (spd.clk_cycle2 == 0x60) {
  255. caslat = caslat - 1;
  256. } else {
  257. caslat = caslat;
  258. }
  259. }
  260. } else if (max_data_rate >= 323) { /* it is DDR 333 */
  261. if (ddrc_clk <= 350 && ddrc_clk > 280) {
  262. /* DDR controller clk at 280~350 */
  263. effective_data_rate = 333; /* 6ns */
  264. caslat = caslat;
  265. } else if (ddrc_clk <= 280 && ddrc_clk > 230) {
  266. /* DDR controller clk at 230~280 */
  267. effective_data_rate = 266; /* 7.5ns */
  268. if (spd.clk_cycle2 == 0x75) {
  269. caslat = caslat - 1;
  270. } else {
  271. caslat = caslat;
  272. }
  273. } else if (ddrc_clk <= 230 && ddrc_clk > 90) {
  274. /* DDR controller clk at 90~230 */
  275. effective_data_rate = 200; /* 10ns */
  276. if (spd.clk_cycle3 == 0xa0) {
  277. caslat = caslat - 2;
  278. } else if (spd.clk_cycle2 == 0x75) {
  279. caslat = caslat - 1;
  280. } else {
  281. caslat = caslat;
  282. }
  283. }
  284. } else if (max_data_rate >= 256) { /* it is DDR 266 */
  285. if (ddrc_clk <= 350 && ddrc_clk > 280) {
  286. /* DDR controller clk at 280~350 */
  287. printf("DDR: DDR controller freq is more than "
  288. "max data rate of the module\n");
  289. return 0;
  290. } else if (ddrc_clk <= 280 && ddrc_clk > 230) {
  291. /* DDR controller clk at 230~280 */
  292. effective_data_rate = 266; /* 7.5ns */
  293. caslat = caslat;
  294. } else if (ddrc_clk <= 230 && ddrc_clk > 90) {
  295. /* DDR controller clk at 90~230 */
  296. effective_data_rate = 200; /* 10ns */
  297. if (spd.clk_cycle2 == 0xa0) {
  298. caslat = caslat - 1;
  299. }
  300. }
  301. } else if (max_data_rate >= 190) { /* it is DDR 200 */
  302. if (ddrc_clk <= 350 && ddrc_clk > 230) {
  303. /* DDR controller clk at 230~350 */
  304. printf("DDR: DDR controller freq is more than "
  305. "max data rate of the module\n");
  306. return 0;
  307. } else if (ddrc_clk <= 230 && ddrc_clk > 90) {
  308. /* DDR controller clk at 90~230 */
  309. effective_data_rate = 200; /* 10ns */
  310. caslat = caslat;
  311. }
  312. }
  313. debug("DDR:Effective data rate is: %dMhz\n", effective_data_rate);
  314. debug("DDR:The MSB 1 of CAS Latency is: %d\n", caslat);
  315. /*
  316. * Errata DDR6 work around: input enable 2 cycles earlier.
  317. * including MPC834x Rev1.0/1.1 and MPC8360 Rev1.1/1.2.
  318. */
  319. if (caslat == 2) {
  320. ddr->debug_reg = 0x201c0000; /* CL=2 */
  321. } else if (caslat == 3) {
  322. ddr->debug_reg = 0x202c0000; /* CL=2.5 */
  323. } else if (caslat == 4) {
  324. ddr->debug_reg = 0x202c0000; /* CL=3.0 */
  325. }
  326. __asm__ __volatile__ ("sync");
  327. debug("Errata DDR6 (debug_reg=0x%08x)\n", ddr->debug_reg);
  328. /*
  329. * note: caslat must also be programmed into ddr->sdram_mode
  330. * register.
  331. *
  332. * note: WRREC(Twr) and WRTORD(Twtr) are not in SPD,
  333. * use conservative value here.
  334. */
  335. caslat_ctrl = (caslat + 1) & 0x07; /* see as above */
  336. ddr->timing_cfg_1 =
  337. (((picos_to_clk(spd.trp * 250) & 0x07) << 28 ) |
  338. ((picos_to_clk(spd.tras * 1000) & 0x0f ) << 24 ) |
  339. ((picos_to_clk(spd.trcd * 250) & 0x07) << 20 ) |
  340. ((caslat_ctrl & 0x07) << 16 ) |
  341. (((picos_to_clk(spd.trfc * 1000) - 8) & 0x0f) << 12 ) |
  342. ( 0x300 ) |
  343. ((picos_to_clk(spd.trrd * 250) & 0x07) << 4) | 1);
  344. ddr->timing_cfg_2 = 0x00000800;
  345. debug("DDR:timing_cfg_1=0x%08x\n", ddr->timing_cfg_1);
  346. debug("DDR:timing_cfg_2=0x%08x\n", ddr->timing_cfg_2);
  347. /* Setup init value, but not enable */
  348. ddr->sdram_cfg = 0x42000000;
  349. /* Check DIMM data bus width */
  350. if (spd.dataw_lsb == 0x20) {
  351. burstlen = 0x03; /* 32 bit data bus, burst len is 8 */
  352. printf("\n DDR DIMM: data bus width is 32 bit");
  353. } else {
  354. burstlen = 0x02; /* Others act as 64 bit bus, burst len is 4 */
  355. printf("\n DDR DIMM: data bus width is 64 bit");
  356. }
  357. /* Is this an ECC DDR chip? */
  358. if (spd.config == 0x02) {
  359. printf(" with ECC\n");
  360. } else {
  361. printf(" without ECC\n");
  362. }
  363. /* Burst length is always 4 for 64 bit data bus, 8 for 32 bit data bus,
  364. Burst type is sequential
  365. */
  366. switch (caslat) {
  367. case 1:
  368. ddr->sdram_mode = 0x50 | burstlen; /* CL=1.5 */
  369. break;
  370. case 2:
  371. ddr->sdram_mode = 0x20 | burstlen; /* CL=2.0 */
  372. break;
  373. case 3:
  374. ddr->sdram_mode = 0x60 | burstlen; /* CL=2.5 */
  375. break;
  376. case 4:
  377. ddr->sdram_mode = 0x30 | burstlen; /* CL=3.0 */
  378. break;
  379. default:
  380. printf("DDR:only CL 1.5, 2.0, 2.5, 3.0 is supported\n");
  381. return 0;
  382. }
  383. debug("DDR:sdram_mode=0x%08x\n", ddr->sdram_mode);
  384. switch (spd.refresh) {
  385. case 0x00:
  386. case 0x80:
  387. refresh_clk = picos_to_clk(15625000);
  388. break;
  389. case 0x01:
  390. case 0x81:
  391. refresh_clk = picos_to_clk(3900000);
  392. break;
  393. case 0x02:
  394. case 0x82:
  395. refresh_clk = picos_to_clk(7800000);
  396. break;
  397. case 0x03:
  398. case 0x83:
  399. refresh_clk = picos_to_clk(31300000);
  400. break;
  401. case 0x04:
  402. case 0x84:
  403. refresh_clk = picos_to_clk(62500000);
  404. break;
  405. case 0x05:
  406. case 0x85:
  407. refresh_clk = picos_to_clk(125000000);
  408. break;
  409. default:
  410. refresh_clk = 0x512;
  411. break;
  412. }
  413. /*
  414. * Set BSTOPRE to 0x100 for page mode
  415. * If auto-charge is used, set BSTOPRE = 0
  416. */
  417. ddr->sdram_interval = ((refresh_clk & 0x3fff) << 16) | 0x100;
  418. debug("DDR:sdram_interval=0x%08x\n", ddr->sdram_interval);
  419. /* SS_EN = 0, source synchronous disable
  420. * CLK_ADJST = 0, MCK/MCK# is launched aligned with addr/cmd
  421. */
  422. ddr->sdram_clk_cntl = 0x00000000;
  423. debug("DDR:sdram_clk_cntl=0x%08x\n", ddr->sdram_clk_cntl);
  424. asm("sync;isync");
  425. udelay(600);
  426. /*
  427. * Figure out the settings for the sdram_cfg register. Build up
  428. * the value in 'sdram_cfg' before writing since the write into
  429. * the register will actually enable the memory controller, and all
  430. * settings must be done before enabling.
  431. *
  432. * sdram_cfg[0] = 1 (ddr sdram logic enable)
  433. * sdram_cfg[1] = 1 (self-refresh-enable)
  434. * sdram_cfg[6:7] = 2 (SDRAM type = DDR SDRAM)
  435. * sdram_cfg[12] = 0 (32_BE =0 , 64 bit bus mode)
  436. * sdram_cfg[13] = 0 (8_BE =0, 4-beat bursts)
  437. */
  438. sdram_cfg = 0xC2000000;
  439. /* sdram_cfg[3] = RD_EN - registered DIMM enable */
  440. if (spd.mod_attr & 0x02) {
  441. sdram_cfg |= 0x10000000;
  442. }
  443. /* The DIMM is 32bit width */
  444. if (spd.dataw_lsb == 0x20) {
  445. sdram_cfg |= 0x000C0000;
  446. }
  447. ddrc_ecc_enable = 0;
  448. #if defined(CONFIG_DDR_ECC)
  449. /* Enable ECC with sdram_cfg[2] */
  450. if (spd.config == 0x02) {
  451. sdram_cfg |= 0x20000000;
  452. ddrc_ecc_enable = 1;
  453. /* disable error detection */
  454. ddr->err_disable = ~ECC_ERROR_ENABLE;
  455. /* set single bit error threshold to maximum value,
  456. * reset counter to zero */
  457. ddr->err_sbe = (255 << ECC_ERROR_MAN_SBET_SHIFT) |
  458. (0 << ECC_ERROR_MAN_SBEC_SHIFT);
  459. }
  460. debug("DDR:err_disable=0x%08x\n", ddr->err_disable);
  461. debug("DDR:err_sbe=0x%08x\n", ddr->err_sbe);
  462. #endif
  463. printf(" DDRC ECC mode: %s\n", ddrc_ecc_enable ? "ON":"OFF");
  464. #if defined(CONFIG_DDR_2T_TIMING)
  465. /*
  466. * Enable 2T timing by setting sdram_cfg[16].
  467. */
  468. sdram_cfg |= SDRAM_CFG_2T_EN;
  469. #endif
  470. /* Enable controller, and GO! */
  471. ddr->sdram_cfg = sdram_cfg;
  472. asm("sync;isync");
  473. udelay(500);
  474. debug("DDR:sdram_cfg=0x%08x\n", ddr->sdram_cfg);
  475. return memsize; /*in MBytes*/
  476. }
  477. #endif /* CONFIG_SPD_EEPROM */
  478. #if defined(CONFIG_DDR_ECC) && !defined(CONFIG_ECC_INIT_VIA_DDRC)
  479. /*
  480. * Use timebase counter, get_timer() is not availabe
  481. * at this point of initialization yet.
  482. */
  483. static __inline__ unsigned long get_tbms (void)
  484. {
  485. unsigned long tbl;
  486. unsigned long tbu1, tbu2;
  487. unsigned long ms;
  488. unsigned long long tmp;
  489. ulong tbclk = get_tbclk();
  490. /* get the timebase ticks */
  491. do {
  492. asm volatile ("mftbu %0":"=r" (tbu1):);
  493. asm volatile ("mftb %0":"=r" (tbl):);
  494. asm volatile ("mftbu %0":"=r" (tbu2):);
  495. } while (tbu1 != tbu2);
  496. /* convert ticks to ms */
  497. tmp = (unsigned long long)(tbu1);
  498. tmp = (tmp << 32);
  499. tmp += (unsigned long long)(tbl);
  500. ms = tmp/(tbclk/1000);
  501. return ms;
  502. }
  503. /*
  504. * Initialize all of memory for ECC, then enable errors.
  505. */
  506. /* #define CONFIG_DDR_ECC_INIT_VIA_DMA */
  507. void ddr_enable_ecc(unsigned int dram_size)
  508. {
  509. uint *p;
  510. volatile immap_t *immap = (immap_t *)CFG_IMMRBAR;
  511. volatile ddr83xx_t *ddr= &immap->ddr;
  512. unsigned long t_start, t_end;
  513. #if defined(CONFIG_DDR_ECC_INIT_VIA_DMA)
  514. uint i;
  515. #endif
  516. debug("Initialize a Cachline in DRAM\n");
  517. icache_enable();
  518. #if defined(CONFIG_DDR_ECC_INIT_VIA_DMA)
  519. /* Initialise DMA for direct Transfers */
  520. dma_init();
  521. #endif
  522. t_start = get_tbms();
  523. #if !defined(CONFIG_DDR_ECC_INIT_VIA_DMA)
  524. debug("DDR init: Cache flush method\n");
  525. for (p = 0; p < (uint *)(dram_size); p++) {
  526. if (((unsigned int)p & 0x1f) == 0) {
  527. ppcDcbz((unsigned long) p);
  528. }
  529. /* write pattern to cache and flush */
  530. *p = (unsigned int)0xdeadbeef;
  531. if (((unsigned int)p & 0x1c) == 0x1c) {
  532. ppcDcbf((unsigned long) p);
  533. }
  534. }
  535. #else
  536. printf("DDR init: DMA method\n");
  537. for (p = 0; p < (uint *)(8 * 1024); p++) {
  538. /* zero one data cache line */
  539. if (((unsigned int)p & 0x1f) == 0) {
  540. ppcDcbz((unsigned long)p);
  541. }
  542. /* write pattern to it and flush */
  543. *p = (unsigned int)0xdeadbeef;
  544. if (((unsigned int)p & 0x1c) == 0x1c) {
  545. ppcDcbf((unsigned long)p);
  546. }
  547. }
  548. dma_xfer((uint *)0x2000, 0x2000, (uint *)0); /* 8K */
  549. dma_xfer((uint *)0x4000, 0x4000, (uint *)0); /* 16K */
  550. dma_xfer((uint *)0x8000, 0x8000, (uint *)0); /* 32K */
  551. dma_xfer((uint *)0x10000, 0x10000, (uint *)0); /* 64K */
  552. dma_xfer((uint *)0x20000, 0x20000, (uint *)0); /* 128K */
  553. dma_xfer((uint *)0x40000, 0x40000, (uint *)0); /* 256K */
  554. dma_xfer((uint *)0x80000, 0x80000, (uint *)0); /* 512K */
  555. dma_xfer((uint *)0x100000, 0x100000, (uint *)0); /* 1M */
  556. dma_xfer((uint *)0x200000, 0x200000, (uint *)0); /* 2M */
  557. dma_xfer((uint *)0x400000, 0x400000, (uint *)0); /* 4M */
  558. for (i = 1; i < dram_size / 0x800000; i++) {
  559. dma_xfer((uint *)(0x800000*i), 0x800000, (uint *)0);
  560. }
  561. #endif
  562. t_end = get_tbms();
  563. icache_disable();
  564. debug("\nREADY!!\n");
  565. debug("ddr init duration: %ld ms\n", t_end - t_start);
  566. /* Clear All ECC Errors */
  567. if ((ddr->err_detect & ECC_ERROR_DETECT_MME) == ECC_ERROR_DETECT_MME)
  568. ddr->err_detect |= ECC_ERROR_DETECT_MME;
  569. if ((ddr->err_detect & ECC_ERROR_DETECT_MBE) == ECC_ERROR_DETECT_MBE)
  570. ddr->err_detect |= ECC_ERROR_DETECT_MBE;
  571. if ((ddr->err_detect & ECC_ERROR_DETECT_SBE) == ECC_ERROR_DETECT_SBE)
  572. ddr->err_detect |= ECC_ERROR_DETECT_SBE;
  573. if ((ddr->err_detect & ECC_ERROR_DETECT_MSE) == ECC_ERROR_DETECT_MSE)
  574. ddr->err_detect |= ECC_ERROR_DETECT_MSE;
  575. /* Disable ECC-Interrupts */
  576. ddr->err_int_en &= ECC_ERR_INT_DISABLE;
  577. /* Enable errors for ECC */
  578. ddr->err_disable &= ECC_ERROR_ENABLE;
  579. __asm__ __volatile__ ("sync");
  580. __asm__ __volatile__ ("isync");
  581. }
  582. #endif /* CONFIG_DDR_ECC */