ddr.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  1. /*******************************************************************
  2. *
  3. * File: ddr_oxsemi.c
  4. *
  5. * Description: Declarations for DDR routines and data objects
  6. *
  7. * Author: Julien Margetts
  8. *
  9. * Copyright: Oxford Semiconductor Ltd, 2009
  10. */
  11. #include <common.h>
  12. #include <asm/arch/clock.h>
  13. #include "ddr.h"
  14. typedef unsigned int UINT;
  15. // DDR TIMING PARAMETERS
  16. typedef struct {
  17. unsigned int holdoff_cmd_A;
  18. unsigned int holdoff_cmd_ARW;
  19. unsigned int holdoff_cmd_N;
  20. unsigned int holdoff_cmd_LM;
  21. unsigned int holdoff_cmd_R;
  22. unsigned int holdoff_cmd_W;
  23. unsigned int holdoff_cmd_PC;
  24. unsigned int holdoff_cmd_RF;
  25. unsigned int holdoff_bank_R;
  26. unsigned int holdoff_bank_W;
  27. unsigned int holdoff_dir_RW;
  28. unsigned int holdoff_dir_WR;
  29. unsigned int holdoff_FAW;
  30. unsigned int latency_CAS;
  31. unsigned int latency_WL;
  32. unsigned int recovery_WR;
  33. unsigned int width_update;
  34. unsigned int odt_offset;
  35. unsigned int odt_drive_all;
  36. unsigned int use_fixed_re;
  37. unsigned int delay_wr_to_re;
  38. unsigned int wr_slave_ratio;
  39. unsigned int rd_slave_ratio0;
  40. unsigned int rd_slave_ratio1;
  41. } T_DDR_TIMING_PARAMETERS;
  42. // DDR CONFIG PARAMETERS
  43. typedef struct {
  44. unsigned int ddr_mode;
  45. unsigned int width;
  46. unsigned int blocs;
  47. unsigned int banks8;
  48. unsigned int rams;
  49. unsigned int asize;
  50. unsigned int speed;
  51. unsigned int cmd_mode_wr_cl_bl;
  52. } T_DDR_CONFIG_PARAMETERS;
  53. //cmd_mode_wr_cl_bl
  54. //when SDR : cmd_mode_wr_cl_bl = 0x80200002 + (latency_CAS_RAM * 16) + (recovery_WR - 1) * 512; -- Sets write rec XX, CL=XX; BL=8
  55. //else cmd_mode_wr_cl_bl = 0x80200003 + (latency_CAS_RAM * 16) + (recovery_WR - 1) * 512; -- Sets write rec XX, CL=XX; BL=8
  56. // cmd_ bank_ dir_ lat_ rec_ width_ odt_ odt_ fix delay ratio
  57. // A F C update offset all re re_to_we w r0 r1
  58. // R L P R R W A A W W
  59. //Timing Parameters A W N M R W C F R W W R W S L R
  60. static const T_DDR_TIMING_PARAMETERS C_TP_DDR2_25E_CL5_1GB = { 4, 5, 0, 2, 4, 4,
  61. 5, 51, 23, 24, 9, 11, 18, 5, 4, 6, 3, 2, 0, 1, 2, 75, 56, 56 }; //elida device.
  62. static const T_DDR_TIMING_PARAMETERS C_TP_DDR2_25E_CL5_2GB = { 4, 5, 0, 2, 4, 4,
  63. 5, 79, 22, 24, 9, 11, 20, 5, 4, 6, 3, 2, 0, 1, 2, 75, 56, 56 };
  64. static const T_DDR_TIMING_PARAMETERS C_TP_DDR2_25_CL6_1GB = { 4, 5, 0, 2, 4, 4,
  65. 4, 51, 22, 26, 10, 12, 18, 6, 5, 6, 3, 2, 0, 1, 2, 75, 56, 56 }; // 400MHz, Speedgrade 25 timings (1Gb parts)
  66. // D B B R A S
  67. // D W L K A S P
  68. //Config Parameters R D C 8 M Z D CMD_MODE
  69. //static const T_DDR_CONFIG_PARAMETERS C_CP_DDR2_25E_CL5 = { 2,16, 1, 0, 1, 32,25,0x80200A53}; // 64 MByte
  70. static const T_DDR_CONFIG_PARAMETERS C_CP_DDR2_25E_CL5 = { 2, 16, 1, 1, 1, 64,
  71. 25, 0x80200A53 }; // 128 MByte
  72. static const T_DDR_CONFIG_PARAMETERS C_CP_DDR2_25_CL6 = { 2, 16, 1, 1, 1, 128,
  73. 25, 0x80200A63 }; // 256 MByte
  74. static void ddr_phy_poll_until_locked(void)
  75. {
  76. volatile UINT reg_tmp = 0;
  77. volatile UINT locked = 0;
  78. //Extra read to put in delay before starting to poll...
  79. reg_tmp = *(volatile UINT *) C_DDR_REG_PHY2; // read
  80. //POLL C_DDR_PHY2_REG register until clock and flock
  81. //!!! Ideally have a timeout on this.
  82. while (locked == 0) {
  83. reg_tmp = *(volatile UINT *) C_DDR_REG_PHY2; // read
  84. //locked when bits 30 and 31 are set
  85. if (reg_tmp & 0xC0000000) {
  86. locked = 1;
  87. }
  88. }
  89. }
  90. static void ddr_poll_until_not_busy(void)
  91. {
  92. volatile UINT reg_tmp = 0;
  93. volatile UINT busy = 1;
  94. //Extra read to put in delay before starting to poll...
  95. reg_tmp = *(volatile UINT *) C_DDR_STAT_REG; // read
  96. //POLL DDR_STAT register until no longer busy
  97. //!!! Ideally have a timeout on this.
  98. while (busy == 1) {
  99. reg_tmp = *(volatile UINT *) C_DDR_STAT_REG; // read
  100. //when bit 31 is clear - core is no longer busy
  101. if ((reg_tmp & 0x80000000) == 0x00000000) {
  102. busy = 0;
  103. }
  104. }
  105. }
  106. static void ddr_issue_command(int commmand)
  107. {
  108. *(volatile UINT *) C_DDR_CMD_REG = commmand;
  109. ddr_poll_until_not_busy();
  110. }
  111. static void ddr_timing_initialisation(
  112. const T_DDR_TIMING_PARAMETERS *ddr_timing_parameters)
  113. {
  114. volatile UINT reg_tmp = 0;
  115. /* update the DDR controller registers for timing parameters */
  116. reg_tmp = (ddr_timing_parameters->holdoff_cmd_A << 0);
  117. reg_tmp = reg_tmp + (ddr_timing_parameters->holdoff_cmd_ARW << 4);
  118. reg_tmp = reg_tmp + (ddr_timing_parameters->holdoff_cmd_N << 8);
  119. reg_tmp = reg_tmp + (ddr_timing_parameters->holdoff_cmd_LM << 12);
  120. reg_tmp = reg_tmp + (ddr_timing_parameters->holdoff_cmd_R << 16);
  121. reg_tmp = reg_tmp + (ddr_timing_parameters->holdoff_cmd_W << 20);
  122. reg_tmp = reg_tmp + (ddr_timing_parameters->holdoff_cmd_PC << 24);
  123. *(volatile UINT *) C_DDR_REG_TIMING0 = reg_tmp;
  124. reg_tmp = (ddr_timing_parameters->holdoff_cmd_RF << 0);
  125. reg_tmp = reg_tmp + (ddr_timing_parameters->holdoff_bank_R << 8);
  126. reg_tmp = reg_tmp + (ddr_timing_parameters->holdoff_bank_W << 16);
  127. reg_tmp = reg_tmp + (ddr_timing_parameters->holdoff_dir_RW << 24);
  128. reg_tmp = reg_tmp + (ddr_timing_parameters->holdoff_dir_WR << 28);
  129. *(volatile UINT *) C_DDR_REG_TIMING1 = reg_tmp;
  130. reg_tmp = (ddr_timing_parameters->latency_CAS << 0);
  131. reg_tmp = reg_tmp + (ddr_timing_parameters->latency_WL << 4);
  132. reg_tmp = reg_tmp + (ddr_timing_parameters->holdoff_FAW << 8);
  133. reg_tmp = reg_tmp + (ddr_timing_parameters->width_update << 16);
  134. reg_tmp = reg_tmp + (ddr_timing_parameters->odt_offset << 21);
  135. reg_tmp = reg_tmp + (ddr_timing_parameters->odt_drive_all << 24);
  136. *(volatile UINT *) C_DDR_REG_TIMING2 = reg_tmp;
  137. /* Program the timing parameters in the PHY too */
  138. reg_tmp = (ddr_timing_parameters->use_fixed_re << 16)
  139. | (ddr_timing_parameters->delay_wr_to_re << 8)
  140. | (ddr_timing_parameters->latency_WL << 4)
  141. | (ddr_timing_parameters->latency_CAS << 0);
  142. *(volatile UINT *) C_DDR_REG_PHY_TIMING = reg_tmp;
  143. reg_tmp = ddr_timing_parameters->wr_slave_ratio;
  144. *(volatile UINT *) C_DDR_REG_PHY_WR_RATIO = reg_tmp;
  145. reg_tmp = ddr_timing_parameters->rd_slave_ratio0;
  146. reg_tmp += ddr_timing_parameters->rd_slave_ratio1 << 8;
  147. *(volatile UINT *) C_DDR_REG_PHY_RD_RATIO = reg_tmp;
  148. }
  149. static void ddr_normal_initialisation(
  150. const T_DDR_CONFIG_PARAMETERS *ddr_config_parameters, int mhz)
  151. {
  152. int i;
  153. volatile UINT tmp = 0;
  154. volatile UINT reg_tmp = 0;
  155. volatile UINT emr_cmd = 0;
  156. UINT refresh;
  157. //Total size of memory in Mbits...
  158. tmp = ddr_config_parameters->rams * ddr_config_parameters->asize
  159. * ddr_config_parameters->width;
  160. //Deduce value to program into DDR_CFG register...
  161. switch (tmp) {
  162. case 16:
  163. reg_tmp = 0x00020000 * 1;
  164. break;
  165. case 32:
  166. reg_tmp = 0x00020000 * 2;
  167. break;
  168. case 64:
  169. reg_tmp = 0x00020000 * 3;
  170. break;
  171. case 128:
  172. reg_tmp = 0x00020000 * 4;
  173. break;
  174. case 256:
  175. reg_tmp = 0x00020000 * 5;
  176. break;
  177. case 512:
  178. reg_tmp = 0x00020000 * 6;
  179. break;
  180. case 1024:
  181. reg_tmp = 0x00020000 * 7;
  182. break;
  183. case 2048:
  184. reg_tmp = 0x00020000 * 8;
  185. break;
  186. default:
  187. reg_tmp = 0; //forces sims not to work if badly configured
  188. }
  189. //Memory width
  190. tmp = ddr_config_parameters->rams * ddr_config_parameters->width;
  191. switch (tmp) {
  192. case 8:
  193. reg_tmp = reg_tmp + 0x00400000;
  194. break;
  195. case 16:
  196. reg_tmp = reg_tmp + 0x00200000;
  197. break;
  198. case 32:
  199. reg_tmp = reg_tmp + 0x00000000;
  200. break;
  201. default:
  202. reg_tmp = 0; //forces sims not to work if badly configured
  203. }
  204. //Setup DDR Mode
  205. switch (ddr_config_parameters->ddr_mode) {
  206. case 0:
  207. reg_tmp = reg_tmp + 0x00000000;
  208. break; //SDR
  209. case 1:
  210. reg_tmp = reg_tmp + 0x40000000;
  211. break; //DDR
  212. case 2:
  213. reg_tmp = reg_tmp + 0x80000000;
  214. break; //DDR2
  215. default:
  216. reg_tmp = 0; //forces sims not to work if badly configured
  217. }
  218. //Setup Banks
  219. if (ddr_config_parameters->banks8 == 1) {
  220. reg_tmp = reg_tmp + 0x00800000;
  221. }
  222. //Program DDR_CFG register...
  223. *(volatile UINT *) C_DDR_CFG_REG = reg_tmp;
  224. //Configure PHY0 reg - se_mode is bit 1,
  225. //needs to be 1 for DDR (single_ended drive)
  226. switch (ddr_config_parameters->ddr_mode) {
  227. case 0:
  228. reg_tmp = 2 + (0 << 4);
  229. break; //SDR
  230. case 1:
  231. reg_tmp = 2 + (4 << 4);
  232. break; //DDR
  233. case 2:
  234. reg_tmp = 0 + (4 << 4);
  235. break; //DDR2
  236. default:
  237. reg_tmp = 0;
  238. }
  239. //Program DDR_PHY0 register...
  240. *(volatile UINT *) C_DDR_REG_PHY0 = reg_tmp;
  241. //Read DDR_PHY* registers to exercise paths for vcd
  242. reg_tmp = *(volatile UINT *) C_DDR_REG_PHY3;
  243. reg_tmp = *(volatile UINT *) C_DDR_REG_PHY2;
  244. reg_tmp = *(volatile UINT *) C_DDR_REG_PHY1;
  245. reg_tmp = *(volatile UINT *) C_DDR_REG_PHY0;
  246. //Start up sequences - Different dependant on DDR mode
  247. switch (ddr_config_parameters->ddr_mode) {
  248. case 2: //DDR2
  249. //Start-up sequence: follows procedure described in Micron datasheet.
  250. //start up DDR PHY DLL
  251. reg_tmp = 0x00022828; // dll on, start point and inc = h28
  252. *(volatile UINT *) C_DDR_REG_PHY2 = reg_tmp;
  253. reg_tmp = 0x00032828; // start on, dll on, start point and inc = h28
  254. *(volatile UINT *) C_DDR_REG_PHY2 = reg_tmp;
  255. ddr_phy_poll_until_locked();
  256. udelay(200); //200us
  257. //Startup SDRAM...
  258. //!!! Software: CK should be running for 200us before wake-up
  259. ddr_issue_command( C_CMD_WAKE_UP);
  260. ddr_issue_command( C_CMD_NOP);
  261. ddr_issue_command( C_CMD_PRECHARGE_ALL);
  262. ddr_issue_command( C_CMD_DDR2_EMR2);
  263. ddr_issue_command( C_CMD_DDR2_EMR3);
  264. emr_cmd = C_CMD_DDR2_EMR1 + C_CMD_ODT_75 + C_CMD_REDUCED_DRIVE
  265. + C_CMD_ENABLE_DLL;
  266. ddr_issue_command(emr_cmd);
  267. //Sets CL=3; BL=8 but also reset DLL to trigger a DLL initialisation...
  268. udelay(1); //1us
  269. ddr_issue_command(
  270. ddr_config_parameters->cmd_mode_wr_cl_bl
  271. + C_CMD_RESET_DLL);
  272. udelay(1); //1us
  273. //!!! Software: Wait 200 CK cycles before...
  274. //for(i=1; i<=2; i++) {
  275. ddr_issue_command(C_CMD_PRECHARGE_ALL);
  276. // !!! Software: Wait here at least 8 CK cycles
  277. //}
  278. //need a wait here to ensure PHY DLL lock before the refresh is issued
  279. udelay(1); //1us
  280. for (i = 1; i <= 2; i++) {
  281. ddr_issue_command( C_CMD_AUTO_REFRESH);
  282. //!!! Software: Wait here at least 8 CK cycles to satify tRFC
  283. udelay(1); //1us
  284. }
  285. //As before but without 'RESET_DLL' bit set...
  286. ddr_issue_command(ddr_config_parameters->cmd_mode_wr_cl_bl);
  287. udelay(1); //1us
  288. // OCD commands
  289. ddr_issue_command(emr_cmd + C_CMD_MODE_DDR2_OCD_DFLT);
  290. ddr_issue_command(emr_cmd + C_CMD_MODE_DDR2_OCD_EXIT);
  291. break;
  292. default:
  293. break; //Do nothing
  294. }
  295. //Enable auto-refresh
  296. // 8192 Refreshes required every 64ms, so maximum refresh period is 7.8125 us
  297. // We have a 400 MHz DDR clock (2.5ns period) so max period is 3125 cycles
  298. // Our core now does 8 refreshes in a go, so we multiply this period by 8
  299. refresh = (64000 * mhz) / 8192; // Refresh period in clocks
  300. reg_tmp = *(volatile UINT *) C_DDR_CFG_REG; // read
  301. #ifdef BURST_REFRESH_ENABLE
  302. reg_tmp |= C_CFG_REFRESH_ENABLE | (refresh * 8);
  303. reg_tmp |= C_CFG_BURST_REFRESH_ENABLE;
  304. #else
  305. reg_tmp |= C_CFG_REFRESH_ENABLE | (refresh * 1);
  306. reg_tmp &= ~C_CFG_BURST_REFRESH_ENABLE;
  307. #endif
  308. *(volatile UINT *) C_DDR_CFG_REG = reg_tmp;
  309. //Verify register contents
  310. reg_tmp = *(volatile UINT *) C_DDR_REG_PHY2; // read
  311. //printf("Warning XXXXXXXXXXXXXXXXXXXXXX - get bad read data from C_DDR_PHY2_REG, though it looks OK on bus XXXXXXXXXXXXXXXXXX");
  312. //TBD Check_data (read_data, dll_reg, "Error: bad C_DDR_PHY2_REG read", tb_pass);
  313. reg_tmp = *(volatile UINT *) C_DDR_CFG_REG; // read
  314. //TBD Check_data (read_data, cfg_reg, "Error: bad DDR_CFG read", tb_pass);
  315. //disable optimised wrapping
  316. if (ddr_config_parameters->ddr_mode == 2) {
  317. reg_tmp = 0xFFFF0000;
  318. *(volatile UINT *) C_DDR_REG_IGNORE = reg_tmp;
  319. }
  320. //enable midbuffer followon
  321. reg_tmp = *(volatile UINT *) C_DDR_ARB_REG; // read
  322. reg_tmp = 0xFFFF0000 | reg_tmp;
  323. *(volatile UINT *) C_DDR_ARB_REG = reg_tmp;
  324. // Enable write behind coherency checking for all clients
  325. reg_tmp = 0xFFFF0000;
  326. *(volatile UINT *) C_DDR_AHB4_REG = reg_tmp;
  327. //Wait for 200 clock cycles for SDRAM DLL to lock...
  328. udelay(1); //1us
  329. }
  330. // Function used to Setup DDR core
  331. void ddr_setup(int mhz)
  332. {
  333. static const T_DDR_TIMING_PARAMETERS *ddr_timing_parameters =
  334. &C_TP_DDR2_25_CL6_1GB;
  335. static const T_DDR_CONFIG_PARAMETERS *ddr_config_parameters =
  336. &C_CP_DDR2_25_CL6;
  337. //Bring core out of Reset
  338. *(volatile UINT *) C_DDR_BLKEN_REG = C_BLKEN_DDR_ON;
  339. //DDR TIMING INITIALISTION
  340. ddr_timing_initialisation(ddr_timing_parameters);
  341. //DDR NORMAL INITIALISATION
  342. ddr_normal_initialisation(ddr_config_parameters, mhz);
  343. // route all writes through one client
  344. *(volatile UINT *) C_DDR_TRANSACTION_ROUTING = (0
  345. << DDR_ROUTE_CPU0_INSTR_SHIFT)
  346. | (1 << DDR_ROUTE_CPU0_RDDATA_SHIFT)
  347. | (3 << DDR_ROUTE_CPU0_WRDATA_SHIFT)
  348. | (2 << DDR_ROUTE_CPU1_INSTR_SHIFT)
  349. | (3 << DDR_ROUTE_CPU1_RDDATA_SHIFT)
  350. | (3 << DDR_ROUTE_CPU1_WRDATA_SHIFT);
  351. //Bring all clients out of reset
  352. *(volatile UINT *) C_DDR_BLKEN_REG = C_BLKEN_DDR_ON + 0x0000FFFF;
  353. }
  354. void set_ddr_timing(unsigned int w, unsigned int i)
  355. {
  356. unsigned int reg;
  357. unsigned int wnow = 16;
  358. unsigned int inow = 32;
  359. /* reset all timing controls to known value (31) */
  360. writel(DDR_PHY_TIMING_W_RST | DDR_PHY_TIMING_I_RST, DDR_PHY_TIMING);
  361. writel(DDR_PHY_TIMING_W_RST | DDR_PHY_TIMING_I_RST | DDR_PHY_TIMING_CK,
  362. DDR_PHY_TIMING);
  363. writel(DDR_PHY_TIMING_W_RST | DDR_PHY_TIMING_I_RST, DDR_PHY_TIMING);
  364. /* step up or down read delay to the requested value */
  365. while (wnow != w) {
  366. if (wnow < w) {
  367. reg = DDR_PHY_TIMING_INC;
  368. wnow++;
  369. } else {
  370. reg = 0;
  371. wnow--;
  372. }
  373. writel(DDR_PHY_TIMING_W_CE | reg, DDR_PHY_TIMING);
  374. writel(DDR_PHY_TIMING_CK | DDR_PHY_TIMING_W_CE | reg,
  375. DDR_PHY_TIMING);
  376. writel(DDR_PHY_TIMING_W_CE | reg, DDR_PHY_TIMING);
  377. }
  378. /* now write delay */
  379. while (inow != i) {
  380. if (inow < i) {
  381. reg = DDR_PHY_TIMING_INC;
  382. inow++;
  383. } else {
  384. reg = 0;
  385. inow--;
  386. }
  387. writel(DDR_PHY_TIMING_I_CE | reg, DDR_PHY_TIMING);
  388. writel(DDR_PHY_TIMING_CK | DDR_PHY_TIMING_I_CE | reg,
  389. DDR_PHY_TIMING);
  390. writel(DDR_PHY_TIMING_I_CE | reg, DDR_PHY_TIMING);
  391. }
  392. }
  393. //Function used to Setup SDRAM in DDR/SDR mode
  394. void init_ddr(int mhz)
  395. {
  396. /* start clocks */
  397. enable_clock(SYS_CTRL_CLK_DDRPHY);
  398. enable_clock(SYS_CTRL_CLK_DDR);
  399. enable_clock(SYS_CTRL_CLK_DDRCK);
  400. /* bring phy and core out of reset */
  401. reset_block(SYS_CTRL_RST_DDR_PHY, 0);
  402. reset_block(SYS_CTRL_RST_DDR, 0);
  403. /* DDR runs at half the speed of the CPU */
  404. ddr_setup(mhz >> 1);
  405. return;
  406. }