spd_sdram.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  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
  58. banksize(unsigned char row_dens)
  59. {
  60. return ((row_dens >> 2) | ((row_dens & 3) << 6)) << 24;
  61. }
  62. long int spd_sdram(int(read_spd)(uint addr))
  63. {
  64. volatile immap_t *immap = (immap_t *)CFG_IMMRBAR;
  65. volatile ddr8349_t *ddr = &immap->ddr;
  66. volatile law8349_t *ecm = &immap->sysconf.ddrlaw[0];
  67. spd_eeprom_t spd;
  68. unsigned tmp, tmp1;
  69. unsigned int memsize;
  70. unsigned int law_size;
  71. unsigned char caslat;
  72. unsigned int trfc, trfc_clk, trfc_low;
  73. #warning Current spd_sdram does not fit its usage... adjust implementation or API...
  74. CFG_READ_SPD(SPD_EEPROM_ADDRESS, 0, 1, (uchar *) & spd, sizeof (spd));
  75. if (spd.nrows > 2) {
  76. puts("DDR:Only two chip selects are supported on ADS.\n");
  77. return 0;
  78. }
  79. if (spd.nrow_addr < 12
  80. || spd.nrow_addr > 14
  81. || spd.ncol_addr < 8
  82. || spd.ncol_addr > 11) {
  83. puts("DDR:Row or Col number unsupported.\n");
  84. return 0;
  85. }
  86. ddr->csbnds[2].csbnds = (banksize(spd.row_dens) >> 24) - 1;
  87. ddr->cs_config[2] = ( 1 << 31
  88. | (spd.nrow_addr - 12) << 8
  89. | (spd.ncol_addr - 8) );
  90. debug("\n");
  91. debug("cs2_bnds = 0x%08x\n",ddr->csbnds[2].csbnds);
  92. debug("cs2_config = 0x%08x\n",ddr->cs_config[2]);
  93. if (spd.nrows == 2) {
  94. ddr->csbnds[3].csbnds = ( (banksize(spd.row_dens) >> 8)
  95. | ((banksize(spd.row_dens) >> 23) - 1) );
  96. ddr->cs_config[3] = ( 1<<31
  97. | (spd.nrow_addr-12) << 8
  98. | (spd.ncol_addr-8) );
  99. debug("cs3_bnds = 0x%08x\n",ddr->csbnds[3].csbnds);
  100. debug("cs3_config = 0x%08x\n",ddr->cs_config[3]);
  101. }
  102. if (spd.mem_type != 0x07) {
  103. puts("No DDR module found!\n");
  104. return 0;
  105. }
  106. /*
  107. * Figure out memory size in Megabytes.
  108. */
  109. memsize = spd.nrows * banksize(spd.row_dens) / 0x100000;
  110. /*
  111. * First supported LAW size is 16M, at LAWAR_SIZE_16M == 23.
  112. */
  113. law_size = 19 + __ilog2(memsize);
  114. /*
  115. * Set up LAWBAR for all of DDR.
  116. */
  117. ecm->bar = ((CFG_DDR_SDRAM_BASE>>12) & 0xfffff);
  118. ecm->ar = (LAWAR_EN | LAWAR_TRGT_IF_DDR | (LAWAR_SIZE & law_size));
  119. debug("DDR:bar=0x%08x\n", ecm->bar);
  120. debug("DDR:ar=0x%08x\n", ecm->ar);
  121. /*
  122. * find the largest CAS
  123. */
  124. if(spd.cas_lat & 0x40) {
  125. caslat = 7;
  126. } else if (spd.cas_lat & 0x20) {
  127. caslat = 6;
  128. } else if (spd.cas_lat & 0x10) {
  129. caslat = 5;
  130. } else if (spd.cas_lat & 0x08) {
  131. caslat = 4;
  132. } else if (spd.cas_lat & 0x04) {
  133. caslat = 3;
  134. } else if (spd.cas_lat & 0x02) {
  135. caslat = 2;
  136. } else if (spd.cas_lat & 0x01) {
  137. caslat = 1;
  138. } else {
  139. puts("DDR:no valid CAS Latency information.\n");
  140. return 0;
  141. }
  142. tmp = 20000 / (((spd.clk_cycle & 0xF0) >> 4) * 10
  143. + (spd.clk_cycle & 0x0f));
  144. debug("DDR:Module maximum data rate is: %dMhz\n", tmp);
  145. tmp1 = get_bus_freq(0) / 1000000;
  146. if (tmp1 < 230 && tmp1 >= 90 && tmp >= 230) {
  147. /* 90~230 range, treated as DDR 200 */
  148. if (spd.clk_cycle3 == 0xa0)
  149. caslat -= 2;
  150. else if(spd.clk_cycle2 == 0xa0)
  151. caslat--;
  152. } else if (tmp1 < 280 && tmp1 >= 230 && tmp >= 280) {
  153. /* 230-280 range, treated as DDR 266 */
  154. if (spd.clk_cycle3 == 0x75)
  155. caslat -= 2;
  156. else if (spd.clk_cycle2 == 0x75)
  157. caslat--;
  158. } else if (tmp1 < 350 && tmp1 >= 280 && tmp >= 350) {
  159. /* 280~350 range, treated as DDR 333 */
  160. if (spd.clk_cycle3 == 0x60)
  161. caslat -= 2;
  162. else if (spd.clk_cycle2 == 0x60)
  163. caslat--;
  164. } else if (tmp1 < 90 || tmp1 >= 350) {
  165. /* DDR rate out-of-range */
  166. puts("DDR:platform frequency is not fit for DDR rate\n");
  167. return 0;
  168. }
  169. /*
  170. * note: caslat must also be programmed into ddr->sdram_mode
  171. * register.
  172. *
  173. * note: WRREC(Twr) and WRTORD(Twtr) are not in SPD,
  174. * use conservative value here.
  175. */
  176. trfc = spd.trfc * 1000; /* up to ps */
  177. trfc_clk = picos_to_clk(trfc);
  178. trfc_low = (trfc_clk - 8) & 0xf;
  179. ddr->timing_cfg_1 =
  180. (((picos_to_clk(spd.trp * 250) & 0x07) << 28 ) |
  181. ((picos_to_clk(spd.tras * 1000) & 0x0f ) << 24 ) |
  182. ((picos_to_clk(spd.trcd * 250) & 0x07) << 20 ) |
  183. ((caslat & 0x07) << 16 ) |
  184. (trfc_low << 12 ) |
  185. ( 0x300 ) |
  186. ((picos_to_clk(spd.trrd * 250) & 0x07) << 4) | 1);
  187. ddr->timing_cfg_2 = 0x00000800;
  188. debug("DDR:timing_cfg_1=0x%08x\n", ddr->timing_cfg_1);
  189. debug("DDR:timing_cfg_2=0x%08x\n", ddr->timing_cfg_2);
  190. /*
  191. * Only DDR I is supported
  192. * DDR I and II have different mode-register-set definition
  193. */
  194. /* burst length is always 4 */
  195. switch(caslat) {
  196. case 2:
  197. ddr->sdram_mode = 0x52; /* 1.5 */
  198. break;
  199. case 3:
  200. ddr->sdram_mode = 0x22; /* 2.0 */
  201. break;
  202. case 4:
  203. ddr->sdram_mode = 0x62; /* 2.5 */
  204. break;
  205. case 5:
  206. ddr->sdram_mode = 0x32; /* 3.0 */
  207. break;
  208. default:
  209. puts("DDR:only CAS Latency 1.5, 2.0, 2.5, 3.0 is supported.\n");
  210. return 0;
  211. }
  212. debug("DDR:sdram_mode=0x%08x\n", ddr->sdram_mode);
  213. switch(spd.refresh) {
  214. case 0x00:
  215. case 0x80:
  216. tmp = picos_to_clk(15625000);
  217. break;
  218. case 0x01:
  219. case 0x81:
  220. tmp = picos_to_clk(3900000);
  221. break;
  222. case 0x02:
  223. case 0x82:
  224. tmp = picos_to_clk(7800000);
  225. break;
  226. case 0x03:
  227. case 0x83:
  228. tmp = picos_to_clk(31300000);
  229. break;
  230. case 0x04:
  231. case 0x84:
  232. tmp = picos_to_clk(62500000);
  233. break;
  234. case 0x05:
  235. case 0x85:
  236. tmp = picos_to_clk(125000000);
  237. break;
  238. default:
  239. tmp = 0x512;
  240. break;
  241. }
  242. /*
  243. * Set BSTOPRE to 0x100 for page mode
  244. * If auto-charge is used, set BSTOPRE = 0
  245. */
  246. ddr->sdram_interval = ((tmp & 0x3fff) << 16) | 0x100;
  247. debug("DDR:sdram_interval=0x%08x\n", ddr->sdram_interval);
  248. /*
  249. * Is this an ECC DDR chip?
  250. */
  251. #if defined(CONFIG_DDR_ECC)
  252. if (spd.config == 0x02) {
  253. ddr->err_disable = 0x0000000d;
  254. ddr->err_sbe = 0x00ff0000;
  255. }
  256. debug("DDR:err_disable=0x%08x\n", ddr->err_disable);
  257. debug("DDR:err_sbe=0x%08x\n", ddr->err_sbe);
  258. #endif
  259. asm("sync;isync");
  260. udelay(500);
  261. /*
  262. * SS_EN=1,
  263. * CLK_ADJST = 2-MCK/MCK_B, is lauched 1/2 of one SDRAM
  264. * clock cycle after address/command
  265. */
  266. ddr->sdram_clk_cntl = 0x82000000;
  267. /*
  268. * Figure out the settings for the sdram_cfg register. Build up
  269. * the entire register in 'tmp' before writing since the write into
  270. * the register will actually enable the memory controller, and all
  271. * settings must be done before enabling.
  272. *
  273. * sdram_cfg[0] = 1 (ddr sdram logic enable)
  274. * sdram_cfg[1] = 1 (self-refresh-enable)
  275. * sdram_cfg[6:7] = 2 (SDRAM type = DDR SDRAM)
  276. */
  277. tmp = 0xc2000000;
  278. /*
  279. * sdram_cfg[3] = RD_EN - registered DIMM enable
  280. * A value of 0x26 indicates micron registered DIMMS (micron.com)
  281. */
  282. if (spd.mod_attr == 0x26) {
  283. tmp |= 0x10000000;
  284. }
  285. #if defined(CONFIG_DDR_ECC)
  286. /*
  287. * If the user wanted ECC (enabled via sdram_cfg[2])
  288. */
  289. if (spd.config == 0x02) {
  290. tmp |= 0x20000000;
  291. }
  292. #endif
  293. #if defined(CONFIG_DDR_2T_TIMING)
  294. /*
  295. * Enable 2T timing by setting sdram_cfg[16].
  296. */
  297. tmp |= SDRAM_CFG_2T_EN;
  298. #endif
  299. ddr->sdram_cfg = tmp;
  300. asm("sync;isync");
  301. udelay(500);
  302. debug("DDR:sdram_cfg=0x%08x\n", ddr->sdram_cfg);
  303. return memsize;/*in MBytes*/
  304. }
  305. #endif /* CONFIG_SPD_EEPROM */
  306. #if defined(CONFIG_DDR_ECC)
  307. /*
  308. * Initialize all of memory for ECC, then enable errors.
  309. */
  310. void
  311. ddr_enable_ecc(unsigned int dram_size)
  312. {
  313. #ifndef FIXME
  314. uint *p = 0;
  315. uint i = 0;
  316. volatile immap_t *immap = (immap_t *)CFG_IMMRBAR;
  317. volatile ccsr_ddr_t *ddr= &immap->im_ddr;
  318. dma_init();
  319. for (*p = 0; p < (uint *)(8 * 1024); p++) {
  320. if (((unsigned int)p & 0x1f) == 0) {
  321. ppcDcbz((unsigned long) p);
  322. }
  323. *p = (unsigned int)0xdeadbeef;
  324. if (((unsigned int)p & 0x1c) == 0x1c) {
  325. ppcDcbf((unsigned long) p);
  326. }
  327. }
  328. /* 8K */
  329. dma_xfer((uint *)0x2000, 0x2000, (uint *)0);
  330. /* 16K */
  331. dma_xfer((uint *)0x4000, 0x4000, (uint *)0);
  332. /* 32K */
  333. dma_xfer((uint *)0x8000, 0x8000, (uint *)0);
  334. /* 64K */
  335. dma_xfer((uint *)0x10000, 0x10000, (uint *)0);
  336. /* 128k */
  337. dma_xfer((uint *)0x20000, 0x20000, (uint *)0);
  338. /* 256k */
  339. dma_xfer((uint *)0x40000, 0x40000, (uint *)0);
  340. /* 512k */
  341. dma_xfer((uint *)0x80000, 0x80000, (uint *)0);
  342. /* 1M */
  343. dma_xfer((uint *)0x100000, 0x100000, (uint *)0);
  344. /* 2M */
  345. dma_xfer((uint *)0x200000, 0x200000, (uint *)0);
  346. /* 4M */
  347. dma_xfer((uint *)0x400000, 0x400000, (uint *)0);
  348. for (i = 1; i < dram_size / 0x800000; i++) {
  349. dma_xfer((uint *)(0x800000*i), 0x800000, (uint *)0);
  350. }
  351. /*
  352. * Enable errors for ECC.
  353. */
  354. ddr->err_disable = 0x00000000;
  355. asm("sync;isync");
  356. #endif
  357. }
  358. #endif /* CONFIG_DDR_ECC */