ddr_ark1668e.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  1. #include <common.h>
  2. #define DDR_256MX16 1
  3. #define DDR_128MX16 0
  4. #define DDR3_REG_BASE 0xE9100000
  5. #define AHB_SYS_BASE 0xE4900000
  6. #define rDDR_MCCR *((volatile unsigned int *)(DDR3_REG_BASE + 0x00))
  7. #define rDDR_MCSR *((volatile unsigned int *)(DDR3_REG_BASE + 0x04))
  8. #define rDDR_MRSVR0 *((volatile unsigned int *)(DDR3_REG_BASE + 0x08))
  9. #define rDDR_MRSVR1 *((volatile unsigned int *)(DDR3_REG_BASE + 0x0c))
  10. #define rDDR_EXRANKR *((volatile unsigned int *)(DDR3_REG_BASE + 0x10))
  11. #define rDDR_TMPR0 *((volatile unsigned int *)(DDR3_REG_BASE + 0x14))
  12. #define rDDR_TMPR1 *((volatile unsigned int *)(DDR3_REG_BASE + 0x18))
  13. #define rDDR_TMPR2 *((volatile unsigned int *)(DDR3_REG_BASE + 0x1c))
  14. #define rDDR_PHYCR0 *((volatile unsigned int *)(DDR3_REG_BASE + 0x20))
  15. #define rDDR_PHYRDTR *((volatile unsigned int *)(DDR3_REG_BASE + 0x24))
  16. #define rDDR_COMPBLKCR *((volatile unsigned int *)(DDR3_REG_BASE + 0x28))
  17. #define rDDR_AODCR *((volatile unsigned int *)(DDR3_REG_BASE + 0x2c))
  18. #define rDDR_CHARBRA *((volatile unsigned int *)(DDR3_REG_BASE + 0x30))
  19. #define rDDR_CHGNTRA *((volatile unsigned int *)(DDR3_REG_BASE + 0x34))
  20. #define rDDR_CHGNTRB *((volatile unsigned int *)(DDR3_REG_BASE + 0x38))
  21. #define rDDR_PHYWRTMR *((volatile unsigned int *)(DDR3_REG_BASE + 0x3c))
  22. #define rDDR_FLUSHCR *((volatile unsigned int *)(DDR3_REG_BASE + 0x40))
  23. #define rDDR_FLUSHSR *((volatile unsigned int *)(DDR3_REG_BASE + 0x44))
  24. #define rDDR_SPLITCR *((volatile unsigned int *)(DDR3_REG_BASE + 0x48))
  25. #define rDDR_UPDCR *((volatile unsigned int *)(DDR3_REG_BASE + 0x4c))
  26. #define rDDR_REVR *((volatile unsigned int *)(DDR3_REG_BASE + 0x50))
  27. #define rDDR_FEATR1 *((volatile unsigned int *)(DDR3_REG_BASE + 0x54))
  28. #define rDDR_FEATR2 *((volatile unsigned int *)(DDR3_REG_BASE + 0x58))
  29. #define rDDR_UDEFR *((volatile unsigned int *)(DDR3_REG_BASE + 0x5c))
  30. #define rDDR_WLEVELCR *((volatile unsigned int *)(DDR3_REG_BASE + 0x60))
  31. #define rDDR_WLEVELBHR *((volatile unsigned int *)(DDR3_REG_BASE + 0x64))
  32. #define rDDR_WLEVELBLR *((volatile unsigned int *)(DDR3_REG_BASE + 0x68))
  33. #define rDDR_PHYMISCR1 *((volatile unsigned int *)(DDR3_REG_BASE + 0x6c))
  34. #define rDDR_RLEVELCR *((volatile unsigned int *)(DDR3_REG_BASE + 0x70))
  35. #define rDDR_MSDLYCR *((volatile unsigned int *)(DDR3_REG_BASE + 0x74))
  36. #define rDDR_WRDLLCR *((volatile unsigned int *)(DDR3_REG_BASE + 0x78))
  37. #define rDDR_TRAFMR *((volatile unsigned int *)(DDR3_REG_BASE + 0x7c))
  38. #define rDDR_CMDCNTR0 *((volatile unsigned int *)(DDR3_REG_BASE + 0x80))
  39. #define rDDR_CMDCNTR1 *((volatile unsigned int *)(DDR3_REG_BASE + 0x84))
  40. #define rDDR_CMDCNTR2 *((volatile unsigned int *)(DDR3_REG_BASE + 0x88))
  41. #define rDDR_CMDCNTR3 *((volatile unsigned int *)(DDR3_REG_BASE + 0x8c))
  42. #define rDDR_CMDCNTR4 *((volatile unsigned int *)(DDR3_REG_BASE + 0x90))
  43. #define rDDR_CMDCNTR5 *((volatile unsigned int *)(DDR3_REG_BASE + 0x94))
  44. #define rDDR_CMDCNTR6 *((volatile unsigned int *)(DDR3_REG_BASE + 0x98))
  45. #define rDDR_CMDCNTR7 *((volatile unsigned int *)(DDR3_REG_BASE + 0x9c))
  46. #define rDDR_AHBRPRER1 *((volatile unsigned int *)(DDR3_REG_BASE + 0xa0))
  47. #define rDDR_AHBRPRER2 *((volatile unsigned int *)(DDR3_REG_BASE + 0xa4))
  48. #define rDDR_INITWCR1 *((volatile unsigned int *)(DDR3_REG_BASE + 0xa8))
  49. #define rDDR_INITWCR2 *((volatile unsigned int *)(DDR3_REG_BASE + 0xac))
  50. #define rDDR_QOSCR *((volatile unsigned int *)(DDR3_REG_BASE + 0xb0))
  51. #define rDDR_QOSCNTRA *((volatile unsigned int *)(DDR3_REG_BASE + 0xb4))
  52. #define rDDR_QOSCNTRB *((volatile unsigned int *)(DDR3_REG_BASE + 0xb8))
  53. #define rDDR_QOSCNTRC *((volatile unsigned int *)(DDR3_REG_BASE + 0xbc))
  54. #define rDDR_QOSCNTRD *((volatile unsigned int *)(DDR3_REG_BASE + 0xc0))
  55. #define rDDR_CHARBRB *((volatile unsigned int *)(DDR3_REG_BASE + 0xc4))
  56. #define rDDR_CHGNTRC *((volatile unsigned int *)(DDR3_REG_BASE + 0xc8))
  57. #define rDDR_CHGNTRD *((volatile unsigned int *)(DDR3_REG_BASE + 0xcc))
  58. #define rDDR_REARBDISR *((volatile unsigned int *)(DDR3_REG_BASE + 0x12c))
  59. #define rDDR_PHYRDTFR *((volatile unsigned int *)(DDR3_REG_BASE + 0x130))
  60. #define rDDR_PHYMISCR2 *((volatile unsigned int *)(DDR3_REG_BASE + 0x134))
  61. #define rDDR_EFIFOCR *((volatile unsigned int *)(DDR3_REG_BASE + 0x138))
  62. #define rDDR_RB0DSKW *((volatile unsigned int *)(DDR3_REG_BASE + 0x160))
  63. #define rDDR_RB1DSKW *((volatile unsigned int *)(DDR3_REG_BASE + 0x164))
  64. #define rDDR_RB2DSKW *((volatile unsigned int *)(DDR3_REG_BASE + 0x168))
  65. #define rDDR_RB3DSKW *((volatile unsigned int *)(DDR3_REG_BASE + 0x16c))
  66. #define rDDR_RB4DSKW *((volatile unsigned int *)(DDR3_REG_BASE + 0x170))
  67. #define rDDR_RB5DSKW *((volatile unsigned int *)(DDR3_REG_BASE + 0x174))
  68. #define rDDR_RB6DSKW *((volatile unsigned int *)(DDR3_REG_BASE + 0x178))
  69. #define rDDR_RB7DSKW *((volatile unsigned int *)(DDR3_REG_BASE + 0x17c))
  70. #define rDDR_WB0DSKW *((volatile unsigned int *)(DDR3_REG_BASE + 0x180))
  71. #define rDDR_WB1DSKW *((volatile unsigned int *)(DDR3_REG_BASE + 0x184))
  72. #define rDDR_WB2DSKW *((volatile unsigned int *)(DDR3_REG_BASE + 0x188))
  73. #define rDDR_WB3DSKW *((volatile unsigned int *)(DDR3_REG_BASE + 0x18c))
  74. #define rDDR_WB4DSKW *((volatile unsigned int *)(DDR3_REG_BASE + 0x190))
  75. #define rDDR_WB5DSKW *((volatile unsigned int *)(DDR3_REG_BASE + 0x194))
  76. #define rDDR_WB6DSKW *((volatile unsigned int *)(DDR3_REG_BASE + 0x198))
  77. #define rDDR_WB7DSKW *((volatile unsigned int *)(DDR3_REG_BASE + 0x19c))
  78. #define rDDR_WDMDSKW *((volatile unsigned int *)(DDR3_REG_BASE + 0x1a0))
  79. #define rDDR_RB8DSKW *((volatile unsigned int *)(DDR3_REG_BASE + 0x1a4))
  80. #define rDDR_WB8DSKW *((volatile unsigned int *)(DDR3_REG_BASE + 0x1a8))
  81. #define rDDR_B8_PHYCR *((volatile unsigned int *)(DDR3_REG_BASE + 0x1ac))
  82. #define SYS_SOFT_RST_N_A *((volatile unsigned int *)(AHB_SYS_BASE + 0x074))
  83. #define SYS_SOFT_RST_N_B *((volatile unsigned int *)(AHB_SYS_BASE + 0x078))
  84. #define DDR_CFG_0 *((volatile unsigned int *)(AHB_SYS_BASE + 0x210))
  85. #define DDR_CFG_1 *((volatile unsigned int *)(AHB_SYS_BASE + 0x214))
  86. #define DDR3_1600
  87. #ifdef DDR3_1600
  88. #define GDS 4
  89. #define MSDLY 0x22
  90. /*
  91. MR0:
  92. [1:0] BL // 00:8; 01:4/8; 10:4; 11:RES
  93. [3] READ Burst Type // 0: Sequential; 1:Interleaved
  94. [6:4,2] CL //0000:RES; 0010:5; 1110:11; 0001:12; 0011:13; 0101:14;
  95. [8] DLL RST //0: NO; 1:YES
  96. [11:9] WR //000:16; 001:5; 010:6; 011:7; 100:8; 101:10; 110:12; 111:14;
  97. [12] PD //0: DLL OFF 1: DLL ON
  98. MR1:
  99. [0] DLL Enable //0 en 1 dis
  100. [5,1] Output Drive St rength //00: 40 01:34
  101. [4:3] Additive Latency (AL) //00: dis 01:CL-1 02:CL-2 11:RES
  102. [9,6,2] RTT,nom //000:DIS 001:60 010:120 011:40 100:20 101:30
  103. [7] Write Levelization //0: dis 1: enable
  104. [11] TDQS //0: dis 1: enable
  105. [12] Q Off //0: enable 1: dis
  106. MR2:
  107. [2:0] PASR //000: default
  108. [5:3] CWL //000: (tCK.AVG ≥ 2.5 ns; 001: 2.5 ns > tCK.AVG ≥ 1.875 ns
  109. 010: 1.875 ns > tCK.AVG ≥ 1.5 ns 011: 1.5 ns > tCK.AVG ≥ 1.25 ns
  110. 100: 1.25 ns > tCK.AVG ≥ 1.07 ns 101: 1.07 ns > tCK.AVG ≥ 0.935ns
  111. [4:3] Additive Latency (AL) //00: dis 01:CL-1 02:CL-2 11:RES
  112. [9,6,2] RTT,nom //000:DIS 001:60 010:120 011:40 100:20 101:30
  113. [7] Write Levelization //0: dis 1: enable
  114. [11] TDQS //0: dis 1: enable
  115. [12] Q Off //0: enable 1: dis
  116. */
  117. #if DDR_256MX16
  118. #define TRFC 0x68 // 104 //(unsigned int)((260*DDR_MCLK)/1000)
  119. #endif
  120. #if DDR_128MX16
  121. #define TRFC 0x40 // 64 (unsigned int)((160*DDR_MCLK)/1000)
  122. #endif
  123. #define TFAW 0x10 // 16 // (unsigned int)((35*DDR_MCLK)/1000)
  124. #define TRC 0x14 // 20 (unsigned int)((50*DDR_MCLK)/1000)
  125. #define TRAS 0xe //(unsigned int)((35*DDR_MCLK)/1000)
  126. #define TWTR 5
  127. #define TRTP 4 //(unsigned int)((8*DDR_MCLK)/1000)
  128. #define TWR 9 //(unsigned int)((15*2*DDR_MCLK)/1000)
  129. #define TMOD 12 //(unsigned int)((15*2*DDR_MCLK)/1000)
  130. #define TMRD 4
  131. #define TRP 6 //(unsigned int)((15*DDR_MCLK)/1000)
  132. #define TRRD 1 //(unsigned int)((7.5*DDR_MCLK)/1000)
  133. #define TRCD 6 //(unsigned int)((15*DDR_MCLK)/1000)
  134. #define TREFI 0x32 //0x64
  135. #define TXSR 0x20
  136. #define TR2w 0
  137. #define TR2R 0
  138. #define TW22 0
  139. #define TW2R 0
  140. #define mr0 0x1d70
  141. #define mr1 0x4
  142. #define mr2 0x258
  143. #define tphy_wrlat 3
  144. #define tphy_wrdata 1
  145. #define trddata_en 4
  146. #define tphy_rdlat 0
  147. #define twl 8 //TWL=CWL
  148. #define trl 11 //TRL=CL
  149. #endif
  150. void ApbWriteFun(unsigned int addr, unsigned int data)
  151. {
  152. * (volatile unsigned int *) addr = data;
  153. }
  154. /*
  155. #define MAGIC_DATA 0x55aaccee
  156. int DDRTraining(void)
  157. {
  158. int i;
  159. int min = -1, max = -1;
  160. int gds = -1;
  161. int gdsnum = 0;
  162. unsigned int val;
  163. for (i = 0; i < 8; i++) {
  164. // set GDS
  165. val = ApbReadFun(0xE9100000);
  166. val &= ~7;
  167. val |= i;
  168. ApbWriteFun(0xE9100000, val);
  169. // check ddr data r/w ok
  170. udelay(10);
  171. ApbWriteFun(0x40000000, MAGIC_DATA);
  172. val = ApbReadFun(0x40000000);
  173. if (val == MAGIC_DATA) {
  174. if (min < 0)
  175. min = i;
  176. max = i;
  177. }
  178. if (val != MAGIC_DATA || i == 7) {
  179. if (min >=0 && max >= 0) {
  180. val = max - min + 1;
  181. if (val > gdsnum) {
  182. gdsnum = val;
  183. gds = min + max / 2;
  184. }
  185. }
  186. min = -1;
  187. max = -1;
  188. }
  189. }
  190. if (gds < 0) {
  191. SendUartString("DDR training fail!\r\n");
  192. return -1;
  193. }
  194. // set GDS
  195. val = ApbReadFun(0xE9100000);
  196. val &= ~7;
  197. val |= gds;
  198. ApbWriteFun(0xE9100000, val);
  199. #if 0
  200. // set read-leveling by hardware
  201. val = ApbReadFun(0xE9100070);
  202. val &= ~3;
  203. ApbWriteFun(0xE9100070, val);
  204. // set write-leveling by hardware
  205. val = ApbReadFun(0xE9100060);
  206. val &= ~(3 << 16);
  207. ApbWriteFun(0xE9100060, val);
  208. #endif
  209. udelay(10);
  210. return 0;
  211. }
  212. */
  213. //#if 1
  214. unsigned int ddr3_sdramc_init(void)
  215. {
  216. int dll_frange=0;
  217. //softa
  218. SYS_SOFT_RST_N_B = 0xfffffffd;
  219. udelay (1); //
  220. /*
  221. dll_frange ddr data rate(freq*2)
  222. 0 : [0-400)
  223. 1 : [400-600)
  224. 2 : [600-700)
  225. 3 : [700-800)
  226. 4 : [800-900)
  227. 5 : [900-1000)
  228. 6 : [1000-1200)
  229. 7 : [1200-1600)
  230. */
  231. dll_frange = 7;
  232. DDR_CFG_0 = 0x06060860|(dll_frange<<28);
  233. //apb_sys, DDR_CFG_1
  234. DDR_CFG_1 = 0;
  235. //softa
  236. SYS_SOFT_RST_N_B = 0xfffffffd;
  237. udelay (100);
  238. DDR_CFG_1 = 0xFFe0BFFF;
  239. udelay (500); // > 50us
  240. DDR_CFG_1 = 0xFFe8BFFF;
  241. udelay (10);
  242. DDR_CFG_1 = 0xFFe8FFFF;
  243. udelay (500);//> 100us
  244. DDR_CFG_1 = 0xFFeBFFFF;
  245. udelay (200);
  246. DDR_CFG_1 = 0xFFeFFFFF;
  247. udelay (200);
  248. DDR_CFG_1 = 0xFFeFEFFF;
  249. udelay (200);
  250. //apb_sys, DDR_CFG_1
  251. // DDR_CFG_1 = 0xffefe374;
  252. DDR_CFG_1 = 0xffefe274;
  253. //softa
  254. SYS_SOFT_RST_N_B = 0xffffffff;
  255. //wait dll locked
  256. while(!((rDDR_PHYCR0>>25)&0x1));//byte1 dll locked
  257. while(!((rDDR_PHYCR0>>24)&0x1));//byte0 dll locked
  258. udelay (1000);
  259. // ark1668e initial DDR3
  260. //0x00 0x8c0e104
  261. // rDDR_MCCR = 1<<27 | 1<<20 | 1<<8 | 4<<0;
  262. rDDR_MCCR = 1<<27 | 1<<23 | 2<<21 | 1<<13 | 1<<8 | 4<<0; //0323
  263. // rDDR_MCCR = 1<<27 | 0<<23 | 0<<21 | 1<<20 | 7<<13 | 1<<8 | 0xc<<0; //0323
  264. //0x08
  265. rDDR_MRSVR0 = (mr1<<16)| mr0;
  266. //0x0C
  267. rDDR_MRSVR1 = mr2;
  268. //0x04
  269. // rDDR_MCSR |= 1<<6 | 1<<1 ;
  270. // while(rDDR_MCSR & (1<<12));
  271. // rDDR_MCSR |= 1<<7 | 1<<1 ;
  272. // while(rDDR_MCSR & (1<<13));
  273. //0x10
  274. #if DDR_128MX16
  275. rDDR_EXRANKR = 5<<4 | 4<<0; // 128Mx16 2G bit
  276. #endif
  277. #if DDR_256MX16
  278. rDDR_EXRANKR = 6<<4 | 5<<0; // 256Mx16 4G bit
  279. #endif
  280. //0x14
  281. rDDR_TMPR0 = TRAS << 0 | TRC << 8| TFAW << 16| TRFC << 24 ;
  282. //0x18
  283. rDDR_TMPR1 = TRCD<< 0 | TRRD<<4 | TRP << 8 | TMRD << 12 | TMOD << 16 | TWR << 20 | TRTP <<24 | TWTR <<28 ;
  284. //0x1c
  285. rDDR_TMPR2 = TREFI << 0 | TXSR<<8 | TR2w << 24 | TR2R << 26 | TW22 << 28 | TW2R << 30;
  286. //0x20
  287. // rDDR_PHYCR0 = 1 << 13 | 1 << 11 | 1 << 10 | 1 << 9 | 1 << 8 | 4<<4 | 4<<0;
  288. rDDR_PHYCR0 = 1 << 13 | 1 << 11 | 1 << 10 | 1 << 9 | 1 << 8 | 2<<4 | 2<<0;
  289. //0x24
  290. rDDR_PHYRDTR = 9<<4 | 9<<0;
  291. //0x130
  292. rDDR_PHYRDTFR = 9<<4 | 9<<0;
  293. //0x78
  294. rDDR_WRDLLCR = 10<<4 | 10<<0;
  295. /*
  296. //0x28
  297. rDDR_COMPBLKCR = 0x1f<<7 | 0x1f<<1 | 1<<0;
  298. //0x2c
  299. rDDR_AODCR = 1<<28 | 0xfff<<16;
  300. */
  301. //0x30
  302. // rDDR_CHARBRA = 3<<30 | 0x1e<<24 | 1<<0 ;
  303. rDDR_CHARBRA = 0xc<<28 | 1<<0 ;
  304. //0x34
  305. rDDR_CHGNTRA = 0x5<<24 | 0x5<<16 | 0x5<<8 |0xff<<0 ;
  306. // rDDR_CHGNTRA = 0x5<<24 | 0x2<<16 | 0x10<<8 |0xff<<0 ;
  307. //0x3c
  308. rDDR_PHYWRTMR = tphy_rdlat<<20 | trddata_en<<16 | tphy_wrdata<<4 | tphy_wrlat;
  309. //0x4c
  310. rDDR_UPDCR = 2<<16 | 0x10<<8 | 5<<0;
  311. //0x60
  312. // rDDR_WLEVELCR = 0xff<<16 | 10<<0;
  313. rDDR_WLEVELCR = 0xff<<16 | 8<<0;
  314. //0x68
  315. rDDR_WLEVELBLR = 0x3<<8 | 0x3<<0; //test for 0--a test fail
  316. //0x6C
  317. // rDDR_PHYMISCR1 = 0<<5 | 0<<4 | 0<<1 | 0<<0;
  318. //0x70
  319. rDDR_RLEVELCR = 0<<16 | 0<<8 | 0xff<<0;
  320. //0x74
  321. // rDDR_MSDLYCR = 2<<4 | 2<<0; //test for 0--3
  322. rDDR_MSDLYCR = 1<<4 | 1<<0; //test for 0--3
  323. // test wr 88;
  324. rDDR_INITWCR1 = 0x30d50<<0;
  325. rDDR_INITWCR2 = 0x618a0<<0;
  326. //0xc4
  327. rDDR_CHARBRB = 0x20<<24 | 3<<16 | 1<<0 ;
  328. //0xc8
  329. rDDR_CHGNTRC = 0x5<<24 | 0x5<<16 | 0x1f<<8 |0x5<<0 ;
  330. //0x134
  331. // rDDR_PHYMISCR2 = 5<<28 | 5<<24 |7<<20 | 3<<16 | 4<<12 | 4<<8 | 4<<4 | 4<<0 ;
  332. //0x138
  333. rDDR_EFIFOCR = 0<<4 | 1<<2 | 1<<0 ;
  334. /*
  335. //0x160
  336. rDDR_RB0DSKW = 4<<28 | 4<<24 |4<<20 | 4<<16 | 4<<12 | 4<<8 | 4<<4 | 4<<0 ;
  337. //0x164
  338. rDDR_RB1DSKW = 4<<28 | 4<<24 |4<<20 | 4<<16 | 4<<12 | 4<<8 | 4<<4 | 4<<0 ;
  339. //0x180
  340. rDDR_WB0DSKW = 3<<28 | 3<<24 |3<<20 | 3<<16 | 3<<12 | 3<<8 | 3<<4 | 3<<0 ;
  341. //0x184
  342. rDDR_WB1DSKW = 3<<28 | 3<<24 |3<<20 | 3<<16 | 3<<12 | 3<<8 | 3<<4 | 3<<0 ;
  343. //0x1a0
  344. rDDR_WDMDSKW = 3<<4 | 3<<0 ;
  345. */
  346. //0x4
  347. rDDR_MCSR = 1;
  348. while(!((rDDR_MCSR>>8)&0x1));
  349. udelay (1);
  350. printf("DDR3 256*16_20221130\n");
  351. return 0;
  352. }