sdram.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /*
  2. * (C) Copyright 2003-2004
  3. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  4. *
  5. * (C) Copyright 2004
  6. * Mark Jonas, Freescale Semiconductor, mark.jonas@freescale.com.
  7. *
  8. * See file CREDITS for list of people who contributed to this
  9. * project.
  10. *
  11. * This program is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU General Public License as
  13. * published by the Free Software Foundation; either version 2 of
  14. * the License, or (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  24. * MA 02111-1307 USA
  25. */
  26. #include <common.h>
  27. #include <mpc5xxx.h>
  28. #include "sdram.h"
  29. #ifndef CONFIG_SYS_RAMBOOT
  30. static void mpc5xxx_sdram_start (sdram_conf_t *sdram_conf, int hi_addr)
  31. {
  32. long hi_addr_bit = hi_addr ? 0x01000000 : 0;
  33. /* unlock mode register */
  34. *(vu_long *)MPC5XXX_SDRAM_CTRL = sdram_conf->control | 0x80000000 | hi_addr_bit;
  35. __asm__ volatile ("sync");
  36. /* precharge all banks */
  37. *(vu_long *)MPC5XXX_SDRAM_CTRL = sdram_conf->control | 0x80000002 | hi_addr_bit;
  38. __asm__ volatile ("sync");
  39. if (sdram_conf->ddr) {
  40. /* set mode register: extended mode */
  41. *(vu_long *)MPC5XXX_SDRAM_MODE = sdram_conf->emode;
  42. __asm__ volatile ("sync");
  43. /* set mode register: reset DLL */
  44. *(vu_long *)MPC5XXX_SDRAM_MODE = sdram_conf->mode | 0x04000000;
  45. __asm__ volatile ("sync");
  46. }
  47. /* precharge all banks */
  48. *(vu_long *)MPC5XXX_SDRAM_CTRL = sdram_conf->control | 0x80000002 | hi_addr_bit;
  49. __asm__ volatile ("sync");
  50. /* auto refresh */
  51. *(vu_long *)MPC5XXX_SDRAM_CTRL = sdram_conf->control | 0x80000004 | hi_addr_bit;
  52. __asm__ volatile ("sync");
  53. /* set mode register */
  54. *(vu_long *)MPC5XXX_SDRAM_MODE = sdram_conf->mode;
  55. __asm__ volatile ("sync");
  56. /* normal operation */
  57. *(vu_long *)MPC5XXX_SDRAM_CTRL = sdram_conf->control | hi_addr_bit;
  58. __asm__ volatile ("sync");
  59. }
  60. #endif
  61. /*
  62. * ATTENTION: Although partially referenced initdram does NOT make real use
  63. * use of CONFIG_SYS_SDRAM_BASE. The code does not work if CONFIG_SYS_SDRAM_BASE
  64. * is something else than 0x00000000.
  65. */
  66. long int mpc5xxx_sdram_init (sdram_conf_t *sdram_conf)
  67. {
  68. ulong dramsize = 0;
  69. ulong dramsize2 = 0;
  70. #ifndef CONFIG_SYS_RAMBOOT
  71. ulong test1, test2;
  72. /* setup SDRAM chip selects */
  73. *(vu_long *)MPC5XXX_SDRAM_CS0CFG = 0x0000001e;/* 2G at 0x0 */
  74. *(vu_long *)MPC5XXX_SDRAM_CS1CFG = 0x80000000;/* disabled */
  75. __asm__ volatile ("sync");
  76. /* setup config registers */
  77. *(vu_long *)MPC5XXX_SDRAM_CONFIG1 = sdram_conf->config1;
  78. *(vu_long *)MPC5XXX_SDRAM_CONFIG2 = sdram_conf->config2;
  79. __asm__ volatile ("sync");
  80. if (sdram_conf->ddr) {
  81. /* set tap delay */
  82. *(vu_long *)MPC5XXX_CDM_PORCFG = sdram_conf->tapdelay;
  83. __asm__ volatile ("sync");
  84. }
  85. /* find RAM size using SDRAM CS0 only */
  86. mpc5xxx_sdram_start(sdram_conf, 0);
  87. test1 = get_ram_size((long *)CONFIG_SYS_SDRAM_BASE, 0x80000000);
  88. mpc5xxx_sdram_start(sdram_conf, 1);
  89. test2 = get_ram_size((long *)CONFIG_SYS_SDRAM_BASE, 0x80000000);
  90. if (test1 > test2) {
  91. mpc5xxx_sdram_start(sdram_conf, 0);
  92. dramsize = test1;
  93. } else {
  94. dramsize = test2;
  95. }
  96. /* memory smaller than 1MB is impossible */
  97. if (dramsize < (1 << 20)) {
  98. dramsize = 0;
  99. }
  100. /* set SDRAM CS0 size according to the amount of RAM found */
  101. if (dramsize > 0) {
  102. *(vu_long *)MPC5XXX_SDRAM_CS0CFG = 0x13 + __builtin_ffs(dramsize >> 20) - 1;
  103. } else {
  104. *(vu_long *)MPC5XXX_SDRAM_CS0CFG = 0; /* disabled */
  105. }
  106. /* let SDRAM CS1 start right after CS0 */
  107. *(vu_long *)MPC5XXX_SDRAM_CS1CFG = dramsize + 0x0000001e;/* 2G */
  108. /* find RAM size using SDRAM CS1 only */
  109. mpc5xxx_sdram_start(sdram_conf, 0);
  110. test1 = get_ram_size((long *)(CONFIG_SYS_SDRAM_BASE + dramsize), 0x80000000);
  111. mpc5xxx_sdram_start(sdram_conf, 1);
  112. test2 = get_ram_size((long *)(CONFIG_SYS_SDRAM_BASE + dramsize), 0x80000000);
  113. if (test1 > test2) {
  114. mpc5xxx_sdram_start(sdram_conf, 0);
  115. dramsize2 = test1;
  116. } else {
  117. dramsize2 = test2;
  118. }
  119. /* memory smaller than 1MB is impossible */
  120. if (dramsize2 < (1 << 20)) {
  121. dramsize2 = 0;
  122. }
  123. /* set SDRAM CS1 size according to the amount of RAM found */
  124. if (dramsize2 > 0) {
  125. *(vu_long *)MPC5XXX_SDRAM_CS1CFG = dramsize
  126. | (0x13 + __builtin_ffs(dramsize2 >> 20) - 1);
  127. } else {
  128. *(vu_long *)MPC5XXX_SDRAM_CS1CFG = dramsize; /* disabled */
  129. }
  130. #else /* CONFIG_SYS_RAMBOOT */
  131. /* retrieve size of memory connected to SDRAM CS0 */
  132. dramsize = *(vu_long *)MPC5XXX_SDRAM_CS0CFG & 0xFF;
  133. if (dramsize >= 0x13) {
  134. dramsize = (1 << (dramsize - 0x13)) << 20;
  135. } else {
  136. dramsize = 0;
  137. }
  138. /* retrieve size of memory connected to SDRAM CS1 */
  139. dramsize2 = *(vu_long *)MPC5XXX_SDRAM_CS1CFG & 0xFF;
  140. if (dramsize2 >= 0x13) {
  141. dramsize2 = (1 << (dramsize2 - 0x13)) << 20;
  142. } else {
  143. dramsize2 = 0;
  144. }
  145. #endif /* CONFIG_SYS_RAMBOOT */
  146. return dramsize + dramsize2;
  147. }