mx35_sdram.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /*
  2. * Copyright (C) 2012, Stefano Babic <sbabic@denx.de>
  3. *
  4. * See file CREDITS for list of people who contributed to this
  5. * project.
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License as
  9. * published by the Free Software Foundation; either version 2 of
  10. * the License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  20. * MA 02111-1307 USA
  21. */
  22. #include <asm/io.h>
  23. #include <asm/errno.h>
  24. #include <asm/arch/imx-regs.h>
  25. #include <linux/types.h>
  26. #include <asm/arch/sys_proto.h>
  27. #define ESDCTL_DDR2_EMR2 0x04000000
  28. #define ESDCTL_DDR2_EMR3 0x06000000
  29. #define ESDCTL_PRECHARGE 0x00000400
  30. #define ESDCTL_DDR2_EN_DLL 0x02000400
  31. #define ESDCTL_DDR2_RESET_DLL 0x00000333
  32. #define ESDCTL_DDR2_MR 0x00000233
  33. #define ESDCTL_DDR2_OCD_DEFAULT 0x02000780
  34. enum {
  35. SMODE_NORMAL = 0,
  36. SMODE_PRECHARGE,
  37. SMODE_AUTO_REFRESH,
  38. SMODE_LOAD_REG,
  39. SMODE_MANUAL_REFRESH
  40. };
  41. #define set_mode(x, en, m) (x | (en << 31) | (m << 28))
  42. static inline void dram_wait(unsigned int count)
  43. {
  44. volatile unsigned int wait = count;
  45. while (wait--)
  46. ;
  47. }
  48. void mx3_setup_sdram_bank(u32 start_address, u32 ddr2_config,
  49. u32 row, u32 col, u32 dsize, u32 refresh)
  50. {
  51. struct esdc_regs *esdc = (struct esdc_regs *)ESDCTL_BASE_ADDR;
  52. u32 *cfg_reg, *ctl_reg;
  53. u32 val;
  54. u32 ctlval;
  55. switch (start_address) {
  56. case CSD0_BASE_ADDR:
  57. cfg_reg = &esdc->esdcfg0;
  58. ctl_reg = &esdc->esdctl0;
  59. break;
  60. case CSD1_BASE_ADDR:
  61. cfg_reg = &esdc->esdcfg1;
  62. ctl_reg = &esdc->esdctl1;
  63. break;
  64. default:
  65. return;
  66. }
  67. /* The MX35 supports 11 up to 14 rows */
  68. if (row < 11 || row > 14 || col < 8 || col > 10)
  69. return;
  70. ctlval = (row - 11) << 24 | (col - 8) << 20 | (dsize << 16);
  71. /* Initialize MISC register for DDR2 */
  72. val = ESDC_MISC_RST | ESDC_MISC_MDDR_EN | ESDC_MISC_MDDR_DL_RST |
  73. ESDC_MISC_DDR_EN | ESDC_MISC_DDR2_EN;
  74. writel(val, &esdc->esdmisc);
  75. val &= ~(ESDC_MISC_RST | ESDC_MISC_MDDR_DL_RST);
  76. writel(val, &esdc->esdmisc);
  77. /*
  78. * according to DDR2 specs, wait a while before
  79. * the PRECHARGE_ALL command
  80. */
  81. dram_wait(0x20000);
  82. /* Load DDR2 config and timing */
  83. writel(ddr2_config, cfg_reg);
  84. /* Precharge ALL */
  85. writel(set_mode(ctlval, 1, SMODE_PRECHARGE),
  86. ctl_reg);
  87. writel(0xda, start_address + ESDCTL_PRECHARGE);
  88. /* Load mode */
  89. writel(set_mode(ctlval, 1, SMODE_LOAD_REG),
  90. ctl_reg);
  91. writeb(0xda, start_address + ESDCTL_DDR2_EMR2); /* EMRS2 */
  92. writeb(0xda, start_address + ESDCTL_DDR2_EMR3); /* EMRS3 */
  93. writeb(0xda, start_address + ESDCTL_DDR2_EN_DLL); /* Enable DLL */
  94. writeb(0xda, start_address + ESDCTL_DDR2_RESET_DLL); /* Reset DLL */
  95. /* Precharge ALL */
  96. writel(set_mode(ctlval, 1, SMODE_PRECHARGE),
  97. ctl_reg);
  98. writel(0xda, start_address + ESDCTL_PRECHARGE);
  99. /* Set mode auto refresh : at least two refresh are required */
  100. writel(set_mode(ctlval, 1, SMODE_AUTO_REFRESH),
  101. ctl_reg);
  102. writel(0xda, start_address);
  103. writel(0xda, start_address);
  104. writel(set_mode(ctlval, 1, SMODE_LOAD_REG),
  105. ctl_reg);
  106. writeb(0xda, start_address + ESDCTL_DDR2_MR);
  107. writeb(0xda, start_address + ESDCTL_DDR2_OCD_DEFAULT);
  108. /* OCD mode exit */
  109. writeb(0xda, start_address + ESDCTL_DDR2_EN_DLL); /* Enable DLL */
  110. /* Set normal mode */
  111. writel(set_mode(ctlval, 1, SMODE_NORMAL) | refresh,
  112. ctl_reg);
  113. dram_wait(0x20000);
  114. /* Do not set delay lines, only for MDDR */
  115. }