spd_sdram.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  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. #define ns2clk(ns) ((ns) / (2000000000 /get_bus_freq(0) + 1) + 1)
  31. long int spd_sdram(void) {
  32. volatile immap_t *immap = (immap_t *)CFG_IMMR;
  33. volatile ccsr_ddr_t *ddr = &immap->im_ddr;
  34. volatile ccsr_local_ecm_t *ecm = &immap->im_local_ecm;
  35. spd_eeprom_t spd;
  36. unsigned int memsize,tmp,tmp1,tmp2;
  37. unsigned char caslat;
  38. i2c_read (SPD_EEPROM_ADDRESS, 0, 1, (uchar *) & spd, sizeof (spd));
  39. if ( spd.nrows > 2 ) {
  40. printf("DDR:Only two chip selects are supported on ADS.\n");
  41. return 0;
  42. }
  43. if ( spd.nrow_addr < 12 || spd.nrow_addr > 14 || spd.ncol_addr < 8 || spd.ncol_addr > 11) {
  44. printf("DDR:Row or Col number unsupported.\n");
  45. return 0;
  46. }
  47. ddr->cs0_bnds = ((spd.row_dens>>2) - 1);
  48. ddr->cs0_config = ( 1<<31 | (spd.nrow_addr-12)<<8 | (spd.ncol_addr-8) );
  49. debug ("\n");
  50. debug ("cs0_bnds = 0x%08x\n",ddr->cs0_bnds);
  51. debug ("cs0_config = 0x%08x\n",ddr->cs0_config);
  52. if ( spd.nrows == 2 ) {
  53. ddr->cs1_bnds = ((spd.row_dens<<14) | ((spd.row_dens>>1) - 1));
  54. ddr->cs1_config = ( 1<<31 | (spd.nrow_addr-12)<<8 | (spd.ncol_addr-8) );
  55. debug ("cs1_bnds = 0x%08x\n",ddr->cs1_bnds);
  56. debug ("cs1_config = 0x%08x\n",ddr->cs1_config);
  57. }
  58. memsize = spd.nrows * (4 * spd.row_dens);
  59. if( spd.mem_type != 0x07 ) {
  60. printf("No DDR module found!\n");
  61. return 0;
  62. }
  63. switch (memsize) {
  64. case 16:
  65. tmp = 7; /* TLB size */
  66. tmp1 = 1; /* TLB entry number */
  67. tmp2 = 23; /* Local Access Window size */
  68. break;
  69. case 32:
  70. tmp = 7;
  71. tmp1 = 2;
  72. tmp2 = 24;
  73. break;
  74. case 64:
  75. tmp = 8;
  76. tmp1 = 1;
  77. tmp2 = 25;
  78. break;
  79. case 128:
  80. tmp = 8;
  81. tmp1 = 2;
  82. tmp2 = 26;
  83. break;
  84. case 256:
  85. tmp = 9;
  86. tmp1 = 1;
  87. tmp2 = 27;
  88. break;
  89. case 512:
  90. tmp = 9;
  91. tmp1 = 2;
  92. tmp2 = 28;
  93. break;
  94. case 1024:
  95. tmp = 10;
  96. tmp1 = 1;
  97. tmp2 = 29;
  98. break;
  99. default:
  100. printf ("DDR:we only added support 16M,32M,64M,128M,256M,512M and 1G DDR I.\n");
  101. return 0;
  102. break;
  103. }
  104. /* configure DDR TLB to TLB1 Entry 4,5 */
  105. mtspr(MAS0, TLB1_MAS0(1,4,0));
  106. mtspr(MAS1, TLB1_MAS1(1,1,0,0,tmp));
  107. mtspr(MAS2, TLB1_MAS2(((CFG_DDR_SDRAM_BASE>>12) & 0xfffff),0,0,0,0,0,0,0,0));
  108. mtspr(MAS3, TLB1_MAS3(((CFG_DDR_SDRAM_BASE>>12) & 0xfffff),0,0,0,0,0,1,0,1,0,1));
  109. asm volatile("isync;msync;tlbwe;isync");
  110. debug ("DDR:MAS0=0x%08x\n",TLB1_MAS0(1,4,0));
  111. debug ("DDR:MAS1=0x%08x\n",TLB1_MAS1(1,1,0,0,tmp));
  112. debug ("DDR:MAS2=0x%08x\n",TLB1_MAS2(((CFG_DDR_SDRAM_BASE>>12) \
  113. & 0xfffff),0,0,0,0,0,0,0,0));
  114. debug ("DDR:MAS3=0x%08x\n",TLB1_MAS3(((CFG_DDR_SDRAM_BASE>>12) \
  115. & 0xfffff),0,0,0,0,0,1,0,1,0,1));
  116. if(tmp1 == 2) {
  117. mtspr(MAS0, TLB1_MAS0(1,5,0));
  118. mtspr(MAS1, TLB1_MAS1(1,1,0,0,tmp));
  119. mtspr(MAS2, TLB1_MAS2((((CFG_DDR_SDRAM_BASE+(memsize*1024*1024)/2)>>12) \
  120. & 0xfffff),0,0,0,0,0,0,0,0));
  121. mtspr(MAS3, TLB1_MAS3((((CFG_DDR_SDRAM_BASE+(memsize*1024*1024)/2)>>12) \
  122. & 0xfffff),0,0,0,0,0,1,0,1,0,1));
  123. asm volatile("isync;msync;tlbwe;isync");
  124. debug ("DDR:MAS0=0x%08x\n",TLB1_MAS0(1,5,0));
  125. debug ("DDR:MAS1=0x%08x\n",TLB1_MAS1(1,1,0,0,tmp));
  126. debug ("DDR:MAS2=0x%08x\n",TLB1_MAS2((((CFG_DDR_SDRAM_BASE \
  127. +(memsize*1024*1024)/2)>>12) & 0xfffff),0,0,0,0,0,0,0,0));
  128. debug ("DDR:MAS3=0x%08x\n",TLB1_MAS3((((CFG_DDR_SDRAM_BASE \
  129. +(memsize*1024*1024)/2)>>12) & 0xfffff),0,0,0,0,0,1,0,1,0,1));
  130. }
  131. #if defined(CONFIG_RAM_AS_FLASH)
  132. ecm->lawbar2 = ((CFG_DDR_SDRAM_BASE>>12) & 0xfffff);
  133. ecm->lawar2 = (LAWAR_EN | LAWAR_TRGT_IF_DDR | (LAWAR_SIZE & tmp2));
  134. debug ("DDR:LAWBAR2=0x%08x\n",ecm->lawbar2);
  135. debug ("DDR:LARAR2=0x%08x\n",ecm->lawar2);
  136. #else
  137. ecm->lawbar1 = ((CFG_DDR_SDRAM_BASE>>12) & 0xfffff);
  138. ecm->lawar1 = (LAWAR_EN | LAWAR_TRGT_IF_DDR | (LAWAR_SIZE & tmp2));
  139. debug ("DDR:LAWBAR1=0x%08x\n",ecm->lawbar1);
  140. debug ("DDR:LARAR1=0x%08x\n",ecm->lawar1);
  141. #endif
  142. tmp = 20000/(((spd.clk_cycle & 0xF0) >> 4) * 10 + (spd.clk_cycle & 0x0f));
  143. debug ("DDR:Module maximum data rate is: %dMhz\n",tmp);
  144. /* find the largest CAS */
  145. if(spd.cas_lat & 0x40) {
  146. caslat = 7;
  147. } else if (spd.cas_lat & 0x20) {
  148. caslat = 6;
  149. } else if (spd.cas_lat & 0x10) {
  150. caslat = 5;
  151. } else if (spd.cas_lat & 0x08) {
  152. caslat = 4;
  153. } else if (spd.cas_lat & 0x04) {
  154. caslat = 3;
  155. } else if (spd.cas_lat & 0x02) {
  156. caslat = 2;
  157. } else if (spd.cas_lat & 0x01) {
  158. caslat = 1;
  159. } else {
  160. printf("DDR:no valid CAS Latency information.\n");
  161. return 0;
  162. }
  163. tmp1 = get_bus_freq(0)/1000000;
  164. if(tmp1<230 && tmp1>=90 && tmp>=230) {
  165. /* 90~230 range, treated as DDR 200 */
  166. if(spd.clk_cycle3 == 0xa0) caslat -= 2;
  167. else if(spd.clk_cycle2 == 0xa0) caslat--;
  168. } else if(tmp1<280 && tmp1>=230 && tmp>=280) {
  169. /* 230-280 range, treated as DDR 266 */
  170. if(spd.clk_cycle3 == 0x75) caslat -= 2;
  171. else if(spd.clk_cycle2 == 0x75) caslat--;
  172. } else if(tmp1<350 && tmp1>=280 && tmp>=350) {
  173. /* 280~350 range, treated as DDR 333 */
  174. if(spd.clk_cycle3 == 0x60) caslat -= 2;
  175. else if(spd.clk_cycle2 == 0x60) caslat--;
  176. } else if(tmp1<90 || tmp1 >=350) { /* DDR rate out-of-range */
  177. printf("DDR:platform frequency is not fit for DDR rate\n");
  178. return 0;
  179. }
  180. /* note: caslat must also be programmed into ddr->sdram_mode
  181. register */
  182. /* note: WRREC(Twr) and WRTORD(Twtr) are not in SPD,use
  183. conservative value here */
  184. ddr->timing_cfg_1 = (((ns2clk(spd.trp/4) & 0x07) << 28 ) | \
  185. ((ns2clk(spd.tras) & 0x0f ) << 24 ) | \
  186. ((ns2clk(spd.trcd/4) & 0x07) << 20 ) | \
  187. ((caslat & 0x07)<< 16 ) | \
  188. (((ns2clk(spd.sset[6]) - 8) & 0x0f) << 12 ) | \
  189. ( 0x300 ) | \
  190. ((ns2clk(spd.trrd/4) & 0x07) << 4) | 1);
  191. debug ("DDR:timing_cfg_1=0x%08x\n",ddr->timing_cfg_1);
  192. ddr->timing_cfg_2 = 0x00000800;
  193. debug ("DDR:timing_cfg_2=0x%08x\n",ddr->timing_cfg_2);
  194. /* only DDR I is supported, DDR I and II have different mode-register-set definition */
  195. /* burst length is always 4 */
  196. switch(caslat) {
  197. case 2:
  198. ddr->sdram_mode = 0x52; /* 1.5 */
  199. break;
  200. case 3:
  201. ddr->sdram_mode = 0x22; /* 2.0 */
  202. break;
  203. case 4:
  204. ddr->sdram_mode = 0x62; /* 2.5 */
  205. break;
  206. case 5:
  207. ddr->sdram_mode = 0x32; /* 3.0 */
  208. break;
  209. default:
  210. printf("DDR:only CAS Latency 1.5,2.0,2.5,3.0 is supported.\n");
  211. return 0;
  212. }
  213. debug ("DDR:sdram_mode=0x%08x\n",ddr->sdram_mode);
  214. switch(spd.refresh) {
  215. case 0x00:
  216. case 0x80:
  217. tmp = ns2clk(15625);
  218. break;
  219. case 0x01:
  220. case 0x81:
  221. tmp = ns2clk(3900);
  222. break;
  223. case 0x02:
  224. case 0x82:
  225. tmp = ns2clk(7800);
  226. break;
  227. case 0x03:
  228. case 0x83:
  229. tmp = ns2clk(31300);
  230. break;
  231. case 0x04:
  232. case 0x84:
  233. tmp = ns2clk(62500);
  234. break;
  235. case 0x05:
  236. case 0x85:
  237. tmp = ns2clk(125000);
  238. break;
  239. default:
  240. tmp = 0x512;
  241. break;
  242. }
  243. /* set BSTOPRE to 0x100 for page mode, if auto-charge is used, set BSTOPRE = 0 */
  244. ddr->sdram_interval = ((tmp & 0x3fff) << 16) | 0x100;
  245. debug ("DDR:sdram_interval=0x%08x\n",ddr->sdram_interval);
  246. /* is this an ECC DDR chip? */
  247. #if defined(CONFIG_DDR_ECC)
  248. if(spd.config == 0x02) {
  249. ddr->err_disable = 0x0000000d;
  250. ddr->err_sbe = 0x00ff0000;
  251. }
  252. debug ("DDR:err_disable=0x%08x\n",ddr->err_disable);
  253. debug ("DDR:err_sbe=0x%08x\n",ddr->err_sbe);
  254. #endif
  255. asm("sync;isync;msync");
  256. udelay(500);
  257. #ifdef MPC85xx_DDR_SDRAM_CLK_CNTL
  258. /* Setup the clock control (8555 and later)
  259. * SDRAM_CLK_CNTL[0] = Source synchronous enable == 1
  260. * SDRAM_CLK_CNTL[5-7] = Clock Adjust == 3 (3/4 cycle late)
  261. */
  262. ddr->sdram_clk_cntl = 0x83000000;
  263. #endif
  264. /* Figure out the settings for the sdram_cfg register. Build up
  265. * the entire register in 'tmp' before writing since the write into
  266. * the register will actually enable the memory controller, and all
  267. * settings must be done before enabling.
  268. *
  269. * sdram_cfg[0] = 1 (ddr sdram logic enable)
  270. * sdram_cfg[1] = 1 (self-refresh-enable)
  271. * sdram_cfg[6:7] = 2 (SDRAM type = DDR SDRAM)
  272. */
  273. tmp = 0xc2000000;
  274. /* sdram_cfg[3] = RD_EN - registered DIMM enable
  275. * A value of 0x26 indicates micron registered DIMMS (micron.com)
  276. */
  277. if (spd.mod_attr == 0x26) {
  278. tmp |= 0x10000000;
  279. }
  280. #if defined(CONFIG_DDR_ECC)
  281. /* If the user wanted ECC (enabled via sdram_cfg[2]) */
  282. if (spd.config == 0x02) {
  283. tmp |= 0x20000000;
  284. }
  285. #endif
  286. /*
  287. * REV1 uses 1T timing.
  288. * REV2 may use 1T or 2T as configured by the user.
  289. */
  290. {
  291. uint pvr = get_pvr();
  292. if (pvr != PVR_85xx_REV1) {
  293. #if defined(CONFIG_DDR_2T_TIMING)
  294. /*
  295. * Enable 2T timing by setting sdram_cfg[16].
  296. */
  297. tmp |= 0x8000;
  298. #endif
  299. }
  300. }
  301. ddr->sdram_cfg = tmp;
  302. asm("sync;isync;msync");
  303. udelay(500);
  304. debug ("DDR:sdram_cfg=0x%08x\n",ddr->sdram_cfg);
  305. return (memsize*1024*1024);
  306. }
  307. #endif /* CONFIG_SPD_EEPROM */