spd_sdram.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  1. /*
  2. * Copyright 2004 Freescale Semiconductor.
  3. * (C) Copyright 2003 Motorola Inc.
  4. * Xianghua Xiao (X.Xiao@motorola.com)
  5. *
  6. * See file CREDITS for list of people who contributed to this
  7. * project.
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License as
  11. * published by the Free Software Foundation; either version 2 of
  12. * the License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  22. * MA 02111-1307 USA
  23. *
  24. * Change log:
  25. *
  26. * 20050101: Eran Liberty (liberty@freescale.com)
  27. * Initial file creating (porting from 85XX & 8260)
  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. #ifdef CONFIG_SPD_EEPROM
  36. #if defined(CONFIG_DDR_ECC)
  37. extern void dma_init(void);
  38. extern uint dma_check(void);
  39. extern int dma_xfer(void *dest, uint count, void *src);
  40. #endif
  41. #ifndef CFG_READ_SPD
  42. #define CFG_READ_SPD i2c_read
  43. #endif
  44. /*
  45. * Convert picoseconds into clock cycles (rounding up if needed).
  46. */
  47. int
  48. picos_to_clk(int picos)
  49. {
  50. int clks;
  51. clks = picos / (2000000000 / (get_bus_freq(0) / 1000));
  52. if (picos % (2000000000 / (get_bus_freq(0) / 1000)) != 0) {
  53. clks++;
  54. }
  55. return clks;
  56. }
  57. unsigned int banksize(unsigned char row_dens)
  58. {
  59. return ((row_dens >> 2) | ((row_dens & 3) << 6)) << 24;
  60. }
  61. int read_spd(uint addr)
  62. {
  63. return ((int) addr);
  64. }
  65. long int spd_sdram(int(read_spd)(uint addr))
  66. {
  67. volatile immap_t *immap = (immap_t *)CFG_IMMRBAR;
  68. volatile ddr8349_t *ddr = &immap->ddr;
  69. volatile law8349_t *ecm = &immap->sysconf.ddrlaw[0];
  70. spd_eeprom_t spd;
  71. unsigned tmp, tmp1;
  72. unsigned int memsize;
  73. unsigned int law_size;
  74. unsigned char caslat;
  75. unsigned int trfc, trfc_clk, trfc_low;
  76. #warning Current spd_sdram does not fit its usage... adjust implementation or API...
  77. CFG_READ_SPD(SPD_EEPROM_ADDRESS, 0, 1, (uchar *) & spd, sizeof (spd));
  78. if (spd.nrows > 2) {
  79. puts("DDR:Only two chip selects are supported on ADS.\n");
  80. return 0;
  81. }
  82. if (spd.nrow_addr < 12
  83. || spd.nrow_addr > 14
  84. || spd.ncol_addr < 8
  85. || spd.ncol_addr > 11) {
  86. puts("DDR:Row or Col number unsupported.\n");
  87. return 0;
  88. }
  89. ddr->csbnds[2].csbnds = (banksize(spd.row_dens) >> 24) - 1;
  90. ddr->cs_config[2] = ( 1 << 31
  91. | (spd.nrow_addr - 12) << 8
  92. | (spd.ncol_addr - 8) );
  93. debug("\n");
  94. debug("cs2_bnds = 0x%08x\n",ddr->csbnds[2].csbnds);
  95. debug("cs2_config = 0x%08x\n",ddr->cs_config[2]);
  96. if (spd.nrows == 2) {
  97. ddr->csbnds[3].csbnds = ( (banksize(spd.row_dens) >> 8)
  98. | ((banksize(spd.row_dens) >> 23) - 1) );
  99. ddr->cs_config[3] = ( 1<<31
  100. | (spd.nrow_addr-12) << 8
  101. | (spd.ncol_addr-8) );
  102. debug("cs3_bnds = 0x%08x\n",ddr->csbnds[3].csbnds);
  103. debug("cs3_config = 0x%08x\n",ddr->cs_config[3]);
  104. }
  105. if (spd.mem_type != 0x07) {
  106. puts("No DDR module found!\n");
  107. return 0;
  108. }
  109. /*
  110. * Figure out memory size in Megabytes.
  111. */
  112. memsize = spd.nrows * banksize(spd.row_dens) / 0x100000;
  113. /*
  114. * First supported LAW size is 16M, at LAWAR_SIZE_16M == 23.
  115. */
  116. law_size = 19 + __ilog2(memsize);
  117. /*
  118. * Set up LAWBAR for all of DDR.
  119. */
  120. ecm->bar = ((CFG_DDR_SDRAM_BASE>>12) & 0xfffff);
  121. ecm->ar = (LAWAR_EN | LAWAR_TRGT_IF_DDR | (LAWAR_SIZE & law_size));
  122. debug("DDR:bar=0x%08x\n", ecm->bar);
  123. debug("DDR:ar=0x%08x\n", ecm->ar);
  124. /*
  125. * find the largest CAS
  126. */
  127. if(spd.cas_lat & 0x40) {
  128. caslat = 7;
  129. } else if (spd.cas_lat & 0x20) {
  130. caslat = 6;
  131. } else if (spd.cas_lat & 0x10) {
  132. caslat = 5;
  133. } else if (spd.cas_lat & 0x08) {
  134. caslat = 4;
  135. } else if (spd.cas_lat & 0x04) {
  136. caslat = 3;
  137. } else if (spd.cas_lat & 0x02) {
  138. caslat = 2;
  139. } else if (spd.cas_lat & 0x01) {
  140. caslat = 1;
  141. } else {
  142. puts("DDR:no valid CAS Latency information.\n");
  143. return 0;
  144. }
  145. tmp = 20000 / (((spd.clk_cycle & 0xF0) >> 4) * 10
  146. + (spd.clk_cycle & 0x0f));
  147. debug("DDR:Module maximum data rate is: %dMhz\n", tmp);
  148. tmp1 = get_bus_freq(0) / 1000000;
  149. if (tmp1 < 230 && tmp1 >= 90 && tmp >= 230) {
  150. /* 90~230 range, treated as DDR 200 */
  151. if (spd.clk_cycle3 == 0xa0)
  152. caslat -= 2;
  153. else if(spd.clk_cycle2 == 0xa0)
  154. caslat--;
  155. } else if (tmp1 < 280 && tmp1 >= 230 && tmp >= 280) {
  156. /* 230-280 range, treated as DDR 266 */
  157. if (spd.clk_cycle3 == 0x75)
  158. caslat -= 2;
  159. else if (spd.clk_cycle2 == 0x75)
  160. caslat--;
  161. } else if (tmp1 < 350 && tmp1 >= 280 && tmp >= 350) {
  162. /* 280~350 range, treated as DDR 333 */
  163. if (spd.clk_cycle3 == 0x60)
  164. caslat -= 2;
  165. else if (spd.clk_cycle2 == 0x60)
  166. caslat--;
  167. } else if (tmp1 < 90 || tmp1 >= 350) {
  168. /* DDR rate out-of-range */
  169. puts("DDR:platform frequency is not fit for DDR rate\n");
  170. return 0;
  171. }
  172. /*
  173. * note: caslat must also be programmed into ddr->sdram_mode
  174. * register.
  175. *
  176. * note: WRREC(Twr) and WRTORD(Twtr) are not in SPD,
  177. * use conservative value here.
  178. */
  179. trfc = spd.trfc * 1000; /* up to ps */
  180. trfc_clk = picos_to_clk(trfc);
  181. trfc_low = (trfc_clk - 8) & 0xf;
  182. ddr->timing_cfg_1 =
  183. (((picos_to_clk(spd.trp * 250) & 0x07) << 28 ) |
  184. ((picos_to_clk(spd.tras * 1000) & 0x0f ) << 24 ) |
  185. ((picos_to_clk(spd.trcd * 250) & 0x07) << 20 ) |
  186. ((caslat & 0x07) << 16 ) |
  187. (trfc_low << 12 ) |
  188. ( 0x300 ) |
  189. ((picos_to_clk(spd.trrd * 250) & 0x07) << 4) | 1);
  190. ddr->timing_cfg_2 = 0x00000800;
  191. debug("DDR:timing_cfg_1=0x%08x\n", ddr->timing_cfg_1);
  192. debug("DDR:timing_cfg_2=0x%08x\n", ddr->timing_cfg_2);
  193. /*
  194. * Only DDR I is supported
  195. * DDR I and II have different mode-register-set definition
  196. */
  197. /* burst length is always 4 */
  198. switch(caslat) {
  199. case 2:
  200. ddr->sdram_mode = 0x52; /* 1.5 */
  201. break;
  202. case 3:
  203. ddr->sdram_mode = 0x22; /* 2.0 */
  204. break;
  205. case 4:
  206. ddr->sdram_mode = 0x62; /* 2.5 */
  207. break;
  208. case 5:
  209. ddr->sdram_mode = 0x32; /* 3.0 */
  210. break;
  211. default:
  212. puts("DDR:only CAS Latency 1.5, 2.0, 2.5, 3.0 is supported.\n");
  213. return 0;
  214. }
  215. debug("DDR:sdram_mode=0x%08x\n", ddr->sdram_mode);
  216. switch(spd.refresh) {
  217. case 0x00:
  218. case 0x80:
  219. tmp = picos_to_clk(15625000);
  220. break;
  221. case 0x01:
  222. case 0x81:
  223. tmp = picos_to_clk(3900000);
  224. break;
  225. case 0x02:
  226. case 0x82:
  227. tmp = picos_to_clk(7800000);
  228. break;
  229. case 0x03:
  230. case 0x83:
  231. tmp = picos_to_clk(31300000);
  232. break;
  233. case 0x04:
  234. case 0x84:
  235. tmp = picos_to_clk(62500000);
  236. break;
  237. case 0x05:
  238. case 0x85:
  239. tmp = picos_to_clk(125000000);
  240. break;
  241. default:
  242. tmp = 0x512;
  243. break;
  244. }
  245. /*
  246. * Set BSTOPRE to 0x100 for page mode
  247. * If auto-charge is used, set BSTOPRE = 0
  248. */
  249. ddr->sdram_interval = ((tmp & 0x3fff) << 16) | 0x100;
  250. debug("DDR:sdram_interval=0x%08x\n", ddr->sdram_interval);
  251. /*
  252. * Is this an ECC DDR chip?
  253. */
  254. #if defined(CONFIG_DDR_ECC)
  255. if (spd.config == 0x02) {
  256. /* disable error detection */
  257. ddr->err_disable = ~ECC_ERROR_ENABLE;
  258. /* set single bit error threshold to maximum value,
  259. * reset counter to zero */
  260. ddr->err_sbe = (255 << ECC_ERROR_MAN_SBET_SHIFT) |
  261. (0 << ECC_ERROR_MAN_SBEC_SHIFT);
  262. }
  263. debug("DDR:err_disable=0x%08x\n", ddr->err_disable);
  264. debug("DDR:err_sbe=0x%08x\n", ddr->err_sbe);
  265. #endif
  266. asm("sync;isync");
  267. udelay(500);
  268. /*
  269. * SS_EN=1,
  270. * CLK_ADJST = 2-MCK/MCK_B, is lauched 1/2 of one SDRAM
  271. * clock cycle after address/command
  272. */
  273. /*ddr->sdram_clk_cntl = 0x82000000;*/
  274. ddr->sdram_clk_cntl = (DDR_SDRAM_CLK_CNTL_SS_EN|DDR_SDRAM_CLK_CNTL_CLK_ADJUST_05);
  275. /*
  276. * Figure out the settings for the sdram_cfg register. Build up
  277. * the entire register in 'tmp' before writing since the write into
  278. * the register will actually enable the memory controller, and all
  279. * settings must be done before enabling.
  280. *
  281. * sdram_cfg[0] = 1 (ddr sdram logic enable)
  282. * sdram_cfg[1] = 1 (self-refresh-enable)
  283. * sdram_cfg[6:7] = 2 (SDRAM type = DDR SDRAM)
  284. */
  285. tmp = 0xc2000000;
  286. /*
  287. * sdram_cfg[3] = RD_EN - registered DIMM enable
  288. * A value of 0x26 indicates micron registered DIMMS (micron.com)
  289. */
  290. if (spd.mod_attr == 0x26) {
  291. tmp |= 0x10000000;
  292. }
  293. #if defined(CONFIG_DDR_ECC)
  294. /*
  295. * If the user wanted ECC (enabled via sdram_cfg[2])
  296. */
  297. if (spd.config == 0x02) {
  298. tmp |= SDRAM_CFG_ECC_EN;
  299. }
  300. #endif
  301. #if defined(CONFIG_DDR_2T_TIMING)
  302. /*
  303. * Enable 2T timing by setting sdram_cfg[16].
  304. */
  305. tmp |= SDRAM_CFG_2T_EN;
  306. #endif
  307. ddr->sdram_cfg = tmp;
  308. asm("sync;isync");
  309. udelay(500);
  310. debug("DDR:sdram_cfg=0x%08x\n", ddr->sdram_cfg);
  311. return memsize;/*in MBytes*/
  312. }
  313. #endif /* CONFIG_SPD_EEPROM */
  314. #if defined(CONFIG_DDR_ECC)
  315. /*
  316. * Use timebase counter, get_timer() is not availabe
  317. * at this point of initialization yet.
  318. */
  319. static __inline__ unsigned long get_tbms (void)
  320. {
  321. unsigned long tbl;
  322. unsigned long tbu1, tbu2;
  323. unsigned long ms;
  324. unsigned long long tmp;
  325. ulong tbclk = get_tbclk();
  326. /* get the timebase ticks */
  327. do {
  328. asm volatile ("mftbu %0":"=r" (tbu1):);
  329. asm volatile ("mftb %0":"=r" (tbl):);
  330. asm volatile ("mftbu %0":"=r" (tbu2):);
  331. } while (tbu1 != tbu2);
  332. /* convert ticks to ms */
  333. tmp = (unsigned long long)(tbu1);
  334. tmp = (tmp << 32);
  335. tmp += (unsigned long long)(tbl);
  336. ms = tmp/(tbclk/1000);
  337. return ms;
  338. }
  339. /*
  340. * Initialize all of memory for ECC, then enable errors.
  341. */
  342. //#define CONFIG_DDR_ECC_INIT_VIA_DMA
  343. void ddr_enable_ecc(unsigned int dram_size)
  344. {
  345. uint *p;
  346. volatile immap_t *immap = (immap_t *)CFG_IMMRBAR;
  347. volatile ddr8349_t *ddr = &immap->ddr;
  348. unsigned long t_start, t_end;
  349. #if defined(CONFIG_DDR_ECC_INIT_VIA_DMA)
  350. uint i;
  351. #endif
  352. debug("Initialize a Cachline in DRAM\n");
  353. icache_enable();
  354. #if defined(CONFIG_DDR_ECC_INIT_VIA_DMA)
  355. /* Initialise DMA for direct Transfers */
  356. dma_init();
  357. #endif
  358. t_start = get_tbms();
  359. #if !defined(CONFIG_DDR_ECC_INIT_VIA_DMA)
  360. debug("DDR init: Cache flush method\n");
  361. for (p = 0; p < (uint *)(dram_size); p++) {
  362. if (((unsigned int)p & 0x1f) == 0) {
  363. ppcDcbz((unsigned long) p);
  364. }
  365. /* write pattern to cache and flush */
  366. *p = (unsigned int)0xdeadbeef;
  367. if (((unsigned int)p & 0x1c) == 0x1c) {
  368. ppcDcbf((unsigned long) p);
  369. }
  370. }
  371. #else
  372. printf("DDR init: DMA method\n");
  373. for (p = 0; p < (uint *)(8 * 1024); p++) {
  374. /* zero one data cache line */
  375. if (((unsigned int)p & 0x1f) == 0) {
  376. ppcDcbz((unsigned long)p);
  377. }
  378. /* write pattern to it and flush */
  379. *p = (unsigned int)0xdeadbeef;
  380. if (((unsigned int)p & 0x1c) == 0x1c) {
  381. ppcDcbf((unsigned long)p);
  382. }
  383. }
  384. /* 8K */
  385. dma_xfer((uint *)0x2000, 0x2000, (uint *)0);
  386. /* 16K */
  387. dma_xfer((uint *)0x4000, 0x4000, (uint *)0);
  388. /* 32K */
  389. dma_xfer((uint *)0x8000, 0x8000, (uint *)0);
  390. /* 64K */
  391. dma_xfer((uint *)0x10000, 0x10000, (uint *)0);
  392. /* 128k */
  393. dma_xfer((uint *)0x20000, 0x20000, (uint *)0);
  394. /* 256k */
  395. dma_xfer((uint *)0x40000, 0x40000, (uint *)0);
  396. /* 512k */
  397. dma_xfer((uint *)0x80000, 0x80000, (uint *)0);
  398. /* 1M */
  399. dma_xfer((uint *)0x100000, 0x100000, (uint *)0);
  400. /* 2M */
  401. dma_xfer((uint *)0x200000, 0x200000, (uint *)0);
  402. /* 4M */
  403. dma_xfer((uint *)0x400000, 0x400000, (uint *)0);
  404. for (i = 1; i < dram_size / 0x800000; i++) {
  405. dma_xfer((uint *)(0x800000*i), 0x800000, (uint *)0);
  406. }
  407. #endif
  408. t_end = get_tbms();
  409. icache_disable();
  410. debug("\nREADY!!\n");
  411. debug("ddr init duration: %ld ms\n", t_end - t_start);
  412. /* Clear All ECC Errors */
  413. if ((ddr->err_detect & ECC_ERROR_DETECT_MME) == ECC_ERROR_DETECT_MME)
  414. ddr->err_detect |= ECC_ERROR_DETECT_MME;
  415. if ((ddr->err_detect & ECC_ERROR_DETECT_MBE) == ECC_ERROR_DETECT_MBE)
  416. ddr->err_detect |= ECC_ERROR_DETECT_MBE;
  417. if ((ddr->err_detect & ECC_ERROR_DETECT_SBE) == ECC_ERROR_DETECT_SBE)
  418. ddr->err_detect |= ECC_ERROR_DETECT_SBE;
  419. if ((ddr->err_detect & ECC_ERROR_DETECT_MSE) == ECC_ERROR_DETECT_MSE)
  420. ddr->err_detect |= ECC_ERROR_DETECT_MSE;
  421. /* Disable ECC-Interrupts */
  422. ddr->err_int_en &= ECC_ERR_INT_DISABLE;
  423. /* Enable errors for ECC */
  424. ddr->err_disable &= ECC_ERROR_ENABLE;
  425. __asm__ __volatile__ ("sync");
  426. __asm__ __volatile__ ("isync");
  427. }
  428. #endif /* CONFIG_DDR_ECC */