spd_sdram.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  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. #include <common.h>
  25. #include <asm/processor.h>
  26. #include <i2c.h>
  27. #include <spd.h>
  28. #include <asm/mmu.h>
  29. #ifdef CONFIG_SPD_EEPROM
  30. #if defined(CONFIG_DDR_ECC)
  31. extern void dma_init(void);
  32. extern uint dma_check(void);
  33. extern int dma_xfer(void *dest, uint count, void *src);
  34. #endif
  35. #ifndef CFG_READ_SPD
  36. #define CFG_READ_SPD i2c_read
  37. #endif
  38. /*
  39. * Convert picoseconds into clock cycles (rounding up if needed).
  40. */
  41. int
  42. picos_to_clk(int picos)
  43. {
  44. int clks;
  45. clks = picos / (2000000000 / (get_bus_freq(0) / 1000));
  46. if (picos % (2000000000 / (get_bus_freq(0) / 1000)) != 0) {
  47. clks++;
  48. }
  49. return clks;
  50. }
  51. unsigned int
  52. banksize(unsigned char row_dens)
  53. {
  54. return ((row_dens >> 2) | ((row_dens & 3) << 6)) << 24;
  55. }
  56. long int
  57. spd_sdram(void)
  58. {
  59. volatile immap_t *immap = (immap_t *)CFG_IMMR;
  60. volatile ccsr_ddr_t *ddr = &immap->im_ddr;
  61. volatile ccsr_local_ecm_t *ecm = &immap->im_local_ecm;
  62. spd_eeprom_t spd;
  63. unsigned tmp, tmp1;
  64. unsigned int memsize;
  65. unsigned int tlb_size;
  66. unsigned int law_size;
  67. unsigned char caslat;
  68. unsigned int ram_tlb_index;
  69. unsigned int ram_tlb_address;
  70. CFG_READ_SPD(SPD_EEPROM_ADDRESS, 0, 1, (uchar *) & spd, sizeof (spd));
  71. if (spd.nrows > 2) {
  72. puts("DDR:Only two chip selects are supported on ADS.\n");
  73. return 0;
  74. }
  75. if (spd.nrow_addr < 12
  76. || spd.nrow_addr > 14
  77. || spd.ncol_addr < 8
  78. || spd.ncol_addr > 11) {
  79. puts("DDR:Row or Col number unsupported.\n");
  80. return 0;
  81. }
  82. ddr->cs0_bnds = (banksize(spd.row_dens) >> 24) - 1;
  83. ddr->cs0_config = ( 1 << 31
  84. | (spd.nrow_addr - 12) << 8
  85. | (spd.ncol_addr - 8) );
  86. debug("\n");
  87. debug("cs0_bnds = 0x%08x\n",ddr->cs0_bnds);
  88. debug("cs0_config = 0x%08x\n",ddr->cs0_config);
  89. if (spd.nrows == 2) {
  90. ddr->cs1_bnds = ( (banksize(spd.row_dens) >> 8)
  91. | ((banksize(spd.row_dens) >> 23) - 1) );
  92. ddr->cs1_config = ( 1<<31
  93. | (spd.nrow_addr-12) << 8
  94. | (spd.ncol_addr-8) );
  95. debug("cs1_bnds = 0x%08x\n",ddr->cs1_bnds);
  96. debug("cs1_config = 0x%08x\n",ddr->cs1_config);
  97. }
  98. if (spd.mem_type != 0x07) {
  99. puts("No DDR module found!\n");
  100. return 0;
  101. }
  102. /*
  103. * Figure out memory size in Megabytes.
  104. */
  105. memsize = spd.nrows * banksize(spd.row_dens) / 0x100000;
  106. /*
  107. * First supported LAW size is 16M, at LAWAR_SIZE_16M == 23. Fnord.
  108. */
  109. law_size = 19 + __ilog2(memsize);
  110. /*
  111. * Determine size of each TLB1 entry.
  112. */
  113. switch (memsize) {
  114. case 16:
  115. case 32:
  116. tlb_size = BOOKE_PAGESZ_16M;
  117. break;
  118. case 64:
  119. case 128:
  120. tlb_size = BOOKE_PAGESZ_64M;
  121. break;
  122. case 256:
  123. case 512:
  124. case 1024:
  125. case 2048:
  126. tlb_size = BOOKE_PAGESZ_256M;
  127. break;
  128. default:
  129. puts("DDR: only 16M,32M,64M,128M,256M,512M,1G and 2G DDR I are supported.\n");
  130. return 0;
  131. break;
  132. }
  133. /*
  134. * Configure DDR TLB1 entries.
  135. * Starting at TLB1 8, use no more than 8 TLB1 entries.
  136. */
  137. ram_tlb_index = 8;
  138. ram_tlb_address = (unsigned int)CFG_DDR_SDRAM_BASE;
  139. while (ram_tlb_address < (memsize * 1024 * 1024)
  140. && ram_tlb_index < 16) {
  141. mtspr(MAS0, TLB1_MAS0(1, ram_tlb_index, 0));
  142. mtspr(MAS1, TLB1_MAS1(1, 1, 0, 0, tlb_size));
  143. mtspr(MAS2, TLB1_MAS2(E500_TLB_EPN(ram_tlb_address),
  144. 0, 0, 0, 0, 0, 0, 0, 0));
  145. mtspr(MAS3, TLB1_MAS3(E500_TLB_RPN(ram_tlb_address),
  146. 0, 0, 0, 0, 0, 1, 0, 1, 0, 1));
  147. asm volatile("isync;msync;tlbwe;isync");
  148. debug("DDR:MAS0=0x%08x\n", TLB1_MAS0(1, ram_tlb_index, 0));
  149. debug("DDR:MAS1=0x%08x\n", TLB1_MAS1(1, 1, 0, 0, tlb_size));
  150. debug("DDR:MAS2=0x%08x\n",
  151. TLB1_MAS2(E500_TLB_EPN(ram_tlb_address),
  152. 0, 0, 0, 0, 0, 0, 0, 0));
  153. debug("DDR:MAS3=0x%08x\n",
  154. TLB1_MAS3(E500_TLB_RPN(ram_tlb_address),
  155. 0, 0, 0, 0, 0, 1, 0, 1, 0, 1));
  156. ram_tlb_address += (0x1000 << ((tlb_size - 1) * 2));
  157. ram_tlb_index++;
  158. }
  159. /*
  160. * Set up LAWBAR for all of DDR.
  161. */
  162. ecm->lawbar1 = ((CFG_DDR_SDRAM_BASE>>12) & 0xfffff);
  163. ecm->lawar1 = (LAWAR_EN | LAWAR_TRGT_IF_DDR | (LAWAR_SIZE & law_size));
  164. debug("DDR:LAWBAR1=0x%08x\n", ecm->lawbar1);
  165. debug("DDR:LARAR1=0x%08x\n", ecm->lawar1);
  166. /*
  167. * find the largest CAS
  168. */
  169. if(spd.cas_lat & 0x40) {
  170. caslat = 7;
  171. } else if (spd.cas_lat & 0x20) {
  172. caslat = 6;
  173. } else if (spd.cas_lat & 0x10) {
  174. caslat = 5;
  175. } else if (spd.cas_lat & 0x08) {
  176. caslat = 4;
  177. } else if (spd.cas_lat & 0x04) {
  178. caslat = 3;
  179. } else if (spd.cas_lat & 0x02) {
  180. caslat = 2;
  181. } else if (spd.cas_lat & 0x01) {
  182. caslat = 1;
  183. } else {
  184. puts("DDR:no valid CAS Latency information.\n");
  185. return 0;
  186. }
  187. tmp = 20000 / (((spd.clk_cycle & 0xF0) >> 4) * 10
  188. + (spd.clk_cycle & 0x0f));
  189. debug("DDR:Module maximum data rate is: %dMhz\n", tmp);
  190. tmp1 = get_bus_freq(0) / 1000000;
  191. if (tmp1 < 230 && tmp1 >= 90 && tmp >= 230) {
  192. /* 90~230 range, treated as DDR 200 */
  193. if (spd.clk_cycle3 == 0xa0)
  194. caslat -= 2;
  195. else if(spd.clk_cycle2 == 0xa0)
  196. caslat--;
  197. } else if (tmp1 < 280 && tmp1 >= 230 && tmp >= 280) {
  198. /* 230-280 range, treated as DDR 266 */
  199. if (spd.clk_cycle3 == 0x75)
  200. caslat -= 2;
  201. else if (spd.clk_cycle2 == 0x75)
  202. caslat--;
  203. } else if (tmp1 < 350 && tmp1 >= 280 && tmp >= 350) {
  204. /* 280~350 range, treated as DDR 333 */
  205. if (spd.clk_cycle3 == 0x60)
  206. caslat -= 2;
  207. else if (spd.clk_cycle2 == 0x60)
  208. caslat--;
  209. } else if (tmp1 < 90 || tmp1 >= 350) {
  210. /* DDR rate out-of-range */
  211. puts("DDR:platform frequency is not fit for DDR rate\n");
  212. return 0;
  213. }
  214. /*
  215. * note: caslat must also be programmed into ddr->sdram_mode
  216. * register.
  217. *
  218. * note: WRREC(Twr) and WRTORD(Twtr) are not in SPD,
  219. * use conservative value here.
  220. */
  221. ddr->timing_cfg_1 =
  222. (((picos_to_clk(spd.trp * 250) & 0x07) << 28 ) |
  223. ((picos_to_clk(spd.tras * 1000) & 0x0f ) << 24 ) |
  224. ((picos_to_clk(spd.trcd * 250) & 0x07) << 20 ) |
  225. ((caslat & 0x07) << 16 ) |
  226. (((picos_to_clk(spd.sset[6] * 1000) - 8) & 0x0f) << 12 ) |
  227. ( 0x300 ) |
  228. ((picos_to_clk(spd.trrd * 250) & 0x07) << 4) | 1);
  229. ddr->timing_cfg_2 = 0x00000800;
  230. debug("DDR:timing_cfg_1=0x%08x\n", ddr->timing_cfg_1);
  231. debug("DDR:timing_cfg_2=0x%08x\n", ddr->timing_cfg_2);
  232. /*
  233. * Only DDR I is supported
  234. * DDR I and II have different mode-register-set definition
  235. */
  236. /* burst length is always 4 */
  237. switch(caslat) {
  238. case 2:
  239. ddr->sdram_mode = 0x52; /* 1.5 */
  240. break;
  241. case 3:
  242. ddr->sdram_mode = 0x22; /* 2.0 */
  243. break;
  244. case 4:
  245. ddr->sdram_mode = 0x62; /* 2.5 */
  246. break;
  247. case 5:
  248. ddr->sdram_mode = 0x32; /* 3.0 */
  249. break;
  250. default:
  251. puts("DDR:only CAS Latency 1.5, 2.0, 2.5, 3.0 is supported.\n");
  252. return 0;
  253. }
  254. debug("DDR:sdram_mode=0x%08x\n", ddr->sdram_mode);
  255. switch(spd.refresh) {
  256. case 0x00:
  257. case 0x80:
  258. tmp = picos_to_clk(15625000);
  259. break;
  260. case 0x01:
  261. case 0x81:
  262. tmp = picos_to_clk(3900000);
  263. break;
  264. case 0x02:
  265. case 0x82:
  266. tmp = picos_to_clk(7800000);
  267. break;
  268. case 0x03:
  269. case 0x83:
  270. tmp = picos_to_clk(31300000);
  271. break;
  272. case 0x04:
  273. case 0x84:
  274. tmp = picos_to_clk(62500000);
  275. break;
  276. case 0x05:
  277. case 0x85:
  278. tmp = picos_to_clk(125000000);
  279. break;
  280. default:
  281. tmp = 0x512;
  282. break;
  283. }
  284. /*
  285. * Set BSTOPRE to 0x100 for page mode
  286. * If auto-charge is used, set BSTOPRE = 0
  287. */
  288. ddr->sdram_interval = ((tmp & 0x3fff) << 16) | 0x100;
  289. debug("DDR:sdram_interval=0x%08x\n", ddr->sdram_interval);
  290. /*
  291. * Is this an ECC DDR chip?
  292. */
  293. #if defined(CONFIG_DDR_ECC)
  294. if (spd.config == 0x02) {
  295. ddr->err_disable = 0x0000000d;
  296. ddr->err_sbe = 0x00ff0000;
  297. }
  298. debug("DDR:err_disable=0x%08x\n", ddr->err_disable);
  299. debug("DDR:err_sbe=0x%08x\n", ddr->err_sbe);
  300. #endif
  301. asm("sync;isync;msync");
  302. udelay(500);
  303. #ifdef MPC85xx_DDR_SDRAM_CLK_CNTL
  304. /* Setup the clock control (8555 and later)
  305. * SDRAM_CLK_CNTL[0] = Source synchronous enable == 1
  306. * SDRAM_CLK_CNTL[5-7] = Clock Adjust == 3 (3/4 cycle late)
  307. */
  308. ddr->sdram_clk_cntl = 0x83000000;
  309. #endif
  310. /*
  311. * Figure out the settings for the sdram_cfg register. Build up
  312. * the entire register in 'tmp' before writing since the write into
  313. * the register will actually enable the memory controller, and all
  314. * settings must be done before enabling.
  315. *
  316. * sdram_cfg[0] = 1 (ddr sdram logic enable)
  317. * sdram_cfg[1] = 1 (self-refresh-enable)
  318. * sdram_cfg[6:7] = 2 (SDRAM type = DDR SDRAM)
  319. */
  320. tmp = 0xc2000000;
  321. /*
  322. * sdram_cfg[3] = RD_EN - registered DIMM enable
  323. * A value of 0x26 indicates micron registered DIMMS (micron.com)
  324. */
  325. if (spd.mod_attr == 0x26) {
  326. tmp |= 0x10000000;
  327. }
  328. #if defined(CONFIG_DDR_ECC)
  329. /*
  330. * If the user wanted ECC (enabled via sdram_cfg[2])
  331. */
  332. if (spd.config == 0x02) {
  333. tmp |= 0x20000000;
  334. }
  335. #endif
  336. /*
  337. * REV1 uses 1T timing.
  338. * REV2 may use 1T or 2T as configured by the user.
  339. */
  340. {
  341. uint pvr = get_pvr();
  342. if (pvr != PVR_85xx_REV1) {
  343. #if defined(CONFIG_DDR_2T_TIMING)
  344. /*
  345. * Enable 2T timing by setting sdram_cfg[16].
  346. */
  347. tmp |= 0x8000;
  348. #endif
  349. }
  350. }
  351. ddr->sdram_cfg = tmp;
  352. asm("sync;isync;msync");
  353. udelay(500);
  354. debug("DDR:sdram_cfg=0x%08x\n", ddr->sdram_cfg);
  355. return memsize * 1024 * 1024;
  356. }
  357. #endif /* CONFIG_SPD_EEPROM */
  358. #if defined(CONFIG_DDR_ECC)
  359. /*
  360. * Initialize all of memory for ECC, then enable errors.
  361. */
  362. void
  363. ddr_enable_ecc(unsigned int dram_size)
  364. {
  365. uint *p = 0;
  366. uint i = 0;
  367. volatile immap_t *immap = (immap_t *)CFG_IMMR;
  368. volatile ccsr_ddr_t *ddr= &immap->im_ddr;
  369. dma_init();
  370. for (*p = 0; p < (uint *)(8 * 1024); p++) {
  371. if (((unsigned int)p & 0x1f) == 0) {
  372. ppcDcbz((unsigned long) p);
  373. }
  374. *p = (unsigned int)0xdeadbeef;
  375. if (((unsigned int)p & 0x1c) == 0x1c) {
  376. ppcDcbf((unsigned long) p);
  377. }
  378. }
  379. /* 8K */
  380. dma_xfer((uint *)0x2000, 0x2000, (uint *)0);
  381. /* 16K */
  382. dma_xfer((uint *)0x4000, 0x4000, (uint *)0);
  383. /* 32K */
  384. dma_xfer((uint *)0x8000, 0x8000, (uint *)0);
  385. /* 64K */
  386. dma_xfer((uint *)0x10000, 0x10000, (uint *)0);
  387. /* 128k */
  388. dma_xfer((uint *)0x20000, 0x20000, (uint *)0);
  389. /* 256k */
  390. dma_xfer((uint *)0x40000, 0x40000, (uint *)0);
  391. /* 512k */
  392. dma_xfer((uint *)0x80000, 0x80000, (uint *)0);
  393. /* 1M */
  394. dma_xfer((uint *)0x100000, 0x100000, (uint *)0);
  395. /* 2M */
  396. dma_xfer((uint *)0x200000, 0x200000, (uint *)0);
  397. /* 4M */
  398. dma_xfer((uint *)0x400000, 0x400000, (uint *)0);
  399. for (i = 1; i < dram_size / 0x800000; i++) {
  400. dma_xfer((uint *)(0x800000*i), 0x800000, (uint *)0);
  401. }
  402. /*
  403. * Enable errors for ECC.
  404. */
  405. ddr->err_disable = 0x00000000;
  406. asm("sync;isync;msync");
  407. }
  408. #endif /* CONFIG_DDR_ECC */