mpddrc.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /*
  2. * Copyright (C) 2013 Atmel Corporation
  3. * Bo Shen <voice.shen@atmel.com>
  4. *
  5. * Copyright (C) 2015 Atmel Corporation
  6. * Wenyou Yang <wenyou.yang@atmel.com>
  7. *
  8. * SPDX-License-Identifier: GPL-2.0+
  9. */
  10. #include <common.h>
  11. #include <asm/io.h>
  12. #include <asm/arch/atmel_mpddrc.h>
  13. #define SAMA5D3_MPDDRC_VERSION 0x140
  14. static inline void atmel_mpddr_op(const struct atmel_mpddr *mpddr,
  15. int mode,
  16. u32 ram_address)
  17. {
  18. writel(mode, &mpddr->mr);
  19. writel(0, ram_address);
  20. }
  21. static int ddr2_decodtype_is_seq(const unsigned int base, u32 cr)
  22. {
  23. struct atmel_mpddr *mpddr = (struct atmel_mpddr *)base;
  24. u16 version = readl(&mpddr->version) & 0xffff;
  25. if ((version >= SAMA5D3_MPDDRC_VERSION) &&
  26. (cr & ATMEL_MPDDRC_CR_DECOD_INTERLEAVED))
  27. return 0;
  28. return 1;
  29. }
  30. int ddr2_init(const unsigned int base,
  31. const unsigned int ram_address,
  32. const struct atmel_mpddrc_config *mpddr_value)
  33. {
  34. const struct atmel_mpddr *mpddr = (struct atmel_mpddr *)base;
  35. u32 ba_off, cr;
  36. /* Compute bank offset according to NC in configuration register */
  37. ba_off = (mpddr_value->cr & ATMEL_MPDDRC_CR_NC_MASK) + 9;
  38. if (ddr2_decodtype_is_seq(base, mpddr_value->cr))
  39. ba_off += ((mpddr_value->cr & ATMEL_MPDDRC_CR_NR_MASK) >> 2) + 11;
  40. ba_off += (mpddr_value->md & ATMEL_MPDDRC_MD_DBW_MASK) ? 1 : 2;
  41. /* Program the memory device type into the memory device register */
  42. writel(mpddr_value->md, &mpddr->md);
  43. /* Program the configuration register */
  44. writel(mpddr_value->cr, &mpddr->cr);
  45. /* Program the timing register */
  46. writel(mpddr_value->tpr0, &mpddr->tpr0);
  47. writel(mpddr_value->tpr1, &mpddr->tpr1);
  48. writel(mpddr_value->tpr2, &mpddr->tpr2);
  49. /* Issue a NOP command */
  50. atmel_mpddr_op(mpddr, ATMEL_MPDDRC_MR_MODE_NOP_CMD, ram_address);
  51. /* A 200 us is provided to precede any signal toggle */
  52. udelay(200);
  53. /* Issue a NOP command */
  54. atmel_mpddr_op(mpddr, ATMEL_MPDDRC_MR_MODE_NOP_CMD, ram_address);
  55. /* Issue an all banks precharge command */
  56. atmel_mpddr_op(mpddr, ATMEL_MPDDRC_MR_MODE_PRCGALL_CMD, ram_address);
  57. /* Issue an extended mode register set(EMRS2) to choose operation */
  58. atmel_mpddr_op(mpddr, ATMEL_MPDDRC_MR_MODE_EXT_LMR_CMD,
  59. ram_address + (0x2 << ba_off));
  60. /* Issue an extended mode register set(EMRS3) to set EMSR to 0 */
  61. atmel_mpddr_op(mpddr, ATMEL_MPDDRC_MR_MODE_EXT_LMR_CMD,
  62. ram_address + (0x3 << ba_off));
  63. /*
  64. * Issue an extended mode register set(EMRS1) to enable DLL and
  65. * program D.I.C (output driver impedance control)
  66. */
  67. atmel_mpddr_op(mpddr, ATMEL_MPDDRC_MR_MODE_EXT_LMR_CMD,
  68. ram_address + (0x1 << ba_off));
  69. /* Enable DLL reset */
  70. cr = readl(&mpddr->cr);
  71. writel(cr | ATMEL_MPDDRC_CR_DLL_RESET_ENABLED, &mpddr->cr);
  72. /* A mode register set(MRS) cycle is issued to reset DLL */
  73. atmel_mpddr_op(mpddr, ATMEL_MPDDRC_MR_MODE_LMR_CMD, ram_address);
  74. /* Issue an all banks precharge command */
  75. atmel_mpddr_op(mpddr, ATMEL_MPDDRC_MR_MODE_PRCGALL_CMD, ram_address);
  76. /* Two auto-refresh (CBR) cycles are provided */
  77. atmel_mpddr_op(mpddr, ATMEL_MPDDRC_MR_MODE_RFSH_CMD, ram_address);
  78. atmel_mpddr_op(mpddr, ATMEL_MPDDRC_MR_MODE_RFSH_CMD, ram_address);
  79. /* Disable DLL reset */
  80. cr = readl(&mpddr->cr);
  81. writel(cr & (~ATMEL_MPDDRC_CR_DLL_RESET_ENABLED), &mpddr->cr);
  82. /* A mode register set (MRS) cycle is issued to disable DLL reset */
  83. atmel_mpddr_op(mpddr, ATMEL_MPDDRC_MR_MODE_LMR_CMD, ram_address);
  84. /* Set OCD calibration in default state */
  85. cr = readl(&mpddr->cr);
  86. writel(cr | ATMEL_MPDDRC_CR_OCD_DEFAULT, &mpddr->cr);
  87. /*
  88. * An extended mode register set (EMRS1) cycle is issued
  89. * to OCD default value
  90. */
  91. atmel_mpddr_op(mpddr, ATMEL_MPDDRC_MR_MODE_EXT_LMR_CMD,
  92. ram_address + (0x1 << ba_off));
  93. /* OCD calibration mode exit */
  94. cr = readl(&mpddr->cr);
  95. writel(cr & (~ATMEL_MPDDRC_CR_OCD_DEFAULT), &mpddr->cr);
  96. /*
  97. * An extended mode register set (EMRS1) cycle is issued
  98. * to enable OCD exit
  99. */
  100. atmel_mpddr_op(mpddr, ATMEL_MPDDRC_MR_MODE_EXT_LMR_CMD,
  101. ram_address + (0x1 << ba_off));
  102. /* A nornal mode command is provided */
  103. atmel_mpddr_op(mpddr, ATMEL_MPDDRC_MR_MODE_NORMAL_CMD, ram_address);
  104. /* Perform a write access to any DDR2-SDRAM address */
  105. writel(0, ram_address);
  106. /* Write the refresh rate */
  107. writel(mpddr_value->rtr, &mpddr->rtr);
  108. return 0;
  109. }
  110. int ddr3_init(const unsigned int base,
  111. const unsigned int ram_address,
  112. const struct atmel_mpddrc_config *mpddr_value)
  113. {
  114. struct atmel_mpddr *mpddr = (struct atmel_mpddr *)base;
  115. u32 ba_off;
  116. /* Compute bank offset according to NC in configuration register */
  117. ba_off = (mpddr_value->cr & ATMEL_MPDDRC_CR_NC_MASK) + 9;
  118. if (ddr2_decodtype_is_seq(base, mpddr_value->cr))
  119. ba_off += ((mpddr_value->cr &
  120. ATMEL_MPDDRC_CR_NR_MASK) >> 2) + 11;
  121. ba_off += (mpddr_value->md & ATMEL_MPDDRC_MD_DBW_MASK) ? 1 : 2;
  122. /* Program the memory device type */
  123. writel(mpddr_value->md, &mpddr->md);
  124. /*
  125. * Program features of the DDR3-SDRAM device and timing parameters
  126. */
  127. writel(mpddr_value->cr, &mpddr->cr);
  128. writel(mpddr_value->tpr0, &mpddr->tpr0);
  129. writel(mpddr_value->tpr1, &mpddr->tpr1);
  130. writel(mpddr_value->tpr2, &mpddr->tpr2);
  131. /* A NOP command is issued to the DDR3-SRAM */
  132. atmel_mpddr_op(mpddr, ATMEL_MPDDRC_MR_MODE_NOP_CMD, ram_address);
  133. /* A pause of at least 500us must be observed before a single toggle. */
  134. udelay(500);
  135. /* A NOP command is issued to the DDR3-SDRAM */
  136. atmel_mpddr_op(mpddr, ATMEL_MPDDRC_MR_MODE_NOP_CMD, ram_address);
  137. /*
  138. * An Extended Mode Register Set (EMRS2) cycle is issued to choose
  139. * between commercial or high temperature operations.
  140. */
  141. atmel_mpddr_op(mpddr, ATMEL_MPDDRC_MR_MODE_EXT_LMR_CMD,
  142. ram_address + (0x2 << ba_off));
  143. /*
  144. * Step 7: An Extended Mode Register Set (EMRS3) cycle is issued to set
  145. * the Extended Mode Register to 0.
  146. */
  147. atmel_mpddr_op(mpddr, ATMEL_MPDDRC_MR_MODE_EXT_LMR_CMD,
  148. ram_address + (0x3 << ba_off));
  149. /*
  150. * An Extended Mode Register Set (EMRS1) cycle is issued to disable and
  151. * to program O.D.S. (Output Driver Strength).
  152. */
  153. atmel_mpddr_op(mpddr, ATMEL_MPDDRC_MR_MODE_EXT_LMR_CMD,
  154. ram_address + (0x1 << ba_off));
  155. /*
  156. * Write a one to the DLL bit (enable DLL reset) in the MPDDRC
  157. * Configuration Register.
  158. */
  159. /* A Mode Register Set (MRS) cycle is issued to reset DLL. */
  160. atmel_mpddr_op(mpddr, ATMEL_MPDDRC_MR_MODE_LMR_CMD, ram_address);
  161. udelay(50);
  162. /*
  163. * A Calibration command (MRS) is issued to calibrate RTT and RON
  164. * values for the Process Voltage Temperature (PVT).
  165. */
  166. atmel_mpddr_op(mpddr, ATMEL_MPDDRC_MR_MODE_DEEP_CMD, ram_address);
  167. /* A Normal Mode command is provided. */
  168. atmel_mpddr_op(mpddr, ATMEL_MPDDRC_MR_MODE_NORMAL_CMD, ram_address);
  169. /* Perform a write access to any DDR3-SDRAM address. */
  170. writel(0, ram_address);
  171. /*
  172. * Write the refresh rate into the COUNT field in the MPDDRC
  173. * Refresh Timer Register (MPDDRC_RTR):
  174. */
  175. writel(mpddr_value->rtr, &mpddr->rtr);
  176. return 0;
  177. }