spd_sdram.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. /*
  2. * (C) Copyright 2003 Motorola Inc.
  3. * Xianghua Xiao (X.Xiao@motorola.com)
  4. *
  5. * See file CREDITS for list of people who contributed to this
  6. * project.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation; either version 2 of
  11. * the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  21. * MA 02111-1307 USA
  22. */
  23. #include <common.h>
  24. #include <asm/processor.h>
  25. #include <i2c.h>
  26. #include <spd.h>
  27. #include <asm/mmu.h>
  28. #ifdef CONFIG_SPD_EEPROM
  29. #undef DEBUG
  30. #if defined(DEBUG)
  31. #define DEB(x) x
  32. #else
  33. #define DEB(x)
  34. #endif
  35. #define ns2clk(ns) ((ns) / (2000000000 /get_bus_freq(0) + 1))
  36. long int spd_sdram(void) {
  37. volatile immap_t *immap = (immap_t *)CFG_IMMR;
  38. volatile ccsr_ddr_t *ddr = &immap->im_ddr;
  39. volatile ccsr_local_ecm_t *ecm = &immap->im_local_ecm;
  40. spd_eeprom_t spd;
  41. unsigned int memsize,tmp,tmp1,tmp2;
  42. unsigned char caslat;
  43. i2c_read (SPD_EEPROM_ADDRESS, 0, 1, (uchar *) & spd, sizeof (spd));
  44. if ( spd.nrows > 2 ) {
  45. printf("DDR:Only two chip selects are supported on ADS.\n");
  46. return 0;
  47. }
  48. if ( spd.nrow_addr < 12 || spd.nrow_addr > 14 || spd.ncol_addr < 8 || spd.ncol_addr > 11) {
  49. printf("DDR:Row or Col number unsupported.\n");
  50. return 0;
  51. }
  52. ddr->cs0_bnds = ((spd.row_dens>>2) - 1);
  53. ddr->cs0_config = ( 1<<31 | (spd.nrow_addr-12)<<8 | (spd.ncol_addr-8) );
  54. DEB(printf("\n"));
  55. DEB(printf("cs0_bnds = 0x%08x\n",ddr->cs0_bnds));
  56. DEB(printf("cs0_config = 0x%08x\n",ddr->cs0_config));
  57. if ( spd.nrows == 2 ) {
  58. ddr->cs1_bnds = ((spd.row_dens<<14) | ((spd.row_dens>>1) - 1));
  59. ddr->cs1_config = ( 1<<31 | (spd.nrow_addr-12)<<8 | (spd.ncol_addr-8) );
  60. DEB(printf("cs1_bnds = 0x%08x\n",ddr->cs1_bnds));
  61. DEB(printf("cs1_config = 0x%08x\n",ddr->cs1_config));
  62. }
  63. memsize = spd.nrows * (4 * spd.row_dens);
  64. if( spd.mem_type == 0x07 ) {
  65. printf("DDR module detected, total size:%dMB.\n",memsize);
  66. } else {
  67. printf("No DDR module found!\n");
  68. return 0;
  69. }
  70. switch(memsize) {
  71. case 16:
  72. tmp = 7; /* TLB size */
  73. tmp1 = 1; /* TLB entry number */
  74. tmp2 = 23; /* Local Access Window size */
  75. break;
  76. case 32:
  77. tmp = 7;
  78. tmp1 = 2;
  79. tmp2 = 24;
  80. break;
  81. case 64:
  82. tmp = 8;
  83. tmp1 = 1;
  84. tmp2 = 25;
  85. break;
  86. case 128:
  87. tmp = 8;
  88. tmp1 = 2;
  89. tmp2 = 26;
  90. break;
  91. case 256:
  92. tmp = 9;
  93. tmp1 = 1;
  94. tmp2 = 27;
  95. break;
  96. case 512:
  97. tmp = 9;
  98. tmp1 = 2;
  99. tmp2 = 28;
  100. break;
  101. case 1024:
  102. tmp = 10;
  103. tmp1 = 1;
  104. tmp2 = 29;
  105. break;
  106. default:
  107. printf("DDR:we only added support 16M,32M,64M,128M,256M,512M and 1G DDR I.\n");
  108. return 0;
  109. break;
  110. }
  111. /* configure DDR TLB to TLB1 Entry 4,5 */
  112. mtspr(MAS0, TLB1_MAS0(1,4,0));
  113. mtspr(MAS1, TLB1_MAS1(1,1,0,0,tmp));
  114. mtspr(MAS2, TLB1_MAS2(((CFG_DDR_SDRAM_BASE>>12) & 0xfffff),0,0,0,0,0,0,0,0));
  115. mtspr(MAS3, TLB1_MAS3(((CFG_DDR_SDRAM_BASE>>12) & 0xfffff),0,0,0,0,0,1,0,1,0,1));
  116. asm volatile("isync;msync;tlbwe;isync");
  117. DEB(printf("DDR:MAS0=0x%08x\n",TLB1_MAS0(1,4,0)));
  118. DEB(printf("DDR:MAS1=0x%08x\n",TLB1_MAS1(1,1,0,0,tmp)));
  119. DEB(printf("DDR:MAS2=0x%08x\n",TLB1_MAS2(((CFG_DDR_SDRAM_BASE>>12) \
  120. & 0xfffff),0,0,0,0,0,0,0,0)));
  121. DEB(printf("DDR:MAS3=0x%08x\n",TLB1_MAS3(((CFG_DDR_SDRAM_BASE>>12) \
  122. & 0xfffff),0,0,0,0,0,1,0,1,0,1)));
  123. if(tmp1 == 2) {
  124. mtspr(MAS0, TLB1_MAS0(1,5,0));
  125. mtspr(MAS1, TLB1_MAS1(1,1,0,0,tmp));
  126. mtspr(MAS2, TLB1_MAS2((((CFG_DDR_SDRAM_BASE+(memsize*1024*1024)/2)>>12) \
  127. & 0xfffff),0,0,0,0,0,0,0,0));
  128. mtspr(MAS3, TLB1_MAS3((((CFG_DDR_SDRAM_BASE+(memsize*1024*1024)/2)>>12) \
  129. & 0xfffff),0,0,0,0,0,1,0,1,0,1));
  130. asm volatile("isync;msync;tlbwe;isync");
  131. DEB(printf("DDR:MAS0=0x%08x\n",TLB1_MAS0(1,5,0)));
  132. DEB(printf("DDR:MAS1=0x%08x\n",TLB1_MAS1(1,1,0,0,tmp)));
  133. DEB(printf("DDR:MAS2=0x%08x\n",TLB1_MAS2((((CFG_DDR_SDRAM_BASE \
  134. +(memsize*1024*1024)/2)>>12) & 0xfffff),0,0,0,0,0,0,0,0)));
  135. DEB(printf("DDR:MAS3=0x%08x\n",TLB1_MAS3((((CFG_DDR_SDRAM_BASE \
  136. +(memsize*1024*1024)/2)>>12) & 0xfffff),0,0,0,0,0,1,0,1,0,1)));
  137. }
  138. #if defined(CONFIG_RAM_AS_FLASH)
  139. ecm->lawbar2 = ((CFG_DDR_SDRAM_BASE>>12) & 0xfffff);
  140. ecm->lawar2 = (LAWAR_EN | LAWAR_TRGT_IF_DDR | (LAWAR_SIZE & tmp2));
  141. DEB(printf("DDR:LAWBAR2=0x%08x\n",ecm->lawbar2));
  142. DEB(printf("DDR:LARAR2=0x%08x\n",ecm->lawar2));
  143. #else
  144. ecm->lawbar1 = ((CFG_DDR_SDRAM_BASE>>12) & 0xfffff);
  145. ecm->lawar1 = (LAWAR_EN | LAWAR_TRGT_IF_DDR | (LAWAR_SIZE & tmp2));
  146. DEB(printf("DDR:LAWBAR1=0x%08x\n",ecm->lawbar1));
  147. DEB(printf("DDR:LARAR1=0x%08x\n",ecm->lawar1));
  148. #endif
  149. tmp = 20000/(((spd.clk_cycle & 0xF0) >> 4) * 10 + (spd.clk_cycle & 0x0f));
  150. DEB(printf("DDR:Module maximum data rate is: %dMhz\n",tmp));
  151. /* find the largest CAS */
  152. if(spd.cas_lat & 0x40) {
  153. caslat = 7;
  154. } else if (spd.cas_lat & 0x20) {
  155. caslat = 6;
  156. } else if (spd.cas_lat & 0x10) {
  157. caslat = 5;
  158. } else if (spd.cas_lat & 0x08) {
  159. caslat = 4;
  160. } else if (spd.cas_lat & 0x04) {
  161. caslat = 3;
  162. } else if (spd.cas_lat & 0x02) {
  163. caslat = 2;
  164. } else if (spd.cas_lat & 0x01) {
  165. caslat = 1;
  166. } else {
  167. printf("DDR:no valid CAS Latency information.\n");
  168. return 0;
  169. }
  170. tmp1 = get_bus_freq(0)/1000000;
  171. if(tmp1<230 && tmp1>=90 && tmp>=230) { /* 90~230 range, treated as DDR 200 */
  172. if(spd.clk_cycle3 == 0xa0) caslat -= 2;
  173. else if(spd.clk_cycle2 == 0xa0) caslat--;
  174. } else if(tmp1<280 && tmp1>=230 && tmp>=280) { /* 230-280 range, treated as DDR 266 */
  175. if(spd.clk_cycle3 == 0x75) caslat -= 2;
  176. else if(spd.clk_cycle2 == 0x75) caslat--;
  177. } else if(tmp1<350 && tmp1>=280 && tmp>=350) { /* 280~350 range, treated as DDR 333 */
  178. if(spd.clk_cycle3 == 0x60) caslat -= 2;
  179. else if(spd.clk_cycle2 == 0x60) caslat--;
  180. } else if(tmp1<90 || tmp1 >=350) { /* DDR rate out-of-range */
  181. printf("DDR:platform frequency is not fit for DDR rate\n");
  182. return 0;
  183. }
  184. /* note: caslat must also be programmed into ddr->sdram_mode register */
  185. /* note: WRREC(Twr) and WRTORD(Twtr) are not in SPD,use conservative value here */
  186. #if 1
  187. ddr->timing_cfg_1 = (((ns2clk(spd.trp/4) & 0x07) << 28 ) | \
  188. ((ns2clk(spd.tras) & 0x0f ) << 24 ) | \
  189. ((ns2clk(spd.trcd/4) & 0x07) << 20 ) | \
  190. ((caslat & 0x07)<< 16 ) | \
  191. (((ns2clk(spd.sset[6]) - 8) & 0x0f) << 12 ) | \
  192. ( 0x300 ) | \
  193. ((ns2clk(spd.trrd/4) & 0x07) << 4) | 1);
  194. #else
  195. ddr->timing_cfg_1 = 0x37344321;
  196. caslat = 4;
  197. #endif
  198. DEB(printf("DDR:timing_cfg_1=0x%08x\n",ddr->timing_cfg_1));
  199. /* note: hand-coded value for timing_cfg_2, see Errata DDR1*/
  200. #if defined(CONFIG_MPC85xx_REV1)
  201. ddr->timing_cfg_2 = 0x00000800;
  202. #endif
  203. DEB(printf("DDR:timing_cfg_2=0x%08x\n",ddr->timing_cfg_2));
  204. /* only DDR I is supported, DDR I and II have different mode-register-set definition */
  205. /* burst length is always 4 */
  206. switch(caslat) {
  207. case 2:
  208. ddr->sdram_mode = 0x52; /* 1.5 */
  209. break;
  210. case 3:
  211. ddr->sdram_mode = 0x22; /* 2.0 */
  212. break;
  213. case 4:
  214. ddr->sdram_mode = 0x62; /* 2.5 */
  215. break;
  216. case 5:
  217. ddr->sdram_mode = 0x32; /* 3.0 */
  218. break;
  219. default:
  220. printf("DDR:only CAS Latency 1.5,2.0,2.5,3.0 is supported.\n");
  221. return 0;
  222. }
  223. DEB(printf("DDR:sdram_mode=0x%08x\n",ddr->sdram_mode));
  224. switch(spd.refresh) {
  225. case 0x00:
  226. case 0x80:
  227. tmp = ns2clk(15625);
  228. break;
  229. case 0x01:
  230. case 0x81:
  231. tmp = ns2clk(3900);
  232. break;
  233. case 0x02:
  234. case 0x82:
  235. tmp = ns2clk(7800);
  236. break;
  237. case 0x03:
  238. case 0x83:
  239. tmp = ns2clk(31300);
  240. break;
  241. case 0x04:
  242. case 0x84:
  243. tmp = ns2clk(62500);
  244. break;
  245. case 0x05:
  246. case 0x85:
  247. tmp = ns2clk(125000);
  248. break;
  249. default:
  250. tmp = 0x512;
  251. break;
  252. }
  253. /* set BSTOPRE to 0x100 for page mode, if auto-charge is used, set BSTOPRE = 0 */
  254. ddr->sdram_interval = ((tmp & 0x3fff) << 16) | 0x100;
  255. DEB(printf("DDR:sdram_interval=0x%08x\n",ddr->sdram_interval));
  256. /* is this an ECC DDR chip? */
  257. #if defined(CONFIG_DDR_ECC)
  258. if(spd.config == 0x02) {
  259. ddr->err_disable = 0x0000000d;
  260. ddr->err_sbe = 0x00ff0000;
  261. }
  262. DEB(printf("DDR:err_disable=0x%08x\n",ddr->err_disable));
  263. DEB(printf("DDR:err_sbe=0x%08x\n",ddr->err_sbe));
  264. #endif
  265. asm("sync;isync;msync");
  266. udelay(500);
  267. /* registered or unbuffered? */
  268. #if defined(CONFIG_DDR_ECC)
  269. ddr->sdram_cfg = (spd.config == 0x02)?0x20000000:0x0;
  270. #endif
  271. ddr->sdram_cfg = 0xc2000000|((spd.mod_attr == 0x20) ? 0x0 : \
  272. ((spd.mod_attr == 0x26) ? 0x10000000:0x0));
  273. asm("sync;isync;msync");
  274. udelay(500);
  275. DEB(printf("DDR:sdram_cfg=0x%08x\n",ddr->sdram_cfg));
  276. return (memsize*1024*1024);
  277. }
  278. #endif /* CONFIG_SPD_EEPROM */