ddr.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. /*
  2. * DDR Configuration for AM33xx devices.
  3. *
  4. * Copyright (C) 2011 Texas Instruments Incorporated - http://www.ti.com/
  5. *
  6. * SPDX-License-Identifier: GPL-2.0+
  7. */
  8. #include <asm/arch/cpu.h>
  9. #include <asm/arch/ddr_defs.h>
  10. #include <asm/arch/sys_proto.h>
  11. #include <asm/io.h>
  12. #include <asm/emif.h>
  13. /**
  14. * Base address for EMIF instances
  15. */
  16. static struct emif_reg_struct *emif_reg[2] = {
  17. (struct emif_reg_struct *)EMIF4_0_CFG_BASE,
  18. (struct emif_reg_struct *)EMIF4_1_CFG_BASE};
  19. /**
  20. * Base addresses for DDR PHY cmd/data regs
  21. */
  22. static struct ddr_cmd_regs *ddr_cmd_reg[2] = {
  23. (struct ddr_cmd_regs *)DDR_PHY_CMD_ADDR,
  24. (struct ddr_cmd_regs *)DDR_PHY_CMD_ADDR2};
  25. static struct ddr_data_regs *ddr_data_reg[2] = {
  26. (struct ddr_data_regs *)DDR_PHY_DATA_ADDR,
  27. (struct ddr_data_regs *)DDR_PHY_DATA_ADDR2};
  28. /**
  29. * Base address for ddr io control instances
  30. */
  31. static struct ddr_cmdtctrl *ioctrl_reg = {
  32. (struct ddr_cmdtctrl *)DDR_CONTROL_BASE_ADDR};
  33. static inline u32 get_mr(int nr, u32 cs, u32 mr_addr)
  34. {
  35. u32 mr;
  36. mr_addr |= cs << EMIF_REG_CS_SHIFT;
  37. writel(mr_addr, &emif_reg[nr]->emif_lpddr2_mode_reg_cfg);
  38. mr = readl(&emif_reg[nr]->emif_lpddr2_mode_reg_data);
  39. debug("get_mr: EMIF1 cs %d mr %08x val 0x%x\n", cs, mr_addr, mr);
  40. if (((mr & 0x0000ff00) >> 8) == (mr & 0xff) &&
  41. ((mr & 0x00ff0000) >> 16) == (mr & 0xff) &&
  42. ((mr & 0xff000000) >> 24) == (mr & 0xff))
  43. return mr & 0xff;
  44. else
  45. return mr;
  46. }
  47. static inline void set_mr(int nr, u32 cs, u32 mr_addr, u32 mr_val)
  48. {
  49. mr_addr |= cs << EMIF_REG_CS_SHIFT;
  50. writel(mr_addr, &emif_reg[nr]->emif_lpddr2_mode_reg_cfg);
  51. writel(mr_val, &emif_reg[nr]->emif_lpddr2_mode_reg_data);
  52. }
  53. static void configure_mr(int nr, u32 cs)
  54. {
  55. u32 mr_addr;
  56. while (get_mr(nr, cs, LPDDR2_MR0) & LPDDR2_MR0_DAI_MASK)
  57. ;
  58. set_mr(nr, cs, LPDDR2_MR10, 0x56);
  59. set_mr(nr, cs, LPDDR2_MR1, 0x43);
  60. set_mr(nr, cs, LPDDR2_MR2, 0x2);
  61. mr_addr = LPDDR2_MR2 | EMIF_REG_REFRESH_EN_MASK;
  62. set_mr(nr, cs, mr_addr, 0x2);
  63. }
  64. /*
  65. * Configure EMIF4D5 registers and MR registers For details about these magic
  66. * values please see the EMIF registers section of the TRM.
  67. */
  68. void config_sdram_emif4d5(const struct emif_regs *regs, int nr)
  69. {
  70. writel(0xA0, &emif_reg[nr]->emif_pwr_mgmt_ctrl);
  71. writel(0xA0, &emif_reg[nr]->emif_pwr_mgmt_ctrl_shdw);
  72. writel(regs->zq_config, &emif_reg[nr]->emif_zq_config);
  73. writel(regs->temp_alert_config, &emif_reg[nr]->emif_temp_alert_config);
  74. writel(regs->emif_rd_wr_lvl_rmp_win,
  75. &emif_reg[nr]->emif_rd_wr_lvl_rmp_win);
  76. writel(regs->emif_rd_wr_lvl_rmp_ctl,
  77. &emif_reg[nr]->emif_rd_wr_lvl_rmp_ctl);
  78. writel(regs->emif_rd_wr_lvl_ctl, &emif_reg[nr]->emif_rd_wr_lvl_ctl);
  79. writel(regs->emif_rd_wr_exec_thresh,
  80. &emif_reg[nr]->emif_rd_wr_exec_thresh);
  81. /*
  82. * for most SOCs these registers won't need to be changed so only
  83. * write to these registers if someone explicitly has set the
  84. * register's value.
  85. */
  86. if(regs->emif_cos_config) {
  87. writel(regs->emif_prio_class_serv_map, &emif_reg[nr]->emif_prio_class_serv_map);
  88. writel(regs->emif_connect_id_serv_1_map, &emif_reg[nr]->emif_connect_id_serv_1_map);
  89. writel(regs->emif_connect_id_serv_2_map, &emif_reg[nr]->emif_connect_id_serv_2_map);
  90. writel(regs->emif_cos_config, &emif_reg[nr]->emif_cos_config);
  91. }
  92. /*
  93. * Sequence to ensure that the PHY is in a known state prior to
  94. * startting hardware leveling. Also acts as to latch some state from
  95. * the EMIF into the PHY.
  96. */
  97. writel(0x2011, &emif_reg[nr]->emif_iodft_tlgc);
  98. writel(0x2411, &emif_reg[nr]->emif_iodft_tlgc);
  99. writel(0x2011, &emif_reg[nr]->emif_iodft_tlgc);
  100. clrbits_le32(&emif_reg[nr]->emif_sdram_ref_ctrl,
  101. EMIF_REG_INITREF_DIS_MASK);
  102. writel(regs->sdram_config, &emif_reg[nr]->emif_sdram_config);
  103. writel(regs->sdram_config, &cstat->secure_emif_sdram_config);
  104. /* Wait 1ms because of L3 timeout error */
  105. udelay(1000);
  106. writel(regs->ref_ctrl, &emif_reg[nr]->emif_sdram_ref_ctrl);
  107. writel(regs->ref_ctrl, &emif_reg[nr]->emif_sdram_ref_ctrl_shdw);
  108. /* Perform hardware leveling for DDR3 */
  109. if (emif_sdram_type(regs->sdram_config) == EMIF_SDRAM_TYPE_DDR3) {
  110. writel(readl(&emif_reg[nr]->emif_ddr_ext_phy_ctrl_36) |
  111. 0x100, &emif_reg[nr]->emif_ddr_ext_phy_ctrl_36);
  112. writel(readl(&emif_reg[nr]->emif_ddr_ext_phy_ctrl_36_shdw) |
  113. 0x100, &emif_reg[nr]->emif_ddr_ext_phy_ctrl_36_shdw);
  114. writel(0x80000000, &emif_reg[nr]->emif_rd_wr_lvl_rmp_ctl);
  115. /* Enable read leveling */
  116. writel(0x80000000, &emif_reg[nr]->emif_rd_wr_lvl_ctl);
  117. /*
  118. * Enable full read and write leveling. Wait for read and write
  119. * leveling bit to clear RDWRLVLFULL_START bit 31
  120. */
  121. while ((readl(&emif_reg[nr]->emif_rd_wr_lvl_ctl) & 0x80000000)
  122. != 0)
  123. ;
  124. /* Check the timeout register to see if leveling is complete */
  125. if ((readl(&emif_reg[nr]->emif_status) & 0x70) != 0)
  126. puts("DDR3 H/W leveling incomplete with errors\n");
  127. } else {
  128. /* DDR2 */
  129. configure_mr(nr, 0);
  130. configure_mr(nr, 1);
  131. }
  132. }
  133. /**
  134. * Configure SDRAM
  135. */
  136. void config_sdram(const struct emif_regs *regs, int nr)
  137. {
  138. if (regs->zq_config) {
  139. writel(regs->zq_config, &emif_reg[nr]->emif_zq_config);
  140. writel(regs->sdram_config, &cstat->secure_emif_sdram_config);
  141. writel(regs->sdram_config, &emif_reg[nr]->emif_sdram_config);
  142. /* Trigger initialization */
  143. writel(0x00003100, &emif_reg[nr]->emif_sdram_ref_ctrl);
  144. /* Wait 1ms because of L3 timeout error */
  145. udelay(1000);
  146. /* Write proper sdram_ref_cref_ctrl value */
  147. writel(regs->ref_ctrl, &emif_reg[nr]->emif_sdram_ref_ctrl);
  148. writel(regs->ref_ctrl, &emif_reg[nr]->emif_sdram_ref_ctrl_shdw);
  149. }
  150. writel(regs->ref_ctrl, &emif_reg[nr]->emif_sdram_ref_ctrl);
  151. writel(regs->ref_ctrl, &emif_reg[nr]->emif_sdram_ref_ctrl_shdw);
  152. writel(regs->sdram_config, &emif_reg[nr]->emif_sdram_config);
  153. /* Write REG_COS_COUNT_1, REG_COS_COUNT_2, and REG_PR_OLD_COUNT. */
  154. if (regs->ocp_config)
  155. writel(regs->ocp_config, &emif_reg[nr]->emif_l3_config);
  156. }
  157. /**
  158. * Set SDRAM timings
  159. */
  160. void set_sdram_timings(const struct emif_regs *regs, int nr)
  161. {
  162. writel(regs->sdram_tim1, &emif_reg[nr]->emif_sdram_tim_1);
  163. writel(regs->sdram_tim1, &emif_reg[nr]->emif_sdram_tim_1_shdw);
  164. writel(regs->sdram_tim2, &emif_reg[nr]->emif_sdram_tim_2);
  165. writel(regs->sdram_tim2, &emif_reg[nr]->emif_sdram_tim_2_shdw);
  166. writel(regs->sdram_tim3, &emif_reg[nr]->emif_sdram_tim_3);
  167. writel(regs->sdram_tim3, &emif_reg[nr]->emif_sdram_tim_3_shdw);
  168. }
  169. /*
  170. * Configure EXT PHY registers for software leveling
  171. */
  172. static void ext_phy_settings_swlvl(const struct emif_regs *regs, int nr)
  173. {
  174. u32 *ext_phy_ctrl_base = 0;
  175. u32 *emif_ext_phy_ctrl_base = 0;
  176. __maybe_unused const u32 *ext_phy_ctrl_const_regs;
  177. u32 i = 0;
  178. __maybe_unused u32 size;
  179. ext_phy_ctrl_base = (u32 *)&(regs->emif_ddr_ext_phy_ctrl_1);
  180. emif_ext_phy_ctrl_base =
  181. (u32 *)&(emif_reg[nr]->emif_ddr_ext_phy_ctrl_1);
  182. /* Configure external phy control timing registers */
  183. for (i = 0; i < EMIF_EXT_PHY_CTRL_TIMING_REG; i++) {
  184. writel(*ext_phy_ctrl_base, emif_ext_phy_ctrl_base++);
  185. /* Update shadow registers */
  186. writel(*ext_phy_ctrl_base++, emif_ext_phy_ctrl_base++);
  187. }
  188. #ifdef CONFIG_AM43XX
  189. /*
  190. * External phy 6-24 registers do not change with ddr frequency.
  191. * These only need to be set on DDR2 on AM43xx.
  192. */
  193. emif_get_ext_phy_ctrl_const_regs(&ext_phy_ctrl_const_regs, &size);
  194. if (!size)
  195. return;
  196. for (i = 0; i < size; i++) {
  197. writel(ext_phy_ctrl_const_regs[i], emif_ext_phy_ctrl_base++);
  198. /* Update shadow registers */
  199. writel(ext_phy_ctrl_const_regs[i], emif_ext_phy_ctrl_base++);
  200. }
  201. #endif
  202. }
  203. /*
  204. * Configure EXT PHY registers for hardware leveling
  205. */
  206. static void ext_phy_settings_hwlvl(const struct emif_regs *regs, int nr)
  207. {
  208. /*
  209. * Enable hardware leveling on the EMIF. For details about these
  210. * magic values please see the EMIF registers section of the TRM.
  211. */
  212. writel(0x08020080, &emif_reg[nr]->emif_ddr_ext_phy_ctrl_1);
  213. writel(0x08020080, &emif_reg[nr]->emif_ddr_ext_phy_ctrl_1_shdw);
  214. writel(0x00000000, &emif_reg[nr]->emif_ddr_ext_phy_ctrl_22);
  215. writel(0x00000000, &emif_reg[nr]->emif_ddr_ext_phy_ctrl_22_shdw);
  216. writel(0x00600020, &emif_reg[nr]->emif_ddr_ext_phy_ctrl_23);
  217. writel(0x00600020, &emif_reg[nr]->emif_ddr_ext_phy_ctrl_23_shdw);
  218. writel(0x40010080, &emif_reg[nr]->emif_ddr_ext_phy_ctrl_24);
  219. writel(0x40010080, &emif_reg[nr]->emif_ddr_ext_phy_ctrl_24_shdw);
  220. writel(0x08102040, &emif_reg[nr]->emif_ddr_ext_phy_ctrl_25);
  221. writel(0x08102040, &emif_reg[nr]->emif_ddr_ext_phy_ctrl_25_shdw);
  222. writel(0x00200020, &emif_reg[nr]->emif_ddr_ext_phy_ctrl_26);
  223. writel(0x00200020, &emif_reg[nr]->emif_ddr_ext_phy_ctrl_26_shdw);
  224. writel(0x00200020, &emif_reg[nr]->emif_ddr_ext_phy_ctrl_27);
  225. writel(0x00200020, &emif_reg[nr]->emif_ddr_ext_phy_ctrl_27_shdw);
  226. writel(0x00200020, &emif_reg[nr]->emif_ddr_ext_phy_ctrl_28);
  227. writel(0x00200020, &emif_reg[nr]->emif_ddr_ext_phy_ctrl_28_shdw);
  228. writel(0x00200020, &emif_reg[nr]->emif_ddr_ext_phy_ctrl_29);
  229. writel(0x00200020, &emif_reg[nr]->emif_ddr_ext_phy_ctrl_29_shdw);
  230. writel(0x00200020, &emif_reg[nr]->emif_ddr_ext_phy_ctrl_30);
  231. writel(0x00200020, &emif_reg[nr]->emif_ddr_ext_phy_ctrl_30_shdw);
  232. writel(0x00000000, &emif_reg[nr]->emif_ddr_ext_phy_ctrl_31);
  233. writel(0x00000000, &emif_reg[nr]->emif_ddr_ext_phy_ctrl_31_shdw);
  234. writel(0x00000000, &emif_reg[nr]->emif_ddr_ext_phy_ctrl_32);
  235. writel(0x00000000, &emif_reg[nr]->emif_ddr_ext_phy_ctrl_32_shdw);
  236. writel(0x00000000, &emif_reg[nr]->emif_ddr_ext_phy_ctrl_33);
  237. writel(0x00000000, &emif_reg[nr]->emif_ddr_ext_phy_ctrl_33_shdw);
  238. writel(0x00000000, &emif_reg[nr]->emif_ddr_ext_phy_ctrl_34);
  239. writel(0x00000000, &emif_reg[nr]->emif_ddr_ext_phy_ctrl_34_shdw);
  240. writel(0x00000000, &emif_reg[nr]->emif_ddr_ext_phy_ctrl_35);
  241. writel(0x00000000, &emif_reg[nr]->emif_ddr_ext_phy_ctrl_35_shdw);
  242. writel(0x000000FF, &emif_reg[nr]->emif_ddr_ext_phy_ctrl_36);
  243. writel(0x000000FF, &emif_reg[nr]->emif_ddr_ext_phy_ctrl_36_shdw);
  244. /*
  245. * Sequence to ensure that the PHY is again in a known state after
  246. * hardware leveling.
  247. */
  248. writel(0x2011, &emif_reg[nr]->emif_iodft_tlgc);
  249. writel(0x2411, &emif_reg[nr]->emif_iodft_tlgc);
  250. writel(0x2011, &emif_reg[nr]->emif_iodft_tlgc);
  251. }
  252. /**
  253. * Configure DDR PHY
  254. */
  255. void config_ddr_phy(const struct emif_regs *regs, int nr)
  256. {
  257. /*
  258. * Disable initialization and refreshes for now until we finish
  259. * programming EMIF regs and set time between rising edge of
  260. * DDR_RESET to rising edge of DDR_CKE to > 500us per memory spec.
  261. * We currently hardcode a value based on a max expected frequency
  262. * of 400MHz.
  263. */
  264. writel(EMIF_REG_INITREF_DIS_MASK | 0x3100,
  265. &emif_reg[nr]->emif_sdram_ref_ctrl);
  266. writel(regs->emif_ddr_phy_ctlr_1,
  267. &emif_reg[nr]->emif_ddr_phy_ctrl_1);
  268. writel(regs->emif_ddr_phy_ctlr_1,
  269. &emif_reg[nr]->emif_ddr_phy_ctrl_1_shdw);
  270. if (get_emif_rev((u32)emif_reg[nr]) == EMIF_4D5) {
  271. if (emif_sdram_type(regs->sdram_config) == EMIF_SDRAM_TYPE_DDR3)
  272. ext_phy_settings_hwlvl(regs, nr);
  273. else
  274. ext_phy_settings_swlvl(regs, nr);
  275. }
  276. }
  277. /**
  278. * Configure DDR CMD control registers
  279. */
  280. void config_cmd_ctrl(const struct cmd_control *cmd, int nr)
  281. {
  282. if (!cmd)
  283. return;
  284. writel(cmd->cmd0csratio, &ddr_cmd_reg[nr]->cm0csratio);
  285. writel(cmd->cmd0iclkout, &ddr_cmd_reg[nr]->cm0iclkout);
  286. writel(cmd->cmd1csratio, &ddr_cmd_reg[nr]->cm1csratio);
  287. writel(cmd->cmd1iclkout, &ddr_cmd_reg[nr]->cm1iclkout);
  288. writel(cmd->cmd2csratio, &ddr_cmd_reg[nr]->cm2csratio);
  289. writel(cmd->cmd2iclkout, &ddr_cmd_reg[nr]->cm2iclkout);
  290. }
  291. /**
  292. * Configure DDR DATA registers
  293. */
  294. void config_ddr_data(const struct ddr_data *data, int nr)
  295. {
  296. int i;
  297. if (!data)
  298. return;
  299. for (i = 0; i < DDR_DATA_REGS_NR; i++) {
  300. writel(data->datardsratio0,
  301. &(ddr_data_reg[nr]+i)->dt0rdsratio0);
  302. writel(data->datawdsratio0,
  303. &(ddr_data_reg[nr]+i)->dt0wdsratio0);
  304. writel(data->datawiratio0,
  305. &(ddr_data_reg[nr]+i)->dt0wiratio0);
  306. writel(data->datagiratio0,
  307. &(ddr_data_reg[nr]+i)->dt0giratio0);
  308. writel(data->datafwsratio0,
  309. &(ddr_data_reg[nr]+i)->dt0fwsratio0);
  310. writel(data->datawrsratio0,
  311. &(ddr_data_reg[nr]+i)->dt0wrsratio0);
  312. }
  313. }
  314. void config_io_ctrl(const struct ctrl_ioregs *ioregs)
  315. {
  316. if (!ioregs)
  317. return;
  318. writel(ioregs->cm0ioctl, &ioctrl_reg->cm0ioctl);
  319. writel(ioregs->cm1ioctl, &ioctrl_reg->cm1ioctl);
  320. writel(ioregs->cm2ioctl, &ioctrl_reg->cm2ioctl);
  321. writel(ioregs->dt0ioctl, &ioctrl_reg->dt0ioctl);
  322. writel(ioregs->dt1ioctl, &ioctrl_reg->dt1ioctl);
  323. #ifdef CONFIG_AM43XX
  324. writel(ioregs->dt2ioctrl, &ioctrl_reg->dt2ioctrl);
  325. writel(ioregs->dt3ioctrl, &ioctrl_reg->dt3ioctrl);
  326. writel(ioregs->emif_sdram_config_ext,
  327. &ioctrl_reg->emif_sdram_config_ext);
  328. #endif
  329. }