sdram_cfg.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /*
  2. * Copyright (C) 2009, 2010 Matthias Kaehlcke <matthias@kaehlcke.net>
  3. *
  4. * Copyright (C) 2006 Dominic Rath <Dominic.Rath@gmx.de>
  5. *
  6. * See file CREDITS for list of people who contributed to this
  7. * project.
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License as
  11. * published by the Free Software Foundation; either version 2 of
  12. * the License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  22. * MA 02111-1307 USA
  23. */
  24. #include <asm/io.h>
  25. #include "sdram_cfg.h"
  26. #include "early_udelay.h"
  27. #define PROGRAM_MODE_REG(bank) (*(volatile uint32_t *) \
  28. (SDRAM_BASE_ADDR | SDRAM_BANK_SEL_##bank | SDRAM_MODE_REG_VAL))
  29. #define PRECHARGE_BANK(bank) (*(volatile uint32_t *) \
  30. (SDRAM_BASE_ADDR | SDRAM_BANK_SEL_##bank)) = 0
  31. static void precharge_all_banks(void);
  32. static void setup_refresh_timer(void);
  33. static void program_mode_registers(void);
  34. void sdram_cfg(void)
  35. {
  36. struct sdram_regs *sdram = (struct sdram_regs *)SDRAM_BASE;
  37. writel(SDRAM_DEVCFG_VAL, &sdram->SDRAM_DEVCFG_REG);
  38. /* Issue continous NOP commands */
  39. writel(GLCONFIG_INIT | GLCONFIG_MRS | GLCONFIG_CKE, &sdram->glconfig);
  40. early_udelay(200);
  41. precharge_all_banks();
  42. setup_refresh_timer();
  43. program_mode_registers();
  44. /* Select normal operation mode */
  45. writel(GLCONFIG_CKE, &sdram->glconfig);
  46. }
  47. static void precharge_all_banks(void)
  48. {
  49. struct sdram_regs *sdram = (struct sdram_regs *)SDRAM_BASE;
  50. /* Issue PRECHARGE ALL commands */
  51. writel(GLCONFIG_INIT | GLCONFIG_CKE, &sdram->glconfig);
  52. /*
  53. * Errata of most EP93xx revisions say that PRECHARGE ALL isn't always
  54. * issued
  55. *
  56. * Cirrus proposes a workaround which consists in performing a read from
  57. * each bank to force the precharge. This causes some boards to hang.
  58. * Writing to the SDRAM banks instead of reading has the same
  59. * side-effect (the SDRAM controller issues the necessary precharges),
  60. * but is known to work on all supported boards
  61. */
  62. PRECHARGE_BANK(0);
  63. #if (CONFIG_NR_DRAM_BANKS >= 2)
  64. PRECHARGE_BANK(1);
  65. #endif
  66. #if (CONFIG_NR_DRAM_BANKS >= 3)
  67. PRECHARGE_BANK(2);
  68. #endif
  69. #if (CONFIG_NR_DRAM_BANKS == 4)
  70. PRECHARGE_BANK(3);
  71. #endif
  72. }
  73. static void setup_refresh_timer(void)
  74. {
  75. struct sdram_regs *sdram = (struct sdram_regs *)SDRAM_BASE;
  76. /* Load refresh timer with 10 to issue refresh every 10 cycles */
  77. writel(0x0a, &sdram->refrshtimr);
  78. /*
  79. * Wait at least 80 clock cycles to provide 8 refresh cycles
  80. * to all SDRAMs
  81. */
  82. early_udelay(1);
  83. /*
  84. * Program refresh timer with normal value
  85. * We need 8192 refresh cycles every 64ms
  86. * at 15ns (HCLK >= 66MHz) per cycle:
  87. * 64ms / 8192 = 7.8125us
  88. * 7.8125us / 15ns = 520 (0x208)
  89. */
  90. /*
  91. * TODO: redboot uses 0x1e0 for the slowest possible device
  92. * but i don't understand how this value is calculated
  93. */
  94. writel(0x208, &sdram->refrshtimr);
  95. }
  96. static void program_mode_registers(void)
  97. {
  98. struct sdram_regs *sdram = (struct sdram_regs *)SDRAM_BASE;
  99. /* Select mode register update mode */
  100. writel(GLCONFIG_MRS | GLCONFIG_CKE, &sdram->glconfig);
  101. /*
  102. * The mode registers are programmed by performing a read from each
  103. * SDRAM bank. The value of the address that is read defines the value
  104. * that is written into the mode register
  105. */
  106. PROGRAM_MODE_REG(0);
  107. #if (CONFIG_NR_DRAM_BANKS >= 2)
  108. PROGRAM_MODE_REG(1);
  109. #endif
  110. #if (CONFIG_NR_DRAM_BANKS >= 3)
  111. PROGRAM_MODE_REG(2);
  112. #endif
  113. #if (CONFIG_NR_DRAM_BANKS == 4)
  114. PROGRAM_MODE_REG(3);
  115. #endif
  116. }